1 /*-
2 * Copyright (c) 2018 Grzegorz Antoniak
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 /* Some tests will want to calculate some CRC32's, and this header can
28 * help. */
29 #define __LIBARCHIVE_BUILD
30 #include <archive_crc32.h>
31 #include <archive_endian.h>
32
33 #define PROLOGUE(reffile) \
34 struct archive_entry *ae; \
35 struct archive *a; \
36 \
37 (void) a; /* Make the compiler happy if we won't use this variables */ \
38 (void) ae; /* in the test cases. */ \
39 \
40 extract_reference_file(reffile); \
41 assert((a = archive_read_new()) != NULL); \
42 assertA(0 == archive_read_support_filter_all(a)); \
43 assertA(0 == archive_read_support_format_all(a)); \
44 assertA(0 == archive_read_open_filename(a, reffile, 10240))
45
46 #define PROLOGUE_MULTI(reffile) \
47 struct archive_entry *ae; \
48 struct archive *a; \
49 \
50 (void) a; \
51 (void) ae; \
52 \
53 extract_reference_files(reffile); \
54 assert((a = archive_read_new()) != NULL); \
55 assertA(0 == archive_read_support_filter_all(a)); \
56 assertA(0 == archive_read_support_format_all(a)); \
57 assertA(0 == archive_read_open_filenames(a, reffile, 10240))
58
59
60 #define EPILOGUE() \
61 assertEqualIntA(a, ARCHIVE_OK, archive_read_close(a)); \
62 assertEqualInt(ARCHIVE_OK, archive_read_free(a))
63
64 static
verify_data(const uint8_t * data_ptr,int magic,int size)65 int verify_data(const uint8_t* data_ptr, int magic, int size) {
66 int i = 0;
67
68 /* This is how the test data inside test files was generated;
69 * we are re-generating it here and we check if our re-generated
70 * test data is the same as in the test file. If this test is
71 * failing it's either because there's a bug in the test case,
72 * or the unpacked data is corrupted. */
73
74 for(i = 0; i < size / 4; ++i) {
75 const int k = i + 1;
76 const signed int* lptr = (const signed int*) &data_ptr[i * 4];
77 signed int val = k * k - 3 * k + (1 + magic);
78
79 if(val < 0)
80 val = 0;
81
82 /* *lptr is a value inside unpacked test file, val is the
83 * value that should be in the unpacked test file. */
84
85 if(archive_le32dec(lptr) != (uint32_t) val)
86 return 0;
87 }
88
89 return 1;
90 }
91
92 static
extract_one(struct archive * a,struct archive_entry * ae,uint32_t crc)93 int extract_one(struct archive* a, struct archive_entry* ae, uint32_t crc) {
94 la_ssize_t fsize, bytes_read;
95 uint8_t* buf;
96 int ret = 1;
97 uint32_t computed_crc;
98
99 fsize = (la_ssize_t) archive_entry_size(ae);
100 buf = malloc(fsize);
101 if(buf == NULL)
102 return 1;
103
104 bytes_read = archive_read_data(a, buf, fsize);
105 if(bytes_read != fsize) {
106 assertEqualInt(bytes_read, fsize);
107 goto fn_exit;
108 }
109
110 computed_crc = crc32(0, buf, fsize);
111 assertEqualInt(computed_crc, crc);
112 ret = 0;
113
114 fn_exit:
115 free(buf);
116 return ret;
117 }
118
DEFINE_TEST(test_read_format_rar5_set_format)119 DEFINE_TEST(test_read_format_rar5_set_format)
120 {
121 struct archive *a;
122 struct archive_entry *ae;
123 const char reffile[] = "test_read_format_rar5_stored.rar";
124
125 extract_reference_file(reffile);
126 assert((a = archive_read_new()) != NULL);
127 assertA(0 == archive_read_support_filter_all(a));
128 assertA(0 == archive_read_set_format(a, ARCHIVE_FORMAT_RAR_V5));
129 assertA(0 == archive_read_open_filename(a, reffile, 10240));
130 assertA(0 == archive_read_next_header(a, &ae));
131 EPILOGUE();
132 }
133
DEFINE_TEST(test_read_format_rar5_stored)134 DEFINE_TEST(test_read_format_rar5_stored)
135 {
136 const char helloworld_txt[] = "hello libarchive test suite!\n";
137 la_ssize_t file_size = sizeof(helloworld_txt) - 1;
138 char buff[64];
139
140 PROLOGUE("test_read_format_rar5_stored.rar");
141
142 assertA(0 == archive_read_next_header(a, &ae));
143 assertEqualString("helloworld.txt", archive_entry_pathname(ae));
144 assertA((int) archive_entry_mtime(ae) > 0);
145 assertA((int) archive_entry_ctime(ae) == 0);
146 assertA((int) archive_entry_atime(ae) == 0);
147 assertEqualInt(file_size, archive_entry_size(ae));
148 assertEqualInt(33188, archive_entry_mode(ae));
149 assertA(file_size == archive_read_data(a, buff, file_size));
150 assertEqualMem(buff, helloworld_txt, file_size);
151 assertEqualInt(archive_entry_is_encrypted(ae), 0);
152
153 assertA(ARCHIVE_EOF == archive_read_next_header(a, &ae));
154
155 EPILOGUE();
156 }
157
DEFINE_TEST(test_read_format_rar5_compressed)158 DEFINE_TEST(test_read_format_rar5_compressed)
159 {
160 const int DATA_SIZE = 1200;
161 uint8_t buff[1200];
162
163 PROLOGUE("test_read_format_rar5_compressed.rar");
164
165 assertA(0 == archive_read_next_header(a, &ae));
166 assertEqualString("test.bin", archive_entry_pathname(ae));
167 assertA((int) archive_entry_mtime(ae) > 0);
168 assertEqualInt(DATA_SIZE, archive_entry_size(ae));
169 assertA(DATA_SIZE == archive_read_data(a, buff, DATA_SIZE));
170 assertA(ARCHIVE_EOF == archive_read_next_header(a, &ae));
171 assertA(1 == verify_data(buff, 0, DATA_SIZE));
172
173 EPILOGUE();
174 }
175
DEFINE_TEST(test_read_format_rar5_multiple_files)176 DEFINE_TEST(test_read_format_rar5_multiple_files)
177 {
178 const int DATA_SIZE = 4096;
179 uint8_t buff[4096];
180
181 PROLOGUE("test_read_format_rar5_multiple_files.rar");
182
183 /* There should be 4 files inside this test file. Check for their
184 * existence, and also check the contents of those test files. */
185
186 assertA(0 == archive_read_next_header(a, &ae));
187 assertEqualString("test1.bin", archive_entry_pathname(ae));
188 assertEqualInt(DATA_SIZE, archive_entry_size(ae));
189 assertA(DATA_SIZE == archive_read_data(a, buff, DATA_SIZE));
190 assertA(1 == verify_data(buff, 1, DATA_SIZE));
191
192 assertA(0 == archive_read_next_header(a, &ae));
193 assertEqualString("test2.bin", archive_entry_pathname(ae));
194 assertEqualInt(DATA_SIZE, archive_entry_size(ae));
195 assertA(DATA_SIZE == archive_read_data(a, buff, DATA_SIZE));
196 assertA(1 == verify_data(buff, 2, DATA_SIZE));
197
198 assertA(0 == archive_read_next_header(a, &ae));
199 assertEqualString("test3.bin", archive_entry_pathname(ae));
200 assertEqualInt(DATA_SIZE, archive_entry_size(ae));
201 assertA(DATA_SIZE == archive_read_data(a, buff, DATA_SIZE));
202 assertA(1 == verify_data(buff, 3, DATA_SIZE));
203
204 assertA(0 == archive_read_next_header(a, &ae));
205 assertEqualString("test4.bin", archive_entry_pathname(ae));
206 assertEqualInt(DATA_SIZE, archive_entry_size(ae));
207 assertA(DATA_SIZE == archive_read_data(a, buff, DATA_SIZE));
208 assertA(1 == verify_data(buff, 4, DATA_SIZE));
209
210 /* There should be no more files in this archive. */
211
212 assertA(ARCHIVE_EOF == archive_read_next_header(a, &ae));
213 EPILOGUE();
214 }
215
216 /* This test is really the same as the test above, but it deals with a solid
217 * archive instead of a regular archive. The test solid archive contains the
218 * same set of files as regular test archive, but it's size is 2x smaller,
219 * because solid archives reuse the window buffer from previous compressed
220 * files, so it's able to compress lots of small files more effectively. */
221
DEFINE_TEST(test_read_format_rar5_multiple_files_solid)222 DEFINE_TEST(test_read_format_rar5_multiple_files_solid)
223 {
224 const int DATA_SIZE = 4096;
225 uint8_t buff[4096];
226
227 PROLOGUE("test_read_format_rar5_multiple_files_solid.rar");
228
229 assertA(0 == archive_read_next_header(a, &ae));
230 assertEqualString("test1.bin", archive_entry_pathname(ae));
231 assertEqualInt(DATA_SIZE, archive_entry_size(ae));
232 assertA(DATA_SIZE == archive_read_data(a, buff, DATA_SIZE));
233 assertA(1 == verify_data(buff, 1, DATA_SIZE));
234
235 assertA(0 == archive_read_next_header(a, &ae));
236 assertEqualString("test2.bin", archive_entry_pathname(ae));
237 assertEqualInt(DATA_SIZE, archive_entry_size(ae));
238 assertA(DATA_SIZE == archive_read_data(a, buff, DATA_SIZE));
239 assertA(1 == verify_data(buff, 2, DATA_SIZE));
240
241 assertA(0 == archive_read_next_header(a, &ae));
242 assertEqualString("test3.bin", archive_entry_pathname(ae));
243 assertEqualInt(DATA_SIZE, archive_entry_size(ae));
244 assertA(DATA_SIZE == archive_read_data(a, buff, DATA_SIZE));
245 assertA(1 == verify_data(buff, 3, DATA_SIZE));
246
247 assertA(0 == archive_read_next_header(a, &ae));
248 assertEqualString("test4.bin", archive_entry_pathname(ae));
249 assertEqualInt(DATA_SIZE, archive_entry_size(ae));
250 assertA(DATA_SIZE == archive_read_data(a, buff, DATA_SIZE));
251 assertA(1 == verify_data(buff, 4, DATA_SIZE));
252
253 assertA(ARCHIVE_EOF == archive_read_next_header(a, &ae));
254 EPILOGUE();
255 }
256
DEFINE_TEST(test_read_format_rar5_multiarchive_skip_all)257 DEFINE_TEST(test_read_format_rar5_multiarchive_skip_all)
258 {
259 const char* reffiles[] = {
260 "test_read_format_rar5_multiarchive.part01.rar",
261 "test_read_format_rar5_multiarchive.part02.rar",
262 "test_read_format_rar5_multiarchive.part03.rar",
263 "test_read_format_rar5_multiarchive.part04.rar",
264 "test_read_format_rar5_multiarchive.part05.rar",
265 "test_read_format_rar5_multiarchive.part06.rar",
266 "test_read_format_rar5_multiarchive.part07.rar",
267 "test_read_format_rar5_multiarchive.part08.rar",
268 NULL
269 };
270
271 PROLOGUE_MULTI(reffiles);
272 assertA(0 == archive_read_next_header(a, &ae));
273 assertEqualString("home/antek/temp/build/unrar5/libarchive/bin/bsdcat_test", archive_entry_pathname(ae));
274 assertA(0 == archive_read_next_header(a, &ae));
275 assertEqualString("home/antek/temp/build/unrar5/libarchive/bin/bsdtar_test", archive_entry_pathname(ae));
276 assertA(ARCHIVE_EOF == archive_read_next_header(a, &ae));
277 EPILOGUE();
278 }
279
DEFINE_TEST(test_read_format_rar5_multiarchive_skip_all_but_first)280 DEFINE_TEST(test_read_format_rar5_multiarchive_skip_all_but_first)
281 {
282 const char* reffiles[] = {
283 "test_read_format_rar5_multiarchive.part01.rar",
284 "test_read_format_rar5_multiarchive.part02.rar",
285 "test_read_format_rar5_multiarchive.part03.rar",
286 "test_read_format_rar5_multiarchive.part04.rar",
287 "test_read_format_rar5_multiarchive.part05.rar",
288 "test_read_format_rar5_multiarchive.part06.rar",
289 "test_read_format_rar5_multiarchive.part07.rar",
290 "test_read_format_rar5_multiarchive.part08.rar",
291 NULL
292 };
293
294 PROLOGUE_MULTI(reffiles);
295 assertA(0 == archive_read_next_header(a, &ae));
296 assertA(0 == extract_one(a, ae, 0x35277473));
297 assertA(0 == archive_read_next_header(a, &ae));
298 assertA(ARCHIVE_EOF == archive_read_next_header(a, &ae));
299 EPILOGUE();
300 }
301
DEFINE_TEST(test_read_format_rar5_multiarchive_skip_all_but_second)302 DEFINE_TEST(test_read_format_rar5_multiarchive_skip_all_but_second)
303 {
304 const char* reffiles[] = {
305 "test_read_format_rar5_multiarchive.part01.rar",
306 "test_read_format_rar5_multiarchive.part02.rar",
307 "test_read_format_rar5_multiarchive.part03.rar",
308 "test_read_format_rar5_multiarchive.part04.rar",
309 "test_read_format_rar5_multiarchive.part05.rar",
310 "test_read_format_rar5_multiarchive.part06.rar",
311 "test_read_format_rar5_multiarchive.part07.rar",
312 "test_read_format_rar5_multiarchive.part08.rar",
313 NULL
314 };
315
316 PROLOGUE_MULTI(reffiles);
317 assertA(0 == archive_read_next_header(a, &ae));
318 assertA(0 == archive_read_next_header(a, &ae));
319 assertA(0 == extract_one(a, ae, 0xE59665F8));
320 assertA(ARCHIVE_EOF == archive_read_next_header(a, &ae));
321 EPILOGUE();
322 }
323
DEFINE_TEST(test_read_format_rar5_blake2)324 DEFINE_TEST(test_read_format_rar5_blake2)
325 {
326 const la_ssize_t proper_size = 814;
327 uint8_t buf[814];
328
329 PROLOGUE("test_read_format_rar5_blake2.rar");
330 assertA(0 == archive_read_next_header(a, &ae));
331 assertEqualInt(proper_size, archive_entry_size(ae));
332
333 /* Should blake2 calculation fail, we'll get a failure return
334 * value from archive_read_data(). */
335
336 assertA(proper_size == archive_read_data(a, buf, proper_size));
337
338 /* To be extra pedantic, let's also check crc32 of the poem. */
339 assertEqualInt(crc32(0, buf, proper_size), 0x7E5EC49E);
340
341 assertA(ARCHIVE_EOF == archive_read_next_header(a, &ae));
342 EPILOGUE();
343 }
344
DEFINE_TEST(test_read_format_rar5_arm_filter)345 DEFINE_TEST(test_read_format_rar5_arm_filter)
346 {
347 /* This test unpacks a file that uses an ARM filter. The DELTA
348 * and X86 filters are tested implicitly in the "multiarchive_skip"
349 * test. */
350
351 const la_ssize_t proper_size = 90808;
352 uint8_t buf[90808];
353
354 PROLOGUE("test_read_format_rar5_arm.rar");
355 assertA(0 == archive_read_next_header(a, &ae));
356 assertEqualInt(proper_size, archive_entry_size(ae));
357 assertA(proper_size == archive_read_data(a, buf, proper_size));
358
359 /* Yes, RARv5 unpacker itself should calculate the CRC, but in case
360 * the DONT_FAIL_ON_CRC_ERROR define option is enabled during compilation,
361 * let's still fail the test if the unpacked data is wrong. */
362 assertEqualInt(crc32(0, buf, proper_size), 0x886F91EB);
363
364 assertA(ARCHIVE_EOF == archive_read_next_header(a, &ae));
365 EPILOGUE();
366 }
367
DEFINE_TEST(test_read_format_rar5_stored_skip_all)368 DEFINE_TEST(test_read_format_rar5_stored_skip_all)
369 {
370 const char* fname = "test_read_format_rar5_stored_manyfiles.rar";
371
372 PROLOGUE(fname);
373 assertA(0 == archive_read_next_header(a, &ae));
374 assertEqualString("make_uue.tcl", archive_entry_pathname(ae));
375 assertA(0 == archive_read_next_header(a, &ae));
376 assertEqualString("cebula.txt", archive_entry_pathname(ae));
377 assertA(0 == archive_read_next_header(a, &ae));
378 assertEqualString("test.bin", archive_entry_pathname(ae));
379 assertA(ARCHIVE_EOF == archive_read_next_header(a, &ae));
380 EPILOGUE();
381 }
382
DEFINE_TEST(test_read_format_rar5_stored_skip_in_part)383 DEFINE_TEST(test_read_format_rar5_stored_skip_in_part)
384 {
385 const char* fname = "test_read_format_rar5_stored_manyfiles.rar";
386 char buf[6];
387
388 /* Skip first, extract in part rest. */
389
390 PROLOGUE(fname);
391 assertA(0 == archive_read_next_header(a, &ae));
392 assertEqualString("make_uue.tcl", archive_entry_pathname(ae));
393 assertA(0 == archive_read_next_header(a, &ae));
394 assertEqualString("cebula.txt", archive_entry_pathname(ae));
395 assertA(6 == archive_read_data(a, buf, 6));
396 assertEqualInt(0, memcmp(buf, "Cebula", 6));
397 assertA(0 == archive_read_next_header(a, &ae));
398 assertEqualString("test.bin", archive_entry_pathname(ae));
399 assertA(4 == archive_read_data(a, buf, 4));
400 assertA(ARCHIVE_EOF == archive_read_next_header(a, &ae));
401 EPILOGUE();
402 }
403
DEFINE_TEST(test_read_format_rar5_stored_skip_all_but_first)404 DEFINE_TEST(test_read_format_rar5_stored_skip_all_but_first)
405 {
406 const char* fname = "test_read_format_rar5_stored_manyfiles.rar";
407 char buf[405];
408
409 /* Extract first, skip rest. */
410
411 PROLOGUE(fname);
412 assertA(0 == archive_read_next_header(a, &ae));
413 assertEqualString("make_uue.tcl", archive_entry_pathname(ae));
414 assertA(405 == archive_read_data(a, buf, sizeof(buf)));
415 assertA(0 == archive_read_next_header(a, &ae));
416 assertEqualString("cebula.txt", archive_entry_pathname(ae));
417 assertA(0 == archive_read_next_header(a, &ae));
418 assertEqualString("test.bin", archive_entry_pathname(ae));
419 assertA(ARCHIVE_EOF == archive_read_next_header(a, &ae));
420 EPILOGUE();
421 }
422
DEFINE_TEST(test_read_format_rar5_stored_skip_all_in_part)423 DEFINE_TEST(test_read_format_rar5_stored_skip_all_in_part)
424 {
425 const char* fname = "test_read_format_rar5_stored_manyfiles.rar";
426 char buf[4];
427
428 /* Extract in part all */
429
430 PROLOGUE(fname);
431 assertA(0 == archive_read_next_header(a, &ae));
432 assertEqualString("make_uue.tcl", archive_entry_pathname(ae));
433 assertA(4 == archive_read_data(a, buf, 4));
434 assertA(0 == archive_read_next_header(a, &ae));
435 assertEqualString("cebula.txt", archive_entry_pathname(ae));
436 assertA(4 == archive_read_data(a, buf, 4));
437 assertA(0 == archive_read_next_header(a, &ae));
438 assertEqualString("test.bin", archive_entry_pathname(ae));
439 assertA(4 == archive_read_data(a, buf, 4));
440 assertA(ARCHIVE_EOF == archive_read_next_header(a, &ae));
441 EPILOGUE();
442 }
443
DEFINE_TEST(test_read_format_rar5_multiarchive_solid_extr_all)444 DEFINE_TEST(test_read_format_rar5_multiarchive_solid_extr_all)
445 {
446 const char* reffiles[] = {
447 "test_read_format_rar5_multiarchive_solid.part01.rar",
448 "test_read_format_rar5_multiarchive_solid.part02.rar",
449 "test_read_format_rar5_multiarchive_solid.part03.rar",
450 "test_read_format_rar5_multiarchive_solid.part04.rar",
451 NULL
452 };
453
454 PROLOGUE_MULTI(reffiles);
455 assertA(0 == archive_read_next_header(a, &ae));
456 assertEqualString("cebula.txt", archive_entry_pathname(ae));
457 assertA(0 == extract_one(a, ae, 0x7E5EC49E));
458
459 assertA(0 == archive_read_next_header(a, &ae));
460 assertEqualString("test.bin", archive_entry_pathname(ae));
461 assertA(0 == extract_one(a, ae, 0x7cca70cd));
462
463 assertA(0 == archive_read_next_header(a, &ae));
464 assertEqualString("test1.bin", archive_entry_pathname(ae));
465 assertA(0 == extract_one(a, ae, 0x7e13b2c6));
466
467 assertA(0 == archive_read_next_header(a, &ae));
468 assertEqualString("test2.bin", archive_entry_pathname(ae));
469 assertA(0 == extract_one(a, ae, 0xf166afcb));
470
471 assertA(0 == archive_read_next_header(a, &ae));
472 assertEqualString("test3.bin", archive_entry_pathname(ae));
473 assertA(0 == extract_one(a, ae, 0x9fb123d9));
474
475 assertA(0 == archive_read_next_header(a, &ae));
476 assertEqualString("test4.bin", archive_entry_pathname(ae));
477 assertA(0 == extract_one(a, ae, 0x10c43ed4));
478
479 assertA(0 == archive_read_next_header(a, &ae));
480 assertEqualString("test5.bin", archive_entry_pathname(ae));
481 assertA(0 == extract_one(a, ae, 0xb9d155f2));
482
483 assertA(0 == archive_read_next_header(a, &ae));
484 assertEqualString("test6.bin", archive_entry_pathname(ae));
485 assertA(0 == extract_one(a, ae, 0x36a448ff));
486
487 assertA(0 == archive_read_next_header(a, &ae));
488 assertEqualString("elf-Linux-ARMv7-ls", archive_entry_pathname(ae));
489 assertA(0 == extract_one(a, ae, 0x886F91EB));
490
491 assertA(ARCHIVE_EOF == archive_read_next_header(a, &ae));
492 EPILOGUE();
493 }
494
DEFINE_TEST(test_read_format_rar5_multiarchive_solid_skip_all)495 DEFINE_TEST(test_read_format_rar5_multiarchive_solid_skip_all)
496 {
497 const char* reffiles[] = {
498 "test_read_format_rar5_multiarchive_solid.part01.rar",
499 "test_read_format_rar5_multiarchive_solid.part02.rar",
500 "test_read_format_rar5_multiarchive_solid.part03.rar",
501 "test_read_format_rar5_multiarchive_solid.part04.rar",
502 NULL
503 };
504
505 PROLOGUE_MULTI(reffiles);
506 assertA(0 == archive_read_next_header(a, &ae));
507 assertEqualString("cebula.txt", archive_entry_pathname(ae));
508 assertA(0 == archive_read_next_header(a, &ae));
509 assertEqualString("test.bin", archive_entry_pathname(ae));
510 assertA(0 == archive_read_next_header(a, &ae));
511 assertEqualString("test1.bin", archive_entry_pathname(ae));
512 assertA(0 == archive_read_next_header(a, &ae));
513 assertEqualString("test2.bin", archive_entry_pathname(ae));
514 assertA(0 == archive_read_next_header(a, &ae));
515 assertEqualString("test3.bin", archive_entry_pathname(ae));
516 assertA(0 == archive_read_next_header(a, &ae));
517 assertEqualString("test4.bin", archive_entry_pathname(ae));
518 assertA(0 == archive_read_next_header(a, &ae));
519 assertEqualString("test5.bin", archive_entry_pathname(ae));
520 assertA(0 == archive_read_next_header(a, &ae));
521 assertEqualString("test6.bin", archive_entry_pathname(ae));
522 assertA(0 == archive_read_next_header(a, &ae));
523 assertEqualString("elf-Linux-ARMv7-ls", archive_entry_pathname(ae));
524 assertA(ARCHIVE_EOF == archive_read_next_header(a, &ae));
525 EPILOGUE();
526 }
527
DEFINE_TEST(test_read_format_rar5_multiarchive_solid_skip_all_but_first)528 DEFINE_TEST(test_read_format_rar5_multiarchive_solid_skip_all_but_first)
529 {
530 const char* reffiles[] = {
531 "test_read_format_rar5_multiarchive_solid.part01.rar",
532 "test_read_format_rar5_multiarchive_solid.part02.rar",
533 "test_read_format_rar5_multiarchive_solid.part03.rar",
534 "test_read_format_rar5_multiarchive_solid.part04.rar",
535 NULL
536 };
537
538 PROLOGUE_MULTI(reffiles);
539 assertA(0 == archive_read_next_header(a, &ae));
540 assertEqualString("cebula.txt", archive_entry_pathname(ae));
541 assertA(0 == extract_one(a, ae, 0x7E5EC49E));
542 assertA(0 == archive_read_next_header(a, &ae));
543 assertEqualString("test.bin", archive_entry_pathname(ae));
544 assertA(0 == archive_read_next_header(a, &ae));
545 assertEqualString("test1.bin", archive_entry_pathname(ae));
546 assertA(0 == archive_read_next_header(a, &ae));
547 assertEqualString("test2.bin", archive_entry_pathname(ae));
548 assertA(0 == archive_read_next_header(a, &ae));
549 assertEqualString("test3.bin", archive_entry_pathname(ae));
550 assertA(0 == archive_read_next_header(a, &ae));
551 assertEqualString("test4.bin", archive_entry_pathname(ae));
552 assertA(0 == archive_read_next_header(a, &ae));
553 assertEqualString("test5.bin", archive_entry_pathname(ae));
554 assertA(0 == archive_read_next_header(a, &ae));
555 assertEqualString("test6.bin", archive_entry_pathname(ae));
556 assertA(0 == archive_read_next_header(a, &ae));
557 assertEqualString("elf-Linux-ARMv7-ls", archive_entry_pathname(ae));
558 assertA(ARCHIVE_EOF == archive_read_next_header(a, &ae));
559 EPILOGUE();
560 }
561
562 /* "skip_all_but_scnd" -> am I hitting the test name limit here after
563 * expansion of "scnd" to "second"? */
564
DEFINE_TEST(test_read_format_rar5_multiarchive_solid_skip_all_but_scnd)565 DEFINE_TEST(test_read_format_rar5_multiarchive_solid_skip_all_but_scnd)
566 {
567 const char* reffiles[] = {
568 "test_read_format_rar5_multiarchive_solid.part01.rar",
569 "test_read_format_rar5_multiarchive_solid.part02.rar",
570 "test_read_format_rar5_multiarchive_solid.part03.rar",
571 "test_read_format_rar5_multiarchive_solid.part04.rar",
572 NULL
573 };
574
575 PROLOGUE_MULTI(reffiles);
576 assertA(0 == archive_read_next_header(a, &ae));
577 assertEqualString("cebula.txt", archive_entry_pathname(ae));
578 assertA(0 == archive_read_next_header(a, &ae));
579 assertEqualString("test.bin", archive_entry_pathname(ae));
580 assertA(0 == extract_one(a, ae, 0x7CCA70CD));
581 assertA(0 == archive_read_next_header(a, &ae));
582 assertEqualString("test1.bin", archive_entry_pathname(ae));
583 assertA(0 == archive_read_next_header(a, &ae));
584 assertEqualString("test2.bin", archive_entry_pathname(ae));
585 assertA(0 == archive_read_next_header(a, &ae));
586 assertEqualString("test3.bin", archive_entry_pathname(ae));
587 assertA(0 == archive_read_next_header(a, &ae));
588 assertEqualString("test4.bin", archive_entry_pathname(ae));
589 assertA(0 == archive_read_next_header(a, &ae));
590 assertEqualString("test5.bin", archive_entry_pathname(ae));
591 assertA(0 == archive_read_next_header(a, &ae));
592 assertEqualString("test6.bin", archive_entry_pathname(ae));
593 assertA(0 == archive_read_next_header(a, &ae));
594 assertEqualString("elf-Linux-ARMv7-ls", archive_entry_pathname(ae));
595 assertA(ARCHIVE_EOF == archive_read_next_header(a, &ae));
596 EPILOGUE();
597 }
598
DEFINE_TEST(test_read_format_rar5_multiarchive_solid_skip_all_but_third)599 DEFINE_TEST(test_read_format_rar5_multiarchive_solid_skip_all_but_third)
600 {
601 const char* reffiles[] = {
602 "test_read_format_rar5_multiarchive_solid.part01.rar",
603 "test_read_format_rar5_multiarchive_solid.part02.rar",
604 "test_read_format_rar5_multiarchive_solid.part03.rar",
605 "test_read_format_rar5_multiarchive_solid.part04.rar",
606 NULL
607 };
608
609 PROLOGUE_MULTI(reffiles);
610 assertA(0 == archive_read_next_header(a, &ae));
611 assertEqualString("cebula.txt", archive_entry_pathname(ae));
612 assertA(0 == archive_read_next_header(a, &ae));
613 assertEqualString("test.bin", archive_entry_pathname(ae));
614 assertA(0 == archive_read_next_header(a, &ae));
615 assertEqualString("test1.bin", archive_entry_pathname(ae));
616 assertA(0 == extract_one(a, ae, 0x7E13B2C6));
617 assertA(0 == archive_read_next_header(a, &ae));
618 assertEqualString("test2.bin", archive_entry_pathname(ae));
619 assertA(0 == archive_read_next_header(a, &ae));
620 assertEqualString("test3.bin", archive_entry_pathname(ae));
621 assertA(0 == archive_read_next_header(a, &ae));
622 assertEqualString("test4.bin", archive_entry_pathname(ae));
623 assertA(0 == archive_read_next_header(a, &ae));
624 assertEqualString("test5.bin", archive_entry_pathname(ae));
625 assertA(0 == archive_read_next_header(a, &ae));
626 assertEqualString("test6.bin", archive_entry_pathname(ae));
627 assertA(0 == archive_read_next_header(a, &ae));
628 assertEqualString("elf-Linux-ARMv7-ls", archive_entry_pathname(ae));
629 assertA(ARCHIVE_EOF == archive_read_next_header(a, &ae));
630 EPILOGUE();
631 }
632
DEFINE_TEST(test_read_format_rar5_multiarchive_solid_skip_all_but_last)633 DEFINE_TEST(test_read_format_rar5_multiarchive_solid_skip_all_but_last)
634 {
635 const char* reffiles[] = {
636 "test_read_format_rar5_multiarchive_solid.part01.rar",
637 "test_read_format_rar5_multiarchive_solid.part02.rar",
638 "test_read_format_rar5_multiarchive_solid.part03.rar",
639 "test_read_format_rar5_multiarchive_solid.part04.rar",
640 NULL
641 };
642
643 PROLOGUE_MULTI(reffiles);
644 assertA(0 == archive_read_next_header(a, &ae));
645 assertEqualString("cebula.txt", archive_entry_pathname(ae));
646 assertA(0 == archive_read_next_header(a, &ae));
647 assertEqualString("test.bin", archive_entry_pathname(ae));
648 assertA(0 == archive_read_next_header(a, &ae));
649 assertEqualString("test1.bin", archive_entry_pathname(ae));
650 assertA(0 == archive_read_next_header(a, &ae));
651 assertEqualString("test2.bin", archive_entry_pathname(ae));
652 assertA(0 == archive_read_next_header(a, &ae));
653 assertEqualString("test3.bin", archive_entry_pathname(ae));
654 assertA(0 == archive_read_next_header(a, &ae));
655 assertEqualString("test4.bin", archive_entry_pathname(ae));
656 assertA(0 == archive_read_next_header(a, &ae));
657 assertEqualString("test5.bin", archive_entry_pathname(ae));
658 assertA(0 == archive_read_next_header(a, &ae));
659 assertEqualString("test6.bin", archive_entry_pathname(ae));
660 assertA(0 == archive_read_next_header(a, &ae));
661 assertEqualString("elf-Linux-ARMv7-ls", archive_entry_pathname(ae));
662 assertA(0 == extract_one(a, ae, 0x886F91EB));
663 assertA(ARCHIVE_EOF == archive_read_next_header(a, &ae));
664 EPILOGUE();
665 }
666
DEFINE_TEST(test_read_format_rar5_solid_skip_all)667 DEFINE_TEST(test_read_format_rar5_solid_skip_all)
668 {
669 const char* reffile = "test_read_format_rar5_solid.rar";
670
671 /* Skip all */
672
673 PROLOGUE(reffile);
674 assertA(0 == archive_read_next_header(a, &ae));
675 assertEqualString("test.bin", archive_entry_pathname(ae));
676 assertA(0 == archive_read_next_header(a, &ae));
677 assertEqualString("test1.bin", archive_entry_pathname(ae));
678 assertA(0 == archive_read_next_header(a, &ae));
679 assertEqualString("test2.bin", archive_entry_pathname(ae));
680 assertA(0 == archive_read_next_header(a, &ae));
681 assertEqualString("test3.bin", archive_entry_pathname(ae));
682 assertA(0 == archive_read_next_header(a, &ae));
683 assertEqualString("test4.bin", archive_entry_pathname(ae));
684 assertA(0 == archive_read_next_header(a, &ae));
685 assertEqualString("test5.bin", archive_entry_pathname(ae));
686 assertA(0 == archive_read_next_header(a, &ae));
687 assertEqualString("test6.bin", archive_entry_pathname(ae));
688 assertA(ARCHIVE_EOF == archive_read_next_header(a, &ae));
689 EPILOGUE();
690 }
691
DEFINE_TEST(test_read_format_rar5_solid_skip_all_but_first)692 DEFINE_TEST(test_read_format_rar5_solid_skip_all_but_first)
693 {
694 const char* reffile = "test_read_format_rar5_solid.rar";
695
696 /* Extract first, skip rest */
697
698 PROLOGUE(reffile);
699 assertA(0 == archive_read_next_header(a, &ae));
700 assertEqualString("test.bin", archive_entry_pathname(ae));
701 assertA(0 == extract_one(a, ae, 0x7CCA70CD));
702 assertA(0 == archive_read_next_header(a, &ae));
703 assertEqualString("test1.bin", archive_entry_pathname(ae));
704 assertA(0 == archive_read_next_header(a, &ae));
705 assertEqualString("test2.bin", archive_entry_pathname(ae));
706 assertA(0 == archive_read_next_header(a, &ae));
707 assertEqualString("test3.bin", archive_entry_pathname(ae));
708 assertA(0 == archive_read_next_header(a, &ae));
709 assertEqualString("test4.bin", archive_entry_pathname(ae));
710 assertA(0 == archive_read_next_header(a, &ae));
711 assertEqualString("test5.bin", archive_entry_pathname(ae));
712 assertA(0 == archive_read_next_header(a, &ae));
713 assertEqualString("test6.bin", archive_entry_pathname(ae));
714 assertA(ARCHIVE_EOF == archive_read_next_header(a, &ae));
715 EPILOGUE();
716 }
717
DEFINE_TEST(test_read_format_rar5_solid_skip_all_but_second)718 DEFINE_TEST(test_read_format_rar5_solid_skip_all_but_second)
719 {
720 const char* reffile = "test_read_format_rar5_solid.rar";
721
722 /* Skip first, extract second, skip rest */
723
724 PROLOGUE(reffile);
725 assertA(0 == archive_read_next_header(a, &ae));
726 assertEqualString("test.bin", archive_entry_pathname(ae));
727 assertA(0 == archive_read_next_header(a, &ae));
728 assertEqualString("test1.bin", archive_entry_pathname(ae));
729 assertA(0 == extract_one(a, ae, 0x7E13B2C6));
730 assertA(0 == archive_read_next_header(a, &ae));
731 assertEqualString("test2.bin", archive_entry_pathname(ae));
732 assertA(0 == archive_read_next_header(a, &ae));
733 assertEqualString("test3.bin", archive_entry_pathname(ae));
734 assertA(0 == archive_read_next_header(a, &ae));
735 assertEqualString("test4.bin", archive_entry_pathname(ae));
736 assertA(0 == archive_read_next_header(a, &ae));
737 assertEqualString("test5.bin", archive_entry_pathname(ae));
738 assertA(0 == archive_read_next_header(a, &ae));
739 assertEqualString("test6.bin", archive_entry_pathname(ae));
740 assertA(ARCHIVE_EOF == archive_read_next_header(a, &ae));
741 EPILOGUE();
742 }
743
DEFINE_TEST(test_read_format_rar5_solid_skip_all_but_last)744 DEFINE_TEST(test_read_format_rar5_solid_skip_all_but_last)
745 {
746 const char* reffile = "test_read_format_rar5_solid.rar";
747
748 /* Skip all but last, extract last */
749
750 PROLOGUE(reffile);
751 assertA(0 == archive_read_next_header(a, &ae));
752 assertEqualString("test.bin", archive_entry_pathname(ae));
753 assertA(0 == archive_read_next_header(a, &ae));
754 assertEqualString("test1.bin", archive_entry_pathname(ae));
755 assertA(0 == archive_read_next_header(a, &ae));
756 assertEqualString("test2.bin", archive_entry_pathname(ae));
757 assertA(0 == archive_read_next_header(a, &ae));
758 assertEqualString("test3.bin", archive_entry_pathname(ae));
759 assertA(0 == archive_read_next_header(a, &ae));
760 assertEqualString("test4.bin", archive_entry_pathname(ae));
761 assertA(0 == archive_read_next_header(a, &ae));
762 assertEqualString("test5.bin", archive_entry_pathname(ae));
763 assertA(0 == archive_read_next_header(a, &ae));
764 assertEqualString("test6.bin", archive_entry_pathname(ae));
765 assertA(0 == extract_one(a, ae, 0x36A448FF));
766 assertA(ARCHIVE_EOF == archive_read_next_header(a, &ae));
767 EPILOGUE();
768 }
769
DEFINE_TEST(test_read_format_rar5_extract_win32)770 DEFINE_TEST(test_read_format_rar5_extract_win32)
771 {
772 PROLOGUE("test_read_format_rar5_win32.rar");
773 assertA(0 == archive_read_next_header(a, &ae));
774 assertEqualString("testdir", archive_entry_pathname(ae));
775 assertEqualInt(archive_entry_mode(ae), AE_IFDIR | 0755);
776 assertA(0 == extract_one(a, ae, 0));
777 assertA(0 == archive_read_next_header(a, &ae));
778 assertEqualString("test.bin", archive_entry_pathname(ae));
779 assertEqualInt(archive_entry_mode(ae), AE_IFREG | 0644);
780 assertA(0 == extract_one(a, ae, 0x7CCA70CD));
781 assertA(0 == archive_read_next_header(a, &ae));
782 assertEqualString("test1.bin", archive_entry_pathname(ae));
783 assertEqualInt(archive_entry_mode(ae), AE_IFREG | 0644);
784 assertA(0 == extract_one(a, ae, 0x7E13B2C6));
785 assertA(0 == archive_read_next_header(a, &ae));
786 /* Read only file */
787 assertEqualString("test2.bin", archive_entry_pathname(ae));
788 assertEqualInt(archive_entry_mode(ae), AE_IFREG | 0444);
789 assertA(0 == extract_one(a, ae, 0xF166AFCB));
790 assertA(0 == archive_read_next_header(a, &ae));
791 assertEqualString("test3.bin", archive_entry_pathname(ae));
792 assertEqualInt(archive_entry_mode(ae), AE_IFREG | 0644);
793 assertA(0 == extract_one(a, ae, 0x9FB123D9));
794 assertA(0 == archive_read_next_header(a, &ae));
795 assertEqualString("test4.bin", archive_entry_pathname(ae));
796 assertEqualInt(archive_entry_mode(ae), AE_IFREG | 0644);
797 assertA(0 == extract_one(a, ae, 0x10C43ED4));
798 assertA(0 == archive_read_next_header(a, &ae));
799 assertEqualString("test5.bin", archive_entry_pathname(ae));
800 assertEqualInt(archive_entry_mode(ae), AE_IFREG | 0644);
801 assertA(0 == extract_one(a, ae, 0xB9D155F2));
802 assertA(0 == archive_read_next_header(a, &ae));
803 assertEqualString("test6.bin", archive_entry_pathname(ae));
804 assertEqualInt(archive_entry_mode(ae), AE_IFREG | 0644);
805 assertA(0 == extract_one(a, ae, 0x36A448FF));
806 EPILOGUE();
807 }
808
DEFINE_TEST(test_read_format_rar5_unicode)809 DEFINE_TEST(test_read_format_rar5_unicode)
810 {
811 #if !defined(WIN32) || defined(__CYGWIN__)
812 skipping("Skipping test on non-Windows");
813 return;
814 #else
815 /* Corresponds to the names:
816 * ����.txt
817 * ���������������� ��������.txt
818 * Ⓗⓐⓡⓓ Ⓛⓘⓝⓚ.txt */
819 const wchar_t* emoji_name = L"\U0001f44b\U0001f30e.txt";
820 const wchar_t* italic_name = L"\U0001d4ae\U0001d4ce\U0001d4c2\U0001d4b7\U0001d45c\U0001d4c1\U0001d4be\U0001d4b8 \U0001d43f\U0001d4be\U0001d4c3\U0001d4c0.txt";
821 const wchar_t* circle_name = L"\u24bd\u24d0\u24e1\u24d3 \u24c1\u24d8\u24dd\u24da.txt";
822
823 PROLOGUE("test_read_format_rar5_unicode.rar");
824 assertA(0 == archive_read_next_header(a, &ae));
825 assertEqualWString(emoji_name, archive_entry_pathname_w(ae));
826 assertEqualInt(archive_entry_mode(ae), AE_IFREG | 0644);
827 assertA(0 == archive_read_next_header(a, &ae));
828 assertEqualWString(circle_name, archive_entry_pathname_w(ae));
829 assertEqualInt(archive_entry_mode(ae), AE_IFREG | 0644);
830 assertEqualWString(emoji_name, archive_entry_hardlink_w(ae));
831 assertA(0 == archive_read_next_header(a, &ae));
832 assertEqualWString(italic_name, archive_entry_pathname_w(ae));
833 assertEqualInt(archive_entry_mode(ae), AE_IFLNK | 0644);
834 assertEqualWString(emoji_name, archive_entry_symlink_w(ae));
835 EPILOGUE();
836 #endif
837 }
838
DEFINE_TEST(test_read_format_rar5_block_by_block)839 DEFINE_TEST(test_read_format_rar5_block_by_block)
840 {
841 /* This test uses strange buffer sizes intentionally. */
842
843 struct archive_entry *ae;
844 struct archive *a;
845 uint8_t buf[173];
846 ssize_t bytes_read;
847 uint32_t computed_crc = 0;
848
849 extract_reference_file("test_read_format_rar5_compressed.rar");
850 assert((a = archive_read_new()) != NULL);
851 assertA(0 == archive_read_support_filter_all(a));
852 assertA(0 == archive_read_support_format_all(a));
853 assertA(0 == archive_read_open_filename(a, "test_read_format_rar5_compressed.rar", 130));
854 assertA(0 == archive_read_next_header(a, &ae));
855 assertEqualString("test.bin", archive_entry_pathname(ae));
856 assertEqualInt(1200, archive_entry_size(ae));
857
858 /* File size is 1200 bytes, we're reading it using a buffer of 173 bytes.
859 * Libarchive is configured to use a buffer of 130 bytes. */
860
861 while(1) {
862 /* archive_read_data should return one of:
863 * a) 0, if there is no more data to be read,
864 * b) negative value, if there was an error,
865 * c) positive value, meaning how many bytes were read.
866 */
867
868 bytes_read = archive_read_data(a, buf, sizeof(buf));
869 assertA(bytes_read >= 0);
870 if(bytes_read <= 0)
871 break;
872
873 computed_crc = crc32(computed_crc, buf, bytes_read);
874 }
875
876 assertEqualInt(computed_crc, 0x7CCA70CD);
877 EPILOGUE();
878 }
879
DEFINE_TEST(test_read_format_rar5_owner)880 DEFINE_TEST(test_read_format_rar5_owner)
881 {
882 const int DATA_SIZE = 5;
883 uint8_t buff[5];
884
885 PROLOGUE("test_read_format_rar5_owner.rar");
886
887 assertA(0 == archive_read_next_header(a, &ae));
888 assertEqualString("root.txt", archive_entry_pathname(ae));
889 assertEqualString("root", archive_entry_uname(ae));
890 assertEqualString("wheel", archive_entry_gname(ae));
891 assertA((int) archive_entry_mtime(ae) > 0);
892 assertEqualInt(DATA_SIZE, archive_entry_size(ae));
893 assertA(DATA_SIZE == archive_read_data(a, buff, DATA_SIZE));
894
895 assertA(0 == archive_read_next_header(a, &ae));
896 assertEqualString("nobody.txt", archive_entry_pathname(ae));
897 assertEqualString("nobody", archive_entry_uname(ae));
898 assertEqualString("nogroup", archive_entry_gname(ae));
899 assertA((int) archive_entry_mtime(ae) > 0);
900 assertEqualInt(DATA_SIZE, archive_entry_size(ae));
901 assertA(DATA_SIZE == archive_read_data(a, buff, DATA_SIZE));
902
903 assertA(0 == archive_read_next_header(a, &ae));
904 assertEqualString("numeric.txt", archive_entry_pathname(ae));
905 assertEqualInt(9999, archive_entry_uid(ae));
906 assertEqualInt(8888, archive_entry_gid(ae));
907 assertA((int) archive_entry_mtime(ae) > 0);
908 assertEqualInt(DATA_SIZE, archive_entry_size(ae));
909 assertA(DATA_SIZE == archive_read_data(a, buff, DATA_SIZE));
910
911 assertA(ARCHIVE_EOF == archive_read_next_header(a, &ae));
912
913 EPILOGUE();
914 }
915
DEFINE_TEST(test_read_format_rar5_symlink)916 DEFINE_TEST(test_read_format_rar5_symlink)
917 {
918 const int DATA_SIZE = 5;
919 uint8_t buff[5];
920
921 PROLOGUE("test_read_format_rar5_symlink.rar");
922
923 assertA(0 == archive_read_next_header(a, &ae));
924 assertEqualString("file.txt", archive_entry_pathname(ae));
925 assertEqualInt(AE_IFREG, archive_entry_filetype(ae));
926 assertA((int) archive_entry_mtime(ae) > 0);
927 assertEqualInt(DATA_SIZE, archive_entry_size(ae));
928 assertA(DATA_SIZE == archive_read_data(a, buff, DATA_SIZE));
929
930 assertA(0 == archive_read_next_header(a, &ae));
931 assertEqualString("symlink.txt", archive_entry_pathname(ae));
932 assertEqualInt(AE_IFLNK, archive_entry_filetype(ae));
933 assertEqualString("file.txt", archive_entry_symlink(ae));
934 assertEqualInt(AE_SYMLINK_TYPE_FILE, archive_entry_symlink_type(ae));
935 assertEqualInt(0, archive_entry_size(ae));
936 assertA(0 == archive_read_data(a, NULL, (size_t)archive_entry_size(ae)));
937
938 assertA(0 == archive_read_next_header(a, &ae));
939 assertEqualString("dirlink", archive_entry_pathname(ae));
940 assertEqualInt(AE_IFLNK, archive_entry_filetype(ae));
941 assertEqualString("dir", archive_entry_symlink(ae));
942 assertEqualInt(AE_SYMLINK_TYPE_DIRECTORY, archive_entry_symlink_type(ae));
943 assertEqualInt(0, archive_entry_size(ae));
944 assertA(0 == archive_read_data(a, NULL, (size_t)archive_entry_size(ae)));
945
946 assertA(0 == archive_read_next_header(a, &ae));
947 assertEqualString("dir", archive_entry_pathname(ae));
948 assertEqualInt(AE_IFDIR, archive_entry_filetype(ae));
949 assertEqualInt(0, archive_entry_size(ae));
950 assertA(0 == archive_read_data(a, NULL, (size_t)archive_entry_size(ae)));
951
952 assertA(ARCHIVE_EOF == archive_read_next_header(a, &ae));
953
954 EPILOGUE();
955 }
956
DEFINE_TEST(test_read_format_rar5_hardlink)957 DEFINE_TEST(test_read_format_rar5_hardlink)
958 {
959 const int DATA_SIZE = 5;
960 uint8_t buff[5];
961
962 PROLOGUE("test_read_format_rar5_hardlink.rar");
963
964 assertA(0 == archive_read_next_header(a, &ae));
965 assertEqualString("file.txt", archive_entry_pathname(ae));
966 assertEqualInt(AE_IFREG, archive_entry_filetype(ae));
967 assertA((int) archive_entry_mtime(ae) > 0);
968 assertEqualInt(DATA_SIZE, archive_entry_size(ae));
969 assertA(DATA_SIZE == archive_read_data(a, buff, DATA_SIZE));
970
971 assertA(0 == archive_read_next_header(a, &ae));
972 assertEqualString("hardlink.txt", archive_entry_pathname(ae));
973 assertEqualInt(AE_IFREG, archive_entry_filetype(ae));
974 assertEqualString("file.txt", archive_entry_hardlink(ae));
975 assertEqualInt(0, archive_entry_size(ae));
976 assertA(0 == archive_read_data(a, NULL, (size_t)archive_entry_size(ae)));
977
978 assertA(ARCHIVE_EOF == archive_read_next_header(a, &ae));
979
980 EPILOGUE();
981 }
982
DEFINE_TEST(test_read_format_rar5_extra_field_version)983 DEFINE_TEST(test_read_format_rar5_extra_field_version)
984 {
985 PROLOGUE("test_read_format_rar5_extra_field_version.rar");
986
987 assertA(0 == archive_read_next_header(a, &ae));
988 assertEqualString("bin/2to3;1", archive_entry_pathname(ae));
989 assertA(0 == extract_one(a, ae, 0xF24181B7));
990
991 assertA(0 == archive_read_next_header(a, &ae));
992 assertEqualString("bin/2to3", archive_entry_pathname(ae));
993 assertA(0 == extract_one(a, ae, 0xF24181B7));
994
995 assertA(ARCHIVE_EOF == archive_read_next_header(a, &ae));
996
997 EPILOGUE();
998 }
999
DEFINE_TEST(test_read_format_rar5_readtables_overflow)1000 DEFINE_TEST(test_read_format_rar5_readtables_overflow)
1001 {
1002 uint8_t buf[16];
1003
1004 PROLOGUE("test_read_format_rar5_readtables_overflow.rar");
1005
1006 /* This archive is invalid. However, processing it shouldn't cause any
1007 * buffer overflow errors during reading rar5 tables. */
1008
1009 (void) archive_read_next_header(a, &ae);
1010 (void) archive_read_data(a, buf, sizeof(buf));
1011 (void) archive_read_next_header(a, &ae);
1012
1013 EPILOGUE();
1014 }
1015
DEFINE_TEST(test_read_format_rar5_leftshift1)1016 DEFINE_TEST(test_read_format_rar5_leftshift1)
1017 {
1018 uint8_t buf[16];
1019
1020 PROLOGUE("test_read_format_rar5_leftshift1.rar");
1021
1022 /* This archive is invalid. However, processing it shouldn't cause any
1023 * errors related to undefined operations when using -fsanitize. */
1024
1025 (void) archive_read_next_header(a, &ae);
1026 (void) archive_read_data(a, buf, sizeof(buf));
1027 (void) archive_read_next_header(a, &ae);
1028
1029 EPILOGUE();
1030 }
1031
DEFINE_TEST(test_read_format_rar5_leftshift2)1032 DEFINE_TEST(test_read_format_rar5_leftshift2)
1033 {
1034 uint8_t buf[16];
1035
1036 PROLOGUE("test_read_format_rar5_leftshift2.rar");
1037
1038 /* This archive is invalid. However, processing it shouldn't cause any
1039 * errors related to undefined operations when using -fsanitize. */
1040
1041 (void) archive_read_next_header(a, &ae);
1042 (void) archive_read_data(a, buf, sizeof(buf));
1043 (void) archive_read_next_header(a, &ae);
1044
1045 EPILOGUE();
1046 }
1047
DEFINE_TEST(test_read_format_rar5_truncated_huff)1048 DEFINE_TEST(test_read_format_rar5_truncated_huff)
1049 {
1050 uint8_t buf[16];
1051
1052 PROLOGUE("test_read_format_rar5_truncated_huff.rar");
1053
1054 /* This archive is invalid. However, processing it shouldn't cause any
1055 * errors related to undefined operations when using -fsanitize. */
1056
1057 (void) archive_read_next_header(a, &ae);
1058 (void) archive_read_data(a, buf, sizeof(buf));
1059 (void) archive_read_next_header(a, &ae);
1060
1061 EPILOGUE();
1062 }
1063
DEFINE_TEST(test_read_format_rar5_invalid_dict_reference)1064 DEFINE_TEST(test_read_format_rar5_invalid_dict_reference)
1065 {
1066 uint8_t buf[16];
1067
1068 PROLOGUE("test_read_format_rar5_invalid_dict_reference.rar");
1069
1070 /* This test should fail on parsing the header. */
1071 assertA(archive_read_next_header(a, &ae) != ARCHIVE_OK);
1072
1073 /* This archive is invalid. However, processing it shouldn't cause any
1074 * errors related to buffer underflow when using -fsanitize. */
1075 assertA(archive_read_data(a, buf, sizeof(buf)) <= 0);
1076
1077 /* This test only cares about not returning success here. */
1078 assertA(ARCHIVE_OK != archive_read_next_header(a, &ae));
1079
1080 EPILOGUE();
1081 }
1082
DEFINE_TEST(test_read_format_rar5_distance_overflow)1083 DEFINE_TEST(test_read_format_rar5_distance_overflow)
1084 {
1085 uint8_t buf[16];
1086
1087 PROLOGUE("test_read_format_rar5_distance_overflow.rar");
1088
1089 /* This archive is invalid. However, processing it shouldn't cause any
1090 * errors related to variable overflows when using -fsanitize. */
1091
1092 (void) archive_read_next_header(a, &ae);
1093 (void) archive_read_data(a, buf, sizeof(buf));
1094 (void) archive_read_next_header(a, &ae);
1095
1096 EPILOGUE();
1097 }
1098
DEFINE_TEST(test_read_format_rar5_nonempty_dir_stream)1099 DEFINE_TEST(test_read_format_rar5_nonempty_dir_stream)
1100 {
1101 uint8_t buf[16];
1102
1103 PROLOGUE("test_read_format_rar5_nonempty_dir_stream.rar");
1104
1105 /* This archive is invalid. However, processing it shouldn't cause any
1106 * errors related to buffer overflows when using -fsanitize. */
1107
1108 (void) archive_read_next_header(a, &ae);
1109 (void) archive_read_data(a, buf, sizeof(buf));
1110 (void) archive_read_next_header(a, &ae);
1111
1112 EPILOGUE();
1113 }
1114
DEFINE_TEST(test_read_format_rar5_fileattr)1115 DEFINE_TEST(test_read_format_rar5_fileattr)
1116 {
1117 unsigned long set, clear, flag;
1118
1119 flag = 0;
1120
1121 PROLOGUE("test_read_format_rar5_fileattr.rar");
1122
1123 assertA(0 == archive_read_next_header(a, &ae));
1124 assertEqualInt(archive_entry_mode(ae), 0444 | AE_IFREG);
1125 assertEqualString("readonly.txt", archive_entry_pathname(ae));
1126 assertEqualString("rdonly", archive_entry_fflags_text(ae));
1127 archive_entry_fflags(ae, &set, &clear);
1128 #if defined(__FreeBSD__)
1129 flag = UF_READONLY;
1130 #elif defined(_WIN32) && !defined(CYGWIN)
1131 flag = FILE_ATTRIBUTE_READONLY;
1132 #endif
1133 assertEqualInt(flag, set & flag);
1134
1135 assertA(0 == archive_read_next_header(a, &ae));
1136 assertEqualInt(archive_entry_mode(ae), 0644 | AE_IFREG);
1137 assertEqualString("hidden.txt", archive_entry_pathname(ae));
1138 assertEqualString("hidden", archive_entry_fflags_text(ae));
1139 archive_entry_fflags(ae, &set, &clear);
1140 #if defined(__FreeBSD__)
1141 flag = UF_HIDDEN;
1142 #elif defined(_WIN32) && !defined(CYGWIN)
1143 flag = FILE_ATTRIBUTE_HIDDEN;
1144 #endif
1145 assertEqualInt(flag, set & flag);
1146
1147 assertA(0 == archive_read_next_header(a, &ae));
1148 assertEqualInt(archive_entry_mode(ae), 0644 | AE_IFREG);
1149 assertEqualString("system.txt", archive_entry_pathname(ae));
1150 assertEqualString("system", archive_entry_fflags_text(ae));
1151 archive_entry_fflags(ae, &set, &clear);
1152 #if defined(__FreeBSD__)
1153 flag = UF_SYSTEM;;
1154 #elif defined(_WIN32) && !defined(CYGWIN)
1155 flag = FILE_ATTRIBUTE_SYSTEM;
1156 #endif
1157 assertEqualInt(flag, set & flag);
1158
1159 assertA(0 == archive_read_next_header(a, &ae));
1160 assertEqualInt(archive_entry_mode(ae), 0444 | AE_IFREG);
1161 assertEqualString("ro_hidden.txt", archive_entry_pathname(ae));
1162 assertEqualString("rdonly,hidden", archive_entry_fflags_text(ae));
1163 archive_entry_fflags(ae, &set, &clear);
1164 #if defined(__FreeBSD__)
1165 flag = UF_READONLY | UF_HIDDEN;
1166 #elif defined(_WIN32) && !defined(CYGWIN)
1167 flag = FILE_ATTRIBUTE_READONLY | FILE_ATTRIBUTE_HIDDEN;
1168 #endif
1169 assertEqualInt(flag, set & flag);
1170
1171 assertA(0 == archive_read_next_header(a, &ae));
1172 assertEqualInt(archive_entry_mode(ae), 0555 | AE_IFDIR);
1173 assertEqualString("dir_readonly", archive_entry_pathname(ae));
1174 assertEqualString("rdonly", archive_entry_fflags_text(ae));
1175 archive_entry_fflags(ae, &set, &clear);
1176 #if defined(__FreeBSD__)
1177 flag = UF_READONLY;
1178 #elif defined(_WIN32) && !defined(CYGWIN)
1179 flag = FILE_ATTRIBUTE_READONLY;
1180 #endif
1181 assertEqualInt(flag, set & flag);
1182
1183 assertA(0 == archive_read_next_header(a, &ae));
1184 assertEqualInt(archive_entry_mode(ae), 0755 | AE_IFDIR);
1185 assertEqualString("dir_hidden", archive_entry_pathname(ae));
1186 assertEqualString("hidden", archive_entry_fflags_text(ae));
1187 archive_entry_fflags(ae, &set, &clear);
1188 #if defined(__FreeBSD__)
1189 flag = UF_HIDDEN;
1190 #elif defined(_WIN32) && !defined(CYGWIN)
1191 flag = FILE_ATTRIBUTE_HIDDEN;
1192 #endif
1193 assertEqualInt(flag, set & flag);
1194
1195 assertA(0 == archive_read_next_header(a, &ae));
1196 assertEqualInt(archive_entry_mode(ae), 0755 | AE_IFDIR);
1197 assertEqualString("dir_system", archive_entry_pathname(ae));
1198 assertEqualString("system", archive_entry_fflags_text(ae));
1199 archive_entry_fflags(ae, &set, &clear);
1200 #if defined(__FreeBSD__)
1201 flag = UF_SYSTEM;
1202 #elif defined(_WIN32) && !defined(CYGWIN)
1203 flag = FILE_ATTRIBUTE_SYSTEM;
1204 #endif
1205 assertEqualInt(flag, set & flag);
1206
1207 assertA(0 == archive_read_next_header(a, &ae));
1208 assertEqualInt(archive_entry_mode(ae), 0555 | AE_IFDIR);
1209 assertEqualString("dir_rohidden", archive_entry_pathname(ae));
1210 assertEqualString("rdonly,hidden", archive_entry_fflags_text(ae));
1211 archive_entry_fflags(ae, &set, &clear);
1212 #if defined(__FreeBSD__)
1213 flag = UF_READONLY | UF_HIDDEN;
1214 #elif defined(_WIN32) && !defined(CYGWIN)
1215 flag = FILE_ATTRIBUTE_READONLY | FILE_ATTRIBUTE_HIDDEN;
1216 #endif
1217 assertEqualInt(flag, set & flag);
1218
1219 EPILOGUE();
1220 }
1221
DEFINE_TEST(test_read_format_rar5_different_window_size)1222 DEFINE_TEST(test_read_format_rar5_different_window_size)
1223 {
1224 char buf[4096];
1225 PROLOGUE("test_read_format_rar5_different_window_size.rar");
1226
1227 /* Return codes of those calls are ignored, because this sample file
1228 * is invalid. However, the unpacker shouldn't produce any SIGSEGV
1229 * errors during processing. */
1230
1231 (void) archive_read_next_header(a, &ae);
1232 while(0 < archive_read_data(a, buf, sizeof(buf))) {}
1233
1234 (void) archive_read_next_header(a, &ae);
1235 while(0 < archive_read_data(a, buf, sizeof(buf))) {}
1236
1237 (void) archive_read_next_header(a, &ae);
1238 while(0 < archive_read_data(a, buf, sizeof(buf))) {}
1239
1240 EPILOGUE();
1241 }
1242
DEFINE_TEST(test_read_format_rar5_window_buf_and_size_desync)1243 DEFINE_TEST(test_read_format_rar5_window_buf_and_size_desync)
1244 {
1245 /* oss fuzz 30442 */
1246
1247 char buf[4096];
1248 PROLOGUE("test_read_format_rar5_window_buf_and_size_desync.rar");
1249
1250 /* Return codes of those calls are ignored, because this sample file
1251 * is invalid. However, the unpacker shouldn't produce any SIGSEGV
1252 * errors during processing. */
1253
1254 (void) archive_read_next_header(a, &ae);
1255 while(0 < archive_read_data(a, buf, 46)) {}
1256
1257 EPILOGUE();
1258 }
1259
DEFINE_TEST(test_read_format_rar5_arm_filter_on_window_boundary)1260 DEFINE_TEST(test_read_format_rar5_arm_filter_on_window_boundary)
1261 {
1262 char buf[4096];
1263 PROLOGUE("test_read_format_rar5_arm_filter_on_window_boundary.rar");
1264
1265 /* Return codes of those calls are ignored, because this sample file
1266 * is invalid. However, the unpacker shouldn't produce any SIGSEGV
1267 * errors during processing. */
1268
1269 (void) archive_read_next_header(a, &ae);
1270 while(0 < archive_read_data(a, buf, sizeof(buf))) {}
1271
1272 EPILOGUE();
1273 }
1274
DEFINE_TEST(test_read_format_rar5_different_solid_window_size)1275 DEFINE_TEST(test_read_format_rar5_different_solid_window_size)
1276 {
1277 char buf[4096];
1278 PROLOGUE("test_read_format_rar5_different_solid_window_size.rar");
1279
1280 /* Return codes of those calls are ignored, because this sample file
1281 * is invalid. However, the unpacker shouldn't produce any SIGSEGV
1282 * errors during processing. */
1283
1284 (void) archive_read_next_header(a, &ae);
1285 while(0 < archive_read_data(a, buf, sizeof(buf))) {}
1286
1287 (void) archive_read_next_header(a, &ae);
1288 while(0 < archive_read_data(a, buf, sizeof(buf))) {}
1289
1290 (void) archive_read_next_header(a, &ae);
1291 while(0 < archive_read_data(a, buf, sizeof(buf))) {}
1292
1293 EPILOGUE();
1294 }
1295
DEFINE_TEST(test_read_format_rar5_different_winsize_on_merge)1296 DEFINE_TEST(test_read_format_rar5_different_winsize_on_merge)
1297 {
1298 char buf[4096];
1299 PROLOGUE("test_read_format_rar5_different_winsize_on_merge.rar");
1300
1301 /* Return codes of those calls are ignored, because this sample file
1302 * is invalid. However, the unpacker shouldn't produce any SIGSEGV
1303 * errors during processing. */
1304
1305 (void) archive_read_next_header(a, &ae);
1306 while(0 < archive_read_data(a, buf, sizeof(buf))) {}
1307
1308 EPILOGUE();
1309 }
1310
DEFINE_TEST(test_read_format_rar5_block_size_is_too_small)1311 DEFINE_TEST(test_read_format_rar5_block_size_is_too_small)
1312 {
1313 char buf[4096];
1314 PROLOGUE("test_read_format_rar5_block_size_is_too_small.rar");
1315
1316 /* This file is damaged, so those functions should return failure.
1317 * Additionally, SIGSEGV shouldn't be raised during execution
1318 * of those functions. */
1319
1320 assertA(archive_read_next_header(a, &ae) != ARCHIVE_OK);
1321 assertA(archive_read_data(a, buf, sizeof(buf)) <= 0);
1322
1323 EPILOGUE();
1324 }
1325
DEFINE_TEST(test_read_format_rar5_sfx)1326 DEFINE_TEST(test_read_format_rar5_sfx)
1327 {
1328 struct archive *a;
1329 struct archive_entry *ae;
1330 int bs = 10240;
1331 char buff[32];
1332 const char reffile[] = "test_read_format_rar5_sfx.exe";
1333 const char test_txt[] = "123";
1334 int size = sizeof(test_txt) - 1;
1335
1336 extract_reference_file(reffile);
1337 assert((a = archive_read_new()) != NULL);
1338 assertA(0 == archive_read_support_filter_all(a));
1339 assertA(0 == archive_read_support_format_all(a));
1340 assertA(0 == archive_read_open_filename(a, reffile, bs));
1341
1342 assertA(0 == archive_read_next_header(a, &ae));
1343 assertEqualString("test.txt.txt", archive_entry_pathname(ae));
1344
1345 assertA(size == archive_read_data(a, buff, size));
1346 assertEqualMem(buff, test_txt, size);
1347
1348 EPILOGUE();
1349 }
1350
DEFINE_TEST(test_read_format_rar5_decode_number_out_of_bounds_read)1351 DEFINE_TEST(test_read_format_rar5_decode_number_out_of_bounds_read)
1352 {
1353 /* oss fuzz 30448 */
1354
1355 char buf[4096];
1356 PROLOGUE("test_read_format_rar5_decode_number_out_of_bounds_read.rar");
1357
1358 /* Return codes of those calls are ignored, because this sample file
1359 * is invalid. However, the unpacker shouldn't produce any SIGSEGV
1360 * errors during processing. */
1361
1362 (void) archive_read_next_header(a, &ae);
1363 while(0 < archive_read_data(a, buf, sizeof(buf))) {}
1364
1365 EPILOGUE();
1366 }
1367
DEFINE_TEST(test_read_format_rar5_bad_window_size_in_multiarchive_file)1368 DEFINE_TEST(test_read_format_rar5_bad_window_size_in_multiarchive_file)
1369 {
1370 /* oss fuzz 30459 */
1371
1372 char buf[4096];
1373 PROLOGUE("test_read_format_rar5_bad_window_sz_in_mltarc_file.rar");
1374
1375 /* This file is damaged, so those functions should return failure.
1376 * Additionally, SIGSEGV shouldn't be raised during execution
1377 * of those functions. */
1378
1379 (void) archive_read_next_header(a, &ae);
1380 while(0 < archive_read_data(a, buf, sizeof(buf))) {}
1381 (void) archive_read_next_header(a, &ae);
1382 while(0 < archive_read_data(a, buf, sizeof(buf))) {}
1383
1384 EPILOGUE();
1385 }
1386
DEFINE_TEST(test_read_format_rar5_read_data_block_uninitialized_offset)1387 DEFINE_TEST(test_read_format_rar5_read_data_block_uninitialized_offset)
1388 {
1389 const void *buf;
1390 size_t size;
1391 la_int64_t offset;
1392
1393 PROLOGUE("test_read_format_rar5_compressed.rar");
1394 assertA(0 == archive_read_next_header(a, &ae));
1395
1396 /* A real code may pass a pointer to an uninitialized variable as an offset
1397 * output argument. Here we want to check this situation. But because
1398 * relying on a value of an uninitialized variable in a test is not a good
1399 * idea, let's pretend that 0xdeadbeef is a random value of the
1400 * uninitialized variable. */
1401 offset = 0xdeadbeef;
1402 assertEqualInt(ARCHIVE_OK, archive_read_data_block(a, &buf, &size, &offset));
1403 /* The test archive doesn't contain a sparse file. And because of that, here
1404 * we assume that the first returned offset should be 0. */
1405 assertEqualInt(0, offset);
1406
1407 EPILOGUE();
1408 }
1409
DEFINE_TEST(test_read_format_rar5_data_ready_pointer_leak)1410 DEFINE_TEST(test_read_format_rar5_data_ready_pointer_leak)
1411 {
1412 /* oss fuzz 70024 */
1413
1414 char buf[4096];
1415 PROLOGUE("test_read_format_rar5_data_ready_pointer_leak.rar");
1416
1417 /* Return codes of those calls are ignored, because this sample file
1418 * is invalid. However, the unpacker shouldn't produce any SIGSEGV
1419 * errors during processing. */
1420
1421 (void) archive_read_next_header(a, &ae);
1422 (void) archive_read_data(a, buf, sizeof(buf));
1423 (void) archive_read_next_header(a, &ae);
1424 (void) archive_read_data(a, buf, sizeof(buf));
1425 (void) archive_read_data(a, buf, sizeof(buf));
1426 (void) archive_read_next_header(a, &ae);
1427 /* This call shouldn't produce SIGSEGV. */
1428 (void) archive_read_data(a, buf, sizeof(buf));
1429
1430 EPILOGUE();
1431 }
1432