int nghttp2_bufs_realloc(nghttp2_bufs *bufs, size_t chunk_length) { int rv; nghttp2_buf_chain *chain; if (chunk_length < bufs->offset) { return NGHTTP2_ERR_INVALID_ARGUMENT; } rv = buf_chain_new(&chain, chunk_length, bufs->mem); if (rv != 0) { return rv; } nghttp2_bufs_free(bufs); bufs->head = chain; bufs->cur = bufs->head; nghttp2_buf_shift_right(&bufs->cur->buf, bufs->offset); bufs->chunk_length = chunk_length; bufs->chunk_used = 1; return 0; }
void test_nghttp2_hd_inflate_newname_inc(void) { nghttp2_hd_inflater inflater; nghttp2_bufs bufs; ssize_t blocklen; nghttp2_nv nv = MAKE_NV("x-rel", "nghttp2"); nva_out out; nghttp2_mem *mem; mem = nghttp2_mem_default(); frame_pack_bufs_init(&bufs); nva_out_init(&out); nghttp2_hd_inflate_init(&inflater, mem); CU_ASSERT(0 == nghttp2_hd_emit_newname_block(&bufs, &nv, 1)); blocklen = nghttp2_bufs_len(&bufs); CU_ASSERT(blocklen > 0); CU_ASSERT(blocklen == inflate_hd(&inflater, &out, &bufs, 0, mem)); CU_ASSERT(1 == out.nvlen); assert_nv_equal(&nv, out.nva, 1, mem); CU_ASSERT(1 == inflater.ctx.hd_table.len); assert_nv_equal( &nv, &GET_TABLE_ENT(&inflater.ctx, NGHTTP2_STATIC_TABLE_LENGTH + inflater.ctx.hd_table.len - 1)->nv, 1, mem); nva_out_reset(&out, mem); nghttp2_bufs_free(&bufs); nghttp2_hd_inflate_free(&inflater); }
static void check_deflate_inflate(nghttp2_hd_deflater *deflater, nghttp2_hd_inflater *inflater, nghttp2_nv *nva, size_t nvlen, nghttp2_mem *mem) { nghttp2_bufs bufs; ssize_t blocklen; nva_out out; int rv; frame_pack_bufs_init(&bufs); nva_out_init(&out); rv = nghttp2_hd_deflate_hd_bufs(deflater, &bufs, nva, nvlen); blocklen = nghttp2_bufs_len(&bufs); CU_ASSERT(0 == rv); CU_ASSERT(blocklen >= 0); CU_ASSERT(blocklen == inflate_hd(inflater, &out, &bufs, 0, mem)); CU_ASSERT(nvlen == out.nvlen); assert_nv_equal(nva, out.nva, nvlen, mem); nva_out_reset(&out, mem); nghttp2_bufs_free(&bufs); }
void test_nghttp2_hd_inflate_zero_length_huffman(void) { nghttp2_hd_inflater inflater; nghttp2_bufs bufs; /* Literal header without indexing - new name */ uint8_t data[] = {0x40, 0x01, 0x78 /* 'x' */, 0x80}; nva_out out; nghttp2_mem *mem; mem = nghttp2_mem_default(); frame_pack_bufs_init(&bufs); nva_out_init(&out); nghttp2_bufs_add(&bufs, data, sizeof(data)); /* /\* Literal header without indexing - new name *\/ */ /* ptr[0] = 0x40; */ /* ptr[1] = 1; */ /* ptr[2] = 'x'; */ /* ptr[3] = 0x80; */ nghttp2_hd_inflate_init(&inflater, mem); CU_ASSERT(4 == inflate_hd(&inflater, &out, &bufs, 0, mem)); CU_ASSERT(1 == out.nvlen); CU_ASSERT(1 == out.nva[0].namelen); CU_ASSERT('x' == out.nva[0].name[0]); CU_ASSERT(NULL == out.nva[0].value); CU_ASSERT(0 == out.nva[0].valuelen); nva_out_reset(&out, mem); nghttp2_bufs_free(&bufs); nghttp2_hd_inflate_free(&inflater); }
void test_nghttp2_bufs_reset(void) { int rv; nghttp2_bufs bufs; nghttp2_buf_chain *ci; ssize_t offset = 9; rv = nghttp2_bufs_init3(&bufs, 250, 3, 1, offset); CU_ASSERT(0 == rv); rv = nghttp2_bufs_add(&bufs, "foo", 3); CU_ASSERT(0 == rv); rv = nghttp2_bufs_advance(&bufs); CU_ASSERT(0 == rv); rv = nghttp2_bufs_add(&bufs, "bar", 3); CU_ASSERT(0 == rv); CU_ASSERT(6 == nghttp2_bufs_len(&bufs)); nghttp2_bufs_reset(&bufs); CU_ASSERT(0 == nghttp2_bufs_len(&bufs)); CU_ASSERT(bufs.cur == bufs.head); for(ci = bufs.head; ci; ci = ci->next) { CU_ASSERT(offset == ci->buf.pos - ci->buf.begin); CU_ASSERT(ci->buf.pos == ci->buf.last); } CU_ASSERT(bufs.head->next == NULL); nghttp2_bufs_free(&bufs); }
void test_nghttp2_bufs_orb(void) { int rv; nghttp2_bufs bufs; rv = nghttp2_bufs_init(&bufs, 1000, 3); CU_ASSERT(0 == rv); *(bufs.cur->buf.last) = 0; rv = nghttp2_bufs_orb_hold(&bufs, 15); CU_ASSERT(0 == rv); CU_ASSERT(0 == nghttp2_buf_len(&bufs.cur->buf)); CU_ASSERT(0 == nghttp2_bufs_len(&bufs)); CU_ASSERT(15 == *(bufs.cur->buf.last)); rv = nghttp2_bufs_orb(&bufs, 240); CU_ASSERT(0 == rv); CU_ASSERT(1 == nghttp2_buf_len(&bufs.cur->buf)); CU_ASSERT(1 == nghttp2_bufs_len(&bufs)); CU_ASSERT(255 == *(bufs.cur->buf.last - 1)); *(bufs.cur->buf.last) = 0; nghttp2_bufs_fast_orb_hold(&bufs, 240); CU_ASSERT(240 == *(bufs.cur->buf.last)); nghttp2_bufs_fast_orb(&bufs, 15); CU_ASSERT(255 == *(bufs.cur->buf.last - 1)); nghttp2_bufs_free(&bufs); }
void test_nghttp2_hd_deflate_bound(void) { nghttp2_hd_deflater deflater; nghttp2_nv nva[] = {MAKE_NV(":method", "GET"), MAKE_NV("alpha", "bravo")}; nghttp2_bufs bufs; size_t bound, bound2; nghttp2_mem *mem; mem = nghttp2_mem_default(); frame_pack_bufs_init(&bufs); nghttp2_hd_deflate_init(&deflater, mem); bound = nghttp2_hd_deflate_bound(&deflater, nva, ARRLEN(nva)); CU_ASSERT(12 + 6 * 2 * 2 + nva[0].namelen + nva[0].valuelen + nva[1].namelen + nva[1].valuelen == bound); nghttp2_hd_deflate_hd_bufs(&deflater, &bufs, nva, ARRLEN(nva)); CU_ASSERT(bound > (size_t)nghttp2_bufs_len(&bufs)); bound2 = nghttp2_hd_deflate_bound(&deflater, nva, ARRLEN(nva)); CU_ASSERT(bound == bound2); nghttp2_bufs_free(&bufs); nghttp2_hd_deflate_free(&deflater); }
void test_nghttp2_bufs_next_present(void) { int rv; nghttp2_bufs bufs; rv = nghttp2_bufs_init(&bufs, 250, 3); CU_ASSERT(0 == rv); CU_ASSERT(0 == nghttp2_bufs_next_present(&bufs)); rv = nghttp2_bufs_advance(&bufs); CU_ASSERT(0 == rv); nghttp2_bufs_rewind(&bufs); CU_ASSERT(0 == nghttp2_bufs_next_present(&bufs)); bufs.cur = bufs.head->next; rv = nghttp2_bufs_addb(&bufs, 1); CU_ASSERT(0 == rv); nghttp2_bufs_rewind(&bufs); CU_ASSERT(0 != nghttp2_bufs_next_present(&bufs)); nghttp2_bufs_free(&bufs); }
void test_nghttp2_bufs_add(void) { int rv; nghttp2_bufs bufs; uint8_t data[2048]; rv = nghttp2_bufs_init(&bufs, 1000, 3); CU_ASSERT(0 == rv); CU_ASSERT(bufs.cur->buf.pos == bufs.cur->buf.last); rv = nghttp2_bufs_add(&bufs, data, 493); CU_ASSERT(0 == rv); CU_ASSERT(493 == nghttp2_buf_len(&bufs.cur->buf)); CU_ASSERT(493 == nghttp2_bufs_len(&bufs)); CU_ASSERT(507 == nghttp2_bufs_cur_avail(&bufs)); rv = nghttp2_bufs_add(&bufs, data, 507); CU_ASSERT(0 == rv); CU_ASSERT(1000 == nghttp2_buf_len(&bufs.cur->buf)); CU_ASSERT(1000 == nghttp2_bufs_len(&bufs)); CU_ASSERT(bufs.cur == bufs.head); rv = nghttp2_bufs_add(&bufs, data, 1); CU_ASSERT(0 == rv); CU_ASSERT(1 == nghttp2_buf_len(&bufs.cur->buf)); CU_ASSERT(1001 == nghttp2_bufs_len(&bufs)); CU_ASSERT(bufs.cur == bufs.head->next); nghttp2_bufs_free(&bufs); }
void test_nghttp2_frame_pack_priority(void) { nghttp2_priority frame, oframe; nghttp2_bufs bufs; nghttp2_priority_spec pri_spec; int rv; frame_pack_bufs_init(&bufs); /* First, pack priority with priority group and weight */ nghttp2_priority_spec_init(&pri_spec, 1000000009, 12, 1); nghttp2_frame_priority_init(&frame, 1000000007, &pri_spec); rv = nghttp2_frame_pack_priority(&bufs, &frame); CU_ASSERT(0 == rv); CU_ASSERT(NGHTTP2_FRAME_HDLEN + 5 == nghttp2_bufs_len(&bufs)); CU_ASSERT(0 == unpack_framebuf((nghttp2_frame*)&oframe, &bufs)); check_frame_header(5, NGHTTP2_PRIORITY, NGHTTP2_FLAG_NONE, 1000000007, &oframe.hd); CU_ASSERT(1000000009 == oframe.pri_spec.stream_id); CU_ASSERT(12 == oframe.pri_spec.weight); CU_ASSERT(1 == oframe.pri_spec.exclusive); nghttp2_frame_priority_free(&oframe); nghttp2_bufs_reset(&bufs); nghttp2_bufs_free(&bufs); nghttp2_frame_priority_free(&frame); }
int nghttp2_bufs_realloc(nghttp2_bufs *bufs, size_t chunk_length) { int rv; nghttp2_buf_chain *chain; if (chunk_length < bufs->offset) { return NGHTTP2_ERR_INVALID_ARGUMENT; } rv = buf_chain_new(&chain, chunk_length, bufs->mem); if (rv != 0) { return rv; } nghttp2_bufs_free(bufs); bufs->head = chain; bufs->cur = bufs->head; nghttp2_buf_shift_right(&bufs->cur->buf, bufs->offset); bufs->chunk_length = chunk_length; bufs->chunk_used = 1; // h1994st: if (bufs->random_enabled) { // Resize the first chunk hx_nghttp2_buf_resize(&bufs->cur->buf, hx_get_buf_chunk_length(bufs->buf_chunk_length_gen)); } return 0; }
static void run_nghttp2_hd(void) { nghttp2_hd_deflater deflater; nghttp2_hd_inflater inflater; nghttp2_bufs bufs; int rv; nghttp2_nv nva1[] = { MAKE_NV(":scheme", "https"), MAKE_NV(":authority", "example.org"), MAKE_NV(":path", "/slashdot"), MAKE_NV("accept-encoding", "gzip, deflate"), MAKE_NV("foo", "bar")}; nghttp2_nv nva2[] = { MAKE_NV(":scheme", "https"), MAKE_NV(":authority", "example.org"), MAKE_NV(":path", "/style.css"), MAKE_NV("cookie", "nghttp2=FTW"), MAKE_NV("foo", "bar2")}; rv = frame_pack_bufs_init(&bufs); if (rv != 0) { return; } rv = nghttp2_hd_deflate_init(&deflater, nghttp2_mem_fm()); if (rv != 0) { goto deflate_init_fail; } rv = nghttp2_hd_inflate_init(&inflater, nghttp2_mem_fm()); if (rv != 0) { goto inflate_init_fail; } rv = deflate_inflate(&deflater, &inflater, &bufs, nva1, ARRLEN(nva1), nghttp2_mem_fm()); if (rv != 0) { goto deflate_hd_fail; } rv = deflate_inflate(&deflater, &inflater, &bufs, nva2, ARRLEN(nva2), nghttp2_mem_fm()); if (rv != 0) { goto deflate_hd_fail; } deflate_hd_fail: nghttp2_hd_inflate_free(&inflater); inflate_init_fail: nghttp2_hd_deflate_free(&deflater); deflate_init_fail: nghttp2_bufs_free(&bufs); }
void test_nghttp2_hd_deflate_same_indexed_repr(void) { nghttp2_hd_deflater deflater; nghttp2_hd_inflater inflater; nghttp2_nv nva1[] = {MAKE_NV("cookie", "alpha"), MAKE_NV("cookie", "alpha")}; nghttp2_nv nva2[] = {MAKE_NV("cookie", "alpha"), MAKE_NV("cookie", "alpha"), MAKE_NV("cookie", "alpha")}; nghttp2_bufs bufs; ssize_t blocklen; nva_out out; int rv; nghttp2_mem *mem; mem = nghttp2_mem_default(); frame_pack_bufs_init(&bufs); nva_out_init(&out); CU_ASSERT(0 == nghttp2_hd_deflate_init(&deflater, mem)); CU_ASSERT(0 == nghttp2_hd_inflate_init(&inflater, mem)); /* Encode 2 same headers. Emit 1 literal reprs and 1 index repr. */ rv = nghttp2_hd_deflate_hd_bufs(&deflater, &bufs, nva1, ARRLEN(nva1)); blocklen = nghttp2_bufs_len(&bufs); CU_ASSERT(0 == rv); CU_ASSERT(blocklen > 0); CU_ASSERT(blocklen == inflate_hd(&inflater, &out, &bufs, 0, mem)); CU_ASSERT(2 == out.nvlen); assert_nv_equal(nva1, out.nva, 2, mem); nva_out_reset(&out, mem); nghttp2_bufs_reset(&bufs); /* Encode 3 same headers. This time, emits 3 index reprs. */ rv = nghttp2_hd_deflate_hd_bufs(&deflater, &bufs, nva2, ARRLEN(nva2)); blocklen = nghttp2_bufs_len(&bufs); CU_ASSERT(0 == rv); CU_ASSERT(blocklen == 3); CU_ASSERT(blocklen == inflate_hd(&inflater, &out, &bufs, 0, mem)); CU_ASSERT(3 == out.nvlen); assert_nv_equal(nva2, out.nva, 3, mem); nva_out_reset(&out, mem); nghttp2_bufs_reset(&bufs); /* Cleanup */ nghttp2_bufs_free(&bufs); nghttp2_hd_inflate_free(&inflater); nghttp2_hd_deflate_free(&deflater); }
void test_nghttp2_hd_ringbuf_reserve(void) { nghttp2_hd_deflater deflater; nghttp2_hd_inflater inflater; nghttp2_nv nv; nghttp2_bufs bufs; nva_out out; int i; ssize_t rv; ssize_t blocklen; nghttp2_mem *mem; mem = nghttp2_mem_default(); frame_pack_bufs_init(&bufs); nva_out_init(&out); nv.flags = NGHTTP2_NV_FLAG_NONE; nv.name = (uint8_t *)"a"; nv.namelen = strlen((const char *)nv.name); nv.valuelen = 4; nv.value = mem->malloc(nv.valuelen + 1, NULL); memset(nv.value, 0, nv.valuelen); nghttp2_hd_deflate_init2(&deflater, 8000, mem); nghttp2_hd_inflate_init(&inflater, mem); nghttp2_hd_inflate_change_table_size(&inflater, 8000); nghttp2_hd_deflate_change_table_size(&deflater, 8000); for (i = 0; i < 150; ++i) { memcpy(nv.value, &i, sizeof(i)); rv = nghttp2_hd_deflate_hd_bufs(&deflater, &bufs, &nv, 1); blocklen = nghttp2_bufs_len(&bufs); CU_ASSERT(0 == rv); CU_ASSERT(blocklen > 0); CU_ASSERT(blocklen == inflate_hd(&inflater, &out, &bufs, 0, mem)); CU_ASSERT(1 == out.nvlen); assert_nv_equal(&nv, out.nva, 1, mem); nva_out_reset(&out, mem); nghttp2_bufs_reset(&bufs); } nghttp2_bufs_free(&bufs); nghttp2_hd_inflate_free(&inflater); nghttp2_hd_deflate_free(&deflater); mem->free(nv.value, NULL); }
static void run_nghttp2_frame_pack_headers(void) { nghttp2_hd_deflater deflater; nghttp2_hd_inflater inflater; nghttp2_frame frame, oframe; nghttp2_bufs bufs; nghttp2_nv nv[] = {MAKE_NV(":host", "example.org"), MAKE_NV(":scheme", "https")}; int rv; nghttp2_nv *nva; size_t nvlen; rv = frame_pack_bufs_init(&bufs); if (rv != 0) { return; } rv = nghttp2_hd_deflate_init(&deflater, nghttp2_mem_fm()); if (rv != 0) { goto deflate_init_fail; } rv = nghttp2_hd_inflate_init(&inflater, nghttp2_mem_fm()); if (rv != 0) { goto inflate_init_fail; } nvlen = ARRLEN(nv); rv = nghttp2_nv_array_copy(&nva, nv, nvlen, nghttp2_mem_fm()); if (rv < 0) { goto nv_copy_fail; } nghttp2_frame_headers_init(&frame.headers, NGHTTP2_FLAG_END_STREAM, 1, NGHTTP2_HCAT_REQUEST, NULL, nva, nvlen); rv = nghttp2_frame_pack_headers(&bufs, &frame.headers, &deflater); if (rv != 0) { goto fail; } rv = unpack_framebuf(&oframe, &bufs); if (rv != 0) { goto fail; } nghttp2_frame_headers_free(&oframe.headers, nghttp2_mem_fm()); fail: nghttp2_frame_headers_free(&frame.headers, nghttp2_mem_fm()); nv_copy_fail: nghttp2_hd_inflate_free(&inflater); inflate_init_fail: nghttp2_hd_deflate_free(&deflater); deflate_init_fail: nghttp2_bufs_free(&bufs); }
void test_nghttp2_hd_inflate_indname_inc_eviction(void) { nghttp2_hd_inflater inflater; nghttp2_bufs bufs; ssize_t blocklen; uint8_t value[1025]; nva_out out; nghttp2_nv nv; nghttp2_mem *mem; mem = nghttp2_mem_default(); frame_pack_bufs_init(&bufs); nva_out_init(&out); nghttp2_hd_inflate_init(&inflater, mem); memset(value, '0', sizeof(value)); value[sizeof(value) - 1] = '\0'; nv.value = value; nv.valuelen = sizeof(value) - 1; nv.flags = NGHTTP2_NV_FLAG_NONE; CU_ASSERT(0 == nghttp2_hd_emit_indname_block(&bufs, 14, &nv, NGHTTP2_HD_WITH_INDEXING)); CU_ASSERT(0 == nghttp2_hd_emit_indname_block(&bufs, 15, &nv, NGHTTP2_HD_WITH_INDEXING)); CU_ASSERT(0 == nghttp2_hd_emit_indname_block(&bufs, 16, &nv, NGHTTP2_HD_WITH_INDEXING)); CU_ASSERT(0 == nghttp2_hd_emit_indname_block(&bufs, 17, &nv, NGHTTP2_HD_WITH_INDEXING)); blocklen = nghttp2_bufs_len(&bufs); CU_ASSERT(blocklen > 0); CU_ASSERT(blocklen == inflate_hd(&inflater, &out, &bufs, 0, mem)); CU_ASSERT(4 == out.nvlen); CU_ASSERT(14 == out.nva[0].namelen); CU_ASSERT(0 == memcmp("accept-charset", out.nva[0].name, out.nva[0].namelen)); CU_ASSERT(sizeof(value) - 1 == out.nva[0].valuelen); nva_out_reset(&out, mem); nghttp2_bufs_reset(&bufs); CU_ASSERT(3 == inflater.ctx.hd_table.len); nghttp2_bufs_free(&bufs); nghttp2_hd_inflate_free(&inflater); }
void test_nghttp2_hd_no_index(void) { nghttp2_hd_deflater deflater; nghttp2_hd_inflater inflater; nghttp2_bufs bufs; ssize_t blocklen; nghttp2_nv nva[] = { MAKE_NV(":method", "GET"), MAKE_NV(":method", "POST"), MAKE_NV(":path", "/foo"), MAKE_NV("version", "HTTP/1.1"), MAKE_NV(":method", "GET"), }; size_t i; nva_out out; int rv; nghttp2_mem *mem; mem = nghttp2_mem_default(); /* 1st :method: GET can be indexable, last one is not */ for (i = 1; i < ARRLEN(nva); ++i) { nva[i].flags = NGHTTP2_NV_FLAG_NO_INDEX; } frame_pack_bufs_init(&bufs); nva_out_init(&out); nghttp2_hd_deflate_init(&deflater, mem); nghttp2_hd_inflate_init(&inflater, mem); rv = nghttp2_hd_deflate_hd_bufs(&deflater, &bufs, nva, ARRLEN(nva)); blocklen = nghttp2_bufs_len(&bufs); CU_ASSERT(0 == rv); CU_ASSERT(blocklen > 0); CU_ASSERT(blocklen == inflate_hd(&inflater, &out, &bufs, 0, mem)); CU_ASSERT(ARRLEN(nva) == out.nvlen); assert_nv_equal(nva, out.nva, ARRLEN(nva), mem); CU_ASSERT(out.nva[0].flags == NGHTTP2_NV_FLAG_NONE); for (i = 1; i < ARRLEN(nva); ++i) { CU_ASSERT(out.nva[i].flags == NGHTTP2_NV_FLAG_NO_INDEX); } nva_out_reset(&out, mem); nghttp2_bufs_free(&bufs); nghttp2_hd_inflate_free(&inflater); nghttp2_hd_deflate_free(&deflater); }
void test_nghttp2_frame_pack_push_promise() { nghttp2_hd_deflater deflater; nghttp2_hd_inflater inflater; nghttp2_push_promise frame, oframe; nghttp2_bufs bufs; nghttp2_nv *nva; ssize_t nvlen; nva_out out; ssize_t hdblocklen; int rv; frame_pack_bufs_init(&bufs); nva_out_init(&out); nghttp2_hd_deflate_init(&deflater); nghttp2_hd_inflate_init(&inflater); nva = headers(); nvlen = HEADERS_LENGTH; nghttp2_frame_push_promise_init(&frame, NGHTTP2_FLAG_END_HEADERS, 1000000007, (1U << 31) - 1, nva, nvlen); rv = nghttp2_frame_pack_push_promise(&bufs, &frame, &deflater); CU_ASSERT(0 == rv); CU_ASSERT(nghttp2_bufs_len(&bufs) > 0); CU_ASSERT(0 == unpack_framebuf((nghttp2_frame*)&oframe, &bufs)); check_frame_header(nghttp2_bufs_len(&bufs) - NGHTTP2_FRAME_HDLEN, NGHTTP2_PUSH_PROMISE, NGHTTP2_FLAG_END_HEADERS, 1000000007, &oframe.hd); CU_ASSERT((1U << 31) - 1 == oframe.promised_stream_id); hdblocklen = nghttp2_bufs_len(&bufs) - NGHTTP2_FRAME_HDLEN - 4; CU_ASSERT(hdblocklen == inflate_hd(&inflater, &out, &bufs, NGHTTP2_FRAME_HDLEN + 4)); CU_ASSERT(7 == out.nvlen); CU_ASSERT(nvnameeq("method", &out.nva[0])); CU_ASSERT(nvvalueeq("GET", &out.nva[0])); nva_out_reset(&out); nghttp2_bufs_free(&bufs); nghttp2_frame_push_promise_free(&oframe); nghttp2_frame_push_promise_free(&frame); nghttp2_hd_inflate_free(&inflater); nghttp2_hd_deflate_free(&deflater); }
static void run_nghttp2_frame_pack_settings(void) { nghttp2_frame frame, oframe; nghttp2_bufs bufs; nghttp2_buf *buf; nghttp2_settings_entry iv[2], *iv_copy; int rv; rv = frame_pack_bufs_init(&bufs); if (rv != 0) { return; } iv[0].settings_id = NGHTTP2_SETTINGS_HEADER_TABLE_SIZE; iv[0].value = 4096; iv[1].settings_id = NGHTTP2_SETTINGS_MAX_CONCURRENT_STREAMS; iv[1].value = 100; iv_copy = nghttp2_frame_iv_copy(iv, 2, nghttp2_mem_fm()); if (iv_copy == NULL) { goto iv_copy_fail; } nghttp2_frame_settings_init(&frame.settings, NGHTTP2_FLAG_NONE, iv_copy, 2); rv = nghttp2_frame_pack_settings(&bufs, &frame.settings); if (rv != 0) { goto fail; } buf = &bufs.head->buf; rv = nghttp2_frame_unpack_settings_payload2( &oframe.settings.iv, &oframe.settings.niv, buf->pos + NGHTTP2_FRAME_HDLEN, nghttp2_buf_len(buf) - NGHTTP2_FRAME_HDLEN, nghttp2_mem_fm()); if (rv != 0) { goto fail; } nghttp2_frame_settings_free(&oframe.settings, nghttp2_mem_fm()); fail: nghttp2_frame_settings_free(&frame.settings, nghttp2_mem_fm()); iv_copy_fail: nghttp2_bufs_free(&bufs); }
void test_nghttp2_frame_pack_goaway() { nghttp2_goaway frame, oframe; nghttp2_bufs bufs; size_t opaque_data_len = 16; uint8_t *opaque_data = malloc(opaque_data_len); int rv; frame_pack_bufs_init(&bufs); memcpy(opaque_data, "0123456789abcdef", opaque_data_len); nghttp2_frame_goaway_init(&frame, 1000000007, NGHTTP2_PROTOCOL_ERROR, opaque_data, opaque_data_len); rv = nghttp2_frame_pack_goaway(&bufs, &frame); CU_ASSERT(0 == rv); CU_ASSERT((ssize_t)(NGHTTP2_FRAME_HDLEN + 8 + opaque_data_len) == nghttp2_bufs_len(&bufs)); CU_ASSERT(0 == unpack_framebuf((nghttp2_frame*)&oframe, &bufs)); check_frame_header(24, NGHTTP2_GOAWAY, NGHTTP2_FLAG_NONE, 0, &oframe.hd); CU_ASSERT(1000000007 == oframe.last_stream_id); CU_ASSERT(NGHTTP2_PROTOCOL_ERROR == oframe.error_code); CU_ASSERT(opaque_data_len == oframe.opaque_data_len); CU_ASSERT(memcmp(opaque_data, oframe.opaque_data, opaque_data_len) == 0); nghttp2_frame_goaway_free(&oframe); nghttp2_bufs_reset(&bufs); /* Unknown error code is passed to callback as is */ frame.error_code = 1000000009; rv = nghttp2_frame_pack_goaway(&bufs, &frame); CU_ASSERT(0 == rv); CU_ASSERT(0 == unpack_framebuf((nghttp2_frame*)&oframe, &bufs)); check_frame_header(24, NGHTTP2_GOAWAY, NGHTTP2_FLAG_NONE, 0, &oframe.hd); CU_ASSERT(1000000009 == oframe.error_code); nghttp2_frame_goaway_free(&oframe); nghttp2_frame_goaway_free(&frame); nghttp2_bufs_free(&bufs); }
void test_nghttp2_bufs_advance(void) { int rv; nghttp2_bufs bufs; int i; rv = nghttp2_bufs_init(&bufs, 250, 3); CU_ASSERT(0 == rv); for(i = 0; i < 2; ++i) { rv = nghttp2_bufs_advance(&bufs); CU_ASSERT(0 == rv); } rv = nghttp2_bufs_advance(&bufs); CU_ASSERT(NGHTTP2_ERR_BUFFER_ERROR == rv); nghttp2_bufs_free(&bufs); }
void test_nghttp2_frame_pack_settings() { nghttp2_settings frame, oframe; nghttp2_bufs bufs; int i; int rv; nghttp2_settings_entry iv[] = { { NGHTTP2_SETTINGS_MAX_CONCURRENT_STREAMS, 256 }, { NGHTTP2_SETTINGS_INITIAL_WINDOW_SIZE, 16384 }, { NGHTTP2_SETTINGS_HEADER_TABLE_SIZE, 4096 } }; frame_pack_bufs_init(&bufs); nghttp2_frame_settings_init(&frame, NGHTTP2_FLAG_NONE, nghttp2_frame_iv_copy(iv, 3), 3); rv = nghttp2_frame_pack_settings(&bufs, &frame); CU_ASSERT(0 == rv); CU_ASSERT(NGHTTP2_FRAME_HDLEN + 3 * NGHTTP2_FRAME_SETTINGS_ENTRY_LENGTH == nghttp2_bufs_len(&bufs)); CU_ASSERT(0 == unpack_framebuf((nghttp2_frame*)&oframe, &bufs)); check_frame_header(3 * NGHTTP2_FRAME_SETTINGS_ENTRY_LENGTH, NGHTTP2_SETTINGS, NGHTTP2_FLAG_NONE, 0, &oframe.hd); CU_ASSERT(3 == oframe.niv); for(i = 0; i < 3; ++i) { CU_ASSERT(iv[i].settings_id == oframe.iv[i].settings_id); CU_ASSERT(iv[i].value == oframe.iv[i].value); } nghttp2_bufs_free(&bufs); nghttp2_frame_settings_free(&frame); nghttp2_frame_settings_free(&oframe); }
void test_nghttp2_hd_inflate_newname_noinc(void) { nghttp2_hd_inflater inflater; nghttp2_bufs bufs; ssize_t blocklen; nghttp2_nv nv[] = {/* Expecting huffman for both */ MAKE_NV("my-long-content-length", "nghttp2"), /* Expecting no huffman for both */ MAKE_NV("x", "y"), /* Huffman for key only */ MAKE_NV("my-long-content-length", "y"), /* Huffman for value only */ MAKE_NV("x", "nghttp2")}; size_t i; nva_out out; nghttp2_mem *mem; mem = nghttp2_mem_default(); frame_pack_bufs_init(&bufs); nva_out_init(&out); nghttp2_hd_inflate_init(&inflater, mem); for (i = 0; i < ARRLEN(nv); ++i) { CU_ASSERT(0 == nghttp2_hd_emit_newname_block(&bufs, &nv[i], NGHTTP2_HD_WITHOUT_INDEXING)); blocklen = nghttp2_bufs_len(&bufs); CU_ASSERT(blocklen > 0); CU_ASSERT(blocklen == inflate_hd(&inflater, &out, &bufs, 0, mem)); CU_ASSERT(1 == out.nvlen); assert_nv_equal(&nv[i], out.nva, 1, mem); CU_ASSERT(0 == inflater.ctx.hd_table.len); nva_out_reset(&out, mem); nghttp2_bufs_reset(&bufs); } nghttp2_bufs_free(&bufs); nghttp2_hd_inflate_free(&inflater); }
void test_nghttp2_hd_inflate_indexed(void) { nghttp2_hd_inflater inflater; nghttp2_bufs bufs; ssize_t blocklen; nghttp2_nv nv = MAKE_NV(":path", "/"); nva_out out; nghttp2_mem *mem; mem = nghttp2_mem_default(); frame_pack_bufs_init(&bufs); nva_out_init(&out); nghttp2_hd_inflate_init(&inflater, mem); nghttp2_bufs_addb(&bufs, (1 << 7) | 4); blocklen = nghttp2_bufs_len(&bufs); CU_ASSERT(1 == blocklen); CU_ASSERT(blocklen == inflate_hd(&inflater, &out, &bufs, 0, mem)); CU_ASSERT(1 == out.nvlen); assert_nv_equal(&nv, out.nva, 1, mem); nva_out_reset(&out, mem); nghttp2_bufs_reset(&bufs); /* index = 0 is error */ nghttp2_bufs_addb(&bufs, 1 << 7); blocklen = nghttp2_bufs_len(&bufs); CU_ASSERT(1 == blocklen); CU_ASSERT(NGHTTP2_ERR_HEADER_COMP == inflate_hd(&inflater, &out, &bufs, 0, mem)); nghttp2_bufs_free(&bufs); nghttp2_hd_inflate_free(&inflater); }
void test_nghttp2_frame_pack_headers_frame_too_large(void) { nghttp2_hd_deflater deflater; nghttp2_headers frame; nghttp2_bufs bufs; nghttp2_nv *nva; size_t big_vallen = NGHTTP2_HD_MAX_NV; nghttp2_nv big_hds[16]; size_t big_hdslen = ARRLEN(big_hds); size_t i; int rv; frame_pack_bufs_init(&bufs); for(i = 0; i < big_hdslen; ++i) { big_hds[i].name = (uint8_t*)"header"; big_hds[i].value = malloc(big_vallen+1); memset(big_hds[i].value, '0' + (int)i, big_vallen); big_hds[i].value[big_vallen] = '\0'; big_hds[i].namelen = strlen((char*)big_hds[i].name); big_hds[i].valuelen = big_vallen; big_hds[i].flags = NGHTTP2_NV_FLAG_NONE; } nghttp2_nv_array_copy(&nva, big_hds, big_hdslen); nghttp2_hd_deflate_init(&deflater); nghttp2_frame_headers_init(&frame, NGHTTP2_FLAG_END_STREAM|NGHTTP2_FLAG_END_HEADERS, 1000000007, NGHTTP2_HCAT_REQUEST, NULL, nva, big_hdslen); rv = nghttp2_frame_pack_headers(&bufs, &frame, &deflater); CU_ASSERT(NGHTTP2_ERR_HEADER_COMP == rv); nghttp2_frame_headers_free(&frame); nghttp2_bufs_free(&bufs); for(i = 0; i < big_hdslen; ++i) { free(big_hds[i].value); } nghttp2_hd_deflate_free(&deflater); }
void test_nghttp2_frame_pack_rst_stream(void) { nghttp2_rst_stream frame, oframe; nghttp2_bufs bufs; int rv; frame_pack_bufs_init(&bufs); nghttp2_frame_rst_stream_init(&frame, 1000000007, NGHTTP2_PROTOCOL_ERROR); rv = nghttp2_frame_pack_rst_stream(&bufs, &frame); CU_ASSERT(0 == rv); CU_ASSERT(NGHTTP2_FRAME_HDLEN + 4 == nghttp2_bufs_len(&bufs)); CU_ASSERT(0 == unpack_framebuf((nghttp2_frame*)&oframe, &bufs)); check_frame_header(4, NGHTTP2_RST_STREAM, NGHTTP2_FLAG_NONE, 1000000007, &oframe.hd); CU_ASSERT(NGHTTP2_PROTOCOL_ERROR == oframe.error_code); nghttp2_frame_rst_stream_free(&oframe); nghttp2_bufs_reset(&bufs); /* Unknown error code is passed to callback as is */ frame.error_code = 1000000009; rv = nghttp2_frame_pack_rst_stream(&bufs, &frame); CU_ASSERT(0 == rv); CU_ASSERT(0 == unpack_framebuf((nghttp2_frame*)&oframe, &bufs)); check_frame_header(4, NGHTTP2_RST_STREAM, NGHTTP2_FLAG_NONE, 1000000007, &oframe.hd); CU_ASSERT(1000000009 == oframe.error_code); nghttp2_frame_rst_stream_free(&oframe); nghttp2_frame_rst_stream_free(&frame); nghttp2_bufs_free(&bufs); }
void test_nghttp2_bufs_remove(void) { int rv; nghttp2_bufs bufs; nghttp2_buf_chain *chain; int i; uint8_t *out; ssize_t outlen; rv = nghttp2_bufs_init(&bufs, 1000, 3); CU_ASSERT(0 == rv); nghttp2_buf_shift_right(&bufs.cur->buf, 10); rv = nghttp2_bufs_add(&bufs, "hello ", 6); CU_ASSERT(0 == rv); for(i = 0; i < 2; ++i) { chain = bufs.cur; rv = nghttp2_bufs_advance(&bufs); CU_ASSERT(0 == rv); CU_ASSERT(chain->next == bufs.cur); } rv = nghttp2_bufs_add(&bufs, "world", 5); CU_ASSERT(0 == rv); outlen = nghttp2_bufs_remove(&bufs, &out); CU_ASSERT(11 == outlen); CU_ASSERT(0 == memcmp("hello world", out, outlen)); CU_ASSERT(0 == nghttp2_bufs_len(&bufs)); CU_ASSERT(bufs.cur->buf.pos == bufs.cur->buf.begin); free(out); nghttp2_bufs_free(&bufs); }
void test_nghttp2_bufs_realloc(void) { int rv; nghttp2_bufs bufs; rv = nghttp2_bufs_init3(&bufs, 266, 3, 1, 10); CU_ASSERT(0 == rv); /* Create new buffer to see that these buffers are deallocated on realloc */ rv = nghttp2_bufs_advance(&bufs); CU_ASSERT(0 == rv); rv = nghttp2_bufs_realloc(&bufs, 522); CU_ASSERT(0 == rv); CU_ASSERT(512 == nghttp2_bufs_cur_avail(&bufs)); rv = nghttp2_bufs_realloc(&bufs, 9); CU_ASSERT(NGHTTP2_ERR_INVALID_ARGUMENT == rv); nghttp2_bufs_free(&bufs); }
void test_nghttp2_frame_pack_window_update(void) { nghttp2_window_update frame, oframe; nghttp2_bufs bufs; int rv; frame_pack_bufs_init(&bufs); nghttp2_frame_window_update_init(&frame, NGHTTP2_FLAG_NONE, 1000000007, 4096); rv = nghttp2_frame_pack_window_update(&bufs, &frame); CU_ASSERT(0 == rv); CU_ASSERT(NGHTTP2_FRAME_HDLEN + 4 == nghttp2_bufs_len(&bufs)); CU_ASSERT(0 == unpack_framebuf((nghttp2_frame*)&oframe, &bufs)); check_frame_header(4, NGHTTP2_WINDOW_UPDATE, NGHTTP2_FLAG_NONE, 1000000007, &oframe.hd); CU_ASSERT(4096 == oframe.window_size_increment); nghttp2_bufs_free(&bufs); nghttp2_frame_window_update_free(&oframe); nghttp2_frame_window_update_free(&frame); }
void test_nghttp2_frame_pack_ping(void) { nghttp2_ping frame, oframe; nghttp2_bufs bufs; const uint8_t opaque_data[] = "01234567"; int rv; frame_pack_bufs_init(&bufs); nghttp2_frame_ping_init(&frame, NGHTTP2_FLAG_ACK, opaque_data); rv = nghttp2_frame_pack_ping(&bufs, &frame); CU_ASSERT(0 == rv); CU_ASSERT(NGHTTP2_FRAME_HDLEN + 8 == nghttp2_bufs_len(&bufs)); CU_ASSERT(0 == unpack_framebuf((nghttp2_frame*)&oframe, &bufs)); check_frame_header(8, NGHTTP2_PING, NGHTTP2_FLAG_ACK, 0, &oframe.hd); CU_ASSERT(memcmp(opaque_data, oframe.opaque_data, sizeof(opaque_data) - 1) == 0); nghttp2_bufs_free(&bufs); nghttp2_frame_ping_free(&oframe); nghttp2_frame_ping_free(&frame); }