2 * nghttp2 - HTTP/2 C Library
4 * Copyright (c) 2013 Tatsuhiro Tsujikawa
6 * Permission is hereby granted, free of charge, to any person obtaining
7 * a copy of this software and associated documentation files (the
8 * "Software"), to deal in the Software without restriction, including
9 * without limitation the rights to use, copy, modify, merge, publish,
10 * distribute, sublicense, and/or sell copies of the Software, and to
11 * permit persons to whom the Software is furnished to do so, subject to
12 * the following conditions:
14 * The above copyright notice and this permission notice shall be
15 * included in all copies or substantial portions of the Software.
17 * THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND,
18 * EXPRESS OR IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF
19 * MERCHANTABILITY, FITNESS FOR A PARTICULAR PURPOSE AND
20 * NONINFRINGEMENT. IN NO EVENT SHALL THE AUTHORS OR COPYRIGHT HOLDERS BE
21 * LIABLE FOR ANY CLAIM, DAMAGES OR OTHER LIABILITY, WHETHER IN AN ACTION
22 * OF CONTRACT, TORT OR OTHERWISE, ARISING FROM, OUT OF OR IN CONNECTION
23 * WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS IN THE SOFTWARE.
25 #include "nghttp2_hd_test.h"
30 #include <CUnit/CUnit.h>
32 #include "nghttp2_hd.h"
33 #include "nghttp2_frame.h"
34 #include "nghttp2_test_helper.h"
36 #define GET_TABLE_ENT(context, index) nghttp2_hd_table_get(context, index)
38 void test_nghttp2_hd_deflate(void) {
39 nghttp2_hd_deflater deflater;
40 nghttp2_hd_inflater inflater;
41 nghttp2_nv nva1[] = {MAKE_NV(":path", "/my-example/index.html"),
42 MAKE_NV(":scheme", "https"), MAKE_NV("hello", "world")};
43 nghttp2_nv nva2[] = {MAKE_NV(":path", "/script.js"),
44 MAKE_NV(":scheme", "https")};
45 nghttp2_nv nva3[] = {MAKE_NV("cookie", "k1=v1"), MAKE_NV("cookie", "k2=v2"),
46 MAKE_NV("via", "proxy")};
47 nghttp2_nv nva4[] = {MAKE_NV(":path", "/style.css"),
48 MAKE_NV("cookie", "k1=v1"), MAKE_NV("cookie", "k1=v1")};
49 nghttp2_nv nva5[] = {MAKE_NV(":path", "/style.css"),
50 MAKE_NV("x-nghttp2", "")};
57 mem = nghttp2_mem_default();
58 frame_pack_bufs_init(&bufs);
61 CU_ASSERT(0 == nghttp2_hd_deflate_init(&deflater, mem));
62 CU_ASSERT(0 == nghttp2_hd_inflate_init(&inflater, mem));
64 rv = nghttp2_hd_deflate_hd_bufs(&deflater, &bufs, nva1, ARRLEN(nva1));
65 blocklen = nghttp2_bufs_len(&bufs);
68 CU_ASSERT(blocklen > 0);
69 CU_ASSERT(blocklen == inflate_hd(&inflater, &out, &bufs, 0, mem));
71 CU_ASSERT(3 == out.nvlen);
72 assert_nv_equal(nva1, out.nva, 3, mem);
74 nva_out_reset(&out, mem);
75 nghttp2_bufs_reset(&bufs);
78 rv = nghttp2_hd_deflate_hd_bufs(&deflater, &bufs, nva2, ARRLEN(nva2));
79 blocklen = nghttp2_bufs_len(&bufs);
82 CU_ASSERT(blocklen > 0);
83 CU_ASSERT(blocklen == inflate_hd(&inflater, &out, &bufs, 0, mem));
85 CU_ASSERT(2 == out.nvlen);
86 assert_nv_equal(nva2, out.nva, 2, mem);
88 nva_out_reset(&out, mem);
89 nghttp2_bufs_reset(&bufs);
91 /* Third headers, including same header field name, but value is not
93 rv = nghttp2_hd_deflate_hd_bufs(&deflater, &bufs, nva3, ARRLEN(nva3));
94 blocklen = nghttp2_bufs_len(&bufs);
97 CU_ASSERT(blocklen > 0);
98 CU_ASSERT(blocklen == inflate_hd(&inflater, &out, &bufs, 0, mem));
100 CU_ASSERT(3 == out.nvlen);
101 assert_nv_equal(nva3, out.nva, 3, mem);
103 nva_out_reset(&out, mem);
104 nghttp2_bufs_reset(&bufs);
106 /* Fourth headers, including duplicate header fields. */
107 rv = nghttp2_hd_deflate_hd_bufs(&deflater, &bufs, nva4, ARRLEN(nva4));
108 blocklen = nghttp2_bufs_len(&bufs);
111 CU_ASSERT(blocklen > 0);
112 CU_ASSERT(blocklen == inflate_hd(&inflater, &out, &bufs, 0, mem));
114 CU_ASSERT(3 == out.nvlen);
115 assert_nv_equal(nva4, out.nva, 3, mem);
117 nva_out_reset(&out, mem);
118 nghttp2_bufs_reset(&bufs);
120 /* Fifth headers includes empty value */
121 rv = nghttp2_hd_deflate_hd_bufs(&deflater, &bufs, nva5, ARRLEN(nva5));
122 blocklen = nghttp2_bufs_len(&bufs);
125 CU_ASSERT(blocklen > 0);
126 CU_ASSERT(blocklen == inflate_hd(&inflater, &out, &bufs, 0, mem));
128 CU_ASSERT(2 == out.nvlen);
129 assert_nv_equal(nva5, out.nva, 2, mem);
131 nva_out_reset(&out, mem);
132 nghttp2_bufs_reset(&bufs);
135 nghttp2_bufs_free(&bufs);
136 nghttp2_hd_inflate_free(&inflater);
137 nghttp2_hd_deflate_free(&deflater);
140 void test_nghttp2_hd_deflate_same_indexed_repr(void) {
141 nghttp2_hd_deflater deflater;
142 nghttp2_hd_inflater inflater;
143 nghttp2_nv nva1[] = {MAKE_NV("host", "alpha"), MAKE_NV("host", "alpha")};
144 nghttp2_nv nva2[] = {MAKE_NV("host", "alpha"), MAKE_NV("host", "alpha"),
145 MAKE_NV("host", "alpha")};
152 mem = nghttp2_mem_default();
153 frame_pack_bufs_init(&bufs);
156 CU_ASSERT(0 == nghttp2_hd_deflate_init(&deflater, mem));
157 CU_ASSERT(0 == nghttp2_hd_inflate_init(&inflater, mem));
159 /* Encode 2 same headers. Emit 1 literal reprs and 1 index repr. */
160 rv = nghttp2_hd_deflate_hd_bufs(&deflater, &bufs, nva1, ARRLEN(nva1));
161 blocklen = nghttp2_bufs_len(&bufs);
164 CU_ASSERT(blocklen > 0);
165 CU_ASSERT(blocklen == inflate_hd(&inflater, &out, &bufs, 0, mem));
167 CU_ASSERT(2 == out.nvlen);
168 assert_nv_equal(nva1, out.nva, 2, mem);
170 nva_out_reset(&out, mem);
171 nghttp2_bufs_reset(&bufs);
173 /* Encode 3 same headers. This time, emits 3 index reprs. */
174 rv = nghttp2_hd_deflate_hd_bufs(&deflater, &bufs, nva2, ARRLEN(nva2));
175 blocklen = nghttp2_bufs_len(&bufs);
178 CU_ASSERT(blocklen == 3);
179 CU_ASSERT(blocklen == inflate_hd(&inflater, &out, &bufs, 0, mem));
181 CU_ASSERT(3 == out.nvlen);
182 assert_nv_equal(nva2, out.nva, 3, mem);
184 nva_out_reset(&out, mem);
185 nghttp2_bufs_reset(&bufs);
188 nghttp2_bufs_free(&bufs);
189 nghttp2_hd_inflate_free(&inflater);
190 nghttp2_hd_deflate_free(&deflater);
193 void test_nghttp2_hd_inflate_indexed(void) {
194 nghttp2_hd_inflater inflater;
197 nghttp2_nv nv = MAKE_NV(":path", "/");
201 mem = nghttp2_mem_default();
202 frame_pack_bufs_init(&bufs);
205 nghttp2_hd_inflate_init(&inflater, mem);
207 nghttp2_bufs_addb(&bufs, (1 << 7) | 4);
209 blocklen = nghttp2_bufs_len(&bufs);
211 CU_ASSERT(1 == blocklen);
212 CU_ASSERT(blocklen == inflate_hd(&inflater, &out, &bufs, 0, mem));
214 CU_ASSERT(1 == out.nvlen);
216 assert_nv_equal(&nv, out.nva, 1, mem);
218 nva_out_reset(&out, mem);
219 nghttp2_bufs_reset(&bufs);
221 /* index = 0 is error */
222 nghttp2_bufs_addb(&bufs, 1 << 7);
224 blocklen = nghttp2_bufs_len(&bufs);
226 CU_ASSERT(1 == blocklen);
227 CU_ASSERT(NGHTTP2_ERR_HEADER_COMP ==
228 inflate_hd(&inflater, &out, &bufs, 0, mem));
230 nghttp2_bufs_free(&bufs);
231 nghttp2_hd_inflate_free(&inflater);
234 void test_nghttp2_hd_inflate_indname_noinc(void) {
235 nghttp2_hd_inflater inflater;
238 nghttp2_nv nv[] = {/* Huffman */
239 MAKE_NV("user-agent", "nghttp2"),
240 /* Expecting no huffman */
241 MAKE_NV("user-agent", "x")};
246 mem = nghttp2_mem_default();
247 frame_pack_bufs_init(&bufs);
250 nghttp2_hd_inflate_init(&inflater, mem);
252 for (i = 0; i < ARRLEN(nv); ++i) {
253 CU_ASSERT(0 == nghttp2_hd_emit_indname_block(&bufs, 57, &nv[i],
254 NGHTTP2_HD_WITHOUT_INDEXING));
256 blocklen = nghttp2_bufs_len(&bufs);
258 CU_ASSERT(blocklen > 0);
259 CU_ASSERT(blocklen == inflate_hd(&inflater, &out, &bufs, 0, mem));
261 CU_ASSERT(1 == out.nvlen);
262 assert_nv_equal(&nv[i], out.nva, 1, mem);
263 CU_ASSERT(0 == inflater.ctx.hd_table.len);
265 nva_out_reset(&out, mem);
266 nghttp2_bufs_reset(&bufs);
269 nghttp2_bufs_free(&bufs);
270 nghttp2_hd_inflate_free(&inflater);
273 void test_nghttp2_hd_inflate_indname_inc(void) {
274 nghttp2_hd_inflater inflater;
277 nghttp2_nv nv = MAKE_NV("user-agent", "nghttp2");
281 mem = nghttp2_mem_default();
282 frame_pack_bufs_init(&bufs);
285 nghttp2_hd_inflate_init(&inflater, mem);
287 CU_ASSERT(0 == nghttp2_hd_emit_indname_block(&bufs, 57, &nv,
288 NGHTTP2_HD_WITH_INDEXING));
290 blocklen = nghttp2_bufs_len(&bufs);
292 CU_ASSERT(blocklen > 0);
293 CU_ASSERT(blocklen == inflate_hd(&inflater, &out, &bufs, 0, mem));
295 CU_ASSERT(1 == out.nvlen);
296 assert_nv_equal(&nv, out.nva, 1, mem);
297 CU_ASSERT(1 == inflater.ctx.hd_table.len);
299 &nv, &GET_TABLE_ENT(&inflater.ctx, NGHTTP2_STATIC_TABLE_LENGTH +
300 inflater.ctx.hd_table.len - 1)->nv,
303 nva_out_reset(&out, mem);
304 nghttp2_bufs_free(&bufs);
305 nghttp2_hd_inflate_free(&inflater);
308 void test_nghttp2_hd_inflate_indname_inc_eviction(void) {
309 nghttp2_hd_inflater inflater;
317 mem = nghttp2_mem_default();
318 frame_pack_bufs_init(&bufs);
321 nghttp2_hd_inflate_init(&inflater, mem);
323 memset(value, '0', sizeof(value));
324 value[sizeof(value) - 1] = '\0';
326 nv.valuelen = sizeof(value) - 1;
328 nv.flags = NGHTTP2_NV_FLAG_NONE;
330 CU_ASSERT(0 == nghttp2_hd_emit_indname_block(&bufs, 14, &nv,
331 NGHTTP2_HD_WITH_INDEXING));
332 CU_ASSERT(0 == nghttp2_hd_emit_indname_block(&bufs, 15, &nv,
333 NGHTTP2_HD_WITH_INDEXING));
334 CU_ASSERT(0 == nghttp2_hd_emit_indname_block(&bufs, 16, &nv,
335 NGHTTP2_HD_WITH_INDEXING));
336 CU_ASSERT(0 == nghttp2_hd_emit_indname_block(&bufs, 17, &nv,
337 NGHTTP2_HD_WITH_INDEXING));
339 blocklen = nghttp2_bufs_len(&bufs);
341 CU_ASSERT(blocklen > 0);
343 CU_ASSERT(blocklen == inflate_hd(&inflater, &out, &bufs, 0, mem));
345 CU_ASSERT(4 == out.nvlen);
346 CU_ASSERT(14 == out.nva[0].namelen);
347 CU_ASSERT(0 == memcmp("accept-charset", out.nva[0].name, out.nva[0].namelen));
348 CU_ASSERT(sizeof(value) - 1 == out.nva[0].valuelen);
350 nva_out_reset(&out, mem);
351 nghttp2_bufs_reset(&bufs);
353 CU_ASSERT(3 == inflater.ctx.hd_table.len);
355 nghttp2_bufs_free(&bufs);
356 nghttp2_hd_inflate_free(&inflater);
359 void test_nghttp2_hd_inflate_newname_noinc(void) {
360 nghttp2_hd_inflater inflater;
363 nghttp2_nv nv[] = {/* Expecting huffman for both */
364 MAKE_NV("my-long-content-length", "nghttp2"),
365 /* Expecting no huffman for both */
367 /* Huffman for key only */
368 MAKE_NV("my-long-content-length", "y"),
369 /* Huffman for value only */
370 MAKE_NV("x", "nghttp2")};
375 mem = nghttp2_mem_default();
376 frame_pack_bufs_init(&bufs);
379 nghttp2_hd_inflate_init(&inflater, mem);
380 for (i = 0; i < ARRLEN(nv); ++i) {
381 CU_ASSERT(0 == nghttp2_hd_emit_newname_block(&bufs, &nv[i],
382 NGHTTP2_HD_WITHOUT_INDEXING));
384 blocklen = nghttp2_bufs_len(&bufs);
386 CU_ASSERT(blocklen > 0);
387 CU_ASSERT(blocklen == inflate_hd(&inflater, &out, &bufs, 0, mem));
389 CU_ASSERT(1 == out.nvlen);
390 assert_nv_equal(&nv[i], out.nva, 1, mem);
391 CU_ASSERT(0 == inflater.ctx.hd_table.len);
393 nva_out_reset(&out, mem);
394 nghttp2_bufs_reset(&bufs);
397 nghttp2_bufs_free(&bufs);
398 nghttp2_hd_inflate_free(&inflater);
401 void test_nghttp2_hd_inflate_newname_inc(void) {
402 nghttp2_hd_inflater inflater;
405 nghttp2_nv nv = MAKE_NV("x-rel", "nghttp2");
409 mem = nghttp2_mem_default();
410 frame_pack_bufs_init(&bufs);
413 nghttp2_hd_inflate_init(&inflater, mem);
416 0 == nghttp2_hd_emit_newname_block(&bufs, &nv, NGHTTP2_HD_WITH_INDEXING));
418 blocklen = nghttp2_bufs_len(&bufs);
420 CU_ASSERT(blocklen > 0);
421 CU_ASSERT(blocklen == inflate_hd(&inflater, &out, &bufs, 0, mem));
423 CU_ASSERT(1 == out.nvlen);
424 assert_nv_equal(&nv, out.nva, 1, mem);
425 CU_ASSERT(1 == inflater.ctx.hd_table.len);
427 &nv, &GET_TABLE_ENT(&inflater.ctx, NGHTTP2_STATIC_TABLE_LENGTH +
428 inflater.ctx.hd_table.len - 1)->nv,
431 nva_out_reset(&out, mem);
432 nghttp2_bufs_free(&bufs);
433 nghttp2_hd_inflate_free(&inflater);
436 void test_nghttp2_hd_inflate_clearall_inc(void) {
437 nghttp2_hd_inflater inflater;
445 mem = nghttp2_mem_default();
446 bufs_large_init(&bufs, 8192);
449 /* Total 4097 bytes space required to hold this entry */
450 nv.name = (uint8_t *)"alpha";
451 nv.namelen = strlen((char *)nv.name);
452 memset(value, '0', sizeof(value));
453 value[sizeof(value) - 1] = '\0';
455 nv.valuelen = sizeof(value) - 1;
457 nv.flags = NGHTTP2_NV_FLAG_NONE;
459 nghttp2_hd_inflate_init(&inflater, mem);
462 0 == nghttp2_hd_emit_newname_block(&bufs, &nv, NGHTTP2_HD_WITH_INDEXING));
464 blocklen = nghttp2_bufs_len(&bufs);
466 CU_ASSERT(blocklen > 0);
467 CU_ASSERT(blocklen == inflate_hd(&inflater, &out, &bufs, 0, mem));
469 CU_ASSERT(1 == out.nvlen);
470 assert_nv_equal(&nv, out.nva, 1, mem);
471 CU_ASSERT(0 == inflater.ctx.hd_table.len);
473 nva_out_reset(&out, mem);
476 CU_ASSERT(blocklen == inflate_hd(&inflater, &out, &bufs, 0, mem));
478 CU_ASSERT(1 == out.nvlen);
479 assert_nv_equal(&nv, out.nva, 1, mem);
480 CU_ASSERT(0 == inflater.ctx.hd_table.len);
482 nva_out_reset(&out, mem);
483 nghttp2_bufs_reset(&bufs);
485 /* This time, 4096 bytes space required, which is just fits in the
487 nv.valuelen = sizeof(value) - 2;
490 0 == nghttp2_hd_emit_newname_block(&bufs, &nv, NGHTTP2_HD_WITH_INDEXING));
492 blocklen = nghttp2_bufs_len(&bufs);
494 CU_ASSERT(blocklen > 0);
495 CU_ASSERT(blocklen == inflate_hd(&inflater, &out, &bufs, 0, mem));
497 CU_ASSERT(1 == out.nvlen);
498 assert_nv_equal(&nv, out.nva, 1, mem);
499 CU_ASSERT(1 == inflater.ctx.hd_table.len);
501 nva_out_reset(&out, mem);
502 nghttp2_bufs_reset(&bufs);
504 nghttp2_bufs_free(&bufs);
505 nghttp2_hd_inflate_free(&inflater);
508 void test_nghttp2_hd_inflate_zero_length_huffman(void) {
509 nghttp2_hd_inflater inflater;
511 /* Literal header without indexing - new name */
512 uint8_t data[] = {0x40, 0x01, 0x78 /* 'x' */, 0x80};
516 mem = nghttp2_mem_default();
517 frame_pack_bufs_init(&bufs);
521 nghttp2_bufs_add(&bufs, data, sizeof(data));
523 /* /\* Literal header without indexing - new name *\/ */
529 nghttp2_hd_inflate_init(&inflater, mem);
530 CU_ASSERT(4 == inflate_hd(&inflater, &out, &bufs, 0, mem));
532 CU_ASSERT(1 == out.nvlen);
533 CU_ASSERT(1 == out.nva[0].namelen);
534 CU_ASSERT('x' == out.nva[0].name[0]);
535 CU_ASSERT(NULL == out.nva[0].value);
536 CU_ASSERT(0 == out.nva[0].valuelen);
538 nva_out_reset(&out, mem);
539 nghttp2_bufs_free(&bufs);
540 nghttp2_hd_inflate_free(&inflater);
543 void test_nghttp2_hd_ringbuf_reserve(void) {
544 nghttp2_hd_deflater deflater;
545 nghttp2_hd_inflater inflater;
554 mem = nghttp2_mem_default();
555 frame_pack_bufs_init(&bufs);
558 nv.flags = NGHTTP2_NV_FLAG_NONE;
559 nv.name = (uint8_t *)"a";
560 nv.namelen = strlen((const char *)nv.name);
562 nv.value = mem->malloc(nv.valuelen + 1, NULL);
563 memset(nv.value, 0, nv.valuelen);
565 nghttp2_hd_deflate_init2(&deflater, 8000, mem);
566 nghttp2_hd_inflate_init(&inflater, mem);
568 nghttp2_hd_inflate_change_table_size(&inflater, 8000);
569 nghttp2_hd_deflate_change_table_size(&deflater, 8000);
571 for (i = 0; i < 150; ++i) {
572 memcpy(nv.value, &i, sizeof(i));
573 rv = nghttp2_hd_deflate_hd_bufs(&deflater, &bufs, &nv, 1);
574 blocklen = nghttp2_bufs_len(&bufs);
577 CU_ASSERT(blocklen > 0);
579 CU_ASSERT(blocklen == inflate_hd(&inflater, &out, &bufs, 0, mem));
581 CU_ASSERT(1 == out.nvlen);
582 assert_nv_equal(&nv, out.nva, 1, mem);
584 nva_out_reset(&out, mem);
585 nghttp2_bufs_reset(&bufs);
588 nghttp2_bufs_free(&bufs);
589 nghttp2_hd_inflate_free(&inflater);
590 nghttp2_hd_deflate_free(&deflater);
592 mem->free(nv.value, NULL);
595 void test_nghttp2_hd_change_table_size(void) {
596 nghttp2_hd_deflater deflater;
597 nghttp2_hd_inflater inflater;
598 nghttp2_nv nva[] = {MAKE_NV("alpha", "bravo"), MAKE_NV("charlie", "delta")};
599 nghttp2_nv nva2[] = {MAKE_NV(":path", "/")};
606 mem = nghttp2_mem_default();
607 frame_pack_bufs_init(&bufs);
611 nghttp2_hd_deflate_init(&deflater, mem);
612 nghttp2_hd_inflate_init(&inflater, mem);
614 /* inflater changes notifies 8000 max header table size */
615 CU_ASSERT(0 == nghttp2_hd_inflate_change_table_size(&inflater, 8000));
616 CU_ASSERT(0 == nghttp2_hd_deflate_change_table_size(&deflater, 8000));
618 CU_ASSERT(4096 == deflater.ctx.hd_table_bufsize_max);
620 CU_ASSERT(8000 == inflater.ctx.hd_table_bufsize_max);
621 CU_ASSERT(8000 == inflater.settings_hd_table_bufsize_max);
623 /* This will emit encoding context update with header table size 4096 */
624 rv = nghttp2_hd_deflate_hd_bufs(&deflater, &bufs, nva, 2);
625 blocklen = nghttp2_bufs_len(&bufs);
628 CU_ASSERT(blocklen > 0);
629 CU_ASSERT(2 == deflater.ctx.hd_table.len);
630 CU_ASSERT(4096 == deflater.ctx.hd_table_bufsize_max);
632 CU_ASSERT(blocklen == inflate_hd(&inflater, &out, &bufs, 0, mem));
633 CU_ASSERT(2 == inflater.ctx.hd_table.len);
634 CU_ASSERT(4096 == inflater.ctx.hd_table_bufsize_max);
635 CU_ASSERT(8000 == inflater.settings_hd_table_bufsize_max);
637 nva_out_reset(&out, mem);
638 nghttp2_bufs_reset(&bufs);
640 /* inflater changes header table size to 1024 */
641 CU_ASSERT(0 == nghttp2_hd_inflate_change_table_size(&inflater, 1024));
642 CU_ASSERT(0 == nghttp2_hd_deflate_change_table_size(&deflater, 1024));
644 CU_ASSERT(1024 == deflater.ctx.hd_table_bufsize_max);
646 CU_ASSERT(1024 == inflater.ctx.hd_table_bufsize_max);
647 CU_ASSERT(1024 == inflater.settings_hd_table_bufsize_max);
649 rv = nghttp2_hd_deflate_hd_bufs(&deflater, &bufs, nva, 2);
650 blocklen = nghttp2_bufs_len(&bufs);
653 CU_ASSERT(blocklen > 0);
654 CU_ASSERT(2 == deflater.ctx.hd_table.len);
655 CU_ASSERT(1024 == deflater.ctx.hd_table_bufsize_max);
657 CU_ASSERT(blocklen == inflate_hd(&inflater, &out, &bufs, 0, mem));
658 CU_ASSERT(2 == inflater.ctx.hd_table.len);
659 CU_ASSERT(1024 == inflater.ctx.hd_table_bufsize_max);
660 CU_ASSERT(1024 == inflater.settings_hd_table_bufsize_max);
662 nva_out_reset(&out, mem);
663 nghttp2_bufs_reset(&bufs);
665 /* inflater changes header table size to 0 */
666 CU_ASSERT(0 == nghttp2_hd_inflate_change_table_size(&inflater, 0));
667 CU_ASSERT(0 == nghttp2_hd_deflate_change_table_size(&deflater, 0));
669 CU_ASSERT(0 == deflater.ctx.hd_table.len);
670 CU_ASSERT(0 == deflater.ctx.hd_table_bufsize_max);
672 CU_ASSERT(0 == inflater.ctx.hd_table.len);
673 CU_ASSERT(0 == inflater.ctx.hd_table_bufsize_max);
674 CU_ASSERT(0 == inflater.settings_hd_table_bufsize_max);
676 rv = nghttp2_hd_deflate_hd_bufs(&deflater, &bufs, nva, 2);
677 blocklen = nghttp2_bufs_len(&bufs);
680 CU_ASSERT(blocklen > 0);
681 CU_ASSERT(0 == deflater.ctx.hd_table.len);
682 CU_ASSERT(0 == deflater.ctx.hd_table_bufsize_max);
684 CU_ASSERT(blocklen == inflate_hd(&inflater, &out, &bufs, 0, mem));
685 CU_ASSERT(0 == inflater.ctx.hd_table.len);
686 CU_ASSERT(0 == inflater.ctx.hd_table_bufsize_max);
687 CU_ASSERT(0 == inflater.settings_hd_table_bufsize_max);
689 nva_out_reset(&out, mem);
690 nghttp2_bufs_reset(&bufs);
692 nghttp2_bufs_free(&bufs);
693 nghttp2_hd_inflate_free(&inflater);
694 nghttp2_hd_deflate_free(&deflater);
696 /* Check table buffer is expanded */
697 frame_pack_bufs_init(&bufs);
699 nghttp2_hd_deflate_init2(&deflater, 8192, mem);
700 nghttp2_hd_inflate_init(&inflater, mem);
702 /* First inflater changes header table size to 8000 */
703 CU_ASSERT(0 == nghttp2_hd_inflate_change_table_size(&inflater, 8000));
704 CU_ASSERT(0 == nghttp2_hd_deflate_change_table_size(&deflater, 8000));
706 CU_ASSERT(8000 == deflater.ctx.hd_table_bufsize_max);
708 CU_ASSERT(8000 == inflater.ctx.hd_table_bufsize_max);
709 CU_ASSERT(8000 == inflater.settings_hd_table_bufsize_max);
711 rv = nghttp2_hd_deflate_hd_bufs(&deflater, &bufs, nva, 2);
712 blocklen = nghttp2_bufs_len(&bufs);
715 CU_ASSERT(blocklen > 0);
716 CU_ASSERT(2 == deflater.ctx.hd_table.len);
717 CU_ASSERT(8000 == deflater.ctx.hd_table_bufsize_max);
719 CU_ASSERT(blocklen == inflate_hd(&inflater, &out, &bufs, 0, mem));
720 CU_ASSERT(2 == inflater.ctx.hd_table.len);
721 CU_ASSERT(8000 == inflater.ctx.hd_table_bufsize_max);
722 CU_ASSERT(8000 == inflater.settings_hd_table_bufsize_max);
724 nva_out_reset(&out, mem);
725 nghttp2_bufs_reset(&bufs);
727 CU_ASSERT(0 == nghttp2_hd_inflate_change_table_size(&inflater, 16383));
728 CU_ASSERT(0 == nghttp2_hd_deflate_change_table_size(&deflater, 16383));
730 CU_ASSERT(8192 == deflater.ctx.hd_table_bufsize_max);
732 CU_ASSERT(16383 == inflater.ctx.hd_table_bufsize_max);
733 CU_ASSERT(16383 == inflater.settings_hd_table_bufsize_max);
735 rv = nghttp2_hd_deflate_hd_bufs(&deflater, &bufs, nva, 2);
736 blocklen = nghttp2_bufs_len(&bufs);
739 CU_ASSERT(blocklen > 0);
740 CU_ASSERT(2 == deflater.ctx.hd_table.len);
741 CU_ASSERT(8192 == deflater.ctx.hd_table_bufsize_max);
743 CU_ASSERT(blocklen == inflate_hd(&inflater, &out, &bufs, 0, mem));
744 CU_ASSERT(2 == inflater.ctx.hd_table.len);
745 CU_ASSERT(8192 == inflater.ctx.hd_table_bufsize_max);
746 CU_ASSERT(16383 == inflater.settings_hd_table_bufsize_max);
748 nva_out_reset(&out, mem);
749 nghttp2_bufs_reset(&bufs);
751 /* Lastly, check the error condition */
753 rv = nghttp2_hd_emit_table_size(&bufs, 25600);
755 CU_ASSERT(NGHTTP2_ERR_HEADER_COMP ==
756 inflate_hd(&inflater, &out, &bufs, 0, mem));
758 nva_out_reset(&out, mem);
759 nghttp2_bufs_reset(&bufs);
761 nghttp2_hd_inflate_free(&inflater);
762 nghttp2_hd_deflate_free(&deflater);
764 /* Check that encoder can handle the case where its allowable buffer
765 size is less than default size, 4096 */
766 nghttp2_hd_deflate_init2(&deflater, 1024, mem);
767 nghttp2_hd_inflate_init(&inflater, mem);
769 CU_ASSERT(1024 == deflater.ctx.hd_table_bufsize_max);
771 /* This emits context update with buffer size 1024 */
772 rv = nghttp2_hd_deflate_hd_bufs(&deflater, &bufs, nva, 2);
773 blocklen = nghttp2_bufs_len(&bufs);
776 CU_ASSERT(blocklen > 0);
777 CU_ASSERT(2 == deflater.ctx.hd_table.len);
778 CU_ASSERT(1024 == deflater.ctx.hd_table_bufsize_max);
780 CU_ASSERT(blocklen == inflate_hd(&inflater, &out, &bufs, 0, mem));
781 CU_ASSERT(2 == inflater.ctx.hd_table.len);
782 CU_ASSERT(1024 == inflater.ctx.hd_table_bufsize_max);
783 CU_ASSERT(4096 == inflater.settings_hd_table_bufsize_max);
785 nva_out_reset(&out, mem);
786 nghttp2_bufs_reset(&bufs);
788 nghttp2_hd_inflate_free(&inflater);
789 nghttp2_hd_deflate_free(&deflater);
791 /* Check that table size UINT32_MAX can be received */
792 nghttp2_hd_deflate_init2(&deflater, UINT32_MAX, mem);
793 nghttp2_hd_inflate_init(&inflater, mem);
795 CU_ASSERT(0 == nghttp2_hd_inflate_change_table_size(&inflater, UINT32_MAX));
796 CU_ASSERT(0 == nghttp2_hd_deflate_change_table_size(&deflater, UINT32_MAX));
798 rv = nghttp2_hd_deflate_hd_bufs(&deflater, &bufs, nva, 2);
799 blocklen = nghttp2_bufs_len(&bufs);
802 CU_ASSERT(UINT32_MAX == deflater.ctx.hd_table_bufsize_max);
804 CU_ASSERT(blocklen == inflate_hd(&inflater, &out, &bufs, 0, mem));
805 CU_ASSERT(UINT32_MAX == inflater.ctx.hd_table_bufsize_max);
806 CU_ASSERT(UINT32_MAX == inflater.settings_hd_table_bufsize_max);
808 nva_out_reset(&out, mem);
809 nghttp2_bufs_reset(&bufs);
811 nghttp2_hd_inflate_free(&inflater);
812 nghttp2_hd_deflate_free(&deflater);
814 /* Check that context update emitted twice */
815 nghttp2_hd_deflate_init2(&deflater, 4096, mem);
816 nghttp2_hd_inflate_init(&inflater, mem);
818 CU_ASSERT(0 == nghttp2_hd_inflate_change_table_size(&inflater, 0));
819 CU_ASSERT(0 == nghttp2_hd_inflate_change_table_size(&inflater, 3000));
820 CU_ASSERT(0 == nghttp2_hd_deflate_change_table_size(&deflater, 0));
821 CU_ASSERT(0 == nghttp2_hd_deflate_change_table_size(&deflater, 3000));
823 CU_ASSERT(0 == deflater.min_hd_table_bufsize_max);
824 CU_ASSERT(3000 == deflater.ctx.hd_table_bufsize_max);
826 rv = nghttp2_hd_deflate_hd_bufs(&deflater, &bufs, nva2, 1);
827 blocklen = nghttp2_bufs_len(&bufs);
830 CU_ASSERT(3 < blocklen);
831 CU_ASSERT(3000 == deflater.ctx.hd_table_bufsize_max);
832 CU_ASSERT(UINT32_MAX == deflater.min_hd_table_bufsize_max);
834 CU_ASSERT(blocklen == inflate_hd(&inflater, &out, &bufs, 0, mem));
835 CU_ASSERT(3000 == inflater.ctx.hd_table_bufsize_max);
836 CU_ASSERT(3000 == inflater.settings_hd_table_bufsize_max);
838 nva_out_reset(&out, mem);
839 nghttp2_bufs_reset(&bufs);
841 nghttp2_hd_inflate_free(&inflater);
842 nghttp2_hd_deflate_free(&deflater);
844 nghttp2_bufs_free(&bufs);
847 static void check_deflate_inflate(nghttp2_hd_deflater *deflater,
848 nghttp2_hd_inflater *inflater,
849 nghttp2_nv *nva, size_t nvlen,
856 frame_pack_bufs_init(&bufs);
859 rv = nghttp2_hd_deflate_hd_bufs(deflater, &bufs, nva, nvlen);
860 blocklen = nghttp2_bufs_len(&bufs);
863 CU_ASSERT(blocklen >= 0);
865 CU_ASSERT(blocklen == inflate_hd(inflater, &out, &bufs, 0, mem));
867 CU_ASSERT(nvlen == out.nvlen);
868 assert_nv_equal(nva, out.nva, nvlen, mem);
870 nva_out_reset(&out, mem);
871 nghttp2_bufs_free(&bufs);
874 void test_nghttp2_hd_deflate_inflate(void) {
875 nghttp2_hd_deflater deflater;
876 nghttp2_hd_inflater inflater;
878 MAKE_NV(":status", "200 OK"),
879 MAKE_NV("access-control-allow-origin", "*"),
880 MAKE_NV("cache-control", "private, max-age=0, must-revalidate"),
881 MAKE_NV("content-length", "76073"),
882 MAKE_NV("content-type", "text/html"),
883 MAKE_NV("date", "Sat, 27 Jul 2013 06:22:12 GMT"),
884 MAKE_NV("expires", "Sat, 27 Jul 2013 06:22:12 GMT"),
885 MAKE_NV("server", "Apache"),
886 MAKE_NV("vary", "foobar"),
887 MAKE_NV("via", "1.1 alphabravo (squid/3.x.x), 1.1 nghttpx"),
888 MAKE_NV("x-cache", "MISS from alphabravo"),
889 MAKE_NV("x-cache-action", "MISS"),
890 MAKE_NV("x-cache-age", "0"),
891 MAKE_NV("x-cache-lookup", "MISS from alphabravo:3128"),
892 MAKE_NV("x-lb-nocache", "true"),
895 MAKE_NV(":status", "304 Not Modified"),
897 MAKE_NV("cache-control", "max-age=56682045"),
898 MAKE_NV("content-type", "text/css"),
899 MAKE_NV("date", "Sat, 27 Jul 2013 06:22:12 GMT"),
900 MAKE_NV("expires", "Thu, 14 May 2015 07:22:57 GMT"),
901 MAKE_NV("last-modified", "Tue, 14 May 2013 07:22:15 GMT"),
902 MAKE_NV("vary", "Accept-Encoding"),
903 MAKE_NV("via", "1.1 alphabravo (squid/3.x.x), 1.1 nghttpx"),
904 MAKE_NV("x-cache", "HIT from alphabravo"),
905 MAKE_NV("x-cache-lookup", "HIT from alphabravo:3128")};
907 MAKE_NV(":status", "304 Not Modified"),
909 MAKE_NV("cache-control", "max-age=56682072"),
910 MAKE_NV("content-type", "text/css"),
911 MAKE_NV("date", "Sat, 27 Jul 2013 06:22:12 GMT"),
912 MAKE_NV("expires", "Thu, 14 May 2015 07:23:24 GMT"),
913 MAKE_NV("last-modified", "Tue, 14 May 2013 07:22:13 GMT"),
914 MAKE_NV("vary", "Accept-Encoding"),
915 MAKE_NV("via", "1.1 alphabravo (squid/3.x.x), 1.1 nghttpx"),
916 MAKE_NV("x-cache", "HIT from alphabravo"),
917 MAKE_NV("x-cache-lookup", "HIT from alphabravo:3128"),
920 MAKE_NV(":status", "304 Not Modified"),
922 MAKE_NV("cache-control", "max-age=56682022"),
923 MAKE_NV("content-type", "text/css"),
924 MAKE_NV("date", "Sat, 27 Jul 2013 06:22:12 GMT"),
925 MAKE_NV("expires", "Thu, 14 May 2015 07:22:34 GMT"),
926 MAKE_NV("last-modified", "Tue, 14 May 2013 07:22:14 GMT"),
927 MAKE_NV("vary", "Accept-Encoding"),
928 MAKE_NV("via", "1.1 alphabravo (squid/3.x.x), 1.1 nghttpx"),
929 MAKE_NV("x-cache", "HIT from alphabravo"),
930 MAKE_NV("x-cache-lookup", "HIT from alphabravo:3128"),
933 MAKE_NV(":status", "304 Not Modified"),
935 MAKE_NV("cache-control", "max-age=4461139"),
936 MAKE_NV("content-type", "application/x-javascript"),
937 MAKE_NV("date", "Sat, 27 Jul 2013 06:22:12 GMT"),
938 MAKE_NV("expires", "Mon, 16 Sep 2013 21:34:31 GMT"),
939 MAKE_NV("last-modified", "Thu, 05 May 2011 09:15:59 GMT"),
940 MAKE_NV("vary", "Accept-Encoding"),
941 MAKE_NV("via", "1.1 alphabravo (squid/3.x.x), 1.1 nghttpx"),
942 MAKE_NV("x-cache", "HIT from alphabravo"),
943 MAKE_NV("x-cache-lookup", "HIT from alphabravo:3128"),
946 MAKE_NV(":status", "304 Not Modified"),
948 MAKE_NV("cache-control", "max-age=18645951"),
949 MAKE_NV("content-type", "application/x-javascript"),
950 MAKE_NV("date", "Sat, 27 Jul 2013 06:22:12 GMT"),
951 MAKE_NV("expires", "Fri, 28 Feb 2014 01:48:03 GMT"),
952 MAKE_NV("last-modified", "Tue, 12 Jul 2011 16:02:59 GMT"),
953 MAKE_NV("vary", "Accept-Encoding"),
954 MAKE_NV("via", "1.1 alphabravo (squid/3.x.x), 1.1 nghttpx"),
955 MAKE_NV("x-cache", "HIT from alphabravo"),
956 MAKE_NV("x-cache-lookup", "HIT from alphabravo:3128"),
959 MAKE_NV(":status", "304 Not Modified"),
961 MAKE_NV("cache-control", "max-age=31536000"),
962 MAKE_NV("content-type", "application/javascript"),
963 MAKE_NV("date", "Sat, 27 Jul 2013 06:22:12 GMT"),
964 MAKE_NV("etag", "\"6807-4dc5b54e0dcc0\""),
965 MAKE_NV("expires", "Wed, 21 May 2014 08:32:17 GMT"),
966 MAKE_NV("last-modified", "Fri, 10 May 2013 11:18:51 GMT"),
967 MAKE_NV("via", "1.1 alphabravo (squid/3.x.x), 1.1 nghttpx"),
968 MAKE_NV("x-cache", "HIT from alphabravo"),
969 MAKE_NV("x-cache-lookup", "HIT from alphabravo:3128"),
972 MAKE_NV(":status", "304 Not Modified"),
974 MAKE_NV("cache-control", "max-age=31536000"),
975 MAKE_NV("content-type", "application/javascript"),
976 MAKE_NV("date", "Sat, 27 Jul 2013 06:22:12 GMT"),
977 MAKE_NV("etag", "\"41c6-4de7d28585b00\""),
978 MAKE_NV("expires", "Thu, 12 Jun 2014 10:00:58 GMT"),
979 MAKE_NV("last-modified", "Thu, 06 Jun 2013 14:30:36 GMT"),
980 MAKE_NV("via", "1.1 alphabravo (squid/3.x.x), 1.1 nghttpx"),
981 MAKE_NV("x-cache", "HIT from alphabravo"),
982 MAKE_NV("x-cache-lookup", "HIT from alphabravo:3128"),
985 MAKE_NV(":status", "304 Not Modified"),
987 MAKE_NV("cache-control", "max-age=31536000"),
988 MAKE_NV("content-type", "application/javascript"),
989 MAKE_NV("date", "Sat, 27 Jul 2013 06:22:12 GMT"),
990 MAKE_NV("etag", "\"19d6e-4dc5b35a541c0\""),
991 MAKE_NV("expires", "Wed, 21 May 2014 08:32:18 GMT"),
992 MAKE_NV("last-modified", "Fri, 10 May 2013 11:10:07 GMT"),
993 MAKE_NV("via", "1.1 alphabravo (squid/3.x.x), 1.1 nghttpx"),
994 MAKE_NV("x-cache", "HIT from alphabravo"),
995 MAKE_NV("x-cache-lookup", "HIT from alphabravo:3128"),
997 nghttp2_nv nv10[] = {
998 MAKE_NV(":status", "304 Not Modified"),
1000 MAKE_NV("cache-control", "max-age=56682045"),
1001 MAKE_NV("content-type", "text/css"),
1002 MAKE_NV("date", "Sat, 27 Jul 2013 06:22:12 GMT"),
1003 MAKE_NV("expires", "Thu, 14 May 2015 07:22:57 GMT"),
1004 MAKE_NV("last-modified", "Tue, 14 May 2013 07:21:53 GMT"),
1005 MAKE_NV("vary", "Accept-Encoding"),
1006 MAKE_NV("via", "1.1 alphabravo (squid/3.x.x), 1.1 nghttpx"),
1007 MAKE_NV("x-cache", "HIT from alphabravo"),
1008 MAKE_NV("x-cache-lookup", "HIT from alphabravo:3128"),
1012 mem = nghttp2_mem_default();
1014 nghttp2_hd_deflate_init(&deflater, mem);
1015 nghttp2_hd_inflate_init(&inflater, mem);
1017 check_deflate_inflate(&deflater, &inflater, nv1, ARRLEN(nv1), mem);
1018 check_deflate_inflate(&deflater, &inflater, nv2, ARRLEN(nv2), mem);
1019 check_deflate_inflate(&deflater, &inflater, nv3, ARRLEN(nv3), mem);
1020 check_deflate_inflate(&deflater, &inflater, nv4, ARRLEN(nv4), mem);
1021 check_deflate_inflate(&deflater, &inflater, nv5, ARRLEN(nv5), mem);
1022 check_deflate_inflate(&deflater, &inflater, nv6, ARRLEN(nv6), mem);
1023 check_deflate_inflate(&deflater, &inflater, nv7, ARRLEN(nv7), mem);
1024 check_deflate_inflate(&deflater, &inflater, nv8, ARRLEN(nv8), mem);
1025 check_deflate_inflate(&deflater, &inflater, nv9, ARRLEN(nv9), mem);
1026 check_deflate_inflate(&deflater, &inflater, nv10, ARRLEN(nv10), mem);
1028 nghttp2_hd_inflate_free(&inflater);
1029 nghttp2_hd_deflate_free(&deflater);
1032 void test_nghttp2_hd_no_index(void) {
1033 nghttp2_hd_deflater deflater;
1034 nghttp2_hd_inflater inflater;
1037 nghttp2_nv nva[] = {
1038 MAKE_NV(":method", "GET"), MAKE_NV(":method", "POST"),
1039 MAKE_NV(":path", "/foo"), MAKE_NV("version", "HTTP/1.1"),
1040 MAKE_NV(":method", "GET"),
1047 mem = nghttp2_mem_default();
1049 /* 1st :method: GET can be indexable, last one is not */
1050 for (i = 1; i < ARRLEN(nva); ++i) {
1051 nva[i].flags = NGHTTP2_NV_FLAG_NO_INDEX;
1054 frame_pack_bufs_init(&bufs);
1058 nghttp2_hd_deflate_init(&deflater, mem);
1059 nghttp2_hd_inflate_init(&inflater, mem);
1061 rv = nghttp2_hd_deflate_hd_bufs(&deflater, &bufs, nva, ARRLEN(nva));
1062 blocklen = nghttp2_bufs_len(&bufs);
1065 CU_ASSERT(blocklen > 0);
1066 CU_ASSERT(blocklen == inflate_hd(&inflater, &out, &bufs, 0, mem));
1068 CU_ASSERT(ARRLEN(nva) == out.nvlen);
1069 assert_nv_equal(nva, out.nva, ARRLEN(nva), mem);
1071 CU_ASSERT(out.nva[0].flags == NGHTTP2_NV_FLAG_NONE);
1072 for (i = 1; i < ARRLEN(nva); ++i) {
1073 CU_ASSERT(out.nva[i].flags == NGHTTP2_NV_FLAG_NO_INDEX);
1076 nva_out_reset(&out, mem);
1078 nghttp2_bufs_free(&bufs);
1079 nghttp2_hd_inflate_free(&inflater);
1080 nghttp2_hd_deflate_free(&deflater);
1083 void test_nghttp2_hd_deflate_bound(void) {
1084 nghttp2_hd_deflater deflater;
1085 nghttp2_nv nva[] = {MAKE_NV(":method", "GET"), MAKE_NV("alpha", "bravo")};
1087 size_t bound, bound2;
1090 mem = nghttp2_mem_default();
1091 frame_pack_bufs_init(&bufs);
1093 nghttp2_hd_deflate_init(&deflater, mem);
1095 bound = nghttp2_hd_deflate_bound(&deflater, nva, ARRLEN(nva));
1097 CU_ASSERT(12 + 6 * 2 * 2 + nva[0].namelen + nva[0].valuelen + nva[1].namelen +
1101 nghttp2_hd_deflate_hd_bufs(&deflater, &bufs, nva, ARRLEN(nva));
1103 CU_ASSERT(bound > (size_t)nghttp2_bufs_len(&bufs));
1105 bound2 = nghttp2_hd_deflate_bound(&deflater, nva, ARRLEN(nva));
1107 CU_ASSERT(bound == bound2);
1109 nghttp2_bufs_free(&bufs);
1110 nghttp2_hd_deflate_free(&deflater);
1113 void test_nghttp2_hd_public_api(void) {
1114 nghttp2_hd_deflater *deflater;
1115 nghttp2_hd_inflater *inflater;
1116 nghttp2_nv nva[] = {MAKE_NV("alpha", "bravo"), MAKE_NV("charlie", "delta")};
1123 mem = nghttp2_mem_default();
1125 CU_ASSERT(0 == nghttp2_hd_deflate_new(&deflater, 4096));
1126 CU_ASSERT(0 == nghttp2_hd_inflate_new(&inflater));
1128 buflen = nghttp2_hd_deflate_bound(deflater, nva, ARRLEN(nva));
1130 blocklen = nghttp2_hd_deflate_hd(deflater, buf, buflen, nva, ARRLEN(nva));
1132 CU_ASSERT(blocklen > 0);
1134 nghttp2_bufs_wrap_init(&bufs, buf, blocklen, mem);
1135 bufs.head->buf.last += blocklen;
1137 CU_ASSERT(blocklen == inflate_hd(inflater, NULL, &bufs, 0, mem));
1139 nghttp2_bufs_wrap_free(&bufs);
1141 nghttp2_hd_inflate_del(inflater);
1142 nghttp2_hd_deflate_del(deflater);
1144 /* See NGHTTP2_ERR_INSUFF_BUFSIZE */
1145 CU_ASSERT(0 == nghttp2_hd_deflate_new(&deflater, 4096));
1148 nghttp2_hd_deflate_hd(deflater, buf, blocklen - 1, nva, ARRLEN(nva));
1150 CU_ASSERT(NGHTTP2_ERR_INSUFF_BUFSIZE == blocklen);
1152 nghttp2_hd_deflate_del(deflater);
1155 static size_t encode_length(uint8_t *buf, uint64_t n, size_t prefix) {
1156 size_t k = (1 << prefix) - 1;
1170 *buf++ = (1 << 7) | (n & 0x7f);
1173 *buf++ = (uint8_t)n;
1180 void test_nghttp2_hd_decode_length(void) {
1190 memset(buf, 0, sizeof(buf));
1191 len = encode_length(buf, UINT32_MAX, 7);
1193 rv = nghttp2_hd_decode_length(&out, &shift, &final, 0, 0, buf, buf + len, 7);
1195 CU_ASSERT((ssize_t)len == rv);
1196 CU_ASSERT(0 != final);
1197 CU_ASSERT(UINT32_MAX == out);
1199 /* Make sure that we can decode integer if we feed 1 byte at a
1206 for (i = 0; i < len; ++i, ++bufp) {
1207 rv = nghttp2_hd_decode_length(&out, &shift, &final, out, shift, bufp,
1217 CU_ASSERT(i == len - 1);
1218 CU_ASSERT(0 != final);
1219 CU_ASSERT(UINT32_MAX == out);
1221 /* Check overflow case */
1222 memset(buf, 0, sizeof(buf));
1223 len = encode_length(buf, 1ll << 32, 7);
1225 rv = nghttp2_hd_decode_length(&out, &shift, &final, 0, 0, buf, buf + len, 7);
1227 CU_ASSERT(-1 == rv);
1230 void test_nghttp2_hd_huff_encode(void) {
1233 nghttp2_bufs bufs, outbufs;
1234 nghttp2_hd_huff_decode_context ctx;
1235 const uint8_t t1[] = {22, 21, 20, 19, 18, 17, 16, 15, 14, 13, 12, 11,
1236 10, 9, 8, 7, 6, 5, 4, 3, 2, 1, 0};
1238 frame_pack_bufs_init(&bufs);
1239 frame_pack_bufs_init(&outbufs);
1241 rv = nghttp2_hd_huff_encode(&bufs, t1, sizeof(t1));
1245 nghttp2_hd_huff_decode_context_init(&ctx);
1247 len = nghttp2_hd_huff_decode(&ctx, &outbufs, bufs.cur->buf.pos,
1248 nghttp2_bufs_len(&bufs), 1);
1250 CU_ASSERT(nghttp2_bufs_len(&bufs) == len);
1251 CU_ASSERT((ssize_t)sizeof(t1) == nghttp2_bufs_len(&outbufs));
1253 CU_ASSERT(0 == memcmp(t1, outbufs.cur->buf.pos, sizeof(t1)));
1255 nghttp2_bufs_free(&bufs);
1256 nghttp2_bufs_free(&outbufs);