xref: /freebsd/contrib/libarchive/libarchive/test/test_write_format_zip_compression_store.c (revision b9128a37faafede823eb456aa65a11ac69997284)
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 
32 /* File data */
33 static const char file_name[] = "file";
34 static const char file_data1[] = {'1', '2', '3', '4', '5'};
35 static const char file_data2[] = {'6', '7', '8', '9', '0'};
36 static const int file_perm = 00644;
37 static const short file_uid = 10;
38 static const short file_gid = 20;
39 
40 /* Folder data */
41 static const char folder_name[] = "folder/";
42 static const int folder_perm = 00755;
43 static const short folder_uid = 30;
44 static const short folder_gid = 40;
45 
46 static time_t now;
47 
48 static unsigned long
49 bitcrc32(unsigned long c, const void *_p, size_t s)
50 {
51 	/* This is a drop-in replacement for crc32() from zlib.
52 	 * Libarchive should be able to correctly generate
53 	 * uncompressed zip archives (including correct CRCs) even
54 	 * when zlib is unavailable, and this function helps us verify
55 	 * that.  Yes, this is very, very slow and unsuitable for
56 	 * production use, but it's correct, compact, and works well
57 	 * enough for this particular usage.  Libarchive internally
58 	 * uses a much more efficient implementation.  */
59 	const unsigned char *p = _p;
60 	int bitctr;
61 
62 	if (p == NULL)
63 		return (0);
64 
65 	for (; s > 0; --s) {
66 		c ^= *p++;
67 		for (bitctr = 8; bitctr > 0; --bitctr) {
68 			if (c & 1) c = (c >> 1);
69 			else	   c = (c >> 1) ^ 0xedb88320;
70 			c ^= 0x80000000;
71 		}
72 	}
73 	return (c);
74 }
75 
76 static void verify_write_uncompressed(struct archive *a)
77 {
78 	struct archive_entry *entry;
79 
80 	/* Write entries. */
81 
82 	/* Regular file */
83 	assert((entry = archive_entry_new()) != NULL);
84 	archive_entry_set_pathname(entry, file_name);
85 	archive_entry_set_mode(entry, S_IFREG | 0644);
86 	archive_entry_set_size(entry, sizeof(file_data1) + sizeof(file_data2));
87 	archive_entry_set_uid(entry, file_uid);
88 	archive_entry_set_gid(entry, file_gid);
89 	archive_entry_set_mtime(entry, now, 0);
90 	archive_entry_set_atime(entry, now + 3, 0);
91 	assertEqualIntA(a, 0, archive_write_header(a, entry));
92 	assertEqualIntA(a, sizeof(file_data1), archive_write_data(a, file_data1, sizeof(file_data1)));
93 	assertEqualIntA(a, sizeof(file_data2), archive_write_data(a, file_data2, sizeof(file_data2)));
94 	archive_entry_free(entry);
95 
96 	/* Folder */
97 	assert((entry = archive_entry_new()) != NULL);
98 	archive_entry_set_pathname(entry, folder_name);
99 	archive_entry_set_mode(entry, S_IFDIR | folder_perm);
100 	archive_entry_set_size(entry, 0);
101 	archive_entry_set_uid(entry, folder_uid);
102 	archive_entry_set_gid(entry, folder_gid);
103 	archive_entry_set_mtime(entry, now, 0);
104 	archive_entry_set_ctime(entry, now + 5, 0);
105 	assertEqualIntA(a, 0, archive_write_header(a, entry));
106 	archive_entry_free(entry);
107 }
108 
109 /* Quick and dirty: Read 2-byte and 4-byte integers from Zip file. */
110 static unsigned int
111 i2(const void *p_)
112 {
113 	const unsigned char *p = p_;
114 	return (p[0] | (p[1] << 8));
115 }
116 
117 static unsigned int
118 i4(const void *p_)
119 {
120 	const unsigned char *p = p_;
121 	return (i2(p) | (i2(p + 2) << 16));
122 }
123 
124 static void verify_uncompressed_contents(const char *buff, size_t used)
125 {
126 	const char *buffend;
127 
128 	/* Misc variables */
129 	unsigned long crc;
130 	struct tm *tm;
131 #if defined(HAVE_LOCALTIME_R) || defined(HAVE_LOCALTIME_S)
132 	struct tm tmbuf;
133 #endif
134 	/* p is the pointer to walk over the central directory,
135 	 * q walks over the local headers, the data and the data descriptors. */
136 	const char *p, *q, *local_header, *extra_start;
137 
138 #if defined(HAVE_LOCALTIME_S)
139 	tm = localtime_s(&tmbuf, &now) ? NULL : &tmbuf;
140 #elif defined(HAVE_LOCALTIME_R)
141 	tm = localtime_r(&now, &tmbuf);
142 #else
143 	tm = localtime(&now);
144 #endif
145 
146 	/* Remember the end of the archive in memory. */
147 	buffend = buff + used;
148 
149 	/* Verify "End of Central Directory" record. */
150 	/* Get address of end-of-central-directory record. */
151 	p = buffend - 22; /* Assumes there is no zip comment field. */
152 	failure("End-of-central-directory begins with PK\\005\\006 signature");
153 	assertEqualMem(p, "PK\005\006", 4);
154 	failure("This must be disk 0");
155 	assertEqualInt(i2(p + 4), 0);
156 	failure("Central dir must start on disk 0");
157 	assertEqualInt(i2(p + 6), 0);
158 	failure("All central dir entries are on this disk");
159 	assertEqualInt(i2(p + 8), i2(p + 10));
160 	failure("CD start (%d) + CD length (%d) should == archive size - 22",
161 	    i4(p + 12), i4(p + 16));
162 	assertEqualInt(i4(p + 12) + i4(p + 16), used - 22);
163 	failure("no zip comment");
164 	assertEqualInt(i2(p + 20), 0);
165 
166 	/* Get address of first entry in central directory. */
167 	p = buff + i4(buffend - 6);
168 	failure("Central file record at offset %d should begin with"
169 	    " PK\\001\\002 signature",
170 	    i4(buffend - 10));
171 
172 	/* Verify file entry in central directory. */
173 	assertEqualMem(p, "PK\001\002", 4); /* Signature */
174 	assertEqualInt(i2(p + 4), 3 * 256 + 10); /* Version made by */
175 	assertEqualInt(i2(p + 6), 10); /* Version needed to extract */
176 	assertEqualInt(i2(p + 8), 8); /* Flags */
177 	assertEqualInt(i2(p + 10), 0); /* Compression method */
178 	assertEqualInt(i2(p + 12), (tm->tm_hour * 2048) + (tm->tm_min * 32) + (tm->tm_sec / 2)); /* File time */
179 	assertEqualInt(i2(p + 14), ((tm->tm_year - 80) * 512) + ((tm->tm_mon + 1) * 32) + tm->tm_mday); /* File date */
180 	crc = bitcrc32(0, file_data1, sizeof(file_data1));
181 	crc = bitcrc32(crc, file_data2, sizeof(file_data2));
182 	assertEqualInt(i4(p + 16), crc); /* CRC-32 */
183 	assertEqualInt(i4(p + 20), sizeof(file_data1) + sizeof(file_data2)); /* Compressed size */
184 	assertEqualInt(i4(p + 24), sizeof(file_data1) + sizeof(file_data2)); /* Uncompressed size */
185 	assertEqualInt(i2(p + 28), strlen(file_name)); /* Pathname length */
186 	assertEqualInt(i2(p + 30), 24); /* Extra field length */
187 	assertEqualInt(i2(p + 32), 0); /* File comment length */
188 	assertEqualInt(i2(p + 34), 0); /* Disk number start */
189 	assertEqualInt(i2(p + 36), 0); /* Internal file attrs */
190 	assertEqualInt(i4(p + 38) >> 16 & 01777, file_perm); /* External file attrs */
191 	assertEqualInt(i4(p + 42), 0); /* Offset of local header */
192 	assertEqualMem(p + 46, file_name, strlen(file_name)); /* Pathname */
193 	p = p + 46 + strlen(file_name);
194 
195 	assertEqualInt(i2(p), 0x7875); /* 'ux' extension header */
196 	assertEqualInt(i2(p + 2), 11); /* 'ux' size */
197 /* TODO */
198 	p = p + 4 + i2(p + 2);
199 
200 	assertEqualInt(i2(p), 0x5455); /* 'UT' extension header */
201 	assertEqualInt(i2(p + 2), 5); /* 'UT' size */
202 	assertEqualInt(p[4], 1); /* 'UT' flags */
203 	assertEqualInt(i4(p + 5), now); /* 'UT' mtime */
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: bit 3 = length-at-end.  Required because CRC32 is unknown */
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), 0); /* Compressed size, must be zero because of length-at-end */
216 	assertEqualInt(i4(q + 22), 0); /* Uncompressed size, must be zero because of length-at-end */
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 
222 	assertEqualInt(i2(q), 0x7875); /* 'ux' extension header */
223 	assertEqualInt(i2(q + 2), 11); /* 'ux' size */
224 	assertEqualInt(q[4], 1); /* 'ux' version */
225 	assertEqualInt(q[5], 4); /* 'ux' uid size */
226 	assertEqualInt(i4(q + 6), file_uid); /* 'Ux' UID */
227 	assertEqualInt(q[10], 4); /* 'ux' gid size */
228 	assertEqualInt(i4(q + 11), file_gid); /* 'Ux' GID */
229 	q = q + 4 + i2(q + 2);
230 
231 	assertEqualInt(i2(q), 0x5455); /* 'UT' extension header */
232 	assertEqualInt(i2(q + 2), 9); /* 'UT' size */
233 	assertEqualInt(q[4], 3); /* 'UT' flags */
234 	assertEqualInt(i4(q + 5), now); /* 'UT' mtime */
235 	assertEqualInt(i4(q + 9), now + 3); /* 'UT' atime */
236 	q = q + 4 + i2(q + 2);
237 
238 	assertEqualInt(i2(q), 0x6c78); /* 'xl' experimental extension header */
239 	assertEqualInt(i2(q + 2), 9); /* size */
240 	assertEqualInt(q[4], 7); /* Bitmap of fields included. */
241 	assertEqualInt(i2(q + 5) >> 8, 3); /* system & version made by */
242 	assertEqualInt(i2(q + 7), 0); /* internal file attributes */
243 	assertEqualInt(i4(q + 9) >> 16 & 01777, file_perm); /* external file attributes */
244 	q = q + 4 + i2(q + 2);
245 
246 	assert(q == extra_start + i2(local_header + 28));
247 	q = extra_start + i2(local_header + 28);
248 
249 	/* Verify data of file entry. */
250 	assertEqualMem(q, file_data1, sizeof(file_data1));
251 	assertEqualMem(q + sizeof(file_data1), file_data2, sizeof(file_data2));
252 	q = q + sizeof(file_data1) + sizeof(file_data2);
253 
254 	/* Verify data descriptor of file entry. */
255 	assertEqualMem(q, "PK\007\010", 4); /* Signature */
256 	assertEqualInt(i4(q + 4), crc); /* CRC-32 */
257 	assertEqualInt(i4(q + 8), sizeof(file_data1) + sizeof(file_data2)); /* Compressed size */
258 	assertEqualInt(i4(q + 12), sizeof(file_data1) + sizeof(file_data2)); /* Uncompressed size */
259 	q = q + 16;
260 
261 	/* Verify folder entry in central directory. */
262 	assertEqualMem(p, "PK\001\002", 4); /* Signature */
263 	assertEqualInt(i2(p + 4), 3 * 256 + 20); /* Version made by */
264 	assertEqualInt(i2(p + 6), 20); /* Version needed to extract */
265 	assertEqualInt(i2(p + 8), 0); /* Flags */
266 	assertEqualInt(i2(p + 10), 0); /* Compression method */
267 	assertEqualInt(i2(p + 12), (tm->tm_hour * 2048) + (tm->tm_min * 32) + (tm->tm_sec / 2)); /* File time */
268 	assertEqualInt(i2(p + 14), ((tm->tm_year - 80) * 512) + ((tm->tm_mon + 1) * 32) + tm->tm_mday); /* File date */
269 	crc = 0;
270 	assertEqualInt(i4(p + 16), crc); /* CRC-32 */
271 	assertEqualInt(i4(p + 20), 0); /* Compressed size */
272 	assertEqualInt(i4(p + 24), 0); /* Uncompressed size */
273 	assertEqualInt(i2(p + 28), strlen(folder_name)); /* Pathname length */
274 	assertEqualInt(i2(p + 30), 24); /* Extra field length */
275 	assertEqualInt(i2(p + 32), 0); /* File comment length */
276 	assertEqualInt(i2(p + 34), 0); /* Disk number start */
277 	assertEqualInt(i2(p + 36), 0); /* Internal file attrs */
278 	assertEqualInt(i4(p + 38) >> 16 & 01777, folder_perm); /* External file attrs */
279 	assertEqualInt(i4(p + 42), q - buff); /* Offset of local header */
280 	assertEqualMem(p + 46, folder_name, strlen(folder_name)); /* Pathname */
281 	p = p + 46 + strlen(folder_name);
282 
283 	assertEqualInt(i2(p), 0x7875); /* 'ux' extension header */
284 	assertEqualInt(i2(p + 2), 11); /* 'ux' size */
285 	assertEqualInt(p[4], 1); /* 'ux' version */
286 	assertEqualInt(p[5], 4); /* 'ux' uid size */
287 	assertEqualInt(i4(p + 6), folder_uid); /* 'ux' UID */
288 	assertEqualInt(p[10], 4); /* 'ux' gid size */
289 	assertEqualInt(i4(p + 11), folder_gid); /* 'ux' GID */
290 	p = p + 4 + i2(p + 2);
291 
292 	assertEqualInt(i2(p), 0x5455); /* 'UT' extension header */
293 	assertEqualInt(i2(p + 2), 5); /* 'UT' size */
294 	assertEqualInt(p[4], 1); /* 'UT' flags */
295 	assertEqualInt(i4(p + 5), now); /* 'UT' mtime */
296 	p = p + 4 + i2(p + 2);
297 
298 	/* Verify local header of folder entry. */
299 	local_header = q;
300 	assertEqualMem(q, "PK\003\004", 4); /* Signature */
301 	assertEqualInt(i2(q + 4), 20); /* Version needed to extract */
302 	assertEqualInt(i2(q + 6), 0); /* Flags */
303 	assertEqualInt(i2(q + 8), 0); /* Compression method */
304 	assertEqualInt(i2(q + 10), (tm->tm_hour * 2048) + (tm->tm_min * 32) + (tm->tm_sec / 2)); /* File time */
305 	assertEqualInt(i2(q + 12), ((tm->tm_year - 80) * 512) + ((tm->tm_mon + 1) * 32) + tm->tm_mday); /* File date */
306 	assertEqualInt(i4(q + 14), 0); /* CRC-32 */
307 	assertEqualInt(i4(q + 18), 0); /* Compressed size */
308 	assertEqualInt(i4(q + 22), 0); /* Uncompressed size */
309 	assertEqualInt(i2(q + 26), strlen(folder_name)); /* Pathname length */
310 	assertEqualInt(i2(q + 28), 41); /* Extra field length */
311 	assertEqualMem(q + 30, folder_name, strlen(folder_name)); /* Pathname */
312 	extra_start = q = q + 30 + strlen(folder_name);
313 
314 	assertEqualInt(i2(q), 0x7875); /* 'ux' extension header */
315 	assertEqualInt(i2(q + 2), 11); /* 'ux' size */
316 	assertEqualInt(q[4], 1); /* 'ux' version */
317 	assertEqualInt(q[5], 4); /* 'ux' uid size */
318 	assertEqualInt(i4(q + 6), folder_uid); /* 'ux' UID */
319 	assertEqualInt(q[10], 4); /* 'ux' gid size */
320 	assertEqualInt(i4(q + 11), folder_gid); /* 'ux' GID */
321 	q = q + 4 + i2(q + 2);
322 
323 	assertEqualInt(i2(q), 0x5455); /* 'UT' extension header */
324 	assertEqualInt(i2(q + 2), 9); /* 'UT' size */
325 	assertEqualInt(q[4], 5); /* 'UT' flags */
326 	assertEqualInt(i4(q + 5), now); /* 'UT' mtime */
327 	assertEqualInt(i4(q + 9), now + 5); /* 'UT' atime */
328 	q = q + 4 + i2(q + 2);
329 
330 	assertEqualInt(i2(q), 0x6c78); /* 'xl' experimental extension header */
331 	assertEqualInt(i2(q + 2), 9); /* size */
332 	assertEqualInt(q[4], 7); /* bitmap of fields */
333 	assertEqualInt(i2(q + 5) >> 8, 3); /* system & version made by */
334 	assertEqualInt(i2(q + 7), 0); /* internal file attributes */
335 	assertEqualInt(i4(q + 9) >> 16 & 01777, folder_perm); /* external file attributes */
336 	q = q + 4 + i2(q + 2);
337 
338 	assert(q == extra_start + i2(local_header + 28));
339 	q = extra_start + i2(local_header + 28);
340 
341 	/* There should not be any data in the folder entry,
342 	 * so the first central directory entry should be next: */
343 	assertEqualMem(q, "PK\001\002", 4); /* Signature */
344 }
345 
346 DEFINE_TEST(test_write_format_zip_compression_store)
347 {
348 	/* Buffer data */
349 	struct archive *a;
350 	char buff[100000];
351 	size_t used;
352 
353 	/* Time data */
354 	now = time(NULL);
355 
356 	/* Create new ZIP archive in memory without padding. */
357 	/* Use compression=store to disable compression. */
358 	assert((a = archive_write_new()) != NULL);
359 	assertEqualIntA(a, ARCHIVE_OK, archive_write_set_format_zip(a));
360 	assertEqualIntA(a, ARCHIVE_OK,
361 	    archive_write_set_options(a, "zip:compression=store"));
362 	assertEqualIntA(a, ARCHIVE_OK,
363 	    archive_write_set_options(a, "zip:experimental"));
364 	assertEqualIntA(a, ARCHIVE_OK, archive_write_add_filter_none(a));
365 	assertEqualIntA(a, ARCHIVE_OK, archive_write_set_bytes_per_block(a, 1));
366 	assertEqualIntA(a, ARCHIVE_OK, archive_write_set_bytes_in_last_block(a, 1));
367 	assertEqualIntA(a, ARCHIVE_OK, archive_write_open_memory(a, buff, sizeof(buff), &used));
368 
369 	verify_write_uncompressed(a);
370 
371 	/* Close the archive . */
372 	assertEqualIntA(a, ARCHIVE_OK, archive_write_close(a));
373 	assertEqualInt(ARCHIVE_OK, archive_write_free(a));
374 	dumpfile("constructed.zip", buff, used);
375 
376 	verify_uncompressed_contents(buff, used);
377 
378 	/* Create new ZIP archive in memory without padding. */
379 	/* Use compression-level=0 to disable compression. */
380 	assert((a = archive_write_new()) != NULL);
381 	assertEqualIntA(a, ARCHIVE_OK, archive_write_set_format_zip(a));
382 	assertEqualIntA(a, ARCHIVE_OK,
383 	    archive_write_set_options(a, "zip:compression-level=0"));
384 	assertEqualIntA(a, ARCHIVE_OK,
385 	    archive_write_set_options(a, "zip:experimental"));
386 	assertEqualIntA(a, ARCHIVE_OK, archive_write_add_filter_none(a));
387 	assertEqualIntA(a, ARCHIVE_OK, archive_write_set_bytes_per_block(a, 1));
388 	assertEqualIntA(a, ARCHIVE_OK, archive_write_set_bytes_in_last_block(a, 1));
389 	assertEqualIntA(a, ARCHIVE_OK, archive_write_open_memory(a, buff, sizeof(buff), &used));
390 
391 	verify_write_uncompressed(a);
392 
393 	/* Close the archive . */
394 	assertEqualIntA(a, ARCHIVE_OK, archive_write_close(a));
395 	assertEqualInt(ARCHIVE_OK, archive_write_free(a));
396 	dumpfile("constructed.zip", buff, used);
397 
398 	verify_uncompressed_contents(buff, used);
399 
400 }
401