2 * Copyright (c) 2010 Michihiro NAKAJIMA
5 * Redistribution and use in source and binary forms, with or without
6 * modification, are permitted provided that the following conditions
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.
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.
29 static const int libz_enabled = 1;
31 static const int libz_enabled = 0;
35 Execute the following command to rebuild the data for this program:
36 tail -n +44 test_read_format_cab.c | /bin/sh
37 And following works are:
38 1. Move /tmp/cab/cab.zip to Windows PC
40 3. Open command prompt and change current directory where you extracted cab.zip
42 5. Then you will see that there is a cabinet file, test.cab
43 6. Move test.cab to posix platform
44 7. Extract test.cab with this version of bsdtar
45 8. Execute the following command to make uuencoded files.
46 uuencode test_read_format_cab_1.cab test_read_format_cab_1.cab > test_read_format_cab_1.cab.uu
47 uuencode test_read_format_cab_2.cab test_read_format_cab_2.cab > test_read_format_cab_2.cab.uu
48 uuencode test_read_format_cab_3.cab test_read_format_cab_3.cab > test_read_format_cab_3.cab.uu
52 # How to make test data.
54 # Temporary directory.
61 # Make contents of a cabinet file.
69 cat > ${base}/dir1/file1 << END
76 cat > ${base}/dir2/file2 << END
86 dd if=/dev/zero of=${base}/zero bs=1 count=33000 > /dev/null 2>&1
88 cab1=test_read_format_cab_1.cab
89 cab2=test_read_format_cab_2.cab
90 cab3=test_read_format_cab_3.cab
93 cat > ${base}/mkcab1 << END
96 .Set InfDate=1980-01-02
98 .Set CabinetName1=${cab1}
100 .Set DestinationDir=dir1
102 .Set DestinationDir=dir2
106 cat > ${base}/mkcab2 << END
107 .Set CompressionType=MSZIP
108 .Set DiskDirectory1=.
109 .Set InfDate=1980-01-02
110 .Set InfTime=00:00:00
111 .Set CabinetName1=${cab2}
114 .Set DestinationDir=dir1
116 .Set DestinationDir=dir2
120 cat > ${base}/mkcab3 << END
121 .Set CompressionType=LZX
122 .Set DiskDirectory1=.
123 .Set InfDate=1980-01-02
124 .Set InfTime=00:00:00
125 .Set CabinetName1=${cab3}
128 .Set DestinationDir=dir1
130 .Set DestinationDir=dir2
134 cat > ${base}/mkcab4 << END
135 .Set CompressionType=MSZIP
136 .Set DiskDirectory1=.
137 .Set CabinetName1=test.cab
143 cat > ${base}/cab.bat << END
144 makecab.exe /F mkcab1
145 makecab.exe /F mkcab2
146 makecab.exe /F mkcab3
147 makecab.exe /F mkcab4
148 del setup.inf setup.rpt
149 del empty zero dir1\file1 dir2\file2 mkcab1 mkcab2 mkcab3 mkcab4
150 del ${cab1} ${cab2} ${cab3}
155 (cd ${base}; zip -q -c $f empty zero dir1/file1 dir2/file2 mkcab1 mkcab2 mkcab3 mkcab4 cab.bat)
160 static const char file1[] = {
166 #define file1_size (sizeof(file1)-1)
167 static const char file2[] = {
176 #define file2_size (sizeof(file2)-1)
184 verify(const char *refname, enum comp_type comp)
186 struct archive_entry *ae;
192 memset(zero, 0, sizeof(zero));
193 extract_reference_file(refname);
194 assert((a = archive_read_new()) != NULL);
195 assertEqualIntA(a, ARCHIVE_OK, archive_read_support_filter_all(a));
196 assertEqualIntA(a, ARCHIVE_OK, archive_read_support_format_all(a));
197 assertEqualIntA(a, ARCHIVE_OK,
198 archive_read_open_filename(a, refname, 10240));
200 /* Verify regular empty. */
201 assertEqualIntA(a, ARCHIVE_OK, archive_read_next_header(a, &ae));
202 assertEqualInt((AE_IFREG | 0666), archive_entry_mode(ae));
203 assertEqualString("empty", archive_entry_pathname(ae));
204 assertEqualInt(0, archive_entry_uid(ae));
205 assertEqualInt(0, archive_entry_gid(ae));
206 assertEqualInt(0, archive_entry_size(ae));
209 /* Verify regular zero.
210 * Maximum CFDATA size is 32768, so we need over 32768 bytes
211 * file to check if we properly handle multiple CFDATA.
213 assertEqualIntA(a, ARCHIVE_OK, archive_read_next_header(a, &ae));
214 assertEqualInt((AE_IFREG | 0666), archive_entry_mode(ae));
215 assertEqualString("zero", archive_entry_pathname(ae));
216 assertEqualInt(0, archive_entry_uid(ae));
217 assertEqualInt(0, archive_entry_gid(ae));
218 assertEqualInt(33000, archive_entry_size(ae));
219 for (s = 0; s + sizeof(buff) < 33000; s+= sizeof(buff)) {
220 ssize_t rsize = archive_read_data(a, buff, sizeof(buff));
221 if (comp == MSZIP && rsize == ARCHIVE_FATAL && !libz_enabled) {
222 skipping("Skipping CAB format(MSZIP) check: %s",
223 archive_error_string(a));
226 assertEqualInt(sizeof(buff), rsize);
227 assertEqualMem(buff, zero, sizeof(buff));
229 assertEqualInt(33000 - s, archive_read_data(a, buff, 33000 - s));
230 assertEqualMem(buff, zero, 33000 - s);
233 /* Verify regular file1. */
234 assertEqualIntA(a, ARCHIVE_OK, archive_read_next_header(a, &ae));
235 assertEqualInt((AE_IFREG | 0666), archive_entry_mode(ae));
236 assertEqualString("dir1/file1", archive_entry_pathname(ae));
237 assertEqualInt(0, archive_entry_uid(ae));
238 assertEqualInt(0, archive_entry_gid(ae));
239 assertEqualInt(file1_size, archive_entry_size(ae));
240 assertEqualInt(file1_size, archive_read_data(a, buff, file1_size));
241 assertEqualMem(buff, file1, file1_size);
243 /* Verify regular file2. */
244 assertEqualIntA(a, ARCHIVE_OK, archive_read_next_header(a, &ae));
245 assertEqualInt((AE_IFREG | 0666), archive_entry_mode(ae));
246 assertEqualString("dir2/file2", archive_entry_pathname(ae));
247 assertEqualInt(0, archive_entry_uid(ae));
248 assertEqualInt(0, archive_entry_gid(ae));
249 assertEqualInt(file2_size, archive_entry_size(ae));
250 assertEqualInt(file2_size, archive_read_data(a, buff, file2_size));
251 assertEqualMem(buff, file2, file2_size);
253 /* End of archive. */
254 assertEqualIntA(a, ARCHIVE_EOF, archive_read_next_header(a, &ae));
257 assertEqualInt(4, archive_file_count(a));
259 assertEqualInt(3, archive_file_count(a));
262 /* Verify archive format. */
263 assertEqualIntA(a, ARCHIVE_FILTER_NONE, archive_filter_code(a, 0));
264 assertEqualIntA(a, ARCHIVE_FORMAT_CAB, archive_format(a));
266 /* Close the archive. */
268 assertEqualInt(ARCHIVE_OK, archive_read_close(a));
269 assertEqualInt(ARCHIVE_OK, archive_read_free(a));
273 * Skip beginning files and Read the last file.
276 verify2(const char *refname, enum comp_type comp)
278 struct archive_entry *ae;
283 if (comp == MSZIP && !libz_enabled) {
284 skipping("Skipping CAB format(MSZIP) check for %s",
288 memset(zero, 0, sizeof(zero));
289 extract_reference_file(refname);
290 assert((a = archive_read_new()) != NULL);
291 assertEqualIntA(a, ARCHIVE_OK, archive_read_support_filter_all(a));
292 assertEqualIntA(a, ARCHIVE_OK, archive_read_support_format_all(a));
293 assertEqualIntA(a, ARCHIVE_OK,
294 archive_read_open_filename(a, refname, 10240));
296 /* Verify regular empty. */
297 assertEqualIntA(a, ARCHIVE_OK, archive_read_next_header(a, &ae));
299 assertEqualIntA(a, ARCHIVE_OK, archive_read_next_header(a, &ae));
301 /* Verify regular file1. */
302 assertEqualIntA(a, ARCHIVE_OK, archive_read_next_header(a, &ae));
304 /* Verify regular file2. */
305 assertEqualIntA(a, ARCHIVE_OK, archive_read_next_header(a, &ae));
306 assertEqualInt((AE_IFREG | 0666), archive_entry_mode(ae));
307 assertEqualString("dir2/file2", archive_entry_pathname(ae));
308 assertEqualInt(0, archive_entry_uid(ae));
309 assertEqualInt(0, archive_entry_gid(ae));
310 assertEqualInt(file2_size, archive_entry_size(ae));
311 assertEqualInt(file2_size, archive_read_data(a, buff, file2_size));
312 assertEqualMem(buff, file2, file2_size);
314 /* End of archive. */
315 assertEqualIntA(a, ARCHIVE_EOF, archive_read_next_header(a, &ae));
318 assertEqualInt(4, archive_file_count(a));
320 assertEqualInt(3, archive_file_count(a));
323 /* Verify archive format. */
324 assertEqualIntA(a, ARCHIVE_FILTER_NONE, archive_filter_code(a, 0));
325 assertEqualIntA(a, ARCHIVE_FORMAT_CAB, archive_format(a));
327 /* Close the archive. */
328 assertEqualInt(ARCHIVE_OK, archive_read_close(a));
329 assertEqualInt(ARCHIVE_OK, archive_read_free(a));
333 * Skip all file like 'bsdtar tvf foo.cab'.
336 verify3(const char *refname, enum comp_type comp)
338 struct archive_entry *ae;
342 memset(zero, 0, sizeof(zero));
343 extract_reference_file(refname);
344 assert((a = archive_read_new()) != NULL);
345 assertEqualIntA(a, ARCHIVE_OK, archive_read_support_filter_all(a));
346 assertEqualIntA(a, ARCHIVE_OK, archive_read_support_format_all(a));
347 assertEqualIntA(a, ARCHIVE_OK,
348 archive_read_open_filename(a, refname, 10240));
350 /* Verify regular empty. */
351 assertEqualIntA(a, ARCHIVE_OK, archive_read_next_header(a, &ae));
353 assertEqualIntA(a, ARCHIVE_OK, archive_read_next_header(a, &ae));
355 /* Verify regular file1. */
356 assertEqualIntA(a, ARCHIVE_OK, archive_read_next_header(a, &ae));
358 /* Verify regular file2. */
359 assertEqualIntA(a, ARCHIVE_OK, archive_read_next_header(a, &ae));
361 /* End of archive. */
362 assertEqualIntA(a, ARCHIVE_EOF, archive_read_next_header(a, &ae));
365 assertEqualInt(4, archive_file_count(a));
367 assertEqualInt(3, archive_file_count(a));
370 /* Verify archive format. */
371 assertEqualIntA(a, ARCHIVE_FILTER_NONE, archive_filter_code(a, 0));
372 assertEqualIntA(a, ARCHIVE_FORMAT_CAB, archive_format(a));
374 /* Close the archive. */
375 assertEqualInt(ARCHIVE_OK, archive_read_close(a));
376 assertEqualInt(ARCHIVE_OK, archive_read_free(a));
379 DEFINE_TEST(test_read_format_cab)
381 /* Verify Cabinet file in no compression. */
382 verify("test_read_format_cab_1.cab", STORE);
383 verify2("test_read_format_cab_1.cab", STORE);
384 verify3("test_read_format_cab_1.cab", STORE);
385 /* Verify Cabinet file in MSZIP. */
386 verify("test_read_format_cab_2.cab", MSZIP);
387 verify2("test_read_format_cab_2.cab", MSZIP);
388 verify3("test_read_format_cab_2.cab", MSZIP);
389 /* Verify Cabinet file in LZX. */
390 verify("test_read_format_cab_3.cab", LZX);
391 verify2("test_read_format_cab_3.cab", LZX);
392 verify3("test_read_format_cab_3.cab", LZX);