package quic_test import ( "context" "crypto/rand" "testing" "time" "github.com/google/go-cmp/cmp" "github.com/xtls/xray-core/common" "github.com/xtls/xray-core/common/buf" "github.com/xtls/xray-core/common/net" "github.com/xtls/xray-core/common/protocol/tls/cert" "github.com/xtls/xray-core/testing/servers/udp" "github.com/xtls/xray-core/transport/internet" "github.com/xtls/xray-core/transport/internet/quic" "github.com/xtls/xray-core/transport/internet/stat" "github.com/xtls/xray-core/transport/internet/tls" ) func TestShortQuicConnection(t *testing.T) { testQuicConnection(t, 1024) } func TestAroundMTUQuicConnection(t *testing.T) { testQuicConnection(t, 1247) } func TestLongQuicConnection(t *testing.T) { testQuicConnection(t, 1500) } func testQuicConnection(t *testing.T, dataLen int32) { port := udp.PickPort() listener, err := quic.Listen(context.Background(), net.LocalHostIP, port, &internet.MemoryStreamConfig{ ProtocolName: "quic", ProtocolSettings: &quic.Config{}, SecurityType: "tls", SecuritySettings: &tls.Config{ Certificate: []*tls.Certificate{ tls.ParseCertificate( cert.MustGenerate(nil, cert.DNSNames("www.example.com"), ), ), }, }, }, func(conn stat.Connection) { go func() { defer conn.Close() b := buf.New() defer b.Release() for { b.Clear() if _, err := b.ReadFrom(conn); err != nil { return } common.Must2(conn.Write(b.Bytes())) } }() }) common.Must(err) defer listener.Close() time.Sleep(time.Second) dctx := context.Background() conn, err := quic.Dial(dctx, net.TCPDestination(net.LocalHostIP, port), &internet.MemoryStreamConfig{ ProtocolName: "quic", ProtocolSettings: &quic.Config{}, SecurityType: "tls", SecuritySettings: &tls.Config{ ServerName: "www.example.com", AllowInsecure: true, }, }) common.Must(err) defer conn.Close() b1 := make([]byte, dataLen) common.Must2(rand.Read(b1)) b2 := buf.New() common.Must2(conn.Write(b1)) b2.Clear() common.Must2(b2.ReadFullFrom(conn, dataLen)) if r := cmp.Diff(b2.Bytes(), b1); r != "" { t.Error(r) } time.Sleep(1000 * time.Millisecond) common.Must2(conn.Write(b1)) b2.Clear() common.Must2(b2.ReadFullFrom(conn, dataLen)) if r := cmp.Diff(b2.Bytes(), b1); r != "" { t.Error(r) } }