1 /*-
2 * Copyright (c) 2016 Tim Kientzle
3 * All rights reserved.
4 *
5 * Redistribution and use in source and binary forms, with or without
6 * modification, are permitted provided that the following conditions
7 * are met:
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.
13 *
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.
24 */
25 #include "test.h"
26
27 #include <locale.h>
28
29 /*
30 * Github Issue 748 reported problems with end-of-entry handling
31 * with highly-compressible data. This resulted in the end of the
32 * data being truncated (extracted as zero bytes).
33 */
34
35 /*
36 * Extract the specific test archive that was used to diagnose
37 * Issue 748:
38 */
DEFINE_TEST(test_read_format_zip_high_compression)39 DEFINE_TEST(test_read_format_zip_high_compression)
40 {
41 const char *refname = "test_read_format_zip_high_compression.zip";
42 char *p;
43 size_t archive_size;
44 struct archive *a;
45 struct archive_entry *entry;
46
47 const void *pv;
48 size_t s;
49 int64_t o;
50
51 if (archive_zlib_version() == NULL) {
52 skipping("Zip compression test requires zlib");
53 return;
54 }
55
56 extract_reference_file(refname);
57 p = slurpfile(&archive_size, "%s", refname);
58
59 assert((a = archive_read_new()) != NULL);
60 assertEqualIntA(a, ARCHIVE_OK, archive_read_support_format_zip(a));
61 assertEqualIntA(a, ARCHIVE_OK, read_open_memory_seek(a, p, archive_size, 16 * 1024));
62 assertEqualInt(ARCHIVE_OK, archive_read_next_header(a, &entry));
63
64 assertEqualInt(ARCHIVE_OK, archive_read_data_block(a, &pv, &s, &o));
65 assertEqualInt(262144, s);
66 assertEqualInt(0, o);
67
68 assertEqualInt(ARCHIVE_OK, archive_read_data_block(a, &pv, &s, &o));
69 assertEqualInt(160, s);
70 assertEqualInt(262144, o);
71
72 assertEqualInt(ARCHIVE_EOF, archive_read_data_block(a, &pv, &s, &o));
73
74 assertEqualInt(ARCHIVE_OK, archive_free(a));
75 free(p);
76 }
77
78 /*
79 * Synthesize a lot of varying inputs that are highly compressible.
80 */
DEFINE_TEST(test_read_format_zip_high_compression2)81 DEFINE_TEST(test_read_format_zip_high_compression2)
82 {
83 const size_t body_size = 1024 * 1024;
84 const size_t buff_size = 2 * 1024 * 1024;
85 char *body, *body_read, *buff;
86 int n;
87
88 if (archive_zlib_version() == NULL) {
89 skipping("Zip compression test requires zlib");
90 return;
91 }
92
93 assert((body = malloc(body_size)) != NULL);
94 assert((body_read = malloc(body_size)) != NULL);
95 assert((buff = malloc(buff_size)) != NULL);
96
97 /* Highly-compressible data: all bytes 255, except for a
98 * single 1 byte.
99 * The body is always 256k + 6 bytes long (the internal deflation
100 * buffer is exactly 256k).
101 */
102
103 for(n = 1024; n < (int)body_size; n += 1024) {
104 struct archive *a;
105 struct archive_entry *entry;
106 size_t used = 0;
107 const void *pv;
108 size_t s;
109 int64_t o;
110
111 memset(body, 255, body_size);
112 body[n] = 1;
113
114 /* Write an archive with a single entry of n bytes. */
115 assert((a = archive_write_new()) != NULL);
116 assertEqualInt(ARCHIVE_OK, archive_write_set_format_zip(a));
117 assertEqualInt(ARCHIVE_OK, archive_write_open_memory(a, buff, buff_size, &used));
118
119 entry = archive_entry_new2(a);
120 archive_entry_set_pathname(entry, "test");
121 archive_entry_set_filetype(entry, AE_IFREG);
122 archive_entry_set_size(entry, 262150);
123 assertEqualInt(ARCHIVE_OK, archive_write_header(a, entry));
124 archive_entry_free(entry);
125 assertEqualInt(262150, archive_write_data(a, body, 262150));
126 assertEqualInt(ARCHIVE_OK, archive_write_free(a));
127
128 /* Read back the entry and verify the contents. */
129 assert((a = archive_read_new()) != NULL);
130 assertEqualIntA(a, ARCHIVE_OK, archive_read_support_format_all(a));
131 assertEqualIntA(a, ARCHIVE_OK, archive_read_support_filter_all(a));
132 assertEqualIntA(a, ARCHIVE_OK, read_open_memory(a, buff, used, 17));
133 assertEqualInt(ARCHIVE_OK, archive_read_next_header(a, &entry));
134
135 assertEqualInt(ARCHIVE_OK, archive_read_data_block(a, &pv, &s, &o));
136 assertEqualInt(262144, s);
137 assertEqualInt(0, o);
138
139 assertEqualInt(ARCHIVE_OK, archive_read_data_block(a, &pv, &s, &o));
140 assertEqualInt(6, s);
141 assertEqualInt(262144, o);
142
143 assertEqualInt(ARCHIVE_EOF, archive_read_data_block(a, &pv, &s, &o));
144
145 assertEqualInt(ARCHIVE_OK, archive_free(a));
146 }
147
148 free(body);
149 free(body_read);
150 free(buff);
151 }
152