2 * Copyright (c) 2003-2007 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.
26 __FBSDID("$FreeBSD$");
28 DEFINE_TEST(test_read_pax_truncated)
30 struct archive_entry *ae;
33 size_t buff_size = 1000000;
34 ssize_t filedata_size = 100000;
35 char *buff = malloc(buff_size);
36 char *buff2 = malloc(buff_size);
37 char *filedata = malloc(filedata_size);
39 /* Create a new archive in memory. */
40 assert((a = archive_write_new()) != NULL);
41 assertA(0 == archive_write_set_format_pax(a));
42 assertA(0 == archive_write_set_compression_none(a));
43 assertA(0 == archive_write_open_memory(a, buff, buff_size, &used));
48 assert((ae = archive_entry_new()) != NULL);
49 archive_entry_copy_pathname(ae, "file");
50 archive_entry_set_mode(ae, S_IFREG | 0755);
51 for (i = 0; i < filedata_size; i++)
52 filedata[i] = (unsigned char)rand();
53 archive_entry_set_atime(ae, 1, 2);
54 archive_entry_set_ctime(ae, 3, 4);
55 archive_entry_set_mtime(ae, 5, 6);
56 archive_entry_set_size(ae, filedata_size);
57 assertA(0 == archive_write_header(a, ae));
58 archive_entry_free(ae);
59 assertA(filedata_size == archive_write_data(a, filedata, filedata_size));
61 /* Close out the archive. */
62 assertA(0 == archive_write_close(a));
63 #if ARCHIVE_API_VERSION > 1
64 assertA(0 == archive_write_finish(a));
66 archive_write_finish(a);
69 /* Now, read back a truncated version of the archive and
70 * verify that we get an appropriate error. */
71 for (i = 1; i < used + 100; i += 100) {
72 assert((a = archive_read_new()) != NULL);
73 assertA(0 == archive_read_support_format_all(a));
74 assertA(0 == archive_read_support_compression_all(a));
75 assertA(0 == read_open_memory(a, buff, i, 13));
78 assertEqualIntA(a, ARCHIVE_FATAL, archive_read_next_header(a, &ae));
81 failure("Archive truncated to %d bytes", i);
82 assertEqualIntA(a, 0, archive_read_next_header(a, &ae));
85 if (i < 1536 + filedata_size) {
86 assertA(ARCHIVE_FATAL == archive_read_data(a, filedata, filedata_size));
89 failure("Archive truncated to %d bytes", i);
90 assertEqualIntA(a, filedata_size,
91 archive_read_data(a, filedata, filedata_size));
94 /* Verify the end of the archive. */
95 /* Archive must be long enough to capture a 512-byte
96 * block of zeroes after the entry. (POSIX requires a
97 * second block of zeros to be written but libarchive
98 * does not return an error if it can't consume
100 if (i < 1536 + 512*((filedata_size + 511)/512) + 512) {
101 assertA(ARCHIVE_FATAL == archive_read_next_header(a, &ae));
103 assertA(ARCHIVE_EOF == archive_read_next_header(a, &ae));
106 assert(0 == archive_read_close(a));
107 #if ARCHIVE_API_VERSION > 1
108 assert(0 == archive_read_finish(a));
110 archive_read_finish(a);
116 /* Same as above, except skip the body instead of reading it. */
117 for (i = 1; i < used + 100; i += 100) {
118 assert((a = archive_read_new()) != NULL);
119 assertA(0 == archive_read_support_format_all(a));
120 assertA(0 == archive_read_support_compression_all(a));
121 assertA(0 == read_open_memory(a, buff, i, 7));
124 assertA(ARCHIVE_FATAL == archive_read_next_header(a, &ae));
127 assertEqualIntA(a, 0, archive_read_next_header(a, &ae));
130 if (i < 1536 + 512*((filedata_size+511)/512)) {
131 assertA(ARCHIVE_FATAL == archive_read_data_skip(a));
134 assertA(ARCHIVE_OK == archive_read_data_skip(a));
137 /* Verify the end of the archive. */
138 /* Archive must be long enough to capture a 512-byte
139 * block of zeroes after the entry. (POSIX requires a
140 * second block of zeros to be written but libarchive
141 * does not return an error if it can't consume
143 if (i < 1536 + 512*((filedata_size + 511)/512) + 512) {
144 assertA(ARCHIVE_FATAL == archive_read_next_header(a, &ae));
146 assertA(ARCHIVE_EOF == archive_read_next_header(a, &ae));
149 assert(0 == archive_read_close(a));
150 #if ARCHIVE_API_VERSION > 1
151 assert(0 == archive_read_finish(a));
153 archive_read_finish(a);
157 /* Now, damage the archive in various ways and test the responses. */
159 /* Damage the first size field in the pax attributes. */
160 memcpy(buff2, buff, buff_size);
163 buff2[514] = 'A'; /* Non-digit in size. */
164 assert((a = archive_read_new()) != NULL);
165 assertA(0 == archive_read_support_format_all(a));
166 assertA(0 == archive_read_support_compression_all(a));
167 assertA(0 == archive_read_open_memory(a, buff2, used));
168 assertEqualIntA(a, ARCHIVE_WARN, archive_read_next_header(a, &ae));
169 assert(0 == archive_read_close(a));
170 #if ARCHIVE_API_VERSION > 1
171 assert(0 == archive_read_finish(a));
173 archive_read_finish(a);
176 /* Damage the size field in the pax attributes. */
177 memcpy(buff2, buff, buff_size);
178 buff2[512] = 'A'; /* First character not a digit. */
179 assert((a = archive_read_new()) != NULL);
180 assertA(0 == archive_read_support_format_all(a));
181 assertA(0 == archive_read_support_compression_all(a));
182 assertA(0 == archive_read_open_memory(a, buff2, used));
183 assertEqualIntA(a, ARCHIVE_WARN, archive_read_next_header(a, &ae));
184 assert(0 == archive_read_close(a));
185 #if ARCHIVE_API_VERSION > 1
186 assert(0 == archive_read_finish(a));
188 archive_read_finish(a);
191 /* Damage the size field in the pax attributes. */
192 memcpy(buff2, buff, buff_size);
193 for (i = 512; i < 520; i++) /* Size over 999999. */
196 assert((a = archive_read_new()) != NULL);
197 assertA(0 == archive_read_support_format_all(a));
198 assertA(0 == archive_read_support_compression_all(a));
199 assertA(0 == archive_read_open_memory(a, buff2, used));
200 assertEqualIntA(a, ARCHIVE_WARN, archive_read_next_header(a, &ae));
201 assert(0 == archive_read_close(a));
202 #if ARCHIVE_API_VERSION > 1
203 assert(0 == archive_read_finish(a));
205 archive_read_finish(a);
208 /* Damage the size field in the pax attributes. */
209 memcpy(buff2, buff, buff_size);
210 buff2[512] = '9'; /* Valid format, but larger than attribute area. */
214 assert((a = archive_read_new()) != NULL);
215 assertA(0 == archive_read_support_format_all(a));
216 assertA(0 == archive_read_support_compression_all(a));
217 assertA(0 == archive_read_open_memory(a, buff2, used));
218 assertEqualIntA(a, ARCHIVE_WARN, archive_read_next_header(a, &ae));
219 assert(0 == archive_read_close(a));
220 #if ARCHIVE_API_VERSION > 1
221 assert(0 == archive_read_finish(a));
223 archive_read_finish(a);
226 /* Damage the size field in the pax attributes. */
227 memcpy(buff2, buff, buff_size);
228 buff2[512] = '1'; /* Too small. */
230 assert((a = archive_read_new()) != NULL);
231 assertA(0 == archive_read_support_format_all(a));
232 assertA(0 == archive_read_support_compression_all(a));
233 assertA(0 == archive_read_open_memory(a, buff2, used));
234 assertEqualIntA(a, ARCHIVE_WARN, archive_read_next_header(a, &ae));
235 assert(0 == archive_read_close(a));
236 #if ARCHIVE_API_VERSION > 1
237 assert(0 == archive_read_finish(a));
239 archive_read_finish(a);
242 /* Damage the size field in the pax attributes. */
243 memcpy(buff2, buff, buff_size);
244 buff2[512] = ' '; /* No size given. */
245 assert((a = archive_read_new()) != NULL);
246 assertA(0 == archive_read_support_format_all(a));
247 assertA(0 == archive_read_support_compression_all(a));
248 assertA(0 == archive_read_open_memory(a, buff2, used));
249 assertEqualIntA(a, ARCHIVE_WARN, archive_read_next_header(a, &ae));
250 assert(0 == archive_read_close(a));
251 #if ARCHIVE_API_VERSION > 1
252 assert(0 == archive_read_finish(a));
254 archive_read_finish(a);
257 /* Damage the ustar header. */
258 memcpy(buff2, buff, buff_size);
259 buff2[1024]++; /* Break the checksum. */
260 assert((a = archive_read_new()) != NULL);
261 assertA(0 == archive_read_support_format_all(a));
262 assertA(0 == archive_read_support_compression_all(a));
263 assertA(0 == archive_read_open_memory(a, buff2, used));
264 assertEqualIntA(a, ARCHIVE_FATAL, archive_read_next_header(a, &ae));
265 assert(0 == archive_read_close(a));
266 #if ARCHIVE_API_VERSION > 1
267 assert(0 == archive_read_finish(a));
269 archive_read_finish(a);
273 * TODO: Damage the ustar header in various ways and fixup the
274 * checksum in order to test boundary cases in the innermost
275 * ustar header parsing.