From 3772fdf756a13f7b6a8e713e5d53eb5d7b2785cd Mon Sep 17 00:00:00 2001 From: =?UTF-8?q?Alexander=20F=C3=A6r=C3=B8y?= Date: Tue, 16 May 2017 19:08:18 +0000 Subject: [PATCH 1/6] Ensure that each compression backend returns a provider version identifier. --- src/test/test_util.c | 5 +++++ 1 file changed, 5 insertions(+) diff --git a/src/test/test_util.c b/src/test/test_util.c index b3f8ecdf5..654498fbb 100644 --- a/src/test/test_util.c +++ b/src/test/test_util.c @@ -2250,6 +2250,11 @@ test_util_compress_impl(compress_method_t method) tt_assert(tor_compress_supports_method(method)); + if (method != NO_METHOD) { + tt_assert(tor_compress_version_str(method) != NULL); + tt_assert(tor_compress_header_version_str(method) != NULL); + } + buf1 = tor_strdup("AAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAZAAAAAAAAAAAAAAAAAAAZ"); tt_assert(detect_compression_method(buf1, strlen(buf1)) == UNKNOWN_METHOD); From d74467e8583b8ff50417b84994c7a87250aef771 Mon Sep 17 00:00:00 2001 From: =?UTF-8?q?Alexander=20F=C3=A6r=C3=B8y?= Date: Tue, 16 May 2017 19:32:20 +0000 Subject: [PATCH 2/6] Check that tor_compress_state_size() returns a value larger than zero. See: https://bugs.torproject.org/22286 --- src/test/test_util.c | 2 ++ 1 file changed, 2 insertions(+) diff --git a/src/test/test_util.c b/src/test/test_util.c index 654498fbb..0b087d171 100644 --- a/src/test/test_util.c +++ b/src/test/test_util.c @@ -2358,6 +2358,8 @@ test_util_compress_stream_impl(compress_method_t method, tt_assert(cp1 > cp2); /* Make sure we really added something. */ } + tt_int_op(tor_compress_state_size(state), OP_GT, 0); + tt_assert(!tor_uncompress(&buf3, &len2, buf1, 1024-len1, method, 1, LOG_WARN)); /* Make sure it compressed right. */ From 77511aed6c0c8cfc16aa4aa47c3c2f4c6efe4dc7 Mon Sep 17 00:00:00 2001 From: =?UTF-8?q?Alexander=20F=C3=A6r=C3=B8y?= Date: Wed, 17 May 2017 12:48:16 +0000 Subject: [PATCH 3/6] Fix whitespace issue. See: https://bugs.torproject.org/22286 --- src/common/compress.h | 2 +- 1 file changed, 1 insertion(+), 1 deletion(-) diff --git a/src/common/compress.h b/src/common/compress.h index 7c0dc1406..59c8b7b9b 100644 --- a/src/common/compress.h +++ b/src/common/compress.h @@ -49,7 +49,7 @@ int tor_compress_is_compression_bomb(size_t size_in, size_t size_out); int tor_compress_supports_method(compress_method_t method); unsigned tor_compress_get_supported_method_bitmask(void); -const char * compression_method_get_name(compress_method_t method); +const char *compression_method_get_name(compress_method_t method); const char *compression_method_get_human_name(compress_method_t method); compress_method_t compression_method_get_by_name(const char *name); From fcf836d239d3545ff02df63d47e1b23b000138e0 Mon Sep 17 00:00:00 2001 From: =?UTF-8?q?Alexander=20F=C3=A6r=C3=B8y?= Date: Wed, 17 May 2017 12:52:07 +0000 Subject: [PATCH 4/6] Add coverage markers in Zstd + LZMA compression backends. See: https://bugs.torproject.org/22286 --- src/common/compress_lzma.c | 14 ++++++++++++-- src/common/compress_zstd.c | 16 ++++++++++++++++ 2 files changed, 28 insertions(+), 2 deletions(-) diff --git a/src/common/compress_lzma.c b/src/common/compress_lzma.c index b5393a6ba..30d5920ca 100644 --- a/src/common/compress_lzma.c +++ b/src/common/compress_lzma.c @@ -46,6 +46,7 @@ memory_level(compression_level_t level) static const char * lzma_error_str(lzma_ret error) { + // LCOV_EXCL_START switch (error) { case LZMA_OK: return "Operation completed successfully"; @@ -74,6 +75,7 @@ lzma_error_str(lzma_ret error) default: return "Unknown LZMA error"; } + // LCOV_EXCL_STOP } #endif // HAVE_LZMA. @@ -144,9 +146,11 @@ tor_lzma_state_size_precalc(int compress, compression_level_t level) memory_usage = lzma_easy_decoder_memusage(memory_level(level)); if (memory_usage == UINT64_MAX) { + // LCOV_EXCL_START log_warn(LD_GENERAL, "Unsupported compression level passed to LZMA %s", compress ? "encoder" : "decoder"); goto err; + // LCOV_EXCL_STOP } if (memory_usage + sizeof(tor_lzma_compress_state_t) > SIZE_MAX) @@ -157,7 +161,7 @@ tor_lzma_state_size_precalc(int compress, compression_level_t level) return (size_t)memory_usage; err: - return 0; + return 0; // LCOV_EXCL_LINE } #endif // HAVE_LZMA. @@ -189,17 +193,21 @@ tor_lzma_compress_new(int compress, retval = lzma_alone_encoder(&result->stream, &stream_options); if (retval != LZMA_OK) { + // LCOV_EXCL_START log_warn(LD_GENERAL, "Error from LZMA encoder: %s (%u).", lzma_error_str(retval), retval); goto err; + // LCOV_EXCL_STOP } } else { retval = lzma_alone_decoder(&result->stream, MEMORY_LIMIT); if (retval != LZMA_OK) { + // LCOV_EXCL_START log_warn(LD_GENERAL, "Error from LZMA decoder: %s (%u).", lzma_error_str(retval), retval); goto err; + // LCOV_EXCL_STOP } } @@ -207,7 +215,7 @@ tor_lzma_compress_new(int compress, return result; err: - tor_free(result); + tor_free(result); // LCOV_EXCL_LINE return NULL; #else // HAVE_LZMA. (void)compress; @@ -295,10 +303,12 @@ tor_lzma_compress_process(tor_lzma_compress_state_t *state, case LZMA_DATA_ERROR: case LZMA_PROG_ERROR: default: + // LCOV_EXCL_START log_warn(LD_GENERAL, "LZMA %s didn't finish: %s.", state->compress ? "compression" : "decompression", lzma_error_str(retval)); return TOR_COMPRESS_ERROR; + // LCOV_EXCL_STOP } #else // HAVE_LZMA. (void)state; diff --git a/src/common/compress_zstd.c b/src/common/compress_zstd.c index 99d05c37b..f54c4e1b3 100644 --- a/src/common/compress_zstd.c +++ b/src/common/compress_zstd.c @@ -194,31 +194,39 @@ tor_zstd_compress_new(int compress, result->u.compress_stream = ZSTD_createCStream(); if (result->u.compress_stream == NULL) { + // LCOV_EXCL_START log_warn(LD_GENERAL, "Error while creating Zstandard stream"); goto err; + // LCOV_EXCL_STOP } retval = ZSTD_initCStream(result->u.compress_stream, preset); if (ZSTD_isError(retval)) { + // LCOV_EXCL_START log_warn(LD_GENERAL, "Zstandard stream initialization error: %s", ZSTD_getErrorName(retval)); goto err; + // LCOV_EXCL_STOP } } else { result->u.decompress_stream = ZSTD_createDStream(); if (result->u.decompress_stream == NULL) { + // LCOV_EXCL_START log_warn(LD_GENERAL, "Error while creating Zstandard stream"); goto err; + // LCOV_EXCL_STOP } retval = ZSTD_initDStream(result->u.decompress_stream); if (ZSTD_isError(retval)) { + // LCOV_EXCL_START log_warn(LD_GENERAL, "Zstandard stream initialization error: %s", ZSTD_getErrorName(retval)); goto err; + // LCOV_EXCL_STOP } } @@ -226,6 +234,7 @@ tor_zstd_compress_new(int compress, return result; err: + // LCOV_EXCL_START if (compress) { ZSTD_freeCStream(result->u.compress_stream); } else { @@ -234,6 +243,7 @@ tor_zstd_compress_new(int compress, tor_free(result); return NULL; + // LCOV_EXCL_STOP #else // HAVE_ZSTD. (void)compress; (void)method; @@ -294,10 +304,12 @@ tor_zstd_compress_process(tor_zstd_compress_state_t *state, } if (ZSTD_isError(retval)) { + // LCOV_EXCL_START log_warn(LD_GENERAL, "Zstandard %s didn't finish: %s.", state->compress ? "compression" : "decompression", ZSTD_getErrorName(retval)); return TOR_COMPRESS_ERROR; + // LCOV_EXCL_STOP } if (state->compress && !finish) { @@ -307,9 +319,11 @@ tor_zstd_compress_process(tor_zstd_compress_state_t *state, *out_len = output.size - output.pos; if (ZSTD_isError(retval)) { + // LCOV_EXCL_START log_warn(LD_GENERAL, "Zstandard compression unable to flush: %s.", ZSTD_getErrorName(retval)); return TOR_COMPRESS_ERROR; + // LCOV_EXCL_STOP } if (retval > 0) @@ -326,10 +340,12 @@ tor_zstd_compress_process(tor_zstd_compress_state_t *state, *out_len = output.size - output.pos; if (ZSTD_isError(retval)) { + // LCOV_EXCL_START log_warn(LD_GENERAL, "Zstandard compression unable to write " "epilogue: %s.", ZSTD_getErrorName(retval)); return TOR_COMPRESS_ERROR; + // LCOV_EXCL_STOP } // endStream returns the number of bytes that is needed to write the From 7465ea4ad906d2c1fb8a31c01c29b62ccfe7a590 Mon Sep 17 00:00:00 2001 From: Nick Mathewson Date: Tue, 8 Aug 2017 10:08:06 -0400 Subject: [PATCH 5/6] Remove some LCOV_EXCL stuff that I think may be testable after all. This is partial revert on 22286. Also, tweak some log messages to be distinct. --- src/common/compress_lzma.c | 4 ---- src/common/compress_zstd.c | 12 ++++-------- 2 files changed, 4 insertions(+), 12 deletions(-) diff --git a/src/common/compress_lzma.c b/src/common/compress_lzma.c index 30d5920ca..d453d9f71 100644 --- a/src/common/compress_lzma.c +++ b/src/common/compress_lzma.c @@ -46,7 +46,6 @@ memory_level(compression_level_t level) static const char * lzma_error_str(lzma_ret error) { - // LCOV_EXCL_START switch (error) { case LZMA_OK: return "Operation completed successfully"; @@ -75,7 +74,6 @@ lzma_error_str(lzma_ret error) default: return "Unknown LZMA error"; } - // LCOV_EXCL_STOP } #endif // HAVE_LZMA. @@ -303,12 +301,10 @@ tor_lzma_compress_process(tor_lzma_compress_state_t *state, case LZMA_DATA_ERROR: case LZMA_PROG_ERROR: default: - // LCOV_EXCL_START log_warn(LD_GENERAL, "LZMA %s didn't finish: %s.", state->compress ? "compression" : "decompression", lzma_error_str(retval)); return TOR_COMPRESS_ERROR; - // LCOV_EXCL_STOP } #else // HAVE_LZMA. (void)state; diff --git a/src/common/compress_zstd.c b/src/common/compress_zstd.c index 5c5026c37..0808bcd9a 100644 --- a/src/common/compress_zstd.c +++ b/src/common/compress_zstd.c @@ -197,7 +197,8 @@ tor_zstd_compress_new(int compress, if (result->u.compress_stream == NULL) { // LCOV_EXCL_START - log_warn(LD_GENERAL, "Error while creating Zstandard stream"); + log_warn(LD_GENERAL, "Error while creating Zstandard compression " + "stream"); goto err; // LCOV_EXCL_STOP } @@ -216,7 +217,8 @@ tor_zstd_compress_new(int compress, if (result->u.decompress_stream == NULL) { // LCOV_EXCL_START - log_warn(LD_GENERAL, "Error while creating Zstandard stream"); + log_warn(LD_GENERAL, "Error while creating Zstandard decompression " + "stream"); goto err; // LCOV_EXCL_STOP } @@ -313,12 +315,10 @@ tor_zstd_compress_process(tor_zstd_compress_state_t *state, } if (ZSTD_isError(retval)) { - // LCOV_EXCL_START log_warn(LD_GENERAL, "Zstandard %s didn't finish: %s.", state->compress ? "compression" : "decompression", ZSTD_getErrorName(retval)); return TOR_COMPRESS_ERROR; - // LCOV_EXCL_STOP } if (state->compress && !state->have_called_end) { @@ -328,11 +328,9 @@ tor_zstd_compress_process(tor_zstd_compress_state_t *state, *out_len = output.size - output.pos; if (ZSTD_isError(retval)) { - // LCOV_EXCL_START log_warn(LD_GENERAL, "Zstandard compression unable to flush: %s.", ZSTD_getErrorName(retval)); return TOR_COMPRESS_ERROR; - // LCOV_EXCL_STOP } // ZSTD_flushStream returns 0 if the frame is done, or >0 if it @@ -359,12 +357,10 @@ tor_zstd_compress_process(tor_zstd_compress_state_t *state, *out_len = output.size - output.pos; if (ZSTD_isError(retval)) { - // LCOV_EXCL_START log_warn(LD_GENERAL, "Zstandard compression unable to write " "epilogue: %s.", ZSTD_getErrorName(retval)); return TOR_COMPRESS_ERROR; - // LCOV_EXCL_STOP } // endStream returns the number of bytes that is needed to write the From 5368eaf62b2b4804e75fc301757f843d64ef941d Mon Sep 17 00:00:00 2001 From: Nick Mathewson Date: Tue, 8 Aug 2017 10:09:54 -0400 Subject: [PATCH 6/6] chages file on 22286 --- changes/bug22286 | 3 +++ 1 file changed, 3 insertions(+) create mode 100644 changes/bug22286 diff --git a/changes/bug22286 b/changes/bug22286 new file mode 100644 index 000000000..f72e8fe2c --- /dev/null +++ b/changes/bug22286 @@ -0,0 +1,3 @@ + o Minor features (tests): + - Add a couple more tests for compression backend initialization. + Closes ticket 22286.