-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathconfig_test.go
More file actions
358 lines (328 loc) · 9.53 KB
/
config_test.go
File metadata and controls
358 lines (328 loc) · 9.53 KB
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
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
307
308
309
310
311
312
313
314
315
316
317
318
319
320
321
322
323
324
325
326
327
328
329
330
331
332
333
334
335
336
337
338
339
340
341
342
343
344
345
346
347
348
349
350
351
352
353
354
355
356
357
358
package main
import (
"net"
"os"
"path/filepath"
"testing"
"github.com/kkkgo/mini-ppdns/mlog"
)
func TestParseINI_Hook(t *testing.T) {
content := `[hook]
exec="wget --spider -q -S http://www.google.com/generate_204"
exit_code=0
keyword="204"
sleep_time=30
retry_time=3
count=5
switch_fall_exec="echo dns down"
switch_main_exec="echo dns up"
`
tmpDir := t.TempDir()
path := filepath.Join(tmpDir, "test.ini")
os.WriteFile(path, []byte(content), 0644)
var args ConfigArgs
err := parseINI(path, &args, mlog.Nop())
if err != nil {
t.Fatalf("parseINI error: %v", err)
}
if args.Hook == nil {
t.Fatal("expected Hook to be parsed")
}
if args.Hook.Exec != "wget --spider -q -S http://www.google.com/generate_204" {
t.Errorf("unexpected exec: %q", args.Hook.Exec)
}
if !args.Hook.ExitCodeSet || args.Hook.ExitCode != 0 {
t.Errorf("expected exit_code=0, got %d (set=%v)", args.Hook.ExitCode, args.Hook.ExitCodeSet)
}
if args.Hook.Keyword != "204" {
t.Errorf("unexpected keyword: %q", args.Hook.Keyword)
}
if args.Hook.SleepTime != 30 {
t.Errorf("expected sleep_time=30, got %d", args.Hook.SleepTime)
}
if args.Hook.RetryTime != 3 {
t.Errorf("expected retry_time=3, got %d", args.Hook.RetryTime)
}
if args.Hook.Count != 5 {
t.Errorf("expected count=5, got %d", args.Hook.Count)
}
if args.Hook.SwitchFallExec != "echo dns down" {
t.Errorf("unexpected switch_fall_exec: %q", args.Hook.SwitchFallExec)
}
if args.Hook.SwitchMainExec != "echo dns up" {
t.Errorf("unexpected switch_main_exec: %q", args.Hook.SwitchMainExec)
}
}
func TestParseINI_HookDefaults(t *testing.T) {
content := `[hook]
exec=ping -c1 8.8.8.8
`
tmpDir := t.TempDir()
path := filepath.Join(tmpDir, "test.ini")
os.WriteFile(path, []byte(content), 0644)
var args ConfigArgs
err := parseINI(path, &args, mlog.Nop())
if err != nil {
t.Fatalf("parseINI error: %v", err)
}
if args.Hook == nil {
t.Fatal("expected Hook to be parsed")
}
if args.Hook.SleepTime != 60 {
t.Errorf("expected default sleep_time=60, got %d", args.Hook.SleepTime)
}
if args.Hook.RetryTime != 5 {
t.Errorf("expected default retry_time=5, got %d", args.Hook.RetryTime)
}
if args.Hook.Count != 10 {
t.Errorf("expected default count=10, got %d", args.Hook.Count)
}
if args.Hook.ExitCodeSet {
t.Error("exit_code should not be set when not configured")
}
if args.Hook.Keyword != "" {
t.Error("keyword should be empty when not configured")
}
}
// TestParseINI_TrustRcode: verify trust_rcode is parsed from INI file.
func TestParseINI_TrustRcode(t *testing.T) {
content := `[adv]
trust_rcode=0,3
`
tmpDir := t.TempDir()
iniPath := filepath.Join(tmpDir, "test.ini")
os.WriteFile(iniPath, []byte(content), 0644)
args := &ConfigArgs{}
err := parseINI(iniPath, args, mlog.Nop())
if err != nil {
t.Fatalf("parseINI error: %v", err)
}
if len(args.TrustRcode) != 2 {
t.Fatalf("expected 2 trust_rcode values, got %d", len(args.TrustRcode))
}
if args.TrustRcode[0] != 0 || args.TrustRcode[1] != 3 {
t.Fatalf("expected [0,3], got %v", args.TrustRcode)
}
}
// TestParseINI_TrustRcode_Empty: verify trust_rcode defaults to empty.
func TestParseINI_TrustRcode_Empty(t *testing.T) {
content := `[adv]
qtime=250
`
tmpDir := t.TempDir()
iniPath := filepath.Join(tmpDir, "test.ini")
os.WriteFile(iniPath, []byte(content), 0644)
args := &ConfigArgs{}
err := parseINI(iniPath, args, mlog.Nop())
if err != nil {
t.Fatalf("parseINI error: %v", err)
}
if len(args.TrustRcode) != 0 {
t.Fatalf("expected empty trust_rcode, got %v", args.TrustRcode)
}
}
// TestParseINI_BogusPrivAndLeaseFile tests INI parsing for new PTR options.
func TestParseINI_BogusPrivAndLeaseFile(t *testing.T) {
content := `[adv]
boguspriv=1
lease_file=/tmp/dhcp.leases,/tmp/dnsmasq.leases
hosts_file=/etc/hosts
`
tmpDir := t.TempDir()
iniPath := filepath.Join(tmpDir, "test.ini")
os.WriteFile(iniPath, []byte(content), 0644)
args := &ConfigArgs{}
err := parseINI(iniPath, args, mlog.Nop())
if err != nil {
t.Fatalf("parseINI error: %v", err)
}
if !args.BogusPriv {
t.Fatal("expected BogusPriv=true")
}
if len(args.LeaseFile) != 2 {
t.Fatalf("expected 2 lease files, got %d: %v", len(args.LeaseFile), args.LeaseFile)
}
if args.LeaseFile[0] != "/tmp/dhcp.leases" || args.LeaseFile[1] != "/tmp/dnsmasq.leases" {
t.Fatalf("unexpected lease files: %v", args.LeaseFile)
}
if len(args.HostsFile) != 1 || args.HostsFile[0] != "/etc/hosts" {
t.Fatalf("unexpected hosts files: %v", args.HostsFile)
}
}
// TestParseINI_BogusPrivDisabled tests INI parsing with boguspriv=0.
func TestParseINI_BogusPrivDisabled(t *testing.T) {
content := `[adv]
boguspriv=0
`
tmpDir := t.TempDir()
iniPath := filepath.Join(tmpDir, "test.ini")
os.WriteFile(iniPath, []byte(content), 0644)
args := &ConfigArgs{}
err := parseINI(iniPath, args, mlog.Nop())
if err != nil {
t.Fatalf("parseINI error: %v", err)
}
if args.BogusPriv {
t.Fatal("expected BogusPriv=false when boguspriv=0")
}
if !args.BogusPrivSet {
t.Fatal("expected BogusPrivSet=true when boguspriv is explicitly set to 0")
}
}
// TestParseINI_BogusPrivNotSet tests that BogusPrivSet is false when boguspriv
// is not mentioned in the config file, so the caller can apply the default.
func TestParseINI_BogusPrivNotSet(t *testing.T) {
content := `[adv]
qtime=300
`
tmpDir := t.TempDir()
iniPath := filepath.Join(tmpDir, "test.ini")
os.WriteFile(iniPath, []byte(content), 0644)
args := &ConfigArgs{}
err := parseINI(iniPath, args, mlog.Nop())
if err != nil {
t.Fatalf("parseINI error: %v", err)
}
if args.BogusPrivSet {
t.Fatal("expected BogusPrivSet=false when boguspriv is not in config")
}
if args.BogusPriv {
t.Fatal("expected BogusPriv=false (zero value) when not set in config")
}
}
// TestParseINI_BogusPrivSetFlag tests that BogusPrivSet is true for all valid values.
func TestParseINI_BogusPrivSetFlag(t *testing.T) {
tests := []struct {
value string
wantPriv bool
}{
{"1", true},
{"yes", true},
{"true", true},
{"0", false},
{"no", false},
{"false", false},
}
for _, tt := range tests {
t.Run("boguspriv="+tt.value, func(t *testing.T) {
content := "[adv]\nboguspriv=" + tt.value + "\n"
tmpDir := t.TempDir()
iniPath := filepath.Join(tmpDir, "test.ini")
os.WriteFile(iniPath, []byte(content), 0644)
args := &ConfigArgs{}
err := parseINI(iniPath, args, mlog.Nop())
if err != nil {
t.Fatalf("parseINI error: %v", err)
}
if !args.BogusPrivSet {
t.Fatal("expected BogusPrivSet=true")
}
if args.BogusPriv != tt.wantPriv {
t.Fatalf("expected BogusPriv=%v, got %v", tt.wantPriv, args.BogusPriv)
}
})
}
}
// TestParseINI_Hosts tests [hosts] section parsing.
func TestParseINI_Hosts(t *testing.T) {
content := `[hosts]
# hosts entry
1.2.3.4 example.com
10.10.10.53 paopao.dns
2001:db8::1 v6.example.com
192.168.1.1 multi1.test multi2.test
`
tmpDir := t.TempDir()
path := filepath.Join(tmpDir, "test.ini")
os.WriteFile(path, []byte(content), 0644)
var args ConfigArgs
err := parseINI(path, &args, mlog.Nop())
if err != nil {
t.Fatalf("parseINI error: %v", err)
}
if args.Hosts == nil {
t.Fatal("expected Hosts to be parsed")
}
// example.com -> 1.2.3.4
ips := args.Hosts["example.com."]
if len(ips) != 1 || !ips[0].Equal(net.ParseIP("1.2.3.4")) {
t.Errorf("unexpected example.com IPs: %v", ips)
}
// paopao.dns -> 10.10.10.53
ips = args.Hosts["paopao.dns."]
if len(ips) != 1 || !ips[0].Equal(net.ParseIP("10.10.10.53")) {
t.Errorf("unexpected paopao.dns IPs: %v", ips)
}
// v6.example.com -> 2001:db8::1
ips = args.Hosts["v6.example.com."]
if len(ips) != 1 || !ips[0].Equal(net.ParseIP("2001:db8::1")) {
t.Errorf("unexpected v6.example.com IPs: %v", ips)
}
// multi1.test and multi2.test -> 192.168.1.1
for _, domain := range []string{"multi1.test.", "multi2.test."} {
ips = args.Hosts[domain]
if len(ips) != 1 || !ips[0].Equal(net.ParseIP("192.168.1.1")) {
t.Errorf("unexpected %s IPs: %v", domain, ips)
}
}
}
// TestParseINI_HostsEmpty tests that empty [hosts] section is handled.
func TestParseINI_HostsEmpty(t *testing.T) {
content := `[hosts]
# only comments
`
tmpDir := t.TempDir()
path := filepath.Join(tmpDir, "test.ini")
os.WriteFile(path, []byte(content), 0644)
var args ConfigArgs
err := parseINI(path, &args, mlog.Nop())
if err != nil {
t.Fatalf("parseINI error: %v", err)
}
if len(args.Hosts) != 0 {
t.Fatalf("expected empty Hosts, got %v", args.Hosts)
}
}
// TestParseINI_HostsInvalidIP tests that invalid IPs are skipped.
func TestParseINI_HostsInvalidIP(t *testing.T) {
content := `[hosts]
notanip example.com
1.2.3.4 valid.com
`
tmpDir := t.TempDir()
path := filepath.Join(tmpDir, "test.ini")
os.WriteFile(path, []byte(content), 0644)
var args ConfigArgs
err := parseINI(path, &args, mlog.Nop())
if err != nil {
t.Fatalf("parseINI error: %v", err)
}
if len(args.Hosts) != 1 {
t.Fatalf("expected 1 host entry, got %d", len(args.Hosts))
}
if _, ok := args.Hosts["valid.com."]; !ok {
t.Fatal("expected valid.com entry")
}
}
// TestParseINI_HostsMultipleIPs tests multiple IPs for the same domain.
func TestParseINI_HostsMultipleIPs(t *testing.T) {
content := `[hosts]
1.2.3.4 example.com
5.6.7.8 example.com
`
tmpDir := t.TempDir()
path := filepath.Join(tmpDir, "test.ini")
os.WriteFile(path, []byte(content), 0644)
var args ConfigArgs
err := parseINI(path, &args, mlog.Nop())
if err != nil {
t.Fatalf("parseINI error: %v", err)
}
ips := args.Hosts["example.com."]
if len(ips) != 2 {
t.Fatalf("expected 2 IPs for example.com, got %d", len(ips))
}
if !ips[0].Equal(net.ParseIP("1.2.3.4")) || !ips[1].Equal(net.ParseIP("5.6.7.8")) {
t.Errorf("unexpected IPs: %v", ips)
}
}