xref: /freebsd/contrib/libarchive/libarchive/test/test_read_format_zip.c (revision b9128a37faafede823eb456aa65a11ac69997284)
1 /*-
2  * Copyright (c) 2003-2007 Tim Kientzle
3  * Copyright (c) 2011 Michihiro NAKAJIMA
4  * All rights reserved.
5  *
6  * Redistribution and use in source and binary forms, with or without
7  * modification, are permitted provided that the following conditions
8  * are met:
9  * 1. Redistributions of source code must retain the above copyright
10  *    notice, this list of conditions and the following disclaimer.
11  * 2. Redistributions in binary form must reproduce the above copyright
12  *    notice, this list of conditions and the following disclaimer in the
13  *    documentation and/or other materials provided with the distribution.
14  *
15  * THIS SOFTWARE IS PROVIDED BY THE AUTHOR(S) ``AS IS'' AND ANY EXPRESS OR
16  * IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE IMPLIED WARRANTIES
17  * OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE ARE DISCLAIMED.
18  * IN NO EVENT SHALL THE AUTHOR(S) BE LIABLE FOR ANY DIRECT, INDIRECT,
19  * INCIDENTAL, SPECIAL, EXEMPLARY, OR CONSEQUENTIAL DAMAGES (INCLUDING, BUT
20  * NOT LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS OR SERVICES; LOSS OF USE,
21  * DATA, OR PROFITS; OR BUSINESS INTERRUPTION) HOWEVER CAUSED AND ON ANY
22  * THEORY OF LIABILITY, WHETHER IN CONTRACT, STRICT LIABILITY, OR TORT
23  * (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY OUT OF THE USE OF
24  * THIS SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF SUCH DAMAGE.
25  */
26 #include "test.h"
27 
28 #define __LIBARCHIVE_BUILD
29 #include <archive_crc32.h>
30 
31 static
32 int extract_one(struct archive* a, struct archive_entry* ae, uint32_t crc)
33 {
34 	la_ssize_t fsize, bytes_read;
35 	uint8_t* buf;
36 	int ret = 1;
37 	uint32_t computed_crc;
38 
39 	fsize = (la_ssize_t) archive_entry_size(ae);
40 	buf = malloc(fsize);
41 	if(buf == NULL)
42 		return 1;
43 
44 	bytes_read = archive_read_data(a, buf, fsize);
45 	if(bytes_read != fsize) {
46 		assertEqualInt(bytes_read, fsize);
47 		goto fn_exit;
48 	}
49 
50 	computed_crc = crc32(0, buf, fsize);
51 	assertEqualInt(computed_crc, crc);
52 	ret = 0;
53 
54 fn_exit:
55 	free(buf);
56 	return ret;
57 }
58 
59 static
60 int extract_one_using_blocks(struct archive* a, int block_size, uint32_t crc)
61 {
62 	uint8_t* buf;
63 	int ret = 1;
64 	uint32_t computed_crc = 0;
65 	la_ssize_t bytes_read;
66 
67 	buf = malloc(block_size);
68 	if(buf == NULL)
69 		return 1;
70 
71 	while(1) {
72 		bytes_read = archive_read_data(a, buf, block_size);
73 		if(bytes_read == ARCHIVE_RETRY)
74 			continue;
75 		else if(bytes_read == 0)
76 			break;
77 		else if(bytes_read < 0) {
78 			/* If we're here, it means the decompressor has failed
79 			 * to properly decode test file. */
80 			assertA(0);
81 			ret = 1;
82 			goto fn_exit;
83 		} else {
84 			/* ok */
85 		}
86 
87 		computed_crc = crc32(computed_crc, buf, bytes_read);
88 	}
89 
90 	assertEqualInt(computed_crc, crc);
91 	ret = 0;
92 
93 fn_exit:
94 	free(buf);
95 	return ret;
96 }
97 
98 /*
99  * The reference file for this has been manually tweaked so that:
100  *   * file2 has length-at-end but file1 does not
101  *   * file2 has an invalid CRC
102  */
103 static void
104 verify_basic(struct archive *a, int seek_checks)
105 {
106 	struct archive_entry *ae;
107 	char *buff[128];
108 	const void *pv;
109 	size_t s;
110 	int64_t o;
111 
112 	assertEqualIntA(a, ARCHIVE_OK, archive_read_next_header(a, &ae));
113 	assertEqualString("ZIP 1.0 (uncompressed)", archive_format_name(a));
114 	assertEqualString("dir/", archive_entry_pathname(ae));
115 	assertEqualInt(1179604249, archive_entry_mtime(ae));
116 	assertEqualInt(0, archive_entry_size(ae));
117 	if (seek_checks)
118 		assertEqualInt(AE_IFDIR | 0755, archive_entry_mode(ae));
119 	assertEqualInt(archive_entry_is_encrypted(ae), 0);
120 	assertEqualIntA(a, archive_read_has_encrypted_entries(a), 0);
121 	assertEqualIntA(a, ARCHIVE_EOF,
122 	    archive_read_data_block(a, &pv, &s, &o));
123 	assertEqualInt((int)s, 0);
124 
125 	assertEqualIntA(a, ARCHIVE_OK, archive_read_next_header(a, &ae));
126 	assertEqualString("ZIP 2.0 (deflation)", archive_format_name(a));
127 	assertEqualString("file1", archive_entry_pathname(ae));
128 	assertEqualInt(1179604289, archive_entry_mtime(ae));
129 	if (seek_checks)
130 		assertEqualInt(AE_IFREG | 0755, archive_entry_mode(ae));
131 	assertEqualInt(18, archive_entry_size(ae));
132 	assertEqualInt(archive_entry_is_encrypted(ae), 0);
133 	assertEqualIntA(a, archive_read_has_encrypted_entries(a), 0);
134 	failure("archive_read_data() returns number of bytes read");
135 	if (archive_zlib_version() != NULL) {
136 		assertEqualInt(18, archive_read_data(a, buff, 19));
137 		assertEqualMem(buff, "hello\nhello\nhello\n", 18);
138 	} else {
139 		assertEqualInt(ARCHIVE_FAILED, archive_read_data(a, buff, 19));
140 		assertEqualString(archive_error_string(a),
141 		    "Unsupported ZIP compression method (8: deflation)");
142 		assert(archive_errno(a) != 0);
143 	}
144 
145 	assertEqualIntA(a, ARCHIVE_OK, archive_read_next_header(a, &ae));
146 	assertEqualString("ZIP 2.0 (deflation)", archive_format_name(a));
147 	assertEqualString("file2", archive_entry_pathname(ae));
148 	assertEqualInt(1179605932, archive_entry_mtime(ae));
149 	assertEqualInt(archive_entry_is_encrypted(ae), 0);
150 	assertEqualIntA(a, archive_read_has_encrypted_entries(a), 0);
151 	if (seek_checks) {
152 		assertEqualInt(AE_IFREG | 0755, archive_entry_mode(ae));
153 	}
154 	assert(archive_entry_size_is_set(ae));
155 	assertEqualInt(18, archive_entry_size(ae));
156 	if (archive_zlib_version() != NULL) {
157 		failure("file2 has a bad CRC, so read should fail and not change buff");
158 		memset(buff, 'a', 19);
159 		assertEqualInt(ARCHIVE_FAILED, archive_read_data(a, buff, 19));
160 		assertEqualMem(buff, "aaaaaaaaaaaaaaaaaaa", 19);
161 	} else {
162 		assertEqualInt(ARCHIVE_FAILED, archive_read_data(a, buff, 19));
163 		assertEqualString(archive_error_string(a),
164 		    "Unsupported ZIP compression method (8: deflation)");
165 		assert(archive_errno(a) != 0);
166 	}
167 	assertEqualInt(ARCHIVE_EOF, archive_read_next_header(a, &ae));
168 	assertEqualString("ZIP 2.0 (deflation)", archive_format_name(a));
169 	/* Verify the number of files read. */
170 	failure("the archive file has three files");
171 	assertEqualInt(3, archive_file_count(a));
172 	assertEqualIntA(a, ARCHIVE_FILTER_NONE, archive_filter_code(a, 0));
173 	assertEqualIntA(a, ARCHIVE_FORMAT_ZIP, archive_format(a));
174 	assertEqualIntA(a, ARCHIVE_OK, archive_read_close(a));
175 	assertEqualInt(ARCHIVE_OK, archive_read_free(a));
176 }
177 
178 static void
179 test_basic(void)
180 {
181 	const char *refname = "test_read_format_zip.zip";
182 	struct archive *a;
183 	char *p;
184 	size_t s;
185 
186 	extract_reference_file(refname);
187 
188 	/* Verify with seeking reader. */
189 	assert((a = archive_read_new()) != NULL);
190 	assertEqualIntA(a, ARCHIVE_OK, archive_read_support_filter_all(a));
191 	assertEqualIntA(a, ARCHIVE_OK, archive_read_support_format_all(a));
192 	assertEqualIntA(a, ARCHIVE_OK, archive_read_open_filename(a, refname, 10240));
193 	verify_basic(a, 1);
194 
195 	/* Verify with streaming reader. */
196 	p = slurpfile(&s, "%s", refname);
197 	assert((a = archive_read_new()) != NULL);
198 	assertEqualIntA(a, ARCHIVE_OK, archive_read_support_filter_all(a));
199 	assertEqualIntA(a, ARCHIVE_OK, archive_read_support_format_all(a));
200 	assertEqualIntA(a, ARCHIVE_OK, read_open_memory(a, p, s, 31));
201 	verify_basic(a, 0);
202 	free(p);
203 }
204 
205 /*
206  * Read Info-ZIP New Unix Extra Field 0x7875 "ux".
207  *  Currently stores Unix UID/GID up to 32 bits.
208  */
209 static void
210 verify_info_zip_ux(struct archive *a, int seek_checks)
211 {
212 	struct archive_entry *ae;
213 	char *buff[128];
214 
215 	assertEqualIntA(a, ARCHIVE_OK, archive_read_next_header(a, &ae));
216 	assertEqualString("file1", archive_entry_pathname(ae));
217 	assertEqualInt(1300668680, archive_entry_mtime(ae));
218 	assertEqualInt(18, archive_entry_size(ae));
219 	assertEqualInt(archive_entry_is_encrypted(ae), 0);
220 	assertEqualIntA(a, archive_read_has_encrypted_entries(a), 0);
221 	if (seek_checks)
222 		assertEqualInt(AE_IFREG | 0644, archive_entry_mode(ae));
223 	failure("zip reader should read Info-ZIP New Unix Extra Field");
224 	assertEqualInt(1001, archive_entry_uid(ae));
225 	assertEqualInt(1001, archive_entry_gid(ae));
226 	if (archive_zlib_version() != NULL) {
227 		failure("archive_read_data() returns number of bytes read");
228 		assertEqualInt(18, archive_read_data(a, buff, 19));
229 		assertEqualMem(buff, "hello\nhello\nhello\n", 18);
230 	} else {
231 		assertEqualInt(ARCHIVE_FAILED, archive_read_data(a, buff, 19));
232 		assertEqualString(archive_error_string(a),
233 		    "Unsupported ZIP compression method (8: deflation)");
234 		assert(archive_errno(a) != 0);
235 	}
236 	assertEqualIntA(a, ARCHIVE_EOF, archive_read_next_header(a, &ae));
237 
238 	/* Verify the number of files read. */
239 	failure("the archive file has just one file");
240 	assertEqualInt(1, archive_file_count(a));
241 
242 	assertEqualIntA(a, ARCHIVE_FILTER_NONE, archive_filter_code(a, 0));
243 	assertEqualIntA(a, ARCHIVE_FORMAT_ZIP, archive_format(a));
244 	assertEqualIntA(a, ARCHIVE_OK, archive_read_close(a));
245 	assertEqualInt(ARCHIVE_OK, archive_read_free(a));
246 }
247 
248 static void
249 test_info_zip_ux(void)
250 {
251 	const char *refname = "test_read_format_zip_ux.zip";
252 	struct archive *a;
253 	char *p;
254 	size_t s;
255 
256 	extract_reference_file(refname);
257 
258 	/* Verify with seeking reader. */
259 	assert((a = archive_read_new()) != NULL);
260 	assertEqualIntA(a, ARCHIVE_OK, archive_read_support_filter_all(a));
261 	assertEqualIntA(a, ARCHIVE_OK, archive_read_support_format_all(a));
262 	assertEqualIntA(a, ARCHIVE_OK, archive_read_open_filename(a, refname, 10240));
263 	verify_info_zip_ux(a, 1);
264 
265 	/* Verify with streaming reader. */
266 	p = slurpfile(&s, "%s", refname);
267 	assert((a = archive_read_new()) != NULL);
268 	assertEqualIntA(a, ARCHIVE_OK, archive_read_support_filter_all(a));
269 	assertEqualIntA(a, ARCHIVE_OK, archive_read_support_format_all(a));
270 	assertEqualIntA(a, ARCHIVE_OK, read_open_memory(a, p, s, 108));
271 	verify_info_zip_ux(a, 0);
272 	free(p);
273 }
274 
275 /*
276  * Verify that test_read_extract correctly works with
277  * Zip entries that use length-at-end.
278  */
279 static void
280 verify_extract_length_at_end(struct archive *a, int seek_checks)
281 {
282 	struct archive_entry *ae;
283 
284 	assertEqualIntA(a, ARCHIVE_OK, archive_read_next_header(a, &ae));
285 
286 	assertEqualInt(archive_entry_is_encrypted(ae), 0);
287 	assertEqualIntA(a, archive_read_has_encrypted_entries(a), 0);
288 	assertEqualString("hello.txt", archive_entry_pathname(ae));
289 	if (seek_checks) {
290 		assertEqualInt(AE_IFREG | 0644, archive_entry_mode(ae));
291 		assert(archive_entry_size_is_set(ae));
292 		assertEqualInt(6, archive_entry_size(ae));
293 	} else {
294 		assert(!archive_entry_size_is_set(ae));
295 		assertEqualInt(0, archive_entry_size(ae));
296 	}
297 
298 	if (archive_zlib_version() != NULL) {
299 		assertEqualIntA(a, ARCHIVE_OK, archive_read_extract(a, ae, 0));
300 		assertFileContents("hello\x0A", 6, "hello.txt");
301 	} else {
302 		assertEqualIntA(a, ARCHIVE_FAILED, archive_read_extract(a, ae, 0));
303 		assertEqualString(archive_error_string(a),
304 		    "Unsupported ZIP compression method (8: deflation)");
305 		assert(archive_errno(a) != 0);
306 	}
307 
308 	assertEqualIntA(a, ARCHIVE_OK, archive_read_close(a));
309 	assertEqualIntA(a, ARCHIVE_OK, archive_read_free(a));
310 }
311 
312 static void
313 test_extract_length_at_end(void)
314 {
315 	const char *refname = "test_read_format_zip_length_at_end.zip";
316 	char *p;
317 	size_t s;
318 	struct archive *a;
319 
320 	extract_reference_file(refname);
321 
322 	/* Verify extraction with seeking reader. */
323 	assert((a = archive_read_new()) != NULL);
324 	assertEqualIntA(a, ARCHIVE_OK, archive_read_support_filter_all(a));
325 	assertEqualIntA(a, ARCHIVE_OK, archive_read_support_format_all(a));
326 	assertEqualIntA(a, ARCHIVE_OK, archive_read_open_filename(a, refname, 10240));
327 	verify_extract_length_at_end(a, 1);
328 
329 	/* Verify extraction with streaming reader. */
330 	p = slurpfile(&s, "%s", refname);
331 	assert((a = archive_read_new()) != NULL);
332 	assertEqualIntA(a, ARCHIVE_OK, archive_read_support_filter_all(a));
333 	assertEqualIntA(a, ARCHIVE_OK, archive_read_support_format_all(a));
334 	assertEqualIntA(a, ARCHIVE_OK, read_open_memory(a, p, s, 108));
335 	verify_extract_length_at_end(a, 0);
336 	free(p);
337 }
338 
339 static void
340 test_symlink(void)
341 {
342 	const char *refname = "test_read_format_zip_symlink.zip";
343 	char *p;
344 	size_t s;
345 	struct archive *a;
346 	struct archive_entry *ae;
347 
348 	extract_reference_file(refname);
349 	p = slurpfile(&s, "%s", refname);
350 
351 	/* Symlinks can only be extracted with the seeking reader. */
352 	assert((a = archive_read_new()) != NULL);
353 	assertEqualIntA(a, ARCHIVE_OK, archive_read_support_format_zip(a));
354 	assertEqualIntA(a, ARCHIVE_OK, read_open_memory_seek(a, p, s, 1));
355 
356 	assertEqualIntA(a, ARCHIVE_OK, archive_read_next_header(a, &ae));
357 	assertEqualString("file", archive_entry_pathname(ae));
358 	assertEqualInt(AE_IFREG, archive_entry_filetype(ae));
359 	assertEqualInt(archive_entry_is_encrypted(ae), 0);
360 	assertEqualIntA(a, archive_read_has_encrypted_entries(a), 0);
361 
362 	assertEqualIntA(a, ARCHIVE_OK, archive_read_next_header(a, &ae));
363 	assertEqualString("symlink", archive_entry_pathname(ae));
364 	assertEqualInt(AE_IFLNK, archive_entry_filetype(ae));
365 	assertEqualInt(0, archive_entry_size(ae));
366 	assertEqualString("file", archive_entry_symlink(ae));
367 	assertEqualInt(archive_entry_is_encrypted(ae), 0);
368 	assertEqualIntA(a, archive_read_has_encrypted_entries(a), 0);
369 
370 	assertEqualIntA(a, ARCHIVE_EOF, archive_read_next_header(a, &ae));
371 	assertEqualIntA(a, ARCHIVE_OK, archive_read_close(a));
372 	assertEqualIntA(a, ARCHIVE_OK, archive_read_free(a));
373 
374 	free(p);
375 }
376 
377 DEFINE_TEST(test_read_format_zip)
378 {
379 	test_basic();
380 	test_info_zip_ux();
381 	test_extract_length_at_end();
382 	test_symlink();
383 }
384 
385 DEFINE_TEST(test_read_format_zip_ppmd_one_file)
386 {
387 	const char *refname = "test_read_format_zip_ppmd8.zipx";
388 	struct archive *a;
389 	struct archive_entry *ae;
390 
391 	extract_reference_file(refname);
392 
393 	assert((a = archive_read_new()) != NULL);
394 	assertEqualIntA(a, ARCHIVE_OK, archive_read_support_format_zip(a));
395 	assertEqualIntA(a, ARCHIVE_OK, archive_read_open_filename(a, refname, 37));
396 	assertEqualIntA(a, ARCHIVE_OK, archive_read_next_header(a, &ae));
397 	assertEqualString("ZIP 6.3 (ppmd-1)", archive_format_name(a));
398 	assertEqualString("vimrc", archive_entry_pathname(ae));
399 	assertEqualIntA(a, 0, extract_one(a, ae, 0xBA8E3BAA));
400 	assertEqualIntA(a, ARCHIVE_EOF, archive_read_next_header(a, &ae));
401 	assertEqualIntA(a, ARCHIVE_OK, archive_read_close(a));
402 	assertEqualIntA(a, ARCHIVE_OK, archive_read_free(a));
403 }
404 
405 DEFINE_TEST(test_read_format_zip_ppmd_one_file_blockread)
406 {
407 	const char *refname = "test_read_format_zip_ppmd8.zipx";
408 	struct archive *a;
409 	struct archive_entry *ae;
410 
411 	extract_reference_file(refname);
412 
413 	assert((a = archive_read_new()) != NULL);
414 	assertEqualIntA(a, ARCHIVE_OK, archive_read_support_format_zip(a));
415 	assertEqualIntA(a, ARCHIVE_OK, archive_read_open_filename(a, refname, 37));
416 	assertEqualIntA(a, ARCHIVE_OK, archive_read_next_header(a, &ae));
417 	assertEqualString("ZIP 6.3 (ppmd-1)", archive_format_name(a));
418 	assertEqualString("vimrc", archive_entry_pathname(ae));
419 	assertEqualIntA(a, 0, extract_one_using_blocks(a, 13, 0xBA8E3BAA));
420 	assertEqualIntA(a, ARCHIVE_EOF, archive_read_next_header(a, &ae));
421 	assertEqualIntA(a, ARCHIVE_OK, archive_read_close(a));
422 	assertEqualIntA(a, ARCHIVE_OK, archive_read_free(a));
423 }
424 
425 DEFINE_TEST(test_read_format_zip_ppmd_multi)
426 {
427 	const char *refname = "test_read_format_zip_ppmd8_multi.zipx";
428 	struct archive *a;
429 	struct archive_entry *ae;
430 
431 	extract_reference_file(refname);
432 
433 	assert((a = archive_read_new()) != NULL);
434 	assertEqualIntA(a, ARCHIVE_OK, archive_read_support_format_zip(a));
435 	assertEqualIntA(a, ARCHIVE_OK, archive_read_open_filename(a, refname, 37));
436 
437 	assertEqualIntA(a, ARCHIVE_OK, archive_read_next_header(a, &ae));
438 	assertEqualString("ZIP 6.3 (ppmd-1)", archive_format_name(a));
439 	assertEqualString("smartd.conf", archive_entry_pathname(ae));
440 	assertEqualIntA(a, 0, extract_one(a, ae, 0x8DD7379E));
441 
442 	assertEqualIntA(a, ARCHIVE_OK, archive_read_next_header(a, &ae));
443 	assertEqualString("ZIP 6.3 (ppmd-1)", archive_format_name(a));
444 	assertEqualString("ts.conf", archive_entry_pathname(ae));
445 	assertEqualIntA(a, 0, extract_one(a, ae, 0x7AE59B31));
446 
447 	assertEqualIntA(a, ARCHIVE_OK, archive_read_next_header(a, &ae));
448 	assertEqualString("ZIP 6.3 (ppmd-1)", archive_format_name(a));
449 	assertEqualString("vimrc", archive_entry_pathname(ae));
450 	assertEqualIntA(a, 0, extract_one(a, ae, 0xBA8E3BAA));
451 
452 	assertEqualIntA(a, ARCHIVE_EOF, archive_read_next_header(a, &ae));
453 	assertEqualIntA(a, ARCHIVE_OK, archive_read_close(a));
454 	assertEqualIntA(a, ARCHIVE_OK, archive_read_free(a));
455 }
456 
457 DEFINE_TEST(test_read_format_zip_ppmd_multi_blockread)
458 {
459 	const char *refname = "test_read_format_zip_ppmd8_multi.zipx";
460 	struct archive *a;
461 	struct archive_entry *ae;
462 
463 	extract_reference_file(refname);
464 
465 	assert((a = archive_read_new()) != NULL);
466 	assertEqualIntA(a, ARCHIVE_OK, archive_read_support_format_zip(a));
467 	assertEqualIntA(a, ARCHIVE_OK, archive_read_open_filename(a, refname, 37));
468 
469 	assertEqualIntA(a, ARCHIVE_OK, archive_read_next_header(a, &ae));
470 	assertEqualString("ZIP 6.3 (ppmd-1)", archive_format_name(a));
471 	assertEqualString("smartd.conf", archive_entry_pathname(ae));
472 	assertEqualIntA(a, 0, extract_one_using_blocks(a, 12, 0x8DD7379E));
473 
474 	assertEqualIntA(a, ARCHIVE_OK, archive_read_next_header(a, &ae));
475 	assertEqualString("ZIP 6.3 (ppmd-1)", archive_format_name(a));
476 	assertEqualString("ts.conf", archive_entry_pathname(ae));
477 	assertEqualIntA(a, 0, extract_one_using_blocks(a, 13, 0x7AE59B31));
478 
479 	assertEqualIntA(a, ARCHIVE_OK, archive_read_next_header(a, &ae));
480 	assertEqualString("ZIP 6.3 (ppmd-1)", archive_format_name(a));
481 	assertEqualString("vimrc", archive_entry_pathname(ae));
482 	assertEqualIntA(a, 0, extract_one_using_blocks(a, 14, 0xBA8E3BAA));
483 
484 	assertEqualIntA(a, ARCHIVE_EOF, archive_read_next_header(a, &ae));
485 	assertEqualIntA(a, ARCHIVE_OK, archive_read_close(a));
486 	assertEqualIntA(a, ARCHIVE_OK, archive_read_free(a));
487 }
488 
489 DEFINE_TEST(test_read_format_zip_lzma_one_file)
490 {
491 	const char *refname = "test_read_format_zip_lzma.zipx";
492 	struct archive *a;
493 	struct archive_entry *ae;
494 
495 	assert((a = archive_read_new()) != NULL);
496 		if (ARCHIVE_OK != archive_read_support_filter_lzma(a)) {
497 				skipping("lzma reading not fully supported on this platform");
498 				assertEqualInt(ARCHIVE_OK, archive_read_free(a));
499 				return;
500 		}
501 	extract_reference_file(refname);
502 
503 	assertEqualIntA(a, ARCHIVE_OK, archive_read_support_format_zip(a));
504 	assertEqualIntA(a, ARCHIVE_OK, archive_read_open_filename(a, refname, 37));
505 	assertEqualIntA(a, ARCHIVE_OK, archive_read_next_header(a, &ae));
506 	assertEqualString("ZIP 6.3 (lzma)", archive_format_name(a));
507 	assertEqualString("vimrc", archive_entry_pathname(ae));
508 	assertEqualIntA(a, 0, extract_one(a, ae, 0xBA8E3BAA));
509 	assertEqualIntA(a, ARCHIVE_EOF, archive_read_next_header(a, &ae));
510 	assertEqualIntA(a, ARCHIVE_OK, archive_read_close(a));
511 	assertEqualIntA(a, ARCHIVE_OK, archive_read_free(a));
512 }
513 
514 DEFINE_TEST(test_read_format_zip_lzma_one_file_blockread)
515 {
516 	const char *refname = "test_read_format_zip_lzma.zipx";
517 	struct archive *a;
518 	struct archive_entry *ae;
519 
520 	assert((a = archive_read_new()) != NULL);
521 	if (ARCHIVE_OK != archive_read_support_filter_lzma(a)) {
522 			skipping("lzma reading not fully supported on this platform");
523 			assertEqualInt(ARCHIVE_OK, archive_read_free(a));
524 			return;
525 	}
526 	extract_reference_file(refname);
527 
528 	assertEqualIntA(a, ARCHIVE_OK, archive_read_support_format_zip(a));
529 	assertEqualIntA(a, ARCHIVE_OK, archive_read_open_filename(a, refname, 37));
530 	assertEqualIntA(a, ARCHIVE_OK, archive_read_next_header(a, &ae));
531 	assertEqualString("ZIP 6.3 (lzma)", archive_format_name(a));
532 	assertEqualString("vimrc", archive_entry_pathname(ae));
533 	assertEqualIntA(a, 0, extract_one_using_blocks(a, 13, 0xBA8E3BAA));
534 	assertEqualIntA(a, ARCHIVE_EOF, archive_read_next_header(a, &ae));
535 	assertEqualIntA(a, ARCHIVE_OK, archive_read_close(a));
536 	assertEqualIntA(a, ARCHIVE_OK, archive_read_free(a));
537 }
538 
539 DEFINE_TEST(test_read_format_zip_lzma_multi)
540 {
541 	const char *refname = "test_read_format_zip_lzma_multi.zipx";
542 	struct archive *a;
543 	struct archive_entry *ae;
544 
545 	assert((a = archive_read_new()) != NULL);
546 	if (ARCHIVE_OK != archive_read_support_filter_lzma(a)) {
547 			skipping("lzma reading not fully supported on this platform");
548 			assertEqualInt(ARCHIVE_OK, archive_read_free(a));
549 			return;
550 	}
551 	extract_reference_file(refname);
552 
553 	assertEqualIntA(a, ARCHIVE_OK, archive_read_support_format_zip(a));
554 	assertEqualIntA(a, ARCHIVE_OK, archive_read_open_filename(a, refname, 37));
555 
556 	assertEqualIntA(a, ARCHIVE_OK, archive_read_next_header(a, &ae));
557 	assertEqualString("ZIP 6.3 (lzma)", archive_format_name(a));
558 	assertEqualString("smartd.conf", archive_entry_pathname(ae));
559 	assertEqualIntA(a, 0, extract_one(a, ae, 0x8DD7379E));
560 
561 	assertEqualIntA(a, ARCHIVE_OK, archive_read_next_header(a, &ae));
562 	assertEqualString("ZIP 6.3 (lzma)", archive_format_name(a));
563 	assertEqualString("ts.conf", archive_entry_pathname(ae));
564 	assertEqualIntA(a, 0, extract_one(a, ae, 0x7AE59B31));
565 
566 	assertEqualIntA(a, ARCHIVE_OK, archive_read_next_header(a, &ae));
567 	assertEqualString("ZIP 6.3 (lzma)", archive_format_name(a));
568 	assertEqualString("vimrc", archive_entry_pathname(ae));
569 	assertEqualIntA(a, 0, extract_one(a, ae, 0xBA8E3BAA));
570 
571 	assertEqualIntA(a, ARCHIVE_EOF, archive_read_next_header(a, &ae));
572 	assertEqualIntA(a, ARCHIVE_OK, archive_read_close(a));
573 	assertEqualIntA(a, ARCHIVE_OK, archive_read_free(a));
574 }
575 
576 DEFINE_TEST(test_read_format_zip_lzma_multi_blockread)
577 {
578 	const char *refname = "test_read_format_zip_lzma_multi.zipx";
579 	struct archive *a;
580 	struct archive_entry *ae;
581 
582 	assert((a = archive_read_new()) != NULL);
583 	if (ARCHIVE_OK != archive_read_support_filter_lzma(a)) {
584 			skipping("lzma reading not fully supported on this platform");
585 			assertEqualInt(ARCHIVE_OK, archive_read_free(a));
586 			return;
587 	}
588 	extract_reference_file(refname);
589 
590 	assertEqualIntA(a, ARCHIVE_OK, archive_read_support_format_zip(a));
591 	assertEqualIntA(a, ARCHIVE_OK, archive_read_open_filename(a, refname, 37));
592 
593 	assertEqualIntA(a, ARCHIVE_OK, archive_read_next_header(a, &ae));
594 	assertEqualString("ZIP 6.3 (lzma)", archive_format_name(a));
595 	assertEqualString("smartd.conf", archive_entry_pathname(ae));
596 	assertEqualIntA(a, 0, extract_one_using_blocks(a, 12, 0x8DD7379E));
597 
598 	assertEqualIntA(a, ARCHIVE_OK, archive_read_next_header(a, &ae));
599 	assertEqualString("ZIP 6.3 (lzma)", archive_format_name(a));
600 	assertEqualString("ts.conf", archive_entry_pathname(ae));
601 	assertEqualIntA(a, 0, extract_one_using_blocks(a, 13, 0x7AE59B31));
602 
603 	assertEqualIntA(a, ARCHIVE_OK, archive_read_next_header(a, &ae));
604 	assertEqualString("ZIP 6.3 (lzma)", archive_format_name(a));
605 	assertEqualString("vimrc", archive_entry_pathname(ae));
606 	assertEqualIntA(a, 0, extract_one_using_blocks(a, 14, 0xBA8E3BAA));
607 
608 	assertEqualIntA(a, ARCHIVE_EOF, archive_read_next_header(a, &ae));
609 	assertEqualIntA(a, ARCHIVE_OK, archive_read_close(a));
610 	assertEqualIntA(a, ARCHIVE_OK, archive_read_free(a));
611 }
612 
613 
614 DEFINE_TEST(test_read_format_zip_bzip2_one_file)
615 {
616 	const char *refname = "test_read_format_zip_bzip2.zipx";
617 	struct archive *a;
618 	struct archive_entry *ae;
619 
620 	assert((a = archive_read_new()) != NULL);
621 	if (ARCHIVE_OK != archive_read_support_filter_bzip2(a)) {
622 		skipping("bzip2 is not fully supported on this platform");
623 		archive_read_close(a);
624 		return;
625 	}
626 	extract_reference_file(refname);
627 
628 	assertEqualIntA(a, ARCHIVE_OK, archive_read_support_format_zip(a));
629 	assertEqualIntA(a, ARCHIVE_OK, archive_read_open_filename(a, refname, 37));
630 	assertEqualIntA(a, ARCHIVE_OK, archive_read_next_header(a, &ae));
631 	assertEqualString("ZIP 4.6 (bzip)", archive_format_name(a));
632 	assertEqualString("vimrc", archive_entry_pathname(ae));
633 	assertEqualIntA(a, 0, extract_one(a, ae, 0xBA8E3BAA));
634 	assertEqualIntA(a, ARCHIVE_EOF, archive_read_next_header(a, &ae));
635 	assertEqualIntA(a, ARCHIVE_OK, archive_read_close(a));
636 	assertEqualIntA(a, ARCHIVE_OK, archive_read_free(a));
637 }
638 
639 DEFINE_TEST(test_read_format_zip_bzip2_one_file_blockread)
640 {
641 	const char *refname = "test_read_format_zip_bzip2.zipx";
642 	struct archive *a;
643 	struct archive_entry *ae;
644 
645 	assert((a = archive_read_new()) != NULL);
646 	if (ARCHIVE_OK != archive_read_support_filter_bzip2(a)) {
647 		skipping("bzip2 is not fully supported on this platform");
648 		archive_read_close(a);
649 		return;
650 	}
651 	extract_reference_file(refname);
652 
653 	assertEqualIntA(a, ARCHIVE_OK, archive_read_support_format_zip(a));
654 	assertEqualIntA(a, ARCHIVE_OK, archive_read_open_filename(a, refname, 37));
655 	assertEqualIntA(a, ARCHIVE_OK, archive_read_next_header(a, &ae));
656 	assertEqualString("ZIP 4.6 (bzip)", archive_format_name(a));
657 	assertEqualString("vimrc", archive_entry_pathname(ae));
658 	assertEqualIntA(a, 0, extract_one_using_blocks(a, 13, 0xBA8E3BAA));
659 	assertEqualIntA(a, ARCHIVE_EOF, archive_read_next_header(a, &ae));
660 	assertEqualIntA(a, ARCHIVE_OK, archive_read_close(a));
661 	assertEqualIntA(a, ARCHIVE_OK, archive_read_free(a));
662 }
663 
664 DEFINE_TEST(test_read_format_zip_bzip2_multi)
665 {
666 	const char *refname = "test_read_format_zip_bzip2_multi.zipx";
667 	struct archive *a;
668 	struct archive_entry *ae;
669 
670 	assert((a = archive_read_new()) != NULL);
671 	if (ARCHIVE_OK != archive_read_support_filter_bzip2(a)) {
672 		skipping("bzip2 is not fully supported on this platform");
673 		archive_read_close(a);
674 		return;
675 	}
676 	extract_reference_file(refname);
677 
678 	assertEqualIntA(a, ARCHIVE_OK, archive_read_support_format_zip(a));
679 	assertEqualIntA(a, ARCHIVE_OK, archive_read_open_filename(a, refname, 37));
680 
681 	assertEqualIntA(a, ARCHIVE_OK, archive_read_next_header(a, &ae));
682 	assertEqualString("ZIP 4.6 (bzip)", archive_format_name(a));
683 	assertEqualString("smartd.conf", archive_entry_pathname(ae));
684 	assertEqualIntA(a, 0, extract_one(a, ae, 0x8DD7379E));
685 
686 	assertEqualIntA(a, ARCHIVE_OK, archive_read_next_header(a, &ae));
687 	assertEqualString("ZIP 4.6 (bzip)", archive_format_name(a));
688 	assertEqualString("ts.conf", archive_entry_pathname(ae));
689 	assertEqualIntA(a, 0, extract_one(a, ae, 0x7AE59B31));
690 
691 	assertEqualIntA(a, ARCHIVE_OK, archive_read_next_header(a, &ae));
692 	assertEqualString("ZIP 4.6 (bzip)", archive_format_name(a));
693 	assertEqualString("vimrc", archive_entry_pathname(ae));
694 	assertEqualIntA(a, 0, extract_one(a, ae, 0xBA8E3BAA));
695 
696 	assertEqualIntA(a, ARCHIVE_EOF, archive_read_next_header(a, &ae));
697 	assertEqualIntA(a, ARCHIVE_OK, archive_read_close(a));
698 	assertEqualIntA(a, ARCHIVE_OK, archive_read_free(a));
699 }
700 
701 DEFINE_TEST(test_read_format_zip_bzip2_multi_blockread)
702 {
703 	const char *refname = "test_read_format_zip_bzip2_multi.zipx";
704 	struct archive *a;
705 	struct archive_entry *ae;
706 
707 	assert((a = archive_read_new()) != NULL);
708 	if (ARCHIVE_OK != archive_read_support_filter_bzip2(a)) {
709 		skipping("bzip2 is not fully supported on this platform");
710 		archive_read_close(a);
711 		return;
712 	}
713 	extract_reference_file(refname);
714 
715 	assertEqualIntA(a, ARCHIVE_OK, archive_read_support_format_zip(a));
716 	assertEqualIntA(a, ARCHIVE_OK, archive_read_open_filename(a, refname, 37));
717 
718 	assertEqualIntA(a, ARCHIVE_OK, archive_read_next_header(a, &ae));
719 	assertEqualString("ZIP 4.6 (bzip)", archive_format_name(a));
720 	assertEqualString("smartd.conf", archive_entry_pathname(ae));
721 	assertEqualIntA(a, 0, extract_one_using_blocks(a, 12, 0x8DD7379E));
722 
723 	assertEqualIntA(a, ARCHIVE_OK, archive_read_next_header(a, &ae));
724 	assertEqualString("ZIP 4.6 (bzip)", archive_format_name(a));
725 	assertEqualString("ts.conf", archive_entry_pathname(ae));
726 	assertEqualIntA(a, 0, extract_one_using_blocks(a, 13, 0x7AE59B31));
727 
728 	assertEqualIntA(a, ARCHIVE_OK, archive_read_next_header(a, &ae));
729 	assertEqualString("ZIP 4.6 (bzip)", archive_format_name(a));
730 	assertEqualString("vimrc", archive_entry_pathname(ae));
731 	assertEqualIntA(a, 0, extract_one_using_blocks(a, 14, 0xBA8E3BAA));
732 
733 	assertEqualIntA(a, ARCHIVE_EOF, archive_read_next_header(a, &ae));
734 	assertEqualIntA(a, ARCHIVE_OK, archive_read_close(a));
735 	assertEqualIntA(a, ARCHIVE_OK, archive_read_free(a));
736 }
737 
738 DEFINE_TEST(test_read_format_zip_zstd_one_file)
739 {
740 	const char *refname = "test_read_format_zip_zstd.zipx";
741 	struct archive *a;
742 	struct archive_entry *ae;
743 
744 	assert((a = archive_read_new()) != NULL);
745 	if (ARCHIVE_OK != archive_read_support_filter_zstd(a)) {
746 		skipping("zstd is not fully supported on this platform");
747 		archive_read_close(a);
748 		archive_read_free(a);
749 		return;
750 	}
751 	extract_reference_file(refname);
752 
753 	assertEqualIntA(a, ARCHIVE_OK, archive_read_support_format_zip(a));
754 	assertEqualIntA(a, ARCHIVE_OK, archive_read_open_filename(a, refname, 37));
755 	assertEqualIntA(a, ARCHIVE_OK, archive_read_next_header(a, &ae));
756 	assertEqualString("ZIP 2.0 (zstd)", archive_format_name(a));
757 	assertEqualString("vimrc", archive_entry_pathname(ae));
758 	assertEqualIntA(a, 0, extract_one(a, ae, 0xBA8E3BAA));
759 	assertEqualIntA(a, ARCHIVE_EOF, archive_read_next_header(a, &ae));
760 	assertEqualIntA(a, ARCHIVE_OK, archive_read_close(a));
761 	assertEqualIntA(a, ARCHIVE_OK, archive_read_free(a));
762 }
763 
764 DEFINE_TEST(test_read_format_zip_zstd_one_file_blockread)
765 {
766 	const char *refname = "test_read_format_zip_zstd.zipx";
767 	struct archive *a;
768 	struct archive_entry *ae;
769 
770 	assert((a = archive_read_new()) != NULL);
771 	if (ARCHIVE_OK != archive_read_support_filter_zstd(a)) {
772 		skipping("zstd is not fully supported on this platform");
773 		archive_read_close(a);
774 		archive_read_free(a);
775 		return;
776 	}
777 	extract_reference_file(refname);
778 
779 	assertEqualIntA(a, ARCHIVE_OK, archive_read_support_format_zip(a));
780 	assertEqualIntA(a, ARCHIVE_OK, archive_read_open_filename(a, refname, 37));
781 	assertEqualIntA(a, ARCHIVE_OK, archive_read_next_header(a, &ae));
782 	assertEqualString("ZIP 2.0 (zstd)", archive_format_name(a));
783 	assertEqualString("vimrc", archive_entry_pathname(ae));
784 	assertEqualIntA(a, 0, extract_one_using_blocks(a, 13, 0xBA8E3BAA));
785 	assertEqualIntA(a, ARCHIVE_EOF, archive_read_next_header(a, &ae));
786 	assertEqualIntA(a, ARCHIVE_OK, archive_read_close(a));
787 	assertEqualIntA(a, ARCHIVE_OK, archive_read_free(a));
788 }
789 
790 DEFINE_TEST(test_read_format_zip_zstd_multi)
791 {
792 	const char *refname = "test_read_format_zip_zstd_multi.zipx";
793 	struct archive *a;
794 	struct archive_entry *ae;
795 
796 	assert((a = archive_read_new()) != NULL);
797 	if (ARCHIVE_OK != archive_read_support_filter_zstd(a)) {
798 		skipping("zstd is not fully supported on this platform");
799 		archive_read_close(a);
800 		archive_read_free(a);
801 		return;
802 	}
803 	extract_reference_file(refname);
804 
805 	assertEqualIntA(a, ARCHIVE_OK, archive_read_support_format_zip(a));
806 	assertEqualIntA(a, ARCHIVE_OK, archive_read_open_filename(a, refname, 37));
807 
808 	assertEqualIntA(a, ARCHIVE_OK, archive_read_next_header(a, &ae));
809 	assertEqualString("ZIP 2.0 (zstd)", archive_format_name(a));
810 	assertEqualString("smartd.conf", archive_entry_pathname(ae));
811 	assertEqualIntA(a, 0, extract_one(a, ae, 0x8DD7379E));
812 
813 	assertEqualIntA(a, ARCHIVE_OK, archive_read_next_header(a, &ae));
814 	assertEqualString("ZIP 2.0 (zstd)", archive_format_name(a));
815 	assertEqualString("ts.conf", archive_entry_pathname(ae));
816 	assertEqualIntA(a, 0, extract_one(a, ae, 0x7AE59B31));
817 
818 	assertEqualIntA(a, ARCHIVE_OK, archive_read_next_header(a, &ae));
819 	assertEqualString("ZIP 2.0 (zstd)", archive_format_name(a));
820 	assertEqualString("vimrc", archive_entry_pathname(ae));
821 	assertEqualIntA(a, 0, extract_one(a, ae, 0xBA8E3BAA));
822 
823 	assertEqualIntA(a, ARCHIVE_EOF, archive_read_next_header(a, &ae));
824 	assertEqualIntA(a, ARCHIVE_OK, archive_read_close(a));
825 	assertEqualIntA(a, ARCHIVE_OK, archive_read_free(a));
826 }
827 
828 DEFINE_TEST(test_read_format_zip_zstd_multi_blockread)
829 {
830 	const char *refname = "test_read_format_zip_zstd_multi.zipx";
831 	struct archive *a;
832 	struct archive_entry *ae;
833 
834 	assert((a = archive_read_new()) != NULL);
835 	if (ARCHIVE_OK != archive_read_support_filter_zstd(a)) {
836 		skipping("zstd is not fully supported on this platform");
837 		archive_read_close(a);
838 		archive_read_free(a);
839 		return;
840 	}
841 	extract_reference_file(refname);
842 
843 	assertEqualIntA(a, ARCHIVE_OK, archive_read_support_format_zip(a));
844 	assertEqualIntA(a, ARCHIVE_OK, archive_read_open_filename(a, refname, 37));
845 
846 	assertEqualIntA(a, ARCHIVE_OK, archive_read_next_header(a, &ae));
847 	assertEqualString("ZIP 2.0 (zstd)", archive_format_name(a));
848 	assertEqualString("smartd.conf", archive_entry_pathname(ae));
849 	assertEqualIntA(a, 0, extract_one_using_blocks(a, 12, 0x8DD7379E));
850 
851 	assertEqualIntA(a, ARCHIVE_OK, archive_read_next_header(a, &ae));
852 	assertEqualString("ZIP 2.0 (zstd)", archive_format_name(a));
853 	assertEqualString("ts.conf", archive_entry_pathname(ae));
854 	assertEqualIntA(a, 0, extract_one_using_blocks(a, 13, 0x7AE59B31));
855 
856 	assertEqualIntA(a, ARCHIVE_OK, archive_read_next_header(a, &ae));
857 	assertEqualString("ZIP 2.0 (zstd)", archive_format_name(a));
858 	assertEqualString("vimrc", archive_entry_pathname(ae));
859 	assertEqualIntA(a, 0, extract_one_using_blocks(a, 14, 0xBA8E3BAA));
860 
861 	assertEqualIntA(a, ARCHIVE_EOF, archive_read_next_header(a, &ae));
862 	assertEqualIntA(a, ARCHIVE_OK, archive_read_close(a));
863 	assertEqualIntA(a, ARCHIVE_OK, archive_read_free(a));
864 }
865 
866 DEFINE_TEST(test_read_format_zip_xz_multi)
867 {
868 	const char *refname = "test_read_format_zip_xz_multi.zipx";
869 	struct archive *a;
870 	struct archive_entry *ae;
871 
872 	assert((a = archive_read_new()) != NULL);
873 	if (ARCHIVE_OK != archive_read_support_filter_lzma(a)) {
874 			skipping("lzma reading not fully supported on this platform");
875 			assertEqualInt(ARCHIVE_OK, archive_read_free(a));
876 			return;
877 	}
878 	extract_reference_file(refname);
879 
880 	assertEqualIntA(a, ARCHIVE_OK, archive_read_support_format_zip(a));
881 	assertEqualIntA(a, ARCHIVE_OK, archive_read_open_filename(a, refname, 37));
882 
883 	assertEqualIntA(a, ARCHIVE_OK, archive_read_next_header(a, &ae));
884 	assertEqualString("ZIP 2.0 (xz)", archive_format_name(a));
885 	assertEqualString("bash.bashrc", archive_entry_pathname(ae));
886 	assertEqualIntA(a, 0, extract_one(a, ae, 0xF751B8C9));
887 
888 	assertEqualIntA(a, ARCHIVE_OK, archive_read_next_header(a, &ae));
889 	assertEqualString("ZIP 2.0 (xz)", archive_format_name(a));
890 	assertEqualString("pacman.conf", archive_entry_pathname(ae));
891 	assertEqualIntA(a, 0, extract_one(a, ae, 0xB20B7F88));
892 
893 	assertEqualIntA(a, ARCHIVE_OK, archive_read_next_header(a, &ae));
894 	assertEqualString("ZIP 2.0 (xz)", archive_format_name(a));
895 	assertEqualString("profile", archive_entry_pathname(ae));
896 	assertEqualIntA(a, 0, extract_one(a, ae, 0x2329F054));
897 
898 	assertEqualIntA(a, ARCHIVE_EOF, archive_read_next_header(a, &ae));
899 	assertEqualIntA(a, ARCHIVE_OK, archive_read_close(a));
900 	assertEqualIntA(a, ARCHIVE_OK, archive_read_free(a));
901 }
902 
903 DEFINE_TEST(test_read_format_zip_xz_multi_blockread)
904 {
905 	const char *refname = "test_read_format_zip_xz_multi.zipx";
906 	struct archive *a;
907 	struct archive_entry *ae;
908 
909 	assert((a = archive_read_new()) != NULL);
910 	if (ARCHIVE_OK != archive_read_support_filter_lzma(a)) {
911 			skipping("lzma reading not fully supported on this platform");
912 			assertEqualInt(ARCHIVE_OK, archive_read_free(a));
913 			return;
914 	}
915 	extract_reference_file(refname);
916 
917 	assertEqualIntA(a, ARCHIVE_OK, archive_read_support_format_zip(a));
918 	assertEqualIntA(a, ARCHIVE_OK, archive_read_open_filename(a, refname, 37));
919 
920 	assertEqualIntA(a, ARCHIVE_OK, archive_read_next_header(a, &ae));
921 	assertEqualString("ZIP 2.0 (xz)", archive_format_name(a));
922 	assertEqualString("bash.bashrc", archive_entry_pathname(ae));
923 	assertEqualIntA(a, 0, extract_one_using_blocks(a, 12, 0xF751B8C9));
924 
925 	assertEqualIntA(a, ARCHIVE_OK, archive_read_next_header(a, &ae));
926 	assertEqualString("ZIP 2.0 (xz)", archive_format_name(a));
927 	assertEqualString("pacman.conf", archive_entry_pathname(ae));
928 	assertEqualIntA(a, 0, extract_one_using_blocks(a, 13, 0xB20B7F88));
929 
930 	assertEqualIntA(a, ARCHIVE_OK, archive_read_next_header(a, &ae));
931 	assertEqualString("ZIP 2.0 (xz)", archive_format_name(a));
932 	assertEqualString("profile", archive_entry_pathname(ae));
933 	assertEqualIntA(a, 0, extract_one_using_blocks(a, 14, 0x2329F054));
934 
935 	assertEqualIntA(a, ARCHIVE_EOF, archive_read_next_header(a, &ae));
936 	assertEqualIntA(a, ARCHIVE_OK, archive_read_close(a));
937 	assertEqualIntA(a, ARCHIVE_OK, archive_read_free(a));
938 }
939 
940 DEFINE_TEST(test_read_format_zip_ppmd8_crash_1)
941 {
942 	const char *refname = "test_read_format_zip_ppmd8_crash_2.zipx";
943 	struct archive *a;
944 	struct archive_entry *ae;
945 	char buf[64];
946 
947 	extract_reference_file(refname);
948 
949 	assert((a = archive_read_new()) != NULL);
950 	assertEqualIntA(a, ARCHIVE_OK, archive_read_support_format_zip(a));
951 	assertEqualIntA(a, ARCHIVE_OK, archive_read_open_filename(a, refname, 100));
952 	assertEqualIntA(a, ARCHIVE_OK, archive_read_next_header(a, &ae));
953 
954 	/* This file shouldn't be properly decompressed, because it's invalid.
955 	 * However, unpacker should return an error during unpacking. Without the
956 	 * proper fix, the unpacker was entering an unlimited loop. */
957 	assertEqualIntA(a, ARCHIVE_FATAL, archive_read_data(a, buf, 1));
958 	assertEqualIntA(a, ARCHIVE_OK, archive_read_close(a));
959 	assertEqualIntA(a, ARCHIVE_OK, archive_read_free(a));
960 }
961 
962 DEFINE_TEST(test_read_format_zip_bz2_hang_on_invalid)
963 {
964 	const char *refname = "test_read_format_zip_bz2_hang.zip";
965 	struct archive *a;
966 	struct archive_entry *ae;
967 	char buf[8];
968 
969 	assert((a = archive_read_new()) != NULL);
970 	if (ARCHIVE_OK != archive_read_support_filter_bzip2(a)) {
971 		skipping("bzip2 is not fully supported on this platform");
972 		archive_read_close(a);
973 		return;
974 	}
975 	extract_reference_file(refname);
976 
977 	assertEqualIntA(a, ARCHIVE_OK, archive_read_support_format_zip(a));
978 	assertEqualIntA(a, ARCHIVE_OK, archive_read_open_filename(a, refname, 37));
979 	assertEqualIntA(a, ARCHIVE_OK, archive_read_next_header(a, &ae));
980 
981 	/* The file `refname` is invalid in this case, so this call should fail.
982 	 * But it shouldn't crash. */
983 	assertEqualIntA(a, ARCHIVE_FATAL, archive_read_data(a, buf, 64));
984 	assertEqualIntA(a, ARCHIVE_OK, archive_read_close(a));
985 	assertEqualIntA(a, ARCHIVE_OK, archive_read_free(a));
986 }
987 
988 DEFINE_TEST(test_read_format_zip_ppmd8_crash_2)
989 {
990 	const char *refname = "test_read_format_zip_ppmd8_crash_2.zipx";
991 	struct archive *a;
992 	struct archive_entry *ae;
993 	char buf[64];
994 
995 	extract_reference_file(refname);
996 
997 	assert((a = archive_read_new()) != NULL);
998 	assertEqualIntA(a, ARCHIVE_OK, archive_read_support_format_zip(a));
999 	assertEqualIntA(a, ARCHIVE_OK, archive_read_open_filename(a, refname, 37));
1000 	assertEqualIntA(a, ARCHIVE_OK, archive_read_next_header(a, &ae));
1001 
1002 	/* The file `refname` is invalid in this case, so this call should fail.
1003 	 * But it shouldn't crash. */
1004 	assertEqualIntA(a, ARCHIVE_FATAL, archive_read_data(a, buf, 64));
1005 	assertEqualIntA(a, ARCHIVE_OK, archive_read_close(a));
1006 	assertEqualIntA(a, ARCHIVE_OK, archive_read_free(a));
1007 }
1008 
1009 DEFINE_TEST(test_read_format_zip_lzma_alone_leak)
1010 {
1011 	const char *refname = "test_read_format_zip_lzma_alone_leak.zipx";
1012 	struct archive *a;
1013 	struct archive_entry *ae;
1014 	char buf[64];
1015 
1016 	/* OSSFuzz #14470 sample file. */
1017 	extract_reference_file(refname);
1018 
1019 	assert((a = archive_read_new()) != NULL);
1020 	if(ARCHIVE_OK != archive_read_support_filter_lzma(a)) {
1021 		skipping("lzma reading is not fully supported on this platform");
1022 		archive_read_close(a);
1023 		archive_read_free(a);
1024 		return;
1025 	}
1026 
1027 	assertEqualIntA(a, ARCHIVE_OK, archive_read_support_format_zip(a));
1028 	assertEqualIntA(a, ARCHIVE_OK, archive_read_open_filename(a, refname, 37));
1029 	assertEqualIntA(a, ARCHIVE_OK, archive_read_next_header(a, &ae));
1030 
1031 	/* Extraction of this file should fail, because the sample file is invalid.
1032 	 * But it shouldn't crash. */
1033 	assertEqualIntA(a, ARCHIVE_FAILED, archive_read_data(a, buf, sizeof(buf)));
1034 	assertEqualIntA(a, ARCHIVE_OK, archive_read_next_header(a, &ae));
1035 
1036 	/* Extraction of this file should fail, because the sample file is invalid.
1037 	 * But it shouldn't crash. */
1038 	assertEqualIntA(a, ARCHIVE_FATAL, archive_read_data(a, buf, sizeof(buf)));
1039 
1040 	assertEqualIntA(a, ARCHIVE_OK, archive_read_close(a));
1041 	assertEqualIntA(a, ARCHIVE_OK, archive_read_free(a));
1042 
1043 	/* This testcase shouldn't produce any memory leaks. When running test
1044 	 * suite under Valgrind or ASan, the test runner won't return with
1045 	 * exit code 0 in case if a memory leak. */
1046 }
1047 
1048 DEFINE_TEST(test_read_format_zip_lzma_stream_end)
1049 {
1050 	const char *refname = "test_read_format_zip_lzma_stream_end.zipx";
1051 	struct archive *a;
1052 	struct archive_entry *ae;
1053 
1054 	assert((a = archive_read_new()) != NULL);
1055 		if (ARCHIVE_OK != archive_read_support_filter_lzma(a)) {
1056 				skipping("lzma reading not fully supported on this platform");
1057 				assertEqualInt(ARCHIVE_OK, archive_read_free(a));
1058 				return;
1059 		}
1060 	extract_reference_file(refname);
1061 
1062 	assertEqualIntA(a, ARCHIVE_OK, archive_read_support_format_zip(a));
1063 	assertEqualIntA(a, ARCHIVE_OK, archive_read_open_filename(a, refname, 37));
1064 	assertEqualIntA(a, ARCHIVE_OK, archive_read_next_header(a, &ae));
1065 	assertEqualString("ZIP 6.3 (lzma)", archive_format_name(a));
1066 	assertEqualString("vimrc", archive_entry_pathname(ae));
1067 	assertEqualIntA(a, 0, extract_one(a, ae, 0xBA8E3BAA));
1068 	assertEqualIntA(a, ARCHIVE_EOF, archive_read_next_header(a, &ae));
1069 	assertEqualIntA(a, ARCHIVE_OK, archive_read_close(a));
1070 	assertEqualIntA(a, ARCHIVE_OK, archive_read_free(a));
1071 }
1072 
1073 DEFINE_TEST(test_read_format_zip_lzma_stream_end_blockread)
1074 {
1075 	const char *refname = "test_read_format_zip_lzma_stream_end.zipx";
1076 	struct archive *a;
1077 	struct archive_entry *ae;
1078 
1079 	assert((a = archive_read_new()) != NULL);
1080 	if (ARCHIVE_OK != archive_read_support_filter_lzma(a)) {
1081 			skipping("lzma reading not fully supported on this platform");
1082 			assertEqualInt(ARCHIVE_OK, archive_read_free(a));
1083 			return;
1084 	}
1085 	extract_reference_file(refname);
1086 
1087 	assertEqualIntA(a, ARCHIVE_OK, archive_read_support_format_zip(a));
1088 	assertEqualIntA(a, ARCHIVE_OK, archive_read_open_filename(a, refname, 37));
1089 	assertEqualIntA(a, ARCHIVE_OK, archive_read_next_header(a, &ae));
1090 	assertEqualString("ZIP 6.3 (lzma)", archive_format_name(a));
1091 	assertEqualString("vimrc", archive_entry_pathname(ae));
1092 	assertEqualIntA(a, 0, extract_one_using_blocks(a, 13, 0xBA8E3BAA));
1093 	assertEqualIntA(a, ARCHIVE_EOF, archive_read_next_header(a, &ae));
1094 	assertEqualIntA(a, ARCHIVE_OK, archive_read_close(a));
1095 	assertEqualIntA(a, ARCHIVE_OK, archive_read_free(a));
1096 }
1097 
1098 DEFINE_TEST(test_read_format_zip_7z_lzma)
1099 {
1100 	const char *refname = "test_read_format_zip_7z_lzma.zip";
1101 	struct archive_entry *ae;
1102 	struct archive *a;
1103 
1104 	assert((a = archive_read_new()) != NULL);
1105 	if (ARCHIVE_OK != archive_read_support_filter_lzma(a)) {
1106 			skipping("lzma reading not fully supported on this platform");
1107 			assertEqualInt(ARCHIVE_OK, archive_read_free(a));
1108 			return;
1109 	}
1110 	extract_reference_file(refname);
1111 
1112 	assertEqualIntA(a, ARCHIVE_OK, archive_read_support_format_zip(a));
1113 	assertEqualIntA(a, ARCHIVE_OK,
1114 		archive_read_open_filename(a, refname, 10240));
1115 	//read directories
1116 	assertEqualIntA(a, ARCHIVE_OK, archive_read_next_header(a, &ae));
1117 	assertEqualIntA(a, ARCHIVE_OK, archive_read_next_header(a, &ae));
1118 	assertEqualIntA(a, ARCHIVE_OK, archive_read_next_header(a, &ae));
1119 	assertEqualIntA(a, ARCHIVE_OK, archive_read_next_header(a, &ae));
1120 	//read symlink
1121 	assertEqualIntA(a, ARCHIVE_OK, archive_read_next_header(a, &ae));
1122 	assertEqualInt(AE_IFLNK, archive_entry_filetype(ae));
1123 	assertEqualString("../samples/abc_measurement_analysis_sample"
1124 		"/src/abc_measurement_analysis_sample.py",
1125 		archive_entry_symlink(ae));
1126 	assertEqualIntA(a, ARCHIVE_OK, archive_read_close(a));
1127 	assertEqualIntA(a, ARCHIVE_OK, archive_read_free(a));
1128 }
1129 
1130 DEFINE_TEST(test_read_format_zip_7z_deflate)
1131 {
1132 	const char *refname = "test_read_format_zip_7z_deflate.zip";
1133 	struct archive_entry *ae;
1134 	struct archive *a;
1135 	int r;
1136 
1137 	assert((a = archive_read_new()) != NULL);
1138 	assertEqualIntA(a, ARCHIVE_OK, archive_read_support_filter_all(a));
1139 	extract_reference_file(refname);
1140 
1141 	assertEqualIntA(a, ARCHIVE_OK, archive_read_support_format_zip(a));
1142 	assertEqualIntA(a, ARCHIVE_OK,
1143 		archive_read_open_filename(a, refname, 10240));
1144 	//read first symlink
1145 	r = archive_read_next_header(a, &ae);
1146 	if (archive_zlib_version() == NULL) {
1147 		assertEqualInt(ARCHIVE_FAILED, r);
1148 		assertEqualString(archive_error_string(a),
1149 		    "Unsupported ZIP compression method during decompression "
1150 		    "of link entry (8: deflation)");
1151 		assert(archive_errno(a) != 0);
1152 	} else {
1153 		assertEqualIntA(a, ARCHIVE_OK, r);
1154 		assertEqualString("libxkbcommon-x11.so.0.0.0",
1155 			archive_entry_symlink(ae));
1156 	}
1157 	assertEqualInt(AE_IFLNK, archive_entry_filetype(ae));
1158 	//read second symlink
1159 	r = archive_read_next_header(a, &ae);
1160 	if (archive_zlib_version() == NULL) {
1161 		assertEqualInt(ARCHIVE_FAILED, r);
1162 		assertEqualString(archive_error_string(a),
1163 		    "Unsupported ZIP compression method during decompression "
1164 		    "of link entry (8: deflation)");
1165 		assert(archive_errno(a) != 0);
1166 	} else {
1167 		assertEqualIntA(a, ARCHIVE_OK, r);
1168 		assertEqualString("libxkbcommon-x11.so.0.0.0",
1169 			archive_entry_symlink(ae));
1170 	}
1171 	assertEqualInt(AE_IFLNK, archive_entry_filetype(ae));
1172 	assertEqualIntA(a, ARCHIVE_OK, archive_read_close(a));
1173 	assertEqualIntA(a, ARCHIVE_OK, archive_read_free(a));
1174 }
1175