1 # This file is sourced by init.sh, *before* its initialization.
3 # This goes hand in hand with the "exec 9>&2;" in tests/Makefile.am's
4 # TESTS_ENVIRONMENT definition.
7 # FIXME: eventually s/error_/fail_/ and remove the definition of error_ below.
8 # FIXME: s/(framework_failure)\>/${1}_/ and remove def. of framework_failure
10 # Having an unsearchable directory in PATH causes execve to fail with EACCES
11 # when applied to an unresolvable program name, contrary to the desired ENOENT.
12 # Avoid the problem by rewriting PATH to exclude unsearchable directories.
15 # FIXME: remove double quotes around $IFS when all tests use init.sh.
16 # They constitute a work-around for a bug in FreeBSD 8.1's /bin/sh.
17 local saved_IFS="$IFS"
26 test -z "$d" && d1=. || d1=$d
27 if ls -d "$d1/." > /dev/null 2>&1; then
28 new_path="$new_path$colon$d"
39 echo "$0: skipping test: $@" | head -1 1>&9
40 echo "$0: skipping test: $@" 1>&2
48 error_ "Error running getlimits"
53 getfacl --version < /dev/null > /dev/null 2>&1 \
54 && setfacl --version < /dev/null > /dev/null 2>&1 \
55 || skip_test_ "This test requires getfacl and setfacl."
57 id -u bin > /dev/null 2>&1 \
58 || skip_test_ "This test requires a local user named bin."
63 test $# = 1 || framework_failure
64 df --local "$1" >/dev/null 2>&1
70 skip_test_ "This test must be run on a local file system."
73 # Skip this test if we're not in SELinux "enforcing" mode.
74 require_selinux_enforcing_()
76 test "$(getenforce)" = Enforcing \
77 || skip_test_ "This test is useful only with SELinux in Enforcing mode."
80 require_openat_support_()
82 # Skip this test if your system has neither the openat-style functions
83 # nor /proc/self/fd support with which to emulate them.
84 test -z "$CONFIG_HEADER" \
85 && skip_test_ 'internal error: CONFIG_HEADER not defined'
88 grep '^#define HAVE_OPENAT' "$CONFIG_HEADER" > /dev/null && _skip=no
89 test -d /proc/self/fd && _skip=no
90 if test $_skip = yes; then
91 skip_test_ 'this system lacks openat support'
98 # Expect to be able to exec a program in 10MB of virtual memory,
99 # but not in 20KB. I chose "date". It must not be a shell built-in
100 # function, so you can't use echo, printf, true, etc.
101 # Of course, in coreutils, I could use $top_builddir/src/true,
102 # but this should be able to work for other projects, too.
103 ( ulimit -v 10000; date ) > /dev/null 2>&1 || ulimit_works=no
104 ( ulimit -v 20; date ) > /dev/null 2>&1 && ulimit_works=no
106 test $ulimit_works = no \
107 && skip_test_ "this shell lacks ulimit support"
110 require_readable_root_()
112 test -r / || skip_test_ "/ is not readable"
115 # Skip the current test if strace is not available or doesn't work
116 # with the named syscall. Usage: require_strace_ unlink
119 test $# = 1 || framework_failure
121 strace -V < /dev/null > /dev/null 2>&1 ||
122 skip_test_ 'no strace program'
124 strace -qe "$1" echo > /dev/null 2>&1 ||
125 skip_test_ 'strace -qe "'"$1"'" does not work'
128 # Require a controlling input `terminal'.
129 require_controlling_input_terminal_()
131 tty -s || have_input_tty=no
132 test -t 0 || have_input_tty=no
133 if test "$have_input_tty" = no; then
134 skip_test_ 'requires controlling input terminal
135 This test must have a controlling input "terminal", so it may not be
136 run via "batch", "at", or "ssh". On some systems, it may not even be
137 run in the background.'
145 case " $built_programs " in
147 *) echo "$i: not built" 1>&2; skip_=yes ;;
151 test $skip_ = yes && skip_test_ "required program(s) not built"
156 # Make sure id -u succeeds.
158 || { echo "$0: cannot run \`id -u'" 1>&2; return 1; }
160 # Make sure it gives valid output.
164 echo "$0: invalid output (\`$my_uid') from \`id -u'" 1>&2
170 get_process_status_()
172 sed -n '/^State:[ ]*\([[:alpha:]]\).*/s//\1/p' /proc/$1/status
175 # Convert an ls-style permission string, like drwxr----x and -rw-r-x-wx
176 # to the equivalent chmod --mode (-m) argument, (=,u=rwx,g=r,o=x and
177 # =,u=rw,g=rx,o=wx). Ignore ACLs.
182 *) echo "$0: wrong number of arguments" 1>&2
183 echo "Usage: $0 ls-style-mode-string" 1>&2
188 [ld-][rwx-][rwx-][rwxsS-][rwx-][rwx-][rwxsS-][rwx-][rwx-][rwxtT-]) ;;
189 [ld-][rwx-][rwx-][rwxsS-][rwx-][rwx-][rwxsS-][rwx-][rwx-][rwxtT-][+.]) ;;
190 *) echo "$0: invalid mode string: $rwx" 1>&2; return;;
193 # Perform these conversions:
198 # The `T' and `t' ones are only valid for `other'.
199 s='s/S/@/;s/s/x@/;s/@/s/'
200 t='s/T/@/;s/t/x@/;s/@/t/'
202 u=`echo $rwx|sed 's/^.\(...\).*/,u=\1/;s/-//g;s/^,u=$//;'$s`
203 g=`echo $rwx|sed 's/^....\(...\).*/,g=\1/;s/-//g;s/^,g=$//;'$s`
204 o=`echo $rwx|sed 's/^.......\(...\).*/,o=\1/;s/-//g;s/^,o=$//;'$s';'$t`
211 root) skip_test_ must be run as root ;;
212 non-root) skip_test_ must be run as non-root ;;
221 skip_test_ "this system (or maybe just" \
222 "the current file system) lacks SELinux support"
229 if test "$RUN_VERY_EXPENSIVE_TESTS" != yes; then
230 skip_test_ 'very expensive: disabled by default
231 This test is very expensive, so it is disabled by default.
232 To run it anyway, rerun make check with the RUN_VERY_EXPENSIVE_TESTS
233 environment variable set to yes. E.g.,
235 env RUN_VERY_EXPENSIVE_TESTS=yes make check
242 if test "$RUN_EXPENSIVE_TESTS" != yes; then
243 skip_test_ 'expensive: disabled by default
244 This test is relatively expensive, so it is disabled by default.
245 To run it anyway, rerun make check with the RUN_EXPENSIVE_TESTS
246 environment variable set to yes. E.g.,
248 env RUN_EXPENSIVE_TESTS=yes make check
255 uid_is_privileged_ || skip_test_ "must be run as root"
256 NON_ROOT_USERNAME=${NON_ROOT_USERNAME=nobody}
257 NON_ROOT_GROUP=${NON_ROOT_GROUP=$(id -g $NON_ROOT_USERNAME)}
260 skip_if_root_() { uid_is_privileged_ && skip_test_ "must be run as non-root"; }
261 error_() { echo "$0: $@" 1>&2; Exit 1; }
262 framework_failure() { error_ 'failure in testing framework'; }
264 # Set `groups' to a space-separated list of at least two groups
265 # of which the user is a member.
266 require_membership_in_two_groups_()
268 test $# = 0 || framework_failure
270 groups=${COREUTILS_GROUPS-`(id -G || /usr/xpg4/bin/id -G) 2>/dev/null`}
273 *) skip_test_ 'requires membership in two groups
274 this test requires that you be a member of more than one group,
275 but running `id -G'\'' either failed or found just one. If you really
276 are a member of at least two groups, then rerun this test with
277 COREUTILS_GROUPS set in your environment to the space-separated list
278 of group names or numbers. E.g.,
280 env COREUTILS_GROUPS='users cdrom' make check
287 # Is /proc/$PID/status supported?
288 require_proc_pid_status_()
293 grep '^State:[ ]*[S]' /proc/$pid/status > /dev/null 2>&1 ||
294 skip_test_ "/proc/$pid/status: missing or 'different'"
298 # Return nonzero if the specified path is on a file system for
299 # which FIEMAP support exists. Note some file systems (like ext3)
300 # only support FIEMAP for files, not directories.
303 if ! python < /dev/null; then
304 echo 'fiemap_capable_: python missing: assuming not fiemap capable' 1>&2
307 python $abs_srcdir/fiemap-capable "$@"
310 # Does the current (working-dir) file system support sparse files?
311 require_sparse_support_()
313 test $# = 0 || framework_failure
314 # Test whether we can create a sparse file.
315 # For example, on Darwin6.5 with a file system of type hfs, it's not possible.
316 # NTFS requires 128K before a hole appears in a sparse file.
318 dd bs=1 seek=128K of=$t < /dev/null 2> /dev/null
322 if test $kb_size -ge 128; then
323 skip_test_ 'this file system does not support sparse files'
329 test $# = 1 || framework_failure
330 if ! mkfifo "$1"; then
331 # Make an exception of this case -- usually we interpret framework-creation
332 # failure as a test failure. However, in this case, when running on a SunOS
333 # system using a disk NFS mounted from OpenBSD, the above fails like this:
334 # mkfifo: cannot make fifo `fifo-10558': Not owner
335 skip_test_ 'NOTICE: unable to create test prerequisites'
339 # Disable the current test if the working directory seems to have
340 # the setgid bit set.
343 setgid_tmpdir=setgid-$$
344 (umask 77; mkdir $setgid_tmpdir)
345 perms=$(stat --printf %A $setgid_tmpdir)
349 drwxr-xr-x);; # Windows98 + DJGPP 2.03
350 *) skip_test_ 'this directory has the setgid bit set';;
354 skip_if_mcstransd_is_running_()
356 test $# = 0 || framework_failure
358 # When mcstransd is running, you'll see only the 3-component
359 # version of file-system context strings. Detect that,
360 # and if it's running, skip this test.
361 __ctx=$(stat --printf='%C\n' .) || framework_failure
363 *:*:*:*) ;; # four components is ok
364 *) # anything else probably means mcstransd is running
365 skip_test_ "unexpected context '$__ctx'; turn off mcstransd" ;;
369 # Skip the current test if umask doesn't work as usual.
370 # This test should be run in the temporary directory that ends
371 # up being removed via the trap commands.
372 working_umask_or_skip_()
377 perms=`ls -l file1 file2 | sed 's/ .*//' | uniq`
382 '*) skip_test_ 'your build directory has unusual umask semantics'
386 # Retry a function requiring a sufficient delay to _pass_
387 # using a truncated exponential backoff method.
388 # Example: retry_delay_ dd_reblock_1 .1 6
389 # This example will call the dd_reblock_1 function with
390 # an initial delay of .1 second and call it at most 6 times
391 # with a max delay of 3.2s (doubled each time), or a total of 6.3s
392 # Note ensure you do _not_ quote the parameter to GNU sleep in
393 # your function, as it may contain separate values that `sleep`
394 # needs to accumulate.
402 local num_sleeps=$attempt
404 while test $attempt -le $max_n_tries; do
405 local delay=$($AWK -v n=$num_sleeps -v s="$init_delay" \
406 'BEGIN { print s * n }')
407 "$test_func" "$delay" && { time_fail=0; break; } || time_fail=1
408 attempt=$(expr $attempt + 1)
409 num_sleeps=$(expr $num_sleeps '*' 2)
411 test "$time_fail" = 0
414 # Call this with a list of programs under test immediately after
418 if test "$VERBOSE" = yes; then