-
Notifications
You must be signed in to change notification settings - Fork 4
/
config_test.go
208 lines (194 loc) · 4.83 KB
/
config_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
203
204
205
206
207
208
package zdns
import (
"fmt"
"strings"
"testing"
"time"
)
func TestConfig(t *testing.T) {
text := `
[dns]
listen = "0.0.0.0:53"
protocol = "udp"
cache_size = 2048
resolvers = [
"192.0.2.1:53",
"192.0.2.2:53=example.com",
]
hijack_mode = "zero" # or: empty, hosts
hosts_refresh_interval = "48h"
database = "/tmp/log.db"
log_mode = "all"
log_ttl = "72h"
[resolver]
protocol = "tcp-tls" # or: "", "udp", "tcp"
timeout = "1s"
[[hosts]]
url = "file:///home/foo/hosts-good"
hijack = false
[[hosts]]
url = "https://raw.githubusercontent.com/StevenBlack/hosts/master/hosts"
timeout = "10s"
hijack = true
[[hosts]]
entries = [
"0.0.0.0 goodhost1",
"0.0.0.0 goodhost2",
]
hijack = false
`
r := strings.NewReader(text)
conf, err := ReadConfig(r)
if err != nil {
t.Fatal(err)
}
var intTests = []struct {
field string
got int
want int
}{
{"DNS.CacheSize", conf.DNS.CacheSize, 2048},
{"len(DNS.Resolvers)", len(conf.DNS.Resolvers), 2},
{"Resolver.Timeout", int(conf.Resolver.Timeout), int(time.Second)},
{"DNS.RefreshInterval", int(conf.DNS.refreshInterval), int(48 * time.Hour)},
{"len(Hosts)", len(conf.Hosts), 3},
{"DNS.LogTTL", int(conf.DNS.LogTTL), int(72 * time.Hour)},
}
for i, tt := range intTests {
if tt.got != tt.want {
t.Errorf("#%d: %s = %d, want %d", i, tt.field, tt.got, tt.want)
}
}
var stringTests = []struct {
field string
got string
want string
}{
{"DNS.Listen", conf.DNS.Listen, "0.0.0.0:53"},
{"DNS.Protocol", conf.DNS.Protocol, "udp"},
{"DNS.Resolvers[0]", conf.DNS.Resolvers[0], "192.0.2.1:53"},
{"DNS.Resolvers[1]", conf.DNS.Resolvers[1], "192.0.2.2:53=example.com"},
{"DNS.HijackMode", conf.DNS.HijackMode, "zero"},
{"DNS.Database", conf.DNS.Database, "/tmp/log.db"},
{"DNS.LogMode", conf.DNS.LogModeString, "all"},
{"DNS.LogTTL", conf.DNS.LogTTLString, "72h"},
{"Resolver.Protocol", conf.Resolver.Protocol, "tcp-tls"},
{"Hosts[0].Source", conf.Hosts[0].URL, "file:///home/foo/hosts-good"},
{"Hosts[1].Source", conf.Hosts[1].URL, "https://raw.githubusercontent.com/StevenBlack/hosts/master/hosts"},
{"Hosts[1].Timeout", conf.Hosts[1].Timeout, "10s"},
{"Hosts[2].hosts", fmt.Sprintf("%+v", conf.Hosts[2].hosts), "map[goodhost1:[{IP:0.0.0.0 Zone:}] goodhost2:[{IP:0.0.0.0 Zone:}]]"},
}
for i, tt := range stringTests {
if tt.got != tt.want {
t.Errorf("#%d: %s = %q, want %q", i, tt.field, tt.got, tt.want)
}
}
var boolTests = []struct {
field string
got bool
want bool
}{
{"Hosts[0].Hijack", conf.Hosts[0].Hijack, false},
{"Hosts[1].Hijack", conf.Hosts[1].Hijack, true},
}
for i, tt := range boolTests {
if tt.got != tt.want {
t.Errorf("#%d: %s = %t, want %t", i, tt.field, tt.got, tt.want)
}
}
}
func TestConfigErrors(t *testing.T) {
baseConf := "[dns]\nlisten = \"0.0.0.0:53\"\n"
conf0 := baseConf + "cache_size = -1"
conf1 := baseConf + `
hijack_mode = "foo"
`
conf2 := baseConf + `
hosts_refresh_interval = "foo"
`
conf3 := baseConf + `
hosts_refresh_interval = "-1h"
`
conf4 := baseConf + `
resolvers = ["foo"]
`
conf5 := baseConf + `
[resolver]
protocol = "foo"
`
conf6 := baseConf + `
[resolver]
timeout = "foo"
`
conf7 := baseConf + `
[resolver]
timeout = "-1s"
`
conf8 := baseConf + `
[[hosts]]
url = ":foo"
`
conf9 := baseConf + `
[[hosts]]
url = "foo://bar"
`
conf10 := baseConf + `
[[hosts]]
url = "file:///tmp/foo"
timeout = "1s"
`
conf11 := baseConf + `
[[hosts]]
entries = ["0.0.0.0 host1"]
timeout = "1s"
`
conf12 := baseConf + `
log_mode = "foo"
[resolver]
timeout = "1s"
`
conf13 := baseConf + `
log_mode = "hijacked"
[resolver]
timeout = "1s"
`
conf14 := baseConf + `
resolvers = ["http://example.com"]
[resolver]
protocol = "https"
`
conf15 := baseConf + `
cache_persist = true
`
var tests = []struct {
in string
err string
}{
{conf0, "cache size must be >= 0"},
{conf1, "invalid hijack mode: foo"},
{conf2, "invalid refresh interval: time: invalid duration \"foo\""},
{conf3, "refresh interval must be >= 0"},
{conf4, "invalid resolver: address foo: missing port in address"},
{conf5, "invalid resolver protocol: foo"},
{conf6, "invalid resolver timeout: foo"},
{conf7, "resolver timeout must be >= 0"},
{conf8, ":foo: invalid url: parse \":foo\": missing protocol scheme"},
{conf9, "foo://bar: unsupported scheme: foo"},
{conf10, "file:///tmp/foo: timeout cannot be set for file url"},
{conf11, "[0.0.0.0 host1]: timeout cannot be set for inline hosts"},
{conf12, "invalid log mode: foo"},
{conf13, `log_mode = "hijacked" requires 'database' to be set`},
{conf14, "protocol https requires https scheme for resolver http://example.com"},
{conf15, "cache_persist = true requires 'database' to be set"},
}
for i, tt := range tests {
var got string
_, err := ReadConfig(strings.NewReader(tt.in))
if err != nil {
got = err.Error()
}
if got != tt.err {
t.Errorf("#%d: want %q, got %q", i, tt.err, got)
}
}
}