-
Notifications
You must be signed in to change notification settings - Fork 4
/
server_test.go
214 lines (200 loc) · 4.81 KB
/
server_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
209
210
211
212
213
214
package zdns
import (
"io/ioutil"
"log"
"net"
"net/http"
"net/http/httptest"
"os"
"reflect"
"testing"
"time"
"github.com/mpolden/zdns/cache"
"github.com/mpolden/zdns/dns"
"github.com/mpolden/zdns/hosts"
)
func init() {
log.SetOutput(ioutil.Discard)
}
const hostsFile1 = `
192.0.2.1 badhost1
2001:db8::1 badhost1
192.0.2.2 badhost2
192.0.2.3 badhost3
`
const hostsFile2 = `
192.0.2.4 badhost4
192.0.2.5 badhost5
192.0.2.6 badhost6
`
func httpHandler(t *testing.T, response string) http.Handler {
return http.HandlerFunc(func(w http.ResponseWriter, r *http.Request) {
if _, err := w.Write([]byte(response)); err != nil {
t.Fatal(err)
}
})
}
func httpServer(t *testing.T, s string) *httptest.Server {
return httptest.NewServer(httpHandler(t, s))
}
func tempFile(t *testing.T, s string) (string, error) {
f, err := ioutil.TempFile("", "zdns")
if err != nil {
return "", err
}
defer f.Close()
if err := ioutil.WriteFile(f.Name(), []byte(s), 0644); err != nil {
return "", err
}
return f.Name(), nil
}
func testServer(t *testing.T, refreshInterval time.Duration) (*Server, func()) {
var (
httpSrv *httptest.Server
srv *Server
file string
err error
)
cleanup := func() {
if httpSrv != nil {
httpSrv.Close()
}
if file != "" {
if err := os.Remove(file); err != nil {
t.Error(err)
}
}
if srv != nil {
if err := srv.Close(); err != nil {
t.Error(err)
}
}
}
httpSrv = httpServer(t, hostsFile1)
file, err = tempFile(t, hostsFile2)
if err != nil {
defer cleanup()
t.Fatal(err)
}
config := Config{
DNS: DNSOptions{Listen: "0.0.0.0:53",
hijackMode: HijackZero,
refreshInterval: refreshInterval,
},
Resolver: ResolverOptions{TimeoutString: "0"},
Hosts: []Hosts{
{URL: httpSrv.URL, Hijack: true},
{URL: "file://" + file, Hijack: true},
{Hosts: []string{"192.0.2.5 badhost5"}},
},
}
if err := config.load(); err != nil {
t.Fatal(err)
}
proxy, err := dns.NewProxy(cache.New(0, nil), nil, nil)
if err != nil {
t.Fatal(err)
}
srv, err = NewServer(proxy, config)
if err != nil {
defer cleanup()
t.Fatal(err)
}
ts := time.Now()
for {
srv.mu.RLock()
hostsLoaded := srv.hosts != nil
srv.mu.RUnlock()
if hostsLoaded {
break
}
time.Sleep(10 * time.Millisecond)
if time.Since(ts) > 2*time.Second {
t.Fatal("timed out waiting initial hosts to load")
}
}
return srv, cleanup
}
func TestLoadHosts(t *testing.T) {
s, cleanup := testServer(t, 10*time.Millisecond)
defer cleanup()
want := hosts.Hosts{
"badhost1": []net.IPAddr{{IP: net.ParseIP("192.0.2.1")}, {IP: net.ParseIP("2001:db8::1")}},
"badhost2": []net.IPAddr{{IP: net.ParseIP("192.0.2.2")}},
"badhost3": []net.IPAddr{{IP: net.ParseIP("192.0.2.3")}},
"badhost4": []net.IPAddr{{IP: net.ParseIP("192.0.2.4")}},
"badhost6": []net.IPAddr{{IP: net.ParseIP("192.0.2.6")}},
}
got := s.hosts
if !reflect.DeepEqual(want, got) {
t.Errorf("got %+v, want %+v", got, want)
}
}
func TestReloadHostsOnTick(t *testing.T) {
s, cleanup := testServer(t, 10*time.Millisecond)
defer cleanup()
oldHosts := s.hosts
if oldHosts == nil {
t.Fatal("expected matcher to be initialized")
}
ts := time.Now()
for &s.hosts == &oldHosts {
time.Sleep(10 * time.Millisecond)
if time.Since(ts) > 2*time.Second {
t.Fatal("timed out waiting hosts to load")
}
}
}
func TestNonFqdn(t *testing.T) {
var tests = []struct {
in, out string
}{
{"", ""},
{"foo", "foo"},
{"foo.", "foo"},
}
for i, tt := range tests {
got := nonFqdn(tt.in)
if got != tt.out {
t.Errorf("#%d: nonFqdn(%q) = %q, want %q", i, tt.in, got, tt.out)
}
}
}
func TestHijack(t *testing.T) {
s := &Server{
Config: Config{},
hosts: hosts.Hosts{
"badhost1": []net.IPAddr{
{IP: net.ParseIP("192.0.2.1")},
{IP: net.ParseIP("2001:db8::1")},
},
},
}
var tests = []struct {
rtype uint16
rname string
mode int
out string
}{
{dns.TypeA, "goodhost1", HijackZero, ""}, // Unmatched host
{dns.TypeAAAA, "goodhost1", HijackZero, ""}, // Unmatched host
{15 /* MX */, "badhost1", HijackZero, ""}, // Unmatched type
{dns.TypeA, "badhost1", HijackZero, "badhost1\t3600\tIN\tA\t0.0.0.0"},
{dns.TypeA, "badhost1", HijackEmpty, ""},
{dns.TypeA, "badhost1", HijackHosts, "badhost1\t3600\tIN\tA\t192.0.2.1"},
{dns.TypeAAAA, "badhost1", HijackZero, "badhost1\t3600\tIN\tAAAA\t::"},
{dns.TypeAAAA, "badhost1", HijackEmpty, ""},
{dns.TypeAAAA, "badhost1", HijackHosts, "badhost1\t3600\tIN\tAAAA\t2001:db8::1"},
}
for i, tt := range tests {
s.Config.DNS.hijackMode = tt.mode
req := &dns.Request{Type: tt.rtype, Name: tt.rname}
reply := s.hijack(&dns.Request{Type: tt.rtype, Name: tt.rname})
if reply == nil && tt.out == "" {
reply = &dns.Reply{}
}
if reply.String() != tt.out {
t.Errorf("#%d: hijack(%+v) = %q, want %q", i, req, reply.String(), tt.out)
}
}
}