From ada8cb601995ca90329f43a90bc519213db64040 Mon Sep 17 00:00:00 2001 From: William Melody Date: Sun, 15 Mar 2020 13:14:26 -0700 Subject: [PATCH] Handle multiple hostnames with `block` and `unblock`. --- README.md | 14 +++---- hosts | 34 ++++++++++------- test/block.bats | 45 +++++++++++++++++++++- test/unblock.bats | 97 ++++++++++++++++++++++++++++++++++++++++++++++- 4 files changed, 165 insertions(+), 25 deletions(-) diff --git a/README.md b/README.md index 7aaaac8..a8bc55a 100644 --- a/README.md +++ b/README.md @@ -46,7 +46,7 @@ A package for Arch users is also [available in the AUR](https://aur.archlinux.or Usage: hosts [] hosts add [] - hosts block + hosts block ... hosts disable ( | | ) hosts disabled hosts edit @@ -57,7 +57,7 @@ Usage: hosts search hosts show ( | | ) hosts remove ( | | ) [--force] - hosts unblock + hosts unblock ... hosts --auto-sudo hosts -h | --help hosts --version @@ -113,11 +113,11 @@ Description: ```text Usage: - hosts block + hosts block ... Description: - Block a given hostname by adding new entries assigning it to `127.0.0.1` - for IPv4 and both `fe80::1%lo0` and `::1` for IPv6. + Block one or more hostnames by adding new entries assigned to \`127.0.0.1\` + for IPv4 and both \`fe80::1%lo0\` and \`::1\` for IPv6. ``` #### Blocklists @@ -262,10 +262,10 @@ Description: ```text Usage: - hosts unblock + hosts unblock ... Description: - Unblock a given hostname by removing its entries from the hosts file. + Unblock one or more hostnames by removing the entries from the hosts file. ``` ### `hosts version` diff --git a/hosts b/hosts index dda1d04..a76b4fa 100755 --- a/hosts +++ b/hosts @@ -611,7 +611,7 @@ Version: ${_VERSION} Usage: ${_ME} [] ${_ME} add [] - ${_ME} block + ${_ME} block ... ${_ME} disable ( | | ) ${_ME} disabled ${_ME} edit @@ -622,7 +622,7 @@ Usage: ${_ME} search ${_ME} show ( | | ) ${_ME} remove ( | | ) [--force] - ${_ME} unblock + ${_ME} unblock ... ${_ME} --auto-sudo ${_ME} -h | --help ${_ME} --version @@ -754,10 +754,10 @@ add() { desc "block" < + $_ME block ... Description: - Block a given hostname by adding new entries assigning it to \`127.0.0.1\` + Block one or more hostnames by adding new entries assigned to \`127.0.0.1\` for IPv4 and both \`fe80::1%lo0\` and \`::1\` for IPv6. HEREDOC block() { @@ -769,10 +769,13 @@ block() { exit 1 fi - "${_ME}" add 127.0.0.1 "${1}" - # block IPv6 - "${_ME}" add "fe80::1%lo0" "${1}" - "${_ME}" add "::1" "${1}" + for __hostname in "${@}" + do + "${_ME}" add 127.0.0.1 "${__hostname}" + # block IPv6 + "${_ME}" add "fe80::1%lo0" "${__hostname}" + "${_ME}" add "::1" "${__hostname}" + done } # --------------------------------------------------------------------- disable @@ -1204,10 +1207,10 @@ show() { desc "unblock" < + $_ME unblock ... Description: - Unblock a given hostname by removing its entries from the hosts file. + Unblock one or more hostnames by removing the entries from the hosts file. HEREDOC unblock() { _verify_write_permissions "$@" @@ -1218,10 +1221,13 @@ unblock() { exit 1 fi - "${_ME}" remove 127.0.0.1 "${1}" --force - # unblock IPv6 - "${_ME}" remove "fe80::1%lo0" "${1}" --force - "${_ME}" remove "::1" "${1}" --force + for __hostname in "${@}" + do + "${_ME}" remove 127.0.0.1 "${__hostname}" --force + # unblock IPv6 + "${_ME}" remove "fe80::1%lo0" "${__hostname}" --force + "${_ME}" remove "::1" "${__hostname}" --force + done } ############################################################################### diff --git a/test/block.bats b/test/block.bats index c181911..9627ca5 100644 --- a/test/block.bats +++ b/test/block.bats @@ -25,7 +25,7 @@ load test_helper printf "\${status}: %s\\n" "${status}" printf "\${output}: '%s'\\n" "${output}" [[ "${lines[0]}" == "Usage:" ]] - [[ "${lines[1]}" == " hosts block " ]] + [[ "${lines[1]}" == " hosts block ..." ]] } # `hosts block ` ################################################# @@ -61,6 +61,47 @@ load test_helper [[ "${lines[5]}" == "::1 example.com" ]] } +# `hosts block ` ######################################## + +@test "\`block \` exits with status 0." { + run "${_HOSTS}" block example.com example2.com + printf "\${status}: %s\\n" "${status}" + printf "\${output}: '%s'\\n" "${output}" + [[ ${status} -eq 0 ]] +} + +@test "\`block \` adds entries to the hosts file." { + _original="$(cat "${HOSTS_PATH}")" + + run "${_HOSTS}" block example.com example2.com + printf "\${status}: %s\\n" "${status}" + printf "\${output}: '%s'\\n" "${output}" + _compare "${_original}" "$(cat "${HOSTS_PATH}")" + _compare '127.0.0.1 example.com' "$(sed -n '11p' "${HOSTS_PATH}")" + _compare '127.0.0.1 example2.com' "$(sed -n '11p' "${HOSTS_PATH}")" + [[ "$(cat "${HOSTS_PATH}")" != "${_original}" ]] + [[ "$(sed -n '11p' "${HOSTS_PATH}")" == "127.0.0.1 example.com" ]] + [[ "$(sed -n '14p' "${HOSTS_PATH}")" == "127.0.0.1 example2.com" ]] +} + +@test "\`block \` prints feedback." { + run "${_HOSTS}" block example.com example2.com + printf "\${status}: %s\\n" "${status}" + printf "\${output}: '%s'\\n" "${output}" + [[ "${lines[0]}" == "Added:" ]] + [[ "${lines[1]}" == "127.0.0.1 example.com" ]] + [[ "${lines[2]}" == "Added:" ]] + [[ "${lines[3]}" == "fe80::1%lo0 example.com" ]] + [[ "${lines[4]}" == "Added:" ]] + [[ "${lines[5]}" == "::1 example.com" ]] + [[ "${lines[6]}" == "Added:" ]] + [[ "${lines[7]}" == "127.0.0.1 example2.com" ]] + [[ "${lines[8]}" == "Added:" ]] + [[ "${lines[9]}" == "fe80::1%lo0 example2.com" ]] + [[ "${lines[10]}" == "Added:" ]] + [[ "${lines[11]}" == "::1 example2.com" ]] +} + # help ######################################################################## @test "\`help block\` exits with status 0." { @@ -73,5 +114,5 @@ load test_helper printf "\${status}: %s\\n" "${status}" printf "\${output}: '%s'\\n" "${output}" [[ "${lines[0]}" == "Usage:" ]] - [[ "${lines[1]}" == " hosts block " ]] + [[ "${lines[1]}" == " hosts block ..." ]] } diff --git a/test/unblock.bats b/test/unblock.bats index 00cbb85..92d3b08 100644 --- a/test/unblock.bats +++ b/test/unblock.bats @@ -25,7 +25,7 @@ load test_helper printf "\${status}: %s\\n" "${status}" printf "\${output}: '%s'\\n" "${output}" [[ "${lines[0]}" == "Usage:" ]] - [[ "${lines[1]}" == " hosts unblock " ]] + [[ "${lines[1]}" == " hosts unblock ..." ]] } # `hosts unblock ` ############################################ @@ -133,6 +133,99 @@ Removed: [[ "${output}" == "${_expected}" ]] } +# `hosts unblock ` ###################################### + +@test "\`unblock \` exits with status 0." { + { + run "${_HOSTS}" add 0.0.0.0 example.com + run "${_HOSTS}" block example.com + run "${_HOSTS}" block example2.com + run "${_HOSTS}" add 127.0.0.1 example.net + } + + run "${_HOSTS}" unblock example.com example2.com + printf "\${status}: %s\\n" "${status}" + printf "\${output}: '%s'\\n" "${output}" + [[ ${status} -eq 0 ]] +} + +@test "\`unblock \` updates the hosts file." { + { + run "${_HOSTS}" add 0.0.0.0 example.com + run "${_HOSTS}" block example.com + run "${_HOSTS}" block example2.com + run "${_HOSTS}" add 127.0.0.1 example.net + } + _original="$(cat "${HOSTS_PATH}")" + + run "${_HOSTS}" unblock example.com example2.com + printf "\${status}: %s\\n" "${status}" + printf "\${output}: '%s'\\n" "${output}" + _compare "${_original}" "$(cat "${HOSTS_PATH}")" + [[ "$(sed -n '11p' "${HOSTS_PATH}")" == "0.0.0.0 example.com" ]] + [[ "$(sed -n '12p' "${HOSTS_PATH}")" == "127.0.0.1 example.net" ]] + [[ "$(sed -n '13p' "${HOSTS_PATH}")" == "" ]] +} + +@test "\`unblock \` removes all matches." { + { + run "${_HOSTS}" add 0.0.0.0 example.com + run "${_HOSTS}" block example.com + run "${_HOSTS}" block example2.com + run "${_HOSTS}" add 127.0.0.1 example.net + } + _original="$(cat "${HOSTS_PATH}")" + + run "${_HOSTS}" unblock example.com example2.com + printf "\${status}: %s\\n" "${status}" + printf "\${output}: '%s'\\n" "${output}" + _expected="\ +## +# Host Database +# +# localhost is used to configure the loopback interface +# when the system is booting. Do not change this entry. +## +127.0.0.1 localhost +255.255.255.255 broadcasthost +::1 localhost +fe80::1%lo0 localhost +0.0.0.0 example.com +127.0.0.1 example.net" + _compare "'${_expected}'" "'$(cat "${HOSTS_PATH}")'" + [[ "$(cat "${HOSTS_PATH}")" != "${_original}" ]] + [[ "$(cat "${HOSTS_PATH}")" == "${_expected}" ]] +} + +@test "\`unblock \` prints feedback." { + { + run "${_HOSTS}" add 0.0.0.0 example.com + run "${_HOSTS}" block example.com + run "${_HOSTS}" block example2.com + run "${_HOSTS}" add 127.0.0.1 example.net + } + + run "${_HOSTS}" unblock example.com example2.com + printf "\${status}: %s\\n" "${status}" + printf "\${output}: '%s'\\n" "${output}" + _expected="\ +Removed: +127.0.0.1 example.com +Removed: +fe80::1%lo0 example.com +Removed: +::1 example.com +Removed: +127.0.0.1 example2.com +Removed: +fe80::1%lo0 example2.com +Removed: +::1 example2.com" + _compare "'${output}'" "'${_expected}'" + diff <(echo "${output}" ) <(echo "${_expected}") + [[ "${output}" == "${_expected}" ]] +} + # help ######################################################################## @test "\`help unblock\` exits with status 0." { @@ -145,5 +238,5 @@ Removed: printf "\${status}: %s\\n" "${status}" printf "\${output}: '%s'\\n" "${output}" [[ "${lines[0]}" == "Usage:" ]] - [[ "${lines[1]}" == " hosts unblock " ]] + [[ "${lines[1]}" == " hosts unblock ..." ]] }