• Home
  • Line#
  • Scopes#
  • Navigate#
  • Raw
  • Download
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()71 void test_nghttp2_frame_pack_headers() {
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   int rv;
214 
215   frame_pack_bufs_init(&bufs);
216 
217   /* First, pack priority with priority group and weight */
218   nghttp2_priority_spec_init(&pri_spec, 1000000009, 12, 1);
219 
220   nghttp2_frame_priority_init(&frame, 1000000007, &pri_spec);
221   rv = nghttp2_frame_pack_priority(&bufs, &frame);
222 
223   CU_ASSERT(0 == rv);
224   CU_ASSERT(NGHTTP2_FRAME_HDLEN + 5 == nghttp2_bufs_len(&bufs));
225   CU_ASSERT(0 == unpack_framebuf((nghttp2_frame *)&oframe, &bufs));
226   check_frame_header(5, NGHTTP2_PRIORITY, NGHTTP2_FLAG_NONE, 1000000007,
227                      &oframe.hd);
228 
229   CU_ASSERT(1000000009 == oframe.pri_spec.stream_id);
230   CU_ASSERT(12 == oframe.pri_spec.weight);
231   CU_ASSERT(1 == oframe.pri_spec.exclusive);
232 
233   nghttp2_frame_priority_free(&oframe);
234   nghttp2_bufs_reset(&bufs);
235 
236   nghttp2_bufs_free(&bufs);
237   nghttp2_frame_priority_free(&frame);
238 }
239 
test_nghttp2_frame_pack_rst_stream(void)240 void test_nghttp2_frame_pack_rst_stream(void) {
241   nghttp2_rst_stream frame, oframe;
242   nghttp2_bufs bufs;
243   int rv;
244 
245   frame_pack_bufs_init(&bufs);
246 
247   nghttp2_frame_rst_stream_init(&frame, 1000000007, NGHTTP2_PROTOCOL_ERROR);
248   rv = nghttp2_frame_pack_rst_stream(&bufs, &frame);
249 
250   CU_ASSERT(0 == rv);
251   CU_ASSERT(NGHTTP2_FRAME_HDLEN + 4 == nghttp2_bufs_len(&bufs));
252   CU_ASSERT(0 == unpack_framebuf((nghttp2_frame *)&oframe, &bufs));
253   check_frame_header(4, NGHTTP2_RST_STREAM, NGHTTP2_FLAG_NONE, 1000000007,
254                      &oframe.hd);
255   CU_ASSERT(NGHTTP2_PROTOCOL_ERROR == oframe.error_code);
256 
257   nghttp2_frame_rst_stream_free(&oframe);
258   nghttp2_bufs_reset(&bufs);
259 
260   /* Unknown error code is passed to callback as is */
261   frame.error_code = 1000000009;
262   rv = nghttp2_frame_pack_rst_stream(&bufs, &frame);
263 
264   CU_ASSERT(0 == rv);
265   CU_ASSERT(0 == unpack_framebuf((nghttp2_frame *)&oframe, &bufs));
266 
267   check_frame_header(4, NGHTTP2_RST_STREAM, NGHTTP2_FLAG_NONE, 1000000007,
268                      &oframe.hd);
269 
270   CU_ASSERT(1000000009 == oframe.error_code);
271 
272   nghttp2_frame_rst_stream_free(&oframe);
273 
274   nghttp2_frame_rst_stream_free(&frame);
275 
276   nghttp2_bufs_free(&bufs);
277 }
278 
test_nghttp2_frame_pack_settings()279 void test_nghttp2_frame_pack_settings() {
280   nghttp2_settings frame, oframe;
281   nghttp2_bufs bufs;
282   int i;
283   int rv;
284   nghttp2_settings_entry iv[] = {{NGHTTP2_SETTINGS_MAX_CONCURRENT_STREAMS, 256},
285                                  {NGHTTP2_SETTINGS_INITIAL_WINDOW_SIZE, 16384},
286                                  {NGHTTP2_SETTINGS_HEADER_TABLE_SIZE, 4096}};
287   nghttp2_mem *mem;
288 
289   mem = nghttp2_mem_default();
290   frame_pack_bufs_init(&bufs);
291 
292   nghttp2_frame_settings_init(&frame, NGHTTP2_FLAG_NONE,
293                               nghttp2_frame_iv_copy(iv, 3, mem), 3);
294   rv = nghttp2_frame_pack_settings(&bufs, &frame);
295 
296   CU_ASSERT(0 == rv);
297   CU_ASSERT(NGHTTP2_FRAME_HDLEN + 3 * NGHTTP2_FRAME_SETTINGS_ENTRY_LENGTH ==
298             nghttp2_bufs_len(&bufs));
299 
300   CU_ASSERT(0 == unpack_framebuf((nghttp2_frame *)&oframe, &bufs));
301   check_frame_header(3 * NGHTTP2_FRAME_SETTINGS_ENTRY_LENGTH, NGHTTP2_SETTINGS,
302                      NGHTTP2_FLAG_NONE, 0, &oframe.hd);
303   CU_ASSERT(3 == oframe.niv);
304   for (i = 0; i < 3; ++i) {
305     CU_ASSERT(iv[i].settings_id == oframe.iv[i].settings_id);
306     CU_ASSERT(iv[i].value == oframe.iv[i].value);
307   }
308 
309   nghttp2_bufs_free(&bufs);
310   nghttp2_frame_settings_free(&frame, mem);
311   nghttp2_frame_settings_free(&oframe, mem);
312 }
313 
test_nghttp2_frame_pack_push_promise()314 void test_nghttp2_frame_pack_push_promise() {
315   nghttp2_hd_deflater deflater;
316   nghttp2_hd_inflater inflater;
317   nghttp2_push_promise frame, oframe;
318   nghttp2_bufs bufs;
319   nghttp2_nv *nva;
320   size_t nvlen;
321   nva_out out;
322   size_t hdblocklen;
323   int rv;
324   nghttp2_mem *mem;
325 
326   mem = nghttp2_mem_default();
327   frame_pack_bufs_init(&bufs);
328 
329   nva_out_init(&out);
330   nghttp2_hd_deflate_init(&deflater, mem);
331   nghttp2_hd_inflate_init(&inflater, mem);
332 
333   nva = headers(mem);
334   nvlen = HEADERS_LENGTH;
335   nghttp2_frame_push_promise_init(&frame, NGHTTP2_FLAG_END_HEADERS, 1000000007,
336                                   (1U << 31) - 1, nva, nvlen);
337   rv = nghttp2_frame_pack_push_promise(&bufs, &frame, &deflater);
338 
339   CU_ASSERT(0 == rv);
340   CU_ASSERT(nghttp2_bufs_len(&bufs) > 0);
341   CU_ASSERT(0 == unpack_framebuf((nghttp2_frame *)&oframe, &bufs));
342 
343   check_frame_header(nghttp2_bufs_len(&bufs) - NGHTTP2_FRAME_HDLEN,
344                      NGHTTP2_PUSH_PROMISE, NGHTTP2_FLAG_END_HEADERS, 1000000007,
345                      &oframe.hd);
346   CU_ASSERT((1U << 31) - 1 == oframe.promised_stream_id);
347 
348   hdblocklen = nghttp2_bufs_len(&bufs) - NGHTTP2_FRAME_HDLEN - 4;
349   CU_ASSERT((ssize_t)hdblocklen ==
350             inflate_hd(&inflater, &out, &bufs, NGHTTP2_FRAME_HDLEN + 4, mem));
351 
352   CU_ASSERT(7 == out.nvlen);
353   CU_ASSERT(nvnameeq("method", &out.nva[0]));
354   CU_ASSERT(nvvalueeq("GET", &out.nva[0]));
355 
356   nva_out_reset(&out, mem);
357   nghttp2_bufs_free(&bufs);
358   nghttp2_frame_push_promise_free(&oframe, mem);
359   nghttp2_frame_push_promise_free(&frame, mem);
360   nghttp2_hd_inflate_free(&inflater);
361   nghttp2_hd_deflate_free(&deflater);
362 }
363 
test_nghttp2_frame_pack_ping(void)364 void test_nghttp2_frame_pack_ping(void) {
365   nghttp2_ping frame, oframe;
366   nghttp2_bufs bufs;
367   const uint8_t opaque_data[] = "01234567";
368   int rv;
369 
370   frame_pack_bufs_init(&bufs);
371 
372   nghttp2_frame_ping_init(&frame, NGHTTP2_FLAG_ACK, opaque_data);
373   rv = nghttp2_frame_pack_ping(&bufs, &frame);
374 
375   CU_ASSERT(0 == rv);
376   CU_ASSERT(NGHTTP2_FRAME_HDLEN + 8 == nghttp2_bufs_len(&bufs));
377   CU_ASSERT(0 == unpack_framebuf((nghttp2_frame *)&oframe, &bufs));
378   check_frame_header(8, NGHTTP2_PING, NGHTTP2_FLAG_ACK, 0, &oframe.hd);
379   CU_ASSERT(memcmp(opaque_data, oframe.opaque_data, sizeof(opaque_data) - 1) ==
380             0);
381 
382   nghttp2_bufs_free(&bufs);
383   nghttp2_frame_ping_free(&oframe);
384   nghttp2_frame_ping_free(&frame);
385 }
386 
test_nghttp2_frame_pack_goaway()387 void test_nghttp2_frame_pack_goaway() {
388   nghttp2_goaway frame, oframe;
389   nghttp2_bufs bufs;
390   size_t opaque_data_len = 16;
391   uint8_t *opaque_data;
392   int rv;
393   nghttp2_mem *mem;
394 
395   mem = nghttp2_mem_default();
396   frame_pack_bufs_init(&bufs);
397 
398   opaque_data = mem->malloc(opaque_data_len, NULL);
399   memcpy(opaque_data, "0123456789abcdef", opaque_data_len);
400   nghttp2_frame_goaway_init(&frame, 1000000007, NGHTTP2_PROTOCOL_ERROR,
401                             opaque_data, opaque_data_len);
402   rv = nghttp2_frame_pack_goaway(&bufs, &frame);
403 
404   CU_ASSERT(0 == rv);
405   CU_ASSERT(NGHTTP2_FRAME_HDLEN + 8 + opaque_data_len ==
406             nghttp2_bufs_len(&bufs));
407   CU_ASSERT(0 == unpack_framebuf((nghttp2_frame *)&oframe, &bufs));
408   check_frame_header(24, NGHTTP2_GOAWAY, NGHTTP2_FLAG_NONE, 0, &oframe.hd);
409   CU_ASSERT(1000000007 == oframe.last_stream_id);
410   CU_ASSERT(NGHTTP2_PROTOCOL_ERROR == oframe.error_code);
411 
412   CU_ASSERT(opaque_data_len == oframe.opaque_data_len);
413   CU_ASSERT(memcmp(opaque_data, oframe.opaque_data, opaque_data_len) == 0);
414 
415   nghttp2_frame_goaway_free(&oframe, mem);
416   nghttp2_bufs_reset(&bufs);
417 
418   /* Unknown error code is passed to callback as is */
419   frame.error_code = 1000000009;
420 
421   rv = nghttp2_frame_pack_goaway(&bufs, &frame);
422 
423   CU_ASSERT(0 == rv);
424   CU_ASSERT(0 == unpack_framebuf((nghttp2_frame *)&oframe, &bufs));
425   check_frame_header(24, NGHTTP2_GOAWAY, NGHTTP2_FLAG_NONE, 0, &oframe.hd);
426   CU_ASSERT(1000000009 == oframe.error_code);
427 
428   nghttp2_frame_goaway_free(&oframe, mem);
429 
430   nghttp2_frame_goaway_free(&frame, mem);
431 
432   nghttp2_bufs_free(&bufs);
433 }
434 
test_nghttp2_frame_pack_window_update(void)435 void test_nghttp2_frame_pack_window_update(void) {
436   nghttp2_window_update frame, oframe;
437   nghttp2_bufs bufs;
438   int rv;
439 
440   frame_pack_bufs_init(&bufs);
441 
442   nghttp2_frame_window_update_init(&frame, NGHTTP2_FLAG_NONE, 1000000007, 4096);
443   rv = nghttp2_frame_pack_window_update(&bufs, &frame);
444 
445   CU_ASSERT(0 == rv);
446   CU_ASSERT(NGHTTP2_FRAME_HDLEN + 4 == nghttp2_bufs_len(&bufs));
447   CU_ASSERT(0 == unpack_framebuf((nghttp2_frame *)&oframe, &bufs));
448   check_frame_header(4, NGHTTP2_WINDOW_UPDATE, NGHTTP2_FLAG_NONE, 1000000007,
449                      &oframe.hd);
450   CU_ASSERT(4096 == oframe.window_size_increment);
451 
452   nghttp2_bufs_free(&bufs);
453   nghttp2_frame_window_update_free(&oframe);
454   nghttp2_frame_window_update_free(&frame);
455 }
456 
test_nghttp2_frame_pack_altsvc(void)457 void test_nghttp2_frame_pack_altsvc(void) {
458   nghttp2_extension frame, oframe;
459   nghttp2_ext_altsvc altsvc, oaltsvc;
460   nghttp2_bufs bufs;
461   int rv;
462   size_t payloadlen;
463   static const uint8_t origin[] = "nghttp2.org";
464   static const uint8_t field_value[] = "h2=\":443\"";
465   nghttp2_buf buf;
466   uint8_t *rawbuf;
467   nghttp2_mem *mem;
468 
469   mem = nghttp2_mem_default();
470 
471   frame_pack_bufs_init(&bufs);
472 
473   frame.payload = &altsvc;
474   oframe.payload = &oaltsvc;
475 
476   rawbuf = nghttp2_mem_malloc(mem, 32);
477   nghttp2_buf_wrap_init(&buf, rawbuf, 32);
478 
479   buf.last = nghttp2_cpymem(buf.last, origin, sizeof(origin) - 1);
480   buf.last = nghttp2_cpymem(buf.last, field_value, sizeof(field_value) - 1);
481 
482   nghttp2_frame_altsvc_init(&frame, 1000000007, buf.pos, sizeof(origin) - 1,
483                             buf.pos + sizeof(origin) - 1,
484                             sizeof(field_value) - 1);
485 
486   payloadlen = 2 + sizeof(origin) - 1 + sizeof(field_value) - 1;
487 
488   rv = nghttp2_frame_pack_altsvc(&bufs, &frame);
489 
490   CU_ASSERT(0 == rv);
491   CU_ASSERT(NGHTTP2_FRAME_HDLEN + payloadlen == nghttp2_bufs_len(&bufs));
492 
493   rv = unpack_framebuf((nghttp2_frame *)&oframe, &bufs);
494 
495   CU_ASSERT(0 == rv);
496 
497   check_frame_header(payloadlen, NGHTTP2_ALTSVC, NGHTTP2_FLAG_NONE, 1000000007,
498                      &oframe.hd);
499 
500   CU_ASSERT(sizeof(origin) - 1 == oaltsvc.origin_len);
501   CU_ASSERT(0 == memcmp(origin, oaltsvc.origin, sizeof(origin) - 1));
502   CU_ASSERT(sizeof(field_value) - 1 == oaltsvc.field_value_len);
503   CU_ASSERT(0 ==
504             memcmp(field_value, oaltsvc.field_value, sizeof(field_value) - 1));
505 
506   nghttp2_frame_altsvc_free(&oframe, mem);
507   nghttp2_frame_altsvc_free(&frame, mem);
508   nghttp2_bufs_free(&bufs);
509 }
510 
test_nghttp2_frame_pack_origin(void)511 void test_nghttp2_frame_pack_origin(void) {
512   nghttp2_extension frame, oframe;
513   nghttp2_ext_origin origin, oorigin;
514   nghttp2_bufs bufs;
515   nghttp2_buf *buf;
516   int rv;
517   size_t payloadlen;
518   static const uint8_t example[] = "https://example.com";
519   static const uint8_t nghttp2[] = "https://nghttp2.org";
520   nghttp2_origin_entry ov[] = {
521       {
522           (uint8_t *)example,
523           sizeof(example) - 1,
524       },
525       {
526           NULL,
527           0,
528       },
529       {
530           (uint8_t *)nghttp2,
531           sizeof(nghttp2) - 1,
532       },
533   };
534   nghttp2_mem *mem;
535 
536   mem = nghttp2_mem_default();
537 
538   frame_pack_bufs_init(&bufs);
539 
540   frame.payload = &origin;
541   oframe.payload = &oorigin;
542 
543   nghttp2_frame_origin_init(&frame, ov, 3);
544 
545   payloadlen = 2 + sizeof(example) - 1 + 2 + 2 + sizeof(nghttp2) - 1;
546 
547   rv = nghttp2_frame_pack_origin(&bufs, &frame);
548 
549   CU_ASSERT(0 == rv);
550   CU_ASSERT(NGHTTP2_FRAME_HDLEN + payloadlen == nghttp2_bufs_len(&bufs));
551 
552   rv = unpack_framebuf((nghttp2_frame *)&oframe, &bufs);
553 
554   CU_ASSERT(0 == rv);
555 
556   check_frame_header(payloadlen, NGHTTP2_ORIGIN, NGHTTP2_FLAG_NONE, 0,
557                      &oframe.hd);
558 
559   CU_ASSERT(2 == oorigin.nov);
560   CU_ASSERT(sizeof(example) - 1 == oorigin.ov[0].origin_len);
561   CU_ASSERT(0 == memcmp(example, oorigin.ov[0].origin, sizeof(example) - 1));
562   CU_ASSERT(sizeof(nghttp2) - 1 == oorigin.ov[1].origin_len);
563   CU_ASSERT(0 == memcmp(nghttp2, oorigin.ov[1].origin, sizeof(nghttp2) - 1));
564 
565   nghttp2_frame_origin_free(&oframe, mem);
566 
567   /* Check the case where origin length is too large */
568   buf = &bufs.head->buf;
569   nghttp2_put_uint16be(buf->pos + NGHTTP2_FRAME_HDLEN,
570                        (uint16_t)(payloadlen - 1));
571 
572   rv = unpack_framebuf((nghttp2_frame *)&oframe, &bufs);
573 
574   CU_ASSERT(NGHTTP2_ERR_FRAME_SIZE_ERROR == rv);
575 
576   nghttp2_bufs_reset(&bufs);
577   memset(&oframe, 0, sizeof(oframe));
578   memset(&oorigin, 0, sizeof(oorigin));
579   oframe.payload = &oorigin;
580 
581   /* Empty ORIGIN frame */
582   nghttp2_frame_origin_init(&frame, NULL, 0);
583 
584   rv = nghttp2_frame_pack_origin(&bufs, &frame);
585 
586   CU_ASSERT(0 == rv);
587   CU_ASSERT(NGHTTP2_FRAME_HDLEN == nghttp2_bufs_len(&bufs));
588 
589   rv = unpack_framebuf((nghttp2_frame *)&oframe, &bufs);
590 
591   CU_ASSERT(0 == rv);
592 
593   check_frame_header(0, NGHTTP2_ORIGIN, NGHTTP2_FLAG_NONE, 0, &oframe.hd);
594 
595   CU_ASSERT(0 == oorigin.nov);
596   CU_ASSERT(NULL == oorigin.ov);
597 
598   nghttp2_frame_origin_free(&oframe, mem);
599 
600   nghttp2_bufs_free(&bufs);
601 }
602 
test_nghttp2_nv_array_copy(void)603 void test_nghttp2_nv_array_copy(void) {
604   nghttp2_nv *nva;
605   ssize_t rv;
606   nghttp2_nv emptynv[] = {MAKE_NV("", ""), MAKE_NV("", "")};
607   nghttp2_nv nv[] = {MAKE_NV("alpha", "bravo"), MAKE_NV("charlie", "delta")};
608   nghttp2_nv bignv;
609   nghttp2_mem *mem;
610 
611   mem = nghttp2_mem_default();
612 
613   bignv.name = (uint8_t *)"echo";
614   bignv.namelen = strlen("echo");
615   bignv.valuelen = (1 << 14) - 1;
616   bignv.value = mem->malloc(bignv.valuelen, NULL);
617   bignv.flags = NGHTTP2_NV_FLAG_NONE;
618   memset(bignv.value, '0', bignv.valuelen);
619 
620   rv = nghttp2_nv_array_copy(&nva, NULL, 0, mem);
621   CU_ASSERT(0 == rv);
622   CU_ASSERT(NULL == nva);
623 
624   rv = nghttp2_nv_array_copy(&nva, emptynv, ARRLEN(emptynv), mem);
625   CU_ASSERT(0 == rv);
626   CU_ASSERT(nva[0].namelen == 0);
627   CU_ASSERT(nva[0].valuelen == 0);
628   CU_ASSERT(nva[1].namelen == 0);
629   CU_ASSERT(nva[1].valuelen == 0);
630 
631   nghttp2_nv_array_del(nva, mem);
632 
633   rv = nghttp2_nv_array_copy(&nva, nv, ARRLEN(nv), mem);
634   CU_ASSERT(0 == rv);
635   CU_ASSERT(nva[0].namelen == 5);
636   CU_ASSERT(0 == memcmp("alpha", nva[0].name, 5));
637   CU_ASSERT(nva[0].valuelen == 5);
638   CU_ASSERT(0 == memcmp("bravo", nva[0].value, 5));
639   CU_ASSERT(nva[1].namelen == 7);
640   CU_ASSERT(0 == memcmp("charlie", nva[1].name, 7));
641   CU_ASSERT(nva[1].valuelen == 5);
642   CU_ASSERT(0 == memcmp("delta", nva[1].value, 5));
643 
644   nghttp2_nv_array_del(nva, mem);
645 
646   /* Large header field is acceptable */
647   rv = nghttp2_nv_array_copy(&nva, &bignv, 1, mem);
648   CU_ASSERT(0 == rv);
649 
650   nghttp2_nv_array_del(nva, mem);
651 
652   mem->free(bignv.value, NULL);
653 }
654 
test_nghttp2_iv_check(void)655 void test_nghttp2_iv_check(void) {
656   nghttp2_settings_entry iv[5];
657 
658   iv[0].settings_id = NGHTTP2_SETTINGS_MAX_CONCURRENT_STREAMS;
659   iv[0].value = 100;
660   iv[1].settings_id = NGHTTP2_SETTINGS_HEADER_TABLE_SIZE;
661   iv[1].value = 1024;
662 
663   CU_ASSERT(nghttp2_iv_check(iv, 2));
664 
665   iv[1].settings_id = NGHTTP2_SETTINGS_INITIAL_WINDOW_SIZE;
666   iv[1].value = NGHTTP2_MAX_WINDOW_SIZE;
667   CU_ASSERT(nghttp2_iv_check(iv, 2));
668 
669   /* Too large window size */
670   iv[1].value = (uint32_t)NGHTTP2_MAX_WINDOW_SIZE + 1;
671   CU_ASSERT(0 == nghttp2_iv_check(iv, 2));
672 
673   /* ENABLE_PUSH only allows 0 or 1 */
674   iv[1].settings_id = NGHTTP2_SETTINGS_ENABLE_PUSH;
675   iv[1].value = 0;
676   CU_ASSERT(nghttp2_iv_check(iv, 2));
677   iv[1].value = 1;
678   CU_ASSERT(nghttp2_iv_check(iv, 2));
679   iv[1].value = 3;
680   CU_ASSERT(!nghttp2_iv_check(iv, 2));
681 
682   /* Undefined SETTINGS ID is allowed */
683   iv[1].settings_id = 1000000009;
684   iv[1].value = 0;
685   CU_ASSERT(nghttp2_iv_check(iv, 2));
686 
687   /* Full size SETTINGS_HEADER_TABLE_SIZE (UINT32_MAX) must be
688      accepted */
689   iv[1].settings_id = NGHTTP2_SETTINGS_HEADER_TABLE_SIZE;
690   iv[1].value = UINT32_MAX;
691   CU_ASSERT(nghttp2_iv_check(iv, 2));
692 
693   /* Too small SETTINGS_MAX_FRAME_SIZE */
694   iv[0].settings_id = NGHTTP2_SETTINGS_MAX_FRAME_SIZE;
695   iv[0].value = NGHTTP2_MAX_FRAME_SIZE_MIN - 1;
696   CU_ASSERT(!nghttp2_iv_check(iv, 1));
697 
698   /* Too large SETTINGS_MAX_FRAME_SIZE */
699   iv[0].settings_id = NGHTTP2_SETTINGS_MAX_FRAME_SIZE;
700   iv[0].value = NGHTTP2_MAX_FRAME_SIZE_MAX + 1;
701   CU_ASSERT(!nghttp2_iv_check(iv, 1));
702 
703   /* Max and min SETTINGS_MAX_FRAME_SIZE */
704   iv[0].settings_id = NGHTTP2_SETTINGS_MAX_FRAME_SIZE;
705   iv[0].value = NGHTTP2_MAX_FRAME_SIZE_MIN;
706   iv[1].settings_id = NGHTTP2_SETTINGS_MAX_FRAME_SIZE;
707   iv[1].value = NGHTTP2_MAX_FRAME_SIZE_MAX;
708   CU_ASSERT(nghttp2_iv_check(iv, 2));
709 }
710