tests: test recent status changes
authorEric Blake <ebb9@byu.net>
Fri, 23 Oct 2009 17:01:25 +0000 (11:01 -0600)
committerEric Blake <ebb9@byu.net>
Fri, 23 Oct 2009 22:32:26 +0000 (16:32 -0600)
* tests/misc/nice: Enhance test.
* tests/misc/chroot-fail: New test.
* tests/misc/env: Likewise.
* tests/misc/nice-fail: Likewise.
* tests/misc/su-fail: Likewise.
* tests/Makefile.am (TESTS): Run new tests.

tests/Makefile.am
tests/misc/chroot-fail [new file with mode: 0755]
tests/misc/env [new file with mode: 0755]
tests/misc/nice
tests/misc/nice-fail [new file with mode: 0755]
tests/misc/su-fail [new file with mode: 0755]

index be92fba..03fe6f1 100644 (file)
@@ -74,7 +74,7 @@ EXTRA_DIST += $(TESTS)
 
 TESTS =                                                \
   misc/help-version                            \
-  tail-2/inotify-race                          \
+  tail-2/inotify-race                          \
   misc/invalid-opt                             \
   rm/ext3-perf                                 \
   rm/cycle                                     \
@@ -126,6 +126,7 @@ TESTS =                                             \
   chgrp/no-x                                   \
   chgrp/posix-H                                        \
   chgrp/recurse                                        \
+  misc/env                                     \
   misc/ptx                                     \
   misc/test                                    \
   misc/seq                                     \
@@ -158,6 +159,7 @@ TESTS =                                             \
   misc/base64                                  \
   misc/basename                                        \
   misc/close-stdout                            \
+  misc/chroot-fail                             \
   misc/comm                                    \
   misc/csplit                                  \
   misc/date-sec                                        \
@@ -182,6 +184,7 @@ TESTS =                                             \
   misc/md5sum-parallel                         \
   misc/mknod                                   \
   misc/nice                                    \
+  misc/nice-fail                               \
   misc/nl                                      \
   misc/nohup                                   \
   misc/od-N                                    \
@@ -225,6 +228,7 @@ TESTS =                                             \
   misc/stty                                    \
   misc/stty-invalid                            \
   misc/stty-row-col                            \
+  misc/su-fail                                 \
   misc/sum                                     \
   misc/sum-sysv                                        \
   misc/tac                                     \
