maint: remove Local Variables: indent-tabs-mode: nil from all sources
[coreutils.git] / tests / test-lib.sh
blobad7a0e9073beb3af04949e3cf3ffdec3c4476929
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_openat_support_()
52 # Skip this test if your system has neither the openat-style functions
53 # nor /proc/self/fd support with which to emulate them.
54 test -z "$CONFIG_HEADER" \
55 && skip_test_ 'internal error: CONFIG_HEADER not defined'
57 _skip=yes
58 grep '^#define HAVE_OPENAT' "$CONFIG_HEADER" > /dev/null && _skip=no
59 test -d /proc/self/fd && _skip=no
60 if test $_skip = yes; then
61 skip_test_ 'this system lacks openat support'
65 require_ulimit_()
67 ulimit_works=yes
68 # Expect to be able to exec a program in 10MB of virtual memory,
69 # but not in 20KB. I chose "date". It must not be a shell built-in
70 # function, so you can't use echo, printf, true, etc.
71 # Of course, in coreutils, I could use $top_builddir/src/true,
72 # but this should be able to work for other projects, too.
73 ( ulimit -v 10000; date ) > /dev/null 2>&1 || ulimit_works=no
74 ( ulimit -v 20; date ) > /dev/null 2>&1 && ulimit_works=no
76 test $ulimit_works = no \
77 && skip_test_ "this shell lacks ulimit support"
80 require_readable_root_()
82 test -r / || skip_test_ "/ is not readable"
85 # Skip the current test if strace is not available or doesn't work
86 # with the named syscall. Usage: require_strace_ unlink
87 require_strace_()
89 test $# = 1 || framework_failure
91 strace -V < /dev/null > /dev/null 2>&1 ||
92 skip_test_ 'no strace program'
94 strace -qe "$1" echo > /dev/null 2>&1 ||
95 skip_test_ 'strace -qe "'"$1"'" does not work'
98 # Require a controlling input `terminal'.
99 require_controlling_input_terminal_()
101 tty -s || have_input_tty=no
102 test -t 0 || have_input_tty=no
103 if test "$have_input_tty" = no; then
104 skip_test_ 'requires controlling input terminal
105 This test must have a controlling input "terminal", so it may not be
106 run via "batch", "at", or "ssh". On some systems, it may not even be
107 run in the background.'
111 require_built_()
113 skip_=no
114 for i in "$@"; do
115 case " $built_programs " in
116 *" $i "*) ;;
117 *) echo "$i: not built" 1>&2; skip_=yes ;;
118 esac
119 done
121 test $skip_ = yes && skip_test_ "required program(s) not built"
124 uid_is_privileged_()
126 # Make sure id -u succeeds.
127 my_uid=$(id -u) \
128 || { echo "$0: cannot run \`id -u'" 1>&2; return 1; }
130 # Make sure it gives valid output.
131 case $my_uid in
132 0) ;;
133 *[!0-9]*)
134 echo "$0: invalid output (\`$my_uid') from \`id -u'" 1>&2
135 return 1 ;;
136 *) return 1 ;;
137 esac
140 get_process_status_()
142 sed -n '/^State:[ ]*\([[:alpha:]]\).*/s//\1/p' /proc/$1/status
145 # Convert an ls-style permission string, like drwxr----x and -rw-r-x-wx
146 # to the equivalent chmod --mode (-m) argument, (=,u=rwx,g=r,o=x and
147 # =,u=rw,g=rx,o=wx). Ignore ACLs.
148 rwx_to_mode_()
150 case $# in
151 1) rwx=$1;;
152 *) echo "$0: wrong number of arguments" 1>&2
153 echo "Usage: $0 ls-style-mode-string" 1>&2
154 return;;
155 esac
157 case $rwx in
158 [ld-][rwx-][rwx-][rwxsS-][rwx-][rwx-][rwxsS-][rwx-][rwx-][rwxtT-]) ;;
159 [ld-][rwx-][rwx-][rwxsS-][rwx-][rwx-][rwxsS-][rwx-][rwx-][rwxtT-][+.]) ;;
160 *) echo "$0: invalid mode string: $rwx" 1>&2; return;;
161 esac
163 # Perform these conversions:
164 # S s
165 # s xs
166 # T t
167 # t xt
168 # The `T' and `t' ones are only valid for `other'.
169 s='s/S/@/;s/s/x@/;s/@/s/'
170 t='s/T/@/;s/t/x@/;s/@/t/'
172 u=`echo $rwx|sed 's/^.\(...\).*/,u=\1/;s/-//g;s/^,u=$//;'$s`
173 g=`echo $rwx|sed 's/^....\(...\).*/,g=\1/;s/-//g;s/^,g=$//;'$s`
174 o=`echo $rwx|sed 's/^.......\(...\).*/,o=\1/;s/-//g;s/^,o=$//;'$s';'$t`
175 echo "=$u$g$o"
178 skip_if_()
180 case $1 in
181 root) skip_test_ must be run as root ;;
182 non-root) skip_test_ must be run as non-root ;;
183 *) ;; # FIXME?
184 esac
187 require_selinux_()
189 case `ls -Zd .` in
190 '? .'|'unlabeled .')
191 skip_test_ "this system (or maybe just" \
192 "the current file system) lacks SELinux support"
194 esac
197 very_expensive_()
199 if test "$RUN_VERY_EXPENSIVE_TESTS" != yes; then
200 skip_test_ 'very expensive: disabled by default
201 This test is very expensive, so it is disabled by default.
202 To run it anyway, rerun make check with the RUN_VERY_EXPENSIVE_TESTS
203 environment variable set to yes. E.g.,
205 env RUN_VERY_EXPENSIVE_TESTS=yes make check
210 expensive_()
212 if test "$RUN_EXPENSIVE_TESTS" != yes; then
213 skip_test_ 'expensive: disabled by default
214 This test is relatively expensive, so it is disabled by default.
215 To run it anyway, rerun make check with the RUN_EXPENSIVE_TESTS
216 environment variable set to yes. E.g.,
218 env RUN_EXPENSIVE_TESTS=yes make check
223 require_root_()
225 uid_is_privileged_ || skip_test_ "must be run as root"
226 NON_ROOT_USERNAME=${NON_ROOT_USERNAME=nobody}
227 NON_ROOT_GROUP=${NON_ROOT_GROUP=$(id -g $NON_ROOT_USERNAME)}
230 skip_if_root_() { uid_is_privileged_ && skip_test_ "must be run as non-root"; }
231 error_() { echo "$0: $@" 1>&2; Exit 1; }
232 framework_failure() { error_ 'failure in testing framework'; }
234 # Set `groups' to a space-separated list of at least two groups
235 # of which the user is a member.
236 require_membership_in_two_groups_()
238 test $# = 0 || framework_failure
240 groups=${COREUTILS_GROUPS-`(id -G || /usr/xpg4/bin/id -G) 2>/dev/null`}
241 case "$groups" in
242 *' '*) ;;
243 *) skip_test_ 'requires membership in two groups
244 this test requires that you be a member of more than one group,
245 but running `id -G'\'' either failed or found just one. If you really
246 are a member of at least two groups, then rerun this test with
247 COREUTILS_GROUPS set in your environment to the space-separated list
248 of group names or numbers. E.g.,
250 env COREUTILS_GROUPS='users cdrom' make check
254 esac
257 # Is /proc/$PID/status supported?
258 require_proc_pid_status_()
260 sleep 2 &
261 local pid=$!
262 sleep .5
263 grep '^State:[ ]*[S]' /proc/$pid/status > /dev/null 2>&1 ||
264 skip_test_ "/proc/$pid/status: missing or 'different'"
265 kill $pid
268 # Does the current (working-dir) file system support sparse files?
269 require_sparse_support_()
271 test $# = 0 || framework_failure
272 # Test whether we can create a sparse file.
273 # For example, on Darwin6.5 with a file system of type hfs, it's not possible.
274 # NTFS requires 128K before a hole appears in a sparse file.
275 t=sparse.$$
276 dd bs=1 seek=128K of=$t < /dev/null 2> /dev/null
277 set x `du -sk $t`
278 kb_size=$2
279 rm -f $t
280 if test $kb_size -ge 128; then
281 skip_test_ 'this file system does not support sparse files'
285 mkfifo_or_skip_()
287 test $# = 1 || framework_failure
288 if ! mkfifo "$1"; then
289 # Make an exception of this case -- usually we interpret framework-creation
290 # failure as a test failure. However, in this case, when running on a SunOS
291 # system using a disk NFS mounted from OpenBSD, the above fails like this:
292 # mkfifo: cannot make fifo `fifo-10558': Not owner
293 skip_test_ 'NOTICE: unable to create test prerequisites'
297 # Disable the current test if the working directory seems to have
298 # the setgid bit set.
299 skip_if_setgid_()
301 setgid_tmpdir=setgid-$$
302 (umask 77; mkdir $setgid_tmpdir)
303 perms=$(stat --printf %A $setgid_tmpdir)
304 rmdir $setgid_tmpdir
305 case $perms in
306 drwx------);;
307 drwxr-xr-x);; # Windows98 + DJGPP 2.03
308 *) skip_test_ 'this directory has the setgid bit set';;
309 esac
312 skip_if_mcstransd_is_running_()
314 test $# = 0 || framework_failure
316 # When mcstransd is running, you'll see only the 3-component
317 # version of file-system context strings. Detect that,
318 # and if it's running, skip this test.
319 __ctx=$(stat --printf='%C\n' .) || framework_failure
320 case $__ctx in
321 *:*:*:*) ;; # four components is ok
322 *) # anything else probably means mcstransd is running
323 skip_test_ "unexpected context '$__ctx'; turn off mcstransd" ;;
324 esac
327 # Skip the current test if umask doesn't work as usual.
328 # This test should be run in the temporary directory that ends
329 # up being removed via the trap commands.
330 working_umask_or_skip_()
332 umask 022
333 touch file1 file2
334 chmod 644 file2
335 perms=`ls -l file1 file2 | sed 's/ .*//' | uniq`
336 rm -f file1 file2
338 case $perms in
340 '*) skip_test_ 'your build directory has unusual umask semantics'
341 esac
344 # We use a trap below for cleanup. This requires us to go through
345 # hoops to get the right exit status transported through the signal.
346 # So use `Exit STATUS' instead of `exit STATUS' inside of the tests.
347 # Turn off errexit here so that we don't trip the bug with OSF1/Tru64
348 # sh inside this function.
349 Exit ()
351 set +e
352 (exit $1)
353 exit $1
356 test_dir_=$(pwd)
358 this_test_() { echo "./$0" | sed 's,.*/,,'; }
359 this_test=$(this_test_)
361 # This is a stub function that is run upon trap (upon regular exit and
362 # interrupt). Override it with a per-test function, e.g., to unmount
363 # a partition, or to undo any other global state changes.
364 cleanup_() { :; }
366 t_=$("$abs_top_builddir/src/mktemp" -d --tmp="$test_dir_" cu-$this_test.XXXXXXXXXX)\
367 || error_ "failed to create temporary directory in $test_dir_"
369 remove_tmp_()
371 __st=$?
372 cleanup_
373 cd "$test_dir_" && chmod -R u+rwx "$t_" && rm -rf "$t_" && exit $__st
376 # Run each test from within a temporary sub-directory named after the
377 # test itself, and arrange to remove it upon exception or normal exit.
378 trap remove_tmp_ 0
379 trap 'Exit $?' 1 2 13 15
381 cd "$t_" || error_ "failed to cd to $t_"
383 if ( diff --version < /dev/null 2>&1 | grep GNU ) 2>&1 > /dev/null; then
384 compare() { diff -u "$@"; }
385 elif ( cmp --version < /dev/null 2>&1 | grep GNU ) 2>&1 > /dev/null; then
386 compare() { cmp -s "$@"; }
387 else
388 compare() { cmp "$@"; }