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_LOCALTIME_S) 133 struct tm tmbuf; 134 #endif 135 /* p is the pointer to walk over the central directory, 136 * q walks over the local headers, the data and the data descriptors. */ 137 const char *p, *q, *local_header, *extra_start; 138 139 #if defined(HAVE_LOCALTIME_S) 140 tm = localtime_s(&tmbuf, &now) ? NULL : &tmbuf; 141 #elif defined(HAVE_LOCALTIME_R) 142 tm = localtime_r(&now, &tmbuf); 143 #else 144 tm = localtime(&now); 145 #endif 146 147 /* Remember the end of the archive in memory. */ 148 buffend = buff + used; 149 150 /* Verify "End of Central Directory" record. */ 151 /* Get address of end-of-central-directory record. */ 152 p = buffend - 22; /* Assumes there is no zip comment field. */ 153 failure("End-of-central-directory begins with PK\\005\\006 signature"); 154 assertEqualMem(p, "PK\005\006", 4); 155 failure("This must be disk 0"); 156 assertEqualInt(i2(p + 4), 0); 157 failure("Central dir must start on disk 0"); 158 assertEqualInt(i2(p + 6), 0); 159 failure("All central dir entries are on this disk"); 160 assertEqualInt(i2(p + 8), i2(p + 10)); 161 failure("CD start (%d) + CD length (%d) should == archive size - 22", 162 i4(p + 12), i4(p + 16)); 163 assertEqualInt(i4(p + 12) + i4(p + 16), used - 22); 164 failure("no zip comment"); 165 assertEqualInt(i2(p + 20), 0); 166 167 /* Get address of first entry in central directory. */ 168 p = buff + i4(buffend - 6); 169 failure("Central file record at offset %d should begin with" 170 " PK\\001\\002 signature", 171 i4(buffend - 10)); 172 173 /* Verify file entry in central directory. */ 174 assertEqualMem(p, "PK\001\002", 4); /* Signature */ 175 assertEqualInt(i2(p + 4), 3 * 256 + 10); /* Version made by */ 176 assertEqualInt(i2(p + 6), 10); /* Version needed to extract */ 177 assertEqualInt(i2(p + 8), 8); /* Flags */ 178 assertEqualInt(i2(p + 10), 0); /* Compression method */ 179 assertEqualInt(i2(p + 12), (tm->tm_hour * 2048) + (tm->tm_min * 32) + (tm->tm_sec / 2)); /* File time */ 180 assertEqualInt(i2(p + 14), ((tm->tm_year - 80) * 512) + ((tm->tm_mon + 1) * 32) + tm->tm_mday); /* File date */ 181 crc = bitcrc32(0, file_data1, sizeof(file_data1)); 182 crc = bitcrc32(crc, file_data2, sizeof(file_data2)); 183 assertEqualInt(i4(p + 16), crc); /* CRC-32 */ 184 assertEqualInt(i4(p + 20), sizeof(file_data1) + sizeof(file_data2)); /* Compressed size */ 185 assertEqualInt(i4(p + 24), sizeof(file_data1) + sizeof(file_data2)); /* Uncompressed size */ 186 assertEqualInt(i2(p + 28), strlen(file_name)); /* Pathname length */ 187 assertEqualInt(i2(p + 30), 28); /* Extra field length */ 188 assertEqualInt(i2(p + 32), 0); /* File comment length */ 189 assertEqualInt(i2(p + 34), 0); /* Disk number start */ 190 assertEqualInt(i2(p + 36), 0); /* Internal file attrs */ 191 assertEqualInt(i4(p + 38) >> 16 & 01777, file_perm); /* External file attrs */ 192 assertEqualInt(i4(p + 42), 0); /* Offset of local header */ 193 assertEqualMem(p + 46, file_name, strlen(file_name)); /* Pathname */ 194 p = p + 46 + strlen(file_name); 195 assertEqualInt(i2(p), 0x5455); /* 'UT' extension header */ 196 assertEqualInt(i2(p + 2), 9); /* 'UT' size */ 197 assertEqualInt(p[4], 3); /* 'UT' flags */ 198 assertEqualInt(i4(p + 5), now); /* 'UT' mtime */ 199 assertEqualInt(i4(p + 9), now + 3); /* 'UT' atime */ 200 p = p + 4 + i2(p + 2); 201 assertEqualInt(i2(p), 0x7875); /* 'ux' extension header */ 202 assertEqualInt(i2(p + 2), 11); /* 'ux' size */ 203 /* TODO */ 204 p = p + 4 + i2(p + 2); 205 206 /* Verify local header of file entry. */ 207 local_header = q = buff; 208 assertEqualMem(q, "PK\003\004", 4); /* Signature */ 209 assertEqualInt(i2(q + 4), 10); /* Version needed to extract */ 210 assertEqualInt(i2(q + 6), 8); /* Flags */ 211 assertEqualInt(i2(q + 8), 0); /* Compression method */ 212 assertEqualInt(i2(q + 10), (tm->tm_hour * 2048) + (tm->tm_min * 32) + (tm->tm_sec / 2)); /* File time */ 213 assertEqualInt(i2(q + 12), ((tm->tm_year - 80) * 512) + ((tm->tm_mon + 1) * 32) + tm->tm_mday); /* File date */ 214 assertEqualInt(i4(q + 14), 0); /* CRC-32 */ 215 assertEqualInt(i4(q + 18), sizeof(file_data1) + sizeof(file_data2)); /* Compressed size */ 216 assertEqualInt(i4(q + 22), sizeof(file_data1) + sizeof(file_data2)); /* Uncompressed size */ 217 assertEqualInt(i2(q + 26), strlen(file_name)); /* Pathname length */ 218 assertEqualInt(i2(q + 28), 41); /* Extra field length */ 219 assertEqualMem(q + 30, file_name, strlen(file_name)); /* Pathname */ 220 extra_start = q = q + 30 + strlen(file_name); 221 assertEqualInt(i2(q), 0x5455); /* 'UT' extension header */ 222 assertEqualInt(i2(q + 2), 9); /* 'UT' size */ 223 assertEqualInt(q[4], 3); /* 'UT' flags */ 224 assertEqualInt(i4(q + 5), now); /* 'UT' mtime */ 225 assertEqualInt(i4(q + 9), now + 3); /* 'UT' atime */ 226 q = q + 4 + i2(q + 2); 227 228 assertEqualInt(i2(q), 0x7875); /* 'ux' extension header */ 229 assertEqualInt(i2(q + 2), 11); /* 'ux' size */ 230 assertEqualInt(q[4], 1); /* 'ux' version */ 231 assertEqualInt(q[5], 4); /* 'ux' uid size */ 232 assertEqualInt(i4(q + 6), file_uid); /* 'Ux' UID */ 233 assertEqualInt(q[10], 4); /* 'ux' gid size */ 234 assertEqualInt(i4(q + 11), file_gid); /* 'Ux' GID */ 235 q = q + 4 + i2(q + 2); 236 237 assertEqualInt(i2(q), 0x6c78); /* 'xl' experimental extension header */ 238 assertEqualInt(i2(q + 2), 9); /* size */ 239 assertEqualInt(q[4], 7); /* Bitmap of fields included. */ 240 assertEqualInt(i2(q + 5) >> 8, 3); /* system & version made by */ 241 assertEqualInt(i2(q + 7), 0); /* internal file attributes */ 242 assertEqualInt(i4(q + 9) >> 16 & 01777, file_perm); /* external file attributes */ 243 q = q + 4 + i2(q + 2); 244 245 assert(q == extra_start + i2(local_header + 28)); 246 q = extra_start + i2(local_header + 28); 247 248 /* Verify data of file entry. */ 249 assertEqualMem(q, file_data1, sizeof(file_data1)); 250 assertEqualMem(q + sizeof(file_data1), file_data2, sizeof(file_data2)); 251 q = q + sizeof(file_data1) + sizeof(file_data2); 252 253 /* Verify data descriptor of file entry. */ 254 assertEqualMem(q, "PK\007\010", 4); /* Signature */ 255 assertEqualInt(i4(q + 4), crc); /* CRC-32 */ 256 assertEqualInt(i4(q + 8), sizeof(file_data1) + sizeof(file_data2)); /* Compressed size */ 257 assertEqualInt(i4(q + 12), sizeof(file_data1) + sizeof(file_data2)); /* Uncompressed size */ 258 q = q + 16; 259 260 /* Verify folder entry in central directory. */ 261 assertEqualMem(p, "PK\001\002", 4); /* Signature */ 262 assertEqualInt(i2(p + 4), 3 * 256 + 20); /* Version made by */ 263 assertEqualInt(i2(p + 6), 20); /* Version needed to extract */ 264 assertEqualInt(i2(p + 8), 0); /* Flags */ 265 assertEqualInt(i2(p + 10), 0); /* Compression method */ 266 assertEqualInt(i2(p + 12), (tm->tm_hour * 2048) + (tm->tm_min * 32) + (tm->tm_sec / 2)); /* File time */ 267 assertEqualInt(i2(p + 14), ((tm->tm_year - 80) * 512) + ((tm->tm_mon + 1) * 32) + tm->tm_mday); /* File date */ 268 crc = 0; 269 assertEqualInt(i4(p + 16), crc); /* CRC-32 */ 270 assertEqualInt(i4(p + 20), 0); /* Compressed size */ 271 assertEqualInt(i4(p + 24), 0); /* Uncompressed size */ 272 assertEqualInt(i2(p + 28), strlen(folder_name)); /* Pathname length */ 273 assertEqualInt(i2(p + 30), 28); /* Extra field length */ 274 assertEqualInt(i2(p + 32), 0); /* File comment length */ 275 assertEqualInt(i2(p + 34), 0); /* Disk number start */ 276 assertEqualInt(i2(p + 36), 0); /* Internal file attrs */ 277 assertEqualInt(i4(p + 38) >> 16 & 01777, folder_perm); /* External file attrs */ 278 assertEqualInt(i4(p + 42), q - buff); /* Offset of local header */ 279 assertEqualMem(p + 46, folder_name, strlen(folder_name)); /* Pathname */ 280 p = p + 46 + strlen(folder_name); 281 assertEqualInt(i2(p), 0x5455); /* 'UT' extension header */ 282 assertEqualInt(i2(p + 2), 9); /* 'UT' size */ 283 assertEqualInt(p[4], 5); /* 'UT' flags */ 284 assertEqualInt(i4(p + 5), now); /* 'UT' mtime */ 285 assertEqualInt(i4(p + 9), now + 5); /* 'UT' atime */ 286 p = p + 4 + i2(p + 2); 287 assertEqualInt(i2(p), 0x7875); /* 'ux' extension header */ 288 assertEqualInt(i2(p + 2), 11); /* 'ux' size */ 289 assertEqualInt(p[4], 1); /* 'ux' version */ 290 assertEqualInt(p[5], 4); /* 'ux' uid size */ 291 assertEqualInt(i4(p + 6), folder_uid); /* 'ux' UID */ 292 assertEqualInt(p[10], 4); /* 'ux' gid size */ 293 assertEqualInt(i4(p + 11), folder_gid); /* 'ux' GID */ 294 /*p = p + 4 + i2(p + 2);*/ 295 296 /* Verify local header of folder entry. */ 297 local_header = q; 298 assertEqualMem(q, "PK\003\004", 4); /* Signature */ 299 assertEqualInt(i2(q + 4), 20); /* Version needed to extract */ 300 assertEqualInt(i2(q + 6), 0); /* Flags */ 301 assertEqualInt(i2(q + 8), 0); /* Compression method */ 302 assertEqualInt(i2(q + 10), (tm->tm_hour * 2048) + (tm->tm_min * 32) + (tm->tm_sec / 2)); /* File time */ 303 assertEqualInt(i2(q + 12), ((tm->tm_year - 80) * 512) + ((tm->tm_mon + 1) * 32) + tm->tm_mday); /* File date */ 304 assertEqualInt(i4(q + 14), 0); /* CRC-32 */ 305 assertEqualInt(i4(q + 18), 0); /* Compressed size */ 306 assertEqualInt(i4(q + 22), 0); /* Uncompressed size */ 307 assertEqualInt(i2(q + 26), strlen(folder_name)); /* Pathname length */ 308 assertEqualInt(i2(q + 28), 41); /* Extra field length */ 309 assertEqualMem(q + 30, folder_name, strlen(folder_name)); /* Pathname */ 310 extra_start = q = q + 30 + strlen(folder_name); 311 assertEqualInt(i2(q), 0x5455); /* 'UT' extension header */ 312 assertEqualInt(i2(q + 2), 9); /* 'UT' size */ 313 assertEqualInt(q[4], 5); /* 'UT' flags */ 314 assertEqualInt(i4(q + 5), now); /* 'UT' mtime */ 315 assertEqualInt(i4(q + 9), now + 5); /* 'UT' atime */ 316 q = q + 4 + i2(q + 2); 317 assertEqualInt(i2(q), 0x7875); /* 'ux' extension header */ 318 assertEqualInt(i2(q + 2), 11); /* 'ux' size */ 319 assertEqualInt(q[4], 1); /* 'ux' version */ 320 assertEqualInt(q[5], 4); /* 'ux' uid size */ 321 assertEqualInt(i4(q + 6), folder_uid); /* 'ux' UID */ 322 assertEqualInt(q[10], 4); /* 'ux' gid size */ 323 assertEqualInt(i4(q + 11), folder_gid); /* 'ux' GID */ 324 q = q + 4 + i2(q + 2); 325 326 assertEqualInt(i2(q), 0x6c78); /* 'xl' experimental extension header */ 327 assertEqualInt(i2(q + 2), 9); /* size */ 328 assertEqualInt(q[4], 7); /* bitmap of fields */ 329 assertEqualInt(i2(q + 5) >> 8, 3); /* system & version made by */ 330 assertEqualInt(i2(q + 7), 0); /* internal file attributes */ 331 assertEqualInt(i4(q + 9) >> 16 & 01777, folder_perm); /* external file attributes */ 332 q = q + 4 + i2(q + 2); 333 334 assert(q == extra_start + i2(local_header + 28)); 335 q = extra_start + i2(local_header + 28); 336 337 /* There should not be any data in the folder entry, 338 * so the first central directory entry should be next: */ 339 assertEqualMem(q, "PK\001\002", 4); /* Signature */ 340 } 341 342 DEFINE_TEST(test_write_format_zip_compression_store) 343 { 344 /* Buffer data */ 345 struct archive *a; 346 char buff[100000]; 347 size_t used; 348 349 /* Time data */ 350 now = time(NULL); 351 352 /* Create new ZIP archive in memory without padding. */ 353 /* Use compression=store to disable compression. */ 354 assert((a = archive_write_new()) != NULL); 355 assertEqualIntA(a, ARCHIVE_OK, archive_write_set_format_zip(a)); 356 assertEqualIntA(a, ARCHIVE_OK, 357 archive_write_set_options(a, "zip:compression=store")); 358 assertEqualIntA(a, ARCHIVE_OK, 359 archive_write_set_options(a, "zip:experimental")); 360 assertEqualIntA(a, ARCHIVE_OK, archive_write_add_filter_none(a)); 361 assertEqualIntA(a, ARCHIVE_OK, archive_write_set_bytes_per_block(a, 1)); 362 assertEqualIntA(a, ARCHIVE_OK, archive_write_set_bytes_in_last_block(a, 1)); 363 assertEqualIntA(a, ARCHIVE_OK, archive_write_open_memory(a, buff, sizeof(buff), &used)); 364 365 verify_write_uncompressed(a); 366 367 /* Close the archive . */ 368 assertEqualIntA(a, ARCHIVE_OK, archive_write_close(a)); 369 assertEqualInt(ARCHIVE_OK, archive_write_free(a)); 370 dumpfile("constructed.zip", buff, used); 371 372 verify_uncompressed_contents(buff, used); 373 374 /* Create new ZIP archive in memory without padding. */ 375 /* Use compression-level=0 to disable compression. */ 376 assert((a = archive_write_new()) != NULL); 377 assertEqualIntA(a, ARCHIVE_OK, archive_write_set_format_zip(a)); 378 assertEqualIntA(a, ARCHIVE_OK, 379 archive_write_set_options(a, "zip:compression-level=0")); 380 assertEqualIntA(a, ARCHIVE_OK, 381 archive_write_set_options(a, "zip:experimental")); 382 assertEqualIntA(a, ARCHIVE_OK, archive_write_add_filter_none(a)); 383 assertEqualIntA(a, ARCHIVE_OK, archive_write_set_bytes_per_block(a, 1)); 384 assertEqualIntA(a, ARCHIVE_OK, archive_write_set_bytes_in_last_block(a, 1)); 385 assertEqualIntA(a, ARCHIVE_OK, archive_write_open_memory(a, buff, sizeof(buff), &used)); 386 387 verify_write_uncompressed(a); 388 389 /* Close the archive . */ 390 assertEqualIntA(a, ARCHIVE_OK, archive_write_close(a)); 391 assertEqualInt(ARCHIVE_OK, archive_write_free(a)); 392 dumpfile("constructed.zip", buff, used); 393 394 verify_uncompressed_contents(buff, used); 395 396 } 397