From cc2fb2a5c782417434f26ebc64322b4c589a32ab Mon Sep 17 00:00:00 2001 From: Rob Landley Date: Sun, 22 May 2022 14:45:57 -0500 Subject: [PATCH] test -x and friends should test access, not permission bit. chmod u-x but g+x doesn't let you access a file. Presumably selinux can also interfere, so use access() instead. Update tests to pass TEST_HOST. Add a couple more at the end. --- tests/test.test | 18 ++++++++++++++++-- toys/posix/test.c | 8 ++++---- 2 files changed, 20 insertions(+), 6 deletions(-) diff --git a/tests/test.test b/tests/test.test index 1295be41..1ea52713 100644 --- a/tests/test.test +++ b/tests/test.test @@ -62,9 +62,12 @@ for i in uu+s gg+s k+t; do testcmd "-${i:0:1}" "-${i:0:1} walrus && echo yes" "yes\n" "" "" done # test each ugo+rwx bit position individually +XX=no for i in 1 10 100; do for j in x w r; do chmod $i walrus - testcmd "-$j $i" "-$j walrus && echo yes" "yes\n" "" "" + + [ $i == 100 ] && XX=yes + testcmd "-$j $i" "-$j walrus && echo yes || echo no" "$XX\n" "" "" i=$((i<<1)) done; done rm -f walrus @@ -94,12 +97,14 @@ testing "-ge" "arith_test -ge" "eg" "" "" testing "-lt" "arith_test -lt" "l" "" "" testing "-le" "arith_test -le" "le" "" "" +testing "positional" "test -a == -a && echo yes" "yes\n" "" "" +testing "! stacks" 'test \! \! \! \! 2 -eq 2 && echo yes' "yes\n" "" "" + # test ! = -o a # test ! \( = -o a \) # test \( ! = \) -o a # test \( \) -#testing "" "[ -a -eq -a ] && echo yes" "yes\n" "" "" # -e == -a # -e == -a -o -d != -o @@ -111,3 +116,12 @@ testing "-le" "arith_test -le" "le" "" "" # // x -o ( x -a x ) -a x -o x # trailing ! and ( +# test \( ! ! ! -e \) \) +# test \( \( "" \) -a "" \) -a "" +# test ! +# test \( \) == \) \) -a x + +# test \( "" \) -a \) == \) +# test \( "x" \) -a \) == \) +# test -e == -a +# test \( "" \) diff --git a/toys/posix/test.c b/toys/posix/test.c index 3fadf3ad..b1f119c4 100644 --- a/toys/posix/test.c +++ b/toys/posix/test.c @@ -20,8 +20,8 @@ config TEST --- Tests with a single argument (after the option): PATH is/has: -b block device -f regular file -p fifo -u setuid bit - -c char device -g setgid -r read bit -w write bit - -d directory -h symlink -S socket -x execute bit + -c char device -g setgid -r readable -w writable + -d directory -h symlink -S socket -x executable -e exists -L symlink -s nonzero size -k sticky bit STRING is: -n nonzero size -z zero size (STRING by itself implies -n) @@ -77,9 +77,9 @@ static int do_test(char **args, int *count) if (-1 != (i = stridx("hLbcdefgkpSusxwr", c))) { struct stat st; - // stat or lstat, then handle rwx and s + if (i>=13) return !access(args[1], 1<<(i-13)); + // stat or lstat, check s if (-1 == ((i<2) ? lstat : stat)(args[1], &st)) return 0; - if (i>=13) return !!(st.st_mode&(0111<<(i-13))); if (c == 's') return !!st.st_size; // otherwise 1<<32 == 0 // handle file type checking and SUID/SGID -- 2.39.2