Commit a2a9f153 authored by Tatsuhiro Tsujikawa's avatar Tatsuhiro Tsujikawa

tests: Use nghttp2_mem instead of raw malloc()/free()

Fixes GH-170
parent 291c27c9
...@@ -433,7 +433,7 @@ void test_nghttp2_frame(void) { ...@@ -433,7 +433,7 @@ void test_nghttp2_frame(void) {
static int deflate_inflate(nghttp2_hd_deflater *deflater, static int deflate_inflate(nghttp2_hd_deflater *deflater,
nghttp2_hd_inflater *inflater, nghttp2_bufs *bufs, nghttp2_hd_inflater *inflater, nghttp2_bufs *bufs,
nghttp2_nv *nva, size_t nvlen) { nghttp2_nv *nva, size_t nvlen, nghttp2_mem *mem) {
int rv; int rv;
rv = nghttp2_hd_deflate_hd_bufs(deflater, bufs, nva, nvlen); rv = nghttp2_hd_deflate_hd_bufs(deflater, bufs, nva, nvlen);
...@@ -442,7 +442,7 @@ static int deflate_inflate(nghttp2_hd_deflater *deflater, ...@@ -442,7 +442,7 @@ static int deflate_inflate(nghttp2_hd_deflater *deflater,
return rv; return rv;
} }
rv = (int)inflate_hd(inflater, NULL, bufs, 0); rv = (int)inflate_hd(inflater, NULL, bufs, 0, mem);
if (rv < 0) { if (rv < 0) {
return rv; return rv;
...@@ -484,13 +484,15 @@ static void run_nghttp2_hd(void) { ...@@ -484,13 +484,15 @@ static void run_nghttp2_hd(void) {
goto inflate_init_fail; goto inflate_init_fail;
} }
rv = deflate_inflate(&deflater, &inflater, &bufs, nva1, ARRLEN(nva1)); rv = deflate_inflate(&deflater, &inflater, &bufs, nva1, ARRLEN(nva1),
nghttp2_mem_fm());
if (rv != 0) { if (rv != 0) {
goto deflate_hd_fail; goto deflate_hd_fail;
} }
rv = deflate_inflate(&deflater, &inflater, &bufs, nva2, ARRLEN(nva2)); rv = deflate_inflate(&deflater, &inflater, &bufs, nva2, ARRLEN(nva2),
nghttp2_mem_fm());
if (rv != 0) { if (rv != 0) {
goto deflate_hd_fail; goto deflate_hd_fail;
......
...@@ -201,7 +201,7 @@ void test_nghttp2_bufs_remove(void) { ...@@ -201,7 +201,7 @@ void test_nghttp2_bufs_remove(void) {
CU_ASSERT(0 == nghttp2_bufs_len(&bufs)); CU_ASSERT(0 == nghttp2_bufs_len(&bufs));
CU_ASSERT(bufs.cur->buf.pos == bufs.cur->buf.begin); CU_ASSERT(bufs.cur->buf.pos == bufs.cur->buf.begin);
free(out); mem->free(out, NULL);
nghttp2_bufs_free(&bufs); nghttp2_bufs_free(&bufs);
} }
......
...@@ -47,8 +47,8 @@ static nghttp2_nv make_nv(const char *name, const char *value) { ...@@ -47,8 +47,8 @@ static nghttp2_nv make_nv(const char *name, const char *value) {
#define HEADERS_LENGTH 7 #define HEADERS_LENGTH 7
static nghttp2_nv *headers(void) { static nghttp2_nv *headers(nghttp2_mem *mem) {
nghttp2_nv *nva = malloc(sizeof(nghttp2_nv) * HEADERS_LENGTH); nghttp2_nv *nva = mem->malloc(sizeof(nghttp2_nv) * HEADERS_LENGTH, NULL);
nva[0] = make_nv("method", "GET"); nva[0] = make_nv("method", "GET");
nva[1] = make_nv("scheme", "https"); nva[1] = make_nv("scheme", "https");
nva[2] = make_nv("url", "/"); nva[2] = make_nv("url", "/");
...@@ -88,7 +88,7 @@ void test_nghttp2_frame_pack_headers() { ...@@ -88,7 +88,7 @@ void test_nghttp2_frame_pack_headers() {
nghttp2_hd_deflate_init(&deflater, mem); nghttp2_hd_deflate_init(&deflater, mem);
nghttp2_hd_inflate_init(&inflater, mem); nghttp2_hd_inflate_init(&inflater, mem);
nva = headers(); nva = headers(mem);
nvlen = HEADERS_LENGTH; nvlen = HEADERS_LENGTH;
nghttp2_priority_spec_default_init(&pri_spec); nghttp2_priority_spec_default_init(&pri_spec);
...@@ -113,14 +113,14 @@ void test_nghttp2_frame_pack_headers() { ...@@ -113,14 +113,14 @@ void test_nghttp2_frame_pack_headers() {
hdblocklen = nghttp2_bufs_len(&bufs) - NGHTTP2_FRAME_HDLEN; hdblocklen = nghttp2_bufs_len(&bufs) - NGHTTP2_FRAME_HDLEN;
CU_ASSERT(hdblocklen == CU_ASSERT(hdblocklen ==
inflate_hd(&inflater, &out, &bufs, NGHTTP2_FRAME_HDLEN)); inflate_hd(&inflater, &out, &bufs, NGHTTP2_FRAME_HDLEN, mem));
CU_ASSERT(7 == out.nvlen); CU_ASSERT(7 == out.nvlen);
CU_ASSERT(nvnameeq("method", &out.nva[0])); CU_ASSERT(nvnameeq("method", &out.nva[0]));
CU_ASSERT(nvvalueeq("GET", &out.nva[0])); CU_ASSERT(nvvalueeq("GET", &out.nva[0]));
nghttp2_frame_headers_free(&oframe, mem); nghttp2_frame_headers_free(&oframe, mem);
nva_out_reset(&out); nva_out_reset(&out, mem);
nghttp2_bufs_reset(&bufs); nghttp2_bufs_reset(&bufs);
memset(&oframe, 0, sizeof(oframe)); memset(&oframe, 0, sizeof(oframe));
...@@ -149,13 +149,14 @@ void test_nghttp2_frame_pack_headers() { ...@@ -149,13 +149,14 @@ void test_nghttp2_frame_pack_headers() {
CU_ASSERT(hdblocklen == CU_ASSERT(hdblocklen ==
inflate_hd(&inflater, &out, &bufs, inflate_hd(&inflater, &out, &bufs,
NGHTTP2_FRAME_HDLEN + NGHTTP2_FRAME_HDLEN +
nghttp2_frame_priority_len(oframe.hd.flags))); nghttp2_frame_priority_len(oframe.hd.flags),
mem));
nghttp2_nv_array_sort(out.nva, out.nvlen); nghttp2_nv_array_sort(out.nva, out.nvlen);
CU_ASSERT(nvnameeq("method", &out.nva[0])); CU_ASSERT(nvnameeq("method", &out.nva[0]));
nghttp2_frame_headers_free(&oframe, mem); nghttp2_frame_headers_free(&oframe, mem);
nva_out_reset(&out); nva_out_reset(&out, mem);
nghttp2_bufs_reset(&bufs); nghttp2_bufs_reset(&bufs);
nghttp2_bufs_free(&bufs); nghttp2_bufs_free(&bufs);
...@@ -181,7 +182,7 @@ void test_nghttp2_frame_pack_headers_frame_too_large(void) { ...@@ -181,7 +182,7 @@ void test_nghttp2_frame_pack_headers_frame_too_large(void) {
for (i = 0; i < big_hdslen; ++i) { for (i = 0; i < big_hdslen; ++i) {
big_hds[i].name = (uint8_t *)"header"; big_hds[i].name = (uint8_t *)"header";
big_hds[i].value = malloc(big_vallen + 1); big_hds[i].value = mem->malloc(big_vallen + 1, NULL);
memset(big_hds[i].value, '0' + (int)i, big_vallen); memset(big_hds[i].value, '0' + (int)i, big_vallen);
big_hds[i].value[big_vallen] = '\0'; big_hds[i].value[big_vallen] = '\0';
big_hds[i].namelen = strlen((char *)big_hds[i].name); big_hds[i].namelen = strlen((char *)big_hds[i].name);
...@@ -200,7 +201,7 @@ void test_nghttp2_frame_pack_headers_frame_too_large(void) { ...@@ -200,7 +201,7 @@ void test_nghttp2_frame_pack_headers_frame_too_large(void) {
nghttp2_frame_headers_free(&frame, mem); nghttp2_frame_headers_free(&frame, mem);
nghttp2_bufs_free(&bufs); nghttp2_bufs_free(&bufs);
for (i = 0; i < big_hdslen; ++i) { for (i = 0; i < big_hdslen; ++i) {
free(big_hds[i].value); mem->free(big_hds[i].value, NULL);
} }
nghttp2_hd_deflate_free(&deflater); nghttp2_hd_deflate_free(&deflater);
} }
...@@ -329,7 +330,7 @@ void test_nghttp2_frame_pack_push_promise() { ...@@ -329,7 +330,7 @@ void test_nghttp2_frame_pack_push_promise() {
nghttp2_hd_deflate_init(&deflater, mem); nghttp2_hd_deflate_init(&deflater, mem);
nghttp2_hd_inflate_init(&inflater, mem); nghttp2_hd_inflate_init(&inflater, mem);
nva = headers(); nva = headers(mem);
nvlen = HEADERS_LENGTH; nvlen = HEADERS_LENGTH;
nghttp2_frame_push_promise_init(&frame, NGHTTP2_FLAG_END_HEADERS, 1000000007, nghttp2_frame_push_promise_init(&frame, NGHTTP2_FLAG_END_HEADERS, 1000000007,
(1U << 31) - 1, nva, nvlen); (1U << 31) - 1, nva, nvlen);
...@@ -346,13 +347,13 @@ void test_nghttp2_frame_pack_push_promise() { ...@@ -346,13 +347,13 @@ void test_nghttp2_frame_pack_push_promise() {
hdblocklen = nghttp2_bufs_len(&bufs) - NGHTTP2_FRAME_HDLEN - 4; hdblocklen = nghttp2_bufs_len(&bufs) - NGHTTP2_FRAME_HDLEN - 4;
CU_ASSERT(hdblocklen == CU_ASSERT(hdblocklen ==
inflate_hd(&inflater, &out, &bufs, NGHTTP2_FRAME_HDLEN + 4)); inflate_hd(&inflater, &out, &bufs, NGHTTP2_FRAME_HDLEN + 4, mem));
CU_ASSERT(7 == out.nvlen); CU_ASSERT(7 == out.nvlen);
CU_ASSERT(nvnameeq("method", &out.nva[0])); CU_ASSERT(nvnameeq("method", &out.nva[0]));
CU_ASSERT(nvvalueeq("GET", &out.nva[0])); CU_ASSERT(nvvalueeq("GET", &out.nva[0]));
nva_out_reset(&out); nva_out_reset(&out, mem);
nghttp2_bufs_free(&bufs); nghttp2_bufs_free(&bufs);
nghttp2_frame_push_promise_free(&oframe, mem); nghttp2_frame_push_promise_free(&oframe, mem);
nghttp2_frame_push_promise_free(&frame, mem); nghttp2_frame_push_promise_free(&frame, mem);
...@@ -387,13 +388,14 @@ void test_nghttp2_frame_pack_goaway() { ...@@ -387,13 +388,14 @@ void test_nghttp2_frame_pack_goaway() {
nghttp2_goaway frame, oframe; nghttp2_goaway frame, oframe;
nghttp2_bufs bufs; nghttp2_bufs bufs;
size_t opaque_data_len = 16; size_t opaque_data_len = 16;
uint8_t *opaque_data = malloc(opaque_data_len); uint8_t *opaque_data;
int rv; int rv;
nghttp2_mem *mem; nghttp2_mem *mem;
mem = nghttp2_mem_default(); mem = nghttp2_mem_default();
frame_pack_bufs_init(&bufs); frame_pack_bufs_init(&bufs);
opaque_data = mem->malloc(opaque_data_len, NULL);
memcpy(opaque_data, "0123456789abcdef", opaque_data_len); memcpy(opaque_data, "0123456789abcdef", opaque_data_len);
nghttp2_frame_goaway_init(&frame, 1000000007, NGHTTP2_PROTOCOL_ERROR, nghttp2_frame_goaway_init(&frame, 1000000007, NGHTTP2_PROTOCOL_ERROR,
opaque_data, opaque_data_len); opaque_data, opaque_data_len);
...@@ -465,7 +467,7 @@ void test_nghttp2_nv_array_copy(void) { ...@@ -465,7 +467,7 @@ void test_nghttp2_nv_array_copy(void) {
bignv.name = (uint8_t *)"echo"; bignv.name = (uint8_t *)"echo";
bignv.namelen = strlen("echo"); bignv.namelen = strlen("echo");
bignv.valuelen = (1 << 14) - 1; bignv.valuelen = (1 << 14) - 1;
bignv.value = malloc(bignv.valuelen); bignv.value = mem->malloc(bignv.valuelen, NULL);
memset(bignv.value, '0', bignv.valuelen); memset(bignv.value, '0', bignv.valuelen);
rv = nghttp2_nv_array_copy(&nva, NULL, 0, mem); rv = nghttp2_nv_array_copy(&nva, NULL, 0, mem);
...@@ -500,7 +502,7 @@ void test_nghttp2_nv_array_copy(void) { ...@@ -500,7 +502,7 @@ void test_nghttp2_nv_array_copy(void) {
nghttp2_nv_array_del(nva, mem); nghttp2_nv_array_del(nva, mem);
free(bignv.value); mem->free(bignv.value, NULL);
} }
void test_nghttp2_iv_check(void) { void test_nghttp2_iv_check(void) {
......
This diff is collapsed.
...@@ -145,15 +145,19 @@ void test_nghttp2_map_functional(void) { ...@@ -145,15 +145,19 @@ void test_nghttp2_map_functional(void) {
nghttp2_map_free(&map); nghttp2_map_free(&map);
} }
static int entry_free(nghttp2_map_entry *entry, void *ptr _U_) { static int entry_free(nghttp2_map_entry *entry, void *ptr) {
free(entry); nghttp2_mem *mem = ptr;
mem->free(entry, NULL);
return 0; return 0;
} }
void test_nghttp2_map_each_free(void) { void test_nghttp2_map_each_free(void) {
strentry *foo = malloc(sizeof(strentry)), *bar = malloc(sizeof(strentry)), nghttp2_mem *mem = nghttp2_mem_default();
*baz = malloc(sizeof(strentry)), strentry *foo = mem->malloc(sizeof(strentry), NULL),
*shrubbery = malloc(sizeof(strentry)); *bar = mem->malloc(sizeof(strentry), NULL),
*baz = mem->malloc(sizeof(strentry), NULL),
*shrubbery = mem->malloc(sizeof(strentry), NULL);
nghttp2_map map; nghttp2_map map;
nghttp2_map_init(&map, nghttp2_mem_default()); nghttp2_map_init(&map, nghttp2_mem_default());
...@@ -167,6 +171,6 @@ void test_nghttp2_map_each_free(void) { ...@@ -167,6 +171,6 @@ void test_nghttp2_map_each_free(void) {
nghttp2_map_insert(&map, &baz->map_entry); nghttp2_map_insert(&map, &baz->map_entry);
nghttp2_map_insert(&map, &shrubbery->map_entry); nghttp2_map_insert(&map, &shrubbery->map_entry);
nghttp2_map_each_free(&map, entry_free, NULL); nghttp2_map_each_free(&map, entry_free, mem);
nghttp2_map_free(&map); nghttp2_map_free(&map);
} }
This diff is collapsed.
...@@ -114,26 +114,26 @@ void nva_out_init(nva_out *out) { ...@@ -114,26 +114,26 @@ void nva_out_init(nva_out *out) {
out->nvlen = 0; out->nvlen = 0;
} }
void nva_out_reset(nva_out *out) { void nva_out_reset(nva_out *out, nghttp2_mem *mem) {
size_t i; size_t i;
for (i = 0; i < out->nvlen; ++i) { for (i = 0; i < out->nvlen; ++i) {
free(out->nva[i].name); mem->free(out->nva[i].name, NULL);
free(out->nva[i].value); mem->free(out->nva[i].value, NULL);
} }
memset(out->nva, 0, sizeof(out->nva)); memset(out->nva, 0, sizeof(out->nva));
out->nvlen = 0; out->nvlen = 0;
} }
void add_out(nva_out *out, nghttp2_nv *nv) { void add_out(nva_out *out, nghttp2_nv *nv, nghttp2_mem *mem) {
nghttp2_nv *onv = &out->nva[out->nvlen]; nghttp2_nv *onv = &out->nva[out->nvlen];
if (nv->namelen) { if (nv->namelen) {
onv->name = malloc(nv->namelen); onv->name = mem->malloc(nv->namelen, NULL);
memcpy(onv->name, nv->name, nv->namelen); memcpy(onv->name, nv->name, nv->namelen);
} else { } else {
onv->name = NULL; onv->name = NULL;
} }
if (nv->valuelen) { if (nv->valuelen) {
onv->value = malloc(nv->valuelen); onv->value = mem->malloc(nv->valuelen, NULL);
memcpy(onv->value, nv->value, nv->valuelen); memcpy(onv->value, nv->value, nv->valuelen);
} else { } else {
onv->value = NULL; onv->value = NULL;
...@@ -147,7 +147,7 @@ void add_out(nva_out *out, nghttp2_nv *nv) { ...@@ -147,7 +147,7 @@ void add_out(nva_out *out, nghttp2_nv *nv) {
} }
ssize_t inflate_hd(nghttp2_hd_inflater *inflater, nva_out *out, ssize_t inflate_hd(nghttp2_hd_inflater *inflater, nva_out *out,
nghttp2_bufs *bufs, size_t offset) { nghttp2_bufs *bufs, size_t offset, nghttp2_mem *mem) {
ssize_t rv; ssize_t rv;
nghttp2_nv nv; nghttp2_nv nv;
int inflate_flags; int inflate_flags;
...@@ -186,7 +186,7 @@ ssize_t inflate_hd(nghttp2_hd_inflater *inflater, nva_out *out, ...@@ -186,7 +186,7 @@ ssize_t inflate_hd(nghttp2_hd_inflater *inflater, nva_out *out,
if (inflate_flags & NGHTTP2_HD_INFLATE_EMIT) { if (inflate_flags & NGHTTP2_HD_INFLATE_EMIT) {
if (out) { if (out) {
add_out(out, &nv); add_out(out, &nv, mem);
} }
} }
if (inflate_flags & NGHTTP2_HD_INFLATE_FINAL) { if (inflate_flags & NGHTTP2_HD_INFLATE_FINAL) {
...@@ -293,10 +293,10 @@ nghttp2_stream *open_stream_with_dep_excl(nghttp2_session *session, ...@@ -293,10 +293,10 @@ nghttp2_stream *open_stream_with_dep_excl(nghttp2_session *session,
dep_stream); dep_stream);
} }
nghttp2_outbound_item *create_data_ob_item(void) { nghttp2_outbound_item *create_data_ob_item(nghttp2_mem *mem) {
nghttp2_outbound_item *item; nghttp2_outbound_item *item;
item = malloc(sizeof(nghttp2_outbound_item)); item = mem->malloc(sizeof(nghttp2_outbound_item), NULL);
memset(item, 0, sizeof(nghttp2_outbound_item)); memset(item, 0, sizeof(nghttp2_outbound_item));
return item; return item;
......
...@@ -40,12 +40,12 @@ ...@@ -40,12 +40,12 @@
} }
#define ARRLEN(ARR) (sizeof(ARR) / sizeof(ARR[0])) #define ARRLEN(ARR) (sizeof(ARR) / sizeof(ARR[0]))
#define assert_nv_equal(A, B, len) \ #define assert_nv_equal(A, B, len, mem) \
do { \ do { \
size_t alloclen = sizeof(nghttp2_nv) * len; \ size_t alloclen = sizeof(nghttp2_nv) * len; \
const nghttp2_nv *sa = A, *sb = B; \ const nghttp2_nv *sa = A, *sb = B; \
nghttp2_nv *a = malloc(alloclen); \ nghttp2_nv *a = mem->malloc(alloclen, NULL); \
nghttp2_nv *b = malloc(alloclen); \ nghttp2_nv *b = mem->malloc(alloclen, NULL); \
ssize_t i_; \ ssize_t i_; \
memcpy(a, sa, alloclen); \ memcpy(a, sa, alloclen); \
memcpy(b, sb, alloclen); \ memcpy(b, sb, alloclen); \
...@@ -54,8 +54,8 @@ ...@@ -54,8 +54,8 @@
for (i_ = 0; i_ < (ssize_t)len; ++i_) { \ for (i_ = 0; i_ < (ssize_t)len; ++i_) { \
CU_ASSERT(nghttp2_nv_equal(&a[i_], &b[i_])); \ CU_ASSERT(nghttp2_nv_equal(&a[i_], &b[i_])); \
} \ } \
free(b); \ mem->free(b, NULL); \
free(a); \ mem->free(a, NULL); \
} while (0); } while (0);
int unpack_framebuf(nghttp2_frame *frame, nghttp2_bufs *bufs); int unpack_framebuf(nghttp2_frame *frame, nghttp2_bufs *bufs);
...@@ -74,12 +74,12 @@ typedef struct { ...@@ -74,12 +74,12 @@ typedef struct {
} nva_out; } nva_out;
void nva_out_init(nva_out *out); void nva_out_init(nva_out *out);
void nva_out_reset(nva_out *out); void nva_out_reset(nva_out *out, nghttp2_mem *mem);
void add_out(nva_out *out, nghttp2_nv *nv); void add_out(nva_out *out, nghttp2_nv *nv, nghttp2_mem *mem);
ssize_t inflate_hd(nghttp2_hd_inflater *inflater, nva_out *out, ssize_t inflate_hd(nghttp2_hd_inflater *inflater, nva_out *out,
nghttp2_bufs *bufs, size_t offset); nghttp2_bufs *bufs, size_t offset, nghttp2_mem *mem);
int pack_headers(nghttp2_bufs *bufs, nghttp2_hd_deflater *deflater, int pack_headers(nghttp2_bufs *bufs, nghttp2_hd_deflater *deflater,
int32_t stream_id, int flags, const nghttp2_nv *nva, int32_t stream_id, int flags, const nghttp2_nv *nva,
...@@ -107,6 +107,6 @@ nghttp2_stream *open_stream_with_dep_excl(nghttp2_session *session, ...@@ -107,6 +107,6 @@ nghttp2_stream *open_stream_with_dep_excl(nghttp2_session *session,
int32_t stream_id, int32_t stream_id,
nghttp2_stream *dep_stream); nghttp2_stream *dep_stream);
nghttp2_outbound_item *create_data_ob_item(void); nghttp2_outbound_item *create_data_ob_item(nghttp2_mem *mem);
#endif /* NGHTTP2_TEST_HELPER_H */ #endif /* NGHTTP2_TEST_HELPER_H */
Markdown is supported
0%
or
You are about to add 0 people to the discussion. Proceed with caution.
Finish editing this message first!
Please register or to comment