tests: fix false failure with disabled SELinux support
[coreutils.git] / init.cfg
blob360d4da2bde0e1672868f7a5ed203b759b7149d4
1 # This file is sourced by init.sh, *before* its initialization.
3 # Copyright (C) 2010-2013 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.
20 stderr_fileno_=9
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.
26 sanitize_path_()
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"
31     IFS=:
32     set -- $PATH
33   IFS=$saved_IFS
35   local d d1
36   local colon=
37   local new_path=
38   for d in "$@"; do
39     test -z "$d" && d1=. || d1=$d
40     if ls -d "$d1/." > /dev/null 2>&1; then
41       new_path="$new_path$colon$d"
42       colon=':'
43     fi
44   done
46   for d in /sbin /usr/sbin ; do
47     case ":$new_path:" in
48       *:$d:*) ;;
49       *) new_path="$new_path:$d" ;;
50     esac
51   done
53   PATH=$new_path
54   export PATH
57 getlimits_()
59   eval $(getlimits)
60   test "$INT_MAX" || fatal_ "running getlimits"
63 require_acl_()
65   getfacl --version < /dev/null > /dev/null 2>&1 \
66     && setfacl --version < /dev/null > /dev/null 2>&1 \
67       || skip_ "This test requires getfacl and setfacl."
69   id -u bin > /dev/null 2>&1 \
70     || skip_ "This test requires a local user named bin."
73 is_local_dir_()
75   test $# = 1 || framework_failure_
76   df --local "$1" >/dev/null 2>&1
79 require_mount_list_()
81   local mount_list_fail='cannot read table of mounted file systems'
82   df 2>&1 | grep -F "$mount_list_fail" >/dev/null &&
83     skip_ "$mount_list_fail"
86 require_local_dir_()
88   require_mount_list_
89   is_local_dir_ . ||
90     skip_ "This test must be run on a local file system."
93 require_selinux_()
95   # When in a chroot of an SELinux-enabled system, but with a mock-simulated
96   # SELinux-*disabled* system, recognize that SELinux is disabled system wide:
97   grep 'selinuxfs$' /proc/filesystems > /dev/null \
98     || skip_ "this system lacks SELinux support"
100   # Independent of whether SELinux is enabled system-wide,
101   # the current file system may lack SELinux support.
102   # Also the current build may have SELinux support disabled.
103   case $(ls -Zd .) in
104     '? .'|'unlabeled .')
105       test -z "$CONFIG_HEADER" \
106         && framework_failure_ 'CONFIG_HEADER not defined'
107       grep '^#define HAVE_SELINUX_SELINUX_H 1' "$CONFIG_HEADER" > /dev/null \
108         && selinux_missing_="(file) system" || selinux_missing_="build"
109       skip_ "this $selinux_missing_ lacks SELinux support"
110     ;;
111   esac
114 # Skip this test if we're not in SELinux "enforcing" mode.
115 require_selinux_enforcing_()
117   require_selinux_
118   test "$(getenforce)" = Enforcing \
119     || skip_ "This test is useful only with SELinux in Enforcing mode."
122 require_openat_support_()
124   # Skip this test if your system has neither the openat-style functions
125   # nor /proc/self/fd support with which to emulate them.
127   test -z "$CONFIG_HEADER" \
128     && framework_failure_ 'CONFIG_HEADER not defined'
130   _skip=yes
131   grep '^#define HAVE_OPENAT' "$CONFIG_HEADER" > /dev/null && _skip=no
132   test -d /proc/self/fd && _skip=no
133   if test $_skip = yes; then
134     skip_ 'this system lacks openat support'
135   fi
138 require_ulimit_v_()
140   local ulimit_works=yes
141   # Expect to be able to exec a program in 10MiB of virtual memory,
142   # (10MiB is usually plenty, but valgrind-wrapped date requires 19000KiB,
143   # so allow more in that case)
144   # but not in 20KiB.  I chose "date".  It must not be a shell built-in
145   # function, so you can't use echo, printf, true, etc.
146   # Of course, in coreutils, I could use $top_builddir/src/true,
147   # but this should be able to work for other projects, too.
148   local vm
149   case $(printenv LD_PRELOAD) in */valgrind/*) vm=22000;; *) vm=10000;; esac
151   ( ulimit -v $vm; date ) > /dev/null 2>&1 || ulimit_works=no
152   ( ulimit -v 20;  date ) > /dev/null 2>&1 && ulimit_works=no
154   test $ulimit_works = no \
155     && skip_ "this shell lacks ulimit support"
158 require_readable_root_()
160   test -r / || skip_ "/ is not readable"
163 # Skip the current test if strace is not available or doesn't work
164 # with the named syscall.  Usage: require_strace_ unlink
165 require_strace_()
167   test $# = 1 || framework_failure_
169   strace -V < /dev/null > /dev/null 2>&1 ||
170     skip_ 'no strace program'
172   strace -qe "$1" echo > /dev/null 2>&1 ||
173     skip_ 'strace -qe "'"$1"'" does not work'
175   # On some linux/sparc64 systems, strace works fine on 32-bit executables,
176   # but prints only one line of output for every 64-bit executable.
177   strace -o log-help ls --help >/dev/null || framework_failure_
178   n_lines_help=$(wc -l < log-help)
179   rm -f log-help
180   if test $n_lines_help = 0 || test $n_lines_help = 1; then
181     skip_ 'strace produces no more than one line of output'
182   fi
185 # Skip the current test if valgrind doesn't work,
186 # which could happen if not installed,
187 # or hasn't support for the built architecture,
188 # or hasn't appropriate error suppressions installed etc.
189 require_valgrind_()
191   valgrind --error-exitcode=1 true 2>/dev/null ||
192     skip_ "requires a working valgrind"
195 require_setfacl_()
197   setfacl -m user::rwx . \
198     || skip_ "setfacl does not work on the current file system"
201 # Require a controlling input 'terminal'.
202 require_controlling_input_terminal_()
204   tty -s || have_input_tty=no
205   test -t 0 || have_input_tty=no
206   if test "$have_input_tty" = no; then
207     skip_ 'requires controlling input terminal
208 This test must have a controlling input "terminal", so it may not be
209 run via "batch", "at", or "ssh".  On some systems, it may not even be
210 run in the background.'
211   fi
214 require_built_()
216   skip_=no
217   for i in "$@"; do
218     case " $built_programs " in
219       *" $i "*) ;;
220       *) echo "$i: not built" 1>&2; skip_=yes ;;
221     esac
222   done
224   test $skip_ = yes && skip_ "required program(s) not built"
227 require_file_system_bytes_free_()
229   local req=$1
230   local expr=$(stat -f --printf "$req / %S <= %a" .)
231   $AWK "BEGIN{ exit !($expr) }" \
232     || skip_ "this test needs at least $req bytes of free space"
235 uid_is_privileged_()
237   # Make sure id -u succeeds.
238   my_uid=$(id -u) \
239     || { echo "$0: cannot run 'id -u'" 1>&2; return 1; }
241   # Make sure it gives valid output.
242   case $my_uid in
243     0) ;;
244     *[!0-9]*)
245       echo "$0: invalid output ('$my_uid') from 'id -u'" 1>&2
246       return 1 ;;
247     *) return 1 ;;
248   esac
251 get_process_status_()
253   sed -n '/^State:[      ]*\([[:alpha:]]\).*/s//\1/p' /proc/$1/status
256 # Convert an ls-style permission string, like drwxr----x and -rw-r-x-wx
257 # to the equivalent chmod --mode (-m) argument, (=,u=rwx,g=r,o=x and
258 # =,u=rw,g=rx,o=wx).  Ignore ACLs.
259 rwx_to_mode_()
261   case $# in
262     1) rwx=$1;;
263     *) echo "$0: wrong number of arguments" 1>&2
264       echo "Usage: $0 ls-style-mode-string" 1>&2
265       return;;
266   esac
268   case $rwx in
269     [ld-][rwx-][rwx-][rwxsS-][rwx-][rwx-][rwxsS-][rwx-][rwx-][rwxtT-]) ;;
270     [ld-][rwx-][rwx-][rwxsS-][rwx-][rwx-][rwxsS-][rwx-][rwx-][rwxtT-][+.]) ;;
271     *) echo "$0: invalid mode string: $rwx" 1>&2; return;;
272   esac
274   # Perform these conversions:
275   # S  s
276   # s  xs
277   # T  t
278   # t  xt
279   # The 'T' and 't' ones are only valid for 'other'.
280   s='s/S/@/;s/s/x@/;s/@/s/'
281   t='s/T/@/;s/t/x@/;s/@/t/'
283   u=$(echo $rwx|sed 's/^.\(...\).*/,u=\1/;s/-//g;s/^,u=$//;'$s)
284   g=$(echo $rwx|sed 's/^....\(...\).*/,g=\1/;s/-//g;s/^,g=$//;'$s)
285   o=$(echo $rwx|sed 's/^.......\(...\).*/,o=\1/;s/-//g;s/^,o=$//;'$s';'$t)
286   echo "=$u$g$o"
289 # Set the global variable stty_reversible_ to a space-separated list of the
290 # reversible settings from stty.c.  stty_reversible_ also starts and ends
291 # with a space.
292 stty_reversible_init_()
294   # Pad start with one space for the first option to match in query function.
295   stty_reversible_=' '$(perl -lne '/^ *{"(.*?)",.*\bREV\b/ and print $1' \
296     "$abs_top_srcdir"/src/stty.c | tr '\n' ' ')
297   # Ensure that there are at least 62, i.e., so we're alerted if
298   # reformatting the source empties the list.
299   test 62 -le $(echo "$stty_reversible_"|wc -w)  \
300     || framework_failure_ "too few reversible settings"
303 # Test whether $1 is one of stty's reversible options.
304 stty_reversible_query_()
306   case $stty_reversible_ in
307     '')
308       framework_failure_ "stty_reversible_init_() not called?";;
309     *" $1 "*)
310       return 0;;
311     *)
312       return 1;;
313   esac
316 skip_if_()
318   case $1 in
319     root) skip_ must be run as root ;;
320     non-root) skip_ must be run as non-root ;;
321     *) ;;  # FIXME?
322   esac
325 very_expensive_()
327   if test "$RUN_VERY_EXPENSIVE_TESTS" != yes; then
328     skip_ 'very expensive: disabled by default
329 This test is very expensive, so it is disabled by default.
330 To run it anyway, rerun make check with the RUN_VERY_EXPENSIVE_TESTS
331 environment variable set to yes.  E.g.,
333   env RUN_VERY_EXPENSIVE_TESTS=yes make check
335 or use the shortcut target of the toplevel Makefile,
337   make check-very-expensive
339   fi
342 expensive_()
344   if test "$RUN_EXPENSIVE_TESTS" != yes; then
345     skip_ 'expensive: disabled by default
346 This test is relatively expensive, so it is disabled by default.
347 To run it anyway, rerun make check with the RUN_EXPENSIVE_TESTS
348 environment variable set to yes.  E.g.,
350   env RUN_EXPENSIVE_TESTS=yes make check
352 or use the shortcut target of the toplevel Makefile,
354   make check-expensive
356   fi
359 # Test whether we can run our just-built rm setuidgid-to-root,
360 # i.e., that $NON_ROOT_USERNAME has access to the build directory.
361 setuidgid_has_perm_()
363   local rm_version=$(
364     setuidgid $NON_ROOT_USERNAME env PATH="$PATH" rm --version |
365     sed -n '1s/.* //p'
366   )
367   case ":$rm_version:" in
368       :$PACKAGE_VERSION:) ;;
369       *) return 1;;
370   esac
373 require_root_()
375   uid_is_privileged_ || skip_ "must be run as root"
376   NON_ROOT_USERNAME=${NON_ROOT_USERNAME=nobody}
377   NON_ROOT_GROUP=${NON_ROOT_GROUP=$(id -g $NON_ROOT_USERNAME)}
379   # When the current test invokes setuidgid, call setuidgid_has_perm_
380   # to check for a common problem.
381   grep '^[ ]*setuidgid' "../$0" \
382     && { setuidgid_has_perm_ \
383            || skip_ "user $NON_ROOT_USERNAME lacks execute permissions"; }
386 skip_if_root_() { uid_is_privileged_ && skip_ "must be run as non-root"; }
388 # Set 'groups' to a space-separated list of at least two groups
389 # of which the user is a member.
390 require_membership_in_two_groups_()
392   test $# = 0 || framework_failure_
394   groups=${COREUTILS_GROUPS-$( (id -G || /usr/xpg4/bin/id -G) 2>/dev/null)}
395   case "$groups" in
396     *' '*) ;;
397     *) skip_ 'requires membership in two groups
398 this test requires that you be a member of more than one group,
399 but running 'id -G'\'' either failed or found just one.  If you really
400 are a member of at least two groups, then rerun this test with
401 COREUTILS_GROUPS set in your environment to the space-separated list
402 of group names or numbers.  E.g.,
404   env COREUTILS_GROUPS='\''users cdrom'\'' make check
407      ;;
408   esac
411 # Is /proc/$PID/status supported?
412 require_proc_pid_status_()
414     sleep 2 &
415     local pid=$!
416     sleep .5
417     grep '^State:[       ]*[S]' /proc/$pid/status > /dev/null 2>&1 ||
418     skip_ "/proc/$pid/status: missing or 'different'"
419     kill $pid
422 # Return nonzero if the specified path is on a file system for
423 # which FIEMAP support exists.  Note some file systems (like ext3 and btrfs)
424 # only support FIEMAP for files, not directories.
425 fiemap_capable_()
427   if ! python < /dev/null; then
428     warn_ 'fiemap_capable_: python missing: assuming not fiemap capable'
429     return 1
430   fi
431   python "$abs_srcdir"/tests/fiemap-capable "$@"
434 # Skip the current test if "." lacks d_type support.
435 require_dirent_d_type_()
437   python < /dev/null \
438     || skip_ python missing: assuming no d_type support
440   # Manually exclude xfs, since the test would mistakenly report
441   # that it has d_type support: d_type == DT_DIR for "." and "..",
442   # but DT_UNKNOWN for all other types.
443   df -x xfs . > /dev/null 2>&1 \
444     || skip_ requires d_type support
446   python "$abs_srcdir"/tests/d_type-check \
447     || skip_ requires d_type support
450 # Skip the current test if we lack Perl.
451 require_perl_()
453   : ${PERL=perl}
454   $PERL -e 'use warnings' > /dev/null 2>&1 \
455     || skip_ 'configure did not find a usable version of Perl'
458 # Does the current (working-dir) file system support sparse files?
459 require_sparse_support_()
461   test $# = 0 || framework_failure_
462   # Test whether we can create a sparse file.
463   # For example, on Darwin6.5 with a file system of type hfs, it's not possible.
464   # NTFS requires 128K before a hole appears in a sparse file.
465   t=sparse.$$
466   dd bs=1 seek=128K of=$t < /dev/null 2> /dev/null
467   set x $(du -sk $t)
468   kb_size=$2
469   rm -f $t
470   if test $kb_size -ge 128; then
471     skip_ 'this file system does not support sparse files'
472   fi
475 # There are a myriad of ways to build shared libs,
476 # so we only consider running tests requiring shared libs,
477 # on platforms that support building them as follows.
478 require_gcc_shared_()
480   $CC -shared -fPIC -O2 -xc -o d.so -ldl - < /dev/null 2>&1 \
481     || skip_ '$CC -shared ... failed to build a shared lib'
484 mkfifo_or_skip_()
486   test $# = 1 || framework_failure_
487   if ! mkfifo "$1"; then
488     # Make an exception of this case -- usually we interpret framework-creation
489     # failure as a test failure.  However, in this case, when running on a SunOS
490     # system using a disk NFS mounted from OpenBSD, the above fails like this:
491     # mkfifo: cannot make fifo 'fifo-10558': Not owner
492     skip_ 'unable to create a fifo'
493   fi
496 # Disable the current test if the working directory seems to have
497 # the setgid bit set.
498 skip_if_setgid_()
500   setgid_tmpdir=setgid-$$
501   (umask 77; mkdir $setgid_tmpdir)
502   perms=$(stat --printf %A $setgid_tmpdir)
503   rmdir $setgid_tmpdir
504   case $perms in
505     drwx------);;
506     drwxr-xr-x);;  # Windows98 + DJGPP 2.03
507     *) skip_ 'this directory has the setgid bit set';;
508   esac
511 # Skip if files are created with a different group to the current user
512 # This can happen due to a setgid dir, or by some other mechanism on OS X:
513 # http://unix.stackexchange.com/q/63865
514 # http://bugs.gnu.org/14024#41
515 skip_if_nondefault_group_()
517   touch grp.$$
518   gen_ug=$(stat -c '%u:%g' grp.$$)
519   rm grp.$$
520   test "$gen_ug" = "$(id -ru):$(id -rg)" ||
521     skip_ 'Files are created with a different gid'
524 skip_if_mcstransd_is_running_()
526   test $# = 0 || framework_failure_
528   # When mcstransd is running, you'll see only the 3-component
529   # version of file-system context strings.  Detect that,
530   # and if it's running, skip this test.
531   __ctx=$(stat --printf='%C\n' .) || framework_failure_
532   case $__ctx in
533     *:*:*:*) ;; # four components is ok
534     *) # anything else probably means mcstransd is running
535         skip_ "unexpected context '$__ctx'; turn off mcstransd" ;;
536   esac
539 # Skip the current test if umask doesn't work as usual.
540 # This test should be run in the temporary directory that ends
541 # up being removed via the trap commands.
542 working_umask_or_skip_()
544   umask 022
545   touch file1 file2
546   chmod 644 file2
547   perms=$(ls -l file1 file2 | sed 's/ .*//' | uniq)
548   rm -f file1 file2
550   case $perms in
551   *'
552   '*) skip_ 'your build directory has unusual umask semantics'
553   esac
556 # Retry a function requiring a sufficient delay to _pass_
557 # using a truncated exponential backoff method.
558 #     Example: retry_delay_ dd_reblock_1 .1 6
559 # This example will call the dd_reblock_1 function with
560 # an initial delay of .1 second and call it at most 6 times
561 # with a max delay of 3.2s (doubled each time), or a total of 6.3s
562 # Note ensure you do _not_ quote the parameter to GNU sleep in
563 # your function, as it may contain separate values that sleep
564 # needs to accumulate.
565 # Further function arguments will be forwarded to the test function.
566 retry_delay_()
568   local test_func=$1
569   local init_delay=$2
570   local max_n_tries=$3
571   shift 3 || return 1
573   local attempt=1
574   local num_sleeps=$attempt
575   local time_fail
576   while test $attempt -le $max_n_tries; do
577     local delay=$($AWK -v n=$num_sleeps -v s="$init_delay" \
578                   'BEGIN { print s * n }')
579     "$test_func" "$delay" "$@" && { time_fail=0; break; } || time_fail=1
580     attempt=$(expr $attempt + 1)
581     num_sleeps=$(expr $num_sleeps '*' 2)
582   done
583   test "$time_fail" = 0
586 # Call this with a list of programs under test immediately after
587 # sourcing init.sh.
588 print_ver_()
590   if test "$VERBOSE" = yes; then
591     local i
592     for i in $*; do
593       env $i --version
594     done
595   fi
598 # Are we running on GNU/Hurd?
599 require_gnu_()
601   test "$(uname)" = GNU \
602     || skip_ 'not running on GNU/Hurd'
605 sanitize_path_