1 /*
2 * nghttp2 - HTTP/2 C Library
3 *
4 * Copyright (c) 2012 Tatsuhiro Tsujikawa
5 *
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:
13 *
14 * The above copyright notice and this permission notice shall be
15 * included in all copies or substantial portions of the Software.
16 *
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.
24 */
25 #include "nghttp2_frame_test.h"
26
27 #include <assert.h>
28 #include <stdio.h>
29
30 #include <CUnit/CUnit.h>
31
32 #include "nghttp2_frame.h"
33 #include "nghttp2_helper.h"
34 #include "nghttp2_test_helper.h"
35 #include "nghttp2_priority_spec.h"
36
make_nv(const char * name,const char * value)37 static nghttp2_nv make_nv(const char *name, const char *value) {
38 nghttp2_nv nv;
39 nv.name = (uint8_t *)name;
40 nv.value = (uint8_t *)value;
41 nv.namelen = strlen(name);
42 nv.valuelen = strlen(value);
43 nv.flags = NGHTTP2_NV_FLAG_NONE;
44
45 return nv;
46 }
47
48 #define HEADERS_LENGTH 7
49
headers(nghttp2_mem * mem)50 static nghttp2_nv *headers(nghttp2_mem *mem) {
51 nghttp2_nv *nva = mem->malloc(sizeof(nghttp2_nv) * HEADERS_LENGTH, NULL);
52 nva[0] = make_nv("method", "GET");
53 nva[1] = make_nv("scheme", "https");
54 nva[2] = make_nv("url", "/");
55 nva[3] = make_nv("x-head", "foo");
56 nva[4] = make_nv("x-head", "bar");
57 nva[5] = make_nv("version", "HTTP/1.1");
58 nva[6] = make_nv("x-empty", "");
59 return nva;
60 }
61
check_frame_header(size_t length,uint8_t type,uint8_t flags,int32_t stream_id,nghttp2_frame_hd * hd)62 static void check_frame_header(size_t length, uint8_t type, uint8_t flags,
63 int32_t stream_id, nghttp2_frame_hd *hd) {
64 CU_ASSERT(length == hd->length);
65 CU_ASSERT(type == hd->type);
66 CU_ASSERT(flags == hd->flags);
67 CU_ASSERT(stream_id == hd->stream_id);
68 CU_ASSERT(0 == hd->reserved);
69 }
70
test_nghttp2_frame_pack_headers(void)71 void test_nghttp2_frame_pack_headers(void) {
72 nghttp2_hd_deflater deflater;
73 nghttp2_hd_inflater inflater;
74 nghttp2_headers frame, oframe;
75 nghttp2_bufs bufs;
76 nghttp2_nv *nva;
77 nghttp2_priority_spec pri_spec;
78 size_t nvlen;
79 nva_out out;
80 size_t hdblocklen;
81 int rv;
82 nghttp2_mem *mem;
83
84 mem = nghttp2_mem_default();
85 frame_pack_bufs_init(&bufs);
86
87 nva_out_init(&out);
88 nghttp2_hd_deflate_init(&deflater, mem);
89 nghttp2_hd_inflate_init(&inflater, mem);
90
91 nva = headers(mem);
92 nvlen = HEADERS_LENGTH;
93
94 nghttp2_priority_spec_default_init(&pri_spec);
95
96 nghttp2_frame_headers_init(
97 &frame, NGHTTP2_FLAG_END_STREAM | NGHTTP2_FLAG_END_HEADERS, 1000000007,
98 NGHTTP2_HCAT_REQUEST, &pri_spec, nva, nvlen);
99 rv = nghttp2_frame_pack_headers(&bufs, &frame, &deflater);
100
101 nghttp2_bufs_rewind(&bufs);
102
103 CU_ASSERT(0 == rv);
104 CU_ASSERT(nghttp2_bufs_len(&bufs) > 0);
105 CU_ASSERT(0 == unpack_framebuf((nghttp2_frame *)&oframe, &bufs));
106
107 check_frame_header(nghttp2_bufs_len(&bufs) - NGHTTP2_FRAME_HDLEN,
108 NGHTTP2_HEADERS,
109 NGHTTP2_FLAG_END_STREAM | NGHTTP2_FLAG_END_HEADERS,
110 1000000007, &oframe.hd);
111 /* We did not include PRIORITY flag */
112 CU_ASSERT(NGHTTP2_DEFAULT_WEIGHT == oframe.pri_spec.weight);
113
114 hdblocklen = nghttp2_bufs_len(&bufs) - NGHTTP2_FRAME_HDLEN;
115 CU_ASSERT((ssize_t)hdblocklen ==
116 inflate_hd(&inflater, &out, &bufs, NGHTTP2_FRAME_HDLEN, mem));
117
118 CU_ASSERT(7 == out.nvlen);
119 CU_ASSERT(nvnameeq("method", &out.nva[0]));
120 CU_ASSERT(nvvalueeq("GET", &out.nva[0]));
121
122 nghttp2_frame_headers_free(&oframe, mem);
123 nva_out_reset(&out, mem);
124 nghttp2_bufs_reset(&bufs);
125
126 memset(&oframe, 0, sizeof(oframe));
127 /* Next, include NGHTTP2_FLAG_PRIORITY */
128 nghttp2_priority_spec_init(&frame.pri_spec, 1000000009, 12, 1);
129 frame.hd.flags |= NGHTTP2_FLAG_PRIORITY;
130
131 rv = nghttp2_frame_pack_headers(&bufs, &frame, &deflater);
132
133 CU_ASSERT(0 == rv);
134 CU_ASSERT(nghttp2_bufs_len(&bufs) > 0);
135 CU_ASSERT(0 == unpack_framebuf((nghttp2_frame *)&oframe, &bufs));
136
137 check_frame_header(nghttp2_bufs_len(&bufs) - NGHTTP2_FRAME_HDLEN,
138 NGHTTP2_HEADERS,
139 NGHTTP2_FLAG_END_STREAM | NGHTTP2_FLAG_END_HEADERS |
140 NGHTTP2_FLAG_PRIORITY,
141 1000000007, &oframe.hd);
142
143 CU_ASSERT(1000000009 == oframe.pri_spec.stream_id);
144 CU_ASSERT(12 == oframe.pri_spec.weight);
145 CU_ASSERT(1 == oframe.pri_spec.exclusive);
146
147 hdblocklen = nghttp2_bufs_len(&bufs) - NGHTTP2_FRAME_HDLEN -
148 nghttp2_frame_priority_len(oframe.hd.flags);
149 CU_ASSERT((ssize_t)hdblocklen ==
150 inflate_hd(&inflater, &out, &bufs,
151 NGHTTP2_FRAME_HDLEN +
152 nghttp2_frame_priority_len(oframe.hd.flags),
153 mem));
154
155 nghttp2_nv_array_sort(out.nva, out.nvlen);
156 CU_ASSERT(nvnameeq("method", &out.nva[0]));
157
158 nghttp2_frame_headers_free(&oframe, mem);
159 nva_out_reset(&out, mem);
160 nghttp2_bufs_reset(&bufs);
161
162 nghttp2_bufs_free(&bufs);
163 nghttp2_frame_headers_free(&frame, mem);
164 nghttp2_hd_inflate_free(&inflater);
165 nghttp2_hd_deflate_free(&deflater);
166 }
167
test_nghttp2_frame_pack_headers_frame_too_large(void)168 void test_nghttp2_frame_pack_headers_frame_too_large(void) {
169 nghttp2_hd_deflater deflater;
170 nghttp2_headers frame;
171 nghttp2_bufs bufs;
172 nghttp2_nv *nva;
173 size_t big_vallen = NGHTTP2_HD_MAX_NV;
174 nghttp2_nv big_hds[16];
175 size_t big_hdslen = ARRLEN(big_hds);
176 size_t i;
177 int rv;
178 nghttp2_mem *mem;
179
180 mem = nghttp2_mem_default();
181 frame_pack_bufs_init(&bufs);
182
183 for (i = 0; i < big_hdslen; ++i) {
184 big_hds[i].name = (uint8_t *)"header";
185 big_hds[i].value = mem->malloc(big_vallen + 1, NULL);
186 memset(big_hds[i].value, '0' + (int)i, big_vallen);
187 big_hds[i].value[big_vallen] = '\0';
188 big_hds[i].namelen = strlen((char *)big_hds[i].name);
189 big_hds[i].valuelen = big_vallen;
190 big_hds[i].flags = NGHTTP2_NV_FLAG_NONE;
191 }
192
193 nghttp2_nv_array_copy(&nva, big_hds, big_hdslen, mem);
194 nghttp2_hd_deflate_init(&deflater, mem);
195 nghttp2_frame_headers_init(
196 &frame, NGHTTP2_FLAG_END_STREAM | NGHTTP2_FLAG_END_HEADERS, 1000000007,
197 NGHTTP2_HCAT_REQUEST, NULL, nva, big_hdslen);
198 rv = nghttp2_frame_pack_headers(&bufs, &frame, &deflater);
199 CU_ASSERT(NGHTTP2_ERR_HEADER_COMP == rv);
200
201 nghttp2_frame_headers_free(&frame, mem);
202 nghttp2_bufs_free(&bufs);
203 for (i = 0; i < big_hdslen; ++i) {
204 mem->free(big_hds[i].value, NULL);
205 }
206 nghttp2_hd_deflate_free(&deflater);
207 }
208
test_nghttp2_frame_pack_priority(void)209 void test_nghttp2_frame_pack_priority(void) {
210 nghttp2_priority frame, oframe;
211 nghttp2_bufs bufs;
212 nghttp2_priority_spec pri_spec;
213
214 frame_pack_bufs_init(&bufs);
215
216 /* First, pack priority with priority group and weight */
217 nghttp2_priority_spec_init(&pri_spec, 1000000009, 12, 1);
218
219 nghttp2_frame_priority_init(&frame, 1000000007, &pri_spec);
220 nghttp2_frame_pack_priority(&bufs, &frame);
221
222 CU_ASSERT(NGHTTP2_FRAME_HDLEN + 5 == nghttp2_bufs_len(&bufs));
223 CU_ASSERT(0 == unpack_framebuf((nghttp2_frame *)&oframe, &bufs));
224 check_frame_header(5, NGHTTP2_PRIORITY, NGHTTP2_FLAG_NONE, 1000000007,
225 &oframe.hd);
226
227 CU_ASSERT(1000000009 == oframe.pri_spec.stream_id);
228 CU_ASSERT(12 == oframe.pri_spec.weight);
229 CU_ASSERT(1 == oframe.pri_spec.exclusive);
230
231 nghttp2_frame_priority_free(&oframe);
232 nghttp2_bufs_reset(&bufs);
233
234 nghttp2_bufs_free(&bufs);
235 nghttp2_frame_priority_free(&frame);
236 }
237
test_nghttp2_frame_pack_rst_stream(void)238 void test_nghttp2_frame_pack_rst_stream(void) {
239 nghttp2_rst_stream frame, oframe;
240 nghttp2_bufs bufs;
241
242 frame_pack_bufs_init(&bufs);
243
244 nghttp2_frame_rst_stream_init(&frame, 1000000007, NGHTTP2_PROTOCOL_ERROR);
245 nghttp2_frame_pack_rst_stream(&bufs, &frame);
246
247 CU_ASSERT(NGHTTP2_FRAME_HDLEN + 4 == nghttp2_bufs_len(&bufs));
248 CU_ASSERT(0 == unpack_framebuf((nghttp2_frame *)&oframe, &bufs));
249 check_frame_header(4, NGHTTP2_RST_STREAM, NGHTTP2_FLAG_NONE, 1000000007,
250 &oframe.hd);
251 CU_ASSERT(NGHTTP2_PROTOCOL_ERROR == oframe.error_code);
252
253 nghttp2_frame_rst_stream_free(&oframe);
254 nghttp2_bufs_reset(&bufs);
255
256 /* Unknown error code is passed to callback as is */
257 frame.error_code = 1000000009;
258 nghttp2_frame_pack_rst_stream(&bufs, &frame);
259
260 CU_ASSERT(0 == unpack_framebuf((nghttp2_frame *)&oframe, &bufs));
261
262 check_frame_header(4, NGHTTP2_RST_STREAM, NGHTTP2_FLAG_NONE, 1000000007,
263 &oframe.hd);
264
265 CU_ASSERT(1000000009 == oframe.error_code);
266
267 nghttp2_frame_rst_stream_free(&oframe);
268
269 nghttp2_frame_rst_stream_free(&frame);
270
271 nghttp2_bufs_free(&bufs);
272 }
273
test_nghttp2_frame_pack_settings(void)274 void test_nghttp2_frame_pack_settings(void) {
275 nghttp2_settings frame, oframe;
276 nghttp2_bufs bufs;
277 int i;
278 int rv;
279 nghttp2_settings_entry iv[] = {{NGHTTP2_SETTINGS_MAX_CONCURRENT_STREAMS, 256},
280 {NGHTTP2_SETTINGS_INITIAL_WINDOW_SIZE, 16384},
281 {NGHTTP2_SETTINGS_HEADER_TABLE_SIZE, 4096}};
282 nghttp2_mem *mem;
283
284 mem = nghttp2_mem_default();
285 frame_pack_bufs_init(&bufs);
286
287 nghttp2_frame_settings_init(&frame, NGHTTP2_FLAG_NONE,
288 nghttp2_frame_iv_copy(iv, 3, mem), 3);
289 rv = nghttp2_frame_pack_settings(&bufs, &frame);
290
291 CU_ASSERT(0 == rv);
292 CU_ASSERT(NGHTTP2_FRAME_HDLEN + 3 * NGHTTP2_FRAME_SETTINGS_ENTRY_LENGTH ==
293 nghttp2_bufs_len(&bufs));
294
295 CU_ASSERT(0 == unpack_framebuf((nghttp2_frame *)&oframe, &bufs));
296 check_frame_header(3 * NGHTTP2_FRAME_SETTINGS_ENTRY_LENGTH, NGHTTP2_SETTINGS,
297 NGHTTP2_FLAG_NONE, 0, &oframe.hd);
298 CU_ASSERT(3 == oframe.niv);
299 for (i = 0; i < 3; ++i) {
300 CU_ASSERT(iv[i].settings_id == oframe.iv[i].settings_id);
301 CU_ASSERT(iv[i].value == oframe.iv[i].value);
302 }
303
304 nghttp2_bufs_free(&bufs);
305 nghttp2_frame_settings_free(&frame, mem);
306 nghttp2_frame_settings_free(&oframe, mem);
307 }
308
test_nghttp2_frame_pack_push_promise(void)309 void test_nghttp2_frame_pack_push_promise(void) {
310 nghttp2_hd_deflater deflater;
311 nghttp2_hd_inflater inflater;
312 nghttp2_push_promise frame, oframe;
313 nghttp2_bufs bufs;
314 nghttp2_nv *nva;
315 size_t nvlen;
316 nva_out out;
317 size_t hdblocklen;
318 int rv;
319 nghttp2_mem *mem;
320
321 mem = nghttp2_mem_default();
322 frame_pack_bufs_init(&bufs);
323
324 nva_out_init(&out);
325 nghttp2_hd_deflate_init(&deflater, mem);
326 nghttp2_hd_inflate_init(&inflater, mem);
327
328 nva = headers(mem);
329 nvlen = HEADERS_LENGTH;
330 nghttp2_frame_push_promise_init(&frame, NGHTTP2_FLAG_END_HEADERS, 1000000007,
331 (1U << 31) - 1, nva, nvlen);
332 rv = nghttp2_frame_pack_push_promise(&bufs, &frame, &deflater);
333
334 CU_ASSERT(0 == rv);
335 CU_ASSERT(nghttp2_bufs_len(&bufs) > 0);
336 CU_ASSERT(0 == unpack_framebuf((nghttp2_frame *)&oframe, &bufs));
337
338 check_frame_header(nghttp2_bufs_len(&bufs) - NGHTTP2_FRAME_HDLEN,
339 NGHTTP2_PUSH_PROMISE, NGHTTP2_FLAG_END_HEADERS, 1000000007,
340 &oframe.hd);
341 CU_ASSERT((1U << 31) - 1 == oframe.promised_stream_id);
342
343 hdblocklen = nghttp2_bufs_len(&bufs) - NGHTTP2_FRAME_HDLEN - 4;
344 CU_ASSERT((ssize_t)hdblocklen ==
345 inflate_hd(&inflater, &out, &bufs, NGHTTP2_FRAME_HDLEN + 4, mem));
346
347 CU_ASSERT(7 == out.nvlen);
348 CU_ASSERT(nvnameeq("method", &out.nva[0]));
349 CU_ASSERT(nvvalueeq("GET", &out.nva[0]));
350
351 nva_out_reset(&out, mem);
352 nghttp2_bufs_free(&bufs);
353 nghttp2_frame_push_promise_free(&oframe, mem);
354 nghttp2_frame_push_promise_free(&frame, mem);
355 nghttp2_hd_inflate_free(&inflater);
356 nghttp2_hd_deflate_free(&deflater);
357 }
358
test_nghttp2_frame_pack_ping(void)359 void test_nghttp2_frame_pack_ping(void) {
360 nghttp2_ping frame, oframe;
361 nghttp2_bufs bufs;
362 const uint8_t opaque_data[] = "01234567";
363
364 frame_pack_bufs_init(&bufs);
365
366 nghttp2_frame_ping_init(&frame, NGHTTP2_FLAG_ACK, opaque_data);
367 nghttp2_frame_pack_ping(&bufs, &frame);
368
369 CU_ASSERT(NGHTTP2_FRAME_HDLEN + 8 == nghttp2_bufs_len(&bufs));
370 CU_ASSERT(0 == unpack_framebuf((nghttp2_frame *)&oframe, &bufs));
371 check_frame_header(8, NGHTTP2_PING, NGHTTP2_FLAG_ACK, 0, &oframe.hd);
372 CU_ASSERT(memcmp(opaque_data, oframe.opaque_data, sizeof(opaque_data) - 1) ==
373 0);
374
375 nghttp2_bufs_free(&bufs);
376 nghttp2_frame_ping_free(&oframe);
377 nghttp2_frame_ping_free(&frame);
378 }
379
test_nghttp2_frame_pack_goaway(void)380 void test_nghttp2_frame_pack_goaway(void) {
381 nghttp2_goaway frame, oframe;
382 nghttp2_bufs bufs;
383 size_t opaque_data_len = 16;
384 uint8_t *opaque_data;
385 int rv;
386 nghttp2_mem *mem;
387
388 mem = nghttp2_mem_default();
389 frame_pack_bufs_init(&bufs);
390
391 opaque_data = mem->malloc(opaque_data_len, NULL);
392 memcpy(opaque_data, "0123456789abcdef", opaque_data_len);
393 nghttp2_frame_goaway_init(&frame, 1000000007, NGHTTP2_PROTOCOL_ERROR,
394 opaque_data, opaque_data_len);
395 rv = nghttp2_frame_pack_goaway(&bufs, &frame);
396
397 CU_ASSERT(0 == rv);
398 CU_ASSERT(NGHTTP2_FRAME_HDLEN + 8 + opaque_data_len ==
399 nghttp2_bufs_len(&bufs));
400 CU_ASSERT(0 == unpack_framebuf((nghttp2_frame *)&oframe, &bufs));
401 check_frame_header(24, NGHTTP2_GOAWAY, NGHTTP2_FLAG_NONE, 0, &oframe.hd);
402 CU_ASSERT(1000000007 == oframe.last_stream_id);
403 CU_ASSERT(NGHTTP2_PROTOCOL_ERROR == oframe.error_code);
404
405 CU_ASSERT(opaque_data_len == oframe.opaque_data_len);
406 CU_ASSERT(memcmp(opaque_data, oframe.opaque_data, opaque_data_len) == 0);
407
408 nghttp2_frame_goaway_free(&oframe, mem);
409 nghttp2_bufs_reset(&bufs);
410
411 /* Unknown error code is passed to callback as is */
412 frame.error_code = 1000000009;
413
414 rv = nghttp2_frame_pack_goaway(&bufs, &frame);
415
416 CU_ASSERT(0 == rv);
417 CU_ASSERT(0 == unpack_framebuf((nghttp2_frame *)&oframe, &bufs));
418 check_frame_header(24, NGHTTP2_GOAWAY, NGHTTP2_FLAG_NONE, 0, &oframe.hd);
419 CU_ASSERT(1000000009 == oframe.error_code);
420
421 nghttp2_frame_goaway_free(&oframe, mem);
422
423 nghttp2_frame_goaway_free(&frame, mem);
424
425 nghttp2_bufs_free(&bufs);
426 }
427
test_nghttp2_frame_pack_window_update(void)428 void test_nghttp2_frame_pack_window_update(void) {
429 nghttp2_window_update frame, oframe;
430 nghttp2_bufs bufs;
431
432 frame_pack_bufs_init(&bufs);
433
434 nghttp2_frame_window_update_init(&frame, NGHTTP2_FLAG_NONE, 1000000007, 4096);
435 nghttp2_frame_pack_window_update(&bufs, &frame);
436
437 CU_ASSERT(NGHTTP2_FRAME_HDLEN + 4 == nghttp2_bufs_len(&bufs));
438 CU_ASSERT(0 == unpack_framebuf((nghttp2_frame *)&oframe, &bufs));
439 check_frame_header(4, NGHTTP2_WINDOW_UPDATE, NGHTTP2_FLAG_NONE, 1000000007,
440 &oframe.hd);
441 CU_ASSERT(4096 == oframe.window_size_increment);
442
443 nghttp2_bufs_free(&bufs);
444 nghttp2_frame_window_update_free(&oframe);
445 nghttp2_frame_window_update_free(&frame);
446 }
447
test_nghttp2_frame_pack_altsvc(void)448 void test_nghttp2_frame_pack_altsvc(void) {
449 nghttp2_extension frame, oframe;
450 nghttp2_ext_altsvc altsvc, oaltsvc;
451 nghttp2_bufs bufs;
452 int rv;
453 size_t payloadlen;
454 static const uint8_t origin[] = "nghttp2.org";
455 static const uint8_t field_value[] = "h2=\":443\"";
456 nghttp2_buf buf;
457 uint8_t *rawbuf;
458 nghttp2_mem *mem;
459
460 mem = nghttp2_mem_default();
461
462 frame_pack_bufs_init(&bufs);
463
464 frame.payload = &altsvc;
465 oframe.payload = &oaltsvc;
466
467 rawbuf = nghttp2_mem_malloc(mem, 32);
468 nghttp2_buf_wrap_init(&buf, rawbuf, 32);
469
470 buf.last = nghttp2_cpymem(buf.last, origin, sizeof(origin) - 1);
471 buf.last = nghttp2_cpymem(buf.last, field_value, sizeof(field_value) - 1);
472
473 nghttp2_frame_altsvc_init(&frame, 1000000007, buf.pos, sizeof(origin) - 1,
474 buf.pos + sizeof(origin) - 1,
475 sizeof(field_value) - 1);
476
477 payloadlen = 2 + sizeof(origin) - 1 + sizeof(field_value) - 1;
478
479 nghttp2_frame_pack_altsvc(&bufs, &frame);
480
481 CU_ASSERT(NGHTTP2_FRAME_HDLEN + payloadlen == nghttp2_bufs_len(&bufs));
482
483 rv = unpack_framebuf((nghttp2_frame *)&oframe, &bufs);
484
485 CU_ASSERT(0 == rv);
486
487 check_frame_header(payloadlen, NGHTTP2_ALTSVC, NGHTTP2_FLAG_NONE, 1000000007,
488 &oframe.hd);
489
490 CU_ASSERT(sizeof(origin) - 1 == oaltsvc.origin_len);
491 CU_ASSERT(0 == memcmp(origin, oaltsvc.origin, sizeof(origin) - 1));
492 CU_ASSERT(sizeof(field_value) - 1 == oaltsvc.field_value_len);
493 CU_ASSERT(0 ==
494 memcmp(field_value, oaltsvc.field_value, sizeof(field_value) - 1));
495
496 nghttp2_frame_altsvc_free(&oframe, mem);
497 nghttp2_frame_altsvc_free(&frame, mem);
498 nghttp2_bufs_free(&bufs);
499 }
500
test_nghttp2_frame_pack_origin(void)501 void test_nghttp2_frame_pack_origin(void) {
502 nghttp2_extension frame, oframe;
503 nghttp2_ext_origin origin, oorigin;
504 nghttp2_bufs bufs;
505 nghttp2_buf *buf;
506 int rv;
507 size_t payloadlen;
508 static const uint8_t example[] = "https://example.com";
509 static const uint8_t nghttp2[] = "https://nghttp2.org";
510 nghttp2_origin_entry ov[] = {
511 {
512 (uint8_t *)example,
513 sizeof(example) - 1,
514 },
515 {
516 NULL,
517 0,
518 },
519 {
520 (uint8_t *)nghttp2,
521 sizeof(nghttp2) - 1,
522 },
523 };
524 nghttp2_mem *mem;
525
526 mem = nghttp2_mem_default();
527
528 frame_pack_bufs_init(&bufs);
529
530 frame.payload = &origin;
531 oframe.payload = &oorigin;
532
533 nghttp2_frame_origin_init(&frame, ov, 3);
534
535 payloadlen = 2 + sizeof(example) - 1 + 2 + 2 + sizeof(nghttp2) - 1;
536
537 rv = nghttp2_frame_pack_origin(&bufs, &frame);
538
539 CU_ASSERT(0 == rv);
540 CU_ASSERT(NGHTTP2_FRAME_HDLEN + payloadlen == nghttp2_bufs_len(&bufs));
541
542 rv = unpack_framebuf((nghttp2_frame *)&oframe, &bufs);
543
544 CU_ASSERT(0 == rv);
545
546 check_frame_header(payloadlen, NGHTTP2_ORIGIN, NGHTTP2_FLAG_NONE, 0,
547 &oframe.hd);
548
549 CU_ASSERT(2 == oorigin.nov);
550 CU_ASSERT(sizeof(example) - 1 == oorigin.ov[0].origin_len);
551 CU_ASSERT(0 == memcmp(example, oorigin.ov[0].origin, sizeof(example) - 1));
552 CU_ASSERT(sizeof(nghttp2) - 1 == oorigin.ov[1].origin_len);
553 CU_ASSERT(0 == memcmp(nghttp2, oorigin.ov[1].origin, sizeof(nghttp2) - 1));
554
555 nghttp2_frame_origin_free(&oframe, mem);
556
557 /* Check the case where origin length is too large */
558 buf = &bufs.head->buf;
559 nghttp2_put_uint16be(buf->pos + NGHTTP2_FRAME_HDLEN,
560 (uint16_t)(payloadlen - 1));
561
562 rv = unpack_framebuf((nghttp2_frame *)&oframe, &bufs);
563
564 CU_ASSERT(NGHTTP2_ERR_FRAME_SIZE_ERROR == rv);
565
566 nghttp2_bufs_reset(&bufs);
567 memset(&oframe, 0, sizeof(oframe));
568 memset(&oorigin, 0, sizeof(oorigin));
569 oframe.payload = &oorigin;
570
571 /* Empty ORIGIN frame */
572 nghttp2_frame_origin_init(&frame, NULL, 0);
573
574 rv = nghttp2_frame_pack_origin(&bufs, &frame);
575
576 CU_ASSERT(0 == rv);
577 CU_ASSERT(NGHTTP2_FRAME_HDLEN == nghttp2_bufs_len(&bufs));
578
579 rv = unpack_framebuf((nghttp2_frame *)&oframe, &bufs);
580
581 CU_ASSERT(0 == rv);
582
583 check_frame_header(0, NGHTTP2_ORIGIN, NGHTTP2_FLAG_NONE, 0, &oframe.hd);
584
585 CU_ASSERT(0 == oorigin.nov);
586 CU_ASSERT(NULL == oorigin.ov);
587
588 nghttp2_frame_origin_free(&oframe, mem);
589
590 nghttp2_bufs_free(&bufs);
591 }
592
test_nghttp2_frame_pack_priority_update(void)593 void test_nghttp2_frame_pack_priority_update(void) {
594 nghttp2_extension frame, oframe;
595 nghttp2_ext_priority_update priority_update, opriority_update;
596 nghttp2_bufs bufs;
597 int rv;
598 size_t payloadlen;
599 static const uint8_t field_value[] = "i,u=0";
600
601 frame_pack_bufs_init(&bufs);
602
603 frame.payload = &priority_update;
604 oframe.payload = &opriority_update;
605
606 nghttp2_frame_priority_update_init(&frame, 1000000007, (uint8_t *)field_value,
607 sizeof(field_value) - 1);
608
609 payloadlen = 4 + sizeof(field_value) - 1;
610
611 nghttp2_frame_pack_priority_update(&bufs, &frame);
612
613 CU_ASSERT(NGHTTP2_FRAME_HDLEN + payloadlen == nghttp2_bufs_len(&bufs));
614
615 rv = unpack_framebuf((nghttp2_frame *)&oframe, &bufs);
616
617 CU_ASSERT(0 == rv);
618
619 check_frame_header(payloadlen, NGHTTP2_PRIORITY_UPDATE, NGHTTP2_FLAG_NONE, 0,
620 &oframe.hd);
621
622 CU_ASSERT(sizeof(field_value) - 1 == opriority_update.field_value_len);
623 CU_ASSERT(0 == memcmp(field_value, opriority_update.field_value,
624 sizeof(field_value) - 1));
625
626 nghttp2_bufs_free(&bufs);
627 }
628
test_nghttp2_nv_array_copy(void)629 void test_nghttp2_nv_array_copy(void) {
630 nghttp2_nv *nva;
631 ssize_t rv;
632 nghttp2_nv emptynv[] = {MAKE_NV("", ""), MAKE_NV("", "")};
633 nghttp2_nv nv[] = {MAKE_NV("alpha", "bravo"), MAKE_NV("charlie", "delta")};
634 nghttp2_nv bignv;
635 nghttp2_mem *mem;
636
637 mem = nghttp2_mem_default();
638
639 bignv.name = (uint8_t *)"echo";
640 bignv.namelen = strlen("echo");
641 bignv.valuelen = (1 << 14) - 1;
642 bignv.value = mem->malloc(bignv.valuelen, NULL);
643 bignv.flags = NGHTTP2_NV_FLAG_NONE;
644 memset(bignv.value, '0', bignv.valuelen);
645
646 rv = nghttp2_nv_array_copy(&nva, NULL, 0, mem);
647 CU_ASSERT(0 == rv);
648 CU_ASSERT(NULL == nva);
649
650 rv = nghttp2_nv_array_copy(&nva, emptynv, ARRLEN(emptynv), mem);
651 CU_ASSERT(0 == rv);
652 CU_ASSERT(nva[0].namelen == 0);
653 CU_ASSERT(nva[0].valuelen == 0);
654 CU_ASSERT(nva[1].namelen == 0);
655 CU_ASSERT(nva[1].valuelen == 0);
656
657 nghttp2_nv_array_del(nva, mem);
658
659 rv = nghttp2_nv_array_copy(&nva, nv, ARRLEN(nv), mem);
660 CU_ASSERT(0 == rv);
661 CU_ASSERT(nva[0].namelen == 5);
662 CU_ASSERT(0 == memcmp("alpha", nva[0].name, 5));
663 CU_ASSERT(nva[0].valuelen == 5);
664 CU_ASSERT(0 == memcmp("bravo", nva[0].value, 5));
665 CU_ASSERT(nva[1].namelen == 7);
666 CU_ASSERT(0 == memcmp("charlie", nva[1].name, 7));
667 CU_ASSERT(nva[1].valuelen == 5);
668 CU_ASSERT(0 == memcmp("delta", nva[1].value, 5));
669
670 nghttp2_nv_array_del(nva, mem);
671
672 /* Large header field is acceptable */
673 rv = nghttp2_nv_array_copy(&nva, &bignv, 1, mem);
674 CU_ASSERT(0 == rv);
675
676 nghttp2_nv_array_del(nva, mem);
677
678 mem->free(bignv.value, NULL);
679 }
680
test_nghttp2_iv_check(void)681 void test_nghttp2_iv_check(void) {
682 nghttp2_settings_entry iv[5];
683
684 iv[0].settings_id = NGHTTP2_SETTINGS_MAX_CONCURRENT_STREAMS;
685 iv[0].value = 100;
686 iv[1].settings_id = NGHTTP2_SETTINGS_HEADER_TABLE_SIZE;
687 iv[1].value = 1024;
688
689 CU_ASSERT(nghttp2_iv_check(iv, 2));
690
691 iv[1].settings_id = NGHTTP2_SETTINGS_INITIAL_WINDOW_SIZE;
692 iv[1].value = NGHTTP2_MAX_WINDOW_SIZE;
693 CU_ASSERT(nghttp2_iv_check(iv, 2));
694
695 /* Too large window size */
696 iv[1].value = (uint32_t)NGHTTP2_MAX_WINDOW_SIZE + 1;
697 CU_ASSERT(0 == nghttp2_iv_check(iv, 2));
698
699 /* ENABLE_PUSH only allows 0 or 1 */
700 iv[1].settings_id = NGHTTP2_SETTINGS_ENABLE_PUSH;
701 iv[1].value = 0;
702 CU_ASSERT(nghttp2_iv_check(iv, 2));
703 iv[1].value = 1;
704 CU_ASSERT(nghttp2_iv_check(iv, 2));
705 iv[1].value = 3;
706 CU_ASSERT(!nghttp2_iv_check(iv, 2));
707
708 /* Undefined SETTINGS ID is allowed */
709 iv[1].settings_id = 1000000009;
710 iv[1].value = 0;
711 CU_ASSERT(nghttp2_iv_check(iv, 2));
712
713 /* Full size SETTINGS_HEADER_TABLE_SIZE (UINT32_MAX) must be
714 accepted */
715 iv[1].settings_id = NGHTTP2_SETTINGS_HEADER_TABLE_SIZE;
716 iv[1].value = UINT32_MAX;
717 CU_ASSERT(nghttp2_iv_check(iv, 2));
718
719 /* Too small SETTINGS_MAX_FRAME_SIZE */
720 iv[0].settings_id = NGHTTP2_SETTINGS_MAX_FRAME_SIZE;
721 iv[0].value = NGHTTP2_MAX_FRAME_SIZE_MIN - 1;
722 CU_ASSERT(!nghttp2_iv_check(iv, 1));
723
724 /* Too large SETTINGS_MAX_FRAME_SIZE */
725 iv[0].settings_id = NGHTTP2_SETTINGS_MAX_FRAME_SIZE;
726 iv[0].value = NGHTTP2_MAX_FRAME_SIZE_MAX + 1;
727 CU_ASSERT(!nghttp2_iv_check(iv, 1));
728
729 /* Max and min SETTINGS_MAX_FRAME_SIZE */
730 iv[0].settings_id = NGHTTP2_SETTINGS_MAX_FRAME_SIZE;
731 iv[0].value = NGHTTP2_MAX_FRAME_SIZE_MIN;
732 iv[1].settings_id = NGHTTP2_SETTINGS_MAX_FRAME_SIZE;
733 iv[1].value = NGHTTP2_MAX_FRAME_SIZE_MAX;
734 CU_ASSERT(nghttp2_iv_check(iv, 2));
735 }
736