2017-03-01 21:43:10 +00:00
|
|
|
#!/usr/bin/env bats
|
|
|
|
|
|
|
|
load test_helper
|
|
|
|
|
|
|
|
# `hosts unblock` #################################################################
|
|
|
|
|
|
|
|
@test "\`unblock\` with no arguments exits with status 1." {
|
|
|
|
run "${_HOSTS}" unblock
|
2018-04-15 21:55:26 +00:00
|
|
|
printf "\${status}: %s\\n" "${status}"
|
|
|
|
printf "\${output}: '%s'\\n" "${output}"
|
2017-03-01 21:43:10 +00:00
|
|
|
[[ ${status} -eq 1 ]]
|
|
|
|
}
|
|
|
|
|
|
|
|
@test "\`unblock\` with no argument does not change the hosts file." {
|
|
|
|
_original="$(cat "${HOSTS_PATH}")"
|
|
|
|
|
|
|
|
run "${_HOSTS}" unblock
|
2018-04-15 21:55:26 +00:00
|
|
|
printf "\${status}: %s\\n" "${status}"
|
|
|
|
printf "\${output}: '%s'\\n" "${output}"
|
2017-03-01 21:43:10 +00:00
|
|
|
[[ "$(cat "${HOSTS_PATH}")" == "${_original}" ]]
|
|
|
|
}
|
|
|
|
|
|
|
|
@test "\`unblock\` with no arguments prints help information." {
|
|
|
|
run "${_HOSTS}" unblock
|
2018-04-15 21:55:26 +00:00
|
|
|
printf "\${status}: %s\\n" "${status}"
|
|
|
|
printf "\${output}: '%s'\\n" "${output}"
|
2017-03-01 21:43:10 +00:00
|
|
|
[[ "${lines[0]}" == "Usage:" ]]
|
2020-03-15 20:14:26 +00:00
|
|
|
[[ "${lines[1]}" == " hosts unblock <hostname>..." ]]
|
2017-03-01 21:43:10 +00:00
|
|
|
}
|
|
|
|
|
|
|
|
# `hosts unblock <invalid>` ############################################
|
|
|
|
|
|
|
|
@test "\`unblock <invalid> \` exits with status 1." {
|
|
|
|
{
|
|
|
|
run "${_HOSTS}" block example.com
|
|
|
|
}
|
|
|
|
|
|
|
|
run "${_HOSTS}" unblock example.net
|
2018-04-15 21:55:26 +00:00
|
|
|
printf "\${status}: %s\\n" "${status}"
|
|
|
|
printf "\${output}: '%s'\\n" "${output}"
|
2017-03-01 21:43:10 +00:00
|
|
|
[[ ${status} -eq 1 ]]
|
|
|
|
}
|
|
|
|
|
|
|
|
@test "\`unblock <invalid>\` prints error message." {
|
|
|
|
{
|
|
|
|
run "${_HOSTS}" block example.com
|
|
|
|
}
|
|
|
|
|
|
|
|
run "${_HOSTS}" unblock example.net
|
2018-04-15 21:55:26 +00:00
|
|
|
printf "\${status}: %s\\n" "${status}"
|
|
|
|
printf "\${output}: '%s'\\n" "${output}"
|
2017-03-01 21:43:10 +00:00
|
|
|
[[ ${output} == "No matching records found." ]]
|
|
|
|
}
|
|
|
|
|
|
|
|
# `hosts unblock <hostname>` ###########################################
|
|
|
|
|
|
|
|
@test "\`unblock <hostname>\` exits with status 0." {
|
|
|
|
{
|
|
|
|
run "${_HOSTS}" add 0.0.0.0 example.com
|
|
|
|
run "${_HOSTS}" block example.com
|
|
|
|
run "${_HOSTS}" add 127.0.0.1 example.net
|
|
|
|
}
|
|
|
|
|
|
|
|
run "${_HOSTS}" unblock example.com
|
2018-04-15 21:55:26 +00:00
|
|
|
printf "\${status}: %s\\n" "${status}"
|
|
|
|
printf "\${output}: '%s'\\n" "${output}"
|
2017-03-01 21:43:10 +00:00
|
|
|
[[ ${status} -eq 0 ]]
|
|
|
|
}
|
|
|
|
|
|
|
|
@test "\`unblock <hostname>\` updates the hosts file." {
|
|
|
|
{
|
|
|
|
run "${_HOSTS}" add 0.0.0.0 example.com
|
|
|
|
run "${_HOSTS}" block example.com
|
|
|
|
run "${_HOSTS}" add 127.0.0.1 example.net
|
|
|
|
}
|
|
|
|
_original="$(cat "${HOSTS_PATH}")"
|
|
|
|
|
|
|
|
run "${_HOSTS}" unblock example.com
|
2018-04-15 21:55:26 +00:00
|
|
|
printf "\${status}: %s\\n" "${status}"
|
|
|
|
printf "\${output}: '%s'\\n" "${output}"
|
2017-03-01 21:43:10 +00:00
|
|
|
_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 <hostname>\` removes all matches." {
|
|
|
|
{
|
|
|
|
run "${_HOSTS}" add 0.0.0.0 example.com
|
|
|
|
run "${_HOSTS}" block example.com
|
|
|
|
run "${_HOSTS}" add 127.0.0.1 example.net
|
|
|
|
}
|
|
|
|
_original="$(cat "${HOSTS_PATH}")"
|
|
|
|
|
|
|
|
run "${_HOSTS}" unblock example.com
|
2018-04-15 21:55:26 +00:00
|
|
|
printf "\${status}: %s\\n" "${status}"
|
|
|
|
printf "\${output}: '%s'\\n" "${output}"
|
2017-03-01 21:43:10 +00:00
|
|
|
_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 <hostname>\` prints feedback." {
|
|
|
|
{
|
|
|
|
run "${_HOSTS}" add 0.0.0.0 example.com
|
|
|
|
run "${_HOSTS}" block example.com
|
|
|
|
run "${_HOSTS}" add 127.0.0.1 example.net
|
|
|
|
}
|
|
|
|
|
|
|
|
run "${_HOSTS}" unblock example.com
|
2018-04-15 21:55:26 +00:00
|
|
|
printf "\${status}: %s\\n" "${status}"
|
|
|
|
printf "\${output}: '%s'\\n" "${output}"
|
2017-03-01 21:43:10 +00:00
|
|
|
_expected="\
|
|
|
|
Removed:
|
|
|
|
127.0.0.1 example.com
|
|
|
|
Removed:
|
|
|
|
fe80::1%lo0 example.com
|
|
|
|
Removed:
|
|
|
|
::1 example.com"
|
|
|
|
[[ "${output}" == "${_expected}" ]]
|
|
|
|
}
|
|
|
|
|
2020-03-15 20:14:26 +00:00
|
|
|
# `hosts unblock <hostname> <hostname2>` ######################################
|
|
|
|
|
|
|
|
@test "\`unblock <hostname> <hostname2>\` 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 <hostname> <hostname2>\` 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 <hostname> <hostname2>\` 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 <hostname> <hostname2>\` 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}" ]]
|
|
|
|
}
|
|
|
|
|
2017-03-01 21:43:10 +00:00
|
|
|
# help ########################################################################
|
|
|
|
|
|
|
|
@test "\`help unblock\` exits with status 0." {
|
|
|
|
run "${_HOSTS}" help unblock
|
|
|
|
[[ ${status} -eq 0 ]]
|
|
|
|
}
|
|
|
|
|
|
|
|
@test "\`help unblock\` prints help information." {
|
|
|
|
run "${_HOSTS}" help unblock
|
2018-04-15 21:55:26 +00:00
|
|
|
printf "\${status}: %s\\n" "${status}"
|
|
|
|
printf "\${output}: '%s'\\n" "${output}"
|
2017-03-01 21:43:10 +00:00
|
|
|
[[ "${lines[0]}" == "Usage:" ]]
|
2020-03-15 20:14:26 +00:00
|
|
|
[[ "${lines[1]}" == " hosts unblock <hostname>..." ]]
|
2017-03-01 21:43:10 +00:00
|
|
|
}
|