xref: /NextBSD/contrib/libarchive/libarchive/test/test_write_zip_set_compression_store.c (revision eb1a5f8de9f7ea602c373a710f531abbf81141c4)
1 /*-
2  * Copyright (c) 2012 Matthias Brantner
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 #include "test.h"
27 
28 static unsigned long
bitcrc32(unsigned long c,void * _p,size_t s)29 bitcrc32(unsigned long c, void *_p, size_t s)
30 {
31 	/* This is a drop-in replacement for crc32() from zlib.
32 	 * Libarchive should be able to correctly generate
33 	 * uncompressed zip archives (including correct CRCs) even
34 	 * when zlib is unavailable, and this function helps us verify
35 	 * that.  Yes, this is very, very slow and unsuitable for
36 	 * production use, but it's correct, compact, and works well
37 	 * enough for this particular usage.  Libarchive internally
38 	 * uses a much more efficient implementation.  */
39 	const unsigned char *p = _p;
40 	int bitctr;
41 
42 	if (p == NULL)
43 		return (0);
44 
45 	for (; s > 0; --s) {
46 		c ^= *p++;
47 		for (bitctr = 8; bitctr > 0; --bitctr) {
48 			if (c & 1) c = (c >> 1);
49 			else	   c = (c >> 1) ^ 0xedb88320;
50 			c ^= 0x80000000;
51 		}
52 	}
53 	return (c);
54 }
55 
56 /* Quick and dirty: Read 2-byte and 4-byte integers from Zip file. */
i2(const char * p)57 static int i2(const char *p) { return ((p[0] & 0xff) | ((p[1] & 0xff) << 8)); }
i4(const char * p)58 static int i4(const char *p) { return (i2(p) | (i2(p + 2) << 16)); }
59 
DEFINE_TEST(test_write_zip_set_compression_store)60 DEFINE_TEST(test_write_zip_set_compression_store)
61 {
62 	/* Buffer data */
63 	struct archive *a;
64 	struct archive_entry *entry;
65 	char buff[100000];
66 	const char *buffend;
67 	/* p is the pointer to walk over the central directory,
68 	 * q walks over the local headers, the data and the data descriptors. */
69 	const char *p, *q;
70 	size_t used;
71 
72 	/* File data */
73 	char file_name[] = "file";
74 	char file_data1[] = {'1', '2', '3', '4', '5'};
75 	char file_data2[] = {'6', '7', '8', '9', '0'};
76 	int file_perm = 00644;
77 	short file_uid = 10;
78 	short file_gid = 20;
79 
80 	/* Folder data */
81 	char folder_name[] = "folder/";
82 	int folder_perm = 00755;
83 	short folder_uid = 30;
84 	short folder_gid = 40;
85 
86 	/* Time data */
87 	time_t t = time(NULL);
88 	struct tm *tm = localtime(&t);
89 
90 	/* Misc variables */
91 	unsigned long crc;
92 
93 	/* Create new ZIP archive in memory without padding. */
94 	assert((a = archive_write_new()) != NULL);
95 	assertA(0 == archive_write_set_format_zip(a));
96 	assertA(0 == archive_write_add_filter_none(a));
97 	assertA(0 == archive_write_set_bytes_per_block(a, 1));
98 	assertA(0 == archive_write_set_bytes_in_last_block(a, 1));
99 	assertA(0 == archive_write_open_memory(a, buff, sizeof(buff), &used));
100 
101 	/* Write entries. */
102 
103 	/* Regular file */
104 	assert((entry = archive_entry_new()) != NULL);
105 	archive_entry_set_pathname(entry, file_name);
106 	archive_entry_set_mode(entry, S_IFREG | 0644);
107 	archive_entry_set_size(entry, sizeof(file_data1) + sizeof(file_data2));
108 	archive_entry_set_uid(entry, file_uid);
109 	archive_entry_set_gid(entry, file_gid);
110 	archive_entry_set_mtime(entry, t, 0);
111 	archive_entry_set_atime(entry, t, 0);
112 	archive_entry_set_ctime(entry, t, 0);
113 	archive_write_zip_set_compression_store(a);
114 	assertEqualIntA(a, 0, archive_write_header(a, entry));
115 	assertEqualIntA(a, sizeof(file_data1), archive_write_data(a, file_data1, sizeof(file_data1)));
116 	assertEqualIntA(a, sizeof(file_data2), archive_write_data(a, file_data2, sizeof(file_data2)));
117 	archive_entry_free(entry);
118 	archive_write_finish_entry(a);
119 
120 	/* Folder */
121 	assert((entry = archive_entry_new()) != NULL);
122 	archive_entry_set_pathname(entry, folder_name);
123 	archive_entry_set_mode(entry, S_IFDIR | folder_perm);
124 	archive_entry_set_size(entry, 0);
125 	archive_entry_set_uid(entry, folder_uid);
126 	archive_entry_set_gid(entry, folder_gid);
127 	archive_entry_set_mtime(entry, t, 0);
128 	archive_entry_set_atime(entry, t, 0);
129 	archive_entry_set_ctime(entry, t, 0);
130 	archive_write_zip_set_compression_store(a);
131 	assertEqualIntA(a, 0, archive_write_header(a, entry));
132 	archive_entry_free(entry);
133 	archive_write_finish_entry(a);
134 
135 	/* Close the archive . */
136 	assertEqualIntA(a, ARCHIVE_OK, archive_write_close(a));
137 	assertEqualInt(ARCHIVE_OK, archive_write_free(a));
138 
139 	/* Remember the end of the archive in memory. */
140 	buffend = buff + used;
141 
142 	/* Verify "End of Central Directory" record. */
143 	/* Get address of end-of-central-directory record. */
144 	p = buffend - 22; /* Assumes there is no zip comment field. */
145 	failure("End-of-central-directory begins with PK\\005\\006 signature");
146 	assertEqualMem(p, "PK\005\006", 4);
147 	failure("This must be disk 0");
148 	assertEqualInt(i2(p + 4), 0);
149 	failure("Central dir must start on disk 0");
150 	assertEqualInt(i2(p + 6), 0);
151 	failure("All central dir entries are on this disk");
152 	assertEqualInt(i2(p + 8), i2(p + 10));
153 	failure("CD start (%d) + CD length (%d) should == archive size - 22",
154 	    i4(p + 12), i4(p + 16));
155 	assertEqualInt(i4(p + 12) + i4(p + 16), used - 22);
156 	failure("no zip comment");
157 	assertEqualInt(i2(p + 20), 0);
158 
159 	/* Get address of first entry in central directory. */
160 	p = buff + i4(buffend - 6);
161 	failure("Central file record at offset %d should begin with"
162 	    " PK\\001\\002 signature",
163 	    i4(buffend - 10));
164 
165 	/* Verify file entry in central directory. */
166 	assertEqualMem(p, "PK\001\002", 4); /* Signature */
167 	assertEqualInt(i2(p + 4), 3 * 256 + 20); /* Version made by */
168 	assertEqualInt(i2(p + 6), 20); /* Version needed to extract */
169 	assertEqualInt(i2(p + 8), 8); /* Flags */
170 	assertEqualInt(i2(p + 10), 0); /* Compression method */
171 	assertEqualInt(i2(p + 12), (tm->tm_hour * 2048) + (tm->tm_min * 32) + (tm->tm_sec / 2)); /* File time */
172 	assertEqualInt(i2(p + 14), ((tm->tm_year - 80) * 512) + ((tm->tm_mon + 1) * 32) + tm->tm_mday); /* File date */
173 	crc = bitcrc32(0, file_data1, sizeof(file_data1));
174 	crc = bitcrc32(crc, file_data2, sizeof(file_data2));
175 	assertEqualInt(i4(p + 16), crc); /* CRC-32 */
176 	assertEqualInt(i4(p + 20), sizeof(file_data1) + sizeof(file_data2)); /* Compressed size */
177 	assertEqualInt(i4(p + 24), sizeof(file_data1) + sizeof(file_data2)); /* Uncompressed size */
178 	assertEqualInt(i2(p + 28), strlen(file_name)); /* Pathname length */
179 	assertEqualInt(i2(p + 30), 13); /* Extra field length */
180 	assertEqualInt(i2(p + 32), 0); /* File comment length */
181 	assertEqualInt(i2(p + 34), 0); /* Disk number start */
182 	assertEqualInt(i2(p + 36), 0); /* Internal file attrs */
183 	assertEqualInt(i4(p + 38) >> 16 & 01777, file_perm); /* External file attrs */
184 	assertEqualInt(i4(p + 42), 0); /* Offset of local header */
185 	assertEqualMem(p + 46, file_name, strlen(file_name)); /* Pathname */
186 	p = p + 46 + strlen(file_name);
187 	assertEqualInt(i2(p), 0x5455); /* 'UT' extension header */
188 	assertEqualInt(i2(p + 2), 5); /* 'UT' size */
189 	assertEqualInt(p[4], 7); /* 'UT' flags */
190 	assertEqualInt(i4(p + 5), t); /* 'UT' mtime */
191 	p = p + 9;
192 	assertEqualInt(i2(p), 0x7875); /* 'ux' extension header */
193 	assertEqualInt(i2(p + 2), 0); /* 'ux' size */
194 	p = p + 4;
195 
196 	/* Verify local header of file entry. */
197 	q = buff;
198 	assertEqualMem(q, "PK\003\004", 4); /* Signature */
199 	assertEqualInt(i2(q + 4), 20); /* 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), 32); /* Extra field length */
209 	assertEqualMem(q + 30, file_name, strlen(file_name)); /* Pathname */
210 	q = q + 30 + strlen(file_name);
211 	assertEqualInt(i2(q), 0x5455); /* 'UT' extension header */
212 	assertEqualInt(i2(q + 2), 13); /* 'UT' size */
213 	assertEqualInt(q[4], 7); /* 'UT' flags */
214 	assertEqualInt(i4(q + 5), t); /* 'UT' mtime */
215 	assertEqualInt(i4(q + 9), t); /* 'UT' atime */
216 	assertEqualInt(i4(q + 13), t); /* 'UT' ctime */
217 	q = q + 17;
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 + 15;
226 
227 	/* Verify data of file entry. */
228 	assertEqualMem(q, file_data1, sizeof(file_data1));
229 	assertEqualMem(q + sizeof(file_data1), file_data2, sizeof(file_data2));
230 	q = q + sizeof(file_data1) + sizeof(file_data2);
231 
232 	/* Verify data descriptor of file entry. */
233 	assertEqualMem(q, "PK\007\010", 4); /* Signature */
234 	assertEqualInt(i4(q + 4), crc); /* CRC-32 */
235 	assertEqualInt(i4(q + 8), sizeof(file_data1) + sizeof(file_data2)); /* Compressed size */
236 	assertEqualInt(i4(q + 12), sizeof(file_data1) + sizeof(file_data2)); /* Uncompressed size */
237 	q = q + 16;
238 
239 	/* Verify folder entry in central directory. */
240 	assertEqualMem(p, "PK\001\002", 4); /* Signature */
241 	assertEqualInt(i2(p + 4), 3 * 256 + 20); /* Version made by */
242 	assertEqualInt(i2(p + 6), 20); /* Version needed to extract */
243 	assertEqualInt(i2(p + 8), 8); /* Flags */
244 	assertEqualInt(i2(p + 10), 0); /* Compression method */
245 	assertEqualInt(i2(p + 12), (tm->tm_hour * 2048) + (tm->tm_min * 32) + (tm->tm_sec / 2)); /* File time */
246 	assertEqualInt(i2(p + 14), ((tm->tm_year - 80) * 512) + ((tm->tm_mon + 1) * 32) + tm->tm_mday); /* File date */
247 	crc = 0;
248 	assertEqualInt(i4(p + 16), crc); /* CRC-32 */
249 	assertEqualInt(i4(p + 20), 0); /* Compressed size */
250 	assertEqualInt(i4(p + 24), 0); /* Uncompressed size */
251 	assertEqualInt(i2(p + 28), strlen(folder_name)); /* Pathname length */
252 	assertEqualInt(i2(p + 30), 13); /* Extra field length */
253 	assertEqualInt(i2(p + 32), 0); /* File comment length */
254 	assertEqualInt(i2(p + 34), 0); /* Disk number start */
255 	assertEqualInt(i2(p + 36), 0); /* Internal file attrs */
256 	assertEqualInt(i4(p + 38) >> 16 & 01777, folder_perm); /* External file attrs */
257 	assertEqualInt(i4(p + 42), q - buff); /* Offset of local header */
258 	assertEqualMem(p + 46, folder_name, strlen(folder_name)); /* Pathname */
259 	p = p + 46 + strlen(folder_name);
260 	assertEqualInt(i2(p), 0x5455); /* 'UT' extension header */
261 	assertEqualInt(i2(p + 2), 5); /* 'UT' size */
262 	assertEqualInt(p[4], 7); /* 'UT' flags */
263 	assertEqualInt(i4(p + 5), t); /* 'UT' mtime */
264 	p = p + 9;
265 	assertEqualInt(i2(p), 0x7875); /* 'ux' extension header */
266 	assertEqualInt(i2(p + 2), 0); /* 'ux' size */
267 	/*p = p + 4;*/
268 
269 	/* Verify local header of folder entry. */
270 	assertEqualMem(q, "PK\003\004", 4); /* Signature */
271 	assertEqualInt(i2(q + 4), 20); /* Version needed to extract */
272 	assertEqualInt(i2(q + 6), 8); /* Flags */
273 	assertEqualInt(i2(q + 8), 0); /* Compression method */
274 	assertEqualInt(i2(q + 10), (tm->tm_hour * 2048) + (tm->tm_min * 32) + (tm->tm_sec / 2)); /* File time */
275 	assertEqualInt(i2(q + 12), ((tm->tm_year - 80) * 512) + ((tm->tm_mon + 1) * 32) + tm->tm_mday); /* File date */
276 	assertEqualInt(i4(q + 14), 0); /* CRC-32 */
277 	assertEqualInt(i4(q + 18), 0); /* Compressed size */
278 	assertEqualInt(i4(q + 22), 0); /* Uncompressed size */
279 	assertEqualInt(i2(q + 26), strlen(folder_name)); /* Pathname length */
280 	assertEqualInt(i2(q + 28), 32); /* Extra field length */
281 	assertEqualMem(q + 30, folder_name, strlen(folder_name)); /* Pathname */
282 	q = q + 30 + strlen(folder_name);
283 	assertEqualInt(i2(q), 0x5455); /* 'UT' extension header */
284 	assertEqualInt(i2(q + 2), 13); /* 'UT' size */
285 	assertEqualInt(q[4], 7); /* 'UT' flags */
286 	assertEqualInt(i4(q + 5), t); /* 'UT' mtime */
287 	assertEqualInt(i4(q + 9), t); /* 'UT' atime */
288 	assertEqualInt(i4(q + 13), t); /* 'UT' ctime */
289 	q = q + 17;
290 	assertEqualInt(i2(q), 0x7875); /* 'ux' extension header */
291 	assertEqualInt(i2(q + 2), 11); /* 'ux' size */
292 	assertEqualInt(q[4], 1); /* 'ux' version */
293 	assertEqualInt(q[5], 4); /* 'ux' uid size */
294 	assertEqualInt(i4(q + 6), folder_uid); /* 'ux' UID */
295 	assertEqualInt(q[10], 4); /* 'ux' gid size */
296 	assertEqualInt(i4(q + 11), folder_gid); /* 'ux' GID */
297 	q = q + 15;
298 
299 	/* There should not be any data in the folder entry,
300 	 * meaning next is the data descriptor header. */
301 
302 	/* Verify data descriptor of folder entry. */
303 	assertEqualMem(q, "PK\007\010", 4); /* Signature */
304 	assertEqualInt(i4(q + 4), crc); /* CRC-32 */
305 	assertEqualInt(i4(q + 8), 0); /* Compressed size */
306 	assertEqualInt(i4(q + 12), 0); /* Uncompressed size */
307 	/*q = q + 16;*/
308 }
309