1 /*-
2 * Copyright (c) 2011 Tim Kientzle
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 * 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.
13 *
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.
24 */
25 #include "test.h"
26
27 /*
28 * Verify that the various archive_read_support_* functions
29 * return appropriate errors when invoked on the wrong kind of
30 * archive handle.
31 */
32
33 typedef struct archive *constructor(void);
34 typedef int enabler(struct archive *);
35 typedef int destructor(struct archive *);
36
37 static int format_code = 0;
format_code_enabler(struct archive * a)38 static int format_code_enabler(struct archive *a)
39 {
40 return archive_read_support_format_by_code(a, format_code);
41 }
42
format_code_setter(struct archive * a)43 static int format_code_setter(struct archive *a)
44 {
45 return archive_read_set_format(a, format_code);
46 }
47
48 static void
test_success(constructor new_,enabler enable_,destructor free_)49 test_success(constructor new_, enabler enable_, destructor free_)
50 {
51 struct archive *a = new_();
52 int result = enable_(a);
53 if (result == ARCHIVE_WARN) {
54 assert(NULL != archive_error_string(a));
55 assertEqualIntA(a, -1, archive_errno(a));
56 } else {
57 assertEqualIntA(a, ARCHIVE_OK, result);
58 assert(NULL == archive_error_string(a));
59 assertEqualIntA(a, 0, archive_errno(a));
60 }
61 free_(a);
62 }
63
64 static void
test_failure(constructor new_,enabler enable_,destructor free_)65 test_failure(constructor new_, enabler enable_, destructor free_)
66 {
67 struct archive *a = new_();
68 assertEqualIntA(a, ARCHIVE_FATAL, enable_(a));
69 assert(NULL != archive_error_string(a));
70 assertEqualIntA(a, -1, archive_errno(a));
71 free_(a);
72 }
73
74 static void
test_filter_or_format(enabler enable)75 test_filter_or_format(enabler enable)
76 {
77 test_success(archive_read_new, enable, archive_read_free);
78 test_failure(archive_write_new, enable, archive_write_free);
79 test_failure(archive_read_disk_new, enable, archive_read_free);
80 test_failure(archive_write_disk_new, enable, archive_write_free);
81 }
82
DEFINE_TEST(test_archive_read_support)83 DEFINE_TEST(test_archive_read_support)
84 {
85 test_filter_or_format(archive_read_support_format_7zip);
86 test_filter_or_format(archive_read_support_format_all);
87 test_filter_or_format(archive_read_support_format_ar);
88 test_filter_or_format(archive_read_support_format_cab);
89 test_filter_or_format(archive_read_support_format_cpio);
90 test_filter_or_format(archive_read_support_format_empty);
91 test_filter_or_format(archive_read_support_format_iso9660);
92 test_filter_or_format(archive_read_support_format_lha);
93 test_filter_or_format(archive_read_support_format_mtree);
94 test_filter_or_format(archive_read_support_format_tar);
95 test_filter_or_format(archive_read_support_format_xar);
96 test_filter_or_format(archive_read_support_format_zip);
97
98 int format_codes[] = {
99 ARCHIVE_FORMAT_CPIO,
100 ARCHIVE_FORMAT_CPIO_POSIX,
101 ARCHIVE_FORMAT_CPIO_BIN_LE,
102 ARCHIVE_FORMAT_CPIO_BIN_BE,
103 ARCHIVE_FORMAT_CPIO_SVR4_NOCRC,
104 ARCHIVE_FORMAT_CPIO_SVR4_CRC,
105 ARCHIVE_FORMAT_CPIO_AFIO_LARGE,
106 ARCHIVE_FORMAT_TAR,
107 ARCHIVE_FORMAT_TAR_USTAR,
108 ARCHIVE_FORMAT_TAR_PAX_INTERCHANGE,
109 ARCHIVE_FORMAT_TAR_PAX_RESTRICTED,
110 ARCHIVE_FORMAT_TAR_GNUTAR,
111 ARCHIVE_FORMAT_ISO9660,
112 ARCHIVE_FORMAT_ISO9660_ROCKRIDGE,
113 ARCHIVE_FORMAT_ZIP,
114 ARCHIVE_FORMAT_EMPTY,
115 ARCHIVE_FORMAT_AR,
116 ARCHIVE_FORMAT_AR_GNU,
117 ARCHIVE_FORMAT_AR_BSD,
118 ARCHIVE_FORMAT_MTREE,
119 ARCHIVE_FORMAT_RAW,
120 ARCHIVE_FORMAT_XAR,
121 ARCHIVE_FORMAT_LHA,
122 ARCHIVE_FORMAT_CAB,
123 ARCHIVE_FORMAT_RAR,
124 ARCHIVE_FORMAT_7ZIP,
125 ARCHIVE_FORMAT_WARC,
126 ARCHIVE_FORMAT_RAR_V5,
127 };
128 unsigned int i;
129
130 for (i = 0; i < sizeof(format_codes) / sizeof(int); i++) {
131 format_code = format_codes[i];
132 test_filter_or_format(format_code_enabler);
133 test_filter_or_format(format_code_setter);
134 }
135
136 test_filter_or_format(archive_read_support_filter_all);
137 test_filter_or_format(archive_read_support_filter_bzip2);
138 test_filter_or_format(archive_read_support_filter_compress);
139 test_filter_or_format(archive_read_support_filter_gzip);
140 test_filter_or_format(archive_read_support_filter_lzip);
141 test_filter_or_format(archive_read_support_filter_lzma);
142 test_filter_or_format(archive_read_support_filter_none);
143 test_filter_or_format(archive_read_support_filter_rpm);
144 test_filter_or_format(archive_read_support_filter_uu);
145 test_filter_or_format(archive_read_support_filter_xz);
146 }
147