]> gitweb.michael.orlitzky.com - apply-default-acl.git/blobdiff - run-tests.sh
safe_open_ex: add a comment about why O_PATH doesn't work.
[apply-default-acl.git] / run-tests.sh
index 900ac1617b1c29e0415287e0dac0781e84b0eb2d..07ad496584faa680cde5992e2fde03dee698e628 100755 (executable)
@@ -4,6 +4,8 @@
 # Exit codes
 #
 
+EXIT_SUCCESS=0
+
 # Exit with this when a test fails.
 EXIT_FAILURE=1
 
@@ -21,11 +23,16 @@ USERS=( bin daemon )
 
 # Check to see if the above users exist. If not, bail.
 for idx in $( seq 0 $((${#USERS[@]} - 1)) ); do
-    id ${USERS[idx]} >/dev/null 2>&1 || exit $EXIT_MISSING_USERS
+    id "${USERS[idx]}" >/dev/null 2>&1
+
+    if [ $? -ne $EXIT_SUCCESS ]; then
+       echo "Error: missing test user ${USERS[idx]}." 1>&2
+       exit $EXIT_MISSING_USERS
+    fi
 done
 
 # The program name.
-BIN=src/apply-default-acl
+BIN=$(realpath src/apply-default-acl)
 
 # The directory where we'll do all the ACL manipulation.
 TESTDIR=test
@@ -79,7 +86,7 @@ other::r--
 EOF
 )
 
-ACTUAL=`getfacl --omit-header "${TARGET}"`
+ACTUAL=$(getfacl --omit-header "${TARGET}")
 compare
 
 # Do the same thing as the last test, except with an extended ACL.
@@ -102,7 +109,7 @@ other::r--
 EOF
 )
 
-ACTUAL=`getfacl --omit-header "${TARGET}"`
+ACTUAL=$(getfacl --omit-header "${TARGET}")
 compare
 
 
@@ -124,7 +131,7 @@ other::r--
 EOF
 )
 
-ACTUAL=`getfacl --omit-header "${TARGET}"`
+ACTUAL=$(getfacl --omit-header "${TARGET}")
 compare
 
 
@@ -150,7 +157,7 @@ default:other::r-x
 EOF
 )
 
-ACTUAL=`getfacl --omit-header "${TARGET}"`
+ACTUAL=$(getfacl --omit-header "${TARGET}")
 compare
 
 
@@ -169,7 +176,7 @@ other::r--
 EOF
 )
 
-ACTUAL=`getfacl --omit-header "${TARGET}"`
+ACTUAL=$(getfacl --omit-header "${TARGET}")
 compare
 
 
@@ -193,7 +200,7 @@ other::r-x
 EOF
 )
 
-ACTUAL=`getfacl --omit-header "${TARGET}"`
+ACTUAL=$(getfacl --omit-header "${TARGET}")
 compare
 
 
@@ -224,7 +231,7 @@ other::r-x
 EOF
 )
 
-ACTUAL=`getfacl --omit-header "${TARGET}"`
+ACTUAL=$(getfacl --omit-header "${TARGET}")
 compare
 
 
@@ -247,7 +254,7 @@ other::r--
 EOF
 )
 
-ACTUAL=`getfacl --omit-header "${TARGET}"`
+ACTUAL=$(getfacl --omit-header "${TARGET}")
 compare
 
 
@@ -267,7 +274,7 @@ other::r-x
 EOF
 )
 
-ACTUAL=`getfacl --omit-header "${TARGET}"`
+ACTUAL=$(getfacl --omit-header "${TARGET}")
 compare
 
 
@@ -292,7 +299,7 @@ other::r-x
 EOF
 )
 
-ACTUAL=`getfacl --omit-header "${TARGET}"`
+ACTUAL=$(getfacl --omit-header "${TARGET}")
 compare
 
 
@@ -316,7 +323,7 @@ other::r--
 EOF
 )
 
