github.com/puellanivis/breton@v0.2.16/lib/files/socketfiles/udp_test.go (about) 1 package socketfiles 2 3 import ( 4 "context" 5 "net" 6 "net/url" 7 "testing" 8 "time" 9 ) 10 11 func TestUDPName(t *testing.T) { 12 sock := &socket{ 13 qaddr: &net.UDPAddr{ 14 IP: []byte{127, 0, 0, 1}, 15 Port: 65535, 16 }, 17 addr: &net.UDPAddr{ 18 IP: []byte{127, 0, 0, 2}, 19 Port: 80, 20 }, 21 22 packetSize: 188, 23 bufferSize: 1024, 24 maxPacketSize: 1316, 25 26 ttl: 100, 27 tos: 0x80, 28 29 throttler: throttler{ 30 bitrate: 2048, 31 }, 32 } 33 34 uri := sock.uri() 35 expected := "udp://127.0.0.2:80?buffer_size=1024&localaddr=127.0.0.1&localport=65535&max_bitrate=2048&max_pkt_size=1316&pkt_size=188&tos=0x80&ttl=100" 36 37 if s := uri.String(); s != expected { 38 t.Errorf("got a bad URI, was expecting, but got:\n\t%v\n\t%v", expected, s) 39 } 40 41 sock = &socket{ 42 qaddr: &net.UDPAddr{ 43 IP: []byte{127, 0, 0, 1}, 44 Port: 65534, 45 }, 46 addr: &net.UDPAddr{ 47 IP: []byte{127, 0, 0, 2}, 48 Port: 443, 49 }, 50 } 51 52 uri = sock.uri() 53 expected = "udp://127.0.0.2:443?localaddr=127.0.0.1&localport=65534" 54 55 if s := uri.String(); s != expected { 56 t.Errorf("got a bad URI, was expecting, but got:\n\t%v\n\t%v", expected, s) 57 } 58 59 sock = &socket{ 60 addr: &net.UDPAddr{ 61 IP: []byte{127, 0, 0, 2}, 62 Port: 8080, 63 }, 64 } 65 66 uri = sock.uri() 67 expected = "udp://127.0.0.2:8080" 68 69 if s := uri.String(); s != expected { 70 t.Errorf("got a bad URI, was expecting, but got:\n\t%v\n\t%v", expected, s) 71 } 72 } 73 74 func TestUDPNoSlashSlash(t *testing.T) { 75 uri, err := url.Parse("udp:0.0.0.0:0") 76 if err != nil { 77 t.Fatal("unexpected error parsing constant URL") 78 } 79 80 ctx, cancel := context.WithTimeout(context.Background(), 1*time.Second) 81 82 _, err = (&udpHandler{}).Open(ctx, uri) 83 if err == nil { 84 t.Fatal("expected Open(\"udp:0.0.0.0:0\") to error, it did not") 85 } 86 cancel() 87 88 ctx, cancel = context.WithTimeout(context.Background(), 1*time.Second) 89 90 _, err = (&udpHandler{}).Create(ctx, uri) 91 if err == nil { 92 t.Fatal("expected Create(\"udp:0.0.0.0:0\") to error, it did not") 93 } 94 cancel() 95 } 96 97 func TestUDPEmptyURL(t *testing.T) { 98 uri, err := url.Parse("udp:") 99 if err != nil { 100 t.Fatal("unexpected error parsing constant URL") 101 } 102 103 ctx, cancel := context.WithTimeout(context.Background(), 1*time.Second) 104 105 _, err = (&udpHandler{}).Open(ctx, uri) 106 if err == nil { 107 t.Fatal("expected Open(\"udp:\") to error, it did not") 108 } 109 cancel() 110 111 ctx, cancel = context.WithTimeout(context.Background(), 1*time.Second) 112 113 _, err = (&udpHandler{}).Create(ctx, uri) 114 if err == nil { 115 t.Fatal("expected Create(\"udp:\") to error, it did not") 116 } 117 cancel() 118 } 119 120 func TestUDPBadLocalAddr(t *testing.T) { 121 uri, err := url.Parse("udp://0.0.0.0:0?localaddr=invalid") 122 if err != nil { 123 t.Fatal("unexpected error parsing constant URL") 124 } 125 126 ctx, cancel := context.WithTimeout(context.Background(), 1*time.Second) 127 128 w, err := (&udpHandler{}).Create(ctx, uri) 129 if err == nil { 130 w.Close() 131 t.Fatal("exepcted Create(\"udp://0.0.0.0:0?localaddr=invalid\") to error, it did not") 132 } 133 cancel() 134 }