2016-01-27 02:50:43 +00:00
|
|
|
#!/usr/bin/env bats
|
|
|
|
|
|
|
|
load test_helper
|
|
|
|
|
|
|
|
# `hosts enable` ##############################################################
|
|
|
|
|
|
|
|
@test "\`enable\` with no arguments exits with status 1." {
|
2016-02-24 02:14:21 +00:00
|
|
|
run "${_HOSTS}" enable
|
2018-04-15 21:55:26 +00:00
|
|
|
printf "\${status}: %s\\n" "${status}"
|
|
|
|
printf "\${output}: '%s'\\n" "${output}"
|
2016-02-24 02:14:21 +00:00
|
|
|
[[ ${status} -eq 1 ]]
|
2016-01-27 02:50:43 +00:00
|
|
|
}
|
|
|
|
|
|
|
|
@test "\`enable\` with no argument does not change the hosts file." {
|
|
|
|
_original="$(cat "${HOSTS_PATH}")"
|
|
|
|
|
2016-02-24 02:14:21 +00:00
|
|
|
run "${_HOSTS}" enable
|
2018-04-15 21:55:26 +00:00
|
|
|
printf "\${status}: %s\\n" "${status}"
|
|
|
|
printf "\${output}: '%s'\\n" "${output}"
|
2016-01-27 02:50:43 +00:00
|
|
|
[[ "$(cat "${HOSTS_PATH}")" == "${_original}" ]]
|
|
|
|
}
|
|
|
|
|
|
|
|
@test "\`enable\` with no arguments prints help information." {
|
2016-02-24 02:14:21 +00:00
|
|
|
run "${_HOSTS}" enable
|
2018-04-15 21:55:26 +00:00
|
|
|
printf "\${status}: %s\\n" "${status}"
|
|
|
|
printf "\${output}: '%s'\\n" "${output}"
|
2016-01-27 02:50:43 +00:00
|
|
|
[[ "${lines[0]}" == "Usage:" ]]
|
2016-01-27 04:25:16 +00:00
|
|
|
[[ "${lines[1]}" == " hosts enable (<ip> | <hostname> | <search string>)" ]]
|
2016-01-27 02:50:43 +00:00
|
|
|
}
|
|
|
|
|
|
|
|
# `hosts enable <ip>` #########################################################
|
|
|
|
|
|
|
|
@test "\`enable <ip>\` exits with status 0." {
|
|
|
|
{
|
2016-02-24 02:14:21 +00:00
|
|
|
run "${_HOSTS}" add 0.0.0.0 example.com
|
|
|
|
run "${_HOSTS}" add 0.0.0.0 example.net
|
|
|
|
run "${_HOSTS}" add 127.0.0.2 example.com
|
|
|
|
run "${_HOSTS}" disable 127.0.0.2
|
2016-01-27 02:50:43 +00:00
|
|
|
}
|
|
|
|
|
2016-02-24 02:14:21 +00:00
|
|
|
run "${_HOSTS}" enable 127.0.0.2
|
2018-04-15 21:55:26 +00:00
|
|
|
printf "\${status}: %s\\n" "${status}"
|
|
|
|
printf "\${output}: '%s'\\n" "${output}"
|
2016-02-24 02:14:21 +00:00
|
|
|
[[ ${status} -eq 0 ]]
|
2016-01-27 02:50:43 +00:00
|
|
|
}
|
|
|
|
|
|
|
|
@test "\`enable <ip>\` updates the hosts file." {
|
|
|
|
{
|
2016-02-24 02:14:21 +00:00
|
|
|
run "${_HOSTS}" add 0.0.0.0 example.com
|
|
|
|
run "${_HOSTS}" add 0.0.0.0 example.net
|
|
|
|
run "${_HOSTS}" add 127.0.0.2 example.com
|
|
|
|
run "${_HOSTS}" disable 0.0.0.0
|
|
|
|
run "${_HOSTS}" disable 127.0.0.2
|
2016-01-27 02:50:43 +00:00
|
|
|
}
|
|
|
|
_original="$(cat "${HOSTS_PATH}")"
|
|
|
|
|
2016-02-24 02:14:21 +00:00
|
|
|
run "${_HOSTS}" enable 127.0.0.2
|
2018-04-15 21:55:26 +00:00
|
|
|
printf "\${status}: %s\\n" "${status}"
|
|
|
|
printf "\${output}: '%s'\\n" "${output}"
|
2016-01-27 02:50:43 +00:00
|
|
|
_compare "${_original}" "$(cat "${HOSTS_PATH}")"
|
2020-04-09 00:27:11 +00:00
|
|
|
[[ "$(sed -n '11p' "${HOSTS_PATH}")" =~ \#disabled:\ 0.0.0.0[[:space:]]+example.com ]]
|
|
|
|
[[ "$(sed -n '12p' "${HOSTS_PATH}")" =~ \#disabled:\ 0.0.0.0[[:space:]]+example.net ]]
|
|
|
|
[[ "$(sed -n '13p' "${HOSTS_PATH}")" =~ 127.0.0.2[[:space:]]+example.com ]]
|
2016-01-27 02:50:43 +00:00
|
|
|
}
|
|
|
|
|
|
|
|
@test "\`enable <ip>\` enables all matches." {
|
|
|
|
{
|
2016-02-24 02:14:21 +00:00
|
|
|
run "${_HOSTS}" add 0.0.0.0 example.com
|
|
|
|
run "${_HOSTS}" add 0.0.0.0 example.net
|
|
|
|
run "${_HOSTS}" add 127.0.0.2 example.com
|
|
|
|
run "${_HOSTS}" disable 0.0.0.0
|
|
|
|
run "${_HOSTS}" disable 127.0.0.2
|
2016-01-27 02:50:43 +00:00
|
|
|
}
|
|
|
|
_original="$(cat "${HOSTS_PATH}")"
|
|
|
|
|
2016-02-24 02:14:21 +00:00
|
|
|
run "${_HOSTS}" enable 0.0.0.0
|
2018-04-15 21:55:26 +00:00
|
|
|
printf "\${status}: %s\\n" "${status}"
|
|
|
|
printf "\${output}: '%s'\\n" "${output}"
|
2016-01-27 02:50:43 +00:00
|
|
|
_compare \
|
|
|
|
"${_original}" \
|
|
|
|
"$(cat "${HOSTS_PATH}")"
|
|
|
|
_compare \
|
|
|
|
"'0.0.0.0 example.com'" \
|
|
|
|
"'$(sed -n '11p' "${HOSTS_PATH}")'"
|
|
|
|
_compare \
|
|
|
|
"'0.0.0.0 example.net'" \
|
|
|
|
"'$(sed -n '12p' "${HOSTS_PATH}")'"
|
|
|
|
_compare \
|
|
|
|
"'#disabled: 127.0.0.2 example.com'" \
|
|
|
|
"'$(sed -n '13p' "${HOSTS_PATH}")'"
|
|
|
|
|
2020-04-09 00:27:11 +00:00
|
|
|
[[ "$(sed -n '11p' "${HOSTS_PATH}")" =~ 0.0.0.0[[:space:]]+example.com ]]
|
|
|
|
[[ "$(sed -n '12p' "${HOSTS_PATH}")" =~ 0.0.0.0[[:space:]]+example.net ]]
|
|
|
|
[[ "$(sed -n '13p' "${HOSTS_PATH}")" =~ \#disabled:\ 127.0.0.2[[:space:]]+example.com ]]
|
2016-01-27 02:50:43 +00:00
|
|
|
}
|
|
|
|
|
2018-08-14 18:29:31 +00:00
|
|
|
@test "\`enable <ip>\` prints feedback." {
|
2016-01-27 02:50:43 +00:00
|
|
|
{
|
2016-02-24 02:14:21 +00:00
|
|
|
run "${_HOSTS}" add 0.0.0.0 example.com
|
|
|
|
run "${_HOSTS}" add 0.0.0.0 example.net
|
|
|
|
run "${_HOSTS}" add 127.0.0.2 example.com
|
|
|
|
run "${_HOSTS}" disable 127.0.0.2
|
2016-01-27 02:50:43 +00:00
|
|
|
}
|
|
|
|
|
2016-02-24 02:14:21 +00:00
|
|
|
run "${_HOSTS}" enable 127.0.0.2
|
2018-04-15 21:55:26 +00:00
|
|
|
printf "\${status}: %s\\n" "${status}"
|
|
|
|
printf "\${output}: '%s'\\n" "${output}"
|
2016-01-27 02:50:43 +00:00
|
|
|
[[ "${lines[0]}" == "Enabling:" ]]
|
2020-04-09 00:27:11 +00:00
|
|
|
[[ "${lines[1]}" =~ 127.0.0.2[[:space:]]+example.com ]]
|
2016-01-27 02:50:43 +00:00
|
|
|
}
|
|
|
|
|
|
|
|
# `hosts enable <hostname>` ###################################################
|
|
|
|
|
|
|
|
@test "\`enable <hostname>\` exits with status 0." {
|
|
|
|
{
|
2016-02-24 02:14:21 +00:00
|
|
|
run "${_HOSTS}" add 0.0.0.0 example.com
|
|
|
|
run "${_HOSTS}" add 0.0.0.0 example.net
|
|
|
|
run "${_HOSTS}" add 127.0.0.2 example.com
|
|
|
|
run "${_HOSTS}" disable 0.0.0.0
|
2016-01-27 02:50:43 +00:00
|
|
|
}
|
|
|
|
|
2016-02-24 02:14:21 +00:00
|
|
|
run "${_HOSTS}" enable example.net
|
2018-04-15 21:55:26 +00:00
|
|
|
printf "\${status}: %s\\n" "${status}"
|
|
|
|
printf "\${output}: '%s'\\n" "${output}"
|
2016-02-24 02:14:21 +00:00
|
|
|
[[ ${status} -eq 0 ]]
|
2016-01-27 02:50:43 +00:00
|
|
|
}
|
|
|
|
|
|
|
|
@test "\`enable <hostname>\` updates the hosts file." {
|
|
|
|
{
|
2016-02-24 02:14:21 +00:00
|
|
|
run "${_HOSTS}" add 0.0.0.0 example.com
|
|
|
|
run "${_HOSTS}" add 0.0.0.0 example.net
|
|
|
|
run "${_HOSTS}" add 127.0.0.2 example.com
|
|
|
|
run "${_HOSTS}" disable 0.0.0.0
|
2016-01-27 02:50:43 +00:00
|
|
|
}
|
|
|
|
_original="$(cat "${HOSTS_PATH}")"
|
|
|
|
|
2016-02-24 02:14:21 +00:00
|
|
|
run "${_HOSTS}" enable example.net
|
2018-04-15 21:55:26 +00:00
|
|
|
printf "\${status}: %s\\n" "${status}"
|
|
|
|
printf "\${output}: '%s'\\n" "${output}"
|
2016-01-27 02:50:43 +00:00
|
|
|
_compare "${_original}" "$(cat "${HOSTS_PATH}")"
|
2020-04-09 00:27:11 +00:00
|
|
|
[[ "$(sed -n '11p' "${HOSTS_PATH}")" =~ \#disabled:\ 0.0.0.0[[:space:]]+example.com ]]
|
|
|
|
[[ "$(sed -n '12p' "${HOSTS_PATH}")" =~ 0.0.0.0[[:space:]]+example.net ]]
|
|
|
|
[[ "$(sed -n '13p' "${HOSTS_PATH}")" =~ 127.0.0.2[[:space:]]+example.com ]]
|
2016-01-27 02:50:43 +00:00
|
|
|
}
|
|
|
|
|
|
|
|
@test "\`enable <hostname>\` enables all matches." {
|
|
|
|
{
|
2016-02-24 02:14:21 +00:00
|
|
|
run "${_HOSTS}" add 0.0.0.0 example.com
|
|
|
|
run "${_HOSTS}" add 0.0.0.0 example.net
|
|
|
|
run "${_HOSTS}" add 127.0.0.2 example.com
|
|
|
|
run "${_HOSTS}" disable 0.0.0.0
|
|
|
|
run "${_HOSTS}" disable 127.0.0.2
|
2016-01-27 02:50:43 +00:00
|
|
|
}
|
|
|
|
_original="$(cat "${HOSTS_PATH}")"
|
|
|
|
|
2016-02-24 02:14:21 +00:00
|
|
|
run "${_HOSTS}" enable example.com
|
2018-04-15 21:55:26 +00:00
|
|
|
printf "\${status}: %s\\n" "${status}"
|
|
|
|
printf "\${output}: '%s'\\n" "${output}"
|
2016-01-27 02:50:43 +00:00
|
|
|
_compare \
|
|
|
|
"${_original}" \
|
|
|
|
"$(cat "${HOSTS_PATH}")"
|
|
|
|
_compare \
|
|
|
|
"'0.0.0.0 example.com'" \
|
|
|
|
"'$(sed -n '11p' "${HOSTS_PATH}")'"
|
|
|
|
_compare \
|
|
|
|
"'#disabled: 0.0.0.0 example.net'" \
|
|
|
|
"'$(sed -n '12p' "${HOSTS_PATH}")'"
|
|
|
|
_compare \
|
|
|
|
"'127.0.0.2 example.com'" \
|
|
|
|
"'$(sed -n '13p' "${HOSTS_PATH}")'"
|
|
|
|
|
2020-04-09 00:27:11 +00:00
|
|
|
[[ "$(sed -n '11p' "${HOSTS_PATH}")" =~ 0.0.0.0[[:space:]]+example.com ]]
|
|
|
|
[[ "$(sed -n '12p' "${HOSTS_PATH}")" =~ \#disabled:\ 0.0.0.0[[:space:]]+example.net ]]
|
|
|
|
[[ "$(sed -n '13p' "${HOSTS_PATH}")" =~ 127.0.0.2[[:space:]]+example.com ]]
|
2016-01-27 02:50:43 +00:00
|
|
|
}
|
|
|
|
|
2018-08-14 18:29:31 +00:00
|
|
|
@test "\`enable <hostname>\` prints feedback." {
|
2016-01-27 02:50:43 +00:00
|
|
|
{
|
2016-02-24 02:14:21 +00:00
|
|
|
run "${_HOSTS}" add 0.0.0.0 example.com
|
|
|
|
run "${_HOSTS}" add 0.0.0.0 example.net
|
|
|
|
run "${_HOSTS}" add 127.0.0.2 example.com
|
|
|
|
run "${_HOSTS}" disable 0.0.0.0
|
2016-01-27 02:50:43 +00:00
|
|
|
}
|
|
|
|
|
2016-02-24 02:14:21 +00:00
|
|
|
run "${_HOSTS}" enable example.net
|
2018-04-15 21:55:26 +00:00
|
|
|
printf "\${status}: %s\\n" "${status}"
|
|
|
|
printf "\${output}: '%s'\\n" "${output}"
|
2016-01-27 02:50:43 +00:00
|
|
|
[[ "${lines[0]}" == "Enabling:" ]]
|
2020-04-09 00:27:11 +00:00
|
|
|
[[ "${lines[1]}" =~ 0.0.0.0[[:space:]]+example.net ]]
|
2016-01-27 02:50:43 +00:00
|
|
|
}
|
2016-01-27 03:04:18 +00:00
|
|
|
|
|
|
|
# help ########################################################################
|
|
|
|
|
|
|
|
@test "\`help enable\` exits with status 0." {
|
2016-02-24 02:14:21 +00:00
|
|
|
run "${_HOSTS}" help enable
|
|
|
|
[[ ${status} -eq 0 ]]
|
2016-01-27 03:04:18 +00:00
|
|
|
}
|
|
|
|
|
|
|
|
@test "\`help enable\` prints help information." {
|
2016-02-24 02:14:21 +00:00
|
|
|
run "${_HOSTS}" help enable
|
2018-04-15 21:55:26 +00:00
|
|
|
printf "\${status}: %s\\n" "${status}"
|
|
|
|
printf "\${output}: '%s'\\n" "${output}"
|
2016-01-27 03:04:18 +00:00
|
|
|
[[ "${lines[0]}" == "Usage:" ]]
|
2016-01-27 04:25:16 +00:00
|
|
|
[[ "${lines[1]}" == " hosts enable (<ip> | <hostname> | <search string>)" ]]
|
2016-01-27 03:04:18 +00:00
|
|
|
}
|