Add unit test for parse_content_range() method

* http.c (test_parse_range_header): New function to test the
    function for parsing the HTTP/1.1 Content-Range header.
    * test.[ch]: Same
    * http.c (parse_content_range): Fix parsing code. Fail on scenarios
    mentioned in rfc 7233.
This commit is contained in:
Darshit Shah 2015-08-29 23:08:39 +05:30
parent c809398e8c
commit b06fca60ac
3 changed files with 54 additions and 1 deletions

View File

@ -909,9 +909,13 @@ parse_content_range (const char *hdr, wgint *first_byte_ptr,
++hdr;
for (num = 0; c_isdigit (*hdr); hdr++)
num = 10 * num + (*hdr - '0');
if (*hdr != '/' || !c_isdigit (*(hdr + 1)))
if (*hdr != '/')
return false;
*last_byte_ptr = num;
if (!(c_isdigit (*(hdr + 1)) || *(hdr + 1) == '*'))
return false;
if (*last_byte_ptr < *first_byte_ptr)
return false;
++hdr;
if (*hdr == '*')
num = -1;
@ -919,6 +923,8 @@ parse_content_range (const char *hdr, wgint *first_byte_ptr,
for (num = 0; c_isdigit (*hdr); hdr++)
num = 10 * num + (*hdr - '0');
*entity_length_ptr = num;
if ((*entity_length_ptr <= *last_byte_ptr) && *entity_length_ptr != -1)
return false;
return true;
}
@ -4897,6 +4903,51 @@ ensure_extension (struct http_stat *hs, const char *ext, int *dt)
}
#ifdef TESTING
const char *
test_parse_range_header(void)
{
static const struct {
const char * rangehdr;
const wgint firstbyte;
const wgint lastbyte;
const wgint length;
const bool shouldPass;
} test_array[] = {
{ "bytes 0-1000/1000", 0, 1000, 1000, false },
{ "bytes 0-999/1000", 0, 999, 1000, true },
{ "bytes 100-99/1000", 100, 99, 1000, false },
{ "bytes 100-100/1000", 100, 100, 1000, true },
{ "bytes 0-1000/100000000", 0, 1000, 100000000, true },
{ "bytes 1-999/1000", 1, 999, 1000, true },
{ "bytes 42-1233/1234", 42, 1233, 1234, true },
{ "bytes 42-1233/*", 42, 1233, -1, true },
{ "bytes 0-2147483648/2147483649", 0, 2147483648, 2147483649, true },
{ "bytes 2147483648-4294967296/4294967297", 2147483648, 4294967296, 4294967297, true }
};
wgint firstbyteptr[sizeof(wgint)];
wgint lastbyteptr[sizeof(wgint)];
wgint lengthptr[sizeof(wgint)];
bool result;
for (unsigned i = 0; i < countof (test_array); i++)
{
result = parse_content_range (test_array[i].rangehdr, firstbyteptr, lastbyteptr, lengthptr);
#if 0
printf ("%ld %ld\n", test_array[i].firstbyte, *firstbyteptr);
printf ("%ld %ld\n", test_array[i].lastbyte, *lastbyteptr);
printf ("%ld %ld\n", test_array[i].length, *lengthptr);
printf ("\n");
#endif
mu_assert ("test_parse_range_header: False Negative", result == test_array[i].shouldPass);
mu_assert ("test_parse_range_header: Bad parse", test_array[i].firstbyte == *firstbyteptr &&
test_array[i].lastbyte == *lastbyteptr &&
test_array[i].length == *lengthptr);
}
return NULL;
}
const char *
test_parse_content_disposition(void)
{

View File

@ -54,6 +54,7 @@ all_tests(void)
mu_run_test (test_has_key);
#endif
mu_run_test (test_parse_content_disposition);
mu_run_test (test_parse_range_header);
mu_run_test (test_subdir_p);
mu_run_test (test_dir_matches_p);
mu_run_test (test_commands_sorted);

View File

@ -48,6 +48,7 @@ const char *test_has_key (void);
const char *test_find_key_value (void);
const char *test_find_key_values (void);
const char *test_parse_content_disposition(void);
const char *test_parse_range_header(void);
const char *test_commands_sorted(void);
const char *test_cmd_spec_restrict_file_names(void);
const char *test_is_robots_txt_url(void);