maint: sync help2man to latest version
[coreutils.git] / tests / id / zero.sh
blob29d922de37b020865f064af95da95f50a74503cd
1 #!/bin/sh
2 # Exercise "id --zero".
4 # Copyright (C) 2013-2024 Free Software Foundation, Inc.
6 # This program is free software: you can redistribute it and/or modify
7 # it under the terms of the GNU General Public License as published by
8 # the Free Software Foundation, either version 3 of the License, or
9 # (at your option) any later version.
11 # This program is distributed in the hope that it will be useful,
12 # but WITHOUT ANY WARRANTY; without even the implied warranty of
13 # MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
14 # GNU General Public License for more details.
16 # You should have received a copy of the GNU General Public License
17 # along with this program. If not, see <https://www.gnu.org/licenses/>.
19 . "${srcdir=.}/tests/init.sh"; path_prepend_ ./src
20 print_ver_ id
22 u="$( id -nu )"
23 id || fail=1
24 id "$u" || fail=1
26 # id(1) should refuse --zero in default format.
27 echo 'id: option --zero not permitted in default format' > err-exp \
28 || framework_failure_
29 id --zero > out 2>err && fail=1
30 compare /dev/null out || fail=1
31 compare err-exp err || fail=1
33 # Create a nice list of users.
34 # Add $USER to ensure we have at least one explicit entry.
35 users="$u"
36 # Add a few typical users to test single group and multiple groups.
37 for u in root man postfix sshd nobody ; do
38 id $u >/dev/null 2>&1 && users="$users $u"
39 done
40 # Add $users and '' (implicit $USER) to list to process.
41 printf '%s\n' $users '' >> users || framework_failure_
43 # Exercise "id -z" with various options.
44 printf '\n' > exp || framework_failure_
45 > out || framework_failure_
47 while read u ; do
48 for o in g gr G Gr u ur ; do
49 for n in '' n ; do
50 printf '%s: ' "id -${o}${n}[z] $u" >> exp || framework_failure_
51 printf '\n%s: ' "id -${o}${n}[z] $u" >> out || framework_failure_
52 # There may be no name corresponding to an id, so don't check
53 # exit status when in name lookup mode
54 id -${o}${n} $u >> exp ||
55 { test $? -ne 1 || test -z "$n" && fail=1; }
56 id -${o}${n}z $u > tmp ||
57 { test $? -ne 1 || test -z "$n" && fail=1; }
58 head -c-1 < tmp >> out || framework_failure_
59 done
60 done
61 done < users
62 printf '\n' >> out || framework_failure_
63 tr '\0' ' ' < out > out2 || framework_failure_
64 compare exp out2 || fail=1
66 # multiuser testing with -z
67 # test if the options work, these tests should pass if the above tests
68 # do.
70 for o in g gr u ur ; do
71 for n in '' n ; do
72 id -${o}${n} $users >> tmp1 ||
73 { test $? -ne 1 || test -z "$n" && fail=1; }
74 id -${o}${n}z $users > tmp2 ||
75 { test $? -ne 1 || test -z "$n" && fail=1; }
76 tr '\0' '\n' < tmp2 >> tmp3
77 done
78 done
79 compare tmp1 tmp3 || fail=1
81 # Separate checks when we are testing for multiple users && -G.
82 # This is done because we terminate the records with two NULs
83 # instead of a regular single NUL.
85 NL='
88 for o in G Gr ; do
89 for n in '' n ; do
90 id -${o}${n} $users >> gtmp1 ||
91 { test $? -ne 1 || test -z "$n" && fail=1; }
92 echo >> gtmp1 || framework_failure_
94 id -${o}${n}z $users > gtmp2 ||
95 { test $? -ne 1 || test -z "$n" && fail=1; }
96 # we replace all NULs with spaces, the result we get is there are two
97 # consecutive spaces instead of two NUL's, we pass this to sed
98 # to replace more than 1 space with a newline. This is ideally where a new
99 # line should be. This should make the output similar to without -z.
100 { tr '\0' ' ' < gtmp2; echo; } | sed "s/ /\\$NL/g" >> gtmp3
101 done
102 done
103 compare gtmp1 gtmp3 || fail=1
105 Exit $fail