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