1 # Functions sourced via testing framework.
7 error_ "Error running getlimits"
12 getfacl --version < /dev/null > /dev/null 2>&1 \
13 && setfacl --version < /dev/null > /dev/null 2>&1 \
14 || skip_ "This test requires getfacl and setfacl."
16 id -u bin > /dev/null 2>&1 \
17 || skip_ "This test requires a local user named bin."
22 mkfs.hfs 2>&1 | grep '^usage:' \
23 || skip_ "This test requires HFS support."
26 # Skip this test if we're not in SELinux "enforcing" mode.
27 require_selinux_enforcing_()
29 test "$(getenforce)" = Enforcing \
30 || skip_ "This test is useful only with SELinux in Enforcing mode."
34 require_openat_support_()
36 # Skip this test if your system has neither the openat-style functions
37 # nor /proc/self/fd support with which to emulate them.
38 test -z "$CONFIG_HEADER" \
39 && skip_ 'internal error: CONFIG_HEADER not defined'
42 grep '^#define HAVE_OPENAT' "$CONFIG_HEADER" > /dev/null && _skip=no
43 test -d /proc/self/fd && _skip=no
44 if test $_skip = yes; then
45 skip_ 'this system lacks openat support'
52 # Expect to be able to exec a program in 10MB of virtual memory,
53 # but not in 20KB. I chose "date". It must not be a shell built-in
54 # function, so you can't use echo, printf, true, etc.
55 # Of course, in coreutils, I could use $top_builddir/src/true,
56 # but this should be able to work for other projects, too.
57 ( ulimit -v 10000; date ) > /dev/null 2>&1 || ulimit_works=no
58 ( ulimit -v 20; date ) > /dev/null 2>&1 && ulimit_works=no
60 test $ulimit_works = no \
61 && skip_ "this shell lacks ulimit support"
64 require_readable_root_()
66 test -r / || skip_ "/ is not readable"
69 # Skip the current test if strace is not available or doesn't work
70 # with the named syscall. Usage: require_strace_ unlink
73 test $# = 1 || framework_failure
75 strace -V < /dev/null > /dev/null 2>&1 ||
76 skip_ 'no strace program'
78 strace -qe "$1" echo > /dev/null 2>&1 ||
79 skip_ 'strace -qe "'"$1"'" does not work'
82 # Require a controlling input `terminal'.
83 require_controlling_input_terminal_()
85 tty -s || have_input_tty=no
86 test -t 0 || have_input_tty=no
87 if test "$have_input_tty" = no; then
88 skip_ 'requires controlling input terminal
89 This test must have a controlling input "terminal", so it may not be
90 run via "batch", "at", or "ssh". On some systems, it may not even be
91 run in the background.'
97 # Make sure id -u succeeds.
99 || { echo "$0: cannot run \`id -u'" 1>&2; return 1; }
101 # Make sure it gives valid output.
105 echo "$0: invalid output (\`$my_uid') from \`id -u'" 1>&2
111 get_process_status_()
113 sed -n '/^State:[ ]*\([[:alpha:]]\).*/s//\1/p' /proc/$1/status
116 # Convert an ls-style permission string, like drwxr----x and -rw-r-x-wx
117 # to the equivalent chmod --mode (-m) argument, (=,u=rwx,g=r,o=x and
118 # =,u=rw,g=rx,o=wx). Ignore ACLs.
123 *) echo "$0: wrong number of arguments" 1>&2
124 echo "Usage: $0 ls-style-mode-string" 1>&2
129 [ld-][rwx-][rwx-][rwxsS-][rwx-][rwx-][rwxsS-][rwx-][rwx-][rwxtT-]) ;;
130 [ld-][rwx-][rwx-][rwxsS-][rwx-][rwx-][rwxsS-][rwx-][rwx-][rwxtT-][+.]) ;;
131 *) echo "$0: invalid mode string: $rwx" 1>&2; return;;
134 # Perform these conversions:
139 # The `T' and `t' ones are only valid for `other'.
140 s='s/S/@/;s/s/x@/;s/@/s/'
141 t='s/T/@/;s/t/x@/;s/@/t/'
143 u=`echo $rwx|sed 's/^.\(...\).*/,u=\1/;s/-//g;s/^,u=$//;'$s`
144 g=`echo $rwx|sed 's/^....\(...\).*/,g=\1/;s/-//g;s/^,g=$//;'$s`
145 o=`echo $rwx|sed 's/^.......\(...\).*/,o=\1/;s/-//g;s/^,o=$//;'$s';'$t`
153 skip_ "this system (or maybe just" \
154 "the current file system) lacks SELinux support"
161 if test "$RUN_VERY_EXPENSIVE_TESTS" != yes; then
162 skip_ 'very expensive: disabled by default
163 This test is very expensive, so it is disabled by default.
164 To run it anyway, rerun make check with the RUN_VERY_EXPENSIVE_TESTS
165 environment variable set to yes. E.g.,
167 env RUN_VERY_EXPENSIVE_TESTS=yes make check
174 if test "$RUN_EXPENSIVE_TESTS" != yes; then
175 skip_ 'expensive: disabled by default
176 This test is relatively expensive, so it is disabled by default.
177 To run it anyway, rerun make check with the RUN_EXPENSIVE_TESTS
178 environment variable set to yes. E.g.,
180 env RUN_EXPENSIVE_TESTS=yes make check
187 uid_is_privileged_ || skip_ "must be run as root"
188 NON_ROOT_USERNAME=${NON_ROOT_USERNAME=nobody}
189 NON_ROOT_GROUP=${NON_ROOT_GROUP=$(id -g $NON_ROOT_USERNAME)}
192 skip_if_root_() { uid_is_privileged_ && skip_ "must be run as non-root"; }
193 error_() { echo "$0: $@" 1>&2; Exit 1; }
194 framework_failure() { error_ 'failure in testing framework'; }
196 # Set `groups' to a space-separated list of at least two groups
197 # of which the user is a member.
198 require_membership_in_two_groups_()
200 test $# = 0 || framework_failure
202 groups=${COREUTILS_GROUPS-`(id -G || /usr/xpg4/bin/id -G) 2>/dev/null`}
205 *) skip_ 'requires membership in two groups
206 this test requires that you be a member of more than one group,
207 but running `id -G'\'' either failed or found just one. If you really
208 are a member of at least two groups, then rerun this test with
209 COREUTILS_GROUPS set in your environment to the space-separated list
210 of group names or numbers. E.g.,
212 env COREUTILS_GROUPS='users cdrom' make check
219 # Is /proc/$PID/status supported?
220 require_proc_pid_status_()
225 grep '^State:[ ]*[S]' /proc/$pid/status > /dev/null 2>&1 ||
226 skip_ "/proc/$pid/status: missing or 'different'"
230 # Does the current (working-dir) file system support sparse files?
231 require_sparse_support_()
233 test $# = 0 || framework_failure
234 # Test whether we can create a sparse file.
235 # For example, on Darwin6.5 with a file system of type hfs, it's not possible.
236 # NTFS requires 128K before a hole appears in a sparse file.
238 dd bs=1 seek=128K of=$t < /dev/null 2> /dev/null
242 if test $kb_size -ge 128; then
243 skip_ 'this file system does not support sparse files'
249 test $# = 1 || framework_failure
250 if ! mkfifo "$1"; then
251 # Make an exception of this case -- usually we interpret framework-creation
252 # failure as a test failure. However, in this case, when running on a SunOS
253 # system using a disk NFS mounted from OpenBSD, the above fails like this:
254 # mkfifo: cannot make fifo `fifo-10558': Not owner
255 skip_ 'NOTICE: unable to create test prerequisites'
259 # Disable the current test if the working directory seems to have
260 # the setgid bit set.
263 setgid_tmpdir=setgid-$$
264 (umask 77; mkdir $setgid_tmpdir)
265 perms=$(stat --printf %A $setgid_tmpdir)
269 drwxr-xr-x);; # Windows98 + DJGPP 2.03
270 *) skip_ 'this directory has the setgid bit set';;
274 skip_if_mcstransd_is_running_()
276 test $# = 0 || framework_failure
278 # When mcstransd is running, you'll see only the 3-component
279 # version of file-system context strings. Detect that,
280 # and if it's running, skip this test.
281 __ctx=$(stat --printf='%C\n' .) || framework_failure
283 *:*:*:*) ;; # four components is ok
284 *) # anything else probably means mcstransd is running
285 skip_ "unexpected context '$__ctx'; turn off mcstransd" ;;
289 # Skip the current test if umask doesn't work as usual.
290 # This test should be run in the temporary directory that ends
291 # up being removed via the trap commands.
292 working_umask_or_skip_()
297 perms=`ls -l file1 file2 | sed 's/ .*//' | uniq`
302 '*) skip_ 'your build directory has unusual umask semantics'
306 emit_superuser_warning()
310 echo 'WARNING: You are not superuser. Watch out for permissions.' || :
315 mdadm --version || skip_ "find mdadm executable"
318 # Will look for an md number that is not in use and create a md device with
319 # that number. If the system has more than 9 md devices, it will fail.
320 mdadm_create_linear_device_()
324 for i in 0 1 2 3 4 5 6 7 8 9 ; do
326 mdadm --create --force $mdd --level=linear --raid-devices=1 $lo_dev \
330 if [ $i -eq 9 ]; then echo $mdd ; return 1 ; fi
337 # Often, when parted cannot use the specified size or start/endpoints
338 # of a partition, it outputs a warning or error like this:
340 # Error: You requested a partition from 512B to 50.7kB (...).
341 # The closest location we can manage is 17.4kB to 33.8kB (...).
343 # But those numbers depend on sector size, so
344 # replace the specific values with place-holders,
345 # so tests do not depend on sector size.
346 normalize_part_diag_()
349 sed 's/ [0-9.k]*B to [0-9.k]*B (sectors .*$/ X to Y./' $file > $file.t \
350 && mv $file.t $file && return 0
356 mkfs.xfs -V || skip_ "this test requires XFS support"
362 || skip_ 'dvhtool is required for this test'
365 # Helper function: wait 2s (via .1s increments) for FILE to appear.
366 # Usage: wait_for_dev_to_appear_ /dev/sdg
367 # Return 0 upon success, 1 upon failure.
368 wait_for_dev_to_appear_()
374 ls "$file" > /dev/null 2>&1 && return 0
375 sleep .1 2>/dev/null || { sleep 1; incr=10; }
376 i=$(expr $i + $incr); test $i = 20 && break
381 # Like the above, but don't hard-code the max timeout.
382 wait_for_dev_to_disappear_()
389 ls "$file" > /dev/null 2>&1 || return 0
390 sleep .1 2>/dev/null || { sleep 1; incr=10; }
391 i=$(expr $i + $incr); test $i -ge $(expr $n_sec \* 10) && break
396 device_mapper_required_()
398 . "$abs_top_srcdir/tests/lvm-utils.sh" \
399 || fail_ "device mapper setup failed"