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 extract_reference_file(refname);
54 p = slurpfile(&archive_size, refname);
56 assert((a = archive_read_new()) != NULL);
57 assertEqualIntA(a, ARCHIVE_OK, archive_read_support_format_zip(a));
58 assertEqualIntA(a, ARCHIVE_OK, read_open_memory_seek(a, p, archive_size, 16 * 1024));
59 assertEqualInt(ARCHIVE_OK, archive_read_next_header(a, &entry));
61 assertEqualInt(ARCHIVE_OK, archive_read_data_block(a, &pv, &s, &o));
62 assertEqualInt(262144, s);
65 assertEqualInt(ARCHIVE_OK, archive_read_data_block(a, &pv, &s, &o));
66 assertEqualInt(160, s);
67 assertEqualInt(262144, o);
69 assertEqualInt(ARCHIVE_EOF, archive_read_data_block(a, &pv, &s, &o));
71 assertEqualInt(ARCHIVE_OK, archive_free(a));
76 * Synthesize a lot of varying inputs that are highly compressible.
78 DEFINE_TEST(test_read_format_zip_high_compression2)
80 const size_t body_size = 1024 * 1024;
81 const size_t buff_size = 2 * 1024 * 1024;
82 char *body, *body_read, *buff;
85 assert((body = malloc(body_size)) != NULL);
86 assert((body_read = malloc(body_size)) != NULL);
87 assert((buff = malloc(buff_size)) != NULL);
89 /* Highly-compressible data: all bytes 255, except for a
91 * The body is always 256k + 6 bytes long (the internal deflation
92 * buffer is exactly 256k).
95 for(n = 1024; n < (int)body_size; n += 1024) {
97 struct archive_entry *entry;
103 memset(body, 255, body_size);
106 /* Write an archive with a single entry of n bytes. */
107 assert((a = archive_write_new()) != NULL);
108 assertEqualInt(ARCHIVE_OK, archive_write_set_format_zip(a));
109 assertEqualInt(ARCHIVE_OK, archive_write_open_memory(a, buff, buff_size, &used));
111 entry = archive_entry_new2(a);
112 archive_entry_set_pathname(entry, "test");
113 archive_entry_set_filetype(entry, AE_IFREG);
114 archive_entry_set_size(entry, 262150);
115 assertEqualInt(ARCHIVE_OK, archive_write_header(a, entry));
116 archive_entry_free(entry);
117 assertEqualInt(262150, archive_write_data(a, body, 262150));
118 assertEqualInt(ARCHIVE_OK, archive_write_free(a));
120 /* Read back the entry and verify the contents. */
121 assert((a = archive_read_new()) != NULL);
122 assertEqualIntA(a, ARCHIVE_OK, archive_read_support_format_all(a));
123 assertEqualIntA(a, ARCHIVE_OK, archive_read_support_filter_all(a));
124 assertEqualIntA(a, ARCHIVE_OK, read_open_memory(a, buff, used, 17));
125 assertEqualInt(ARCHIVE_OK, archive_read_next_header(a, &entry));
127 assertEqualInt(ARCHIVE_OK, archive_read_data_block(a, &pv, &s, &o));
128 assertEqualInt(262144, s);
129 assertEqualInt(0, o);
131 assertEqualInt(ARCHIVE_OK, archive_read_data_block(a, &pv, &s, &o));
132 assertEqualInt(6, s);
133 assertEqualInt(262144, o);
135 assertEqualInt(ARCHIVE_EOF, archive_read_data_block(a, &pv, &s, &o));
137 assertEqualInt(ARCHIVE_OK, archive_free(a));