-
Notifications
You must be signed in to change notification settings - Fork 0
/
dns_test.go
73 lines (57 loc) · 1.45 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
package main
import (
"net"
"sync"
"testing"
"time"
"github.com/miekg/dns"
"github.com/stretchr/testify/assert"
)
func RunLocalUDPServer(laddr string) (*dns.Server, string, error) {
server, l, _, err := RunLocalUDPServerWithFinChan(laddr)
return server, l, err
}
func RunLocalUDPServerWithFinChan(laddr string) (*dns.Server, string, chan struct{}, error) {
pc, err := net.ListenPacket("udp", laddr)
if err != nil {
return nil, "", nil, err
}
server := &dns.Server{PacketConn: pc, ReadTimeout: time.Hour, WriteTimeout: time.Hour}
waitLock := sync.Mutex{}
waitLock.Lock()
server.NotifyStartedFunc = waitLock.Unlock
fin := make(chan struct{}, 0)
go func() {
server.ActivateAndServe()
close(fin)
pc.Close()
}()
waitLock.Lock()
return server, pc.LocalAddr().String(), fin, nil
}
func TestDNS_OK(t *testing.T) {
h := newDefaultDNSServerHandler()
dns.HandleFunc("test.", serve)
defer dns.HandleRemove("test.")
err := h.ds.ListenAndServe()
if err != nil {
assert.Error(t, err)
}
m := new(dns.Msg)
m.SetQuestion("go.test.", dns.TypeA)
s, addrstr, err := RunLocalUDPServer("127.0.0.1:0")
if err != nil {
t.Fatalf("unable to run test server: %v", err)
}
defer s.Shutdown()
cn, err := dns.Dial("udp", addrstr)
if err != nil {
t.Errorf("failed to dial %s: %v", addrstr, err)
}
err = cn.WriteMsg(m)
assert.NoError(t, err)
r, err := cn.ReadMsg()
assert.NoError(t, err)
assert.NotNil(t, r)
assert.NotEqual(t, r, dns.RcodeSuccess)
}