func TestSort(t *testing.T) { // Getting 100% coverage on this is kinda tricky. It's pretty close and // this is already too long. hosts := hostess.NewHostlist() hosts.Add(hostess.NewHostname("google.com", "8.8.8.8", true)) hosts.Add(hostess.NewHostname("google3.com", "::1", true)) hosts.Add(hostess.NewHostname(domain, ip, false)) hosts.Add(hostess.NewHostname("google2.com", "8.8.4.4", true)) hosts.Add(hostess.NewHostname("blah2", "10.20.1.1", true)) hosts.Add(hostess.NewHostname("blah3", "10.20.1.1", true)) hosts.Add(hostess.NewHostname("blah33", "10.20.1.1", true)) hosts.Add(hostess.NewHostname("blah", "10.20.1.1", true)) hosts.Add(hostess.NewHostname("hostname", "127.0.1.1", true)) hosts.Add(hostess.NewHostname("devsite", "127.0.0.1", true)) hosts.Sort() CheckIndexDomain(t, 0, "localhost", hosts) CheckIndexDomain(t, 1, "devsite", hosts) CheckIndexDomain(t, 2, "hostname", hosts) CheckIndexDomain(t, 3, "google2.com", hosts) CheckIndexDomain(t, 4, "google.com", hosts) CheckIndexDomain(t, 5, "blah", hosts) CheckIndexDomain(t, 6, "blah2", hosts) CheckIndexDomain(t, 7, "blah3", hosts) CheckIndexDomain(t, 8, "blah33", hosts) CheckIndexDomain(t, 9, "google3.com", hosts) }
func TestContainsDomainIp(t *testing.T) { hosts := hostess.NewHostlist() hosts.Add(hostess.NewHostname(domain, ip, false)) hosts.Add(hostess.NewHostname("google.com", "8.8.8.8", true)) if !hosts.ContainsDomain(domain) { t.Errorf("Expected to find %s", domain) } const extraneousDomain = "yahoo.com" if hosts.ContainsDomain(extraneousDomain) { t.Errorf("Did not expect to find %s", extraneousDomain) } var expectedIP = net.ParseIP(ip) if !hosts.ContainsIP(expectedIP) { t.Errorf("Expected to find %s", ip) } var extraneousIP = net.ParseIP("1.2.3.4") if hosts.ContainsIP(extraneousIP) { t.Errorf("Did not expect to find %s", extraneousIP) } expectedHostname := hostess.NewHostname(domain, ip, true) if !hosts.Contains(expectedHostname) { t.Errorf("Expected to find %s", expectedHostname) } extraneousHostname := hostess.NewHostname("yahoo.com", "4.3.2.1", false) if hosts.Contains(extraneousHostname) { t.Errorf("Did not expect to find %s", extraneousHostname) } }
func ExampleHostlist_1() { hosts := hostess.NewHostlist() hosts.Add(hostess.NewHostname("google.com", "127.0.0.1", false)) hosts.Add(hostess.NewHostname("google.com", "::1", true)) fmt.Printf("%s\n", hosts.Format()) // Output: // # 127.0.0.1 google.com // ::1 google.com }
func TestEqualIP(t *testing.T) { a := hostess.NewHostname("localhost", "127.0.0.1", true) c := hostess.NewHostname("localhost", "127.0.1.1", false) ip := net.ParseIP("127.0.0.1") if !a.EqualIP(ip) { t.Errorf("%s and %s should be equal", a.IP, ip) } if a.EqualIP(c.IP) { t.Errorf("%s and %s should not be equal", a.IP, c.IP) } }
func TestEqual(t *testing.T) { a := hostess.NewHostname("localhost", "127.0.0.1", true) b := hostess.NewHostname("localhost", "127.0.0.1", false) c := hostess.NewHostname("localhost", "127.0.1.1", false) if !a.Equal(b) { t.Errorf("%s and %s should be equal", a, b) } if a.Equal(c) { t.Errorf("%s and %s should not be equal", a, c) } }
func TestFormat(t *testing.T) { hosts := hostess.NewHostlist() hosts.Add(hostess.NewHostname(domain, ip, false)) hosts.Add(hostess.NewHostname("google.com", "8.8.8.8", true)) expected := `# 127.0.0.1 localhost 8.8.8.8 google.com ` if string(hosts.Format()) != expected { t.Error("Formatted hosts list is not formatted correctly") } }
func TestDump(t *testing.T) { hosts := hostess.NewHostlist() hosts.Add(hostess.NewHostname("google.com", "127.0.0.1", false)) hosts.Add(hostess.NewHostname("google.com", "::1", true)) expected := []byte(hostsjson) actual, _ := hosts.Dump() if !bytes.Equal(actual, expected) { t.Errorf("JSON output did not match expected output: %s", Diff(string(expected), string(actual))) } }
func TestIsValid(t *testing.T) { a := hostess.NewHostname("localhost", "127.0.0.1", true) d := hostess.NewHostname("", "127.0.0.1", true) e := hostess.NewHostname("localhost", "localhost", true) if !a.IsValid() { t.Errorf("%s should be a valid hostname", a) } if d.IsValid() { t.Errorf("%s should be invalid because the name is blank", d) } if e.IsValid() { t.Errorf("%s should be invalid because the ip is malformed", e) } }
func TestAddConflict(t *testing.T) { hostnameA := hostess.NewHostname("mysite", "1.2.3.4", true) hostnameB := hostess.NewHostname("mysite", "5.2.3.4", false) list := hostess.NewHostlist() list.Add(hostnameA) err := list.Add(hostnameB) assert.NotNil(t, err, "Expected conflict error") if !(*list)[0].Equal(hostnameB) { t.Error("Expected second hostname to overwrite") } if (*list)[0].Enabled { t.Error("Expected second hostname to be disabled") } }
func TestParseLine(t *testing.T) { var hosts hostess.Hostlist // Blank line hosts = hostess.ParseLine("") if len(hosts) > 0 { t.Error("Expected to find zero hostnames") } // Comment hosts = hostess.ParseLine("# The following lines are desirable for IPv6 capable hosts") if len(hosts) > 0 { t.Error("Expected to find zero hostnames") } // Single word comment hosts = hostess.ParseLine("#blah") if len(hosts) > 0 { t.Error("Expected to find zero hostnames") } hosts = hostess.ParseLine("#66.33.99.11 test.domain.com") if !hosts.Contains(hostess.NewHostname("test.domain.com", "66.33.99.11", false)) || len(hosts) != 1 { t.Error("Expected to find test.domain.com (disabled)") } hosts = hostess.ParseLine("# 66.33.99.11 test.domain.com domain.com") if !hosts.Contains(hostess.NewHostname("test.domain.com", "66.33.99.11", false)) || !hosts.Contains(hostess.NewHostname("domain.com", "66.33.99.11", false)) || len(hosts) != 2 { t.Error("Expected to find domain.com and test.domain.com (disabled)") t.Errorf("Found %s", hosts) } // Not Commented stuff hosts = hostess.ParseLine("255.255.255.255 broadcasthost test.domain.com domain.com") if !hosts.Contains(hostess.NewHostname("broadcasthost", "255.255.255.255", true)) || !hosts.Contains(hostess.NewHostname("test.domain.com", "255.255.255.255", true)) || !hosts.Contains(hostess.NewHostname("domain.com", "255.255.255.255", true)) || len(hosts) != 3 { t.Error("Expected to find broadcasthost, domain.com, and test.domain.com (enabled)") } // Ipv6 stuff hosts = hostess.ParseLine("::1 localhost") if !hosts.Contains(hostess.NewHostname("localhost", "::1", true)) || len(hosts) != 1 { t.Error("Expected to find localhost ipv6 (enabled)") } hosts = hostess.ParseLine("ff02::1 ip6-allnodes") if !hosts.Contains(hostess.NewHostname("ip6-allnodes", "ff02::1", true)) || len(hosts) != 1 { t.Error("Expected to find ip6-allnodes ipv6 (enabled)") } }
func TestApply(t *testing.T) { hosts := hostess.NewHostlist() hosts.Apply([]byte(hostsjson)) hostnameA := hostess.NewHostname("google.com", "127.0.0.1", false) if !hosts.Contains(hostnameA) { t.Errorf("Expected to find %s", hostnameA.Format()) } hostnameB := hostess.NewHostname("google.com", "::1", true) if !hosts.Contains(hostnameB) { t.Errorf("Expected to find %s", hostnameB.Format()) } hosts.Apply([]byte(hostsjson)) if hosts.Len() != 2 { t.Error("Hostslist contains the wrong number of items, expected 2") } }
func TestRemoveDomain(t *testing.T) { hosts := hostess.NewHostlist() h1 := hostess.NewHostname("google.com", "127.0.0.1", false) h2 := hostess.NewHostname("google.com", "::1", true) hosts.Add(h1) hosts.Add(h2) hosts.RemoveDomainV("google.com", 4) if hosts.Contains(h1) { t.Error("Should not contain ipv4 hostname") } if !hosts.Contains(h2) { t.Error("Should still contain ipv6 hostname") } hosts.RemoveDomainV("google.com", 6) if len(*hosts) != 0 { t.Error("Should no longer contain any hostnames") } }
func TestAddDuplicate(t *testing.T) { list := hostess.NewHostlist() hostname := hostess.NewHostname("mysite", "1.2.3.4", false) err := list.Add(hostname) assert.Nil(t, err, "Expected no errors when adding a hostname for the first time") hostname.Enabled = true err = list.Add(hostname) assert.NotNil(t, err, "Expected error when adding a duplicate") assert.True(t, (*list)[0].Enabled, "Expected hostname to be in enabled state") }
func TestRemove(t *testing.T) { hosts := hostess.NewHostlist() hosts.Add(hostess.NewHostname(domain, ip, false)) hosts.Add(hostess.NewHostname("google.com", "8.8.8.8", true)) removed := hosts.Remove(1) if removed != 1 { t.Error("Expected to remove 1 item") } if len(*hosts) > 1 { t.Errorf("Expected hostlist to have 1 item, found %d", len(*hosts)) } if hosts.ContainsDomain("google.com") { t.Errorf("Expected not to find google.com") } hosts.Add(hostess.NewHostname(domain, "::1", enabled)) removed = hosts.RemoveDomain(domain) if removed != 2 { t.Error("Expected to remove 2 items") } }
func TestHostname(t *testing.T) { h := hostess.NewHostname(domain, ip, enabled) if h.Domain != domain { t.Errorf("Domain should be %s", domain) } if !h.IP.Equal(net.ParseIP(ip)) { t.Errorf("IP should be %s", ip) } if h.Enabled != enabled { t.Errorf("Enabled should be %t", enabled) } }
func TestFormatHostname(t *testing.T) { hostname := hostess.NewHostname(domain, ip, enabled) const exp_enabled = "127.0.0.1 localhost" if hostname.Format() != exp_enabled { t.Errorf("Hostname format doesn't match desired output: %s", Diff(hostname.Format(), exp_enabled)) } hostname.Enabled = false const exp_disabled = "# 127.0.0.1 localhost" if hostname.Format() != exp_disabled { t.Errorf("Hostname format doesn't match desired output: %s", Diff(hostname.Format(), exp_disabled)) } }
func TestFormatEnabled(t *testing.T) { hostname := hostess.NewHostname(domain, ip, enabled) const expectedOn = "(On)" if hostname.FormatEnabled() != expectedOn { t.Errorf("Expected hostname to be turned %s", expectedOn) } const expectedHumanOn = "localhost -> 127.0.0.1 (On)" if hostname.FormatHuman() != expectedHumanOn { t.Errorf("Unexpected output%s", Diff(expectedHumanOn, hostname.FormatHuman())) } hostname.Enabled = false if hostname.FormatEnabled() != "(Off)" { t.Error("Expected hostname to be turned (Off)") } }
func TestLoadHostfile(t *testing.T) { hostfile := hostess.NewHostfile() hostfile.Read() if !strings.Contains(string(hostfile.GetData()), domain) { t.Errorf("Expected to find %s", domain) } hostfile.Parse() on := enabled if runtime.GOOS == "windows" { on = false } hostname := hostess.NewHostname(domain, ip, on) found := hostfile.Hosts.Contains(hostname) if !found { t.Errorf("Expected to find %#v", hostname) } }
func TestFormatHostfile(t *testing.T) { // 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 ` hostfile := hostess.NewHostfile() hostfile.Path = "./hosts" hostfile.Hosts.Add(hostess.NewHostname("localhost", "127.0.0.1", true)) hostfile.Hosts.Add(hostess.NewHostname("ip-10-37-12-18", "127.0.1.1", true)) hostfile.Hosts.Add(hostess.NewHostname("devsite", "127.0.0.1", true)) hostfile.Hosts.Add(hostess.NewHostname("google.com", "8.8.8.8", false)) hostfile.Hosts.Add(hostess.NewHostname("devsite.com", "10.37.12.18", true)) hostfile.Hosts.Add(hostess.NewHostname("m.devsite.com", "10.37.12.18", true)) f := string(hostfile.Format()) if f != expected { t.Errorf("Hostfile output is not formatted correctly: %s", Diff(expected, f)) } }