X-Git-Url: http://gitweb.michael.orlitzky.com/?p=apply-default-acl.git;a=blobdiff_plain;f=run-tests.sh;h=cb04d47cf759f1d44205eb4fab66e4725320f9ad;hp=0ce549c84ca1e6a64a1a7cb9f1dd50d19d3b57e8;hb=6f5f41a8a87ac60de171e18e6d233f34f61c4454;hpb=d89f22c28473cd2e201500af0f970478105c632a diff --git a/run-tests.sh b/run-tests.sh index 0ce549c..cb04d47 100755 --- a/run-tests.sh +++ b/run-tests.sh @@ -1,14 +1,49 @@ #!/bin/bash +# +# Exit codes +# + +EXIT_SUCCESS=0 + +# Exit with this when a test fails. +EXIT_FAILURE=1 + +# We use a few system users in the tests. If these users aren't +# present, we exit with a different (non-EXIT_FAILURE). +EXIT_MISSING_USERS=2 + +# Define the users that we'll use in the tests below. We store the +# names as variables to avoid repeating them everywhere. +# +# WARNING: These must be in alphabetical order; otherwise the getfacl +# output will not match. +# +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 + + 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 +# Will auto-increment. +TESTNUM=0 + acl_reset() { # Remove any ACLs on our test directory and remove its contents. - setfacl --remove-all --recursive "$TESTDIR" + setfacl --remove-all --recursive "${TESTDIR}" + chmod 755 "${TESTDIR}" rm -rf "${TESTDIR}"/* } @@ -18,11 +53,15 @@ compare() { acl_reset else echo "Failure (#${TESTNUM})" - echo "Expected result:" + echo 'Expected result:' + echo '================' echo "${EXPECTED}" - echo "Actual result:" + echo '================' + echo 'Actual result:' + echo '================' echo "${ACTUAL}" - exit 1 + echo '================' + exit $EXIT_FAILURE fi } @@ -33,13 +72,13 @@ mkdir "${TESTDIR}" # When using a minimal ACL, the default user, group, and other # permissions should all be propagated to the mode bits. -TESTNUM=1 +((TESTNUM++)) TARGET="${TESTDIR}"/foo +touch "${TARGET}" +chmod 777 "${TARGET}" setfacl -d -m user::r-- "${TESTDIR}" setfacl -d -m group::r-- "${TESTDIR}" setfacl -d -m other::r-- "${TESTDIR}" -touch "${TARGET}" -chmod 777 "${TARGET}" $BIN "${TARGET}" EXPECTED=$(cat <&1 ) +EXPECTED="test/nonexistent: No such file or directory" +compare + + +# Same as the previous test, but with --recursive. +((TESTNUM++)) +ACTUAL=$( "${BIN}" --recursive test/nonexistent 2>&1 ) +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++)) +DUMMY1="${TESTDIR}/dummy1" +DUMMY2="${TESTDIR}/dummy2" +touch "${DUMMY1}" "${DUMMY2}" +ACTUAL=$( "${BIN}" "${DUMMY1}" test/nonexistent "${DUMMY2}" 2>&1 ) +EXPECTED="test/nonexistent: No such file or directory" +compare + + +# Ensure that symlinks are not followed. +((TESTNUM++)) +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 < /dev/null +"${BIN}" bar +popd > /dev/null +ACTUAL=$( getfacl --omit-header "${TARGET}" ) +EXPECTED=$(cat </dev/null +ACTUAL="$?" +EXPECTED="1" +compare + + +# Test that one "failure" exit code overrides two "successes" +# We need a default ACL on ${TESTDIR} because otherwise we do +# nothing, successfully, on the symlink path. +((TESTNUM++)) +mkdir "${TESTDIR}/foo" +ln -s foo "${TESTDIR}/bar" +mkdir "${TESTDIR}/baz" +setfacl --default --modify user:${USERS[0]}:rw "${TESTDIR}" +"${BIN}" "${TESTDIR}/foo" "${TESTDIR}/bar" "${TESTDIR}/baz" +ACTUAL="$?" +EXPECTED="1" +compare + + +# The failure should prevail when using --recursive, too. +((TESTNUM++)) +mkdir "${TESTDIR}/foo" +ln -s foo "${TESTDIR}/bar" +mkdir "${TESTDIR}/baz" +"${BIN}" --recursive "${TESTDIR}" +ACTUAL="$?" +EXPECTED="1" +compare + + +# We should get "Not a directory" if we stick a trailing slash on the +# end of the path to a file. +((TESTNUM++)) +TARGET="${TESTDIR}/foo" +touch "${TARGET}" +ACTUAL=$( "${BIN}" "${TARGET}/" 2>&1 ) +EXPECTED="${TARGET}/: Not a directory" +compare + + +# We should be a no-op on files contained in directories that have no +# default ACL. +((TESTNUM++)) +TARGET="${TESTDIR}/foo" +touch "${TARGET}" +setfacl --modify user:${USERS[0]}:rw "${TARGET}" +EXPECTED=$( getfacl --omit-header "${TARGET}" ) +"${BIN}" "${TARGET}" +ACTUAL=$( getfacl --omit-header "${TARGET}" ) +compare + + +# We should be a no-op on directories contained in directories that +# have no default ACL (same as the previous test, but with a directory). +((TESTNUM++)) +TARGET="${TESTDIR}/foo" +mkdir "${TARGET}" +setfacl --modify user:${USERS[0]}:rw "${TARGET}" +setfacl --default --modify user:${USERS[0]}:rw "${TARGET}" +EXPECTED=$( getfacl --omit-header "${TARGET}" ) +"${BIN}" --recursive "${TARGET}" +ACTUAL=$( getfacl --omit-header "${TARGET}" ) +compare + + +# Make sure we descend into subdirectories that don't have default ACLs. +((TESTNUM++)) +TARGET="${TESTDIR}/foo/bar/baz" +mkdir -p $(dirname "${TARGET}") +touch "${TARGET}" +touch "${TARGET}-direct" +setfacl --default --modify user:${USERS[0]}:rw $(dirname "${TARGET}") +"${BIN}" "${TARGET}-direct" +EXPECTED=$( getfacl --omit-header "${TARGET}-direct" ) +"${BIN}" --recursive "${TESTDIR}" +ACTUAL=$( getfacl --omit-header "${TARGET}" ) +compare + + +# Ensure that we don't get "error" results for symlinks encountered +# during a recursive traversal. +((TESTNUM++)) +TARGET="${TESTDIR}" +mkdir "${TARGET}/foo" +mkdir "${TARGET}/bar" +ln -s "../foo" "${TARGET}/bar/baz" +setfacl --default --modify user:${USERS[0]}:rw "${TARGET}" +EXPECTED="1" +"${BIN}" --recursive "${TARGET}" +ACTUAL=$? +compare + + +# Ensure that "." works as an argument. +((TESTNUM++)) +TARGET="${TESTDIR}" +mkdir "${TARGET}/foo" +mkdir "${TARGET}/bar" +setfacl --default --modify user:${USERS[0]}:rw "${TARGET}" +"${BIN}" "${TARGET}/foo" +EXPECTED=$( getfacl --omit-header "${TARGET}/foo" ) +pushd "${TARGET}/bar" > /dev/null +"${BIN}" "." +ACTUAL=$( getfacl --omit-header "." ) +popd > /dev/null +compare + +# Ensure that "." works as an argument (recursive). +((TESTNUM++)) +TARGET="${TESTDIR}" +mkdir -p "${TARGET}/foo/baz" +mkdir -p "${TARGET}/bar/baz" +setfacl --default --modify user:${USERS[0]}:rw "${TARGET}" +"${BIN}" --recursive "${TARGET}/foo" +EXPECTED=$( getfacl --omit-header "${TARGET}/foo/baz" ) +pushd "${TARGET}/bar" > /dev/null +"${BIN}" --recursive "." +ACTUAL=$( getfacl --omit-header "./baz" ) +popd > /dev/null +compare + +# Ensure that "./" works as an argument. +((TESTNUM++)) +TARGET="${TESTDIR}" +mkdir "${TARGET}/foo" +mkdir "${TARGET}/bar" +setfacl --default --modify user:${USERS[0]}:rw "${TARGET}" +"${BIN}" "${TARGET}/foo" +EXPECTED=$( getfacl --omit-header "${TARGET}/foo" ) +pushd "${TARGET}/bar" > /dev/null +"${BIN}" "./" +ACTUAL=$( getfacl --omit-header "./" ) +popd > /dev/null +compare + +# Ensure that ".." works as an argument. +((TESTNUM++)) +TARGET="${TESTDIR}" +mkdir "${TARGET}/foo" +mkdir -p "${TARGET}/bar/baz" +setfacl --default --modify user:${USERS[0]}:rw "${TARGET}" +"${BIN}" "${TARGET}/foo" +EXPECTED=$( getfacl --omit-header "${TARGET}/foo" ) +pushd "${TARGET}/bar/baz" > /dev/null +"${BIN}" ".." +ACTUAL=$( getfacl --omit-header ".." ) +popd > /dev/null +compare + +# Ensure that ".." works as an argument (recursive). +((TESTNUM++)) +TARGET="${TESTDIR}" +mkdir -p "${TARGET}/foo/baz" +mkdir -p "${TARGET}/bar/baz" +setfacl --default --modify user:${USERS[0]}:rw "${TARGET}" +"${BIN}" --recursive "${TARGET}/foo" +EXPECTED=$( getfacl --omit-header "${TARGET}/foo/baz" ) +pushd "${TARGET}/bar/baz" > /dev/null +"${BIN}" --recursive ".." +ACTUAL=$( getfacl --omit-header "." ) +popd > /dev/null +compare -ACTUAL=`getfacl --omit-header "${TARGET}"/bar` +# Ensure that "../" works as an argument. +((TESTNUM++)) +TARGET="${TESTDIR}" +mkdir "${TARGET}/foo" +mkdir -p "${TARGET}/bar/baz" +setfacl --default --modify user:${USERS[0]}:rw "${TARGET}" +"${BIN}" "${TARGET}/foo" +EXPECTED=$( getfacl --omit-header "${TARGET}/foo" ) +pushd "${TARGET}/bar/baz" > /dev/null +"${BIN}" "../" +ACTUAL=$( getfacl --omit-header "../" ) +popd > /dev/null compare