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