1#!/bin/sh
2# Exercise "id --zero".
3
4# Copyright (C) 2013-2023 Free Software Foundation, Inc.
5
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.
10
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.
15
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/>.
18
19. "${srcdir=.}/tests/init.sh"; path_prepend_ ./src
20print_ver_ id
21
22u="$( id -nu )"
23id || fail=1
24id "$u" || fail=1
25
26# id(1) should refuse --zero in default format.
27echo 'id: option --zero not permitted in default format' > err-exp \
28  || framework_failure_
29id --zero > out 2>err && fail=1
30compare /dev/null out || fail=1
31compare err-exp   err || fail=1
32
33# Create a nice list of users.
34# Add $USER to ensure we have at least one explicit entry.
35users="$u"
36# Add a few typical users to test single group and multiple groups.
37for u in root man postfix sshd nobody ; do
38  id $u >/dev/null 2>&1 && users="$users $u"
39done
40# Add $users and '' (implicit $USER) to list to process.
41printf '%s\n' $users '' >> users || framework_failure_
42
43# Exercise "id -z" with various options.
44printf '\n' > exp || framework_failure_
45> out || framework_failure_
46
47while 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
61done < users
62printf '\n' >> out || framework_failure_
63tr '\0' ' ' < out > out2 || framework_failure_
64compare exp out2 || fail=1
65
66# multiuser testing with -z
67# test if the options work, these tests should pass if the above tests
68# do.
69
70for 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
78done
79compare tmp1 tmp3 || fail=1
80
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.
84
85NL='
86'
87
88for 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_
93
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
102done
103compare gtmp1 gtmp3 || fail=1
104
105Exit $fail
106