]> git.ipfire.org Git - thirdparty/dovecot/core.git/commitdiff
lib-compression: Add unit tests for gz header handling bugs
authorTimo Sirainen <timo.sirainen@open-xchange.com>
Tue, 7 Apr 2020 07:49:10 +0000 (10:49 +0300)
committertimo.sirainen <timo.sirainen@open-xchange.com>
Wed, 8 Apr 2020 08:01:46 +0000 (08:01 +0000)
src/lib-compression/test-compression.c

index dfd9dd8c0433d483c8274de414341d4e0a47da43..072f79c3defd00fd01dee2482e43117a566451e8 100644 (file)
@@ -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
        };