github.com/haraldrudell/parl@v0.4.176/pnet/udp_test.go (about) 1 /* 2 © 2021–present Harald Rudell <harald.rudell@gmail.com> (https://haraldrudell.github.io/haraldrudell/) 3 ISC License 4 */ 5 6 package pnet 7 8 import ( 9 "bytes" 10 "context" 11 "net" 12 "sync" 13 "testing" 14 15 "github.com/haraldrudell/parl/perrors" 16 ) 17 18 func TestNewUdp(t *testing.T) { 19 network := "udp" 20 address := "127.0.0.1:0" 21 maxSize := 0 22 var udp *UDP 23 packet := []byte{65} 24 packetHandler := func(b []byte, oob []byte, flags int, addr *net.UDPAddr) { 25 t.Logf("packetHandler: bytes: %d oob: %d flags: %x addr: %s", len(b), len(oob), flags, addr.String()) 26 if len(oob) > 0 { 27 t.Errorf("oob not zero") 28 return 29 } 30 if !bytes.Equal(b, packet) { 31 t.Errorf("received packet different from sent packet") 32 return 33 } 34 if flags != 0 { 35 t.Errorf("flags not zero") 36 return 37 } 38 t.Logf("shutting down") 39 udp.Shutdown() // this will cause errCh to close 40 } 41 42 // listen to udp packets 43 udp = NewUDP(network, address, packetHandler, maxSize) 44 if udp.MaxSize != udpDefaultMaxSize { 45 t.Errorf("Bad udp.MaxSize: %d expected %d", udp.MaxSize, udpDefaultMaxSize) 46 return 47 } 48 49 // error listening thread 50 errCh := udp.Listen() 51 var errChWg sync.WaitGroup 52 errChWg.Add(1) 53 go func() { 54 defer errChWg.Done() 55 t.Log("reading errCh") 56 err, ok := <-errCh 57 if !ok { 58 t.Log("errCh closed") 59 return 60 } 61 t.Error("errCh has error") 62 panic(err) 63 }() 64 isUp, addr := udp.WaitForUp() 65 if !isUp { 66 t.Logf("bad: WaitForUp: is not up") 67 errChWg.Wait() 68 t.Errorf("%v", perrors.New("WaitForUp: is not up")) 69 return 70 } 71 t.Logf("Listening at %s", addr.String()) 72 73 // send udp packet 74 var dialer net.Dialer 75 ctx := context.Background() 76 netConn, err := dialer.DialContext(ctx, addr.Network(), addr.String()) 77 if err != nil { 78 t.Errorf("%v", perrors.Errorf("DialContext: '%w'", err)) 79 return 80 } 81 t.Logf("sending socket: %s", netConn.LocalAddr().String()) 82 if n, err := netConn.Write(packet); err != nil { 83 t.Errorf("%v", perrors.Errorf("netConn.Write: '%w'", err)) 84 return 85 } else { 86 t.Logf("wrote %d bytes", n) 87 } 88 89 // wait for shutdown from packetHandler 90 errChWg.Wait() 91 92 t.Log("Completed successfully") 93 } 94 95 /* 96 To continuously listen to 97 98 func net.Listen(network string, address string) (net.Listener, error) 99 func net.ListenIP(network string, laddr *net.IPAddr) (*net.IPConn, error) 100 func net.ListenMulticastUDP(network string, ifi *net.Interface, gaddr *net.UDPAddr) (*net.UDPConn, error) 101 func net.ListenPacket(network string, address string) (net.PacketConn, error) 102 func net.ListenTCP(network string, laddr *net.TCPAddr) (*net.TCPListener, error) 103 func net.ListenUDP(network string, laddr *net.UDPAddr) (*net.UDPConn, error) 104 func net.ListenUnix(network string, laddr *net.UnixAddr) (*net.UnixListener, error) 105 func net.ListenUnixgram(network string, laddr *net.UnixAddr) (*net.UnixConn, error) 106 107 Network connection from file: 108 func net.FileConn(f *os.File) (c net.Conn, err error) 109 func net.FileListener(f *os.File) (ln net.Listener, err error) 110 func net.FilePacketConn(f *os.File) (c net.PacketConn, err error) 111 112 Networks: 113 "udp" 114 "udp4" 115 "udp6" 116 "unixgram" 117 "ip" 118 "ip4" 119 "ip6" 120 121 var protocols = map[string]int{ 122 "icmp": 1, 123 "igmp": 2, 124 "tcp": 6, 125 "udp": 17, 126 "ipv6-icmp": 58, 127 } 128 var services = map[string]map[string]int{ 129 "udp": { 130 "domain": 53, 131 }, 132 "tcp": { 133 "ftp": 21, 134 "ftps": 990, 135 "gopher": 70, // ʕ◔ϖ◔ʔ 136 "http": 80, 137 "https": 443, 138 "imap2": 143, 139 "imap3": 220, 140 "imaps": 993, 141 "pop3": 110, 142 "pop3s": 995, 143 "smtp": 25, 144 "ssh": 22, 145 "telnet": 23, 146 */