aboutsummaryrefslogtreecommitdiff
path: root/src
diff options
context:
space:
mode:
authorNick Mathewson <nickm@torproject.org>2014-05-08 12:41:01 -0400
committerNick Mathewson <nickm@torproject.org>2014-05-08 12:41:01 -0400
commit891d239e012aadc907a0d98fb7a8268ff40bb62a (patch)
tree0e3addd17f5f1c36932ecd7af34d6f25516f5661 /src
parent4eb3018f940e1d41f8060837afd453f9a52a6fad (diff)
downloadtor-891d239e012aadc907a0d98fb7a8268ff40bb62a.tar
tor-891d239e012aadc907a0d98fb7a8268ff40bb62a.tar.gz
More unit tests for #11648-related stuff
These are actually tests for #311. It appears to me that we didn't fix #311 properly when we thought we did in 475eb5d6; instead, the real fix was 05eff35ac6d64b, a few minutes earlier.
Diffstat (limited to 'src')
-rw-r--r--src/test/test_buffers.c83
1 files changed, 79 insertions, 4 deletions
diff --git a/src/test/test_buffers.c b/src/test/test_buffers.c
index e86ec8ea8..cb78a4bb5 100644
--- a/src/test/test_buffers.c
+++ b/src/test/test_buffers.c
@@ -598,7 +598,7 @@ test_buffer_time_tracking(void *arg)
}
static void
-test_buffers_zlib(void *arg)
+test_buffers_zlib_impl(int finalize_with_nil)
{
char *msg = NULL;
char *contents = NULL;
@@ -606,8 +606,7 @@ test_buffers_zlib(void *arg)
buf_t *buf = NULL;
tor_zlib_state_t *zlib_state = NULL;
size_t out_len, in_len;
-
- (void) arg;
+ int done;
buf = buf_new_with_capacity(128); /* will round up */
zlib_state = tor_zlib_new(1, ZLIB_METHOD);
@@ -617,7 +616,11 @@ test_buffers_zlib(void *arg)
tt_int_op(write_to_buf_zlib(buf, zlib_state, msg, 128, 0), ==, 0);
tt_int_op(write_to_buf_zlib(buf, zlib_state, msg+128, 128, 0), ==, 0);
tt_int_op(write_to_buf_zlib(buf, zlib_state, msg+256, 256, 0), ==, 0);
- tt_int_op(write_to_buf_zlib(buf, zlib_state, "all done", 9, 1), ==, 0);
+ done = !finalize_with_nil;
+ tt_int_op(write_to_buf_zlib(buf, zlib_state, "all done", 9, done), ==, 0);
+ if (finalize_with_nil) {
+ tt_int_op(write_to_buf_zlib(buf, zlib_state, "", 0, 1), ==, 0);
+ }
in_len = buf_datalen(buf);
contents = tor_malloc(in_len);
@@ -644,6 +647,75 @@ test_buffers_zlib(void *arg)
tor_free(msg);
}
+static void
+test_buffers_zlib(void *arg)
+{
+ (void) arg;
+ test_buffers_zlib_impl(0);
+}
+static void
+test_buffers_zlib_fin_with_nil(void *arg)
+{
+ (void) arg;
+ test_buffers_zlib_impl(1);
+}
+
+static void
+test_buffers_zlib_fin_at_chunk_end(void *arg)
+{
+ (void) arg;
+
+ char *msg = NULL;
+ char *contents = NULL;
+ char *expanded = NULL;
+ buf_t *buf = NULL;
+ tor_zlib_state_t *zlib_state = NULL;
+ size_t out_len, in_len;
+ size_t sz, headerjunk;
+
+ buf = buf_new_with_capacity(128); /* will round up */
+ sz = buf_get_default_chunk_size(buf);
+ msg = tor_malloc_zero(sz);
+
+ write_to_buf(msg, 1, buf);
+ tt_assert(buf->head);
+
+ /* Fill up the chunk so the zlib stuff won't fit in one chunk. */
+ tt_uint_op(buf->head->memlen, <, sz);
+ headerjunk = buf->head->memlen - 7;
+ write_to_buf(msg, headerjunk-1, buf);
+ tt_uint_op(buf->head->datalen, ==, headerjunk);
+ printf("<%u>\n", (unsigned)buf_datalen(buf));
+ tt_uint_op(buf_datalen(buf), ==, headerjunk);
+ /* Write an empty string, with finalization on. */
+ zlib_state = tor_zlib_new(1, ZLIB_METHOD);
+ tt_int_op(write_to_buf_zlib(buf, zlib_state, "", 0, 1), ==, 0);
+
+ printf("<%u>\n", (unsigned)buf_datalen(buf));
+
+ in_len = buf_datalen(buf);
+ contents = tor_malloc(in_len);
+
+ tt_int_op(fetch_from_buf(contents, in_len, buf), ==, 0);
+
+ tt_uint_op(in_len, >, headerjunk);
+
+ tt_int_op(0, ==, tor_gzip_uncompress(&expanded, &out_len,
+ contents + headerjunk, in_len - headerjunk,
+ ZLIB_METHOD, 1,
+ LOG_WARN));
+
+ tt_int_op(out_len, ==, 0);
+ tt_assert(expanded);
+
+ done:
+ buf_free(buf);
+ tor_zlib_free(zlib_state);
+ tor_free(contents);
+ tor_free(expanded);
+ tor_free(msg);
+}
+
struct testcase_t buffer_tests[] = {
{ "basic", test_buffers_basic, TT_FORK, NULL, NULL },
{ "copy", test_buffer_copy, TT_FORK, NULL, NULL },
@@ -653,6 +725,9 @@ struct testcase_t buffer_tests[] = {
NULL, NULL },
{ "time_tracking", test_buffer_time_tracking, TT_FORK, NULL, NULL },
{ "zlib", test_buffers_zlib, TT_FORK, NULL, NULL },
+ { "zlib_fin_with_nil", test_buffers_zlib_fin_with_nil, TT_FORK, NULL, NULL },
+ { "zlib_fin_at_chunk_end", test_buffers_zlib_fin_at_chunk_end, TT_FORK,
+ NULL, NULL},
END_OF_TESTCASES
};