]> git.ipfire.org Git - thirdparty/dovecot/core.git/commitdiff
lib-compression: Add unit test to compress large input
authorTimo Sirainen <timo.sirainen@open-xchange.com>
Wed, 9 Sep 2020 09:08:38 +0000 (12:08 +0300)
committertimo.sirainen <timo.sirainen@open-xchange.com>
Thu, 10 Sep 2020 13:52:25 +0000 (13:52 +0000)
This catches earlier zstd and lzma bugs.

src/lib-compression/test-compression.c

index cd6fd20384aed01a7ca4781138d562fce7536272..4513cce819f924fcda1396607c56dadb212ebc89 100644 (file)
@@ -3,6 +3,7 @@
 #include "lib.h"
 #include "buffer.h"
 #include "istream.h"
+#include "istream-hash.h"
 #include "ostream.h"
 #include "sha1.h"
 #include "randgen.h"
@@ -526,6 +527,66 @@ test_compression_handler_random_io(const struct compression_handler *handler)
        buffer_free(&dec_buf);
 }
 
+static void
+test_compression_handler_large_random_io(const struct compression_handler *handler)
+{
+#define RANDOMNESS_SIZE (1024*1024)
+       unsigned char *randomness;
+       struct istream *input, *dec_input;
+       struct ostream *buf_output, *output;
+       buffer_t *buf;
+       const unsigned char *data;
+       size_t size;
+       int ret;
+
+       test_begin(t_strdup_printf("compression handler %s (large random io)", handler->name));
+       randomness = i_malloc(RANDOMNESS_SIZE);
+       random_fill(randomness, RANDOMNESS_SIZE);
+
+       /* write 1 MB of randomness to buffer */
+       input = i_stream_create_from_data(randomness, RANDOMNESS_SIZE);
+
+       buf = buffer_create_dynamic(default_pool, 1024);
+       buf_output = o_stream_create_buffer(buf);
+       output = handler->create_ostream(buf_output, i_rand_minmax(1, 6));
+
+       switch (o_stream_send_istream(output, input)) {
+       case OSTREAM_SEND_ISTREAM_RESULT_ERROR_INPUT:
+       case OSTREAM_SEND_ISTREAM_RESULT_ERROR_OUTPUT:
+               test_assert(FALSE);
+               break;
+       case OSTREAM_SEND_ISTREAM_RESULT_WAIT_OUTPUT:
+       case OSTREAM_SEND_ISTREAM_RESULT_WAIT_INPUT:
+               i_unreached();
+       case OSTREAM_SEND_ISTREAM_RESULT_FINISHED:
+               test_assert(o_stream_finish(output) == 1);
+               break;
+       }
+       test_assert(output->offset == RANDOMNESS_SIZE);
+       test_assert(output->stream_errno == 0);
+       o_stream_unref(&buf_output);
+       o_stream_unref(&output);
+       i_stream_unref(&input);
+
+       /* verify that reading the input works */
+
+       input = i_stream_create_from_data(buf->data, buf->used);
+       dec_input = handler->create_istream(input, FALSE);
+
+       while ((ret = i_stream_read_more(dec_input, &data, &size)) > 0) {
+               test_assert(memcmp(data, randomness + dec_input->v_offset, size) == 0);
+               i_stream_skip(dec_input, size);
+       }
+       test_assert(ret == -1);
+       test_assert(dec_input->v_offset == RANDOMNESS_SIZE);
+       test_assert(dec_input->stream_errno == 0);
+
+       i_stream_unref(&dec_input);
+       i_stream_unref(&input);
+       buffer_free(&buf);
+       test_end();
+}
+
 static void test_compression_handler_errors(const struct compression_handler *handler)
 {
        test_begin(t_strdup_printf("compression handler %s (errors)", handler->name));
@@ -608,6 +669,7 @@ static void test_compression(void)
                        test_compression_handler_reset(&compression_handlers[i]);
                        test_compression_handler_partial_parent_write(&compression_handlers[i]);
                        test_compression_handler_random_io(&compression_handlers[i]);
+                       test_compression_handler_large_random_io(&compression_handlers[i]);
                        test_compression_handler_errors(&compression_handlers[i]);
                } T_END;
        }