-ACTUAL=`getfacl --omit-header "${TARGET}"`
+ACTUAL=$(getfacl --omit-header "${TARGET}")
 compare
 
 
@@ -341,7 +348,7 @@ other::r--
 EOF
 )
 
-ACTUAL=`getfacl --omit-header "${TARGET}"/bar/quux`
+ACTUAL=$(getfacl --omit-header "${TARGET}"/bar/quux)
 compare
 
 
@@ -370,7 +377,7 @@ default:other::r--
 EOF
 )
 
-ACTUAL=`getfacl --omit-header "${TARGET}"/bar`
+ACTUAL=$(getfacl --omit-header "${TARGET}"/bar)
 compare
 
 
@@ -400,7 +407,7 @@ default:other::r-x
 EOF
 )
 
-ACTUAL=`getfacl --omit-header "${TARGET}"`
+ACTUAL=$(getfacl --omit-header "${TARGET}")
 compare
 
 
@@ -430,7 +437,7 @@ default:other::r-x
 EOF
 )
 
-ACTUAL=`getfacl --omit-header "${TARGET}"`
+ACTUAL=$(getfacl --omit-header "${TARGET}")
 compare
 
 
@@ -454,7 +461,7 @@ other::r--
 EOF
 )
 
-ACTUAL=`getfacl --omit-header "${TARGET}"`
+ACTUAL=$(getfacl --omit-header "${TARGET}")
 compare
 
 
@@ -476,7 +483,7 @@ other::r-x
 EOF
 )
 
-ACTUAL=`getfacl --omit-header "${TARGET}"`
+ACTUAL=$(getfacl --omit-header "${TARGET}")
 compare
 
 
@@ -498,7 +505,7 @@ other::r-x
 EOF
 )
 
-ACTUAL=`getfacl --omit-header "${TARGET}"`
+ACTUAL=$(getfacl --omit-header "${TARGET}")
 compare
 
 
@@ -520,7 +527,7 @@ other::r-x
 EOF
 )
 
-ACTUAL=`getfacl --omit-header "${TARGET}"`
+ACTUAL=$(getfacl --omit-header "${TARGET}")
 compare
 
 
@@ -547,7 +554,7 @@ other::r-x
 EOF
 )
 
-ACTUAL=`getfacl --omit-header "${TARGET}"`
+ACTUAL=$(getfacl --omit-header "${TARGET}")
 compare
 
 
@@ -573,7 +580,7 @@ other::r-x
 EOF
 )
 
-ACTUAL=`getfacl --omit-header "${TARGET}"`
+ACTUAL=$(getfacl --omit-header "${TARGET}")
 compare
 
 
@@ -601,5 +608,261 @@ other::r--
 EOF
 )
 
