Updated tests

This commit is contained in:
Héctor Molinero Fernández 2020-10-31 16:25:46 +01:00
commit 9f716ffb94
34 changed files with 186 additions and 61 deletions

View file

@ -50,10 +50,10 @@ build: ./dist/hosts $(ALT_FORMATS_OUT)
stats: ./dist/most_abused_tlds.txt ./dist/most_abused_suffixes.txt
./dist/most_abused_tlds.txt: ./dist/hosts_domains.txt
./resources/stats/suffix.sh ./dist/hosts_domains.txt none > '$@'
./resources/stats/stats.sh ./dist/hosts_domains.txt no-psl > '$@'
./dist/most_abused_suffixes.txt: ./dist/hosts_domains.txt
./resources/stats/suffix.sh ./dist/hosts_domains.txt > '$@'
./resources/stats/stats.sh ./dist/hosts_domains.txt > '$@'
##################################################
## "index" target
@ -63,7 +63,7 @@ stats: ./dist/most_abused_tlds.txt ./dist/most_abused_suffixes.txt
index: ./dist/index.html
./dist/index.html: ./dist/hosts $(ALT_FORMATS_OUT) ./dist/most_abused_tlds.txt ./dist/most_abused_suffixes.txt
./resources/templates/index.sh ./dist/ > '$@'
./resources/index/index.sh ./dist/ > '$@'
##################################################
## "man" target
@ -93,7 +93,7 @@ lint:
test:
find ./resources/tests/ -type f -name 'test-*.sh' | sort -n | xargs -n1 env -i \
PATH='/usr/sbin:/usr/bin:/sbin:/bin' TEST_SHELL='$(TEST_SHELL)' HBLOCK_SCRIPT='./hblock'
PATH='/usr/sbin:/usr/bin:/sbin:/bin' TEST_SHELL='$(TEST_SHELL)'
##################################################
## "install" target

View file

