1 # This file is sourced by init.sh, *before* its initialization.
3 # Copyright (C) 2010-2015 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 # This goes hand in hand with the "exec 9>&2;" in tests/Makefile.am's
19 # TESTS_ENVIRONMENT definition.
22 # Having an unsearchable directory in PATH causes execve to fail with EACCES
23 # when applied to an unresolvable program name, contrary to the desired ENOENT.
24 # Avoid the problem by rewriting PATH to exclude unsearchable directories.
25 # Also, if PATH lacks /sbin and/or /usr/sbin, append it/them.
28 # FIXME: remove double quotes around $IFS when all tests use init.sh.
29 # They constitute a work-around for a bug in FreeBSD 8.1's /bin/sh.
30 local saved_IFS="$IFS"
39 test -z "$d" && d1=. || d1=$d
40 if ls -d "$d1/." > /dev/null 2>&1; then
41 new_path="$new_path$colon$d"
46 for d in /sbin /usr/sbin ; do
49 *) new_path="$new_path:$d" ;;
60 test "$INT_MAX" || fatal_ "running getlimits"
63 require_no_default_acl_()
65 if getfacl --version < /dev/null > /dev/null 2>&1; then
66 getfacl "$1" | grep '^default:' && skip_ 'Default ACL detected'
68 ls -ld "$1" | grep '.........+' && skip_ 'ACL detected'
74 getfacl --version < /dev/null > /dev/null 2>&1 \
75 && setfacl --version < /dev/null > /dev/null 2>&1 \
76 || skip_ "This test requires getfacl and setfacl."
78 id -u bin > /dev/null 2>&1 \
79 || skip_ "This test requires a local user named bin."
84 test $# = 1 || framework_failure_
85 df --local "$1" >/dev/null 2>&1
90 local mount_list_fail='cannot read table of mounted file systems'
91 df --local 2>&1 | grep -F "$mount_list_fail" >/dev/null &&
92 skip_ "$mount_list_fail"
99 skip_ "This test must be run on a local file system."
104 # When in a chroot of an SELinux-enabled system, but with a mock-simulated
105 # SELinux-*disabled* system, recognize that SELinux is disabled system wide:
106 grep 'selinuxfs$' /proc/filesystems > /dev/null \
107 || skip_ "this system lacks SELinux support"
109 # Independent of whether SELinux is enabled system-wide,
110 # the current file system may lack SELinux support.
111 # Also the current build may have SELinux support disabled.
114 test -z "$CONFIG_HEADER" \
115 && framework_failure_ 'CONFIG_HEADER not defined'
116 grep '^#define HAVE_SELINUX_SELINUX_H 1' "$CONFIG_HEADER" > /dev/null \
117 && selinux_missing_="(file) system" || selinux_missing_="build"
118 skip_ "this $selinux_missing_ lacks SELinux support"
123 # Skip this test if we're not in SELinux "enforcing" mode.
124 require_selinux_enforcing_()
127 test "$(getenforce)" = Enforcing \
128 || skip_ "This test is useful only with SELinux in Enforcing mode."
133 grep 'smackfs$' /proc/filesystems > /dev/null \
134 || skip_ "this system lacks SMACK support"
136 test "$(ls -Zd .)" != '? .' \
137 || skip_ "this file system lacks SMACK support"
140 require_openat_support_()
142 # Skip this test if your system has neither the openat-style functions
143 # nor /proc/self/fd support with which to emulate them.
145 test -z "$CONFIG_HEADER" \
146 && framework_failure_ 'CONFIG_HEADER not defined'
149 grep '^#define HAVE_OPENAT' "$CONFIG_HEADER" > /dev/null && _skip=no
150 test -d /proc/self/fd && _skip=no
151 if test $_skip = yes; then
152 skip_ 'this system lacks openat support'
156 # Determine the minimum required VM limit to run the given command.
157 # Output that value to stdout ... to be used by the caller.
158 # Return 0 in case of success, and a non-Zero value otherwise.
164 # Increase result by this amount to avoid alignment issues
165 page_size=$(getconf PAGESIZE || echo 4096)
166 page_size=$(($page_size / 1024))
168 for v in $( seq 5000 5000 50000 ); do
169 if ( ulimit -v $v && "$@" ) >/dev/null; then
172 for v in $( seq $(($prev_v-1000)) -1000 1000 ); do
173 ( ulimit -v $v && "$@" ) >/dev/null || \
175 ret_v=$((prev_v + $page_size))
183 # The above did not find a working limit. Echo a very small number - just
184 # in case the caller does not handle the non-Zero return value.
188 require_readable_root_()
190 test -r / || skip_ "/ is not readable"
193 # Skip the current test if strace is not available or doesn't work
194 # with the named syscall. Usage: require_strace_ unlink
197 test $# = 1 || framework_failure_
199 strace -V < /dev/null > /dev/null 2>&1 ||
200 skip_ 'no strace program'
202 strace -qe "$1" echo > /dev/null 2>&1 ||
203 skip_ 'strace -qe "'"$1"'" does not work'
205 # On some linux/sparc64 systems, strace works fine on 32-bit executables,
206 # but prints only one line of output for every 64-bit executable.
207 strace -o log-help ls --help >/dev/null || framework_failure_
208 n_lines_help=$(wc -l < log-help)
210 if test $n_lines_help = 0 || test $n_lines_help = 1; then
211 skip_ 'strace produces no more than one line of output'
215 # Skip the current test if valgrind doesn't work,
216 # which could happen if not installed,
217 # or hasn't support for the built architecture,
218 # or hasn't appropriate error suppressions installed etc.
221 valgrind --error-exitcode=1 true 2>/dev/null ||
222 skip_ "requires a working valgrind"
225 # Skip the current test if setfacl doesn't work on the current file system,
226 # which could happen if not installed, or if ACLs are not supported by the
227 # kernel or the file system, or are turned off via mount options.
229 # Work around the following two issues:
231 # 1) setfacl maps ACLs into file permission bits if on "noacl" file systems.
233 # On file systems which do not support ACLs (e.g. ext4 mounted with -o noacl),
234 # setfacl operates on the regular file permission bits, and only fails if the
235 # given ACL spec does not fit into there. Thus, to test if ACLs really work
236 # on the current file system, pass an ACL spec which can't be mapped that way.
237 # "Default" ACLs (-d) seem to fulfill this requirement.
239 # 2) setfacl only invokes the underlying system call if the ACL would change.
241 # If the given ACL spec would not change the ACLs on the file, then setfacl
242 # does not invoke the underlying system call - setxattr(). Therefore, to test
243 # if setting ACLs really works on the current file system, call setfacl twice
244 # with conflictive ACL specs.
247 local d='acltestdir_'
248 mkdir $d || framework_failure_
251 setfacl -d -m user::r-x $d \
252 && setfacl -d -m user::rwx $d \
254 rm -rf $d || framework_failure_
256 || skip_ "setfacl does not work on the current file system"
259 # Require a controlling input 'terminal'.
260 require_controlling_input_terminal_()
262 tty -s || have_input_tty=no
263 test -t 0 || have_input_tty=no
264 if test "$have_input_tty" = no; then
265 skip_ 'requires controlling input terminal
266 This test must have a controlling input "terminal", so it may not be
267 run via "batch", "at", or "ssh". On some systems, it may not even be
268 run in the background.'
276 case " $built_programs " in
278 *) echo "$i: not built" 1>&2; skip_=yes ;;
282 test $skip_ = yes && skip_ "required program(s) not built"
285 require_file_system_bytes_free_()
288 local expr=$(stat -f --printf "$req / %S <= %a" .)
289 $AWK "BEGIN{ exit !($expr) }" \
290 || skip_ "this test needs at least $req bytes of free space"
295 # Make sure id -u succeeds.
297 || { echo "$0: cannot run 'id -u'" 1>&2; return 1; }
299 # Make sure it gives valid output.
303 echo "$0: invalid output ('$my_uid') from 'id -u'" 1>&2
309 get_process_status_()
311 sed -n '/^State:[ ]*\([[:alpha:]]\).*/s//\1/p' /proc/$1/status
314 # Convert an ls-style permission string, like drwxr----x and -rw-r-x-wx
315 # to the equivalent chmod --mode (-m) argument, (=,u=rwx,g=r,o=x and
316 # =,u=rw,g=rx,o=wx). Ignore ACLs.
321 *) echo "$0: wrong number of arguments" 1>&2
322 echo "Usage: $0 ls-style-mode-string" 1>&2
327 [ld-][rwx-][rwx-][rwxsS-][rwx-][rwx-][rwxsS-][rwx-][rwx-][rwxtT-]) ;;
328 [ld-][rwx-][rwx-][rwxsS-][rwx-][rwx-][rwxsS-][rwx-][rwx-][rwxtT-][+.]) ;;
329 *) echo "$0: invalid mode string: $rwx" 1>&2; return;;
332 # Perform these conversions:
337 # The 'T' and 't' ones are only valid for 'other'.
338 s='s/S/@/;s/s/x@/;s/@/s/'
339 t='s/T/@/;s/t/x@/;s/@/t/'
341 u=$(echo $rwx|sed 's/^.\(...\).*/,u=\1/;s/-//g;s/^,u=$//;'$s)
342 g=$(echo $rwx|sed 's/^....\(...\).*/,g=\1/;s/-//g;s/^,g=$//;'$s)
343 o=$(echo $rwx|sed 's/^.......\(...\).*/,o=\1/;s/-//g;s/^,o=$//;'$s';'$t)
347 # Set the global variable stty_reversible_ to a space-separated list of the
348 # reversible settings from stty.c. stty_reversible_ also starts and ends
350 stty_reversible_init_()
352 # Pad start with one space for the first option to match in query function.
353 stty_reversible_=' '$(perl -lne '/^ *{"(.*?)",.*\bREV\b/ and print $1' \
354 "$abs_top_srcdir"/src/stty.c | tr '\n' ' ')
355 # Ensure that there are at least 62, i.e., so we're alerted if
356 # reformatting the source empties the list.
357 test 62 -le $(echo "$stty_reversible_"|wc -w) \
358 || framework_failure_ "too few reversible settings"
361 # Test whether $1 is one of stty's reversible options.
362 stty_reversible_query_()
364 case $stty_reversible_ in
366 framework_failure_ "stty_reversible_init_() not called?";;
377 root) skip_ must be run as root ;;
378 non-root) skip_ must be run as non-root ;;
385 if test "$RUN_VERY_EXPENSIVE_TESTS" != yes; then
386 skip_ 'very expensive: disabled by default
387 This test is very expensive, so it is disabled by default.
388 To run it anyway, rerun make check with the RUN_VERY_EXPENSIVE_TESTS
389 environment variable set to yes. E.g.,
391 env RUN_VERY_EXPENSIVE_TESTS=yes make check
393 or use the shortcut target of the toplevel Makefile,
395 make check-very-expensive
402 if test "$RUN_EXPENSIVE_TESTS" != yes; then
403 skip_ 'expensive: disabled by default
404 This test is relatively expensive, so it is disabled by default.
405 To run it anyway, rerun make check with the RUN_EXPENSIVE_TESTS
406 environment variable set to yes. E.g.,
408 env RUN_EXPENSIVE_TESTS=yes make check
410 or use the shortcut target of the toplevel Makefile,
417 # Test whether we can run our just-built root owned rm,
418 # i.e., that $NON_ROOT_USERNAME has access to the build directory.
421 require_built_ chroot
424 chroot --skip-chdir --user=$NON_ROOT_USERNAME / env PATH="$PATH" \
428 case ":$rm_version:" in
429 :$PACKAGE_VERSION:) ;;
436 uid_is_privileged_ || skip_ "must be run as root"
437 NON_ROOT_USERNAME=${NON_ROOT_USERNAME=nobody}
438 NON_ROOT_GID=${NON_ROOT_GID=$(id -g $NON_ROOT_USERNAME)}
440 # When the current test invokes chroot, call nonroot_has_perm_
441 # to check for a common problem.
442 grep '^[ ]*chroot' "../$0" \
443 && { nonroot_has_perm_ \
444 || skip_ "user $NON_ROOT_USERNAME lacks execute permissions"; }
447 skip_if_root_() { uid_is_privileged_ && skip_ "must be run as non-root"; }
449 # Set 'groups' to a space-separated list of at least two groups
450 # of which the user is a member.
451 require_membership_in_two_groups_()
453 test $# = 0 || framework_failure_
455 groups=${COREUTILS_GROUPS-$( (id -G || /usr/xpg4/bin/id -G) 2>/dev/null)}
458 *) skip_ 'requires membership in two groups
459 this test requires that you be a member of more than one group,
460 but running 'id -G'\'' either failed or found just one. If you really
461 are a member of at least two groups, then rerun this test with
462 COREUTILS_GROUPS set in your environment to the space-separated list
463 of group names or numbers. E.g.,
465 env COREUTILS_GROUPS='\''users cdrom'\'' make check
472 # Is /proc/$PID/status supported?
473 require_proc_pid_status_()
478 grep '^State:[ ]*[S]' /proc/$pid/status > /dev/null 2>&1 ||
479 skip_ "/proc/$pid/status: missing or 'different'"
483 # Does trap support signal names?
484 # Old versions of ash did not.
485 require_trap_signame_()
487 (trap '' CHLD) || skip_ 'requires trap with signal name support'
490 # Does kill support sending signal to whole group?
491 # dash 0.5.8 at least does not.
492 require_kill_group_()
494 kill -0 -- -1 || skip_ 'requires kill with group signalling support'
497 # Return nonzero if the specified path is on a file system for
498 # which FIEMAP support exists. Note some file systems (like ext3 and btrfs)
499 # only support FIEMAP for files, not directories.
502 if ! python < /dev/null; then
503 warn_ 'fiemap_capable_: python missing: assuming not fiemap capable'
506 python "$abs_srcdir"/tests/fiemap-capable "$@"
509 # Skip the current test if "." lacks d_type support.
510 require_dirent_d_type_()
513 || skip_ python missing: assuming no d_type support
515 python "$abs_srcdir"/tests/d_type-check \
516 || skip_ requires d_type support
519 # Skip the current test if we lack Perl.
523 $PERL -e 'use warnings' > /dev/null 2>&1 \
524 || skip_ 'configure did not find a usable version of Perl'
527 # Does the current (working-dir) file system support sparse files?
528 require_sparse_support_()
530 test $# = 0 || framework_failure_
531 # Test whether we can create a sparse file.
532 # For example, on Darwin6.5 with a file system of type hfs, it's not possible.
533 # NTFS requires 128K before a hole appears in a sparse file.
535 dd bs=1 seek=128K of=$t < /dev/null 2> /dev/null
539 if test $kb_size -ge 128; then
540 skip_ 'this file system does not support sparse files'
544 # Compile a shared lib using the GCC options for doing so.
545 # Pass input and output file as parameters respectively.
546 # Any other optional parmeters are passed to $CC.
553 $CC -Wall -shared --std=gnu99 -fPIC -O2 $* "$in" -o "$out" -ldl
556 # There are a myriad of ways to build shared libs,
557 # so we only consider running tests requiring shared libs,
558 # on platforms that support building them as follows.
559 require_gcc_shared_()
561 gcc_shared_ '-' 'd.so' -xc < /dev/null 2>&1 \
562 || skip_ '$CC -shared ... failed to build a shared lib'
568 test $# = 1 || framework_failure_
569 if ! mkfifo "$1"; then
570 # Make an exception of this case -- usually we interpret framework-creation
571 # failure as a test failure. However, in this case, when running on a SunOS
572 # system using a disk NFS mounted from OpenBSD, the above fails like this:
573 # mkfifo: cannot make fifo 'fifo-10558': Not owner
574 skip_ 'unable to create a fifo'
578 # Disable the current test if the working directory seems to have
579 # the setgid bit set.
582 setgid_tmpdir=setgid-$$
583 (umask 77; mkdir $setgid_tmpdir)
584 perms=$(stat --printf %A $setgid_tmpdir)
588 drwxr-xr-x);; # Windows98 + DJGPP 2.03
589 *) skip_ 'this directory has the setgid bit set';;
593 # Skip if files are created with a different group to the current user
594 # This can happen due to a setgid dir, or by some other mechanism on OS X:
595 # http://unix.stackexchange.com/q/63865
596 # http://bugs.gnu.org/14024#41
597 skip_if_nondefault_group_()
600 gen_ug=$(stat -c '%u:%g' grp.$$)
602 test "$gen_ug" = "$(id -ru):$(id -rg)" ||
603 skip_ 'Files are created with a different gid'
606 skip_if_mcstransd_is_running_()
608 test $# = 0 || framework_failure_
610 # When mcstransd is running, you'll see only the 3-component
611 # version of file-system context strings. Detect that,
612 # and if it's running, skip this test.
613 __ctx=$(stat --printf='%C\n' .) || framework_failure_
615 *:*:*:*) ;; # four components is ok
616 *) # anything else probably means mcstransd is running
617 skip_ "unexpected context '$__ctx'; turn off mcstransd" ;;
621 # Skip the current test if umask doesn't work as usual.
622 # This test should be run in the temporary directory that ends
623 # up being removed via the trap commands.
624 working_umask_or_skip_()
629 perms=$(ls -l file1 file2 | sed 's/ .*//' | uniq)
634 '*) skip_ 'your build directory has unusual umask semantics'
638 # Retry a function requiring a sufficient delay to _pass_
639 # using a truncated exponential backoff method.
640 # Example: retry_delay_ dd_reblock_1 .1 6
641 # This example will call the dd_reblock_1 function with
642 # an initial delay of .1 second and call it at most 6 times
643 # with a max delay of 3.2s (doubled each time), or a total of 6.3s
644 # Note ensure you do _not_ quote the parameter to GNU sleep in
645 # your function, as it may contain separate values that sleep
646 # needs to accumulate.
647 # Further function arguments will be forwarded to the test function.
656 local num_sleeps=$attempt
658 while test $attempt -le $max_n_tries; do
659 local delay=$($AWK -v n=$num_sleeps -v s="$init_delay" \
660 'BEGIN { print s * n }')
661 "$test_func" "$delay" "$@" && { time_fail=0; break; } || time_fail=1
662 attempt=$(expr $attempt + 1)
663 num_sleeps=$(expr $num_sleeps '*' 2)
665 test "$time_fail" = 0
668 # Call this with a list of programs under test immediately after
673 if test "$VERBOSE" = yes; then
681 # Are we running on GNU/Hurd?
684 test "$(uname)" = GNU \
685 || skip_ 'not running on GNU/Hurd'