2015-02-01 02:20:42 +00:00
|
|
|
package hostess
|
|
|
|
|
|
|
|
import (
|
|
|
|
"testing"
|
|
|
|
)
|
|
|
|
|
2015-02-01 08:38:28 +00:00
|
|
|
const asserts = `
|
|
|
|
--- Expected ---
|
|
|
|
%s
|
|
|
|
---- Actual ----
|
|
|
|
%s`
|
|
|
|
|
2015-02-01 02:20:42 +00:00
|
|
|
const ipv4_pass = `
|
|
|
|
127.0.0.1
|
|
|
|
127.0.1.1
|
|
|
|
10.200.30.50
|
|
|
|
99.99.99.99
|
|
|
|
999.999.999.999
|
|
|
|
0.1.1.0
|
|
|
|
`
|
|
|
|
|
|
|
|
const ipv4_fail = `
|
|
|
|
1234.1.1.1
|
|
|
|
123.5.6
|
|
|
|
12.12
|
|
|
|
76.76.67.67.45
|
|
|
|
`
|
|
|
|
|
|
|
|
const ipv6 = ``
|
|
|
|
|
2015-02-01 03:22:07 +00:00
|
|
|
const domain = "localhost"
|
|
|
|
const ip = "127.0.0.1"
|
|
|
|
const enabled = true
|
|
|
|
|
2015-02-01 02:20:42 +00:00
|
|
|
func TestHostname(t *testing.T) {
|
|
|
|
|
|
|
|
h := Hostname{}
|
|
|
|
h.Domain = domain
|
|
|
|
h.Ip = ip
|
|
|
|
h.Enabled = enabled
|
|
|
|
|
|
|
|
if h.Domain != domain {
|
2015-02-01 03:22:07 +00:00
|
|
|
t.Errorf("Domain should be %s", domain)
|
2015-02-01 02:20:42 +00:00
|
|
|
}
|
|
|
|
if h.Ip != ip {
|
2015-02-01 03:22:07 +00:00
|
|
|
t.Errorf("Domain should be %s", ip)
|
2015-02-01 02:20:42 +00:00
|
|
|
}
|
|
|
|
if h.Enabled != enabled {
|
2015-02-01 03:22:07 +00:00
|
|
|
t.Errorf("Enabled should be %s", enabled)
|
2015-02-01 02:20:42 +00:00
|
|
|
}
|
|
|
|
}
|
|
|
|
|
|
|
|
func TestGetHostsPath(t *testing.T) {
|
|
|
|
path := GetHostsPath()
|
|
|
|
const expected = "/etc/hosts"
|
|
|
|
if path != expected {
|
|
|
|
t.Error("Hosts path should be " + expected)
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
|
|
|
func TestHostfile(t *testing.T) {
|
2015-02-01 08:38:28 +00:00
|
|
|
hostfile := NewHostfile("./hosts")
|
2015-02-01 03:22:07 +00:00
|
|
|
hostfile.Add(Hostname{domain, ip, true})
|
|
|
|
if hostfile.Hosts[domain].Ip != ip {
|
|
|
|
t.Errorf("Hostsfile should have %s pointing to %s", domain, ip)
|
|
|
|
}
|
|
|
|
|
|
|
|
hostfile.Disable(domain)
|
|
|
|
if hostfile.Hosts[domain].Enabled != false {
|
|
|
|
t.Errorf("%s should be disabled", domain)
|
|
|
|
}
|
|
|
|
|
|
|
|
hostfile.Enable(domain)
|
|
|
|
if hostfile.Hosts[domain].Enabled != true {
|
|
|
|
t.Errorf("%s should be enabled", domain)
|
|
|
|
}
|
|
|
|
|
|
|
|
hostfile.Delete(domain)
|
|
|
|
if hostfile.Hosts[domain] != nil {
|
|
|
|
t.Errorf("Did not expect to find %s", domain)
|
|
|
|
}
|
2015-02-01 08:38:28 +00:00
|
|
|
}
|
|
|
|
|
|
|
|
func TestHostFileDuplicates(t *testing.T) {
|
|
|
|
hostfile := NewHostfile("./hosts")
|
|
|
|
|
|
|
|
const exp_duplicate = "Duplicate hostname entry for localhost -> 127.0.0.1"
|
|
|
|
hostfile.Add(Hostname{domain, ip, true})
|
|
|
|
err := hostfile.Add(Hostname{domain, ip, true})
|
|
|
|
if err.Error() != exp_duplicate {
|
|
|
|
t.Errorf(asserts, exp_duplicate, err)
|
|
|
|
}
|
|
|
|
|
|
|
|
const exp_conflict = "Conflicting hostname entries for localhost -> 127.0.1.1 and -> 127.0.0.1"
|
|
|
|
err2 := hostfile.Add(Hostname{domain, "127.0.1.1", true})
|
|
|
|
if err2.Error() != exp_conflict {
|
|
|
|
t.Errorf(asserts, exp_conflict, err2)
|
|
|
|
}
|
|
|
|
|
|
|
|
}
|
|
|
|
|
|
|
|
func TestFormatHostname(t *testing.T) {
|
|
|
|
hostname := Hostname{domain, ip, enabled}
|
|
|
|
|
|
|
|
const exp_enabled = "127.0.0.1 localhost"
|
|
|
|
if hostname.Format() != exp_enabled {
|
|
|
|
t.Errorf(asserts, hostname.Format(), exp_enabled)
|
|
|
|
}
|
2015-02-01 03:22:07 +00:00
|
|
|
|
2015-02-01 08:38:28 +00:00
|
|
|
hostname.Enabled = false
|
|
|
|
const exp_disabled = "# 127.0.0.1 localhost"
|
|
|
|
if hostname.Format() != exp_disabled {
|
|
|
|
t.Errorf(asserts, hostname.Format(), exp_disabled)
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
|
|
|
func TestFormatHostfile(t *testing.T) {
|
|
|
|
// t.Skip("Not implemented")
|
|
|
|
const expected = `127.0.0.1 localhost devsite
|
|
|
|
127.0.1.1 ip-10-37-12-18
|
|
|
|
# 8.8.8.8 google.com
|
|
|
|
10.37.12.18 devsite.com m.devsite.com`
|
|
|
|
|
|
|
|
hostfile := NewHostfile("./hosts")
|
|
|
|
hostfile.Add(Hostname{"localhost", "127.0.0.1", true})
|
|
|
|
hostfile.Add(Hostname{"ip-10-37-12-18", "127.0.1.1", true})
|
|
|
|
hostfile.Add(Hostname{"devsite", "127.0.0.1", true})
|
|
|
|
hostfile.Add(Hostname{"google.com", "8.8.8.8", false})
|
|
|
|
hostfile.Add(Hostname{"devsite.com", "10.37.12.18", true})
|
|
|
|
hostfile.Add(Hostname{"m.devsite.com", "10.37.12.18", true})
|
|
|
|
if hostfile.Format() != expected {
|
|
|
|
t.Errorf(asserts, expected, hostfile.Format())
|
|
|
|
}
|
2015-02-01 02:20:42 +00:00
|
|
|
}
|