maint: revert last change: we're not ready for "local" in scripts, yet
[coreutils.git] / tests / test-lib.sh
blobd99e3a96627a97821d51763f9b94b517452f765d
1 # source this file; set up for tests
3 # Copyright (C) 2009 Free Software Foundation, Inc.
5 # This program is free software: you can redistribute it and/or modify
6 # it under the terms of the GNU General Public License as published by
7 # the Free Software Foundation, either version 3 of the License, or
8 # (at your option) any later version.
10 # This program is distributed in the hope that it will be useful,
11 # but WITHOUT ANY WARRANTY; without even the implied warranty of
12 # MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
13 # GNU General Public License for more details.
15 # You should have received a copy of the GNU General Public License
16 # along with this program. If not, see <http://www.gnu.org/licenses/>.
18 # Skip this test if the shell lacks support for functions.
19 unset function_test
20 eval 'function_test() { return 11; }; function_test'
21 if test $? != 11; then
22 echo "$0: /bin/sh lacks support for functions; skipping this test." 1>&2
23 Exit 77
26 skip_test_()
28 echo "$0: skipping test: $@" | head -1 1>&9
29 echo "$0: skipping test: $@" 1>&2
30 Exit 77
33 getlimits_()
35 eval $(getlimits)
36 test "$INT_MAX" ||
37 error_ "Error running getlimits"
40 require_acl_()
42 getfacl --version < /dev/null > /dev/null 2>&1 \
43 && setfacl --version < /dev/null > /dev/null 2>&1 \
44 || skip_test_ "This test requires getfacl and setfacl."
46 id -u bin > /dev/null 2>&1 \
47 || skip_test_ "This test requires a local user named bin."
50 require_ulimit_()
52 ulimit_works=yes
53 # Expect to be able to exec a program in 10MB of virtual memory,
54 # but not in 20KB. I chose "date". It must not be a shell built-in
55 # function, so you can't use echo, printf, true, etc.
56 # Of course, in coreutils, I could use $top_builddir/src/true,
57 # but this should be able to work for other projects, too.
58 ( ulimit -v 10000; date ) > /dev/null 2>&1 || ulimit_works=no
59 ( ulimit -v 20; date ) > /dev/null 2>&1 && ulimit_works=no
61 test $ulimit_works = no \
62 && skip_test_ "this shell lacks ulimit support"
65 require_readable_root_()
67 test -r / || skip_test_ "/ is not readable"
70 # Skip the current test if strace is not available or doesn't work
71 # with the named syscall. Usage: require_strace_ unlink
72 require_strace_()
74 test $# = 1 || framework_failure
76 strace -V < /dev/null > /dev/null 2>&1 ||
77 skip_test_ 'no strace program'
79 strace -qe "$1" echo > /dev/null 2>&1 ||
80 skip_test_ 'strace -qe "'"$1"'" does not work'
83 # Require a controlling input `terminal'.
84 require_controlling_input_terminal_()
86 tty -s || have_input_tty=no
87 test -t 0 || have_input_tty=no
88 if test "$have_input_tty" = no; then
89 skip_test_ 'requires controlling input terminal
90 This test must have a controlling input "terminal", so it may not be
91 run via "batch", "at", or "ssh". On some systems, it may not even be
92 run in the background.'
96 require_built_()
98 skip_=no
99 for i in "$@"; do
100 case " $built_programs " in
101 *" $i "*) ;;
102 *) echo "$i: not built" 1>&2; skip_=yes ;;
103 esac
104 done
106 test $skip_ = yes && skip_test_ "required program(s) not built"
109 uid_is_privileged_()
111 # Make sure id -u succeeds.
112 my_uid=$(id -u) \
113 || { echo "$0: cannot run \`id -u'" 1>&2; return 1; }
115 # Make sure it gives valid output.
116 case $my_uid in
117 0) ;;
118 *[!0-9]*)
119 echo "$0: invalid output (\`$my_uid') from \`id -u'" 1>&2
120 return 1 ;;
121 *) return 1 ;;
122 esac
125 get_process_status_()
127 sed -n '/^State:[ ]*\([[:alpha:]]\).*/s//\1/p' /proc/$1/status
130 # Convert an ls-style permission string, like drwxr----x and -rw-r-x-wx
131 # to the equivalent chmod --mode (-m) argument, (=,u=rwx,g=r,o=x and
132 # =,u=rw,g=rx,o=wx). Ignore ACLs.
133 rwx_to_mode_()
135 case $# in
136 1) rwx=$1;;
137 *) echo "$0: wrong number of arguments" 1>&2
138 echo "Usage: $0 ls-style-mode-string" 1>&2
139 return;;
140 esac
142 case $rwx in
143 [ld-][rwx-][rwx-][rwxsS-][rwx-][rwx-][rwxsS-][rwx-][rwx-][rwxtT-]) ;;
144 [ld-][rwx-][rwx-][rwxsS-][rwx-][rwx-][rwxsS-][rwx-][rwx-][rwxtT-][+.]) ;;
145 *) echo "$0: invalid mode string: $rwx" 1>&2; return;;
146 esac
148 # Perform these conversions:
149 # S s
150 # s xs
151 # T t
152 # t xt
153 # The `T' and `t' ones are only valid for `other'.
154 s='s/S/@/;s/s/x@/;s/@/s/'
155 t='s/T/@/;s/t/x@/;s/@/t/'
157 u=`echo $rwx|sed 's/^.\(...\).*/,u=\1/;s/-//g;s/^,u=$//;'$s`
158 g=`echo $rwx|sed 's/^....\(...\).*/,g=\1/;s/-//g;s/^,g=$//;'$s`
159 o=`echo $rwx|sed 's/^.......\(...\).*/,o=\1/;s/-//g;s/^,o=$//;'$s';'$t`
160 echo "=$u$g$o"
163 skip_if_()
165 case $1 in
166 root) skip_test_ must be run as root ;;
167 non-root) skip_test_ must be run as non-root ;;
168 *) ;; # FIXME?
169 esac
172 require_selinux_()
174 case `ls -Zd .` in
175 '? .'|'unlabeled .')
176 skip_test_ "this system (or maybe just" \
177 "the current file system) lacks SELinux support"
179 esac
182 very_expensive_()
184 if test "$RUN_VERY_EXPENSIVE_TESTS" != yes; then
185 skip_test_ 'very expensive: disabled by default
186 This test is very expensive, so it is disabled by default.
187 To run it anyway, rerun make check with the RUN_VERY_EXPENSIVE_TESTS
188 environment variable set to yes. E.g.,
190 env RUN_VERY_EXPENSIVE_TESTS=yes make check
195 expensive_()
197 if test "$RUN_EXPENSIVE_TESTS" != yes; then
198 skip_test_ 'expensive: disabled by default
199 This test is relatively expensive, so it is disabled by default.
200 To run it anyway, rerun make check with the RUN_EXPENSIVE_TESTS
201 environment variable set to yes. E.g.,
203 env RUN_EXPENSIVE_TESTS=yes make check
208 require_root_()
210 uid_is_privileged_ || skip_test_ "must be run as root"
211 NON_ROOT_USERNAME=${NON_ROOT_USERNAME=nobody}
212 NON_ROOT_GROUP=${NON_ROOT_GROUP=nobody}
215 skip_if_root_() { uid_is_privileged_ && skip_test_ "must be run as non-root"; }
216 error_() { echo "$0: $@" 1>&2; Exit 1; }
217 framework_failure() { error_ 'failure in testing framework'; }
219 # Set `groups' to a space-separated list of at least two groups
220 # of which the user is a member.
221 require_membership_in_two_groups_()
223 test $# = 0 || framework_failure
225 groups=${COREUTILS_GROUPS-`(id -G || /usr/xpg4/bin/id -G) 2>/dev/null`}
226 case "$groups" in
227 *' '*) ;;
228 *) skip_test_ 'requires membership in two groups
229 this test requires that you be a member of more than one group,
230 but running `id -G'\'' either failed or found just one. If you really
231 are a member of at least two groups, then rerun this test with
232 COREUTILS_GROUPS set in your environment to the space-separated list
233 of group names or numbers. E.g.,
235 env COREUTILS_GROUPS='users cdrom' make check
239 esac
242 # Is /proc/$PID/status supported?
243 require_proc_pid_status_()
245 sleep 2 &
246 local pid=$!
247 sleep .5
248 grep '^State:[ ]*[S]' /proc/$pid/status > /dev/null 2>&1 ||
249 skip_test_ "/proc/$pid/status: missing or 'different'"
250 kill $pid
253 # Does the current (working-dir) file system support sparse files?
254 require_sparse_support_()
256 test $# = 0 || framework_failure
257 # Test whether we can create a sparse file.
258 # For example, on Darwin6.5 with a file system of type hfs, it's not possible.
259 # NTFS requires 128K before a hole appears in a sparse file.
260 t=sparse.$$
261 dd bs=1 seek=128K of=$t < /dev/null 2> /dev/null
262 set x `du -sk $t`
263 kb_size=$2
264 rm -f $t
265 if test $kb_size -ge 128; then
266 skip_test_ 'this file system does not support sparse files'
270 mkfifo_or_skip_()
272 test $# = 1 || framework_failure
273 if ! mkfifo "$1"; then
274 # Make an exception of this case -- usually we interpret framework-creation
275 # failure as a test failure. However, in this case, when running on a SunOS
276 # system using a disk NFS mounted from OpenBSD, the above fails like this:
277 # mkfifo: cannot make fifo `fifo-10558': Not owner
278 skip_test_ 'NOTICE: unable to create test prerequisites'
282 # Disable the current test if the working directory seems to have
283 # the setgid bit set.
284 skip_if_setgid_()
286 setgid_tmpdir=setgid-$$
287 (umask 77; mkdir $setgid_tmpdir)
288 perms=$(stat --printf %A $setgid_tmpdir)
289 rmdir $setgid_tmpdir
290 case $perms in
291 drwx------);;
292 drwxr-xr-x);; # Windows98 + DJGPP 2.03
293 *) skip_test_ 'this directory has the setgid bit set';;
294 esac
297 skip_if_mcstransd_is_running_()
299 test $# = 0 || framework_failure
301 # When mcstransd is running, you'll see only the 3-component
302 # version of file-system context strings. Detect that,
303 # and if it's running, skip this test.
304 __ctx=$(stat --printf='%C\n' .) || framework_failure
305 case $__ctx in
306 *:*:*:*) ;; # four components is ok
307 *) # anything else probably means mcstransd is running
308 skip_test_ "unexpected context '$__ctx'; turn off mcstransd" ;;
309 esac
312 # Skip the current test if umask doesn't work as usual.
313 # This test should be run in the temporary directory that ends
314 # up being removed via the trap commands.
315 working_umask_or_skip_()
317 umask 022
318 touch file1 file2
319 chmod 644 file2
320 perms=`ls -l file1 file2 | sed 's/ .*//' | uniq`
321 rm -f file1 file2
323 case $perms in
325 '*) skip_test_ 'your build directory has unusual umask semantics'
326 esac
329 # We use a trap below for cleanup. This requires us to go through
330 # hoops to get the right exit status transported through the signal.
331 # So use `Exit STATUS' instead of `exit STATUS' inside of the tests.
332 # Turn off errexit here so that we don't trip the bug with OSF1/Tru64
333 # sh inside this function.
334 Exit ()
336 set +e
337 (exit $1)
338 exit $1
341 test_dir_=$(pwd)
343 this_test_() { echo "./$0" | sed 's,.*/,,'; }
344 this_test=$(this_test_)
346 # This is a stub function that is run upon trap (upon regular exit and
347 # interrupt). Override it with a per-test function, e.g., to unmount
348 # a partition, or to undo any other global state changes.
349 cleanup_() { :; }
351 t_=$("$abs_top_builddir/src/mktemp" -d --tmp="$test_dir_" cu-$this_test.XXXXXXXXXX)\
352 || error_ "failed to create temporary directory in $test_dir_"
354 remove_tmp_()
356 __st=$?
357 cleanup_
358 cd "$test_dir_" && chmod -R u+rwx "$t_" && rm -rf "$t_" && exit $__st
361 # Run each test from within a temporary sub-directory named after the
362 # test itself, and arrange to remove it upon exception or normal exit.
363 trap remove_tmp_ 0
364 trap 'Exit $?' 1 2 13 15
366 cd "$t_" || error_ "failed to cd to $t_"
368 if ( diff --version < /dev/null 2>&1 | grep GNU ) 2>&1 > /dev/null; then
369 compare() { diff -u "$@"; }
370 elif ( cmp --version < /dev/null 2>&1 | grep GNU ) 2>&1 > /dev/null; then
371 compare() { cmp -s "$@"; }
372 else
373 compare() { cmp "$@"; }
376 # Local Variables:
377 # indent-tabs-mode: nil
378 # End: