Example #1
0
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);
}
Example #2
0
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);
}
Example #3
0
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);
}
Example #4
0
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);
}
Example #5
0
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);
}
Example #6
0
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);
}
Example #7
0
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);
}
Example #8
0
static int deflate_inflate(nghttp2_hd_deflater *deflater,
                           nghttp2_hd_inflater *inflater, nghttp2_bufs *bufs,
                           nghttp2_nv *nva, size_t nvlen, nghttp2_mem *mem) {
  int rv;

  rv = nghttp2_hd_deflate_hd_bufs(deflater, bufs, nva, nvlen);

  if (rv != 0) {
    return rv;
  }

  rv = (int)inflate_hd(inflater, NULL, bufs, 0, mem);

  if (rv < 0) {
    return rv;
  }

  nghttp2_bufs_reset(bufs);

  return 0;
}
Example #9
0
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);
}
Example #10
0
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);
}
Example #11
0
void test_nghttp2_hd_inflate_indname_noinc(void) {
  nghttp2_hd_inflater inflater;
  nghttp2_bufs bufs;
  ssize_t blocklen;
  nghttp2_nv nv[] = {/* Huffman */
                     MAKE_NV("user-agent", "nghttp2"),
                     /* Expecting no huffman */
                     MAKE_NV("user-agent", "x")};
  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_indname_block(&bufs, 57, &nv[i], 0));

    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);
}
Example #12
0
void test_nghttp2_frame_pack_headers()
{
  nghttp2_hd_deflater deflater;
  nghttp2_hd_inflater inflater;
  nghttp2_headers frame, oframe;
  nghttp2_bufs bufs;
  nghttp2_nv *nva;
  nghttp2_priority_spec pri_spec;
  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_priority_spec_default_init(&pri_spec);

  nghttp2_frame_headers_init(&frame,
                             NGHTTP2_FLAG_END_STREAM |
                             NGHTTP2_FLAG_END_HEADERS,
                             1000000007, NGHTTP2_HCAT_REQUEST,
                             &pri_spec, nva, nvlen);
  rv = nghttp2_frame_pack_headers(&bufs, &frame, &deflater);

  nghttp2_bufs_rewind(&bufs);

  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_HEADERS,
                     NGHTTP2_FLAG_END_STREAM | NGHTTP2_FLAG_END_HEADERS,
                     1000000007, &oframe.hd);
  /* We did not include PRIORITY flag */
  CU_ASSERT(NGHTTP2_DEFAULT_WEIGHT == oframe.pri_spec.weight);

  hdblocklen = nghttp2_bufs_len(&bufs) - NGHTTP2_FRAME_HDLEN;
  CU_ASSERT(hdblocklen ==
            inflate_hd(&inflater, &out, &bufs, NGHTTP2_FRAME_HDLEN));

  CU_ASSERT(7 == out.nvlen);
  CU_ASSERT(nvnameeq("method", &out.nva[0]));
  CU_ASSERT(nvvalueeq("GET", &out.nva[0]));

  nghttp2_frame_headers_free(&oframe);
  nva_out_reset(&out);
  nghttp2_bufs_reset(&bufs);

  memset(&oframe, 0, sizeof(oframe));
  /* Next, include NGHTTP2_FLAG_PRIORITY */
  nghttp2_priority_spec_init(&frame.pri_spec, 1000000009, 12, 1);
  frame.hd.flags |= NGHTTP2_FLAG_PRIORITY;

  rv = nghttp2_frame_pack_headers(&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_HEADERS,
                     NGHTTP2_FLAG_END_STREAM | NGHTTP2_FLAG_END_HEADERS |
                     NGHTTP2_FLAG_PRIORITY,
                     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);

  hdblocklen = nghttp2_bufs_len(&bufs) - NGHTTP2_FRAME_HDLEN
    - nghttp2_frame_priority_len(oframe.hd.flags);
  CU_ASSERT(hdblocklen ==
            inflate_hd(&inflater, &out, &bufs, NGHTTP2_FRAME_HDLEN
                       + nghttp2_frame_priority_len(oframe.hd.flags)));

  nghttp2_nv_array_sort(out.nva, out.nvlen);
  CU_ASSERT(nvnameeq("method", &out.nva[0]));

  nghttp2_frame_headers_free(&oframe);
  nva_out_reset(&out);
  nghttp2_bufs_reset(&bufs);

  nghttp2_bufs_free(&bufs);
  nghttp2_frame_headers_free(&frame);
  nghttp2_hd_inflate_free(&inflater);
  nghttp2_hd_deflate_free(&deflater);
}
Example #13
0
void test_nghttp2_hd_change_table_size(void) {
  nghttp2_hd_deflater deflater;
  nghttp2_hd_inflater inflater;
  nghttp2_nv nva[] = {MAKE_NV("alpha", "bravo"), MAKE_NV("charlie", "delta")};
  nghttp2_nv nva2[] = {MAKE_NV(":path", "/")};
  nghttp2_bufs bufs;
  ssize_t rv;
  nva_out out;
  ssize_t blocklen;
  nghttp2_mem *mem;

  mem = nghttp2_mem_default();
  frame_pack_bufs_init(&bufs);

  nva_out_init(&out);

  nghttp2_hd_deflate_init(&deflater, mem);
  nghttp2_hd_inflate_init(&inflater, mem);

  /* inflater changes notifies 8000 max header table size */
  CU_ASSERT(0 == nghttp2_hd_inflate_change_table_size(&inflater, 8000));
  CU_ASSERT(0 == nghttp2_hd_deflate_change_table_size(&deflater, 8000));

  CU_ASSERT(4096 == deflater.ctx.hd_table_bufsize_max);

  CU_ASSERT(8000 == inflater.ctx.hd_table_bufsize_max);
  CU_ASSERT(8000 == inflater.settings_hd_table_bufsize_max);

  /* This will emit encoding context update with header table size 4096 */
  rv = nghttp2_hd_deflate_hd_bufs(&deflater, &bufs, nva, 2);
  blocklen = nghttp2_bufs_len(&bufs);

  CU_ASSERT(0 == rv);
  CU_ASSERT(blocklen > 0);
  CU_ASSERT(2 == deflater.ctx.hd_table.len);
  CU_ASSERT(4096 == deflater.ctx.hd_table_bufsize_max);

  CU_ASSERT(blocklen == inflate_hd(&inflater, &out, &bufs, 0, mem));
  CU_ASSERT(2 == inflater.ctx.hd_table.len);
  CU_ASSERT(4096 == inflater.ctx.hd_table_bufsize_max);
  CU_ASSERT(8000 == inflater.settings_hd_table_bufsize_max);

  nva_out_reset(&out, mem);
  nghttp2_bufs_reset(&bufs);

  /* inflater changes header table size to 1024 */
  CU_ASSERT(0 == nghttp2_hd_inflate_change_table_size(&inflater, 1024));
  CU_ASSERT(0 == nghttp2_hd_deflate_change_table_size(&deflater, 1024));

  CU_ASSERT(1024 == deflater.ctx.hd_table_bufsize_max);

  CU_ASSERT(1024 == inflater.ctx.hd_table_bufsize_max);
  CU_ASSERT(1024 == inflater.settings_hd_table_bufsize_max);

  rv = nghttp2_hd_deflate_hd_bufs(&deflater, &bufs, nva, 2);
  blocklen = nghttp2_bufs_len(&bufs);

  CU_ASSERT(0 == rv);
  CU_ASSERT(blocklen > 0);
  CU_ASSERT(2 == deflater.ctx.hd_table.len);
  CU_ASSERT(1024 == deflater.ctx.hd_table_bufsize_max);

  CU_ASSERT(blocklen == inflate_hd(&inflater, &out, &bufs, 0, mem));
  CU_ASSERT(2 == inflater.ctx.hd_table.len);
  CU_ASSERT(1024 == inflater.ctx.hd_table_bufsize_max);
  CU_ASSERT(1024 == inflater.settings_hd_table_bufsize_max);

  nva_out_reset(&out, mem);
  nghttp2_bufs_reset(&bufs);

  /* inflater changes header table size to 0 */
  CU_ASSERT(0 == nghttp2_hd_inflate_change_table_size(&inflater, 0));
  CU_ASSERT(0 == nghttp2_hd_deflate_change_table_size(&deflater, 0));

  CU_ASSERT(0 == deflater.ctx.hd_table.len);
  CU_ASSERT(0 == deflater.ctx.hd_table_bufsize_max);

  CU_ASSERT(0 == inflater.ctx.hd_table.len);
  CU_ASSERT(0 == inflater.ctx.hd_table_bufsize_max);
  CU_ASSERT(0 == inflater.settings_hd_table_bufsize_max);

  rv = nghttp2_hd_deflate_hd_bufs(&deflater, &bufs, nva, 2);
  blocklen = nghttp2_bufs_len(&bufs);

  CU_ASSERT(0 == rv);
  CU_ASSERT(blocklen > 0);
  CU_ASSERT(0 == deflater.ctx.hd_table.len);
  CU_ASSERT(0 == deflater.ctx.hd_table_bufsize_max);

  CU_ASSERT(blocklen == inflate_hd(&inflater, &out, &bufs, 0, mem));
  CU_ASSERT(0 == inflater.ctx.hd_table.len);
  CU_ASSERT(0 == inflater.ctx.hd_table_bufsize_max);
  CU_ASSERT(0 == inflater.settings_hd_table_bufsize_max);

  nva_out_reset(&out, mem);
  nghttp2_bufs_reset(&bufs);

  nghttp2_bufs_free(&bufs);
  nghttp2_hd_inflate_free(&inflater);
  nghttp2_hd_deflate_free(&deflater);

  /* Check table buffer is expanded */
  frame_pack_bufs_init(&bufs);

  nghttp2_hd_deflate_init2(&deflater, 8192, mem);
  nghttp2_hd_inflate_init(&inflater, mem);

  /* First inflater changes header table size to 8000 */
  CU_ASSERT(0 == nghttp2_hd_inflate_change_table_size(&inflater, 8000));
  CU_ASSERT(0 == nghttp2_hd_deflate_change_table_size(&deflater, 8000));

  CU_ASSERT(8000 == deflater.ctx.hd_table_bufsize_max);

  CU_ASSERT(8000 == inflater.ctx.hd_table_bufsize_max);
  CU_ASSERT(8000 == inflater.settings_hd_table_bufsize_max);

  rv = nghttp2_hd_deflate_hd_bufs(&deflater, &bufs, nva, 2);
  blocklen = nghttp2_bufs_len(&bufs);

  CU_ASSERT(0 == rv);
  CU_ASSERT(blocklen > 0);
  CU_ASSERT(2 == deflater.ctx.hd_table.len);
  CU_ASSERT(8000 == deflater.ctx.hd_table_bufsize_max);

  CU_ASSERT(blocklen == inflate_hd(&inflater, &out, &bufs, 0, mem));
  CU_ASSERT(2 == inflater.ctx.hd_table.len);
  CU_ASSERT(8000 == inflater.ctx.hd_table_bufsize_max);
  CU_ASSERT(8000 == inflater.settings_hd_table_bufsize_max);

  nva_out_reset(&out, mem);
  nghttp2_bufs_reset(&bufs);

  CU_ASSERT(0 == nghttp2_hd_inflate_change_table_size(&inflater, 16383));
  CU_ASSERT(0 == nghttp2_hd_deflate_change_table_size(&deflater, 16383));

  CU_ASSERT(8192 == deflater.ctx.hd_table_bufsize_max);

  CU_ASSERT(16383 == inflater.ctx.hd_table_bufsize_max);
  CU_ASSERT(16383 == inflater.settings_hd_table_bufsize_max);

  rv = nghttp2_hd_deflate_hd_bufs(&deflater, &bufs, nva, 2);
  blocklen = nghttp2_bufs_len(&bufs);

  CU_ASSERT(0 == rv);
  CU_ASSERT(blocklen > 0);
  CU_ASSERT(2 == deflater.ctx.hd_table.len);
  CU_ASSERT(8192 == deflater.ctx.hd_table_bufsize_max);

  CU_ASSERT(blocklen == inflate_hd(&inflater, &out, &bufs, 0, mem));
  CU_ASSERT(2 == inflater.ctx.hd_table.len);
  CU_ASSERT(8192 == inflater.ctx.hd_table_bufsize_max);
  CU_ASSERT(16383 == inflater.settings_hd_table_bufsize_max);

  nva_out_reset(&out, mem);
  nghttp2_bufs_reset(&bufs);

  /* Lastly, check the error condition */

  rv = nghttp2_hd_emit_table_size(&bufs, 25600);
  CU_ASSERT(rv == 0);
  CU_ASSERT(NGHTTP2_ERR_HEADER_COMP ==
            inflate_hd(&inflater, &out, &bufs, 0, mem));

  nva_out_reset(&out, mem);
  nghttp2_bufs_reset(&bufs);

  nghttp2_hd_inflate_free(&inflater);
  nghttp2_hd_deflate_free(&deflater);

  /* Check that encoder can handle the case where its allowable buffer
     size is less than default size, 4096 */
  nghttp2_hd_deflate_init2(&deflater, 1024, mem);
  nghttp2_hd_inflate_init(&inflater, mem);

  CU_ASSERT(1024 == deflater.ctx.hd_table_bufsize_max);

  /* This emits context update with buffer size 1024 */
  rv = nghttp2_hd_deflate_hd_bufs(&deflater, &bufs, nva, 2);
  blocklen = nghttp2_bufs_len(&bufs);

  CU_ASSERT(0 == rv);
  CU_ASSERT(blocklen > 0);
  CU_ASSERT(2 == deflater.ctx.hd_table.len);
  CU_ASSERT(1024 == deflater.ctx.hd_table_bufsize_max);

  CU_ASSERT(blocklen == inflate_hd(&inflater, &out, &bufs, 0, mem));
  CU_ASSERT(2 == inflater.ctx.hd_table.len);
  CU_ASSERT(1024 == inflater.ctx.hd_table_bufsize_max);
  CU_ASSERT(4096 == inflater.settings_hd_table_bufsize_max);

  nva_out_reset(&out, mem);
  nghttp2_bufs_reset(&bufs);

  nghttp2_hd_inflate_free(&inflater);
  nghttp2_hd_deflate_free(&deflater);

  /* Check that table size UINT32_MAX can be received */
  nghttp2_hd_deflate_init2(&deflater, UINT32_MAX, mem);
  nghttp2_hd_inflate_init(&inflater, mem);

  CU_ASSERT(0 == nghttp2_hd_inflate_change_table_size(&inflater, UINT32_MAX));
  CU_ASSERT(0 == nghttp2_hd_deflate_change_table_size(&deflater, UINT32_MAX));

  rv = nghttp2_hd_deflate_hd_bufs(&deflater, &bufs, nva, 2);
  blocklen = nghttp2_bufs_len(&bufs);

  CU_ASSERT(0 == rv);
  CU_ASSERT(UINT32_MAX == deflater.ctx.hd_table_bufsize_max);

  CU_ASSERT(blocklen == inflate_hd(&inflater, &out, &bufs, 0, mem));
  CU_ASSERT(UINT32_MAX == inflater.ctx.hd_table_bufsize_max);
  CU_ASSERT(UINT32_MAX == inflater.settings_hd_table_bufsize_max);

  nva_out_reset(&out, mem);
  nghttp2_bufs_reset(&bufs);

  nghttp2_hd_inflate_free(&inflater);
  nghttp2_hd_deflate_free(&deflater);

  /* Check that context update emitted twice */
  nghttp2_hd_deflate_init2(&deflater, 4096, mem);
  nghttp2_hd_inflate_init(&inflater, mem);

  CU_ASSERT(0 == nghttp2_hd_inflate_change_table_size(&inflater, 0));
  CU_ASSERT(0 == nghttp2_hd_inflate_change_table_size(&inflater, 3000));
  CU_ASSERT(0 == nghttp2_hd_deflate_change_table_size(&deflater, 0));
  CU_ASSERT(0 == nghttp2_hd_deflate_change_table_size(&deflater, 3000));

  CU_ASSERT(0 == deflater.min_hd_table_bufsize_max);
  CU_ASSERT(3000 == deflater.ctx.hd_table_bufsize_max);

  rv = nghttp2_hd_deflate_hd_bufs(&deflater, &bufs, nva2, 1);
  blocklen = nghttp2_bufs_len(&bufs);

  CU_ASSERT(0 == rv);
  CU_ASSERT(3 < blocklen);
  CU_ASSERT(3000 == deflater.ctx.hd_table_bufsize_max);
  CU_ASSERT(UINT32_MAX == deflater.min_hd_table_bufsize_max);

  CU_ASSERT(blocklen == inflate_hd(&inflater, &out, &bufs, 0, mem));
  CU_ASSERT(3000 == inflater.ctx.hd_table_bufsize_max);
  CU_ASSERT(3000 == inflater.settings_hd_table_bufsize_max);

  nva_out_reset(&out, mem);
  nghttp2_bufs_reset(&bufs);

  nghttp2_hd_inflate_free(&inflater);
  nghttp2_hd_deflate_free(&deflater);

  nghttp2_bufs_free(&bufs);
}
Example #14
0
void test_nghttp2_hd_inflate_clearall_inc(void) {
  nghttp2_hd_inflater inflater;
  nghttp2_bufs bufs;
  ssize_t blocklen;
  nghttp2_nv nv;
  uint8_t value[4061];
  nva_out out;
  nghttp2_mem *mem;

  mem = nghttp2_mem_default();
  bufs_large_init(&bufs, 8192);

  nva_out_init(&out);
  /* Total 4097 bytes space required to hold this entry */
  nv.name = (uint8_t *)"alpha";
  nv.namelen = strlen((char *)nv.name);
  memset(value, '0', sizeof(value));
  value[sizeof(value) - 1] = '\0';
  nv.value = value;
  nv.valuelen = sizeof(value) - 1;

  nv.flags = NGHTTP2_NV_FLAG_NONE;

  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(0 == inflater.ctx.hd_table.len);

  nva_out_reset(&out, mem);

  /* Do it again */
  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(0 == inflater.ctx.hd_table.len);

  nva_out_reset(&out, mem);
  nghttp2_bufs_reset(&bufs);

  /* This time, 4096 bytes space required, which is just fits in the
     header table */
  nv.valuelen = sizeof(value) - 2;

  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);

  nva_out_reset(&out, mem);
  nghttp2_bufs_reset(&bufs);

  nghttp2_bufs_free(&bufs);
  nghttp2_hd_inflate_free(&inflater);
}
Example #15
0
void test_nghttp2_hd_deflate(void) {
  nghttp2_hd_deflater deflater;
  nghttp2_hd_inflater inflater;
  nghttp2_nv nva1[] = {MAKE_NV(":path", "/my-example/index.html"),
                       MAKE_NV(":scheme", "https"), MAKE_NV("hello", "world")};
  nghttp2_nv nva2[] = {MAKE_NV(":path", "/script.js"),
                       MAKE_NV(":scheme", "https")};
  nghttp2_nv nva3[] = {MAKE_NV("cookie", "k1=v1"), MAKE_NV("cookie", "k2=v2"),
                       MAKE_NV("via", "proxy")};
  nghttp2_nv nva4[] = {MAKE_NV(":path", "/style.css"),
                       MAKE_NV("cookie", "k1=v1"), MAKE_NV("cookie", "k1=v1")};
  nghttp2_nv nva5[] = {MAKE_NV(":path", "/style.css"),
                       MAKE_NV("x-nghttp2", "")};
  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));

  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(3 == out.nvlen);
  assert_nv_equal(nva1, out.nva, 3, mem);

  nva_out_reset(&out, mem);
  nghttp2_bufs_reset(&bufs);

  /* Second headers */
  rv = nghttp2_hd_deflate_hd_bufs(&deflater, &bufs, nva2, ARRLEN(nva2));
  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(nva2, out.nva, 2, mem);

  nva_out_reset(&out, mem);
  nghttp2_bufs_reset(&bufs);

  /* Third headers, including same header field name, but value is not
     the same. */
  rv = nghttp2_hd_deflate_hd_bufs(&deflater, &bufs, nva3, ARRLEN(nva3));
  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(3 == out.nvlen);
  assert_nv_equal(nva3, out.nva, 3, mem);

  nva_out_reset(&out, mem);
  nghttp2_bufs_reset(&bufs);

  /* Fourth headers, including duplicate header fields. */
  rv = nghttp2_hd_deflate_hd_bufs(&deflater, &bufs, nva4, ARRLEN(nva4));
  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(3 == out.nvlen);
  assert_nv_equal(nva4, out.nva, 3, mem);

  nva_out_reset(&out, mem);
  nghttp2_bufs_reset(&bufs);

  /* Fifth headers includes empty value */
  rv = nghttp2_hd_deflate_hd_bufs(&deflater, &bufs, nva5, ARRLEN(nva5));
  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(nva5, out.nva, 2, 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);
}
Example #16
0
static void run_nghttp2_session_recv(void) {
  nghttp2_session *session;
  nghttp2_session_callbacks callbacks;
  nghttp2_hd_deflater deflater;
  nghttp2_frame frame;
  nghttp2_bufs bufs;
  nghttp2_nv nv[] = {MAKE_NV(":authority", "example.org"),
                     MAKE_NV(":scheme", "https")};
  nghttp2_settings_entry iv[2];
  my_user_data ud;
  data_feed df;
  int rv;
  nghttp2_nv *nva;
  size_t nvlen;

  rv = frame_pack_bufs_init(&bufs);

  if (rv != 0) {
    return;
  }

  memset(&callbacks, 0, sizeof(nghttp2_session_callbacks));
  callbacks.recv_callback = data_feed_recv_callback;
  ud.df = &df;

  nghttp2_failmalloc_pause();
  nvlen = ARRLEN(nv);
  nghttp2_nv_array_copy(&nva, nv, nvlen, nghttp2_mem_fm());
  nghttp2_hd_deflate_init(&deflater, nghttp2_mem_fm());
  nghttp2_session_server_new3(&session, &callbacks, &ud, NULL,
                              nghttp2_mem_fm());
  nghttp2_failmalloc_unpause();

  /* HEADERS */
  nghttp2_failmalloc_pause();
  nghttp2_frame_headers_init(&frame.headers, NGHTTP2_FLAG_END_STREAM, 1,
                             NGHTTP2_HCAT_REQUEST, NULL, nva, nvlen);
  nghttp2_frame_pack_headers(&bufs, &frame.headers, &deflater);
  nghttp2_frame_headers_free(&frame.headers, nghttp2_mem_fm());
  data_feed_init(&df, &bufs);
  nghttp2_bufs_reset(&bufs);

  nghttp2_failmalloc_unpause();

  rv = nghttp2_session_recv(session);
  if (rv != 0) {
    goto fail;
  }

  /* PING */
  nghttp2_failmalloc_pause();
  nghttp2_frame_ping_init(&frame.ping, NGHTTP2_FLAG_NONE, NULL);
  nghttp2_frame_pack_ping(&bufs, &frame.ping);
  nghttp2_frame_ping_free(&frame.ping);
  data_feed_init(&df, &bufs);
  nghttp2_bufs_reset(&bufs);

  nghttp2_failmalloc_unpause();

  rv = nghttp2_session_recv(session);
  if (rv != 0) {
    goto fail;
  }

  /* RST_STREAM */
  nghttp2_failmalloc_pause();
  nghttp2_frame_rst_stream_init(&frame.rst_stream, 1, NGHTTP2_PROTOCOL_ERROR);
  nghttp2_frame_pack_rst_stream(&bufs, &frame.rst_stream);
  nghttp2_frame_rst_stream_free(&frame.rst_stream);
  nghttp2_bufs_reset(&bufs);

  nghttp2_failmalloc_unpause();

  rv = nghttp2_session_recv(session);
  if (rv != 0) {
    goto fail;
  }

  /* SETTINGS */
  nghttp2_failmalloc_pause();
  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;
  nghttp2_frame_settings_init(&frame.settings, NGHTTP2_FLAG_NONE,
                              nghttp2_frame_iv_copy(iv, 2, nghttp2_mem_fm()),
                              2);
  nghttp2_frame_pack_settings(&bufs, &frame.settings);
  nghttp2_frame_settings_free(&frame.settings, nghttp2_mem_fm());
  nghttp2_bufs_reset(&bufs);

  nghttp2_failmalloc_unpause();

  rv = nghttp2_session_recv(session);
  if (rv != 0) {
    goto fail;
  }

fail:
  nghttp2_bufs_free(&bufs);
  nghttp2_session_del(session);
  nghttp2_hd_deflate_free(&deflater);
}