changeset 20959:7aca58261697

lib-compression: Added unit tests for reading .gz files
author Timo Sirainen <timo.sirainen@dovecot.fi>
date Mon, 24 Oct 2016 18:52:24 +0300
parents 330b83e97c5f
children 5ecd72688313
files src/lib-compression/test-compression.c
diffstat 1 files changed, 66 insertions(+), 0 deletions(-) [+]
line wrap: on
line diff
--- a/src/lib-compression/test-compression.c	Mon Oct 24 18:31:53 2016 +0300
+++ b/src/lib-compression/test-compression.c	Mon Oct 24 18:52:24 2016 +0300
@@ -1,6 +1,7 @@
 /* Copyright (c) 2014-2016 Dovecot authors, see the included COPYING file */
 
 #include "lib.h"
+#include "buffer.h"
 #include "istream.h"
 #include "ostream.h"
 #include "sha1.h"
@@ -94,6 +95,69 @@
 	}
 }
 
+static void test_gz(const char *str1, const char *str2)
+{
+	const struct compression_handler *gz = compression_lookup_handler("gz");
+	struct ostream *buf_output, *output;
+	struct istream *test_input, *input;
+	buffer_t *buf = buffer_create_dynamic(pool_datastack_create(), 512);
+
+	if (gz == NULL || gz->create_ostream == NULL)
+		return; /* not compiled in */
+
+	/* write concated output */
+	buf_output = o_stream_create_buffer(buf);
+
+	output = gz->create_ostream(buf_output, 6);
+	o_stream_nsend_str(output, str1);
+	test_assert(o_stream_nfinish(output) == 0);
+	o_stream_destroy(&output);
+
+	if (str2[0] != '\0') {
+		output = gz->create_ostream(buf_output, 6);
+		o_stream_nsend_str(output, "world");
+		test_assert(o_stream_nfinish(output) == 0);
+		o_stream_destroy(&output);
+	}
+
+	o_stream_destroy(&buf_output);
+
+	/* read concated input */
+	const unsigned char *data;
+	size_t size;
+	test_input = test_istream_create_data(buf->data, buf->used);
+	test_istream_set_allow_eof(test_input, FALSE);
+	input = gz->create_istream(test_input, 1);
+	for (size_t i = 0; i <= buf->used; i++) {
+		test_istream_set_size(test_input, i);
+		test_assert(i_stream_read(input) >= 0);
+	}
+	test_istream_set_allow_eof(test_input, TRUE);
+	test_assert(i_stream_read(input) == -1);
+	test_assert(input->stream_errno == 0);
+
+	data = i_stream_get_data(input, &size);
+	test_assert(size == strlen(str1)+strlen(str2) &&
+		    memcmp(data, str1, strlen(str1)) == 0 &&
+		    memcmp(data+strlen(str1), str2, strlen(str2)) == 0);
+	i_stream_unref(&input);
+	i_stream_unref(&test_input);
+}
+
+static void test_gz_concat(void)
+{
+	test_begin("gz concat");
+	test_gz("hello", "world");
+	test_end();
+}
+
+static void test_gz_no_concat(void)
+{
+	test_begin("gz concat");
+	test_gz("hello", "");
+	test_end();
+}
+
 static void test_uncompress_file(const char *path)
 {
 	const struct compression_handler *handler;
@@ -182,6 +246,8 @@
 {
 	static void (*test_functions[])(void) = {
 		test_compression,
+		test_gz_concat,
+		test_gz_no_concat,
 		NULL
 	};
 	if (argc == 2) {