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 defined(_WIN32) && !defined(__CYGWIN__)
28 #define close _close
29 #define open _open
30 #endif
31
32 #define __LIBARCHIVE_BUILD
33 #include <archive_crc32.h>
34
35 /*
36 * Extract a non-encoded file.
37 * The header of the 7z archive files is not encoded.
38 */
39 static void
test_copy(int use_open_fd)40 test_copy(int use_open_fd)
41 {
42 const char *refname = "test_read_format_7zip_copy.7z";
43 struct archive_entry *ae;
44 struct archive *a;
45 char buff[128];
46 int fd = -1;
47
48 extract_reference_file(refname);
49 assert((a = archive_read_new()) != NULL);
50 assertEqualIntA(a, ARCHIVE_OK, archive_read_support_filter_all(a));
51 assertEqualIntA(a, ARCHIVE_OK, archive_read_support_format_all(a));
52 if (use_open_fd) {
53 fd = open(refname, O_RDONLY | O_BINARY);
54 assertEqualIntA(a, ARCHIVE_OK,
55 archive_read_open_fd(a, fd, 10240));
56 } else {
57 assertEqualIntA(a, ARCHIVE_OK,
58 archive_read_open_filename(a, refname, 10240));
59 }
60
61 /* Verify regular file1. */
62 assertEqualIntA(a, ARCHIVE_OK, archive_read_next_header(a, &ae));
63 assertEqualInt((AE_IFREG | 0644), archive_entry_mode(ae));
64 assertEqualString("file1", archive_entry_pathname(ae));
65 assertEqualInt(86401, archive_entry_mtime(ae));
66 assertEqualInt(60, archive_entry_size(ae));
67 assertEqualInt(archive_entry_is_encrypted(ae), 0);
68 assert(archive_read_has_encrypted_entries(a) > ARCHIVE_READ_FORMAT_ENCRYPTION_UNSUPPORTED);
69 assertEqualInt(60, archive_read_data(a, buff, sizeof(buff)));
70 assertEqualMem(buff, " ", 4);
71
72 assertEqualInt(1, archive_file_count(a));
73
74 /* End of archive. */
75 assertEqualIntA(a, ARCHIVE_EOF, archive_read_next_header(a, &ae));
76
77 /* Verify archive format. */
78 assertEqualIntA(a, ARCHIVE_FILTER_NONE, archive_filter_code(a, 0));
79 assertEqualIntA(a, ARCHIVE_FORMAT_7ZIP, archive_format(a));
80
81 /* Close the archive. */
82 assertEqualInt(ARCHIVE_OK, archive_read_close(a));
83 assertEqualInt(ARCHIVE_OK, archive_read_free(a));
84 if (fd != -1)
85 close(fd);
86 }
87
88 /*
89 * An archive file has no entry.
90 */
91 static void
test_empty_archive(void)92 test_empty_archive(void)
93 {
94 const char *refname = "test_read_format_7zip_empty_archive.7z";
95 struct archive_entry *ae;
96 struct archive *a;
97
98 extract_reference_file(refname);
99 assert((a = archive_read_new()) != NULL);
100 assertEqualIntA(a, ARCHIVE_OK, archive_read_support_filter_all(a));
101 assertEqualIntA(a, ARCHIVE_OK, archive_read_support_format_all(a));
102 assertEqualIntA(a, ARCHIVE_OK,
103 archive_read_open_filename(a, refname, 10240));
104
105 /* End of archive. */
106 assertEqualIntA(a, ARCHIVE_EOF, archive_read_next_header(a, &ae));
107
108 assertEqualInt(0, archive_file_count(a));
109
110 /* Verify archive format. */
111 assertEqualIntA(a, ARCHIVE_FILTER_NONE, archive_filter_code(a, 0));
112 assertEqualIntA(a, ARCHIVE_FORMAT_7ZIP, archive_format(a));
113
114 /* Close the archive. */
115 assertEqualInt(ARCHIVE_OK, archive_read_close(a));
116 assertEqualInt(ARCHIVE_OK, archive_read_free(a));
117 }
118
119 /*
120 * An archive file has one empty file. It means there is no content
121 * in the archive file except for a header.
122 */
123 static void
test_empty_file(void)124 test_empty_file(void)
125 {
126 const char *refname = "test_read_format_7zip_empty_file.7z";
127 struct archive_entry *ae;
128 struct archive *a;
129
130 extract_reference_file(refname);
131 assert((a = archive_read_new()) != NULL);
132 assertEqualIntA(a, ARCHIVE_OK, archive_read_support_filter_all(a));
133 assertEqualIntA(a, ARCHIVE_OK, archive_read_support_format_all(a));
134 assertEqualIntA(a, ARCHIVE_OK,
135 archive_read_open_filename(a, refname, 10240));
136
137 /* Verify regular empty. */
138 assertEqualIntA(a, ARCHIVE_OK, archive_read_next_header(a, &ae));
139 assertEqualInt((AE_IFREG | 0644), archive_entry_mode(ae));
140 assertEqualString("empty", archive_entry_pathname(ae));
141 assertEqualInt(86401, archive_entry_mtime(ae));
142 assertEqualInt(0, archive_entry_size(ae));
143 assertEqualInt(archive_entry_is_encrypted(ae), 0);
144 assertEqualIntA(a, archive_read_has_encrypted_entries(a), 0);
145
146 assertEqualInt(1, archive_file_count(a));
147
148 /* End of archive. */
149 assertEqualIntA(a, ARCHIVE_EOF, archive_read_next_header(a, &ae));
150
151 /* Verify archive format. */
152 assertEqualIntA(a, ARCHIVE_FILTER_NONE, archive_filter_code(a, 0));
153 assertEqualIntA(a, ARCHIVE_FORMAT_7ZIP, archive_format(a));
154
155 /* Close the archive. */
156 assertEqualInt(ARCHIVE_OK, archive_read_close(a));
157 assertEqualInt(ARCHIVE_OK, archive_read_free(a));
158 }
159
160 /*
161 * Extract an encoded file.
162 * The header of the 7z archive files is not encoded.
163 */
164 static void
test_plain_header(const char * refname)165 test_plain_header(const char *refname)
166 {
167 struct archive_entry *ae;
168 struct archive *a;
169 char buff[128];
170
171 extract_reference_file(refname);
172 assert((a = archive_read_new()) != NULL);
173 assertEqualIntA(a, ARCHIVE_OK, archive_read_support_filter_all(a));
174 assertEqualIntA(a, ARCHIVE_OK, archive_read_support_format_all(a));
175 assertEqualIntA(a, ARCHIVE_OK,
176 archive_read_open_filename(a, refname, 10240));
177
178 /* Verify regular file1. */
179 assertEqualIntA(a, ARCHIVE_OK, archive_read_next_header(a, &ae));
180 assertEqualInt((AE_IFREG | 0644), archive_entry_mode(ae));
181 assertEqualString("file1", archive_entry_pathname(ae));
182 assertEqualInt(1322058763, archive_entry_mtime(ae));
183 assertEqualInt(2844, archive_entry_size(ae));
184 assertEqualInt(archive_entry_is_encrypted(ae), 0);
185 assertEqualIntA(a, archive_read_has_encrypted_entries(a), 0);
186 assertEqualInt(sizeof(buff), archive_read_data(a, buff, sizeof(buff)));
187 assertEqualMem(buff, "The libarchive distribution ", 28);
188
189 assertEqualInt(1, archive_file_count(a));
190
191 /* End of archive. */
192 assertEqualIntA(a, ARCHIVE_EOF, archive_read_next_header(a, &ae));
193
194 /* Verify archive format. */
195 assertEqualIntA(a, ARCHIVE_FILTER_NONE, archive_filter_code(a, 0));
196 assertEqualIntA(a, ARCHIVE_FORMAT_7ZIP, archive_format(a));
197
198 /* Close the archive. */
199 assertEqualInt(ARCHIVE_OK, archive_read_close(a));
200 assertEqualInt(ARCHIVE_OK, archive_read_free(a));
201 }
202
203 /*
204 * Extract multi files.
205 * The header of the 7z archive files is encoded with LZMA.
206 */
207 static void
test_extract_all_files(const char * refname)208 test_extract_all_files(const char *refname)
209 {
210 struct archive_entry *ae;
211 struct archive *a;
212 char buff[128];
213
214 extract_reference_file(refname);
215 assert((a = archive_read_new()) != NULL);
216 assertEqualIntA(a, ARCHIVE_OK, archive_read_support_filter_all(a));
217 assertEqualIntA(a, ARCHIVE_OK, archive_read_support_format_all(a));
218 assertEqualIntA(a, ARCHIVE_OK,
219 archive_read_open_filename(a, refname, 10240));
220
221 /* Verify regular file1. */
222 assertEqualIntA(a, ARCHIVE_OK, archive_read_next_header(a, &ae));
223 assertEqualInt((AE_IFREG | 0644), archive_entry_mode(ae));
224 assertEqualString("dir1/file1", archive_entry_pathname(ae));
225 assertEqualInt(86401, archive_entry_mtime(ae));
226 assertEqualInt(13, archive_entry_size(ae));
227 assertEqualInt(archive_entry_is_encrypted(ae), 0);
228 assertEqualIntA(a, archive_read_has_encrypted_entries(a), 0);
229 assertEqualInt(13, archive_read_data(a, buff, sizeof(buff)));
230 assertEqualMem(buff, "aaaaaaaaaaaa\n", 13);
231
232 /* Verify regular file2. */
233 assertEqualIntA(a, ARCHIVE_OK, archive_read_next_header(a, &ae));
234 assertEqualInt((AE_IFREG | 0644), archive_entry_mode(ae));
235 assertEqualString("file2", archive_entry_pathname(ae));
236 assertEqualInt(86401, archive_entry_mtime(ae));
237 assertEqualInt(26, archive_entry_size(ae));
238 assertEqualInt(archive_entry_is_encrypted(ae), 0);
239 assertEqualIntA(a, archive_read_has_encrypted_entries(a), 0);
240 assertEqualInt(26, archive_read_data(a, buff, sizeof(buff)));
241 assertEqualMem(buff, "aaaaaaaaaaaa\nbbbbbbbbbbbb\n", 26);
242
243 /* Verify regular file3. */
244 assertEqualIntA(a, ARCHIVE_OK, archive_read_next_header(a, &ae));
245 assertEqualInt((AE_IFREG | 0644), archive_entry_mode(ae));
246 assertEqualString("file3", archive_entry_pathname(ae));
247 assertEqualInt(86401, archive_entry_mtime(ae));
248 assertEqualInt(39, archive_entry_size(ae));
249 assertEqualInt(archive_entry_is_encrypted(ae), 0);
250 assertEqualIntA(a, archive_read_has_encrypted_entries(a), 0);
251 assertEqualInt(39, archive_read_data(a, buff, sizeof(buff)));
252 assertEqualMem(buff, "aaaaaaaaaaaa\nbbbbbbbbbbbb\ncccccccccccc\n", 39);
253
254 /* Verify regular file4. */
255 assertEqualIntA(a, ARCHIVE_OK, archive_read_next_header(a, &ae));
256 assertEqualInt((AE_IFREG | 0644), archive_entry_mode(ae));
257 assertEqualString("file4", archive_entry_pathname(ae));
258 assertEqualInt(86401, archive_entry_mtime(ae));
259 assertEqualInt(52, archive_entry_size(ae));
260 assertEqualInt(archive_entry_is_encrypted(ae), 0);
261 assertEqualIntA(a, archive_read_has_encrypted_entries(a), 0);
262 assertEqualInt(52, archive_read_data(a, buff, sizeof(buff)));
263 assertEqualMem(buff,
264 "aaaaaaaaaaaa\nbbbbbbbbbbbb\ncccccccccccc\ndddddddddddd\n", 52);
265
266 /* Verify directory dir1. */
267 assertEqualIntA(a, ARCHIVE_OK, archive_read_next_header(a, &ae));
268 assertEqualInt((AE_IFDIR | 0755), archive_entry_mode(ae));
269 assertEqualString("dir1/", archive_entry_pathname(ae));
270 assertEqualInt(2764801, archive_entry_mtime(ae));
271 assertEqualInt(archive_entry_is_encrypted(ae), 0);
272 assertEqualIntA(a, archive_read_has_encrypted_entries(a), 0);
273
274 assertEqualInt(5, archive_file_count(a));
275
276 /* End of archive. */
277 assertEqualIntA(a, ARCHIVE_EOF, archive_read_next_header(a, &ae));
278
279 /* Verify archive format. */
280 assertEqualIntA(a, ARCHIVE_FILTER_NONE, archive_filter_code(a, 0));
281 assertEqualIntA(a, ARCHIVE_FORMAT_7ZIP, archive_format(a));
282
283 /* Close the archive. */
284 assertEqualInt(ARCHIVE_OK, archive_read_close(a));
285 assertEqualInt(ARCHIVE_OK, archive_read_free(a));
286 }
287
288 /*
289 * Extract multi files.
290 * Like test_extract_all_files, but with zstandard compression.
291 */
292 static void
test_extract_all_files_zstd(const char * refname)293 test_extract_all_files_zstd(const char *refname)
294 {
295 struct archive_entry *ae;
296 struct archive *a;
297 char buff[128];
298
299 extract_reference_file(refname);
300 assert((a = archive_read_new()) != NULL);
301 assertEqualIntA(a, ARCHIVE_OK, archive_read_support_filter_all(a));
302 assertEqualIntA(a, ARCHIVE_OK, archive_read_support_format_all(a));
303 assertEqualIntA(a, ARCHIVE_OK,
304 archive_read_open_filename(a, refname, 10240));
305
306 /* Verify directory dir1. Note that this comes before the dir1/file1 entry in recent versions of 7-Zip. */
307 assertEqualIntA(a, ARCHIVE_OK, archive_read_next_header(a, &ae));
308 assertEqualInt((AE_IFDIR | 0755), archive_entry_mode(ae));
309 assertEqualString("dir1/", archive_entry_pathname(ae));
310 assertEqualInt(2764801, archive_entry_mtime(ae));
311 assertEqualInt(archive_entry_is_encrypted(ae), 0);
312 assertEqualIntA(a, archive_read_has_encrypted_entries(a), 0);
313
314 /* Verify regular file1. */
315 assertEqualIntA(a, ARCHIVE_OK, archive_read_next_header(a, &ae));
316 assertEqualInt((AE_IFREG | 0644), archive_entry_mode(ae));
317 assertEqualString("dir1/file1", archive_entry_pathname(ae));
318 assertEqualInt(86401, archive_entry_mtime(ae));
319 assertEqualInt(13, archive_entry_size(ae));
320 assertEqualInt(archive_entry_is_encrypted(ae), 0);
321 assertEqualIntA(a, archive_read_has_encrypted_entries(a), 0);
322 assertEqualInt(13, archive_read_data(a, buff, sizeof(buff)));
323 assertEqualMem(buff, "aaaaaaaaaaaa\n", 13);
324
325 /* Verify regular file2. */
326 assertEqualIntA(a, ARCHIVE_OK, archive_read_next_header(a, &ae));
327 assertEqualInt((AE_IFREG | 0644), archive_entry_mode(ae));
328 assertEqualString("file2", archive_entry_pathname(ae));
329 assertEqualInt(86401, archive_entry_mtime(ae));
330 assertEqualInt(26, archive_entry_size(ae));
331 assertEqualInt(archive_entry_is_encrypted(ae), 0);
332 assertEqualIntA(a, archive_read_has_encrypted_entries(a), 0);
333 assertEqualInt(26, archive_read_data(a, buff, sizeof(buff)));
334 assertEqualMem(buff, "aaaaaaaaaaaa\nbbbbbbbbbbbb\n", 26);
335
336 /* Verify regular file3. */
337 assertEqualIntA(a, ARCHIVE_OK, archive_read_next_header(a, &ae));
338 assertEqualInt((AE_IFREG | 0644), archive_entry_mode(ae));
339 assertEqualString("file3", archive_entry_pathname(ae));
340 assertEqualInt(86401, archive_entry_mtime(ae));
341 assertEqualInt(39, archive_entry_size(ae));
342 assertEqualInt(archive_entry_is_encrypted(ae), 0);
343 assertEqualIntA(a, archive_read_has_encrypted_entries(a), 0);
344 assertEqualInt(39, archive_read_data(a, buff, sizeof(buff)));
345 assertEqualMem(buff, "aaaaaaaaaaaa\nbbbbbbbbbbbb\ncccccccccccc\n", 39);
346
347 /* Verify regular file4. */
348 assertEqualIntA(a, ARCHIVE_OK, archive_read_next_header(a, &ae));
349 assertEqualInt((AE_IFREG | 0644), archive_entry_mode(ae));
350 assertEqualString("file4", archive_entry_pathname(ae));
351 assertEqualInt(86401, archive_entry_mtime(ae));
352 assertEqualInt(52, archive_entry_size(ae));
353 assertEqualInt(archive_entry_is_encrypted(ae), 0);
354 assertEqualIntA(a, archive_read_has_encrypted_entries(a), 0);
355 assertEqualInt(52, archive_read_data(a, buff, sizeof(buff)));
356 assertEqualMem(buff,
357 "aaaaaaaaaaaa\nbbbbbbbbbbbb\ncccccccccccc\ndddddddddddd\n", 52);
358
359 assertEqualInt(5, archive_file_count(a));
360
361 /* End of archive. */
362 assertEqualIntA(a, ARCHIVE_EOF, archive_read_next_header(a, &ae));
363
364 /* Verify archive format. */
365 assertEqualIntA(a, ARCHIVE_FILTER_NONE, archive_filter_code(a, 0));
366 assertEqualIntA(a, ARCHIVE_FORMAT_7ZIP, archive_format(a));
367
368 /* Close the archive. */
369 assertEqualInt(ARCHIVE_OK, archive_read_close(a));
370 assertEqualInt(ARCHIVE_OK, archive_read_free(a));
371 }
372
373 /*
374 * Extract file from an archives using ZSTD compression with and without BCJ.
375 */
376 static void
test_extract_file_zstd_bcj_nobjc(const char * refname)377 test_extract_file_zstd_bcj_nobjc(const char *refname)
378 {
379 struct archive_entry *ae;
380 struct archive *a;
381 char buff[4096];
382 uint32_t computed_crc = 0;
383 uint32_t expected_crc = 0xbd66eebc;
384
385 extract_reference_file(refname);
386 assert((a = archive_read_new()) != NULL);
387 assertEqualIntA(a, ARCHIVE_OK, archive_read_support_filter_all(a));
388 assertEqualIntA(a, ARCHIVE_OK, archive_read_support_format_all(a));
389 assertEqualIntA(a, ARCHIVE_OK,
390 archive_read_open_filename(a, refname, 10240));
391
392 /* Verify regular file: hw. */
393 assertEqualIntA(a, ARCHIVE_OK, archive_read_next_header(a, &ae));
394 assertEqualInt((AE_IFREG | 0775), archive_entry_mode(ae));
395 assertEqualString("hw", archive_entry_pathname(ae));
396 assertEqualInt(1685913368, archive_entry_mtime(ae));
397 assertEqualInt(15952, archive_entry_size(ae));
398 assertEqualInt(archive_entry_is_encrypted(ae), 0);
399 assertEqualIntA(a, archive_read_has_encrypted_entries(a), 0);
400
401 for (;;) {
402 la_ssize_t bytes_read = archive_read_data(a, buff, sizeof(buff));
403 assert(bytes_read >= 0);
404 if (bytes_read == 0) break;
405 computed_crc = crc32(computed_crc, buff, bytes_read);
406 }
407 assertEqualInt(computed_crc, expected_crc);
408
409 assertEqualInt(1, archive_file_count(a));
410
411 /* End of archive. */
412 assertEqualIntA(a, ARCHIVE_EOF, archive_read_next_header(a, &ae));
413
414 /* Verify archive format. */
415 assertEqualIntA(a, ARCHIVE_FILTER_NONE, archive_filter_code(a, 0));
416 assertEqualIntA(a, ARCHIVE_FORMAT_7ZIP, archive_format(a));
417
418 /* Close the archive. */
419 assertEqualInt(ARCHIVE_OK, archive_read_close(a));
420 assertEqualInt(ARCHIVE_OK, archive_read_free(a));
421 }
422
423 /*
424 * Extract last file.
425 * The header of the 7z archive files is encoded with LZMA.
426 */
427 static void
test_extract_last_file(const char * refname)428 test_extract_last_file(const char *refname)
429 {
430 struct archive_entry *ae;
431 struct archive *a;
432 char buff[128];
433
434 extract_reference_file(refname);
435 assert((a = archive_read_new()) != NULL);
436 assertEqualIntA(a, ARCHIVE_OK, archive_read_support_filter_all(a));
437 assertEqualIntA(a, ARCHIVE_OK, archive_read_support_format_all(a));
438 assertEqualIntA(a, ARCHIVE_OK,
439 archive_read_open_filename(a, refname, 10240));
440
441 /* Verify regular file1. */
442 assertEqualIntA(a, ARCHIVE_OK, archive_read_next_header(a, &ae));
443 assertEqualInt((AE_IFREG | 0644), archive_entry_mode(ae));
444 assertEqualString("dir1/file1", archive_entry_pathname(ae));
445 assertEqualInt(86401, archive_entry_mtime(ae));
446 assertEqualInt(13, archive_entry_size(ae));
447 assertEqualInt(archive_entry_is_encrypted(ae), 0);
448 assertEqualIntA(a, archive_read_has_encrypted_entries(a), 0);
449
450 /* Verify regular file2. */
451 assertEqualIntA(a, ARCHIVE_OK, archive_read_next_header(a, &ae));
452 assertEqualInt((AE_IFREG | 0644), archive_entry_mode(ae));
453 assertEqualString("file2", archive_entry_pathname(ae));
454 assertEqualInt(86401, archive_entry_mtime(ae));
455 assertEqualInt(26, archive_entry_size(ae));
456 assertEqualInt(archive_entry_is_encrypted(ae), 0);
457 assertEqualIntA(a, archive_read_has_encrypted_entries(a), 0);
458
459 /* Verify regular file3. */
460 assertEqualIntA(a, ARCHIVE_OK, archive_read_next_header(a, &ae));
461 assertEqualInt((AE_IFREG | 0644), archive_entry_mode(ae));
462 assertEqualString("file3", archive_entry_pathname(ae));
463 assertEqualInt(86401, archive_entry_mtime(ae));
464 assertEqualInt(39, archive_entry_size(ae));
465 assertEqualInt(archive_entry_is_encrypted(ae), 0);
466 assertEqualIntA(a, archive_read_has_encrypted_entries(a), 0);
467
468 /* Verify regular file4. */
469 assertEqualIntA(a, ARCHIVE_OK, archive_read_next_header(a, &ae));
470 assertEqualInt((AE_IFREG | 0644), archive_entry_mode(ae));
471 assertEqualString("file4", archive_entry_pathname(ae));
472 assertEqualInt(86401, archive_entry_mtime(ae));
473 assertEqualInt(52, archive_entry_size(ae));
474 assertEqualInt(archive_entry_is_encrypted(ae), 0);
475 assertEqualIntA(a, archive_read_has_encrypted_entries(a), 0);
476 assertEqualInt(52, archive_read_data(a, buff, sizeof(buff)));
477 assertEqualMem(buff,
478 "aaaaaaaaaaaa\nbbbbbbbbbbbb\ncccccccccccc\ndddddddddddd\n", 52);
479
480 /* Verify directory dir1. */
481 assertEqualIntA(a, ARCHIVE_OK, archive_read_next_header(a, &ae));
482 assertEqualInt((AE_IFDIR | 0755), archive_entry_mode(ae));
483 assertEqualString("dir1/", archive_entry_pathname(ae));
484 assertEqualInt(2764801, archive_entry_mtime(ae));
485 assertEqualInt(archive_entry_is_encrypted(ae), 0);
486 assertEqualIntA(a, archive_read_has_encrypted_entries(a), 0);
487
488 assertEqualInt(5, archive_file_count(a));
489
490 /* End of archive. */
491 assertEqualIntA(a, ARCHIVE_EOF, archive_read_next_header(a, &ae));
492
493 /* Verify archive format. */
494 assertEqualIntA(a, ARCHIVE_FILTER_NONE, archive_filter_code(a, 0));
495 assertEqualIntA(a, ARCHIVE_FORMAT_7ZIP, archive_format(a));
496
497 /* Close the archive. */
498 assertEqualInt(ARCHIVE_OK, archive_read_close(a));
499 assertEqualInt(ARCHIVE_OK, archive_read_free(a));
500 }
501
502 /*
503 * Extract a mixed archive file which has both LZMA and LZMA2 encoded files.
504 * LZMA: file1, file2, file3, file4
505 * LZMA2: zfile1, zfile2, zfile3, zfile4
506 */
507 static void
test_extract_all_files2(const char * refname)508 test_extract_all_files2(const char *refname)
509 {
510 struct archive_entry *ae;
511 struct archive *a;
512 char buff[128];
513
514 extract_reference_file(refname);
515 assert((a = archive_read_new()) != NULL);
516 assertEqualIntA(a, ARCHIVE_OK, archive_read_support_filter_all(a));
517 assertEqualIntA(a, ARCHIVE_OK, archive_read_support_format_all(a));
518 assertEqualIntA(a, ARCHIVE_OK,
519 archive_read_open_filename(a, refname, 10240));
520
521 /* Verify regular file1. */
522 assertEqualIntA(a, ARCHIVE_OK, archive_read_next_header(a, &ae));
523 assertEqualInt((AE_IFREG | 0644), archive_entry_mode(ae));
524 assertEqualString("dir1/file1", archive_entry_pathname(ae));
525 assertEqualInt(86401, archive_entry_mtime(ae));
526 assertEqualInt(13, archive_entry_size(ae));
527 assertEqualInt(archive_entry_is_encrypted(ae), 0);
528 assertEqualIntA(a, archive_read_has_encrypted_entries(a), 0);
529 assertEqualInt(13, archive_read_data(a, buff, sizeof(buff)));
530 assertEqualMem(buff, "aaaaaaaaaaaa\n", 13);
531
532 /* Verify regular file2. */
533 assertEqualIntA(a, ARCHIVE_OK, archive_read_next_header(a, &ae));
534 assertEqualInt((AE_IFREG | 0644), archive_entry_mode(ae));
535 assertEqualString("file2", archive_entry_pathname(ae));
536 assertEqualInt(86401, archive_entry_mtime(ae));
537 assertEqualInt(26, archive_entry_size(ae));
538 assertEqualInt(archive_entry_is_encrypted(ae), 0);
539 assertEqualIntA(a, archive_read_has_encrypted_entries(a), 0);
540 assertEqualInt(26, archive_read_data(a, buff, sizeof(buff)));
541 assertEqualMem(buff, "aaaaaaaaaaaa\nbbbbbbbbbbbb\n", 26);
542
543 /* Verify regular file3. */
544 assertEqualIntA(a, ARCHIVE_OK, archive_read_next_header(a, &ae));
545 assertEqualInt((AE_IFREG | 0644), archive_entry_mode(ae));
546 assertEqualString("file3", archive_entry_pathname(ae));
547 assertEqualInt(86401, archive_entry_mtime(ae));
548 assertEqualInt(39, archive_entry_size(ae));
549 assertEqualInt(archive_entry_is_encrypted(ae), 0);
550 assertEqualIntA(a, archive_read_has_encrypted_entries(a), 0);
551 assertEqualInt(39, archive_read_data(a, buff, sizeof(buff)));
552 assertEqualMem(buff, "aaaaaaaaaaaa\nbbbbbbbbbbbb\ncccccccccccc\n", 39);
553
554 /* Verify regular file4. */
555 assertEqualIntA(a, ARCHIVE_OK, archive_read_next_header(a, &ae));
556 assertEqualInt((AE_IFREG | 0644), archive_entry_mode(ae));
557 assertEqualString("file4", archive_entry_pathname(ae));
558 assertEqualInt(86401, archive_entry_mtime(ae));
559 assertEqualInt(52, archive_entry_size(ae));
560 assertEqualInt(archive_entry_is_encrypted(ae), 0);
561 assertEqualIntA(a, archive_read_has_encrypted_entries(a), 0);
562 assertEqualInt(52, archive_read_data(a, buff, sizeof(buff)));
563 assertEqualMem(buff,
564 "aaaaaaaaaaaa\nbbbbbbbbbbbb\ncccccccccccc\ndddddddddddd\n", 52);
565
566 /* Verify regular zfile1. */
567 assertEqualIntA(a, ARCHIVE_OK, archive_read_next_header(a, &ae));
568 assertEqualInt((AE_IFREG | 0644), archive_entry_mode(ae));
569 assertEqualString("dir1/zfile1", archive_entry_pathname(ae));
570 assertEqualInt(5184001, archive_entry_mtime(ae));
571 assertEqualInt(13, archive_entry_size(ae));
572 assertEqualInt(archive_entry_is_encrypted(ae), 0);
573 assertEqualIntA(a, archive_read_has_encrypted_entries(a), 0);
574 assertEqualInt(13, archive_read_data(a, buff, sizeof(buff)));
575 assertEqualMem(buff, "aaaaaaaaaaaa\n", 13);
576
577 /* Verify regular zfile2. */
578 assertEqualIntA(a, ARCHIVE_OK, archive_read_next_header(a, &ae));
579 assertEqualInt((AE_IFREG | 0644), archive_entry_mode(ae));
580 assertEqualString("zfile2", archive_entry_pathname(ae));
581 assertEqualInt(5184001, archive_entry_mtime(ae));
582 assertEqualInt(26, archive_entry_size(ae));
583 assertEqualInt(archive_entry_is_encrypted(ae), 0);
584 assertEqualIntA(a, archive_read_has_encrypted_entries(a), 0);
585 assertEqualInt(26, archive_read_data(a, buff, sizeof(buff)));
586 assertEqualMem(buff, "aaaaaaaaaaaa\nbbbbbbbbbbbb\n", 26);
587
588 /* Verify regular zfile3. */
589 assertEqualIntA(a, ARCHIVE_OK, archive_read_next_header(a, &ae));
590 assertEqualInt((AE_IFREG | 0644), archive_entry_mode(ae));
591 assertEqualString("zfile3", archive_entry_pathname(ae));
592 assertEqualInt(5184001, archive_entry_mtime(ae));
593 assertEqualInt(39, archive_entry_size(ae));
594 assertEqualInt(archive_entry_is_encrypted(ae), 0);
595 assertEqualIntA(a, archive_read_has_encrypted_entries(a), 0);
596 assertEqualInt(39, archive_read_data(a, buff, sizeof(buff)));
597 assertEqualMem(buff, "aaaaaaaaaaaa\nbbbbbbbbbbbb\ncccccccccccc\n", 39);
598
599 /* Verify regular zfile4. */
600 assertEqualIntA(a, ARCHIVE_OK, archive_read_next_header(a, &ae));
601 assertEqualInt((AE_IFREG | 0644), archive_entry_mode(ae));
602 assertEqualString("zfile4", archive_entry_pathname(ae));
603 assertEqualInt(5184001, archive_entry_mtime(ae));
604 assertEqualInt(52, archive_entry_size(ae));
605 assertEqualInt(archive_entry_is_encrypted(ae), 0);
606 assertEqualIntA(a, archive_read_has_encrypted_entries(a), 0);
607 assertEqualInt(52, archive_read_data(a, buff, sizeof(buff)));
608 assertEqualMem(buff,
609 "aaaaaaaaaaaa\nbbbbbbbbbbbb\ncccccccccccc\ndddddddddddd\n", 52);
610
611 /* Verify directory dir1. */
612 assertEqualIntA(a, ARCHIVE_OK, archive_read_next_header(a, &ae));
613 assertEqualInt((AE_IFDIR | 0755), archive_entry_mode(ae));
614 assertEqualString("dir1/", archive_entry_pathname(ae));
615 assertEqualInt(2764801, archive_entry_mtime(ae));
616 assertEqualInt(archive_entry_is_encrypted(ae), 0);
617 assertEqualIntA(a, archive_read_has_encrypted_entries(a), 0);
618
619 assertEqualInt(9, archive_file_count(a));
620
621 /* End of archive. */
622 assertEqualIntA(a, ARCHIVE_EOF, archive_read_next_header(a, &ae));
623
624 /* Verify archive format. */
625 assertEqualIntA(a, ARCHIVE_FILTER_NONE, archive_filter_code(a, 0));
626 assertEqualIntA(a, ARCHIVE_FORMAT_7ZIP, archive_format(a));
627
628 /* Close the archive. */
629 assertEqualInt(ARCHIVE_OK, archive_read_close(a));
630 assertEqualInt(ARCHIVE_OK, archive_read_free(a));
631 }
632
633 /*
634 * Extract a file compressed with DELTA + LZMA[12].
635 */
636 static void
test_delta_lzma(const char * refname)637 test_delta_lzma(const char *refname)
638 {
639 struct archive_entry *ae;
640 struct archive *a;
641 size_t remaining;
642 ssize_t bytes;
643 char buff[1024];
644
645 extract_reference_file(refname);
646 assert((a = archive_read_new()) != NULL);
647 assertEqualIntA(a, ARCHIVE_OK, archive_read_support_filter_all(a));
648 assertEqualIntA(a, ARCHIVE_OK, archive_read_support_format_all(a));
649 assertEqualIntA(a, ARCHIVE_OK,
650 archive_read_open_filename(a, refname, 10240));
651
652 /* Verify regular file1. */
653 assertEqualIntA(a, ARCHIVE_OK, archive_read_next_header(a, &ae));
654 assertEqualInt((AE_IFREG | 0644), archive_entry_mode(ae));
655 assertEqualString("file1", archive_entry_pathname(ae));
656 assertEqualInt(172802, archive_entry_mtime(ae));
657 assertEqualInt(27627, archive_entry_size(ae));
658 assertEqualInt(archive_entry_is_encrypted(ae), 0);
659 assertEqualIntA(a, archive_read_has_encrypted_entries(a), 0);
660 remaining = (size_t)archive_entry_size(ae);
661 while (remaining) {
662 if (remaining < sizeof(buff))
663 assertEqualInt(remaining,
664 bytes = archive_read_data(a, buff, sizeof(buff)));
665 else
666 assertEqualInt(sizeof(buff),
667 bytes = archive_read_data(a, buff, sizeof(buff)));
668 if (bytes > 0)
669 remaining -= bytes;
670 else
671 break;
672 }
673 assertEqualInt(0, remaining);
674
675 assertEqualInt(1, archive_file_count(a));
676
677 /* End of archive. */
678 assertEqualIntA(a, ARCHIVE_EOF, archive_read_next_header(a, &ae));
679
680 /* Verify archive format. */
681 assertEqualIntA(a, ARCHIVE_FILTER_NONE, archive_filter_code(a, 0));
682 assertEqualIntA(a, ARCHIVE_FORMAT_7ZIP, archive_format(a));
683
684 /* Close the archive. */
685 assertEqualInt(ARCHIVE_OK, archive_read_close(a));
686 assertEqualInt(ARCHIVE_OK, archive_read_free(a));
687 }
688
689 /*
690 * Extract a file compressed with BCJ + LZMA2.
691 */
692 static void
test_bcj(const char * refname)693 test_bcj(const char *refname)
694 {
695 struct archive_entry *ae;
696 struct archive *a;
697 size_t remaining;
698 ssize_t bytes;
699 char buff[1024];
700
701 extract_reference_file(refname);
702 assert((a = archive_read_new()) != NULL);
703 assertEqualIntA(a, ARCHIVE_OK, archive_read_support_filter_all(a));
704 assertEqualIntA(a, ARCHIVE_OK, archive_read_support_format_all(a));
705 assertEqualIntA(a, ARCHIVE_OK,
706 archive_read_open_filename(a, refname, 10240));
707
708 /* Verify regular x86exe. */
709 assertEqualIntA(a, ARCHIVE_OK, archive_read_next_header(a, &ae));
710 assertEqualInt((AE_IFREG | 0444), archive_entry_mode(ae) & ~0111);
711 assertEqualString("x86exe", archive_entry_pathname(ae));
712 assertEqualInt(172802, archive_entry_mtime(ae));
713 assertEqualInt(27328, archive_entry_size(ae));
714 assertEqualInt(archive_entry_is_encrypted(ae), 0);
715 assertEqualIntA(a, archive_read_has_encrypted_entries(a), 0);
716 remaining = (size_t)archive_entry_size(ae);
717 while (remaining) {
718 if (remaining < sizeof(buff))
719 assertEqualInt(remaining,
720 bytes = archive_read_data(a, buff, sizeof(buff)));
721 else
722 assertEqualInt(sizeof(buff),
723 bytes = archive_read_data(a, buff, sizeof(buff)));
724 if (bytes > 0)
725 remaining -= bytes;
726 else
727 break;
728 }
729 assertEqualInt(0, remaining);
730
731 assertEqualInt(1, archive_file_count(a));
732
733 /* End of archive. */
734 assertEqualIntA(a, ARCHIVE_EOF, archive_read_next_header(a, &ae));
735
736 /* Verify archive format. */
737 assertEqualIntA(a, ARCHIVE_FILTER_NONE, archive_filter_code(a, 0));
738 assertEqualIntA(a, ARCHIVE_FORMAT_7ZIP, archive_format(a));
739
740 /* Close the archive. */
741 assertEqualInt(ARCHIVE_OK, archive_read_close(a));
742 assertEqualInt(ARCHIVE_OK, archive_read_free(a));
743 }
744
745 /*
746 * Extract a file compressed with PPMd.
747 */
748 static void
test_ppmd(void)749 test_ppmd(void)
750 {
751 const char *refname = "test_read_format_7zip_ppmd.7z";
752 struct archive_entry *ae;
753 struct archive *a;
754 size_t remaining;
755 ssize_t bytes;
756 char buff[1024];
757
758 extract_reference_file(refname);
759 assert((a = archive_read_new()) != NULL);
760 assertEqualIntA(a, ARCHIVE_OK, archive_read_support_filter_all(a));
761 assertEqualIntA(a, ARCHIVE_OK, archive_read_support_format_all(a));
762 assertEqualIntA(a, ARCHIVE_OK,
763 archive_read_open_filename(a, refname, 10240));
764
765 /* Verify regular file1. */
766 assertEqualIntA(a, ARCHIVE_OK, archive_read_next_header(a, &ae));
767 assertEqualInt((AE_IFREG | 0644), archive_entry_mode(ae));
768 assertEqualString("ppmd_test.txt", archive_entry_pathname(ae));
769 assertEqualInt(1322464589, archive_entry_mtime(ae));
770 assertEqualInt(102400, archive_entry_size(ae));
771 assertEqualInt(archive_entry_is_encrypted(ae), 0);
772 assertEqualIntA(a, archive_read_has_encrypted_entries(a), 0);
773 remaining = (size_t)archive_entry_size(ae);
774 while (remaining) {
775 if (remaining < sizeof(buff))
776 assertEqualInt(remaining,
777 bytes = archive_read_data(a, buff, sizeof(buff)));
778 else
779 assertEqualInt(sizeof(buff),
780 bytes = archive_read_data(a, buff, sizeof(buff)));
781 if (bytes > 0)
782 remaining -= bytes;
783 else
784 break;
785 }
786 assertEqualInt(0, remaining);
787
788 assertEqualInt(1, archive_file_count(a));
789
790 /* End of archive. */
791 assertEqualIntA(a, ARCHIVE_EOF, archive_read_next_header(a, &ae));
792
793 /* Verify archive format. */
794 assertEqualIntA(a, ARCHIVE_FILTER_NONE, archive_filter_code(a, 0));
795 assertEqualIntA(a, ARCHIVE_FORMAT_7ZIP, archive_format(a));
796
797 /* Close the archive. */
798 assertEqualInt(ARCHIVE_OK, archive_read_close(a));
799 assertEqualInt(ARCHIVE_OK, archive_read_free(a));
800 }
801
802 static void
test_symname(void)803 test_symname(void)
804 {
805 const char *refname = "test_read_format_7zip_symbolic_name.7z";
806 struct archive_entry *ae;
807 struct archive *a;
808 char buff[128];
809
810 extract_reference_file(refname);
811 assert((a = archive_read_new()) != NULL);
812 assertEqualIntA(a, ARCHIVE_OK, archive_read_support_filter_all(a));
813 assertEqualIntA(a, ARCHIVE_OK, archive_read_support_format_all(a));
814 assertEqualIntA(a, ARCHIVE_OK,
815 archive_read_open_filename(a, refname, 10240));
816
817 /* Verify regular file1. */
818 assertEqualIntA(a, ARCHIVE_OK, archive_read_next_header(a, &ae));
819 assertEqualInt((AE_IFREG | 0644), archive_entry_mode(ae));
820 assertEqualString("file1", archive_entry_pathname(ae));
821 assertEqualInt(86401, archive_entry_mtime(ae));
822 assertEqualInt(32, archive_entry_size(ae));
823 assertEqualInt(archive_entry_is_encrypted(ae), 0);
824 assertEqualIntA(a, archive_read_has_encrypted_entries(a), 0);
825 assertEqualInt(32, archive_read_data(a, buff, sizeof(buff)));
826 assertEqualMem(buff, "hellohellohello\nhellohellohello\n", 32);
827
828 /* Verify symbolic-link symlinkfile. */
829 assertEqualIntA(a, ARCHIVE_OK, archive_read_next_header(a, &ae));
830 assertEqualInt((AE_IFLNK | 0755), archive_entry_mode(ae));
831 assertEqualString("symlinkfile", archive_entry_pathname(ae));
832 assertEqualString("file1", archive_entry_symlink(ae));
833 assertEqualInt(86401, archive_entry_mtime(ae));
834 assertEqualInt(archive_entry_is_encrypted(ae), 0);
835 assertEqualIntA(a, archive_read_has_encrypted_entries(a), 0);
836
837 assertEqualInt(2, archive_file_count(a));
838
839 /* End of archive. */
840 assertEqualIntA(a, ARCHIVE_EOF, archive_read_next_header(a, &ae));
841
842 /* Verify archive format. */
843 assertEqualIntA(a, ARCHIVE_FILTER_NONE, archive_filter_code(a, 0));
844 assertEqualIntA(a, ARCHIVE_FORMAT_7ZIP, archive_format(a));
845
846 /* Close the archive. */
847 assertEqualInt(ARCHIVE_OK, archive_read_close(a));
848 assertEqualInt(ARCHIVE_OK, archive_read_free(a));
849 }
850
851
DEFINE_TEST(test_read_format_7zip)852 DEFINE_TEST(test_read_format_7zip)
853 {
854 struct archive *a;
855
856 assert((a = archive_read_new()) != NULL);
857
858 /* Extracting with liblzma */
859 if (ARCHIVE_OK != archive_read_support_filter_xz(a)) {
860 skipping("7zip:lzma decoding is not supported on this "
861 "platform");
862 } else {
863 test_symname();
864 test_extract_all_files("test_read_format_7zip_copy_2.7z");
865 test_extract_last_file("test_read_format_7zip_copy_2.7z");
866 test_extract_all_files2("test_read_format_7zip_lzma1_lzma2.7z");
867 test_bcj("test_read_format_7zip_bcj2_copy_lzma.7z");
868 }
869 assertEqualInt(ARCHIVE_OK, archive_read_free(a));
870 }
871
DEFINE_TEST(test_read_format_7zip_bzip2)872 DEFINE_TEST(test_read_format_7zip_bzip2)
873 {
874 struct archive *a;
875
876 assert((a = archive_read_new()) != NULL);
877
878 /* Extracting with libbzip2 */
879 if (ARCHIVE_OK != archive_read_support_filter_bzip2(a)) {
880 skipping("7zip:bzip2 decoding is not supported on this platform");
881 } else {
882 test_plain_header("test_read_format_7zip_bzip2.7z");
883 test_bcj("test_read_format_7zip_bcj_bzip2.7z");
884 test_bcj("test_read_format_7zip_bcj2_bzip2.7z");
885 }
886
887 assertEqualInt(ARCHIVE_OK, archive_read_free(a));
888 }
889
DEFINE_TEST(test_read_format_7zip_from_fd)890 DEFINE_TEST(test_read_format_7zip_from_fd)
891 {
892 test_copy(1);/* read a 7zip file from a file descriptor. */
893 }
894
DEFINE_TEST(test_read_format_7zip_copy)895 DEFINE_TEST(test_read_format_7zip_copy)
896 {
897 test_copy(0);
898 test_bcj("test_read_format_7zip_bcj_copy.7z");
899 test_bcj("test_read_format_7zip_bcj2_copy_1.7z");
900 test_bcj("test_read_format_7zip_bcj2_copy_2.7z");
901 }
902
DEFINE_TEST(test_read_format_7zip_deflate)903 DEFINE_TEST(test_read_format_7zip_deflate)
904 {
905 struct archive *a;
906
907 assert((a = archive_read_new()) != NULL);
908
909 /* Extracting with libz */
910 if (ARCHIVE_OK != archive_read_support_filter_gzip(a)) {
911 skipping(
912 "7zip:deflate decoding is not supported on this platform");
913 } else {
914 test_plain_header("test_read_format_7zip_deflate.7z");
915 test_bcj("test_read_format_7zip_bcj_deflate.7z");
916 test_bcj("test_read_format_7zip_bcj2_deflate.7z");
917 }
918
919 assertEqualInt(ARCHIVE_OK, archive_read_free(a));
920 }
921
DEFINE_TEST(test_read_format_7zip_zstd)922 DEFINE_TEST(test_read_format_7zip_zstd)
923 {
924 struct archive *a;
925
926 assert((a = archive_read_new()) != NULL);
927
928 /* Extracting with libzstd */
929 if (ARCHIVE_OK != archive_read_support_filter_zstd(a)) {
930 skipping(
931 "7zip:zstd decoding is not supported on this platform");
932 } else {
933 test_extract_all_files_zstd("test_read_format_7zip_zstd.7z");
934 }
935
936 assertEqualInt(ARCHIVE_OK, archive_read_free(a));
937 }
938
DEFINE_TEST(test_read_format_7zip_zstd_solid)939 DEFINE_TEST(test_read_format_7zip_zstd_solid)
940 {
941 struct archive *a;
942
943 assert((a = archive_read_new()) != NULL);
944
945 /* Extracting with libzstd */
946 if (ARCHIVE_OK != archive_read_support_filter_zstd(a)) {
947 skipping(
948 "7zip:zstd decoding is not supported on this platform");
949 } else {
950 test_extract_all_files_zstd("test_read_format_7zip_solid_zstd.7z");
951 }
952
953 assertEqualInt(ARCHIVE_OK, archive_read_free(a));
954 }
955
DEFINE_TEST(test_read_format_7zip_zstd_bcj)956 DEFINE_TEST(test_read_format_7zip_zstd_bcj)
957 {
958 struct archive *a;
959
960 assert((a = archive_read_new()) != NULL);
961
962 /* Extracting with libzstd */
963 if (ARCHIVE_OK != archive_read_support_filter_zstd(a)) {
964 skipping(
965 "7zip:zstd decoding is not supported on this platform");
966 } else {
967 test_extract_file_zstd_bcj_nobjc("test_read_format_7zip_zstd_bcj.7z");
968 }
969
970 assertEqualInt(ARCHIVE_OK, archive_read_free(a));
971 }
972
DEFINE_TEST(test_read_format_7zip_zstd_nobcj)973 DEFINE_TEST(test_read_format_7zip_zstd_nobcj)
974 {
975 struct archive *a;
976
977 assert((a = archive_read_new()) != NULL);
978
979 /* Extracting with libzstd */
980 if (ARCHIVE_OK != archive_read_support_filter_zstd(a)) {
981 skipping(
982 "7zip:zstd decoding is not supported on this platform");
983 } else {
984 test_extract_file_zstd_bcj_nobjc("test_read_format_7zip_zstd_nobcj.7z");
985 }
986
987 assertEqualInt(ARCHIVE_OK, archive_read_free(a));
988 }
989
DEFINE_TEST(test_read_format_7zip_empty)990 DEFINE_TEST(test_read_format_7zip_empty)
991 {
992 test_empty_archive();
993 test_empty_file();
994 }
995
DEFINE_TEST(test_read_format_7zip_lzma1)996 DEFINE_TEST(test_read_format_7zip_lzma1)
997 {
998 struct archive *a;
999
1000 assert((a = archive_read_new()) != NULL);
1001
1002 /* Extracting with liblzma */
1003 if (ARCHIVE_OK != archive_read_support_filter_xz(a)) {
1004 skipping("7zip:lzma decoding is not supported on this "
1005 "platform");
1006 } else {
1007 test_plain_header("test_read_format_7zip_lzma1.7z");
1008 test_extract_all_files("test_read_format_7zip_lzma1_2.7z");
1009 test_extract_last_file("test_read_format_7zip_lzma1_2.7z");
1010 test_bcj("test_read_format_7zip_bcj_lzma1.7z");
1011 test_bcj("test_read_format_7zip_bcj2_lzma1_1.7z");
1012 test_bcj("test_read_format_7zip_bcj2_lzma1_2.7z");
1013 test_delta_lzma("test_read_format_7zip_delta_lzma1.7z");
1014 test_delta_lzma("test_read_format_7zip_delta4_lzma1.7z");
1015 }
1016 assertEqualInt(ARCHIVE_OK, archive_read_free(a));
1017 }
1018
DEFINE_TEST(test_read_format_7zip_lzma2)1019 DEFINE_TEST(test_read_format_7zip_lzma2)
1020 {
1021 struct archive *a;
1022
1023 assert((a = archive_read_new()) != NULL);
1024
1025 /* Extracting with liblzma */
1026 if (ARCHIVE_OK != archive_read_support_filter_xz(a)) {
1027 skipping("7zip:lzma decoding is not supported on this "
1028 "platform");
1029 } else {
1030 test_plain_header("test_read_format_7zip_lzma2.7z");
1031 test_bcj("test_read_format_7zip_bcj_lzma2.7z");
1032 test_bcj("test_read_format_7zip_bcj2_lzma2_1.7z");
1033 test_bcj("test_read_format_7zip_bcj2_lzma2_2.7z");
1034 test_delta_lzma("test_read_format_7zip_delta_lzma2.7z");
1035 test_delta_lzma("test_read_format_7zip_delta4_lzma2.7z");
1036 }
1037 assertEqualInt(ARCHIVE_OK, archive_read_free(a));
1038 }
1039
1040 static void
test_arm_filter(const char * refname)1041 test_arm_filter(const char *refname)
1042 {
1043 struct archive *a;
1044 struct archive_entry *ae;
1045 char buff[7804];
1046 uint32_t computed_crc = 0;
1047 uint32_t expected_crc = 0x355ec4e1;
1048
1049 assert((a = archive_read_new()) != NULL);
1050
1051 extract_reference_file(refname);
1052
1053 assertEqualInt(ARCHIVE_OK, archive_read_free(a));
1054 assert((a = archive_read_new()) != NULL);
1055 assertEqualIntA(a, ARCHIVE_OK, archive_read_support_filter_all(a));
1056 assertEqualIntA(a, ARCHIVE_OK, archive_read_support_format_all(a));
1057
1058 assertEqualIntA(a, ARCHIVE_OK,
1059 archive_read_open_filename(a, refname, 10240));
1060
1061 assertEqualIntA(a, ARCHIVE_OK, archive_read_next_header(a, &ae));
1062 assertEqualInt((AE_IFREG | 0755), archive_entry_mode(ae));
1063 assertEqualString("hw-gnueabihf", archive_entry_pathname(ae));
1064 assertEqualInt(sizeof(buff), archive_entry_size(ae));
1065 assertEqualInt(sizeof(buff), archive_read_data(a, buff, sizeof(buff)));
1066 computed_crc = crc32(computed_crc, buff, sizeof(buff));
1067 assertEqualInt(computed_crc, expected_crc);
1068
1069 assertEqualInt(1, archive_file_count(a));
1070
1071 assertEqualIntA(a, ARCHIVE_EOF, archive_read_next_header(a, &ae));
1072
1073 assertEqualInt(ARCHIVE_OK, archive_read_close(a));
1074 assertEqualInt(ARCHIVE_OK, archive_read_free(a));
1075 }
1076
DEFINE_TEST(test_read_format_7zip_zstd_arm)1077 DEFINE_TEST(test_read_format_7zip_zstd_arm)
1078 {
1079 struct archive *a;
1080
1081 assert((a = archive_read_new()) != NULL);
1082
1083 if (ARCHIVE_OK != archive_read_support_filter_zstd(a)) {
1084 skipping(
1085 "7zip:zstd decoding is not supported on this platform");
1086 } else {
1087 test_arm_filter("test_read_format_7zip_zstd_arm.7z");
1088 }
1089
1090 assertEqualInt(ARCHIVE_OK, archive_read_free(a));
1091 }
1092
DEFINE_TEST(test_read_format_7zip_lzma2_arm)1093 DEFINE_TEST(test_read_format_7zip_lzma2_arm)
1094 {
1095 struct archive *a;
1096
1097 assert((a = archive_read_new()) != NULL);
1098
1099 if (ARCHIVE_OK != archive_read_support_filter_lzma(a)) {
1100 skipping(
1101 "7zip:lzma decoding is not supported on this platform");
1102 } else {
1103 test_arm_filter("test_read_format_7zip_lzma2_arm.7z");
1104 }
1105
1106 assertEqualInt(ARCHIVE_OK, archive_read_free(a));
1107 }
1108
DEFINE_TEST(test_read_format_7zip_ppmd)1109 DEFINE_TEST(test_read_format_7zip_ppmd)
1110 {
1111 test_ppmd();
1112 }
1113
1114 static void
test_arm64_filter(const char * refname)1115 test_arm64_filter(const char *refname)
1116 {
1117 struct archive *a;
1118 struct archive_entry *ae;
1119 char buff[70368];
1120 uint32_t computed_crc = 0;
1121 uint32_t expected_crc = 0xde97d594;
1122
1123 assert((a = archive_read_new()) != NULL);
1124
1125 extract_reference_file(refname);
1126
1127 assertEqualInt(ARCHIVE_OK, archive_read_free(a));
1128 assert((a = archive_read_new()) != NULL);
1129 assertEqualIntA(a, ARCHIVE_OK, archive_read_support_filter_all(a));
1130 assertEqualIntA(a, ARCHIVE_OK, archive_read_support_format_all(a));
1131
1132 assertEqualIntA(a, ARCHIVE_OK,
1133 archive_read_open_filename(a, refname, 10240));
1134
1135 assertEqualIntA(a, ARCHIVE_OK, archive_read_next_header(a, &ae));
1136 assertEqualInt((AE_IFREG | 0775), archive_entry_mode(ae));
1137 assertEqualString("hw-arm64", archive_entry_pathname(ae));
1138 assertEqualInt(sizeof(buff), archive_entry_size(ae));
1139 assertEqualInt(sizeof(buff), archive_read_data(a, buff, sizeof(buff)));
1140 computed_crc = crc32(computed_crc, buff, sizeof(buff));
1141 assertEqualInt(computed_crc, expected_crc);
1142
1143 assertEqualInt(1, archive_file_count(a));
1144
1145 assertEqualIntA(a, ARCHIVE_EOF, archive_read_next_header(a, &ae));
1146
1147 assertEqualInt(ARCHIVE_OK, archive_read_close(a));
1148 assertEqualInt(ARCHIVE_OK, archive_read_free(a));
1149 }
1150
DEFINE_TEST(test_read_format_7zip_lzma2_arm64)1151 DEFINE_TEST(test_read_format_7zip_lzma2_arm64)
1152 {
1153 #ifdef HAVE_LZMA_FILTER_ARM64
1154 struct archive *a;
1155
1156 assert((a = archive_read_new()) != NULL);
1157
1158 if (ARCHIVE_OK != archive_read_support_filter_lzma(a)) {
1159 skipping(
1160 "7zip:lzma decoding is not supported on this platform");
1161 } else {
1162 test_arm64_filter("test_read_format_7zip_lzma2_arm64.7z");
1163 }
1164
1165 assertEqualInt(ARCHIVE_OK, archive_read_free(a));
1166 #else
1167 skipping("This version of liblzma does not support LZMA_FILTER_ARM64");
1168 #endif
1169 }
1170
DEFINE_TEST(test_read_format_7zip_deflate_arm64)1171 DEFINE_TEST(test_read_format_7zip_deflate_arm64)
1172 {
1173 struct archive *a;
1174
1175 assert((a = archive_read_new()) != NULL);
1176
1177 if (ARCHIVE_OK != archive_read_support_filter_gzip(a)) {
1178 skipping(
1179 "7zip:deflate decoding is not supported on this platform");
1180 } else {
1181 test_arm64_filter("test_read_format_7zip_deflate_arm64.7z");
1182 }
1183
1184 assertEqualInt(ARCHIVE_OK, archive_read_free(a));
1185 }
1186
DEFINE_TEST(test_read_format_7zip_win_attrib)1187 DEFINE_TEST(test_read_format_7zip_win_attrib)
1188 {
1189 struct archive *a;
1190
1191 assert((a = archive_read_new()) != NULL);
1192
1193 if (ARCHIVE_OK != archive_read_support_filter_lzma(a)) {
1194 skipping(
1195 "7zip:lzma decoding is not supported on this platform");
1196 assertEqualInt(ARCHIVE_OK, archive_read_free(a));
1197 return;
1198 }
1199
1200 assertEqualIntA(a, ARCHIVE_OK, archive_read_support_format_all(a));
1201
1202 // This archive has four files and four directories:
1203 // * hidden directory
1204 // * readonly directory
1205 // * regular directory
1206 // * system directory
1207 // * regular "archive" file
1208 // * hidden file
1209 // * readonly file
1210 // * system file
1211 const char *refname = "test_read_format_7zip_win_attrib.7z";
1212 extract_reference_file(refname);
1213
1214 assertEqualIntA(a, ARCHIVE_OK,
1215 archive_read_open_filename(a, refname, 10240));
1216
1217 struct archive_entry *ae;
1218
1219 assertEqualIntA(a, ARCHIVE_OK, archive_read_next_header(a, &ae));
1220 assertEqualString("hidden_dir/", archive_entry_pathname(ae));
1221 assertEqualInt((AE_IFDIR | 0755), archive_entry_mode(ae));
1222 assertEqualString("hidden", archive_entry_fflags_text(ae));
1223
1224 assertEqualIntA(a, ARCHIVE_OK, archive_read_next_header(a, &ae));
1225 assertEqualString("readonly_dir/", archive_entry_pathname(ae));
1226 assertEqualInt((AE_IFDIR | 0555), archive_entry_mode(ae));
1227 assertEqualString("rdonly", archive_entry_fflags_text(ae));
1228
1229 assertEqualIntA(a, ARCHIVE_OK, archive_read_next_header(a, &ae));
1230 assertEqualString("regular_dir/", archive_entry_pathname(ae));
1231 assertEqualInt((AE_IFDIR | 0755), archive_entry_mode(ae));
1232 assertEqualString(NULL, archive_entry_fflags_text(ae));
1233
1234 assertEqualIntA(a, ARCHIVE_OK, archive_read_next_header(a, &ae));
1235 assertEqualString("system_dir/", archive_entry_pathname(ae));
1236 assertEqualInt((AE_IFDIR | 0755), archive_entry_mode(ae));
1237 assertEqualString("system", archive_entry_fflags_text(ae));
1238
1239 assertEqualIntA(a, ARCHIVE_OK, archive_read_next_header(a, &ae));
1240 assertEqualString("archive_file.txt", archive_entry_pathname(ae));
1241 assertEqualInt((AE_IFREG | 0644), archive_entry_mode(ae));
1242 assertEqualString(NULL, archive_entry_fflags_text(ae));
1243
1244 assertEqualIntA(a, ARCHIVE_OK, archive_read_next_header(a, &ae));
1245 assertEqualString("hidden_file.txt", archive_entry_pathname(ae));
1246 assertEqualInt((AE_IFREG | 0644), archive_entry_mode(ae));
1247 assertEqualString("hidden", archive_entry_fflags_text(ae));
1248
1249 assertEqualIntA(a, ARCHIVE_OK, archive_read_next_header(a, &ae));
1250 assertEqualString("readonly_file.txt", archive_entry_pathname(ae));
1251 assertEqualInt((AE_IFREG | 0444), archive_entry_mode(ae));
1252 assertEqualString("rdonly", archive_entry_fflags_text(ae));
1253
1254 assertEqualIntA(a, ARCHIVE_OK, archive_read_next_header(a, &ae));
1255 assertEqualString("system_file.txt", archive_entry_pathname(ae));
1256 assertEqualInt((AE_IFREG | 0644), archive_entry_mode(ae));
1257 assertEqualString("system", archive_entry_fflags_text(ae));
1258
1259
1260 assertEqualInt(ARCHIVE_OK, archive_read_free(a));
1261 }
1262
DEFINE_TEST(test_read_format_7zip_extract_second)1263 DEFINE_TEST(test_read_format_7zip_extract_second)
1264 {
1265 struct archive *a;
1266 char buffer[256];
1267
1268 assert((a = archive_read_new()) != NULL);
1269
1270 if (ARCHIVE_OK != archive_read_support_filter_lzma(a)) {
1271 skipping(
1272 "7zip:lzma decoding is not supported on this platform");
1273 assertEqualInt(ARCHIVE_OK, archive_read_free(a));
1274 return;
1275 }
1276
1277 assertEqualIntA(a, ARCHIVE_OK, archive_read_support_format_all(a));
1278
1279 /*
1280 * The test archive has two files: first.txt which is a 65,536 file (the
1281 * size of the uncompressed buffer), and second.txt which has contents
1282 * we will validate. This test ensures we can skip first.txt and still
1283 * be able to read the contents of second.txt
1284 */
1285 const char *refname = "test_read_format_7zip_extract_second.7z";
1286 extract_reference_file(refname);
1287
1288 assertEqualIntA(a, ARCHIVE_OK,
1289 archive_read_open_filename(a, refname, 10240));
1290
1291 struct archive_entry *ae;
1292
1293 assertEqualIntA(a, ARCHIVE_OK, archive_read_next_header(a, &ae));
1294 assertEqualString("first.txt", archive_entry_pathname(ae));
1295
1296 assertEqualIntA(a, ARCHIVE_OK, archive_read_next_header(a, &ae));
1297 assertEqualString("second.txt", archive_entry_pathname(ae));
1298
1299 assertEqualInt(23, archive_read_data(a, buffer, sizeof(buffer)));
1300 assertEqualMem("This is from second.txt", buffer, 23);
1301
1302 assertEqualInt(ARCHIVE_OK, archive_read_free(a));
1303 }
1304