1 /*- 2 * Copyright (c) 2008 Anselm Strauss 3 * All rights reserved. 4 * 5 * Redistribution and use in source and binary forms, with or without 6 * modification, are permitted provided that the following conditions 7 * are met: 8 * 1. Redistributions of source code must retain the above copyright 9 * notice, this list of conditions and the following disclaimer. 10 * 2. Redistributions in binary form must reproduce the above copyright 11 * notice, this list of conditions and the following disclaimer in the 12 * documentation and/or other materials provided with the distribution. 13 * 14 * THIS SOFTWARE IS PROVIDED BY THE AUTHOR(S) ``AS IS'' AND ANY EXPRESS OR 15 * IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE IMPLIED WARRANTIES 16 * OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE ARE DISCLAIMED. 17 * IN NO EVENT SHALL THE AUTHOR(S) BE LIABLE FOR ANY DIRECT, INDIRECT, 18 * INCIDENTAL, SPECIAL, EXEMPLARY, OR CONSEQUENTIAL DAMAGES (INCLUDING, BUT 19 * NOT LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS OR SERVICES; LOSS OF USE, 20 * DATA, OR PROFITS; OR BUSINESS INTERRUPTION) HOWEVER CAUSED AND ON ANY 21 * THEORY OF LIABILITY, WHETHER IN CONTRACT, STRICT LIABILITY, OR TORT 22 * (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY OUT OF THE USE OF 23 * THIS SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF SUCH DAMAGE. 24 */ 25 26 /* 27 * Development supported by Google Summer of Code 2008. 28 */ 29 30 #include "test.h" 31 __FBSDID("$FreeBSD: head/lib/libarchive/test/test_write_format_zip_no_compression.c 201247 2009-12-30 05:59:21Z kientzle $"); 32 33 /* File data */ 34 static const char file_name[] = "file"; 35 static const char file_data1[] = {'1', '2', '3', '4', '5'}; 36 static const char file_data2[] = {'6', '7', '8', '9', '0'}; 37 static const int file_perm = 00644; 38 static const short file_uid = 10; 39 static const short file_gid = 20; 40 41 /* Folder data */ 42 static const char folder_name[] = "folder/"; 43 static const int folder_perm = 00755; 44 static const short folder_uid = 30; 45 static const short folder_gid = 40; 46 47 static time_t now; 48 49 static unsigned long 50 bitcrc32(unsigned long c, const void *_p, size_t s) 51 { 52 /* This is a drop-in replacement for crc32() from zlib. 53 * Libarchive should be able to correctly generate 54 * uncompressed zip archives (including correct CRCs) even 55 * when zlib is unavailable, and this function helps us verify 56 * that. Yes, this is very, very slow and unsuitable for 57 * production use, but it's correct, compact, and works well 58 * enough for this particular usage. Libarchive internally 59 * uses a much more efficient implementation. */ 60 const unsigned char *p = _p; 61 int bitctr; 62 63 if (p == NULL) 64 return (0); 65 66 for (; s > 0; --s) { 67 c ^= *p++; 68 for (bitctr = 8; bitctr > 0; --bitctr) { 69 if (c & 1) c = (c >> 1); 70 else c = (c >> 1) ^ 0xedb88320; 71 c ^= 0x80000000; 72 } 73 } 74 return (c); 75 } 76 77 static void verify_write_uncompressed(struct archive *a) 78 { 79 struct archive_entry *entry; 80 81 /* Write entries. */ 82 83 /* Regular file */ 84 assert((entry = archive_entry_new()) != NULL); 85 archive_entry_set_pathname(entry, file_name); 86 archive_entry_set_mode(entry, S_IFREG | 0644); 87 archive_entry_set_size(entry, sizeof(file_data1) + sizeof(file_data2)); 88 archive_entry_set_uid(entry, file_uid); 89 archive_entry_set_gid(entry, file_gid); 90 archive_entry_set_mtime(entry, now, 0); 91 archive_entry_set_atime(entry, now + 3, 0); 92 assertEqualIntA(a, 0, archive_write_header(a, entry)); 93 assertEqualIntA(a, sizeof(file_data1), archive_write_data(a, file_data1, sizeof(file_data1))); 94 assertEqualIntA(a, sizeof(file_data2), archive_write_data(a, file_data2, sizeof(file_data2))); 95 archive_entry_free(entry); 96 97 /* Folder */ 98 assert((entry = archive_entry_new()) != NULL); 99 archive_entry_set_pathname(entry, folder_name); 100 archive_entry_set_mode(entry, S_IFDIR | folder_perm); 101 archive_entry_set_size(entry, 0); 102 archive_entry_set_uid(entry, folder_uid); 103 archive_entry_set_gid(entry, folder_gid); 104 archive_entry_set_mtime(entry, now, 0); 105 archive_entry_set_ctime(entry, now + 5, 0); 106 assertEqualIntA(a, 0, archive_write_header(a, entry)); 107 archive_entry_free(entry); 108 } 109 110 /* Quick and dirty: Read 2-byte and 4-byte integers from Zip file. */ 111 static unsigned int 112 i2(const void *p_) 113 { 114 const unsigned char *p = p_; 115 return (p[0] | (p[1] << 8)); 116 } 117 118 static unsigned int 119 i4(const void *p_) 120 { 121 const unsigned char *p = p_; 122 return (i2(p) | (i2(p + 2) << 16)); 123 } 124 125 static void verify_uncompressed_contents(const char *buff, size_t used) 126 { 127 const char *buffend; 128 129 /* Misc variables */ 130 unsigned long crc; 131 struct tm *tm; 132 #if defined(HAVE_LOCALTIME_R) || defined(HAVE__LOCALTIME64_S) 133 struct tm tmbuf; 134 #endif 135 #if defined(HAVE__LOCALTIME64_S) 136 errno_t terr; 137 __time64_t tmptime; 138 #endif 139 /* p is the pointer to walk over the central directory, 140 * q walks over the local headers, the data and the data descriptors. */ 141 const char *p, *q, *local_header, *extra_start; 142 143 #if defined(HAVE_LOCALTIME_R) 144 tm = localtime_r(&now, &tmbuf); 145 #elif defined(HAVE__LOCALTIME64_S) 146 tmptime = now; 147 terr = _localtime64_s(&tmbuf, &tmptime); 148 if (terr) 149 tm = NULL; 150 else 151 tm = &tmbuf; 152 #else 153 tm = localtime(&now); 154 #endif 155 156 /* Remember the end of the archive in memory. */ 157 buffend = buff + used; 158 159 /* Verify "End of Central Directory" record. */ 160 /* Get address of end-of-central-directory record. */ 161 p = buffend - 22; /* Assumes there is no zip comment field. */ 162 failure("End-of-central-directory begins with PK\\005\\006 signature"); 163 assertEqualMem(p, "PK\005\006", 4); 164 failure("This must be disk 0"); 165 assertEqualInt(i2(p + 4), 0); 166 failure("Central dir must start on disk 0"); 167 assertEqualInt(i2(p + 6), 0); 168 failure("All central dir entries are on this disk"); 169 assertEqualInt(i2(p + 8), i2(p + 10)); 170 failure("CD start (%d) + CD length (%d) should == archive size - 22", 171 i4(p + 12), i4(p + 16)); 172 assertEqualInt(i4(p + 12) + i4(p + 16), used - 22); 173 failure("no zip comment"); 174 assertEqualInt(i2(p + 20), 0); 175 176 /* Get address of first entry in central directory. */ 177 p = buff + i4(buffend - 6); 178 failure("Central file record at offset %d should begin with" 179 " PK\\001\\002 signature", 180 i4(buffend - 10)); 181 182 /* Verify file entry in central directory. */ 183 assertEqualMem(p, "PK\001\002", 4); /* Signature */ 184 assertEqualInt(i2(p + 4), 3 * 256 + 10); /* Version made by */ 185 assertEqualInt(i2(p + 6), 10); /* Version needed to extract */ 186 assertEqualInt(i2(p + 8), 8); /* Flags */ 187 assertEqualInt(i2(p + 10), 0); /* Compression method */ 188 assertEqualInt(i2(p + 12), (tm->tm_hour * 2048) + (tm->tm_min * 32) + (tm->tm_sec / 2)); /* File time */ 189 assertEqualInt(i2(p + 14), ((tm->tm_year - 80) * 512) + ((tm->tm_mon + 1) * 32) + tm->tm_mday); /* File date */ 190 crc = bitcrc32(0, file_data1, sizeof(file_data1)); 191 crc = bitcrc32(crc, file_data2, sizeof(file_data2)); 192 assertEqualInt(i4(p + 16), crc); /* CRC-32 */ 193 assertEqualInt(i4(p + 20), sizeof(file_data1) + sizeof(file_data2)); /* Compressed size */ 194 assertEqualInt(i4(p + 24), sizeof(file_data1) + sizeof(file_data2)); /* Uncompressed size */ 195 assertEqualInt(i2(p + 28), strlen(file_name)); /* Pathname length */ 196 assertEqualInt(i2(p + 30), 28); /* Extra field length */ 197 assertEqualInt(i2(p + 32), 0); /* File comment length */ 198 assertEqualInt(i2(p + 34), 0); /* Disk number start */ 199 assertEqualInt(i2(p + 36), 0); /* Internal file attrs */ 200 assertEqualInt(i4(p + 38) >> 16 & 01777, file_perm); /* External file attrs */ 201 assertEqualInt(i4(p + 42), 0); /* Offset of local header */ 202 assertEqualMem(p + 46, file_name, strlen(file_name)); /* Pathname */ 203 p = p + 46 + strlen(file_name); 204 assertEqualInt(i2(p), 0x5455); /* 'UT' extension header */ 205 assertEqualInt(i2(p + 2), 9); /* 'UT' size */ 206 assertEqualInt(p[4], 3); /* 'UT' flags */ 207 assertEqualInt(i4(p + 5), now); /* 'UT' mtime */ 208 assertEqualInt(i4(p + 9), now + 3); /* 'UT' atime */ 209 p = p + 4 + i2(p + 2); 210 assertEqualInt(i2(p), 0x7875); /* 'ux' extension header */ 211 assertEqualInt(i2(p + 2), 11); /* 'ux' size */ 212 /* TODO */ 213 p = p + 4 + i2(p + 2); 214 215 /* Verify local header of file entry. */ 216 local_header = q = buff; 217 assertEqualMem(q, "PK\003\004", 4); /* Signature */ 218 assertEqualInt(i2(q + 4), 10); /* Version needed to extract */ 219 assertEqualInt(i2(q + 6), 8); /* Flags */ 220 assertEqualInt(i2(q + 8), 0); /* Compression method */ 221 assertEqualInt(i2(q + 10), (tm->tm_hour * 2048) + (tm->tm_min * 32) + (tm->tm_sec / 2)); /* File time */ 222 assertEqualInt(i2(q + 12), ((tm->tm_year - 80) * 512) + ((tm->tm_mon + 1) * 32) + tm->tm_mday); /* File date */ 223 assertEqualInt(i4(q + 14), 0); /* CRC-32 */ 224 assertEqualInt(i4(q + 18), sizeof(file_data1) + sizeof(file_data2)); /* Compressed size */ 225 assertEqualInt(i4(q + 22), sizeof(file_data1) + sizeof(file_data2)); /* Uncompressed size */ 226 assertEqualInt(i2(q + 26), strlen(file_name)); /* Pathname length */ 227 assertEqualInt(i2(q + 28), 41); /* Extra field length */ 228 assertEqualMem(q + 30, file_name, strlen(file_name)); /* Pathname */ 229 extra_start = q = q + 30 + strlen(file_name); 230 assertEqualInt(i2(q), 0x5455); /* 'UT' extension header */ 231 assertEqualInt(i2(q + 2), 9); /* 'UT' size */ 232 assertEqualInt(q[4], 3); /* 'UT' flags */ 233 assertEqualInt(i4(q + 5), now); /* 'UT' mtime */ 234 assertEqualInt(i4(q + 9), now + 3); /* 'UT' atime */ 235 q = q + 4 + i2(q + 2); 236 237 assertEqualInt(i2(q), 0x7875); /* 'ux' extension header */ 238 assertEqualInt(i2(q + 2), 11); /* 'ux' size */ 239 assertEqualInt(q[4], 1); /* 'ux' version */ 240 assertEqualInt(q[5], 4); /* 'ux' uid size */ 241 assertEqualInt(i4(q + 6), file_uid); /* 'Ux' UID */ 242 assertEqualInt(q[10], 4); /* 'ux' gid size */ 243 assertEqualInt(i4(q + 11), file_gid); /* 'Ux' GID */ 244 q = q + 4 + i2(q + 2); 245 246 assertEqualInt(i2(q), 0x6c78); /* 'xl' experimental extension header */ 247 assertEqualInt(i2(q + 2), 9); /* size */ 248 assertEqualInt(q[4], 7); /* Bitmap of fields included. */ 249 assertEqualInt(i2(q + 5) >> 8, 3); /* system & version made by */ 250 assertEqualInt(i2(q + 7), 0); /* internal file attributes */ 251 assertEqualInt(i4(q + 9) >> 16 & 01777, file_perm); /* external file attributes */ 252 q = q + 4 + i2(q + 2); 253 254 assert(q == extra_start + i2(local_header + 28)); 255 q = extra_start + i2(local_header + 28); 256 257 /* Verify data of file entry. */ 258 assertEqualMem(q, file_data1, sizeof(file_data1)); 259 assertEqualMem(q + sizeof(file_data1), file_data2, sizeof(file_data2)); 260 q = q + sizeof(file_data1) + sizeof(file_data2); 261 262 /* Verify data descriptor of file entry. */ 263 assertEqualMem(q, "PK\007\010", 4); /* Signature */ 264 assertEqualInt(i4(q + 4), crc); /* CRC-32 */ 265 assertEqualInt(i4(q + 8), sizeof(file_data1) + sizeof(file_data2)); /* Compressed size */ 266 assertEqualInt(i4(q + 12), sizeof(file_data1) + sizeof(file_data2)); /* Uncompressed size */ 267 q = q + 16; 268 269 /* Verify folder entry in central directory. */ 270 assertEqualMem(p, "PK\001\002", 4); /* Signature */ 271 assertEqualInt(i2(p + 4), 3 * 256 + 20); /* Version made by */ 272 assertEqualInt(i2(p + 6), 20); /* Version needed to extract */ 273 assertEqualInt(i2(p + 8), 0); /* Flags */ 274 assertEqualInt(i2(p + 10), 0); /* Compression method */ 275 assertEqualInt(i2(p + 12), (tm->tm_hour * 2048) + (tm->tm_min * 32) + (tm->tm_sec / 2)); /* File time */ 276 assertEqualInt(i2(p + 14), ((tm->tm_year - 80) * 512) + ((tm->tm_mon + 1) * 32) + tm->tm_mday); /* File date */ 277 crc = 0; 278 assertEqualInt(i4(p + 16), crc); /* CRC-32 */ 279 assertEqualInt(i4(p + 20), 0); /* Compressed size */ 280 assertEqualInt(i4(p + 24), 0); /* Uncompressed size */ 281 assertEqualInt(i2(p + 28), strlen(folder_name)); /* Pathname length */ 282 assertEqualInt(i2(p + 30), 28); /* Extra field length */ 283 assertEqualInt(i2(p + 32), 0); /* File comment length */ 284 assertEqualInt(i2(p + 34), 0); /* Disk number start */ 285 assertEqualInt(i2(p + 36), 0); /* Internal file attrs */ 286 assertEqualInt(i4(p + 38) >> 16 & 01777, folder_perm); /* External file attrs */ 287 assertEqualInt(i4(p + 42), q - buff); /* Offset of local header */ 288 assertEqualMem(p + 46, folder_name, strlen(folder_name)); /* Pathname */ 289 p = p + 46 + strlen(folder_name); 290 assertEqualInt(i2(p), 0x5455); /* 'UT' extension header */ 291 assertEqualInt(i2(p + 2), 9); /* 'UT' size */ 292 assertEqualInt(p[4], 5); /* 'UT' flags */ 293 assertEqualInt(i4(p + 5), now); /* 'UT' mtime */ 294 assertEqualInt(i4(p + 9), now + 5); /* 'UT' atime */ 295 p = p + 4 + i2(p + 2); 296 assertEqualInt(i2(p), 0x7875); /* 'ux' extension header */ 297 assertEqualInt(i2(p + 2), 11); /* 'ux' size */ 298 assertEqualInt(p[4], 1); /* 'ux' version */ 299 assertEqualInt(p[5], 4); /* 'ux' uid size */ 300 assertEqualInt(i4(p + 6), folder_uid); /* 'ux' UID */ 301 assertEqualInt(p[10], 4); /* 'ux' gid size */ 302 assertEqualInt(i4(p + 11), folder_gid); /* 'ux' GID */ 303 /*p = p + 4 + i2(p + 2);*/ 304 305 /* Verify local header of folder entry. */ 306 local_header = q; 307 assertEqualMem(q, "PK\003\004", 4); /* Signature */ 308 assertEqualInt(i2(q + 4), 20); /* Version needed to extract */ 309 assertEqualInt(i2(q + 6), 0); /* Flags */ 310 assertEqualInt(i2(q + 8), 0); /* Compression method */ 311 assertEqualInt(i2(q + 10), (tm->tm_hour * 2048) + (tm->tm_min * 32) + (tm->tm_sec / 2)); /* File time */ 312 assertEqualInt(i2(q + 12), ((tm->tm_year - 80) * 512) + ((tm->tm_mon + 1) * 32) + tm->tm_mday); /* File date */ 313 assertEqualInt(i4(q + 14), 0); /* CRC-32 */ 314 assertEqualInt(i4(q + 18), 0); /* Compressed size */ 315 assertEqualInt(i4(q + 22), 0); /* Uncompressed size */ 316 assertEqualInt(i2(q + 26), strlen(folder_name)); /* Pathname length */ 317 assertEqualInt(i2(q + 28), 41); /* Extra field length */ 318 assertEqualMem(q + 30, folder_name, strlen(folder_name)); /* Pathname */ 319 extra_start = q = q + 30 + strlen(folder_name); 320 assertEqualInt(i2(q), 0x5455); /* 'UT' extension header */ 321 assertEqualInt(i2(q + 2), 9); /* 'UT' size */ 322 assertEqualInt(q[4], 5); /* 'UT' flags */ 323 assertEqualInt(i4(q + 5), now); /* 'UT' mtime */ 324 assertEqualInt(i4(q + 9), now + 5); /* 'UT' atime */ 325 q = q + 4 + i2(q + 2); 326 assertEqualInt(i2(q), 0x7875); /* 'ux' extension header */ 327 assertEqualInt(i2(q + 2), 11); /* 'ux' size */ 328 assertEqualInt(q[4], 1); /* 'ux' version */ 329 assertEqualInt(q[5], 4); /* 'ux' uid size */ 330 assertEqualInt(i4(q + 6), folder_uid); /* 'ux' UID */ 331 assertEqualInt(q[10], 4); /* 'ux' gid size */ 332 assertEqualInt(i4(q + 11), folder_gid); /* 'ux' GID */ 333 q = q + 4 + i2(q + 2); 334 335 assertEqualInt(i2(q), 0x6c78); /* 'xl' experimental extension header */ 336 assertEqualInt(i2(q + 2), 9); /* size */ 337 assertEqualInt(q[4], 7); /* bitmap of fields */ 338 assertEqualInt(i2(q + 5) >> 8, 3); /* system & version made by */ 339 assertEqualInt(i2(q + 7), 0); /* internal file attributes */ 340 assertEqualInt(i4(q + 9) >> 16 & 01777, folder_perm); /* external file attributes */ 341 q = q + 4 + i2(q + 2); 342 343 assert(q == extra_start + i2(local_header + 28)); 344 q = extra_start + i2(local_header + 28); 345 346 /* There should not be any data in the folder entry, 347 * so the first central directory entry should be next: */ 348 assertEqualMem(q, "PK\001\002", 4); /* Signature */ 349 } 350 351 DEFINE_TEST(test_write_format_zip_compression_store) 352 { 353 /* Buffer data */ 354 struct archive *a; 355 char buff[100000]; 356 size_t used; 357 358 /* Time data */ 359 now = time(NULL); 360 361 /* Create new ZIP archive in memory without padding. */ 362 /* Use compression=store to disable compression. */ 363 assert((a = archive_write_new()) != NULL); 364 assertEqualIntA(a, ARCHIVE_OK, archive_write_set_format_zip(a)); 365 assertEqualIntA(a, ARCHIVE_OK, 366 archive_write_set_options(a, "zip:compression=store")); 367 assertEqualIntA(a, ARCHIVE_OK, 368 archive_write_set_options(a, "zip:experimental")); 369 assertEqualIntA(a, ARCHIVE_OK, archive_write_add_filter_none(a)); 370 assertEqualIntA(a, ARCHIVE_OK, archive_write_set_bytes_per_block(a, 1)); 371 assertEqualIntA(a, ARCHIVE_OK, archive_write_set_bytes_in_last_block(a, 1)); 372 assertEqualIntA(a, ARCHIVE_OK, archive_write_open_memory(a, buff, sizeof(buff), &used)); 373 374 verify_write_uncompressed(a); 375 376 /* Close the archive . */ 377 assertEqualIntA(a, ARCHIVE_OK, archive_write_close(a)); 378 assertEqualInt(ARCHIVE_OK, archive_write_free(a)); 379 dumpfile("constructed.zip", buff, used); 380 381 verify_uncompressed_contents(buff, used); 382 383 /* Create new ZIP archive in memory without padding. */ 384 /* Use compression-level=0 to disable compression. */ 385 assert((a = archive_write_new()) != NULL); 386 assertEqualIntA(a, ARCHIVE_OK, archive_write_set_format_zip(a)); 387 assertEqualIntA(a, ARCHIVE_OK, 388 archive_write_set_options(a, "zip:compression-level=0")); 389 assertEqualIntA(a, ARCHIVE_OK, 390 archive_write_set_options(a, "zip:experimental")); 391 assertEqualIntA(a, ARCHIVE_OK, archive_write_add_filter_none(a)); 392 assertEqualIntA(a, ARCHIVE_OK, archive_write_set_bytes_per_block(a, 1)); 393 assertEqualIntA(a, ARCHIVE_OK, archive_write_set_bytes_in_last_block(a, 1)); 394 assertEqualIntA(a, ARCHIVE_OK, archive_write_open_memory(a, buff, sizeof(buff), &used)); 395 396 verify_write_uncompressed(a); 397 398 /* Close the archive . */ 399 assertEqualIntA(a, ARCHIVE_OK, archive_write_close(a)); 400 assertEqualInt(ARCHIVE_OK, archive_write_free(a)); 401 dumpfile("constructed.zip", buff, used); 402 403 verify_uncompressed_contents(buff, used); 404 405 } 406