1/*-
2 * Copyright (c) 2011 Michihiro NAKAJIMA
3 * All rights reserved.
4 *
5 * Redistribution and use in source and binary forms, with or without
6 * modification, are permitted provided that the following conditions
7 * are met:
8 * 1. Redistributions of source code must retain the above copyright
9 *    notice, this list of conditions and the following disclaimer.
10 * 2. Redistributions in binary form must reproduce the above copyright
11 *    notice, this list of conditions and the following disclaimer in the
12 *    documentation and/or other materials provided with the distribution.
13 *
14 * THIS SOFTWARE IS PROVIDED BY THE AUTHOR(S) ``AS IS'' AND ANY EXPRESS OR
15 * IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE IMPLIED WARRANTIES
16 * OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE ARE DISCLAIMED.
17 * IN NO EVENT SHALL THE AUTHOR(S) BE LIABLE FOR ANY DIRECT, INDIRECT,
18 * INCIDENTAL, SPECIAL, EXEMPLARY, OR CONSEQUENTIAL DAMAGES (INCLUDING, BUT
19 * NOT LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS OR SERVICES; LOSS OF USE,
20 * DATA, OR PROFITS; OR BUSINESS INTERRUPTION) HOWEVER CAUSED AND ON ANY
21 * THEORY OF LIABILITY, WHETHER IN CONTRACT, STRICT LIABILITY, OR TORT
22 * (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY OUT OF THE USE OF
23 * THIS SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF SUCH DAMAGE.
24 */
25#include "test.h"
26__FBSDID("$FreeBSD");
27
28#if defined(_WIN32) && !defined(__CYGWIN__)
29#define	close		_close
30#define	open		_open
31#endif
32
33/*
34 * Extract a non-encoded file.
35 * The header of the 7z archive files is not encoded.
36 */
37static void
38test_copy(int use_open_fd)
39{
40	const char *refname = "test_read_format_7zip_copy.7z";
41	struct archive_entry *ae;
42	struct archive *a;
43	char buff[128];
44	int fd = -1;
45
46	extract_reference_file(refname);
47	assert((a = archive_read_new()) != NULL);
48	assertEqualIntA(a, ARCHIVE_OK, archive_read_support_filter_all(a));
49	assertEqualIntA(a, ARCHIVE_OK, archive_read_support_format_all(a));
50	if (use_open_fd) {
51		fd = open(refname, O_RDONLY | O_BINARY);
52		assertEqualIntA(a, ARCHIVE_OK,
53		    archive_read_open_fd(a, fd, 10240));
54	} else {
55		assertEqualIntA(a, ARCHIVE_OK,
56		    archive_read_open_filename(a, refname, 10240));
57	}
58
59	/* Verify regular file1. */
60	assertEqualIntA(a, ARCHIVE_OK, archive_read_next_header(a, &ae));
61	assertEqualInt((AE_IFREG | 0666), archive_entry_mode(ae));
62	assertEqualString("file1", archive_entry_pathname(ae));
63	assertEqualInt(86401, archive_entry_mtime(ae));
64	assertEqualInt(60, archive_entry_size(ae));
65	assertEqualInt(archive_entry_is_encrypted(ae), 0);
66	assert(archive_read_has_encrypted_entries(a) > ARCHIVE_READ_FORMAT_ENCRYPTION_UNSUPPORTED);
67	assertEqualInt(60, archive_read_data(a, buff, sizeof(buff)));
68	assertEqualMem(buff, "    ", 4);
69
70	assertEqualInt(1, archive_file_count(a));
71
72	/* End of archive. */
73	assertEqualIntA(a, ARCHIVE_EOF, archive_read_next_header(a, &ae));
74
75	/* Verify archive format. */
76	assertEqualIntA(a, ARCHIVE_FILTER_NONE, archive_filter_code(a, 0));
77	assertEqualIntA(a, ARCHIVE_FORMAT_7ZIP, archive_format(a));
78
79	/* Close the archive. */
80	assertEqualInt(ARCHIVE_OK, archive_read_close(a));
81	assertEqualInt(ARCHIVE_OK, archive_read_free(a));
82	if (fd != -1)
83		close(fd);
84}
85
86/*
87 * An archive file has no entry.
88 */
89static void
90test_empty_archive(void)
91{
92	const char *refname = "test_read_format_7zip_empty_archive.7z";
93	struct archive_entry *ae;
94	struct archive *a;
95
96	extract_reference_file(refname);
97	assert((a = archive_read_new()) != NULL);
98	assertEqualIntA(a, ARCHIVE_OK, archive_read_support_filter_all(a));
99	assertEqualIntA(a, ARCHIVE_OK, archive_read_support_format_all(a));
100	assertEqualIntA(a, ARCHIVE_OK,
101	    archive_read_open_filename(a, refname, 10240));
102
103	/* End of archive. */
104	assertEqualIntA(a, ARCHIVE_EOF, archive_read_next_header(a, &ae));
105
106	assertEqualInt(0, archive_file_count(a));
107
108	/* Verify archive format. */
109	assertEqualIntA(a, ARCHIVE_FILTER_NONE, archive_filter_code(a, 0));
110	assertEqualIntA(a, ARCHIVE_FORMAT_7ZIP, archive_format(a));
111
112	/* Close the archive. */
113	assertEqualInt(ARCHIVE_OK, archive_read_close(a));
114	assertEqualInt(ARCHIVE_OK, archive_read_free(a));
115}
116
117/*
118 * An archive file has one empty file. It means there is no content
119 * in the archive file except for a header.
120 */
121static void
122test_empty_file(void)
123{
124	const char *refname = "test_read_format_7zip_empty_file.7z";
125	struct archive_entry *ae;
126	struct archive *a;
127
128	extract_reference_file(refname);
129	assert((a = archive_read_new()) != NULL);
130	assertEqualIntA(a, ARCHIVE_OK, archive_read_support_filter_all(a));
131	assertEqualIntA(a, ARCHIVE_OK, archive_read_support_format_all(a));
132	assertEqualIntA(a, ARCHIVE_OK,
133	    archive_read_open_filename(a, refname, 10240));
134
135	/* Verify regular empty. */
136	assertEqualIntA(a, ARCHIVE_OK, archive_read_next_header(a, &ae));
137	assertEqualInt((AE_IFREG | 0644), archive_entry_mode(ae));
138	assertEqualString("empty", archive_entry_pathname(ae));
139	assertEqualInt(86401, archive_entry_mtime(ae));
140	assertEqualInt(0, archive_entry_size(ae));
141	assertEqualInt(archive_entry_is_encrypted(ae), 0);
142	assertEqualIntA(a, archive_read_has_encrypted_entries(a), 0);
143
144	assertEqualInt(1, archive_file_count(a));
145
146	/* End of archive. */
147	assertEqualIntA(a, ARCHIVE_EOF, archive_read_next_header(a, &ae));
148
149	/* Verify archive format. */
150	assertEqualIntA(a, ARCHIVE_FILTER_NONE, archive_filter_code(a, 0));
151	assertEqualIntA(a, ARCHIVE_FORMAT_7ZIP, archive_format(a));
152
153	/* Close the archive. */
154	assertEqualInt(ARCHIVE_OK, archive_read_close(a));
155	assertEqualInt(ARCHIVE_OK, archive_read_free(a));
156}
157
158/*
159 * Extract an encoded file.
160 * The header of the 7z archive files is not encoded.
161 */
162static void
163test_plain_header(const char *refname)
164{
165	struct archive_entry *ae;
166	struct archive *a;
167	char buff[128];
168
169	extract_reference_file(refname);
170	assert((a = archive_read_new()) != NULL);
171	assertEqualIntA(a, ARCHIVE_OK, archive_read_support_filter_all(a));
172	assertEqualIntA(a, ARCHIVE_OK, archive_read_support_format_all(a));
173	assertEqualIntA(a, ARCHIVE_OK,
174	    archive_read_open_filename(a, refname, 10240));
175
176	/* Verify regular file1. */
177	assertEqualIntA(a, ARCHIVE_OK, archive_read_next_header(a, &ae));
178	assertEqualInt((AE_IFREG | 0644), archive_entry_mode(ae));
179	assertEqualString("file1", archive_entry_pathname(ae));
180	assertEqualInt(1322058763, archive_entry_mtime(ae));
181	assertEqualInt(2844, archive_entry_size(ae));
182	assertEqualInt(archive_entry_is_encrypted(ae), 0);
183	assertEqualIntA(a, archive_read_has_encrypted_entries(a), 0);
184	assertEqualInt(sizeof(buff), archive_read_data(a, buff, sizeof(buff)));
185	assertEqualMem(buff, "The libarchive distribution ", 28);
186
187	assertEqualInt(1, archive_file_count(a));
188
189	/* End of archive. */
190	assertEqualIntA(a, ARCHIVE_EOF, archive_read_next_header(a, &ae));
191
192	/* Verify archive format. */
193	assertEqualIntA(a, ARCHIVE_FILTER_NONE, archive_filter_code(a, 0));
194	assertEqualIntA(a, ARCHIVE_FORMAT_7ZIP, archive_format(a));
195
196	/* Close the archive. */
197	assertEqualInt(ARCHIVE_OK, archive_read_close(a));
198	assertEqualInt(ARCHIVE_OK, archive_read_free(a));
199}
200
201/*
202 * Extract multi files.
203 * The header of the 7z archive files is encoded with LZMA.
204 */
205static void
206test_extract_all_files(const char *refname)
207{
208	struct archive_entry *ae;
209	struct archive *a;
210	char buff[128];
211
212	extract_reference_file(refname);
213	assert((a = archive_read_new()) != NULL);
214	assertEqualIntA(a, ARCHIVE_OK, archive_read_support_filter_all(a));
215	assertEqualIntA(a, ARCHIVE_OK, archive_read_support_format_all(a));
216	assertEqualIntA(a, ARCHIVE_OK,
217	    archive_read_open_filename(a, refname, 10240));
218
219	/* Verify regular file1. */
220	assertEqualIntA(a, ARCHIVE_OK, archive_read_next_header(a, &ae));
221	assertEqualInt((AE_IFREG | 0644), archive_entry_mode(ae));
222	assertEqualString("dir1/file1", archive_entry_pathname(ae));
223	assertEqualInt(86401, archive_entry_mtime(ae));
224	assertEqualInt(13, archive_entry_size(ae));
225	assertEqualInt(archive_entry_is_encrypted(ae), 0);
226	assertEqualIntA(a, archive_read_has_encrypted_entries(a), 0);
227	assertEqualInt(13, archive_read_data(a, buff, sizeof(buff)));
228	assertEqualMem(buff, "aaaaaaaaaaaa\n", 13);
229
230	/* Verify regular file2. */
231	assertEqualIntA(a, ARCHIVE_OK, archive_read_next_header(a, &ae));
232	assertEqualInt((AE_IFREG | 0644), archive_entry_mode(ae));
233	assertEqualString("file2", archive_entry_pathname(ae));
234	assertEqualInt(86401, archive_entry_mtime(ae));
235	assertEqualInt(26, archive_entry_size(ae));
236	assertEqualInt(archive_entry_is_encrypted(ae), 0);
237	assertEqualIntA(a, archive_read_has_encrypted_entries(a), 0);
238	assertEqualInt(26, archive_read_data(a, buff, sizeof(buff)));
239	assertEqualMem(buff, "aaaaaaaaaaaa\nbbbbbbbbbbbb\n", 26);
240
241	/* Verify regular file3. */
242	assertEqualIntA(a, ARCHIVE_OK, archive_read_next_header(a, &ae));
243	assertEqualInt((AE_IFREG | 0644), archive_entry_mode(ae));
244	assertEqualString("file3", archive_entry_pathname(ae));
245	assertEqualInt(86401, archive_entry_mtime(ae));
246	assertEqualInt(39, archive_entry_size(ae));
247	assertEqualInt(archive_entry_is_encrypted(ae), 0);
248	assertEqualIntA(a, archive_read_has_encrypted_entries(a), 0);
249	assertEqualInt(39, archive_read_data(a, buff, sizeof(buff)));
250	assertEqualMem(buff, "aaaaaaaaaaaa\nbbbbbbbbbbbb\ncccccccccccc\n", 39);
251
252	/* Verify regular file4. */
253	assertEqualIntA(a, ARCHIVE_OK, archive_read_next_header(a, &ae));
254	assertEqualInt((AE_IFREG | 0644), archive_entry_mode(ae));
255	assertEqualString("file4", archive_entry_pathname(ae));
256	assertEqualInt(86401, archive_entry_mtime(ae));
257	assertEqualInt(52, archive_entry_size(ae));
258	assertEqualInt(archive_entry_is_encrypted(ae), 0);
259	assertEqualIntA(a, archive_read_has_encrypted_entries(a), 0);
260	assertEqualInt(52, archive_read_data(a, buff, sizeof(buff)));
261	assertEqualMem(buff,
262	    "aaaaaaaaaaaa\nbbbbbbbbbbbb\ncccccccccccc\ndddddddddddd\n", 52);
263
264	/* Verify directory dir1. */
265	assertEqualIntA(a, ARCHIVE_OK, archive_read_next_header(a, &ae));
266	assertEqualInt((AE_IFDIR | 0755), archive_entry_mode(ae));
267	assertEqualString("dir1/", archive_entry_pathname(ae));
268	assertEqualInt(2764801, archive_entry_mtime(ae));
269	assertEqualInt(archive_entry_is_encrypted(ae), 0);
270	assertEqualIntA(a, archive_read_has_encrypted_entries(a), 0);
271
272	assertEqualInt(5, archive_file_count(a));
273
274	/* End of archive. */
275	assertEqualIntA(a, ARCHIVE_EOF, archive_read_next_header(a, &ae));
276
277	/* Verify archive format. */
278	assertEqualIntA(a, ARCHIVE_FILTER_NONE, archive_filter_code(a, 0));
279	assertEqualIntA(a, ARCHIVE_FORMAT_7ZIP, archive_format(a));
280
281	/* Close the archive. */
282	assertEqualInt(ARCHIVE_OK, archive_read_close(a));
283	assertEqualInt(ARCHIVE_OK, archive_read_free(a));
284}
285
286/*
287 * Extract last file.
288 * The header of the 7z archive files is encoded with LZMA.
289 */
290static void
291test_extract_last_file(const char *refname)
292{
293	struct archive_entry *ae;
294	struct archive *a;
295	char buff[128];
296
297	extract_reference_file(refname);
298	assert((a = archive_read_new()) != NULL);
299	assertEqualIntA(a, ARCHIVE_OK, archive_read_support_filter_all(a));
300	assertEqualIntA(a, ARCHIVE_OK, archive_read_support_format_all(a));
301	assertEqualIntA(a, ARCHIVE_OK,
302	    archive_read_open_filename(a, refname, 10240));
303
304	/* Verify regular file1. */
305	assertEqualIntA(a, ARCHIVE_OK, archive_read_next_header(a, &ae));
306	assertEqualInt((AE_IFREG | 0644), archive_entry_mode(ae));
307	assertEqualString("dir1/file1", archive_entry_pathname(ae));
308	assertEqualInt(86401, archive_entry_mtime(ae));
309	assertEqualInt(13, archive_entry_size(ae));
310	assertEqualInt(archive_entry_is_encrypted(ae), 0);
311	assertEqualIntA(a, archive_read_has_encrypted_entries(a), 0);
312
313	/* Verify regular file2. */
314	assertEqualIntA(a, ARCHIVE_OK, archive_read_next_header(a, &ae));
315	assertEqualInt((AE_IFREG | 0644), archive_entry_mode(ae));
316	assertEqualString("file2", archive_entry_pathname(ae));
317	assertEqualInt(86401, archive_entry_mtime(ae));
318	assertEqualInt(26, archive_entry_size(ae));
319	assertEqualInt(archive_entry_is_encrypted(ae), 0);
320	assertEqualIntA(a, archive_read_has_encrypted_entries(a), 0);
321
322	/* Verify regular file3. */
323	assertEqualIntA(a, ARCHIVE_OK, archive_read_next_header(a, &ae));
324	assertEqualInt((AE_IFREG | 0644), archive_entry_mode(ae));
325	assertEqualString("file3", archive_entry_pathname(ae));
326	assertEqualInt(86401, archive_entry_mtime(ae));
327	assertEqualInt(39, archive_entry_size(ae));
328	assertEqualInt(archive_entry_is_encrypted(ae), 0);
329	assertEqualIntA(a, archive_read_has_encrypted_entries(a), 0);
330
331	/* Verify regular file4. */
332	assertEqualIntA(a, ARCHIVE_OK, archive_read_next_header(a, &ae));
333	assertEqualInt((AE_IFREG | 0644), archive_entry_mode(ae));
334	assertEqualString("file4", archive_entry_pathname(ae));
335	assertEqualInt(86401, archive_entry_mtime(ae));
336	assertEqualInt(52, archive_entry_size(ae));
337	assertEqualInt(archive_entry_is_encrypted(ae), 0);
338	assertEqualIntA(a, archive_read_has_encrypted_entries(a), 0);
339	assertEqualInt(52, archive_read_data(a, buff, sizeof(buff)));
340	assertEqualMem(buff,
341	    "aaaaaaaaaaaa\nbbbbbbbbbbbb\ncccccccccccc\ndddddddddddd\n", 52);
342
343	/* Verify directory dir1. */
344	assertEqualIntA(a, ARCHIVE_OK, archive_read_next_header(a, &ae));
345	assertEqualInt((AE_IFDIR | 0755), archive_entry_mode(ae));
346	assertEqualString("dir1/", archive_entry_pathname(ae));
347	assertEqualInt(2764801, archive_entry_mtime(ae));
348	assertEqualInt(archive_entry_is_encrypted(ae), 0);
349	assertEqualIntA(a, archive_read_has_encrypted_entries(a), 0);
350
351	assertEqualInt(5, archive_file_count(a));
352
353	/* End of archive. */
354	assertEqualIntA(a, ARCHIVE_EOF, archive_read_next_header(a, &ae));
355
356	/* Verify archive format. */
357	assertEqualIntA(a, ARCHIVE_FILTER_NONE, archive_filter_code(a, 0));
358	assertEqualIntA(a, ARCHIVE_FORMAT_7ZIP, archive_format(a));
359
360	/* Close the archive. */
361	assertEqualInt(ARCHIVE_OK, archive_read_close(a));
362	assertEqualInt(ARCHIVE_OK, archive_read_free(a));
363}
364
365/*
366 * Extract a mixed archive file which has both LZMA and LZMA2 encoded files.
367 *  LZMA: file1, file2, file3, file4
368 *  LZMA2: zfile1, zfile2, zfile3, zfile4
369 */
370static void
371test_extract_all_files2(const char *refname)
372{
373	struct archive_entry *ae;
374	struct archive *a;
375	char buff[128];
376
377	extract_reference_file(refname);
378	assert((a = archive_read_new()) != NULL);
379	assertEqualIntA(a, ARCHIVE_OK, archive_read_support_filter_all(a));
380	assertEqualIntA(a, ARCHIVE_OK, archive_read_support_format_all(a));
381	assertEqualIntA(a, ARCHIVE_OK,
382	    archive_read_open_filename(a, refname, 10240));
383
384	/* Verify regular file1. */
385	assertEqualIntA(a, ARCHIVE_OK, archive_read_next_header(a, &ae));
386	assertEqualInt((AE_IFREG | 0644), archive_entry_mode(ae));
387	assertEqualString("dir1/file1", archive_entry_pathname(ae));
388	assertEqualInt(86401, archive_entry_mtime(ae));
389	assertEqualInt(13, archive_entry_size(ae));
390	assertEqualInt(archive_entry_is_encrypted(ae), 0);
391	assertEqualIntA(a, archive_read_has_encrypted_entries(a), 0);
392	assertEqualInt(13, archive_read_data(a, buff, sizeof(buff)));
393	assertEqualMem(buff, "aaaaaaaaaaaa\n", 13);
394
395	/* Verify regular file2. */
396	assertEqualIntA(a, ARCHIVE_OK, archive_read_next_header(a, &ae));
397	assertEqualInt((AE_IFREG | 0644), archive_entry_mode(ae));
398	assertEqualString("file2", archive_entry_pathname(ae));
399	assertEqualInt(86401, archive_entry_mtime(ae));
400	assertEqualInt(26, archive_entry_size(ae));
401	assertEqualInt(archive_entry_is_encrypted(ae), 0);
402	assertEqualIntA(a, archive_read_has_encrypted_entries(a), 0);
403	assertEqualInt(26, archive_read_data(a, buff, sizeof(buff)));
404	assertEqualMem(buff, "aaaaaaaaaaaa\nbbbbbbbbbbbb\n", 26);
405
406	/* Verify regular file3. */
407	assertEqualIntA(a, ARCHIVE_OK, archive_read_next_header(a, &ae));
408	assertEqualInt((AE_IFREG | 0644), archive_entry_mode(ae));
409	assertEqualString("file3", archive_entry_pathname(ae));
410	assertEqualInt(86401, archive_entry_mtime(ae));
411	assertEqualInt(39, archive_entry_size(ae));
412	assertEqualInt(archive_entry_is_encrypted(ae), 0);
413	assertEqualIntA(a, archive_read_has_encrypted_entries(a), 0);
414	assertEqualInt(39, archive_read_data(a, buff, sizeof(buff)));
415	assertEqualMem(buff, "aaaaaaaaaaaa\nbbbbbbbbbbbb\ncccccccccccc\n", 39);
416
417	/* Verify regular file4. */
418	assertEqualIntA(a, ARCHIVE_OK, archive_read_next_header(a, &ae));
419	assertEqualInt((AE_IFREG | 0644), archive_entry_mode(ae));
420	assertEqualString("file4", archive_entry_pathname(ae));
421	assertEqualInt(86401, archive_entry_mtime(ae));
422	assertEqualInt(52, archive_entry_size(ae));
423	assertEqualInt(archive_entry_is_encrypted(ae), 0);
424	assertEqualIntA(a, archive_read_has_encrypted_entries(a), 0);
425	assertEqualInt(52, archive_read_data(a, buff, sizeof(buff)));
426	assertEqualMem(buff,
427	    "aaaaaaaaaaaa\nbbbbbbbbbbbb\ncccccccccccc\ndddddddddddd\n", 52);
428
429	/* Verify regular zfile1. */
430	assertEqualIntA(a, ARCHIVE_OK, archive_read_next_header(a, &ae));
431	assertEqualInt((AE_IFREG | 0644), archive_entry_mode(ae));
432	assertEqualString("dir1/zfile1", archive_entry_pathname(ae));
433	assertEqualInt(5184001, archive_entry_mtime(ae));
434	assertEqualInt(13, archive_entry_size(ae));
435	assertEqualInt(archive_entry_is_encrypted(ae), 0);
436	assertEqualIntA(a, archive_read_has_encrypted_entries(a), 0);
437	assertEqualInt(13, archive_read_data(a, buff, sizeof(buff)));
438	assertEqualMem(buff, "aaaaaaaaaaaa\n", 13);
439
440	/* Verify regular zfile2. */
441	assertEqualIntA(a, ARCHIVE_OK, archive_read_next_header(a, &ae));
442	assertEqualInt((AE_IFREG | 0644), archive_entry_mode(ae));
443	assertEqualString("zfile2", archive_entry_pathname(ae));
444	assertEqualInt(5184001, archive_entry_mtime(ae));
445	assertEqualInt(26, archive_entry_size(ae));
446	assertEqualInt(archive_entry_is_encrypted(ae), 0);
447	assertEqualIntA(a, archive_read_has_encrypted_entries(a), 0);
448	assertEqualInt(26, archive_read_data(a, buff, sizeof(buff)));
449	assertEqualMem(buff, "aaaaaaaaaaaa\nbbbbbbbbbbbb\n", 26);
450
451	/* Verify regular zfile3. */
452	assertEqualIntA(a, ARCHIVE_OK, archive_read_next_header(a, &ae));
453	assertEqualInt((AE_IFREG | 0644), archive_entry_mode(ae));
454	assertEqualString("zfile3", archive_entry_pathname(ae));
455	assertEqualInt(5184001, archive_entry_mtime(ae));
456	assertEqualInt(39, archive_entry_size(ae));
457	assertEqualInt(archive_entry_is_encrypted(ae), 0);
458	assertEqualIntA(a, archive_read_has_encrypted_entries(a), 0);
459	assertEqualInt(39, archive_read_data(a, buff, sizeof(buff)));
460	assertEqualMem(buff, "aaaaaaaaaaaa\nbbbbbbbbbbbb\ncccccccccccc\n", 39);
461
462	/* Verify regular zfile4. */
463	assertEqualIntA(a, ARCHIVE_OK, archive_read_next_header(a, &ae));
464	assertEqualInt((AE_IFREG | 0644), archive_entry_mode(ae));
465	assertEqualString("zfile4", archive_entry_pathname(ae));
466	assertEqualInt(5184001, archive_entry_mtime(ae));
467	assertEqualInt(52, archive_entry_size(ae));
468	assertEqualInt(archive_entry_is_encrypted(ae), 0);
469	assertEqualIntA(a, archive_read_has_encrypted_entries(a), 0);
470	assertEqualInt(52, archive_read_data(a, buff, sizeof(buff)));
471	assertEqualMem(buff,
472	    "aaaaaaaaaaaa\nbbbbbbbbbbbb\ncccccccccccc\ndddddddddddd\n", 52);
473
474	/* Verify directory dir1. */
475	assertEqualIntA(a, ARCHIVE_OK, archive_read_next_header(a, &ae));
476	assertEqualInt((AE_IFDIR | 0755), archive_entry_mode(ae));
477	assertEqualString("dir1/", archive_entry_pathname(ae));
478	assertEqualInt(2764801, archive_entry_mtime(ae));
479	assertEqualInt(archive_entry_is_encrypted(ae), 0);
480	assertEqualIntA(a, archive_read_has_encrypted_entries(a), 0);
481
482	assertEqualInt(9, archive_file_count(a));
483
484	/* End of archive. */
485	assertEqualIntA(a, ARCHIVE_EOF, archive_read_next_header(a, &ae));
486
487	/* Verify archive format. */
488	assertEqualIntA(a, ARCHIVE_FILTER_NONE, archive_filter_code(a, 0));
489	assertEqualIntA(a, ARCHIVE_FORMAT_7ZIP, archive_format(a));
490
491	/* Close the archive. */
492	assertEqualInt(ARCHIVE_OK, archive_read_close(a));
493	assertEqualInt(ARCHIVE_OK, archive_read_free(a));
494}
495
496/*
497 * Extract a file compressed with DELTA + LZMA[12].
498 */
499static void
500test_delta_lzma(const char *refname)
501{
502	struct archive_entry *ae;
503	struct archive *a;
504	size_t remaining;
505	ssize_t bytes;
506	char buff[1024];
507
508	extract_reference_file(refname);
509	assert((a = archive_read_new()) != NULL);
510	assertEqualIntA(a, ARCHIVE_OK, archive_read_support_filter_all(a));
511	assertEqualIntA(a, ARCHIVE_OK, archive_read_support_format_all(a));
512	assertEqualIntA(a, ARCHIVE_OK,
513	    archive_read_open_filename(a, refname, 10240));
514
515	/* Verify regular file1. */
516	assertEqualIntA(a, ARCHIVE_OK, archive_read_next_header(a, &ae));
517	assertEqualInt((AE_IFREG | 0644), archive_entry_mode(ae));
518	assertEqualString("file1", archive_entry_pathname(ae));
519	assertEqualInt(172802, archive_entry_mtime(ae));
520	assertEqualInt(27627, archive_entry_size(ae));
521	assertEqualInt(archive_entry_is_encrypted(ae), 0);
522	assertEqualIntA(a, archive_read_has_encrypted_entries(a), 0);
523	remaining = (size_t)archive_entry_size(ae);
524	while (remaining) {
525		if (remaining < sizeof(buff))
526			assertEqualInt(remaining,
527			    bytes = archive_read_data(a, buff, sizeof(buff)));
528		else
529			assertEqualInt(sizeof(buff),
530			    bytes = archive_read_data(a, buff, sizeof(buff)));
531		if (bytes > 0)
532			remaining -= bytes;
533		else
534			break;
535	}
536	assertEqualInt(0, remaining);
537
538	assertEqualInt(1, archive_file_count(a));
539
540	/* End of archive. */
541	assertEqualIntA(a, ARCHIVE_EOF, archive_read_next_header(a, &ae));
542
543	/* Verify archive format. */
544	assertEqualIntA(a, ARCHIVE_FILTER_NONE, archive_filter_code(a, 0));
545	assertEqualIntA(a, ARCHIVE_FORMAT_7ZIP, archive_format(a));
546
547	/* Close the archive. */
548	assertEqualInt(ARCHIVE_OK, archive_read_close(a));
549	assertEqualInt(ARCHIVE_OK, archive_read_free(a));
550}
551
552/*
553 * Extract a file compressed with BCJ + LZMA2.
554 */
555static void
556test_bcj(const char *refname)
557{
558	struct archive_entry *ae;
559	struct archive *a;
560	size_t remaining;
561	ssize_t bytes;
562	char buff[1024];
563
564	extract_reference_file(refname);
565	assert((a = archive_read_new()) != NULL);
566	assertEqualIntA(a, ARCHIVE_OK, archive_read_support_filter_all(a));
567	assertEqualIntA(a, ARCHIVE_OK, archive_read_support_format_all(a));
568	assertEqualIntA(a, ARCHIVE_OK,
569	    archive_read_open_filename(a, refname, 10240));
570
571	/* Verify regular x86exe. */
572	assertEqualIntA(a, ARCHIVE_OK, archive_read_next_header(a, &ae));
573	assertEqualInt((AE_IFREG | 0444), archive_entry_mode(ae) & ~0111);
574	assertEqualString("x86exe", archive_entry_pathname(ae));
575	assertEqualInt(172802, archive_entry_mtime(ae));
576	assertEqualInt(27328, archive_entry_size(ae));
577	assertEqualInt(archive_entry_is_encrypted(ae), 0);
578	assertEqualIntA(a, archive_read_has_encrypted_entries(a), 0);
579	remaining = (size_t)archive_entry_size(ae);
580	while (remaining) {
581		if (remaining < sizeof(buff))
582			assertEqualInt(remaining,
583			    bytes = archive_read_data(a, buff, sizeof(buff)));
584		else
585			assertEqualInt(sizeof(buff),
586			    bytes = archive_read_data(a, buff, sizeof(buff)));
587		if (bytes > 0)
588			remaining -= bytes;
589		else
590			break;
591	}
592	assertEqualInt(0, remaining);
593
594	assertEqualInt(1, archive_file_count(a));
595
596	/* End of archive. */
597	assertEqualIntA(a, ARCHIVE_EOF, archive_read_next_header(a, &ae));
598
599	/* Verify archive format. */
600	assertEqualIntA(a, ARCHIVE_FILTER_NONE, archive_filter_code(a, 0));
601	assertEqualIntA(a, ARCHIVE_FORMAT_7ZIP, archive_format(a));
602
603	/* Close the archive. */
604	assertEqualInt(ARCHIVE_OK, archive_read_close(a));
605	assertEqualInt(ARCHIVE_OK, archive_read_free(a));
606}
607
608/*
609 * Extract a file compressed with PPMd.
610 */
611static void
612test_ppmd(void)
613{
614	const char *refname = "test_read_format_7zip_ppmd.7z";
615	struct archive_entry *ae;
616	struct archive *a;
617	size_t remaining;
618	ssize_t bytes;
619	char buff[1024];
620
621	extract_reference_file(refname);
622	assert((a = archive_read_new()) != NULL);
623	assertEqualIntA(a, ARCHIVE_OK, archive_read_support_filter_all(a));
624	assertEqualIntA(a, ARCHIVE_OK, archive_read_support_format_all(a));
625	assertEqualIntA(a, ARCHIVE_OK,
626	    archive_read_open_filename(a, refname, 10240));
627
628	/* Verify regular file1. */
629	assertEqualIntA(a, ARCHIVE_OK, archive_read_next_header(a, &ae));
630	assertEqualInt((AE_IFREG | 0666), archive_entry_mode(ae));
631	assertEqualString("ppmd_test.txt", archive_entry_pathname(ae));
632	assertEqualInt(1322464589, archive_entry_mtime(ae));
633	assertEqualInt(102400, archive_entry_size(ae));
634	assertEqualInt(archive_entry_is_encrypted(ae), 0);
635	assertEqualIntA(a, archive_read_has_encrypted_entries(a), 0);
636	remaining = (size_t)archive_entry_size(ae);
637	while (remaining) {
638		if (remaining < sizeof(buff))
639			assertEqualInt(remaining,
640			    bytes = archive_read_data(a, buff, sizeof(buff)));
641		else
642			assertEqualInt(sizeof(buff),
643			    bytes = archive_read_data(a, buff, sizeof(buff)));
644		if (bytes > 0)
645			remaining -= bytes;
646		else
647			break;
648	}
649	assertEqualInt(0, remaining);
650
651	assertEqualInt(1, archive_file_count(a));
652
653	/* End of archive. */
654	assertEqualIntA(a, ARCHIVE_EOF, archive_read_next_header(a, &ae));
655
656	/* Verify archive format. */
657	assertEqualIntA(a, ARCHIVE_FILTER_NONE, archive_filter_code(a, 0));
658	assertEqualIntA(a, ARCHIVE_FORMAT_7ZIP, archive_format(a));
659
660	/* Close the archive. */
661	assertEqualInt(ARCHIVE_OK, archive_read_close(a));
662	assertEqualInt(ARCHIVE_OK, archive_read_free(a));
663}
664
665static void
666test_symname(void)
667{
668	const char *refname = "test_read_format_7zip_symbolic_name.7z";
669	struct archive_entry *ae;
670	struct archive *a;
671	char buff[128];
672
673	extract_reference_file(refname);
674	assert((a = archive_read_new()) != NULL);
675	assertEqualIntA(a, ARCHIVE_OK, archive_read_support_filter_all(a));
676	assertEqualIntA(a, ARCHIVE_OK, archive_read_support_format_all(a));
677	assertEqualIntA(a, ARCHIVE_OK,
678	    archive_read_open_filename(a, refname, 10240));
679
680	/* Verify regular file1. */
681	assertEqualIntA(a, ARCHIVE_OK, archive_read_next_header(a, &ae));
682	assertEqualInt((AE_IFREG | 0644), archive_entry_mode(ae));
683	assertEqualString("file1", archive_entry_pathname(ae));
684	assertEqualInt(86401, archive_entry_mtime(ae));
685	assertEqualInt(32, archive_entry_size(ae));
686	assertEqualInt(archive_entry_is_encrypted(ae), 0);
687	assertEqualIntA(a, archive_read_has_encrypted_entries(a), 0);
688	assertEqualInt(32, archive_read_data(a, buff, sizeof(buff)));
689	assertEqualMem(buff, "hellohellohello\nhellohellohello\n", 32);
690
691	/* Verify symbolic-link symlinkfile. */
692	assertEqualIntA(a, ARCHIVE_OK, archive_read_next_header(a, &ae));
693	assertEqualInt((AE_IFLNK | 0755), archive_entry_mode(ae));
694	assertEqualString("symlinkfile", archive_entry_pathname(ae));
695	assertEqualString("file1", archive_entry_symlink(ae));
696	assertEqualInt(86401, archive_entry_mtime(ae));
697	assertEqualInt(archive_entry_is_encrypted(ae), 0);
698	assertEqualIntA(a, archive_read_has_encrypted_entries(a), 0);
699
700	assertEqualInt(2, archive_file_count(a));
701
702	/* End of archive. */
703	assertEqualIntA(a, ARCHIVE_EOF, archive_read_next_header(a, &ae));
704
705	/* Verify archive format. */
706	assertEqualIntA(a, ARCHIVE_FILTER_NONE, archive_filter_code(a, 0));
707	assertEqualIntA(a, ARCHIVE_FORMAT_7ZIP, archive_format(a));
708
709	/* Close the archive. */
710	assertEqualInt(ARCHIVE_OK, archive_read_close(a));
711	assertEqualInt(ARCHIVE_OK, archive_read_free(a));
712}
713
714
715DEFINE_TEST(test_read_format_7zip)
716{
717	struct archive *a;
718
719	assert((a = archive_read_new()) != NULL);
720
721	/* Extracting with liblzma */
722	if (ARCHIVE_OK != archive_read_support_filter_xz(a)) {
723		skipping("7zip:lzma decoding is not supported on this "
724		    "platform");
725	} else {
726		test_symname();
727		test_extract_all_files("test_read_format_7zip_copy_2.7z");
728		test_extract_last_file("test_read_format_7zip_copy_2.7z");
729		test_extract_all_files2("test_read_format_7zip_lzma1_lzma2.7z");
730		test_bcj("test_read_format_7zip_bcj2_copy_lzma.7z");
731	}
732	assertEqualInt(ARCHIVE_OK, archive_read_free(a));
733}
734
735DEFINE_TEST(test_read_format_7zip_bzip2)
736{
737	struct archive *a;
738
739	assert((a = archive_read_new()) != NULL);
740
741	/* Extracting with libbzip2 */
742	if (ARCHIVE_OK != archive_read_support_filter_bzip2(a)) {
743		skipping("7zip:bzip2 decoding is not supported on this platform");
744	} else {
745		test_plain_header("test_read_format_7zip_bzip2.7z");
746		test_bcj("test_read_format_7zip_bcj_bzip2.7z");
747		test_bcj("test_read_format_7zip_bcj2_bzip2.7z");
748	}
749
750	assertEqualInt(ARCHIVE_OK, archive_read_free(a));
751}
752
753DEFINE_TEST(test_read_format_7zip_from_fd)
754{
755	test_copy(1);/* read a 7zip file from a file descriptor. */
756}
757
758DEFINE_TEST(test_read_format_7zip_copy)
759{
760	test_copy(0);
761	test_bcj("test_read_format_7zip_bcj_copy.7z");
762	test_bcj("test_read_format_7zip_bcj2_copy_1.7z");
763	test_bcj("test_read_format_7zip_bcj2_copy_2.7z");
764}
765
766DEFINE_TEST(test_read_format_7zip_deflate)
767{
768	struct archive *a;
769
770	assert((a = archive_read_new()) != NULL);
771
772	/* Extracting with libz */
773	if (ARCHIVE_OK != archive_read_support_filter_gzip(a)) {
774		skipping(
775		    "7zip:deflate decoding is not supported on this platform");
776	} else {
777		test_plain_header("test_read_format_7zip_deflate.7z");
778		test_bcj("test_read_format_7zip_bcj_deflate.7z");
779		test_bcj("test_read_format_7zip_bcj2_deflate.7z");
780	}
781
782	assertEqualInt(ARCHIVE_OK, archive_read_free(a));
783}
784
785DEFINE_TEST(test_read_format_7zip_empty)
786{
787	test_empty_archive();
788	test_empty_file();
789}
790
791DEFINE_TEST(test_read_format_7zip_lzma1)
792{
793	struct archive *a;
794
795	assert((a = archive_read_new()) != NULL);
796
797	/* Extracting with liblzma */
798	if (ARCHIVE_OK != archive_read_support_filter_xz(a)) {
799		skipping("7zip:lzma decoding is not supported on this "
800		    "platform");
801	} else {
802		test_plain_header("test_read_format_7zip_lzma1.7z");
803		test_extract_all_files("test_read_format_7zip_lzma1_2.7z");
804		test_extract_last_file("test_read_format_7zip_lzma1_2.7z");
805		test_bcj("test_read_format_7zip_bcj_lzma1.7z");
806		test_bcj("test_read_format_7zip_bcj2_lzma1_1.7z");
807		test_bcj("test_read_format_7zip_bcj2_lzma1_2.7z");
808		test_delta_lzma("test_read_format_7zip_delta_lzma1.7z");
809		test_delta_lzma("test_read_format_7zip_delta4_lzma1.7z");
810	}
811	assertEqualInt(ARCHIVE_OK, archive_read_free(a));
812}
813
814DEFINE_TEST(test_read_format_7zip_lzma2)
815{
816	struct archive *a;
817
818	assert((a = archive_read_new()) != NULL);
819
820	/* Extracting with liblzma */
821	if (ARCHIVE_OK != archive_read_support_filter_xz(a)) {
822		skipping("7zip:lzma decoding is not supported on this "
823		    "platform");
824	} else {
825		test_plain_header("test_read_format_7zip_lzma2.7z");
826		test_bcj("test_read_format_7zip_bcj_lzma2.7z");
827		test_bcj("test_read_format_7zip_bcj2_lzma2_1.7z");
828		test_bcj("test_read_format_7zip_bcj2_lzma2_2.7z");
829		test_delta_lzma("test_read_format_7zip_delta_lzma2.7z");
830		test_delta_lzma("test_read_format_7zip_delta4_lzma2.7z");
831	}
832	assertEqualInt(ARCHIVE_OK, archive_read_free(a));
833}
834
835DEFINE_TEST(test_read_format_7zip_ppmd)
836{
837	test_ppmd();
838}
839