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"
154 require_file_system_bytes_free_()
157 local expr=$(stat -f --printf "$req / %S <= %a" .)
158 awk "BEGIN{ exit !($expr) }" \
159 || skip_test_ "this test needs at least $req bytes of free space"
164 # Make sure id -u succeeds.
166 || { echo "$0: cannot run \`id -u'" 1>&2; return 1; }
168 # Make sure it gives valid output.
172 echo "$0: invalid output (\`$my_uid') from \`id -u'" 1>&2
178 get_process_status_()
180 sed -n '/^State:[ ]*\([[:alpha:]]\).*/s//\1/p' /proc/$1/status
183 # Convert an ls-style permission string, like drwxr----x and -rw-r-x-wx
184 # to the equivalent chmod --mode (-m) argument, (=,u=rwx,g=r,o=x and
185 # =,u=rw,g=rx,o=wx). Ignore ACLs.
190 *) echo "$0: wrong number of arguments" 1>&2
191 echo "Usage: $0 ls-style-mode-string" 1>&2
196 [ld-][rwx-][rwx-][rwxsS-][rwx-][rwx-][rwxsS-][rwx-][rwx-][rwxtT-]) ;;
197 [ld-][rwx-][rwx-][rwxsS-][rwx-][rwx-][rwxsS-][rwx-][rwx-][rwxtT-][+.]) ;;
198 *) echo "$0: invalid mode string: $rwx" 1>&2; return;;
201 # Perform these conversions:
206 # The `T' and `t' ones are only valid for `other'.
207 s='s/S/@/;s/s/x@/;s/@/s/'
208 t='s/T/@/;s/t/x@/;s/@/t/'
210 u=`echo $rwx|sed 's/^.\(...\).*/,u=\1/;s/-//g;s/^,u=$//;'$s`
211 g=`echo $rwx|sed 's/^....\(...\).*/,g=\1/;s/-//g;s/^,g=$//;'$s`
212 o=`echo $rwx|sed 's/^.......\(...\).*/,o=\1/;s/-//g;s/^,o=$//;'$s';'$t`
219 root) skip_test_ must be run as root ;;
220 non-root) skip_test_ must be run as non-root ;;
227 # When in a chroot of an SELinux-enabled system, but with a mock-simulated
228 # SELinux-*disabled* system, recognize that SELinux is disabled system wide:
229 grep 'selinuxfs$' /proc/filesystems > /dev/null \
230 || skip_test_ "this system lacks SELinux support"
232 # Independent of whether SELinux is enabled system-wide,
233 # the current file system may lack SELinux support.
236 skip_test_ "this system (or maybe just" \
237 "the current file system) lacks SELinux support"
244 if test "$RUN_VERY_EXPENSIVE_TESTS" != yes; then
245 skip_test_ 'very expensive: disabled by default
246 This test is very expensive, so it is disabled by default.
247 To run it anyway, rerun make check with the RUN_VERY_EXPENSIVE_TESTS
248 environment variable set to yes. E.g.,
250 env RUN_VERY_EXPENSIVE_TESTS=yes make check
257 if test "$RUN_EXPENSIVE_TESTS" != yes; then
258 skip_test_ 'expensive: disabled by default
259 This test is relatively expensive, so it is disabled by default.
260 To run it anyway, rerun make check with the RUN_EXPENSIVE_TESTS
261 environment variable set to yes. E.g.,
263 env RUN_EXPENSIVE_TESTS=yes make check
270 uid_is_privileged_ || skip_test_ "must be run as root"
271 NON_ROOT_USERNAME=${NON_ROOT_USERNAME=nobody}
272 NON_ROOT_GROUP=${NON_ROOT_GROUP=$(id -g $NON_ROOT_USERNAME)}
275 skip_if_root_() { uid_is_privileged_ && skip_test_ "must be run as non-root"; }
276 error_() { echo "$0: $@" 1>&2; Exit 1; }
277 framework_failure() { error_ 'failure in testing framework'; }
279 # Set `groups' to a space-separated list of at least two groups
280 # of which the user is a member.
281 require_membership_in_two_groups_()
283 test $# = 0 || framework_failure
285 groups=${COREUTILS_GROUPS-`(id -G || /usr/xpg4/bin/id -G) 2>/dev/null`}
288 *) skip_test_ 'requires membership in two groups
289 this test requires that you be a member of more than one group,
290 but running `id -G'\'' either failed or found just one. If you really
291 are a member of at least two groups, then rerun this test with
292 COREUTILS_GROUPS set in your environment to the space-separated list
293 of group names or numbers. E.g.,
295 env COREUTILS_GROUPS='users cdrom' make check
302 # Is /proc/$PID/status supported?
303 require_proc_pid_status_()
308 grep '^State:[ ]*[S]' /proc/$pid/status > /dev/null 2>&1 ||
309 skip_test_ "/proc/$pid/status: missing or 'different'"
313 # Return nonzero if the specified path is on a file system for
314 # which FIEMAP support exists. Note some file systems (like ext3 and btrfs)
315 # only support FIEMAP for files, not directories.
318 if ! python < /dev/null; then
319 warn_ 'fiemap_capable_: python missing: assuming not fiemap capable'
322 python $abs_srcdir/fiemap-capable "$@"
325 # Skip the current test if "." lacks d_type support.
326 require_dirent_d_type_()
329 || skip_test_ python missing: assuming no d_type support
331 python $abs_srcdir/d_type-check \
332 || skip_test_ requires d_type support
335 # Does the current (working-dir) file system support sparse files?
336 require_sparse_support_()
338 test $# = 0 || framework_failure
339 # Test whether we can create a sparse file.
340 # For example, on Darwin6.5 with a file system of type hfs, it's not possible.
341 # NTFS requires 128K before a hole appears in a sparse file.
343 dd bs=1 seek=128K of=$t < /dev/null 2> /dev/null
347 if test $kb_size -ge 128; then
348 skip_test_ 'this file system does not support sparse files'
354 test $# = 1 || framework_failure
355 if ! mkfifo "$1"; then
356 # Make an exception of this case -- usually we interpret framework-creation
357 # failure as a test failure. However, in this case, when running on a SunOS
358 # system using a disk NFS mounted from OpenBSD, the above fails like this:
359 # mkfifo: cannot make fifo `fifo-10558': Not owner
360 skip_test_ 'NOTICE: unable to create test prerequisites'
364 # Disable the current test if the working directory seems to have
365 # the setgid bit set.
368 setgid_tmpdir=setgid-$$
369 (umask 77; mkdir $setgid_tmpdir)
370 perms=$(stat --printf %A $setgid_tmpdir)
374 drwxr-xr-x);; # Windows98 + DJGPP 2.03
375 *) skip_test_ 'this directory has the setgid bit set';;
379 skip_if_mcstransd_is_running_()
381 test $# = 0 || framework_failure
383 # When mcstransd is running, you'll see only the 3-component
384 # version of file-system context strings. Detect that,
385 # and if it's running, skip this test.
386 __ctx=$(stat --printf='%C\n' .) || framework_failure
388 *:*:*:*) ;; # four components is ok
389 *) # anything else probably means mcstransd is running
390 skip_test_ "unexpected context '$__ctx'; turn off mcstransd" ;;
394 # Skip the current test if umask doesn't work as usual.
395 # This test should be run in the temporary directory that ends
396 # up being removed via the trap commands.
397 working_umask_or_skip_()
402 perms=`ls -l file1 file2 | sed 's/ .*//' | uniq`
407 '*) skip_test_ 'your build directory has unusual umask semantics'
411 # Retry a function requiring a sufficient delay to _pass_
412 # using a truncated exponential backoff method.
413 # Example: retry_delay_ dd_reblock_1 .1 6
414 # This example will call the dd_reblock_1 function with
415 # an initial delay of .1 second and call it at most 6 times
416 # with a max delay of 3.2s (doubled each time), or a total of 6.3s
417 # Note ensure you do _not_ quote the parameter to GNU sleep in
418 # your function, as it may contain separate values that `sleep`
419 # needs to accumulate.
427 local num_sleeps=$attempt
429 while test $attempt -le $max_n_tries; do
430 local delay=$($AWK -v n=$num_sleeps -v s="$init_delay" \
431 'BEGIN { print s * n }')
432 "$test_func" "$delay" && { time_fail=0; break; } || time_fail=1
433 attempt=$(expr $attempt + 1)
434 num_sleeps=$(expr $num_sleeps '*' 2)
436 test "$time_fail" = 0
439 # Call this with a list of programs under test immediately after
443 if test "$VERBOSE" = yes; then