-
Notifications
You must be signed in to change notification settings - Fork 16
/
Copy pathhosts_test.go
126 lines (117 loc) · 3.38 KB
/
hosts_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
package hostsfile
import (
"testing"
)
func TestHostsReverseLookup(t *testing.T) {
res, err := ReverseLookup("127.0.0.1")
if err != nil {
t.Fatal(err)
}
if len(res) == 0 {
t.Errorf("Expected len(res) > 0 but actaul=%v res=%+v", len(res), res)
}
}
func TestParseHosts(t *testing.T) {
testCases := []struct {
hostsFileContent string
expectedEntries map[string]int
forbiddenEntries []string
}{
{
hostsFileContent: `##
# Host Database
#
# localhost is used to configure the loopback interface
# when the system is booting. Do not change this entry.
##
127.0.0.1 localhost localhost.local localhost.localdomain jays-computer jays-computer.local jays-computer.localdomain
127.0.0.1 wow.this.line.is.Ka.r.az.y
255.255.255.255 broadcasthost
::1 localhost
fe80::1%lo0 localhost
garbage
#172.1.2.12 mesos-primary1a
#172.1.2.180 mesos-primary2a
#172.1.2.182 mesos-primary3a
#172.1.2.63 mesos-worker1a
#172.1.2.115 mesos-worker2a
127.0.0.2 foo.bar
192.168.1.34 hello-app.lan talksbythebay-lan
; 192.168.1.240 should-not.resolve really-it-should.nt
`,
expectedEntries: map[string]int{
"127.0.0.1": 7,
"127.0.0.2": 1,
"192.168.1.34": 2,
},
forbiddenEntries: []string{
"172.1.2.12",
"192.168.1.240",
},
},
{
hostsFileContent: `;;
; Host Database
;
; localhost is used to configure the loopback interface
; when the system is booting. Do not change this entry.
;;
127.0.0.1 localhost localhost.local localhost.localdomain jays-computer jays-computer.local jays-computer.localdomain
255.255.255.255 broadcasthost
::1 localhost
fe80::1%lo0 localhost
192.168.1.34 hello-app.lan talksbythebay-lan
# 192.168.1.240 should-not.resolve really-it-should.nt`,
expectedEntries: map[string]int{
"127.0.0.1": 6,
"192.168.1.34": 2,
},
forbiddenEntries: []string{
"192.168.1.240",
},
},
{
hostsFileContent: `;;
; Host Database
;
; localhost is used to configure the loopback interface
; when the system is booting. Do not change this entry.
;;
127.0.0.1 localhost localhost.local localhost.localdomain jays-computer jays-computer.local jays-computer.localdomain
255.255.255.255 broadcasthost
::1 localhost
fe80::1%lo0 localhost
192.168.1.34 hello-app.lan talksbythebay-lan # example.com
192.168.1.32 hello-app.lan talksbythebay-lan #example.com
# 192.168.1.240 should-not.resolve really-it-should.nt`,
expectedEntries: map[string]int{
"127.0.0.1": 6,
"192.168.1.34": 2,
"192.168.1.32": 2,
},
forbiddenEntries: []string{
"192.168.1.240",
},
},
}
for i, testCase := range testCases {
res, err := ParseHosts([]byte(testCase.hostsFileContent), nil)
if err != nil {
t.Fatalf("[i=%v] Error parsing hosts content: %s", i, err)
}
for entry, expectedCount := range testCase.expectedEntries {
if reverses, ok := res[entry]; ok {
if expected, actual := expectedCount, len(reverses); actual != expected {
t.Errorf("[i=%v] Expected len(res['%v'])=%v but actual=%v; reverses=%+v", i, entry, expected, actual, reverses)
}
} else {
t.Errorf("[i=%v] Expected to find entries for ip=%v but none were found", i, entry)
}
}
for _, forbiddenEntry := range testCase.forbiddenEntries {
if reverses, ok := res[forbiddenEntry]; ok {
t.Errorf("[i=%v] Expected '%v' to be absent from res but actual=%+v", i, forbiddenEntry, reverses)
}
}
}
}