github.com/moqsien/xraycore@v1.8.5/transport/internet/quic/quic_test.go (about) 1 package quic_test 2 3 import ( 4 "context" 5 "crypto/rand" 6 "testing" 7 "time" 8 9 "github.com/google/go-cmp/cmp" 10 "github.com/moqsien/xraycore/common" 11 "github.com/moqsien/xraycore/common/buf" 12 "github.com/moqsien/xraycore/common/net" 13 "github.com/moqsien/xraycore/common/protocol" 14 "github.com/moqsien/xraycore/common/protocol/tls/cert" 15 "github.com/moqsien/xraycore/common/serial" 16 "github.com/moqsien/xraycore/testing/servers/udp" 17 "github.com/moqsien/xraycore/transport/internet" 18 "github.com/moqsien/xraycore/transport/internet/headers/wireguard" 19 "github.com/moqsien/xraycore/transport/internet/quic" 20 "github.com/moqsien/xraycore/transport/internet/stat" 21 "github.com/moqsien/xraycore/transport/internet/tls" 22 ) 23 24 func TestQuicConnection(t *testing.T) { 25 port := udp.PickPort() 26 27 listener, err := quic.Listen(context.Background(), net.LocalHostIP, port, &internet.MemoryStreamConfig{ 28 ProtocolName: "quic", 29 ProtocolSettings: &quic.Config{}, 30 SecurityType: "tls", 31 SecuritySettings: &tls.Config{ 32 Certificate: []*tls.Certificate{ 33 tls.ParseCertificate( 34 cert.MustGenerate(nil, 35 cert.DNSNames("www.example.com"), 36 ), 37 ), 38 }, 39 }, 40 }, func(conn stat.Connection) { 41 go func() { 42 defer conn.Close() 43 44 b := buf.New() 45 defer b.Release() 46 47 for { 48 b.Clear() 49 if _, err := b.ReadFrom(conn); err != nil { 50 return 51 } 52 common.Must2(conn.Write(b.Bytes())) 53 } 54 }() 55 }) 56 common.Must(err) 57 58 defer listener.Close() 59 60 time.Sleep(time.Second) 61 62 dctx := context.Background() 63 conn, err := quic.Dial(dctx, net.TCPDestination(net.LocalHostIP, port), &internet.MemoryStreamConfig{ 64 ProtocolName: "quic", 65 ProtocolSettings: &quic.Config{}, 66 SecurityType: "tls", 67 SecuritySettings: &tls.Config{ 68 ServerName: "www.example.com", 69 AllowInsecure: true, 70 }, 71 }) 72 common.Must(err) 73 defer conn.Close() 74 75 const N = 1024 76 b1 := make([]byte, N) 77 common.Must2(rand.Read(b1)) 78 b2 := buf.New() 79 80 common.Must2(conn.Write(b1)) 81 82 b2.Clear() 83 common.Must2(b2.ReadFullFrom(conn, N)) 84 if r := cmp.Diff(b2.Bytes(), b1); r != "" { 85 t.Error(r) 86 } 87 88 common.Must2(conn.Write(b1)) 89 90 b2.Clear() 91 common.Must2(b2.ReadFullFrom(conn, N)) 92 if r := cmp.Diff(b2.Bytes(), b1); r != "" { 93 t.Error(r) 94 } 95 } 96 97 func TestQuicConnectionWithoutTLS(t *testing.T) { 98 port := udp.PickPort() 99 100 listener, err := quic.Listen(context.Background(), net.LocalHostIP, port, &internet.MemoryStreamConfig{ 101 ProtocolName: "quic", 102 ProtocolSettings: &quic.Config{}, 103 }, func(conn stat.Connection) { 104 go func() { 105 defer conn.Close() 106 107 b := buf.New() 108 defer b.Release() 109 110 for { 111 b.Clear() 112 if _, err := b.ReadFrom(conn); err != nil { 113 return 114 } 115 common.Must2(conn.Write(b.Bytes())) 116 } 117 }() 118 }) 119 common.Must(err) 120 121 defer listener.Close() 122 123 time.Sleep(time.Second) 124 125 dctx := context.Background() 126 conn, err := quic.Dial(dctx, net.TCPDestination(net.LocalHostIP, port), &internet.MemoryStreamConfig{ 127 ProtocolName: "quic", 128 ProtocolSettings: &quic.Config{}, 129 }) 130 common.Must(err) 131 defer conn.Close() 132 133 const N = 1024 134 b1 := make([]byte, N) 135 common.Must2(rand.Read(b1)) 136 b2 := buf.New() 137 138 common.Must2(conn.Write(b1)) 139 140 b2.Clear() 141 common.Must2(b2.ReadFullFrom(conn, N)) 142 if r := cmp.Diff(b2.Bytes(), b1); r != "" { 143 t.Error(r) 144 } 145 146 common.Must2(conn.Write(b1)) 147 148 b2.Clear() 149 common.Must2(b2.ReadFullFrom(conn, N)) 150 if r := cmp.Diff(b2.Bytes(), b1); r != "" { 151 t.Error(r) 152 } 153 } 154 155 func TestQuicConnectionAuthHeader(t *testing.T) { 156 port := udp.PickPort() 157 158 listener, err := quic.Listen(context.Background(), net.LocalHostIP, port, &internet.MemoryStreamConfig{ 159 ProtocolName: "quic", 160 ProtocolSettings: &quic.Config{ 161 Header: serial.ToTypedMessage(&wireguard.WireguardConfig{}), 162 Key: "abcd", 163 Security: &protocol.SecurityConfig{ 164 Type: protocol.SecurityType_AES128_GCM, 165 }, 166 }, 167 }, func(conn stat.Connection) { 168 go func() { 169 defer conn.Close() 170 171 b := buf.New() 172 defer b.Release() 173 174 for { 175 b.Clear() 176 if _, err := b.ReadFrom(conn); err != nil { 177 return 178 } 179 common.Must2(conn.Write(b.Bytes())) 180 } 181 }() 182 }) 183 common.Must(err) 184 185 defer listener.Close() 186 187 time.Sleep(time.Second) 188 189 dctx := context.Background() 190 conn, err := quic.Dial(dctx, net.TCPDestination(net.LocalHostIP, port), &internet.MemoryStreamConfig{ 191 ProtocolName: "quic", 192 ProtocolSettings: &quic.Config{ 193 Header: serial.ToTypedMessage(&wireguard.WireguardConfig{}), 194 Key: "abcd", 195 Security: &protocol.SecurityConfig{ 196 Type: protocol.SecurityType_AES128_GCM, 197 }, 198 }, 199 }) 200 common.Must(err) 201 defer conn.Close() 202 203 const N = 1024 204 b1 := make([]byte, N) 205 common.Must2(rand.Read(b1)) 206 b2 := buf.New() 207 208 common.Must2(conn.Write(b1)) 209 210 b2.Clear() 211 common.Must2(b2.ReadFullFrom(conn, N)) 212 if r := cmp.Diff(b2.Bytes(), b1); r != "" { 213 t.Error(r) 214 } 215 216 common.Must2(conn.Write(b1)) 217 218 b2.Clear() 219 common.Must2(b2.ReadFullFrom(conn, N)) 220 if r := cmp.Diff(b2.Bytes(), b1); r != "" { 221 t.Error(r) 222 } 223 }