1 /*- 2 * Copyright (c) 2003-2008 Tim Kientzle 3 * Copyright (c) 2008 Anselm Strauss 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 27 /* 28 * Development supported by Google Summer of Code 2008. 29 */ 30 31 #include "test.h" 32 33 /* 34 * These tests verify that our reader can read files 35 * created by our writer. 36 */ 37 38 /* 39 * Write a variety of different file types into the archive. 40 */ 41 static void 42 write_contents(struct archive *a) 43 { 44 struct archive_entry *ae; 45 46 /* 47 * First write things with the "default" compression. 48 * The library will choose "deflate" for most things if it's 49 * available, else "store". 50 */ 51 52 /* 53 * Write a file to it. 54 */ 55 assert((ae = archive_entry_new()) != NULL); 56 archive_entry_set_mtime(ae, 1, 10); 57 archive_entry_copy_pathname(ae, "file"); 58 archive_entry_set_mode(ae, AE_IFREG | 0755); 59 archive_entry_set_size(ae, 8); 60 assertEqualInt(0, archive_write_header(a, ae)); 61 archive_entry_free(ae); 62 assertEqualInt(8, archive_write_data(a, "12345678", 9)); 63 assertEqualInt(0, archive_write_data(a, "1", 1)); 64 65 /* 66 * Write another file to it. 67 */ 68 assert((ae = archive_entry_new()) != NULL); 69 archive_entry_set_mtime(ae, 1, 10); 70 archive_entry_copy_pathname(ae, "file2"); 71 archive_entry_set_mode(ae, AE_IFREG | 0755); 72 archive_entry_set_size(ae, 4); 73 assertEqualInt(ARCHIVE_OK, archive_write_header(a, ae)); 74 archive_entry_free(ae); 75 assertEqualInt(4, archive_write_data(a, "1234", 4)); 76 77 /* 78 * Write a file with an unknown size. 79 */ 80 assert((ae = archive_entry_new()) != NULL); 81 archive_entry_set_mtime(ae, 2, 15); 82 archive_entry_copy_pathname(ae, "file3"); 83 archive_entry_set_mode(ae, AE_IFREG | 0621); 84 archive_entry_unset_size(ae); 85 assertEqualInt(ARCHIVE_OK, archive_write_header(a, ae)); 86 archive_entry_free(ae); 87 assertEqualInt(5, archive_write_data(a, "mnopq", 5)); 88 89 /* 90 * Write symbolic link. 91 */ 92 assert((ae = archive_entry_new()) != NULL); 93 archive_entry_set_mtime(ae, 1, 10); 94 assertEqualInt(1, archive_entry_mtime(ae)); 95 assertEqualInt(10, archive_entry_mtime_nsec(ae)); 96 archive_entry_copy_pathname(ae, "symlink"); 97 assertEqualString("symlink", archive_entry_pathname(ae)); 98 archive_entry_copy_symlink(ae, "file1"); 99 assertEqualString("file1", archive_entry_symlink(ae)); 100 archive_entry_set_mode(ae, AE_IFLNK | 0755); 101 assertEqualInt((AE_IFLNK | 0755), archive_entry_mode(ae)); 102 archive_entry_set_size(ae, 4); 103 104 assertEqualInt(ARCHIVE_OK, archive_write_header(a, ae)); 105 archive_entry_free(ae); 106 107 /* 108 * Write a directory to it. 109 */ 110 assert((ae = archive_entry_new()) != NULL); 111 archive_entry_set_mtime(ae, 11, 110); 112 archive_entry_copy_pathname(ae, "dir"); 113 archive_entry_set_mode(ae, S_IFDIR | 0755); 114 archive_entry_set_size(ae, 512); 115 assertEqualIntA(a, ARCHIVE_OK, archive_write_header(a, ae)); 116 failure("size should be zero so that applications know not to write"); 117 assertEqualInt(0, archive_entry_size(ae)); 118 archive_entry_free(ae); 119 assertEqualIntA(a, 0, archive_write_data(a, "12345678", 9)); 120 121 /* 122 * Force "deflate" compression if the platform supports it. 123 */ 124 #ifdef HAVE_ZLIB_H 125 assertEqualIntA(a, ARCHIVE_OK, archive_write_zip_set_compression_deflate(a)); 126 127 /* 128 * Write a file to it. 129 */ 130 assert((ae = archive_entry_new()) != NULL); 131 archive_entry_set_mtime(ae, 1, 10); 132 archive_entry_copy_pathname(ae, "file_deflate"); 133 archive_entry_set_mode(ae, AE_IFREG | 0755); 134 archive_entry_set_size(ae, 8); 135 assertEqualIntA(a, ARCHIVE_OK, archive_write_header(a, ae)); 136 archive_entry_free(ae); 137 assertEqualInt(8, archive_write_data(a, "12345678", 9)); 138 assertEqualInt(0, archive_write_data(a, "1", 1)); 139 140 /* 141 * Write another file to it. 142 */ 143 assert((ae = archive_entry_new()) != NULL); 144 archive_entry_set_mtime(ae, 1, 10); 145 archive_entry_copy_pathname(ae, "file2_deflate"); 146 archive_entry_set_mode(ae, AE_IFREG | 0755); 147 archive_entry_set_size(ae, 4); 148 assertEqualIntA(a, ARCHIVE_OK, archive_write_header(a, ae)); 149 archive_entry_free(ae); 150 assertEqualInt(4, archive_write_data(a, "1234", 4)); 151 152 /* 153 * Write a file with an unknown size. 154 */ 155 assert((ae = archive_entry_new()) != NULL); 156 archive_entry_set_mtime(ae, 2, 15); 157 archive_entry_copy_pathname(ae, "file3_deflate"); 158 archive_entry_set_mode(ae, AE_IFREG | 0621); 159 archive_entry_unset_size(ae); 160 assertEqualInt(ARCHIVE_OK, archive_write_header(a, ae)); 161 archive_entry_free(ae); 162 assertEqualInt(5, archive_write_data(a, "ghijk", 5)); 163 164 /* 165 * Write symbolic like file to it. 166 */ 167 assert((ae = archive_entry_new()) != NULL); 168 archive_entry_set_mtime(ae, 1, 10); 169 archive_entry_copy_pathname(ae, "symlink_deflate"); 170 archive_entry_copy_symlink(ae, "file1"); 171 archive_entry_set_mode(ae, AE_IFLNK | 0755); 172 archive_entry_set_size(ae, 4); 173 assertEqualInt(ARCHIVE_OK, archive_write_header(a, ae)); 174 archive_entry_free(ae); 175 176 /* 177 * Write a directory to it. 178 */ 179 assert((ae = archive_entry_new()) != NULL); 180 archive_entry_set_mtime(ae, 11, 110); 181 archive_entry_copy_pathname(ae, "dir_deflate"); 182 archive_entry_set_mode(ae, S_IFDIR | 0755); 183 archive_entry_set_size(ae, 512); 184 assertEqualIntA(a, ARCHIVE_OK, archive_write_header(a, ae)); 185 failure("size should be zero so that applications know not to write"); 186 assertEqualInt(0, archive_entry_size(ae)); 187 archive_entry_free(ae); 188 assertEqualIntA(a, 0, archive_write_data(a, "12345678", 9)); 189 assertEqualIntA(a, ARCHIVE_OK, archive_write_finish_entry(a)); 190 #endif 191 192 /* 193 * Now write a bunch of entries with "store" compression. 194 */ 195 assertEqualIntA(a, ARCHIVE_OK, archive_write_zip_set_compression_store(a)); 196 197 /* 198 * Write a file to it. 199 */ 200 assert((ae = archive_entry_new()) != NULL); 201 archive_entry_set_mtime(ae, 1, 10); 202 archive_entry_copy_pathname(ae, "file_stored"); 203 archive_entry_set_mode(ae, AE_IFREG | 0755); 204 archive_entry_set_size(ae, 8); 205 assertEqualInt(0, archive_write_header(a, ae)); 206 archive_entry_free(ae); 207 assertEqualInt(8, archive_write_data(a, "12345678", 9)); 208 assertEqualInt(0, archive_write_data(a, "1", 1)); 209 210 /* 211 * Write another file to it. 212 */ 213 assert((ae = archive_entry_new()) != NULL); 214 archive_entry_set_mtime(ae, 1, 10); 215 archive_entry_copy_pathname(ae, "file2_stored"); 216 archive_entry_set_mode(ae, AE_IFREG | 0755); 217 archive_entry_set_size(ae, 4); 218 assertEqualInt(ARCHIVE_OK, archive_write_header(a, ae)); 219 archive_entry_free(ae); 220 assertEqualInt(4, archive_write_data(a, "ACEG", 4)); 221 222 /* 223 * Write a file with an unknown size. 224 */ 225 assert((ae = archive_entry_new()) != NULL); 226 archive_entry_set_mtime(ae, 2, 15); 227 archive_entry_copy_pathname(ae, "file3_stored"); 228 archive_entry_set_mode(ae, AE_IFREG | 0621); 229 archive_entry_unset_size(ae); 230 assertEqualInt(ARCHIVE_OK, archive_write_header(a, ae)); 231 archive_entry_free(ae); 232 assertEqualInt(5, archive_write_data(a, "ijklm", 5)); 233 234 /* 235 * Write symbolic like file to it. 236 */ 237 assert((ae = archive_entry_new()) != NULL); 238 archive_entry_set_mtime(ae, 1, 10); 239 archive_entry_copy_pathname(ae, "symlink_stored"); 240 archive_entry_copy_symlink(ae, "file1"); 241 archive_entry_set_mode(ae, AE_IFLNK | 0755); 242 archive_entry_set_size(ae, 4); 243 assertEqualInt(ARCHIVE_OK, archive_write_header(a, ae)); 244 archive_entry_free(ae); 245 246 /* 247 * Write a directory to it. 248 */ 249 assert((ae = archive_entry_new()) != NULL); 250 archive_entry_set_mtime(ae, 11, 110); 251 archive_entry_copy_pathname(ae, "dir_stored"); 252 archive_entry_set_mode(ae, S_IFDIR | 0755); 253 archive_entry_set_size(ae, 512); 254 assertEqualIntA(a, ARCHIVE_OK, archive_write_header(a, ae)); 255 failure("size should be zero so that applications know not to write"); 256 assertEqualInt(0, archive_entry_size(ae)); 257 archive_entry_free(ae); 258 assertEqualIntA(a, 0, archive_write_data(a, "12345678", 9)); 259 260 261 /* Close out the archive. */ 262 assertEqualInt(ARCHIVE_OK, archive_write_close(a)); 263 assertEqualInt(ARCHIVE_OK, archive_write_free(a)); 264 } 265 266 /* 267 * Read back all of the entries and verify their values. 268 */ 269 static void 270 verify_contents(struct archive *a, int seeking, int improved_streaming) 271 { 272 char filedata[64]; 273 struct archive_entry *ae; 274 275 /* 276 * Default compression options: 277 */ 278 279 /* Read and verify first file. */ 280 assertEqualIntA(a, ARCHIVE_OK, archive_read_next_header(a, &ae)); 281 assertEqualInt(1, archive_entry_mtime(ae)); 282 /* Zip doesn't store high-resolution mtime. */ 283 assertEqualInt(0, archive_entry_mtime_nsec(ae)); 284 assertEqualInt(0, archive_entry_atime(ae)); 285 assertEqualInt(0, archive_entry_ctime(ae)); 286 assertEqualString("file", archive_entry_pathname(ae)); 287 if (seeking || improved_streaming) { 288 assertEqualInt(AE_IFREG | 0755, archive_entry_mode(ae)); 289 } 290 if (seeking) { 291 assertEqualInt(8, archive_entry_size(ae)); 292 assert(archive_entry_size_is_set(ae)); 293 } else { 294 assertEqualInt(0, archive_entry_size_is_set(ae)); 295 } 296 assertEqualIntA(a, 8, 297 archive_read_data(a, filedata, sizeof(filedata))); 298 assertEqualMem(filedata, "12345678", 8); 299 300 301 /* Read the second file back. */ 302 assertEqualIntA(a, ARCHIVE_OK, archive_read_next_header(a, &ae)); 303 assertEqualInt(1, archive_entry_mtime(ae)); 304 assertEqualInt(0, archive_entry_mtime_nsec(ae)); 305 assertEqualInt(0, archive_entry_atime(ae)); 306 assertEqualInt(0, archive_entry_ctime(ae)); 307 assertEqualString("file2", archive_entry_pathname(ae)); 308 if (seeking || improved_streaming) { 309 assertEqualInt(AE_IFREG | 0755, archive_entry_mode(ae)); 310 } 311 if (seeking) { 312 assertEqualInt(4, archive_entry_size(ae)); 313 assert(archive_entry_size_is_set(ae)); 314 } else { 315 assertEqualInt(0, archive_entry_size_is_set(ae)); 316 } 317 assertEqualIntA(a, 4, 318 archive_read_data(a, filedata, sizeof(filedata))); 319 assertEqualMem(filedata, "1234", 4); 320 321 /* Read the third file back. */ 322 assertEqualIntA(a, ARCHIVE_OK, archive_read_next_header(a, &ae)); 323 assertEqualInt(2, archive_entry_mtime(ae)); 324 assertEqualInt(0, archive_entry_mtime_nsec(ae)); 325 assertEqualInt(0, archive_entry_atime(ae)); 326 assertEqualInt(0, archive_entry_ctime(ae)); 327 assertEqualString("file3", archive_entry_pathname(ae)); 328 if (seeking || improved_streaming) { 329 assertEqualInt(AE_IFREG | 0621, archive_entry_mode(ae)); 330 } 331 if (seeking) { 332 assertEqualInt(5, archive_entry_size(ae)); 333 assert(archive_entry_size_is_set(ae)); 334 } else { 335 assertEqualInt(0, archive_entry_size_is_set(ae)); 336 } 337 assertEqualIntA(a, 5, 338 archive_read_data(a, filedata, sizeof(filedata))); 339 assertEqualMem(filedata, "mnopq", 5); 340 341 /* Read symlink. */ 342 assertEqualIntA(a, ARCHIVE_OK, archive_read_next_header(a, &ae)); 343 assertEqualInt(1, archive_entry_mtime(ae)); 344 assertEqualInt(0, archive_entry_mtime_nsec(ae)); 345 assertEqualInt(0, archive_entry_atime(ae)); 346 assertEqualInt(0, archive_entry_ctime(ae)); 347 assertEqualString("symlink", archive_entry_pathname(ae)); 348 if (seeking || improved_streaming) { 349 assertEqualInt(AE_IFLNK | 0755, archive_entry_mode(ae)); 350 assertEqualInt(0, archive_entry_size(ae)); 351 assertEqualString("file1", archive_entry_symlink(ae)); 352 } else { 353 /* Streaming cannot read file type, so 354 * symlink body shows as regular file contents. */ 355 assertEqualInt(AE_IFREG | 0664, archive_entry_mode(ae)); 356 assertEqualInt(5, archive_entry_size(ae)); 357 assert(archive_entry_size_is_set(ae)); 358 } 359 360 /* Read the dir entry back. */ 361 assertEqualIntA(a, ARCHIVE_OK, archive_read_next_header(a, &ae)); 362 assertEqualInt(11, archive_entry_mtime(ae)); 363 assertEqualInt(0, archive_entry_mtime_nsec(ae)); 364 assertEqualInt(0, archive_entry_atime(ae)); 365 assertEqualInt(0, archive_entry_ctime(ae)); 366 assertEqualString("dir/", archive_entry_pathname(ae)); 367 if (seeking || improved_streaming) { 368 assertEqualInt(AE_IFDIR | 0755, archive_entry_mode(ae)); 369 } 370 assertEqualInt(0, archive_entry_size(ae)); 371 assert(archive_entry_size_is_set(ae)); 372 assertEqualIntA(a, 0, archive_read_data(a, filedata, 10)); 373 374 #ifdef HAVE_ZLIB_H 375 /* 376 * Deflate compression option: 377 */ 378 379 /* Read and verify first file. */ 380 assertEqualIntA(a, ARCHIVE_OK, archive_read_next_header(a, &ae)); 381 assertEqualInt(1, archive_entry_mtime(ae)); 382 /* Zip doesn't store high-resolution mtime. */ 383 assertEqualInt(0, archive_entry_mtime_nsec(ae)); 384 assertEqualInt(0, archive_entry_atime(ae)); 385 assertEqualInt(0, archive_entry_ctime(ae)); 386 assertEqualString("file_deflate", archive_entry_pathname(ae)); 387 if (seeking || improved_streaming) { 388 assertEqualInt(AE_IFREG | 0755, archive_entry_mode(ae)); 389 } 390 if (seeking) { 391 assertEqualInt(8, archive_entry_size(ae)); 392 assert(archive_entry_size_is_set(ae)); 393 } else { 394 assertEqualInt(0, archive_entry_size_is_set(ae)); 395 } 396 assertEqualIntA(a, 8, 397 archive_read_data(a, filedata, sizeof(filedata))); 398 assertEqualMem(filedata, "12345678", 8); 399 400 401 /* Read the second file back. */ 402 assertEqualIntA(a, ARCHIVE_OK, archive_read_next_header(a, &ae)); 403 assertEqualInt(1, archive_entry_mtime(ae)); 404 assertEqualInt(0, archive_entry_mtime_nsec(ae)); 405 assertEqualInt(0, archive_entry_atime(ae)); 406 assertEqualInt(0, archive_entry_ctime(ae)); 407 assertEqualString("file2_deflate", archive_entry_pathname(ae)); 408 if (seeking || improved_streaming) { 409 assertEqualInt(AE_IFREG | 0755, archive_entry_mode(ae)); 410 } 411 if (seeking) { 412 assertEqualInt(4, archive_entry_size(ae)); 413 assert(archive_entry_size_is_set(ae)); 414 } else { 415 assertEqualInt(0, archive_entry_size_is_set(ae)); 416 } 417 assertEqualIntA(a, 4, 418 archive_read_data(a, filedata, sizeof(filedata))); 419 assertEqualMem(filedata, "1234", 4); 420 421 /* Read the third file back. */ 422 assertEqualIntA(a, ARCHIVE_OK, archive_read_next_header(a, &ae)); 423 assertEqualInt(2, archive_entry_mtime(ae)); 424 assertEqualInt(0, archive_entry_mtime_nsec(ae)); 425 assertEqualInt(0, archive_entry_atime(ae)); 426 assertEqualInt(0, archive_entry_ctime(ae)); 427 assertEqualString("file3_deflate", archive_entry_pathname(ae)); 428 if (seeking || improved_streaming) { 429 assertEqualInt(AE_IFREG | 0621, archive_entry_mode(ae)); 430 } 431 if (seeking) { 432 assertEqualInt(5, archive_entry_size(ae)); 433 assert(archive_entry_size_is_set(ae)); 434 } else { 435 assertEqualInt(0, archive_entry_size_is_set(ae)); 436 } 437 assertEqualIntA(a, 5, 438 archive_read_data(a, filedata, sizeof(filedata))); 439 assertEqualMem(filedata, "ghijk", 4); 440 441 /* Read symlink. */ 442 assertEqualIntA(a, ARCHIVE_OK, archive_read_next_header(a, &ae)); 443 assertEqualInt(1, archive_entry_mtime(ae)); 444 assertEqualInt(0, archive_entry_mtime_nsec(ae)); 445 assertEqualInt(0, archive_entry_atime(ae)); 446 assertEqualInt(0, archive_entry_ctime(ae)); 447 assertEqualString("symlink_deflate", archive_entry_pathname(ae)); 448 if (seeking || improved_streaming) { 449 assertEqualInt(AE_IFLNK | 0755, archive_entry_mode(ae)); 450 assertEqualInt(0, archive_entry_size(ae)); 451 assertEqualString("file1", archive_entry_symlink(ae)); 452 } else { 453 assertEqualInt(AE_IFREG | 0664, archive_entry_mode(ae)); 454 assertEqualInt(5, archive_entry_size(ae)); 455 assertEqualIntA(a, 5, archive_read_data(a, filedata, 10)); 456 assertEqualMem(filedata, "file1", 5); 457 } 458 459 /* Read the dir entry back. */ 460 assertEqualIntA(a, ARCHIVE_OK, archive_read_next_header(a, &ae)); 461 assertEqualInt(11, archive_entry_mtime(ae)); 462 assertEqualInt(0, archive_entry_mtime_nsec(ae)); 463 assertEqualInt(0, archive_entry_atime(ae)); 464 assertEqualInt(0, archive_entry_ctime(ae)); 465 assertEqualString("dir_deflate/", archive_entry_pathname(ae)); 466 if (seeking) { 467 assertEqualInt(AE_IFDIR | 0755, archive_entry_mode(ae)); 468 } 469 assertEqualInt(0, archive_entry_size(ae)); 470 assert(archive_entry_size_is_set(ae)); 471 assertEqualIntA(a, 0, archive_read_data(a, filedata, 10)); 472 #endif 473 474 /* 475 * Store compression option: 476 */ 477 478 /* Read and verify first file. */ 479 assertEqualIntA(a, ARCHIVE_OK, archive_read_next_header(a, &ae)); 480 assertEqualInt(1, archive_entry_mtime(ae)); 481 /* Zip doesn't store high-resolution mtime. */ 482 assertEqualInt(0, archive_entry_mtime_nsec(ae)); 483 assertEqualInt(0, archive_entry_atime(ae)); 484 assertEqualInt(0, archive_entry_ctime(ae)); 485 assertEqualString("file_stored", archive_entry_pathname(ae)); 486 if (seeking || improved_streaming) { 487 assertEqualInt(AE_IFREG | 0755, archive_entry_mode(ae)); 488 } 489 if (seeking) { 490 assert(archive_entry_size_is_set(ae)); 491 assertEqualInt(8, archive_entry_size(ae)); 492 } else { 493 assertEqualInt(0, archive_entry_size_is_set(ae)); 494 } 495 assertEqualIntA(a, 8, 496 archive_read_data(a, filedata, sizeof(filedata))); 497 assertEqualMem(filedata, "12345678", 8); 498 499 500 /* Read the second file back. */ 501 assertEqualIntA(a, ARCHIVE_OK, archive_read_next_header(a, &ae)); 502 assertEqualInt(1, archive_entry_mtime(ae)); 503 assertEqualInt(0, archive_entry_mtime_nsec(ae)); 504 assertEqualInt(0, archive_entry_atime(ae)); 505 assertEqualInt(0, archive_entry_ctime(ae)); 506 assertEqualString("file2_stored", archive_entry_pathname(ae)); 507 if (seeking || improved_streaming) { 508 assertEqualInt(AE_IFREG | 0755, archive_entry_mode(ae)); 509 } 510 if (seeking) { 511 assertEqualInt(4, archive_entry_size(ae)); 512 assert(archive_entry_size_is_set(ae)); 513 } else { 514 assertEqualInt(0, archive_entry_size_is_set(ae)); 515 } 516 assertEqualIntA(a, 4, 517 archive_read_data(a, filedata, sizeof(filedata))); 518 assertEqualMem(filedata, "ACEG", 4); 519 520 /* Read the third file back. */ 521 assertEqualIntA(a, ARCHIVE_OK, archive_read_next_header(a, &ae)); 522 assertEqualInt(2, archive_entry_mtime(ae)); 523 assertEqualInt(0, archive_entry_mtime_nsec(ae)); 524 assertEqualInt(0, archive_entry_atime(ae)); 525 assertEqualInt(0, archive_entry_ctime(ae)); 526 assertEqualString("file3_stored", archive_entry_pathname(ae)); 527 if (seeking || improved_streaming) 528 assertEqualInt(AE_IFREG | 0621, archive_entry_mode(ae)); 529 if (seeking) { 530 assertEqualInt(5, archive_entry_size(ae)); 531 assert(archive_entry_size_is_set(ae)); 532 } else { 533 assertEqualInt(0, archive_entry_size_is_set(ae)); 534 } 535 assertEqualIntA(a, 5, 536 archive_read_data(a, filedata, sizeof(filedata))); 537 assertEqualMem(filedata, "ijklm", 4); 538 539 /* Read symlink. */ 540 assertEqualIntA(a, ARCHIVE_OK, archive_read_next_header(a, &ae)); 541 assertEqualInt(1, archive_entry_mtime(ae)); 542 assertEqualInt(0, archive_entry_mtime_nsec(ae)); 543 assertEqualInt(0, archive_entry_atime(ae)); 544 assertEqualInt(0, archive_entry_ctime(ae)); 545 assertEqualString("symlink_stored", archive_entry_pathname(ae)); 546 if (seeking || improved_streaming) { 547 assertEqualInt(AE_IFLNK | 0755, archive_entry_mode(ae)); 548 assertEqualInt(0, archive_entry_size(ae)); 549 assertEqualString("file1", archive_entry_symlink(ae)); 550 } else { 551 assertEqualInt(AE_IFREG | 0664, archive_entry_mode(ae)); 552 assertEqualInt(5, archive_entry_size(ae)); 553 assertEqualIntA(a, 5, archive_read_data(a, filedata, 10)); 554 assertEqualMem(filedata, "file1", 5); 555 } 556 557 /* Read the dir entry back. */ 558 assertEqualIntA(a, ARCHIVE_OK, archive_read_next_header(a, &ae)); 559 assertEqualInt(11, archive_entry_mtime(ae)); 560 assertEqualInt(0, archive_entry_mtime_nsec(ae)); 561 assertEqualInt(0, archive_entry_atime(ae)); 562 assertEqualInt(0, archive_entry_ctime(ae)); 563 assertEqualString("dir_stored/", archive_entry_pathname(ae)); 564 if (seeking || improved_streaming) 565 assertEqualInt(AE_IFDIR | 0755, archive_entry_mode(ae)); 566 assertEqualInt(0, archive_entry_size(ae)); 567 assertEqualIntA(a, 0, archive_read_data(a, filedata, 10)); 568 569 /* Verify the end of the archive. */ 570 assertEqualIntA(a, ARCHIVE_EOF, archive_read_next_header(a, &ae)); 571 assertEqualInt(ARCHIVE_OK, archive_read_close(a)); 572 assertEqualInt(ARCHIVE_OK, archive_read_free(a)); 573 } 574 575 /* 576 * Do a write-then-read roundtrip. 577 */ 578 DEFINE_TEST(test_write_read_format_zip) 579 { 580 struct archive *a; 581 size_t used; 582 size_t buffsize = 1000000; 583 char *buff; 584 585 buff = malloc(buffsize); 586 587 /* Create a new archive in memory. */ 588 assert((a = archive_write_new()) != NULL); 589 assertEqualIntA(a, ARCHIVE_OK, archive_write_set_format_zip(a)); 590 assertEqualIntA(a, ARCHIVE_OK, archive_write_add_filter_none(a)); 591 assertEqualIntA(a, ARCHIVE_OK, 592 archive_write_open_memory(a, buff, buffsize, &used)); 593 write_contents(a); 594 dumpfile("constructed.zip", buff, used); 595 596 /* 597 * Now, read the data back. 598 */ 599 /* With the standard memory reader. */ 600 assert((a = archive_read_new()) != NULL); 601 assertEqualIntA(a, ARCHIVE_OK, archive_read_support_format_all(a)); 602 assertEqualIntA(a, ARCHIVE_OK, archive_read_support_filter_all(a)); 603 assertEqualIntA(a, ARCHIVE_OK, archive_read_open_memory(a, buff, used)); 604 verify_contents(a, 1, 0); 605 606 /* With the test memory reader -- streaming mode. */ 607 assert((a = archive_read_new()) != NULL); 608 assertEqualIntA(a, ARCHIVE_OK, archive_read_support_format_all(a)); 609 assertEqualIntA(a, ARCHIVE_OK, archive_read_support_filter_all(a)); 610 assertEqualIntA(a, ARCHIVE_OK, read_open_memory(a, buff, used, 7)); 611 /* Streaming reader doesn't see mode information from Central Directory. */ 612 verify_contents(a, 0, 0); 613 614 /* With the test memory reader -- seeking mode. */ 615 assert((a = archive_read_new()) != NULL); 616 assertEqualIntA(a, ARCHIVE_OK, archive_read_support_format_all(a)); 617 assertEqualIntA(a, ARCHIVE_OK, archive_read_support_filter_all(a)); 618 assertEqualIntA(a, ARCHIVE_OK, read_open_memory_seek(a, buff, used, 7)); 619 verify_contents(a, 1, 0); 620 621 free(buff); 622 } 623 624 /* 625 * Do a write-then-read roundtrip with 'el' extension enabled. 626 */ 627 DEFINE_TEST(test_write_read_format_zip_improved_streaming) 628 { 629 struct archive *a; 630 size_t used; 631 size_t buffsize = 1000000; 632 char *buff; 633 634 buff = malloc(buffsize); 635 636 /* Create a new archive in memory. */ 637 assert((a = archive_write_new()) != NULL); 638 assertEqualIntA(a, ARCHIVE_OK, archive_write_set_format_zip(a)); 639 assertEqualIntA(a, ARCHIVE_OK, archive_write_add_filter_none(a)); 640 assertEqualIntA(a, ARCHIVE_OK, archive_write_set_options(a, "zip:experimental")); 641 assertEqualIntA(a, ARCHIVE_OK, 642 archive_write_open_memory(a, buff, buffsize, &used)); 643 write_contents(a); 644 dumpfile("constructed.zip", buff, used); 645 646 /* 647 * Now, read the data back. 648 */ 649 /* With the standard memory reader. */ 650 assert((a = archive_read_new()) != NULL); 651 assertEqualIntA(a, ARCHIVE_OK, archive_read_support_format_all(a)); 652 assertEqualIntA(a, ARCHIVE_OK, archive_read_support_filter_all(a)); 653 assertEqualIntA(a, ARCHIVE_OK, archive_read_open_memory(a, buff, used)); 654 verify_contents(a, 1, 1); 655 656 /* With the test memory reader -- streaming mode. */ 657 assert((a = archive_read_new()) != NULL); 658 assertEqualIntA(a, ARCHIVE_OK, archive_read_support_format_all(a)); 659 assertEqualIntA(a, ARCHIVE_OK, archive_read_support_filter_all(a)); 660 assertEqualIntA(a, ARCHIVE_OK, read_open_memory(a, buff, used, 7)); 661 /* Streaming reader doesn't see mode information from Central Directory. */ 662 verify_contents(a, 0, 1); 663 664 /* With the test memory reader -- seeking mode. */ 665 assert((a = archive_read_new()) != NULL); 666 assertEqualIntA(a, ARCHIVE_OK, archive_read_support_format_all(a)); 667 assertEqualIntA(a, ARCHIVE_OK, archive_read_support_filter_all(a)); 668 assertEqualIntA(a, ARCHIVE_OK, read_open_memory_seek(a, buff, used, 7)); 669 verify_contents(a, 1, 1); 670 671 free(buff); 672 } 673 674 /* 675 * Do a write-then-read roundtrip with Zip64 enabled. 676 */ 677 DEFINE_TEST(test_write_read_format_zip64) 678 { 679 struct archive *a; 680 size_t used; 681 size_t buffsize = 1000000; 682 char *buff; 683 684 buff = malloc(buffsize); 685 686 /* Create a new archive in memory. */ 687 assert((a = archive_write_new()) != NULL); 688 assertEqualIntA(a, ARCHIVE_OK, archive_write_set_format_zip(a)); 689 assertEqualIntA(a, ARCHIVE_OK, archive_write_add_filter_none(a)); 690 assertEqualIntA(a, ARCHIVE_OK, archive_write_set_options(a, "zip:zip64")); 691 #if ZIP_IMPROVED_STREAMING 692 assertEqualIntA(a, ARCHIVE_OK, archive_write_set_options(a, "zip:experimental")); 693 #endif 694 assertEqualIntA(a, ARCHIVE_OK, 695 archive_write_open_memory(a, buff, buffsize, &used)); 696 write_contents(a); 697 dumpfile("constructed64.zip", buff, used); 698 699 /* 700 * Now, read the data back. 701 */ 702 /* With the standard memory reader. */ 703 assert((a = archive_read_new()) != NULL); 704 assertEqualIntA(a, ARCHIVE_OK, archive_read_support_format_all(a)); 705 assertEqualIntA(a, ARCHIVE_OK, archive_read_support_filter_all(a)); 706 assertEqualIntA(a, ARCHIVE_OK, archive_read_open_memory(a, buff, used)); 707 verify_contents(a, 1, 0); 708 709 /* With the test memory reader -- streaming mode. */ 710 assert((a = archive_read_new()) != NULL); 711 assertEqualIntA(a, ARCHIVE_OK, archive_read_support_format_all(a)); 712 assertEqualIntA(a, ARCHIVE_OK, archive_read_support_filter_all(a)); 713 assertEqualIntA(a, ARCHIVE_OK, read_open_memory(a, buff, used, 7)); 714 /* Streaming reader doesn't see mode information from Central Directory. */ 715 verify_contents(a, 0, 0); 716 717 /* With the test memory reader -- seeking mode. */ 718 assert((a = archive_read_new()) != NULL); 719 assertEqualIntA(a, ARCHIVE_OK, archive_read_support_format_all(a)); 720 assertEqualIntA(a, ARCHIVE_OK, archive_read_support_filter_all(a)); 721 assertEqualIntA(a, ARCHIVE_OK, read_open_memory_seek(a, buff, used, 7)); 722 verify_contents(a, 1, 0); 723 724 free(buff); 725 } 726 727 728 /* 729 * Do a write-then-read roundtrip with Zip64 enabled and 'el' extension enabled. 730 */ 731 DEFINE_TEST(test_write_read_format_zip64_improved_streaming) 732 { 733 struct archive *a; 734 size_t used; 735 size_t buffsize = 1000000; 736 char *buff; 737 738 buff = malloc(buffsize); 739 740 /* Create a new archive in memory. */ 741 assert((a = archive_write_new()) != NULL); 742 assertEqualIntA(a, ARCHIVE_OK, archive_write_set_format_zip(a)); 743 assertEqualIntA(a, ARCHIVE_OK, archive_write_add_filter_none(a)); 744 assertEqualIntA(a, ARCHIVE_OK, archive_write_set_options(a, "zip:zip64")); 745 assertEqualIntA(a, ARCHIVE_OK, archive_write_set_options(a, "zip:experimental")); 746 assertEqualIntA(a, ARCHIVE_OK, 747 archive_write_open_memory(a, buff, buffsize, &used)); 748 write_contents(a); 749 dumpfile("constructed64.zip", buff, used); 750 751 /* 752 * Now, read the data back. 753 */ 754 /* With the standard memory reader. */ 755 assert((a = archive_read_new()) != NULL); 756 assertEqualIntA(a, ARCHIVE_OK, archive_read_support_format_all(a)); 757 assertEqualIntA(a, ARCHIVE_OK, archive_read_support_filter_all(a)); 758 assertEqualIntA(a, ARCHIVE_OK, archive_read_open_memory(a, buff, used)); 759 verify_contents(a, 1, 1); 760 761 /* With the test memory reader -- streaming mode. */ 762 assert((a = archive_read_new()) != NULL); 763 assertEqualIntA(a, ARCHIVE_OK, archive_read_support_format_all(a)); 764 assertEqualIntA(a, ARCHIVE_OK, archive_read_support_filter_all(a)); 765 assertEqualIntA(a, ARCHIVE_OK, read_open_memory(a, buff, used, 7)); 766 /* Streaming reader doesn't see mode information from Central Directory. */ 767 verify_contents(a, 0, 1); 768 769 /* With the test memory reader -- seeking mode. */ 770 assert((a = archive_read_new()) != NULL); 771 assertEqualIntA(a, ARCHIVE_OK, archive_read_support_format_all(a)); 772 assertEqualIntA(a, ARCHIVE_OK, archive_read_support_filter_all(a)); 773 assertEqualIntA(a, ARCHIVE_OK, read_open_memory_seek(a, buff, used, 7)); 774 verify_contents(a, 1, 1); 775 776 free(buff); 777 } 778