forked from cbednarski/hostess
-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathhostfile_test.go
202 lines (181 loc) · 5.03 KB
/
hostfile_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
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
package hostess_test
import (
"fmt"
"runtime"
"strings"
"testing"
"github.com/cbednarski/hostess"
)
const ipv4Pass = `
127.0.0.1
127.0.1.1
10.200.30.50
99.99.99.99
999.999.999.999
0.1.1.0
`
const ipv4Fail = `
1234.1.1.1
123.5.6
12.12
76.76.67.67.45
`
const ipv6 = ``
const domain = "localhost"
const ip = "127.0.0.1"
const enabled = true
func Diff(expected, actual string) string {
return fmt.Sprintf(`
---- Expected ----
%s
----- Actual -----
%s
`, expected, actual)
}
func TestGetHostsPath(t *testing.T) {
path := hostess.GetHostsPath()
var expected string
if runtime.GOOS == "windows" {
expected = "C:\\Windows\\System32\\drivers\\etc\\hosts"
} else {
expected = "/etc/hosts"
}
if path != expected {
t.Error("Hosts path should be " + expected)
}
}
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
# 8.8.8.8 google.com
10.37.12.18 devsite.com m.devsite.com
`
hostfile := hostess.NewHostfile()
hostfile.Path = "./hosts"
hostfile.Hosts.Add(hostess.MustHostname("localhost", "127.0.0.1", true))
hostfile.Hosts.Add(hostess.MustHostname("ip-10-37-12-18", "127.0.1.1", true))
hostfile.Hosts.Add(hostess.MustHostname("devsite", "127.0.0.1", true))
hostfile.Hosts.Add(hostess.MustHostname("google.com", "8.8.8.8", false))
hostfile.Hosts.Add(hostess.MustHostname("devsite.com", "10.37.12.18", true))
hostfile.Hosts.Add(hostess.MustHostname("m.devsite.com", "10.37.12.18", true))
f := string(hostfile.Format())
if f != expected {
t.Errorf("Hostfile output is not formatted correctly: %s", Diff(expected, f))
}
}
func TestTrimWS(t *testing.T) {
const expected = ` candy
`
actual := hostess.TrimWS(expected)
if actual != "candy" {
t.Errorf("Output was not trimmed correctly: %s", Diff(expected, actual))
}
}
func TestParseLineBlank(t *testing.T) {
// Blank line
hosts, err := hostess.ParseLine("")
expected := "line is blank"
if err.Error() != expected {
t.Errorf("Expected error %q; found %q", expected, err.Error())
}
if len(hosts) > 0 {
t.Error("Expected to find zero hostnames")
}
}
func TestParseLineComment(t *testing.T) {
// Comment
hosts, err := hostess.ParseLine("# The following lines are desirable for IPv6 capable hosts")
if err == nil {
t.Error(err)
}
if len(hosts) > 0 {
t.Error("Expected to find zero hostnames")
}
}
func TestParseLineOneWordComment(t *testing.T) {
// Single word comment
hosts, err := hostess.ParseLine("#blah")
if err != nil {
t.Error(err)
}
if len(hosts) > 0 {
t.Error("Expected to find zero hostnames")
}
}
func TestParseLineBasicHostnameComment(t *testing.T) {
hosts, err := hostess.ParseLine("#66.33.99.11 test.domain.com")
if err != nil {
t.Error(err)
}
if !hosts.Contains(hostess.MustHostname("test.domain.com", "66.33.99.11", false)) ||
len(hosts) != 1 {
t.Error("Expected to find test.domain.com (disabled)")
}
}
func TestParseLineMultiHostnameComment(t *testing.T) {
hosts, err := hostess.ParseLine("# 66.33.99.11 test.domain.com domain.com")
if err != nil {
t.Error(err)
}
if !hosts.Contains(hostess.MustHostname("test.domain.com", "66.33.99.11", false)) ||
!hosts.Contains(hostess.MustHostname("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 %+v", hosts)
}
}
func TestParseLineMultiHostname(t *testing.T) {
// Not Commented stuff
hosts, err := hostess.ParseLine("255.255.255.255 broadcasthost test.domain.com domain.com")
if err != nil {
t.Error(err)
}
if !hosts.Contains(hostess.MustHostname("broadcasthost", "255.255.255.255", true)) ||
!hosts.Contains(hostess.MustHostname("test.domain.com", "255.255.255.255", true)) ||
!hosts.Contains(hostess.MustHostname("domain.com", "255.255.255.255", true)) ||
len(hosts) != 3 {
t.Error("Expected to find broadcasthost, domain.com, and test.domain.com (enabled)")
}
}
func TestParseLineIPv6A(t *testing.T) {
// Ipv6 stuff
hosts, err := hostess.ParseLine("::1 localhost")
if err != nil {
t.Error(err)
}
if !hosts.Contains(hostess.MustHostname("localhost", "::1", true)) ||
len(hosts) != 1 {
t.Error("Expected to find localhost ipv6 (enabled)")
}
}
func TestParseLineIPv6B(t *testing.T) {
hosts, err := hostess.ParseLine("ff02::1 ip6-allnodes")
if err != nil {
t.Error(err)
}
if !hosts.Contains(hostess.MustHostname("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()
hostfile.Read()
if !strings.Contains(string(hostfile.GetData()), domain) {
t.Errorf("Expected to find %s", domain)
}
hostfile.Parse()
on := enabled
if runtime.GOOS == "windows" {
on = false
}
hostname := hostess.MustHostname(domain, ip, on)
found := hostfile.Hosts.Contains(hostname)
if !found {
t.Errorf("Expected to find %#v", hostname)
}
}