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.
This commit is contained in:
Nick Mathewson 2017-04-27 11:19:51 -04:00
parent 1bc21111d8
commit 3836d9481f
2 changed files with 29 additions and 8 deletions

View File

@ -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
};

View File

@ -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),