xref: /freebsd/contrib/libarchive/libarchive/test/test_archive_write_set_options.c (revision 185becb1e1bd2657c156f78aeb52edac05ba5fb5)
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 
26 #include "test.h"
27 
28 #define should_write_set_filter_option(__a, __code, __m, __o, __v) \
29 assertEqualInt(__code, archive_write_set_filter_option(__a, __m, __o, __v))
30 
31 static void
write_set_filter_option_test(int pristine)32 write_set_filter_option_test(int pristine)
33 {
34 	struct archive* a = archive_write_new();
35 
36 	if (!pristine)
37 		archive_write_add_filter_gzip(a);
38 
39 	should_write_set_filter_option(a, ARCHIVE_OK, NULL, NULL, NULL);
40 	should_write_set_filter_option(a, ARCHIVE_OK, "", "", "");
41 
42 	should_write_set_filter_option(a, ARCHIVE_FAILED, NULL, "fubar", NULL);
43 	should_write_set_filter_option(a, ARCHIVE_FAILED, NULL, "fubar", "snafu");
44 	should_write_set_filter_option(a, ARCHIVE_FAILED, "fubar", "snafu", NULL);
45 	should_write_set_filter_option(a, ARCHIVE_FAILED, "fubar", "snafu", "betcha");
46 
47 	archive_write_free(a);
48 }
49 
DEFINE_TEST(test_archive_write_set_filter_option)50 DEFINE_TEST(test_archive_write_set_filter_option)
51 {
52 	write_set_filter_option_test(1);
53 	write_set_filter_option_test(0);
54 }
55 
56 #define should_write_set_format_option(__a, __code, __m, __o, __v) \
57 assertEqualInt(__code, archive_write_set_format_option(__a, __m, __o, __v))
58 
59 static void
write_set_format_option_test(int pristine)60 write_set_format_option_test(int pristine)
61 {
62 	struct archive* a = archive_write_new();
63 	int known_option_rv = pristine ? ARCHIVE_FAILED : ARCHIVE_OK;
64 
65 	if (!pristine)
66 		archive_write_set_format_iso9660(a);
67 
68 	/* NULL and "" denote `no option', so they're ok no matter
69 	 * what, if any, formats are registered */
70 	should_write_set_format_option(a, ARCHIVE_OK, NULL, NULL, NULL);
71 	should_write_set_format_option(a, ARCHIVE_OK, "", "", "");
72 
73 	/* unknown modules and options */
74 	should_write_set_format_option(a, ARCHIVE_FAILED, "fubar", "snafu", NULL);
75 	should_write_set_format_option(a, ARCHIVE_FAILED, "fubar", "snafu", "betcha");
76 
77 	/* unknown modules and options */
78 	should_write_set_format_option(a, ARCHIVE_FAILED, NULL, "snafu", NULL);
79 	should_write_set_format_option(a, ARCHIVE_FAILED, NULL, "snafu", "betcha");
80 
81 	/* ARCHIVE_OK with iso9660 loaded, ARCHIVE_WARN otherwise */
82 	should_write_set_format_option(a, known_option_rv, "iso9660", "joliet", NULL);
83 	should_write_set_format_option(a, known_option_rv, "iso9660", "joliet", NULL);
84 	should_write_set_format_option(a, known_option_rv, NULL, "joliet", NULL);
85 	should_write_set_format_option(a, known_option_rv, NULL, "joliet", NULL);
86 
87 	archive_write_free(a);
88 }
89 
DEFINE_TEST(test_archive_write_set_format_option)90 DEFINE_TEST(test_archive_write_set_format_option)
91 {
92 	write_set_format_option_test(1);
93 	write_set_format_option_test(0);
94 }
95 
96 #define should_write_set_option(__a, __code, __m, __o, __v) \
97 assertEqualInt(__code, archive_write_set_option(__a, __m, __o, __v))
98 
99 static void
write_set_option_test(int pristine)100 write_set_option_test(int pristine)
101 {
102 	struct archive* a = archive_write_new();
103 	int known_option_rv = pristine ? ARCHIVE_FAILED : ARCHIVE_OK;
104 
105 	if (!pristine) {
106 		archive_write_add_filter_gzip(a);
107 		archive_write_set_format_iso9660(a);
108         }
109 
110 	/* NULL and "" denote `no option', so they're ok no matter
111 	 * what, if any, formats are registered */
112 	should_write_set_option(a, ARCHIVE_OK, NULL, NULL, NULL);
113 	should_write_set_option(a, ARCHIVE_OK, "", "", "");
114 
115 	/* unknown modules and options */
116 	should_write_set_option(a, ARCHIVE_FAILED, "fubar", "snafu", NULL);
117 	should_write_set_option(a, ARCHIVE_FAILED, "fubar", "snafu", "betcha");
118 
119 	/* unknown modules and options */
120 	should_write_set_option(a, ARCHIVE_FAILED, NULL, "snafu", NULL);
121 	should_write_set_option(a, ARCHIVE_FAILED, NULL, "snafu", "betcha");
122 
123 	/* ARCHIVE_OK with iso9660 loaded, ARCHIVE_WARN otherwise */
124 	should_write_set_option(a, known_option_rv, "iso9660", "joliet", NULL);
125 	should_write_set_option(a, known_option_rv, "iso9660", "joliet", NULL);
126 	should_write_set_option(a, known_option_rv, NULL, "joliet", NULL);
127 	should_write_set_option(a, known_option_rv, NULL, "joliet", NULL);
128 
129 	archive_write_free(a);
130 }
131 
DEFINE_TEST(test_archive_write_set_option)132 DEFINE_TEST(test_archive_write_set_option)
133 {
134 	write_set_option_test(1);
135 	write_set_option_test(0);
136 }
137 
138 #define should_write_set_options(__a, __code, __opts) \
139 assertEqualInt(__code, archive_write_set_options(__a, __opts))
140 
141 static void
write_set_options_test(int pristine)142 write_set_options_test(int pristine)
143 {
144 	struct archive* a = archive_write_new();
145 	int halfempty_options_rv = pristine ? ARCHIVE_FAILED : ARCHIVE_OK;
146 	int known_option_rv = pristine ? ARCHIVE_FAILED : ARCHIVE_OK;
147 
148 	if (!pristine) {
149 		archive_write_add_filter_gzip(a);
150 		archive_write_set_format_iso9660(a);
151 	}
152 
153 	/* NULL and "" denote `no option', so they're ok no matter
154 	 * what, if any, formats are registered */
155 	should_write_set_options(a, ARCHIVE_OK, NULL);
156 	should_write_set_options(a, ARCHIVE_OK, "");
157 
158 	/* unknown modules and options */
159 	should_write_set_options(a, ARCHIVE_FAILED, "fubar:snafu");
160 	assertEqualString("Unknown module name: `fubar'",
161 	    archive_error_string(a));
162 	should_write_set_options(a, ARCHIVE_FAILED, "fubar:snafu=betcha");
163 	assertEqualString("Unknown module name: `fubar'",
164 	    archive_error_string(a));
165 
166 	/* unknown modules and options */
167 	should_write_set_options(a, ARCHIVE_FAILED, "snafu");
168 	assertEqualString("Undefined option: `snafu'",
169 	    archive_error_string(a));
170 	should_write_set_options(a, ARCHIVE_FAILED, "snafu=betcha");
171 	assertEqualString("Undefined option: `snafu'",
172 	    archive_error_string(a));
173 
174 	/* ARCHIVE_OK with iso9660 loaded, ARCHIVE_FAILED otherwise */
175 	should_write_set_options(a, known_option_rv, "iso9660:joliet");
176 	if (pristine) {
177 		assertEqualString("Unknown module name: `iso9660'",
178 		    archive_error_string(a));
179 	}
180 	should_write_set_options(a, known_option_rv, "iso9660:joliet");
181 	if (pristine) {
182 		assertEqualString("Unknown module name: `iso9660'",
183 		    archive_error_string(a));
184 	}
185 	should_write_set_options(a, known_option_rv, "joliet");
186 	if (pristine) {
187 		assertEqualString("Undefined option: `joliet'",
188 		    archive_error_string(a));
189 	}
190 	should_write_set_options(a, known_option_rv, "!joliet");
191 	if (pristine) {
192 		assertEqualString("Undefined option: `joliet'",
193 		    archive_error_string(a));
194 	}
195 
196 	should_write_set_options(a, ARCHIVE_OK, ",");
197 	should_write_set_options(a, ARCHIVE_OK, ",,");
198 
199 	should_write_set_options(a, halfempty_options_rv, ",joliet");
200 	if (pristine) {
201 		assertEqualString("Undefined option: `joliet'",
202 		    archive_error_string(a));
203 	}
204 	should_write_set_options(a, halfempty_options_rv, "joliet,");
205 	if (pristine) {
206 		assertEqualString("Undefined option: `joliet'",
207 		    archive_error_string(a));
208 	}
209 
210 	should_write_set_options(a, ARCHIVE_FAILED, "joliet,snafu");
211 	if (pristine) {
212 		assertEqualString("Undefined option: `joliet'",
213 		    archive_error_string(a));
214 	} else {
215 		assertEqualString("Undefined option: `snafu'",
216 		    archive_error_string(a));
217 	}
218 
219 	should_write_set_options(a, ARCHIVE_FAILED, "iso9660:snafu");
220 	if (pristine) {
221 		assertEqualString("Unknown module name: `iso9660'",
222 		    archive_error_string(a));
223 	} else {
224 		assertEqualString("Undefined option: `iso9660:snafu'",
225 		    archive_error_string(a));
226 	}
227 
228 	archive_write_free(a);
229 }
230 
DEFINE_TEST(test_archive_write_set_options)231 DEFINE_TEST(test_archive_write_set_options)
232 {
233 	write_set_options_test(1);
234 	write_set_options_test(0);
235 }
236