Commit d46e13ae authored by Tatsuhiro Tsujikawa's avatar Tatsuhiro Tsujikawa

tests: Fix failmalloc tests

parent 0707720b
/* /*
* nghttp2 - HTTP/2 C Library * nghttp2 - HTTP/2 C Library
* *
* Copyright (c) 2012 Tatsuhiro Tsujikawa * Copyright (c) 2012, 2014 Tatsuhiro Tsujikawa
* *
* Permission is hereby granted, free of charge, to any person obtaining * Permission is hereby granted, free of charge, to any person obtaining
* a copy of this software and associated documentation files (the * a copy of this software and associated documentation files (the
...@@ -46,12 +46,18 @@ typedef struct { ...@@ -46,12 +46,18 @@ typedef struct {
size_t data_source_length; size_t data_source_length;
} my_user_data; } my_user_data;
static void data_feed_init(data_feed *df, uint8_t *data, size_t data_length) static void data_feed_init(data_feed *df, nghttp2_bufs *bufs)
{ {
nghttp2_buf *buf;
size_t data_length;
buf = &bufs->head->buf;
data_length = nghttp2_buf_len(buf);
assert(data_length <= sizeof(df->data)); assert(data_length <= sizeof(df->data));
memcpy(df->data, data, data_length); memcpy(df->data, buf->pos, data_length);
df->datamark = df->data; df->datamark = df->data;
df->datalimit = df->data+data_length; df->datalimit = df->data + data_length;
} }
static ssize_t null_send_callback(nghttp2_session *session, static ssize_t null_send_callback(nghttp2_session *session,
...@@ -75,7 +81,7 @@ static ssize_t data_feed_recv_callback(nghttp2_session *session, ...@@ -75,7 +81,7 @@ static ssize_t data_feed_recv_callback(nghttp2_session *session,
static ssize_t fixed_length_data_source_read_callback static ssize_t fixed_length_data_source_read_callback
(nghttp2_session *session, int32_t stream_id, (nghttp2_session *session, int32_t stream_id,
uint8_t *buf, size_t len, int *eof, uint8_t *buf, size_t len, uint32_t *data_flags,
nghttp2_data_source *source, void *user_data) nghttp2_data_source *source, void *user_data)
{ {
my_user_data *ud = (my_user_data*)user_data; my_user_data *ud = (my_user_data*)user_data;
...@@ -87,12 +93,13 @@ static ssize_t fixed_length_data_source_read_callback ...@@ -87,12 +93,13 @@ static ssize_t fixed_length_data_source_read_callback
} }
ud->data_source_length -= wlen; ud->data_source_length -= wlen;
if(ud->data_source_length == 0) { if(ud->data_source_length == 0) {
*eof = 1; *data_flags = NGHTTP2_DATA_FLAG_EOF;
} }
return wlen; return wlen;
} }
#define TEST_FAILMALLOC_RUN(FUN) \ #define TEST_FAILMALLOC_RUN(FUN) \
do { \
size_t nmalloc, i; \ size_t nmalloc, i; \
\ \
nghttp2_failmalloc = 0; \ nghttp2_failmalloc = 0; \
...@@ -108,7 +115,8 @@ static ssize_t fixed_length_data_source_read_callback ...@@ -108,7 +115,8 @@ static ssize_t fixed_length_data_source_read_callback
FUN(); \ FUN(); \
/* printf("nmalloc=%d\n", nghttp2_nmalloc); */ \ /* printf("nmalloc=%d\n", nghttp2_nmalloc); */ \
} \ } \
nghttp2_failmalloc = 0; nghttp2_failmalloc = 0; \
} while(0)
static void run_nghttp2_session_send(void) static void run_nghttp2_session_send(void)
{ {
...@@ -137,13 +145,13 @@ static void run_nghttp2_session_send(void) ...@@ -137,13 +145,13 @@ static void run_nghttp2_session_send(void)
if(rv != 0) { if(rv != 0) {
goto client_new_fail; goto client_new_fail;
} }
rv = nghttp2_submit_request(session, 3, nv, ARRLEN(nv), &data_prd, NULL); rv = nghttp2_submit_request(session, NULL, nv, ARRLEN(nv), &data_prd, NULL);
if(rv != 0) { if(rv < 0) {
goto fail; goto fail;
} }
rv = nghttp2_submit_headers(session, NGHTTP2_FLAG_NONE, -1, rv = nghttp2_submit_headers(session, NGHTTP2_FLAG_NONE, -1, NULL,
NGHTTP2_PRI_DEFAULT, nv, ARRLEN(nv), NULL); nv, ARRLEN(nv), NULL);
if(rv != 0) { if(rv < 0) {
goto fail; goto fail;
} }
rv = nghttp2_session_send(session); rv = nghttp2_session_send(session);
...@@ -152,8 +160,8 @@ static void run_nghttp2_session_send(void) ...@@ -152,8 +160,8 @@ static void run_nghttp2_session_send(void)
} }
/* The HEADERS submitted by the previous nghttp2_submit_headers will /* The HEADERS submitted by the previous nghttp2_submit_headers will
have stream ID 3. Send HEADERS to that stream. */ have stream ID 3. Send HEADERS to that stream. */
rv = nghttp2_submit_headers(session, NGHTTP2_FLAG_NONE, 3, rv = nghttp2_submit_headers(session, NGHTTP2_FLAG_NONE, 3, NULL,
NGHTTP2_PRI_DEFAULT, nv, ARRLEN(nv), NULL); nv, ARRLEN(nv), NULL);
if(rv != 0) { if(rv != 0) {
goto fail; goto fail;
} }
...@@ -175,8 +183,8 @@ static void run_nghttp2_session_send(void) ...@@ -175,8 +183,8 @@ static void run_nghttp2_session_send(void)
goto fail; goto fail;
} }
/* Sending against half-closed stream */ /* Sending against half-closed stream */
rv = nghttp2_submit_headers(session, NGHTTP2_FLAG_NONE, 3, rv = nghttp2_submit_headers(session, NGHTTP2_FLAG_NONE, 3, NULL,
NGHTTP2_PRI_DEFAULT, nv, ARRLEN(nv), NULL); nv, ARRLEN(nv), NULL);
if(rv != 0) { if(rv != 0) {
goto fail; goto fail;
} }
...@@ -205,6 +213,7 @@ static void run_nghttp2_session_send(void) ...@@ -205,6 +213,7 @@ static void run_nghttp2_session_send(void)
if(rv != 0) { if(rv != 0) {
goto fail; goto fail;
} }
fail: fail:
nghttp2_session_del(session); nghttp2_session_del(session);
client_new_fail: client_new_fail:
...@@ -220,11 +229,9 @@ static void run_nghttp2_session_recv(void) ...@@ -220,11 +229,9 @@ static void run_nghttp2_session_recv(void)
{ {
nghttp2_session *session; nghttp2_session *session;
nghttp2_session_callbacks callbacks; nghttp2_session_callbacks callbacks;
nghttp2_hd_context deflater; nghttp2_hd_deflater deflater;
nghttp2_frame frame; nghttp2_frame frame;
uint8_t *buf = NULL; nghttp2_bufs bufs;
size_t buflen = 0;
ssize_t framelen;
nghttp2_nv nv[] = { nghttp2_nv nv[] = {
MAKE_NV(":authority", "example.org"), MAKE_NV(":authority", "example.org"),
MAKE_NV(":scheme", "https") MAKE_NV(":scheme", "https")
...@@ -236,24 +243,31 @@ static void run_nghttp2_session_recv(void) ...@@ -236,24 +243,31 @@ static void run_nghttp2_session_recv(void)
nghttp2_nv *nva; nghttp2_nv *nva;
ssize_t nvlen; ssize_t nvlen;
rv = frame_pack_bufs_init(&bufs);
if(rv != 0) {
return;
}
memset(&callbacks, 0, sizeof(nghttp2_session_callbacks)); memset(&callbacks, 0, sizeof(nghttp2_session_callbacks));
callbacks.recv_callback = data_feed_recv_callback; callbacks.recv_callback = data_feed_recv_callback;
ud.df = &df; ud.df = &df;
nghttp2_failmalloc_pause(); nghttp2_failmalloc_pause();
nvlen = nghttp2_nv_array_copy(&nva, nv, ARRLEN(nv)); nvlen = nghttp2_nv_array_copy(&nva, nv, ARRLEN(nv));
nghttp2_hd_deflate_init(&deflater, NGHTTP2_HD_SIDE_REQUEST); nghttp2_hd_deflate_init(&deflater);
nghttp2_session_server_new(&session, &callbacks, &ud); nghttp2_session_server_new(&session, &callbacks, &ud);
nghttp2_failmalloc_unpause(); nghttp2_failmalloc_unpause();
/* HEADERS */ /* HEADERS */
nghttp2_failmalloc_pause(); nghttp2_failmalloc_pause();
nghttp2_frame_headers_init(&frame.headers, NGHTTP2_FLAG_END_STREAM, nghttp2_frame_headers_init(&frame.headers, NGHTTP2_FLAG_END_STREAM,
1, NGHTTP2_PRI_DEFAULT, nva, nvlen); 1, NGHTTP2_HCAT_REQUEST, NULL, nva, nvlen);
framelen = nghttp2_frame_pack_headers(&buf, &buflen, &frame.headers, nghttp2_frame_pack_headers(&bufs, &frame.headers, &deflater);
&deflater);
nghttp2_frame_headers_free(&frame.headers); nghttp2_frame_headers_free(&frame.headers);
data_feed_init(&df, buf, framelen); data_feed_init(&df, &bufs);
nghttp2_bufs_reset(&bufs);
nghttp2_failmalloc_unpause(); nghttp2_failmalloc_unpause();
rv = nghttp2_session_recv(session); rv = nghttp2_session_recv(session);
...@@ -264,9 +278,11 @@ static void run_nghttp2_session_recv(void) ...@@ -264,9 +278,11 @@ static void run_nghttp2_session_recv(void)
/* PING */ /* PING */
nghttp2_failmalloc_pause(); nghttp2_failmalloc_pause();
nghttp2_frame_ping_init(&frame.ping, NGHTTP2_FLAG_NONE, NULL); nghttp2_frame_ping_init(&frame.ping, NGHTTP2_FLAG_NONE, NULL);
framelen = nghttp2_frame_pack_ping(&buf, &buflen, &frame.ping); nghttp2_frame_pack_ping(&bufs, &frame.ping);
nghttp2_frame_ping_free(&frame.ping); nghttp2_frame_ping_free(&frame.ping);
data_feed_init(&df, buf, framelen); data_feed_init(&df, &bufs);
nghttp2_bufs_reset(&bufs);
nghttp2_failmalloc_unpause(); nghttp2_failmalloc_unpause();
rv = nghttp2_session_recv(session); rv = nghttp2_session_recv(session);
...@@ -277,8 +293,10 @@ static void run_nghttp2_session_recv(void) ...@@ -277,8 +293,10 @@ static void run_nghttp2_session_recv(void)
/* RST_STREAM */ /* RST_STREAM */
nghttp2_failmalloc_pause(); nghttp2_failmalloc_pause();
nghttp2_frame_rst_stream_init(&frame.rst_stream, 1, NGHTTP2_PROTOCOL_ERROR); nghttp2_frame_rst_stream_init(&frame.rst_stream, 1, NGHTTP2_PROTOCOL_ERROR);
framelen = nghttp2_frame_pack_rst_stream(&buf, &buflen, &frame.rst_stream); nghttp2_frame_pack_rst_stream(&bufs, &frame.rst_stream);
nghttp2_frame_rst_stream_free(&frame.rst_stream); nghttp2_frame_rst_stream_free(&frame.rst_stream);
nghttp2_bufs_reset(&bufs);
nghttp2_failmalloc_unpause(); nghttp2_failmalloc_unpause();
rv = nghttp2_session_recv(session); rv = nghttp2_session_recv(session);
...@@ -294,8 +312,10 @@ static void run_nghttp2_session_recv(void) ...@@ -294,8 +312,10 @@ static void run_nghttp2_session_recv(void)
iv[1].value = 100; iv[1].value = 100;
nghttp2_frame_settings_init(&frame.settings, NGHTTP2_FLAG_NONE, nghttp2_frame_settings_init(&frame.settings, NGHTTP2_FLAG_NONE,
nghttp2_frame_iv_copy(iv, 2), 2); nghttp2_frame_iv_copy(iv, 2), 2);
framelen = nghttp2_frame_pack_settings(&buf, &buflen, &frame.settings); nghttp2_frame_pack_settings(&bufs, &frame.settings);
nghttp2_frame_settings_free(&frame.settings); nghttp2_frame_settings_free(&frame.settings);
nghttp2_bufs_reset(&bufs);
nghttp2_failmalloc_unpause(); nghttp2_failmalloc_unpause();
rv = nghttp2_session_recv(session); rv = nghttp2_session_recv(session);
...@@ -304,7 +324,7 @@ static void run_nghttp2_session_recv(void) ...@@ -304,7 +324,7 @@ static void run_nghttp2_session_recv(void)
} }
fail: fail:
free(buf); nghttp2_bufs_free(&bufs);
nghttp2_session_del(session); nghttp2_session_del(session);
nghttp2_hd_deflate_free(&deflater); nghttp2_hd_deflate_free(&deflater);
} }
...@@ -316,11 +336,10 @@ void test_nghttp2_session_recv(void) ...@@ -316,11 +336,10 @@ void test_nghttp2_session_recv(void)
static void run_nghttp2_frame_pack_headers(void) static void run_nghttp2_frame_pack_headers(void)
{ {
nghttp2_hd_context deflater, inflater; nghttp2_hd_deflater deflater;
nghttp2_hd_inflater inflater;
nghttp2_frame frame, oframe; nghttp2_frame frame, oframe;
uint8_t *buf = NULL; nghttp2_bufs bufs;
size_t buflen = 0;
ssize_t framelen;
nghttp2_nv nv[] = { nghttp2_nv nv[] = {
MAKE_NV(":host", "example.org"), MAKE_NV(":host", "example.org"),
MAKE_NV(":scheme", "https") MAKE_NV(":scheme", "https")
...@@ -329,11 +348,17 @@ static void run_nghttp2_frame_pack_headers(void) ...@@ -329,11 +348,17 @@ static void run_nghttp2_frame_pack_headers(void)
nghttp2_nv *nva; nghttp2_nv *nva;
ssize_t nvlen; ssize_t nvlen;
rv = nghttp2_hd_deflate_init(&deflater, NGHTTP2_HD_SIDE_REQUEST); rv = frame_pack_bufs_init(&bufs);
if(rv != 0) {
return;
}
rv = nghttp2_hd_deflate_init(&deflater);
if(rv != 0) { if(rv != 0) {
goto deflate_init_fail; goto deflate_init_fail;
} }
rv = nghttp2_hd_inflate_init(&inflater, NGHTTP2_HD_SIDE_REQUEST); rv = nghttp2_hd_inflate_init(&inflater);
if(rv != 0) { if(rv != 0) {
goto inflate_init_fail; goto inflate_init_fail;
} }
...@@ -342,125 +367,81 @@ static void run_nghttp2_frame_pack_headers(void) ...@@ -342,125 +367,81 @@ static void run_nghttp2_frame_pack_headers(void)
goto nv_copy_fail; goto nv_copy_fail;
} }
nghttp2_frame_headers_init(&frame.headers, NGHTTP2_FLAG_END_STREAM, nghttp2_frame_headers_init(&frame.headers, NGHTTP2_FLAG_END_STREAM,
1, NGHTTP2_PRI_DEFAULT, nva, nvlen); 1, NGHTTP2_HCAT_REQUEST, NULL, nva, nvlen);
framelen = nghttp2_frame_pack_headers(&buf, &buflen, &frame.headers, rv = nghttp2_frame_pack_headers(&bufs, &frame.headers, &deflater);
&deflater); if(rv != 0) {
if(framelen < 0) {
goto fail; goto fail;
} }
rv = unpack_frame_with_nv_block(&oframe, NGHTTP2_HEADERS, &inflater, rv = unpack_framebuf(&oframe, &bufs);
buf, framelen);
if(rv != 0) { if(rv != 0) {
goto fail; goto fail;
} }
nghttp2_frame_headers_free(&oframe.headers); nghttp2_frame_headers_free(&oframe.headers);
fail: fail:
free(buf);
nghttp2_frame_headers_free(&frame.headers); nghttp2_frame_headers_free(&frame.headers);
nv_copy_fail: nv_copy_fail:
nghttp2_hd_inflate_free(&inflater); nghttp2_hd_inflate_free(&inflater);
inflate_init_fail: inflate_init_fail:
nghttp2_hd_deflate_free(&deflater); nghttp2_hd_deflate_free(&deflater);
deflate_init_fail: deflate_init_fail:
; nghttp2_bufs_free(&bufs);
}
static void run_nghttp2_frame_pack_ping(void)
{
nghttp2_frame frame;
uint8_t *buf = NULL;
size_t buflen = 0;
nghttp2_frame_ping_init(&frame.ping, NGHTTP2_FLAG_NONE, NULL);
nghttp2_frame_pack_ping(&buf, &buflen, &frame.ping);
free(buf);
nghttp2_frame_ping_free(&frame.ping);
}
static void run_nghttp2_frame_pack_goaway(void)
{
nghttp2_frame frame;
uint8_t *buf = NULL;
size_t buflen = 0;
nghttp2_frame_goaway_init(&frame.goaway, 1000000007, NGHTTP2_PROTOCOL_ERROR,
NULL, 0);
nghttp2_frame_pack_goaway(&buf, &buflen, &frame.goaway);
free(buf);
nghttp2_frame_goaway_free(&frame.goaway);
}
static void run_nghttp2_frame_pack_rst_stream(void)
{
nghttp2_frame frame;
uint8_t *buf = NULL;
size_t buflen = 0;
nghttp2_frame_rst_stream_init(&frame.rst_stream, 1, NGHTTP2_PROTOCOL_ERROR);
nghttp2_frame_pack_rst_stream(&buf, &buflen, &frame.rst_stream);
free(buf);
nghttp2_frame_rst_stream_free(&frame.rst_stream);
}
static void run_nghttp2_frame_pack_window_update(void)
{
nghttp2_frame frame;
uint8_t *buf = NULL;
size_t buflen = 0;
nghttp2_frame_window_update_init(&frame.window_update, NGHTTP2_FLAG_NONE,
1000000007, 4096);
nghttp2_frame_pack_window_update(&buf, &buflen,
&frame.window_update);
free(buf);
nghttp2_frame_window_update_free(&frame.window_update);
} }
static void run_nghttp2_frame_pack_settings(void) static void run_nghttp2_frame_pack_settings(void)
{ {
nghttp2_frame frame, oframe; nghttp2_frame frame, oframe;
uint8_t *buf = NULL; nghttp2_bufs bufs;
size_t buflen = 0; nghttp2_buf *buf;
ssize_t framelen;
nghttp2_settings_entry iv[2], *iv_copy; nghttp2_settings_entry iv[2], *iv_copy;
int rv; int rv;
rv = frame_pack_bufs_init(&bufs);
if(rv != 0) {
return;
}
iv[0].settings_id = NGHTTP2_SETTINGS_HEADER_TABLE_SIZE; iv[0].settings_id = NGHTTP2_SETTINGS_HEADER_TABLE_SIZE;
iv[0].value = 4096; iv[0].value = 4096;
iv[1].settings_id = NGHTTP2_SETTINGS_MAX_CONCURRENT_STREAMS; iv[1].settings_id = NGHTTP2_SETTINGS_MAX_CONCURRENT_STREAMS;
iv[1].value = 100; iv[1].value = 100;
iv_copy = nghttp2_frame_iv_copy(iv, 2); iv_copy = nghttp2_frame_iv_copy(iv, 2);
if(iv_copy == NULL) { if(iv_copy == NULL) {
goto iv_copy_fail; goto iv_copy_fail;
} }
nghttp2_frame_settings_init(&frame.settings, NGHTTP2_FLAG_NONE, iv_copy, 2); nghttp2_frame_settings_init(&frame.settings, NGHTTP2_FLAG_NONE, iv_copy, 2);
framelen = nghttp2_frame_pack_settings(&buf, &buflen, &frame.settings);
if(framelen < 0) { rv = nghttp2_frame_pack_settings(&bufs, &frame.settings);
if(rv != 0) {
goto fail; goto fail;
} }
rv = nghttp2_frame_unpack_settings(&oframe.settings,
&buf[0], NGHTTP2_FRAME_HDLEN, buf = &bufs.head->buf;
&buf[NGHTTP2_FRAME_HDLEN],
framelen-NGHTTP2_FRAME_HDLEN); rv = nghttp2_frame_unpack_settings_payload2
(&oframe.settings.iv,
&oframe.settings.niv,
buf->pos + NGHTTP2_FRAME_HDLEN,
nghttp2_buf_len(buf) - NGHTTP2_FRAME_HDLEN);
if(rv != 0) { if(rv != 0) {
goto fail; goto fail;
} }
nghttp2_frame_settings_free(&oframe.settings); nghttp2_frame_settings_free(&oframe.settings);
fail: fail:
free(buf);
nghttp2_frame_settings_free(&frame.settings); nghttp2_frame_settings_free(&frame.settings);
iv_copy_fail: iv_copy_fail:
; nghttp2_bufs_free(&bufs);
}
static void run_nghttp2_frame(void)
{
run_nghttp2_frame_pack_headers();
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();
} }
void test_nghttp2_frame(void) void test_nghttp2_frame(void)
{ {
TEST_FAILMALLOC_RUN(run_nghttp2_frame); TEST_FAILMALLOC_RUN(run_nghttp2_frame_pack_headers);
TEST_FAILMALLOC_RUN(run_nghttp2_frame_pack_settings);
} }
...@@ -220,10 +220,10 @@ ssize_t inflate_hd(nghttp2_hd_inflater *inflater, nva_out *out, ...@@ -220,10 +220,10 @@ ssize_t inflate_hd(nghttp2_hd_inflater *inflater, nva_out *out,
return processed; return processed;
} }
void frame_pack_bufs_init(nghttp2_bufs *bufs) int frame_pack_bufs_init(nghttp2_bufs *bufs)
{ {
/* 2 for PAD_HIGH and PAD_LOW */ /* 2 for PAD_HIGH and PAD_LOW */
nghttp2_bufs_init2(bufs, 4096, 16, NGHTTP2_FRAME_HDLEN + 2); return nghttp2_bufs_init2(bufs, 4096, 16, NGHTTP2_FRAME_HDLEN + 2);
} }
void bufs_large_init(nghttp2_bufs *bufs, size_t chunk_size) void bufs_large_init(nghttp2_bufs *bufs, size_t chunk_size)
......
...@@ -79,7 +79,7 @@ void add_out(nva_out *out, nghttp2_nv *nv); ...@@ -79,7 +79,7 @@ void add_out(nva_out *out, nghttp2_nv *nv);
ssize_t inflate_hd(nghttp2_hd_inflater *inflater, nva_out *out, ssize_t inflate_hd(nghttp2_hd_inflater *inflater, nva_out *out,
nghttp2_bufs *bufs, size_t offset); nghttp2_bufs *bufs, size_t offset);
void frame_pack_bufs_init(nghttp2_bufs *bufs); int frame_pack_bufs_init(nghttp2_bufs *bufs);
void bufs_large_init(nghttp2_bufs *bufs, size_t chunk_size); void bufs_large_init(nghttp2_bufs *bufs, size_t chunk_size);
......
Markdown is supported
0%
or
You are about to add 0 people to the discussion. Proceed with caution.
Finish editing this message first!
Please register or to comment