Some preliminary work and a lot of commented code

pull/13/head
Chris Bednarski 9 years ago
parent 1fdc180a62
commit f5dea78ac3

@ -150,13 +150,13 @@ func Ls(c *cli.Context) {
}
}
for _, domain := range hostsfile.ListDomains() {
hostname := hostsfile.Hosts[domain]
fmt.Printf("%s -> %s %s\n",
StrPadRight(hostname.Domain, maxdomain),
StrPadRight(hostname.Ip.String(), maxip),
ShowEnabled(hostname.Enabled))
}
// for _, domain := range hostsfile.ListDomains() {
// hostname := hostsfile.Hosts[domain]
// fmt.Printf("%s -> %s %s\n",
// StrPadRight(hostname.Domain, maxdomain),
// StrPadRight(hostname.Ip.String(), maxip),
// ShowEnabled(hostname.Enabled))
// }
}
const fix_help = `Programmatically rewrite your hostsfile.

@ -1,7 +1,7 @@
package hostess
import (
"errors"
// "errors"
"fmt"
"io/ioutil"
"net"
@ -41,13 +41,13 @@ ff02::3 ip6-allhosts
// includes a list of Hostnames. Hostfile includes
type Hostfile struct {
Path string
Hosts map[string]*Hostname
Hosts Hostlist
data string
}
// NewHostFile creates a new Hostfile object from the specified file.
func NewHostfile(path string) *Hostfile {
return &Hostfile{path, make(map[string]*Hostname), ""}
return &Hostfile{path, Hostlist{}, ""}
}
func (h *Hostfile) Load() string {
@ -84,8 +84,8 @@ func TrimWS(s string) string {
return strings.Trim(s, " \n\t")
}
func ParseLine(line string) []*Hostname {
var hostnames []*Hostname
func ParseLine(line string) Hostlist {
var hostnames Hostlist
if len(line) == 0 {
return hostnames
@ -201,61 +201,61 @@ func (h *Hostfile) Format() string {
}
}
localhosts_keys := getSortedMapKeys(localhosts)
ips_keys := getSortedMapKeys(ips)
// localhosts_keys := getSortedMapKeys(localhosts)
// ips_keys := getSortedMapKeys(ips)
var out []string
for _, ip := range localhosts_keys {
enabled := ip
enabled_b := false
disabled := "# " + ip
disabled_b := false
IP := net.ParseIP(ip)
for _, domain := range h.ListDomainsByIp(IP) {
hostname := *h.Hosts[domain]
if hostname.Ip.Equal(IP) {
if hostname.Enabled {
enabled += " " + hostname.Domain
enabled_b = true
} else {
disabled += " " + hostname.Domain
disabled_b = true
}
}
}
if enabled_b {
out = append(out, enabled)
}
if disabled_b {
out = append(out, disabled)
}
}
for _, ip := range ips_keys {
enabled := ip
enabled_b := false
disabled := "# " + ip
disabled_b := false
IP := net.ParseIP(ip)
for _, domain := range h.ListDomainsByIp(IP) {
hostname := *h.Hosts[domain]
if hostname.Ip.Equal(IP) {
if hostname.Enabled {
enabled += " " + hostname.Domain
enabled_b = true
} else {
disabled += " " + hostname.Domain
disabled_b = true
}
}
}
if enabled_b {
out = append(out, enabled)
}
if disabled_b {
out = append(out, disabled)
}
}
// for _, ip := range localhosts_keys {
// enabled := ip
// enabled_b := false
// disabled := "# " + ip
// disabled_b := false
// IP := net.ParseIP(ip)
// for _, domain := range h.ListDomainsByIp(IP) {
// hostname := *h.Hosts[domain]
// if hostname.Ip.Equal(IP) {
// if hostname.Enabled {
// enabled += " " + hostname.Domain
// enabled_b = true
// } else {
// disabled += " " + hostname.Domain
// disabled_b = true
// }
// }
// }
// if enabled_b {
// out = append(out, enabled)
// }
// if disabled_b {
// out = append(out, disabled)
// }
// }
// for _, ip := range ips_keys {
// enabled := ip
// enabled_b := false
// disabled := "# " + ip
// disabled_b := false
// IP := net.ParseIP(ip)
// for _, domain := range h.ListDomainsByIp(IP) {
// hostname := *h.Hosts[domain]
// if hostname.Ip.Equal(IP) {
// if hostname.Enabled {
// enabled += " " + hostname.Domain
// enabled_b = true
// } else {
// disabled += " " + hostname.Domain
// disabled_b = true
// }
// }
// }
// if enabled_b {
// out = append(out, enabled)
// }
// if disabled_b {
// out = append(out, disabled)
// }
// }
return strings.Join(out, "\n")
}
@ -284,37 +284,37 @@ func (h *Hostfile) ContainsDomain(search string) bool {
}
func (h *Hostfile) Add(host *Hostname) error {
host_f, found := h.Hosts[host.Domain]
if found {
if host_f.Ip.Equal(host.Ip) {
return errors.New(fmt.Sprintf("Duplicate hostname entry for %s -> %s",
host.Domain, host.Ip))
} else {
return errors.New(fmt.Sprintf("Conflicting hostname entries for %s -> %s and -> %s",
host.Domain, host.Ip, host_f.Ip))
}
} else {
h.Hosts[host.Domain] = host
}
// host_f, found := h.Hosts[host.Domain]
// if found {
// if host_f.Ip.Equal(host.Ip) {
// return errors.New(fmt.Sprintf("Duplicate hostname entry for %s -> %s",
// host.Domain, host.Ip))
// } else {
// return errors.New(fmt.Sprintf("Conflicting hostname entries for %s -> %s and -> %s",
// host.Domain, host.Ip, host_f.Ip))
// }
// } else {
// h.Hosts[host.Domain] = host
// }
return nil
}
func (h *Hostfile) Delete(domain string) {
delete(h.Hosts, domain)
// delete(h.Hosts, domain)
}
func (h *Hostfile) Enable(domain string) {
_, ok := h.Hosts[domain]
if ok {
h.Hosts[domain].Enabled = true
}
// _, ok := h.Hosts[domain]
// if ok {
// h.Hosts[domain].Enabled = true
// }
}
func (h *Hostfile) Disable(domain string) {
_, ok := h.Hosts[domain]
if ok {
h.Hosts[domain].Enabled = false
}
// _, ok := h.Hosts[domain]
// if ok {
// h.Hosts[domain].Enabled = false
// }
}
func GetHostsPath() string {

@ -1,11 +1,11 @@
package hostess_test
import (
"github.com/cbednarski/hostess"
"net"
"strings"
"testing"
)
// import (
// "github.com/cbednarski/hostess"
// "net"
// "strings"
// "testing"
// )
const asserts = `
--- Expected ---
@ -31,185 +31,184 @@ const ipv4_fail = `
const ipv6 = ``
const domain = "localhost"
const ip = "127.0.0.1"
const enabled = true
var ip = net.ParseIP("127.0.0.1")
func TestGetHostsPath(t *testing.T) {
path := hostess.GetHostsPath()
const expected = "/etc/hosts"
if path != expected {
t.Error("Hosts path should be " + expected)
}
}
func TestHostfile(t *testing.T) {
hostfile := hostess.NewHostfile("./hosts")
hostfile.Add(&hostess.Hostname{domain, ip, true, false})
if !hostfile.Hosts[domain].Ip.Equal(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)
}
}
func TestHostFileDuplicates(t *testing.T) {
hostfile := hostess.NewHostfile("./hosts")
const exp_duplicate = "Duplicate hostname entry for localhost -> 127.0.0.1"
hostfile.Add(&hostess.Hostname{domain, ip, true, false})
err := hostfile.Add(&hostess.Hostname{domain, ip, true, false})
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(&hostess.Hostname{domain, net.ParseIP("127.0.1.1"), true, false})
if err2.Error() != exp_conflict {
t.Errorf(asserts, exp_conflict, err2)
}
// @TODO Add an additional test case here: Adding a domain twice with one
// enabled and one disabled should just add the domain once enabled.
}
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
10.37.12.18 devsite.com m.devsite.com
# 8.8.8.8 google.com`
hostfile := hostess.NewHostfile("./hosts")
hostfile.Add(&hostess.Hostname{"localhost", net.ParseIP("127.0.0.1"), true, false})
hostfile.Add(&hostess.Hostname{"ip-10-37-12-18", net.ParseIP("127.0.1.1"), true, false})
hostfile.Add(&hostess.Hostname{"devsite", net.ParseIP("127.0.0.1"), true, false})
hostfile.Add(&hostess.Hostname{"google.com", net.ParseIP("8.8.8.8"), false, false})
hostfile.Add(&hostess.Hostname{"devsite.com", net.ParseIP("10.37.12.18"), true, false})
hostfile.Add(&hostess.Hostname{"m.devsite.com", net.ParseIP("10.37.12.18"), true, false})
f := hostfile.Format()
if f != expected {
t.Errorf(asserts, expected, f)
}
}
func TestTrimWS(t *testing.T) {
const expected = ` candy
`
got := hostess.TrimWS(expected)
if got != "candy" {
t.Errorf(asserts, expected, got)
}
}
func TestListDomainsByIp(t *testing.T) {
hostfile := hostess.NewHostfile("./hosts")
hostfile.Add(&hostess.Hostname{"devsite.com", net.ParseIP("10.37.12.18"), true, false})
hostfile.Add(&hostess.Hostname{"m.devsite.com", net.ParseIP("10.37.12.18"), true, false})
hostfile.Add(&hostess.Hostname{"google.com", net.ParseIP("8.8.8.8"), false, false})
names := hostfile.ListDomainsByIp(net.ParseIP("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 := hostess.NewHostfile("./hosts")
hostfile2.Add(&hostess.Hostname{"localhost", net.ParseIP("127.0.0.1"), true, false})
hostfile2.Add(&hostess.Hostname{"ip-10-37-12-18", net.ParseIP("127.0.1.1"), true, false})
hostfile2.Add(&hostess.Hostname{"devsite", net.ParseIP("127.0.0.1"), true, false})
names2 := hostfile2.ListDomainsByIp(net.ParseIP("127.0.0.1"))
if !(names2[0] == "localhost" && names2[1] == "devsite") {
t.Errorf("Expected localhost and devsite. Got %s", names2)
}
}
func TestParseLine(t *testing.T) {
var hosts = []*hostess.Hostname{}
// 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 !hostess.ContainsHostname(hosts, &hostess.Hostname{"test.domain.com", net.ParseIP("66.33.99.11"), false, 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 !hostess.ContainsHostname(hosts, &hostess.Hostname{"test.domain.com", net.ParseIP("66.33.99.11"), false, false}) ||
!hostess.ContainsHostname(hosts, &hostess.Hostname{"domain.com", net.ParseIP("66.33.99.11"), false, 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 !hostess.ContainsHostname(hosts, &hostess.Hostname{"broadcasthost", net.ParseIP("255.255.255.255"), true, false}) ||
!hostess.ContainsHostname(hosts, &hostess.Hostname{"test.domain.com", net.ParseIP("255.255.255.255"), true, false}) ||
!hostess.ContainsHostname(hosts, &hostess.Hostname{"domain.com", net.ParseIP("255.255.255.255"), true, false}) ||
len(hosts) != 3 {
t.Error("Expected to find broadcasthost, domain.com, and test.domain.com (enabled)")
}
// Ipv6 stuff
hosts = hostess.ParseLine("::1 localhost")
if !hostess.ContainsHostname(hosts, &hostess.Hostname{"localhost", net.ParseIP("::1"), true, true}) ||
len(hosts) != 1 {
t.Error("Expected to find localhost ipv6 (enabled)")
}
hosts = hostess.ParseLine("ff02::1 ip6-allnodes")
if !hostess.ContainsHostname(hosts, &hostess.Hostname{"ip6-allnodes", net.ParseIP("ff02::1"), true, true}) ||
len(hosts) != 1 {
t.Error("Expected to find ip6-allnodes ipv6 (enabled)")
}
}
func TestLoadHostfile(t *testing.T) {
hostfile := hostess.NewHostfile(hostess.GetHostsPath())
data := hostfile.Load()
if !strings.Contains(data, domain) {
t.Errorf("Expected to find %s", domain)
}
hostfile.Parse()
hostname := hostess.Hostname{domain, ip, enabled, false}
_, found := hostfile.Hosts[hostname.Domain]
if !found {
t.Errorf("Expected to find %s", hostname)
}
}
// func TestGetHostsPath(t *testing.T) {
// path := hostess.GetHostsPath()
// const expected = "/etc/hosts"
// if path != expected {
// t.Error("Hosts path should be " + expected)
// }
// }
// func TestHostfile(t *testing.T) {
// hostfile := hostess.NewHostfile("./hosts")
// hostfile.Add(hostess.NewHostname(domain, ip, true))
// if !hostfile.Hosts[domain].Ip.Equal(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)
// }
// }
// func TestHostFileDuplicates(t *testing.T) {
// hostfile := hostess.NewHostfile("./hosts")
// const exp_duplicate = "Duplicate hostname entry for localhost -> 127.0.0.1"
// hostfile.Add(hostess.NewHostname(domain, ip, true))
// err := hostfile.Add(hostess.NewHostname(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(hostess.NewHostname(domain, "127.0.1.1", true))
// if err2.Error() != exp_conflict {
// t.Errorf(asserts, exp_conflict, err2)
// }
// // @TODO Add an additional test case here: Adding a domain twice with one
// // enabled and one disabled should just add the domain once enabled.
// }
// 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
// 10.37.12.18 devsite.com m.devsite.com
// # 8.8.8.8 google.com`
// hostfile := hostess.NewHostfile("./hosts")
// hostfile.Add(hostess.NewHostname("localhost", "127.0.0.1", true))
// hostfile.Add(hostess.NewHostname("ip-10-37-12-18", "127.0.1.1", true))
// hostfile.Add(hostess.NewHostname("devsite", "127.0.0.1", true))
// hostfile.Add(hostess.NewHostname("google.com", "8.8.8.8", false))
// hostfile.Add(hostess.NewHostname("devsite.com", "10.37.12.18", true))
// hostfile.Add(hostess.NewHostname("m.devsite.com", "10.37.12.18", true))
// f := hostfile.Format()
// if f != expected {
// t.Errorf(asserts, expected, f)
// }
// }
// func TestTrimWS(t *testing.T) {
// const expected = ` candy
// `
// got := hostess.TrimWS(expected)
// if got != "candy" {
// t.Errorf(asserts, expected, got)
// }
// }
// func TestListDomainsByIp(t *testing.T) {
// hostfile := hostess.NewHostfile("./hosts")
// hostfile.Add(hostess.NewHostname("devsite.com", "10.37.12.18", true))
// hostfile.Add(hostess.NewHostname("m.devsite.com", "10.37.12.18", true))
// hostfile.Add(hostess.NewHostname("google.com", "8.8.8.8", false))
// names := hostfile.ListDomainsByIp(net.ParseIP("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 := hostess.NewHostfile("./hosts")
// hostfile2.Add(hostess.NewHostname("localhost", "127.0.0.1", true))
// hostfile2.Add(hostess.NewHostname("ip-10-37-12-18", "127.0.1.1", true))
// hostfile2.Add(hostess.NewHostname("devsite", "127.0.0.1", true))
// names2 := hostfile2.ListDomainsByIp(net.ParseIP("127.0.0.1"))
// if !(names2[0] == "localhost" && names2[1] == "devsite") {
// t.Errorf("Expected localhost and devsite. Got %s", names2)
// }
// }
// func TestParseLine(t *testing.T) {
// var hosts = hostess.NewHostlist()
// // 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.ContainsHostname(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.ContainsHostname(hostess.NewHostname("test.domain.com", "66.33.99.11", false)) ||
// !hosts.ContainsHostname(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.ContainsHostname(hostess.NewHostname("broadcasthost", "255.255.255.255", true)) ||
// !hosts.ContainsHostname(hostess.NewHostname("test.domain.com", "255.255.255.255", true)) ||
// !hosts.ContainsHostname(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.ContainsHostname(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.ContainsHostname(hostess.NewHostname("ip6-allnodes", "ff02::1", true)) ||
// len(hosts) != 1 {
// t.Error("Expected to find ip6-allnodes ipv6 (enabled)")
// }
// }
// func TestLoadHostfile(t *testing.T) {
// hostfile := hostess.NewHostfile(hostess.GetHostsPath())
// data := hostfile.Load()
// if !strings.Contains(data, domain) {
// t.Errorf("Expected to find %s", domain)
// }
// hostfile.Parse()
// hostname := hostess.NewHostname(domain, ip, enabled)
// _, found := hostfile.Hosts[hostname.Domain]
// if !found {
// t.Errorf("Expected to find %s", hostname)
// }
// }

@ -1,6 +1,8 @@
package hostess
import (
"errors"
"fmt"
"net"
)
@ -10,8 +12,14 @@ import (
// Sort
// Other things that maybe implemented in hostfile
func ContainsHostname(hostnames []*Hostname, b *Hostname) bool {
for _, a := range hostnames {
type Hostlist []*Hostname
func NewHostlist() *Hostlist {
return &Hostlist{}
}
func (h *Hostlist) ContainsHostname(b *Hostname) bool {
for _, a := range *h {
if a.Equal(b) {
return true
}
@ -19,8 +27,8 @@ func ContainsHostname(hostnames []*Hostname, b *Hostname) bool {
return false
}
func ContainsDomain(hostnames []*Hostname, domain string) bool {
for _, hostname := range hostnames {
func (h *Hostlist) ContainsDomain(domain string) bool {
for _, hostname := range *h {
if hostname.Domain == domain {
return true
}
@ -28,11 +36,98 @@ func ContainsDomain(hostnames []*Hostname, domain string) bool {
return false
}
func ContainsIp(hostnames []*Hostname, ip net.IP) bool {
for _, hostname := range hostnames {
func (h *Hostlist) ContainsIp(ip net.IP) bool {
for _, hostname := range *h {
if hostname.EqualIp(ip) {
return true
}
}
return false
}
func (h *Hostlist) Add(host *Hostname) error {
for _, found := range *h {
if found.Equal(host) {
return errors.New(fmt.Sprintf("Duplicate hostname entry for %s -> %s",
host.Domain, host.Ip))
} else if found.Domain == host.Domain && found.Ipv6 == host.Ipv6 {
return errors.New(fmt.Sprintf("Conflicting hostname entries for %s -> %s and -> %s",
host.Domain, host.Ip, found.Ip))
}
}
return nil
}
func (h *Hostlist) Get(host *Hostname) *Hostname {
for _, found := range *h {
if found.Equal(host) {
return found
}
}
return nil
}
func (h *Hostlist) IndexOf(host *Hostname) int {
for index, found := range *h {
if found.Equal(host) {
return index
}
}
return -1
}
func (h *Hostlist) IndexOfDomainIpv4(domain string) int {
for index, found := range *h {
if found.Domain == domain && found.Ipv6 == false {
return index
}
}
return -1
}
func (h *Hostlist) IndexOfDomainIpv6(domain string) int {
for index, found := range *h {
if found.Domain == domain && found.Ipv6 == true {
return index
}
}
return -1
}
func (h *Hostlist) Remove(index int) {
// var a *Hostlist
// copy(a, h[0:index])
// a = append(a, *h[index:])
// *h[index] = nil
// // return a
}
func (h *Hostlist) RemoveIpv4(domain string) {
}
func (h *Hostlist) RemoveIpv6() {
}
func (h *Hostlist) Enable(domain string) {
for _, hostname := range *h {
if hostname.Domain == domain {
hostname.Enabled = true
}
}
}
func (h *Hostlist) Disable(domain string) {
for _, hostname := range *h {
if hostname.Domain == domain {
hostname.Enabled = false
}
}
}
func (h *Hostlist) Copy() *Hostlist {
var n *Hostlist
copy(*h, *n)
return n
}

@ -7,36 +7,52 @@ import (
)
func TestContainsDomainIp(t *testing.T) {
hosts := []*hostess.Hostname{
&hostess.Hostname{domain, ip, false, false},
&hostess.Hostname{"google.com", net.ParseIP("8.8.8.8"), true, false},
}
hosts := hostess.NewHostlist()
hosts.Add(hostess.NewHostname(domain, ip, false))
hosts.Add(hostess.NewHostname("google.com", "8.8.8.8", true))
if !hostess.ContainsDomain(hosts, domain) {
if !hosts.ContainsDomain(domain) {
t.Errorf("Expected to find %s", domain)
}
const extra_domain = "yahoo.com"
if hostess.ContainsDomain(hosts, extra_domain) {
if hosts.ContainsDomain(extra_domain) {
t.Errorf("Did not expect to find %s", extra_domain)
}
if !hostess.ContainsIp(hosts, ip) {
var first_ip = net.ParseIP(ip)
if !hosts.ContainsIp(first_ip) {
t.Errorf("Expected to find %s", ip)
}
var extra_ip = net.ParseIP("1.2.3.4")
if hostess.ContainsIp(hosts, extra_ip) {
if hosts.ContainsIp(extra_ip) {
t.Errorf("Did not expect to find %s", extra_ip)
}
hostname := &hostess.Hostname{domain, ip, true, false}
if !hostess.ContainsHostname(hosts, hostname) {
hostname := hostess.NewHostname(domain, ip, true)
if !hosts.ContainsHostname(hostname) {
t.Errorf("Expected to find %s", hostname)
}
extra_hostname := &hostess.Hostname{"yahoo.com", net.ParseIP("4.3.2.1"), false, false}
if hostess.ContainsHostname(hosts, extra_hostname) {
extra_hostname := hostess.NewHostname("yahoo.com", "4.3.2.1", false)
if hosts.ContainsHostname(extra_hostname) {
t.Errorf("Did not expect to find %s", extra_hostname)
}
}
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))
hosts.Remove(hosts.IndexOfDomainIpv4("google.com"))
// if len(hosts) > 1 {
// t.Errorf("Expected hostlist to have one item, found %s", len(hosts))
// }
if hosts.ContainsDomain("google.com") {
t.Errorf("Expected not to find google.com in %s", hosts)
}
}

@ -7,19 +7,16 @@ import (
)
func TestHostname(t *testing.T) {
h := hostess.Hostname{}
h.Domain = domain
h.Ip = ip
h.Enabled = enabled
h := hostess.NewHostname(domain, ip, enabled)
if h.Domain != domain {
t.Errorf("Domain should be %s", domain)
}
if !h.Ip.Equal(ip) {
if !h.Ip.Equal(net.ParseIP(ip)) {
t.Errorf("Ip should be %s", ip)
}
if h.Enabled != enabled {
t.Errorf("Enabled should be %s", enabled)
t.Errorf("Enabled should be %t", enabled)
}
}
@ -66,7 +63,7 @@ func TestIsValid(t *testing.T) {
}
func TestFormatHostname(t *testing.T) {
hostname := &hostess.Hostname{domain, ip, enabled, false}
hostname := hostess.NewHostname(domain, ip, enabled)
const exp_enabled = "127.0.0.1 localhost"
if hostname.Format() != exp_enabled {

Loading…
Cancel
Save