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