-
Notifications
You must be signed in to change notification settings - Fork 12
/
Copy pathdns_test.go
139 lines (119 loc) · 4.13 KB
/
dns_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
package main
import (
"net"
"sync"
"testing"
"time"
"github.com/miekg/dns"
)
func RunLocalDNSServer(laddr string, echo bool) (*dns.Server, string, error) {
pc, err := net.ListenPacket("udp", laddr)
if err != nil {
return nil, "", err
}
server := &dns.Server{PacketConn: pc, ReadTimeout: time.Hour, WriteTimeout: time.Hour}
if echo { // Act as a simple echo server
server.Handler = dns.HandlerFunc(func(w dns.ResponseWriter, r *dns.Msg) {
m := new(dns.Msg)
m.SetReply(r)
w.WriteMsg(m)
})
}
waitLock := sync.Mutex{}
waitLock.Lock()
server.NotifyStartedFunc = waitLock.Unlock
go func() {
server.ActivateAndServe()
pc.Close()
}()
waitLock.Lock()
return server, pc.LocalAddr().String(), nil
}
func Test_dnsHandler(t *testing.T) {
db.Reset()
s, addrstr, err := RunLocalDNSServer("127.0.0.1:0", false)
if err != nil {
t.Fatalf("unable to run test server: %v", err)
}
defer s.Shutdown()
es, eaddrstr, err := RunLocalDNSServer("127.0.0.1:0", true)
if err != nil {
t.Fatalf("unable to run echo test server: %v", err)
}
defer es.Shutdown()
*dnsProxyTo = eaddrstr
dns.HandleFunc(".", dnsHandler)
defer dns.HandleRemove(".")
if err := db.put("test.disallowed", &Record{}); err != nil {
t.Errorf("failed to put: %+v", err)
}
if err := db.put("test.allowed", &Record{Paused: true}); err != nil {
t.Errorf("failed to put: %+v", err)
}
// Disallowed record
m := new(dns.Msg)
m.SetQuestion("test.disallowed.", dns.TypeA)
r, err := dns.Exchange(m, addrstr)
if err != nil {
t.Errorf("failed to exchange: %+v", err)
}
testEqual(t, "Disallowed Rcode = %+v, want %+v", r.Rcode, dns.RcodeNameError)
testEqual(t, "Disallowed Authoritative = %+v, want %+v", r.Authoritative, true)
testEqual(t, "Disallowed RecursionAvailable = %+v, want %+v", r.RecursionAvailable, false)
testEqual(t, "Disallowed Questions = %+v, want %+v", r.Question, m.Question)
// Disallowed record, isDisabled
isDisabledMu.Lock()
isDisabled = true
isDisabledMu.Unlock()
m = new(dns.Msg)
m.SetQuestion("test.disallowed.", dns.TypeA)
r, err = dns.Exchange(m, addrstr)
if err != nil {
t.Errorf("failed to exchange: %+v", err)
}
testEqual(t, "Allowed Rcode = %+v, want %+v", r.Rcode, dns.RcodeSuccess)
testEqual(t, "Allowed Questions = %+v, want %+v", r.Question, m.Question)
isDisabledMu.Lock()
isDisabled = false
isDisabledMu.Unlock()
// Allowed record
m = new(dns.Msg)
m.SetQuestion("test.allowed.", dns.TypeA)
r, err = dns.Exchange(m, addrstr)
if err != nil {
t.Errorf("failed to exchange: %+v", err)
}
testEqual(t, "Allowed Rcode = %+v, want %+v", r.Rcode, dns.RcodeSuccess)
testEqual(t, "Allowed Questions = %+v, want %+v", r.Question, m.Question)
// Multiple questions
m = new(dns.Msg)
m.SetQuestion("test.allowed.", dns.TypeA)
m.Question = append(m.Question, dns.Question{Name: "test.disallowed.", Qtype: dns.TypeA, Qclass: dns.ClassINET})
r, err = dns.Exchange(m, addrstr)
if err != nil {
t.Errorf("failed to exchange: %+v", err)
}
testEqual(t, "Multiple Rcode = %+v, want %+v", r.Rcode, dns.RcodeSuccess)
testEqual(t, "Multiple Response len() = %+v, want %+v", len(r.Question), 1)
testEqual(t, "Multiple Response Question = %+v, want %+v", r.Question[0].Name, "test.allowed.")
}
func Test_filterQuestions(t *testing.T) {
db.Reset()
if err := db.put("test.allowed", &Record{Paused: true}); err != nil {
t.Errorf("failed to put: %+v", err)
}
if err := db.put("test.disallowed", &Record{}); err != nil {
t.Errorf("failed to put: %+v", err)
}
qs := filterQuestions([]dns.Question{{Name: "Test.Allowed."}, {Name: "Test.disallowed"}})
testEqual(t, "len(filterQuestions(...)) = %+v, want %+v", len(qs), 1)
testEqual(t, "filterQuestions(...)[0].Name = %+v, want %+v", qs[0].Name, "Test.Allowed.")
}
func Test_isNameAllowed(t *testing.T) {
db.Reset()
db.put("Test.allowed", &Record{Paused: true})
db.put("Test.disallowed", &Record{})
testEqual(t, "isNameAllowed('test.allowed') = %+v, want %+v", isNameAllowed("Test.Allowed."), true)
testEqual(t, "isNameAllowed('test.disallowed') = %+v, want %+v", isNameAllowed("Test.Disallowed"), false)
testEqual(t, "isNameAllowed('not.in.db') = %+v, want %+v", isNameAllowed("not.in.db."), true)
}