-ACTUAL=`getfacl --omit-header "${TARGET}"`
+ACTUAL=$(getfacl --omit-header "${TARGET}")
+compare
+
+
+# Same as test #2, except we pass multiple files on the command
+# line and check the result of the first one.
+TESTNUM=23
+setfacl -d -m user::r--     "${TESTDIR}"
+setfacl -d -m group::r--    "${TESTDIR}"
+setfacl -d -m other::r--    "${TESTDIR}"
+setfacl -d -m user:${USERS[0]}:rwx "${TESTDIR}"
+DUMMY="${TESTDIR}/dummy"
+touch "${DUMMY}"
+chmod 777 "${DUMMY}"
+touch "${TARGET}"
+chmod 777 "${TARGET}"
+$BIN "${TARGET}" "${DUMMY}"
+
+EXPECTED=$(cat <<EOF
+user::r--
+user:${USERS[0]}:rwx
+group::r--
+mask::rwx
+other::r--
+
+EOF
+)
+
+ACTUAL=$(getfacl --omit-header "${TARGET}")
+compare
+
+
+
+# Same as the previous test with the argument order switched.
+TESTNUM=24
+setfacl -d -m user::r--     "${TESTDIR}"
+setfacl -d -m group::r--    "${TESTDIR}"
+setfacl -d -m other::r--    "${TESTDIR}"
+setfacl -d -m user:${USERS[0]}:rwx "${TESTDIR}"
+DUMMY="${TESTDIR}/dummy"
+touch "${DUMMY}"
+chmod 777 "${DUMMY}"
+touch "${TARGET}"
+chmod 777 "${TARGET}"
+$BIN "${DUMMY}" "${TARGET}"
+
+EXPECTED=$(cat <<EOF
+user::r--
+user:${USERS[0]}:rwx
+group::r--
+mask::rwx
+other::r--
+
+EOF
+)
+
+ACTUAL=$(getfacl --omit-header "${TARGET}")
+compare
+
+
+# If we call apply-default-acl on a single file that does not exist,
+# we get the expected error.
+TESTNUM=25
+ACTUAL=$( "${BIN}" test/nonexistent 2>&1 )
+ACTUAL="${ACTUAL#*: }"
+EXPECTED="test/nonexistent: No such file or directory"
+compare
+
+# Same as the previous test, but with --recursive.
+TESTNUM=26
+ACTUAL=$( "${BIN}" --recursive test/nonexistent 2>&1 )
+ACTUAL="${ACTUAL#*: }"
+EXPECTED="test/nonexistent: No such file or directory"
+compare
+
+# If we call apply-default-acl on more than one file, it should report any
+# that don't exist (but proceed to operate on the others).
+TESTNUM=27
+DUMMY1="${TESTDIR}/dummy1"
+DUMMY2="${TESTDIR}/dummy2"
+touch "${DUMMY1}" "${DUMMY2}"
+ACTUAL=$( "${BIN}" "${DUMMY1}" test/nonexistent "${DUMMY2}" 2>&1 )
+ACTUAL="${ACTUAL#*: }"
+EXPECTED="test/nonexistent: No such file or directory"
+compare
+
+
+# Ensure that symlinks are not followed.
+TESTNUM=28
+TARGET="${TESTDIR}/foo"
+LINK2TARGET="${TESTDIR}/foo-sym"
+touch "${TARGET}"
+ln -s "${TARGET#${TESTDIR}/}" "${LINK2TARGET}"
+setfacl --default --modify user:${USERS[0]}:rwx "${TESTDIR}"
+"${BIN}" "${LINK2TARGET}"
+ACTUAL=$( getfacl --omit-header "${TARGET}" )
+EXPECTED=$(cat <<EOF
+user::rw-
+group::r--
+other::r--
+
+EOF
+)
+compare
+
+
+# Ensure that symlinks are not followed in subdirectories
+# (recursively).
+TESTNUM=29
+TARGET="${TESTDIR}/bar"
+touch "${TARGET}"
+mkdir "${TESTDIR}/foo"
+LINK2TARGET="${TESTDIR}/foo/bar-sym"
+ln -s "../bar" "${LINK2TARGET}"
+setfacl --default --modify user:${USERS[0]}:rwx "${TESTDIR}/foo"
+"${BIN}" --recursive "${TESTDIR}/foo"
+ACTUAL=$( getfacl --omit-header "${TARGET}" )
+EXPECTED=$(cat <<EOF
+user::rw-
+group::r--
+other::r--
+
+EOF
+)
+compare
+
+
+# Ensure that hard links are ignored.
+TESTNUM=30
+TARGET="${TESTDIR}/foo"
+LINK2TARGET="${TESTDIR}/bar"
+touch "${TARGET}"
+ln "${TARGET}" "${LINK2TARGET}"
+setfacl --default --modify user:${USERS[0]}:rwx "${TESTDIR}"
+"${BIN}" "${LINK2TARGET}"
+ACTUAL=$( getfacl --omit-header "${TARGET}" )
+EXPECTED=$(cat <<EOF
+user::rw-
+group::r--
+other::r--
+
+EOF
+)
+compare
+
+
+# We should be able to run the tool with a relative path from within a
+# directory that contains a symlink, so long as the relative path
+# doesn't contain one.
+TESTNUM=31
+TARGET="${TESTDIR}/foo/bar"
+LINK2TARGET="${TESTDIR}/baz"
+mkdir -p $(dirname "${TARGET}")
+touch "${TARGET}"
+ln -s foo "${TESTDIR}/baz"
+setfacl --default --modify user:${USERS[0]}:rw $(dirname "${TARGET}")
+pushd "${TESTDIR}/baz" > /dev/null
+"${BIN}" bar
+popd > /dev/null
+ACTUAL=$( getfacl --omit-header "${TARGET}" )
+EXPECTED=$(cat <<EOF
+user::rw-
+user:${USERS[0]}:rw-
+group::r--
+mask::rw-
+other::r--
+
+EOF
+)
+compare
+
+
+# Ensure that symlinks in non-terminal path components are not followed.
+TESTNUM=32
+TARGET="${TESTDIR}/foo/bar/baz"
+LINK2FOO="${TESTDIR}/quux"
+mkdir -p $(dirname "${TARGET}")
+touch "${TARGET}"
+ln -s foo "${LINK2FOO}"
+setfacl --default --modify user:${USERS[0]}:rw $(dirname "${TARGET}")
+"${BIN}" "${LINK2FOO}/bar/baz"
+ACTUAL=$( getfacl --omit-header "${TARGET}" )
+EXPECTED=$(cat <<EOF
+user::rw-
+group::r--
+other::r--
+
+EOF
+)
+compare
+
+
+# Test that our exit code succeeds on a single, normal path.
+TESTNUM=33
+TARGET="${TESTDIR}/foo"
+touch "${TARGET}"
+setfacl --default --modify user:${USERS[0]}:rw "${TESTDIR}"
+"${BIN}" "${TARGET}"
+ACTUAL="$?"
+EXPECTED="0"
+compare
+
+
+# Test that our exit code fails on a symlink.
+TESTNUM=34
+TARGET="${TESTDIR}/bar"
+touch "${TESTDIR}/foo"
+ln -s foo "${TARGET}"
+setfacl --default --modify user:${USERS[0]}:rw "${TESTDIR}"
+"${BIN}" "${TARGET}"
+ACTUAL="$?"
+EXPECTED="1"
+compare
+
+
+# The previous test should "succeed" if we use --recursive. This is
+# buggy, but it's documented.
+TESTNUM=35
+TARGET="${TESTDIR}/bar"
+touch "${TESTDIR}/foo"
+ln -s foo "${TARGET}"
+setfacl --default --modify user:${USERS[0]}:rw "${TESTDIR}"
+"${BIN}" --recursive "${TARGET}"
+ACTUAL="$?"
+EXPECTED="0"
+compare
+
+
+# Test the return value for nonexistent paths.
+TESTNUM=36
+TARGET="${TESTDIR}/foo"
+"${BIN}" "${TARGET}" &>/dev/null
+ACTUAL="$?"
+EXPECTED="1"
+compare
+
+
+# Test that one "failure" exit code overrides two "successes"
+TESTNUM=37
+mkdir "${TESTDIR}/foo"
+ln -s foo "${TESTDIR}/bar"
+mkdir "${TESTDIR}/baz"
+"${BIN}" "${TESTDIR}/foo" "${TESTDIR}/bar" "${TESTDIR}/baz"
+ACTUAL="$?"
+EXPECTED="1"
+compare
+
+
+# And test the buggy behavior again; the previous test should return
+# success (ignoring the failure) when --recursive is used.
+TESTNUM=38
+mkdir "${TESTDIR}/foo"
+ln -s foo "${TESTDIR}/bar"
+mkdir "${TESTDIR}/baz"
+"${BIN}" --recursive "${TESTDIR}"
+ACTUAL="$?"
+EXPECTED="0"
 compare