xref: /freebsd/contrib/libarchive/libarchive/test/test_read_set_format.c (revision 185becb1e1bd2657c156f78aeb52edac05ba5fb5)
1 /*-
2  * Copyright (c) 2003-2007 Tim Kientzle
3  * Copyright (c) 2012 Andres Mejia
4  * Copyright (c) 2011-2012 Michihiro NAKAJIMA
5  * All rights reserved.
6  *
7  * Redistribution and use in source and binary forms, with or without
8  * modification, are permitted provided that the following conditions
9  * are met:
10  * 1. Redistributions of source code must retain the above copyright
11  *    notice, this list of conditions and the following disclaimer.
12  * 2. Redistributions in binary form must reproduce the above copyright
13  *    notice, this list of conditions and the following disclaimer in the
14  *    documentation and/or other materials provided with the distribution.
15  *
16  * THIS SOFTWARE IS PROVIDED BY THE AUTHOR(S) ``AS IS'' AND ANY EXPRESS OR
17  * IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE IMPLIED WARRANTIES
18  * OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE ARE DISCLAIMED.
19  * IN NO EVENT SHALL THE AUTHOR(S) BE LIABLE FOR ANY DIRECT, INDIRECT,
20  * INCIDENTAL, SPECIAL, EXEMPLARY, OR CONSEQUENTIAL DAMAGES (INCLUDING, BUT
21  * NOT LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS OR SERVICES; LOSS OF USE,
22  * DATA, OR PROFITS; OR BUSINESS INTERRUPTION) HOWEVER CAUSED AND ON ANY
23  * THEORY OF LIABILITY, WHETHER IN CONTRACT, STRICT LIABILITY, OR TORT
24  * (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY OUT OF THE USE OF
25  * THIS SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF SUCH DAMAGE.
26  */
27 #include "test.h"
28 
DEFINE_TEST(test_read_set_format)29 DEFINE_TEST(test_read_set_format)
30 {
31   char buff[64];
32   const char reffile[] = "test_read_format_rar.rar";
33   const char test_txt[] = "test text document\r\n";
34   int size = sizeof(test_txt)-1;
35   struct archive_entry *ae;
36   struct archive *a;
37 
38   extract_reference_file(reffile);
39   assert((a = archive_read_new()) != NULL);
40   assertA(0 == archive_read_set_format(a, ARCHIVE_FORMAT_RAR));
41   assertA(0 == archive_read_append_filter(a, ARCHIVE_FILTER_NONE));
42   assertA(0 == archive_read_open_filename(a, reffile, 10240));
43 
44   /* First header. */
45   assertA(0 == archive_read_next_header(a, &ae));
46   assertEqualString("test.txt", archive_entry_pathname(ae));
47   assertA((int)archive_entry_mtime(ae));
48   assertA((int)archive_entry_ctime(ae));
49   assertA((int)archive_entry_atime(ae));
50   assertEqualInt(20, archive_entry_size(ae));
51   assertEqualInt(33188, archive_entry_mode(ae));
52   assertA(size == archive_read_data(a, buff, size));
53   assertEqualMem(buff, test_txt, size);
54 
55   /* Second header. */
56   assertA(0 == archive_read_next_header(a, &ae));
57   assertEqualString("testlink", archive_entry_pathname(ae));
58   assertA((int)archive_entry_mtime(ae));
59   assertA((int)archive_entry_ctime(ae));
60   assertA((int)archive_entry_atime(ae));
61   assertEqualInt(0, archive_entry_size(ae));
62   assertEqualInt(41471, archive_entry_mode(ae));
63   assertEqualString("test.txt", archive_entry_symlink(ae));
64   assertEqualIntA(a, 0, archive_read_data(a, buff, sizeof(buff)));
65 
66   /* Third header. */
67   assertA(0 == archive_read_next_header(a, &ae));
68   assertEqualString("testdir/test.txt", archive_entry_pathname(ae));
69   assertA((int)archive_entry_mtime(ae));
70   assertA((int)archive_entry_ctime(ae));
71   assertA((int)archive_entry_atime(ae));
72   assertEqualInt(20, archive_entry_size(ae));
73   assertEqualInt(33188, archive_entry_mode(ae));
74   assertA(size == archive_read_data(a, buff, size));
75   assertEqualMem(buff, test_txt, size);
76 
77   /* Fourth header. */
78   assertA(0 == archive_read_next_header(a, &ae));
79   assertEqualString("testdir", archive_entry_pathname(ae));
80   assertA((int)archive_entry_mtime(ae));
81   assertA((int)archive_entry_ctime(ae));
82   assertA((int)archive_entry_atime(ae));
83   assertEqualInt(0, archive_entry_size(ae));
84   assertEqualInt(16877, archive_entry_mode(ae));
85 
86   /* Fifth header. */
87   assertA(0 == archive_read_next_header(a, &ae));
88   assertEqualString("testemptydir", archive_entry_pathname(ae));
89   assertA((int)archive_entry_mtime(ae));
90   assertA((int)archive_entry_ctime(ae));
91   assertA((int)archive_entry_atime(ae));
92   assertEqualInt(0, archive_entry_size(ae));
93   assertEqualInt(16877, archive_entry_mode(ae));
94 
95   /* Test EOF */
96   assertA(1 == archive_read_next_header(a, &ae));
97   assertEqualInt(5, archive_file_count(a));
98   assertEqualIntA(a, ARCHIVE_OK, archive_read_close(a));
99   assertEqualInt(ARCHIVE_OK, archive_read_free(a));
100 }
101 
DEFINE_TEST(test_read_set_wrong_format)102 DEFINE_TEST(test_read_set_wrong_format)
103 {
104   const char reffile[] = "test_read_format_zip.zip";
105   struct archive_entry *ae;
106   struct archive *a;
107 
108   extract_reference_file(reffile);
109   assert((a = archive_read_new()) != NULL);
110   assertA(0 == archive_read_set_format(a, ARCHIVE_FORMAT_RAR));
111   assertA(0 == archive_read_append_filter(a, ARCHIVE_FILTER_NONE));
112   assertA(0 == archive_read_open_filename(a, reffile, 10240));
113 
114   /* Check that this actually fails, then close the archive. */
115   assertA(archive_read_next_header(a, &ae) < (ARCHIVE_WARN));
116   assertEqualIntA(a, ARCHIVE_OK, archive_read_close(a));
117   assertEqualInt(ARCHIVE_OK, archive_read_free(a));
118 }
119 
120 static unsigned char archive[] = {
121 31,139,8,0,222,'C','p','C',0,3,211,'c',160,'=','0','0','0','0','7','5','U',
122 0,210,134,230,166,6,200,'4',28,'(',24,26,24,27,155,24,152,24,154,27,155,')',
123 24,24,26,152,154,25,'2','(',152,210,193,'m',12,165,197,'%',137,'E','@',167,
124 148,'d',230,226,'U','G','H',30,234,15,'8','=',10,'F',193,'(',24,5,131,28,
125 0,0,29,172,5,240,0,6,0,0};
126 
DEFINE_TEST(test_read_append_filter)127 DEFINE_TEST(test_read_append_filter)
128 {
129   struct archive_entry *ae;
130   struct archive *a;
131   int r;
132 
133   assert((a = archive_read_new()) != NULL);
134   assertA(0 == archive_read_set_format(a, ARCHIVE_FORMAT_TAR));
135   r = archive_read_append_filter(a, ARCHIVE_FILTER_GZIP);
136   if (r != ARCHIVE_OK && archive_zlib_version() == NULL && !canGzip()) {
137     skipping("gzip tests require zlib or working gzip command");
138     assertEqualInt(ARCHIVE_OK, archive_read_free(a));
139     return;
140   }
141   if (r == ARCHIVE_WARN && canGzip())
142     assertEqualString(archive_error_string(a), "Using external gzip program");
143   else
144     assertEqualIntA(a, ARCHIVE_OK, r);
145   assertEqualInt(ARCHIVE_OK,
146       archive_read_open_memory(a, archive, sizeof(archive)));
147   assertEqualInt(ARCHIVE_OK, archive_read_next_header(a, &ae));
148   assertEqualInt(1, archive_file_count(a));
149   assertEqualInt(archive_filter_code(a, 0), ARCHIVE_COMPRESSION_GZIP);
150   assertEqualInt(archive_format(a), ARCHIVE_FORMAT_TAR_USTAR);
151   assertEqualIntA(a, ARCHIVE_OK, archive_read_close(a));
152   assertEqualInt(ARCHIVE_OK,archive_read_free(a));
153 }
154 
DEFINE_TEST(test_read_append_wrong_filter)155 DEFINE_TEST(test_read_append_wrong_filter)
156 {
157   struct archive_entry *ae;
158   struct archive *a;
159   int r;
160 
161   assert((a = archive_read_new()) != NULL);
162   assertA(0 == archive_read_set_format(a, ARCHIVE_FORMAT_TAR));
163   r = archive_read_append_filter(a, ARCHIVE_FILTER_XZ);
164   if (r == ARCHIVE_WARN && !canXz()) {
165     skipping("xz reading not fully supported on this platform");
166     assertEqualInt(ARCHIVE_OK, archive_read_free(a));
167     return;
168   }
169   assertEqualInt(ARCHIVE_OK,
170       archive_read_open_memory(a, archive, sizeof(archive)));
171   assertA(archive_read_next_header(a, &ae) < (ARCHIVE_WARN));
172   if (r == ARCHIVE_WARN && canXz()) {
173     assertEqualIntA(a, ARCHIVE_WARN, archive_read_close(a));
174   } else {
175     assertEqualIntA(a, ARCHIVE_OK, archive_read_close(a));
176   }
177   assertEqualInt(ARCHIVE_OK,archive_read_free(a));
178 }
179 
DEFINE_TEST(test_read_append_lzop_filter)180 DEFINE_TEST(test_read_append_lzop_filter)
181 {
182   struct archive *a;
183   int r;
184 
185   assert((a = archive_read_new()) != NULL);
186   assertA(0 == archive_read_set_format(a, ARCHIVE_FORMAT_TAR));
187   r = archive_read_append_filter(a, ARCHIVE_FILTER_LZOP);
188   if (archive_liblzo2_version() != NULL) {
189     assertEqualIntA(a, ARCHIVE_OK, r);
190   } else if (canLzop()) {
191     // We're using an external program
192     assertEqualIntA(a, ARCHIVE_WARN, r);
193   }
194 
195   archive_read_free(a);
196 }
197 
DEFINE_TEST(test_read_append_grzip_filter)198 DEFINE_TEST(test_read_append_grzip_filter)
199 {
200   struct archive *a;
201   int r;
202 
203   assert((a = archive_read_new()) != NULL);
204   assertA(0 == archive_read_set_format(a, ARCHIVE_FORMAT_TAR));
205   r = archive_read_append_filter(a, ARCHIVE_FILTER_GRZIP);
206   // Grzip currently always uses an external program.
207   if (canGrzip()) {
208     assertEqualIntA(a, ARCHIVE_WARN, r);
209   }
210 
211   archive_read_free(a);
212 }
213 
DEFINE_TEST(test_read_append_compress_filter)214 DEFINE_TEST(test_read_append_compress_filter)
215 {
216   struct archive *a;
217   int r;
218 
219   assert((a = archive_read_new()) != NULL);
220   assertA(0 == archive_read_set_format(a, ARCHIVE_FORMAT_TAR));
221   r = archive_read_append_filter(a, ARCHIVE_FILTER_COMPRESS);
222   assertEqualIntA(a, ARCHIVE_OK, r);
223   assertEqualInt(ARCHIVE_OK, archive_read_free(a));
224 }
225 
DEFINE_TEST(test_read_append_bzip2_filter)226 DEFINE_TEST(test_read_append_bzip2_filter)
227 {
228   struct archive *a;
229   int r;
230 
231   assert((a = archive_read_new()) != NULL);
232   assertA(0 == archive_read_set_format(a, ARCHIVE_FORMAT_TAR));
233   r = archive_read_append_filter(a, ARCHIVE_FILTER_BZIP2);
234   if (r != ARCHIVE_OK && archive_bzlib_version() == NULL && !canBzip2()) {
235     skipping("bzip2 tests require bzlib or working bzip2 command");
236     archive_read_free(a);
237     return;
238   }
239   if (r == ARCHIVE_WARN && canBzip2())
240     assertEqualString(archive_error_string(a), "Using external bzip2 program");
241   else
242     assertEqualIntA(a, ARCHIVE_OK, r);
243   archive_read_free(a);
244 }
245 
DEFINE_TEST(test_read_append_lrzip_filter)246 DEFINE_TEST(test_read_append_lrzip_filter)
247 {
248   struct archive *a;
249   int r;
250 
251   assert((a = archive_read_new()) != NULL);
252   assertA(0 == archive_read_set_format(a, ARCHIVE_FORMAT_TAR));
253   r = archive_read_append_filter(a, ARCHIVE_FILTER_LRZIP);
254   if (r != ARCHIVE_OK && !canLrzip()) {
255     skipping("lrzip tests require working lrzip command");
256     archive_read_free(a);
257     return;
258   }
259   if (r == ARCHIVE_WARN && canLrzip())
260     assertEqualString(archive_error_string(a), "Using external lrzip program for lrzip decompression");
261   else
262     assertEqualIntA(a, ARCHIVE_OK, r);
263   archive_read_free(a);
264 }
265 
DEFINE_TEST(test_read_append_lz4_filter)266 DEFINE_TEST(test_read_append_lz4_filter)
267 {
268   struct archive *a;
269   int r;
270 
271   assert((a = archive_read_new()) != NULL);
272   assertA(0 == archive_read_set_format(a, ARCHIVE_FORMAT_TAR));
273   r = archive_read_append_filter(a, ARCHIVE_FILTER_LZ4);
274   if (r != ARCHIVE_OK && archive_liblz4_version() == NULL && !canLz4()) {
275     skipping("lz4 tests require liblz4 or working lz4 command");
276     archive_read_free(a);
277     return;
278   }
279   if (r == ARCHIVE_WARN && canLz4())
280     assertEqualString(archive_error_string(a), "Using external lz4 program");
281   else
282     assertEqualIntA(a, ARCHIVE_OK, r);
283   archive_read_free(a);
284 }
285 
DEFINE_TEST(test_read_append_lzip_filter)286 DEFINE_TEST(test_read_append_lzip_filter)
287 {
288   struct archive *a;
289   int r;
290 
291   assert((a = archive_read_new()) != NULL);
292   assertA(0 == archive_read_set_format(a, ARCHIVE_FORMAT_TAR));
293   r = archive_read_append_filter(a, ARCHIVE_FILTER_LZIP);
294   if (r != ARCHIVE_OK && archive_liblzma_version() == NULL && !canLzip()) {
295     skipping("lzip tests require liblzma or working lzip command");
296     archive_read_free(a);
297     return;
298   }
299   if (r == ARCHIVE_WARN && canLzip())
300     assertEqualString(archive_error_string(a), "Using external lzip program for lzip decompression");
301   else
302     assertEqualIntA(a, ARCHIVE_OK, r);
303   archive_read_free(a);
304 }
305 
DEFINE_TEST(test_read_append_lzma_filter)306 DEFINE_TEST(test_read_append_lzma_filter)
307 {
308   struct archive *a;
309   int r;
310 
311   assert((a = archive_read_new()) != NULL);
312   assertA(0 == archive_read_set_format(a, ARCHIVE_FORMAT_TAR));
313   r = archive_read_append_filter(a, ARCHIVE_FILTER_LZMA);
314   if (r != ARCHIVE_OK && archive_liblzma_version() == NULL && !canLzma()) {
315     skipping("lzma tests require liblzma or working lzma command");
316     archive_read_free(a);
317     return;
318   }
319   if (r == ARCHIVE_WARN && canLzma())
320     assertEqualString(archive_error_string(a), "Using external lzma program for lzma decompression");
321   else
322     assertEqualIntA(a, ARCHIVE_OK, r);
323   archive_read_free(a);
324 }
325 
DEFINE_TEST(test_read_append_zstd_filter)326 DEFINE_TEST(test_read_append_zstd_filter)
327 {
328   struct archive *a;
329   int r;
330 
331   assert((a = archive_read_new()) != NULL);
332   assertA(0 == archive_read_set_format(a, ARCHIVE_FORMAT_TAR));
333   r = archive_read_append_filter(a, ARCHIVE_FILTER_ZSTD);
334   if (r != ARCHIVE_OK && archive_libzstd_version() == NULL && !canZstd()) {
335     skipping("zstd tests require libzstd or working zstd command");
336     archive_read_free(a);
337     return;
338   }
339   if (r == ARCHIVE_WARN && canZstd())
340     assertEqualString(archive_error_string(a), "Using external zstd program for zstd decompression");
341   else
342     assertEqualIntA(a, ARCHIVE_OK, r);
343   archive_read_free(a);
344 }
345 
DEFINE_TEST(test_read_append_rpm_filter)346 DEFINE_TEST(test_read_append_rpm_filter)
347 {
348   struct archive *a;
349   int r;
350 
351   assert((a = archive_read_new()) != NULL);
352   assertA(0 == archive_read_set_format(a, ARCHIVE_FORMAT_TAR));
353   r = archive_read_append_filter(a, ARCHIVE_FILTER_RPM);
354   assertEqualIntA(a, ARCHIVE_OK, r);
355   archive_read_free(a);
356 }
357 
DEFINE_TEST(test_read_append_uu_filter)358 DEFINE_TEST(test_read_append_uu_filter)
359 {
360   struct archive *a;
361   int r;
362 
363   assert((a = archive_read_new()) != NULL);
364   assertA(0 == archive_read_set_format(a, ARCHIVE_FORMAT_TAR));
365   r = archive_read_append_filter(a, ARCHIVE_FILTER_UU);
366   assertEqualIntA(a, ARCHIVE_OK, r);
367   archive_read_free(a);
368 }
369 
DEFINE_TEST(test_read_append_none_filter)370 DEFINE_TEST(test_read_append_none_filter)
371 {
372   struct archive *a;
373   int r;
374 
375   assert((a = archive_read_new()) != NULL);
376   assertA(0 == archive_read_set_format(a, ARCHIVE_FORMAT_TAR));
377   r = archive_read_append_filter(a, ARCHIVE_FILTER_NONE);
378   assertEqualIntA(a, ARCHIVE_OK, r);
379   archive_read_free(a);
380 }
381 
DEFINE_TEST(test_read_append_filter_program)382 DEFINE_TEST(test_read_append_filter_program)
383 {
384   struct archive_entry *ae;
385   struct archive *a;
386 
387   if (!canGzip()) {
388     skipping("Can't run gzip program on this platform");
389     return;
390   }
391   assert((a = archive_read_new()) != NULL);
392   assertA(0 == archive_read_set_format(a, ARCHIVE_FORMAT_TAR));
393   assertEqualIntA(a, ARCHIVE_OK,
394       archive_read_append_filter_program(a, "gzip -d"));
395   assertEqualIntA(a, ARCHIVE_OK,
396       archive_read_open_memory(a, archive, sizeof(archive)));
397   assertEqualIntA(a, ARCHIVE_OK,
398       archive_read_next_header(a, &ae));
399   assertEqualInt(archive_filter_code(a, 0), ARCHIVE_FILTER_PROGRAM);
400   assertEqualInt(archive_format(a), ARCHIVE_FORMAT_TAR_USTAR);
401   assertEqualIntA(a, ARCHIVE_OK, archive_read_close(a));
402   assertEqualInt(ARCHIVE_OK, archive_read_free(a));
403 }
404 
DEFINE_TEST(test_read_append_filter_wrong_program)405 DEFINE_TEST(test_read_append_filter_wrong_program)
406 {
407   struct archive_entry *ae;
408   struct archive *a;
409 #if !defined(_WIN32) || defined(__CYGWIN__)
410   FILE * fp;
411   int fd;
412   fpos_t pos;
413 #endif
414 
415   /*
416    * If we have "bunzip2 -q", try using that.
417    */
418   if (!canRunCommand("bunzip2 -h", NULL)) {
419     skipping("Can't run bunzip2 program on this platform");
420     return;
421   }
422 
423 #if !defined(_WIN32) || defined(__CYGWIN__)
424   /* bunzip2 will write to stderr, redirect it to a file */
425   fflush(stderr);
426   fgetpos(stderr, &pos);
427   assert((fd = dup(fileno(stderr))) != -1);
428   fp = freopen("stderr1", "w", stderr);
429 #endif
430 
431   assert((a = archive_read_new()) != NULL);
432   assertA(0 == archive_read_set_format(a, ARCHIVE_FORMAT_TAR));
433   assertEqualIntA(a, ARCHIVE_OK,
434       archive_read_append_filter_program(a, "bunzip2 -q"));
435   assertEqualIntA(a, ARCHIVE_OK,
436       archive_read_open_memory(a, archive, sizeof(archive)));
437   assertA(archive_read_next_header(a, &ae) < (ARCHIVE_WARN));
438   assertEqualIntA(a, ARCHIVE_WARN, archive_read_close(a));
439   assertEqualInt(ARCHIVE_OK, archive_read_free(a));
440 
441 #if !defined(_WIN32) || defined(__CYGWIN__)
442   /* restore stderr */
443   if (fp != NULL) {
444     fflush(stderr);
445     dup2(fd, fileno(stderr));
446     clearerr(stderr);
447     (void)fsetpos(stderr, &pos);
448   }
449   close(fd);
450 #endif
451 }
452