1#!/bin/sh
2# Make sure all of these programs work properly
3# when invoked with --help or --version.
4
5# Copyright (C) 2000-2023 Free Software Foundation, Inc.
6
7# This program is free software: you can redistribute it and/or modify
8# it under the terms of the GNU General Public License as published by
9# the Free Software Foundation, either version 3 of the License, or
10# (at your option) any later version.
11
12# This program is distributed in the hope that it will be useful,
13# but WITHOUT ANY WARRANTY; without even the implied warranty of
14# MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the
15# GNU General Public License for more details.
16
17# You should have received a copy of the GNU General Public License
18# along with this program.  If not, see <https://www.gnu.org/licenses/>.
19
20. "${srcdir=.}/tests/init.sh"; path_prepend_ ./src
21
22# Terminate any background processes
23cleanup_() { kill $pid 2>/dev/null && wait $pid; }
24
25expected_failure_status_chroot=125
26expected_failure_status_env=125
27expected_failure_status_nice=125
28expected_failure_status_nohup=125
29expected_failure_status_runcon=125
30expected_failure_status_stdbuf=125
31expected_failure_status_timeout=125
32expected_failure_status_printenv=2
33expected_failure_status_tty=3
34expected_failure_status_sort=2
35expected_failure_status_expr=3
36expected_failure_status_lbracket=2
37expected_failure_status_dir=2
38expected_failure_status_ls=2
39expected_failure_status_vdir=2
40
41expected_failure_status_cmp=2
42expected_failure_status_zcmp=2
43expected_failure_status_sdiff=2
44expected_failure_status_diff3=2
45expected_failure_status_diff=2
46expected_failure_status_zdiff=2
47expected_failure_status_zgrep=2
48expected_failure_status_zegrep=2
49expected_failure_status_zfgrep=2
50
51expected_failure_status_grep=2
52expected_failure_status_egrep=2
53expected_failure_status_fgrep=2
54
55test "$built_programs" \
56  || fail_ "built_programs not specified!?!"
57
58test "$VERSION" \
59  || fail_ "set envvar VERSION; it is required for a PATH sanity-check"
60
61# Extract version from --version output of the first program
62for i in $built_programs; do
63  v=$(env $i --version | sed -n '1s/.* //p;q')
64  break
65done
66
67# Ensure that it matches $VERSION.
68test "x$v" = "x$VERSION" \
69  || fail_ "--version-\$VERSION mismatch"
70
71for i in $built_programs; do
72
73  # Skip 'test'; it doesn't accept --help or --version.
74  test $i = test && continue
75
76  # false fails even when invoked with --help or --version.
77  # true and false are tested with these options separately.
78  test $i = false || test $i = true && continue
79
80  # The just-built install executable is always named 'ginstall'.
81  test $i = install && i=ginstall
82
83  # Make sure they exit successfully, under normal conditions.
84  env $i --help    >/dev/null || fail=1
85  env $i --version >/dev/null || fail=1
86
87  # Make sure they fail upon 'file system full' error.
88  if test -w /dev/full && test -c /dev/full &&
89       ! printf x >/dev/full 2>/dev/null; then
90    test $i = [ && prog=lbracket || prog=$(echo $i|sed "s/$EXEEXT$//")
91    eval "expected=\$expected_failure_status_$prog"
92    test x$expected = x && expected=1
93
94    returns_ $expected env $i --help    >/dev/full 2>/dev/null &&
95    returns_ $expected env $i --version >/dev/full 2>/dev/null ||
96    {
97      fail=1
98      env $i --help >/dev/full 2>/dev/null
99      status=$?
100      echo "*** $i: bad exit status '$status' (expected $expected)," 1>&2
101      echo "  with --help or --version output redirected to /dev/full" 1>&2
102    }
103  fi
104done
105
106bigZ_in=bigZ-in.Z
107zin=zin.gz
108zin2=zin2.gz
109
110tmp=tmp-$$
111tmp_in=in-$$
112tmp_in2=in2-$$
113tmp_dir=dir-$$
114tmp_out=out-$$
115mkdir $tmp || fail=1
116cd $tmp || fail=1
117
118comm_setup () { args="$tmp_in $tmp_in"; }
119csplit_setup () { args="$tmp_in //"; }
120cut_setup () { args='-f 1'; }
121join_setup () { args="$tmp_in $tmp_in"; }
122tr_setup () { args='a a'; }
123
124chmod_setup () { args="a+x $tmp_in"; }
125# Punt on these.
126chgrp_setup () { args=--version; }
127chown_setup () { args=--version; }
128mkfifo_setup () { args=--version; }
129mknod_setup () { args=--version; }
130# Punt on uptime, since it fails (e.g., failing to get boot time)
131# on some systems, and we shouldn't let that stop 'make check'.
132uptime_setup () { args=--version; }
133
134# Create a file in the current directory, not in $TMPDIR.
135mktemp_setup () { args=mktemp.XXXX; }
136
137cmp_setup () { args="$tmp_in $tmp_in2"; }
138
139# Tell dd not to print the line with transfer rate and total.
140# The transfer rate would vary between runs.
141dd_setup () { args=status=noxfer; }
142
143zdiff_setup () { args="$zin $zin2"; }
144zcmp_setup () { zdiff_setup; }
145zcat_setup () { TERM=dumb; export TERM; args=$zin; }
146gunzip_setup () { zcat_setup; }
147zmore_setup () { zcat_setup; }
148zless_setup () { zcat_setup; }
149znew_setup () { args=$bigZ_in; }
150zforce_setup () { zcat_setup; }
151zgrep_setup () { args="z $zin"; }
152zegrep_setup () { zgrep_setup; }
153zfgrep_setup () { zgrep_setup; }
154gzexe_setup () { args=$tmp_in; }
155
156# We know that $tmp_in contains a "0"
157grep_setup () { args="0 $tmp_in"; }
158egrep_setup () { args="0 $tmp_in"; }
159fgrep_setup () { args="0 $tmp_in"; }
160
161diff_setup () { args="$tmp_in $tmp_in2"; }
162sdiff_setup () { args="$tmp_in $tmp_in2"; }
163diff3_setup () { args="$tmp_in $tmp_in2 $tmp_in2"; }
164cp_setup () { args="$tmp_in $tmp_in2"; }
165ln_setup () { args="$tmp_in ln-target"; }
166ginstall_setup () { args="$tmp_in $tmp_in2"; }
167mv_setup () { args="$tmp_in $tmp_in2"; }
168mkdir_setup () { args=$tmp_dir/subdir; }
169realpath_setup () { args=$tmp_in; }
170rmdir_setup () { args=$tmp_dir; }
171rm_setup () { args=$tmp_in; }
172shred_setup () { args=$tmp_in; }
173touch_setup () { args=$tmp_in2; }
174truncate_setup () { args="--reference=$tmp_in $tmp_in2"; }
175
176mkid_setup () { printf 'f(){}\ntypedef int t;\n' > f.c; args=. ; }
177lid_setup () { args=; }
178fid_setup () { args=f.c; }
179fnid_setup () { args=; }
180xtokid_setup () { args=; }
181aid_setup () { args=f; }
182eid_setup () { args=--version; }
183gid_setup () { args=f; }
184defid_setup () { args=t; }
185
186basename_setup () { args=$tmp_in; }
187dirname_setup () { args=$tmp_in; }
188expr_setup () { args=foo; }
189basenc_setup () { args=--version; }
190
191# Punt, in case GNU 'id' hasn't been installed yet.
192groups_setup () { args=--version; }
193
194pathchk_setup () { args=$tmp_in; }
195yes_setup () { args=--version; }
196logname_setup () { args=--version; }
197nohup_setup () { args=--version; }
198printf_setup () { args=foo; }
199seq_setup () { args=10; }
200sleep_setup () { args=0; }
201stdbuf_setup () { args="-oL true"; }
202timeout_setup () { args=--version; }
203
204# I'd rather not run sync, since it spins up disks that I've
205# deliberately caused to spin down (but not unmounted).
206sync_setup () { args=--version; }
207
208test_setup () { args=foo; }
209
210# This is necessary in the unusual event that there is
211# no valid entry in /etc/mtab.
212df_setup () { args=/; }
213
214# This is necessary in the unusual event that getpwuid (getuid ()) fails.
215id_setup () { args=-u; }
216
217# Use env to avoid invoking built-in sleep of Solaris 11's /bin/sh.
218kill_setup () {
219  external=env
220  $external sleep 10m & pid=$!
221  args=$pid
222}
223
224link_setup () { args="$tmp_in link-target"; }
225unlink_setup () { args=$tmp_in; }
226
227readlink_setup () {
228  ln -s . slink
229  args=slink;
230}
231
232stat_setup () { args=$tmp_in; }
233unlink_setup () { args=$tmp_in; }
234lbracket_setup () { args=": ]"; }
235
236parted_setup () { args="-s $tmp_in mklabel gpt"
237  dd if=/dev/null of=$tmp_in seek=2000; }
238
239# Ensure that each program "works" (exits successfully) when doing
240# something more than --help or --version.
241for i in $built_programs; do
242  # Skip these.
243  case $i in chroot|stty|tty|false|chcon|runcon|coreutils) continue;; esac
244
245  rm -rf $tmp_in $tmp_in2 $tmp_dir $tmp_out $bigZ_in $zin $zin2
246  echo z |gzip > $zin
247  cp $zin $zin2
248  cp $zin $bigZ_in
249
250  # This is sort of kludgey: use numbers so this is valid input for factor,
251  # and two tokens so it's valid input for tsort.
252  echo 2147483647 0 > $tmp_in
253  # Make $tmp_in2 identical. Then, using $tmp_in and $tmp_in2 as arguments
254  # to the likes of cmp and diff makes them exit successfully.
255  cp $tmp_in $tmp_in2
256  mkdir $tmp_dir
257  # echo ================== $i
258  test $i = [ && prog=lbracket || prog=$(echo $i|sed "s/$EXEEXT$//")
259  if type ${prog}_setup > /dev/null 2>&1; then
260    ${prog}_setup
261  else
262    args=
263  fi
264  if env $i $args < $tmp_in > $tmp_out; then
265    : # ok
266  else
267    echo FAIL: $i
268    fail=1
269  fi
270  rm -rf $tmp_in $tmp_in2 $tmp_out $tmp_dir
271done
272
273Exit $fail
274