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 __FBSDID("$FreeBSD$");
27
28 /*
29 * Verify that the various archive_read_support_* functions
30 * return appropriate errors when invoked on the wrong kind of
31 * archive handle.
32 */
33
34 typedef struct archive *constructor(void);
35 typedef int enabler(struct archive *);
36 typedef int destructor(struct archive *);
37
38 static void
test_success(constructor new_,enabler enable_,destructor free_)39 test_success(constructor new_, enabler enable_, destructor free_)
40 {
41 struct archive *a = new_();
42 int result = enable_(a);
43 if (result == ARCHIVE_WARN) {
44 assert(NULL != archive_error_string(a));
45 assertEqualIntA(a, -1, archive_errno(a));
46 } else {
47 assertEqualIntA(a, ARCHIVE_OK, result);
48 assert(NULL == archive_error_string(a));
49 assertEqualIntA(a, 0, archive_errno(a));
50 }
51 free_(a);
52 }
53
54 static void
test_failure(constructor new_,enabler enable_,destructor free_)55 test_failure(constructor new_, enabler enable_, destructor free_)
56 {
57 struct archive *a = new_();
58 assertEqualIntA(a, ARCHIVE_FATAL, enable_(a));
59 assert(NULL != archive_error_string(a));
60 assertEqualIntA(a, -1, archive_errno(a));
61 free_(a);
62 }
63
64 static void
test_filter_or_format(enabler enable)65 test_filter_or_format(enabler enable)
66 {
67 test_success(archive_read_new, enable, archive_read_free);
68 test_failure(archive_write_new, enable, archive_write_free);
69 test_failure(archive_read_disk_new, enable, archive_read_free);
70 test_failure(archive_write_disk_new, enable, archive_write_free);
71 }
72
DEFINE_TEST(test_archive_read_support)73 DEFINE_TEST(test_archive_read_support)
74 {
75 test_filter_or_format(archive_read_support_format_7zip);
76 test_filter_or_format(archive_read_support_format_all);
77 test_filter_or_format(archive_read_support_format_ar);
78 test_filter_or_format(archive_read_support_format_cab);
79 test_filter_or_format(archive_read_support_format_cpio);
80 test_filter_or_format(archive_read_support_format_empty);
81 test_filter_or_format(archive_read_support_format_iso9660);
82 test_filter_or_format(archive_read_support_format_lha);
83 test_filter_or_format(archive_read_support_format_mtree);
84 test_filter_or_format(archive_read_support_format_tar);
85 test_filter_or_format(archive_read_support_format_xar);
86 test_filter_or_format(archive_read_support_format_zip);
87
88 test_filter_or_format(archive_read_support_filter_all);
89 test_filter_or_format(archive_read_support_filter_bzip2);
90 test_filter_or_format(archive_read_support_filter_compress);
91 test_filter_or_format(archive_read_support_filter_gzip);
92 test_filter_or_format(archive_read_support_filter_lzip);
93 test_filter_or_format(archive_read_support_filter_lzma);
94 test_filter_or_format(archive_read_support_filter_none);
95 test_filter_or_format(archive_read_support_filter_rpm);
96 test_filter_or_format(archive_read_support_filter_uu);
97 test_filter_or_format(archive_read_support_filter_xz);
98 }
99