2 * Copyright (c) 2011 Tim Kientzle
3 * Copyright (c) 2014 Michihiro NAKAJIMA
6 * Redistribution and use in source and binary forms, with or without
7 * modification, are permitted provided that the following conditions
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.
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.
28 __FBSDID("$FreeBSD$");
31 extern void __archive_read_reset_passphrase(struct archive_read *);
32 extern const char * __archive_read_next_passphrase(struct archive_read *);
37 struct archive* a = archive_read_new();
40 archive_read_support_filter_all(a);
41 archive_read_support_format_all(a);
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));
53 DEFINE_TEST(test_archive_read_add_passphrase)
59 DEFINE_TEST(test_archive_read_add_passphrase_incorrect_sequance)
61 struct archive* a = archive_read_new();
62 struct archive_read *ar = (struct archive_read *)a;
64 assertEqualInt(ARCHIVE_OK, archive_read_add_passphrase(a, "pass1"));
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));
73 DEFINE_TEST(test_archive_read_add_passphrase_single)
75 struct archive* a = archive_read_new();
76 struct archive_read *ar = (struct archive_read *)a;
78 assertEqualInt(ARCHIVE_OK, archive_read_add_passphrase(a, "pass1"));
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));
90 DEFINE_TEST(test_archive_read_add_passphrase_multiple)
92 struct archive* a = archive_read_new();
93 struct archive_read *ar = (struct archive_read *)a;
95 assertEqualInt(ARCHIVE_OK, archive_read_add_passphrase(a, "pass1"));
96 assertEqualInt(ARCHIVE_OK, archive_read_add_passphrase(a, "pass2"));
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));
107 archive_read_free(a);
111 callback1(struct archive *a, void *_client_data)
113 (void)a; /* UNUSED */
114 (void)_client_data; /* UNUSED */
115 return ("passCallBack");
118 DEFINE_TEST(test_archive_read_add_passphrase_set_callback1)
120 struct archive* a = archive_read_new();
121 struct archive_read *ar = (struct archive_read *)a;
123 assertEqualInt(ARCHIVE_OK,
124 archive_read_set_passphrase_callback(a, NULL, callback1));
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));
132 archive_read_free(a);
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));
145 archive_read_free(a);
149 callback2(struct archive *a, void *_client_data)
151 int *cd = (int *)_client_data;
153 (void)a; /* UNUSED */
157 return ("passCallBack");
162 DEFINE_TEST(test_archive_read_add_passphrase_set_callback2)
164 struct archive* a = archive_read_new();
165 struct archive_read *ar = (struct archive_read *)a;
168 assertEqualInt(ARCHIVE_OK,
169 archive_read_set_passphrase_callback(a, &client_data, callback2));
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));
178 archive_read_free(a);
181 DEFINE_TEST(test_archive_read_add_passphrase_set_callback3)
183 struct archive* a = archive_read_new();
184 struct archive_read *ar = (struct archive_read *)a;
187 assertEqualInt(ARCHIVE_OK,
188 archive_read_set_passphrase_callback(a, &client_data, callback2));
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));
200 archive_read_free(a);
203 DEFINE_TEST(test_archive_read_add_passphrase_multiple_with_callback)
205 struct archive* a = archive_read_new();
206 struct archive_read *ar = (struct archive_read *)a;
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));
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));
225 archive_read_free(a);
228 DEFINE_TEST(test_archive_read_add_passphrase_multiple_with_callback2)
230 struct archive* a = archive_read_new();
231 struct archive_read *ar = (struct archive_read *)a;
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));
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));
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));
258 archive_read_free(a);