tests: avoid false-positive failures on linux/sparc64
[coreutils.git] / tests / init.cfg
blob915f38a8d35ae1bc6ef268d81d09e8d213cbce75
1 # This file is sourced by init.sh, *before* its initialization.
3 # Copyright (C) 2010-2011 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/>.
19 # This goes hand in hand with the "exec 9>&2;" in tests/Makefile.am's
20 # TESTS_ENVIRONMENT definition.
21 stderr_fileno_=9
23 # Having an unsearchable directory in PATH causes execve to fail with EACCES
24 # when applied to an unresolvable program name, contrary to the desired ENOENT.
25 # Avoid the problem by rewriting PATH to exclude unsearchable directories.
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   PATH=$new_path
47   export PATH
50 getlimits_()
52   eval $(getlimits)
53   test "$INT_MAX" || fatal_ "running getlimits"
56 require_acl_()
58   getfacl --version < /dev/null > /dev/null 2>&1 \
59     && setfacl --version < /dev/null > /dev/null 2>&1 \
60       || skip_ "This test requires getfacl and setfacl."
62   id -u bin > /dev/null 2>&1 \
63     || skip_ "This test requires a local user named bin."
66 is_local_dir_()
68   test $# = 1 || framework_failure_
69   df --local "$1" >/dev/null 2>&1
72 require_local_dir_()
74   is_local_dir_ . ||
75     skip_ "This test must be run on a local file system."
78 # Skip this test if we're not in SELinux "enforcing" mode.
79 require_selinux_enforcing_()
81   test "$(getenforce)" = Enforcing \
82     || skip_ "This test is useful only with SELinux in Enforcing mode."
85 require_openat_support_()
87   # Skip this test if your system has neither the openat-style functions
88   # nor /proc/self/fd support with which to emulate them.
89   test -z "$CONFIG_HEADER" \
90     && skip_ 'internal error: CONFIG_HEADER not defined'
92   _skip=yes
93   grep '^#define HAVE_OPENAT' "$CONFIG_HEADER" > /dev/null && _skip=no
94   test -d /proc/self/fd && _skip=no
95   if test $_skip = yes; then
96     skip_ 'this system lacks openat support'
97   fi
100 require_ulimit_()
102   ulimit_works=yes
103   # Expect to be able to exec a program in 10MB of virtual memory,
104   # but not in 20KB.  I chose "date".  It must not be a shell built-in
105   # function, so you can't use echo, printf, true, etc.
106   # Of course, in coreutils, I could use $top_builddir/src/true,
107   # but this should be able to work for other projects, too.
108   ( ulimit -v 10000; date ) > /dev/null 2>&1 || ulimit_works=no
109   ( ulimit -v 20;    date ) > /dev/null 2>&1 && ulimit_works=no
111   test $ulimit_works = no \
112     && skip_ "this shell lacks ulimit support"
115 require_readable_root_()
117   test -r / || skip_ "/ is not readable"
120 # Skip the current test if strace is not available or doesn't work
121 # with the named syscall.  Usage: require_strace_ unlink
122 require_strace_()
124   test $# = 1 || framework_failure_
126   strace -V < /dev/null > /dev/null 2>&1 ||
127     skip_ 'no strace program'
129   strace -qe "$1" echo > /dev/null 2>&1 ||
130     skip_ 'strace -qe "'"$1"'" does not work'
132   # On some linux/sparc64 systems, strace works fine on 32-bit executables,
133   # but prints only one line of output for every 64-bit executable.
134   strace -o log-help ls --help >/dev/null || framework_failure_
135   n_lines_help=$(wc -l < log-help)
136   rm -f log-help
137   if test $n_lines_help = 0 || test $n_lines_help = 1; then
138     skip_ 'strace produces no more than one line of output'
139   fi
142 require_setfacl_()
144   setfacl -m user::rwx . \
145     || skip_ "setfacl does not work on the current file system"
148 # Require a controlling input `terminal'.
149 require_controlling_input_terminal_()
151   tty -s || have_input_tty=no
152   test -t 0 || have_input_tty=no
153   if test "$have_input_tty" = no; then
154     skip_ 'requires controlling input terminal
155 This test must have a controlling input "terminal", so it may not be
156 run via "batch", "at", or "ssh".  On some systems, it may not even be
157 run in the background.'
158   fi
161 require_built_()
163   skip_=no
164   for i in "$@"; do
165     case " $built_programs " in
166       *" $i "*) ;;
167       *) echo "$i: not built" 1>&2; skip_=yes ;;
168     esac
169   done
171   test $skip_ = yes && skip_ "required program(s) not built"
174 require_file_system_bytes_free_()
176   local req=$1
177   local expr=$(stat -f --printf "$req / %S <= %a" .)
178   awk "BEGIN{ exit !($expr) }" \
179     || skip_ "this test needs at least $req bytes of free space"
182 uid_is_privileged_()
184   # Make sure id -u succeeds.
185   my_uid=$(id -u) \
186     || { echo "$0: cannot run \`id -u'" 1>&2; return 1; }
188   # Make sure it gives valid output.
189   case $my_uid in
190     0) ;;
191     *[!0-9]*)
192       echo "$0: invalid output (\`$my_uid') from \`id -u'" 1>&2
193       return 1 ;;
194     *) return 1 ;;
195   esac
198 # Some versions of sudo do not include /sbin in PATH.
199 # Test if mkfs is in PATH, otherwise try to adapt PATH.
200 require_mkfs_PATH_()
202   type mkfs && return
204   case ":$PATH:" in
205     *:/sbin:*) skip_ "no usable mkfs found" ;;
206   esac
208   test -x /sbin/mkfs \
209     || skip_ "no usable mkfs found"
211   PATH="$PATH:/sbin"
212   export PATH
215 get_process_status_()
217   sed -n '/^State:[      ]*\([[:alpha:]]\).*/s//\1/p' /proc/$1/status
220 # Convert an ls-style permission string, like drwxr----x and -rw-r-x-wx
221 # to the equivalent chmod --mode (-m) argument, (=,u=rwx,g=r,o=x and
222 # =,u=rw,g=rx,o=wx).  Ignore ACLs.
223 rwx_to_mode_()
225   case $# in
226     1) rwx=$1;;
227     *) echo "$0: wrong number of arguments" 1>&2
228       echo "Usage: $0 ls-style-mode-string" 1>&2
229       return;;
230   esac
232   case $rwx in
233     [ld-][rwx-][rwx-][rwxsS-][rwx-][rwx-][rwxsS-][rwx-][rwx-][rwxtT-]) ;;
234     [ld-][rwx-][rwx-][rwxsS-][rwx-][rwx-][rwxsS-][rwx-][rwx-][rwxtT-][+.]) ;;
235     *) echo "$0: invalid mode string: $rwx" 1>&2; return;;
236   esac
238   # Perform these conversions:
239   # S  s
240   # s  xs
241   # T  t
242   # t  xt
243   # The `T' and `t' ones are only valid for `other'.
244   s='s/S/@/;s/s/x@/;s/@/s/'
245   t='s/T/@/;s/t/x@/;s/@/t/'
247   u=`echo $rwx|sed 's/^.\(...\).*/,u=\1/;s/-//g;s/^,u=$//;'$s`
248   g=`echo $rwx|sed 's/^....\(...\).*/,g=\1/;s/-//g;s/^,g=$//;'$s`
249   o=`echo $rwx|sed 's/^.......\(...\).*/,o=\1/;s/-//g;s/^,o=$//;'$s';'$t`
250   echo "=$u$g$o"
253 skip_if_()
255   case $1 in
256     root) skip_ must be run as root ;;
257     non-root) skip_ must be run as non-root ;;
258     *) ;;  # FIXME?
259   esac
262 require_selinux_()
264   # When in a chroot of an SELinux-enabled system, but with a mock-simulated
265   # SELinux-*disabled* system, recognize that SELinux is disabled system wide:
266   grep 'selinuxfs$' /proc/filesystems > /dev/null \
267     || skip_ "this system lacks SELinux support"
269   # Independent of whether SELinux is enabled system-wide,
270   # the current file system may lack SELinux support.
271   case `ls -Zd .` in
272     '? .'|'unlabeled .')
273       skip_ "this system (or maybe just" \
274         "the current file system) lacks SELinux support"
275     ;;
276   esac
279 very_expensive_()
281   if test "$RUN_VERY_EXPENSIVE_TESTS" != yes; then
282     skip_ 'very expensive: disabled by default
283 This test is very expensive, so it is disabled by default.
284 To run it anyway, rerun make check with the RUN_VERY_EXPENSIVE_TESTS
285 environment variable set to yes.  E.g.,
287   env RUN_VERY_EXPENSIVE_TESTS=yes make check
289 or use the shortcut target of the toplevel Makefile,
291   make check-very-expensive
293   fi
296 expensive_()
298   if test "$RUN_EXPENSIVE_TESTS" != yes; then
299     skip_ 'expensive: disabled by default
300 This test is relatively expensive, so it is disabled by default.
301 To run it anyway, rerun make check with the RUN_EXPENSIVE_TESTS
302 environment variable set to yes.  E.g.,
304   env RUN_EXPENSIVE_TESTS=yes make check
306 or use the shortcut target of the toplevel Makefile,
308   make check-expensive
310   fi
313 require_root_()
315   uid_is_privileged_ || skip_ "must be run as root"
316   NON_ROOT_USERNAME=${NON_ROOT_USERNAME=nobody}
317   NON_ROOT_GROUP=${NON_ROOT_GROUP=$(id -g $NON_ROOT_USERNAME)}
320 skip_if_root_() { uid_is_privileged_ && skip_ "must be run as non-root"; }
322 # Set `groups' to a space-separated list of at least two groups
323 # of which the user is a member.
324 require_membership_in_two_groups_()
326   test $# = 0 || framework_failure_
328   groups=${COREUTILS_GROUPS-`(id -G || /usr/xpg4/bin/id -G) 2>/dev/null`}
329   case "$groups" in
330     *' '*) ;;
331     *) skip_ 'requires membership in two groups
332 this test requires that you be a member of more than one group,
333 but running `id -G'\'' either failed or found just one.  If you really
334 are a member of at least two groups, then rerun this test with
335 COREUTILS_GROUPS set in your environment to the space-separated list
336 of group names or numbers.  E.g.,
338   env COREUTILS_GROUPS='\''users cdrom'\'' make check
341      ;;
342   esac
345 # Is /proc/$PID/status supported?
346 require_proc_pid_status_()
348     sleep 2 &
349     local pid=$!
350     sleep .5
351     grep '^State:[       ]*[S]' /proc/$pid/status > /dev/null 2>&1 ||
352     skip_ "/proc/$pid/status: missing or 'different'"
353     kill $pid
356 # Return nonzero if the specified path is on a file system for
357 # which FIEMAP support exists.  Note some file systems (like ext3 and btrfs)
358 # only support FIEMAP for files, not directories.
359 fiemap_capable_()
361   if ! python < /dev/null; then
362     warn_ 'fiemap_capable_: python missing: assuming not fiemap capable'
363     return 1
364   fi
365   python $abs_srcdir/fiemap-capable "$@"
368 # Skip the current test if "." lacks d_type support.
369 require_dirent_d_type_()
371   python < /dev/null \
372     || skip_ python missing: assuming no d_type support
374   python $abs_srcdir/d_type-check \
375     || skip_ requires d_type support
378 # Skip the current test if we lack Perl.
379 require_perl_()
381   : ${PERL=perl}
382   $PERL -e 'use warnings' > /dev/null 2>&1 \
383     || skip_ 'configure did not find a usable version of Perl'
386 # Does the current (working-dir) file system support sparse files?
387 require_sparse_support_()
389   test $# = 0 || framework_failure_
390   # Test whether we can create a sparse file.
391   # For example, on Darwin6.5 with a file system of type hfs, it's not possible.
392   # NTFS requires 128K before a hole appears in a sparse file.
393   t=sparse.$$
394   dd bs=1 seek=128K of=$t < /dev/null 2> /dev/null
395   set x `du -sk $t`
396   kb_size=$2
397   rm -f $t
398   if test $kb_size -ge 128; then
399     skip_ 'this file system does not support sparse files'
400   fi
403 mkfifo_or_skip_()
405   test $# = 1 || framework_failure_
406   if ! mkfifo "$1"; then
407     # Make an exception of this case -- usually we interpret framework-creation
408     # failure as a test failure.  However, in this case, when running on a SunOS
409     # system using a disk NFS mounted from OpenBSD, the above fails like this:
410     # mkfifo: cannot make fifo `fifo-10558': Not owner
411     skip_ 'unable to create a fifo'
412   fi
415 # Disable the current test if the working directory seems to have
416 # the setgid bit set.
417 skip_if_setgid_()
419   setgid_tmpdir=setgid-$$
420   (umask 77; mkdir $setgid_tmpdir)
421   perms=$(stat --printf %A $setgid_tmpdir)
422   rmdir $setgid_tmpdir
423   case $perms in
424     drwx------);;
425     drwxr-xr-x);;  # Windows98 + DJGPP 2.03
426     *) skip_ 'this directory has the setgid bit set';;
427   esac
430 skip_if_mcstransd_is_running_()
432   test $# = 0 || framework_failure_
434   # When mcstransd is running, you'll see only the 3-component
435   # version of file-system context strings.  Detect that,
436   # and if it's running, skip this test.
437   __ctx=$(stat --printf='%C\n' .) || framework_failure_
438   case $__ctx in
439     *:*:*:*) ;; # four components is ok
440     *) # anything else probably means mcstransd is running
441         skip_ "unexpected context '$__ctx'; turn off mcstransd" ;;
442   esac
445 # Skip the current test if umask doesn't work as usual.
446 # This test should be run in the temporary directory that ends
447 # up being removed via the trap commands.
448 working_umask_or_skip_()
450   umask 022
451   touch file1 file2
452   chmod 644 file2
453   perms=`ls -l file1 file2 | sed 's/ .*//' | uniq`
454   rm -f file1 file2
456   case $perms in
457   *'
458   '*) skip_ 'your build directory has unusual umask semantics'
459   esac
462 # Retry a function requiring a sufficient delay to _pass_
463 # using a truncated exponential backoff method.
464 #     Example: retry_delay_ dd_reblock_1 .1 6
465 # This example will call the dd_reblock_1 function with
466 # an initial delay of .1 second and call it at most 6 times
467 # with a max delay of 3.2s (doubled each time), or a total of 6.3s
468 # Note ensure you do _not_ quote the parameter to GNU sleep in
469 # your function, as it may contain separate values that `sleep`
470 # needs to accumulate.
471 retry_delay_()
473   local test_func=$1
474   local init_delay=$2
475   local max_n_tries=$3
477   local attempt=1
478   local num_sleeps=$attempt
479   local time_fail
480   while test $attempt -le $max_n_tries; do
481     local delay=$($AWK -v n=$num_sleeps -v s="$init_delay" \
482                   'BEGIN { print s * n }')
483     "$test_func" "$delay" && { time_fail=0; break; } || time_fail=1
484     attempt=$(expr $attempt + 1)
485     num_sleeps=$(expr $num_sleeps '*' 2)
486   done
487   test "$time_fail" = 0
490 # Call this with a list of programs under test immediately after
491 # sourcing init.sh.
492 print_ver_()
494   if test "$VERBOSE" = yes; then
495     local i
496     for i in $*; do
497       env $i --version
498     done
499   fi
502 sanitize_path_