|
|
|
@ -115,11 +115,14 @@ func TestFormatHostname(t *testing.T) {
|
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
func TestFormatHostfile(t *testing.T) {
|
|
|
|
|
// t.Skip("Not implemented")
|
|
|
|
|
// The sort order here is a bit weird.
|
|
|
|
|
// 1. We want localhost entries at the top
|
|
|
|
|
// 2. The rest are sorted by IP as STRINGS, not numeric values, so 10
|
|
|
|
|
// precedes 8
|
|
|
|
|
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`
|
|
|
|
|
10.37.12.18 devsite.com m.devsite.com
|
|
|
|
|
# 8.8.8.8 google.com`
|
|
|
|
|
|
|
|
|
|
hostfile := NewHostfile("./hosts")
|
|
|
|
|
hostfile.Add(Hostname{"localhost", "127.0.0.1", true})
|
|
|
|
@ -128,7 +131,40 @@ func TestFormatHostfile(t *testing.T) {
|
|
|
|
|
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())
|
|
|
|
|
f := hostfile.Format()
|
|
|
|
|
if f != expected {
|
|
|
|
|
t.Errorf(asserts, expected, f)
|
|
|
|
|
}
|
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
func TestTrimWS(t *testing.T) {
|
|
|
|
|
const expected = ` candy
|
|
|
|
|
|
|
|
|
|
`
|
|
|
|
|
got := TrimWS(expected)
|
|
|
|
|
if got != "candy" {
|
|
|
|
|
t.Errorf(asserts, expected, got)
|
|
|
|
|
}
|
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
func TestListDomainsByIp(t *testing.T) {
|
|
|
|
|
hostfile := NewHostfile("./hosts")
|
|
|
|
|
hostfile.Add(Hostname{"devsite.com", "10.37.12.18", true})
|
|
|
|
|
hostfile.Add(Hostname{"m.devsite.com", "10.37.12.18", true})
|
|
|
|
|
hostfile.Add(Hostname{"google.com", "8.8.8.8", false})
|
|
|
|
|
|
|
|
|
|
names := hostfile.ListDomainsByIp("10.37.12.18")
|
|
|
|
|
if !(names[0] == "devsite.com" && names[1] == "m.devsite.com") {
|
|
|
|
|
t.Errorf("Expected devsite.com and m.devsite.com. Got %s", names)
|
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
hostfile2 := NewHostfile("./hosts")
|
|
|
|
|
hostfile2.Add(Hostname{"localhost", "127.0.0.1", true})
|
|
|
|
|
hostfile2.Add(Hostname{"ip-10-37-12-18", "127.0.1.1", true})
|
|
|
|
|
hostfile2.Add(Hostname{"devsite", "127.0.0.1", true})
|
|
|
|
|
|
|
|
|
|
names2 := hostfile2.ListDomainsByIp("127.0.0.1")
|
|
|
|
|
if !(names2[0] == "localhost" && names2[1] == "devsite") {
|
|
|
|
|
t.Errorf("Expected localhost and devsite. Got %s", names2)
|
|
|
|
|
}
|
|
|
|
|
}
|
|
|
|
|