1 /*-
2 * Copyright (c) 2008 Anselm Strauss
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
26 /*
27 * Development supported by Google Summer of Code 2008.
28 */
29
30 #include "test.h"
31
32 /* File data */
33 static const char file_name[] = "file";
34 static const char file_data1[] = {'1', '2', '3', '4', '5'};
35 static const char file_data2[] = {'6', '7', '8', '9', '0'};
36 static const int file_perm = 00644;
37 static const short file_uid = 10;
38 static const short file_gid = 20;
39
40 /* Folder data */
41 static const char folder_name[] = "folder/";
42 static const int folder_perm = 00755;
43 static const short folder_uid = 30;
44 static const short folder_gid = 40;
45
46 static time_t now;
47
verify_write_uncompressed(struct archive * a)48 static void verify_write_uncompressed(struct archive *a)
49 {
50 struct archive_entry *entry;
51
52 /* Write entries. */
53
54 /* Regular file */
55 assert((entry = archive_entry_new()) != NULL);
56 archive_entry_set_pathname(entry, file_name);
57 archive_entry_set_mode(entry, S_IFREG | 0644);
58 archive_entry_set_size(entry, sizeof(file_data1) + sizeof(file_data2));
59 archive_entry_set_uid(entry, file_uid);
60 archive_entry_set_gid(entry, file_gid);
61 archive_entry_set_mtime(entry, now, 0);
62 archive_entry_set_atime(entry, now + 3, 0);
63 assertEqualIntA(a, 0, archive_write_header(a, entry));
64 assertEqualIntA(a, sizeof(file_data1), archive_write_data(a, file_data1, sizeof(file_data1)));
65 assertEqualIntA(a, sizeof(file_data2), archive_write_data(a, file_data2, sizeof(file_data2)));
66 archive_entry_free(entry);
67
68 /* Folder */
69 assert((entry = archive_entry_new()) != NULL);
70 archive_entry_set_pathname(entry, folder_name);
71 archive_entry_set_mode(entry, S_IFDIR | folder_perm);
72 archive_entry_set_size(entry, 0);
73 archive_entry_set_uid(entry, folder_uid);
74 archive_entry_set_gid(entry, folder_gid);
75 archive_entry_set_mtime(entry, now, 0);
76 archive_entry_set_ctime(entry, now + 5, 0);
77 assertEqualIntA(a, 0, archive_write_header(a, entry));
78 archive_entry_free(entry);
79 }
80
verify_uncompressed_contents(const char * buff,size_t used)81 static void verify_uncompressed_contents(const char *buff, size_t used)
82 {
83 const char *buffend;
84
85 /* Misc variables */
86 unsigned long crc;
87 struct tm *tm;
88 #if defined(HAVE_LOCALTIME_R) || defined(HAVE_LOCALTIME_S)
89 struct tm tmbuf;
90 #endif
91 /* p is the pointer to walk over the central directory,
92 * q walks over the local headers, the data and the data descriptors. */
93 const char *p, *q, *local_header, *extra_start;
94
95 #if defined(HAVE_LOCALTIME_S)
96 tm = localtime_s(&tmbuf, &now) ? NULL : &tmbuf;
97 #elif defined(HAVE_LOCALTIME_R)
98 tm = localtime_r(&now, &tmbuf);
99 #else
100 tm = localtime(&now);
101 #endif
102
103 /* Remember the end of the archive in memory. */
104 buffend = buff + used;
105
106 /* Verify "End of Central Directory" record. */
107 /* Get address of end-of-central-directory record. */
108 p = buffend - 22; /* Assumes there is no zip comment field. */
109 failure("End-of-central-directory begins with PK\\005\\006 signature");
110 assertEqualMem(p, "PK\005\006", 4);
111 failure("This must be disk 0");
112 assertEqualInt(i2le(p + 4), 0);
113 failure("Central dir must start on disk 0");
114 assertEqualInt(i2le(p + 6), 0);
115 failure("All central dir entries are on this disk");
116 assertEqualInt(i2le(p + 8), i2le(p + 10));
117 failure("CD start (%u) + CD length (%u) should == archive size - 22",
118 i4le(p + 12), i4le(p + 16));
119 assertEqualInt(i4le(p + 12) + i4le(p + 16), used - 22);
120 failure("no zip comment");
121 assertEqualInt(i2le(p + 20), 0);
122
123 /* Get address of first entry in central directory. */
124 p = buff + i4le(buffend - 6);
125 failure("Central file record at offset %u should begin with"
126 " PK\\001\\002 signature",
127 i4le(buffend - 10));
128
129 /* Verify file entry in central directory. */
130 assertEqualMem(p, "PK\001\002", 4); /* Signature */
131 assertEqualInt(i2le(p + 4), 3 * 256 + 10); /* Version made by */
132 assertEqualInt(i2le(p + 6), 10); /* Version needed to extract */
133 assertEqualInt(i2le(p + 8), 8); /* Flags */
134 assertEqualInt(i2le(p + 10), 0); /* Compression method */
135 assertEqualInt(i2le(p + 12), (tm->tm_hour * 2048) + (tm->tm_min * 32) + (tm->tm_sec / 2)); /* File time */
136 assertEqualInt(i2le(p + 14), ((tm->tm_year - 80) * 512) + ((tm->tm_mon + 1) * 32) + tm->tm_mday); /* File date */
137 crc = bitcrc32(0, file_data1, sizeof(file_data1));
138 crc = bitcrc32(crc, file_data2, sizeof(file_data2));
139 assertEqualInt(i4le(p + 16), crc); /* CRC-32 */
140 assertEqualInt(i4le(p + 20), sizeof(file_data1) + sizeof(file_data2)); /* Compressed size */
141 assertEqualInt(i4le(p + 24), sizeof(file_data1) + sizeof(file_data2)); /* Uncompressed size */
142 assertEqualInt(i2le(p + 28), strlen(file_name)); /* Pathname length */
143 assertEqualInt(i2le(p + 30), 24); /* Extra field length */
144 assertEqualInt(i2le(p + 32), 0); /* File comment length */
145 assertEqualInt(i2le(p + 34), 0); /* Disk number start */
146 assertEqualInt(i2le(p + 36), 0); /* Internal file attrs */
147 assertEqualInt(i4le(p + 38) >> 16 & 01777, file_perm); /* External file attrs */
148 assertEqualInt(i4le(p + 42), 0); /* Offset of local header */
149 assertEqualMem(p + 46, file_name, strlen(file_name)); /* Pathname */
150 p = p + 46 + strlen(file_name);
151
152 assertEqualInt(i2le(p), 0x7875); /* 'ux' extension header */
153 assertEqualInt(i2le(p + 2), 11); /* 'ux' size */
154 /* TODO */
155 p = p + 4 + i2le(p + 2);
156
157 assertEqualInt(i2le(p), 0x5455); /* 'UT' extension header */
158 assertEqualInt(i2le(p + 2), 5); /* 'UT' size */
159 assertEqualInt(p[4], 1); /* 'UT' flags */
160 assertEqualInt(i4le(p + 5), now); /* 'UT' mtime */
161 p = p + 4 + i2le(p + 2);
162
163 /* Verify local header of file entry. */
164 local_header = q = buff;
165 assertEqualMem(q, "PK\003\004", 4); /* Signature */
166 assertEqualInt(i2le(q + 4), 10); /* Version needed to extract */
167 assertEqualInt(i2le(q + 6), 8); /* Flags: bit 3 = length-at-end. Required because CRC32 is unknown */
168 assertEqualInt(i2le(q + 8), 0); /* Compression method */
169 assertEqualInt(i2le(q + 10), (tm->tm_hour * 2048) + (tm->tm_min * 32) + (tm->tm_sec / 2)); /* File time */
170 assertEqualInt(i2le(q + 12), ((tm->tm_year - 80) * 512) + ((tm->tm_mon + 1) * 32) + tm->tm_mday); /* File date */
171 assertEqualInt(i4le(q + 14), 0); /* CRC-32 */
172 assertEqualInt(i4le(q + 18), 0); /* Compressed size, must be zero because of length-at-end */
173 assertEqualInt(i4le(q + 22), 0); /* Uncompressed size, must be zero because of length-at-end */
174 assertEqualInt(i2le(q + 26), strlen(file_name)); /* Pathname length */
175 assertEqualInt(i2le(q + 28), 41); /* Extra field length */
176 assertEqualMem(q + 30, file_name, strlen(file_name)); /* Pathname */
177 extra_start = q = q + 30 + strlen(file_name);
178
179 assertEqualInt(i2le(q), 0x7875); /* 'ux' extension header */
180 assertEqualInt(i2le(q + 2), 11); /* 'ux' size */
181 assertEqualInt(q[4], 1); /* 'ux' version */
182 assertEqualInt(q[5], 4); /* 'ux' uid size */
183 assertEqualInt(i4le(q + 6), file_uid); /* 'Ux' UID */
184 assertEqualInt(q[10], 4); /* 'ux' gid size */
185 assertEqualInt(i4le(q + 11), file_gid); /* 'Ux' GID */
186 q = q + 4 + i2le(q + 2);
187
188 assertEqualInt(i2le(q), 0x5455); /* 'UT' extension header */
189 assertEqualInt(i2le(q + 2), 9); /* 'UT' size */
190 assertEqualInt(q[4], 3); /* 'UT' flags */
191 assertEqualInt(i4le(q + 5), now); /* 'UT' mtime */
192 assertEqualInt(i4le(q + 9), now + 3); /* 'UT' atime */
193 q = q + 4 + i2le(q + 2);
194
195 assertEqualInt(i2le(q), 0x6c78); /* 'xl' experimental extension header */
196 assertEqualInt(i2le(q + 2), 9); /* size */
197 assertEqualInt(q[4], 7); /* Bitmap of fields included. */
198 assertEqualInt(i2le(q + 5) >> 8, 3); /* system & version made by */
199 assertEqualInt(i2le(q + 7), 0); /* internal file attributes */
200 assertEqualInt(i4le(q + 9) >> 16 & 01777, file_perm); /* external file attributes */
201 q = q + 4 + i2le(q + 2);
202
203 assert(q == extra_start + i2le(local_header + 28));
204 q = extra_start + i2le(local_header + 28);
205
206 /* Verify data of file entry. */
207 assertEqualMem(q, file_data1, sizeof(file_data1));
208 assertEqualMem(q + sizeof(file_data1), file_data2, sizeof(file_data2));
209 q = q + sizeof(file_data1) + sizeof(file_data2);
210
211 /* Verify data descriptor of file entry. */
212 assertEqualMem(q, "PK\007\010", 4); /* Signature */
213 assertEqualInt(i4le(q + 4), crc); /* CRC-32 */
214 assertEqualInt(i4le(q + 8), sizeof(file_data1) + sizeof(file_data2)); /* Compressed size */
215 assertEqualInt(i4le(q + 12), sizeof(file_data1) + sizeof(file_data2)); /* Uncompressed size */
216 q = q + 16;
217
218 /* Verify folder entry in central directory. */
219 assertEqualMem(p, "PK\001\002", 4); /* Signature */
220 assertEqualInt(i2le(p + 4), 3 * 256 + 20); /* Version made by */
221 assertEqualInt(i2le(p + 6), 20); /* Version needed to extract */
222 assertEqualInt(i2le(p + 8), 0); /* Flags */
223 assertEqualInt(i2le(p + 10), 0); /* Compression method */
224 assertEqualInt(i2le(p + 12), (tm->tm_hour * 2048) + (tm->tm_min * 32) + (tm->tm_sec / 2)); /* File time */
225 assertEqualInt(i2le(p + 14), ((tm->tm_year - 80) * 512) + ((tm->tm_mon + 1) * 32) + tm->tm_mday); /* File date */
226 crc = 0;
227 assertEqualInt(i4le(p + 16), crc); /* CRC-32 */
228 assertEqualInt(i4le(p + 20), 0); /* Compressed size */
229 assertEqualInt(i4le(p + 24), 0); /* Uncompressed size */
230 assertEqualInt(i2le(p + 28), strlen(folder_name)); /* Pathname length */
231 assertEqualInt(i2le(p + 30), 24); /* Extra field length */
232 assertEqualInt(i2le(p + 32), 0); /* File comment length */
233 assertEqualInt(i2le(p + 34), 0); /* Disk number start */
234 assertEqualInt(i2le(p + 36), 0); /* Internal file attrs */
235 assertEqualInt(i4le(p + 38) >> 16 & 01777, folder_perm); /* External file attrs */
236 assertEqualInt(i4le(p + 42), q - buff); /* Offset of local header */
237 assertEqualMem(p + 46, folder_name, strlen(folder_name)); /* Pathname */
238 p = p + 46 + strlen(folder_name);
239
240 assertEqualInt(i2le(p), 0x7875); /* 'ux' extension header */
241 assertEqualInt(i2le(p + 2), 11); /* 'ux' size */
242 assertEqualInt(p[4], 1); /* 'ux' version */
243 assertEqualInt(p[5], 4); /* 'ux' uid size */
244 assertEqualInt(i4le(p + 6), folder_uid); /* 'ux' UID */
245 assertEqualInt(p[10], 4); /* 'ux' gid size */
246 assertEqualInt(i4le(p + 11), folder_gid); /* 'ux' GID */
247 p = p + 4 + i2le(p + 2);
248
249 assertEqualInt(i2le(p), 0x5455); /* 'UT' extension header */
250 assertEqualInt(i2le(p + 2), 5); /* 'UT' size */
251 assertEqualInt(p[4], 1); /* 'UT' flags */
252 assertEqualInt(i4le(p + 5), now); /* 'UT' mtime */
253 p = p + 4 + i2le(p + 2);
254
255 /* Verify local header of folder entry. */
256 local_header = q;
257 assertEqualMem(q, "PK\003\004", 4); /* Signature */
258 assertEqualInt(i2le(q + 4), 20); /* Version needed to extract */
259 assertEqualInt(i2le(q + 6), 0); /* Flags */
260 assertEqualInt(i2le(q + 8), 0); /* Compression method */
261 assertEqualInt(i2le(q + 10), (tm->tm_hour * 2048) + (tm->tm_min * 32) + (tm->tm_sec / 2)); /* File time */
262 assertEqualInt(i2le(q + 12), ((tm->tm_year - 80) * 512) + ((tm->tm_mon + 1) * 32) + tm->tm_mday); /* File date */
263 assertEqualInt(i4le(q + 14), 0); /* CRC-32 */
264 assertEqualInt(i4le(q + 18), 0); /* Compressed size */
265 assertEqualInt(i4le(q + 22), 0); /* Uncompressed size */
266 assertEqualInt(i2le(q + 26), strlen(folder_name)); /* Pathname length */
267 assertEqualInt(i2le(q + 28), 41); /* Extra field length */
268 assertEqualMem(q + 30, folder_name, strlen(folder_name)); /* Pathname */
269 extra_start = q = q + 30 + strlen(folder_name);
270
271 assertEqualInt(i2le(q), 0x7875); /* 'ux' extension header */
272 assertEqualInt(i2le(q + 2), 11); /* 'ux' size */
273 assertEqualInt(q[4], 1); /* 'ux' version */
274 assertEqualInt(q[5], 4); /* 'ux' uid size */
275 assertEqualInt(i4le(q + 6), folder_uid); /* 'ux' UID */
276 assertEqualInt(q[10], 4); /* 'ux' gid size */
277 assertEqualInt(i4le(q + 11), folder_gid); /* 'ux' GID */
278 q = q + 4 + i2le(q + 2);
279
280 assertEqualInt(i2le(q), 0x5455); /* 'UT' extension header */
281 assertEqualInt(i2le(q + 2), 9); /* 'UT' size */
282 assertEqualInt(q[4], 5); /* 'UT' flags */
283 assertEqualInt(i4le(q + 5), now); /* 'UT' mtime */
284 assertEqualInt(i4le(q + 9), now + 5); /* 'UT' atime */
285 q = q + 4 + i2le(q + 2);
286
287 assertEqualInt(i2le(q), 0x6c78); /* 'xl' experimental extension header */
288 assertEqualInt(i2le(q + 2), 9); /* size */
289 assertEqualInt(q[4], 7); /* bitmap of fields */
290 assertEqualInt(i2le(q + 5) >> 8, 3); /* system & version made by */
291 assertEqualInt(i2le(q + 7), 0); /* internal file attributes */
292 assertEqualInt(i4le(q + 9) >> 16 & 01777, folder_perm); /* external file attributes */
293 q = q + 4 + i2le(q + 2);
294
295 assert(q == extra_start + i2le(local_header + 28));
296 q = extra_start + i2le(local_header + 28);
297
298 /* There should not be any data in the folder entry,
299 * so the first central directory entry should be next: */
300 assertEqualMem(q, "PK\001\002", 4); /* Signature */
301 }
302
DEFINE_TEST(test_write_format_zip_compression_store)303 DEFINE_TEST(test_write_format_zip_compression_store)
304 {
305 /* Buffer data */
306 struct archive *a;
307 char buff[100000];
308 size_t used;
309
310 /* Time data */
311 now = time(NULL);
312
313 /* Create new ZIP archive in memory without padding. */
314 /* Use compression=store to disable compression. */
315 assert((a = archive_write_new()) != NULL);
316 assertEqualIntA(a, ARCHIVE_OK, archive_write_set_format_zip(a));
317 assertEqualIntA(a, ARCHIVE_OK,
318 archive_write_set_options(a, "zip:compression=store"));
319 assertEqualIntA(a, ARCHIVE_OK,
320 archive_write_set_options(a, "zip:experimental"));
321 assertEqualIntA(a, ARCHIVE_OK, archive_write_add_filter_none(a));
322 assertEqualIntA(a, ARCHIVE_OK, archive_write_set_bytes_per_block(a, 1));
323 assertEqualIntA(a, ARCHIVE_OK, archive_write_set_bytes_in_last_block(a, 1));
324 assertEqualIntA(a, ARCHIVE_OK, archive_write_open_memory(a, buff, sizeof(buff), &used));
325
326 verify_write_uncompressed(a);
327
328 /* Close the archive . */
329 assertEqualIntA(a, ARCHIVE_OK, archive_write_close(a));
330 assertEqualInt(ARCHIVE_OK, archive_write_free(a));
331 dumpfile("constructed.zip", buff, used);
332
333 verify_uncompressed_contents(buff, used);
334
335 /* Create new ZIP archive in memory without padding. */
336 /* Use compression-level=0 to disable compression. */
337 assert((a = archive_write_new()) != NULL);
338 assertEqualIntA(a, ARCHIVE_OK, archive_write_set_format_zip(a));
339 assertEqualIntA(a, ARCHIVE_OK,
340 archive_write_set_options(a, "zip:compression-level=0"));
341 assertEqualIntA(a, ARCHIVE_OK,
342 archive_write_set_options(a, "zip:experimental"));
343 assertEqualIntA(a, ARCHIVE_OK, archive_write_add_filter_none(a));
344 assertEqualIntA(a, ARCHIVE_OK, archive_write_set_bytes_per_block(a, 1));
345 assertEqualIntA(a, ARCHIVE_OK, archive_write_set_bytes_in_last_block(a, 1));
346 assertEqualIntA(a, ARCHIVE_OK, archive_write_open_memory(a, buff, sizeof(buff), &used));
347
348 verify_write_uncompressed(a);
349
350 /* Close the archive . */
351 assertEqualIntA(a, ARCHIVE_OK, archive_write_close(a));
352 assertEqualInt(ARCHIVE_OK, archive_write_free(a));
353 dumpfile("constructed.zip", buff, used);
354
355 verify_uncompressed_contents(buff, used);
356
357 }
358