aboutsummaryrefslogtreecommitdiff
path: root/src/test/test_util.c
diff options
context:
space:
mode:
Diffstat (limited to 'src/test/test_util.c')
-rw-r--r--src/test/test_util.c339
1 files changed, 284 insertions, 55 deletions
diff --git a/src/test/test_util.c b/src/test/test_util.c
index 203f9dd1c4..1e33de82ae 100644
--- a/src/test/test_util.c
+++ b/src/test/test_util.c
@@ -2249,46 +2249,49 @@ test_util_gzip(void *arg)
char *buf1=NULL, *buf2=NULL, *buf3=NULL, *cp1, *cp2;
const char *ccp2;
size_t len1, len2;
- tor_zlib_state_t *state = NULL;
+ tor_compress_state_t *state = NULL;
(void)arg;
+ tt_assert(tor_compress_supports_method(GZIP_METHOD));
+ tt_assert(tor_compress_supports_method(ZLIB_METHOD));
+
buf1 = tor_strdup("AAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAZAAAAAAAAAAAAAAAAAAAZ");
tt_assert(detect_compression_method(buf1, strlen(buf1)) == UNKNOWN_METHOD);
- tt_assert(!tor_gzip_compress(&buf2, &len1, buf1, strlen(buf1)+1,
- GZIP_METHOD));
- tt_assert(buf2);
- tt_assert(len1 < strlen(buf1));
- tt_assert(detect_compression_method(buf2, len1) == GZIP_METHOD);
+ tt_assert(!tor_compress(&buf2, &len1, buf1, strlen(buf1)+1,
+ GZIP_METHOD));
+ tt_assert(buf2 != NULL);
+ tt_int_op(len1, OP_LT, strlen(buf1));
+ tt_int_op(detect_compression_method(buf2, len1), OP_EQ, GZIP_METHOD);
- tt_assert(!tor_gzip_uncompress(&buf3, &len2, buf2, len1,
- GZIP_METHOD, 1, LOG_INFO));
- tt_assert(buf3);
- tt_int_op(strlen(buf1) + 1,OP_EQ, len2);
- tt_str_op(buf1,OP_EQ, buf3);
+ tt_assert(!tor_uncompress(&buf3, &len2, buf2, len1,
+ GZIP_METHOD, 1, LOG_INFO));
+ tt_assert(buf3 != NULL);
+ tt_int_op(strlen(buf1) + 1, OP_EQ, len2);
+ tt_str_op(buf1, OP_EQ, buf3);
tor_free(buf2);
tor_free(buf3);
- tt_assert(!tor_gzip_compress(&buf2, &len1, buf1, strlen(buf1)+1,
- ZLIB_METHOD));
+ tt_assert(!tor_compress(&buf2, &len1, buf1, strlen(buf1)+1,
+ ZLIB_METHOD));
tt_assert(buf2);
- tt_assert(detect_compression_method(buf2, len1) == ZLIB_METHOD);
+ tt_int_op(detect_compression_method(buf2, len1), OP_EQ, ZLIB_METHOD);
- tt_assert(!tor_gzip_uncompress(&buf3, &len2, buf2, len1,
- ZLIB_METHOD, 1, LOG_INFO));
- tt_assert(buf3);
- tt_int_op(strlen(buf1) + 1,OP_EQ, len2);
- tt_str_op(buf1,OP_EQ, buf3);
+ tt_assert(!tor_uncompress(&buf3, &len2, buf2, len1,
+ ZLIB_METHOD, 1, LOG_INFO));
+ tt_assert(buf3 != NULL);
+ tt_int_op(strlen(buf1) + 1, OP_EQ, len2);
+ tt_str_op(buf1, OP_EQ, buf3);
/* Check whether we can uncompress concatenated, compressed strings. */
tor_free(buf3);
buf2 = tor_reallocarray(buf2, len1, 2);
memcpy(buf2+len1, buf2, len1);
- tt_assert(!tor_gzip_uncompress(&buf3, &len2, buf2, len1*2,
- ZLIB_METHOD, 1, LOG_INFO));
- tt_int_op((strlen(buf1)+1)*2,OP_EQ, len2);
- tt_mem_op(buf3,OP_EQ,
+ tt_assert(!tor_uncompress(&buf3, &len2, buf2, len1*2,
+ ZLIB_METHOD, 1, LOG_INFO));
+ tt_int_op((strlen(buf1)+1)*2, OP_EQ, len2);
+ tt_mem_op(buf3, OP_EQ,
"AAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAZAAAAAAAAAAAAAAAAAAAZ\0"
"AAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAZAAAAAAAAAAAAAAAAAAAZ\0",
(strlen(buf1)+1)*2);
@@ -2300,53 +2303,53 @@ test_util_gzip(void *arg)
/* Check whether we can uncompress partial strings. */
buf1 =
tor_strdup("String with low redundancy that won't be compressed much.");
- tt_assert(!tor_gzip_compress(&buf2, &len1, buf1, strlen(buf1)+1,
- ZLIB_METHOD));
- tt_assert(len1>16);
+ tt_assert(!tor_compress(&buf2, &len1, buf1, strlen(buf1)+1,
+ ZLIB_METHOD));
+ tt_int_op(len1, OP_GT, 16);
/* when we allow an incomplete string, we should succeed.*/
- tt_assert(!tor_gzip_uncompress(&buf3, &len2, buf2, len1-16,
- ZLIB_METHOD, 0, LOG_INFO));
+ tt_assert(!tor_uncompress(&buf3, &len2, buf2, len1-16,
+ ZLIB_METHOD, 0, LOG_INFO));
tt_assert(len2 > 5);
buf3[len2]='\0';
tt_assert(!strcmpstart(buf1, buf3));
/* when we demand a complete string, this must fail. */
tor_free(buf3);
- tt_assert(tor_gzip_uncompress(&buf3, &len2, buf2, len1-16,
- ZLIB_METHOD, 1, LOG_INFO));
- tt_assert(!buf3);
+ tt_assert(tor_uncompress(&buf3, &len2, buf2, len1-16,
+ ZLIB_METHOD, 1, LOG_INFO));
+ tt_assert(buf3 == NULL);
/* Now, try streaming compression. */
tor_free(buf1);
tor_free(buf2);
tor_free(buf3);
- state = tor_zlib_new(1, ZLIB_METHOD, HIGH_COMPRESSION);
+ state = tor_compress_new(1, ZLIB_METHOD, HIGH_COMPRESSION);
tt_assert(state);
cp1 = buf1 = tor_malloc(1024);
len1 = 1024;
ccp2 = "ABCDEFGHIJABCDEFGHIJ";
len2 = 21;
- tt_assert(tor_zlib_process(state, &cp1, &len1, &ccp2, &len2, 0)
- == TOR_ZLIB_OK);
- tt_int_op(0,OP_EQ, len2); /* Make sure we compressed it all. */
+ tt_int_op(tor_compress_process(state, &cp1, &len1, &ccp2, &len2, 0),
+ OP_EQ, TOR_COMPRESS_OK);
+ tt_int_op(0, OP_EQ, len2); /* Make sure we compressed it all. */
tt_assert(cp1 > buf1);
len2 = 0;
cp2 = cp1;
- tt_assert(tor_zlib_process(state, &cp1, &len1, &ccp2, &len2, 1)
- == TOR_ZLIB_DONE);
- tt_int_op(0,OP_EQ, len2);
+ 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. */
- tt_assert(!tor_gzip_uncompress(&buf3, &len2, buf1, 1024-len1,
- ZLIB_METHOD, 1, LOG_WARN));
+ tt_assert(!tor_uncompress(&buf3, &len2, buf1, 1024-len1,
+ ZLIB_METHOD, 1, LOG_WARN));
/* Make sure it compressed right. */
tt_str_op(buf3, OP_EQ, "ABCDEFGHIJABCDEFGHIJ");
- tt_int_op(21,OP_EQ, len2);
+ tt_int_op(21, OP_EQ, len2);
done:
if (state)
- tor_zlib_free(state);
+ tor_compress_free(state);
tor_free(buf2);
tor_free(buf3);
tor_free(buf1);
@@ -2364,13 +2367,13 @@ test_util_gzip_compression_bomb(void *arg)
char *one_mb = tor_malloc_zero(one_million);
char *result = NULL;
size_t result_len = 0;
- tor_zlib_state_t *state = NULL;
+ tor_compress_state_t *state = NULL;
/* Make sure we can't produce a compression bomb */
setup_full_capture_of_logs(LOG_WARN);
- tt_int_op(-1, OP_EQ, tor_gzip_compress(&result, &result_len,
- one_mb, one_million,
- ZLIB_METHOD));
+ tt_int_op(-1, OP_EQ, tor_compress(&result, &result_len,
+ one_mb, one_million,
+ ZLIB_METHOD));
expect_single_log_msg_containing(
"We compressed something and got an insanely high "
"compression factor; other Tors would think this "
@@ -2381,27 +2384,251 @@ test_util_gzip_compression_bomb(void *arg)
const char compression_bomb[1039] =
{ 0x78, 0xDA, 0xED, 0xC1, 0x31, 0x01, 0x00, 0x00, 0x00, 0xC2,
0xA0, 0xF5, 0x4F, 0x6D, 0x08, 0x5F, 0xA0 /* .... */ };
- tt_int_op(-1, OP_EQ, tor_gzip_uncompress(&result, &result_len,
- compression_bomb, 1039,
- ZLIB_METHOD, 0, LOG_WARN));
+ tt_int_op(-1, OP_EQ, tor_uncompress(&result, &result_len,
+ compression_bomb, 1039,
+ ZLIB_METHOD, 0, LOG_WARN));
/* Now try streaming that. */
- state = tor_zlib_new(0, ZLIB_METHOD, HIGH_COMPRESSION);
- tor_zlib_output_t r;
+ state = tor_compress_new(0, ZLIB_METHOD, HIGH_COMPRESSION);
+ tor_compress_output_t r;
const char *inp = compression_bomb;
size_t inlen = 1039;
do {
char *outp = one_mb;
size_t outleft = 4096; /* small on purpose */
- r = tor_zlib_process(state, &outp, &outleft, &inp, &inlen, 0);
+ r = tor_compress_process(state, &outp, &outleft, &inp, &inlen, 0);
tt_int_op(inlen, OP_NE, 0);
- } while (r == TOR_ZLIB_BUF_FULL);
+ } while (r == TOR_COMPRESS_BUFFER_FULL);
- tt_int_op(r, OP_EQ, TOR_ZLIB_ERR);
+ tt_int_op(r, OP_EQ, TOR_COMPRESS_ERROR);
done:
tor_free(one_mb);
- tor_zlib_free(state);
+ tor_compress_free(state);
+}
+
+static void
+test_util_lzma(void *arg)
+{
+#ifdef HAVE_LZMA
+ char *buf1=NULL, *buf2=NULL, *buf3=NULL, *cp1, *cp2;
+ const char *ccp2;
+ size_t len1, len2;
+ tor_compress_state_t *state = NULL;
+
+ (void)arg;
+ tt_assert(tor_compress_supports_method(LZMA_METHOD));
+
+ buf1 = tor_strdup("AAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAZAAAAAAAAAAAAAAAAAAAZ");
+ tt_assert(detect_compression_method(buf1, strlen(buf1)) == UNKNOWN_METHOD);
+
+ tt_assert(!tor_compress(&buf2, &len1, buf1, strlen(buf1)+1,
+ LZMA_METHOD));
+ tt_assert(buf2 != NULL);
+ tt_int_op(len1, OP_LT, strlen(buf1));
+ tt_int_op(detect_compression_method(buf2, len1), OP_EQ, LZMA_METHOD);
+
+ tt_assert(!tor_uncompress(&buf3, &len2, buf2, len1,
+ LZMA_METHOD, 1, LOG_INFO));
+ tt_assert(buf3 != NULL);
+ tt_int_op(strlen(buf1) + 1, OP_EQ, len2);
+ tt_str_op(buf1, OP_EQ, buf3);
+
+ tor_free(buf1);
+ tor_free(buf2);
+ tor_free(buf3);
+
+#if 0
+ /* Check whether we can uncompress concatenated, compressed strings. */
+ tor_free(buf3);
+ buf2 = tor_reallocarray(buf2, len1, 2);
+ memcpy(buf2+len1, buf2, len1);
+ tt_assert(!tor_uncompress(&buf3, &len2, buf2, len1*2,
+ LZMA_METHOD, 1, LOG_INFO));
+ tt_int_op((strlen(buf1)+1)*2, OP_EQ, len2);
+ tt_mem_op(buf3, OP_EQ,
+ "AAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAZAAAAAAAAAAAAAAAAAAAZ\0"
+ "AAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAZAAAAAAAAAAAAAAAAAAAZ\0",
+ (strlen(buf1)+1)*2);
+
+ tor_free(buf1);
+ tor_free(buf2);
+ tor_free(buf3);
+
+ /* Check whether we can uncompress partial strings. */
+ buf1 =
+ tor_strdup("String with low redundancy that won't be compressed much.");
+ tt_assert(!tor_compress(&buf2, &len1, buf1, strlen(buf1)+1,
+ LZMA_METHOD));
+ tt_int_op(len1, OP_GT, 16);
+ /* when we allow an incomplete string, we should succeed.*/
+ tt_assert(!tor_uncompress(&buf3, &len2, buf2, len1-16,
+ LZMA_METHOD, 0, LOG_INFO));
+ tt_assert(len2 > 5);
+ buf3[len2]='\0';
+ tt_assert(!strcmpstart(buf1, buf3));
+
+ /* when we demand a complete string, this must fail. */
+ tor_free(buf3);
+ tt_assert(tor_uncompress(&buf3, &len2, buf2, len1-16,
+ LZMA_METHOD, 1, LOG_INFO));
+ tt_assert(buf3 == NULL);
+
+ tor_free(buf1);
+ tor_free(buf2);
+ tor_free(buf3);
+#endif
+
+ /* Now, try streaming compression. */
+ state = tor_compress_new(1, LZMA_METHOD, HIGH_COMPRESSION);
+ tt_assert(state);
+ cp1 = buf1 = tor_malloc(1024);
+ len1 = 1024;
+ ccp2 = "ABCDEFGHIJABCDEFGHIJ";
+ len2 = 21;
+ tt_int_op(tor_compress_process(state, &cp1, &len1, &ccp2, &len2, 0),
+ OP_EQ, TOR_COMPRESS_OK);
+ tt_int_op(0, OP_EQ, len2); /* Make sure we compressed it all. */
+ tt_assert(cp1 > buf1);
+
+ len2 = 0;
+ cp2 = cp1;
+ 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. */
+
+ tt_assert(!tor_uncompress(&buf3, &len2, buf1, 1024-len1,
+ LZMA_METHOD, 1, LOG_WARN));
+ /* Make sure it compressed right. */
+ tt_str_op(buf3, OP_EQ, "ABCDEFGHIJABCDEFGHIJ");
+ tt_int_op(21, OP_EQ, len2);
+
+ done:
+ if (state)
+ tor_compress_free(state);
+ tor_free(buf2);
+ tor_free(buf3);
+ tor_free(buf1);
+#else
+ (void)arg;
+ tt_assert(! tor_compress_supports_method(LZMA_METHOD));
+
+ done:
+ ;
+#endif // HAVE_LZMA.
+}
+
+static void
+test_util_zstd(void *arg)
+{
+#ifdef HAVE_ZSTD
+ char *buf1=NULL, *buf2=NULL, *buf3=NULL, *cp1, *cp2;
+ const char *ccp2;
+ size_t len1, len2;
+ tor_compress_state_t *state = NULL;
+
+ (void)arg;
+ tt_assert(tor_compress_supports_method(ZSTD_METHOD));
+
+ buf1 = tor_strdup("AAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAZAAAAAAAAAAAAAAAAAAAZ");
+ tt_assert(detect_compression_method(buf1, strlen(buf1)) == UNKNOWN_METHOD);
+
+ tt_assert(!tor_compress(&buf2, &len1, buf1, strlen(buf1)+1,
+ ZSTD_METHOD));
+ tt_assert(buf2 != NULL);
+ tt_int_op(len1, OP_LT, strlen(buf1));
+ tt_int_op(detect_compression_method(buf2, len1), OP_EQ, ZSTD_METHOD);
+
+ tt_assert(!tor_uncompress(&buf3, &len2, buf2, len1,
+ ZSTD_METHOD, 1, LOG_INFO));
+ tt_assert(buf3 != NULL);
+ tt_int_op(strlen(buf1) + 1, OP_EQ, len2);
+ tt_str_op(buf1, OP_EQ, buf3);
+
+ tor_free(buf1);
+ tor_free(buf2);
+ tor_free(buf3);
+
+#if 0
+ /* Check whether we can uncompress concatenated, compressed strings. */
+ tor_free(buf3);
+ buf2 = tor_reallocarray(buf2, len1, 2);
+ memcpy(buf2+len1, buf2, len1);
+ tt_assert(!tor_uncompress(&buf3, &len2, buf2, len1*2,
+ ZSTD_METHOD, 1, LOG_INFO));
+ tt_int_op((strlen(buf1)+1)*2, OP_EQ, len2);
+ tt_mem_op(buf3, OP_EQ,
+ "AAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAZAAAAAAAAAAAAAAAAAAAZ\0"
+ "AAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAZAAAAAAAAAAAAAAAAAAAZ\0",
+ (strlen(buf1)+1)*2);
+
+ tor_free(buf1);
+ tor_free(buf2);
+ tor_free(buf3);
+
+ /* Check whether we can uncompress partial strings. */
+ buf1 =
+ tor_strdup("String with low redundancy that won't be compressed much.");
+ tt_assert(!tor_compress(&buf2, &len1, buf1, strlen(buf1)+1,
+ ZSTD_METHOD));
+ tt_int_op(len1, OP_GT, 16);
+ /* when we allow an incomplete string, we should succeed.*/
+ tt_assert(!tor_uncompress(&buf3, &len2, buf2, len1-16,
+ ZSTD_METHOD, 0, LOG_INFO));
+ tt_assert(len2 > 5);
+ buf3[len2]='\0';
+ tt_assert(!strcmpstart(buf1, buf3));
+
+ /* when we demand a complete string, this must fail. */
+ tor_free(buf3);
+ tt_assert(tor_uncompress(&buf3, &len2, buf2, len1-16,
+ ZSTD_METHOD, 1, LOG_INFO));
+ tt_assert(buf3 == NULL);
+
+ tor_free(buf1);
+ tor_free(buf2);
+ tor_free(buf3);
+#endif
+
+ /* Now, try streaming compression. */
+ state = tor_compress_new(1, ZSTD_METHOD, HIGH_COMPRESSION);
+ tt_assert(state);
+ cp1 = buf1 = tor_malloc(1024);
+ len1 = 1024;
+ ccp2 = "ABCDEFGHIJABCDEFGHIJ";
+ len2 = 21;
+ tt_int_op(tor_compress_process(state, &cp1, &len1, &ccp2, &len2, 0),
+ OP_EQ, TOR_COMPRESS_OK);
+ tt_int_op(0, OP_EQ, len2); /* Make sure we compressed it all. */
+// tt_assert(cp1 > buf1);
+
+ len2 = 0;
+ cp2 = cp1;
+ 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. */
+
+ tt_assert(!tor_uncompress(&buf3, &len2, buf1, 1024-len1,
+ ZSTD_METHOD, 1, LOG_WARN));
+ /* Make sure it compressed right. */
+ tt_str_op(buf3, OP_EQ, "ABCDEFGHIJABCDEFGHIJ");
+ tt_int_op(21, OP_EQ, len2);
+
+ done:
+ if (state)
+ tor_compress_free(state);
+ tor_free(buf2);
+ tor_free(buf3);
+ tor_free(buf1);
+#else
+ (void)arg;
+ tt_assert(! tor_compress_supports_method(ZSTD_METHOD));
+
+ done:
+ ;
+#endif // HAVE_ZSTD.
}
/** Run unit tests for mmap() wrapper functionality. */
@@ -5717,6 +5944,8 @@ struct testcase_t util_tests[] = {
UTIL_LEGACY(pow2),
UTIL_LEGACY(gzip),
UTIL_TEST(gzip_compression_bomb, TT_FORK),
+ UTIL_LEGACY(lzma),
+ UTIL_LEGACY(zstd),
UTIL_LEGACY(datadir),
UTIL_LEGACY(memarea),
UTIL_LEGACY(control_formats),