2015-02-24 10:15:23 +00:00
|
|
|
package hostess_test
|
|
|
|
|
|
|
|
import (
|
2015-03-02 10:20:34 +00:00
|
|
|
"fmt"
|
2015-02-24 10:15:23 +00:00
|
|
|
"github.com/cbednarski/hostess"
|
2015-02-24 12:17:31 +00:00
|
|
|
"net"
|
2015-02-24 10:15:23 +00:00
|
|
|
"testing"
|
|
|
|
)
|
|
|
|
|
2015-03-22 01:11:09 +00:00
|
|
|
func TestMakeSurrogateIP(t *testing.T) {
|
|
|
|
const orig = "127.0.0.1"
|
|
|
|
const expected1 = "0.0.0.1"
|
|
|
|
IP1 := string(hostess.MakeSurrogateIP(net.IP(orig)))
|
|
|
|
if IP1 != expected1 {
|
|
|
|
t.Errorf("Expected %s to convert to %s; got %s", orig, expected1, IP1)
|
|
|
|
}
|
|
|
|
|
|
|
|
const expected2 = "10.20.30.40"
|
|
|
|
IP2 := string(hostess.MakeSurrogateIP(net.IP(expected2)))
|
|
|
|
if IP2 != expected2 {
|
|
|
|
t.Errorf("Expected %s to remain unchanged; got %s", expected2, IP2)
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
2015-02-24 10:15:23 +00:00
|
|
|
func TestContainsDomainIp(t *testing.T) {
|
2015-02-25 10:33:18 +00:00
|
|
|
hosts := hostess.NewHostlist()
|
|
|
|
hosts.Add(hostess.NewHostname(domain, ip, false))
|
|
|
|
hosts.Add(hostess.NewHostname("google.com", "8.8.8.8", true))
|
2015-02-24 10:15:23 +00:00
|
|
|
|
2015-02-25 10:33:18 +00:00
|
|
|
if !hosts.ContainsDomain(domain) {
|
2015-02-24 10:15:23 +00:00
|
|
|
t.Errorf("Expected to find %s", domain)
|
|
|
|
}
|
|
|
|
|
|
|
|
const extra_domain = "yahoo.com"
|
2015-02-25 10:33:18 +00:00
|
|
|
if hosts.ContainsDomain(extra_domain) {
|
2015-02-24 10:15:23 +00:00
|
|
|
t.Errorf("Did not expect to find %s", extra_domain)
|
|
|
|
}
|
|
|
|
|
2015-02-25 10:33:18 +00:00
|
|
|
var first_ip = net.ParseIP(ip)
|
2015-03-02 08:22:05 +00:00
|
|
|
if !hosts.ContainsIP(first_ip) {
|
2015-02-24 10:15:23 +00:00
|
|
|
t.Errorf("Expected to find %s", ip)
|
|
|
|
}
|
|
|
|
|
2015-02-24 12:17:31 +00:00
|
|
|
var extra_ip = net.ParseIP("1.2.3.4")
|
2015-03-02 08:22:05 +00:00
|
|
|
if hosts.ContainsIP(extra_ip) {
|
2015-02-24 10:15:23 +00:00
|
|
|
t.Errorf("Did not expect to find %s", extra_ip)
|
|
|
|
}
|
|
|
|
|
2015-02-25 10:33:18 +00:00
|
|
|
hostname := hostess.NewHostname(domain, ip, true)
|
2015-03-02 07:15:02 +00:00
|
|
|
if !hosts.Contains(hostname) {
|
2015-02-24 10:15:23 +00:00
|
|
|
t.Errorf("Expected to find %s", hostname)
|
|
|
|
}
|
|
|
|
|
2015-02-25 10:33:18 +00:00
|
|
|
extra_hostname := hostess.NewHostname("yahoo.com", "4.3.2.1", false)
|
2015-03-02 07:15:02 +00:00
|
|
|
if hosts.Contains(extra_hostname) {
|
2015-02-24 10:15:23 +00:00
|
|
|
t.Errorf("Did not expect to find %s", extra_hostname)
|
|
|
|
}
|
|
|
|
}
|
2015-02-25 10:33:18 +00:00
|
|
|
|
2015-03-02 02:18:43 +00:00
|
|
|
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
|
|
|
|
`
|
2015-03-22 00:18:54 +00:00
|
|
|
if string(hosts.Format()) != expected {
|
2015-03-02 02:18:43 +00:00
|
|
|
t.Error("Formatted hosts list is not formatted correctly")
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
2015-02-25 10:33:18 +00:00
|
|
|
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))
|
|
|
|
|
2015-03-02 02:10:01 +00:00
|
|
|
hosts.Remove(1)
|
|
|
|
if len(*hosts) > 1 {
|
|
|
|
t.Errorf("Expected hostlist to have 1 item, found %d", len(*hosts))
|
|
|
|
}
|
2015-02-25 10:33:18 +00:00
|
|
|
|
|
|
|
if hosts.ContainsDomain("google.com") {
|
2015-02-25 10:37:57 +00:00
|
|
|
t.Errorf("Expected not to find google.com")
|
2015-02-25 10:33:18 +00:00
|
|
|
}
|
|
|
|
}
|
2015-03-02 02:18:43 +00:00
|
|
|
|
2015-03-02 05:54:51 +00:00
|
|
|
func TestRemoveDomain(t *testing.T) {
|
2015-03-02 02:18:43 +00:00
|
|
|
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)
|
|
|
|
|
2015-03-02 05:54:51 +00:00
|
|
|
hosts.RemoveDomainV("google.com", 4)
|
2015-03-02 07:15:02 +00:00
|
|
|
if hosts.Contains(h1) {
|
2015-03-02 02:18:43 +00:00
|
|
|
t.Error("Should not contain ipv4 hostname")
|
|
|
|
}
|
2015-03-02 07:15:02 +00:00
|
|
|
if !hosts.Contains(h2) {
|
2015-03-02 02:18:43 +00:00
|
|
|
t.Error("Should still contain ipv6 hostname")
|
|
|
|
}
|
|
|
|
|
2015-03-02 05:54:51 +00:00
|
|
|
hosts.RemoveDomainV("google.com", 6)
|
2015-03-02 02:18:43 +00:00
|
|
|
if len(*hosts) != 0 {
|
|
|
|
t.Error("Should no longer contain any hostnames")
|
|
|
|
}
|
|
|
|
}
|
2015-03-02 07:15:02 +00:00
|
|
|
|
|
|
|
func TestSort(t *testing.T) {
|
2015-03-02 09:46:12 +00:00
|
|
|
// Getting 100% coverage on this is kinda tricky. It's pretty close and
|
|
|
|
// this is already too long.
|
|
|
|
|
2015-03-02 07:15:02 +00:00
|
|
|
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))
|
2015-03-02 09:46:12 +00:00
|
|
|
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))
|
2015-03-02 07:15:02 +00:00
|
|
|
|
|
|
|
hosts.Sort()
|
|
|
|
if (*hosts)[0].Domain != "localhost" {
|
|
|
|
t.Error("Expected localhost to be first")
|
2015-03-02 09:46:12 +00:00
|
|
|
t.Error(hosts.Format())
|
2015-03-02 07:15:02 +00:00
|
|
|
}
|
|
|
|
if (*hosts)[1].Domain != "google2.com" {
|
|
|
|
t.Error("Expected google2 to be second")
|
2015-03-02 09:46:12 +00:00
|
|
|
t.Error(hosts.Format())
|
2015-03-02 07:15:02 +00:00
|
|
|
}
|
|
|
|
if (*hosts)[2].Domain != "google.com" {
|
|
|
|
t.Error("Expected google3 to be third")
|
2015-03-02 09:46:12 +00:00
|
|
|
t.Error(hosts.Format())
|
|
|
|
}
|
|
|
|
if (*hosts)[3].Domain != "blah" {
|
|
|
|
t.Error("Expected blah to be fourth")
|
|
|
|
t.Error(hosts.Format())
|
|
|
|
}
|
|
|
|
if (*hosts)[4].Domain != "blah2" {
|
|
|
|
t.Error("Expected blah2 to be fifth")
|
|
|
|
t.Error(hosts.Format())
|
|
|
|
}
|
|
|
|
if (*hosts)[5].Domain != "blah3" {
|
|
|
|
t.Error("Expected blah3 to be sixth")
|
|
|
|
t.Error(hosts.Format())
|
|
|
|
}
|
|
|
|
if (*hosts)[6].Domain != "blah33" {
|
|
|
|
t.Error("Expected blah33 to be seventh")
|
|
|
|
t.Error(hosts.Format())
|
2015-03-02 07:15:02 +00:00
|
|
|
}
|
2015-03-02 09:46:12 +00:00
|
|
|
// IPv6 Domains
|
|
|
|
if (*hosts)[7].Domain != "google3.com" {
|
|
|
|
t.Error("Expected google3 to be eigth")
|
|
|
|
t.Error(hosts.Format())
|
2015-03-02 07:15:02 +00:00
|
|
|
}
|
|
|
|
}
|
2015-03-02 10:20:34 +00:00
|
|
|
|
|
|
|
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))
|
|
|
|
|
2015-03-22 00:18:54 +00:00
|
|
|
fmt.Printf("%s\n", hosts.Format())
|
2015-03-02 10:20:34 +00:00
|
|
|
// Output:
|
|
|
|
// # 127.0.0.1 google.com
|
|
|
|
// ::1 google.com
|
|
|
|
}
|