Files
rdlink/test/totest.sh
2022-03-23 10:04:05 +01:00

208 lines
4.9 KiB
Bash

#!/usr/bin/env bash
########## Competition Block ##########
# tc = test compete
_tc_tmp="${test_dir:-"/tmp"}/tmp_compete"
# Each competition link is given to both tools
# and the output is compared
#
# Array format:
# [0] = Title
# [1] = path for competition
# [2] = path for competition
# ...
# Compete test suites (arrays)
compete_canonicalize=( "Canonicalize invalid path"
"///tmp//./b"
"//tmp//./b/.."
"//tmp//./b/."
"//tmp//./b/"
"///notthere//...../help/."
)
compete_relative=( "Resolving of relative links"
# Invalid
"nofile_l"
"nopath_l"
# Valid
"dir_1/file" # Valid relative link to path with relative element
)
compete_all=( "Test - everything starting from /"
/**/*
)
tocompete_init() {
# initialize custom test structure
{
mkdir -p "${_tc_tmp}"
cd "${_tc_tmp}"
# compete_links
touch "${_tc_tmp}/a"
ln -s "${_tc_tmp}/a" "${_tc_tmp}/a 2"
ln -s "${_tc_tmp}/a 2" "${_tc_tmp}/a 3"
mkdir -p "dir_1"
mkdir -p "dir_2/dir_22"
touch "dir_2/dir_22/file"
ln -s "dir_2/dir_22" "dir_3_l"
ln -s "../dir_3_l/file" "dir_1/file"
# compete_no_permission
mkdir -p "${_tc_tmp}/nopermdir"
chmod 400 "${_tc_tmp}/nopermdir"
ln -s "nopermdir" "${_tc_tmp}/nopermdirl"
ln -s "/" "${_tc_tmp}/lroot"
ln -s "/root" "${_tc_tmp}/lroothome"
# compete_relative
ln -s "../nofile" "${_tc_tmp}/nofile_l"
ln -s "../nodir/nofile" "${_tc_tmp}/nopath_l"
#echo "rl: " && readlink "${_tc_tmp}/lnoperm"
#ls -l "${_tc_tmp}"
}
# Base directory for the test
cd "${_tc_tmp}"
# Compete test arrays with "dynamic" cases need to be inside the init function
# e.g. "$(cd ../test && pwd)/file" - base directory must be set first
# e.g. "${_tc_tmp}/"* - _tc_tmp must be populated first
compete_invalid=( "Invalid files with and without valid path"
"${_tc_tmp}/invalid_file"
"/invalid_file"
"/invalid_direcotry/invalidfile"
"-v" # Not recommended file naming
"../-v" # Not recommended file naming
"-v/.." # Not recommended file naming
)
compete_links=( "Test - Valid links"
"${_tc_tmp}/a 3" # slink chain a3 -> a2 -> a
"${_tc_tmp}/a"*
"/dev/stdin"
#"/dev/stdout" skip - Always different
#"/dev/fd" # Test skip - /dev/fd is different on every call
#"/etc/mtab" # skip - Always different
#"/proc/mounts" # skip - Always different
#"/proc/net/"* # skip - Always different
)
compete_no_permission=( "No permission to enter directory (direct and link)"
"nopermdir"*
"lroot"*
"/"
"/root"
"/root/"
"/root/."
"/root/.."
#"/proc/"**/cwd # special - no permission for links
)
# Add tests to global test array from test_rdlink
tocompete+=(
compete_canonicalize
compete_invalid
compete_relative
compete_no_permission
compete_links
#compete_all
)
}
tocompete_clean() {
rm -rf "${_tc_tmp}"
}
########## Assertion Block ##########
# ta = test assert
_ta_tmp="${test_dir:-"/tmp"}/tmp_assert"
# Assertion string compare test arrays
#
# Array format
# [0] = Title
# [1] input == [2] expected
# [3] input == [4] expected
# ...
toassert_init() {
{
mkdir -p "${_ta_tmp}"
}
# Base directory for the test
cd "${_ta_tmp}"
# Assert test arrays with "dynamic" cases need to be inside the init function
# e.g. "$(cd ../test && pwd)/file" - base directory must be set first
# e.g. "${_tc_tmp}/"* - _tc_tmp must be populated first
assert_invalid_files=( "Assert - invalid files"
"${_ta_tmp}/missing_file" "${_ta_tmp}/missing_file"
"${_ta_tmp}/missd/missf" ""
"${_ta_tmp}/miss c" "${_ta_tmp}/miss c"
"rel_a" "${_ta_tmp}/rel_a"
"../rel_b" "$(cd ".." && pwd)/rel_b"
)
# Add test arrays to global test array from test_rdlink
toassert+=(
assert_invalid_files
)
}
toassert_clean() {
rm -rf "${_ta_tmp}"
}
########## Common data and functions ##########
#
# [0] exclude regex [1] Reason for exclution
# [2] exclude regex [3] Reason for exclution
exclude_path=(
"/dev/fd$" "Different on every call"
"/dev/stdout$" "Always different in pipes"
"/etc/mtab$" "Different on every call"
"/proc/mounts$" "Different on every call"
"/proc/net$" "Different on every call"
"/proc/net/.*" "Different on every call"
"/proc/self/fd/(1|3|255)" "Different on every call"
"/proc/self$" "Different on every call"
"/proc/self/(attr|fdinfo|map_files|net|ns|task)/.*" "Different on every call"
"/proc/thread-self$" "Different on every call"
)
toexclude() {
local path=
local exclude=
for path in "${exclude_path[@]}" ; do
if [[ ! "${exclude}" ]] ; then
exclude="${path}"
else
# return reason for exclution
if [[ "${1:-}" =~ ${exclude} ]] ; then
printf "${path}"
return 0
fi
exclude=
fi
done
return 1
}
########## Clean custom test data ##########
#
totest_cleanall() {
tocompete_clean
toassert_clean
}
trap totest_cleanall EXIT