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