nghttp2/tests/failmalloc_test.c

603 lines
18 KiB
C
Raw Normal View History

2012-04-24 15:51:06 +02:00
/*
2013-07-12 17:19:03 +02:00
* nghttp2 - HTTP/2.0 C Library
2012-04-24 15:51:06 +02:00
*
* Copyright (c) 2012 Tatsuhiro Tsujikawa
*
* Permission is hereby granted, free of charge, to any person obtaining
* a copy of this software and associated documentation files (the
* "Software"), to deal in the Software without restriction, including
* without limitation the rights to use, copy, modify, merge, publish,
* distribute, sublicense, and/or sell copies of the Software, and to
* permit persons to whom the Software is furnished to do so, subject to
* the following conditions:
*
* The above copyright notice and this permission notice shall be
* included in all copies or substantial portions of the Software.
*
* THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND,
* EXPRESS OR IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF
* MERCHANTABILITY, FITNESS FOR A PARTICULAR PURPOSE AND
* NONINFRINGEMENT. IN NO EVENT SHALL THE AUTHORS OR COPYRIGHT HOLDERS BE
* LIABLE FOR ANY CLAIM, DAMAGES OR OTHER LIABILITY, WHETHER IN AN ACTION
* OF CONTRACT, TORT OR OTHERWISE, ARISING FROM, OUT OF OR IN CONNECTION
* WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS IN THE SOFTWARE.
*/
#include "failmalloc_test.h"
#include <CUnit/CUnit.h>
#include <stdio.h>
#include <assert.h>
2013-07-12 17:19:03 +02:00
#include "nghttp2_session.h"
#include "nghttp2_stream.h"
#include "nghttp2_frame.h"
#include "nghttp2_helper.h"
2012-04-24 15:51:06 +02:00
#include "malloc_wrapper.h"
2013-07-12 17:19:03 +02:00
#include "nghttp2_test_helper.h"
2012-04-24 15:51:06 +02:00
static char* strcopy(const char* s)
{
size_t len = strlen(s);
char *dest = malloc(len+1);
if(dest == NULL) {
return NULL;
}
memcpy(dest, s, len);
dest[len] = '\0';
return dest;
}
typedef struct {
uint8_t data[8192];
uint8_t *datamark, *datalimit;
} data_feed;
typedef struct {
data_feed *df;
size_t data_source_length;
} my_user_data;
static void data_feed_init(data_feed *df, uint8_t *data, size_t data_length)
{
assert(data_length <= sizeof(df->data));
memcpy(df->data, data, data_length);
df->datamark = df->data;
df->datalimit = df->data+data_length;
}
2013-07-12 17:19:03 +02:00
static ssize_t null_send_callback(nghttp2_session *session,
2012-04-24 15:51:06 +02:00
const uint8_t* data, size_t len, int flags,
void *user_data)
{
return len;
}
2013-07-12 17:19:03 +02:00
static ssize_t data_feed_recv_callback(nghttp2_session *session,
2012-04-24 15:51:06 +02:00
uint8_t* data, size_t len, int flags,
void *user_data)
{
data_feed *df = ((my_user_data*)user_data)->df;
size_t avail = df->datalimit - df->datamark;
2013-07-12 17:19:03 +02:00
size_t wlen = nghttp2_min(avail, len);
2012-04-24 15:51:06 +02:00
memcpy(data, df->datamark, wlen);
df->datamark += wlen;
return wlen;
}
static ssize_t fixed_length_data_source_read_callback
2013-07-12 17:19:03 +02:00
(nghttp2_session *session, int32_t stream_id,
2012-04-24 15:51:06 +02:00
uint8_t *buf, size_t len, int *eof,
2013-07-12 17:19:03 +02:00
nghttp2_data_source *source, void *user_data)
2012-04-24 15:51:06 +02:00
{
my_user_data *ud = (my_user_data*)user_data;
size_t wlen;
if(len < ud->data_source_length) {
wlen = len;
} else {
wlen = ud->data_source_length;
}
ud->data_source_length -= wlen;
if(ud->data_source_length == 0) {
*eof = 1;
}
return wlen;
}
2013-07-12 17:19:03 +02:00
static ssize_t get_credential_ncerts(nghttp2_session *session,
const nghttp2_origin *origin,
2012-04-24 15:51:06 +02:00
void *user_data)
{
if(strcmp("example.org", origin->host) == 0 &&
strcmp("https", origin->scheme) == 0 &&
443 == origin->port) {
return 2;
} else {
return 0;
}
}
2013-07-12 17:19:03 +02:00
static ssize_t get_credential_cert(nghttp2_session *session,
const nghttp2_origin *origin,
size_t idx,
2012-04-24 15:51:06 +02:00
uint8_t *cert, size_t certlen,
void *user_data)
{
size_t len = strlen(origin->host);
if(certlen == 0) {
return len;
} else {
assert(certlen == len);
memcpy(cert, origin->host, len);
return 0;
}
}
2013-07-12 17:19:03 +02:00
static ssize_t get_credential_proof(nghttp2_session *session,
const nghttp2_origin *origin,
2012-04-24 15:51:06 +02:00
uint8_t *proof, size_t prooflen,
void *uer_data)
{
size_t len = strlen(origin->scheme);
if(prooflen == 0) {
return len;
} else {
assert(prooflen == len);
memcpy(proof, origin->scheme, len);
return 0;
}
}
#define TEST_FAILMALLOC_RUN(FUN) \
size_t nmalloc, i; \
\
2013-07-12 17:19:03 +02:00
nghttp2_failmalloc = 0; \
nghttp2_nmalloc = 0; \
2012-04-24 15:51:06 +02:00
FUN(); \
2013-07-12 17:19:03 +02:00
nmalloc = nghttp2_nmalloc; \
2012-04-24 15:51:06 +02:00
\
2013-07-12 17:19:03 +02:00
nghttp2_failmalloc = 1; \
2012-04-24 15:51:06 +02:00
for(i = 0; i < nmalloc; ++i) { \
2013-07-12 17:19:03 +02:00
nghttp2_nmalloc = 0; \
nghttp2_failstart = i; \
2012-04-24 15:51:06 +02:00
/* printf("i=%zu\n", i); */ \
FUN(); \
2013-07-12 17:19:03 +02:00
/* printf("nmalloc=%d\n", nghttp2_nmalloc); */ \
} \
2013-07-12 17:19:03 +02:00
nghttp2_failmalloc = 0;
2012-04-24 15:51:06 +02:00
2013-07-12 17:19:03 +02:00
static void run_nghttp2_session_send(void)
2012-04-24 15:51:06 +02:00
{
2013-07-12 17:19:03 +02:00
nghttp2_session *session;
nghttp2_session_callbacks callbacks;
2012-04-24 15:51:06 +02:00
const char *nv[] = { ":host", "example.org",
":scheme", "https",
NULL };
2013-07-12 17:19:03 +02:00
nghttp2_data_provider data_prd;
nghttp2_settings_entry iv[2];
2012-04-24 15:51:06 +02:00
my_user_data ud;
int rv;
2013-07-12 17:19:03 +02:00
memset(&callbacks, 0, sizeof(nghttp2_session_callbacks));
2012-04-24 15:51:06 +02:00
callbacks.send_callback = null_send_callback;
callbacks.get_credential_ncerts = get_credential_ncerts;
callbacks.get_credential_cert = get_credential_cert;
callbacks.get_credential_proof = get_credential_proof;
data_prd.read_callback = fixed_length_data_source_read_callback;
ud.data_source_length = 64*1024;
2013-07-12 17:19:03 +02:00
iv[0].settings_id = NGHTTP2_SETTINGS_UPLOAD_BANDWIDTH;
iv[0].flags = NGHTTP2_ID_FLAG_SETTINGS_PERSIST_VALUE;
2012-04-24 15:51:06 +02:00
iv[0].value = 256;
2013-07-12 17:19:03 +02:00
iv[1].settings_id = NGHTTP2_SETTINGS_MAX_CONCURRENT_STREAMS;
iv[1].flags = NGHTTP2_ID_FLAG_SETTINGS_NONE;
2012-04-24 15:51:06 +02:00
iv[1].value = 100;
2013-07-12 17:19:03 +02:00
rv = nghttp2_session_client_new(&session, NGHTTP2_PROTO_SPDY3,
2012-04-24 15:51:06 +02:00
&callbacks, &ud);
if(rv != 0) {
goto client_new_fail;
}
2013-07-12 17:19:03 +02:00
rv = nghttp2_submit_request(session, 3, nv, &data_prd, NULL);
2012-04-24 15:51:06 +02:00
if(rv != 0) {
goto fail;
}
2013-07-12 17:19:03 +02:00
rv = nghttp2_submit_syn_stream(session, NGHTTP2_CTRL_FLAG_NONE,
2012-04-24 15:51:06 +02:00
0, 3, nv, NULL);
if(rv != 0) {
goto fail;
}
2013-07-12 17:19:03 +02:00
rv = nghttp2_session_send(session);
2012-04-24 15:51:06 +02:00
if(rv != 0) {
goto fail;
}
/* The SYN_STREAM submitted by the previous
2013-07-12 17:19:03 +02:00
nghttp2_submit_syn_stream will have stream ID 3. Send HEADERS to
2012-04-24 15:51:06 +02:00
that stream. */
2013-07-12 17:19:03 +02:00
rv = nghttp2_submit_headers(session, NGHTTP2_CTRL_FLAG_NONE, 3, nv);
2012-04-24 15:51:06 +02:00
if(rv != 0) {
goto fail;
}
2013-07-12 17:19:03 +02:00
rv = nghttp2_submit_data(session, 3, NGHTTP2_DATA_FLAG_FIN, &data_prd);
2012-04-24 15:51:06 +02:00
if(rv != 0) {
goto fail;
}
2013-07-12 17:19:03 +02:00
rv = nghttp2_session_send(session);
2012-04-24 15:51:06 +02:00
if(rv != 0) {
goto fail;
}
2013-07-12 17:19:03 +02:00
rv = nghttp2_submit_rst_stream(session, 3, NGHTTP2_CANCEL);
2012-04-24 15:51:06 +02:00
if(rv != 0) {
goto fail;
}
2013-07-12 17:19:03 +02:00
rv = nghttp2_session_send(session);
2012-04-24 15:51:06 +02:00
if(rv != 0) {
goto fail;
}
/* Sending against half-closed stream */
2013-07-12 17:19:03 +02:00
rv = nghttp2_submit_headers(session, NGHTTP2_CTRL_FLAG_NONE, 3, nv);
2012-04-24 15:51:06 +02:00
if(rv != 0) {
goto fail;
}
2013-07-12 17:19:03 +02:00
rv = nghttp2_submit_data(session, 3, NGHTTP2_DATA_FLAG_FIN, &data_prd);
2012-04-24 15:51:06 +02:00
if(rv != 0) {
goto fail;
}
2013-07-12 17:19:03 +02:00
rv = nghttp2_submit_ping(session);
2012-04-24 15:51:06 +02:00
if(rv != 0) {
goto fail;
}
2013-07-12 17:19:03 +02:00
rv = nghttp2_submit_settings(session, NGHTTP2_FLAG_SETTINGS_NONE, iv, 2);
2012-04-24 15:51:06 +02:00
if(rv != 0) {
goto fail;
}
2013-07-12 17:19:03 +02:00
rv = nghttp2_session_send(session);
2012-04-24 15:51:06 +02:00
if(rv != 0) {
goto fail;
}
2013-07-12 17:19:03 +02:00
rv = nghttp2_submit_goaway(session, NGHTTP2_GOAWAY_OK);
2012-04-24 15:51:06 +02:00
if(rv != 0) {
goto fail;
}
2013-07-12 17:19:03 +02:00
rv = nghttp2_session_send(session);
2012-04-24 15:51:06 +02:00
if(rv != 0) {
goto fail;
}
fail:
2013-07-12 17:19:03 +02:00
nghttp2_session_del(session);
2012-04-24 15:51:06 +02:00
client_new_fail:
;
}
2013-07-12 17:19:03 +02:00
void test_nghttp2_session_send(void)
2012-04-24 15:51:06 +02:00
{
2013-07-12 17:19:03 +02:00
TEST_FAILMALLOC_RUN(run_nghttp2_session_send);
2012-04-24 15:51:06 +02:00
}
2013-07-12 17:19:03 +02:00
static void run_nghttp2_session_recv(void)
2012-04-24 15:51:06 +02:00
{
2013-07-12 17:19:03 +02:00
nghttp2_session *session;
nghttp2_session_callbacks callbacks;
nghttp2_zlib deflater;
nghttp2_frame frame;
2012-04-24 15:51:06 +02:00
uint8_t *buf = NULL, *nvbuf = NULL;
size_t buflen = 0, nvbuflen = 0;
ssize_t framelen;
const char *nv[] = { ":host", "example.org",
":scheme", "https",
NULL };
2013-07-12 17:19:03 +02:00
nghttp2_settings_entry iv[2];
nghttp2_mem_chunk proof;
nghttp2_mem_chunk *certs;
2012-04-24 15:51:06 +02:00
size_t ncerts;
my_user_data ud;
data_feed df;
int rv;
2013-07-12 17:19:03 +02:00
memset(&callbacks, 0, sizeof(nghttp2_session_callbacks));
2012-04-24 15:51:06 +02:00
callbacks.recv_callback = data_feed_recv_callback;
ud.df = &df;
2013-07-12 17:19:03 +02:00
nghttp2_failmalloc_pause();
nghttp2_zlib_deflate_hd_init(&deflater, 1, NGHTTP2_PROTO_SPDY3);
nghttp2_session_server_new(&session, NGHTTP2_PROTO_SPDY3, &callbacks, &ud);
nghttp2_failmalloc_unpause();
2012-04-24 15:51:06 +02:00
/* SYN_STREAM */
2013-07-12 17:19:03 +02:00
nghttp2_failmalloc_pause();
nghttp2_frame_syn_stream_init(&frame.syn_stream, NGHTTP2_PROTO_SPDY3,
NGHTTP2_CTRL_FLAG_FIN, 1, 0, 2,
nghttp2_frame_nv_copy(nv));
framelen = nghttp2_frame_pack_syn_stream(&buf, &buflen,
2012-04-24 15:51:06 +02:00
&nvbuf, &nvbuflen,
&frame.syn_stream, &deflater);
2013-07-12 17:19:03 +02:00
nghttp2_frame_syn_stream_free(&frame.syn_stream);
2012-04-24 15:51:06 +02:00
data_feed_init(&df, buf, framelen);
2013-07-12 17:19:03 +02:00
nghttp2_failmalloc_unpause();
2012-04-24 15:51:06 +02:00
2013-07-12 17:19:03 +02:00
rv = nghttp2_session_recv(session);
2012-04-24 15:51:06 +02:00
if(rv != 0) {
goto fail;
}
/* PING */
2013-07-12 17:19:03 +02:00
nghttp2_failmalloc_pause();
nghttp2_frame_ping_init(&frame.ping, NGHTTP2_PROTO_SPDY3, 1);
framelen = nghttp2_frame_pack_ping(&buf, &buflen, &frame.ping);
nghttp2_frame_ping_free(&frame.ping);
2012-04-24 15:51:06 +02:00
data_feed_init(&df, buf, framelen);
2013-07-12 17:19:03 +02:00
nghttp2_failmalloc_unpause();
2012-04-24 15:51:06 +02:00
2013-07-12 17:19:03 +02:00
rv = nghttp2_session_recv(session);
2012-04-24 15:51:06 +02:00
if(rv != 0) {
goto fail;
}
/* RST_STREAM */
2013-07-12 17:19:03 +02:00
nghttp2_failmalloc_pause();
nghttp2_frame_rst_stream_init(&frame.rst_stream, NGHTTP2_PROTO_SPDY3, 1,
NGHTTP2_PROTOCOL_ERROR);
framelen = nghttp2_frame_pack_rst_stream(&buf, &buflen, &frame.rst_stream);
nghttp2_frame_rst_stream_free(&frame.rst_stream);
nghttp2_failmalloc_unpause();
rv = nghttp2_session_recv(session);
2012-04-24 15:51:06 +02:00
if(rv != 0) {
goto fail;
}
/* SETTINGS */
2013-07-12 17:19:03 +02:00
nghttp2_failmalloc_pause();
iv[0].settings_id = NGHTTP2_SETTINGS_UPLOAD_BANDWIDTH;
iv[0].flags = NGHTTP2_ID_FLAG_SETTINGS_PERSIST_VALUE;
2012-04-24 15:51:06 +02:00
iv[0].value = 256;
2013-07-12 17:19:03 +02:00
iv[1].settings_id = NGHTTP2_SETTINGS_MAX_CONCURRENT_STREAMS;
iv[1].flags = NGHTTP2_ID_FLAG_SETTINGS_NONE;
2012-04-24 15:51:06 +02:00
iv[1].value = 100;
2013-07-12 17:19:03 +02:00
nghttp2_frame_settings_init(&frame.settings, NGHTTP2_PROTO_SPDY3,
NGHTTP2_FLAG_SETTINGS_CLEAR_SETTINGS,
nghttp2_frame_iv_copy(iv, 2), 2);
framelen = nghttp2_frame_pack_settings(&buf, &buflen, &frame.settings);
nghttp2_frame_settings_free(&frame.settings);
nghttp2_failmalloc_unpause();
rv = nghttp2_session_recv(session);
2012-04-24 15:51:06 +02:00
if(rv != 0) {
goto fail;
}
/* CREDENTIAL */
2013-07-12 17:19:03 +02:00
nghttp2_failmalloc_pause();
2012-04-24 15:51:06 +02:00
proof.data = (uint8_t*)strcopy("PROOF");
proof.length = strlen("PROOF");
ncerts = 2;
2013-07-12 17:19:03 +02:00
certs = malloc(sizeof(nghttp2_mem_chunk)*ncerts);
2012-04-24 15:51:06 +02:00
certs[0].data = (uint8_t*)strcopy("CERT0");
certs[0].length = strlen("CERT0");
certs[1].data = (uint8_t*)strcopy("CERT1");
certs[1].length = strlen("CERT1");
2013-07-12 17:19:03 +02:00
nghttp2_frame_credential_init(&frame.credential, NGHTTP2_PROTO_SPDY3,
2012-04-24 15:51:06 +02:00
1, &proof, certs, ncerts);
2013-07-12 17:19:03 +02:00
framelen = nghttp2_frame_pack_credential(&buf, &buflen, &frame.credential);
nghttp2_frame_credential_free(&frame.credential);
nghttp2_failmalloc_unpause();
2012-04-24 15:51:06 +02:00
2013-07-12 17:19:03 +02:00
rv = nghttp2_session_recv(session);
2012-04-24 15:51:06 +02:00
if(rv != 0) {
goto fail;
}
fail:
free(buf);
free(nvbuf);
2013-07-12 17:19:03 +02:00
nghttp2_session_del(session);
nghttp2_zlib_deflate_free(&deflater);
2012-04-24 15:51:06 +02:00
}
2013-07-12 17:19:03 +02:00
void test_nghttp2_session_recv(void)
2012-04-24 15:51:06 +02:00
{
2013-07-12 17:19:03 +02:00
TEST_FAILMALLOC_RUN(run_nghttp2_session_recv);
2012-04-24 15:51:06 +02:00
}
2013-07-12 17:19:03 +02:00
static void run_nghttp2_frame_pack_syn_stream(void)
2012-04-24 15:51:06 +02:00
{
2013-07-12 17:19:03 +02:00
nghttp2_zlib deflater, inflater;
nghttp2_frame frame, oframe;
2012-04-24 15:51:06 +02:00
uint8_t *buf = NULL, *nvbuf = NULL;
size_t buflen = 0, nvbuflen = 0;
2013-07-12 17:19:03 +02:00
nghttp2_buffer inflatebuf;
2012-04-24 15:51:06 +02:00
ssize_t framelen;
const char *nv[] = { ":host", "example.org",
":scheme", "https",
NULL };
char **nv_copy;
int rv;
2013-07-12 17:19:03 +02:00
nghttp2_buffer_init(&inflatebuf, 4096);
rv = nghttp2_zlib_deflate_hd_init(&deflater, 1, NGHTTP2_PROTO_SPDY3);
2012-04-24 15:51:06 +02:00
if(rv != 0) {
goto deflate_init_fail;
}
2013-07-12 17:19:03 +02:00
rv = nghttp2_zlib_inflate_hd_init(&inflater, NGHTTP2_PROTO_SPDY3);
2012-04-24 15:51:06 +02:00
if(rv != 0) {
goto inflate_init_fail;
}
2013-07-12 17:19:03 +02:00
nv_copy = nghttp2_frame_nv_copy(nv);
2012-04-24 15:51:06 +02:00
if(nv_copy == NULL) {
goto nv_copy_fail;
}
2013-07-12 17:19:03 +02:00
nghttp2_frame_syn_stream_init(&frame.syn_stream, NGHTTP2_PROTO_SPDY3,
NGHTTP2_CTRL_FLAG_FIN, 1, 0, 2, nv_copy);
framelen = nghttp2_frame_pack_syn_stream(&buf, &buflen,
2012-04-24 15:51:06 +02:00
&nvbuf, &nvbuflen,
&frame.syn_stream, &deflater);
if(framelen < 0) {
goto fail;
}
2013-07-12 17:19:03 +02:00
rv = unpack_frame_with_nv_block(NGHTTP2_SYN_STREAM, NGHTTP2_PROTO_SPDY3,
&oframe, &inflater, buf, framelen);
2012-04-24 15:51:06 +02:00
if(rv != 0) {
goto fail;
}
2013-07-12 17:19:03 +02:00
nghttp2_frame_syn_stream_free(&oframe.syn_stream);
2012-10-01 14:51:24 +02:00
fail:
2012-04-24 15:51:06 +02:00
free(buf);
free(nvbuf);
2013-07-12 17:19:03 +02:00
nghttp2_frame_syn_stream_free(&frame.syn_stream);
2012-04-24 15:51:06 +02:00
nv_copy_fail:
2013-07-12 17:19:03 +02:00
nghttp2_zlib_inflate_free(&inflater);
2012-04-24 15:51:06 +02:00
inflate_init_fail:
2013-07-12 17:19:03 +02:00
nghttp2_zlib_deflate_free(&deflater);
2012-10-01 14:51:24 +02:00
deflate_init_fail:
2013-07-12 17:19:03 +02:00
nghttp2_buffer_free(&inflatebuf);
2012-04-24 15:51:06 +02:00
}
2013-07-12 17:19:03 +02:00
static void run_nghttp2_frame_pack_ping(void)
2012-04-24 15:51:06 +02:00
{
2013-07-12 17:19:03 +02:00
nghttp2_frame frame;
2012-04-24 15:51:06 +02:00
uint8_t *buf = NULL;
size_t buflen = 0;
2013-07-12 17:19:03 +02:00
nghttp2_frame_ping_init(&frame.ping, NGHTTP2_PROTO_SPDY3, 1);
nghttp2_frame_pack_ping(&buf, &buflen, &frame.ping);
2012-04-24 15:51:06 +02:00
free(buf);
2013-07-12 17:19:03 +02:00
nghttp2_frame_ping_free(&frame.ping);
2012-04-24 15:51:06 +02:00
}
2013-07-12 17:19:03 +02:00
static void run_nghttp2_frame_pack_goaway(void)
2012-04-24 15:51:06 +02:00
{
2013-07-12 17:19:03 +02:00
nghttp2_frame frame;
2012-04-24 15:51:06 +02:00
uint8_t *buf = NULL;
size_t buflen = 0;
2013-07-12 17:19:03 +02:00
nghttp2_frame_goaway_init(&frame.goaway, NGHTTP2_PROTO_SPDY3, 1000000007,
NGHTTP2_GOAWAY_PROTOCOL_ERROR);
nghttp2_frame_pack_goaway(&buf, &buflen, &frame.goaway);
2012-04-24 15:51:06 +02:00
free(buf);
2013-07-12 17:19:03 +02:00
nghttp2_frame_goaway_free(&frame.goaway);
2012-04-24 15:51:06 +02:00
}
2013-07-12 17:19:03 +02:00
static void run_nghttp2_frame_pack_rst_stream(void)
2012-04-24 15:51:06 +02:00
{
2013-07-12 17:19:03 +02:00
nghttp2_frame frame;
2012-04-24 15:51:06 +02:00
uint8_t *buf = NULL;
size_t buflen = 0;
2013-07-12 17:19:03 +02:00
nghttp2_frame_rst_stream_init(&frame.rst_stream, NGHTTP2_PROTO_SPDY3, 1,
NGHTTP2_PROTOCOL_ERROR);
nghttp2_frame_pack_rst_stream(&buf, &buflen, &frame.rst_stream);
2012-04-24 15:51:06 +02:00
free(buf);
2013-07-12 17:19:03 +02:00
nghttp2_frame_rst_stream_free(&frame.rst_stream);
2012-04-24 15:51:06 +02:00
}
2013-07-12 17:19:03 +02:00
static void run_nghttp2_frame_pack_window_update(void)
2012-04-24 15:51:06 +02:00
{
2013-07-12 17:19:03 +02:00
nghttp2_frame frame;
2012-04-24 15:51:06 +02:00
uint8_t *buf = NULL;
size_t buflen = 0;
2013-07-12 17:19:03 +02:00
nghttp2_frame_window_update_init(&frame.window_update, NGHTTP2_PROTO_SPDY3,
2012-04-24 15:51:06 +02:00
1000000007, 4096);
2013-07-12 17:19:03 +02:00
nghttp2_frame_pack_window_update(&buf, &buflen,
2012-04-24 15:51:06 +02:00
&frame.window_update);
free(buf);
2013-07-12 17:19:03 +02:00
nghttp2_frame_window_update_free(&frame.window_update);
2012-04-24 15:51:06 +02:00
}
2013-07-12 17:19:03 +02:00
static void run_nghttp2_frame_pack_settings(void)
2012-04-24 15:51:06 +02:00
{
2013-07-12 17:19:03 +02:00
nghttp2_frame frame, oframe;
2012-04-24 15:51:06 +02:00
uint8_t *buf = NULL;
size_t buflen = 0;
ssize_t framelen;
2013-07-12 17:19:03 +02:00
nghttp2_settings_entry iv[2], *iv_copy;
2012-04-24 15:51:06 +02:00
int rv;
2013-07-12 17:19:03 +02:00
iv[0].settings_id = NGHTTP2_SETTINGS_UPLOAD_BANDWIDTH;
iv[0].flags = NGHTTP2_ID_FLAG_SETTINGS_PERSIST_VALUE;
2012-04-24 15:51:06 +02:00
iv[0].value = 256;
2013-07-12 17:19:03 +02:00
iv[1].settings_id = NGHTTP2_SETTINGS_MAX_CONCURRENT_STREAMS;
iv[1].flags = NGHTTP2_ID_FLAG_SETTINGS_NONE;
2012-04-24 15:51:06 +02:00
iv[1].value = 100;
2013-07-12 17:19:03 +02:00
iv_copy = nghttp2_frame_iv_copy(iv, 2);
2012-04-24 15:51:06 +02:00
if(iv_copy == NULL) {
goto iv_copy_fail;
}
2013-07-12 17:19:03 +02:00
nghttp2_frame_settings_init(&frame.settings, NGHTTP2_PROTO_SPDY3,
NGHTTP2_FLAG_SETTINGS_CLEAR_SETTINGS,
2012-04-24 15:51:06 +02:00
iv_copy, 2);
2013-07-12 17:19:03 +02:00
framelen = nghttp2_frame_pack_settings(&buf, &buflen, &frame.settings);
2012-04-24 15:51:06 +02:00
if(framelen < 0) {
goto fail;
}
2013-07-12 17:19:03 +02:00
rv = nghttp2_frame_unpack_settings(&oframe.settings,
&buf[0], NGHTTP2_FRAME_HEAD_LENGTH,
&buf[NGHTTP2_FRAME_HEAD_LENGTH],
framelen-NGHTTP2_FRAME_HEAD_LENGTH);
2012-04-24 15:51:06 +02:00
if(rv != 0) {
goto fail;
}
2013-07-12 17:19:03 +02:00
nghttp2_frame_settings_free(&oframe.settings);
2012-04-24 15:51:06 +02:00
fail:
free(buf);
2013-07-12 17:19:03 +02:00
nghttp2_frame_settings_free(&frame.settings);
2012-04-24 15:51:06 +02:00
iv_copy_fail:
;
}
2013-07-12 17:19:03 +02:00
static void run_nghttp2_frame_pack_credential(void)
2012-04-24 15:51:06 +02:00
{
2013-07-12 17:19:03 +02:00
nghttp2_frame frame, oframe;
2012-04-24 15:51:06 +02:00
uint8_t *buf = NULL;
size_t buflen = 0;
ssize_t framelen;
2013-07-12 17:19:03 +02:00
nghttp2_mem_chunk proof;
nghttp2_mem_chunk *certs;
2012-04-24 15:51:06 +02:00
size_t ncerts;
int rv;
2013-07-12 17:19:03 +02:00
nghttp2_failmalloc_pause();
2012-04-24 15:51:06 +02:00
proof.data = (uint8_t*)strcopy("PROOF");
proof.length = strlen("PROOF");
ncerts = 2;
2013-07-12 17:19:03 +02:00
certs = malloc(sizeof(nghttp2_mem_chunk)*ncerts);
2012-04-24 15:51:06 +02:00
certs[0].data = (uint8_t*)strcopy("CERT0");
certs[0].length = strlen("CERT0");
certs[1].data = (uint8_t*)strcopy("CERT1");
certs[1].length = strlen("CERT1");
2013-07-12 17:19:03 +02:00
nghttp2_failmalloc_unpause();
2012-04-24 15:51:06 +02:00
2013-07-12 17:19:03 +02:00
nghttp2_frame_credential_init(&frame.credential, NGHTTP2_PROTO_SPDY3,
2012-04-24 15:51:06 +02:00
1, &proof, certs, ncerts);
2013-07-12 17:19:03 +02:00
framelen = nghttp2_frame_pack_credential(&buf, &buflen, &frame.credential);
2012-04-24 15:51:06 +02:00
if(framelen < 0) {
goto fail;
}
2013-07-12 17:19:03 +02:00
rv = nghttp2_frame_unpack_credential(&oframe.credential,
&buf[0], NGHTTP2_FRAME_HEAD_LENGTH,
&buf[NGHTTP2_FRAME_HEAD_LENGTH],
framelen-NGHTTP2_FRAME_HEAD_LENGTH);
2012-04-24 15:51:06 +02:00
if(rv != 0) {
goto fail;
}
2013-07-12 17:19:03 +02:00
nghttp2_frame_credential_free(&oframe.credential);
2012-04-24 15:51:06 +02:00
fail:
free(buf);
2013-07-12 17:19:03 +02:00
nghttp2_frame_credential_free(&frame.credential);
2012-04-24 15:51:06 +02:00
}
2013-07-12 17:19:03 +02:00
static void run_nghttp2_frame(void)
2012-04-24 15:51:06 +02:00
{
2013-07-12 17:19:03 +02:00
run_nghttp2_frame_pack_syn_stream();
run_nghttp2_frame_pack_ping();
run_nghttp2_frame_pack_goaway();
run_nghttp2_frame_pack_rst_stream();
run_nghttp2_frame_pack_window_update();
run_nghttp2_frame_pack_settings();
run_nghttp2_frame_pack_credential();
2012-04-24 15:51:06 +02:00
}
2013-07-12 17:19:03 +02:00
void test_nghttp2_frame(void)
2012-04-24 15:51:06 +02:00
{
2013-07-12 17:19:03 +02:00
TEST_FAILMALLOC_RUN(run_nghttp2_frame);
2012-04-24 15:51:06 +02:00
}