[tests] test_http_range.c

add some tests for http_range.c; more needed
This commit is contained in:
Glenn Strauss 2023-07-29 01:48:24 -04:00
parent 75c3d35698
commit 6b06de447d
5 changed files with 97 additions and 0 deletions

View File

@ -1005,6 +1005,7 @@ add_executable(test_common
t/test_burl.c
t/test_http_header.c
t/test_http_kv.c
t/test_http_range.c
t/test_keyvalue.c
t/test_request.c
log.c

View File

@ -506,6 +506,7 @@ t_test_common_SOURCES = t/test_common.c \
t/test_burl.c \
t/test_http_header.c \
t/test_http_kv.c \
t/test_http_range.c \
t/test_keyvalue.c \
t/test_request.c \
log.c \

View File

@ -700,6 +700,7 @@ test('test_common', executable('test_common',
't/test_burl.c',
't/test_http_header.c',
't/test_http_kv.c',
't/test_http_range.c',
't/test_keyvalue.c',
't/test_request.c',
'log.c',

View File

@ -9,6 +9,7 @@ void test_buffer (void);
void test_burl (void);
void test_http_header (void);
void test_http_kv (void);
void test_http_range (void);
void test_keyvalue (void);
void test_request (void);
@ -19,6 +20,7 @@ int main(void) {
test_burl();
test_http_header();
test_http_kv();
test_http_range();
test_keyvalue();
test_request();

92
src/t/test_http_range.c Normal file
View File

@ -0,0 +1,92 @@
#include "first.h"
#undef NDEBUG
#include <assert.h>
#include <stdio.h>
#include <stdlib.h>
/* stub functions to avoid pulling in chunk.c and fdevent.c */
#include "chunk.h"
#define chunkqueue_append_mem(cq, mem_len) do { } while (0)
#define chunkqueue_append_cq_range(dst, src, offset, len) do { } while (0)
#define chunkqueue_steal(dest, src, len) do { } while (0)
#define chunkqueue_mark_written(cq, len) do { } while (0)
#define chunkqueue_reset(cq) do { } while (0)
void chunkqueue_append_mem_min (chunkqueue * restrict cq, const char * restrict mem, size_t len) {
UNUSED(cq);
UNUSED(mem);
UNUSED(len);
}
#include "http_range.c"
static void test_http_range_parse (void) {
const char *http_range;
int n;
off_t content_length;
off_t ranges[RMAX*2];
http_range = "bytes=0-0";
content_length = 1;
n = http_range_parse(http_range+sizeof("bytes=")-1, content_length, ranges);
assert(2 == n);
http_range = "bytes=0-1";
content_length = 2;
n = http_range_parse(http_range+sizeof("bytes=")-1, content_length, ranges);
assert(2 == n);
/* consolidate ranges: sorted */
http_range = "bytes=0-1,1-2,2-3,3-4,4-5,5-6,6-7,7-8,8-9,9-10,10-11,11-12";
content_length = 1000;
n = http_range_parse(http_range+sizeof("bytes=")-1, content_length, ranges);
assert(2 == n && ranges[0] == 0 && ranges[1] == 12);
/* consolidate ranges: unsorted */
http_range = "bytes=1-2,2-3,3-4,4-5,5-6,6-7,7-8,8-9,0-1";
content_length = 1000;
n = http_range_parse(http_range+sizeof("bytes=")-1, content_length, ranges);
assert(2 == n && ranges[0] == 0 && ranges[1] == 9);
/*(test constructions below based on 10 unsorted limit)*/
assert(RMAX_UNSORTED == 10);
/* unsorted ranges up to RMAX_UNSORTED num ranges, consolidated */
http_range = "bytes=9-10,8-9,7-8,6-7,5-6,4-5,3-4,2-3,1-2,0-1";
content_length = 1000;
n = http_range_parse(http_range+sizeof("bytes=")-1, content_length, ranges);
assert(2 == n && ranges[0] == 0 && ranges[1] == 10);
/* unsorted range ignored after RMAX_UNSORTED num ranges, consolidated */
http_range = "bytes=10-11,9-10,8-9,7-8,6-7,5-6,4-5,3-4,2-3,1-2,0-1";
content_length = 1000;
n = http_range_parse(http_range+sizeof("bytes=")-1, content_length, ranges);
assert(2 == n && ranges[0] == 1 && ranges[1] == 11);
/* sorted ranges processed above RMAX_UNSORTED (up to RMAX) */
http_range = "bytes=0-1,100-100,200-200,300-300,400-400,500-500,600-600,700-700,800-800,900-900,1000-1000,1100-1100";
content_length = 10000;
n = http_range_parse(http_range+sizeof("bytes=")-1, content_length, ranges);
assert(24 == n && ranges[0] == 0 && ranges[22] == 1100);
/* unsorted range ignored after RMAX_UNSORTED num ranges */
http_range = "bytes=100-100,200-200,300-300,400-400,500-500,600-600,700-700,800-800,900-900,1000-1000,1100-1100,0-100";
content_length = 10000;
n = http_range_parse(http_range+sizeof("bytes=")-1, content_length, ranges);
assert(22 == n && ranges[0] == 100 && ranges[20] == 1100);
/* unsorted range after RMAX_UNSORTED, but prior sorted, consolidated */
http_range = "bytes=1-2,2-3,3-4,4-5,5-6,6-7,7-8,8-9,9-10,10-11,11-12,0-1";
content_length = 1000;
n = http_range_parse(http_range+sizeof("bytes=")-1, content_length, ranges);
assert(2 == n && ranges[0] == 0 && ranges[1] == 12);
/* TODO (more) */
}
void test_http_range (void);
void test_http_range (void)
{
test_http_range_parse();
/* TODO (more) */
}