summaryrefslogtreecommitdiffstats
path: root/contrib/libarchive/libarchive/test/test_read_format_cab.c
blob: e09d46d238358d11c98ca535578b34a92165e15d (plain)
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
307
308
309
310
311
312
313
314
315
316
317
318
319
320
321
322
323
324
325
326
327
328
329
330
331
332
333
334
335
336
337
338
339
340
341
342
343
344
345
346
347
348
349
350
351
352
353
354
355
356
357
358
359
360
361
362
363
364
365
366
367
368
369
370
371
372
373
374
375
376
377
378
379
380
381
382
383
384
385
386
387
388
389
390
391
392
393
394
/*-
 * Copyright (c) 2010 Michihiro NAKAJIMA
 * All rights reserved.
 *
 * Redistribution and use in source and binary forms, with or without
 * modification, are permitted provided that the following conditions
 * are met:
 * 1. Redistributions of source code must retain the above copyright
 *    notice, this list of conditions and the following disclaimer.
 * 2. Redistributions in binary form must reproduce the above copyright
 *    notice, this list of conditions and the following disclaimer in the
 *    documentation and/or other materials provided with the distribution.
 *
 * THIS SOFTWARE IS PROVIDED BY THE AUTHOR(S) ``AS IS'' AND ANY EXPRESS OR
 * IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE IMPLIED WARRANTIES
 * OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE ARE DISCLAIMED.
 * IN NO EVENT SHALL THE AUTHOR(S) BE LIABLE FOR ANY DIRECT, INDIRECT,
 * INCIDENTAL, SPECIAL, EXEMPLARY, OR CONSEQUENTIAL DAMAGES (INCLUDING, BUT
 * NOT LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS OR SERVICES; LOSS OF USE,
 * DATA, OR PROFITS; OR BUSINESS INTERRUPTION) HOWEVER CAUSED AND ON ANY
 * THEORY OF LIABILITY, WHETHER IN CONTRACT, STRICT LIABILITY, OR TORT
 * (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY OUT OF THE USE OF
 * THIS SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF SUCH DAMAGE.
 */
#include "test.h"
__FBSDID("$FreeBSD");

#ifdef HAVE_LIBZ
static const int libz_enabled = 1;
#else
static const int libz_enabled = 0;
#endif

