2016-01-27 03:35:04 +00:00
|
|
|
#!/usr/bin/env bats
|
|
|
|
|
|
|
|
load test_helper
|
|
|
|
|
|
|
|
# `hosts remove` #################################################################
|
|
|
|
|
|
|
|
@test "\`remove\` with no arguments exits with status 1." {
|
2016-02-24 02:14:21 +00:00
|
|
|
run "${_HOSTS}" remove
|
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 03:35:04 +00:00
|
|
|
}
|
|
|
|
|
|
|
|
@test "\`remove\` with no argument does not change the hosts file." {
|
|
|
|
_original="$(cat "${HOSTS_PATH}")"
|
|
|
|
|
2016-02-24 02:14:21 +00:00
|
|
|
run "${_HOSTS}" remove
|
2018-04-15 21:55:26 +00:00
|
|
|
printf "\${status}: %s\\n" "${status}"
|
|
|
|
printf "\${output}: '%s'\\n" "${output}"
|
2016-01-27 03:35:04 +00:00
|
|
|
[[ "$(cat "${HOSTS_PATH}")" == "${_original}" ]]
|
|
|
|
}
|
|
|
|
|
|
|
|
@test "\`remove\` with no arguments prints help information." {
|
2016-02-24 02:14:21 +00:00
|
|
|
run "${_HOSTS}" remove
|
2018-04-15 21:55:26 +00:00
|
|
|
printf "\${status}: %s\\n" "${status}"
|
|
|
|
printf "\${output}: '%s'\\n" "${output}"
|
2016-01-27 03:35:04 +00:00
|
|
|
[[ "${lines[0]}" == "Usage:" ]]
|
2016-01-27 04:25:16 +00:00
|
|
|
[[ "${lines[1]}" == " hosts remove (<ip> | <hostname> | <search string>) [--force]" ]]
|
2016-01-27 03:35:04 +00:00
|
|
|
}
|
|
|
|
|
2016-01-27 04:17:55 +00:00
|
|
|
# `hosts remove <invalid> --force` ############################################
|
|
|
|
|
|
|
|
@test "\`remove <invalid> --force\` exits with status 1." {
|
|
|
|
{
|
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
|
2016-01-27 04:17:55 +00:00
|
|
|
}
|
|
|
|
|
2016-02-24 02:14:21 +00:00
|
|
|
run "${_HOSTS}" remove 127.0.0.3 --force
|
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 04:17:55 +00:00
|
|
|
}
|
|
|
|
|
|
|
|
@test "\`remove <invalid> --force\` prints error message." {
|
|
|
|
{
|
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
|
2016-01-27 04:17:55 +00:00
|
|
|
}
|
|
|
|
|
2016-02-24 02:14:21 +00:00
|
|
|
run "${_HOSTS}" remove 127.0.0.3 --force
|
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
|
|
|
[[ ${output} == "No matching records found." ]]
|
2016-01-27 04:17:55 +00:00
|
|
|
}
|
|
|
|
|
2016-01-27 03:35:04 +00:00
|
|
|
# `hosts remove <ip> --force` #################################################
|
|
|
|
|
|
|
|
@test "\`remove <ip> --force\` 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
|
2016-01-27 03:35:04 +00:00
|
|
|
}
|
|
|
|
|
2016-02-24 02:14:21 +00:00
|
|
|
run "${_HOSTS}" remove 127.0.0.2 --force
|
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 03:35:04 +00:00
|
|
|
}
|
|
|
|
|
|
|
|
@test "\`remove <ip> --force\` 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
|
2016-01-27 03:35:04 +00:00
|
|
|
}
|
|
|
|
_original="$(cat "${HOSTS_PATH}")"
|
|
|
|
|
2016-02-24 02:14:21 +00:00
|
|
|
run "${_HOSTS}" remove 127.0.0.2 --force
|
2018-04-15 21:55:26 +00:00
|
|
|
printf "\${status}: %s\\n" "${status}"
|
|
|
|
printf "\${output}: '%s'\\n" "${output}"
|
2016-01-27 03:35:04 +00:00
|
|
|
_compare "${_original}" "$(cat "${HOSTS_PATH}")"
|
|
|
|
[[ "$(sed -n '11p' "${HOSTS_PATH}")" == "0.0.0.0 example.com" ]]
|
|
|
|
[[ "$(sed -n '12p' "${HOSTS_PATH}")" == "0.0.0.0 example.net" ]]
|
|
|
|
[[ "$(sed -n '13p' "${HOSTS_PATH}")" == "" ]]
|
|
|
|
}
|
|
|
|
|
|
|
|
@test "\`remove <ip>\` removes 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 0.0.0.0 example.dev
|
|
|
|
run "${_HOSTS}" add 127.0.0.2 example.com
|
|
|
|
run "${_HOSTS}" disable example.dev
|
2016-01-27 03:35:04 +00:00
|
|
|
}
|
|
|
|
_original="$(cat "${HOSTS_PATH}")"
|
|
|
|
|
2016-02-24 02:14:21 +00:00
|
|
|
run "${_HOSTS}" remove 0.0.0.0 --force
|
2018-04-15 21:55:26 +00:00
|
|
|
printf "\${status}: %s\\n" "${status}"
|
|
|
|
printf "\${output}: '%s'\\n" "${output}"
|
2016-01-27 03:35:04 +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
|
|
|
|
127.0.0.2 example.com"
|
2016-02-24 02:14:21 +00:00
|
|
|
_compare "'${_expected}'" "'$(cat "${HOSTS_PATH}")'"
|
|
|
|
[[ "$(cat "${HOSTS_PATH}")" != "${_original}" ]]
|
|
|
|
[[ "$(cat "${HOSTS_PATH}")" == "${_expected}" ]]
|
2016-01-27 03:35:04 +00:00
|
|
|
}
|
|
|
|
|
|
|
|
@test "\`remove <ip>\` prints feedback." {
|
|
|
|
{
|
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
|
2016-01-27 03:35:04 +00:00
|
|
|
}
|
|
|
|
|
2016-02-24 02:14:21 +00:00
|
|
|
run "${_HOSTS}" remove 127.0.0.2 --force
|
2018-04-15 21:55:26 +00:00
|
|
|
printf "\${status}: %s\\n" "${status}"
|
|
|
|
printf "\${output}: '%s'\\n" "${output}"
|
2016-01-27 03:35:04 +00:00
|
|
|
[[ "${lines[0]}" == "Removed:" ]]
|
|
|
|
[[ "${lines[1]}" == "127.0.0.2 example.com" ]]
|
|
|
|
}
|
|
|
|
|
|
|
|
# `hosts remove <hostname> --force` ###########################################
|
|
|
|
|
|
|
|
@test "\`remove <hostname> --force\` 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
|
2016-01-27 03:35:04 +00:00
|
|
|
}
|
|
|
|
|
2016-02-24 02:14:21 +00:00
|
|
|
run "${_HOSTS}" remove example.com --force
|
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 03:35:04 +00:00
|
|
|
}
|
|
|
|
|
|
|
|
@test "\`remove <hostname> --force\` 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
|
2016-01-27 03:35:04 +00:00
|
|
|
}
|
|
|
|
_original="$(cat "${HOSTS_PATH}")"
|
|
|
|
|
2016-02-24 02:14:21 +00:00
|
|
|
run "${_HOSTS}" remove example.com --force
|
2018-04-15 21:55:26 +00:00
|
|
|
printf "\${status}: %s\\n" "${status}"
|
|
|
|
printf "\${output}: '%s'\\n" "${output}"
|
2016-01-27 03:35:04 +00:00
|
|
|
_compare "${_original}" "$(cat "${HOSTS_PATH}")"
|
|
|
|
[[ "$(sed -n '11p' "${HOSTS_PATH}")" == "0.0.0.0 example.net" ]]
|
|
|
|
[[ "$(sed -n '12p' "${HOSTS_PATH}")" == "" ]]
|
|
|
|
}
|
|
|
|
|
|
|
|
@test "\`remove <hostname>\` removes 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 0.0.0.0 example.dev
|
|
|
|
run "${_HOSTS}" add 127.0.0.2 example.com
|
|
|
|
run "${_HOSTS}" disable example.dev
|
2016-01-27 03:35:04 +00:00
|
|
|
}
|
|
|
|
_original="$(cat "${HOSTS_PATH}")"
|
|
|
|
|
2016-02-24 02:14:21 +00:00
|
|
|
run "${_HOSTS}" remove example.com --force
|
2018-04-15 21:55:26 +00:00
|
|
|
printf "\${status}: %s\\n" "${status}"
|
|
|
|
printf "\${output}: '%s'\\n" "${output}"
|
2016-01-27 03:35:04 +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.net
|
|
|
|
#disabled: 0.0.0.0 example.dev"
|
2016-02-24 02:14:21 +00:00
|
|
|
_compare "'${_expected}'" "'$(cat "${HOSTS_PATH}")'"
|
|
|
|
[[ "$(cat "${HOSTS_PATH}")" != "${_original}" ]]
|
|
|
|
[[ "$(cat "${HOSTS_PATH}")" == "${_expected}" ]]
|
2016-01-27 03:35:04 +00:00
|
|
|
}
|
|
|
|
|
|
|
|
@test "\`remove <hostname>\` prints feedback." {
|
|
|
|
{
|
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
|
2016-01-27 03:35:04 +00:00
|
|
|
}
|
|
|
|
|
2016-02-24 02:14:21 +00:00
|
|
|
run "${_HOSTS}" remove example.com --force
|
2018-04-15 21:55:26 +00:00
|
|
|
printf "\${status}: %s\\n" "${status}"
|
|
|
|
printf "\${output}: '%s'\\n" "${output}"
|
2016-01-27 03:35:04 +00:00
|
|
|
_expected="\
|
|
|
|
Removed:
|
|
|
|
0.0.0.0 example.com
|
|
|
|
127.0.0.2 example.com"
|
2016-02-24 02:14:21 +00:00
|
|
|
[[ "${output}" == "${_expected}" ]]
|
2016-01-27 03:35:04 +00:00
|
|
|
}
|
|
|
|
|
|
|
|
# help ########################################################################
|
|
|
|
|
|
|
|
@test "\`help remove\` exits with status 0." {
|
2016-02-24 02:14:21 +00:00
|
|
|
run "${_HOSTS}" help remove
|
|
|
|
[[ ${status} -eq 0 ]]
|
2016-01-27 03:35:04 +00:00
|
|
|
}
|
|
|
|
|
|
|
|
@test "\`help remove\` prints help information." {
|
2016-02-24 02:14:21 +00:00
|
|
|
run "${_HOSTS}" help remove
|
2018-04-15 21:55:26 +00:00
|
|
|
printf "\${status}: %s\\n" "${status}"
|
|
|
|
printf "\${output}: '%s'\\n" "${output}"
|
2016-01-27 03:35:04 +00:00
|
|
|
[[ "${lines[0]}" == "Usage:" ]]
|
2016-01-27 04:25:16 +00:00
|
|
|
[[ "${lines[1]}" == " hosts remove (<ip> | <hostname> | <search string>) [--force]" ]]
|
2016-01-27 03:35:04 +00:00
|
|
|
}
|