Skip to content

Commit

Permalink
improve script & test sutie (#44)
Browse files Browse the repository at this point in the history
* 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 <[email protected]>
  • Loading branch information
Andrwe authored Jan 9, 2024
1 parent d0ada82 commit bafe92d
Show file tree
Hide file tree
Showing 5 changed files with 177 additions and 42 deletions.
50 changes: 28 additions & 22 deletions privoxy-blocklist.sh
Original file line number Diff line number Diff line change
Expand Up @@ -37,18 +37,36 @@ 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."
echo " -v 3: Enable verbosity 3. Show all possible output and don't delete temporary files.(For debugging only!!)"
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"
Expand All @@ -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
Expand Down Expand Up @@ -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

Expand Down Expand Up @@ -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
Expand Down Expand Up @@ -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"
Expand Down
30 changes: 30 additions & 0 deletions tests/configs/debugging.conf
Original file line number Diff line number Diff line change
@@ -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
31 changes: 31 additions & 0 deletions tests/configs/url_extended_config.conf
Original file line number Diff line number Diff line change
@@ -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
2 changes: 1 addition & 1 deletion tests/run.sh
Original file line number Diff line number Diff line change
Expand Up @@ -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
Expand Down
106 changes: 87 additions & 19 deletions tests/test_01_root_execute.py
Original file line number Diff line number Diff line change
Expand Up @@ -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:
Expand All @@ -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

0 comments on commit bafe92d

Please sign in to comment.