Example #1
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 #2
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 #3
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 #4
0
static void run_nghttp2_frame_pack_headers(void) {
  nghttp2_hd_deflater deflater;
  nghttp2_hd_inflater inflater;
  nghttp2_frame frame, oframe;
  nghttp2_bufs bufs;
  nghttp2_nv nv[] = {MAKE_NV(":host", "example.org"),
                     MAKE_NV(":scheme", "https")};
  int rv;
  nghttp2_nv *nva;
  size_t nvlen;

  rv = frame_pack_bufs_init(&bufs);

  if (rv != 0) {
    return;
  }

  rv = nghttp2_hd_deflate_init(&deflater, nghttp2_mem_fm());
  if (rv != 0) {
    goto deflate_init_fail;
  }
  rv = nghttp2_hd_inflate_init(&inflater, nghttp2_mem_fm());
  if (rv != 0) {
    goto inflate_init_fail;
  }
  nvlen = ARRLEN(nv);
  rv = nghttp2_nv_array_copy(&nva, nv, nvlen, nghttp2_mem_fm());
  if (rv < 0) {
    goto nv_copy_fail;
  }
  nghttp2_frame_headers_init(&frame.headers, NGHTTP2_FLAG_END_STREAM, 1,
                             NGHTTP2_HCAT_REQUEST, NULL, nva, nvlen);
  rv = nghttp2_frame_pack_headers(&bufs, &frame.headers, &deflater);
  if (rv != 0) {
    goto fail;
  }
  rv = unpack_framebuf(&oframe, &bufs);
  if (rv != 0) {
    goto fail;
  }
  nghttp2_frame_headers_free(&oframe.headers, nghttp2_mem_fm());

fail:
  nghttp2_frame_headers_free(&frame.headers, nghttp2_mem_fm());
nv_copy_fail:
  nghttp2_hd_inflate_free(&inflater);
inflate_init_fail:
  nghttp2_hd_deflate_free(&deflater);
deflate_init_fail:
  nghttp2_bufs_free(&bufs);
}
Example #5
0
void test_nghttp2_frame_pack_push_promise()
{
  nghttp2_hd_deflater deflater;
  nghttp2_hd_inflater inflater;
  nghttp2_push_promise frame, oframe;
  nghttp2_bufs bufs;
  nghttp2_nv *nva;
  ssize_t nvlen;
  nva_out out;
  ssize_t hdblocklen;
  int rv;

  frame_pack_bufs_init(&bufs);

  nva_out_init(&out);
  nghttp2_hd_deflate_init(&deflater);
  nghttp2_hd_inflate_init(&inflater);

  nva = headers();
  nvlen = HEADERS_LENGTH;
  nghttp2_frame_push_promise_init(&frame, NGHTTP2_FLAG_END_HEADERS,
                                  1000000007, (1U << 31) - 1, nva, nvlen);
  rv = nghttp2_frame_pack_push_promise(&bufs, &frame, &deflater);

  CU_ASSERT(0 == rv);
  CU_ASSERT(nghttp2_bufs_len(&bufs) > 0);
  CU_ASSERT(0 == unpack_framebuf((nghttp2_frame*)&oframe, &bufs));

  check_frame_header(nghttp2_bufs_len(&bufs) - NGHTTP2_FRAME_HDLEN,
                     NGHTTP2_PUSH_PROMISE,
                     NGHTTP2_FLAG_END_HEADERS, 1000000007, &oframe.hd);
  CU_ASSERT((1U << 31) - 1 == oframe.promised_stream_id);

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

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

  nva_out_reset(&out);
  nghttp2_bufs_free(&bufs);
  nghttp2_frame_push_promise_free(&oframe);
  nghttp2_frame_push_promise_free(&frame);
  nghttp2_hd_inflate_free(&inflater);
  nghttp2_hd_deflate_free(&deflater);
}
Example #6
0
void test_nghttp2_frame_pack_settings()
{
  nghttp2_settings frame, oframe;
  nghttp2_bufs bufs;
  int i;
  int rv;
  nghttp2_settings_entry iv[] =
    {
      {
        NGHTTP2_SETTINGS_MAX_CONCURRENT_STREAMS, 256
      },
      {
        NGHTTP2_SETTINGS_INITIAL_WINDOW_SIZE, 16384
      },
      {
        NGHTTP2_SETTINGS_HEADER_TABLE_SIZE, 4096
      }
    };

  frame_pack_bufs_init(&bufs);

  nghttp2_frame_settings_init(&frame, NGHTTP2_FLAG_NONE,
                              nghttp2_frame_iv_copy(iv, 3), 3);
  rv = nghttp2_frame_pack_settings(&bufs, &frame);

  CU_ASSERT(0 == rv);
  CU_ASSERT(NGHTTP2_FRAME_HDLEN + 3 * NGHTTP2_FRAME_SETTINGS_ENTRY_LENGTH ==
            nghttp2_bufs_len(&bufs));

  CU_ASSERT(0 == unpack_framebuf((nghttp2_frame*)&oframe, &bufs));
  check_frame_header(3 * NGHTTP2_FRAME_SETTINGS_ENTRY_LENGTH,
                     NGHTTP2_SETTINGS, NGHTTP2_FLAG_NONE, 0, &oframe.hd);
  CU_ASSERT(3 == oframe.niv);
  for(i = 0; i < 3; ++i) {
    CU_ASSERT(iv[i].settings_id == oframe.iv[i].settings_id);
    CU_ASSERT(iv[i].value == oframe.iv[i].value);
  }

  nghttp2_bufs_free(&bufs);
  nghttp2_frame_settings_free(&frame);
  nghttp2_frame_settings_free(&oframe);
}
Example #7
0
void test_nghttp2_frame_pack_window_update(void)
{
  nghttp2_window_update frame, oframe;
  nghttp2_bufs bufs;
  int rv;

  frame_pack_bufs_init(&bufs);

  nghttp2_frame_window_update_init(&frame, NGHTTP2_FLAG_NONE,
                                   1000000007, 4096);
  rv = nghttp2_frame_pack_window_update(&bufs, &frame);

  CU_ASSERT(0 == rv);
  CU_ASSERT(NGHTTP2_FRAME_HDLEN + 4 == nghttp2_bufs_len(&bufs));
  CU_ASSERT(0 == unpack_framebuf((nghttp2_frame*)&oframe, &bufs));
  check_frame_header(4, NGHTTP2_WINDOW_UPDATE, NGHTTP2_FLAG_NONE,
                     1000000007, &oframe.hd);
  CU_ASSERT(4096 == oframe.window_size_increment);

  nghttp2_bufs_free(&bufs);
  nghttp2_frame_window_update_free(&oframe);
  nghttp2_frame_window_update_free(&frame);
}
Example #8
0
void test_nghttp2_frame_pack_ping(void)
{
  nghttp2_ping frame, oframe;
  nghttp2_bufs bufs;
  const uint8_t opaque_data[] = "01234567";
  int rv;

  frame_pack_bufs_init(&bufs);

  nghttp2_frame_ping_init(&frame, NGHTTP2_FLAG_ACK, opaque_data);
  rv = nghttp2_frame_pack_ping(&bufs, &frame);

  CU_ASSERT(0 == rv);
  CU_ASSERT(NGHTTP2_FRAME_HDLEN + 8 == nghttp2_bufs_len(&bufs));
  CU_ASSERT(0 == unpack_framebuf((nghttp2_frame*)&oframe, &bufs));
  check_frame_header(8, NGHTTP2_PING, NGHTTP2_FLAG_ACK, 0, &oframe.hd);
  CU_ASSERT(memcmp(opaque_data, oframe.opaque_data, sizeof(opaque_data) - 1)
            == 0);

  nghttp2_bufs_free(&bufs);
  nghttp2_frame_ping_free(&oframe);
  nghttp2_frame_ping_free(&frame);
}
Example #9
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 #10
0
void test_nghttp2_frame_pack_altsvc(void)
{
  nghttp2_extension frame, oframe;
  nghttp2_ext_altsvc altsvc, oaltsvc;
  nghttp2_bufs bufs;
  nghttp2_buf *buf;
  size_t protocol_id_len, host_len, origin_len;
  uint8_t *protocol_id, *host, *origin;
  uint8_t *data;
  size_t datalen;
  int rv;
  size_t payloadlen;

  protocol_id_len = strlen("h2");
  host_len = strlen("h2.example.org");
  origin_len = strlen("www.example.org");

  datalen = protocol_id_len + host_len + origin_len;
  data = malloc(datalen);

  memcpy(data, "h2", protocol_id_len);
  protocol_id = data;

  memcpy(data + protocol_id_len, "h2.example.org", host_len);
  host = data + protocol_id_len;

  memcpy(data + protocol_id_len + host_len,
         "http://www.example.org", origin_len);
  origin = data + protocol_id_len + host_len;

  frame_pack_bufs_init(&bufs);

  frame.payload = &altsvc;

  nghttp2_frame_altsvc_init(&frame, 1000000007, 1u << 31, 4000,
                            protocol_id, protocol_id_len,
                            host, host_len, origin, origin_len);

  rv = nghttp2_frame_pack_altsvc(&bufs, &frame);

  CU_ASSERT(0 == rv);

  CU_ASSERT((ssize_t)(NGHTTP2_FRAME_HDLEN + NGHTTP2_ALTSVC_MINLEN + datalen) ==
            nghttp2_bufs_len(&bufs));

  oframe.payload = &oaltsvc;

  CU_ASSERT(0 == unpack_framebuf((nghttp2_frame*)&oframe, &bufs));

  check_frame_header(NGHTTP2_ALTSVC_MINLEN + datalen,
                     NGHTTP2_EXT_ALTSVC, NGHTTP2_FLAG_NONE,
                     1000000007, &oframe.hd);
  CU_ASSERT(1u << 31 == oaltsvc.max_age);
  CU_ASSERT(4000 == oaltsvc.port);

  CU_ASSERT(protocol_id_len == oaltsvc.protocol_id_len);
  CU_ASSERT(memcmp(protocol_id, oaltsvc.protocol_id, protocol_id_len) == 0);

  CU_ASSERT(host_len == oaltsvc.host_len);
  CU_ASSERT(memcmp(host, oaltsvc.host, host_len) == 0);

  CU_ASSERT(origin_len == oaltsvc.origin_len);
  CU_ASSERT(memcmp(origin, oaltsvc.origin, origin_len) == 0);

  nghttp2_frame_altsvc_free(&oframe);
  nghttp2_frame_altsvc_free(&frame);

  memset(&oframe, 0, sizeof(oframe));
  memset(&oaltsvc, 0, sizeof(oaltsvc));

  buf = &bufs.head->buf;

  CU_ASSERT(buf->pos - buf->begin == 1);

  /* Check no origin case */

  payloadlen = NGHTTP2_ALTSVC_MINLEN + protocol_id_len + host_len;
  nghttp2_put_uint32be(buf->pos, (uint32_t)((payloadlen << 8) + buf->pos[3]));

  oframe.payload = &oaltsvc;

  CU_ASSERT(0 ==
            nghttp2_frame_unpack_altsvc_payload
            (&oframe,
             buf->pos + NGHTTP2_FRAME_HDLEN,
             NGHTTP2_ALTSVC_FIXED_PARTLEN,
             buf->pos + NGHTTP2_FRAME_HDLEN + NGHTTP2_ALTSVC_FIXED_PARTLEN,
             payloadlen - NGHTTP2_ALTSVC_FIXED_PARTLEN));

  CU_ASSERT(protocol_id_len == oaltsvc.protocol_id_len);
  CU_ASSERT(host_len == oaltsvc.host_len);
  CU_ASSERT(0 == oaltsvc.origin_len);

  memset(&oframe, 0, sizeof(oframe));
  memset(&oaltsvc, 0, sizeof(oaltsvc));

  /* Check insufficient payload length for host */
  payloadlen = NGHTTP2_ALTSVC_MINLEN + protocol_id_len + host_len - 1;
  nghttp2_put_uint32be(buf->pos, (uint32_t)((payloadlen << 8) + buf->pos[3]));

  oframe.payload = &oaltsvc;

  CU_ASSERT(NGHTTP2_ERR_FRAME_SIZE_ERROR ==
            nghttp2_frame_unpack_altsvc_payload
            (&oframe,
             buf->pos + NGHTTP2_FRAME_HDLEN,
             NGHTTP2_ALTSVC_FIXED_PARTLEN,
             buf->pos + NGHTTP2_FRAME_HDLEN + NGHTTP2_ALTSVC_FIXED_PARTLEN,
             payloadlen - NGHTTP2_ALTSVC_FIXED_PARTLEN));

  memset(&oframe, 0, sizeof(oframe));
  memset(&oaltsvc, 0, sizeof(oaltsvc));

  /* Check no host case */
  payloadlen = NGHTTP2_ALTSVC_MINLEN + protocol_id_len;
  nghttp2_put_uint32be(buf->pos, (uint32_t)((payloadlen << 8) + buf->pos[3]));
  buf->pos[NGHTTP2_FRAME_HDLEN + NGHTTP2_ALTSVC_FIXED_PARTLEN
           + protocol_id_len] = 0;

  oframe.payload = &oaltsvc;

  CU_ASSERT(0 ==
            nghttp2_frame_unpack_altsvc_payload
            (&oframe,
             buf->pos + NGHTTP2_FRAME_HDLEN,
             NGHTTP2_ALTSVC_FIXED_PARTLEN,
             buf->pos + NGHTTP2_FRAME_HDLEN + NGHTTP2_ALTSVC_FIXED_PARTLEN,
             payloadlen - NGHTTP2_ALTSVC_FIXED_PARTLEN));

  CU_ASSERT(protocol_id_len == oaltsvc.protocol_id_len);
  CU_ASSERT(0 == oaltsvc.host_len);
  CU_ASSERT(0 == oaltsvc.origin_len);

  memset(&oframe, 0, sizeof(oframe));
  memset(&oaltsvc, 0, sizeof(oaltsvc));

  /* Check missing Host-Len */
  payloadlen = NGHTTP2_ALTSVC_FIXED_PARTLEN + protocol_id_len;
  nghttp2_put_uint32be(buf->pos, (uint32_t)((payloadlen << 8) + buf->pos[3]));

  oframe.payload = &oaltsvc;

  CU_ASSERT(NGHTTP2_ERR_FRAME_SIZE_ERROR ==
            nghttp2_frame_unpack_altsvc_payload
            (&oframe,
             buf->pos + NGHTTP2_FRAME_HDLEN,
             NGHTTP2_ALTSVC_FIXED_PARTLEN,
             buf->pos + NGHTTP2_FRAME_HDLEN + NGHTTP2_ALTSVC_FIXED_PARTLEN,
             payloadlen - NGHTTP2_ALTSVC_FIXED_PARTLEN));

  memset(&oframe, 0, sizeof(oframe));
  memset(&oaltsvc, 0, sizeof(oaltsvc));

  nghttp2_bufs_free(&bufs);
}