xref: /netbsd-src/tests/usr.bin/id/t_id.sh (revision d8f5b04c091e033c7c2978066c7b27dbd1aa9d53)
1*d8f5b04cSrillig# $NetBSD: t_id.sh,v 1.2 2024/04/28 07:27:42 rillig Exp $
228604916Sjruoho#
328604916Sjruoho# Copyright (c) 2007, 2008 The NetBSD Foundation, Inc.
428604916Sjruoho# All rights reserved.
528604916Sjruoho#
628604916Sjruoho# Redistribution and use in source and binary forms, with or without
728604916Sjruoho# modification, are permitted provided that the following conditions
828604916Sjruoho# are met:
928604916Sjruoho# 1. Redistributions of source code must retain the above copyright
1028604916Sjruoho#    notice, this list of conditions and the following disclaimer.
1128604916Sjruoho# 2. Redistributions in binary form must reproduce the above copyright
1228604916Sjruoho#    notice, this list of conditions and the following disclaimer in the
1328604916Sjruoho#    documentation and/or other materials provided with the distribution.
1428604916Sjruoho#
1528604916Sjruoho# THIS SOFTWARE IS PROVIDED BY THE NETBSD FOUNDATION, INC. AND CONTRIBUTORS
1628604916Sjruoho# ``AS IS'' AND ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED
1728604916Sjruoho# TO, THE IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR
1828604916Sjruoho# PURPOSE ARE DISCLAIMED.  IN NO EVENT SHALL THE FOUNDATION OR CONTRIBUTORS
1928604916Sjruoho# BE LIABLE FOR ANY DIRECT, INDIRECT, INCIDENTAL, SPECIAL, EXEMPLARY, OR
2028604916Sjruoho# CONSEQUENTIAL DAMAGES (INCLUDING, BUT NOT LIMITED TO, PROCUREMENT OF
2128604916Sjruoho# SUBSTITUTE GOODS OR SERVICES; LOSS OF USE, DATA, OR PROFITS; OR BUSINESS
2228604916Sjruoho# INTERRUPTION) HOWEVER CAUSED AND ON ANY THEORY OF LIABILITY, WHETHER IN
2328604916Sjruoho# CONTRACT, STRICT LIABILITY, OR TORT (INCLUDING NEGLIGENCE OR OTHERWISE)
2428604916Sjruoho# ARISING IN ANY WAY OUT OF THE USE OF THIS SOFTWARE, EVEN IF ADVISED OF THE
2528604916Sjruoho# POSSIBILITY OF SUCH DAMAGE.
2628604916Sjruoho#
2728604916Sjruoho
2828604916Sjruohocreate_run_id() {
2928604916Sjruoho	cat >run_id.sh <<EOF
3028604916Sjruoho#! /bin/sh
3128604916Sjruoho[ -f ./id ] || ln -s $(atf_get_srcdir)/h_id ./id
3228604916Sjruoho./id "\${@}"
3328604916SjruohoEOF
3428604916Sjruoho	chmod +x run_id.sh
3528604916Sjruoho}
3628604916Sjruoho
3728604916Sjruohoatf_test_case default
3828604916Sjruohodefault_head() {
3928604916Sjruoho	atf_set "descr" "Checks that the output without options is correct"
4028604916Sjruoho}
4128604916Sjruohodefault_body() {
4228604916Sjruoho	create_run_id
4328604916Sjruoho
4428604916Sjruoho	echo "uid=100(test) gid=100(users) groups=100(users),0(wheel)" >expout
45*d8f5b04cSrillig	atf_check -s exit:0 -o file:expout -e empty ./run_id.sh
46*d8f5b04cSrillig	atf_check -s exit:0 -o file:expout -e empty ./run_id.sh 100
47*d8f5b04cSrillig	atf_check -s exit:0 -o file:expout -e empty ./run_id.sh test
4828604916Sjruoho
4928604916Sjruoho	echo "uid=0(root) gid=0(wheel) groups=0(wheel)" >expout
50*d8f5b04cSrillig	atf_check -s exit:0 -o file:expout -e empty ./run_id.sh 0
51*d8f5b04cSrillig	atf_check -s exit:0 -o file:expout -e empty ./run_id.sh root
5228604916Sjruoho
5328604916Sjruoho	export LIBFAKE_EGID_ROOT=1 LIBFAKE_EUID_ROOT=1
5428604916Sjruoho	echo "uid=100(test) gid=100(users) euid=0(root) egid=0(wheel) groups=100(users),0(wheel)" >expout
55*d8f5b04cSrillig	atf_check -s exit:0 -o file:expout -e empty ./run_id.sh
5628604916Sjruoho	unset LIBFAKE_EGID_ROOT LIBFAKE_EUID_ROOT
5728604916Sjruoho
5828604916Sjruoho	echo 'id: nonexistent: No such user' >experr
59*d8f5b04cSrillig	atf_check -s exit:1 -o empty -e file:experr ./run_id.sh nonexistent
6028604916Sjruoho
61*d8f5b04cSrillig	atf_check -s exit:1 -o empty -e save:stderr ./run_id.sh root nonexistent
62*d8f5b04cSrillig	atf_check -s exit:0 -o ignore -e empty grep ^usage: stderr
6328604916Sjruoho}
6428604916Sjruoho
6528604916Sjruohoatf_test_case primaries
6628604916Sjruohoprimaries_head() {
6728604916Sjruoho	atf_set "descr" "Checks that giving multiple primaries" \
6828604916Sjruoho	                "simultaneously provides an error"
6928604916Sjruoho}
7028604916Sjruohoprimaries_body() {
7128604916Sjruoho	create_run_id
7228604916Sjruoho
7328604916Sjruoho	for p1 in -G -g -p -u; do
7428604916Sjruoho		for p2 in -G -g -p -u; do
7528604916Sjruoho			if [ ${p1} != ${p2} ]; then
76*d8f5b04cSrillig				atf_check -s exit:1 -o empty -e save:stderr \
7728604916Sjruoho				    ./run_id.sh ${p1} ${p2}
78*d8f5b04cSrillig				atf_check -s exit:0 -o ignore -e empty \
7928604916Sjruoho				    grep ^usage: stderr
8028604916Sjruoho			fi
8128604916Sjruoho		done
8228604916Sjruoho	done
8328604916Sjruoho}
8428604916Sjruoho
8528604916Sjruohoatf_test_case Gflag
8628604916SjruohoGflag_head() {
8728604916Sjruoho	atf_set "descr" "Checks that the -G primary flag works"
8828604916Sjruoho}
8928604916SjruohoGflag_body() {
9028604916Sjruoho	create_run_id
9128604916Sjruoho
9228604916Sjruoho	echo "100 0" >expout
93*d8f5b04cSrillig	atf_check -s exit:0 -o file:expout -e empty ./run_id.sh -G
94*d8f5b04cSrillig	atf_check -s exit:0 -o file:expout -e empty ./run_id.sh -G 100
95*d8f5b04cSrillig	atf_check -s exit:0 -o file:expout -e empty ./run_id.sh -G test
9628604916Sjruoho
9728604916Sjruoho	echo "users wheel" >expout
98*d8f5b04cSrillig	atf_check -s exit:0 -o file:expout -e empty ./run_id.sh -G -n
99*d8f5b04cSrillig	atf_check -s exit:0 -o file:expout -e empty ./run_id.sh -G -n 100
100*d8f5b04cSrillig	atf_check -s exit:0 -o file:expout -e empty ./run_id.sh -G -n test
10128604916Sjruoho
10228604916Sjruoho	echo "0" >expout
103*d8f5b04cSrillig	atf_check -s exit:0 -o file:expout -e empty ./run_id.sh -G 0
104*d8f5b04cSrillig	atf_check -s exit:0 -o file:expout -e empty ./run_id.sh -G root
10528604916Sjruoho
10628604916Sjruoho	echo "wheel" >expout
107*d8f5b04cSrillig	atf_check -s exit:0 -o file:expout -e empty ./run_id.sh -G -n 0
108*d8f5b04cSrillig	atf_check -s exit:0 -o file:expout -e empty ./run_id.sh -G -n root
10928604916Sjruoho
11028604916Sjruoho	echo 'id: nonexistent: No such user' >experr
111*d8f5b04cSrillig	atf_check -s exit:1 -o empty -e file:experr ./run_id.sh -G nonexistent
11228604916Sjruoho
113*d8f5b04cSrillig	atf_check -s exit:1 -o empty -e save:stderr ./run_id.sh -G root nonexistent
114*d8f5b04cSrillig	atf_check -s exit:0 -o ignore -e empty grep ^usage: stderr
11528604916Sjruoho}
11628604916Sjruoho
11728604916Sjruohoatf_test_case gflag
11828604916Sjruohogflag_head() {
11928604916Sjruoho	atf_set "descr" "Checks that the -g primary flag works"
12028604916Sjruoho}
12128604916Sjruohogflag_body() {
12228604916Sjruoho	create_run_id
12328604916Sjruoho
12428604916Sjruoho	echo "100" >expout
125*d8f5b04cSrillig	atf_check -s exit:0 -o file:expout -e empty ./run_id.sh -g
126*d8f5b04cSrillig	atf_check -s exit:0 -o file:expout -e empty ./run_id.sh -g 100
127*d8f5b04cSrillig	atf_check -s exit:0 -o file:expout -e empty ./run_id.sh -g test
12828604916Sjruoho
12928604916Sjruoho	echo "users" >expout
130*d8f5b04cSrillig	atf_check -s exit:0 -o file:expout -e empty ./run_id.sh -g -n
131*d8f5b04cSrillig	atf_check -s exit:0 -o file:expout -e empty ./run_id.sh -g -n 100
132*d8f5b04cSrillig	atf_check -s exit:0 -o file:expout -e empty ./run_id.sh -g -n test
13328604916Sjruoho
13428604916Sjruoho	echo "0" >expout
135*d8f5b04cSrillig	atf_check -s exit:0 -o file:expout -e empty ./run_id.sh -g 0
136*d8f5b04cSrillig	atf_check -s exit:0 -o file:expout -e empty ./run_id.sh -g root
13728604916Sjruoho
13828604916Sjruoho	echo "wheel" >expout
139*d8f5b04cSrillig	atf_check -s exit:0 -o file:expout -e empty ./run_id.sh -g -n 0
140*d8f5b04cSrillig	atf_check -s exit:0 -o file:expout -e empty ./run_id.sh -g -n root
14128604916Sjruoho
14228604916Sjruoho	echo "100" >expout
143*d8f5b04cSrillig	atf_check -s exit:0 -o file:expout -e empty ./run_id.sh -g -r
14428604916Sjruoho
14528604916Sjruoho	echo "users" >expout
146*d8f5b04cSrillig	atf_check -s exit:0 -o file:expout -e empty ./run_id.sh -g -r -n
14728604916Sjruoho
14828604916Sjruoho	echo "100" >expout
149*d8f5b04cSrillig	atf_check -s exit:0 -o file:expout -e empty ./run_id.sh -g -r 100
150*d8f5b04cSrillig	atf_check -s exit:0 -o file:expout -e empty ./run_id.sh -g -r test
15128604916Sjruoho
15228604916Sjruoho	echo "users" >expout
153*d8f5b04cSrillig	atf_check -s exit:0 -o file:expout -e empty ./run_id.sh -g -r -n 100
154*d8f5b04cSrillig	atf_check -s exit:0 -o file:expout -e empty ./run_id.sh -g -r -n test
15528604916Sjruoho
15628604916Sjruoho	export LIBFAKE_EGID_ROOT=1 LIBFAKE_EUID_ROOT=1
15728604916Sjruoho
15828604916Sjruoho	echo "0" >expout
159*d8f5b04cSrillig	atf_check -s exit:0 -o file:expout -e empty ./run_id.sh -g
16028604916Sjruoho
16128604916Sjruoho	echo "wheel" >expout
162*d8f5b04cSrillig	atf_check -s exit:0 -o file:expout -e empty ./run_id.sh -g -n
16328604916Sjruoho
16428604916Sjruoho	echo "100" >expout
165*d8f5b04cSrillig	atf_check -s exit:0 -o file:expout -e empty ./run_id.sh -g -r
16628604916Sjruoho
16728604916Sjruoho	echo "users" >expout
168*d8f5b04cSrillig	atf_check -s exit:0 -o file:expout -e empty ./run_id.sh -g -r -n
16928604916Sjruoho
17028604916Sjruoho	echo "100" >expout
171*d8f5b04cSrillig	atf_check -s exit:0 -o file:expout -e empty ./run_id.sh -g -r 100
172*d8f5b04cSrillig	atf_check -s exit:0 -o file:expout -e empty ./run_id.sh -g -r test
17328604916Sjruoho
17428604916Sjruoho	echo "users" >expout
175*d8f5b04cSrillig	atf_check -s exit:0 -o file:expout -e empty ./run_id.sh -g -r -n 100
176*d8f5b04cSrillig	atf_check -s exit:0 -o file:expout -e empty ./run_id.sh -g -r -n test
17728604916Sjruoho
17828604916Sjruoho	unset LIBFAKE_EGID_ROOT LIBFAKE_EUID_ROOT
17928604916Sjruoho
18028604916Sjruoho	echo 'id: nonexistent: No such user' >experr
181*d8f5b04cSrillig	atf_check -s exit:1 -o empty -e file:experr ./run_id.sh -g nonexistent
18228604916Sjruoho
183*d8f5b04cSrillig	atf_check -s exit:1 -o empty -e save:stderr ./run_id.sh -g root nonexistent
184*d8f5b04cSrillig	atf_check -s exit:0 -o ignore -e empty grep ^usage: stderr
18528604916Sjruoho}
18628604916Sjruoho
18728604916Sjruohoatf_test_case pflag
18828604916Sjruohopflag_head() {
18928604916Sjruoho	atf_set "descr" "Checks that the -p primary flag works"
19028604916Sjruoho}
19128604916Sjruohopflag_body() {
19228604916Sjruoho	create_run_id
19328604916Sjruoho
19428604916Sjruoho	cat >expout <<EOF
19528604916Sjruohouid	test
19628604916Sjruohogroups	users wheel
19728604916SjruohoEOF
198*d8f5b04cSrillig	atf_check -s exit:0 -o file:expout -e empty ./run_id.sh -p
199*d8f5b04cSrillig	atf_check -s exit:0 -o file:expout -e empty ./run_id.sh -p 100
200*d8f5b04cSrillig	atf_check -s exit:0 -o file:expout -e empty ./run_id.sh -p test
20128604916Sjruoho
20228604916Sjruoho	cat >expout <<EOF
20328604916Sjruohouid	root
20428604916Sjruohogroups	wheel
20528604916SjruohoEOF
206*d8f5b04cSrillig	atf_check -s exit:0 -o file:expout -e empty ./run_id.sh -p 0
207*d8f5b04cSrillig	atf_check -s exit:0 -o file:expout -e empty ./run_id.sh -p root
20828604916Sjruoho
20928604916Sjruoho	export LIBFAKE_EGID_ROOT=1 LIBFAKE_EUID_ROOT=1
21028604916Sjruoho	cat >expout <<EOF
21128604916Sjruohouid	test
21228604916Sjruohoeuid	root
21328604916Sjruohorgid	users
21428604916Sjruohogroups	users wheel
21528604916SjruohoEOF
216*d8f5b04cSrillig	atf_check -s exit:0 -o file:expout -e empty ./run_id.sh -p
21728604916Sjruoho	unset LIBFAKE_EGID_ROOT LIBFAKE_EUID_ROOT
21828604916Sjruoho
21928604916Sjruoho	echo 'id: nonexistent: No such user' >experr
220*d8f5b04cSrillig	atf_check -s exit:1 -o empty -e file:experr ./run_id.sh -p nonexistent
22128604916Sjruoho
222*d8f5b04cSrillig	atf_check -s exit:1 -o empty -e save:stderr ./run_id.sh -p root nonexistent
223*d8f5b04cSrillig	atf_check -s exit:0 -o ignore -e empty grep ^usage: stderr
22428604916Sjruoho}
22528604916Sjruoho
22628604916Sjruohoatf_test_case uflag
22728604916Sjruohouflag_head() {
22828604916Sjruoho	atf_set "descr" "Checks that the -u primary flag works"
22928604916Sjruoho}
23028604916Sjruohouflag_body() {
23128604916Sjruoho	create_run_id
23228604916Sjruoho
23328604916Sjruoho	echo "100" >expout
234*d8f5b04cSrillig	atf_check -s exit:0 -o file:expout -e empty ./run_id.sh -u
235*d8f5b04cSrillig	atf_check -s exit:0 -o file:expout -e empty ./run_id.sh -u 100
236*d8f5b04cSrillig	atf_check -s exit:0 -o file:expout -e empty ./run_id.sh -u test
23728604916Sjruoho
23828604916Sjruoho	echo "test" >expout
239*d8f5b04cSrillig	atf_check -s exit:0 -o file:expout -e empty ./run_id.sh -u -n
240*d8f5b04cSrillig	atf_check -s exit:0 -o file:expout -e empty ./run_id.sh -u -n 100
241*d8f5b04cSrillig	atf_check -s exit:0 -o file:expout -e empty ./run_id.sh -u -n test
24228604916Sjruoho
24328604916Sjruoho	echo "0" >expout
244*d8f5b04cSrillig	atf_check -s exit:0 -o file:expout -e empty ./run_id.sh -u 0
245*d8f5b04cSrillig	atf_check -s exit:0 -o file:expout -e empty ./run_id.sh -u root
24628604916Sjruoho
24728604916Sjruoho	echo "root" >expout
248*d8f5b04cSrillig	atf_check -s exit:0 -o file:expout -e empty ./run_id.sh -u -n 0
249*d8f5b04cSrillig	atf_check -s exit:0 -o file:expout -e empty ./run_id.sh -u -n root
25028604916Sjruoho
25128604916Sjruoho	echo "100" >expout
252*d8f5b04cSrillig	atf_check -s exit:0 -o file:expout -e empty ./run_id.sh -u -r
25328604916Sjruoho
25428604916Sjruoho	echo "test" >expout
255*d8f5b04cSrillig	atf_check -s exit:0 -o file:expout -e empty ./run_id.sh -u -r -n
25628604916Sjruoho
25728604916Sjruoho	echo "100" >expout
258*d8f5b04cSrillig	atf_check -s exit:0 -o file:expout -e empty ./run_id.sh -u -r 100
259*d8f5b04cSrillig	atf_check -s exit:0 -o file:expout -e empty ./run_id.sh -u -r test
26028604916Sjruoho
26128604916Sjruoho	echo "test" >expout
262*d8f5b04cSrillig	atf_check -s exit:0 -o file:expout -e empty ./run_id.sh -u -r -n 100
263*d8f5b04cSrillig	atf_check -s exit:0 -o file:expout -e empty ./run_id.sh -u -r -n test
26428604916Sjruoho
26528604916Sjruoho	export LIBFAKE_EGID_ROOT=1 LIBFAKE_EUID_ROOT=1
26628604916Sjruoho
26728604916Sjruoho	echo "0" >expout
268*d8f5b04cSrillig	atf_check -s exit:0 -o file:expout -e empty ./run_id.sh -u
26928604916Sjruoho
27028604916Sjruoho	echo "root" >expout
271*d8f5b04cSrillig	atf_check -s exit:0 -o file:expout -e empty ./run_id.sh -u -n
27228604916Sjruoho
27328604916Sjruoho	echo "100" >expout
274*d8f5b04cSrillig	atf_check -s exit:0 -o file:expout -e empty ./run_id.sh -u -r
27528604916Sjruoho
27628604916Sjruoho	echo "test" >expout
277*d8f5b04cSrillig	atf_check -s exit:0 -o file:expout -e empty ./run_id.sh -u -r -n
27828604916Sjruoho
27928604916Sjruoho	echo "100" >expout
280*d8f5b04cSrillig	atf_check -s exit:0 -o file:expout -e empty ./run_id.sh -u -r 100
281*d8f5b04cSrillig	atf_check -s exit:0 -o file:expout -e empty ./run_id.sh -u -r test
28228604916Sjruoho
28328604916Sjruoho	echo "test" >expout
284*d8f5b04cSrillig	atf_check -s exit:0 -o file:expout -e empty ./run_id.sh -u -r -n 100
285*d8f5b04cSrillig	atf_check -s exit:0 -o file:expout -e empty ./run_id.sh -u -r -n test
28628604916Sjruoho
28728604916Sjruoho	unset LIBFAKE_EGID_ROOT LIBFAKE_EUID_ROOT
28828604916Sjruoho
28928604916Sjruoho	echo 'id: nonexistent: No such user' >experr
290*d8f5b04cSrillig	atf_check -s exit:1 -o empty -e file:experr ./run_id.sh -u nonexistent
29128604916Sjruoho
292*d8f5b04cSrillig	atf_check -s exit:1 -o empty -e save:stderr \
29328604916Sjruoho	    ./run_id.sh -u root nonexistent
294*d8f5b04cSrillig	atf_check -s exit:0 -o ignore -e empty grep ^usage: stderr
29528604916Sjruoho}
29628604916Sjruoho
29728604916Sjruohoatf_init_test_cases()
29828604916Sjruoho{
29928604916Sjruoho	atf_add_test_case default
30028604916Sjruoho	atf_add_test_case primaries
30128604916Sjruoho	atf_add_test_case Gflag
30228604916Sjruoho	atf_add_test_case gflag
30328604916Sjruoho	atf_add_test_case pflag
30428604916Sjruoho	atf_add_test_case uflag
30528604916Sjruoho}
306