xref: /freebsd/contrib/libarchive/libarchive/test/test_write_format_zip.c (revision b8a496dfb6df7b86e014d0d4476cd75850e060c1)
1 /*-
2  * Copyright (c) 2003-2008 Tim Kientzle
3  * Copyright (c) 2008 Anselm Strauss
4  * Copyright (c) 2014 Michihiro NAKAJIMA
5  * All rights reserved.
6  *
7  * Redistribution and use in source and binary forms, with or without
8  * modification, are permitted provided that the following conditions
9  * are met:
10  * 1. Redistributions of source code must retain the above copyright
11  *    notice, this list of conditions and the following disclaimer.
12  * 2. Redistributions in binary form must reproduce the above copyright
13  *    notice, this list of conditions and the following disclaimer in the
14  *    documentation and/or other materials provided with the distribution.
15  *
16  * THIS SOFTWARE IS PROVIDED BY THE AUTHOR(S) ``AS IS'' AND ANY EXPRESS OR
17  * IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE IMPLIED WARRANTIES
18  * OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE ARE DISCLAIMED.
19  * IN NO EVENT SHALL THE AUTHOR(S) BE LIABLE FOR ANY DIRECT, INDIRECT,
20  * INCIDENTAL, SPECIAL, EXEMPLARY, OR CONSEQUENTIAL DAMAGES (INCLUDING, BUT
21  * NOT LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS OR SERVICES; LOSS OF USE,
22  * DATA, OR PROFITS; OR BUSINESS INTERRUPTION) HOWEVER CAUSED AND ON ANY
23  * THEORY OF LIABILITY, WHETHER IN CONTRACT, STRICT LIABILITY, OR TORT
24  * (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY OUT OF THE USE OF
25  * THIS SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF SUCH DAMAGE.
26  */
27 
28 /*
29  * Development supported by Google Summer of Code 2008.
30  */
31 
32 #include "test.h"
33 
34 /*
35  * This test doesn't actually check that the zip writer is
36  * correct, just that our zip reader can read the output of
37  * our zip writer.  We do more detailed checks of the bits
38  * elsewhere.
39  */
40 
41 /*
42  * Write a variety of different file types into the archive.
43  */
44 static void
45 write_contents(struct archive *a)
46 {
47 	struct archive_entry *ae;
48 
49 	/*
50 	 * First write things with the "default" compression.
51 	 * The library will choose "deflate" for most things if it's
52 	 * available, else "store".
53 	 */
54 
55 	/*
56 	 * Write a file to it.
57 	 */
58 	assert((ae = archive_entry_new()) != NULL);
59 	archive_entry_set_mtime(ae, 1, 10);
60 	archive_entry_copy_pathname(ae, "file");
61 	archive_entry_set_mode(ae, AE_IFREG | 0755);
62 	archive_entry_set_size(ae, 8);
63 	assertEqualInt(0, archive_write_header(a, ae));
64 	archive_entry_free(ae);
65 	assertEqualInt(8, archive_write_data(a, "12345678", 9));
66 	assertEqualInt(0, archive_write_data(a, "1", 1));
67 
68 	/*
69 	 * Write another file to it.
70 	 */
71 	assert((ae = archive_entry_new()) != NULL);
72 	archive_entry_set_mtime(ae, 1, 10);
73 	archive_entry_copy_pathname(ae, "file2");
74 	archive_entry_set_mode(ae, AE_IFREG | 0755);
75 	archive_entry_set_size(ae, 4);
76 	assertEqualInt(ARCHIVE_OK, archive_write_header(a, ae));
77 	archive_entry_free(ae);
78 	assertEqualInt(4, archive_write_data(a, "1234", 4));
79 
80 	/*
81 	 * Write a file with an unknown size.
82 	 */
83 	assert((ae = archive_entry_new()) != NULL);
84 	archive_entry_set_mtime(ae, 2, 15);
85 	archive_entry_copy_pathname(ae, "file3");
86 	archive_entry_set_mode(ae, AE_IFREG | 0621);
87 	archive_entry_unset_size(ae);
88 	assertEqualInt(ARCHIVE_OK, archive_write_header(a, ae));
89 	archive_entry_free(ae);
90 	assertEqualInt(5, archive_write_data(a, "mnopq", 5));
91 
92 	/*
93 	 * Write symbolic link.
94 	 */
95 	assert((ae = archive_entry_new()) != NULL);
96 	archive_entry_set_mtime(ae, 1, 10);
97 	assertEqualInt(1, archive_entry_mtime(ae));
98 	assertEqualInt(10, archive_entry_mtime_nsec(ae));
99 	archive_entry_copy_pathname(ae, "symlink");
100 	assertEqualString("symlink", archive_entry_pathname(ae));
101 	archive_entry_copy_symlink(ae, "file1");
102 	assertEqualString("file1", archive_entry_symlink(ae));
103 	archive_entry_set_mode(ae, AE_IFLNK | 0755);
104 	assertEqualInt((AE_IFLNK | 0755), archive_entry_mode(ae));
105 	archive_entry_set_size(ae, 4);
106 
107 	assertEqualInt(ARCHIVE_OK, archive_write_header(a, ae));
108 	archive_entry_free(ae);
109 
110 	/*
111 	 * Write a directory to it.
112 	 */
113 	assert((ae = archive_entry_new()) != NULL);
114 	archive_entry_set_mtime(ae, 11, 110);
115 	archive_entry_copy_pathname(ae, "dir");
116 	archive_entry_set_mode(ae, S_IFDIR | 0755);
117 	archive_entry_set_size(ae, 512);
118 	assertEqualIntA(a, ARCHIVE_OK, archive_write_header(a, ae));
119 	failure("size should be zero so that applications know not to write");
120 	assertEqualInt(0, archive_entry_size(ae));
121 	archive_entry_free(ae);
122 	assertEqualIntA(a, 0, archive_write_data(a, "12345678", 9));
123 
124 	/*
125 	 * Force "deflate" compression if the platform supports it.
126 	 */
127 #ifdef HAVE_ZLIB_H
128 	assertEqualIntA(a, ARCHIVE_OK, archive_write_zip_set_compression_deflate(a));
129 
130 	/*
131 	 * Write a file to it.
132 	 */
133 	assert((ae = archive_entry_new()) != NULL);
134 	archive_entry_set_mtime(ae, 1, 10);
135 	archive_entry_copy_pathname(ae, "file_deflate");
136 	archive_entry_set_mode(ae, AE_IFREG | 0755);
137 	archive_entry_set_size(ae, 8);
138 	assertEqualIntA(a, ARCHIVE_OK, archive_write_header(a, ae));
139 	archive_entry_free(ae);
140 	assertEqualInt(8, archive_write_data(a, "12345678", 9));
141 	assertEqualInt(0, archive_write_data(a, "1", 1));
142 
143 	/*
144 	 * Write another file to it.
145 	 */
146 	assert((ae = archive_entry_new()) != NULL);
147 	archive_entry_set_mtime(ae, 1, 10);
148 	archive_entry_copy_pathname(ae, "file2_deflate");
149 	archive_entry_set_mode(ae, AE_IFREG | 0755);
150 	archive_entry_set_size(ae, 4);
151 	assertEqualIntA(a, ARCHIVE_OK, archive_write_header(a, ae));
152 	archive_entry_free(ae);
153 	assertEqualInt(4, archive_write_data(a, "1234", 4));
154 
155 	/*
156 	 * Write a file with an unknown size.
157 	 */
158 	assert((ae = archive_entry_new()) != NULL);
159 	archive_entry_set_mtime(ae, 2, 15);
160 	archive_entry_copy_pathname(ae, "file3_deflate");
161 	archive_entry_set_mode(ae, AE_IFREG | 0621);
162 	archive_entry_unset_size(ae);
163 	assertEqualInt(ARCHIVE_OK, archive_write_header(a, ae));
164 	archive_entry_free(ae);
165 	assertEqualInt(5, archive_write_data(a, "ghijk", 5));
166 
167 	/*
168 	 * Write symbolic like file to it.
169 	 */
170 	assert((ae = archive_entry_new()) != NULL);
171 	archive_entry_set_mtime(ae, 1, 10);
172 	archive_entry_copy_pathname(ae, "symlink_deflate");
173 	archive_entry_copy_symlink(ae, "file1");
174 	archive_entry_set_mode(ae, AE_IFLNK | 0755);
175 	archive_entry_set_size(ae, 4);
176 	assertEqualInt(ARCHIVE_OK, archive_write_header(a, ae));
177 	archive_entry_free(ae);
178 
179 	/*
180 	 * Write a directory to it.
181 	 */
182 	assert((ae = archive_entry_new()) != NULL);
183 	archive_entry_set_mtime(ae, 11, 110);
184 	archive_entry_copy_pathname(ae, "dir_deflate");
185 	archive_entry_set_mode(ae, S_IFDIR | 0755);
186 	archive_entry_set_size(ae, 512);
187 	assertEqualIntA(a, ARCHIVE_OK, archive_write_header(a, ae));
188 	failure("size should be zero so that applications know not to write");
189 	assertEqualInt(0, archive_entry_size(ae));
190 	archive_entry_free(ae);
191 	assertEqualIntA(a, 0, archive_write_data(a, "12345678", 9));
192 	assertEqualIntA(a, ARCHIVE_OK, archive_write_finish_entry(a));
193 #endif
194 
195 	/*
196 	 * Now write a bunch of entries with "store" compression.
197 	 */
198 	assertEqualIntA(a, ARCHIVE_OK, archive_write_zip_set_compression_store(a));
199 
200 	/*
201 	 * Write a file to it.
202 	 */
203 	assert((ae = archive_entry_new()) != NULL);
204 	archive_entry_set_mtime(ae, 1, 10);
205 	archive_entry_copy_pathname(ae, "file_stored");
206 	archive_entry_set_mode(ae, AE_IFREG | 0755);
207 	archive_entry_set_size(ae, 8);
208 	assertEqualInt(0, archive_write_header(a, ae));
209 	archive_entry_free(ae);
210 	assertEqualInt(8, archive_write_data(a, "12345678", 9));
211 	assertEqualInt(0, archive_write_data(a, "1", 1));
212 
213 	/*
214 	 * Write another file to it.
215 	 */
216 	assert((ae = archive_entry_new()) != NULL);
217 	archive_entry_set_mtime(ae, 1, 10);
218 	archive_entry_copy_pathname(ae, "file2_stored");
219 	archive_entry_set_mode(ae, AE_IFREG | 0755);
220 	archive_entry_set_size(ae, 4);
221 	assertEqualInt(ARCHIVE_OK, archive_write_header(a, ae));
222 	archive_entry_free(ae);
223 	assertEqualInt(4, archive_write_data(a, "ACEG", 4));
224 
225 	/*
226 	 * Write a file with an unknown size.
227 	 */
228 	assert((ae = archive_entry_new()) != NULL);
229 	archive_entry_set_mtime(ae, 2, 15);
230 	archive_entry_copy_pathname(ae, "file3_stored");
231 	archive_entry_set_mode(ae, AE_IFREG | 0621);
232 	archive_entry_unset_size(ae);
233 	assertEqualInt(ARCHIVE_OK, archive_write_header(a, ae));
234 	archive_entry_free(ae);
235 	assertEqualInt(5, archive_write_data(a, "ijklm", 5));
236 
237 	/*
238 	 * Write symbolic like file to it.
239 	 */
240 	assert((ae = archive_entry_new()) != NULL);
241 	archive_entry_set_mtime(ae, 1, 10);
242 	archive_entry_copy_pathname(ae, "symlink_stored");
243 	archive_entry_copy_symlink(ae, "file1");
244 	archive_entry_set_mode(ae, AE_IFLNK | 0755);
245 	archive_entry_set_size(ae, 4);
246 	assertEqualInt(ARCHIVE_OK, archive_write_header(a, ae));
247 	archive_entry_free(ae);
248 
249 	/*
250 	 * Write a directory to it.
251 	 */
252 	assert((ae = archive_entry_new()) != NULL);
253 	archive_entry_set_mtime(ae, 11, 110);
254 	archive_entry_copy_pathname(ae, "dir_stored");
255 	archive_entry_set_mode(ae, S_IFDIR | 0755);
256 	archive_entry_set_size(ae, 512);
257 	assertEqualIntA(a, ARCHIVE_OK, archive_write_header(a, ae));
258 	failure("size should be zero so that applications know not to write");
259 	assertEqualInt(0, archive_entry_size(ae));
260 	archive_entry_free(ae);
261 	assertEqualIntA(a, 0, archive_write_data(a, "12345678", 9));
262 
263 
264 	/* Close out the archive. */
265 	assertEqualInt(ARCHIVE_OK, archive_write_close(a));
266 	assertEqualInt(ARCHIVE_OK, archive_write_free(a));
267 }
268 
269 /*
270  * Read back all of the entries and verify their values.
271  */
272 static void
273 verify_contents(struct archive *a, int seeking, int content)
274 {
275 	char filedata[64];
276 	struct archive_entry *ae;
277 
278 	/*
279 	 * Default compression options:
280 	 */
281 
282 	/* Read and verify first file. */
283 	assertEqualIntA(a, ARCHIVE_OK, archive_read_next_header(a, &ae));
284 	assertEqualInt(1, archive_entry_mtime(ae));
285 	/* Zip doesn't store high-resolution mtime. */
286 	assertEqualInt(0, archive_entry_mtime_nsec(ae));
287 	assertEqualInt(0, archive_entry_atime(ae));
288 	assertEqualInt(0, archive_entry_ctime(ae));
289 	assertEqualString("file", archive_entry_pathname(ae));
290 	if (seeking) {
291 		assertEqualInt(AE_IFREG | 0755, archive_entry_mode(ae));
292 		assert(archive_entry_size_is_set(ae));
293 		assertEqualInt(8, archive_entry_size(ae));
294 	} else {
295 		assertEqualInt(0, archive_entry_size_is_set(ae));
296 	}
297 	if (content) {
298 		assertEqualIntA(a, 8,
299 		    archive_read_data(a, filedata, sizeof(filedata)));
300 		assertEqualMem(filedata, "12345678", 8);
301 	}
302 
303 	/* Read the second file back. */
304 	assertEqualIntA(a, ARCHIVE_OK, archive_read_next_header(a, &ae));
305 	assertEqualInt(1, archive_entry_mtime(ae));
306 	assertEqualInt(0, archive_entry_mtime_nsec(ae));
307 	assertEqualInt(0, archive_entry_atime(ae));
308 	assertEqualInt(0, archive_entry_ctime(ae));
309 	assertEqualString("file2", archive_entry_pathname(ae));
310 	if (seeking) {
311 		assertEqualInt(AE_IFREG | 0755, archive_entry_mode(ae));
312 		assertEqualInt(4, archive_entry_size(ae));
313 		assert(archive_entry_size_is_set(ae));
314 	} else {
315 		assertEqualInt(0, archive_entry_size_is_set(ae));
316 	}
317 	if (content) {
318 		assertEqualIntA(a, 4,
319 		    archive_read_data(a, filedata, sizeof(filedata)));
320 		assertEqualMem(filedata, "1234", 4);
321 	}
322 
323 	/* Read the third file back. */
324 	assertEqualIntA(a, ARCHIVE_OK, archive_read_next_header(a, &ae));
325 	assertEqualInt(2, archive_entry_mtime(ae));
326 	assertEqualInt(0, archive_entry_mtime_nsec(ae));
327 	assertEqualInt(0, archive_entry_atime(ae));
328 	assertEqualInt(0, archive_entry_ctime(ae));
329 	assertEqualString("file3", archive_entry_pathname(ae));
330 	if (seeking) {
331 		assertEqualInt(5, archive_entry_size(ae));
332 		assertEqualInt(AE_IFREG | 0621, archive_entry_mode(ae));
333 	} else {
334 		assertEqualInt(0, archive_entry_size_is_set(ae));
335 	}
336 	if (content) {
337 		assertEqualIntA(a, 5,
338 		    archive_read_data(a, filedata, sizeof(filedata)));
339 		assertEqualMem(filedata, "mnopq", 5);
340 	}
341 
342 	/* Read symlink. */
343 	assertEqualIntA(a, ARCHIVE_OK, archive_read_next_header(a, &ae));
344 	assertEqualInt(1, archive_entry_mtime(ae));
345 	assertEqualInt(0, archive_entry_mtime_nsec(ae));
346 	assertEqualInt(0, archive_entry_atime(ae));
347 	assertEqualInt(0, archive_entry_ctime(ae));
348 	assertEqualString("symlink", archive_entry_pathname(ae));
349 	assertEqualInt(AE_IFLNK | 0755, archive_entry_mode(ae));
350 	assertEqualInt(0, archive_entry_size(ae));
351 	assertEqualString("file1", archive_entry_symlink(ae));
352 
353 	/* Read the dir entry back. */
354 	assertEqualIntA(a, ARCHIVE_OK, archive_read_next_header(a, &ae));
355 	assertEqualInt(11, archive_entry_mtime(ae));
356 	assertEqualInt(0, archive_entry_mtime_nsec(ae));
357 	assertEqualInt(0, archive_entry_atime(ae));
358 	assertEqualInt(0, archive_entry_ctime(ae));
359 	assertEqualString("dir/", archive_entry_pathname(ae));
360 	if (seeking)
361 		assertEqualInt(AE_IFDIR | 0755, archive_entry_mode(ae));
362 	assertEqualInt(0, archive_entry_size(ae));
363 	if (content) {
364 		assertEqualIntA(a, 0, archive_read_data(a, filedata, 10));
365 	}
366 
367 #ifdef HAVE_ZLIB_H
368 	/*
369 	 * Deflate compression option:
370 	 */
371 
372 	/* Read and verify first file. */
373 	assertEqualIntA(a, ARCHIVE_OK, archive_read_next_header(a, &ae));
374 	assertEqualInt(1, archive_entry_mtime(ae));
375 	/* Zip doesn't store high-resolution mtime. */
376 	assertEqualInt(0, archive_entry_mtime_nsec(ae));
377 	assertEqualInt(0, archive_entry_atime(ae));
378 	assertEqualInt(0, archive_entry_ctime(ae));
379 	assertEqualString("file_deflate", archive_entry_pathname(ae));
380 	if (seeking) {
381 		assertEqualInt(AE_IFREG | 0755, archive_entry_mode(ae));
382 		assertEqualInt(8, archive_entry_size(ae));
383 		assert(archive_entry_size_is_set(ae));
384 	} else {
385 		assertEqualInt(0, archive_entry_size_is_set(ae));
386 	}
387 	if (content) {
388 		assertEqualIntA(a, 8,
389 		    archive_read_data(a, filedata, sizeof(filedata)));
390 		assertEqualMem(filedata, "12345678", 8);
391 	}
392 
393 
394 	/* Read the second file back. */
395 	assertEqualIntA(a, ARCHIVE_OK, archive_read_next_header(a, &ae));
396 	assertEqualInt(1, archive_entry_mtime(ae));
397 	assertEqualInt(0, archive_entry_mtime_nsec(ae));
398 	assertEqualInt(0, archive_entry_atime(ae));
399 	assertEqualInt(0, archive_entry_ctime(ae));
400 	assertEqualString("file2_deflate", archive_entry_pathname(ae));
401 	if (seeking) {
402 		assertEqualInt(AE_IFREG | 0755, archive_entry_mode(ae));
403 		assertEqualInt(4, archive_entry_size(ae));
404 		assert(archive_entry_size_is_set(ae));
405 	} else {
406 		assertEqualInt(0, archive_entry_size_is_set(ae));
407 	}
408 	if (content) {
409 		assertEqualIntA(a, 4,
410 		    archive_read_data(a, filedata, sizeof(filedata)));
411 		assertEqualMem(filedata, "1234", 4);
412 	}
413 
414 	/* Read the third file back. */
415 	assertEqualIntA(a, ARCHIVE_OK, archive_read_next_header(a, &ae));
416 	assertEqualInt(2, archive_entry_mtime(ae));
417 	assertEqualInt(0, archive_entry_mtime_nsec(ae));
418 	assertEqualInt(0, archive_entry_atime(ae));
419 	assertEqualInt(0, archive_entry_ctime(ae));
420 	assertEqualString("file3_deflate", archive_entry_pathname(ae));
421 	if (seeking) {
422 		assert(archive_entry_size_is_set(ae));
423 		assertEqualInt(5, archive_entry_size(ae));
424 		assertEqualInt(AE_IFREG | 0621, archive_entry_mode(ae));
425 	} else {
426 		assertEqualInt(0, archive_entry_size_is_set(ae));
427 	}
428 	if (content) {
429 		assertEqualIntA(a, 5,
430 		    archive_read_data(a, filedata, sizeof(filedata)));
431 		assertEqualMem(filedata, "ghijk", 4);
432 	}
433 
434 	/* Read symlink. */
435 	assertEqualIntA(a, ARCHIVE_OK, archive_read_next_header(a, &ae));
436 	assertEqualInt(1, archive_entry_mtime(ae));
437 	assertEqualInt(0, archive_entry_mtime_nsec(ae));
438 	assertEqualInt(0, archive_entry_atime(ae));
439 	assertEqualInt(0, archive_entry_ctime(ae));
440 	assertEqualString("symlink_deflate", archive_entry_pathname(ae));
441 	assertEqualInt(AE_IFLNK | 0755, archive_entry_mode(ae));
442 	assertEqualInt(0, archive_entry_size(ae));
443 	assertEqualString("file1", archive_entry_symlink(ae));
444 
445 	/* Read the dir entry back. */
446 	assertEqualIntA(a, ARCHIVE_OK, archive_read_next_header(a, &ae));
447 	assertEqualInt(11, archive_entry_mtime(ae));
448 	assertEqualInt(0, archive_entry_mtime_nsec(ae));
449 	assertEqualInt(0, archive_entry_atime(ae));
450 	assertEqualInt(0, archive_entry_ctime(ae));
451 	assertEqualString("dir_deflate/", archive_entry_pathname(ae));
452 	if (seeking) {
453 		assertEqualInt(AE_IFDIR | 0755, archive_entry_mode(ae));
454 	}
455 	assertEqualInt(0, archive_entry_size(ae));
456 	if (content) {
457 		assertEqualIntA(a, 0, archive_read_data(a, filedata, 10));
458 	}
459 #endif
460 
461 	/*
462 	 * Store compression option:
463 	 */
464 
465 	/* Read and verify first file. */
466 	assertEqualIntA(a, ARCHIVE_OK, archive_read_next_header(a, &ae));
467 	assertEqualInt(1, archive_entry_mtime(ae));
468 	/* Zip doesn't store high-resolution mtime. */
469 	assertEqualInt(0, archive_entry_mtime_nsec(ae));
470 	assertEqualInt(0, archive_entry_atime(ae));
471 	assertEqualInt(0, archive_entry_ctime(ae));
472 	assertEqualString("file_stored", archive_entry_pathname(ae));
473 	if (seeking) {
474 		assertEqualInt(AE_IFREG | 0755, archive_entry_mode(ae));
475 		assert(archive_entry_size_is_set(ae));
476 		assertEqualInt(8, archive_entry_size(ae));
477 	} else {
478 		assertEqualInt(0, archive_entry_size_is_set(ae));
479 	}
480 	if (content) {
481 		assertEqualIntA(a, 8,
482 		    archive_read_data(a, filedata, sizeof(filedata)));
483 		assertEqualMem(filedata, "12345678", 8);
484 	}
485 
486 
487 	/* Read the second file back. */
488 	assertEqualIntA(a, ARCHIVE_OK, archive_read_next_header(a, &ae));
489 	assertEqualInt(1, archive_entry_mtime(ae));
490 	assertEqualInt(0, archive_entry_mtime_nsec(ae));
491 	assertEqualInt(0, archive_entry_atime(ae));
492 	assertEqualInt(0, archive_entry_ctime(ae));
493 	assertEqualString("file2_stored", archive_entry_pathname(ae));
494 	if (seeking) {
495 		assertEqualInt(AE_IFREG | 0755, archive_entry_mode(ae));
496 		assertEqualInt(4, archive_entry_size(ae));
497 		assert(archive_entry_size_is_set(ae));
498 	} else {
499 		assertEqualInt(0, archive_entry_size_is_set(ae));
500 	}
501 	if (content) {
502 		assertEqualIntA(a, 4,
503 		    archive_read_data(a, filedata, sizeof(filedata)));
504 		assertEqualMem(filedata, "ACEG", 4);
505 	}
506 
507 	/* Read the third file back. */
508 	assertEqualIntA(a, ARCHIVE_OK, archive_read_next_header(a, &ae));
509 	assertEqualInt(2, archive_entry_mtime(ae));
510 	assertEqualInt(0, archive_entry_mtime_nsec(ae));
511 	assertEqualInt(0, archive_entry_atime(ae));
512 	assertEqualInt(0, archive_entry_ctime(ae));
513 	assertEqualString("file3_stored", archive_entry_pathname(ae));
514 	if (seeking) {
515 		assertEqualInt(5, archive_entry_size(ae));
516 		assertEqualInt(AE_IFREG | 0621, archive_entry_mode(ae));
517 	} else {
518 		assertEqualInt(0, archive_entry_size_is_set(ae));
519 	}
520 	if (content) {
521 		assertEqualIntA(a, 5,
522 		    archive_read_data(a, filedata, sizeof(filedata)));
523 		assertEqualMem(filedata, "ijklm", 4);
524 	}
525 
526 	/* Read symlink. */
527 	assertEqualIntA(a, ARCHIVE_OK, archive_read_next_header(a, &ae));
528 	assertEqualInt(1, archive_entry_mtime(ae));
529 	assertEqualInt(0, archive_entry_mtime_nsec(ae));
530 	assertEqualInt(0, archive_entry_atime(ae));
531 	assertEqualInt(0, archive_entry_ctime(ae));
532 	assertEqualString("symlink_stored", archive_entry_pathname(ae));
533 	assertEqualInt(AE_IFLNK | 0755, archive_entry_mode(ae));
534 	assertEqualInt(0, archive_entry_size(ae));
535 	assertEqualString("file1", archive_entry_symlink(ae));
536 
537 	/* Read the dir entry back. */
538 	assertEqualIntA(a, ARCHIVE_OK, archive_read_next_header(a, &ae));
539 	assertEqualInt(11, archive_entry_mtime(ae));
540 	assertEqualInt(0, archive_entry_mtime_nsec(ae));
541 	assertEqualInt(0, archive_entry_atime(ae));
542 	assertEqualInt(0, archive_entry_ctime(ae));
543 	assertEqualString("dir_stored/", archive_entry_pathname(ae));
544 	if (seeking)
545 		assertEqualInt(AE_IFDIR | 0755, archive_entry_mode(ae));
546 	assertEqualInt(0, archive_entry_size(ae));
547 	if (content) {
548 		assertEqualIntA(a, 0, archive_read_data(a, filedata, 10));
549 	}
550 
551 	/* Verify the end of the archive. */
552 	assertEqualIntA(a, ARCHIVE_EOF, archive_read_next_header(a, &ae));
553 	assertEqualInt(ARCHIVE_OK, archive_read_close(a));
554 	assertEqualInt(ARCHIVE_OK, archive_read_free(a));
555 }
556 
557 /*
558  * Do a write-then-read roundtrip.
559  */
560 DEFINE_TEST(test_write_format_zip)
561 {
562 	struct archive *a;
563 	size_t used;
564 	size_t buffsize = 1000000;
565 	char *buff;
566 
567 	buff = malloc(buffsize);
568 
569 	/* Create a new archive in memory. */
570 	assert((a = archive_write_new()) != NULL);
571 	assertEqualIntA(a, ARCHIVE_OK, archive_write_set_format_zip(a));
572 	assertEqualIntA(a, ARCHIVE_OK, archive_write_add_filter_none(a));
573 	assertEqualIntA(a, ARCHIVE_OK,
574 	    archive_write_set_options(a, "zip:experimental"));
575 	assertEqualIntA(a, ARCHIVE_OK,
576 	    archive_write_open_memory(a, buff, buffsize, &used));
577 	write_contents(a);
578 	dumpfile("constructed.zip", buff, used);
579 
580 	/*
581 	 * Now, read the data back.
582 	 */
583 	/* With the standard memory reader. */
584 	assert((a = archive_read_new()) != NULL);
585 	assertEqualIntA(a, ARCHIVE_OK, archive_read_support_format_all(a));
586 	assertEqualIntA(a, ARCHIVE_OK, archive_read_support_filter_all(a));
587 	assertEqualIntA(a, ARCHIVE_OK, archive_read_open_memory(a, buff, used));
588 	verify_contents(a, 1, 1);
589 
590 	/* With the test memory reader -- streaming mode. */
591 	assert((a = archive_read_new()) != NULL);
592 	assertEqualIntA(a, ARCHIVE_OK, archive_read_support_format_all(a));
593 	assertEqualIntA(a, ARCHIVE_OK, archive_read_support_filter_all(a));
594 	assertEqualIntA(a, ARCHIVE_OK, read_open_memory(a, buff, used, 7));
595 	/* Streaming reader doesn't see mode information from Central Directory. */
596 	verify_contents(a, 0, 1);
597 
598 	assert((a = archive_read_new()) != NULL);
599 	assertEqualIntA(a, ARCHIVE_OK, archive_read_support_format_all(a));
600 	assertEqualIntA(a, ARCHIVE_OK, archive_read_support_filter_all(a));
601 	assertEqualIntA(a, ARCHIVE_OK, read_open_memory(a, buff, used, 7));
602 	/* Streaming reader doesn't see mode information from Central Directory. */
603 	verify_contents(a, 0, 0);
604 
605 	/* With the test memory reader -- seeking mode. */
606 	assert((a = archive_read_new()) != NULL);
607 	assertEqualIntA(a, ARCHIVE_OK, archive_read_support_format_all(a));
608 	assertEqualIntA(a, ARCHIVE_OK, archive_read_support_filter_all(a));
609 	assertEqualIntA(a, ARCHIVE_OK, read_open_memory_seek(a, buff, used, 7));
610 	verify_contents(a, 1, 1);
611 
612 	assert((a = archive_read_new()) != NULL);
613 	assertEqualIntA(a, ARCHIVE_OK, archive_read_support_format_all(a));
614 	assertEqualIntA(a, ARCHIVE_OK, archive_read_support_filter_all(a));
615 	assertEqualIntA(a, ARCHIVE_OK, read_open_memory_seek(a, buff, used, 7));
616 	verify_contents(a, 1, 0);
617 
618 	free(buff);
619 }
620 
621 /*
622  * Do a write-then-read roundtrip with Zip64 enabled.
623  */
624 DEFINE_TEST(test_write_format_zip64)
625 {
626 	struct archive *a;
627 	size_t used;
628 	size_t buffsize = 1000000;
629 	char *buff;
630 
631 	buff = malloc(buffsize);
632 
633 	/* Create a new archive in memory. */
634 	assert((a = archive_write_new()) != NULL);
635 	assertEqualIntA(a, ARCHIVE_OK, archive_write_set_format_zip(a));
636 	assertEqualIntA(a, ARCHIVE_OK, archive_write_add_filter_none(a));
637 	assertEqualIntA(a, ARCHIVE_OK,
638 	    archive_write_set_options(a, "zip:zip64"));
639 	assertEqualIntA(a, ARCHIVE_OK,
640 	    archive_write_set_options(a, "zip:experimental"));
641 	assertEqualIntA(a, ARCHIVE_OK,
642 	    archive_write_open_memory(a, buff, buffsize, &used));
643 	write_contents(a);
644 	dumpfile("constructed64.zip", buff, used);
645 
646 	/*
647 	 * Now, read the data back.
648 	 */
649 	/* With the standard memory reader. */
650 	assert((a = archive_read_new()) != NULL);
651 	assertEqualIntA(a, ARCHIVE_OK, archive_read_support_format_all(a));
652 	assertEqualIntA(a, ARCHIVE_OK, archive_read_support_filter_all(a));
653 	assertEqualIntA(a, ARCHIVE_OK, archive_read_open_memory(a, buff, used));
654 	verify_contents(a, 1, 1);
655 
656 	/* With the test memory reader -- streaming mode. */
657 	assert((a = archive_read_new()) != NULL);
658 	assertEqualIntA(a, ARCHIVE_OK, archive_read_support_format_all(a));
659 	assertEqualIntA(a, ARCHIVE_OK, archive_read_support_filter_all(a));
660 	assertEqualIntA(a, ARCHIVE_OK, read_open_memory(a, buff, used, 7));
661 	/* Streaming reader doesn't see mode information from Central Directory. */
662 	verify_contents(a, 0, 1);
663 
664 	assert((a = archive_read_new()) != NULL);
665 	assertEqualIntA(a, ARCHIVE_OK, archive_read_support_format_all(a));
666 	assertEqualIntA(a, ARCHIVE_OK, archive_read_support_filter_all(a));
667 	assertEqualIntA(a, ARCHIVE_OK, read_open_memory(a, buff, used, 7));
668 	/* Streaming reader doesn't see mode information from Central Directory. */
669 	verify_contents(a, 0, 0);
670 
671 	/* With the test memory reader -- seeking mode. */
672 	assert((a = archive_read_new()) != NULL);
673 	assertEqualIntA(a, ARCHIVE_OK, archive_read_support_format_all(a));
674 	assertEqualIntA(a, ARCHIVE_OK, archive_read_support_filter_all(a));
675 	assertEqualIntA(a, ARCHIVE_OK, read_open_memory_seek(a, buff, used, 7));
676 	verify_contents(a, 1, 1);
677 
678 	assert((a = archive_read_new()) != NULL);
679 	assertEqualIntA(a, ARCHIVE_OK, archive_read_support_format_all(a));
680 	assertEqualIntA(a, ARCHIVE_OK, archive_read_support_filter_all(a));
681 	assertEqualIntA(a, ARCHIVE_OK, read_open_memory_seek(a, buff, used, 7));
682 	verify_contents(a, 1, 0);
683 
684 	free(buff);
685 }
686 
687 DEFINE_TEST(test_write_format_zip_traditional_pkware_encryption)
688 {
689 	struct archive *a;
690 	size_t used;
691 	size_t buffsize = 1000000;
692 	char *buff;
693 
694 	buff = malloc(buffsize);
695 
696 	/* Create a new archive in memory. */
697 	assert((a = archive_write_new()) != NULL);
698 	assertEqualIntA(a, ARCHIVE_OK, archive_write_set_format_zip(a));
699 	assertEqualIntA(a, ARCHIVE_OK, archive_write_add_filter_none(a));
700 	if (ARCHIVE_OK != archive_write_set_options(a,
701 		    "zip:encryption=zipcrypt")) {
702 		skipping("This system does not have cryptographic liberary");
703 		archive_write_free(a);
704 		free(buff);
705 		return;
706 	}
707 	assertEqualIntA(a, ARCHIVE_OK,
708 	    archive_write_set_passphrase(a, "password1234"));
709 	assertEqualIntA(a, ARCHIVE_OK,
710 	    archive_write_set_options(a, "zip:experimental"));
711 	assertEqualIntA(a, ARCHIVE_OK,
712 	    archive_write_open_memory(a, buff, buffsize, &used));
713 	write_contents(a);
714 	dumpfile("constructed.zip", buff, used);
715 
716 	/*
717 	 * Now, read the data back.
718 	 */
719 	/* With the standard memory reader. */
720 	assert((a = archive_read_new()) != NULL);
721 	assertEqualIntA(a, ARCHIVE_OK, archive_read_support_format_all(a));
722 	assertEqualIntA(a, ARCHIVE_OK, archive_read_support_filter_all(a));
723 	assertEqualIntA(a, ARCHIVE_OK,
724 	    archive_read_add_passphrase(a, "password1234"));
725 	assertEqualIntA(a, ARCHIVE_OK, archive_read_open_memory(a, buff, used));
726 	verify_contents(a, 1, 1);
727 
728 	/* With the test memory reader -- streaming mode. */
729 	assert((a = archive_read_new()) != NULL);
730 	assertEqualIntA(a, ARCHIVE_OK, archive_read_support_format_all(a));
731 	assertEqualIntA(a, ARCHIVE_OK, archive_read_support_filter_all(a));
732 	assertEqualIntA(a, ARCHIVE_OK,
733 	    archive_read_add_passphrase(a, "password1234"));
734 	assertEqualIntA(a, ARCHIVE_OK, read_open_memory(a, buff, used, 7));
735 	/* Streaming reader doesn't see mode information from Central Directory. */
736 	verify_contents(a, 0, 1);
737 
738 	assert((a = archive_read_new()) != NULL);
739 	assertEqualIntA(a, ARCHIVE_OK, archive_read_support_format_all(a));
740 	assertEqualIntA(a, ARCHIVE_OK, archive_read_support_filter_all(a));
741 	assertEqualIntA(a, ARCHIVE_OK,
742 	    archive_read_add_passphrase(a, "password1234"));
743 	assertEqualIntA(a, ARCHIVE_OK, read_open_memory(a, buff, used, 7));
744 	/* Streaming reader doesn't see mode information from Central Directory. */
745 	verify_contents(a, 0, 0);
746 
747 	/* With the test memory reader -- seeking mode. */
748 	assert((a = archive_read_new()) != NULL);
749 	assertEqualIntA(a, ARCHIVE_OK, archive_read_support_format_all(a));
750 	assertEqualIntA(a, ARCHIVE_OK, archive_read_support_filter_all(a));
751 	assertEqualIntA(a, ARCHIVE_OK,
752 	    archive_read_add_passphrase(a, "password1234"));
753 	assertEqualIntA(a, ARCHIVE_OK, read_open_memory_seek(a, buff, used, 7));
754 	verify_contents(a, 1, 1);
755 
756 	assert((a = archive_read_new()) != NULL);
757 	assertEqualIntA(a, ARCHIVE_OK, archive_read_support_format_all(a));
758 	assertEqualIntA(a, ARCHIVE_OK, archive_read_support_filter_all(a));
759 	assertEqualIntA(a, ARCHIVE_OK,
760 	    archive_read_add_passphrase(a, "password1234"));
761 	assertEqualIntA(a, ARCHIVE_OK, read_open_memory_seek(a, buff, used, 7));
762 	verify_contents(a, 1, 0);
763 
764 	free(buff);
765 }
766 
767 DEFINE_TEST(test_write_format_zip_winzip_aes128_encryption)
768 {
769 	struct archive *a;
770 	size_t used;
771 	size_t buffsize = 1000000;
772 	char *buff;
773 
774 	buff = malloc(buffsize);
775 
776 	/* Create a new archive in memory. */
777 	assert((a = archive_write_new()) != NULL);
778 	assertEqualIntA(a, ARCHIVE_OK, archive_write_set_format_zip(a));
779 	assertEqualIntA(a, ARCHIVE_OK, archive_write_add_filter_none(a));
780 	if (ARCHIVE_OK != archive_write_set_options(a, "zip:encryption=aes128"))
781 	{
782 		skipping("This system does not have cryptographic liberary");
783 		archive_write_free(a);
784 		free(buff);
785 		return;
786 	}
787 	assertEqualIntA(a, ARCHIVE_OK,
788 	    archive_write_set_passphrase(a, "password1234"));
789 	assertEqualIntA(a, ARCHIVE_OK,
790 	    archive_write_set_options(a, "zip:experimental"));
791 	assertEqualIntA(a, ARCHIVE_OK,
792 	    archive_write_open_memory(a, buff, buffsize, &used));
793 	write_contents(a);
794 	dumpfile("constructed.zip", buff, used);
795 
796 	/*
797 	 * Now, read the data back.
798 	 */
799 	/* With the standard memory reader. */
800 	assert((a = archive_read_new()) != NULL);
801 	assertEqualIntA(a, ARCHIVE_OK, archive_read_support_format_all(a));
802 	assertEqualIntA(a, ARCHIVE_OK, archive_read_support_filter_all(a));
803 	assertEqualIntA(a, ARCHIVE_OK,
804 	    archive_read_add_passphrase(a, "password1234"));
805 	assertEqualIntA(a, ARCHIVE_OK, archive_read_open_memory(a, buff, used));
806 	verify_contents(a, 1, 1);
807 
808 	/* With the test memory reader -- streaming mode. */
809 	assert((a = archive_read_new()) != NULL);
810 	assertEqualIntA(a, ARCHIVE_OK, archive_read_support_format_all(a));
811 	assertEqualIntA(a, ARCHIVE_OK, archive_read_support_filter_all(a));
812 	assertEqualIntA(a, ARCHIVE_OK,
813 	    archive_read_add_passphrase(a, "password1234"));
814 	assertEqualIntA(a, ARCHIVE_OK, read_open_memory(a, buff, used, 7));
815 	/* Streaming reader doesn't see mode information from Central Directory. */
816 	verify_contents(a, 0, 1);
817 
818 	assert((a = archive_read_new()) != NULL);
819 	assertEqualIntA(a, ARCHIVE_OK, archive_read_support_format_all(a));
820 	assertEqualIntA(a, ARCHIVE_OK, archive_read_support_filter_all(a));
821 	assertEqualIntA(a, ARCHIVE_OK,
822 	    archive_read_add_passphrase(a, "password1234"));
823 	assertEqualIntA(a, ARCHIVE_OK, read_open_memory(a, buff, used, 7));
824 	/* Streaming reader doesn't see mode information from Central Directory. */
825 	verify_contents(a, 0, 0);
826 
827 	/* With the test memory reader -- seeking mode. */
828 	assert((a = archive_read_new()) != NULL);
829 	assertEqualIntA(a, ARCHIVE_OK, archive_read_support_format_all(a));
830 	assertEqualIntA(a, ARCHIVE_OK, archive_read_support_filter_all(a));
831 	assertEqualIntA(a, ARCHIVE_OK,
832 	    archive_read_add_passphrase(a, "password1234"));
833 	assertEqualIntA(a, ARCHIVE_OK, read_open_memory_seek(a, buff, used, 7));
834 	verify_contents(a, 1, 1);
835 
836 	assert((a = archive_read_new()) != NULL);
837 	assertEqualIntA(a, ARCHIVE_OK, archive_read_support_format_all(a));
838 	assertEqualIntA(a, ARCHIVE_OK, archive_read_support_filter_all(a));
839 	assertEqualIntA(a, ARCHIVE_OK,
840 	    archive_read_add_passphrase(a, "password1234"));
841 	assertEqualIntA(a, ARCHIVE_OK, read_open_memory_seek(a, buff, used, 7));
842 	verify_contents(a, 1, 0);
843 
844 	free(buff);
845 }
846 
847 DEFINE_TEST(test_write_format_zip_winzip_aes256_encryption)
848 {
849 	struct archive *a;
850 	size_t used;
851 	size_t buffsize = 1000000;
852 	char *buff;
853 
854 	buff = malloc(buffsize);
855 
856 	/* Create a new archive in memory. */
857 	assert((a = archive_write_new()) != NULL);
858 	assertEqualIntA(a, ARCHIVE_OK, archive_write_set_format_zip(a));
859 	assertEqualIntA(a, ARCHIVE_OK, archive_write_add_filter_none(a));
860 	if (ARCHIVE_OK != archive_write_set_options(a, "zip:encryption=aes256"))
861 	{
862 		skipping("This system does not have cryptographic liberary");
863 		archive_write_free(a);
864 		free(buff);
865 		return;
866 	}
867 	assertEqualIntA(a, ARCHIVE_OK,
868 	    archive_write_set_passphrase(a, "password1234"));
869 	assertEqualIntA(a, ARCHIVE_OK,
870 	    archive_write_set_options(a, "zip:experimental"));
871 	assertEqualIntA(a, ARCHIVE_OK,
872 	    archive_write_open_memory(a, buff, buffsize, &used));
873 	write_contents(a);
874 	dumpfile("constructed.zip", buff, used);
875 
876 	/*
877 	 * Now, read the data back.
878 	 */
879 	/* With the standard memory reader. */
880 	assert((a = archive_read_new()) != NULL);
881 	assertEqualIntA(a, ARCHIVE_OK, archive_read_support_format_all(a));
882 	assertEqualIntA(a, ARCHIVE_OK, archive_read_support_filter_all(a));
883 	assertEqualIntA(a, ARCHIVE_OK,
884 	    archive_read_add_passphrase(a, "password1234"));
885 	assertEqualIntA(a, ARCHIVE_OK, archive_read_open_memory(a, buff, used));
886 	verify_contents(a, 1, 1);
887 
888 	/* With the test memory reader -- streaming mode. */
889 	assert((a = archive_read_new()) != NULL);
890 	assertEqualIntA(a, ARCHIVE_OK, archive_read_support_format_all(a));
891 	assertEqualIntA(a, ARCHIVE_OK, archive_read_support_filter_all(a));
892 	assertEqualIntA(a, ARCHIVE_OK,
893 	    archive_read_add_passphrase(a, "password1234"));
894 	assertEqualIntA(a, ARCHIVE_OK, read_open_memory(a, buff, used, 7));
895 	/* Streaming reader doesn't see mode information from Central Directory. */
896 	verify_contents(a, 0, 1);
897 
898 	assert((a = archive_read_new()) != NULL);
899 	assertEqualIntA(a, ARCHIVE_OK, archive_read_support_format_all(a));
900 	assertEqualIntA(a, ARCHIVE_OK, archive_read_support_filter_all(a));
901 	assertEqualIntA(a, ARCHIVE_OK,
902 	    archive_read_add_passphrase(a, "password1234"));
903 	assertEqualIntA(a, ARCHIVE_OK, read_open_memory(a, buff, used, 7));
904 	/* Streaming reader doesn't see mode information from Central Directory. */
905 	verify_contents(a, 0, 0);
906 
907 	/* With the test memory reader -- seeking mode. */
908 	assert((a = archive_read_new()) != NULL);
909 	assertEqualIntA(a, ARCHIVE_OK, archive_read_support_format_all(a));
910 	assertEqualIntA(a, ARCHIVE_OK, archive_read_support_filter_all(a));
911 	assertEqualIntA(a, ARCHIVE_OK,
912 	    archive_read_add_passphrase(a, "password1234"));
913 	assertEqualIntA(a, ARCHIVE_OK, read_open_memory_seek(a, buff, used, 7));
914 	verify_contents(a, 1, 1);
915 
916 	assert((a = archive_read_new()) != NULL);
917 	assertEqualIntA(a, ARCHIVE_OK, archive_read_support_format_all(a));
918 	assertEqualIntA(a, ARCHIVE_OK, archive_read_support_filter_all(a));
919 	assertEqualIntA(a, ARCHIVE_OK,
920 	    archive_read_add_passphrase(a, "password1234"));
921 	assertEqualIntA(a, ARCHIVE_OK, read_open_memory_seek(a, buff, used, 7));
922 	verify_contents(a, 1, 0);
923 
924 	free(buff);
925 }
926