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
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
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 
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 
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 
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 
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 
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 
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 
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 
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 
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 
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 
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 
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 
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 
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 
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 
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 
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 
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 
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 
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 
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 
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 
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 
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 
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 
809 DEFINE_TEST(test_read_format_rar5_block_by_block)
810 {
811 	/* This test uses strange buffer sizes intentionally. */
812 
813 	struct archive_entry *ae;
814 	struct archive *a;
815 	uint8_t buf[173];
816 	int bytes_read;
817 	uint32_t computed_crc = 0;
818 
819 	extract_reference_file("test_read_format_rar5_compressed.rar");
820 	assert((a = archive_read_new()) != NULL);
821 	assertA(0 == archive_read_support_filter_all(a));
822 	assertA(0 == archive_read_support_format_all(a));
823 	assertA(0 == archive_read_open_filename(a, "test_read_format_rar5_compressed.rar", 130));
824 	assertA(0 == archive_read_next_header(a, &ae));
825 	assertEqualString("test.bin", archive_entry_pathname(ae));
826 	assertEqualInt(1200, archive_entry_size(ae));
827 
828 	/* File size is 1200 bytes, we're reading it using a buffer of 173 bytes.
829 	 * Libarchive is configured to use a buffer of 130 bytes. */
830 
831 	while(1) {
832 		/* archive_read_data should return one of:
833 		 * a) 0, if there is no more data to be read,
834 		 * b) negative value, if there was an error,
835 		 * c) positive value, meaning how many bytes were read.
836 		 */
837 
838 		bytes_read = archive_read_data(a, buf, sizeof(buf));
839 		assertA(bytes_read >= 0);
840 		if(bytes_read <= 0)
841 			break;
842 
843 		computed_crc = crc32(computed_crc, buf, bytes_read);
844 	}
845 
846 	assertEqualInt(computed_crc, 0x7CCA70CD);
847 	EPILOGUE();
848 }
849 
850 DEFINE_TEST(test_read_format_rar5_owner)
851 {
852 	const int DATA_SIZE = 5;
853 	uint8_t buff[5];
854 
855 	PROLOGUE("test_read_format_rar5_owner.rar");
856 
857 	assertA(0 == archive_read_next_header(a, &ae));
858 	assertEqualString("root.txt", archive_entry_pathname(ae));
859 	assertEqualString("root", archive_entry_uname(ae));
860 	assertEqualString("wheel", archive_entry_gname(ae));
861 	assertA((int) archive_entry_mtime(ae) > 0);
862 	assertEqualInt(DATA_SIZE, archive_entry_size(ae));
863 	assertA(DATA_SIZE == archive_read_data(a, buff, DATA_SIZE));
864 
865 	assertA(0 == archive_read_next_header(a, &ae));
866 	assertEqualString("nobody.txt", archive_entry_pathname(ae));
867 	assertEqualString("nobody", archive_entry_uname(ae));
868 	assertEqualString("nogroup", archive_entry_gname(ae));
869 	assertA((int) archive_entry_mtime(ae) > 0);
870 	assertEqualInt(DATA_SIZE, archive_entry_size(ae));
871 	assertA(DATA_SIZE == archive_read_data(a, buff, DATA_SIZE));
872 
873 	assertA(0 == archive_read_next_header(a, &ae));
874 	assertEqualString("numeric.txt", archive_entry_pathname(ae));
875 	assertEqualInt(9999, archive_entry_uid(ae));
876 	assertEqualInt(8888, archive_entry_gid(ae));
877 	assertA((int) archive_entry_mtime(ae) > 0);
878 	assertEqualInt(DATA_SIZE, archive_entry_size(ae));
879 	assertA(DATA_SIZE == archive_read_data(a, buff, DATA_SIZE));
880 
881 	assertA(ARCHIVE_EOF == archive_read_next_header(a, &ae));
882 
883 	EPILOGUE();
884 }
885 
886 DEFINE_TEST(test_read_format_rar5_symlink)
887 {
888 	const int DATA_SIZE = 5;
889 	uint8_t buff[5];
890 
891 	PROLOGUE("test_read_format_rar5_symlink.rar");
892 
893 	assertA(0 == archive_read_next_header(a, &ae));
894 	assertEqualString("file.txt", archive_entry_pathname(ae));
895 	assertEqualInt(AE_IFREG, archive_entry_filetype(ae));
896 	assertA((int) archive_entry_mtime(ae) > 0);
897 	assertEqualInt(DATA_SIZE, archive_entry_size(ae));
898 	assertA(DATA_SIZE == archive_read_data(a, buff, DATA_SIZE));
899 
900 	assertA(0 == archive_read_next_header(a, &ae));
901 	assertEqualString("symlink.txt", archive_entry_pathname(ae));
902 	assertEqualInt(AE_IFLNK, archive_entry_filetype(ae));
903 	assertEqualString("file.txt", archive_entry_symlink(ae));
904 	assertEqualInt(AE_SYMLINK_TYPE_FILE, archive_entry_symlink_type(ae));
905 	assertA(0 == archive_read_data(a, NULL, archive_entry_size(ae)));
906 
907 	assertA(0 == archive_read_next_header(a, &ae));
908 	assertEqualString("dirlink", archive_entry_pathname(ae));
909 	assertEqualInt(AE_IFLNK, archive_entry_filetype(ae));
910 	assertEqualString("dir", archive_entry_symlink(ae));
911 	assertEqualInt(AE_SYMLINK_TYPE_DIRECTORY, archive_entry_symlink_type(ae));
912 	assertA(0 == archive_read_data(a, NULL, archive_entry_size(ae)));
913 
914 	assertA(0 == archive_read_next_header(a, &ae));
915 	assertEqualString("dir", archive_entry_pathname(ae));
916 	assertEqualInt(AE_IFDIR, archive_entry_filetype(ae));
917 	assertA(0 == archive_read_data(a, NULL, archive_entry_size(ae)));
918 
919 	assertA(ARCHIVE_EOF == archive_read_next_header(a, &ae));
920 
921 	EPILOGUE();
922 }
923 
924 DEFINE_TEST(test_read_format_rar5_hardlink)
925 {
926 	const int DATA_SIZE = 5;
927 	uint8_t buff[5];
928 
929 	PROLOGUE("test_read_format_rar5_hardlink.rar");
930 
931 	assertA(0 == archive_read_next_header(a, &ae));
932 	assertEqualString("file.txt", archive_entry_pathname(ae));
933 	assertEqualInt(AE_IFREG, archive_entry_filetype(ae));
934 	assertA((int) archive_entry_mtime(ae) > 0);
935 	assertEqualInt(DATA_SIZE, archive_entry_size(ae));
936 	assertA(DATA_SIZE == archive_read_data(a, buff, DATA_SIZE));
937 
938 	assertA(0 == archive_read_next_header(a, &ae));
939 	assertEqualString("hardlink.txt", archive_entry_pathname(ae));
940 	assertEqualInt(AE_IFREG, archive_entry_filetype(ae));
941 	assertEqualString("file.txt", archive_entry_hardlink(ae));
942 	assertA(0 == archive_read_data(a, NULL, archive_entry_size(ae)));
943 
944 	assertA(ARCHIVE_EOF == archive_read_next_header(a, &ae));
945 
946 	EPILOGUE();
947 }
948 
949 DEFINE_TEST(test_read_format_rar5_extra_field_version)
950 {
951 	PROLOGUE("test_read_format_rar5_extra_field_version.rar");
952 
953 	assertA(0 == archive_read_next_header(a, &ae));
954 	assertEqualString("bin/2to3;1", archive_entry_pathname(ae));
955 	assertA(0 == extract_one(a, ae, 0xF24181B7));
956 
957 	assertA(0 == archive_read_next_header(a, &ae));
958 	assertEqualString("bin/2to3", archive_entry_pathname(ae));
959 	assertA(0 == extract_one(a, ae, 0xF24181B7));
960 
961 	assertA(ARCHIVE_EOF == archive_read_next_header(a, &ae));
962 
963 	EPILOGUE();
964 }
965 
966 DEFINE_TEST(test_read_format_rar5_readtables_overflow)
967 {
968 	uint8_t buf[16];
969 
970 	PROLOGUE("test_read_format_rar5_readtables_overflow.rar");
971 
972 	/* This archive is invalid. However, processing it shouldn't cause any
973 	 * buffer overflow errors during reading rar5 tables. */
974 
975 	(void) archive_read_next_header(a, &ae);
976 	(void) archive_read_data(a, buf, sizeof(buf));
977 	(void) archive_read_next_header(a, &ae);
978 
979 	EPILOGUE();
980 }
981 
982 DEFINE_TEST(test_read_format_rar5_leftshift1)
983 {
984 	uint8_t buf[16];
985 
986 	PROLOGUE("test_read_format_rar5_leftshift1.rar");
987 
988 	/* This archive is invalid. However, processing it shouldn't cause any
989 	 * errors related to undefined operations when using -fsanitize. */
990 
991 	(void) archive_read_next_header(a, &ae);
992 	(void) archive_read_data(a, buf, sizeof(buf));
993 	(void) archive_read_next_header(a, &ae);
994 
995 	EPILOGUE();
996 }
997 
998 DEFINE_TEST(test_read_format_rar5_leftshift2)
999 {
1000 	uint8_t buf[16];
1001 
1002 	PROLOGUE("test_read_format_rar5_leftshift2.rar");
1003 
1004 	/* This archive is invalid. However, processing it shouldn't cause any
1005 	 * errors related to undefined operations when using -fsanitize. */
1006 
1007 	(void) archive_read_next_header(a, &ae);
1008 	(void) archive_read_data(a, buf, sizeof(buf));
1009 	(void) archive_read_next_header(a, &ae);
1010 
1011 	EPILOGUE();
1012 }
1013 
1014 DEFINE_TEST(test_read_format_rar5_truncated_huff)
1015 {
1016 	uint8_t buf[16];
1017 
1018 	PROLOGUE("test_read_format_rar5_truncated_huff.rar");
1019 
1020 	/* This archive is invalid. However, processing it shouldn't cause any
1021 	 * errors related to undefined operations when using -fsanitize. */
1022 
1023 	(void) archive_read_next_header(a, &ae);
1024 	(void) archive_read_data(a, buf, sizeof(buf));
1025 	(void) archive_read_next_header(a, &ae);
1026 
1027 	EPILOGUE();
1028 }
1029 
1030 DEFINE_TEST(test_read_format_rar5_invalid_dict_reference)
1031 {
1032 	uint8_t buf[16];
1033 
1034 	PROLOGUE("test_read_format_rar5_invalid_dict_reference.rar");
1035 
1036 	/* This test should fail on parsing the header. */
1037 	assertA(archive_read_next_header(a, &ae) != ARCHIVE_OK);
1038 
1039 	/* This archive is invalid. However, processing it shouldn't cause any
1040 	 * errors related to buffer underflow when using -fsanitize. */
1041 	assertA(archive_read_data(a, buf, sizeof(buf)) <= 0);
1042 
1043 	/* This test only cares about not returning success here. */
1044 	assertA(ARCHIVE_OK != archive_read_next_header(a, &ae));
1045 
1046 	EPILOGUE();
1047 }
1048 
1049 DEFINE_TEST(test_read_format_rar5_distance_overflow)
1050 {
1051 	uint8_t buf[16];
1052 
1053 	PROLOGUE("test_read_format_rar5_distance_overflow.rar");
1054 
1055 	/* This archive is invalid. However, processing it shouldn't cause any
1056 	 * errors related to variable overflows when using -fsanitize. */
1057 
1058 	(void) archive_read_next_header(a, &ae);
1059 	(void) archive_read_data(a, buf, sizeof(buf));
1060 	(void) archive_read_next_header(a, &ae);
1061 
1062 	EPILOGUE();
1063 }
1064 
1065 DEFINE_TEST(test_read_format_rar5_nonempty_dir_stream)
1066 {
1067 	uint8_t buf[16];
1068 
1069 	PROLOGUE("test_read_format_rar5_nonempty_dir_stream.rar");
1070 
1071 	/* This archive is invalid. However, processing it shouldn't cause any
1072 	 * errors related to buffer overflows when using -fsanitize. */
1073 
1074 	(void) archive_read_next_header(a, &ae);
1075 	(void) archive_read_data(a, buf, sizeof(buf));
1076 	(void) archive_read_next_header(a, &ae);
1077 
1078 	EPILOGUE();
1079 }
1080 
1081 DEFINE_TEST(test_read_format_rar5_fileattr)
1082 {
1083 	unsigned long set, clear, flag;
1084 
1085 	flag = 0;
1086 
1087 	PROLOGUE("test_read_format_rar5_fileattr.rar");
1088 
1089 	assertA(0 == archive_read_next_header(a, &ae));
1090 	assertEqualInt(archive_entry_mode(ae), 0444 | AE_IFREG);
1091 	assertEqualString("readonly.txt", archive_entry_pathname(ae));
1092 	assertEqualString("rdonly", archive_entry_fflags_text(ae));
1093 	archive_entry_fflags(ae, &set, &clear);
1094 #if defined(__FreeBSD__)
1095 	flag = UF_READONLY;
1096 #elif defined(_WIN32) && !defined(CYGWIN)
1097 	flag = FILE_ATTRIBUTE_READONLY;
1098 #endif
1099 	assertEqualInt(flag, set & flag);
1100 
1101 	assertA(0 == archive_read_next_header(a, &ae));
1102 	assertEqualInt(archive_entry_mode(ae), 0644 | AE_IFREG);
1103 	assertEqualString("hidden.txt", archive_entry_pathname(ae));
1104 	assertEqualString("hidden", archive_entry_fflags_text(ae));
1105 	archive_entry_fflags(ae, &set, &clear);
1106 #if defined(__FreeBSD__)
1107 	flag = UF_HIDDEN;
1108 #elif defined(_WIN32) && !defined(CYGWIN)
1109 	flag = FILE_ATTRIBUTE_HIDDEN;
1110 #endif
1111 	assertEqualInt(flag, set & flag);
1112 
1113 	assertA(0 == archive_read_next_header(a, &ae));
1114 	assertEqualInt(archive_entry_mode(ae), 0644 | AE_IFREG);
1115 	assertEqualString("system.txt", archive_entry_pathname(ae));
1116 	assertEqualString("system", archive_entry_fflags_text(ae));
1117 	archive_entry_fflags(ae, &set, &clear);
1118 #if defined(__FreeBSD__)
1119 	flag = UF_SYSTEM;;
1120 #elif defined(_WIN32) && !defined(CYGWIN)
1121 	flag = FILE_ATTRIBUTE_SYSTEM;
1122 #endif
1123 	assertEqualInt(flag, set & flag);
1124 
1125 	assertA(0 == archive_read_next_header(a, &ae));
1126 	assertEqualInt(archive_entry_mode(ae), 0444 | AE_IFREG);
1127 	assertEqualString("ro_hidden.txt", archive_entry_pathname(ae));
1128 	assertEqualString("rdonly,hidden", archive_entry_fflags_text(ae));
1129 	archive_entry_fflags(ae, &set, &clear);
1130 #if defined(__FreeBSD__)
1131 	flag = UF_READONLY | UF_HIDDEN;
1132 #elif defined(_WIN32) && !defined(CYGWIN)
1133 	flag = FILE_ATTRIBUTE_READONLY | FILE_ATTRIBUTE_HIDDEN;
1134 #endif
1135 	assertEqualInt(flag, set & flag);
1136 
1137 	assertA(0 == archive_read_next_header(a, &ae));
1138 	assertEqualInt(archive_entry_mode(ae), 0555 | AE_IFDIR);
1139 	assertEqualString("dir_readonly", archive_entry_pathname(ae));
1140 	assertEqualString("rdonly", archive_entry_fflags_text(ae));
1141 	archive_entry_fflags(ae, &set, &clear);
1142 #if defined(__FreeBSD__)
1143 	flag = UF_READONLY;
1144 #elif defined(_WIN32) && !defined(CYGWIN)
1145 	flag = FILE_ATTRIBUTE_READONLY;
1146 #endif
1147 	assertEqualInt(flag, set & flag);
1148 
1149 	assertA(0 == archive_read_next_header(a, &ae));
1150 	assertEqualInt(archive_entry_mode(ae), 0755 | AE_IFDIR);
1151 	assertEqualString("dir_hidden", archive_entry_pathname(ae));
1152 	assertEqualString("hidden", archive_entry_fflags_text(ae));
1153 	archive_entry_fflags(ae, &set, &clear);
1154 #if defined(__FreeBSD__)
1155 	flag = UF_HIDDEN;
1156 #elif defined(_WIN32) && !defined(CYGWIN)
1157 	flag = FILE_ATTRIBUTE_HIDDEN;
1158 #endif
1159 	assertEqualInt(flag, set & flag);
1160 
1161 	assertA(0 == archive_read_next_header(a, &ae));
1162 	assertEqualInt(archive_entry_mode(ae), 0755 | AE_IFDIR);
1163 	assertEqualString("dir_system", archive_entry_pathname(ae));
1164 	assertEqualString("system", archive_entry_fflags_text(ae));
1165 	archive_entry_fflags(ae, &set, &clear);
1166 #if defined(__FreeBSD__)
1167 	flag = UF_SYSTEM;
1168 #elif defined(_WIN32) && !defined(CYGWIN)
1169 	flag = FILE_ATTRIBUTE_SYSTEM;
1170 #endif
1171 	assertEqualInt(flag, set & flag);
1172 
1173 	assertA(0 == archive_read_next_header(a, &ae));
1174 	assertEqualInt(archive_entry_mode(ae), 0555 | AE_IFDIR);
1175 	assertEqualString("dir_rohidden", archive_entry_pathname(ae));
1176 	assertEqualString("rdonly,hidden", archive_entry_fflags_text(ae));
1177 	archive_entry_fflags(ae, &set, &clear);
1178 #if defined(__FreeBSD__)
1179 	flag = UF_READONLY | UF_HIDDEN;
1180 #elif defined(_WIN32) && !defined(CYGWIN)
1181 	flag = FILE_ATTRIBUTE_READONLY | FILE_ATTRIBUTE_HIDDEN;
1182 #endif
1183 	assertEqualInt(flag, set & flag);
1184 
1185 	EPILOGUE();
1186 }
1187 
1188 DEFINE_TEST(test_read_format_rar5_different_window_size)
1189 {
1190 	char buf[4096];
1191 	PROLOGUE("test_read_format_rar5_different_window_size.rar");
1192 
1193 	/* Return codes of those calls are ignored, because this sample file
1194 	 * is invalid. However, the unpacker shouldn't produce any SIGSEGV
1195 	 * errors during processing. */
1196 
1197 	(void) archive_read_next_header(a, &ae);
1198 	while(0 < archive_read_data(a, buf, sizeof(buf))) {}
1199 
1200 	(void) archive_read_next_header(a, &ae);
1201 	while(0 < archive_read_data(a, buf, sizeof(buf))) {}
1202 
1203 	(void) archive_read_next_header(a, &ae);
1204 	while(0 < archive_read_data(a, buf, sizeof(buf))) {}
1205 
1206 	EPILOGUE();
1207 }
1208 
1209 DEFINE_TEST(test_read_format_rar5_window_buf_and_size_desync)
1210 {
1211 	/* oss fuzz 30442 */
1212 
1213 	char buf[4096];
1214 	PROLOGUE("test_read_format_rar5_window_buf_and_size_desync.rar");
1215 
1216 	/* Return codes of those calls are ignored, because this sample file
1217 	 * is invalid. However, the unpacker shouldn't produce any SIGSEGV
1218 	 * errors during processing. */
1219 
1220 	(void) archive_read_next_header(a, &ae);
1221 	while(0 < archive_read_data(a, buf, 46)) {}
1222 
1223 	EPILOGUE();
1224 }
1225 
1226 DEFINE_TEST(test_read_format_rar5_arm_filter_on_window_boundary)
1227 {
1228 	char buf[4096];
1229 	PROLOGUE("test_read_format_rar5_arm_filter_on_window_boundary.rar");
1230 
1231 	/* Return codes of those calls are ignored, because this sample file
1232 	 * is invalid. However, the unpacker shouldn't produce any SIGSEGV
1233 	 * errors during processing. */
1234 
1235 	(void) archive_read_next_header(a, &ae);
1236 	while(0 < archive_read_data(a, buf, sizeof(buf))) {}
1237 
1238 	EPILOGUE();
1239 }
1240 
1241 DEFINE_TEST(test_read_format_rar5_different_solid_window_size)
1242 {
1243 	char buf[4096];
1244 	PROLOGUE("test_read_format_rar5_different_solid_window_size.rar");
1245 
1246 	/* Return codes of those calls are ignored, because this sample file
1247 	 * is invalid. However, the unpacker shouldn't produce any SIGSEGV
1248 	 * errors during processing. */
1249 
1250 	(void) archive_read_next_header(a, &ae);
1251 	while(0 < archive_read_data(a, buf, sizeof(buf))) {}
1252 
1253 	(void) archive_read_next_header(a, &ae);
1254 	while(0 < archive_read_data(a, buf, sizeof(buf))) {}
1255 
1256 	(void) archive_read_next_header(a, &ae);
1257 	while(0 < archive_read_data(a, buf, sizeof(buf))) {}
1258 
1259 	EPILOGUE();
1260 }
1261 
1262 DEFINE_TEST(test_read_format_rar5_different_winsize_on_merge)
1263 {
1264 	char buf[4096];
1265 	PROLOGUE("test_read_format_rar5_different_winsize_on_merge.rar");
1266 
1267 	/* Return codes of those calls are ignored, because this sample file
1268 	 * is invalid. However, the unpacker shouldn't produce any SIGSEGV
1269 	 * errors during processing. */
1270 
1271 	(void) archive_read_next_header(a, &ae);
1272 	while(0 < archive_read_data(a, buf, sizeof(buf))) {}
1273 
1274 	EPILOGUE();
1275 }
1276 
1277 DEFINE_TEST(test_read_format_rar5_block_size_is_too_small)
1278 {
1279 	char buf[4096];
1280 	PROLOGUE("test_read_format_rar5_block_size_is_too_small.rar");
1281 
1282 	/* This file is damaged, so those functions should return failure.
1283 	 * Additionally, SIGSEGV shouldn't be raised during execution
1284 	 * of those functions. */
1285 
1286 	assertA(archive_read_next_header(a, &ae) != ARCHIVE_OK);
1287 	assertA(archive_read_data(a, buf, sizeof(buf)) <= 0);
1288 
1289 	EPILOGUE();
1290 }
1291 
1292 DEFINE_TEST(test_read_format_rar5_sfx)
1293 {
1294 	struct archive *a;
1295 	struct archive_entry *ae;
1296 	int bs = 10240;
1297 	char buff[32];
1298 	const char reffile[] = "test_read_format_rar5_sfx.exe";
1299 	const char test_txt[] = "123";
1300 	int size = sizeof(test_txt) - 1;
1301 
1302 	extract_reference_file(reffile);
1303 	assert((a = archive_read_new()) != NULL);
1304 	assertA(0 == archive_read_support_filter_all(a));
1305 	assertA(0 == archive_read_support_format_all(a));
1306 	assertA(0 == archive_read_open_filename(a, reffile, bs));
1307 
1308 	assertA(0 == archive_read_next_header(a, &ae));
1309 	assertEqualString("test.txt.txt", archive_entry_pathname(ae));
1310 
1311 	assertA(size == archive_read_data(a, buff, size));
1312 	assertEqualMem(buff, test_txt, size);
1313 
1314 	EPILOGUE();
1315 }
1316 
1317 DEFINE_TEST(test_read_format_rar5_decode_number_out_of_bounds_read)
1318 {
1319 	/* oss fuzz 30448 */
1320 
1321 	char buf[4096];
1322 	PROLOGUE("test_read_format_rar5_decode_number_out_of_bounds_read.rar");
1323 
1324 	/* Return codes of those calls are ignored, because this sample file
1325 	 * is invalid. However, the unpacker shouldn't produce any SIGSEGV
1326 	 * errors during processing. */
1327 
1328 	(void) archive_read_next_header(a, &ae);
1329 	while(0 < archive_read_data(a, buf, sizeof(buf))) {}
1330 
1331 	EPILOGUE();
1332 }
1333 
1334 DEFINE_TEST(test_read_format_rar5_bad_window_size_in_multiarchive_file)
1335 {
1336 	/* oss fuzz 30459 */
1337 
1338 	char buf[4096];
1339 	PROLOGUE("test_read_format_rar5_bad_window_sz_in_mltarc_file.rar");
1340 
1341 	/* This file is damaged, so those functions should return failure.
1342 	 * Additionally, SIGSEGV shouldn't be raised during execution
1343 	 * of those functions. */
1344 
1345 	(void) archive_read_next_header(a, &ae);
1346 	while(0 < archive_read_data(a, buf, sizeof(buf))) {}
1347 	(void) archive_read_next_header(a, &ae);
1348 	while(0 < archive_read_data(a, buf, sizeof(buf))) {}
1349 
1350 	EPILOGUE();
1351 }
1352 
1353 DEFINE_TEST(test_read_format_rar5_read_data_block_uninitialized_offset)
1354 {
1355 	const void *buf;
1356 	size_t size;
1357 	la_int64_t offset;
1358 
1359 	PROLOGUE("test_read_format_rar5_compressed.rar");
1360 	assertA(0 == archive_read_next_header(a, &ae));
1361 
1362 	/* A real code may pass a pointer to an uninitialized variable as an offset
1363 	 * output argument. Here we want to check this situation. But because
1364 	 * relying on a value of an uninitialized variable in a test is not a good
1365 	 * idea, let's pretend that 0xdeadbeef is a random value of the
1366 	 * uninitialized variable. */
1367 	offset = 0xdeadbeef;
1368 	assertEqualInt(ARCHIVE_OK, archive_read_data_block(a, &buf, &size, &offset));
1369 	/* The test archive doesn't contain a sparse file. And because of that, here
1370 	 * we assume that the first returned offset should be 0. */
1371 	assertEqualInt(0, offset);
1372 
1373 	EPILOGUE();
1374 }
1375