From bafe92dad390789f3bab9c0730d79084d65c2cdc Mon Sep 17 00:00:00 2001 From: Andrwe Lord Weber Date: Wed, 10 Jan 2024 00:51:58 +0100 Subject: [PATCH] improve script & test sutie (#44) * privoxy-blocklist: fix handling of debug configuration vs. argument * privoxy-blocklist: show configuration path in help * privoxy-blocklist: fix unset variable when INIT_CONF is missing * privoxy-blocklist: support lists without Adblock-header * tests: run tests with different predefined configs * tests: fix Dockerfile path when running run.sh * tests: add helper function check_in and check_not_in * tests: move helper function to the bottom Signed-off-by: Andrwe Lord Weber --- privoxy-blocklist.sh | 50 +++++++----- tests/configs/debugging.conf | 30 +++++++ tests/configs/url_extended_config.conf | 31 ++++++++ tests/run.sh | 2 +- tests/test_01_root_execute.py | 106 ++++++++++++++++++++----- 5 files changed, 177 insertions(+), 42 deletions(-) create mode 100644 tests/configs/debugging.conf create mode 100644 tests/configs/url_extended_config.conf diff --git a/privoxy-blocklist.sh b/privoxy-blocklist.sh index bd985bd..8fd2b7b 100755 --- a/privoxy-blocklist.sh +++ b/privoxy-blocklist.sh @@ -37,11 +37,12 @@ DEPENDS=('privoxy' 'sed' 'grep' 'bash' 'wget') ###################################################################### function usage() { - echo "${TMPNAME:-this} is a script to convert AdBlockPlus-lists into Privoxy-lists and install them." + get_config_path + echo "${TMPNAME:-This} is a script to convert AdBlockPlus-lists into Privoxy-lists and install them." echo " " echo "Options:" echo " -h: Show this help." - echo " -c: Path to script configuration file. (default = OS specific)" + echo " -c: Path to script configuration file. (default = ${SCRIPTCONF} - OS specific)" echo " -q: Don't give any output." echo " -v 1: Enable verbosity 1. Show a little bit more output." echo " -v 2: Enable verbosity 2. Show a lot more output." @@ -49,6 +50,23 @@ function usage() { echo " -r: Remove all lists build by this script." } +function get_config_path() { + if [ -z "${SCRIPTCONF:-}" ]; then + # script config-file + case "${OS}" in + "Darwin") + SCRIPTCONF="/usr/local/etc/privoxy-blocklist.conf" + ;; + *) + SCRIPTCONF="/etc/privoxy-blocklist.conf" + ;; + esac + if [ -f "/etc/conf.d/privoxy-blacklist" ]; then + SCRIPTCONF="/etc/conf.d/privoxy-blacklist" + fi + fi +} + function prepare() { if [ ${UID} -ne 0 ]; then error -e "Root privileges needed. Exit.\n" @@ -67,18 +85,7 @@ function prepare() { OS="$(uname)" if [ -z "${SCRIPTCONF:-}" ]; then - # script config-file - case "${OS}" in - "Darwin") - SCRIPTCONF="/usr/local/etc/privoxy-blocklist.conf" - ;; - *) - SCRIPTCONF="/etc/privoxy-blocklist.conf" - ;; - esac - if [ -f "/etc/conf.d/privoxy-blacklist" ]; then - SCRIPTCONF="/etc/conf.d/privoxy-blacklist" - fi + get_config_path fi if [[ ! -d "$(dirname "${SCRIPTCONF}")" ]]; then @@ -124,14 +131,14 @@ EOF debug "Can't read ${SCRIPTCONF}. Permission denied." -1 fi - # load script config - _dbg="${DBG:-0}" # shellcheck disable=SC1090 source "${SCRIPTCONF}" - DBG="${_dbg}" + if [ -n "${OPT_DBG:-}" ]; then + DBG="${OPT_DBG}" + fi # load privoxy config # shellcheck disable=SC1090 - if [[ -r "${INIT_CONF}" ]]; then + if [[ -r "${INIT_CONF:-no-init-conf}" ]]; then source "${INIT_CONF}" fi @@ -203,8 +210,7 @@ function main() { debug "$(cat "${TMPDIR}/wget-${url//\//#}.log")" 2 debug ".. downloading done." 0 if ! grep -qE '^.*\[Adblock.*\].*$' "${file}"; then - echo "The list recieved from ${url} isn't an AdblockPlus list. Skipped" - continue + info "The list recieved from ${url} does not contain AdblockPlus list header. Try to process anyway." fi # remove comments @@ -348,11 +354,11 @@ while getopts ":c:hrqv:V" opt; do SCRIPTCONF="${OPTARG}" ;; "v") - DBG="${OPTARG}" + OPT_DBG="${OPTARG}" VERBOSE=("-v") ;; "q") - DBG=-1 + OPT_DBG=-1 ;; "r") method="remove" diff --git a/tests/configs/debugging.conf b/tests/configs/debugging.conf new file mode 100644 index 0000000..05f13ef --- /dev/null +++ b/tests/configs/debugging.conf @@ -0,0 +1,30 @@ +# Config of privoxy-blocklist + +# array of URL for AdblockPlus lists +# for more sources just add it within the round brackets +URLS=( + "https://easylist-downloads.adblockplus.org/easylistgermany.txt" + "https://easylist-downloads.adblockplus.org/easylist.txt" +) + +# config for privoxy initscript providing PRIVOXY_CONF, PRIVOXY_USER and PRIVOXY_GROUP +#INIT_CONF="/etc/conf.d/privoxy" + +# !! set these when config INIT_CONF doesn't exist and default values do not match your system !! +# !! These values will be overwritten by INIT_CONF when exists !! +PRIVOXY_USER="privoxy" +PRIVOXY_GROUP="root" +PRIVOXY_CONF="/etc/privoxy/config" + +# name for lock file (default: script name) +TMPNAME="$(basename "$(readlink -f "${0}")")" +# directory for temporary files +TMPDIR="/tmp/${TMPNAME}" + +# Debug-level +# -1 = quiet +# 0 = normal +# 1 = verbose +# 2 = more verbose (debugging) +# 3 = incredibly loud (function debugging) +DBG=3 diff --git a/tests/configs/url_extended_config.conf b/tests/configs/url_extended_config.conf new file mode 100644 index 0000000..236b1f8 --- /dev/null +++ b/tests/configs/url_extended_config.conf @@ -0,0 +1,31 @@ +# Config of privoxy-blocklist + +# array of URL for AdblockPlus lists +# for more sources just add it within the round brackets +URLS=( + "https://easylist-downloads.adblockplus.org/easylistgermany.txt" + "https://easylist-downloads.adblockplus.org/easylist.txt" + "https://raw.githubusercontent.com/easylist/easylist/master/easylist/easylist_allowlist_general_hide.txt" +) + +# config for privoxy initscript providing PRIVOXY_CONF, PRIVOXY_USER and PRIVOXY_GROUP +#INIT_CONF="/etc/conf.d/privoxy" + +# !! set these when config INIT_CONF doesn't exist and default values do not match your system !! +# !! These values will be overwritten by INIT_CONF when exists !! +PRIVOXY_USER="privoxy" +PRIVOXY_GROUP="root" +PRIVOXY_CONF="/etc/privoxy/config" + +# name for lock file (default: script name) +TMPNAME="$(basename "$(readlink -f "${0}")")" +# directory for temporary files +TMPDIR="/tmp/${TMPNAME}" + +# Debug-level +# -1 = quiet +# 0 = normal +# 1 = verbose +# 2 = more verbose (debugging) +# 3 = incredibly loud (function debugging) +DBG=0 diff --git a/tests/run.sh b/tests/run.sh index a609243..ee3fc3f 100755 --- a/tests/run.sh +++ b/tests/run.sh @@ -26,7 +26,7 @@ while getopts ":o:r" opt; do done img_tag="privoxy-blocklist-test:${os}" -dockerfile="tests/Dockerfile_${os}" +dockerfile="${SCRIPT_DIR}/Dockerfile_${os}" pytest_cache="pytest_cache_${os}" if ! [ -f "${dockerfile}" ]; then diff --git a/tests/test_01_root_execute.py b/tests/test_01_root_execute.py index d3bfff7..fa722fe 100644 --- a/tests/test_01_root_execute.py +++ b/tests/test_01_root_execute.py @@ -76,6 +76,93 @@ def test_request_block_url(start_privoxy, supported_schemes) -> None: run_requests(start_privoxy, supported_schemes, urls, [403]) +def test_predefined_custom_config_generator(shell, privoxy_blocklist) -> None: + """Run tests for all pre-defined configs.""" + checks = { + "url_extended_config.conf": [ + ( + check_in, + "Processing https://raw.githubusercontent.com/easylist/easylist/master/" + "easylist/easylist_allowlist_general_hide.txt", + ), + ( + check_in, + "Processing https://easylist-downloads.adblockplus.org/easylistgermany.txt", + ), + ( + check_in, + "The list recieved from https://raw.githubusercontent.com/easylist/easylist/master" + "/easylist/easylist_allowlist_general_hide.txt does not contain AdblockPlus list " + "header. Try to process anyway.", + ), + ( + check_not_in, + "created and added image handler", + ), + ], + "debugging.conf": [ + ( + check_in, + "Processing https://easylist-downloads.adblockplus.org/easylistgermany.txt", + ), + ( + check_not_in, + "does not contain AdblockPlus list header.", + ), + ( + check_in, + "‘/tmp/privoxy-blocklist.sh/easylist.txt’ saved", + ), + ( + check_in, + "created and added image handler", + ), + ], + } + test_config_dir = Path(__file__).parent / "configs" + for config in test_config_dir.iterdir(): + if not config.is_file(): + continue + ret = shell.run(privoxy_blocklist, "-c", str(config)) + assert ret.returncode == 0 + assert check_not_in("Creating default one and exiting", ret.stdout) + for check in checks.get(config.name, []): + assert check[0](check[1], ret.stdout) + assert config.exists() + + +# must be last test as it will uninstall dependencies and check error handling +def test_missing_deps(shell, privoxy_blocklist) -> None: + """Test error when dependency is missing.""" + if which("apk"): + ret_pkg = shell.run("apk", "del", "privoxy") + elif which("apt-get"): + ret_pkg = shell.run( + "apt-get", + "remove", + "--yes", + "privoxy", + env={"DEBIAN_FRONTEND": "noninteractive"}, + ) + assert ret_pkg.returncode == 0 + ret_script = shell.run(privoxy_blocklist) + assert ret_script.returncode == 1 + assert "Please install the package providing" in ret_script.stderr + + +# Heloer functions + + +def check_in(needle: str, haystack: str) -> bool: + """Check given haystack for given string.""" + return needle in haystack + + +def check_not_in(needle: str, haystack: str) -> bool: + """Check that given string is not in given text.""" + return needle not in haystack + + def run_requests( start_privoxy, supported_schemes, urls: list[str], expected_code: list[int] ) -> None: @@ -101,22 +188,3 @@ def run_request( # run assert here to see affected URL in assertion assert resp.status_code in expected_code return resp - - -# must be last test as it will uninstall dependencies and check error handling -def test_missing_deps(shell, privoxy_blocklist) -> None: - """Test error when dependency is missing.""" - if which("apk"): - ret_pkg = shell.run("apk", "del", "privoxy") - elif which("apt-get"): - ret_pkg = shell.run( - "apt-get", - "remove", - "--yes", - "privoxy", - env={"DEBIAN_FRONTEND": "noninteractive"}, - ) - assert ret_pkg.returncode == 0 - ret_script = shell.run(privoxy_blocklist) - assert ret_script.returncode == 1 - assert "Please install the package providing" in ret_script.stderr