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