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;
32 size_t used, i, buff_size = 1000000;
33 size_t filedata_size = 100000;
34 char *buff = malloc(buff_size);
35 char *buff2 = malloc(buff_size);
36 char *filedata = malloc(filedata_size);
38 /* Create a new archive in memory. */
39 assert((a = archive_write_new()) != NULL);
40 assertEqualIntA(a, ARCHIVE_OK, archive_write_set_format_pax(a));
41 assertEqualIntA(a, ARCHIVE_OK, archive_write_add_filter_none(a));
42 assertEqualIntA(a, ARCHIVE_OK,
43 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 assertEqualIntA(a, ARCHIVE_OK, archive_write_header(a, ae));
58 archive_entry_free(ae);
59 assertEqualIntA(a, (int)filedata_size,
60 (int)archive_write_data(a, filedata, filedata_size));
62 /* Close out the archive. */
63 assertEqualIntA(a, ARCHIVE_OK, archive_write_close(a));
64 assertEqualInt(ARCHIVE_OK, archive_write_free(a));
66 /* Now, read back a truncated version of the archive and
67 * verify that we get an appropriate error. */
68 for (i = 1; i < used + 100; i += 100) {
69 assert((a = archive_read_new()) != NULL);
70 assertEqualIntA(a, ARCHIVE_OK, archive_read_support_format_all(a));
71 assertEqualIntA(a, ARCHIVE_OK, archive_read_support_filter_all(a));
72 /* If it's truncated very early, the file type detection should fail. */
74 assertEqualIntA(a, ARCHIVE_FATAL, read_open_memory_minimal(a, buff, i, 13));
77 assertEqualIntA(a, ARCHIVE_OK, read_open_memory_minimal(a, buff, i, 13));
80 /* If it's truncated in a header, the header read should fail. */
82 assertEqualIntA(a, ARCHIVE_FATAL, archive_read_next_header(a, &ae));
85 failure("Archive truncated to %d bytes", i);
86 assertEqualIntA(a, 0, archive_read_next_header(a, &ae));
89 /* If it's truncated in the body, the body read should fail. */
90 if (i < 1536 + filedata_size) {
91 assertEqualIntA(a, ARCHIVE_FATAL, archive_read_data(a, filedata, filedata_size));
94 failure("Archive truncated to %d bytes", i);
95 assertEqualIntA(a, filedata_size,
96 archive_read_data(a, filedata, filedata_size));
99 /* Verify the end of the archive. */
100 /* Archive must be long enough to capture a 512-byte
101 * block of zeroes after the entry. (POSIX requires a
102 * second block of zeros to be written but libarchive
103 * does not return an error if it can't consume
105 if (i < 1536 + 512*((filedata_size + 511)/512) + 512) {
106 failure("i=%d minsize=%d", i,
107 1536 + 512*((filedata_size + 511)/512) + 512);
108 assertEqualIntA(a, ARCHIVE_FATAL,
109 archive_read_next_header(a, &ae));
111 assertEqualIntA(a, ARCHIVE_EOF,
112 archive_read_next_header(a, &ae));
115 assertEqualIntA(a, ARCHIVE_OK, archive_read_close(a));
116 assertEqualInt(ARCHIVE_OK, archive_read_free(a));
121 /* Same as above, except skip the body instead of reading it. */
122 for (i = 1; i < used + 100; i += 100) {
123 assert((a = archive_read_new()) != NULL);
124 assertEqualIntA(a, ARCHIVE_OK, archive_read_support_format_all(a));
125 assertEqualIntA(a, ARCHIVE_OK, archive_read_support_filter_all(a));
126 /* If it's truncated very early, file type detection should fail. */
128 assertEqualIntA(a, ARCHIVE_FATAL, read_open_memory(a, buff, i, 7));
131 assertEqualIntA(a, ARCHIVE_OK, read_open_memory(a, buff, i, 7));
135 assertEqualIntA(a, ARCHIVE_FATAL, archive_read_next_header(a, &ae));
138 assertEqualIntA(a, 0, archive_read_next_header(a, &ae));
141 if (i < 1536 + 512*((filedata_size+511)/512)) {
142 assertEqualIntA(a, ARCHIVE_FATAL, archive_read_data_skip(a));
145 assertEqualIntA(a, ARCHIVE_OK, archive_read_data_skip(a));
148 /* Verify the end of the archive. */
149 /* Archive must be long enough to capture a 512-byte
150 * block of zeroes after the entry. (POSIX requires a
151 * second block of zeros to be written but libarchive
152 * does not return an error if it can't consume
154 if (i < 1536 + 512*((filedata_size + 511)/512) + 512) {
155 assertEqualIntA(a, ARCHIVE_FATAL,
156 archive_read_next_header(a, &ae));
158 assertEqualIntA(a, ARCHIVE_EOF,
159 archive_read_next_header(a, &ae));
162 assertEqualIntA(a, ARCHIVE_OK, archive_read_close(a));
163 assertEqualInt(ARCHIVE_OK, archive_read_free(a));
166 /* Now, damage the archive in various ways and test the responses. */
168 /* Damage the first size field in the pax attributes. */
169 memcpy(buff2, buff, buff_size);
172 buff2[514] = 'A'; /* Non-digit in size. */
173 assert((a = archive_read_new()) != NULL);
174 assertEqualIntA(a, ARCHIVE_OK, archive_read_support_format_all(a));
175 assertEqualIntA(a, ARCHIVE_OK, archive_read_support_filter_all(a));
176 assertEqualIntA(a, ARCHIVE_OK, archive_read_open_memory(a, buff2, used));
177 assertEqualIntA(a, ARCHIVE_WARN, archive_read_next_header(a, &ae));
178 assertEqualIntA(a, ARCHIVE_OK, archive_read_close(a));
179 assertEqualInt(ARCHIVE_OK, archive_read_free(a));
181 /* Damage the size field in the pax attributes. */
182 memcpy(buff2, buff, buff_size);
183 buff2[512] = 'A'; /* First character not a digit. */
184 assert((a = archive_read_new()) != NULL);
185 assertEqualIntA(a, ARCHIVE_OK, archive_read_support_format_all(a));
186 assertEqualIntA(a, ARCHIVE_OK, archive_read_support_filter_all(a));
187 assertEqualIntA(a, ARCHIVE_OK, archive_read_open_memory(a, buff2, used));
188 assertEqualIntA(a, ARCHIVE_WARN, archive_read_next_header(a, &ae));
189 assertEqualIntA(a, ARCHIVE_OK, archive_read_close(a));
190 assertEqualInt(ARCHIVE_OK, archive_read_free(a));
192 /* Damage the size field in the pax attributes. */
193 memcpy(buff2, buff, buff_size);
194 for (i = 512; i < 520; i++) /* Size over 999999. */
197 assert((a = archive_read_new()) != NULL);
198 assertEqualIntA(a, ARCHIVE_OK, archive_read_support_format_all(a));
199 assertEqualIntA(a, ARCHIVE_OK, archive_read_support_filter_all(a));
200 assertEqualIntA(a, ARCHIVE_OK, archive_read_open_memory(a, buff2, used));
201 assertEqualIntA(a, ARCHIVE_WARN, archive_read_next_header(a, &ae));
202 assertEqualIntA(a, ARCHIVE_OK, archive_read_close(a));
203 assertEqualInt(ARCHIVE_OK, archive_read_free(a));
205 /* Damage the size field in the pax attributes. */
206 memcpy(buff2, buff, buff_size);
207 buff2[512] = '9'; /* Valid format, but larger than attribute area. */
211 assert((a = archive_read_new()) != NULL);
212 assertEqualIntA(a, ARCHIVE_OK, archive_read_support_format_all(a));
213 assertEqualIntA(a, ARCHIVE_OK, archive_read_support_filter_all(a));
214 assertEqualIntA(a, ARCHIVE_OK, archive_read_open_memory(a, buff2, used));
215 assertEqualIntA(a, ARCHIVE_WARN, archive_read_next_header(a, &ae));
216 assertEqualIntA(a, ARCHIVE_OK, archive_read_close(a));
217 assertEqualInt(ARCHIVE_OK, archive_read_free(a));
219 /* Damage the size field in the pax attributes. */
220 memcpy(buff2, buff, buff_size);
221 buff2[512] = '1'; /* Too small. */
223 assert((a = archive_read_new()) != NULL);
224 assertEqualIntA(a, ARCHIVE_OK, archive_read_support_format_all(a));
225 assertEqualIntA(a, ARCHIVE_OK, archive_read_support_filter_all(a));
226 assertEqualIntA(a, ARCHIVE_OK, archive_read_open_memory(a, buff2, used));
227 assertEqualIntA(a, ARCHIVE_WARN, archive_read_next_header(a, &ae));
228 assertEqualIntA(a, ARCHIVE_OK, archive_read_close(a));
229 assertEqualInt(ARCHIVE_OK, archive_read_free(a));
231 /* Damage the size field in the pax attributes. */
232 memcpy(buff2, buff, buff_size);
233 buff2[512] = ' '; /* No size given. */
234 assert((a = archive_read_new()) != NULL);
235 assertEqualIntA(a, ARCHIVE_OK, archive_read_support_format_all(a));
236 assertEqualIntA(a, ARCHIVE_OK, archive_read_support_filter_all(a));
237 assertEqualIntA(a, ARCHIVE_OK, archive_read_open_memory(a, buff2, used));
238 assertEqualIntA(a, ARCHIVE_WARN, archive_read_next_header(a, &ae));
239 assertEqualIntA(a, ARCHIVE_OK, archive_read_close(a));
240 assertEqualInt(ARCHIVE_OK, archive_read_free(a));
242 /* Damage the ustar header. */
243 memcpy(buff2, buff, buff_size);
244 buff2[1024]++; /* Break the checksum. */
245 assert((a = archive_read_new()) != NULL);
246 assertEqualIntA(a, ARCHIVE_OK, archive_read_support_format_all(a));
247 assertEqualIntA(a, ARCHIVE_OK, archive_read_support_filter_all(a));
248 assertEqualIntA(a, ARCHIVE_OK, archive_read_open_memory(a, buff2, used));
249 assertEqualIntA(a, ARCHIVE_FATAL, archive_read_next_header(a, &ae));
250 assertEqualIntA(a, ARCHIVE_OK, archive_read_close(a));
251 assertEqualInt(ARCHIVE_OK, archive_read_free(a));
254 * TODO: Damage the ustar header in various ways and fixup the
255 * checksum in order to test boundary cases in the innermost
256 * ustar header parsing.