xref: /freebsd/contrib/libarchive/libarchive/test/test_read_format_7zip.c (revision 185becb1e1bd2657c156f78aeb52edac05ba5fb5)
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 
27 #if HAVE_LZMA_H
28 #include <lzma.h>
29 #endif
30 
31 #if defined(_WIN32) && !defined(__CYGWIN__)
32 #define	close		_close
33 #define	open		_open
34 #endif
35 
36 #define __LIBARCHIVE_BUILD
37 
38 /*
39  * Extract a non-encoded file.
40  * The header of the 7z archive files is not encoded.
41  */
42 static void
test_copy(int use_open_fd)43 test_copy(int use_open_fd)
44 {
45 	const char *refname = "test_read_format_7zip_copy.7z";
46 	struct archive_entry *ae;
47 	struct archive *a;
48 	char buff[128];
49 	int fd = -1;
50 
51 	extract_reference_file(refname);
52 	assert((a = archive_read_new()) != NULL);
53 	assertEqualIntA(a, ARCHIVE_OK, archive_read_support_filter_all(a));
54 	assertEqualIntA(a, ARCHIVE_OK, archive_read_support_format_all(a));
55 	if (use_open_fd) {
56 		fd = open(refname, O_RDONLY | O_BINARY);
57 		assertEqualIntA(a, ARCHIVE_OK,
58 		    archive_read_open_fd(a, fd, 10240));
59 	} else {
60 		assertEqualIntA(a, ARCHIVE_OK,
61 		    archive_read_open_filename(a, refname, 10240));
62 	}
63 
64 	/* Verify regular file1. */
65 	assertEqualIntA(a, ARCHIVE_OK, archive_read_next_header(a, &ae));
66 	assertEqualInt((AE_IFREG | 0644), archive_entry_mode(ae));
67 	assertEqualString("file1", archive_entry_pathname(ae));
68 	assertEqualInt(86401, archive_entry_mtime(ae));
69 	assertEqualInt(60, archive_entry_size(ae));
70 	assertEqualInt(archive_entry_is_encrypted(ae), 0);
71 	assert(archive_read_has_encrypted_entries(a) > ARCHIVE_READ_FORMAT_ENCRYPTION_UNSUPPORTED);
72 	assertEqualInt(60, archive_read_data(a, buff, sizeof(buff)));
73 	assertEqualMem(buff, "    ", 4);
74 
75 	assertEqualInt(1, archive_file_count(a));
76 
77 	/* End of archive. */
78 	assertEqualIntA(a, ARCHIVE_EOF, archive_read_next_header(a, &ae));
79 
80 	/* Verify archive format. */
81 	assertEqualIntA(a, ARCHIVE_FILTER_NONE, archive_filter_code(a, 0));
82 	assertEqualIntA(a, ARCHIVE_FORMAT_7ZIP, archive_format(a));
83 
84 	/* Close the archive. */
85 	assertEqualIntA(a, ARCHIVE_OK, archive_read_close(a));
86 	assertEqualInt(ARCHIVE_OK, archive_read_free(a));
87 	if (fd != -1)
88 		close(fd);
89 }
90 
91 /*
92  * An archive file has no entry.
93  */
94 static void
test_empty_archive(void)95 test_empty_archive(void)
96 {
97 	const char *refname = "test_read_format_7zip_empty_archive.7z";
98 	struct archive_entry *ae;
99 	struct archive *a;
100 
101 	extract_reference_file(refname);
102 	assert((a = archive_read_new()) != NULL);
103 	assertEqualIntA(a, ARCHIVE_OK, archive_read_support_filter_all(a));
104 	assertEqualIntA(a, ARCHIVE_OK, archive_read_support_format_all(a));
105 	assertEqualIntA(a, ARCHIVE_OK,
106 	    archive_read_open_filename(a, refname, 10240));
107 
108 	/* End of archive. */
109 	assertEqualIntA(a, ARCHIVE_EOF, archive_read_next_header(a, &ae));
110 
111 	assertEqualInt(0, archive_file_count(a));
112 
113 	/* Verify archive format. */
114 	assertEqualIntA(a, ARCHIVE_FILTER_NONE, archive_filter_code(a, 0));
115 	assertEqualIntA(a, ARCHIVE_FORMAT_7ZIP, archive_format(a));
116 
117 	/* Close the archive. */
118 	assertEqualIntA(a, ARCHIVE_OK, archive_read_close(a));
119 	assertEqualInt(ARCHIVE_OK, archive_read_free(a));
120 }
121 
122 /*
123  * An archive file has one empty file. It means there is no content
124  * in the archive file except for a header.
125  */
126 static void
test_empty_file(void)127 test_empty_file(void)
128 {
129 	const char *refname = "test_read_format_7zip_empty_file.7z";
130 	struct archive_entry *ae;
131 	struct archive *a;
132 
133 	extract_reference_file(refname);
134 	assert((a = archive_read_new()) != NULL);
135 	assertEqualIntA(a, ARCHIVE_OK, archive_read_support_filter_all(a));
136 	assertEqualIntA(a, ARCHIVE_OK, archive_read_support_format_all(a));
137 	assertEqualIntA(a, ARCHIVE_OK,
138 	    archive_read_open_filename(a, refname, 10240));
139 
140 	/* Verify regular empty. */
141 	assertEqualIntA(a, ARCHIVE_OK, archive_read_next_header(a, &ae));
142 	assertEqualInt((AE_IFREG | 0644), archive_entry_mode(ae));
143 	assertEqualString("empty", archive_entry_pathname(ae));
144 	assertEqualInt(86401, archive_entry_mtime(ae));
145 	assertEqualInt(0, archive_entry_size(ae));
146 	assertEqualInt(archive_entry_is_encrypted(ae), 0);
147 	assertEqualIntA(a, archive_read_has_encrypted_entries(a), 0);
148 
149 	assertEqualInt(1, archive_file_count(a));
150 
151 	/* End of archive. */
152 	assertEqualIntA(a, ARCHIVE_EOF, archive_read_next_header(a, &ae));
153 
154 	/* Verify archive format. */
155 	assertEqualIntA(a, ARCHIVE_FILTER_NONE, archive_filter_code(a, 0));
156 	assertEqualIntA(a, ARCHIVE_FORMAT_7ZIP, archive_format(a));
157 
158 	/* Close the archive. */
159 	assertEqualIntA(a, ARCHIVE_OK, archive_read_close(a));
160 	assertEqualInt(ARCHIVE_OK, archive_read_free(a));
161 }
162 
DEFINE_TEST(test_read_format_7zip_archive_properties)163 DEFINE_TEST(test_read_format_7zip_archive_properties)
164 {
165 	const char *refname = "test_read_format_7zip_archive_properties.7z";
166 	struct archive_entry *ae;
167 	struct archive *a;
168 
169 	extract_reference_file(refname);
170 
171 	assert((a = archive_read_new()) != NULL);
172 	assertEqualIntA(a, ARCHIVE_OK, archive_read_support_filter_all(a));
173 	assertEqualIntA(a, ARCHIVE_OK, archive_read_support_format_all(a));
174 	assertEqualIntA(a, ARCHIVE_OK,
175 	    archive_read_open_filename(a, refname, 10240));
176 
177 	assertEqualIntA(a, ARCHIVE_OK, archive_read_next_header(a, &ae));
178 	assertEqualString("empty", archive_entry_pathname(ae));
179 	assertEqualInt(0, archive_entry_size(ae));
180 	assertEqualInt(1, archive_file_count(a));
181 
182 	assertEqualIntA(a, ARCHIVE_EOF, archive_read_next_header(a, &ae));
183 	assertEqualIntA(a, ARCHIVE_FORMAT_7ZIP, archive_format(a));
184 
185 	assertEqualIntA(a, ARCHIVE_OK, archive_read_close(a));
186 	assertEqualInt(ARCHIVE_OK, archive_read_free(a));
187 }
188 
189 /*
190  * Extract an encoded file.
191  * The header of the 7z archive files is not encoded.
192  */
193 static void
test_plain_header(const char * refname)194 test_plain_header(const char *refname)
195 {
196 	struct archive_entry *ae;
197 	struct archive *a;
198 	char buff[128];
199 
200 	extract_reference_file(refname);
201 	assert((a = archive_read_new()) != NULL);
202 	assertEqualIntA(a, ARCHIVE_OK, archive_read_support_filter_all(a));
203 	assertEqualIntA(a, ARCHIVE_OK, archive_read_support_format_all(a));
204 	assertEqualIntA(a, ARCHIVE_OK,
205 	    archive_read_open_filename(a, refname, 10240));
206 
207 	/* Verify regular file1. */
208 	assertEqualIntA(a, ARCHIVE_OK, archive_read_next_header(a, &ae));
209 	assertEqualInt((AE_IFREG | 0644), archive_entry_mode(ae));
210 	assertEqualString("file1", archive_entry_pathname(ae));
211 	assertEqualInt(1322058763, archive_entry_mtime(ae));
212 	assertEqualInt(2844, archive_entry_size(ae));
213 	assertEqualInt(archive_entry_is_encrypted(ae), 0);
214 	assertEqualIntA(a, archive_read_has_encrypted_entries(a), 0);
215 	assertEqualInt(sizeof(buff), archive_read_data(a, buff, sizeof(buff)));
216 	assertEqualMem(buff, "The libarchive distribution ", 28);
217 
218 	assertEqualInt(1, archive_file_count(a));
219 
220 	/* End of archive. */
221 	assertEqualIntA(a, ARCHIVE_EOF, archive_read_next_header(a, &ae));
222 
223 	/* Verify archive format. */
224 	assertEqualIntA(a, ARCHIVE_FILTER_NONE, archive_filter_code(a, 0));
225 	assertEqualIntA(a, ARCHIVE_FORMAT_7ZIP, archive_format(a));
226 
227 	/* Close the archive. */
228 	assertEqualIntA(a, ARCHIVE_OK, archive_read_close(a));
229 	assertEqualInt(ARCHIVE_OK, archive_read_free(a));
230 }
231 
232 /*
233  * Extract multi files.
234  * The header of the 7z archive files is encoded with LZMA.
235  */
236 static void
test_extract_all_files(const char * refname)237 test_extract_all_files(const char *refname)
238 {
239 	struct archive_entry *ae;
240 	struct archive *a;
241 	char buff[128];
242 
243 	extract_reference_file(refname);
244 	assert((a = archive_read_new()) != NULL);
245 	assertEqualIntA(a, ARCHIVE_OK, archive_read_support_filter_all(a));
246 	assertEqualIntA(a, ARCHIVE_OK, archive_read_support_format_all(a));
247 	assertEqualIntA(a, ARCHIVE_OK,
248 	    archive_read_open_filename(a, refname, 10240));
249 
250 	/* Verify regular file1. */
251 	assertEqualIntA(a, ARCHIVE_OK, archive_read_next_header(a, &ae));
252 	assertEqualInt((AE_IFREG | 0644), archive_entry_mode(ae));
253 	assertEqualString("dir1/file1", archive_entry_pathname(ae));
254 	assertEqualInt(86401, archive_entry_mtime(ae));
255 	assertEqualInt(13, archive_entry_size(ae));
256 	assertEqualInt(archive_entry_is_encrypted(ae), 0);
257 	assertEqualIntA(a, archive_read_has_encrypted_entries(a), 0);
258 	assertEqualInt(13, archive_read_data(a, buff, sizeof(buff)));
259 	assertEqualMem(buff, "aaaaaaaaaaaa\n", 13);
260 
261 	/* Verify regular file2. */
262 	assertEqualIntA(a, ARCHIVE_OK, archive_read_next_header(a, &ae));
263 	assertEqualInt((AE_IFREG | 0644), archive_entry_mode(ae));
264 	assertEqualString("file2", archive_entry_pathname(ae));
265 	assertEqualInt(86401, archive_entry_mtime(ae));
266 	assertEqualInt(26, archive_entry_size(ae));
267 	assertEqualInt(archive_entry_is_encrypted(ae), 0);
268 	assertEqualIntA(a, archive_read_has_encrypted_entries(a), 0);
269 	assertEqualInt(26, archive_read_data(a, buff, sizeof(buff)));
270 	assertEqualMem(buff, "aaaaaaaaaaaa\nbbbbbbbbbbbb\n", 26);
271 
272 	/* Verify regular file3. */
273 	assertEqualIntA(a, ARCHIVE_OK, archive_read_next_header(a, &ae));
274 	assertEqualInt((AE_IFREG | 0644), archive_entry_mode(ae));
275 	assertEqualString("file3", archive_entry_pathname(ae));
276 	assertEqualInt(86401, archive_entry_mtime(ae));
277 	assertEqualInt(39, archive_entry_size(ae));
278 	assertEqualInt(archive_entry_is_encrypted(ae), 0);
279 	assertEqualIntA(a, archive_read_has_encrypted_entries(a), 0);
280 	assertEqualInt(39, archive_read_data(a, buff, sizeof(buff)));
281 	assertEqualMem(buff, "aaaaaaaaaaaa\nbbbbbbbbbbbb\ncccccccccccc\n", 39);
282 
283 	/* Verify regular file4. */
284 	assertEqualIntA(a, ARCHIVE_OK, archive_read_next_header(a, &ae));
285 	assertEqualInt((AE_IFREG | 0644), archive_entry_mode(ae));
286 	assertEqualString("file4", archive_entry_pathname(ae));
287 	assertEqualInt(86401, archive_entry_mtime(ae));
288 	assertEqualInt(52, archive_entry_size(ae));
289 	assertEqualInt(archive_entry_is_encrypted(ae), 0);
290 	assertEqualIntA(a, archive_read_has_encrypted_entries(a), 0);
291 	assertEqualInt(52, archive_read_data(a, buff, sizeof(buff)));
292 	assertEqualMem(buff,
293 	    "aaaaaaaaaaaa\nbbbbbbbbbbbb\ncccccccccccc\ndddddddddddd\n", 52);
294 
295 	/* Verify directory dir1. */
296 	assertEqualIntA(a, ARCHIVE_OK, archive_read_next_header(a, &ae));
297 	assertEqualInt((AE_IFDIR | 0755), archive_entry_mode(ae));
298 	assertEqualString("dir1/", archive_entry_pathname(ae));
299 	assertEqualInt(2764801, archive_entry_mtime(ae));
300 	assertEqualInt(archive_entry_is_encrypted(ae), 0);
301 	assertEqualIntA(a, archive_read_has_encrypted_entries(a), 0);
302 
303 	assertEqualInt(5, archive_file_count(a));
304 
305 	/* End of archive. */
306 	assertEqualIntA(a, ARCHIVE_EOF, archive_read_next_header(a, &ae));
307 
308 	/* Verify archive format. */
309 	assertEqualIntA(a, ARCHIVE_FILTER_NONE, archive_filter_code(a, 0));
310 	assertEqualIntA(a, ARCHIVE_FORMAT_7ZIP, archive_format(a));
311 
312 	/* Close the archive. */
313 	assertEqualIntA(a, ARCHIVE_OK, archive_read_close(a));
314 	assertEqualInt(ARCHIVE_OK, archive_read_free(a));
315 }
316 
317 /*
318  * Extract multi files.
319  * Like test_extract_all_files, but with zstandard compression.
320  */
321 static void
test_extract_all_files_zstd(const char * refname)322 test_extract_all_files_zstd(const char *refname)
323 {
324 	struct archive_entry *ae;
325 	struct archive *a;
326 	char buff[128];
327 
328 	extract_reference_file(refname);
329 	assert((a = archive_read_new()) != NULL);
330 	assertEqualIntA(a, ARCHIVE_OK, archive_read_support_filter_all(a));
331 	assertEqualIntA(a, ARCHIVE_OK, archive_read_support_format_all(a));
332 	assertEqualIntA(a, ARCHIVE_OK,
333 	    archive_read_open_filename(a, refname, 10240));
334 
335 	/* Verify directory dir1. Note that this comes before the dir1/file1 entry in recent versions of 7-Zip. */
336 	assertEqualIntA(a, ARCHIVE_OK, archive_read_next_header(a, &ae));
337 	assertEqualInt((AE_IFDIR | 0755), archive_entry_mode(ae));
338 	assertEqualString("dir1/", archive_entry_pathname(ae));
339 	assertEqualInt(2764801, archive_entry_mtime(ae));
340 	assertEqualInt(archive_entry_is_encrypted(ae), 0);
341 	assertEqualIntA(a, archive_read_has_encrypted_entries(a), 0);
342 
343 	/* Verify regular file1. */
344 	assertEqualIntA(a, ARCHIVE_OK, archive_read_next_header(a, &ae));
345 	assertEqualInt((AE_IFREG | 0644), archive_entry_mode(ae));
346 	assertEqualString("dir1/file1", archive_entry_pathname(ae));
347 	assertEqualInt(86401, archive_entry_mtime(ae));
348 	assertEqualInt(13, archive_entry_size(ae));
349 	assertEqualInt(archive_entry_is_encrypted(ae), 0);
350 	assertEqualIntA(a, archive_read_has_encrypted_entries(a), 0);
351 	assertEqualInt(13, archive_read_data(a, buff, sizeof(buff)));
352 	assertEqualMem(buff, "aaaaaaaaaaaa\n", 13);
353 
354 	/* Verify regular file2. */
355 	assertEqualIntA(a, ARCHIVE_OK, archive_read_next_header(a, &ae));
356 	assertEqualInt((AE_IFREG | 0644), archive_entry_mode(ae));
357 	assertEqualString("file2", archive_entry_pathname(ae));
358 	assertEqualInt(86401, archive_entry_mtime(ae));
359 	assertEqualInt(26, archive_entry_size(ae));
360 	assertEqualInt(archive_entry_is_encrypted(ae), 0);
361 	assertEqualIntA(a, archive_read_has_encrypted_entries(a), 0);
362 	assertEqualInt(26, archive_read_data(a, buff, sizeof(buff)));
363 	assertEqualMem(buff, "aaaaaaaaaaaa\nbbbbbbbbbbbb\n", 26);
364 
365 	/* Verify regular file3. */
366 	assertEqualIntA(a, ARCHIVE_OK, archive_read_next_header(a, &ae));
367 	assertEqualInt((AE_IFREG | 0644), archive_entry_mode(ae));
368 	assertEqualString("file3", archive_entry_pathname(ae));
369 	assertEqualInt(86401, archive_entry_mtime(ae));
370 	assertEqualInt(39, archive_entry_size(ae));
371 	assertEqualInt(archive_entry_is_encrypted(ae), 0);
372 	assertEqualIntA(a, archive_read_has_encrypted_entries(a), 0);
373 	assertEqualInt(39, archive_read_data(a, buff, sizeof(buff)));
374 	assertEqualMem(buff, "aaaaaaaaaaaa\nbbbbbbbbbbbb\ncccccccccccc\n", 39);
375 
376 	/* Verify regular file4. */
377 	assertEqualIntA(a, ARCHIVE_OK, archive_read_next_header(a, &ae));
378 	assertEqualInt((AE_IFREG | 0644), archive_entry_mode(ae));
379 	assertEqualString("file4", archive_entry_pathname(ae));
380 	assertEqualInt(86401, archive_entry_mtime(ae));
381 	assertEqualInt(52, archive_entry_size(ae));
382 	assertEqualInt(archive_entry_is_encrypted(ae), 0);
383 	assertEqualIntA(a, archive_read_has_encrypted_entries(a), 0);
384 	assertEqualInt(52, archive_read_data(a, buff, sizeof(buff)));
385 	assertEqualMem(buff,
386 	    "aaaaaaaaaaaa\nbbbbbbbbbbbb\ncccccccccccc\ndddddddddddd\n", 52);
387 
388 	assertEqualInt(5, archive_file_count(a));
389 
390 	/* End of archive. */
391 	assertEqualIntA(a, ARCHIVE_EOF, archive_read_next_header(a, &ae));
392 
393 	/* Verify archive format. */
394 	assertEqualIntA(a, ARCHIVE_FILTER_NONE, archive_filter_code(a, 0));
395 	assertEqualIntA(a, ARCHIVE_FORMAT_7ZIP, archive_format(a));
396 
397 	/* Close the archive. */
398 	assertEqualIntA(a, ARCHIVE_OK, archive_read_close(a));
399 	assertEqualInt(ARCHIVE_OK, archive_read_free(a));
400 }
401 
402 /*
403  * Extract file from an archives using ZSTD compression with and without BCJ.
404  */
405 static void
test_extract_file_zstd_bcj_nobjc(const char * refname)406 test_extract_file_zstd_bcj_nobjc(const char *refname)
407 {
408 	struct archive_entry *ae;
409 	struct archive *a;
410 	char buff[4096];
411 	uint32_t computed_crc = 0;
412 	uint32_t expected_crc = 0xbd66eebc;
413 
414 	extract_reference_file(refname);
415 	assert((a = archive_read_new()) != NULL);
416 	assertEqualIntA(a, ARCHIVE_OK, archive_read_support_filter_all(a));
417 	assertEqualIntA(a, ARCHIVE_OK, archive_read_support_format_all(a));
418 	assertEqualIntA(a, ARCHIVE_OK,
419 	    archive_read_open_filename(a, refname, 10240));
420 
421 	/* Verify regular file: hw. */
422 	assertEqualIntA(a, ARCHIVE_OK, archive_read_next_header(a, &ae));
423 	assertEqualInt((AE_IFREG | 0775), archive_entry_mode(ae));
424 	assertEqualString("hw", archive_entry_pathname(ae));
425 	assertEqualInt(1685913368, archive_entry_mtime(ae));
426 	assertEqualInt(15952, archive_entry_size(ae));
427 	assertEqualInt(archive_entry_is_encrypted(ae), 0);
428 	assertEqualIntA(a, archive_read_has_encrypted_entries(a), 0);
429 
430 	for (;;) {
431 		la_ssize_t bytes_read = archive_read_data(a, buff, sizeof(buff));
432 		assert(bytes_read >= 0);
433 		if (bytes_read == 0) break;
434 		computed_crc = bitcrc32(computed_crc, buff, bytes_read);
435 	}
436 	assertEqualInt(computed_crc, expected_crc);
437 
438 	assertEqualInt(1, archive_file_count(a));
439 
440 	/* End of archive. */
441 	assertEqualIntA(a, ARCHIVE_EOF, archive_read_next_header(a, &ae));
442 
443 	/* Verify archive format. */
444 	assertEqualIntA(a, ARCHIVE_FILTER_NONE, archive_filter_code(a, 0));
445 	assertEqualIntA(a, ARCHIVE_FORMAT_7ZIP, archive_format(a));
446 
447 	/* Close the archive. */
448 	assertEqualIntA(a, ARCHIVE_OK, archive_read_close(a));
449 	assertEqualInt(ARCHIVE_OK, archive_read_free(a));
450 }
451 
452 /*
453  * Extract last file.
454  * The header of the 7z archive files is encoded with LZMA.
455  */
456 static void
test_extract_last_file(const char * refname)457 test_extract_last_file(const char *refname)
458 {
459 	struct archive_entry *ae;
460 	struct archive *a;
461 	char buff[128];
462 
463 	extract_reference_file(refname);
464 	assert((a = archive_read_new()) != NULL);
465 	assertEqualIntA(a, ARCHIVE_OK, archive_read_support_filter_all(a));
466 	assertEqualIntA(a, ARCHIVE_OK, archive_read_support_format_all(a));
467 	assertEqualIntA(a, ARCHIVE_OK,
468 	    archive_read_open_filename(a, refname, 10240));
469 
470 	/* Verify regular file1. */
471 	assertEqualIntA(a, ARCHIVE_OK, archive_read_next_header(a, &ae));
472 	assertEqualInt((AE_IFREG | 0644), archive_entry_mode(ae));
473 	assertEqualString("dir1/file1", archive_entry_pathname(ae));
474 	assertEqualInt(86401, archive_entry_mtime(ae));
475 	assertEqualInt(13, archive_entry_size(ae));
476 	assertEqualInt(archive_entry_is_encrypted(ae), 0);
477 	assertEqualIntA(a, archive_read_has_encrypted_entries(a), 0);
478 
479 	/* Verify regular file2. */
480 	assertEqualIntA(a, ARCHIVE_OK, archive_read_next_header(a, &ae));
481 	assertEqualInt((AE_IFREG | 0644), archive_entry_mode(ae));
482 	assertEqualString("file2", archive_entry_pathname(ae));
483 	assertEqualInt(86401, archive_entry_mtime(ae));
484 	assertEqualInt(26, archive_entry_size(ae));
485 	assertEqualInt(archive_entry_is_encrypted(ae), 0);
486 	assertEqualIntA(a, archive_read_has_encrypted_entries(a), 0);
487 
488 	/* Verify regular file3. */
489 	assertEqualIntA(a, ARCHIVE_OK, archive_read_next_header(a, &ae));
490 	assertEqualInt((AE_IFREG | 0644), archive_entry_mode(ae));
491 	assertEqualString("file3", archive_entry_pathname(ae));
492 	assertEqualInt(86401, archive_entry_mtime(ae));
493 	assertEqualInt(39, archive_entry_size(ae));
494 	assertEqualInt(archive_entry_is_encrypted(ae), 0);
495 	assertEqualIntA(a, archive_read_has_encrypted_entries(a), 0);
496 
497 	/* Verify regular file4. */
498 	assertEqualIntA(a, ARCHIVE_OK, archive_read_next_header(a, &ae));
499 	assertEqualInt((AE_IFREG | 0644), archive_entry_mode(ae));
500 	assertEqualString("file4", archive_entry_pathname(ae));
501 	assertEqualInt(86401, archive_entry_mtime(ae));
502 	assertEqualInt(52, archive_entry_size(ae));
503 	assertEqualInt(archive_entry_is_encrypted(ae), 0);
504 	assertEqualIntA(a, archive_read_has_encrypted_entries(a), 0);
505 	assertEqualInt(52, archive_read_data(a, buff, sizeof(buff)));
506 	assertEqualMem(buff,
507 	    "aaaaaaaaaaaa\nbbbbbbbbbbbb\ncccccccccccc\ndddddddddddd\n", 52);
508 
509 	/* Verify directory dir1. */
510 	assertEqualIntA(a, ARCHIVE_OK, archive_read_next_header(a, &ae));
511 	assertEqualInt((AE_IFDIR | 0755), archive_entry_mode(ae));
512 	assertEqualString("dir1/", archive_entry_pathname(ae));
513 	assertEqualInt(2764801, archive_entry_mtime(ae));
514 	assertEqualInt(archive_entry_is_encrypted(ae), 0);
515 	assertEqualIntA(a, archive_read_has_encrypted_entries(a), 0);
516 
517 	assertEqualInt(5, archive_file_count(a));
518 
519 	/* End of archive. */
520 	assertEqualIntA(a, ARCHIVE_EOF, archive_read_next_header(a, &ae));
521 
522 	/* Verify archive format. */
523 	assertEqualIntA(a, ARCHIVE_FILTER_NONE, archive_filter_code(a, 0));
524 	assertEqualIntA(a, ARCHIVE_FORMAT_7ZIP, archive_format(a));
525 
526 	/* Close the archive. */
527 	assertEqualIntA(a, ARCHIVE_OK, archive_read_close(a));
528 	assertEqualInt(ARCHIVE_OK, archive_read_free(a));
529 }
530 
531 /*
532  * Extract a mixed archive file which has both LZMA and LZMA2 encoded files.
533  *  LZMA: file1, file2, file3, file4
534  *  LZMA2: zfile1, zfile2, zfile3, zfile4
535  */
536 static void
test_extract_all_files2(const char * refname)537 test_extract_all_files2(const char *refname)
538 {
539 	struct archive_entry *ae;
540 	struct archive *a;
541 	char buff[128];
542 
543 	extract_reference_file(refname);
544 	assert((a = archive_read_new()) != NULL);
545 	assertEqualIntA(a, ARCHIVE_OK, archive_read_support_filter_all(a));
546 	assertEqualIntA(a, ARCHIVE_OK, archive_read_support_format_all(a));
547 	assertEqualIntA(a, ARCHIVE_OK,
548 	    archive_read_open_filename(a, refname, 10240));
549 
550 	/* Verify regular file1. */
551 	assertEqualIntA(a, ARCHIVE_OK, archive_read_next_header(a, &ae));
552 	assertEqualInt((AE_IFREG | 0644), archive_entry_mode(ae));
553 	assertEqualString("dir1/file1", archive_entry_pathname(ae));
554 	assertEqualInt(86401, archive_entry_mtime(ae));
555 	assertEqualInt(13, archive_entry_size(ae));
556 	assertEqualInt(archive_entry_is_encrypted(ae), 0);
557 	assertEqualIntA(a, archive_read_has_encrypted_entries(a), 0);
558 	assertEqualInt(13, archive_read_data(a, buff, sizeof(buff)));
559 	assertEqualMem(buff, "aaaaaaaaaaaa\n", 13);
560 
561 	/* Verify regular file2. */
562 	assertEqualIntA(a, ARCHIVE_OK, archive_read_next_header(a, &ae));
563 	assertEqualInt((AE_IFREG | 0644), archive_entry_mode(ae));
564 	assertEqualString("file2", archive_entry_pathname(ae));
565 	assertEqualInt(86401, archive_entry_mtime(ae));
566 	assertEqualInt(26, archive_entry_size(ae));
567 	assertEqualInt(archive_entry_is_encrypted(ae), 0);
568 	assertEqualIntA(a, archive_read_has_encrypted_entries(a), 0);
569 	assertEqualInt(26, archive_read_data(a, buff, sizeof(buff)));
570 	assertEqualMem(buff, "aaaaaaaaaaaa\nbbbbbbbbbbbb\n", 26);
571 
572 	/* Verify regular file3. */
573 	assertEqualIntA(a, ARCHIVE_OK, archive_read_next_header(a, &ae));
574 	assertEqualInt((AE_IFREG | 0644), archive_entry_mode(ae));
575 	assertEqualString("file3", archive_entry_pathname(ae));
576 	assertEqualInt(86401, archive_entry_mtime(ae));
577 	assertEqualInt(39, archive_entry_size(ae));
578 	assertEqualInt(archive_entry_is_encrypted(ae), 0);
579 	assertEqualIntA(a, archive_read_has_encrypted_entries(a), 0);
580 	assertEqualInt(39, archive_read_data(a, buff, sizeof(buff)));
581 	assertEqualMem(buff, "aaaaaaaaaaaa\nbbbbbbbbbbbb\ncccccccccccc\n", 39);
582 
583 	/* Verify regular file4. */
584 	assertEqualIntA(a, ARCHIVE_OK, archive_read_next_header(a, &ae));
585 	assertEqualInt((AE_IFREG | 0644), archive_entry_mode(ae));
586 	assertEqualString("file4", archive_entry_pathname(ae));
587 	assertEqualInt(86401, archive_entry_mtime(ae));
588 	assertEqualInt(52, archive_entry_size(ae));
589 	assertEqualInt(archive_entry_is_encrypted(ae), 0);
590 	assertEqualIntA(a, archive_read_has_encrypted_entries(a), 0);
591 	assertEqualInt(52, archive_read_data(a, buff, sizeof(buff)));
592 	assertEqualMem(buff,
593 	    "aaaaaaaaaaaa\nbbbbbbbbbbbb\ncccccccccccc\ndddddddddddd\n", 52);
594 
595 	/* Verify regular zfile1. */
596 	assertEqualIntA(a, ARCHIVE_OK, archive_read_next_header(a, &ae));
597 	assertEqualInt((AE_IFREG | 0644), archive_entry_mode(ae));
598 	assertEqualString("dir1/zfile1", archive_entry_pathname(ae));
599 	assertEqualInt(5184001, archive_entry_mtime(ae));
600 	assertEqualInt(13, archive_entry_size(ae));
601 	assertEqualInt(archive_entry_is_encrypted(ae), 0);
602 	assertEqualIntA(a, archive_read_has_encrypted_entries(a), 0);
603 	assertEqualInt(13, archive_read_data(a, buff, sizeof(buff)));
604 	assertEqualMem(buff, "aaaaaaaaaaaa\n", 13);
605 
606 	/* Verify regular zfile2. */
607 	assertEqualIntA(a, ARCHIVE_OK, archive_read_next_header(a, &ae));
608 	assertEqualInt((AE_IFREG | 0644), archive_entry_mode(ae));
609 	assertEqualString("zfile2", archive_entry_pathname(ae));
610 	assertEqualInt(5184001, archive_entry_mtime(ae));
611 	assertEqualInt(26, archive_entry_size(ae));
612 	assertEqualInt(archive_entry_is_encrypted(ae), 0);
613 	assertEqualIntA(a, archive_read_has_encrypted_entries(a), 0);
614 	assertEqualInt(26, archive_read_data(a, buff, sizeof(buff)));
615 	assertEqualMem(buff, "aaaaaaaaaaaa\nbbbbbbbbbbbb\n", 26);
616 
617 	/* Verify regular zfile3. */
618 	assertEqualIntA(a, ARCHIVE_OK, archive_read_next_header(a, &ae));
619 	assertEqualInt((AE_IFREG | 0644), archive_entry_mode(ae));
620 	assertEqualString("zfile3", archive_entry_pathname(ae));
621 	assertEqualInt(5184001, archive_entry_mtime(ae));
622 	assertEqualInt(39, archive_entry_size(ae));
623 	assertEqualInt(archive_entry_is_encrypted(ae), 0);
624 	assertEqualIntA(a, archive_read_has_encrypted_entries(a), 0);
625 	assertEqualInt(39, archive_read_data(a, buff, sizeof(buff)));
626 	assertEqualMem(buff, "aaaaaaaaaaaa\nbbbbbbbbbbbb\ncccccccccccc\n", 39);
627 
628 	/* Verify regular zfile4. */
629 	assertEqualIntA(a, ARCHIVE_OK, archive_read_next_header(a, &ae));
630 	assertEqualInt((AE_IFREG | 0644), archive_entry_mode(ae));
631 	assertEqualString("zfile4", archive_entry_pathname(ae));
632 	assertEqualInt(5184001, archive_entry_mtime(ae));
633 	assertEqualInt(52, archive_entry_size(ae));
634 	assertEqualInt(archive_entry_is_encrypted(ae), 0);
635 	assertEqualIntA(a, archive_read_has_encrypted_entries(a), 0);
636 	assertEqualInt(52, archive_read_data(a, buff, sizeof(buff)));
637 	assertEqualMem(buff,
638 	    "aaaaaaaaaaaa\nbbbbbbbbbbbb\ncccccccccccc\ndddddddddddd\n", 52);
639 
640 	/* Verify directory dir1. */
641 	assertEqualIntA(a, ARCHIVE_OK, archive_read_next_header(a, &ae));
642 	assertEqualInt((AE_IFDIR | 0755), archive_entry_mode(ae));
643 	assertEqualString("dir1/", archive_entry_pathname(ae));
644 	assertEqualInt(2764801, archive_entry_mtime(ae));
645 	assertEqualInt(archive_entry_is_encrypted(ae), 0);
646 	assertEqualIntA(a, archive_read_has_encrypted_entries(a), 0);
647 
648 	assertEqualInt(9, archive_file_count(a));
649 
650 	/* End of archive. */
651 	assertEqualIntA(a, ARCHIVE_EOF, archive_read_next_header(a, &ae));
652 
653 	/* Verify archive format. */
654 	assertEqualIntA(a, ARCHIVE_FILTER_NONE, archive_filter_code(a, 0));
655 	assertEqualIntA(a, ARCHIVE_FORMAT_7ZIP, archive_format(a));
656 
657 	/* Close the archive. */
658 	assertEqualIntA(a, ARCHIVE_OK, archive_read_close(a));
659 	assertEqualInt(ARCHIVE_OK, archive_read_free(a));
660 }
661 
662 /*
663  * Extract a file compressed with DELTA + LZMA[12].
664  */
665 static void
test_delta_lzma(const char * refname)666 test_delta_lzma(const char *refname)
667 {
668 	struct archive_entry *ae;
669 	struct archive *a;
670 	size_t remaining;
671 	ssize_t bytes;
672 	char buff[1024];
673 
674 	extract_reference_file(refname);
675 	assert((a = archive_read_new()) != NULL);
676 	assertEqualIntA(a, ARCHIVE_OK, archive_read_support_filter_all(a));
677 	assertEqualIntA(a, ARCHIVE_OK, archive_read_support_format_all(a));
678 	assertEqualIntA(a, ARCHIVE_OK,
679 	    archive_read_open_filename(a, refname, 10240));
680 
681 	/* Verify regular file1. */
682 	assertEqualIntA(a, ARCHIVE_OK, archive_read_next_header(a, &ae));
683 	assertEqualInt((AE_IFREG | 0644), archive_entry_mode(ae));
684 	assertEqualString("file1", archive_entry_pathname(ae));
685 	assertEqualInt(172802, archive_entry_mtime(ae));
686 	assertEqualInt(27627, archive_entry_size(ae));
687 	assertEqualInt(archive_entry_is_encrypted(ae), 0);
688 	assertEqualIntA(a, archive_read_has_encrypted_entries(a), 0);
689 	remaining = (size_t)archive_entry_size(ae);
690 	while (remaining) {
691 		if (remaining < sizeof(buff))
692 			assertEqualInt(remaining,
693 			    bytes = archive_read_data(a, buff, sizeof(buff)));
694 		else
695 			assertEqualInt(sizeof(buff),
696 			    bytes = archive_read_data(a, buff, sizeof(buff)));
697 		if (bytes > 0)
698 			remaining -= bytes;
699 		else
700 			break;
701 	}
702 	assertEqualInt(0, remaining);
703 
704 	assertEqualInt(1, archive_file_count(a));
705 
706 	/* End of archive. */
707 	assertEqualIntA(a, ARCHIVE_EOF, archive_read_next_header(a, &ae));
708 
709 	/* Verify archive format. */
710 	assertEqualIntA(a, ARCHIVE_FILTER_NONE, archive_filter_code(a, 0));
711 	assertEqualIntA(a, ARCHIVE_FORMAT_7ZIP, archive_format(a));
712 
713 	/* Close the archive. */
714 	assertEqualIntA(a, ARCHIVE_OK, archive_read_close(a));
715 	assertEqualInt(ARCHIVE_OK, archive_read_free(a));
716 }
717 
718 /*
719  * Extract a file compressed with BCJ + LZMA2.
720  */
721 static void
test_bcj(const char * refname)722 test_bcj(const char *refname)
723 {
724 	struct archive_entry *ae;
725 	struct archive *a;
726 	size_t remaining;
727 	ssize_t bytes;
728 	char buff[1024];
729 
730 	extract_reference_file(refname);
731 	assert((a = archive_read_new()) != NULL);
732 	assertEqualIntA(a, ARCHIVE_OK, archive_read_support_filter_all(a));
733 	assertEqualIntA(a, ARCHIVE_OK, archive_read_support_format_all(a));
734 	assertEqualIntA(a, ARCHIVE_OK,
735 	    archive_read_open_filename(a, refname, 10240));
736 
737 	/* Verify regular x86exe. */
738 	assertEqualIntA(a, ARCHIVE_OK, archive_read_next_header(a, &ae));
739 	assertEqualInt((AE_IFREG | 0444), archive_entry_mode(ae) & ~0111);
740 	assertEqualString("x86exe", archive_entry_pathname(ae));
741 	assertEqualInt(172802, archive_entry_mtime(ae));
742 	assertEqualInt(27328, archive_entry_size(ae));
743 	assertEqualInt(archive_entry_is_encrypted(ae), 0);
744 	assertEqualIntA(a, archive_read_has_encrypted_entries(a), 0);
745 	remaining = (size_t)archive_entry_size(ae);
746 	while (remaining) {
747 		if (remaining < sizeof(buff))
748 			assertEqualInt(remaining,
749 			    bytes = archive_read_data(a, buff, sizeof(buff)));
750 		else
751 			assertEqualInt(sizeof(buff),
752 			    bytes = archive_read_data(a, buff, sizeof(buff)));
753 		if (bytes > 0)
754 			remaining -= bytes;
755 		else
756 			break;
757 	}
758 	assertEqualInt(0, remaining);
759 
760 	assertEqualInt(1, archive_file_count(a));
761 
762 	/* End of archive. */
763 	assertEqualIntA(a, ARCHIVE_EOF, archive_read_next_header(a, &ae));
764 
765 	/* Verify archive format. */
766 	assertEqualIntA(a, ARCHIVE_FILTER_NONE, archive_filter_code(a, 0));
767 	assertEqualIntA(a, ARCHIVE_FORMAT_7ZIP, archive_format(a));
768 
769 	/* Close the archive. */
770 	assertEqualIntA(a, ARCHIVE_OK, archive_read_close(a));
771 	assertEqualInt(ARCHIVE_OK, archive_read_free(a));
772 }
773 
774 /*
775  * Extract a file compressed with PPMd.
776  */
777 static void
test_ppmd(void)778 test_ppmd(void)
779 {
780 	const char *refname = "test_read_format_7zip_ppmd.7z";
781 	struct archive_entry *ae;
782 	struct archive *a;
783 	size_t remaining;
784 	ssize_t bytes;
785 	char buff[1024];
786 
787 	extract_reference_file(refname);
788 	assert((a = archive_read_new()) != NULL);
789 	assertEqualIntA(a, ARCHIVE_OK, archive_read_support_filter_all(a));
790 	assertEqualIntA(a, ARCHIVE_OK, archive_read_support_format_all(a));
791 	assertEqualIntA(a, ARCHIVE_OK,
792 	    archive_read_open_filename(a, refname, 10240));
793 
794 	/* Verify regular file1. */
795 	assertEqualIntA(a, ARCHIVE_OK, archive_read_next_header(a, &ae));
796 	assertEqualInt((AE_IFREG | 0644), archive_entry_mode(ae));
797 	assertEqualString("ppmd_test.txt", archive_entry_pathname(ae));
798 	assertEqualInt(1322464589, archive_entry_mtime(ae));
799 	assertEqualInt(102400, archive_entry_size(ae));
800 	assertEqualInt(archive_entry_is_encrypted(ae), 0);
801 	assertEqualIntA(a, archive_read_has_encrypted_entries(a), 0);
802 	remaining = (size_t)archive_entry_size(ae);
803 	while (remaining) {
804 		if (remaining < sizeof(buff))
805 			assertEqualInt(remaining,
806 			    bytes = archive_read_data(a, buff, sizeof(buff)));
807 		else
808 			assertEqualInt(sizeof(buff),
809 			    bytes = archive_read_data(a, buff, sizeof(buff)));
810 		if (bytes > 0)
811 			remaining -= bytes;
812 		else
813 			break;
814 	}
815 	assertEqualInt(0, remaining);
816 
817 	assertEqualInt(1, archive_file_count(a));
818 
819 	/* End of archive. */
820 	assertEqualIntA(a, ARCHIVE_EOF, archive_read_next_header(a, &ae));
821 
822 	/* Verify archive format. */
823 	assertEqualIntA(a, ARCHIVE_FILTER_NONE, archive_filter_code(a, 0));
824 	assertEqualIntA(a, ARCHIVE_FORMAT_7ZIP, archive_format(a));
825 
826 	/* Close the archive. */
827 	assertEqualIntA(a, ARCHIVE_OK, archive_read_close(a));
828 	assertEqualInt(ARCHIVE_OK, archive_read_free(a));
829 }
830 
831 static void
test_symname(void)832 test_symname(void)
833 {
834 	const char *refname = "test_read_format_7zip_symbolic_name.7z";
835 	struct archive_entry *ae;
836 	struct archive *a;
837 	char buff[128];
838 
839 	extract_reference_file(refname);
840 	assert((a = archive_read_new()) != NULL);
841 	assertEqualIntA(a, ARCHIVE_OK, archive_read_support_filter_all(a));
842 	assertEqualIntA(a, ARCHIVE_OK, archive_read_support_format_all(a));
843 	assertEqualIntA(a, ARCHIVE_OK,
844 	    archive_read_open_filename(a, refname, 10240));
845 
846 	/* Verify regular file1. */
847 	assertEqualIntA(a, ARCHIVE_OK, archive_read_next_header(a, &ae));
848 	assertEqualInt((AE_IFREG | 0644), archive_entry_mode(ae));
849 	assertEqualString("file1", archive_entry_pathname(ae));
850 	assertEqualInt(86401, archive_entry_mtime(ae));
851 	assertEqualInt(32, archive_entry_size(ae));
852 	assertEqualInt(archive_entry_is_encrypted(ae), 0);
853 	assertEqualIntA(a, archive_read_has_encrypted_entries(a), 0);
854 	assertEqualInt(32, archive_read_data(a, buff, sizeof(buff)));
855 	assertEqualMem(buff, "hellohellohello\nhellohellohello\n", 32);
856 
857 	/* Verify symbolic-link symlinkfile. */
858 	assertEqualIntA(a, ARCHIVE_OK, archive_read_next_header(a, &ae));
859 	assertEqualInt((AE_IFLNK | 0755), archive_entry_mode(ae));
860 	assertEqualString("symlinkfile", archive_entry_pathname(ae));
861 	assertEqualString("file1", archive_entry_symlink(ae));
862 	assertEqualInt(86401, archive_entry_mtime(ae));
863 	assertEqualInt(archive_entry_is_encrypted(ae), 0);
864 	assertEqualIntA(a, archive_read_has_encrypted_entries(a), 0);
865 
866 	assertEqualInt(2, archive_file_count(a));
867 
868 	/* End of archive. */
869 	assertEqualIntA(a, ARCHIVE_EOF, archive_read_next_header(a, &ae));
870 
871 	/* Verify archive format. */
872 	assertEqualIntA(a, ARCHIVE_FILTER_NONE, archive_filter_code(a, 0));
873 	assertEqualIntA(a, ARCHIVE_FORMAT_7ZIP, archive_format(a));
874 
875 	/* Close the archive. */
876 	assertEqualIntA(a, ARCHIVE_OK, archive_read_close(a));
877 	assertEqualInt(ARCHIVE_OK, archive_read_free(a));
878 }
879 
880 
DEFINE_TEST(test_read_format_7zip)881 DEFINE_TEST(test_read_format_7zip)
882 {
883 	struct archive *a;
884 
885 	assert((a = archive_read_new()) != NULL);
886 
887 	/* Extracting with liblzma */
888 	if (ARCHIVE_OK != archive_read_support_filter_xz(a)) {
889 		skipping("7zip:lzma decoding is not supported on this "
890 		    "platform");
891 	} else {
892 		test_symname();
893 		test_extract_all_files("test_read_format_7zip_copy_2.7z");
894 		test_extract_last_file("test_read_format_7zip_copy_2.7z");
895 		test_extract_all_files2("test_read_format_7zip_lzma1_lzma2.7z");
896 		test_bcj("test_read_format_7zip_bcj2_copy_lzma.7z");
897 	}
898 	assertEqualInt(ARCHIVE_OK, archive_read_free(a));
899 }
900 
DEFINE_TEST(test_read_format_7zip_bzip2)901 DEFINE_TEST(test_read_format_7zip_bzip2)
902 {
903 	struct archive *a;
904 
905 	assert((a = archive_read_new()) != NULL);
906 
907 	/* Extracting with libbzip2 */
908 	if (ARCHIVE_OK != archive_read_support_filter_bzip2(a)) {
909 		skipping("7zip:bzip2 decoding is not supported on this platform");
910 	} else {
911 		test_plain_header("test_read_format_7zip_bzip2.7z");
912 		test_bcj("test_read_format_7zip_bcj_bzip2.7z");
913 		test_bcj("test_read_format_7zip_bcj2_bzip2.7z");
914 	}
915 
916 	assertEqualInt(ARCHIVE_OK, archive_read_free(a));
917 }
918 
DEFINE_TEST(test_read_format_7zip_from_fd)919 DEFINE_TEST(test_read_format_7zip_from_fd)
920 {
921 	test_copy(1);/* read a 7zip file from a file descriptor. */
922 }
923 
DEFINE_TEST(test_read_format_7zip_copy)924 DEFINE_TEST(test_read_format_7zip_copy)
925 {
926 	test_copy(0);
927 	test_bcj("test_read_format_7zip_bcj_copy.7z");
928 	test_bcj("test_read_format_7zip_bcj2_copy_1.7z");
929 	test_bcj("test_read_format_7zip_bcj2_copy_2.7z");
930 }
931 
DEFINE_TEST(test_read_format_7zip_deflate)932 DEFINE_TEST(test_read_format_7zip_deflate)
933 {
934 	struct archive *a;
935 
936 	assert((a = archive_read_new()) != NULL);
937 
938 	/* Extracting with libz */
939 	if (ARCHIVE_OK != archive_read_support_filter_gzip(a)) {
940 		skipping(
941 		    "7zip:deflate decoding is not supported on this platform");
942 	} else {
943 		test_plain_header("test_read_format_7zip_deflate.7z");
944 		test_bcj("test_read_format_7zip_bcj_deflate.7z");
945 		test_bcj("test_read_format_7zip_bcj2_deflate.7z");
946 	}
947 
948 	assertEqualInt(ARCHIVE_OK, archive_read_free(a));
949 }
950 
DEFINE_TEST(test_read_format_7zip_zstd)951 DEFINE_TEST(test_read_format_7zip_zstd)
952 {
953 	struct archive *a;
954 
955 	assert((a = archive_read_new()) != NULL);
956 
957 	/* Extracting with libzstd */
958 	if (ARCHIVE_OK != archive_read_support_filter_zstd(a)) {
959 		skipping(
960 		    "7zip:zstd decoding is not supported on this platform");
961 	} else if (ARCHIVE_OK != archive_read_support_filter_xz(a)) {
962 		// The directory header entries in the test file uses lzma.
963 		skipping(
964 		    "7zip:lzma decoding is not supported on this platform");
965 	} else {
966 		test_extract_all_files_zstd("test_read_format_7zip_zstd.7z");
967 	}
968 
969 	assertEqualInt(ARCHIVE_OK, archive_read_free(a));
970 }
971 
DEFINE_TEST(test_read_format_7zip_zstd_solid)972 DEFINE_TEST(test_read_format_7zip_zstd_solid)
973 {
974 	struct archive *a;
975 
976 	assert((a = archive_read_new()) != NULL);
977 
978 	/* Extracting with libzstd */
979 	if (ARCHIVE_OK != archive_read_support_filter_zstd(a)) {
980 		skipping(
981 		    "7zip:zstd decoding is not supported on this platform");
982 	} else if (ARCHIVE_OK != archive_read_support_filter_xz(a)) {
983 		// The directory header entries in the test file uses lzma.
984 		skipping(
985 		    "7zip:lzma decoding is not supported on this platform");
986 	} else {
987 		test_extract_all_files_zstd("test_read_format_7zip_solid_zstd.7z");
988 	}
989 
990 	assertEqualInt(ARCHIVE_OK, archive_read_free(a));
991 }
992 
DEFINE_TEST(test_read_format_7zip_zstd_bcj)993 DEFINE_TEST(test_read_format_7zip_zstd_bcj)
994 {
995 	struct archive *a;
996 
997 	assert((a = archive_read_new()) != NULL);
998 
999 	/* Extracting with libzstd */
1000 	if (ARCHIVE_OK != archive_read_support_filter_zstd(a)) {
1001 		skipping(
1002 		    "7zip:zstd decoding is not supported on this platform");
1003 	} else {
1004 		test_extract_file_zstd_bcj_nobjc("test_read_format_7zip_zstd_bcj.7z");
1005 	}
1006 
1007 	assertEqualInt(ARCHIVE_OK, archive_read_free(a));
1008 }
1009 
DEFINE_TEST(test_read_format_7zip_zstd_nobcj)1010 DEFINE_TEST(test_read_format_7zip_zstd_nobcj)
1011 {
1012 	struct archive *a;
1013 
1014 	assert((a = archive_read_new()) != NULL);
1015 
1016 	/* Extracting with libzstd */
1017 	if (ARCHIVE_OK != archive_read_support_filter_zstd(a)) {
1018 		skipping(
1019 		    "7zip:zstd decoding is not supported on this platform");
1020 	} else {
1021 		test_extract_file_zstd_bcj_nobjc("test_read_format_7zip_zstd_nobcj.7z");
1022 	}
1023 
1024 	assertEqualInt(ARCHIVE_OK, archive_read_free(a));
1025 }
1026 
DEFINE_TEST(test_read_format_7zip_empty)1027 DEFINE_TEST(test_read_format_7zip_empty)
1028 {
1029 	test_empty_archive();
1030 	test_empty_file();
1031 }
1032 
DEFINE_TEST(test_read_format_7zip_lzma1)1033 DEFINE_TEST(test_read_format_7zip_lzma1)
1034 {
1035 	struct archive *a;
1036 
1037 	assert((a = archive_read_new()) != NULL);
1038 
1039 	/* Extracting with liblzma */
1040 	if (ARCHIVE_OK != archive_read_support_filter_xz(a)) {
1041 		skipping("7zip:lzma decoding is not supported on this "
1042 		    "platform");
1043 	} else {
1044 		test_plain_header("test_read_format_7zip_lzma1.7z");
1045 		test_extract_all_files("test_read_format_7zip_lzma1_2.7z");
1046 		test_extract_last_file("test_read_format_7zip_lzma1_2.7z");
1047 		test_bcj("test_read_format_7zip_bcj_lzma1.7z");
1048 		test_bcj("test_read_format_7zip_bcj2_lzma1_1.7z");
1049 		test_bcj("test_read_format_7zip_bcj2_lzma1_2.7z");
1050 		test_delta_lzma("test_read_format_7zip_delta_lzma1.7z");
1051 		test_delta_lzma("test_read_format_7zip_delta4_lzma1.7z");
1052 	}
1053 	assertEqualInt(ARCHIVE_OK, archive_read_free(a));
1054 }
1055 
DEFINE_TEST(test_read_format_7zip_lzma2)1056 DEFINE_TEST(test_read_format_7zip_lzma2)
1057 {
1058 	struct archive *a;
1059 
1060 	assert((a = archive_read_new()) != NULL);
1061 
1062 	/* Extracting with liblzma */
1063 	if (ARCHIVE_OK != archive_read_support_filter_xz(a)) {
1064 		skipping("7zip:lzma decoding is not supported on this "
1065 		    "platform");
1066 	} else {
1067 		test_plain_header("test_read_format_7zip_lzma2.7z");
1068 		test_bcj("test_read_format_7zip_bcj_lzma2.7z");
1069 		test_bcj("test_read_format_7zip_bcj2_lzma2_1.7z");
1070 		test_bcj("test_read_format_7zip_bcj2_lzma2_2.7z");
1071 		test_delta_lzma("test_read_format_7zip_delta_lzma2.7z");
1072 		test_delta_lzma("test_read_format_7zip_delta4_lzma2.7z");
1073 	}
1074 	assertEqualInt(ARCHIVE_OK, archive_read_free(a));
1075 }
1076 
1077 static void
test_arm_filter(const char * refname)1078 test_arm_filter(const char *refname)
1079 {
1080 	struct archive *a;
1081 	struct archive_entry *ae;
1082 	char buff[7804];
1083 	uint32_t computed_crc = 0;
1084 	uint32_t expected_crc = 0x355ec4e1;
1085 
1086 	assert((a = archive_read_new()) != NULL);
1087 
1088 	extract_reference_file(refname);
1089 
1090 	assertEqualInt(ARCHIVE_OK, archive_read_free(a));
1091 	assert((a = archive_read_new()) != NULL);
1092 	assertEqualIntA(a, ARCHIVE_OK, archive_read_support_filter_all(a));
1093 	assertEqualIntA(a, ARCHIVE_OK, archive_read_support_format_all(a));
1094 
1095 	assertEqualIntA(a, ARCHIVE_OK,
1096 		archive_read_open_filename(a, refname, 10240));
1097 
1098 	assertEqualIntA(a, ARCHIVE_OK, archive_read_next_header(a, &ae));
1099 	assertEqualInt((AE_IFREG | 0755), archive_entry_mode(ae));
1100 	assertEqualString("hw-gnueabihf", archive_entry_pathname(ae));
1101 	assertEqualInt(sizeof(buff), archive_entry_size(ae));
1102 	assertEqualInt(sizeof(buff), archive_read_data(a, buff, sizeof(buff)));
1103 	computed_crc = bitcrc32(computed_crc, buff, sizeof(buff));
1104 	assertEqualInt(computed_crc, expected_crc);
1105 
1106 	assertEqualInt(1, archive_file_count(a));
1107 
1108 	assertEqualIntA(a, ARCHIVE_EOF, archive_read_next_header(a, &ae));
1109 
1110 	assertEqualIntA(a, ARCHIVE_OK, archive_read_close(a));
1111 	assertEqualInt(ARCHIVE_OK, archive_read_free(a));
1112 }
1113 
DEFINE_TEST(test_read_format_7zip_zstd_arm)1114 DEFINE_TEST(test_read_format_7zip_zstd_arm)
1115 {
1116 	struct archive *a;
1117 
1118 	assert((a = archive_read_new()) != NULL);
1119 
1120 	if (ARCHIVE_OK != archive_read_support_filter_zstd(a)) {
1121 		skipping(
1122 		    "7zip:zstd decoding is not supported on this platform");
1123 	} else {
1124 		test_arm_filter("test_read_format_7zip_zstd_arm.7z");
1125 	}
1126 
1127 	assertEqualInt(ARCHIVE_OK, archive_read_free(a));
1128 }
1129 
DEFINE_TEST(test_read_format_7zip_lzma2_arm)1130 DEFINE_TEST(test_read_format_7zip_lzma2_arm)
1131 {
1132 	struct archive *a;
1133 
1134 	assert((a = archive_read_new()) != NULL);
1135 
1136 	if (ARCHIVE_OK != archive_read_support_filter_lzma(a)) {
1137 		skipping(
1138 		    "7zip:lzma decoding is not supported on this platform");
1139 	} else {
1140 		test_arm_filter("test_read_format_7zip_lzma2_arm.7z");
1141 	}
1142 
1143 	assertEqualInt(ARCHIVE_OK, archive_read_free(a));
1144 }
1145 
DEFINE_TEST(test_read_format_7zip_ppmd)1146 DEFINE_TEST(test_read_format_7zip_ppmd)
1147 {
1148 	test_ppmd();
1149 }
1150 
1151 static void
test_arm64_filter(const char * refname)1152 test_arm64_filter(const char *refname)
1153 {
1154 	struct archive *a;
1155 	struct archive_entry *ae;
1156 	char buff[70368];
1157 	uint32_t computed_crc = 0;
1158 	uint32_t expected_crc = 0xde97d594;
1159 
1160 	assert((a = archive_read_new()) != NULL);
1161 
1162 	extract_reference_file(refname);
1163 
1164 	assertEqualInt(ARCHIVE_OK, archive_read_free(a));
1165 	assert((a = archive_read_new()) != NULL);
1166 	assertEqualIntA(a, ARCHIVE_OK, archive_read_support_filter_all(a));
1167 	assertEqualIntA(a, ARCHIVE_OK, archive_read_support_format_all(a));
1168 
1169 	assertEqualIntA(a, ARCHIVE_OK,
1170 		archive_read_open_filename(a, refname, 10240));
1171 
1172 	assertEqualIntA(a, ARCHIVE_OK, archive_read_next_header(a, &ae));
1173 	assertEqualInt((AE_IFREG | 0775), archive_entry_mode(ae));
1174 	assertEqualString("hw-arm64", archive_entry_pathname(ae));
1175 	assertEqualInt(sizeof(buff), archive_entry_size(ae));
1176 	assertEqualInt(sizeof(buff), archive_read_data(a, buff, sizeof(buff)));
1177 	computed_crc = bitcrc32(computed_crc, buff, sizeof(buff));
1178 	assertEqualInt(computed_crc, expected_crc);
1179 
1180 	assertEqualInt(1, archive_file_count(a));
1181 
1182 	assertEqualIntA(a, ARCHIVE_EOF, archive_read_next_header(a, &ae));
1183 
1184 	assertEqualIntA(a, ARCHIVE_OK, archive_read_close(a));
1185 	assertEqualInt(ARCHIVE_OK, archive_read_free(a));
1186 }
1187 
DEFINE_TEST(test_read_format_7zip_lzma2_arm64)1188 DEFINE_TEST(test_read_format_7zip_lzma2_arm64)
1189 {
1190 #ifdef LZMA_FILTER_ARM64
1191 	struct archive *a;
1192 
1193 	assert((a = archive_read_new()) != NULL);
1194 
1195 	if (ARCHIVE_OK != archive_read_support_filter_lzma(a)) {
1196 		skipping(
1197 		    "7zip:lzma decoding is not supported on this platform");
1198 	} else {
1199 		test_arm64_filter("test_read_format_7zip_lzma2_arm64.7z");
1200 	}
1201 
1202 	assertEqualInt(ARCHIVE_OK, archive_read_free(a));
1203 #else
1204 	skipping("This version of liblzma does not support LZMA_FILTER_ARM64");
1205 #endif
1206 }
1207 
DEFINE_TEST(test_read_format_7zip_deflate_arm64)1208 DEFINE_TEST(test_read_format_7zip_deflate_arm64)
1209 {
1210 	struct archive *a;
1211 
1212 	assert((a = archive_read_new()) != NULL);
1213 
1214 	if (ARCHIVE_OK != archive_read_support_filter_gzip(a)) {
1215 		skipping(
1216 		    "7zip:deflate decoding is not supported on this platform");
1217 	} else {
1218 		test_arm64_filter("test_read_format_7zip_deflate_arm64.7z");
1219 	}
1220 
1221 	assertEqualInt(ARCHIVE_OK, archive_read_free(a));
1222 }
1223 
DEFINE_TEST(test_read_format_7zip_win_attrib)1224 DEFINE_TEST(test_read_format_7zip_win_attrib)
1225 {
1226 	struct archive *a;
1227 
1228 	assert((a = archive_read_new()) != NULL);
1229 
1230 	if (ARCHIVE_OK != archive_read_support_filter_lzma(a)) {
1231 		skipping(
1232 		    "7zip:lzma decoding is not supported on this platform");
1233 		assertEqualInt(ARCHIVE_OK, archive_read_free(a));
1234 		return;
1235 	}
1236 
1237 	assertEqualIntA(a, ARCHIVE_OK, archive_read_support_format_all(a));
1238 
1239 	// This archive has four files and four directories:
1240 	// * hidden directory
1241 	// * readonly directory
1242 	// * regular directory
1243 	// * system directory
1244 	// * regular "archive" file
1245 	// * hidden file
1246 	// * readonly file
1247 	// * system file
1248 	const char *refname = "test_read_format_7zip_win_attrib.7z";
1249 	extract_reference_file(refname);
1250 
1251 	assertEqualIntA(a, ARCHIVE_OK,
1252 		archive_read_open_filename(a, refname, 10240));
1253 
1254 	struct archive_entry *ae;
1255 
1256 	assertEqualIntA(a, ARCHIVE_OK, archive_read_next_header(a, &ae));
1257 	assertEqualString("hidden_dir/", archive_entry_pathname(ae));
1258 	assertEqualInt((AE_IFDIR | 0755), archive_entry_mode(ae));
1259 	assertEqualString("hidden", archive_entry_fflags_text(ae));
1260 
1261 	assertEqualIntA(a, ARCHIVE_OK, archive_read_next_header(a, &ae));
1262 	assertEqualString("readonly_dir/", archive_entry_pathname(ae));
1263 	assertEqualInt((AE_IFDIR | 0555), archive_entry_mode(ae));
1264 	assertEqualString("rdonly", archive_entry_fflags_text(ae));
1265 
1266 	assertEqualIntA(a, ARCHIVE_OK, archive_read_next_header(a, &ae));
1267 	assertEqualString("regular_dir/", archive_entry_pathname(ae));
1268 	assertEqualInt((AE_IFDIR | 0755), archive_entry_mode(ae));
1269 	assertEqualString(NULL, archive_entry_fflags_text(ae));
1270 
1271 	assertEqualIntA(a, ARCHIVE_OK, archive_read_next_header(a, &ae));
1272 	assertEqualString("system_dir/", archive_entry_pathname(ae));
1273 	assertEqualInt((AE_IFDIR | 0755), archive_entry_mode(ae));
1274 	assertEqualString("system", archive_entry_fflags_text(ae));
1275 
1276 	assertEqualIntA(a, ARCHIVE_OK, archive_read_next_header(a, &ae));
1277 	assertEqualString("archive_file.txt", archive_entry_pathname(ae));
1278 	assertEqualInt((AE_IFREG | 0644), archive_entry_mode(ae));
1279 	assertEqualString(NULL, archive_entry_fflags_text(ae));
1280 
1281 	assertEqualIntA(a, ARCHIVE_OK, archive_read_next_header(a, &ae));
1282 	assertEqualString("hidden_file.txt", archive_entry_pathname(ae));
1283 	assertEqualInt((AE_IFREG | 0644), archive_entry_mode(ae));
1284 	assertEqualString("hidden", archive_entry_fflags_text(ae));
1285 
1286 	assertEqualIntA(a, ARCHIVE_OK, archive_read_next_header(a, &ae));
1287 	assertEqualString("readonly_file.txt", archive_entry_pathname(ae));
1288 	assertEqualInt((AE_IFREG | 0444), archive_entry_mode(ae));
1289 	assertEqualString("rdonly", archive_entry_fflags_text(ae));
1290 
1291 	assertEqualIntA(a, ARCHIVE_OK, archive_read_next_header(a, &ae));
1292 	assertEqualString("system_file.txt", archive_entry_pathname(ae));
1293 	assertEqualInt((AE_IFREG | 0644), archive_entry_mode(ae));
1294 	assertEqualString("system", archive_entry_fflags_text(ae));
1295 
1296 
1297 	assertEqualInt(ARCHIVE_OK, archive_read_free(a));
1298 }
1299 
DEFINE_TEST(test_read_format_7zip_sfx_pe)1300 DEFINE_TEST(test_read_format_7zip_sfx_pe)
1301 {
1302 	/*
1303 	 * This is a regular 7z SFX PE file
1304 	 * created by 7z tool v22.01 on Windows 64-bit
1305 	 */
1306 	struct archive *a;
1307 	struct archive_entry *ae;
1308 	int bs = 10240;
1309 	char buff[32];
1310 	const char reffile[] = "test_read_format_7zip_sfx_pe.exe";
1311 	const char test_txt[] = "123";
1312 	int size = sizeof(test_txt) - 1;
1313 
1314 	assert((a = archive_read_new()) != NULL);
1315 
1316 	if (ARCHIVE_OK != archive_read_support_filter_lzma(a)) {
1317 		skipping(
1318 		    "7zip:lzma decoding is not supported on this platform");
1319 	} else {
1320 		extract_reference_file(reffile);
1321 		assertA(0 == archive_read_support_filter_all(a));
1322 		assertA(0 == archive_read_support_format_all(a));
1323 		assertA(0 == archive_read_open_filename(a, reffile, bs));
1324 
1325 		assertA(0 == archive_read_next_header(a, &ae));
1326 		assertEqualString("test.txt.txt", archive_entry_pathname(ae));
1327 
1328 		assertA(size == archive_read_data(a, buff, size));
1329 		assertEqualMem(buff, test_txt, size);
1330 
1331 		assertEqualIntA(a, ARCHIVE_OK, archive_read_close(a));
1332 	}
1333 
1334 	assertEqualInt(ARCHIVE_OK, archive_read_free(a));
1335 }
1336 
DEFINE_TEST(test_read_format_7zip_sfx_modified_pe)1337 DEFINE_TEST(test_read_format_7zip_sfx_modified_pe)
1338 {
1339 	/*
1340 	 * This test simulates a modified 7z SFX PE
1341 	 * the compressed data in the SFX file is still stored as PE overlay
1342 	 * but the decompressor code is replaced
1343 	 */
1344 	struct archive *a;
1345 	struct archive_entry *ae;
1346 	int bs = 10240;
1347 	char buff[32];
1348 	const char reffile[] = "test_read_format_7zip_sfx_modified_pe.exe";
1349 	const char test_txt[] = "123";
1350 	int size = sizeof(test_txt) - 1;
1351 
1352 	assert((a = archive_read_new()) != NULL);
1353 
1354 	if (ARCHIVE_OK != archive_read_support_filter_lzma(a)) {
1355 		skipping(
1356 		    "7zip:lzma decoding is not supported on this platform");
1357 	} else {
1358 		extract_reference_file(reffile);
1359 		assertA(0 == archive_read_support_filter_all(a));
1360 		assertA(0 == archive_read_support_format_all(a));
1361 		assertA(0 == archive_read_open_filename(a, reffile, bs));
1362 
1363 		assertA(0 == archive_read_next_header(a, &ae));
1364 		assertEqualString("test.txt.txt", archive_entry_pathname(ae));
1365 
1366 		assertA(size == archive_read_data(a, buff, size));
1367 		assertEqualMem(buff, test_txt, size);
1368 
1369 		assertEqualIntA(a, ARCHIVE_OK, archive_read_close(a));
1370 	}
1371 
1372 	assertEqualInt(ARCHIVE_OK, archive_read_free(a));
1373 }
1374 
DEFINE_TEST(test_read_format_7zip_sfx_elf)1375 DEFINE_TEST(test_read_format_7zip_sfx_elf)
1376 {
1377 	/*
1378 	 * This is a regular 7z SFX ELF file
1379 	 * created by 7z tool v16.02 on Ubuntu
1380 	 */
1381 	struct archive *a;
1382 	struct archive_entry *ae;
1383 	int bs = 10240;
1384 	char buff[32];
1385 	const char reffile[] = "test_read_format_7zip_sfx_elf.elf";
1386 	const char test_txt[] = "123";
1387 	int size = sizeof(test_txt) - 1;
1388 
1389 	assert((a = archive_read_new()) != NULL);
1390 
1391 	if (ARCHIVE_OK != archive_read_support_filter_lzma(a)) {
1392 		skipping(
1393 		    "7zip:lzma decoding is not supported on this platform");
1394 	} else {
1395 		extract_reference_file(reffile);
1396 		assertA(0 == archive_read_support_filter_all(a));
1397 		assertA(0 == archive_read_support_format_all(a));
1398 		assertA(0 == archive_read_open_filename(a, reffile, bs));
1399 
1400 		assertA(0 == archive_read_next_header(a, &ae));
1401 		assertEqualString("test.txt.txt", archive_entry_pathname(ae));
1402 
1403 		assertA(size == archive_read_data(a, buff, size));
1404 		assertEqualMem(buff, test_txt, size);
1405 
1406 		assertEqualIntA(a, ARCHIVE_OK, archive_read_close(a));
1407 	}
1408 
1409 	assertEqualInt(ARCHIVE_OK, archive_read_free(a));
1410 }
1411 
1412 static void
test_sfx_boundary_empty_7z(const char * refname)1413 test_sfx_boundary_empty_7z(const char *refname)
1414 {
1415 	struct archive_entry *ae = NULL;
1416 	struct archive *a;
1417 	const int bs = 10240;
1418 
1419 	extract_reference_file(refname);
1420 
1421 	assert((a = archive_read_new()) != NULL);
1422 	assertEqualIntA(a, ARCHIVE_OK, archive_read_support_filter_all(a));
1423 	assertEqualIntA(a, ARCHIVE_OK, archive_read_support_format_all(a));
1424 	assertEqualIntA(a, ARCHIVE_OK,
1425 	    archive_read_open_filename(a, refname, bs));
1426 	assertEqualIntA(a, ARCHIVE_EOF, archive_read_next_header(a, &ae));
1427 	assertEqualInt(0, archive_file_count(a));
1428 	assertEqualIntA(a, ARCHIVE_FORMAT_7ZIP, archive_format(a));
1429 	assertEqualIntA(a, ARCHIVE_OK, archive_read_close(a));
1430 	assertEqualInt(ARCHIVE_OK, archive_read_free(a));
1431 }
1432 
DEFINE_TEST(test_read_format_7zip_sfx_boundary_pe)1433 DEFINE_TEST(test_read_format_7zip_sfx_boundary_pe)
1434 {
1435 	/*
1436 	 * The embedded empty 7z archive starts at the final exact
1437 	 * 32-byte scan window after the PE overlay offset.
1438 	 */
1439 	test_sfx_boundary_empty_7z(
1440 	    "test_read_format_7zip_sfx_boundary_pe.exe");
1441 }
1442 
DEFINE_TEST(test_read_format_7zip_sfx_boundary_elf)1443 DEFINE_TEST(test_read_format_7zip_sfx_boundary_elf)
1444 {
1445 	/*
1446 	 * The ELF section string table contains "\0.data\0", so
1447 	 * ".data\0" starts at the final valid string-table position.
1448 	 */
1449 	test_sfx_boundary_empty_7z(
1450 	    "test_read_format_7zip_sfx_boundary_elf.elf");
1451 }
1452 
1453 /*
1454  * A truncated ELF 64-bit MSB file.
1455  */
DEFINE_TEST(test_read_format_7zip_sfx_elf64trunc)1456 DEFINE_TEST(test_read_format_7zip_sfx_elf64trunc)
1457 {
1458 	const char *reffile = "test_read_format_7zip_sfx_elf64trunc.elf";
1459 	struct archive_entry *ae;
1460 	struct archive *a;
1461 
1462 	assert((a = archive_read_new()) != NULL);
1463 
1464 	assertEqualIntA(a, ARCHIVE_OK, archive_read_support_filter_all(a));
1465 	assertEqualIntA(a, ARCHIVE_OK, archive_read_support_format_all(a));
1466 
1467 	extract_reference_file(reffile);
1468 	assertEqualIntA(a, ARCHIVE_FATAL,
1469 	    archive_read_open_filename(a, reffile, 10240));
1470 
1471 	assertEqualIntA(a, ARCHIVE_FATAL, archive_read_next_header(a, &ae));
1472 	assertEqualInt(0, archive_file_count(a));
1473 
1474 	assertEqualIntA(a, ARCHIVE_OK, archive_read_close(a));
1475 
1476 	assertEqualInt(ARCHIVE_OK, archive_read_free(a));
1477 }
1478 
DEFINE_TEST(test_read_format_7zip_extract_second)1479 DEFINE_TEST(test_read_format_7zip_extract_second)
1480 {
1481 	struct archive *a;
1482 	char buffer[256];
1483 
1484 	assert((a = archive_read_new()) != NULL);
1485 
1486 	if (ARCHIVE_OK != archive_read_support_filter_lzma(a)) {
1487 		skipping(
1488 		    "7zip:lzma decoding is not supported on this platform");
1489 		assertEqualInt(ARCHIVE_OK, archive_read_free(a));
1490 		return;
1491 	}
1492 
1493 	assertEqualIntA(a, ARCHIVE_OK, archive_read_support_format_all(a));
1494 
1495 	/*
1496 	 * The test archive has two files: first.txt which is a 65,536 file (the
1497 	 * size of the uncompressed buffer), and second.txt which has contents
1498 	 * we will validate. This test ensures we can skip first.txt and still
1499 	 * be able to read the contents of second.txt
1500 	 */
1501 	const char *refname = "test_read_format_7zip_extract_second.7z";
1502 	extract_reference_file(refname);
1503 
1504 	assertEqualIntA(a, ARCHIVE_OK,
1505 		archive_read_open_filename(a, refname, 10240));
1506 
1507 	struct archive_entry *ae;
1508 
1509 	assertEqualIntA(a, ARCHIVE_OK, archive_read_next_header(a, &ae));
1510 	assertEqualString("first.txt", archive_entry_pathname(ae));
1511 
1512 	assertEqualIntA(a, ARCHIVE_OK, archive_read_next_header(a, &ae));
1513 	assertEqualString("second.txt", archive_entry_pathname(ae));
1514 
1515 	assertEqualInt(23, archive_read_data(a, buffer, sizeof(buffer)));
1516 	assertEqualMem("This is from second.txt", buffer, 23);
1517 
1518 	assertEqualInt(ARCHIVE_OK, archive_read_free(a));
1519 }
1520 
1521 #ifdef LZMA_FILTER_RISCV
1522 static void
test_riscv_filter(const char * refname)1523 test_riscv_filter(const char *refname)
1524 {
1525 	struct archive *a;
1526 	struct archive_entry *ae;
1527 	char buff[8488];
1528 	uint32_t computed_crc = 0;
1529 	uint32_t expected_crc = 0xf7ed24e7;
1530 
1531 	assert((a = archive_read_new()) != NULL);
1532 
1533 	extract_reference_file(refname);
1534 
1535 	assertEqualInt(ARCHIVE_OK, archive_read_free(a));
1536 	assert((a = archive_read_new()) != NULL);
1537 	assertEqualIntA(a, ARCHIVE_OK, archive_read_support_filter_all(a));
1538 	assertEqualIntA(a, ARCHIVE_OK, archive_read_support_format_all(a));
1539 
1540 	assertEqualIntA(a, ARCHIVE_OK,
1541 		archive_read_open_filename(a, refname, 10240));
1542 
1543 	assertEqualIntA(a, ARCHIVE_OK, archive_read_next_header(a, &ae));
1544 	assertEqualInt((AE_IFREG | 0775), archive_entry_mode(ae));
1545 	assertEqualString("hw-riscv64", archive_entry_pathname(ae));
1546 	assertEqualIntA(a, sizeof(buff), archive_entry_size(ae));
1547 	assertEqualIntA(a, sizeof(buff), archive_read_data(a, buff, sizeof(buff)));
1548 
1549 	computed_crc = bitcrc32(computed_crc, buff, sizeof(buff));
1550 	assertEqualInt(computed_crc, expected_crc);
1551 
1552 	assertEqualInt(1, archive_file_count(a));
1553 
1554 	assertEqualIntA(a, ARCHIVE_EOF, archive_read_next_header(a, &ae));
1555 
1556 	assertEqualIntA(a, ARCHIVE_OK, archive_read_close(a));
1557 	assertEqualInt(ARCHIVE_OK, archive_read_free(a));
1558 }
1559 #endif
1560 
DEFINE_TEST(test_read_format_7zip_lzma2_riscv)1561 DEFINE_TEST(test_read_format_7zip_lzma2_riscv)
1562 {
1563 #ifdef LZMA_FILTER_RISCV
1564 	struct archive *a;
1565 
1566 	assert((a = archive_read_new()) != NULL);
1567 
1568 	if (ARCHIVE_OK != archive_read_support_filter_lzma(a)) {
1569 		skipping("7zip:lzma decoding is not supported on this platform");
1570 	} else if (!lzma_filter_decoder_is_supported(LZMA_FILTER_RISCV)) {
1571 		skipping("liblzma does not support RISC-V BCJ filter at runtime");
1572 	} else {
1573 		test_riscv_filter("test_read_format_7zip_lzma2_riscv.7z");
1574 	}
1575 	archive_read_free(a);
1576 #else
1577 	skipping("This version of liblzma does not support LZMA_FILTER_RISCV");
1578 #endif
1579 }
1580 
1581 static void
test_sparc_filter(const char * refname)1582 test_sparc_filter(const char *refname)
1583 {
1584 	struct archive *a;
1585 	struct archive_entry *ae;
1586 	size_t expected_entry_size = 1053016;
1587 	char *buff = malloc(expected_entry_size);
1588 	uint32_t computed_crc = 0;
1589 	uint32_t expected_crc = 0x6b5b364d;
1590 
1591 	assert((a = archive_read_new()) != NULL);
1592 
1593 	extract_reference_file(refname);
1594 
1595 	assertEqualInt(ARCHIVE_OK, archive_read_free(a));
1596 	assert((a = archive_read_new()) != NULL);
1597 	assertEqualIntA(a, ARCHIVE_OK, archive_read_support_filter_all(a));
1598 	assertEqualIntA(a, ARCHIVE_OK, archive_read_support_format_all(a));
1599 
1600 	assertEqualIntA(a, ARCHIVE_OK,
1601 		archive_read_open_filename(a, refname, 10240));
1602 
1603 	assertEqualIntA(a, ARCHIVE_OK, archive_read_next_header(a, &ae));
1604 	assertEqualInt((AE_IFREG | 0775), archive_entry_mode(ae));
1605 	assertEqualString("hw-sparc64", archive_entry_pathname(ae));
1606 	assertEqualInt(expected_entry_size, archive_entry_size(ae));
1607 	assertEqualInt(expected_entry_size, archive_read_data(a, buff, expected_entry_size));
1608 
1609 	computed_crc = bitcrc32(computed_crc, buff, expected_entry_size);
1610 	assertEqualInt(computed_crc, expected_crc);
1611 
1612 	assertEqualInt(1, archive_file_count(a));
1613 
1614 	assertEqualIntA(a, ARCHIVE_EOF, archive_read_next_header(a, &ae));
1615 
1616 	assertEqualIntA(a, ARCHIVE_OK, archive_read_close(a));
1617 	assertEqualInt(ARCHIVE_OK, archive_read_free(a));
1618 
1619 	free(buff);
1620 }
1621 
DEFINE_TEST(test_read_format_7zip_lzma2_sparc)1622 DEFINE_TEST(test_read_format_7zip_lzma2_sparc)
1623 {
1624 	struct archive *a;
1625 
1626 	assert((a = archive_read_new()) != NULL);
1627 
1628 	if (ARCHIVE_OK != archive_read_support_filter_lzma(a)) {
1629 		skipping(
1630 		    "7zip:lzma decoding is not supported on this platform");
1631 	} else {
1632 		test_sparc_filter("test_read_format_7zip_lzma2_sparc.7z");
1633 	}
1634 
1635 	assertEqualInt(ARCHIVE_OK, archive_read_free(a));
1636 }
1637 
DEFINE_TEST(test_read_format_7zip_zstd_sparc)1638 DEFINE_TEST(test_read_format_7zip_zstd_sparc)
1639 {
1640 	struct archive *a;
1641 
1642 	assert((a = archive_read_new()) != NULL);
1643 
1644 	if (ARCHIVE_OK != archive_read_support_filter_zstd(a)) {
1645 		skipping(
1646 		    "7zip:zstd decoding is not supported on this platform");
1647 	} else {
1648 		test_sparc_filter("test_read_format_7zip_zstd_sparc.7z");
1649 	}
1650 
1651 	assertEqualInt(ARCHIVE_OK, archive_read_free(a));
1652 }
1653 
1654 static void
test_powerpc_filter(const char * refname)1655 test_powerpc_filter(const char *refname)
1656 {
1657 	struct archive *a;
1658 	struct archive_entry *ae;
1659 	size_t expected_entry_size = 68340;
1660 	char *buff = malloc(expected_entry_size);
1661 	uint32_t computed_crc = 0;
1662 	uint32_t expected_crc = 0x71fb03c9;
1663 
1664 	assert((a = archive_read_new()) != NULL);
1665 
1666 	extract_reference_file(refname);
1667 
1668 	assertEqualInt(ARCHIVE_OK, archive_read_free(a));
1669 	assert((a = archive_read_new()) != NULL);
1670 	assertEqualIntA(a, ARCHIVE_OK, archive_read_support_filter_all(a));
1671 	assertEqualIntA(a, ARCHIVE_OK, archive_read_support_format_all(a));
1672 
1673 	assertEqualIntA(a, ARCHIVE_OK,
1674 		archive_read_open_filename(a, refname, 10240));
1675 
1676 	assertEqualIntA(a, ARCHIVE_OK, archive_read_next_header(a, &ae));
1677 	assertEqualInt((AE_IFREG | 0775), archive_entry_mode(ae));
1678 	assertEqualString("hw-powerpc", archive_entry_pathname(ae));
1679 	assertEqualInt(expected_entry_size, archive_entry_size(ae));
1680 	assertEqualInt(expected_entry_size, archive_read_data(a, buff, expected_entry_size));
1681 
1682 	computed_crc = bitcrc32(computed_crc, buff, expected_entry_size);
1683 	assertEqualInt(computed_crc, expected_crc);
1684 
1685 	assertEqualInt(1, archive_file_count(a));
1686 
1687 	assertEqualIntA(a, ARCHIVE_EOF, archive_read_next_header(a, &ae));
1688 
1689 	assertEqualIntA(a, ARCHIVE_OK, archive_read_close(a));
1690 	assertEqualInt(ARCHIVE_OK, archive_read_free(a));
1691 
1692 	free(buff);
1693 }
1694 
DEFINE_TEST(test_read_format_7zip_deflate_powerpc)1695 DEFINE_TEST(test_read_format_7zip_deflate_powerpc)
1696 {
1697 	struct archive *a;
1698 
1699 	assert((a = archive_read_new()) != NULL);
1700 
1701 	if (ARCHIVE_OK != archive_read_support_filter_gzip(a)) {
1702 		skipping(
1703 		    "7zip:deflate decoding is not supported on this platform");
1704 	} else {
1705 		test_powerpc_filter("test_read_format_7zip_deflate_powerpc.7z");
1706 	}
1707 
1708 	assertEqualInt(ARCHIVE_OK, archive_read_free(a));
1709 }
1710 
DEFINE_TEST(test_read_format_7zip_lzma2_powerpc)1711 DEFINE_TEST(test_read_format_7zip_lzma2_powerpc)
1712 {
1713 	struct archive *a;
1714 
1715 	assert((a = archive_read_new()) != NULL);
1716 
1717 	if (ARCHIVE_OK != archive_read_support_filter_lzma(a)) {
1718 		skipping(
1719 		    "7zip:lzma decoding is not supported on this platform");
1720 	} else {
1721 		test_powerpc_filter("test_read_format_7zip_lzma2_powerpc.7z");
1722 	}
1723 
1724 	assertEqualInt(ARCHIVE_OK, archive_read_free(a));
1725 }
1726