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