-
Notifications
You must be signed in to change notification settings - Fork 12
/
writer_test.go
50 lines (40 loc) · 1.25 KB
/
writer_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
package fastdns
import (
"net"
"net/netip"
"testing"
)
func TestResponseWriterUDP(t *testing.T) {
rw := &udpResponseWriter{
AddrPort: netip.AddrPortFrom(netip.AddrFrom4([4]byte{1, 1, 1, 1}), 53),
}
var err error
rw.Conn, err = net.DialUDP("udp", nil, net.UDPAddrFromAddrPort(rw.RemoteAddr()))
if err != nil {
t.Errorf("response writer dial udp error: %+v", err)
}
_, _ = rw.Write([]byte("test"))
if s := rw.RemoteAddr().String(); s != "1.1.1.1:53" {
t.Errorf("response writer return error remote address: %+v", s)
}
if s := rw.LocalAddr().String(); s == "" {
t.Errorf("response writer return empty local address")
}
}
func TestResponseWriterMem(t *testing.T) {
rw := &MemResponseWriter{
Laddr: netip.AddrPortFrom(netip.AddrFrom4([4]byte{1, 1, 1, 1}), 53),
Raddr: netip.AddrPortFrom(netip.AddrFrom4([4]byte{1, 1, 1, 1}), 53),
}
const data = "testdata"
n, err := rw.Write([]byte(data))
if err != nil || n != len(data) {
t.Errorf("response writer write error: %+v length: %d", err, n)
}
if s := rw.RemoteAddr().String(); s != "1.1.1.1:53" {
t.Errorf("response writer return error remote address: %+v", s)
}
if s := rw.LocalAddr().String(); s != "1.1.1.1:53" {
t.Errorf("response writer return error local address: %+v", s)
}
}