1238825Smm/*-
2238825Smm * Copyright (c) 2012 Michihiro NAKAJIMA
3238825Smm * All rights reserved.
4238825Smm *
5238825Smm * Redistribution and use in source and binary forms, with or without
6238825Smm * modification, are permitted provided that the following conditions
7238825Smm * are met:
8238825Smm * 1. Redistributions of source code must retain the above copyright
9238825Smm *    notice, this list of conditions and the following disclaimer.
10238825Smm * 2. Redistributions in binary form must reproduce the above copyright
11238825Smm *    notice, this list of conditions and the following disclaimer in the
12238825Smm *    documentation and/or other materials provided with the distribution.
13238825Smm *
14238825Smm * THIS SOFTWARE IS PROVIDED BY THE AUTHOR(S) ``AS IS'' AND ANY EXPRESS OR
15238825Smm * IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE IMPLIED WARRANTIES
16238825Smm * OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE ARE DISCLAIMED.
17238825Smm * IN NO EVENT SHALL THE AUTHOR(S) BE LIABLE FOR ANY DIRECT, INDIRECT,
18238825Smm * INCIDENTAL, SPECIAL, EXEMPLARY, OR CONSEQUENTIAL DAMAGES (INCLUDING, BUT
19238825Smm * NOT LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS OR SERVICES; LOSS OF USE,
20238825Smm * DATA, OR PROFITS; OR BUSINESS INTERRUPTION) HOWEVER CAUSED AND ON ANY
21238825Smm * THEORY OF LIABILITY, WHETHER IN CONTRACT, STRICT LIABILITY, OR TORT
22238825Smm * (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY OUT OF THE USE OF
23238825Smm * THIS SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF SUCH DAMAGE.
24238825Smm */
25238825Smm
26238825Smm#include "test.h"
27238825Smm__FBSDID("$FreeBSD$");
28238825Smm
29238825Smmstatic void
30238825Smmtest_uid(void)
31238825Smm{
32238825Smm	struct archive_entry *ae;
33238825Smm	struct archive *m;
34238825Smm
35238825Smm	if (!assert((m = archive_match_new()) != NULL))
36238825Smm		return;
37238825Smm	if (!assert((ae = archive_entry_new()) != NULL)) {
38238825Smm		archive_match_free(m);
39238825Smm		return;
40238825Smm	}
41238825Smm
42238825Smm	assertEqualIntA(m, 0, archive_match_include_uid(m, 1000));
43238825Smm	assertEqualIntA(m, 0, archive_match_include_uid(m, 1002));
44238825Smm
45238825Smm	archive_entry_set_uid(ae, 0);
46238825Smm	failure("uid 0 should be excluded");
47238825Smm	assertEqualInt(1, archive_match_owner_excluded(m, ae));
48238825Smm	assertEqualInt(1, archive_match_excluded(m, ae));
49238825Smm	archive_entry_set_uid(ae, 1000);
50238825Smm	failure("uid 1000 should not be excluded");
51238825Smm	assertEqualInt(0, archive_match_owner_excluded(m, ae));
52238825Smm	assertEqualInt(0, archive_match_excluded(m, ae));
53238825Smm	archive_entry_set_uid(ae, 1001);
54238825Smm	failure("uid 1001 should be excluded");
55238825Smm	assertEqualInt(1, archive_match_owner_excluded(m, ae));
56238825Smm	assertEqualInt(1, archive_match_excluded(m, ae));
57238825Smm	archive_entry_set_uid(ae, 1002);
58238825Smm	failure("uid 1002 should not be excluded");
59238825Smm	assertEqualInt(0, archive_match_owner_excluded(m, ae));
60238825Smm	assertEqualInt(0, archive_match_excluded(m, ae));
61238825Smm	archive_entry_set_uid(ae, 1003);
62238825Smm	failure("uid 1003 should be excluded");
63238825Smm	assertEqualInt(1, archive_match_owner_excluded(m, ae));
64238825Smm	assertEqualInt(1, archive_match_excluded(m, ae));
65238825Smm
66238825Smm	/* Clean up. */
67238825Smm	archive_entry_free(ae);
68238825Smm	archive_match_free(m);
69238825Smm}
70238825Smm
71238825Smmstatic void
72238825Smmtest_gid(void)
73238825Smm{
74238825Smm	struct archive_entry *ae;
75238825Smm	struct archive *m;
76238825Smm
77238825Smm	if (!assert((m = archive_match_new()) != NULL))
78238825Smm		return;
79238825Smm	if (!assert((ae = archive_entry_new()) != NULL)) {
80238825Smm		archive_match_free(m);
81238825Smm		return;
82238825Smm	}
83238825Smm
84238825Smm	assertEqualIntA(m, 0, archive_match_include_gid(m, 1000));
85238825Smm	assertEqualIntA(m, 0, archive_match_include_gid(m, 1002));
86238825Smm
87238825Smm	archive_entry_set_gid(ae, 0);
88238825Smm	failure("uid 0 should be excluded");
89238825Smm	assertEqualInt(1, archive_match_owner_excluded(m, ae));
90238825Smm	assertEqualInt(1, archive_match_excluded(m, ae));
91238825Smm	archive_entry_set_gid(ae, 1000);
92238825Smm	failure("uid 1000 should not be excluded");
93238825Smm	assertEqualInt(0, archive_match_owner_excluded(m, ae));
94238825Smm	assertEqualInt(0, archive_match_excluded(m, ae));
95238825Smm	archive_entry_set_gid(ae, 1001);
96238825Smm	failure("uid 1001 should be excluded");
97238825Smm	assertEqualInt(1, archive_match_owner_excluded(m, ae));
98238825Smm	assertEqualInt(1, archive_match_excluded(m, ae));
99238825Smm	archive_entry_set_gid(ae, 1002);
100238825Smm	failure("uid 1002 should not be excluded");
101238825Smm	assertEqualInt(0, archive_match_owner_excluded(m, ae));
102238825Smm	assertEqualInt(0, archive_match_excluded(m, ae));
103238825Smm	archive_entry_set_gid(ae, 1003);
104238825Smm	failure("uid 1003 should be excluded");
105238825Smm	assertEqualInt(1, archive_match_owner_excluded(m, ae));
106238825Smm	assertEqualInt(1, archive_match_excluded(m, ae));
107238825Smm
108238825Smm	/* Clean up. */
109238825Smm	archive_entry_free(ae);
110238825Smm	archive_match_free(m);
111238825Smm}
112238825Smm
113238825Smmstatic void
114238825Smmtest_uname_mbs(void)
115238825Smm{
116238825Smm	struct archive_entry *ae;
117238825Smm	struct archive *m;
118238825Smm
119238825Smm	if (!assert((m = archive_match_new()) != NULL))
120238825Smm		return;
121238825Smm	if (!assert((ae = archive_entry_new()) != NULL)) {
122238825Smm		archive_match_free(m);
123238825Smm		return;
124238825Smm	}
125238825Smm
126238825Smm	assertEqualIntA(m, 0, archive_match_include_uname(m, "foo"));
127238825Smm	assertEqualIntA(m, 0, archive_match_include_uname(m, "bar"));
128238825Smm
129238825Smm	archive_entry_copy_uname(ae, "unknown");
130238825Smm	failure("User 'unknown' should be excluded");
131238825Smm	assertEqualInt(1, archive_match_owner_excluded(m, ae));
132238825Smm	assertEqualInt(1, archive_match_excluded(m, ae));
133238825Smm	archive_entry_copy_uname(ae, "foo");
134238825Smm	failure("User 'foo' should not be excluded");
135238825Smm	assertEqualInt(0, archive_match_owner_excluded(m, ae));
136238825Smm	assertEqualInt(0, archive_match_excluded(m, ae));
137238825Smm	archive_entry_copy_uname(ae, "foo1");
138238825Smm	failure("User 'foo1' should be excluded");
139238825Smm	assertEqualInt(1, archive_match_owner_excluded(m, ae));
140238825Smm	assertEqualInt(1, archive_match_excluded(m, ae));
141238825Smm	archive_entry_copy_uname(ae, "bar");
142238825Smm	failure("User 'bar' should not be excluded");
143238825Smm	assertEqualInt(0, archive_match_owner_excluded(m, ae));
144238825Smm	assertEqualInt(0, archive_match_excluded(m, ae));
145238825Smm	archive_entry_copy_uname(ae, "bar1");
146238825Smm	failure("User 'bar1' should be excluded");
147238825Smm	assertEqualInt(1, archive_match_owner_excluded(m, ae));
148238825Smm	assertEqualInt(1, archive_match_excluded(m, ae));
149238825Smm
150238825Smm	/* Clean up. */
151238825Smm	archive_entry_free(ae);
152238825Smm	archive_match_free(m);
153238825Smm}
154238825Smm
155238825Smmstatic void
156238825Smmtest_uname_wcs(void)
157238825Smm{
158238825Smm	struct archive_entry *ae;
159238825Smm	struct archive *m;
160238825Smm
161238825Smm	if (!assert((m = archive_match_new()) != NULL))
162238825Smm		return;
163238825Smm	if (!assert((ae = archive_entry_new()) != NULL)) {
164238825Smm		archive_match_free(m);
165238825Smm		return;
166238825Smm	}
167238825Smm
168238825Smm	assertEqualIntA(m, 0, archive_match_include_uname_w(m, L"foo"));
169238825Smm	assertEqualIntA(m, 0, archive_match_include_uname_w(m, L"bar"));
170238825Smm
171238825Smm	archive_entry_copy_uname_w(ae, L"unknown");
172238825Smm	failure("User 'unknown' should be excluded");
173238825Smm	assertEqualInt(1, archive_match_owner_excluded(m, ae));
174238825Smm	assertEqualInt(1, archive_match_excluded(m, ae));
175238825Smm	archive_entry_copy_uname_w(ae, L"foo");
176238825Smm	failure("User 'foo' should not be excluded");
177238825Smm	assertEqualInt(0, archive_match_owner_excluded(m, ae));
178238825Smm	assertEqualInt(0, archive_match_excluded(m, ae));
179238825Smm	archive_entry_copy_uname_w(ae, L"foo1");
180238825Smm	failure("User 'foo1' should be excluded");
181238825Smm	assertEqualInt(1, archive_match_owner_excluded(m, ae));
182238825Smm	assertEqualInt(1, archive_match_excluded(m, ae));
183238825Smm	archive_entry_copy_uname_w(ae, L"bar");
184238825Smm	failure("User 'bar' should not be excluded");
185238825Smm	assertEqualInt(0, archive_match_owner_excluded(m, ae));
186238825Smm	assertEqualInt(0, archive_match_excluded(m, ae));
187238825Smm	archive_entry_copy_uname_w(ae, L"bar1");
188238825Smm	failure("User 'bar1' should be excluded");
189238825Smm	assertEqualInt(1, archive_match_owner_excluded(m, ae));
190238825Smm	assertEqualInt(1, archive_match_excluded(m, ae));
191238825Smm
192238825Smm	/* Clean up. */
193238825Smm	archive_entry_free(ae);
194238825Smm	archive_match_free(m);
195238825Smm}
196238825Smm
197238825Smmstatic void
198238825Smmtest_gname_mbs(void)
199238825Smm{
200238825Smm	struct archive_entry *ae;
201238825Smm	struct archive *m;
202238825Smm
203238825Smm	if (!assert((m = archive_match_new()) != NULL))
204238825Smm		return;
205238825Smm	if (!assert((ae = archive_entry_new()) != NULL)) {
206238825Smm		archive_match_free(m);
207238825Smm		return;
208238825Smm	}
209238825Smm
210238825Smm	assertEqualIntA(m, 0, archive_match_include_gname(m, "foo"));
211238825Smm	assertEqualIntA(m, 0, archive_match_include_gname(m, "bar"));
212238825Smm
213238825Smm	archive_entry_copy_gname(ae, "unknown");
214238825Smm	failure("Group 'unknown' should be excluded");
215238825Smm	assertEqualInt(1, archive_match_owner_excluded(m, ae));
216238825Smm	assertEqualInt(1, archive_match_excluded(m, ae));
217238825Smm	archive_entry_copy_gname(ae, "foo");
218238825Smm	failure("Group 'foo' should not be excluded");
219238825Smm	assertEqualInt(0, archive_match_owner_excluded(m, ae));
220238825Smm	assertEqualInt(0, archive_match_excluded(m, ae));
221238825Smm	archive_entry_copy_gname(ae, "foo1");
222238825Smm	failure("Group 'foo1' should be excluded");
223238825Smm	assertEqualInt(1, archive_match_owner_excluded(m, ae));
224238825Smm	assertEqualInt(1, archive_match_excluded(m, ae));
225238825Smm	archive_entry_copy_gname(ae, "bar");
226238825Smm	failure("Group 'bar' should not be excluded");
227238825Smm	assertEqualInt(0, archive_match_owner_excluded(m, ae));
228238825Smm	assertEqualInt(0, archive_match_excluded(m, ae));
229238825Smm	archive_entry_copy_gname(ae, "bar1");
230238825Smm	failure("Group 'bar1' should be excluded");
231238825Smm	assertEqualInt(1, archive_match_owner_excluded(m, ae));
232238825Smm	assertEqualInt(1, archive_match_excluded(m, ae));
233238825Smm
234238825Smm	/* Clean up. */
235238825Smm	archive_entry_free(ae);
236238825Smm	archive_match_free(m);
237238825Smm}
238238825Smm
239238825Smmstatic void
240238825Smmtest_gname_wcs(void)
241238825Smm{
242238825Smm	struct archive_entry *ae;
243238825Smm	struct archive *m;
244238825Smm
245238825Smm	if (!assert((m = archive_match_new()) != NULL))
246238825Smm		return;
247238825Smm	if (!assert((ae = archive_entry_new()) != NULL)) {
248238825Smm		archive_match_free(m);
249238825Smm		return;
250238825Smm	}
251238825Smm
252238825Smm	assertEqualIntA(m, 0, archive_match_include_gname_w(m, L"foo"));
253238825Smm	assertEqualIntA(m, 0, archive_match_include_gname_w(m, L"bar"));
254238825Smm
255238825Smm	archive_entry_copy_gname_w(ae, L"unknown");
256238825Smm	failure("Group 'unknown' should be excluded");
257238825Smm	assertEqualInt(1, archive_match_owner_excluded(m, ae));
258238825Smm	assertEqualInt(1, archive_match_excluded(m, ae));
259238825Smm	archive_entry_copy_gname_w(ae, L"foo");
260238825Smm	failure("Group 'foo' should not be excluded");
261238825Smm	assertEqualInt(0, archive_match_owner_excluded(m, ae));
262238825Smm	assertEqualInt(0, archive_match_excluded(m, ae));
263238825Smm	archive_entry_copy_gname_w(ae, L"foo1");
264238825Smm	failure("Group 'foo1' should be excluded");
265238825Smm	assertEqualInt(1, archive_match_owner_excluded(m, ae));
266238825Smm	assertEqualInt(1, archive_match_excluded(m, ae));
267238825Smm	archive_entry_copy_gname_w(ae, L"bar");
268238825Smm	failure("Group 'bar' should not be excluded");
269238825Smm	assertEqualInt(0, archive_match_owner_excluded(m, ae));
270238825Smm	assertEqualInt(0, archive_match_excluded(m, ae));
271238825Smm	archive_entry_copy_gname_w(ae, L"bar1");
272238825Smm	failure("Group 'bar1' should be excluded");
273238825Smm	assertEqualInt(1, archive_match_owner_excluded(m, ae));
274238825Smm	assertEqualInt(1, archive_match_excluded(m, ae));
275238825Smm
276238825Smm	/* Clean up. */
277238825Smm	archive_entry_free(ae);
278238825Smm	archive_match_free(m);
279238825Smm}
280238825Smm
281238825SmmDEFINE_TEST(test_archive_match_owner)
282238825Smm{
283238825Smm	test_uid();
284238825Smm	test_gid();
285238825Smm	test_uname_mbs();
286238825Smm	test_uname_wcs();
287238825Smm	test_gname_mbs();
288238825Smm	test_gname_wcs();
289238825Smm}
290