-
Notifications
You must be signed in to change notification settings - Fork 2
/
nameserver_test.go
94 lines (63 loc) · 1.95 KB
/
nameserver_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
package nameserver
import (
"net"
. "gopkg.in/check.v1"
)
type NameserverSuite struct{}
var _ = Suite(&NameserverSuite{})
func (s *NameserverSuite) TearDownTest(c *C) {
ns.teardown()
}
func (s *NameserverSuite) TestSuccessfulConnectionHasValidAddress(c *C) {
ns = localServer(true)
err := ns.Connect()
c.Assert(err, Equals, nil)
c.Assert(ns.ucon.LocalAddr().Network(), Equals, "udp")
c.Assert(ns.ucon.LocalAddr().String(), Equals, "127.0.0.1:8899")
}
func (s *NameserverSuite) TestUnsuccessfulConnectionReturnsError(c *C) {
ns = localServer(false)
err := ns.Connect()
c.Assert(err, ErrorMatches, "unable to connect:.*")
}
func (s *NameserverSuite) TestCannotUseServeWithoutConnecting(c *C) {
ns = localServer(true)
err := ns.Serve()
c.Assert(err, ErrorMatches, "not connected: must successfully connect with nameserver.Connect first")
}
func setUpListener(resPort int) ([]byte, chan error, chan *net.UDPAddr) {
errChan := make(chan error)
addrChan := make(chan *net.UDPAddr)
b := make([]byte, 512)
resolverListener, _ := net.ListenUDP("udp", localhost(resPort))
go func() {
_, ra, err := resolverListener.ReadFromUDP(b)
defer resolverListener.Close()
addrChan <- ra
errChan <- err
}()
return b, errChan, addrChan
}
func (s *NameserverSuite) Test_reply_OnCorrectListeningPort(c *C) {
const resPort = 8845
ns = localServer(true)
ns.Connect()
var _, errChan, addrChan = setUpListener(resPort)
ns.reply([]byte("hi"), localhost(resPort))
retAddr := <-addrChan
errRead := <-errChan
c.Assert(retAddr.IP.String(), Equals, "127.0.0.1")
c.Assert(retAddr.Port, Equals, nsPort)
c.Assert(errRead, IsNil)
}
func (s *NameserverSuite) Test_reply_WritesToResolverSuccessfully(c *C) {
const resPort = 8845
ns = localServer(true)
ns.Connect()
var b, errChan, addrChan = setUpListener(resPort)
ns.reply([]byte("hi"), localhost(resPort))
<-addrChan
errRead := <-errChan
c.Assert(b[:2], DeepEquals, []byte("hi"))
c.Assert(errRead, IsNil)
}