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 * Detailed byte-for-byte verification of the format of a zip archive 35 * with a single file written to it. 36 */ 37 38 static unsigned long 39 bitcrc32(unsigned long c, void *_p, size_t s) 40 { 41 /* This is a drop-in replacement for crc32() from zlib. 42 * Libarchive should be able to correctly generate 43 * uncompressed zip archives (including correct CRCs) even 44 * when zlib is unavailable, and this function helps us verify 45 * that. Yes, this is very, very slow and unsuitable for 46 * production use, but it's correct, compact, and works well 47 * enough for this particular usage. Libarchive internally 48 * uses a much more efficient implementation. */ 49 const unsigned char *p = _p; 50 int bitctr; 51 52 if (p == NULL) 53 return (0); 54 55 for (; s > 0; --s) { 56 c ^= *p++; 57 for (bitctr = 8; bitctr > 0; --bitctr) { 58 if (c & 1) c = (c >> 1); 59 else c = (c >> 1) ^ 0xedb88320; 60 c ^= 0x80000000; 61 } 62 } 63 return (c); 64 } 65 66 /* Quick and dirty: Read 2-byte and 4-byte integers from Zip file. */ 67 static unsigned i2(const unsigned char *p) { return ((p[0] & 0xff) | ((p[1] & 0xff) << 8)); } 68 static unsigned i4(const unsigned char *p) { return (i2(p) | (i2(p + 2) << 16)); } 69 70 DEFINE_TEST(test_write_format_zip_file) 71 { 72 struct archive *a; 73 struct archive_entry *ae; 74 time_t t = 1234567890; 75 struct tm *tm; 76 #if defined(HAVE_LOCALTIME_R) || defined(HAVE_LOCALTIME_S) 77 struct tm tmbuf; 78 #endif 79 size_t used, buffsize = 1000000; 80 unsigned long crc; 81 int file_perm = 00644; 82 int zip_version = 20; 83 int zip_compression = 8; 84 short file_uid = 10, file_gid = 20; 85 unsigned char *buff, *buffend, *p; 86 unsigned char *central_header, *local_header, *eocd, *eocd_record; 87 unsigned char *extension_start, *extension_end; 88 char file_data[] = {'1', '2', '3', '4', '5', '6', '7', '8'}; 89 const char *file_name = "file"; 90 91 #ifndef HAVE_ZLIB_H 92 zip_version = 10; 93 zip_compression = 0; 94 #endif 95 96 #if defined(HAVE_LOCALTIME_S) 97 tm = localtime_s(&tmbuf, &t) ? NULL : &tmbuf; 98 #elif defined(HAVE_LOCALTIME_R) 99 tm = localtime_r(&t, &tmbuf); 100 #else 101 tm = localtime(&t); 102 #endif 103 buff = malloc(buffsize); 104 105 /* Create a new archive in memory. */ 106 assert((a = archive_write_new()) != NULL); 107 assertEqualIntA(a, ARCHIVE_OK, archive_write_set_format_zip(a)); 108 assertEqualIntA(a, ARCHIVE_OK, 109 archive_write_set_options(a, "zip:experimental")); 110 assertEqualIntA(a, ARCHIVE_OK, 111 archive_write_open_memory(a, buff, buffsize, &used)); 112 113 assert((ae = archive_entry_new()) != NULL); 114 archive_entry_copy_pathname(ae, file_name); 115 archive_entry_set_mode(ae, AE_IFREG | file_perm); 116 archive_entry_set_size(ae, sizeof(file_data)); 117 archive_entry_set_uid(ae, file_uid); 118 archive_entry_set_gid(ae, file_gid); 119 archive_entry_set_mtime(ae, t, 0); 120 assertEqualInt(0, archive_write_header(a, ae)); 121 archive_entry_free(ae); 122 assertEqualInt(8, archive_write_data(a, file_data, sizeof(file_data))); 123 assertEqualIntA(a, ARCHIVE_OK, archive_write_close(a)); 124 assertEqualInt(ARCHIVE_OK, archive_write_free(a)); 125 buffend = buff + used; 126 dumpfile("constructed.zip", buff, used); 127 128 /* Verify "End of Central Directory" record. */ 129 /* Get address of end-of-central-directory record. */ 130 eocd_record = p = buffend - 22; /* Assumes there is no zip comment field. */ 131 failure("End-of-central-directory begins with PK\\005\\006 signature"); 132 assertEqualMem(p, "PK\005\006", 4); 133 failure("This must be disk 0"); 134 assertEqualInt(i2(p + 4), 0); 135 failure("Central dir must start on disk 0"); 136 assertEqualInt(i2(p + 6), 0); 137 failure("All central dir entries are on this disk"); 138 assertEqualInt(i2(p + 8), i2(p + 10)); 139 eocd = buff + i4(p + 12) + i4(p + 16); 140 failure("no zip comment"); 141 assertEqualInt(i2(p + 20), 0); 142 143 /* Get address of first entry in central directory. */ 144 central_header = p = buff + i4(buffend - 6); 145 failure("Central file record at offset %d should begin with" 146 " PK\\001\\002 signature", 147 i4(buffend - 10)); 148 149 /* Verify file entry in central directory. */ 150 assertEqualMem(p, "PK\001\002", 4); /* Signature */ 151 assertEqualInt(i2(p + 4), 3 * 256 + zip_version); /* Version made by */ 152 assertEqualInt(i2(p + 6), zip_version); /* Version needed to extract */ 153 assertEqualInt(i2(p + 8), 8); /* Flags */ 154 assertEqualInt(i2(p + 10), zip_compression); /* Compression method */ 155 assertEqualInt(i2(p + 12), (tm->tm_hour * 2048) + (tm->tm_min * 32) + (tm->tm_sec / 2)); /* File time */ 156 assertEqualInt(i2(p + 14), ((tm->tm_year - 80) * 512) + ((tm->tm_mon + 1) * 32) + tm->tm_mday); /* File date */ 157 crc = bitcrc32(0, file_data, sizeof(file_data)); 158 assertEqualInt(i4(p + 16), crc); /* CRC-32 */ 159 /* assertEqualInt(i4(p + 20), sizeof(file_data)); */ /* Compressed size */ 160 assertEqualInt(i4(p + 24), sizeof(file_data)); /* Uncompressed size */ 161 assertEqualInt(i2(p + 28), strlen(file_name)); /* Pathname length */ 162 /* assertEqualInt(i2(p + 30), 28); */ /* Extra field length: See below */ 163 assertEqualInt(i2(p + 32), 0); /* File comment length */ 164 assertEqualInt(i2(p + 34), 0); /* Disk number start */ 165 assertEqualInt(i2(p + 36), 0); /* Internal file attrs */ 166 assertEqualInt(i4(p + 38) >> 16 & 01777, file_perm); /* External file attrs */ 167 assertEqualInt(i4(p + 42), 0); /* Offset of local header */ 168 assertEqualMem(p + 46, file_name, strlen(file_name)); /* Pathname */ 169 p = extension_start = central_header + 46 + strlen(file_name); 170 extension_end = extension_start + i2(central_header + 30); 171 172 assertEqualInt(i2(p), 0x7875); /* 'ux' extension header */ 173 assertEqualInt(i2(p + 2), 11); /* 'ux' size */ 174 /* TODO: verify 'ux' contents */ 175 p += 4 + i2(p + 2); 176 177 assertEqualInt(i2(p), 0x5455); /* 'UT' extension header */ 178 assertEqualInt(i2(p + 2), 5); /* 'UT' size */ 179 assertEqualInt(p[4], 1); /* 'UT' flags */ 180 assertEqualInt(i4(p + 5), t); /* 'UT' mtime */ 181 p += 4 + i2(p + 2); 182 183 /* Just in case: Report any extra extensions. */ 184 while (p < extension_end) { 185 failure("Unexpected extension 0x%04X", i2(p)); 186 assert(0); 187 p += 4 + i2(p + 2); 188 } 189 190 /* Should have run exactly to end of extra data. */ 191 assertEqualAddress(p, extension_end); 192 193 assertEqualAddress(p, eocd); 194 195 /* Regular EOCD immediately follows central directory. */ 196 assertEqualAddress(p, eocd_record); 197 198 /* Verify local header of file entry. */ 199 p = local_header = buff; 200 assertEqualMem(p, "PK\003\004", 4); /* Signature */ 201 assertEqualInt(i2(p + 4), zip_version); /* Version needed to extract */ 202 assertEqualInt(i2(p + 6), 8); /* Flags: bit 3 = length-at-end */ 203 assertEqualInt(i2(p + 8), zip_compression); /* Compression method */ 204 assertEqualInt(i2(p + 10), (tm->tm_hour * 2048) + (tm->tm_min * 32) + (tm->tm_sec / 2)); /* File time */ 205 assertEqualInt(i2(p + 12), ((tm->tm_year - 80) * 512) + ((tm->tm_mon + 1) * 32) + tm->tm_mday); /* File date */ 206 assertEqualInt(i4(p + 14), 0); /* CRC-32 stored as zero because we're using length-at-end */ 207 assertEqualInt(i4(p + 18), 0); /* Compressed size stored as zero because we're using length-at-end. */ 208 assertEqualInt(i4(p + 22), 0); /* Uncompressed size stored as zero because we're using length-at-end. */ 209 assertEqualInt(i2(p + 26), strlen(file_name)); /* Pathname length */ 210 assertEqualInt(i2(p + 28), 37); /* Extra field length */ 211 assertEqualMem(p + 30, file_name, strlen(file_name)); /* Pathname */ 212 p = extension_start = local_header + 30 + strlen(file_name); 213 extension_end = extension_start + i2(local_header + 28); 214 215 assertEqualInt(i2(p), 0x7875); /* 'ux' extension header */ 216 assertEqualInt(i2(p + 2), 11); /* size */ 217 assertEqualInt(p[4], 1); /* 'ux' version */ 218 assertEqualInt(p[5], 4); /* 'ux' uid size */ 219 assertEqualInt(i4(p + 6), file_uid); /* 'Ux' UID */ 220 assertEqualInt(p[10], 4); /* 'ux' gid size */ 221 assertEqualInt(i4(p + 11), file_gid); /* 'Ux' GID */ 222 p += 4 + i2(p + 2); 223 224 assertEqualInt(i2(p), 0x5455); /* 'UT' extension header */ 225 assertEqualInt(i2(p + 2), 5); /* size */ 226 assertEqualInt(p[4], 1); /* 'UT' flags */ 227 assertEqualInt(i4(p + 5), t); /* 'UT' mtime */ 228 p += 4 + i2(p + 2); 229 230 assertEqualInt(i2(p), 0x6c78); /* 'xl' experimental extension block */ 231 assertEqualInt(i2(p + 2), 9); /* size */ 232 assertEqualInt(p[4], 7); /* bitmap of fields in this block */ 233 assertEqualInt(i2(p + 5) >> 8, 3); /* System & version made by */ 234 assertEqualInt(i2(p + 7), 0); /* internal file attributes */ 235 assertEqualInt(i4(p + 9) >> 16 & 01777, file_perm); /* external file attributes */ 236 p += 4 + i2(p + 2); 237 238 /* Just in case: Report any extra extensions. */ 239 while (p < extension_end) { 240 failure("Unexpected extension 0x%04X", i2(p)); 241 assert(0); 242 p += 4 + i2(p + 2); 243 } 244 245 /* Should have run exactly to end of extra data. */ 246 assertEqualAddress(p, extension_end); 247 248 /* Data descriptor should follow compressed data. */ 249 while (p < central_header && memcmp(p, "PK\007\010", 4) != 0) 250 ++p; 251 assertEqualMem(p, "PK\007\010", 4); 252 assertEqualInt(i4(p + 4), crc); /* CRC-32 */ 253 assertEqualInt(i4(p + 8), p - extension_end); /* compressed size */ 254 assertEqualInt(i4(p + 12), sizeof(file_data)); /* uncompressed size */ 255 256 /* Central directory should immediately follow the only entry. */ 257 assertEqualAddress(p + 16, central_header); 258 259 free(buff); 260 } 261