This repository has been archived by the owner on Jan 12, 2021. It is now read-only.
forked from lextoumbourou/goodhosts
-
Notifications
You must be signed in to change notification settings - Fork 1
/
Copy pathgoodhosts_test.go
167 lines (132 loc) · 4.01 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
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
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.")
}
}
func TestHostnamesHas(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.HasHostname("nada") {
t.Error("Couldn't find entry in hosts file.")
}
// We shouldn't find this entry
if hosts.HasHostname("shuda") {
t.Error("Found entry that isn't in hosts file.")
}
}
func TestHostsRemoveByHostname(t *testing.T) {
hosts := new(Hosts)
hosts.Lines = []HostsLine{
NewHostsLine("127.0.0.1 yadda"),
NewHostsLine("168.1.1.1 yadda"),
}
hosts.RemoveByHostname("yadda")
// We shouldn't find this entry
if hosts.HasHostname("yadda") {
t.Error("Found entry that isn't in hosts file.")
}
}
func TestHostsRemoveByHostnameWhenHostnameNotExist(t *testing.T) {
hosts := new(Hosts)
hosts.Lines = []HostsLine{
NewHostsLine("127.0.0.1 prada"),
}
hosts.RemoveByHostname("yadda")
// We shouldn't find this entry
if hosts.HasHostname("yadda") {
t.Error("Found entry that isn't in hosts file.")
}
if !hosts.HasHostname("prada") {
t.Error("Found entry that is in hosts file.")
}
}