]> CyberLeo.Net >> Repos - FreeBSD/releng/7.2.git/blob - lib/libarchive/test/test_read_pax_truncated.c
Create releng/7.2 from stable/7 in preparation for 7.2-RELEASE.
[FreeBSD/releng/7.2.git] / lib / libarchive / test / test_read_pax_truncated.c
1 /*-
2  * Copyright (c) 2003-2007 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 __FBSDID("$FreeBSD$");
27
28 DEFINE_TEST(test_read_pax_truncated)
29 {
30         struct archive_entry *ae;
31         struct archive *a;
32         ssize_t used, i;
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);
38
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));
44
45         /*
46          * Write a file to it.
47          */
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));
60
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));
65 #else
66         archive_write_finish(a);
67 #endif
68
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));
76
77                 if (i < 1536) {
78                         assertEqualIntA(a, ARCHIVE_FATAL, archive_read_next_header(a, &ae));
79                         goto wrap_up;
80                 } else {
81                         failure("Archive truncated to %d bytes", i);
82                         assertEqualIntA(a, 0, archive_read_next_header(a, &ae));
83                 }
84
85                 if (i < 1536 + filedata_size) {
86                         assertA(ARCHIVE_FATAL == archive_read_data(a, filedata, filedata_size));
87                         goto wrap_up;
88                 } else {
89                         failure("Archive truncated to %d bytes", i);
90                         assertEqualIntA(a, filedata_size,
91                             archive_read_data(a, filedata, filedata_size));
92                 }
93
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
99                  * it.) */
100                 if (i < 1536 + 512*((filedata_size + 511)/512) + 512) {
101                         assertA(ARCHIVE_FATAL == archive_read_next_header(a, &ae));
102                 } else {
103                         assertA(ARCHIVE_EOF == archive_read_next_header(a, &ae));
104                 }
105         wrap_up:
106                 assert(0 == archive_read_close(a));
107 #if ARCHIVE_API_VERSION > 1
108                 assert(0 == archive_read_finish(a));
109 #else
110                 archive_read_finish(a);
111 #endif
112         }
113
114
115
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));
122
123                 if (i < 1536) {
124                         assertA(ARCHIVE_FATAL == archive_read_next_header(a, &ae));
125                         goto wrap_up2;
126                 } else {
127                         assertEqualIntA(a, 0, archive_read_next_header(a, &ae));
128                 }
129
130                 if (i < 1536 + 512*((filedata_size+511)/512)) {
131                         assertA(ARCHIVE_FATAL == archive_read_data_skip(a));
132                         goto wrap_up2;
133                 } else {
134                         assertA(ARCHIVE_OK == archive_read_data_skip(a));
135                 }
136
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
142                  * it.) */
143                 if (i < 1536 + 512*((filedata_size + 511)/512) + 512) {
144                         assertA(ARCHIVE_FATAL == archive_read_next_header(a, &ae));
145                 } else {
146                         assertA(ARCHIVE_EOF == archive_read_next_header(a, &ae));
147                 }
148         wrap_up2:
149                 assert(0 == archive_read_close(a));
150 #if ARCHIVE_API_VERSION > 1
151                 assert(0 == archive_read_finish(a));
152 #else
153                 archive_read_finish(a);
154 #endif
155         }
156
157         /* Now, damage the archive in various ways and test the responses. */
158
159         /* Damage the first size field in the pax attributes. */
160         memcpy(buff2, buff, buff_size);
161         buff2[512] = '9';
162         buff2[513] = '9';
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));
172 #else
173         archive_read_finish(a);
174 #endif
175
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));
187 #else
188         archive_read_finish(a);
189 #endif
190
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. */
194                 buff2[i] = '9';
195         buff2[i] = ' ';
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));
204 #else
205         archive_read_finish(a);
206 #endif
207
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. */
211         buff2[513] = '9';
212         buff2[514] = '9';
213         buff2[515] = ' ';
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));
222 #else
223         archive_read_finish(a);
224 #endif
225
226         /* Damage the size field in the pax attributes. */
227         memcpy(buff2, buff, buff_size);
228         buff2[512] = '1'; /* Too small. */
229         buff2[513] = ' ';
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));
238 #else
239         archive_read_finish(a);
240 #endif
241
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));
253 #else
254         archive_read_finish(a);
255 #endif
256
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));
268 #else
269         archive_read_finish(a);
270 #endif
271
272         /*
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.
276          */
277
278         free(buff);
279         free(buff2);
280         free(filedata);
281 }