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