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