/*
Execute the following command to rebuild the data for this program:
   tail -n +44 test_read_format_cab.c | /bin/sh
And following works are:
1. Move /tmp/cab/cab.zip to Windows PC
2. Extract cab.zip
3. Open command prompt and change current directory where you extracted cab.zip
4. Execute cab.bat
5. Then you will see that there is a cabinet file, test.cab
6. Move test.cab to posix platform
7. Extract test.cab with this version of bsdtar
8. Execute the following command to make uuencoded files.
 uuencode test_read_format_cab_1.cab test_read_format_cab_1.cab > test_read_format_cab_1.cab.uu
 uuencode test_read_format_cab_2.cab test_read_format_cab_2.cab > test_read_format_cab_2.cab.uu
 uuencode test_read_format_cab_3.cab test_read_format_cab_3.cab > test_read_format_cab_3.cab.uu

#!/bin/sh
#
# How to make test data.
#
# Temporary directory.
base=/tmp/cab
# Owner id
owner=1001
# Group id
group=1001
#
# Make contents of a cabinet file.
#
rm -rf ${base}
mkdir ${base}
mkdir ${base}/dir1
mkdir ${base}/dir2
#
touch ${base}/empty
cat > ${base}/dir1/file1 << END
                          file 1 contents
hello
hello
hello
END
#
cat > ${base}/dir2/file2 << END
                          file 2 contents
hello
hello
hello
hello
hello
hello
END
#
dd if=/dev/zero of=${base}/zero bs=1 count=33000 > /dev/null 2>&1
#
cab1=test_read_format_cab_1.cab
cab2=test_read_format_cab_2.cab
cab3=test_read_format_cab_3.cab
#
#
cat > ${base}/mkcab1 << END
.Set Compress=OFF
.Set DiskDirectory1=.
.Set InfDate=1980-01-02
.Set InfTime=00:00:00
.Set CabinetName1=${cab1}
empty
.Set DestinationDir=dir1
dir1/file1
.Set DestinationDir=dir2
dir2/file2
END
#
cat > ${base}/mkcab2 << END
.Set CompressionType=MSZIP
.Set DiskDirectory1=.
.Set InfDate=1980-01-02
.Set InfTime=00:00:00
.Set CabinetName1=${cab2}
empty
zero
.Set DestinationDir=dir1
dir1/file1
.Set DestinationDir=dir2
dir2/file2
END
#
cat > ${base}/mkcab3 << END
.Set CompressionType=LZX
.Set DiskDirectory1=.
.Set InfDate=1980-01-02
.Set InfTime=00:00:00
.Set CabinetName1=${cab3}
empty
zero
.Set DestinationDir=dir1
dir1/file1
.Set DestinationDir=dir2
dir2/file2
END
#
cat > ${base}/mkcab4 << END
.Set CompressionType=MSZIP
.Set DiskDirectory1=.
.Set CabinetName1=test.cab
${cab1}
${cab2}
${cab3}
END
#
cat > ${base}/cab.bat << END
makecab.exe /F mkcab1
makecab.exe /F mkcab2
makecab.exe /F mkcab3
makecab.exe /F mkcab4
del setup.inf setup.rpt
del empty zero dir1\file1 dir2\file2 mkcab1 mkcab2 mkcab3 mkcab4
del ${cab1} ${cab2} ${cab3}
rmdir dir1 dir2
END
#
f=cab.zip
(cd ${base}; zip -q -c $f empty zero dir1/file1 dir2/file2 mkcab1 mkcab2 mkcab3 mkcab4 cab.bat)
#
exit 1
*/

static const char file1[] = {
"                          file 1 contents\n"
"hello\n"
"hello\n"
"hello\n"
};
#define file1_size (sizeof(file1)-1)
static const char file2[] = {
"                          file 2 contents\n"
"hello\n"
"hello\n"
"hello\n"
"hello\n"
"hello\n"
"hello\n"
};
#define file2_size (sizeof(file2)-1)

