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
29 /* File data */
30 static const char file_name[] = "file";
31 static const char file_data1[] = {'a', 'b', 'c', 'd', 'e'};
32 static const char file_data2[] = {'f', 'g', 'h', 'i', 'j'};
33 static const int file_perm = 00644;
34 static const short file_uid = 10;
35 static const short file_gid = 20;
36
37 /* Folder data */
38 static const char folder_name[] = "folder/";
39 static const int folder_perm = 00755;
40 static const short folder_uid = 30;
41 static const short folder_gid = 40;
42
43 #define ZIP_ENTRY_FLAG_LENGTH_AT_END (1 << 3)
44
write_archive(struct archive * a)45 static void write_archive(struct archive *a)
46 {
47 struct archive_entry *entry = archive_entry_new();
48 assert(entry != NULL);
49
50 /* Does not set size for file entry */
51 archive_entry_set_pathname(entry, file_name);
52 archive_entry_set_mode(entry, S_IFREG | 0644);
53 archive_entry_set_uid(entry, file_uid);
54 archive_entry_set_gid(entry, file_gid);
55 assertEqualIntA(a, ARCHIVE_OK, archive_write_header(a, entry));
56 assertEqualIntA(a, sizeof(file_data1), archive_write_data(a, file_data1, sizeof(file_data1)));
57 assertEqualIntA(a, sizeof(file_data2), archive_write_data(a, file_data2, sizeof(file_data2)));
58 archive_entry_free(entry);
59
60 /* Folder */
61 assert((entry = archive_entry_new()) != NULL);
62 archive_entry_set_pathname(entry, folder_name);
63 archive_entry_set_mode(entry, S_IFDIR | folder_perm);
64 archive_entry_set_size(entry, 0);
65 archive_entry_set_uid(entry, folder_uid);
66 archive_entry_set_gid(entry, folder_gid);
67 assertEqualIntA(a, ARCHIVE_OK, archive_write_header(a, entry));
68 archive_entry_free(entry);
69 }
70
verify_contents(const char * zip_buff,size_t size)71 static void verify_contents(const char *zip_buff, size_t size)
72 {
73 unsigned long crc = bitcrc32(0, file_data1, sizeof(file_data1));
74 crc = bitcrc32(crc, file_data2, sizeof(file_data2));
75
76 const char *zip_end = zip_buff + size;
77 /* Since there are no comments, the end of central directory
78 * is 22 bytes from the end of content */
79 const char *end_of_central_dir = zip_end - 22;
80 /* Check for end of central directory signature */
81 assertEqualMem(end_of_central_dir, "PK\x5\x6", 4);
82 /* Check for number of disk */
83 assertEqualInt(i2le(end_of_central_dir + 4), 0);
84 /* Check for disk where central directory starts */
85 assertEqualInt(i2le(end_of_central_dir + 6), 0);
86 /* Check for number of central directory records on disk */
87 assertEqualInt(i2le(end_of_central_dir + 8), 2);
88 /* Check for total number of central directory records */
89 assertEqualInt(i2le(end_of_central_dir + 10), 2);
90 /* Check for size of central directory and offset
91 * The size + offset must equal the end of the central directory */
92 assertEqualInt(i4le(end_of_central_dir + 12) + i4le(end_of_central_dir + 16), end_of_central_dir - zip_buff);
93 /* Check for empty comment length */
94 assertEqualInt(i2le(end_of_central_dir + 20), 0);
95
96 /* Get address of central directory */
97 const char *central_directory = zip_buff + i4le(end_of_central_dir + 16);
98
99 /* Check for entry in central directory signature */
100 assertEqualMem(central_directory, "PK\x1\x2", 4);
101 /* Check for version used to write entry */
102 assertEqualInt(i2le(central_directory + 4), 3 * 256 + 10);
103 /* Check for version needed to extract entry */
104 assertEqualInt(i2le(central_directory + 6), 10);
105 /* Check flags */
106 assertEqualInt(i2le(central_directory + 8), ZIP_ENTRY_FLAG_LENGTH_AT_END);
107 /* Check compression method */
108 assertEqualInt(i2le(central_directory + 10), 0);
109 /* Check crc value */
110 assertEqualInt(i4le(central_directory + 16), crc);
111 /* Check compressed size*/
112 assertEqualInt(i4le(central_directory + 20), sizeof(file_data1) + sizeof(file_data2));
113 /* Check uncompressed size */
114 assertEqualInt(i4le(central_directory + 24), sizeof(file_data1) + sizeof(file_data2));
115 /* Check file name length */
116 assertEqualInt(i2le(central_directory + 28), strlen(file_name));
117 /* Check extra field length */
118 assertEqualInt(i2le(central_directory + 30), 15);
119 /* Check file comment length */
120 assertEqualInt(i2le(central_directory + 32), 0);
121 /* Check disk number where file starts */
122 assertEqualInt(i2le(central_directory + 34), 0);
123 /* Check internal file attrs */
124 assertEqualInt(i2le(central_directory + 36), 0);
125 /* Check external file attrs */
126 assertEqualInt(i4le(central_directory + 38) >> 16 & 01777, file_perm);
127 /* Check offset of local header */
128 assertEqualInt(i4le(central_directory + 42), 0);
129 /* Check for file name contents */
130 assertEqualMem(central_directory + 46, file_name, strlen(file_name));
131
132 /* Get address of local file entry */
133 const char *local_file_header = zip_buff;
134
135 /* Check local file header signature */
136 assertEqualMem(local_file_header, "PK\x3\x4", 4);
137 /* Check version needed to extract */
138 assertEqualInt(i2le(local_file_header + 4), 10);
139 /* Check flags */
140 assertEqualInt(i2le(local_file_header + 6), 8);
141 /* Check compression method */
142 assertEqualInt(i2le(local_file_header + 8), 0);
143 /* Check crc */
144 assertEqualInt(i4le(local_file_header + 14), 0);
145 /* Check compressed size
146 * 0 because it was unknown at time of writing */
147 assertEqualInt(i4le(local_file_header + 18), 0);
148 /* Check uncompressed size
149 * 0 because it was unknown at time of writing */
150 assertEqualInt(i4le(local_file_header + 22), 0);
151 /* Check pathname length */
152 assertEqualInt(i2le(local_file_header + 26), strlen(file_name));
153 /* Check extra field length */
154 assertEqualInt(i2le(local_file_header + 28), 15);
155 /* Check path name match */
156 assertEqualMem(local_file_header + 30, file_name, strlen(file_name));
157
158 /* Start of data */
159 const char *data = local_file_header + i2le(local_file_header + 28) + strlen(file_name) + 30;
160 /* Check for file data match */
161 assertEqualMem(data, file_data1, sizeof(file_data1));
162 assertEqualMem(data + sizeof(file_data1), file_data2, sizeof(file_data2));
163
164 /* Start of data descriptor */
165 const char *data_descriptor = data + sizeof(file_data1) + sizeof(file_data2);
166 /* Check data descriptor signature */
167 assertEqualMem(data_descriptor, "PK\x7\x8", 4);
168 /* Check crc value */
169 assertEqualInt(i4le(data_descriptor + 4), crc);
170 /* Check compressed size */
171 assertEqualInt(i4le(data_descriptor + 8), sizeof(file_data1) + sizeof(file_data2));
172 /* Check uncompressed size */
173 assertEqualInt(i4le(data_descriptor + 12), sizeof(file_data1) + sizeof(file_data2));
174
175 /* Get folder entry in central directory */
176 const char *central_directory_folder_entry = central_directory + 46 + i2le(local_file_header + 28) + strlen(file_name);
177
178 /* Get start of folder entry */
179 const char *local_folder_header = data_descriptor + 16;
180
181 /* Check for entry in central directory signature */
182 assertEqualMem(central_directory_folder_entry, "PK\x1\x2", 4);
183 /* Check version made by */
184 assertEqualInt(i2le(central_directory_folder_entry + 4), 3 * 256 + 20);
185 /* Check version needed to extract */
186 assertEqualInt(i2le(central_directory_folder_entry + 6), 20);
187 /* Check flags */
188 assertEqualInt(i2le(central_directory_folder_entry + 8), 0);
189 /* Check compression method */
190 assertEqualInt(i2le(central_directory_folder_entry + 10), 0);
191 /* Check crc */
192 assertEqualInt(i2le(central_directory_folder_entry + 16), 0);
193 /* Check compressed size */
194 assertEqualInt(i4le(central_directory_folder_entry + 20), 0);
195 /* Check uncompressed size */
196 assertEqualInt(i4le(central_directory_folder_entry + 24), 0);
197 /* Check path name length */
198 assertEqualInt(i2le(central_directory_folder_entry + 28), strlen(folder_name));
199 /* Check extra field length */
200 assertEqualInt(i2le(central_directory_folder_entry + 30), 15);
201 /* Check file comment length */
202 assertEqualInt(i2le(central_directory_folder_entry + 32), 0);
203 /* Check disk number start */
204 assertEqualInt(i2le(central_directory_folder_entry + 34), 0);
205 /* Check internal file attrs */
206 assertEqualInt(i2le(central_directory_folder_entry + 36), 0);
207 /* Check external file attrs */
208 assertEqualInt(i4le(central_directory_folder_entry + 38) >> 16 & 01777, folder_perm);
209 /* Check offset of local header*/
210 assertEqualInt(i4le(central_directory_folder_entry + 42), local_folder_header - zip_buff);
211 /* Check path name */
212 assertEqualMem(central_directory_folder_entry + 46, folder_name, strlen(folder_name));
213
214 /* Check local header */
215 assertEqualMem(local_folder_header, "PK\x3\x4", 4);
216 /* Check version to extract */
217 assertEqualInt(i2le(local_folder_header + 4), 20);
218 /* Check flags */
219 assertEqualInt(i2le(local_folder_header + 6), 0);
220 /* Check compression method */
221 assertEqualInt(i2le(local_folder_header + 8), 0);
222 /* Check crc */
223 assertEqualInt(i4le(local_folder_header + 14), 0);
224 /* Check compressed size */
225 assertEqualInt(i2le(local_folder_header + 18), 0);
226 /* Check uncompressed size */
227 assertEqualInt(i4le(local_folder_header + 22), 0);
228 /* Check path name length */
229 assertEqualInt(i2le(local_folder_header + 26), strlen(folder_name));
230 /* Check extra field length */
231 assertEqualInt(i2le(local_folder_header + 28), 15);
232 /* Check path name */
233 assertEqualMem(local_folder_header + 30, folder_name, strlen(folder_name));
234
235 const char *post_local_folder = local_folder_header + 30 + i2le(local_folder_header + 28) + strlen(folder_name);
236 assertEqualMem(post_local_folder, central_directory, 4);
237 }
238
DEFINE_TEST(test_write_format_zip_size_unset)239 DEFINE_TEST(test_write_format_zip_size_unset)
240 {
241 struct archive *a;
242 char zip_buffer[100000];
243 size_t size;
244
245 /* Use compression=store to disable compression. */
246 assert((a = archive_write_new()) != NULL);
247 assertEqualIntA(a, ARCHIVE_OK, archive_write_set_format_zip(a));
248 assertEqualIntA(a, ARCHIVE_OK, archive_write_set_options(a, "zip:compression=store"));
249 /* Disable zip64 explicitly since it is automatically enabled if no size is set */
250 assertEqualIntA(a, ARCHIVE_OK, archive_write_set_options(a, "zip:zip64="));
251 assertEqualIntA(a, ARCHIVE_OK, archive_write_add_filter_none(a));
252 assertEqualIntA(a, ARCHIVE_OK, archive_write_set_bytes_per_block(a, 1));
253 assertEqualIntA(a, ARCHIVE_OK, archive_write_set_bytes_in_last_block(a, 1));
254 assertEqualIntA(a, ARCHIVE_OK, archive_write_open_memory(a, zip_buffer, sizeof(zip_buffer), &size));
255
256 write_archive(a);
257
258 /* Close the archive . */
259 assertEqualIntA(a, ARCHIVE_OK, archive_write_close(a));
260 assertEqualInt(ARCHIVE_OK, archive_write_free(a));
261 dumpfile("constructed_size_unset.zip", zip_buffer, size);
262
263 verify_contents(zip_buffer, size);
264
265 /* Use compression-level=0 to disable compression. */
266 assert((a = archive_write_new()) != NULL);
267 assertEqualIntA(a, ARCHIVE_OK, archive_write_set_format_zip(a));
268 assertEqualIntA(a, ARCHIVE_OK, archive_write_set_options(a, "zip:compression-level=0"));
269 /* Disable zip64 explicitly since it is automatically enabled if no size is set */
270 assertEqualIntA(a, ARCHIVE_OK, archive_write_set_options(a, "zip:zip64="));
271 assertEqualIntA(a, ARCHIVE_OK, archive_write_add_filter_none(a));
272 assertEqualIntA(a, ARCHIVE_OK, archive_write_set_bytes_per_block(a, 1));
273 assertEqualIntA(a, ARCHIVE_OK, archive_write_set_bytes_in_last_block(a, 1));
274 assertEqualIntA(a, ARCHIVE_OK, archive_write_open_memory(a, zip_buffer, sizeof(zip_buffer), &size));
275
276 write_archive(a);
277
278 /* Close the archive . */
279 assertEqualIntA(a, ARCHIVE_OK, archive_write_close(a));
280 assertEqualInt(ARCHIVE_OK, archive_write_free(a));
281 dumpfile("constructed_size_unset.zip", zip_buffer, size);
282
283 verify_contents(zip_buffer, size);
284 }
285