-
Notifications
You must be signed in to change notification settings - Fork 1.1k
/
clientconfig_test.go
177 lines (159 loc) · 4.52 KB
/
clientconfig_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
package dns
import (
"os"
"path/filepath"
"strings"
"testing"
)
const normal string = `
# Comment
domain somedomain.com
nameserver 10.28.10.2
nameserver 11.28.10.1
`
const missingNewline string = `
domain somedomain.com
nameserver 10.28.10.2
nameserver 11.28.10.1` // <- NOTE: NO newline.
func testConfig(t *testing.T, data string) {
cc, err := ClientConfigFromReader(strings.NewReader(data))
if err != nil {
t.Errorf("error parsing resolv.conf: %v", err)
}
if l := len(cc.Servers); l != 2 {
t.Errorf("incorrect number of nameservers detected: %d", l)
}
if l := len(cc.Search); l != 1 {
t.Errorf("domain directive not parsed correctly: %v", cc.Search)
} else {
if cc.Search[0] != "somedomain.com" {
t.Errorf("domain is unexpected: %v", cc.Search[0])
}
}
}
func TestNameserver(t *testing.T) { testConfig(t, normal) }
func TestMissingFinalNewLine(t *testing.T) { testConfig(t, missingNewline) }
func TestNdots(t *testing.T) {
ndotsVariants := map[string]int{
"options ndots:0": 0,
"options ndots:1": 1,
"options ndots:15": 15,
"options ndots:16": 15,
"options ndots:-1": 0,
"": 1,
}
for data := range ndotsVariants {
cc, err := ClientConfigFromReader(strings.NewReader(data))
if err != nil {
t.Errorf("error parsing resolv.conf: %v", err)
}
if cc.Ndots != ndotsVariants[data] {
t.Errorf("Ndots not properly parsed: (Expected: %d / Was: %d)", ndotsVariants[data], cc.Ndots)
}
}
}
func TestClientConfigFromReaderAttempts(t *testing.T) {
testCases := []struct {
data string
expected int
}{
{data: "options attempts:0", expected: 1},
{data: "options attempts:1", expected: 1},
{data: "options attempts:15", expected: 15},
{data: "options attempts:16", expected: 16},
{data: "options attempts:-1", expected: 1},
{data: "options attempt:", expected: 2},
}
for _, test := range testCases {
test := test
t.Run(strings.Replace(test.data, ":", " ", -1), func(t *testing.T) {
t.Parallel()
cc, err := ClientConfigFromReader(strings.NewReader(test.data))
if err != nil {
t.Errorf("error parsing resolv.conf: %v", err)
}
if cc.Attempts != test.expected {
t.Errorf("A attempts not properly parsed: (Expected: %d / Was: %d)", test.expected, cc.Attempts)
}
})
}
}
func TestReadFromFile(t *testing.T) {
tempDir := t.TempDir()
path := filepath.Join(tempDir, "resolv.conf")
if err := os.WriteFile(path, []byte(normal), 0o644); err != nil {
t.Fatalf("writeFile: %v", err)
}
cc, err := ClientConfigFromFile(path)
if err != nil {
t.Errorf("error parsing resolv.conf: %v", err)
}
if l := len(cc.Servers); l != 2 {
t.Errorf("incorrect number of nameservers detected: %d", l)
}
if l := len(cc.Search); l != 1 {
t.Errorf("domain directive not parsed correctly: %v", cc.Search)
} else {
if cc.Search[0] != "somedomain.com" {
t.Errorf("domain is unexpected: %v", cc.Search[0])
}
}
}
func TestNameListNdots1(t *testing.T) {
cfg := ClientConfig{
Ndots: 1,
}
// fqdn should be only result returned
names := cfg.NameList("miek.nl.")
if len(names) != 1 {
t.Errorf("NameList returned != 1 names: %v", names)
} else if names[0] != "miek.nl." {
t.Errorf("NameList didn't return sent fqdn domain: %v", names[0])
}
cfg.Search = []string{
"test",
}
// Sent domain has NDots and search
names = cfg.NameList("miek.nl")
if len(names) != 2 {
t.Errorf("NameList returned != 2 names: %v", names)
} else if names[0] != "miek.nl." {
t.Errorf("NameList didn't return sent domain first: %v", names[0])
} else if names[1] != "miek.nl.test." {
t.Errorf("NameList didn't return search last: %v", names[1])
}
}
func TestNameListNdots2(t *testing.T) {
cfg := ClientConfig{
Ndots: 2,
}
// Sent domain has less than NDots and search
cfg.Search = []string{
"test",
}
names := cfg.NameList("miek.nl")
if len(names) != 2 {
t.Errorf("NameList returned != 2 names: %v", names)
} else if names[0] != "miek.nl.test." {
t.Errorf("NameList didn't return search first: %v", names[0])
} else if names[1] != "miek.nl." {
t.Errorf("NameList didn't return sent domain last: %v", names[1])
}
}
func TestNameListNdots0(t *testing.T) {
cfg := ClientConfig{
Ndots: 0,
}
cfg.Search = []string{
"test",
}
// Sent domain has less than NDots and search
names := cfg.NameList("miek")
if len(names) != 2 {
t.Errorf("NameList returned != 2 names: %v", names)
} else if names[0] != "miek." {
t.Errorf("NameList didn't return search first: %v", names[0])
} else if names[1] != "miek.test." {
t.Errorf("NameList didn't return sent domain last: %v", names[1])
}
}