Source-Changes-HG archive

[Date Prev][Date Next][Thread Prev][Thread Next][Date Index][Thread Index][Old Index]

[src/trunk]: src/tests/usr.bin/realpath Add ATF tests for realpath(1)



details:   https://anonhg.NetBSD.org/src/rev/ee407734586f
branches:  trunk
changeset: 368603:ee407734586f
user:      kre <kre%NetBSD.org@localhost>
date:      Thu Jul 21 09:52:48 2022 +0000

description:
Add ATF tests for realpath(1)

Note that realpath can act differently for root than for other users
(where an ordinary user will see EACCESS root just barrels right through).

The tests adapt themselves, when run as root, less error cases can be
tested than when run as some other user.

diffstat:

 distrib/sets/lists/tests/mi          |    6 +-
 etc/mtree/NetBSD.dist.tests          |    3 +-
 tests/usr.bin/Makefile               |    4 +-
 tests/usr.bin/realpath/Makefile      |    9 +
 tests/usr.bin/realpath/t_realpath.sh |  670 +++++++++++++++++++++++++++++++++++
 5 files changed, 688 insertions(+), 4 deletions(-)

diffs (truncated from 742 to 300 lines):

diff -r f1b7228fff36 -r ee407734586f distrib/sets/lists/tests/mi
--- a/distrib/sets/lists/tests/mi       Thu Jul 21 09:51:53 2022 +0000
+++ b/distrib/sets/lists/tests/mi       Thu Jul 21 09:52:48 2022 +0000
@@ -1,4 +1,4 @@
-# $NetBSD: mi,v 1.1216 2022/07/05 22:50:41 rillig Exp $
+# $NetBSD: mi,v 1.1217 2022/07/21 09:52:48 kre Exp $
 #
 # Note: don't delete entries from here - mark them as "obsolete" instead.
 #
@@ -6254,6 +6254,10 @@
 ./usr/tests/usr.bin/pwhash/Atffile                     tests-usr.bin-tests     compattestfile,atf
 ./usr/tests/usr.bin/pwhash/Kyuafile                    tests-usr.bin-tests     compattestfile,atf,kyua
 ./usr/tests/usr.bin/pwhash/t_pwhash                    tests-usr.bin-tests     compattestfile,atf
+./usr/tests/usr.bin/realpath                           tests-usr.bin-tests     compattestfile,atf
+./usr/tests/usr.bin/realpath/Atffile                   tests-usr.bin-tests     compattestfile,atf
+./usr/tests/usr.bin/realpath/Kyuafile                  tests-usr.bin-tests     compattestfile,atf,kyua
+./usr/tests/usr.bin/realpath/t_realpath                        tests-usr.bin-tests     compattestfile,atf
 ./usr/tests/usr.bin/rump_server                                tests-usr.bin-tests     compattestfile,atf
 ./usr/tests/usr.bin/rump_server/Atffile                        tests-usr.bin-tests     compattestfile,atf
 ./usr/tests/usr.bin/rump_server/Kyuafile               tests-usr.bin-tests     compattestfile,atf,kyua
diff -r f1b7228fff36 -r ee407734586f etc/mtree/NetBSD.dist.tests
--- a/etc/mtree/NetBSD.dist.tests       Thu Jul 21 09:51:53 2022 +0000
+++ b/etc/mtree/NetBSD.dist.tests       Thu Jul 21 09:52:48 2022 +0000
@@ -1,4 +1,4 @@
-#      $NetBSD: NetBSD.dist.tests,v 1.192 2022/05/22 17:55:08 rillig Exp $
+#      $NetBSD: NetBSD.dist.tests,v 1.193 2022/07/21 09:52:48 kre Exp $
 
 ./usr/libdata/debug/usr/tests
 ./usr/libdata/debug/usr/tests/atf
@@ -456,6 +456,7 @@
 ./usr/tests/usr.bin/pr
 ./usr/tests/usr.bin/printf
 ./usr/tests/usr.bin/pwhash
+./usr/tests/usr.bin/realpath
 ./usr/tests/usr.bin/rump_server
 ./usr/tests/usr.bin/sdiff
 ./usr/tests/usr.bin/sed
