commit 3836d9481f81cc1617a9a48de2c2ca178f4804c8
Author: Nick Mathewson <nickm(a)torproject.org>
Date: Thu Apr 27 11:19:51 2017 -0400
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.
---
src/test/test_buffers.c | 8 +++++++-
src/test/test_util.c | 29 ++++++++++++++++++++++-------
2 files changed, 29 insertions(+), 8 deletions(-)
diff --git a/src/test/test_buffers.c b/src/test/test_buffers.c
index ce5ac97..38b0824 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 dec1d52..56e39a3 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),