2 * Copyright (c) 2008 Anselm Strauss
5 * Redistribution and use in source and binary forms, with or without
6 * modification, are permitted provided that the following conditions
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.
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.
27 * Development supported by Google Summer of Code 2008.
31 __FBSDID("$FreeBSD$");
34 bitcrc32(unsigned long c, void *_p, size_t s)
36 /* This is a drop-in replacement for crc32() from zlib.
37 * Libarchive should be able to correctly generate
38 * uncompressed zip archives (including correct CRCs) even
39 * when zlib is unavailable, and this function helps us verify
40 * that. Yes, this is very, very slow and unsuitable for
41 * production use, but it's correct, compact, and works well
42 * enough for this particular usage. Libarchive internally
43 * uses a much more efficient implementation. */
44 const unsigned char *p = _p;
52 for (bitctr = 8; bitctr > 0; --bitctr) {
53 if (c & 1) c = (c >> 1);
54 else c = (c >> 1) ^ 0xedb88320;
61 /* Quick and dirty: Read 2-byte and 4-byte integers from Zip file. */
62 static int i2(const char *p) { return ((p[0] & 0xff) | ((p[1] & 0xff) << 8)); }
63 static int i4(const char *p) { return (i2(p) | (i2(p + 2) << 16)); }
65 DEFINE_TEST(test_write_format_zip_no_compression)
69 struct archive_entry *entry;
72 /* p is the pointer to walk over the central directory,
73 * q walks over the local headers, the data and the data descriptors. */
78 char file_name[] = "file";
79 char file_data1[] = {'1', '2', '3', '4', '5'};
80 char file_data2[] = {'6', '7', '8', '9', '0'};
81 int file_perm = 00644;
86 char folder_name[] = "folder/";
87 int folder_perm = 00755;
88 short folder_uid = 30;
89 short folder_gid = 40;
92 time_t t = time(NULL);
93 struct tm *tm = localtime(&t);
98 /* Create new ZIP archive in memory without padding. */
99 assert((a = archive_write_new()) != NULL);
100 assertA(0 == archive_write_set_format_zip(a));
101 assertA(0 == archive_write_set_format_options(a, "zip:compression=store"));
102 assertA(0 == archive_write_set_compression_none(a));
103 assertA(0 == archive_write_set_bytes_per_block(a, 1));
104 assertA(0 == archive_write_set_bytes_in_last_block(a, 1));
105 assertA(0 == archive_write_open_memory(a, buff, sizeof(buff), &used));
110 assert((entry = archive_entry_new()) != NULL);
111 archive_entry_set_pathname(entry, file_name);
112 archive_entry_set_mode(entry, S_IFREG | 0644);
113 archive_entry_set_size(entry, sizeof(file_data1) + sizeof(file_data2));
114 archive_entry_set_uid(entry, file_uid);
115 archive_entry_set_gid(entry, file_gid);
116 archive_entry_set_mtime(entry, t, 0);
117 archive_entry_set_atime(entry, t, 0);
118 archive_entry_set_ctime(entry, t, 0);
119 assertEqualIntA(a, 0, archive_write_header(a, entry));
120 assertEqualIntA(a, sizeof(file_data1), archive_write_data(a, file_data1, sizeof(file_data1)));
121 assertEqualIntA(a, sizeof(file_data2), archive_write_data(a, file_data2, sizeof(file_data2)));
122 archive_entry_free(entry);
125 assert((entry = archive_entry_new()) != NULL);
126 archive_entry_set_pathname(entry, folder_name);
127 archive_entry_set_mode(entry, S_IFDIR | folder_perm);
128 archive_entry_set_size(entry, 0);
129 archive_entry_set_uid(entry, folder_uid);
130 archive_entry_set_gid(entry, folder_gid);
131 archive_entry_set_mtime(entry, t, 0);
132 archive_entry_set_atime(entry, t, 0);
133 archive_entry_set_ctime(entry, t, 0);
134 assertEqualIntA(a, 0, archive_write_header(a, entry));
135 archive_entry_free(entry);
137 /* Close the archive . */
138 assertA(0 == archive_write_close(a));
139 assertA(0 == archive_write_finish(a));
141 /* Remember the end of the archive in memory. */
142 buffend = buff + used;
144 /* Verify "End of Central Directory" record. */
145 /* Get address of end-of-central-directory record. */
146 p = buffend - 22; /* Assumes there is no zip comment field. */
147 failure("End-of-central-directory begins with PK\\005\\006 signature");
148 assertEqualMem(p, "PK\005\006", 4);
149 failure("This must be disk 0");
150 assertEqualInt(i2(p + 4), 0);
151 failure("Central dir must start on disk 0");
152 assertEqualInt(i2(p + 6), 0);
153 failure("All central dir entries are on this disk");
154 assertEqualInt(i2(p + 8), i2(p + 10));
155 failure("CD start (%d) + CD length (%d) should == archive size - 22",
156 i4(p + 12), i4(p + 16));
157 assertEqualInt(i4(p + 12) + i4(p + 16), used - 22);
158 failure("no zip comment");
159 assertEqualInt(i2(p + 20), 0);
161 /* Get address of first entry in central directory. */
162 p = buff + i4(buffend - 6);
163 failure("Central file record at offset %d should begin with"
164 " PK\\001\\002 signature",
167 /* Verify file entry in central directory. */
168 assertEqualMem(p, "PK\001\002", 4); /* Signature */
169 assertEqualInt(i2(p + 4), 3 * 256 + 20); /* Version made by */
170 assertEqualInt(i2(p + 6), 20); /* Version needed to extract */
171 assertEqualInt(i2(p + 8), 8); /* Flags */
172 assertEqualInt(i2(p + 10), 0); /* Compression method */
173 assertEqualInt(i2(p + 12), (tm->tm_hour * 2048) + (tm->tm_min * 32) + (tm->tm_sec / 2)); /* File time */
174 assertEqualInt(i2(p + 14), ((tm->tm_year - 80) * 512) + ((tm->tm_mon + 1) * 32) + tm->tm_mday); /* File date */
175 crc = bitcrc32(0, file_data1, sizeof(file_data1));
176 crc = bitcrc32(crc, file_data2, sizeof(file_data2));
177 assertEqualInt(i4(p + 16), crc); /* CRC-32 */
178 assertEqualInt(i4(p + 20), sizeof(file_data1) + sizeof(file_data2)); /* Compressed size */
179 assertEqualInt(i4(p + 24), sizeof(file_data1) + sizeof(file_data2)); /* Uncompressed size */
180 assertEqualInt(i2(p + 28), strlen(file_name)); /* Pathname length */
181 assertEqualInt(i2(p + 30), 13); /* Extra field length */
182 assertEqualInt(i2(p + 32), 0); /* File comment length */
183 assertEqualInt(i2(p + 34), 0); /* Disk number start */
184 assertEqualInt(i2(p + 36), 0); /* Internal file attrs */
185 assertEqualInt(i4(p + 38) >> 16 & 01777, file_perm); /* External file attrs */
186 assertEqualInt(i4(p + 42), 0); /* Offset of local header */
187 assertEqualMem(p + 46, file_name, strlen(file_name)); /* Pathname */
188 p = p + 46 + strlen(file_name);
189 assertEqualInt(i2(p), 0x5455); /* 'UT' extension header */
190 assertEqualInt(i2(p + 2), 5); /* 'UT' size */
191 assertEqualInt(p[4], 7); /* 'UT' flags */
192 assertEqualInt(i4(p + 5), t); /* 'UT' mtime */
194 assertEqualInt(i2(p), 0x7855); /* 'Ux' extension header */
195 assertEqualInt(i2(p + 2), 0); /* 'Ux' size */
198 /* Verify local header of file entry. */
200 assertEqualMem(q, "PK\003\004", 4); /* Signature */
201 assertEqualInt(i2(q + 4), 20); /* Version needed to extract */
202 assertEqualInt(i2(q + 6), 8); /* Flags */
203 assertEqualInt(i2(q + 8), 0); /* Compression method */
204 assertEqualInt(i2(q + 10), (tm->tm_hour * 2048) + (tm->tm_min * 32) + (tm->tm_sec / 2)); /* File time */
205 assertEqualInt(i2(q + 12), ((tm->tm_year - 80) * 512) + ((tm->tm_mon + 1) * 32) + tm->tm_mday); /* File date */
206 assertEqualInt(i4(q + 14), 0); /* CRC-32 */
207 assertEqualInt(i4(q + 18), sizeof(file_data1) + sizeof(file_data2)); /* Compressed size */
208 assertEqualInt(i4(q + 22), sizeof(file_data1) + sizeof(file_data2)); /* Uncompressed size */
209 assertEqualInt(i2(q + 26), strlen(file_name)); /* Pathname length */
210 assertEqualInt(i2(q + 28), 25); /* Extra field length */
211 assertEqualMem(q + 30, file_name, strlen(file_name)); /* Pathname */
212 q = q + 30 + strlen(file_name);
213 assertEqualInt(i2(q), 0x5455); /* 'UT' extension header */
214 assertEqualInt(i2(q + 2), 13); /* 'UT' size */
215 assertEqualInt(q[4], 7); /* 'UT' flags */
216 assertEqualInt(i4(q + 5), t); /* 'UT' mtime */
217 assertEqualInt(i4(q + 9), t); /* 'UT' atime */
218 assertEqualInt(i4(q + 13), t); /* 'UT' ctime */
220 assertEqualInt(i2(q), 0x7855); /* 'Ux' extension header */
221 assertEqualInt(i2(q + 2), 4); /* 'Ux' size */
222 assertEqualInt(i2(q + 4), file_uid); /* 'Ux' UID */
223 assertEqualInt(i2(q + 6), file_gid); /* 'Ux' GID */
226 /* Verify data of file entry. */
227 assertEqualMem(q, file_data1, sizeof(file_data1));
228 assertEqualMem(q + sizeof(file_data1), file_data2, sizeof(file_data2));
229 q = q + sizeof(file_data1) + sizeof(file_data2);
231 /* Verify data descriptor of file entry. */
232 assertEqualMem(q, "PK\007\010", 4); /* Signature */
233 assertEqualInt(i4(q + 4), crc); /* CRC-32 */
234 assertEqualInt(i4(q + 8), sizeof(file_data1) + sizeof(file_data2)); /* Compressed size */
235 assertEqualInt(i4(q + 12), sizeof(file_data1) + sizeof(file_data2)); /* Uncompressed size */
238 /* Verify folder entry in central directory. */
239 assertEqualMem(p, "PK\001\002", 4); /* Signature */
240 assertEqualInt(i2(p + 4), 3 * 256 + 20); /* Version made by */
241 assertEqualInt(i2(p + 6), 20); /* Version needed to extract */
242 assertEqualInt(i2(p + 8), 8); /* Flags */
243 assertEqualInt(i2(p + 10), 0); /* Compression method */
244 assertEqualInt(i2(p + 12), (tm->tm_hour * 2048) + (tm->tm_min * 32) + (tm->tm_sec / 2)); /* File time */
245 assertEqualInt(i2(p + 14), ((tm->tm_year - 80) * 512) + ((tm->tm_mon + 1) * 32) + tm->tm_mday); /* File date */
247 assertEqualInt(i4(p + 16), crc); /* CRC-32 */
248 assertEqualInt(i4(p + 20), 0); /* Compressed size */
249 assertEqualInt(i4(p + 24), 0); /* Uncompressed size */
250 assertEqualInt(i2(p + 28), strlen(folder_name)); /* Pathname length */
251 assertEqualInt(i2(p + 30), 13); /* Extra field length */
252 assertEqualInt(i2(p + 32), 0); /* File comment length */
253 assertEqualInt(i2(p + 34), 0); /* Disk number start */
254 assertEqualInt(i2(p + 36), 0); /* Internal file attrs */
255 assertEqualInt(i4(p + 38) >> 16 & 01777, folder_perm); /* External file attrs */
256 assertEqualInt(i4(p + 42), q - buff); /* Offset of local header */
257 assertEqualMem(p + 46, folder_name, strlen(folder_name)); /* Pathname */
258 p = p + 46 + strlen(folder_name);
259 assertEqualInt(i2(p), 0x5455); /* 'UT' extension header */
260 assertEqualInt(i2(p + 2), 5); /* 'UT' size */
261 assertEqualInt(p[4], 7); /* 'UT' flags */
262 assertEqualInt(i4(p + 5), t); /* 'UT' mtime */
264 assertEqualInt(i2(p), 0x7855); /* 'Ux' extension header */
265 assertEqualInt(i2(p + 2), 0); /* 'Ux' size */
268 /* Verify local header of folder entry. */
269 assertEqualMem(q, "PK\003\004", 4); /* Signature */
270 assertEqualInt(i2(q + 4), 20); /* Version needed to extract */
271 assertEqualInt(i2(q + 6), 8); /* Flags */
272 assertEqualInt(i2(q + 8), 0); /* Compression method */
273 assertEqualInt(i2(q + 10), (tm->tm_hour * 2048) + (tm->tm_min * 32) + (tm->tm_sec / 2)); /* File time */
274 assertEqualInt(i2(q + 12), ((tm->tm_year - 80) * 512) + ((tm->tm_mon + 1) * 32) + tm->tm_mday); /* File date */
275 assertEqualInt(i4(q + 14), 0); /* CRC-32 */
276 assertEqualInt(i4(q + 18), 0); /* Compressed size */
277 assertEqualInt(i4(q + 22), 0); /* Uncompressed size */
278 assertEqualInt(i2(q + 26), strlen(folder_name)); /* Pathname length */
279 assertEqualInt(i2(q + 28), 25); /* Extra field length */
280 assertEqualMem(q + 30, folder_name, strlen(folder_name)); /* Pathname */
281 q = q + 30 + strlen(folder_name);
282 assertEqualInt(i2(q), 0x5455); /* 'UT' extension header */
283 assertEqualInt(i2(q + 2), 13); /* 'UT' size */
284 assertEqualInt(q[4], 7); /* 'UT' flags */
285 assertEqualInt(i4(q + 5), t); /* 'UT' mtime */
286 assertEqualInt(i4(q + 9), t); /* 'UT' atime */
287 assertEqualInt(i4(q + 13), t); /* 'UT' ctime */
289 assertEqualInt(i2(q), 0x7855); /* 'Ux' extension header */
290 assertEqualInt(i2(q + 2), 4); /* 'Ux' size */
291 assertEqualInt(i2(q + 4), folder_uid); /* 'Ux' UID */
292 assertEqualInt(i2(q + 6), folder_gid); /* 'Ux' GID */
295 /* There should not be any data in the folder entry,
296 * meaning next is the data descriptor header. */
298 /* Verify data descriptor of folder entry. */
299 assertEqualMem(q, "PK\007\010", 4); /* Signature */
300 assertEqualInt(i4(q + 4), crc); /* CRC-32 */
301 assertEqualInt(i4(q + 8), 0); /* Compressed size */
302 assertEqualInt(i4(q + 12), 0); /* Uncompressed size */