doc: Add an example for cutting fields separated by runs of blanks
[coreutils.git] / tests / test-lib.sh
blob7ad4331b6ae8491f88c1476d9ccc8cbd2ae9cecc
1 # source this file; set up for tests
3 # Copyright (C) 2009-2010 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 # Skip this test if the shell lacks support for functions.
19 unset function_test
20 eval 'function_test() { return 11; }; function_test'
21 if test $? != 11; then
22 echo "$0: /bin/sh lacks support for functions; skipping this test." 1>&2
23 Exit 77
26 # Having an unsearchable directory in PATH causes execve to fail with EACCES
27 # when applied to an unresolvable program name, contrary to the desired ENOENT.
28 # Avoid the problem by rewriting PATH to exclude unsearchable directories.
29 sanitize_path_()
31 local saved_IFS=$IFS
32 IFS=:
33 set -- $PATH
34 IFS=$saved_IFS
36 local d d1
37 local colon=
38 local new_path=
39 for d in "$@"; do
40 test -z "$d" && d1=. || d1=$d
41 if ls -d "$d1/." > /dev/null 2>&1; then
42 new_path="$new_path$colon$d"
43 colon=':'
45 done
47 PATH=$new_path
48 export PATH
51 skip_test_()
53 echo "$0: skipping test: $@" | head -1 1>&9
54 echo "$0: skipping test: $@" 1>&2
55 Exit 77
58 getlimits_()
60 eval $(getlimits)
61 test "$INT_MAX" ||
62 error_ "Error running getlimits"
65 require_acl_()
67 getfacl --version < /dev/null > /dev/null 2>&1 \
68 && setfacl --version < /dev/null > /dev/null 2>&1 \
69 || skip_test_ "This test requires getfacl and setfacl."
71 id -u bin > /dev/null 2>&1 \
72 || skip_test_ "This test requires a local user named bin."
75 # Skip this test if we're not in SELinux "enforcing" mode.
76 require_selinux_enforcing_()
78 test "$(getenforce)" = Enforcing \
79 || skip_test_ "This test is useful only with SELinux in Enforcing mode."
82 require_openat_support_()
84 # Skip this test if your system has neither the openat-style functions
85 # nor /proc/self/fd support with which to emulate them.
86 test -z "$CONFIG_HEADER" \
87 && skip_test_ 'internal error: CONFIG_HEADER not defined'
89 _skip=yes
90 grep '^#define HAVE_OPENAT' "$CONFIG_HEADER" > /dev/null && _skip=no
91 test -d /proc/self/fd && _skip=no
92 if test $_skip = yes; then
93 skip_test_ 'this system lacks openat support'
97 require_ulimit_()
99 ulimit_works=yes
100 # Expect to be able to exec a program in 10MB of virtual memory,
101 # but not in 20KB. I chose "date". It must not be a shell built-in
102 # function, so you can't use echo, printf, true, etc.
103 # Of course, in coreutils, I could use $top_builddir/src/true,
104 # but this should be able to work for other projects, too.
105 ( ulimit -v 10000; date ) > /dev/null 2>&1 || ulimit_works=no
106 ( ulimit -v 20; date ) > /dev/null 2>&1 && ulimit_works=no
108 test $ulimit_works = no \
109 && skip_test_ "this shell lacks ulimit support"
112 require_readable_root_()
114 test -r / || skip_test_ "/ is not readable"
117 # Skip the current test if strace is not available or doesn't work
118 # with the named syscall. Usage: require_strace_ unlink
119 require_strace_()
121 test $# = 1 || framework_failure
123 strace -V < /dev/null > /dev/null 2>&1 ||
124 skip_test_ 'no strace program'
126 strace -qe "$1" echo > /dev/null 2>&1 ||
127 skip_test_ 'strace -qe "'"$1"'" does not work'
130 # Require a controlling input `terminal'.
131 require_controlling_input_terminal_()
133 tty -s || have_input_tty=no
134 test -t 0 || have_input_tty=no
135 if test "$have_input_tty" = no; then
136 skip_test_ 'requires controlling input terminal
137 This test must have a controlling input "terminal", so it may not be
138 run via "batch", "at", or "ssh". On some systems, it may not even be
139 run in the background.'
143 require_built_()
145 skip_=no
146 for i in "$@"; do
147 case " $built_programs " in
148 *" $i "*) ;;
149 *) echo "$i: not built" 1>&2; skip_=yes ;;
150 esac
151 done
153 test $skip_ = yes && skip_test_ "required program(s) not built"
156 uid_is_privileged_()
158 # Make sure id -u succeeds.
159 my_uid=$(id -u) \
160 || { echo "$0: cannot run \`id -u'" 1>&2; return 1; }
162 # Make sure it gives valid output.
163 case $my_uid in
164 0) ;;
165 *[!0-9]*)
166 echo "$0: invalid output (\`$my_uid') from \`id -u'" 1>&2
167 return 1 ;;
168 *) return 1 ;;
169 esac
172 get_process_status_()
174 sed -n '/^State:[ ]*\([[:alpha:]]\).*/s//\1/p' /proc/$1/status
177 # Convert an ls-style permission string, like drwxr----x and -rw-r-x-wx
178 # to the equivalent chmod --mode (-m) argument, (=,u=rwx,g=r,o=x and
179 # =,u=rw,g=rx,o=wx). Ignore ACLs.
180 rwx_to_mode_()
182 case $# in
183 1) rwx=$1;;
184 *) echo "$0: wrong number of arguments" 1>&2
185 echo "Usage: $0 ls-style-mode-string" 1>&2
186 return;;
187 esac
189 case $rwx in
190 [ld-][rwx-][rwx-][rwxsS-][rwx-][rwx-][rwxsS-][rwx-][rwx-][rwxtT-]) ;;
191 [ld-][rwx-][rwx-][rwxsS-][rwx-][rwx-][rwxsS-][rwx-][rwx-][rwxtT-][+.]) ;;
192 *) echo "$0: invalid mode string: $rwx" 1>&2; return;;
193 esac
195 # Perform these conversions:
196 # S s
197 # s xs
198 # T t
199 # t xt
200 # The `T' and `t' ones are only valid for `other'.
201 s='s/S/@/;s/s/x@/;s/@/s/'
202 t='s/T/@/;s/t/x@/;s/@/t/'
204 u=`echo $rwx|sed 's/^.\(...\).*/,u=\1/;s/-//g;s/^,u=$//;'$s`
205 g=`echo $rwx|sed 's/^....\(...\).*/,g=\1/;s/-//g;s/^,g=$//;'$s`
206 o=`echo $rwx|sed 's/^.......\(...\).*/,o=\1/;s/-//g;s/^,o=$//;'$s';'$t`
207 echo "=$u$g$o"
210 skip_if_()
212 case $1 in
213 root) skip_test_ must be run as root ;;
214 non-root) skip_test_ must be run as non-root ;;
215 *) ;; # FIXME?
216 esac
219 require_selinux_()
221 case `ls -Zd .` in
222 '? .'|'unlabeled .')
223 skip_test_ "this system (or maybe just" \
224 "the current file system) lacks SELinux support"
226 esac
229 very_expensive_()
231 if test "$RUN_VERY_EXPENSIVE_TESTS" != yes; then
232 skip_test_ 'very expensive: disabled by default
233 This test is very expensive, so it is disabled by default.
234 To run it anyway, rerun make check with the RUN_VERY_EXPENSIVE_TESTS
235 environment variable set to yes. E.g.,
237 env RUN_VERY_EXPENSIVE_TESTS=yes make check
242 expensive_()
244 if test "$RUN_EXPENSIVE_TESTS" != yes; then
245 skip_test_ 'expensive: disabled by default
246 This test is relatively expensive, so it is disabled by default.
247 To run it anyway, rerun make check with the RUN_EXPENSIVE_TESTS
248 environment variable set to yes. E.g.,
250 env RUN_EXPENSIVE_TESTS=yes make check
255 require_root_()
257 uid_is_privileged_ || skip_test_ "must be run as root"
258 NON_ROOT_USERNAME=${NON_ROOT_USERNAME=nobody}
259 NON_ROOT_GROUP=${NON_ROOT_GROUP=$(id -g $NON_ROOT_USERNAME)}
262 skip_if_root_() { uid_is_privileged_ && skip_test_ "must be run as non-root"; }
263 error_() { echo "$0: $@" 1>&2; Exit 1; }
264 framework_failure() { error_ 'failure in testing framework'; }
266 # Set `groups' to a space-separated list of at least two groups
267 # of which the user is a member.
268 require_membership_in_two_groups_()
270 test $# = 0 || framework_failure
272 groups=${COREUTILS_GROUPS-`(id -G || /usr/xpg4/bin/id -G) 2>/dev/null`}
273 case "$groups" in
274 *' '*) ;;
275 *) skip_test_ 'requires membership in two groups
276 this test requires that you be a member of more than one group,
277 but running `id -G'\'' either failed or found just one. If you really
278 are a member of at least two groups, then rerun this test with
279 COREUTILS_GROUPS set in your environment to the space-separated list
280 of group names or numbers. E.g.,
282 env COREUTILS_GROUPS='users cdrom' make check
286 esac
289 # Is /proc/$PID/status supported?
290 require_proc_pid_status_()
292 sleep 2 &
293 local pid=$!
294 sleep .5
295 grep '^State:[ ]*[S]' /proc/$pid/status > /dev/null 2>&1 ||
296 skip_test_ "/proc/$pid/status: missing or 'different'"
297 kill $pid
300 # Does the current (working-dir) file system support sparse files?
301 require_sparse_support_()
303 test $# = 0 || framework_failure
304 # Test whether we can create a sparse file.
305 # For example, on Darwin6.5 with a file system of type hfs, it's not possible.
306 # NTFS requires 128K before a hole appears in a sparse file.
307 t=sparse.$$
308 dd bs=1 seek=128K of=$t < /dev/null 2> /dev/null
309 set x `du -sk $t`
310 kb_size=$2
311 rm -f $t
312 if test $kb_size -ge 128; then
313 skip_test_ 'this file system does not support sparse files'
317 mkfifo_or_skip_()
319 test $# = 1 || framework_failure
320 if ! mkfifo "$1"; then
321 # Make an exception of this case -- usually we interpret framework-creation
322 # failure as a test failure. However, in this case, when running on a SunOS
323 # system using a disk NFS mounted from OpenBSD, the above fails like this:
324 # mkfifo: cannot make fifo `fifo-10558': Not owner
325 skip_test_ 'NOTICE: unable to create test prerequisites'
329 # Disable the current test if the working directory seems to have
330 # the setgid bit set.
331 skip_if_setgid_()
333 setgid_tmpdir=setgid-$$
334 (umask 77; mkdir $setgid_tmpdir)
335 perms=$(stat --printf %A $setgid_tmpdir)
336 rmdir $setgid_tmpdir
337 case $perms in
338 drwx------);;
339 drwxr-xr-x);; # Windows98 + DJGPP 2.03
340 *) skip_test_ 'this directory has the setgid bit set';;
341 esac
344 skip_if_mcstransd_is_running_()
346 test $# = 0 || framework_failure
348 # When mcstransd is running, you'll see only the 3-component
349 # version of file-system context strings. Detect that,
350 # and if it's running, skip this test.
351 __ctx=$(stat --printf='%C\n' .) || framework_failure
352 case $__ctx in
353 *:*:*:*) ;; # four components is ok
354 *) # anything else probably means mcstransd is running
355 skip_test_ "unexpected context '$__ctx'; turn off mcstransd" ;;
356 esac
359 # Skip the current test if umask doesn't work as usual.
360 # This test should be run in the temporary directory that ends
361 # up being removed via the trap commands.
362 working_umask_or_skip_()
364 umask 022
365 touch file1 file2
366 chmod 644 file2
367 perms=`ls -l file1 file2 | sed 's/ .*//' | uniq`
368 rm -f file1 file2
370 case $perms in
372 '*) skip_test_ 'your build directory has unusual umask semantics'
373 esac
376 # We use a trap below for cleanup. This requires us to go through
377 # hoops to get the right exit status transported through the signal.
378 # So use `Exit STATUS' instead of `exit STATUS' inside of the tests.
379 # Turn off errexit here so that we don't trip the bug with OSF1/Tru64
380 # sh inside this function.
381 Exit ()
383 set +e
384 (exit $1)
385 exit $1
388 test_dir_=$(pwd)
390 this_test_() { echo "./$0" | sed 's,.*/,,'; }
391 this_test=$(this_test_)
393 # This is a stub function that is run upon trap (upon regular exit and
394 # interrupt). Override it with a per-test function, e.g., to unmount
395 # a partition, or to undo any other global state changes.
396 cleanup_() { :; }
398 t_=$("$abs_top_builddir/src/mktemp" -d --tmp="$test_dir_" cu-$this_test.XXXXXXXXXX)\
399 || error_ "failed to create temporary directory in $test_dir_"
401 remove_tmp_()
403 __st=$?
404 cleanup_
405 cd "$test_dir_" && chmod -R u+rwx "$t_" && rm -rf "$t_" && exit $__st
408 # Run each test from within a temporary sub-directory named after the
409 # test itself, and arrange to remove it upon exception or normal exit.
410 trap remove_tmp_ 0
411 trap 'Exit $?' 1 2 13 15
413 cd "$t_" || error_ "failed to cd to $t_"
415 if ( diff --version < /dev/null 2>&1 | grep GNU ) 2>&1 > /dev/null; then
416 compare() { diff -u "$@"; }
417 elif ( cmp --version < /dev/null 2>&1 | grep GNU ) 2>&1 > /dev/null; then
418 compare() { cmp -s "$@"; }
419 else
420 compare() { cmp "$@"; }
423 sanitize_path_
425 # Initialize; all bourne shell scripts end with "Exit $fail".
426 fail=0