diff --git a/tests/misc/chroot-fail b/tests/misc/chroot-fail
new file mode 100755 (executable)
index 0000000..5b58293
--- /dev/null
@@ -0,0 +1,50 @@
+#!/bin/sh
+# Verify that internal failure in chroot gives exact status.
+
+# Copyright (C) 2009 Free Software Foundation, Inc.
+
+# This program is free software: you can redistribute it and/or modify
+# it under the terms of the GNU General Public License as published by
+# the Free Software Foundation, either version 3 of the License, or
+# (at your option) any later version.
+
+# This program is distributed in the hope that it will be useful,
+# but WITHOUT ANY WARRANTY; without even the implied warranty of
+# MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the
+# GNU General Public License for more details.
+
+# You should have received a copy of the GNU General Public License
+# along with this program.  If not, see <http://www.gnu.org/licenses/>.
+
+
+if test "$VERBOSE" = yes; then
+  set -x
+  chroot --version
+fi
+
+. $srcdir/test-lib.sh
+
+fail=0
+
+# These tests verify exact status of internal failure; since none of
+# them actually run a command, we don't need root privileges
+chroot # missing argument
+test $? = 125 || fail=1
+chroot --help --version # too many options
+test $? = 125 || fail=1
+chroot --- / true # unknown option
+test $? = 125 || fail=1
+
+# chroot("/") succeeds for non-root users on some systems, but not all.
+if chroot / true ; then
+  chroot / sh -c 'exit 2' # exit status propagation
+  test $? = 2 || fail=1
+  chroot / . # invalid command
+  test $? = 126 || fail=1
+  chroot / ... # no such command
+  test $? = 127 || fail=1
+else
+  test $? = 125 || fail=1
+fi
+
+Exit $fail
diff --git a/tests/misc/env b/tests/misc/env
new file mode 100755 (executable)
index 0000000..a3d435b
--- /dev/null
@@ -0,0 +1,116 @@
+#!/bin/sh
+# Verify behavior of env.
+
+# Copyright (C) 2009 Free Software Foundation, Inc.
+
+# This program is free software: you can redistribute it and/or modify
+# it under the terms of the GNU General Public License as published by
+# the Free Software Foundation, either version 3 of the License, or
+# (at your option) any later version.
+
+# This program is distributed in the hope that it will be useful,
+# but WITHOUT ANY WARRANTY; without even the implied warranty of
+# MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the
+# GNU General Public License for more details.
+
+# You should have received a copy of the GNU General Public License
+# along with this program.  If not, see <http://www.gnu.org/licenses/>.
+
+
+if test "$VERBOSE" = yes; then
+  set -x
+  env --version
+fi
+
+. $srcdir/test-lib.sh
+
+fail=0
+
+# Verify clearing the environment
+a=1
+export a
+env - > out || fail=1
+test -s out && fail=1
+env -i > out || fail=1
+test -s out && fail=1
+env -u a -i -u a -- > out || fail=1
+test -s out && fail=1
+env -i -- a=b > out || fail=1
+echo a=b > exp || framework_failure
+compare exp out || fail=1
+
+# These tests verify exact status of internal failure.
+env --- # unknown option
+test $? = 125 || fail=1
+env -u # missing option argument
+test $? = 125 || fail=1
+env sh -c 'exit 2' # exit status propagation
+test $? = 2 || fail=2
+env . # invalid command
+test $? = 126 || fail=1
+env ... # no such command
+test $? = 127 || fail=1
+
+# Cygwin requires a minimal environment to launch new processes, so a
+# subsequent env will show more than just what we set.  Hence, it is
+# more portable to grep that our desired changes took place.
+if env | grep '^ENV_TEST' >/dev/null ; then
+  skip_test_ "environment has potential interference from ENV_TEST*"
+fi
+
+ENV_TEST1=a
+export ENV_TEST1
+: >out || framework_failure
+env ENV_TEST2= > all || fail=1
+grep '^ENV_TEST' all | LC_ALL=C sort >> out || framework_failure
+env -u ENV_TEST1 ENV_TEST3=c > all || fail=1
+grep '^ENV_TEST' all | LC_ALL=C sort >> out || framework_failure
+env ENV_TEST1=b > all || fail=1
+grep '^ENV_TEST' all | LC_ALL=C sort >> out || framework_failure
+env ENV_TEST2= env > all || fail=1
+grep '^ENV_TEST' all | LC_ALL=C sort >> out || framework_failure
+env -u ENV_TEST1 ENV_TEST3=c env > all || fail=1
+grep '^ENV_TEST' all | LC_ALL=C sort >> out || framework_failure
+env ENV_TEST1=b env > all || fail=1
+grep '^ENV_TEST' all | LC_ALL=C sort >> out || framework_failure
+cat <<EOF >exp || framework_failure
+ENV_TEST1=a
+ENV_TEST2=
+ENV_TEST3=c
+ENV_TEST1=b
+ENV_TEST1=a
+ENV_TEST2=
+ENV_TEST3=c
+ENV_TEST1=b
+EOF
+compare exp out || fail=1
+
+# Use -- to end arguments.
+cat <<EOF >./-i || framework_failure
+#!/bin/sh
+echo pass
+EOF
+chmod +x ./-i || framework_failure
+case `env -i PATH="$PATH" echo good` in
+  good) ;;
+  *) fail=1 ;;
+esac
+case `env -- -i PATH="$PATH" echo fail` in
+  pass) ;;
+  *) fail=1 ;;
+esac
+
+# FIXME - POSIX does not allow this; decide what we want to do
+# cat <<EOF >./c=d || framework_failure
+# #!/bin/sh
+# echo pass
+# EOF
+# chmod +x c=d || framework_failure
+# test "x`env c=d echo fail`" = xfail || fail=1
+# test "x`env -- c=d echo fail`" = xpass || fail=1
+
+# FIXME - decide whether we like this behavior
+# test `env -i -u a=b` = a=b || fail=1
+# env -u '' true || fail=1
+
+Exit $fail
index 1cdd56a..f271eb4 100755 (executable)
@@ -48,11 +48,6 @@ NA LAST NA
 '
 set $tests
 
