1 /*-
2  * Copyright (c) 2011 Tim Kientzle
3  * Copyright (c) 2014 Michihiro NAKAJIMA
4  * All rights reserved.
5  *
6  * Redistribution and use in source and binary forms, with or without
7  * modification, are permitted provided that the following conditions
8  * are met:
9  * 1. Redistributions of source code must retain the above copyright
10  *    notice, this list of conditions and the following disclaimer.
11  * 2. Redistributions in binary form must reproduce the above copyright
12  *    notice, this list of conditions and the following disclaimer in the
13  *    documentation and/or other materials provided with the distribution.
14  *
15  * THIS SOFTWARE IS PROVIDED BY THE AUTHOR(S) ``AS IS'' AND ANY EXPRESS OR
16  * IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE IMPLIED WARRANTIES
17  * OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE ARE DISCLAIMED.
18  * IN NO EVENT SHALL THE AUTHOR(S) BE LIABLE FOR ANY DIRECT, INDIRECT,
19  * INCIDENTAL, SPECIAL, EXEMPLARY, OR CONSEQUENTIAL DAMAGES (INCLUDING, BUT
20  * NOT LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS OR SERVICES; LOSS OF USE,
21  * DATA, OR PROFITS; OR BUSINESS INTERRUPTION) HOWEVER CAUSED AND ON ANY
22  * THEORY OF LIABILITY, WHETHER IN CONTRACT, STRICT LIABILITY, OR TORT
23  * (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY OUT OF THE USE OF
24  * THIS SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF SUCH DAMAGE.
25  */
26 
27 #include "test.h"
28 __FBSDID("$FreeBSD$");
29 
30 struct archive_read;
31 extern void __archive_read_reset_passphrase(struct archive_read *);
32 extern const char * __archive_read_next_passphrase(struct archive_read *);
33 
34 static void
35 test(int pristine)
36 {
37 	struct archive* a = archive_read_new();
38 
39 	if (!pristine) {
40 		archive_read_support_filter_all(a);
41 		archive_read_support_format_all(a);
42         }
43 
44 	assertEqualInt(ARCHIVE_OK, archive_read_add_passphrase(a, "pass1"));
45 	/* An empty passphrase cannot be accepted. */
46 	assertEqualInt(ARCHIVE_FAILED, archive_read_add_passphrase(a, ""));
47 	/* NULL passphrases cannot be accepted. */
48 	assertEqualInt(ARCHIVE_FAILED, archive_read_add_passphrase(a, NULL));
49 
50 	archive_read_free(a);
51 }
52 
53 DEFINE_TEST(test_archive_read_add_passphrase)
54 {
55 	test(1);
56 	test(0);
57 }
58 
59 DEFINE_TEST(test_archive_read_add_passphrase_incorrect_sequance)
60 {
61 	struct archive* a = archive_read_new();
62 	struct archive_read *ar = (struct archive_read *)a;
63 
64 	assertEqualInt(ARCHIVE_OK, archive_read_add_passphrase(a, "pass1"));
65 
66 	/* No call of __archive_read_reset_passphrase() leads to
67 	 * get NULL even if a user has passed a passphrases. */
68 	assertEqualString(NULL, __archive_read_next_passphrase(ar));
69 
70 	archive_read_free(a);
71 }
72 
73 DEFINE_TEST(test_archive_read_add_passphrase_single)
74 {
75 	struct archive* a = archive_read_new();
76 	struct archive_read *ar = (struct archive_read *)a;
77 
78 	assertEqualInt(ARCHIVE_OK, archive_read_add_passphrase(a, "pass1"));
79 
80 	__archive_read_reset_passphrase(ar);
81 	/* Fist call, we should get "pass1" as a passphrase. */
82 	assertEqualString("pass1", __archive_read_next_passphrase(ar));
83 	/* Second call, we should get NULL which means all the passphrases
84 	 * are passed already. */
85 	assertEqualString(NULL, __archive_read_next_passphrase(ar));
86 
87 	archive_read_free(a);
88 }
89 
90 DEFINE_TEST(test_archive_read_add_passphrase_multiple)
91 {
92 	struct archive* a = archive_read_new();
93 	struct archive_read *ar = (struct archive_read *)a;
94 
95 	assertEqualInt(ARCHIVE_OK, archive_read_add_passphrase(a, "pass1"));
96 	assertEqualInt(ARCHIVE_OK, archive_read_add_passphrase(a, "pass2"));
97 
98 	__archive_read_reset_passphrase(ar);
99 	/* Fist call, we should get "pass1" as a passphrase. */
100 	assertEqualString("pass1", __archive_read_next_passphrase(ar));
101 	/* Second call, we should get "pass2" as a passphrase. */
102 	assertEqualString("pass2", __archive_read_next_passphrase(ar));
103 	/* Third call, we should get NULL which means all the passphrases
104 	 * are passed already. */
105 	assertEqualString(NULL, __archive_read_next_passphrase(ar));
106 
107 	archive_read_free(a);
108 }
109 
110 static const char *
111 callback1(struct archive *a, void *_client_data)
112 {
113 	(void)a; /* UNUSED */
114 	(void)_client_data; /* UNUSED */
115 	return ("passCallBack");
116 }
117 
118 DEFINE_TEST(test_archive_read_add_passphrase_set_callback1)
119 {
120 	struct archive* a = archive_read_new();
121 	struct archive_read *ar = (struct archive_read *)a;
122 
123 	assertEqualInt(ARCHIVE_OK,
124 	    archive_read_set_passphrase_callback(a, NULL, callback1));
125 
126 	__archive_read_reset_passphrase(ar);
127 	/* Fist call, we should get "passCallBack" as a passphrase. */
128 	assertEqualString("passCallBack", __archive_read_next_passphrase(ar));
129 	/* Second call, we still get "passCallBack" as a passphrase. */
130 	assertEqualString("passCallBack", __archive_read_next_passphrase(ar));
131 
132 	archive_read_free(a);
133 
134 	/* Without __archive_read_reset_passphrase call, the callback
135 	 * should work fine. */
136 	a = archive_read_new();
137 	ar = (struct archive_read *)a;
138 	assertEqualInt(ARCHIVE_OK,
139 	    archive_read_set_passphrase_callback(a, NULL, callback1));
140 	/* Fist call, we should get "passCallBack" as a passphrase. */
141 	assertEqualString("passCallBack", __archive_read_next_passphrase(ar));
142 	/* Second call, we still get "passCallBack" as a passphrase. */
143 	assertEqualString("passCallBack", __archive_read_next_passphrase(ar));
144 
145 	archive_read_free(a);
146 }
147 
148 static const char *
149 callback2(struct archive *a, void *_client_data)
150 {
151 	int *cd = (int *)_client_data;
152 
153 	(void)a; /* UNUSED */
154 
155 	if (*cd == 0) {
156 		*cd = 1;
157 		return ("passCallBack");
158 	}
159 	return (NULL);
160 }
161 
162 DEFINE_TEST(test_archive_read_add_passphrase_set_callback2)
163 {
164 	struct archive* a = archive_read_new();
165 	struct archive_read *ar = (struct archive_read *)a;
166 	int client_data = 0;
167 
168 	assertEqualInt(ARCHIVE_OK,
169 	    archive_read_set_passphrase_callback(a, &client_data, callback2));
170 
171 	__archive_read_reset_passphrase(ar);
172 	/* Fist call, we should get "passCallBack" as a passphrase. */
173 	assertEqualString("passCallBack", __archive_read_next_passphrase(ar));
174 	/* Second call, we should get NULL which means all the passphrases
175 	 * are passed already. */
176 	assertEqualString(NULL, __archive_read_next_passphrase(ar));
177 
178 	archive_read_free(a);
179 }
180 
181 DEFINE_TEST(test_archive_read_add_passphrase_set_callback3)
182 {
183 	struct archive* a = archive_read_new();
184 	struct archive_read *ar = (struct archive_read *)a;
185 	int client_data = 0;
186 
187 	assertEqualInt(ARCHIVE_OK,
188 	    archive_read_set_passphrase_callback(a, &client_data, callback2));
189 
190 	__archive_read_reset_passphrase(ar);
191 	/* Fist call, we should get "passCallBack" as a passphrase. */
192 	assertEqualString("passCallBack", __archive_read_next_passphrase(ar));
193 	__archive_read_reset_passphrase(ar);
194 	/* After reset passphrase, we should get "passCallBack" passphrase. */
195 	assertEqualString("passCallBack", __archive_read_next_passphrase(ar));
196 	/* Second call, we should get NULL which means all the passphrases
197 	 * are passed already. */
198 	assertEqualString(NULL, __archive_read_next_passphrase(ar));
199 
200 	archive_read_free(a);
201 }
202 
203 DEFINE_TEST(test_archive_read_add_passphrase_multiple_with_callback)
204 {
205 	struct archive* a = archive_read_new();
206 	struct archive_read *ar = (struct archive_read *)a;
207 	int client_data = 0;
208 
209 	assertEqualInt(ARCHIVE_OK, archive_read_add_passphrase(a, "pass1"));
210 	assertEqualInt(ARCHIVE_OK, archive_read_add_passphrase(a, "pass2"));
211 	assertEqualInt(ARCHIVE_OK,
212 	    archive_read_set_passphrase_callback(a, &client_data, callback2));
213 
214 	__archive_read_reset_passphrase(ar);
215 	/* Fist call, we should get "pass1" as a passphrase. */
216 	assertEqualString("pass1", __archive_read_next_passphrase(ar));
217 	/* Second call, we should get "pass2" as a passphrase. */
218 	assertEqualString("pass2", __archive_read_next_passphrase(ar));
219 	/* Third call, we should get "passCallBack" as a passphrase. */
220 	assertEqualString("passCallBack", __archive_read_next_passphrase(ar));
221 	/* Fourth call, we should get NULL which means all the passphrases
222 	 * are passed already. */
223 	assertEqualString(NULL, __archive_read_next_passphrase(ar));
224 
225 	archive_read_free(a);
226 }
227 
228 DEFINE_TEST(test_archive_read_add_passphrase_multiple_with_callback2)
229 {
230 	struct archive* a = archive_read_new();
231 	struct archive_read *ar = (struct archive_read *)a;
232 	int client_data = 0;
233 
234 	assertEqualInt(ARCHIVE_OK, archive_read_add_passphrase(a, "pass1"));
235 	assertEqualInt(ARCHIVE_OK, archive_read_add_passphrase(a, "pass2"));
236 	assertEqualInt(ARCHIVE_OK,
237 	    archive_read_set_passphrase_callback(a, &client_data, callback2));
238 
239 	__archive_read_reset_passphrase(ar);
240 	/* Fist call, we should get "pass1" as a passphrase. */
241 	assertEqualString("pass1", __archive_read_next_passphrase(ar));
242 	/* Second call, we should get "pass2" as a passphrase. */
243 	assertEqualString("pass2", __archive_read_next_passphrase(ar));
244 	/* Third call, we should get "passCallBack" as a passphrase. */
245 	assertEqualString("passCallBack", __archive_read_next_passphrase(ar));
246 
247 	__archive_read_reset_passphrase(ar);
248 	/* After reset passphrase, we should get "passCallBack" passphrase. */
249 	assertEqualString("passCallBack", __archive_read_next_passphrase(ar));
250 	/* Second call, we should get "pass1" as a passphrase. */
251 	assertEqualString("pass1", __archive_read_next_passphrase(ar));
252 	/* Third call, we should get "passCallBack" as a passphrase. */
253 	assertEqualString("pass2", __archive_read_next_passphrase(ar));
254 	/* Fourth call, we should get NULL which means all the passphrases
255 	 * are passed already. */
256 	assertEqualString(NULL, __archive_read_next_passphrase(ar));
257 
258 	archive_read_free(a);
259 }
260 
261