forked from lobre/goodhosts
-
Notifications
You must be signed in to change notification settings - Fork 0
/
goodhosts_test.go
120 lines (93 loc) · 2.92 KB
/
goodhosts_test.go
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
package goodhosts
import (
"fmt"
"reflect"
"testing"
)
func TestHostsLineIsComment(t *testing.T) {
comment := " # This is a comment "
line := NewHostsLine(comment)
result := line.IsComment()
if !result {
t.Error(fmt.Sprintf("'%s' should be a comment"), comment)
}
}
func TestNewHostsLineWithEmptyLine(t *testing.T) {
line := NewHostsLine("")
if line.Raw != "" {
t.Error("Failed to load empty line.")
}
}
func TestHostsHas(t *testing.T) {
hosts := new(Hosts)
hosts.Lines = []HostsLine{
NewHostsLine("127.0.0.1 yadda"), NewHostsLine("10.0.0.7 nada")}
// We should find this entry.
if !hosts.Has("10.0.0.7", "nada") {
t.Error("Couldn't find entry in hosts file.")
}
// We shouldn't find this entry
if hosts.Has("10.0.0.7", "shuda") {
t.Error("Found entry that isn't in hosts file.")
}
}
func TestHostsHasDoesntFindMissingEntry(t *testing.T) {
hosts := new(Hosts)
hosts.Lines = []HostsLine{
NewHostsLine("127.0.0.1 yadda"), NewHostsLine("10.0.0.7 nada")}
if hosts.Has("10.0.0.7", "brada") {
t.Error("Found missing entry.")
}
}
func TestHostsAddWhenIpHasOtherHosts(t *testing.T) {
hosts := new(Hosts)
hosts.Lines = []HostsLine{
NewHostsLine("127.0.0.1 yadda"), NewHostsLine("10.0.0.7 nada yadda")}
hosts.Add("10.0.0.7", "brada", "yadda")
expectedLines := []HostsLine{
NewHostsLine("127.0.0.1 yadda"), NewHostsLine("10.0.0.7 nada yadda brada")}
if !reflect.DeepEqual(hosts.Lines, expectedLines) {
t.Error("Add entry failed to append entry.")
}
}
func TestHostsAddWhenIpDoesntExist(t *testing.T) {
hosts := new(Hosts)
hosts.Lines = []HostsLine{
NewHostsLine("127.0.0.1 yadda")}
hosts.Add("10.0.0.7", "brada", "yadda")
expectedLines := []HostsLine{
NewHostsLine("127.0.0.1 yadda"), NewHostsLine("10.0.0.7 brada yadda")}
if !reflect.DeepEqual(hosts.Lines, expectedLines) {
t.Error("Add entry failed to append entry.")
}
}
func TestHostsRemoveWhenLastHostIpCombo(t *testing.T) {
hosts := new(Hosts)
hosts.Lines = []HostsLine{
NewHostsLine("127.0.0.1 yadda"), NewHostsLine("10.0.0.7 nada")}
hosts.Remove("10.0.0.7", "nada")
expectedLines := []HostsLine{NewHostsLine("127.0.0.1 yadda")}
if !reflect.DeepEqual(hosts.Lines, expectedLines) {
t.Error("Remove entry failed to remove entry.")
}
}
func TestHostsRemoveWhenIpHasOtherHosts(t *testing.T) {
hosts := new(Hosts)
hosts.Lines = []HostsLine{
NewHostsLine("127.0.0.1 yadda"), NewHostsLine("10.0.0.7 nada brada")}
hosts.Remove("10.0.0.7", "nada")
expectedLines := []HostsLine{
NewHostsLine("127.0.0.1 yadda"), NewHostsLine("10.0.0.7 brada")}
if !reflect.DeepEqual(hosts.Lines, expectedLines) {
t.Error("Remove entry failed to remove entry.")
}
}
func TestHostsRemoveMultipleEntries(t *testing.T) {
hosts := new(Hosts)
hosts.Lines = []HostsLine{
NewHostsLine("127.0.0.1 yadda nadda prada")}
hosts.Remove("127.0.0.1", "yadda", "prada")
if hosts.Lines[0].Raw != "127.0.0.1 nadda" {
t.Error("Failed to remove multiple entries.")
}
}