xref: /NextBSD/contrib/libarchive/libarchive/test/test_write_format_zip_no_compression.c (revision eb1a5f8de9f7ea602c373a710f531abbf81141c4)
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$");
32 
33 static unsigned long
bitcrc32(unsigned long c,void * _p,size_t s)34 bitcrc32(unsigned long c, void *_p, size_t s)
35 {
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;
45 	int bitctr;
46 
47 	if (p == NULL)
48 		return (0);
49 
50 	for (; s > 0; --s) {
51 		c ^= *p++;
52 		for (bitctr = 8; bitctr > 0; --bitctr) {
53 			if (c & 1) c = (c >> 1);
54 			else	   c = (c >> 1) ^ 0xedb88320;
55 			c ^= 0x80000000;
56 		}
57 	}
58 	return (c);
59 }
60 
61 /* Quick and dirty: Read 2-byte and 4-byte integers from Zip file. */
i2(const char * p)62 static int i2(const char *p) { return ((p[0] & 0xff) | ((p[1] & 0xff) << 8)); }
i4(const char * p)63 static int i4(const char *p) { return (i2(p) | (i2(p + 2) << 16)); }
64 
DEFINE_TEST(test_write_format_zip_no_compression)65 DEFINE_TEST(test_write_format_zip_no_compression)
66 {
67 	/* Buffer data */
68 	struct archive *a;
69 	struct archive_entry *entry;
70 	char buff[100000];
71 	const char *buffend;
72 	/* p is the pointer to walk over the central directory,
73 	 * q walks over the local headers, the data and the data descriptors. */
74 	const char *p, *q;
75 	size_t used;
76 
77 	/* File data */
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;
82 	short file_uid = 10;
83 	short file_gid = 20;
84 
85 	/* Folder data */
86 	char folder_name[] = "folder/";
87 	int folder_perm = 00755;
88 	short folder_uid = 30;
89 	short folder_gid = 40;
90 
91 	/* Time data */
92 	time_t t = time(NULL);
93 	struct tm *tm = localtime(&t);
94 
95 	/* Misc variables */
96 	unsigned long crc;
97 
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_options(a, "zip:compression=store"));
102 	assertA(0 == archive_write_add_filter_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));
106 
107 	/* Write entries. */
108 
109 	/* Regular file */
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);
123 
124 	/* Folder */
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);
136 
137 	/* Close the archive . */
138 	assertEqualIntA(a, ARCHIVE_OK, archive_write_close(a));
139 	assertEqualInt(ARCHIVE_OK, archive_write_free(a));
140 
141 	/* Remember the end of the archive in memory. */
142 	buffend = buff + used;
143 
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);
160 
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",
165 	    i4(buffend - 10));
166 
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 */
193 	p = p + 9;
194 	assertEqualInt(i2(p), 0x7875); /* 'ux' extension header */
195 	assertEqualInt(i2(p + 2), 0); /* 'ux' size */
196 	p = p + 4;
197 
198 	/* Verify local header of file entry. */
199 	q = buff;
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), 32); /* 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 */
219 	q = q + 17;
220 	assertEqualInt(i2(q), 0x7875); /* 'ux' extension header */
221 	assertEqualInt(i2(q + 2), 11); /* 'ux' size */
222 	assertEqualInt(q[4], 1); /* 'ux' version */
223 	assertEqualInt(q[5], 4); /* 'ux' uid size */
224 	assertEqualInt(i4(q + 6), file_uid); /* 'Ux' UID */
225 	assertEqualInt(q[10], 4); /* 'ux' gid size */
226 	assertEqualInt(i4(q + 11), file_gid); /* 'Ux' GID */
227 	q = q + 15;
228 
229 	/* Verify data of file entry. */
230 	assertEqualMem(q, file_data1, sizeof(file_data1));
231 	assertEqualMem(q + sizeof(file_data1), file_data2, sizeof(file_data2));
232 	q = q + sizeof(file_data1) + sizeof(file_data2);
233 
234 	/* Verify data descriptor of file entry. */
235 	assertEqualMem(q, "PK\007\010", 4); /* Signature */
236 	assertEqualInt(i4(q + 4), crc); /* CRC-32 */
237 	assertEqualInt(i4(q + 8), sizeof(file_data1) + sizeof(file_data2)); /* Compressed size */
238 	assertEqualInt(i4(q + 12), sizeof(file_data1) + sizeof(file_data2)); /* Uncompressed size */
239 	q = q + 16;
240 
241 	/* Verify folder entry in central directory. */
242 	assertEqualMem(p, "PK\001\002", 4); /* Signature */
243 	assertEqualInt(i2(p + 4), 3 * 256 + 20); /* Version made by */
244 	assertEqualInt(i2(p + 6), 20); /* Version needed to extract */
245 	assertEqualInt(i2(p + 8), 8); /* Flags */
246 	assertEqualInt(i2(p + 10), 0); /* Compression method */
247 	assertEqualInt(i2(p + 12), (tm->tm_hour * 2048) + (tm->tm_min * 32) + (tm->tm_sec / 2)); /* File time */
248 	assertEqualInt(i2(p + 14), ((tm->tm_year - 80) * 512) + ((tm->tm_mon + 1) * 32) + tm->tm_mday); /* File date */
249 	crc = 0;
250 	assertEqualInt(i4(p + 16), crc); /* CRC-32 */
251 	assertEqualInt(i4(p + 20), 0); /* Compressed size */
252 	assertEqualInt(i4(p + 24), 0); /* Uncompressed size */
253 	assertEqualInt(i2(p + 28), strlen(folder_name)); /* Pathname length */
254 	assertEqualInt(i2(p + 30), 13); /* Extra field length */
255 	assertEqualInt(i2(p + 32), 0); /* File comment length */
256 	assertEqualInt(i2(p + 34), 0); /* Disk number start */
257 	assertEqualInt(i2(p + 36), 0); /* Internal file attrs */
258 	assertEqualInt(i4(p + 38) >> 16 & 01777, folder_perm); /* External file attrs */
259 	assertEqualInt(i4(p + 42), q - buff); /* Offset of local header */
260 	assertEqualMem(p + 46, folder_name, strlen(folder_name)); /* Pathname */
261 	p = p + 46 + strlen(folder_name);
262 	assertEqualInt(i2(p), 0x5455); /* 'UT' extension header */
263 	assertEqualInt(i2(p + 2), 5); /* 'UT' size */
264 	assertEqualInt(p[4], 7); /* 'UT' flags */
265 	assertEqualInt(i4(p + 5), t); /* 'UT' mtime */
266 	p = p + 9;
267 	assertEqualInt(i2(p), 0x7875); /* 'ux' extension header */
268 	assertEqualInt(i2(p + 2), 0); /* 'ux' size */
269 	/*p = p + 4;*/
270 
271 	/* Verify local header of folder entry. */
272 	assertEqualMem(q, "PK\003\004", 4); /* Signature */
273 	assertEqualInt(i2(q + 4), 20); /* Version needed to extract */
274 	assertEqualInt(i2(q + 6), 8); /* Flags */
275 	assertEqualInt(i2(q + 8), 0); /* Compression method */
276 	assertEqualInt(i2(q + 10), (tm->tm_hour * 2048) + (tm->tm_min * 32) + (tm->tm_sec / 2)); /* File time */
277 	assertEqualInt(i2(q + 12), ((tm->tm_year - 80) * 512) + ((tm->tm_mon + 1) * 32) + tm->tm_mday); /* File date */
278 	assertEqualInt(i4(q + 14), 0); /* CRC-32 */
279 	assertEqualInt(i4(q + 18), 0); /* Compressed size */
280 	assertEqualInt(i4(q + 22), 0); /* Uncompressed size */
281 	assertEqualInt(i2(q + 26), strlen(folder_name)); /* Pathname length */
282 	assertEqualInt(i2(q + 28), 32); /* Extra field length */
283 	assertEqualMem(q + 30, folder_name, strlen(folder_name)); /* Pathname */
284 	q = q + 30 + strlen(folder_name);
285 	assertEqualInt(i2(q), 0x5455); /* 'UT' extension header */
286 	assertEqualInt(i2(q + 2), 13); /* 'UT' size */
287 	assertEqualInt(q[4], 7); /* 'UT' flags */
288 	assertEqualInt(i4(q + 5), t); /* 'UT' mtime */
289 	assertEqualInt(i4(q + 9), t); /* 'UT' atime */
290 	assertEqualInt(i4(q + 13), t); /* 'UT' ctime */
291 	q = q + 17;
292 	assertEqualInt(i2(q), 0x7875); /* 'ux' extension header */
293 	assertEqualInt(i2(q + 2), 11); /* 'ux' size */
294 	assertEqualInt(q[4], 1); /* 'ux' version */
295 	assertEqualInt(q[5], 4); /* 'ux' uid size */
296 	assertEqualInt(i4(q + 6), folder_uid); /* 'ux' UID */
297 	assertEqualInt(q[10], 4); /* 'ux' gid size */
298 	assertEqualInt(i4(q + 11), folder_gid); /* 'ux' GID */
299 	q = q + 15;
300 
301 	/* There should not be any data in the folder entry,
302 	 * meaning next is the data descriptor header. */
303 
304 	/* Verify data descriptor of folder entry. */
305 	assertEqualMem(q, "PK\007\010", 4); /* Signature */
306 	assertEqualInt(i4(q + 4), crc); /* CRC-32 */
307 	assertEqualInt(i4(q + 8), 0); /* Compressed size */
308 	assertEqualInt(i4(q + 12), 0); /* Uncompressed size */
309 	/*q = q + 16;*/
310 }
311