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