xref: /freebsd/contrib/netbsd-tests/usr.bin/id/t_id.sh (revision 2e3507c25e42292b45a5482e116d278f5515d04d)
1# $NetBSD: t_id.sh,v 1.1 2012/03/17 16:33:14 jruoho Exp $
2#
3# Copyright (c) 2007, 2008 The NetBSD Foundation, Inc.
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 NETBSD FOUNDATION, INC. AND CONTRIBUTORS
16# ``AS IS'' AND ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED
17# TO, THE IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR
18# PURPOSE ARE DISCLAIMED.  IN NO EVENT SHALL THE FOUNDATION OR CONTRIBUTORS
19# BE LIABLE FOR ANY DIRECT, INDIRECT, INCIDENTAL, SPECIAL, EXEMPLARY, OR
20# CONSEQUENTIAL DAMAGES (INCLUDING, BUT NOT LIMITED TO, PROCUREMENT OF
21# SUBSTITUTE GOODS OR SERVICES; LOSS OF USE, DATA, OR PROFITS; OR BUSINESS
22# INTERRUPTION) HOWEVER CAUSED AND ON ANY THEORY OF LIABILITY, WHETHER IN
23# CONTRACT, STRICT LIABILITY, OR TORT (INCLUDING NEGLIGENCE OR OTHERWISE)
24# ARISING IN ANY WAY OUT OF THE USE OF THIS SOFTWARE, EVEN IF ADVISED OF THE
25# POSSIBILITY OF SUCH DAMAGE.
26#
27
28create_run_id() {
29	cat >run_id.sh <<EOF
30#! /bin/sh
31[ -f ./id ] || ln -s $(atf_get_srcdir)/h_id ./id
32./id "\${@}"
33EOF
34	chmod +x run_id.sh
35}
36
37atf_test_case default
38default_head() {
39	atf_set "descr" "Checks that the output without options is correct"
40}
41default_body() {
42	create_run_id
43
44	echo "uid=100(test) gid=100(users) groups=100(users),0(wheel)" >expout
45	atf_check -s eq:0 -o file:expout -e empty ./run_id.sh
46	atf_check -s eq:0 -o file:expout -e empty ./run_id.sh 100
47	atf_check -s eq:0 -o file:expout -e empty ./run_id.sh test
48
49	echo "uid=0(root) gid=0(wheel) groups=0(wheel)" >expout
50	atf_check -s eq:0 -o file:expout -e empty ./run_id.sh 0
51	atf_check -s eq:0 -o file:expout -e empty ./run_id.sh root
52
53	export LIBFAKE_EGID_ROOT=1 LIBFAKE_EUID_ROOT=1
54	echo "uid=100(test) gid=100(users) euid=0(root) egid=0(wheel) groups=100(users),0(wheel)" >expout
55	atf_check -s eq:0 -o file:expout -e empty ./run_id.sh
56	unset LIBFAKE_EGID_ROOT LIBFAKE_EUID_ROOT
57
58	echo 'id: nonexistent: No such user' >experr
59	atf_check -s eq:1 -o empty -e file:experr ./run_id.sh nonexistent
60
61	atf_check -s eq:1 -o empty -e save:stderr ./run_id.sh root nonexistent
62	atf_check -s eq:0 -o ignore -e empty grep ^usage: stderr
63}
64
65atf_test_case primaries
66primaries_head() {
67	atf_set "descr" "Checks that giving multiple primaries" \
68	                "simultaneously provides an error"
69}
70primaries_body() {
71	create_run_id
72
73	for p1 in -G -g -p -u; do
74		for p2 in -G -g -p -u; do
75			if [ ${p1} != ${p2} ]; then
76				atf_check -s eq:1 -o empty -e save:stderr \
77				    ./run_id.sh ${p1} ${p2}
78				atf_check -s eq:0 -o ignore -e empty \
79				    grep ^usage: stderr
80			fi
81		done
82	done
83}
84
85atf_test_case Gflag
86Gflag_head() {
87	atf_set "descr" "Checks that the -G primary flag works"
88}
89Gflag_body() {
90	create_run_id
91
92	echo "100 0" >expout
93	atf_check -s eq:0 -o file:expout -e empty ./run_id.sh -G
94	atf_check -s eq:0 -o file:expout -e empty ./run_id.sh -G 100
95	atf_check -s eq:0 -o file:expout -e empty ./run_id.sh -G test
96
97	echo "users wheel" >expout
98	atf_check -s eq:0 -o file:expout -e empty ./run_id.sh -G -n
99	atf_check -s eq:0 -o file:expout -e empty ./run_id.sh -G -n 100
100	atf_check -s eq:0 -o file:expout -e empty ./run_id.sh -G -n test
101
102	echo "0" >expout
103	atf_check -s eq:0 -o file:expout -e empty ./run_id.sh -G 0
104	atf_check -s eq:0 -o file:expout -e empty ./run_id.sh -G root
105
106	echo "wheel" >expout
107	atf_check -s eq:0 -o file:expout -e empty ./run_id.sh -G -n 0
108	atf_check -s eq:0 -o file:expout -e empty ./run_id.sh -G -n root
109
110	echo 'id: nonexistent: No such user' >experr
111	atf_check -s eq:1 -o empty -e file:experr ./run_id.sh -G nonexistent
112
113	atf_check -s eq:1 -o empty -e save:stderr ./run_id.sh -G root nonexistent
114	atf_check -s eq:0 -o ignore -e empty grep ^usage: stderr
115}
116
117atf_test_case gflag
118gflag_head() {
119	atf_set "descr" "Checks that the -g primary flag works"
120}
121gflag_body() {
122	create_run_id
123
124	echo "100" >expout
125	atf_check -s eq:0 -o file:expout -e empty ./run_id.sh -g
126	atf_check -s eq:0 -o file:expout -e empty ./run_id.sh -g 100
127	atf_check -s eq:0 -o file:expout -e empty ./run_id.sh -g test
128
129	echo "users" >expout
130	atf_check -s eq:0 -o file:expout -e empty ./run_id.sh -g -n
131	atf_check -s eq:0 -o file:expout -e empty ./run_id.sh -g -n 100
132	atf_check -s eq:0 -o file:expout -e empty ./run_id.sh -g -n test
133
134	echo "0" >expout
135	atf_check -s eq:0 -o file:expout -e empty ./run_id.sh -g 0
136	atf_check -s eq:0 -o file:expout -e empty ./run_id.sh -g root
137
138	echo "wheel" >expout
139	atf_check -s eq:0 -o file:expout -e empty ./run_id.sh -g -n 0
140	atf_check -s eq:0 -o file:expout -e empty ./run_id.sh -g -n root
141
142	echo "100" >expout
143	atf_check -s eq:0 -o file:expout -e empty ./run_id.sh -g -r
144
145	echo "users" >expout
146	atf_check -s eq:0 -o file:expout -e empty ./run_id.sh -g -r -n
147
148	echo "100" >expout
149	atf_check -s eq:0 -o file:expout -e empty ./run_id.sh -g -r 100
150	atf_check -s eq:0 -o file:expout -e empty ./run_id.sh -g -r test
151
152	echo "users" >expout
153	atf_check -s eq:0 -o file:expout -e empty ./run_id.sh -g -r -n 100
154	atf_check -s eq:0 -o file:expout -e empty ./run_id.sh -g -r -n test
155
156	export LIBFAKE_EGID_ROOT=1 LIBFAKE_EUID_ROOT=1
157
158	echo "0" >expout
159	atf_check -s eq:0 -o file:expout -e empty ./run_id.sh -g
160
161	echo "wheel" >expout
162	atf_check -s eq:0 -o file:expout -e empty ./run_id.sh -g -n
163
164	echo "100" >expout
165	atf_check -s eq:0 -o file:expout -e empty ./run_id.sh -g -r
166
167	echo "users" >expout
168	atf_check -s eq:0 -o file:expout -e empty ./run_id.sh -g -r -n
169
170	echo "100" >expout
171	atf_check -s eq:0 -o file:expout -e empty ./run_id.sh -g -r 100
172	atf_check -s eq:0 -o file:expout -e empty ./run_id.sh -g -r test
173
174	echo "users" >expout
175	atf_check -s eq:0 -o file:expout -e empty ./run_id.sh -g -r -n 100
176	atf_check -s eq:0 -o file:expout -e empty ./run_id.sh -g -r -n test
177
178	unset LIBFAKE_EGID_ROOT LIBFAKE_EUID_ROOT
179
180	echo 'id: nonexistent: No such user' >experr
181	atf_check -s eq:1 -o empty -e file:experr ./run_id.sh -g nonexistent
182
183	atf_check -s eq:1 -o empty -e save:stderr ./run_id.sh -g root nonexistent
184	atf_check -s eq:0 -o ignore -e empty grep ^usage: stderr
185}
186
187atf_test_case pflag
188pflag_head() {
189	atf_set "descr" "Checks that the -p primary flag works"
190}
191pflag_body() {
192	create_run_id
193
194	cat >expout <<EOF
195uid	test
196groups	users wheel
197EOF
198	atf_check -s eq:0 -o file:expout -e empty ./run_id.sh -p
199	atf_check -s eq:0 -o file:expout -e empty ./run_id.sh -p 100
200	atf_check -s eq:0 -o file:expout -e empty ./run_id.sh -p test
201
202	cat >expout <<EOF
203uid	root
204groups	wheel
205EOF
206	atf_check -s eq:0 -o file:expout -e empty ./run_id.sh -p 0
207	atf_check -s eq:0 -o file:expout -e empty ./run_id.sh -p root
208
209	export LIBFAKE_EGID_ROOT=1 LIBFAKE_EUID_ROOT=1
210	cat >expout <<EOF
211uid	test
212euid	root
213rgid	users
214groups	users wheel
215EOF
216	atf_check -s eq:0 -o file:expout -e empty ./run_id.sh -p
217	unset LIBFAKE_EGID_ROOT LIBFAKE_EUID_ROOT
218
219	echo 'id: nonexistent: No such user' >experr
220	atf_check -s eq:1 -o empty -e file:experr ./run_id.sh -p nonexistent
221
222	atf_check -s eq:1 -o empty -e save:stderr ./run_id.sh -p root nonexistent
223	atf_check -s eq:0 -o ignore -e empty grep ^usage: stderr
224}
225
226atf_test_case uflag
227uflag_head() {
228	atf_set "descr" "Checks that the -u primary flag works"
229}
230uflag_body() {
231	create_run_id
232
233	echo "100" >expout
234	atf_check -s eq:0 -o file:expout -e empty ./run_id.sh -u
235	atf_check -s eq:0 -o file:expout -e empty ./run_id.sh -u 100
236	atf_check -s eq:0 -o file:expout -e empty ./run_id.sh -u test
237
238	echo "test" >expout
239	atf_check -s eq:0 -o file:expout -e empty ./run_id.sh -u -n
240	atf_check -s eq:0 -o file:expout -e empty ./run_id.sh -u -n 100
241	atf_check -s eq:0 -o file:expout -e empty ./run_id.sh -u -n test
242
243	echo "0" >expout
244	atf_check -s eq:0 -o file:expout -e empty ./run_id.sh -u 0
245	atf_check -s eq:0 -o file:expout -e empty ./run_id.sh -u root
246
247	echo "root" >expout
248	atf_check -s eq:0 -o file:expout -e empty ./run_id.sh -u -n 0
249	atf_check -s eq:0 -o file:expout -e empty ./run_id.sh -u -n root
250
251	echo "100" >expout
252	atf_check -s eq:0 -o file:expout -e empty ./run_id.sh -u -r
253
254	echo "test" >expout
255	atf_check -s eq:0 -o file:expout -e empty ./run_id.sh -u -r -n
256
257	echo "100" >expout
258	atf_check -s eq:0 -o file:expout -e empty ./run_id.sh -u -r 100
259	atf_check -s eq:0 -o file:expout -e empty ./run_id.sh -u -r test
260
261	echo "test" >expout
262	atf_check -s eq:0 -o file:expout -e empty ./run_id.sh -u -r -n 100
263	atf_check -s eq:0 -o file:expout -e empty ./run_id.sh -u -r -n test
264
265	export LIBFAKE_EGID_ROOT=1 LIBFAKE_EUID_ROOT=1
266
267	echo "0" >expout
268	atf_check -s eq:0 -o file:expout -e empty ./run_id.sh -u
269
270	echo "root" >expout
271	atf_check -s eq:0 -o file:expout -e empty ./run_id.sh -u -n
272
273	echo "100" >expout
274	atf_check -s eq:0 -o file:expout -e empty ./run_id.sh -u -r
275
276	echo "test" >expout
277	atf_check -s eq:0 -o file:expout -e empty ./run_id.sh -u -r -n
278
279	echo "100" >expout
280	atf_check -s eq:0 -o file:expout -e empty ./run_id.sh -u -r 100
281	atf_check -s eq:0 -o file:expout -e empty ./run_id.sh -u -r test
282
283	echo "test" >expout
284	atf_check -s eq:0 -o file:expout -e empty ./run_id.sh -u -r -n 100
285	atf_check -s eq:0 -o file:expout -e empty ./run_id.sh -u -r -n test
286
287	unset LIBFAKE_EGID_ROOT LIBFAKE_EUID_ROOT
288
289	echo 'id: nonexistent: No such user' >experr
290	atf_check -s eq:1 -o empty -e file:experr ./run_id.sh -u nonexistent
291
292	atf_check -s eq:1 -o empty -e save:stderr \
293	    ./run_id.sh -u root nonexistent
294	atf_check -s eq:0 -o ignore -e empty grep ^usage: stderr
295}
296
297atf_init_test_cases()
298{
299	atf_add_test_case default
300	atf_add_test_case primaries
301	atf_add_test_case Gflag
302	atf_add_test_case gflag
303	atf_add_test_case pflag
304	atf_add_test_case uflag
305}
306