1 /*-
2  * Copyright (c) 2012 Michihiro NAKAJIMA
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 __FBSDID("$FreeBSD$");
28 
29 static void
30 test_uid(void)
31 {
32 	struct archive_entry *ae;
33 	struct archive *m;
34 
35 	if (!assert((m = archive_match_new()) != NULL))
36 		return;
37 	if (!assert((ae = archive_entry_new()) != NULL)) {
38 		archive_match_free(m);
39 		return;
40 	}
41 
42 	assertEqualIntA(m, 0, archive_match_include_uid(m, 1000));
43 	assertEqualIntA(m, 0, archive_match_include_uid(m, 1002));
44 
45 	archive_entry_set_uid(ae, 0);
46 	failure("uid 0 should be excluded");
47 	assertEqualInt(1, archive_match_owner_excluded(m, ae));
48 	assertEqualInt(1, archive_match_excluded(m, ae));
49 	archive_entry_set_uid(ae, 1000);
50 	failure("uid 1000 should not be excluded");
51 	assertEqualInt(0, archive_match_owner_excluded(m, ae));
52 	assertEqualInt(0, archive_match_excluded(m, ae));
53 	archive_entry_set_uid(ae, 1001);
54 	failure("uid 1001 should be excluded");
55 	assertEqualInt(1, archive_match_owner_excluded(m, ae));
56 	assertEqualInt(1, archive_match_excluded(m, ae));
57 	archive_entry_set_uid(ae, 1002);
58 	failure("uid 1002 should not be excluded");
59 	assertEqualInt(0, archive_match_owner_excluded(m, ae));
60 	assertEqualInt(0, archive_match_excluded(m, ae));
61 	archive_entry_set_uid(ae, 1003);
62 	failure("uid 1003 should be excluded");
63 	assertEqualInt(1, archive_match_owner_excluded(m, ae));
64 	assertEqualInt(1, archive_match_excluded(m, ae));
65 
66 	/* Clean up. */
67 	archive_entry_free(ae);
68 	archive_match_free(m);
69 }
70 
71 static void
72 test_gid(void)
73 {
74 	struct archive_entry *ae;
75 	struct archive *m;
76 
77 	if (!assert((m = archive_match_new()) != NULL))
78 		return;
79 	if (!assert((ae = archive_entry_new()) != NULL)) {
80 		archive_match_free(m);
81 		return;
82 	}
83 
84 	assertEqualIntA(m, 0, archive_match_include_gid(m, 1000));
85 	assertEqualIntA(m, 0, archive_match_include_gid(m, 1002));
86 
87 	archive_entry_set_gid(ae, 0);
88 	failure("uid 0 should be excluded");
89 	assertEqualInt(1, archive_match_owner_excluded(m, ae));
90 	assertEqualInt(1, archive_match_excluded(m, ae));
91 	archive_entry_set_gid(ae, 1000);
92 	failure("uid 1000 should not be excluded");
93 	assertEqualInt(0, archive_match_owner_excluded(m, ae));
94 	assertEqualInt(0, archive_match_excluded(m, ae));
95 	archive_entry_set_gid(ae, 1001);
96 	failure("uid 1001 should be excluded");
97 	assertEqualInt(1, archive_match_owner_excluded(m, ae));
98 	assertEqualInt(1, archive_match_excluded(m, ae));
99 	archive_entry_set_gid(ae, 1002);
100 	failure("uid 1002 should not be excluded");
101 	assertEqualInt(0, archive_match_owner_excluded(m, ae));
102 	assertEqualInt(0, archive_match_excluded(m, ae));
103 	archive_entry_set_gid(ae, 1003);
104 	failure("uid 1003 should be excluded");
105 	assertEqualInt(1, archive_match_owner_excluded(m, ae));
106 	assertEqualInt(1, archive_match_excluded(m, ae));
107 
108 	/* Clean up. */
109 	archive_entry_free(ae);
110 	archive_match_free(m);
111 }
112 
113 static void
114 test_uname_mbs(void)
115 {
116 	struct archive_entry *ae;
117 	struct archive *m;
118 
119 	if (!assert((m = archive_match_new()) != NULL))
120 		return;
121 	if (!assert((ae = archive_entry_new()) != NULL)) {
122 		archive_match_free(m);
123 		return;
124 	}
125 
126 	assertEqualIntA(m, 0, archive_match_include_uname(m, "foo"));
127 	assertEqualIntA(m, 0, archive_match_include_uname(m, "bar"));
128 
129 	archive_entry_copy_uname(ae, "unknown");
130 	failure("User 'unknown' should be excluded");
131 	assertEqualInt(1, archive_match_owner_excluded(m, ae));
132 	assertEqualInt(1, archive_match_excluded(m, ae));
133 	archive_entry_copy_uname(ae, "foo");
134 	failure("User 'foo' should not be excluded");
135 	assertEqualInt(0, archive_match_owner_excluded(m, ae));
136 	assertEqualInt(0, archive_match_excluded(m, ae));
137 	archive_entry_copy_uname(ae, "foo1");
138 	failure("User 'foo1' should be excluded");
139 	assertEqualInt(1, archive_match_owner_excluded(m, ae));
140 	assertEqualInt(1, archive_match_excluded(m, ae));
141 	archive_entry_copy_uname(ae, "bar");
142 	failure("User 'bar' should not be excluded");
143 	assertEqualInt(0, archive_match_owner_excluded(m, ae));
144 	assertEqualInt(0, archive_match_excluded(m, ae));
145 	archive_entry_copy_uname(ae, "bar1");
146 	failure("User 'bar1' should be excluded");
147 	assertEqualInt(1, archive_match_owner_excluded(m, ae));
148 	assertEqualInt(1, archive_match_excluded(m, ae));
149 
150 	/* Clean up. */
151 	archive_entry_free(ae);
152 	archive_match_free(m);
153 }
154 
155 static void
156 test_uname_wcs(void)
157 {
158 	struct archive_entry *ae;
159 	struct archive *m;
160 
161 	if (!assert((m = archive_match_new()) != NULL))
162 		return;
163 	if (!assert((ae = archive_entry_new()) != NULL)) {
164 		archive_match_free(m);
165 		return;
166 	}
167 
168 	assertEqualIntA(m, 0, archive_match_include_uname_w(m, L"foo"));
169 	assertEqualIntA(m, 0, archive_match_include_uname_w(m, L"bar"));
170 
171 	archive_entry_copy_uname_w(ae, L"unknown");
172 	failure("User 'unknown' should be excluded");
173 	assertEqualInt(1, archive_match_owner_excluded(m, ae));
174 	assertEqualInt(1, archive_match_excluded(m, ae));
175 	archive_entry_copy_uname_w(ae, L"foo");
176 	failure("User 'foo' should not be excluded");
177 	assertEqualInt(0, archive_match_owner_excluded(m, ae));
178 	assertEqualInt(0, archive_match_excluded(m, ae));
179 	archive_entry_copy_uname_w(ae, L"foo1");
180 	failure("User 'foo1' should be excluded");
181 	assertEqualInt(1, archive_match_owner_excluded(m, ae));
182 	assertEqualInt(1, archive_match_excluded(m, ae));
183 	archive_entry_copy_uname_w(ae, L"bar");
184 	failure("User 'bar' should not be excluded");
185 	assertEqualInt(0, archive_match_owner_excluded(m, ae));
186 	assertEqualInt(0, archive_match_excluded(m, ae));
187 	archive_entry_copy_uname_w(ae, L"bar1");
188 	failure("User 'bar1' should be excluded");
189 	assertEqualInt(1, archive_match_owner_excluded(m, ae));
190 	assertEqualInt(1, archive_match_excluded(m, ae));
191 
192 	/* Clean up. */
193 	archive_entry_free(ae);
194 	archive_match_free(m);
195 }
196 
197 static void
198 test_gname_mbs(void)
199 {
200 	struct archive_entry *ae;
201 	struct archive *m;
202 
203 	if (!assert((m = archive_match_new()) != NULL))
204 		return;
205 	if (!assert((ae = archive_entry_new()) != NULL)) {
206 		archive_match_free(m);
207 		return;
208 	}
209 
210 	assertEqualIntA(m, 0, archive_match_include_gname(m, "foo"));
211 	assertEqualIntA(m, 0, archive_match_include_gname(m, "bar"));
212 
213 	archive_entry_copy_gname(ae, "unknown");
214 	failure("Group 'unknown' should be excluded");
215 	assertEqualInt(1, archive_match_owner_excluded(m, ae));
216 	assertEqualInt(1, archive_match_excluded(m, ae));
217 	archive_entry_copy_gname(ae, "foo");
218 	failure("Group 'foo' should not be excluded");
219 	assertEqualInt(0, archive_match_owner_excluded(m, ae));
220 	assertEqualInt(0, archive_match_excluded(m, ae));
221 	archive_entry_copy_gname(ae, "foo1");
222 	failure("Group 'foo1' should be excluded");
223 	assertEqualInt(1, archive_match_owner_excluded(m, ae));
224 	assertEqualInt(1, archive_match_excluded(m, ae));
225 	archive_entry_copy_gname(ae, "bar");
226 	failure("Group 'bar' should not be excluded");
227 	assertEqualInt(0, archive_match_owner_excluded(m, ae));
228 	assertEqualInt(0, archive_match_excluded(m, ae));
229 	archive_entry_copy_gname(ae, "bar1");
230 	failure("Group 'bar1' should be excluded");
231 	assertEqualInt(1, archive_match_owner_excluded(m, ae));
232 	assertEqualInt(1, archive_match_excluded(m, ae));
233 
234 	/* Clean up. */
235 	archive_entry_free(ae);
236 	archive_match_free(m);
237 }
238 
239 static void
240 test_gname_wcs(void)
241 {
242 	struct archive_entry *ae;
243 	struct archive *m;
244 
245 	if (!assert((m = archive_match_new()) != NULL))
246 		return;
247 	if (!assert((ae = archive_entry_new()) != NULL)) {
248 		archive_match_free(m);
249 		return;
250 	}
251 
252 	assertEqualIntA(m, 0, archive_match_include_gname_w(m, L"foo"));
253 	assertEqualIntA(m, 0, archive_match_include_gname_w(m, L"bar"));
254 
255 	archive_entry_copy_gname_w(ae, L"unknown");
256 	failure("Group 'unknown' should be excluded");
257 	assertEqualInt(1, archive_match_owner_excluded(m, ae));
258 	assertEqualInt(1, archive_match_excluded(m, ae));
259 	archive_entry_copy_gname_w(ae, L"foo");
260 	failure("Group 'foo' should not be excluded");
261 	assertEqualInt(0, archive_match_owner_excluded(m, ae));
262 	assertEqualInt(0, archive_match_excluded(m, ae));
263 	archive_entry_copy_gname_w(ae, L"foo1");
264 	failure("Group 'foo1' should be excluded");
265 	assertEqualInt(1, archive_match_owner_excluded(m, ae));
266 	assertEqualInt(1, archive_match_excluded(m, ae));
267 	archive_entry_copy_gname_w(ae, L"bar");
268 	failure("Group 'bar' should not be excluded");
269 	assertEqualInt(0, archive_match_owner_excluded(m, ae));
270 	assertEqualInt(0, archive_match_excluded(m, ae));
271 	archive_entry_copy_gname_w(ae, L"bar1");
272 	failure("Group 'bar1' should be excluded");
273 	assertEqualInt(1, archive_match_owner_excluded(m, ae));
274 	assertEqualInt(1, archive_match_excluded(m, ae));
275 
276 	/* Clean up. */
277 	archive_entry_free(ae);
278 	archive_match_free(m);
279 }
280 
281 DEFINE_TEST(test_archive_match_owner)
282 {
283 	test_uid();
284 	test_gid();
285 	test_uname_mbs();
286 	test_uname_wcs();
287 	test_gname_mbs();
288 	test_gname_wcs();
289 }
290