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.
24 test -z "$d" && d1=. || d1=$d
25 if ls -d "$d1/." > /dev/null 2>&1; then
26 new_path="$new_path$colon$d"
37 echo "$0: skipping test: $@" | head -1 1>&9
38 echo "$0: skipping test: $@" 1>&2
46 error_ "Error running getlimits"
51 getfacl --version < /dev/null > /dev/null 2>&1 \
52 && setfacl --version < /dev/null > /dev/null 2>&1 \
53 || skip_test_ "This test requires getfacl and setfacl."
55 id -u bin > /dev/null 2>&1 \
56 || skip_test_ "This test requires a local user named bin."
59 # Skip this test if we're not in SELinux "enforcing" mode.
60 require_selinux_enforcing_()
62 test "$(getenforce)" = Enforcing \
63 || skip_test_ "This test is useful only with SELinux in Enforcing mode."
66 require_openat_support_()
68 # Skip this test if your system has neither the openat-style functions
69 # nor /proc/self/fd support with which to emulate them.
70 test -z "$CONFIG_HEADER" \
71 && skip_test_ 'internal error: CONFIG_HEADER not defined'
74 grep '^#define HAVE_OPENAT' "$CONFIG_HEADER" > /dev/null && _skip=no
75 test -d /proc/self/fd && _skip=no
76 if test $_skip = yes; then
77 skip_test_ 'this system lacks openat support'
84 # Expect to be able to exec a program in 10MB of virtual memory,
85 # but not in 20KB. I chose "date". It must not be a shell built-in
86 # function, so you can't use echo, printf, true, etc.
87 # Of course, in coreutils, I could use $top_builddir/src/true,
88 # but this should be able to work for other projects, too.
89 ( ulimit -v 10000; date ) > /dev/null 2>&1 || ulimit_works=no
90 ( ulimit -v 20; date ) > /dev/null 2>&1 && ulimit_works=no
92 test $ulimit_works = no \
93 && skip_test_ "this shell lacks ulimit support"
96 require_readable_root_()
98 test -r / || skip_test_ "/ is not readable"
101 # Skip the current test if strace is not available or doesn't work
102 # with the named syscall. Usage: require_strace_ unlink
105 test $# = 1 || framework_failure
107 strace -V < /dev/null > /dev/null 2>&1 ||
108 skip_test_ 'no strace program'
110 strace -qe "$1" echo > /dev/null 2>&1 ||
111 skip_test_ 'strace -qe "'"$1"'" does not work'
114 # Require a controlling input `terminal'.
115 require_controlling_input_terminal_()
117 tty -s || have_input_tty=no
118 test -t 0 || have_input_tty=no
119 if test "$have_input_tty" = no; then
120 skip_test_ 'requires controlling input terminal
121 This test must have a controlling input "terminal", so it may not be
122 run via "batch", "at", or "ssh". On some systems, it may not even be
123 run in the background.'
131 case " $built_programs " in
133 *) echo "$i: not built" 1>&2; skip_=yes ;;
137 test $skip_ = yes && skip_test_ "required program(s) not built"
142 # Make sure id -u succeeds.
144 || { echo "$0: cannot run \`id -u'" 1>&2; return 1; }
146 # Make sure it gives valid output.
150 echo "$0: invalid output (\`$my_uid') from \`id -u'" 1>&2
156 get_process_status_()
158 sed -n '/^State:[ ]*\([[:alpha:]]\).*/s//\1/p' /proc/$1/status
161 # Convert an ls-style permission string, like drwxr----x and -rw-r-x-wx
162 # to the equivalent chmod --mode (-m) argument, (=,u=rwx,g=r,o=x and
163 # =,u=rw,g=rx,o=wx). Ignore ACLs.
168 *) echo "$0: wrong number of arguments" 1>&2
169 echo "Usage: $0 ls-style-mode-string" 1>&2
174 [ld-][rwx-][rwx-][rwxsS-][rwx-][rwx-][rwxsS-][rwx-][rwx-][rwxtT-]) ;;
175 [ld-][rwx-][rwx-][rwxsS-][rwx-][rwx-][rwxsS-][rwx-][rwx-][rwxtT-][+.]) ;;
176 *) echo "$0: invalid mode string: $rwx" 1>&2; return;;
179 # Perform these conversions:
184 # The `T' and `t' ones are only valid for `other'.
185 s='s/S/@/;s/s/x@/;s/@/s/'
186 t='s/T/@/;s/t/x@/;s/@/t/'
188 u=`echo $rwx|sed 's/^.\(...\).*/,u=\1/;s/-//g;s/^,u=$//;'$s`
189 g=`echo $rwx|sed 's/^....\(...\).*/,g=\1/;s/-//g;s/^,g=$//;'$s`
190 o=`echo $rwx|sed 's/^.......\(...\).*/,o=\1/;s/-//g;s/^,o=$//;'$s';'$t`
197 root) skip_test_ must be run as root ;;
198 non-root) skip_test_ must be run as non-root ;;
207 skip_test_ "this system (or maybe just" \
208 "the current file system) lacks SELinux support"
215 if test "$RUN_VERY_EXPENSIVE_TESTS" != yes; then
216 skip_test_ 'very expensive: disabled by default
217 This test is very expensive, so it is disabled by default.
218 To run it anyway, rerun make check with the RUN_VERY_EXPENSIVE_TESTS
219 environment variable set to yes. E.g.,
221 env RUN_VERY_EXPENSIVE_TESTS=yes make check
228 if test "$RUN_EXPENSIVE_TESTS" != yes; then
229 skip_test_ 'expensive: disabled by default
230 This test is relatively expensive, so it is disabled by default.
231 To run it anyway, rerun make check with the RUN_EXPENSIVE_TESTS
232 environment variable set to yes. E.g.,
234 env RUN_EXPENSIVE_TESTS=yes make check
241 uid_is_privileged_ || skip_test_ "must be run as root"
242 NON_ROOT_USERNAME=${NON_ROOT_USERNAME=nobody}
243 NON_ROOT_GROUP=${NON_ROOT_GROUP=$(id -g $NON_ROOT_USERNAME)}
246 skip_if_root_() { uid_is_privileged_ && skip_test_ "must be run as non-root"; }
247 error_() { echo "$0: $@" 1>&2; Exit 1; }
248 framework_failure() { error_ 'failure in testing framework'; }
250 # Set `groups' to a space-separated list of at least two groups
251 # of which the user is a member.
252 require_membership_in_two_groups_()
254 test $# = 0 || framework_failure
256 groups=${COREUTILS_GROUPS-`(id -G || /usr/xpg4/bin/id -G) 2>/dev/null`}
259 *) skip_test_ 'requires membership in two groups
260 this test requires that you be a member of more than one group,
261 but running `id -G'\'' either failed or found just one. If you really
262 are a member of at least two groups, then rerun this test with
263 COREUTILS_GROUPS set in your environment to the space-separated list
264 of group names or numbers. E.g.,
266 env COREUTILS_GROUPS='users cdrom' make check
273 # Is /proc/$PID/status supported?
274 require_proc_pid_status_()
279 grep '^State:[ ]*[S]' /proc/$pid/status > /dev/null 2>&1 ||
280 skip_test_ "/proc/$pid/status: missing or 'different'"
284 # Does the current (working-dir) file system support sparse files?
285 require_sparse_support_()
287 test $# = 0 || framework_failure
288 # Test whether we can create a sparse file.
289 # For example, on Darwin6.5 with a file system of type hfs, it's not possible.
290 # NTFS requires 128K before a hole appears in a sparse file.
292 dd bs=1 seek=128K of=$t < /dev/null 2> /dev/null
296 if test $kb_size -ge 128; then
297 skip_test_ 'this file system does not support sparse files'
303 test $# = 1 || framework_failure
304 if ! mkfifo "$1"; then
305 # Make an exception of this case -- usually we interpret framework-creation
306 # failure as a test failure. However, in this case, when running on a SunOS
307 # system using a disk NFS mounted from OpenBSD, the above fails like this:
308 # mkfifo: cannot make fifo `fifo-10558': Not owner
309 skip_test_ 'NOTICE: unable to create test prerequisites'
313 # Disable the current test if the working directory seems to have
314 # the setgid bit set.
317 setgid_tmpdir=setgid-$$
318 (umask 77; mkdir $setgid_tmpdir)
319 perms=$(stat --printf %A $setgid_tmpdir)
323 drwxr-xr-x);; # Windows98 + DJGPP 2.03
324 *) skip_test_ 'this directory has the setgid bit set';;
328 skip_if_mcstransd_is_running_()
330 test $# = 0 || framework_failure
332 # When mcstransd is running, you'll see only the 3-component
333 # version of file-system context strings. Detect that,
334 # and if it's running, skip this test.
335 __ctx=$(stat --printf='%C\n' .) || framework_failure
337 *:*:*:*) ;; # four components is ok
338 *) # anything else probably means mcstransd is running
339 skip_test_ "unexpected context '$__ctx'; turn off mcstransd" ;;
343 # Skip the current test if umask doesn't work as usual.
344 # This test should be run in the temporary directory that ends
345 # up being removed via the trap commands.
346 working_umask_or_skip_()
351 perms=`ls -l file1 file2 | sed 's/ .*//' | uniq`
356 '*) skip_test_ 'your build directory has unusual umask semantics'
360 # Continually retry a function requiring a sufficient delay to _pass_
361 # Example: retry_delay dd_reblock_1 .1 6
362 # This example will call the dd_reblock_1 function with
363 # an initial delay of .1 second and call it at most 6 times
364 # with a max delay of 3.2s (doubled each time), or a total of 6.3s
365 # Note ensure you do _not_ quote the parameter to GNU sleep in
366 # your function, as it may contain separate values that `sleep`
367 # needs to accumulate.
375 local num_sleeps=$attempt
377 while test $attempt -le $max_n_tries; do
378 local delay=$($AWK -v n=$num_sleeps -v s="$init_delay" \
379 'BEGIN { for (i=0;i<n;i++) t = s" "t; print t }')
380 "$test_func" "$delay" && { time_fail=0; break; } || time_fail=1
381 attempt=$(expr $attempt + 1)
382 num_sleeps=$(expr $num_sleeps '*' 2)
384 test "$time_fail" = 0