diff -r f1b7228fff36 -r ee407734586f tests/usr.bin/Makefile
--- a/tests/usr.bin/Makefile    Thu Jul 21 09:51:53 2022 +0000
+++ b/tests/usr.bin/Makefile    Thu Jul 21 09:52:48 2022 +0000
@@ -1,4 +1,4 @@
-#      $NetBSD: Makefile,v 1.36 2022/05/22 17:55:08 rillig Exp $
+#      $NetBSD: Makefile,v 1.37 2022/07/21 09:52:48 kre Exp $
 #
 
 .include <bsd.own.mk>
@@ -8,7 +8,7 @@
 TESTS_SUBDIRS= awk basename bzip2 cc cmp compress config cpio col cut \
                diff dirname find fstat gdb grep gzip id indent \
                infocmp jot ld locale m4 make mixerctl mkdep nbperf \
-               netpgpverify patch pkill pr printf pwhash rump_server \
+               netpgpverify patch pkill pr printf pwhash realpath rump_server \
                shmif_dumpbus sdiff sed sort tar tmux tr unifdef uniq \
                vmstat xlint ztest
 
diff -r f1b7228fff36 -r ee407734586f tests/usr.bin/realpath/Makefile
--- /dev/null   Thu Jan 01 00:00:00 1970 +0000
+++ b/tests/usr.bin/realpath/Makefile   Thu Jul 21 09:52:48 2022 +0000
@@ -0,0 +1,9 @@
+# $NetBSD: Makefile,v 1.1 2022/07/21 09:52:49 kre Exp $
+
+.include <bsd.own.mk>
+
+TESTSDIR=       ${TESTSBASE}/usr.bin/realpath
+
+TESTS_SH=      t_realpath
+
+.include <bsd.test.mk>
diff -r f1b7228fff36 -r ee407734586f tests/usr.bin/realpath/t_realpath.sh
--- /dev/null   Thu Jan 01 00:00:00 1970 +0000
+++ b/tests/usr.bin/realpath/t_realpath.sh      Thu Jul 21 09:52:48 2022 +0000
@@ -0,0 +1,670 @@
+# $NetBSD: t_realpath.sh,v 1.1 2022/07/21 09:52:49 kre Exp $
+#
+# Copyright (c) 2022 The NetBSD Foundation, Inc.
+# All rights reserved.
+#
+# Redistribution and use in source and binary forms, with or without
+# modification, are permitted provided that the following conditions
+# are met:
+# 1. Redistributions of source code must retain the above copyright
+#    notice, this list of conditions and the following disclaimer.
+# 2. Redistributions in binary form must reproduce the above copyright
+#    notice, this list of conditions and the following disclaimer in the
+#    documentation and/or other materials provided with the distribution.
+#
+# THIS SOFTWARE IS PROVIDED BY THE NETBSD FOUNDATION, INC. AND CONTRIBUTORS
+# ``AS IS'' AND ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED
+# TO, THE IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR
+# PURPOSE ARE DISCLAIMED.  IN NO EVENT SHALL THE FOUNDATION OR CONTRIBUTORS
+# BE LIABLE FOR ANY DIRECT, INDIRECT, INCIDENTAL, SPECIAL, EXEMPLARY, OR
+# CONSEQUENTIAL DAMAGES (INCLUDING, BUT NOT LIMITED TO, PROCUREMENT OF
+# SUBSTITUTE GOODS OR SERVICES; LOSS OF USE, DATA, OR PROFITS; OR BUSINESS
+# INTERRUPTION) HOWEVER CAUSED AND ON ANY THEORY OF LIABILITY, WHETHER IN
+# CONTRACT, STRICT LIABILITY, OR TORT (INCLUDING NEGLIGENCE OR OTHERWISE)
+# ARISING IN ANY WAY OUT OF THE USE OF THIS SOFTWARE, EVEN IF ADVISED OF THE
+# POSSIBILITY OF SUCH DAMAGE.
+#
+
+# ===========================================================
+#
+# Test data and expected results
+
+# Note that the empty line calls realpath with no file arg
+existing='.
+
+../Dir/StdOut
+./S1
+./S1/../S4
+./Snr/../S4
+S1/S2/File
+S1/S3/Link
+Snr/HoHo
+Snr/Link
+L
+/
+/bin
+Self
+Self/
+S4/S1/'
+
+exist_results='Dir
+Dir
+Dir/StdOut
+Dir/S1
+Dir/S4
+Dir/S4
+Dir/S1/S2/File
+Dir/S1/S2/File
+Dir/Snr/HoHo
+Dir/S1
+Dir/StdOut
+/
+/bin
+Dir
+Dir
+Dir/S1'
+
+exist_root_only='Snx/HaHa
+Snx/Link'
+
+exist_root_results='Dir/Snx/HaHa
+Dir/S1/S2/File'
+
+nofile='-
+trash
+Snr/Haha
+T1
+T2
+T3
+T4
+T5
+../Dir/T2
+../Dir/T3
+/nonsense
+/bin/../nonsense
+./Self/Self/Self/Self/S1/../Self/../Dir/Self/T1
+Self/nonsense'
+
+nofile_results='Dir/-
+Dir/trash
+Dir/Snr/Haha
+Dir/NoSuchFile
+Dir/S1/NoSuchFile
+Dir/S1/NoSuchFile
+Dir/S1/S2/NoSuchFile
+Dir/S1/S2/NoSuchFile
+Dir/S1/NoSuchFile
+Dir/S1/NoSuchFile
+/nonsense
+/nonsense
+Dir/NoSuchFile
+Dir/nonsense'
+
+always_fail='StdOut/
+StdOut/../StdErr
+Loop
+S1/S5/Link
+Loop/../StdOut
+BigLoop
+U1
+U2
+U3
+U4
+U5
+U6
+U7
+U8
+U9
+T1/NoSuchFile
+T1/../NoSuchFile
+U9/../NoSuchFile
+U9/../StdOut'
+
+
+# ===========================================================
+# Helper functions
+#
+
+# Create the test environment
+setup()
+{
+       atf_require_prog /usr/bin/mktemp
+       atf_require_prog /bin/ln
+       atf_require_prog /bin/cp
+       atf_require_prog /bin/mkdir
+       atf_require_prog /bin/chmod
+
+       DIR=${PWD}/$(mktemp -d Dir.XXXXX) ||
+               atf_fail "Did not make test directory"
+       cd "${DIR}" || atf_fail "Unable to cd $DIR"
+
+       ID=$( set -- $( type id ) && test "$1" = id && test "$2" = is &&
+               test $# -eq 3 && printf %s "$3"  || printf no-id-program)
+
+       mkdir Dir && cd Dir                     || atf_fail "enter Dir"
+
+       >StdOut                                 || atf_fail "setup StdOut"
+       >StdErr                                 || atf_fail "setup StdErr"
+       ln -s ../Dir Dir                        || atf_fail "setup Dir"
+       ln -s Loop Loop                         || atf_fail "setup Loop"
+       ln -s . Self                            || atf_fail "setup Self"
+       mkdir S1 S1/S2 S1/S3 S4 S4/S5           || atf_fail "setup subdirs"
+       echo S1/S2/File > S1/S2/File            || atf_fail "setup File"
+       ln -s ../S2/File S1/S3/Link             || atf_fail "setup S3/Link"
+       ln -s ../S1 S4/S1                       || atf_fail "setup S4/S1"
+       ln -s StdOut L1                         || atf_fail "setup L1"
+       ln -s L1 L2                             || atf_fail "setup L2"
+       ln -s ../L2 S1/L3                       || atf_fail "setup L3"
+       ln -s ../L3 S1/S2/L4                    || atf_fail "setup L4"
+       ln -s ../S2/L4 S1/S3/L5                 || atf_fail "setup L5"
+       ln -s S1/S3/L5 L                        || atf_fail "setup L"
+       ln -s ${PWD}/S1 S4/PWDS1                || atf_fail "setup PWDS1"
+       ln -s ${PWD}/S9 S4/PWDS9                || atf_fail "setup PWDS9"
+       ln -s ${PWD}/S9/File S4/PWDS9F          || atf_fail "setup PWDS9F"
+       ln -s ../S4/BigLoop S1/BigLoop          || atf_fail "setup S1/BigLoop"
+       ln -s ../BigLoop S4/BigLoop             || atf_fail "setup S4/BigLoop"
+       ln -s "${DIR}"/Dir/S1/BigLoop BigLoop   || atf_fail "setup BigLoop"
+       mkdir Snx                               || atf_fail "setup Snx"
+       cp /dev/null Snx/HaHa                   || atf_fail "setup Snx/HaHa"
+       ln -s "${DIR}"/Dir/S1/S2/File Snx/Link  || atf_fail "setup Snx/Link"
+       mkdir Snr                               || atf_fail "setup Snr"
+       cp /dev/null Snr/HoHo                   || atf_fail "setup Snr/HoHo"
+       ln -s "${DIR}"/Dir/S4/PWDS1 Snr/Link    || atf_fail "setup Snr/Link"
+       ln -s ../Snx/HaHa Snr/HaHa              || atf_fail "setup HaHa"
+       ln -s "${DIR}"/Dir/NoSuchFile T1        || atf_fail "setup T1"
+       ln -s "${DIR}"/Dir/S1/NoSuchFile T2     || atf_fail "setup T2"
+       ln -s S1/NoSuchFile T3                  || atf_fail "setup T3"
+       ln -s "${DIR}"/Dir/S1/S2/NoSuchFile T4  || atf_fail "setup T4"
+       ln -s S1/S2/NoSuchFile T5               || atf_fail "setup T5"
+       ln -s "${DIR}"/Dir/StdOut/CannotExist T6 || atf_fail "setup T6"
+       ln -s "${DIR}"/Dir/NoDir/WhoKnows U1    || atf_fail "setup U1"
+       ln -s "${DIR}"/Dir/S1/NoDir/WhoKnows U2 || atf_fail "setup U2"
+       ln -s "${DIR}"/Dir/S1/S2/NoDir/WhoKnows U3 || atf_fail "setup U3"
+       ln -s "${DIR}"/Dir/S1/../NoDir/WhoKnows U4 || atf_fail "setup U4"
+       ln -s "${DIR}"/Dir/NoDir/../StdOut U5   || atf_fail "setup U5"
+       ln -s NoDir/../StdOut U6                || atf_fail "setup U6"
+       ln -s S1/NoDir/../../StdOut U7          || atf_fail "setup U7"
+       ln -s "${DIR}"/Dir/Missing/NoDir/WhoKnows U8 || atf_fail "setup U8"
+       ln -s "${DIR}"/Dir/Missing/NoDir/../../StdOut U9 || atf_fail "setup U9"
+       chmod a+r,a-x Snx                       || atf_fail "setup a-x "
+       chmod a+x,a-r Snr                       || atf_fail "setup a-r"
+}
+
+# ATF will remove all the files we made, just ensure perms are OK
+cleanup()
+{
+       chmod -R u+rwx .
+       return 0
+}
+
+run_tests_pass()
+{
+       opt=$1
+       tests=$2
+       results=$3
+
+       FAILS=
+       FAILURES=0
+       T=0
+
+       while [ "${#tests}" -gt 0 ]
+       do
+               FILE=${tests%%$'\n'*}
+               EXP=${results%%$'\n'*}
+
+               tests=${tests#"${FILE}"};       tests=${tests#$'\n'}
+               results=${results#"${EXP}"};    results=${results#$'\n'}
+
+               test -z "${EXP}" && atf_fail "Too few results (test botch)"
+
+               T=$(( $T + 1 ))
+
+               GOT=$(realpath $opt -- ${FILE:+"${FILE}"})
+               STATUS=$?
+
+               case "${GOT}" in
+               '')     ;;              # nothing printed, deal with that below
+
+               /*)                     # Full Path (what we want)



Home | Main Index | Thread Index | Old Index