2 * Copyright (c) 2016 Tim Kientzle
5 * Redistribution and use in source and binary forms, with or without
6 * modification, are permitted provided that the following conditions
8 * 1. Redistributions of source code must retain the above copyright
9 * notice, this list of conditions and the following disclaimer.
10 * 2. Redistributions in binary form must reproduce the above copyright
11 * notice, this list of conditions and the following disclaimer in the
12 * documentation and/or other materials provided with the distribution.
14 * THIS SOFTWARE IS PROVIDED BY THE AUTHOR(S) ``AS IS'' AND ANY EXPRESS OR
15 * IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE IMPLIED WARRANTIES
16 * OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE ARE DISCLAIMED.
17 * IN NO EVENT SHALL THE AUTHOR(S) BE LIABLE FOR ANY DIRECT, INDIRECT,
18 * INCIDENTAL, SPECIAL, EXEMPLARY, OR CONSEQUENTIAL DAMAGES (INCLUDING, BUT
19 * NOT LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS OR SERVICES; LOSS OF USE,
20 * DATA, OR PROFITS; OR BUSINESS INTERRUPTION) HOWEVER CAUSED AND ON ANY
21 * THEORY OF LIABILITY, WHETHER IN CONTRACT, STRICT LIABILITY, OR TORT
22 * (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY OUT OF THE USE OF
23 * THIS SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF SUCH DAMAGE.
32 * Github Issue 748 reported problems with end-of-entry handling
33 * with highly-compressible data. This resulted in the end of the
34 * data being truncated (extracted as zero bytes).
38 * Extract the specific test archive that was used to diagnose
41 DEFINE_TEST(test_read_format_zip_high_compression)
43 const char *refname = "test_read_format_zip_high_compression.zip";
47 struct archive_entry *entry;
53 if (archive_zlib_version() == NULL) {
54 skipping("Zip compression test requires zlib");
58 extract_reference_file(refname);
59 p = slurpfile(&archive_size, "%s", refname);
61 assert((a = archive_read_new()) != NULL);
62 assertEqualIntA(a, ARCHIVE_OK, archive_read_support_format_zip(a));
63 assertEqualIntA(a, ARCHIVE_OK, read_open_memory_seek(a, p, archive_size, 16 * 1024));
64 assertEqualInt(ARCHIVE_OK, archive_read_next_header(a, &entry));
66 assertEqualInt(ARCHIVE_OK, archive_read_data_block(a, &pv, &s, &o));
67 assertEqualInt(262144, s);
70 assertEqualInt(ARCHIVE_OK, archive_read_data_block(a, &pv, &s, &o));
71 assertEqualInt(160, s);
72 assertEqualInt(262144, o);
74 assertEqualInt(ARCHIVE_EOF, archive_read_data_block(a, &pv, &s, &o));
76 assertEqualInt(ARCHIVE_OK, archive_free(a));
81 * Synthesize a lot of varying inputs that are highly compressible.
83 DEFINE_TEST(test_read_format_zip_high_compression2)
85 const size_t body_size = 1024 * 1024;
86 const size_t buff_size = 2 * 1024 * 1024;
87 char *body, *body_read, *buff;
90 if (archive_zlib_version() == NULL) {
91 skipping("Zip compression test requires zlib");
95 assert((body = malloc(body_size)) != NULL);
96 assert((body_read = malloc(body_size)) != NULL);
97 assert((buff = malloc(buff_size)) != NULL);
99 /* Highly-compressible data: all bytes 255, except for a
101 * The body is always 256k + 6 bytes long (the internal deflation
102 * buffer is exactly 256k).
105 for(n = 1024; n < (int)body_size; n += 1024) {
107 struct archive_entry *entry;
113 memset(body, 255, body_size);
116 /* Write an archive with a single entry of n bytes. */
117 assert((a = archive_write_new()) != NULL);
118 assertEqualInt(ARCHIVE_OK, archive_write_set_format_zip(a));
119 assertEqualInt(ARCHIVE_OK, archive_write_open_memory(a, buff, buff_size, &used));
121 entry = archive_entry_new2(a);
122 archive_entry_set_pathname(entry, "test");
123 archive_entry_set_filetype(entry, AE_IFREG);
124 archive_entry_set_size(entry, 262150);
125 assertEqualInt(ARCHIVE_OK, archive_write_header(a, entry));
126 archive_entry_free(entry);
127 assertEqualInt(262150, archive_write_data(a, body, 262150));
128 assertEqualInt(ARCHIVE_OK, archive_write_free(a));
130 /* Read back the entry and verify the contents. */
131 assert((a = archive_read_new()) != NULL);
132 assertEqualIntA(a, ARCHIVE_OK, archive_read_support_format_all(a));
133 assertEqualIntA(a, ARCHIVE_OK, archive_read_support_filter_all(a));
134 assertEqualIntA(a, ARCHIVE_OK, read_open_memory(a, buff, used, 17));
135 assertEqualInt(ARCHIVE_OK, archive_read_next_header(a, &entry));
137 assertEqualInt(ARCHIVE_OK, archive_read_data_block(a, &pv, &s, &o));
138 assertEqualInt(262144, s);
139 assertEqualInt(0, o);
141 assertEqualInt(ARCHIVE_OK, archive_read_data_block(a, &pv, &s, &o));
142 assertEqualInt(6, s);
143 assertEqualInt(262144, o);
145 assertEqualInt(ARCHIVE_EOF, archive_read_data_block(a, &pv, &s, &o));
147 assertEqualInt(ARCHIVE_OK, archive_free(a));