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