github.com/phuslu/fastdns@v0.8.3-0.20240310041952-69506fc67dd1/writer_test.go (about) 1 package fastdns 2 3 import ( 4 "net" 5 "net/netip" 6 "testing" 7 ) 8 9 func TestResponseWriterUDP(t *testing.T) { 10 rw := &udpResponseWriter{ 11 AddrPort: netip.AddrPortFrom(netip.AddrFrom4([4]byte{1, 1, 1, 1}), 53), 12 } 13 14 var err error 15 rw.Conn, err = net.DialUDP("udp", nil, net.UDPAddrFromAddrPort(rw.RemoteAddr())) 16 if err != nil { 17 t.Errorf("response writer dial udp error: %+v", err) 18 } 19 _, _ = rw.Write([]byte("test")) 20 21 if s := rw.RemoteAddr().String(); s != "1.1.1.1:53" { 22 t.Errorf("response writer return error remote address: %+v", s) 23 } 24 25 if s := rw.LocalAddr().String(); s == "" { 26 t.Errorf("response writer return empty local address") 27 } 28 } 29 30 func TestResponseWriterMem(t *testing.T) { 31 rw := &MemResponseWriter{ 32 Laddr: netip.AddrPortFrom(netip.AddrFrom4([4]byte{1, 1, 1, 1}), 53), 33 Raddr: netip.AddrPortFrom(netip.AddrFrom4([4]byte{1, 1, 1, 1}), 53), 34 } 35 36 const data = "testdata" 37 38 n, err := rw.Write([]byte(data)) 39 if err != nil || n != len(data) { 40 t.Errorf("response writer write error: %+v length: %d", err, n) 41 } 42 43 if s := rw.RemoteAddr().String(); s != "1.1.1.1:53" { 44 t.Errorf("response writer return error remote address: %+v", s) 45 } 46 47 if s := rw.LocalAddr().String(); s != "1.1.1.1:53" { 48 t.Errorf("response writer return error local address: %+v", s) 49 } 50 }