enum comp_type {
	STORE = 0,
	MSZIP,
	LZX
};
static void
verify(const char *refname, enum comp_type comp)
{
	struct archive_entry *ae;
	struct archive *a;
	char buff[128];
	char zero[128];
	size_t s;

	memset(zero, 0, sizeof(zero));
	extract_reference_file(refname);
	assert((a = archive_read_new()) != NULL);
	assertEqualIntA(a, ARCHIVE_OK, archive_read_support_filter_all(a));
	assertEqualIntA(a, ARCHIVE_OK, archive_read_support_format_all(a));
	assertEqualIntA(a, ARCHIVE_OK,
	    archive_read_open_filename(a, refname, 10240));

	/* Verify regular empty. */
	assertEqualIntA(a, ARCHIVE_OK, archive_read_next_header(a, &ae));
	assertEqualInt((AE_IFREG | 0666), archive_entry_mode(ae));
	assertEqualString("empty", archive_entry_pathname(ae));
	assertEqualInt(0, archive_entry_uid(ae));
	assertEqualInt(0, archive_entry_gid(ae));
	assertEqualInt(0, archive_entry_size(ae));

	if (comp != STORE) {
		/* Verify regular zero.
		 * Maximum CFDATA size is 32768, so we need over 32768 bytes
		 * file to check if we properly handle multiple CFDATA.
		 */
		assertEqualIntA(a, ARCHIVE_OK, archive_read_next_header(a, &ae));
		assertEqualInt((AE_IFREG | 0666), archive_entry_mode(ae));
		assertEqualString("zero", archive_entry_pathname(ae));
		assertEqualInt(0, archive_entry_uid(ae));
		assertEqualInt(0, archive_entry_gid(ae));
		assertEqualInt(33000, archive_entry_size(ae));
		for (s = 0; s + sizeof(buff) < 33000; s+= sizeof(buff)) {
			ssize_t rsize = archive_read_data(a, buff, sizeof(buff));
			if (comp == MSZIP && rsize == ARCHIVE_FATAL && !libz_enabled) {
				skipping("Skipping CAB format(MSZIP) check: %s",
				    archive_error_string(a));
				goto finish;
			}
			assertEqualInt(sizeof(buff), rsize);
			assertEqualMem(buff, zero, sizeof(buff));
		}
		assertEqualInt(33000 - s, archive_read_data(a, buff, 33000 - s));
		assertEqualMem(buff, zero, 33000 - s);
	}

	/* Verify regular file1. */
	assertEqualIntA(a, ARCHIVE_OK, archive_read_next_header(a, &ae));
	assertEqualInt((AE_IFREG | 0666), archive_entry_mode(ae));
	assertEqualString("dir1/file1", archive_entry_pathname(ae));
	assertEqualInt(0, archive_entry_uid(ae));
	assertEqualInt(0, archive_entry_gid(ae));
	assertEqualInt(file1_size, archive_entry_size(ae));
	assertEqualInt(file1_size, archive_read_data(a, buff, file1_size));
	assertEqualMem(buff, file1, file1_size);

	/* Verify regular file2. */
	assertEqualIntA(a, ARCHIVE_OK, archive_read_next_header(a, &ae));
	assertEqualInt((AE_IFREG | 0666), archive_entry_mode(ae));
	assertEqualString("dir2/file2", archive_entry_pathname(ae));
	assertEqualInt(0, archive_entry_uid(ae));
	assertEqualInt(0, archive_entry_gid(ae));
	assertEqualInt(file2_size, archive_entry_size(ae));
	assertEqualInt(file2_size, archive_read_data(a, buff, file2_size));
	assertEqualMem(buff, file2, file2_size);

	/* End of archive. */
	assertEqualIntA(a, ARCHIVE_EOF, archive_read_next_header(a, &ae));

	if (comp != STORE) {
		assertEqualInt(4, archive_file_count(a));
	} else {
		assertEqualInt(3, archive_file_count(a));
	}

	/* Verify archive format. */
	assertEqualIntA(a, ARCHIVE_FILTER_NONE, archive_filter_code(a, 0));
	assertEqualIntA(a, ARCHIVE_FORMAT_CAB, archive_format(a));

	/* Close the archive. */
finish:
	assertEqualInt(ARCHIVE_OK, archive_read_close(a));
	assertEqualInt(ARCHIVE_OK, archive_read_free(a));
}

/*
 * Skip beginning files and Read the last file.
 */
