xref: /freebsd/contrib/libarchive/libarchive/test/test_archive_read_add_passphrase.c (revision b9128a37faafede823eb456aa65a11ac69997284)
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 
29 struct archive_read;
30 extern void __archive_read_reset_passphrase(struct archive_read *);
31 extern const char * __archive_read_next_passphrase(struct archive_read *);
32 
33 static void
34 test(int pristine)
35 {
36 	struct archive* a = archive_read_new();
37 
38 	if (!pristine) {
39 		archive_read_support_filter_all(a);
40 		archive_read_support_format_all(a);
41         }
42 
43 	assertEqualInt(ARCHIVE_OK, archive_read_add_passphrase(a, "pass1"));
44 	/* An empty passphrase cannot be accepted. */
45 	assertEqualInt(ARCHIVE_FAILED, archive_read_add_passphrase(a, ""));
46 	/* NULL passphrases cannot be accepted. */
47 	assertEqualInt(ARCHIVE_FAILED, archive_read_add_passphrase(a, NULL));
48 
49 	archive_read_free(a);
50 }
51 
52 DEFINE_TEST(test_archive_read_add_passphrase)
53 {
54 	test(1);
55 	test(0);
56 }
57 
58 DEFINE_TEST(test_archive_read_add_passphrase_incorrect_sequance)
59 {
60 	struct archive* a = archive_read_new();
61 	struct archive_read *ar = (struct archive_read *)a;
62 
63 	assertEqualInt(ARCHIVE_OK, archive_read_add_passphrase(a, "pass1"));
64 
65 	/* No call of __archive_read_reset_passphrase() leads to
66 	 * get NULL even if a user has passed a passphrases. */
67 	assertEqualString(NULL, __archive_read_next_passphrase(ar));
68 
69 	archive_read_free(a);
70 }
71 
72 DEFINE_TEST(test_archive_read_add_passphrase_single)
73 {
74 	struct archive* a = archive_read_new();
75 	struct archive_read *ar = (struct archive_read *)a;
76 
77 	assertEqualInt(ARCHIVE_OK, archive_read_add_passphrase(a, "pass1"));
78 
79 	__archive_read_reset_passphrase(ar);
80 	/* Fist call, we should get "pass1" as a passphrase. */
81 	assertEqualString("pass1", __archive_read_next_passphrase(ar));
82 	/* Second call, we should get NULL which means all the passphrases
83 	 * are passed already. */
84 	assertEqualString(NULL, __archive_read_next_passphrase(ar));
85 
86 	archive_read_free(a);
87 }
88 
89 DEFINE_TEST(test_archive_read_add_passphrase_multiple)
90 {
91 	struct archive* a = archive_read_new();
92 	struct archive_read *ar = (struct archive_read *)a;
93 
94 	assertEqualInt(ARCHIVE_OK, archive_read_add_passphrase(a, "pass1"));
95 	assertEqualInt(ARCHIVE_OK, archive_read_add_passphrase(a, "pass2"));
96 
97 	__archive_read_reset_passphrase(ar);
98 	/* Fist call, we should get "pass1" as a passphrase. */
99 	assertEqualString("pass1", __archive_read_next_passphrase(ar));
100 	/* Second call, we should get "pass2" as a passphrase. */
101 	assertEqualString("pass2", __archive_read_next_passphrase(ar));
102 	/* Third call, we should get NULL which means all the passphrases
103 	 * are passed already. */
104 	assertEqualString(NULL, __archive_read_next_passphrase(ar));
105 
106 	archive_read_free(a);
107 }
108 
109 static const char *
110 callback1(struct archive *a, void *_client_data)
111 {
112 	(void)a; /* UNUSED */
113 	(void)_client_data; /* UNUSED */
114 	return ("passCallBack");
115 }
116 
117 DEFINE_TEST(test_archive_read_add_passphrase_set_callback1)
118 {
119 	struct archive* a = archive_read_new();
120 	struct archive_read *ar = (struct archive_read *)a;
121 
122 	assertEqualInt(ARCHIVE_OK,
123 	    archive_read_set_passphrase_callback(a, NULL, callback1));
124 
125 	__archive_read_reset_passphrase(ar);
126 	/* Fist call, we should get "passCallBack" as a passphrase. */
127 	assertEqualString("passCallBack", __archive_read_next_passphrase(ar));
128 	/* Second call, we still get "passCallBack" as a passphrase. */
129 	assertEqualString("passCallBack", __archive_read_next_passphrase(ar));
130 
131 	archive_read_free(a);
132 
133 	/* Without __archive_read_reset_passphrase call, the callback
134 	 * should work fine. */
135 	a = archive_read_new();
136 	ar = (struct archive_read *)a;
137 	assertEqualInt(ARCHIVE_OK,
138 	    archive_read_set_passphrase_callback(a, NULL, callback1));
139 	/* Fist call, we should get "passCallBack" as a passphrase. */
140 	assertEqualString("passCallBack", __archive_read_next_passphrase(ar));
141 	/* Second call, we still get "passCallBack" as a passphrase. */
142 	assertEqualString("passCallBack", __archive_read_next_passphrase(ar));
143 
144 	archive_read_free(a);
145 }
146 
147 static const char *
148 callback2(struct archive *a, void *_client_data)
149 {
150 	int *cd = (int *)_client_data;
151 
152 	(void)a; /* UNUSED */
153 
154 	if (*cd == 0) {
155 		*cd = 1;
156 		return ("passCallBack");
157 	}
158 	return (NULL);
159 }
160 
161 DEFINE_TEST(test_archive_read_add_passphrase_set_callback2)
162 {
163 	struct archive* a = archive_read_new();
164 	struct archive_read *ar = (struct archive_read *)a;
165 	int client_data = 0;
166 
167 	assertEqualInt(ARCHIVE_OK,
168 	    archive_read_set_passphrase_callback(a, &client_data, callback2));
169 
170 	__archive_read_reset_passphrase(ar);
171 	/* Fist call, we should get "passCallBack" as a passphrase. */
172 	assertEqualString("passCallBack", __archive_read_next_passphrase(ar));
173 	/* Second call, we should get NULL which means all the passphrases
174 	 * are passed already. */
175 	assertEqualString(NULL, __archive_read_next_passphrase(ar));
176 
177 	archive_read_free(a);
178 }
179 
180 DEFINE_TEST(test_archive_read_add_passphrase_set_callback3)
181 {
182 	struct archive* a = archive_read_new();
183 	struct archive_read *ar = (struct archive_read *)a;
184 	int client_data = 0;
185 
186 	assertEqualInt(ARCHIVE_OK,
187 	    archive_read_set_passphrase_callback(a, &client_data, callback2));
188 
189 	__archive_read_reset_passphrase(ar);
190 	/* Fist call, we should get "passCallBack" as a passphrase. */
191 	assertEqualString("passCallBack", __archive_read_next_passphrase(ar));
192 	__archive_read_reset_passphrase(ar);
193 	/* After reset passphrase, we should get "passCallBack" passphrase. */
194 	assertEqualString("passCallBack", __archive_read_next_passphrase(ar));
195 	/* Second call, we should get NULL which means all the passphrases
196 	 * are passed already. */
197 	assertEqualString(NULL, __archive_read_next_passphrase(ar));
198 
199 	archive_read_free(a);
200 }
201 
202 DEFINE_TEST(test_archive_read_add_passphrase_multiple_with_callback)
203 {
204 	struct archive* a = archive_read_new();
205 	struct archive_read *ar = (struct archive_read *)a;
206 	int client_data = 0;
207 
208 	assertEqualInt(ARCHIVE_OK, archive_read_add_passphrase(a, "pass1"));
209 	assertEqualInt(ARCHIVE_OK, archive_read_add_passphrase(a, "pass2"));
210 	assertEqualInt(ARCHIVE_OK,
211 	    archive_read_set_passphrase_callback(a, &client_data, callback2));
212 
213 	__archive_read_reset_passphrase(ar);
214 	/* Fist call, we should get "pass1" as a passphrase. */
215 	assertEqualString("pass1", __archive_read_next_passphrase(ar));
216 	/* Second call, we should get "pass2" as a passphrase. */
217 	assertEqualString("pass2", __archive_read_next_passphrase(ar));
218 	/* Third call, we should get "passCallBack" as a passphrase. */
219 	assertEqualString("passCallBack", __archive_read_next_passphrase(ar));
220 	/* Fourth call, we should get NULL which means all the passphrases
221 	 * are passed already. */
222 	assertEqualString(NULL, __archive_read_next_passphrase(ar));
223 
224 	archive_read_free(a);
225 }
226 
227 DEFINE_TEST(test_archive_read_add_passphrase_multiple_with_callback2)
228 {
229 	struct archive* a = archive_read_new();
230 	struct archive_read *ar = (struct archive_read *)a;
231 	int client_data = 0;
232 
233 	assertEqualInt(ARCHIVE_OK, archive_read_add_passphrase(a, "pass1"));
234 	assertEqualInt(ARCHIVE_OK, archive_read_add_passphrase(a, "pass2"));
235 	assertEqualInt(ARCHIVE_OK,
236 	    archive_read_set_passphrase_callback(a, &client_data, callback2));
237 
238 	__archive_read_reset_passphrase(ar);
239 	/* Fist call, we should get "pass1" as a passphrase. */
240 	assertEqualString("pass1", __archive_read_next_passphrase(ar));
241 	/* Second call, we should get "pass2" as a passphrase. */
242 	assertEqualString("pass2", __archive_read_next_passphrase(ar));
243 	/* Third call, we should get "passCallBack" as a passphrase. */
244 	assertEqualString("passCallBack", __archive_read_next_passphrase(ar));
245 
246 	__archive_read_reset_passphrase(ar);
247 	/* After reset passphrase, we should get "passCallBack" passphrase. */
248 	assertEqualString("passCallBack", __archive_read_next_passphrase(ar));
249 	/* Second call, we should get "pass1" as a passphrase. */
250 	assertEqualString("pass1", __archive_read_next_passphrase(ar));
251 	/* Third call, we should get "passCallBack" as a passphrase. */
252 	assertEqualString("pass2", __archive_read_next_passphrase(ar));
253 	/* Fourth call, we should get NULL which means all the passphrases
254 	 * are passed already. */
255 	assertEqualString(NULL, __archive_read_next_passphrase(ar));
256 
257 	archive_read_free(a);
258 }
259 
260