mirror of
https://gitlab.torproject.org/tpo/core/tor.git
synced 2024-12-06 19:41:15 +01:00
Refactor the streaming compression code.
This patch refactors our streaming compression code to allow us to extend it with non-zlib/non-gzip based compression schemas. See https://bugs.torproject.org/21663
This commit is contained in:
@@ -584,12 +584,12 @@ test_buffers_zlib_impl(int finalize_with_nil)
|
||||
char *contents = NULL;
|
||||
char *expanded = NULL;
|
||||
buf_t *buf = NULL;
|
||||
tor_zlib_state_t *zlib_state = NULL;
|
||||
tor_compress_state_t *zlib_state = NULL;
|
||||
size_t out_len, in_len;
|
||||
int done;
|
||||
|
||||
buf = buf_new_with_capacity(128); /* will round up */
|
||||
zlib_state = tor_zlib_new(1, ZLIB_METHOD, HIGH_COMPRESSION);
|
||||
zlib_state = tor_compress_new(1, ZLIB_METHOD, HIGH_COMPRESSION);
|
||||
|
||||
msg = tor_malloc(512);
|
||||
crypto_rand(msg, 512);
|
||||
@@ -621,7 +621,7 @@ test_buffers_zlib_impl(int finalize_with_nil)
|
||||
|
||||
done:
|
||||
buf_free(buf);
|
||||
tor_zlib_free(zlib_state);
|
||||
tor_compress_free(zlib_state);
|
||||
tor_free(contents);
|
||||
tor_free(expanded);
|
||||
tor_free(msg);
|
||||
@@ -647,7 +647,7 @@ test_buffers_zlib_fin_at_chunk_end(void *arg)
|
||||
char *contents = NULL;
|
||||
char *expanded = NULL;
|
||||
buf_t *buf = NULL;
|
||||
tor_zlib_state_t *zlib_state = NULL;
|
||||
tor_compress_state_t *zlib_state = NULL;
|
||||
size_t out_len, in_len;
|
||||
size_t sz, headerjunk;
|
||||
(void) arg;
|
||||
@@ -666,7 +666,7 @@ test_buffers_zlib_fin_at_chunk_end(void *arg)
|
||||
tt_uint_op(buf->head->datalen, OP_EQ, headerjunk);
|
||||
tt_uint_op(buf_datalen(buf), OP_EQ, headerjunk);
|
||||
/* Write an empty string, with finalization on. */
|
||||
zlib_state = tor_zlib_new(1, ZLIB_METHOD, HIGH_COMPRESSION);
|
||||
zlib_state = tor_compress_new(1, ZLIB_METHOD, HIGH_COMPRESSION);
|
||||
tt_int_op(write_to_buf_zlib(buf, zlib_state, "", 0, 1), OP_EQ, 0);
|
||||
|
||||
in_len = buf_datalen(buf);
|
||||
@@ -687,7 +687,7 @@ test_buffers_zlib_fin_at_chunk_end(void *arg)
|
||||
|
||||
done:
|
||||
buf_free(buf);
|
||||
tor_zlib_free(zlib_state);
|
||||
tor_compress_free(zlib_state);
|
||||
tor_free(contents);
|
||||
tor_free(expanded);
|
||||
tor_free(msg);
|
||||
|
||||
+14
-14
@@ -2249,7 +2249,7 @@ 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;
|
||||
buf1 = tor_strdup("AAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAZAAAAAAAAAAAAAAAAAAAZ");
|
||||
@@ -2320,21 +2320,21 @@ test_util_gzip(void *arg)
|
||||
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(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(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. */
|
||||
|
||||
@@ -2346,7 +2346,7 @@ test_util_gzip(void *arg)
|
||||
|
||||
done:
|
||||
if (state)
|
||||
tor_zlib_free(state);
|
||||
tor_compress_free(state);
|
||||
tor_free(buf2);
|
||||
tor_free(buf3);
|
||||
tor_free(buf1);
|
||||
@@ -2364,7 +2364,7 @@ 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);
|
||||
@@ -2386,22 +2386,22 @@ test_util_gzip_compression_bomb(void *arg)
|
||||
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);
|
||||
}
|
||||
|
||||
/** Run unit tests for mmap() wrapper functionality. */
|
||||
|
||||
Reference in New Issue
Block a user