xref: /freebsd/contrib/libarchive/libarchive/test/test_archive_match_path.c (revision 5b56413d04e608379c9a306373554a8e4d321bc0)
1 /*-
2  * Copyright (c) 2012 Michihiro NAKAJIMA
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 static void
29 test_exclusion_mbs(void)
30 {
31 	struct archive_entry *ae;
32 	struct archive *m;
33 
34 	if (!assert((m = archive_match_new()) != NULL))
35 		return;
36 	if (!assert((ae = archive_entry_new()) != NULL)) {
37 		archive_match_free(m);
38 		return;
39 	}
40 
41 	/* Test for pattern "^aa*" */
42 	assertEqualIntA(m, 0, archive_match_exclude_pattern(m, "^aa*"));
43 
44 	/* Test with 'aa1234', which should be excluded. */
45 	archive_entry_copy_pathname(ae, "aa1234");
46 	failure("'aa1234' should be excluded");
47 	assertEqualInt(1, archive_match_path_excluded(m, ae));
48 	assertEqualInt(1, archive_match_excluded(m, ae));
49 	archive_entry_clear(ae);
50 	archive_entry_copy_pathname_w(ae, L"aa1234");
51 	failure("'aa1234' should be excluded");
52 	assertEqualInt(1, archive_match_path_excluded(m, ae));
53 	assertEqualInt(1, archive_match_excluded(m, ae));
54 
55 	/* Test with 'a1234', which should not be excluded. */
56 	archive_entry_copy_pathname(ae, "a1234");
57 	failure("'a1234' should not be excluded");
58 	assertEqualInt(0, archive_match_path_excluded(m, ae));
59 	assertEqualInt(0, archive_match_excluded(m, ae));
60 	archive_entry_clear(ae);
61 	archive_entry_copy_pathname_w(ae, L"a1234");
62 	failure("'a1234' should not be excluded");
63 	assertEqualInt(0, archive_match_path_excluded(m, ae));
64 	assertEqualInt(0, archive_match_excluded(m, ae));
65 
66 	/* Clean up. */
67 	archive_entry_free(ae);
68 	archive_match_free(m);
69 }
70 
71 static void
72 test_exclusion_wcs(void)
73 {
74 	struct archive_entry *ae;
75 	struct archive *m;
76 
77 	if (!assert((m = archive_match_new()) != NULL))
78 		return;
79 	if (!assert((ae = archive_entry_new()) != NULL)) {
80 		archive_match_free(m);
81 		return;
82 	}
83 
84 	/* Test for pattern "^aa*" */
85 	assertEqualIntA(m, 0, archive_match_exclude_pattern_w(m, L"^aa*"));
86 
87 	/* Test with 'aa1234', which should be excluded. */
88 	archive_entry_copy_pathname(ae, "aa1234");
89 	failure("'aa1234' should be excluded");
90 	assertEqualInt(1, archive_match_path_excluded(m, ae));
91 	assertEqualInt(1, archive_match_excluded(m, ae));
92 	archive_entry_clear(ae);
93 	archive_entry_copy_pathname_w(ae, L"aa1234");
94 	failure("'aa1234' should be excluded");
95 	assertEqualInt(1, archive_match_path_excluded(m, ae));
96 	assertEqualInt(1, archive_match_excluded(m, ae));
97 
98 	/* Test with 'a1234', which should not be excluded. */
99 	archive_entry_copy_pathname(ae, "a1234");
100 	failure("'a1234' should not be excluded");
101 	assertEqualInt(0, archive_match_path_excluded(m, ae));
102 	assertEqualInt(0, archive_match_excluded(m, ae));
103 	archive_entry_clear(ae);
104 	archive_entry_copy_pathname_w(ae, L"a1234");
105 	failure("'a1234' should not be excluded");
106 	assertEqualInt(0, archive_match_path_excluded(m, ae));
107 	assertEqualInt(0, archive_match_excluded(m, ae));
108 
109 	/* Clean up. */
110 	archive_entry_free(ae);
111 	archive_match_free(m);
112 }
113 
114 static void
115 exclusion_from_file(struct archive *m)
116 {
117 	struct archive_entry *ae;
118 
119 	if (!assert((ae = archive_entry_new()) != NULL)) {
120 		archive_match_free(m);
121 		return;
122 	}
123 
124 	/* Test with 'first', which should not be excluded. */
125 	archive_entry_copy_pathname(ae, "first");
126 	failure("'first' should not be excluded");
127 	assertEqualInt(0, archive_match_path_excluded(m, ae));
128 	assertEqualInt(0, archive_match_excluded(m, ae));
129 	archive_entry_clear(ae);
130 	archive_entry_copy_pathname_w(ae, L"first");
131 	failure("'first' should not be excluded");
132 	assertEqualInt(0, archive_match_path_excluded(m, ae));
133 	assertEqualInt(0, archive_match_excluded(m, ae));
134 
135 	/* Test with 'second', which should be excluded. */
136 	archive_entry_copy_pathname(ae, "second");
137 	failure("'second' should be excluded");
138 	assertEqualInt(1, archive_match_path_excluded(m, ae));
139 	assertEqualInt(1, archive_match_excluded(m, ae));
140 	archive_entry_clear(ae);
141 	archive_entry_copy_pathname_w(ae, L"second");
142 	failure("'second' should be excluded");
143 	assertEqualInt(1, archive_match_path_excluded(m, ae));
144 	assertEqualInt(1, archive_match_excluded(m, ae));
145 
146 	/* Test with 'third', which should not be excluded. */
147 	archive_entry_copy_pathname(ae, "third");
148 	failure("'third' should not be excluded");
149 	assertEqualInt(0, archive_match_path_excluded(m, ae));
150 	assertEqualInt(0, archive_match_excluded(m, ae));
151 	archive_entry_clear(ae);
152 	archive_entry_copy_pathname_w(ae, L"third");
153 	failure("'third' should not be excluded");
154 	assertEqualInt(0, archive_match_path_excluded(m, ae));
155 	assertEqualInt(0, archive_match_excluded(m, ae));
156 
157 	/* Test with 'four', which should be excluded. */
158 	archive_entry_copy_pathname(ae, "four");
159 	failure("'four' should be excluded");
160 	assertEqualInt(1, archive_match_path_excluded(m, ae));
161 	assertEqualInt(1, archive_match_excluded(m, ae));
162 	archive_entry_clear(ae);
163 	archive_entry_copy_pathname_w(ae, L"four");
164 	failure("'four' should be excluded");
165 	assertEqualInt(1, archive_match_path_excluded(m, ae));
166 	assertEqualInt(1, archive_match_excluded(m, ae));
167 
168 	/* Clean up. */
169 	archive_entry_free(ae);
170 }
171 
172 static void
173 test_exclusion_from_file_mbs(void)
174 {
175 	struct archive *m;
176 
177 	/* Test1: read exclusion patterns from file */
178 	if (!assert((m = archive_match_new()) != NULL))
179 		return;
180 	assertEqualIntA(m, 0,
181 	    archive_match_exclude_pattern_from_file(m, "exclusion", 0));
182 	exclusion_from_file(m);
183 	/* Clean up. */
184 	archive_match_free(m);
185 
186 	/* Test2: read exclusion patterns in a null separator from file */
187 	if (!assert((m = archive_match_new()) != NULL))
188 		return;
189 	/* Test for pattern reading from file */
190 	assertEqualIntA(m, 0,
191 	    archive_match_exclude_pattern_from_file(m, "exclusion_null", 1));
192 	exclusion_from_file(m);
193 	/* Clean up. */
194 	archive_match_free(m);
195 }
196 
197 static void
198 test_exclusion_from_file_wcs(void)
199 {
200 	struct archive *m;
201 
202 	/* Test1: read exclusion patterns from file */
203 	if (!assert((m = archive_match_new()) != NULL))
204 		return;
205 	assertEqualIntA(m, 0,
206 	    archive_match_exclude_pattern_from_file_w(m, L"exclusion", 0));
207 	exclusion_from_file(m);
208 	/* Clean up. */
209 	archive_match_free(m);
210 
211 	/* Test2: read exclusion patterns in a null separator from file */
212 	if (!assert((m = archive_match_new()) != NULL))
213 		return;
214 	/* Test for pattern reading from file */
215 	assertEqualIntA(m, 0,
216 	    archive_match_exclude_pattern_from_file_w(m, L"exclusion_null", 1));
217 	exclusion_from_file(m);
218 	/* Clean up. */
219 	archive_match_free(m);
220 }
221 
222 static void
223 test_inclusion_mbs(void)
224 {
225 	struct archive_entry *ae;
226 	struct archive *m;
227 	const char *mp;
228 
229 	if (!assert((m = archive_match_new()) != NULL))
230 		return;
231 	if (!assert((ae = archive_entry_new()) != NULL)) {
232 		archive_match_free(m);
233 		return;
234 	}
235 
236 	/* Test for pattern "^aa*" */
237 	assertEqualIntA(m, 0, archive_match_include_pattern(m, "^aa*"));
238 
239 	/* Test with 'aa1234', which should not be excluded. */
240 	archive_entry_copy_pathname(ae, "aa1234");
241 	failure("'aa1234' should not be excluded");
242 	assertEqualInt(0, archive_match_path_excluded(m, ae));
243 	assertEqualInt(0, archive_match_excluded(m, ae));
244 	archive_entry_clear(ae);
245 	archive_entry_copy_pathname_w(ae, L"aa1234");
246 	failure("'aa1234' should not be excluded");
247 	assertEqualInt(0, archive_match_path_excluded(m, ae));
248 	assertEqualInt(0, archive_match_excluded(m, ae));
249 
250 	/* Test with 'a1234', which should be excluded. */
251 	archive_entry_copy_pathname(ae, "a1234");
252 	failure("'a1234' should be excluded");
253 	assertEqualInt(1, archive_match_path_excluded(m, ae));
254 	assertEqualInt(1, archive_match_excluded(m, ae));
255 	archive_entry_clear(ae);
256 	archive_entry_copy_pathname_w(ae, L"a1234");
257 	failure("'a1234' should be excluded");
258 	assertEqualInt(1, archive_match_path_excluded(m, ae));
259 	assertEqualInt(1, archive_match_excluded(m, ae));
260 
261 	/* Verify unmatched_inclusions. */
262 	assertEqualInt(0, archive_match_path_unmatched_inclusions(m));
263 	assertEqualIntA(m, ARCHIVE_EOF,
264 	    archive_match_path_unmatched_inclusions_next(m, &mp));
265 
266 	/* Clean up. */
267 	archive_entry_free(ae);
268 	archive_match_free(m);
269 }
270 
271 static void
272 test_inclusion_wcs(void)
273 {
274 	struct archive_entry *ae;
275 	struct archive *m;
276 	const char *mp;
277 
278 	if (!assert((m = archive_match_new()) != NULL))
279 		return;
280 	if (!assert((ae = archive_entry_new()) != NULL)) {
281 		archive_match_free(m);
282 		return;
283 	}
284 
285 	/* Test for pattern "^aa*" */
286 	assertEqualIntA(m, 0, archive_match_include_pattern_w(m, L"^aa*"));
287 
288 	/* Test with 'aa1234', which should not be excluded. */
289 	archive_entry_copy_pathname(ae, "aa1234");
290 	failure("'aa1234' should not be excluded");
291 	assertEqualInt(0, archive_match_path_excluded(m, ae));
292 	assertEqualInt(0, archive_match_excluded(m, ae));
293 	archive_entry_clear(ae);
294 	archive_entry_copy_pathname_w(ae, L"aa1234");
295 	failure("'aa1234' should not be excluded");
296 	assertEqualInt(0, archive_match_path_excluded(m, ae));
297 	assertEqualInt(0, archive_match_excluded(m, ae));
298 
299 	/* Test with 'a1234', which should be excluded. */
300 	archive_entry_copy_pathname(ae, "a1234");
301 	failure("'a1234' should be excluded");
302 	assertEqualInt(1, archive_match_path_excluded(m, ae));
303 	assertEqualInt(1, archive_match_excluded(m, ae));
304 	archive_entry_clear(ae);
305 	archive_entry_copy_pathname_w(ae, L"a1234");
306 	failure("'a1234' should be excluded");
307 	assertEqualInt(1, archive_match_path_excluded(m, ae));
308 	assertEqualInt(1, archive_match_excluded(m, ae));
309 
310 	/* Verify unmatched_inclusions. */
311 	assertEqualInt(0, archive_match_path_unmatched_inclusions(m));
312 	assertEqualIntA(m, ARCHIVE_EOF,
313 	    archive_match_path_unmatched_inclusions_next(m, &mp));
314 
315 	/* Clean up. */
316 	archive_entry_free(ae);
317 	archive_match_free(m);
318 }
319 
320 static void
321 test_inclusion_from_file_mbs(void)
322 {
323 	struct archive *m;
324 
325 	/* Test1: read inclusion patterns from file */
326 	if (!assert((m = archive_match_new()) != NULL))
327 		return;
328 	assertEqualIntA(m, 0,
329 	    archive_match_include_pattern_from_file(m, "inclusion", 0));
330 	exclusion_from_file(m);
331 	/* Clean up. */
332 	archive_match_free(m);
333 
334 	/* Test2: read inclusion patterns in a null separator from file */
335 	if (!assert((m = archive_match_new()) != NULL))
336 		return;
337 	assertEqualIntA(m, 0,
338 	    archive_match_include_pattern_from_file(m, "inclusion_null", 1));
339 	exclusion_from_file(m);
340 	/* Clean up. */
341 	archive_match_free(m);
342 }
343 
344 static void
345 test_inclusion_from_file_wcs(void)
346 {
347 	struct archive *m;
348 
349 	/* Test1: read inclusion patterns from file */
350 	if (!assert((m = archive_match_new()) != NULL))
351 		return;
352 	/* Test for pattern reading from file */
353 	assertEqualIntA(m, 0,
354 	    archive_match_include_pattern_from_file_w(m, L"inclusion", 0));
355 	exclusion_from_file(m);
356 	/* Clean up. */
357 	archive_match_free(m);
358 
359 	/* Test2: read inclusion patterns in a null separator from file */
360 	if (!assert((m = archive_match_new()) != NULL))
361 		return;
362 	/* Test for pattern reading from file */
363 	assertEqualIntA(m, 0,
364 	    archive_match_include_pattern_from_file_w(m, L"inclusion_null", 1));
365 	exclusion_from_file(m);
366 	/* Clean up. */
367 	archive_match_free(m);
368 }
369 
370 static void
371 test_exclusion_and_inclusion(void)
372 {
373 	struct archive_entry *ae;
374 	struct archive *m;
375 	const char *mp;
376 	const wchar_t *wp;
377 
378 	if (!assert((m = archive_match_new()) != NULL))
379 		return;
380 	if (!assert((ae = archive_entry_new()) != NULL)) {
381 		archive_match_free(m);
382 		return;
383 	}
384 
385 	assertEqualIntA(m, 0, archive_match_exclude_pattern(m, "^aaa*"));
386 	assertEqualIntA(m, 0, archive_match_include_pattern_w(m, L"^aa*"));
387 	assertEqualIntA(m, 0, archive_match_include_pattern(m, "^a1*"));
388 
389 	/* Test with 'aa1234', which should not be excluded. */
390 	archive_entry_copy_pathname(ae, "aa1234");
391 	failure("'aa1234' should not be excluded");
392 	assertEqualInt(0, archive_match_path_excluded(m, ae));
393 	assertEqualInt(0, archive_match_excluded(m, ae));
394 	archive_entry_clear(ae);
395 	archive_entry_copy_pathname_w(ae, L"aa1234");
396 	failure("'aa1234' should not be excluded");
397 	assertEqualInt(0, archive_match_path_excluded(m, ae));
398 	assertEqualInt(0, archive_match_excluded(m, ae));
399 
400 	/* Test with 'aaa1234', which should be excluded. */
401 	archive_entry_copy_pathname(ae, "aaa1234");
402 	failure("'aaa1234' should be excluded");
403 	assertEqualInt(1, archive_match_path_excluded(m, ae));
404 	assertEqualInt(1, archive_match_excluded(m, ae));
405 	archive_entry_clear(ae);
406 	archive_entry_copy_pathname_w(ae, L"aaa1234");
407 	failure("'aaa1234' should be excluded");
408 	assertEqualInt(1, archive_match_path_excluded(m, ae));
409 	assertEqualInt(1, archive_match_excluded(m, ae));
410 
411 	/* Verify unmatched_inclusions. */
412 	assertEqualInt(1, archive_match_path_unmatched_inclusions(m));
413 	/* Verify unmatched inclusion patterns. */
414 	assertEqualIntA(m, ARCHIVE_OK,
415 	    archive_match_path_unmatched_inclusions_next(m, &mp));
416 	assertEqualString("^a1*", mp);
417 	assertEqualIntA(m, ARCHIVE_EOF,
418 	    archive_match_path_unmatched_inclusions_next(m, &mp));
419 	/* Verify unmatched inclusion patterns again in Wide-Char. */
420 	assertEqualIntA(m, ARCHIVE_OK,
421 	    archive_match_path_unmatched_inclusions_next_w(m, &wp));
422 	assertEqualWString(L"^a1*", wp);
423 	assertEqualIntA(m, ARCHIVE_EOF,
424 	    archive_match_path_unmatched_inclusions_next_w(m, &wp));
425 
426 	/* Clean up. */
427 	archive_entry_free(ae);
428 	archive_match_free(m);
429 }
430 
431 DEFINE_TEST(test_archive_match_path)
432 {
433 	/* Make exclusion sample files which contain exclusion patterns. */
434 	assertMakeFile("exclusion", 0666, "second\nfour\n");
435 	assertMakeBinFile("exclusion_null", 0666, 12, "second\0four\0");
436 	/* Make inclusion sample files which contain inclusion patterns. */
437 	assertMakeFile("inclusion", 0666, "first\nthird\n");
438 	assertMakeBinFile("inclusion_null", 0666, 12, "first\0third\0");
439 
440 	test_exclusion_mbs();
441 	test_exclusion_wcs();
442 	test_exclusion_from_file_mbs();
443 	test_exclusion_from_file_wcs();
444 	test_inclusion_mbs();
445 	test_inclusion_wcs();
446 	test_inclusion_from_file_mbs();
447 	test_inclusion_from_file_wcs();
448 	test_exclusion_and_inclusion();
449 }
450