1 /*-
2 * Copyright (c) 2021 Jia Cheong Tan
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 * in this position and unchanged.
11 * 2. Redistributions in binary form must reproduce the above copyright
12 * notice, this list of conditions and the following disclaimer in the
13 * documentation and/or other materials provided with the distribution.
14 *
15 * THIS SOFTWARE IS PROVIDED BY THE AUTHOR(S) ``AS IS'' AND ANY EXPRESS OR
16 * IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE IMPLIED WARRANTIES
17 * OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE ARE DISCLAIMED.
18 * IN NO EVENT SHALL THE AUTHOR(S) BE LIABLE FOR ANY DIRECT, INDIRECT,
19 * INCIDENTAL, SPECIAL, EXEMPLARY, OR CONSEQUENTIAL DAMAGES (INCLUDING, BUT
20 * NOT LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS OR SERVICES; LOSS OF USE,
21 * DATA, OR PROFITS; OR BUSINESS INTERRUPTION) HOWEVER CAUSED AND ON ANY
22 * THEORY OF LIABILITY, WHETHER IN CONTRACT, STRICT LIABILITY, OR TORT
23 * (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY OUT OF THE USE OF
24 * THIS SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF SUCH DAMAGE.
25 */
26
27 #include "test.h"
28 __FBSDID("$FreeBSD$");
29
30 /* File data */
31 static const char file_name[] = "file";
32 static const char file_data1[] = {'a', 'b', 'c', 'd', 'e'};
33 static const char file_data2[] = {'f', 'g', 'h', 'i', 'j'};
34 static const int file_perm = 00644;
35 static const short file_uid = 10;
36 static const short file_gid = 20;
37
38 /* Folder data */
39 static const char folder_name[] = "folder/";
40 static const int folder_perm = 00755;
41 static const short folder_uid = 30;
42 static const short folder_gid = 40;
43
44 #define ZIP_ENTRY_FLAG_LENGTH_AT_END (1 << 3)
45
46 /* Quick and dirty: Read 2-byte and 4-byte integers from Zip file. */
i2(const char * p)47 static unsigned i2(const char *p) { return ((p[0] & 0xff) | ((p[1] & 0xff) << 8)); }
i4(const char * p)48 static unsigned i4(const char *p) { return (i2(p) | (i2(p + 2) << 16)); }
49
50 static unsigned long
bitcrc32(unsigned long c,const void * _p,size_t s)51 bitcrc32(unsigned long c, const void *_p, size_t s)
52 {
53 /* This is a drop-in replacement for crc32() from zlib.
54 * Libarchive should be able to correctly generate
55 * uncompressed zip archives (including correct CRCs) even
56 * when zlib is unavailable, and this function helps us verify
57 * that. Yes, this is very, very slow and unsuitable for
58 * production use, but it's correct, compact, and works well
59 * enough for this particular usage. Libarchive internally
60 * uses a much more efficient implementation. */
61 const unsigned char *p = _p;
62 int bitctr;
63
64 if (p == NULL)
65 return (0);
66
67 for (; s > 0; --s)
68 {
69 c ^= *p++;
70 for (bitctr = 8; bitctr > 0; --bitctr)
71 {
72 if (c & 1)
73 c = (c >> 1);
74 else
75 c = (c >> 1) ^ 0xedb88320;
76 c ^= 0x80000000;
77 }
78 }
79 return (c);
80 }
81
write_archive(struct archive * a)82 static void write_archive(struct archive *a)
83 {
84 struct archive_entry *entry = archive_entry_new();
85 assert(entry != NULL);
86
87 /* Does not set size for file entry */
88 archive_entry_set_pathname(entry, file_name);
89 archive_entry_set_mode(entry, S_IFREG | 0644);
90 archive_entry_set_uid(entry, file_uid);
91 archive_entry_set_gid(entry, file_gid);
92 assertEqualIntA(a, ARCHIVE_OK, 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 assertEqualIntA(a, ARCHIVE_OK, archive_write_header(a, entry));
105 archive_entry_free(entry);
106 }
107
verify_contents(const char * zip_buff,size_t size)108 static void verify_contents(const char *zip_buff, size_t size)
109 {
110 unsigned long crc = bitcrc32(0, file_data1, sizeof(file_data1));
111 crc = bitcrc32(crc, file_data2, sizeof(file_data2));
112
113 const char *zip_end = zip_buff + size;
114 /* Since there are no comments, the end of central directory
115 * is 22 bytes from the end of content */
116 const char *end_of_central_dir = zip_end - 22;
117 /* Check for end of central directory signature */
118 assertEqualMem(end_of_central_dir, "PK\x5\x6", 4);
119 /* Check for number of disk */
120 assertEqualInt(i2(end_of_central_dir + 4), 0);
121 /* Check for disk where central directory starts */
122 assertEqualInt(i2(end_of_central_dir + 6), 0);
123 /* Check for number of central directory records on disk */
124 assertEqualInt(i2(end_of_central_dir + 8), 2);
125 /* Check for total number of central directory records */
126 assertEqualInt(i2(end_of_central_dir + 10), 2);
127 /* Check for size of central directory and offset
128 * The size + offset must equal the end of the central directory */
129 assertEqualInt(i4(end_of_central_dir + 12) + i4(end_of_central_dir + 16), end_of_central_dir - zip_buff);
130 /* Check for empty comment length */
131 assertEqualInt(i2(end_of_central_dir + 20), 0);
132
133 /* Get address of central directory */
134 const char *central_directory = zip_buff + i4(end_of_central_dir + 16);
135
136 /* Check for entry in central directory signature */
137 assertEqualMem(central_directory, "PK\x1\x2", 4);
138 /* Check for version used to write entry */
139 assertEqualInt(i2(central_directory + 4), 3 * 256 + 10);
140 /* Check for version needed to extract entry */
141 assertEqualInt(i2(central_directory + 6), 10);
142 /* Check flags */
143 assertEqualInt(i2(central_directory + 8), ZIP_ENTRY_FLAG_LENGTH_AT_END);
144 /* Check compression method */
145 assertEqualInt(i2(central_directory + 10), 0);
146 /* Check crc value */
147 assertEqualInt(i4(central_directory + 16), crc);
148 /* Check compressed size*/
149 assertEqualInt(i4(central_directory + 20), sizeof(file_data1) + sizeof(file_data2));
150 /* Check uncompressed size */
151 assertEqualInt(i4(central_directory + 24), sizeof(file_data1) + sizeof(file_data2));
152 /* Check file name length */
153 assertEqualInt(i2(central_directory + 28), strlen(file_name));
154 /* Check extra field length */
155 assertEqualInt(i2(central_directory + 30), 20);
156 /* Check file comment length */
157 assertEqualInt(i2(central_directory + 32), 0);
158 /* Check disk number where file starts */
159 assertEqualInt(i2(central_directory + 34), 0);
160 /* Check internal file attrs */
161 assertEqualInt(i2(central_directory + 36), 0);
162 /* Check external file attrs */
163 assertEqualInt(i4(central_directory + 38) >> 16 & 01777, file_perm);
164 /* Check offset of local header */
165 assertEqualInt(i4(central_directory + 42), 0);
166 /* Check for file name contents */
167 assertEqualMem(central_directory + 46, file_name, strlen(file_name));
168
169 /* Get address of local file entry */
170 const char *local_file_header = zip_buff;
171
172 /* Check local file header signature */
173 assertEqualMem(local_file_header, "PK\x3\x4", 4);
174 /* Check version needed to extract */
175 assertEqualInt(i2(local_file_header + 4), 10);
176 /* Check flags */
177 assertEqualInt(i2(local_file_header + 6), 8);
178 /* Check compression method */
179 assertEqualInt(i2(local_file_header + 8), 0);
180 /* Check crc */
181 assertEqualInt(i4(local_file_header + 14), 0);
182 /* Check compressed size
183 * 0 because it was unknown at time of writing */
184 assertEqualInt(i4(local_file_header + 18), 0);
185 /* Check uncompressed size
186 * 0 because it was unknown at time of writing */
187 assertEqualInt(i4(local_file_header + 22), 0);
188 /* Check pathname length */
189 assertEqualInt(i2(local_file_header + 26), strlen(file_name));
190 /* Check extra field length */
191 assertEqualInt(i2(local_file_header + 28), 20);
192 /* Check path name match */
193 assertEqualMem(local_file_header + 30, file_name, strlen(file_name));
194
195 /* Start of data */
196 const char *data = local_file_header + i2(local_file_header + 28) + strlen(file_name) + 30;
197 /* Check for file data match */
198 assertEqualMem(data, file_data1, sizeof(file_data1));
199 assertEqualMem(data + sizeof(file_data1), file_data2, sizeof(file_data2));
200
201 /* Start of data descriptor */
202 const char *data_descriptor = data + sizeof(file_data1) + sizeof(file_data2);
203 /* Check data descriptor signature */
204 assertEqualMem(data_descriptor, "PK\x7\x8", 4);
205 /* Check crc value */
206 assertEqualInt(i4(data_descriptor + 4), crc);
207 /* Check compressed size */
208 assertEqualInt(i4(data_descriptor + 8), sizeof(file_data1) + sizeof(file_data2));
209 /* Chcek uncompresed size */
210 assertEqualInt(i4(data_descriptor + 12), sizeof(file_data1) + sizeof(file_data2));
211
212 /* Get folder entry in central directory */
213 const char *central_directory_folder_entry = central_directory + 46 + 20 + strlen(file_name);
214
215 /* Get start of folder entry */
216 const char *local_folder_header = data_descriptor + 16;
217
218 /* Check for entry in central directory signature */
219 assertEqualMem(central_directory_folder_entry, "PK\x1\x2", 4);
220 /* Check version made by */
221 assertEqualInt(i2(central_directory_folder_entry + 4), 3 * 256 + 20);
222 /* Check version needed to extract */
223 assertEqualInt(i2(central_directory_folder_entry + 6), 20);
224 /* Check flags */
225 assertEqualInt(i2(central_directory_folder_entry + 8), 0);
226 /* Check compression method */
227 assertEqualInt(i2(central_directory_folder_entry + 10), 0);
228 /* Check crc */
229 assertEqualInt(i2(central_directory_folder_entry + 16), 0);
230 /* Check compressed size */
231 assertEqualInt(i4(central_directory_folder_entry + 20), 0);
232 /* Check uncompressed size */
233 assertEqualInt(i4(central_directory_folder_entry + 24), 0);
234 /* Check path name length */
235 assertEqualInt(i2(central_directory_folder_entry + 28), strlen(folder_name));
236 /* Check extra field length */
237 assertEqualInt(i2(central_directory_folder_entry + 30), 20);
238 /* Check file comment length */
239 assertEqualInt(i2(central_directory_folder_entry + 32), 0);
240 /* Check disk number start */
241 assertEqualInt(i2(central_directory_folder_entry + 34), 0);
242 /* Check internal file attrs */
243 assertEqualInt(i2(central_directory_folder_entry + 36), 0);
244 /* Check external file attrs */
245 assertEqualInt(i4(central_directory_folder_entry + 38) >> 16 & 01777, folder_perm);
246 /* Check offset of local header*/
247 assertEqualInt(i4(central_directory_folder_entry + 42), local_folder_header - zip_buff);
248 /* Check path name */
249 assertEqualMem(central_directory_folder_entry + 46, folder_name, strlen(folder_name));
250
251 /* Check local header */
252 assertEqualMem(local_folder_header, "PK\x3\x4", 4);
253 /* Check version to extract */
254 assertEqualInt(i2(local_folder_header + 4), 20);
255 /* Check flags */
256 assertEqualInt(i2(local_folder_header + 6), 0);
257 /* Check compression method */
258 assertEqualInt(i2(local_folder_header + 8), 0);
259 /* Check crc */
260 assertEqualInt(i4(local_folder_header + 14), 0);
261 /* Check compressed size */
262 assertEqualInt(i2(local_folder_header + 18), 0);
263 /* Check uncompressed size */
264 assertEqualInt(i4(local_folder_header + 22), 0);
265 /* Check path name length */
266 assertEqualInt(i2(local_folder_header + 26), strlen(folder_name));
267 /* Check extra field length */
268 assertEqualInt(i2(local_folder_header + 28), 20);
269 /* Check path name */
270 assertEqualMem(local_folder_header + 30, folder_name, strlen(folder_name));
271
272 const char *post_local_folder = local_folder_header + 30 + strlen(folder_name) + 20;
273 assertEqualMem(post_local_folder, central_directory, 4);
274 }
275
DEFINE_TEST(test_write_format_zip_size_unset)276 DEFINE_TEST(test_write_format_zip_size_unset)
277 {
278 struct archive *a;
279 char zip_buffer[100000];
280 size_t size;
281
282 /* Use compression=store to disable compression. */
283 assert((a = archive_write_new()) != NULL);
284 assertEqualIntA(a, ARCHIVE_OK, archive_write_set_format_zip(a));
285 assertEqualIntA(a, ARCHIVE_OK, archive_write_set_options(a, "zip:compression=store"));
286 /* Disable zip64 explicitly since it is automatically enabled if no size is set */
287 assertEqualIntA(a, ARCHIVE_OK, archive_write_set_options(a, "zip:zip64="));
288 assertEqualIntA(a, ARCHIVE_OK, archive_write_add_filter_none(a));
289 assertEqualIntA(a, ARCHIVE_OK, archive_write_set_bytes_per_block(a, 1));
290 assertEqualIntA(a, ARCHIVE_OK, archive_write_set_bytes_in_last_block(a, 1));
291 assertEqualIntA(a, ARCHIVE_OK, archive_write_open_memory(a, zip_buffer, sizeof(zip_buffer), &size));
292
293 write_archive(a);
294
295 /* Close the archive . */
296 assertEqualIntA(a, ARCHIVE_OK, archive_write_close(a));
297 assertEqualInt(ARCHIVE_OK, archive_write_free(a));
298 dumpfile("constructed_size_unset.zip", zip_buffer, size);
299
300 verify_contents(zip_buffer, size);
301
302 /* Use compression-level=0 to disable compression. */
303 assert((a = archive_write_new()) != NULL);
304 assertEqualIntA(a, ARCHIVE_OK, archive_write_set_format_zip(a));
305 assertEqualIntA(a, ARCHIVE_OK, archive_write_set_options(a, "zip:compression-level=0"));
306 /* Disable zip64 explicitly since it is automatically enabled if no size is set */
307 assertEqualIntA(a, ARCHIVE_OK, archive_write_set_options(a, "zip:zip64="));
308 assertEqualIntA(a, ARCHIVE_OK, archive_write_add_filter_none(a));
309 assertEqualIntA(a, ARCHIVE_OK, archive_write_set_bytes_per_block(a, 1));
310 assertEqualIntA(a, ARCHIVE_OK, archive_write_set_bytes_in_last_block(a, 1));
311 assertEqualIntA(a, ARCHIVE_OK, archive_write_open_memory(a, zip_buffer, sizeof(zip_buffer), &size));
312
313 write_archive(a);
314
315 /* Close the archive . */
316 assertEqualIntA(a, ARCHIVE_OK, archive_write_close(a));
317 assertEqualInt(ARCHIVE_OK, archive_write_free(a));
318 dumpfile("constructed_size_unset.zip", zip_buffer, size);
319
320 verify_contents(zip_buffer, size);
321 }
322