-if test "$VERBOSE" = yes; then
-  nice --version
-  set -x
-fi
-
 # Require that this test be run at `nice' level 0.
 niceness=`nice`
 if test "$niceness" = 0; then
@@ -76,8 +71,23 @@ while :; do
   test x`nice $args nice 2> /dev/null` = x$expected_result \
     && ok=ok || ok=FAIL fail=1
   test "$VERBOSE" = yes && echo $ok
-  test x`nice $args nice 2> /dev/null` = x$expected_result || fail=1
   shift; shift; shift
 done
 
+# Test negative niceness - command must be run whether or not change happens.
+if test `nice -n -1 nice 2> /dev/null` = 0 ; then
+  # unprivileged user - warn about failure to change
+  nice -n -1 true 2> err || fail=1
+  test -s err || fail=1
+  mv err exp || framework_failure
+  nice --1 true 2> err || fail=1
+  compare exp err || fail=1
+else
+  # superuser - change succeeds
+  nice -n -1 nice 2> err || fail=1
+  test -s err && fail=1
+  test `nice -n -1 nice` = -1 || fail=1
+  test `nice --1 nice` = -1 || fail=1
+fi
+
 Exit $fail
diff --git a/tests/misc/nice-fail b/tests/misc/nice-fail
new file mode 100755 (executable)
index 0000000..da85eb2
--- /dev/null
@@ -0,0 +1,45 @@
+#!/bin/sh
+# Verify that internal failure in nice gives exact status.
+
+# Copyright (C) 2009 Free Software Foundation, Inc.
+
+# This program is free software: you can redistribute it and/or modify
+# it under the terms of the GNU General Public License as published by
+# the Free Software Foundation, either version 3 of the License, or
+# (at your option) any later version.
+
+# This program is distributed in the hope that it will be useful,
+# but WITHOUT ANY WARRANTY; without even the implied warranty of
+# MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the
+# GNU General Public License for more details.
+
+# You should have received a copy of the GNU General Public License
+# along with this program.  If not, see <http://www.gnu.org/licenses/>.
+
+
+if test "$VERBOSE" = yes; then
+  set -x
+  nice --version
+fi
+
+. $srcdir/test-lib.sh
+
+fail=0
+
+# These tests verify exact status of internal failure.
+nice -n 1 # missing command
+test $? = 125 || fail=1
+nice --help --version # too many options
+test $? = 125 || fail=1
+nice --- # unknown option
+test $? = 125 || fail=1
+nice -n 1a # invalid adjustment
+test $? = 125 || fail=1
+nice sh -c 'exit 2' # exit status propagation
+test $? = 2 || fail=2
+nice . # invalid command
+test $? = 126 || fail=1
+nice ... # no such command
+test $? = 127 || fail=1
+
+Exit $fail
diff --git a/tests/misc/su-fail b/tests/misc/su-fail
new file mode 100755 (executable)
index 0000000..bba7d1f
--- /dev/null
@@ -0,0 +1,33 @@
+#!/bin/sh
+# Test su failure cases
+
+# Copyright (C) 2009 Free Software Foundation, Inc.
+
+# This program is free software: you can redistribute it and/or modify
+# it under the terms of the GNU General Public License as published by
+# the Free Software Foundation, either version 3 of the License, or
+# (at your option) any later version.
+
+# This program is distributed in the hope that it will be useful,
+# but WITHOUT ANY WARRANTY; without even the implied warranty of
+# MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the
+# GNU General Public License for more details.
+
+# You should have received a copy of the GNU General Public License
+# along with this program.  If not, see <http://www.gnu.org/licenses/>.
+
+. $srcdir/test-lib.sh
+require_built_ su
+
+if test "$VERBOSE" = yes; then
+  set -x
+  su --version
+fi
+
+# Very little that we can test without a root password
+su --- / true # unknown option
+test $? = 125 || fail=1
+su no_such_user
+test $? = 125 || fail=1
+
+Exit $fail