aboutsummaryrefslogtreecommitdiff
diff options
context:
space:
mode:
authorNick Mathewson <nickm@torproject.org>2017-04-27 11:19:51 -0400
committerNick Mathewson <nickm@torproject.org>2017-05-02 08:31:32 -0400
commit3836d9481f81cc1617a9a48de2c2ca178f4804c8 (patch)
tree820bd37f2d5397552b16b8778997352e925e9f6f
parent1bc21111d82579b6472357d43f07a85142b6614a (diff)
downloadtor-3836d9481f81cc1617a9a48de2c2ca178f4804c8.tar.gz
tor-3836d9481f81cc1617a9a48de2c2ca178f4804c8.zip
Add unit tests for the NO_METHOD compressor
These required some special-casing, since some of the assumption about real compression algorithms don't actually hold for the identity transform. Specifically, we had assumed: - compression functions typically change the lengths of their inputs. - decompression functions can detect truncated inputs - compression functions have detectable headers None of those is true for the identity transformation.
-rw-r--r--src/test/test_buffers.c8
-rw-r--r--src/test/test_util.c29
2 files changed, 29 insertions, 8 deletions
diff --git a/src/test/test_buffers.c b/src/test/test_buffers.c
index ce5ac97b3d..38b0824304 100644
--- a/src/test/test_buffers.c
+++ b/src/test/test_buffers.c
@@ -611,7 +611,11 @@ test_buffers_compress_fin_at_chunk_end_impl(compress_method_t method,
tt_int_op(fetch_from_buf(contents, in_len, buf), OP_EQ, 0);
- tt_uint_op(in_len, OP_GT, headerjunk);
+ if (method == NO_METHOD) {
+ tt_uint_op(in_len, OP_EQ, headerjunk);
+ } else {
+ tt_uint_op(in_len, OP_GT, headerjunk);
+ }
tt_int_op(0, OP_EQ, tor_uncompress(&expanded, &out_len,
contents + headerjunk,
@@ -855,6 +859,8 @@ struct testcase_t buffer_tests[] = {
&passthrough_setup, (char*)"x-zstd" },
{ "compress/lzma", test_buffers_compress, TT_FORK,
&passthrough_setup, (char*)"x-lzma" },
+ { "compress/none", test_buffers_compress, TT_FORK,
+ &passthrough_setup, (char*)"identity" },
END_OF_TESTCASES
};
diff --git a/src/test/test_util.c b/src/test/test_util.c
index dec1d526c8..56e39a39be 100644
--- a/src/test/test_util.c
+++ b/src/test/test_util.c
@@ -2255,8 +2255,15 @@ test_util_compress_impl(compress_method_t method)
tt_assert(!tor_compress(&buf2, &len1, buf1, strlen(buf1)+1, method));
tt_assert(buf2 != NULL);
- tt_int_op(len1, OP_LT, strlen(buf1));
- tt_int_op(detect_compression_method(buf2, len1), OP_EQ, method);
+ if (method == NO_METHOD) {
+ // The identity transform doesn't actually compress, and it isn't
+ // detectable as "the identity transform."
+ tt_int_op(len1, OP_EQ, strlen(buf1)+1);
+ tt_int_op(detect_compression_method(buf2, len1), OP_EQ, UNKNOWN_METHOD);
+ } else {
+ tt_int_op(len1, OP_LT, strlen(buf1));
+ tt_int_op(detect_compression_method(buf2, len1), OP_EQ, method);
+ }
tt_assert(!tor_uncompress(&buf3, &len2, buf2, len1, method, 1, LOG_INFO));
tt_assert(buf3 != NULL);
@@ -2300,11 +2307,14 @@ test_util_compress_impl(compress_method_t method)
tt_assert(fast_memeq(buf1, buf3, len2));
tt_int_op(buf3[len2], OP_EQ, 0);
- /* when we demand a complete output, this must fail. */
+ /* when we demand a complete output from a real compression method, this
+ * must fail. */
tor_free(buf3);
- tt_assert(tor_uncompress(&buf3, &len2, buf2, len1-16,
- method, 1, LOG_INFO));
- tt_assert(buf3 == NULL);
+ if (method != NO_METHOD) {
+ tt_assert(tor_uncompress(&buf3, &len2, buf2, len1-16,
+ method, 1, LOG_INFO));
+ tt_assert(buf3 == NULL);
+ }
done:
tor_free(buf1);
@@ -2337,7 +2347,11 @@ test_util_compress_stream_impl(compress_method_t method,
tt_int_op(tor_compress_process(state, &cp1, &len1, &ccp2, &len2, 1),
OP_EQ, TOR_COMPRESS_DONE);
tt_int_op(0, OP_EQ, len2);
- tt_assert(cp1 > cp2); /* Make sure we really added something. */
+ if (method == NO_METHOD) {
+ tt_ptr_op(cp1, OP_EQ, cp2);
+ } else {
+ tt_assert(cp1 > cp2); /* Make sure we really added something. */
+ }
tt_assert(!tor_uncompress(&buf3, &len2, buf1, 1024-len1,
method, 1, LOG_WARN));
@@ -5755,6 +5769,7 @@ struct testcase_t util_tests[] = {
COMPRESS(gzip, "gzip"),
COMPRESS(lzma, "x-lzma"),
COMPRESS(zstd, "x-zstd"),
+ COMPRESS(none, "identity"),
UTIL_TEST(gzip_compression_bomb, TT_FORK),
UTIL_LEGACY(datadir),
UTIL_LEGACY(memarea),