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(__a, __code, __opts) \
29 assertEqualInt(__code, archive_read_set_options(__a, __opts))
30 
31 static void
32 test(int pristine)
33 {
34 	struct archive* a = archive_read_new();
35 	int halfempty_options_rv = pristine ? ARCHIVE_FAILED : ARCHIVE_OK;
36 	int known_option_rv = pristine ? ARCHIVE_FAILED : ARCHIVE_OK;
37 
38 	if (!pristine) {
39 		archive_read_support_filter_all(a);
40 		archive_read_support_format_all(a);
41 	}
42 
43 	/* NULL and "" denote `no option', so they're ok no matter
44 	 * what, if any, formats are registered */
45 	should(a, ARCHIVE_OK, NULL);
46 	should(a, ARCHIVE_OK, "");
47 
48 	/* unknown modules and options */
49 	should(a, ARCHIVE_FAILED, "fubar:snafu");
50 	assertEqualString("Unknown module name: `fubar'",
51 	    archive_error_string(a));
52 	should(a, ARCHIVE_FAILED, "fubar:snafu=betcha");
53 	assertEqualString("Unknown module name: `fubar'",
54 	    archive_error_string(a));
55 
56 	/* unknown modules and options */
57 	should(a, ARCHIVE_FAILED, "snafu");
58 	assertEqualString("Undefined option: `snafu'",
59 	    archive_error_string(a));
60 	should(a, ARCHIVE_FAILED, "snafu=betcha");
61 	assertEqualString("Undefined option: `snafu'",
62 	    archive_error_string(a));
63 
64 	/* ARCHIVE_OK with iso9660 loaded, ARCHIVE_FAILED otherwise */
65 	should(a, known_option_rv, "iso9660:joliet");
66 	if (pristine) {
67 		assertEqualString("Unknown module name: `iso9660'",
68 		    archive_error_string(a));
69 	}
70 	should(a, known_option_rv, "iso9660:joliet");
71 	if (pristine) {
72 		assertEqualString("Unknown module name: `iso9660'",
73 		    archive_error_string(a));
74 	}
75 	should(a, known_option_rv, "joliet");
76 	if (pristine) {
77 		assertEqualString("Undefined option: `joliet'",
78 		    archive_error_string(a));
79 	}
80 	should(a, known_option_rv, "!joliet");
81 	if (pristine) {
82 		assertEqualString("Undefined option: `joliet'",
83 		    archive_error_string(a));
84 	}
85 
86 	should(a, ARCHIVE_OK, ",");
87 	should(a, ARCHIVE_OK, ",,");
88 
89 	should(a, halfempty_options_rv, ",joliet");
90 	if (pristine) {
91 		assertEqualString("Undefined option: `joliet'",
92 		    archive_error_string(a));
93 	}
94 	should(a, halfempty_options_rv, "joliet,");
95 	if (pristine) {
96 		assertEqualString("Undefined option: `joliet'",
97 		    archive_error_string(a));
98 	}
99 
100 	should(a, ARCHIVE_FAILED, "joliet,snafu");
101 	if (pristine) {
102 		assertEqualString("Undefined option: `joliet'",
103 		    archive_error_string(a));
104 	} else {
105 		assertEqualString("Undefined option: `snafu'",
106 		    archive_error_string(a));
107 	}
108 
109 	should(a, ARCHIVE_FAILED, "iso9660:snafu");
110 	if (pristine) {
111 		assertEqualString("Unknown module name: `iso9660'",
112 		    archive_error_string(a));
113 	} else {
114 		assertEqualString("Undefined option: `iso9660:snafu'",
115 		    archive_error_string(a));
116 	}
117 
118 	archive_read_free(a);
119 }
120 
121 DEFINE_TEST(test_archive_read_set_options)
122 {
123 	test(1);
124 	test(0);
125 }
126