From 67a217b80799c92fd91cfa1e5b9198fbd0ed1076 Mon Sep 17 00:00:00 2001 From: Timo Sirainen Date: Tue, 7 Apr 2020 10:49:10 +0300 Subject: [PATCH] lib-compression: Add unit tests for gz header handling bugs --- src/lib-compression/test-compression.c | 31 ++++++++++++++++++++++++++ 1 file changed, 31 insertions(+) diff --git a/src/lib-compression/test-compression.c b/src/lib-compression/test-compression.c index dfd9dd8c04..072f79c3de 100644 --- a/src/lib-compression/test-compression.c +++ b/src/lib-compression/test-compression.c @@ -660,6 +660,36 @@ static void test_gz_no_concat(void) test_end(); } +static void test_gz_header(void) +{ + const struct compression_handler *gz = compression_lookup_handler("gz"); + const char *input_strings[] = { + "\x1F\x8B", + "\x1F\x8B\x01\x02"/* GZ_FLAG_FHCRC */"\xFF\xFF\x01\x01\x01\x01", + "\x1F\x8B\x01\x04"/* GZ_FLAG_FEXTRA */"\xFF\xFF\x01\x01\x01\x01", + "\x1F\x8B\x01\x08"/* GZ_FLAG_FNAME */"\x01\x01\x01\x01\x01\x01", + "\x1F\x8B\x01\x10"/* GZ_FLAG_FCOMMENT */"\x01\x01\x01\x01\x01\x01", + "\x1F\x8B\x01\x0C"/* GZ_FLAG_FEXTRA | GZ_FLAG_FNAME */"\xFF\xFF\x01\x01\x01\x01", + }; + struct istream *file_input, *input; + + if (gz == NULL || gz->create_istream == NULL) + return; /* not compiled in */ + + test_begin("gz header"); + for (unsigned int i = 0; i < N_ELEMENTS(input_strings); i++) { + file_input = test_istream_create_data(input_strings[i], + strlen(input_strings[i])); + file_input->blocking = TRUE; + input = gz->create_istream(file_input, FALSE); + test_assert_idx(i_stream_read(input) == -1, i); + test_assert_idx(input->stream_errno == EINVAL, i); + i_stream_unref(&input); + i_stream_unref(&file_input); + } + test_end(); +} + static void test_gz_large_header(void) { const struct compression_handler *gz = compression_lookup_handler("gz"); @@ -792,6 +822,7 @@ int main(int argc, char *argv[]) test_compression, test_gz_concat, test_gz_no_concat, + test_gz_header, test_gz_large_header, NULL }; -- 2.47.3