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