-
Notifications
You must be signed in to change notification settings - Fork 2
/
Copy pathparser_test.go
199 lines (167 loc) · 6.97 KB
/
parser_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
package resolvconf_test
import (
"github.com/stretchr/testify/assert"
"net"
"strings"
"testing"
"." // import the main package
)
func TestReadNewNameserver(t *testing.T) {
conf, err := resolvconf.ReadConf(strings.NewReader("nameserver 8.8.8.8"))
assert.Nil(t, err)
assert.NotNil(t, conf.Find(resolvconf.NewNameserver(net.ParseIP("8.8.8.8"))))
conf, err = resolvconf.ReadConf(strings.NewReader("nameserver 8.8.8.9"))
assert.Nil(t, err)
assert.NotNil(t, conf.Find(resolvconf.NewNameserver(net.ParseIP("8.8.8.9"))))
}
func TestReadFaultyNewNameserver(t *testing.T) {
conf, err := resolvconf.ReadConf(strings.NewReader("nameserver 8.8.8"))
assert.NotNil(t, err)
assert.Nil(t, conf.Find(resolvconf.NewNameserver(net.ParseIP("8.8.8"))))
conf, err = resolvconf.ReadConf(strings.NewReader("nameserver 8.8.8.8.8"))
assert.NotNil(t, err)
assert.Nil(t, conf.Find(resolvconf.NewNameserver(net.ParseIP("8.8.8.8.8"))))
conf, err = resolvconf.ReadConf(strings.NewReader("nameserver www.golang.org"))
assert.NotNil(t, err)
assert.Nil(t, conf.Find(resolvconf.NewNameserver(net.ParseIP("www.golang.org"))))
}
func TestReadUnknownConfNewOption(t *testing.T) {
_, err := resolvconf.ReadConf(strings.NewReader("nameserv 8.8.8.9"))
assert.NotNil(t, err)
}
func TestReadSeveralNameservers(t *testing.T) {
conf_str := "nameserver 8.8.8.8\n" +
"nameserver 8.8.8.9\n"
conf, _ := resolvconf.ReadConf(strings.NewReader(conf_str))
assert.NotNil(t, conf.Find(resolvconf.NewNameserver(net.ParseIP("8.8.8.8"))))
assert.NotNil(t, conf.Find(resolvconf.NewNameserver(net.ParseIP("8.8.8.9"))))
}
func TestMaxThreeNameservers(t *testing.T) {
conf_str := "nameserver 8.8.8.8\n" +
"nameserver 8.8.8.9\n" +
"nameserver 8.8.8.10\n" +
"nameserver 8.8.8.11\n"
conf, err := resolvconf.ReadConf(strings.NewReader(conf_str))
assert.NotNil(t, err)
assert.NotNil(t, conf.Find(resolvconf.NewNameserver(net.ParseIP("8.8.8.8"))))
assert.NotNil(t, conf.Find(resolvconf.NewNameserver(net.ParseIP("8.8.8.9"))))
assert.NotNil(t, conf.Find(resolvconf.NewNameserver(net.ParseIP("8.8.8.10"))))
// Should not be there
assert.Nil(t, conf.Find(resolvconf.NewNameserver(net.ParseIP("8.8.8.11"))))
}
func TestAddSameNameserverGivesError(t *testing.T) {
conf_str := "nameserver 8.8.8.8\n" +
"nameserver 8.8.8.8\n"
conf, err := resolvconf.ReadConf(strings.NewReader(conf_str))
assert.NotNil(t, err)
assert.NotNil(t, conf.Find(resolvconf.NewNameserver(net.ParseIP("8.8.8.8"))))
}
func TestCommentsAndBlankLinesAreSkipped(t *testing.T) {
_, err := resolvconf.ReadConf(strings.NewReader("# This is a comment"))
assert.Nil(t, err)
_, err = resolvconf.ReadConf(strings.NewReader("; This is a forth comment"))
assert.Nil(t, err)
// Empty line
_, err = resolvconf.ReadConf(strings.NewReader("\n"))
assert.Nil(t, err)
}
func TestReadDomain(t *testing.T) {
conf, err := resolvconf.ReadConf(strings.NewReader("domain foo.com"))
assert.Nil(t, err)
assert.Equal(t, "foo.com", conf.GetDomain().Name)
conf, err = resolvconf.ReadConf(strings.NewReader("domain foo.com"))
assert.Nil(t, err)
assert.Equal(t, "foo.com", conf.GetDomain().Name)
conf, err = resolvconf.ReadConf(strings.NewReader(" domain foo.com"))
assert.Nil(t, err)
assert.Equal(t, "foo.com", conf.GetDomain().Name)
}
func TestReadSearch(t *testing.T) {
conf, err := resolvconf.ReadConf(strings.NewReader("search foo.com"))
assert.Nil(t, err)
assert.Equal(t, 1, len(conf.GetSearchDomains()))
assert.NotNil(t, conf.Find(resolvconf.NewSearchDomain("foo.com")))
}
func TestReadMultiSearch(t *testing.T) {
conf, err := resolvconf.ReadConf(strings.NewReader("search foo.com bar.com baz.com"))
assert.Nil(t, err)
assert.Equal(t, 3, len(conf.GetSearchDomains()))
for _, dom := range []string{"foo.com", "bar.com", "baz.com"} {
assert.NotNil(t, conf.Find(resolvconf.NewSearchDomain(dom)))
}
}
func TestReadSortlist(t *testing.T) {
conf, err := resolvconf.ReadConf(strings.NewReader("sortlist 130.155.160.0"))
assert.Nil(t, err)
assert.Equal(t, 1, len(conf.GetSortItems()))
assert.NotNil(t, conf.Find(*resolvconf.NewSortItem(net.ParseIP("130.155.160.0"))))
}
func TestReadSortlistFaultyAddress(t *testing.T) {
conf, err := resolvconf.ReadConf(strings.NewReader("sortlist 130.155.160"))
assert.NotNil(t, err)
assert.Equal(t, 0, len(conf.GetSortItems()))
}
func TestReadMultiSortlist(t *testing.T) {
conf, err := resolvconf.ReadConf(strings.NewReader("sortlist 130.155.160.0 130.155.0.0"))
assert.Nil(t, err)
assert.Equal(t, 2, len(conf.GetSortItems()))
assert.NotNil(t, conf.Find(*resolvconf.NewSortItem(net.ParseIP("130.155.160.0"))))
assert.NotNil(t, conf.Find(*resolvconf.NewSortItem(net.ParseIP("130.155.0.0"))))
}
func TestReadSortlistWithNetmask(t *testing.T) {
conf, err := resolvconf.ReadConf(strings.NewReader("sortlist 130.155.160.0/255.255.240.0"))
assert.Nil(t, err)
assert.Equal(t, 1, len(conf.GetSortItems()))
assert.NotNil(t, conf.Find(*resolvconf.NewSortItem(net.ParseIP("130.155.160.0"))))
}
func TestReadSortlistWithBadNetmask(t *testing.T) {
conf, err := resolvconf.ReadConf(strings.NewReader("sortlist 130.155.160.0/255.255.240"))
assert.NotNil(t, err)
assert.Equal(t, 0, len(conf.GetSortItems()))
}
func TestMaxTenSortlistPairsMayBeDefined(t *testing.T) {
conf_str := "sortlist 1.1.1.0 1.1.1.1 " +
"1.1.1.2 1.1.1.3 1.1.1.4 1.1.1.5 1.1.1.6 " +
"1.1.1.7 1.1.1.8 1.1.1.9 1.1.1.10"
conf, err := resolvconf.ReadConf(strings.NewReader(conf_str))
assert.NotNil(t, err)
assert.Equal(t, 10, len(conf.GetSortItems()))
}
func TestBasicOptions(t *testing.T) {
conf, err := resolvconf.ReadConf(strings.NewReader("options debug"))
assert.Nil(t, err)
assert.Equal(t, 1, len(conf.GetOptions()))
assert.NotNil(t, conf.Find(resolvconf.NewOption("debug")))
conf, err = resolvconf.ReadConf(strings.NewReader("options debug rotate"))
assert.Nil(t, err)
assert.Equal(t, 2, len(conf.GetOptions()))
assert.NotNil(t, conf.Find(resolvconf.NewOption("rotate")))
conf, err = resolvconf.ReadConf(strings.NewReader("options debug rotate ndots:12"))
assert.Nil(t, err)
assert.Equal(t, 3, len(conf.GetOptions()))
opt := conf.Find(resolvconf.NewOption("ndots"))
assert.NotNil(t, opt)
assert.Equal(t, 12, opt.(*resolvconf.Option).Get())
}
func TestUnknownNewOption(t *testing.T) {
conf, err := resolvconf.ReadConf(strings.NewReader("options foo"))
assert.NotNil(t, err)
assert.Equal(t, 0, len(conf.GetOptions()))
}
func TestBadNewOption(t *testing.T) {
conf, err := resolvconf.ReadConf(strings.NewReader("options ndots:"))
assert.NotNil(t, err)
assert.Equal(t, 0, len(conf.GetOptions()))
conf, err = resolvconf.ReadConf(strings.NewReader("options ndots:foos"))
assert.NotNil(t, err)
assert.Equal(t, 0, len(conf.GetOptions()))
}
func TestAllOptions(t *testing.T) {
conf_str := "options debug ndots:3 timeout:5 attempts:4 " +
"rotate no-check-names inet6 ip6-bytestring ip6-dotint " +
"no-ip6-dotint edns0 single-request single-request-reopen " +
"no-tld-query use-vc"
conf, err := resolvconf.ReadConf(strings.NewReader(conf_str))
assert.Nil(t, err)
assert.Equal(t, 15, len(conf.GetOptions()))
}