@ -73,7 +73,7 @@ main() {
| sort | uniq -c > "${blocklistFile:?}.aux" \
&& mv -f -- "${blocklistFile:?}.aux" "${blocklistFile:?}"
if [ "${publicSuffixList:?}" != 'none' ]; then
if [ "${publicSuffixList:?}" != 'no-psl' ]; then
# Download public suffix list.
fetchUrl "${publicSuffixList:?}" > "${blocklistFile:?}.suffixes"

View file

@ -0,0 +1,42 @@
example-000.arpa
example-001.xn--od0alg.xn--j6w193g
example-002.net
example-003.jp
example-004.net
example-005.home.arpa
example-006.xn--j6w193g
example-007.arpa
example-008.xn--j6w193g
example-009.xn--od0alg.xn--j6w193g
example-010.xn--j6w193g
example-011.xn--j6w193g
example-012.jp
example-013.jp
example-014.com
example-015.unknown
example-016.home.arpa
example-017.org
example-018.xn--od0alg.xn--j6w193g
example-019.arpa
example-020.xn--1lqs71d.jp
example-021.org
example-022.xn--od0alg.xn--j6w193g
example-023.xn--j6w193g
example-024.com
example-025.com
example-026.arpa
example-027.xn--od0alg.xn--j6w193g
example-028.xn--1lqs71d.jp
example-029.home.arpa
example-030.xn--1lqs71d.jp
example-031.xn--od0alg.xn--j6w193g
example-032.jp
example-033.com
example-034.home.arpa
example-035.home.arpa
example-036.net
example-037.net
example-038.xn--1lqs71d.jp
example-039.org
example-040.xn--od0alg.xn--j6w193g
example-041.jp

View file

@ -9,6 +9,7 @@ export LC_ALL='C'
SCRIPT_DIR="$(CDPATH='' cd -- "$(dirname -- "${0:?}")" && pwd -P)"
# Set base environment.
export HOSTNAME='hblock'
export HBLOCK_OUTPUT_FILE='/dev/null'
export HBLOCK_HEADER_FILE='builtin'
@ -18,8 +19,8 @@ export HBLOCK_SOURCES="file://${SCRIPT_DIR:?}/sources.txt"
export HBLOCK_ALLOWLIST_FILE='builtin'
export HBLOCK_DENYLIST_FILE='builtin'
hBlockInTestShell() {
${TEST_SHELL:-/bin/sh} -- "${HBLOCK_SCRIPT:-hblock}" "${@-}" 2>&1 ||:
runInTestShell() {
${TEST_SHELL:-/bin/sh} -- "${@-}" 2>&1 ||:
}
assertEquals() {

15
resources/tests/psl.txt Normal file
View file

@ -0,0 +1,15 @@
// Public suffix list sample file
com
info
org
net
arpa
home.arpa
jp
東京.jp
香港
網絡.香港

View file

@ -15,16 +15,16 @@ SCRIPT_DIR="$(CDPATH='' cd -- "$(dirname -- "${0:?}")" && pwd -P)"
main() {
export HBLOCK_COMMENT='#'
printf 'Test: "-C" short option\n'
actual="$(hBlockInTestShell -qO- -C '# %')"
expected="$(cat -- "${SCRIPT_DIR:?}"/test-comment-opt.out)"
printf -- 'Test - Main: "-C" short option\n'
actual="$(runInTestShell "${SCRIPT_DIR:?}/../../hblock" -qO- -C '# %')"
expected="$(cat -- "${0%.sh}".out)"
if ! assertEquals "${actual?}" "${expected?}"; then
exit 1
fi
printf 'Test: "--comment" long option\n'
actual="$(hBlockInTestShell -qO- --comment='# %')"
expected="$(cat -- "${SCRIPT_DIR:?}"/test-comment-opt.out)"
printf -- 'Test - Main: "--comment" long option\n'
actual="$(runInTestShell "${SCRIPT_DIR:?}/../../hblock" -qO- --comment='# %')"
expected="$(cat -- "${0%.sh}".out)"
if ! assertEquals "${actual?}" "${expected?}"; then
exit 1
fi

View file

@ -13,9 +13,9 @@ SCRIPT_DIR="$(CDPATH='' cd -- "$(dirname -- "${0:?}")" && pwd -P)"
. "${SCRIPT_DIR:?}"/env.sh
main() {
printf 'Test: default options\n'
actual="$(hBlockInTestShell -qO-)"
expected="$(cat -- "${SCRIPT_DIR:?}"/test-default-opts.out)"
printf -- 'Test - Main: Default options\n'
actual="$(runInTestShell "${SCRIPT_DIR:?}/../../hblock" -qO-)"
expected="$(cat -- "${0%.sh}".out)"
if ! assertEquals "${actual?}" "${expected?}"; then
exit 1
fi

View file

@ -13,9 +13,9 @@ SCRIPT_DIR="$(CDPATH='' cd -- "$(dirname -- "${0:?}")" && pwd -P)"
. "${SCRIPT_DIR:?}"/env.sh
main() {
printf 'Test: invalid short option\n'
actual="$(hBlockInTestShell -qO- -i 'VALUE')"
expected="$(cat -- "${SCRIPT_DIR:?}"/test-invalid-short-opt.out)"
printf -- 'Test - Main: Double dash argument\n'
actual="$(runInTestShell "${SCRIPT_DIR:?}/../../hblock" -qO- -- -v)"
expected="$(cat -- "${0%.sh}".out)"
if ! assertEquals "${actual?}" "${expected?}"; then
exit 1
fi

View file

@ -13,9 +13,9 @@ SCRIPT_DIR="$(CDPATH='' cd -- "$(dirname -- "${0:?}")" && pwd -P)"
. "${SCRIPT_DIR:?}"/env.sh
main() {
printf 'Test: invalid long option\n'
actual="$(hBlockInTestShell -qO- --invalid='VALUE')"
expected="$(cat -- "${SCRIPT_DIR:?}"/test-invalid-long-opt.out)"
printf -- 'Test - Main: Invalid long option\n'
actual="$(runInTestShell "${SCRIPT_DIR:?}/../../hblock" -qO- --invalid='VALUE')"
expected="$(cat -- "${0%.sh}".out)"
if ! assertEquals "${actual?}" "${expected?}"; then
exit 1
fi

View file

@ -13,9 +13,9 @@ SCRIPT_DIR="$(CDPATH='' cd -- "$(dirname -- "${0:?}")" && pwd -P)"
. "${SCRIPT_DIR:?}"/env.sh
main() {
printf 'Test: double dash argument\n'
actual="$(hBlockInTestShell -qO- -- -v)"
expected="$(cat -- "${SCRIPT_DIR:?}"/test-double-dash-arg.out)"
printf -- 'Test - Main: Invalid short option\n'
actual="$(runInTestShell "${SCRIPT_DIR:?}/../../hblock" -qO- -i 'VALUE')"
expected="$(cat -- "${0%.sh}".out)"
if ! assertEquals "${actual?}" "${expected?}"; then
exit 1
fi

View file

@ -15,16 +15,16 @@ SCRIPT_DIR="$(CDPATH='' cd -- "$(dirname -- "${0:?}")" && pwd -P)"
main() {
export HBLOCK_LENIENT='false'
printf 'Test: "-l" short option\n'
actual="$(hBlockInTestShell -qO- -l)"
expected="$(cat -- "${SCRIPT_DIR:?}"/test-lenient-opt.out)"
printf -- 'Test - Main: "-l" short option\n'
actual="$(runInTestShell "${SCRIPT_DIR:?}/../../hblock" -qO- -l)"
expected="$(cat -- "${0%.sh}".out)"
if ! assertEquals "${actual?}" "${expected?}"; then
exit 1
fi
printf 'Test: "--lenient" long option\n'
actual="$(hBlockInTestShell -qO- --lenient)"
expected="$(cat -- "${SCRIPT_DIR:?}"/test-lenient-opt.out)"
printf -- 'Test - Main: "--lenient" long option\n'
actual="$(runInTestShell "${SCRIPT_DIR:?}/../../hblock" -qO- --lenient)"
expected="$(cat -- "${0%.sh}".out)"
if ! assertEquals "${actual?}" "${expected?}"; then
exit 1
fi

View file

@ -15,9 +15,9 @@ SCRIPT_DIR="$(CDPATH='' cd -- "$(dirname -- "${0:?}")" && pwd -P)"
main() {
export HBLOCK_LENIENT='true'
printf 'Test: "--no-lenient" long option\n'
actual="$(hBlockInTestShell -qO- --no-lenient)"
expected="$(cat -- "${SCRIPT_DIR:?}"/test-no-lenient-opt.out)"
printf -- 'Test - Main: "--no-lenient" long option\n'
actual="$(runInTestShell "${SCRIPT_DIR:?}/../../hblock" -qO- --no-lenient)"
expected="$(cat -- "${0%.sh}".out)"
if ! assertEquals "${actual?}" "${expected?}"; then
exit 1
fi

View file

@ -22,9 +22,9 @@ main() {
EOF
)"
printf 'Test: "--no-regex" long option\n'
actual="$(hBlockInTestShell -qO- --no-regex)"
expected="$(cat -- "${SCRIPT_DIR:?}"/test-no-regex-opt.out)"
printf -- 'Test - Main: "--no-regex" long option\n'
actual="$(runInTestShell "${SCRIPT_DIR:?}/../../hblock" -qO- --no-regex)"
expected="$(cat -- "${0%.sh}".out)"
if ! assertEquals "${actual?}" "${expected?}"; then
exit 1
fi

View file

@ -15,16 +15,16 @@ SCRIPT_DIR="$(CDPATH='' cd -- "$(dirname -- "${0:?}")" && pwd -P)"
main() {
export HBLOCK_REDIRECTION='0.0.0.0'
printf 'Test: "-R" short option\n'
actual="$(hBlockInTestShell -qO- -R '::1')"
expected="$(cat -- "${SCRIPT_DIR:?}"/test-redirection-opt.out)"
printf -- 'Test - Main: "-R" short option\n'
actual="$(runInTestShell "${SCRIPT_DIR:?}/../../hblock" -qO- -R '::1')"
expected="$(cat -- "${0%.sh}".out)"
if ! assertEquals "${actual?}" "${expected?}"; then
exit 1
fi
printf 'Test: "--redirection" long option\n'
actual="$(hBlockInTestShell -qO- --redirection='::1')"
expected="$(cat -- "${SCRIPT_DIR:?}"/test-redirection-opt.out)"
printf -- 'Test - Main: "--redirection" long option\n'
actual="$(runInTestShell "${SCRIPT_DIR:?}/../../hblock" -qO- --redirection='::1')"
expected="$(cat -- "${0%.sh}".out)"
if ! assertEquals "${actual?}" "${expected?}"; then
exit 1
fi

View file

@ -21,16 +21,16 @@ main() {
EOF
)"
printf 'Test: "-r" short option\n'
actual="$(hBlockInTestShell -qO- -r)"
expected="$(cat -- "${SCRIPT_DIR:?}"/test-regex-opt.out)"
printf -- 'Test - Main: "-r" short option\n'
actual="$(runInTestShell "${SCRIPT_DIR:?}/../../hblock" -qO- -r)"
expected="$(cat -- "${0%.sh}".out)"
if ! assertEquals "${actual?}" "${expected?}"; then
exit 1
fi
printf 'Test: "--regex" long option\n'
actual="$(hBlockInTestShell -qO- --regex)"
expected="$(cat -- "${SCRIPT_DIR:?}"/test-regex-opt.out)"
printf -- 'Test - Main: "--regex" long option\n'
actual="$(runInTestShell "${SCRIPT_DIR:?}/../../hblock" -qO- --regex)"
expected="$(cat -- "${0%.sh}".out)"
if ! assertEquals "${actual?}" "${expected?}"; then
exit 1
fi

View file

@ -15,16 +15,16 @@ SCRIPT_DIR="$(CDPATH='' cd -- "$(dirname -- "${0:?}")" && pwd -P)"
main() {
export HBLOCK_TEMPLATE='%R %D'
printf 'Test: "-T" short option\n'
actual="$(hBlockInTestShell -qO- -T '%D\n\t└─ %R')"
expected="$(cat -- "${SCRIPT_DIR:?}"/test-template-opt.out)"
printf -- 'Test - Main: "-T" short option\n'
actual="$(runInTestShell "${SCRIPT_DIR:?}/../../hblock" -qO- -T '%D\n\t└─ %R')"
expected="$(cat -- "${0%.sh}".out)"
if ! assertEquals "${actual?}" "${expected?}"; then
exit 1
fi
printf 'Test: "--template" long option\n'
actual="$(hBlockInTestShell -qO- --template='%D\n\t└─ %R')"
expected="$(cat -- "${SCRIPT_DIR:?}"/test-template-opt.out)"
printf -- 'Test - Main: "--template" long option\n'
actual="$(runInTestShell "${SCRIPT_DIR:?}/../../hblock" -qO- --template='%D\n\t└─ %R')"
expected="$(cat -- "${0%.sh}".out)"
if ! assertEquals "${actual?}" "${expected?}"; then
exit 1
fi

View file

@ -15,16 +15,16 @@ SCRIPT_DIR="$(CDPATH='' cd -- "$(dirname -- "${0:?}")" && pwd -P)"
main() {
export HBLOCK_WRAP='1'
printf 'Test: "-W" short option\n'
actual="$(hBlockInTestShell -qO- -W '5')"
expected="$(cat -- "${SCRIPT_DIR:?}"/test-wrap-opt.out)"
printf -- 'Test - Main: "-W" short option\n'
actual="$(runInTestShell "${SCRIPT_DIR:?}/../../hblock" -qO- -W '5')"
expected="$(cat -- "${0%.sh}".out)"
if ! assertEquals "${actual?}" "${expected?}"; then
exit 1
fi
printf 'Test: "--wrap" long option\n'
actual="$(hBlockInTestShell -qO- --wrap='5')"
expected="$(cat -- "${SCRIPT_DIR:?}"/test-wrap-opt.out)"
printf -- 'Test - Main: "--wrap" long option\n'
actual="$(runInTestShell "${SCRIPT_DIR:?}/../../hblock" -qO- --wrap='5')"
expected="$(cat -- "${0%.sh}".out)"
if ! assertEquals "${actual?}" "${expected?}"; then
exit 1
fi

View file

@ -0,0 +1,11 @@
Top Hosts Suffix
1 7 .xn--od0alg.xn--j6w193g
2 5 .home.arpa
3 5 .jp
4 5 .xn--j6w193g
5 4 .arpa
6 4 .com
7 4 .net
8 4 .xn--1lqs71d.jp
9 3 .org
10 1 .unknown

View file

@ -0,0 +1,24 @@
#!/bin/sh
# Author: Héctor Molinero Fernández <hector@molinero.dev>
# License: MIT, https://opensource.org/licenses/MIT
# Repository: https://github.com/hectorm/hblock
set -eu
export LC_ALL='C'
SCRIPT_DIR="$(CDPATH='' cd -- "$(dirname -- "${0:?}")" && pwd -P)"
# shellcheck disable=SC1090
. "${SCRIPT_DIR:?}"/env.sh
main() {
printf -- 'Test - Stats: Suffixes\n'
actual="$(runInTestShell "${SCRIPT_DIR:?}/../stats/stats.sh" "${SCRIPT_DIR:?}/domains.txt" "file://${SCRIPT_DIR:?}/psl.txt")"
expected="$(cat -- "${0%.sh}".out)"
if ! assertEquals "${actual?}" "${expected?}"; then
exit 1
fi
}
main "${@-}"

View file

@ -0,0 +1,8 @@
Top Hosts Suffix
1 12 .xn--j6w193g
2 9 .arpa
3 9 .jp
4 4 .com
5 4 .net
6 3 .org
7 1 .unknown

View file

@ -0,0 +1,24 @@
#!/bin/sh
# Author: Héctor Molinero Fernández <hector@molinero.dev>
# License: MIT, https://opensource.org/licenses/MIT
# Repository: https://github.com/hectorm/hblock
set -eu
export LC_ALL='C'
SCRIPT_DIR="$(CDPATH='' cd -- "$(dirname -- "${0:?}")" && pwd -P)"
# shellcheck disable=SC1090
. "${SCRIPT_DIR:?}"/env.sh
main() {
printf -- 'Test - Stats: TLDs\n'
actual="$(runInTestShell "${SCRIPT_DIR:?}/../stats/stats.sh" "${SCRIPT_DIR:?}/domains.txt" no-psl)"
expected="$(cat -- "${0%.sh}".out)"
if ! assertEquals "${actual?}" "${expected?}"; then
exit 1
fi
}
main "${@-}"