static void
verify2(const char *refname, enum comp_type comp)
{
	struct archive_entry *ae;
	struct archive *a;
	char buff[128];
	char zero[128];

	if (comp == MSZIP && !libz_enabled) {
		skipping("Skipping CAB format(MSZIP) check for %s",
		  refname);
		return;
	}
	memset(zero, 0, sizeof(zero));
	extract_reference_file(refname);
	assert((a = archive_read_new()) != NULL);
	assertEqualIntA(a, ARCHIVE_OK, archive_read_support_filter_all(a));
	assertEqualIntA(a, ARCHIVE_OK, archive_read_support_format_all(a));
	assertEqualIntA(a, ARCHIVE_OK,
	    archive_read_open_filename(a, refname, 10240));

	/* Verify regular empty. */
	assertEqualIntA(a, ARCHIVE_OK, archive_read_next_header(a, &ae));
	if (comp != STORE) {
		assertEqualIntA(a, ARCHIVE_OK, archive_read_next_header(a, &ae));
	}
	/* Verify regular file1. */
	assertEqualIntA(a, ARCHIVE_OK, archive_read_next_header(a, &ae));

	/* Verify regular file2. */
	assertEqualIntA(a, ARCHIVE_OK, archive_read_next_header(a, &ae));
	assertEqualInt((AE_IFREG | 0666), archive_entry_mode(ae));
	assertEqualString("dir2/file2", archive_entry_pathname(ae));
	assertEqualInt(0, archive_entry_uid(ae));
	assertEqualInt(0, archive_entry_gid(ae));
	assertEqualInt(file2_size, archive_entry_size(ae));
	assertEqualInt(file2_size, archive_read_data(a, buff, file2_size));
	assertEqualMem(buff, file2, file2_size);

	/* End of archive. */
	assertEqualIntA(a, ARCHIVE_EOF, archive_read_next_header(a, &ae));

	if (comp != STORE) {
		assertEqualInt(4, archive_file_count(a));
	} else {
		assertEqualInt(3, archive_file_count(a));
	}

	/* Verify archive format. */
	assertEqualIntA(a, ARCHIVE_FILTER_NONE, archive_filter_code(a, 0));
	assertEqualIntA(a, ARCHIVE_FORMAT_CAB, archive_format(a));

	/* Close the archive. */
	assertEqualInt(ARCHIVE_OK, archive_read_close(a));
	assertEqualInt(ARCHIVE_OK, archive_read_free(a));
}

/*
 * Skip all file like 'bsdtar tvf foo.cab'.
 */
static void
verify3(const char *refname, enum comp_type comp)
{
	struct archive_entry *ae;
	struct archive *a;
	char zero[128];

	memset(zero, 0, sizeof(zero));
	extract_reference_file(refname);
	assert((a = archive_read_new()) != NULL);
	assertEqualIntA(a, ARCHIVE_OK, archive_read_support_filter_all(a));
	assertEqualIntA(a, ARCHIVE_OK, archive_read_support_format_all(a));
	assertEqualIntA(a, ARCHIVE_OK,
	    archive_read_open_filename(a, refname, 10240));

	/* Verify regular empty. */
	assertEqualIntA(a, ARCHIVE_OK, archive_read_next_header(a, &ae));
	if (comp != STORE) {
		assertEqualIntA(a, ARCHIVE_OK, archive_read_next_header(a, &ae));
	}
	/* Verify regular file1. */
	assertEqualIntA(a, ARCHIVE_OK, archive_read_next_header(a, &ae));

	/* Verify regular file2. */
	assertEqualIntA(a, ARCHIVE_OK, archive_read_next_header(a, &ae));

	/* End of archive. */
	assertEqualIntA(a, ARCHIVE_EOF, archive_read_next_header(a, &ae));

	if (comp != STORE) {
		assertEqualInt(4, archive_file_count(a));
	} else {
		assertEqualInt(3, archive_file_count(a));
	}

	/* Verify archive format. */
	assertEqualIntA(a, ARCHIVE_FILTER_NONE, archive_filter_code(a, 0));
	assertEqualIntA(a, ARCHIVE_FORMAT_CAB, archive_format(a));

	/* Close the archive. */
	assertEqualInt(ARCHIVE_OK, archive_read_close(a));
	assertEqualInt(ARCHIVE_OK, archive_read_free(a));
}

DEFINE_TEST(test_read_format_cab)
{
	/* Verify Cabinet file in no compression. */
	verify("test_read_format_cab_1.cab", STORE);
	verify2("test_read_format_cab_1.cab", STORE);
	verify3("test_read_format_cab_1.cab", STORE);
	/* Verify Cabinet file in MSZIP. */
	verify("test_read_format_cab_2.cab", MSZIP);
	verify2("test_read_format_cab_2.cab", MSZIP);
	verify3("test_read_format_cab_2.cab", MSZIP);
	/* Verify Cabinet file in LZX. */
	verify("test_read_format_cab_3.cab", LZX);
	verify2("test_read_format_cab_3.cab", LZX);
	verify3("test_read_format_cab_3.cab", LZX);
}

OpenPOWER on IntegriCloud