1
0
mirror of https://github.com/v2fly/v2ray-core.git synced 2024-11-04 17:27:23 -05:00
v2fly/transport/internet/quic/quic_test.go

218 lines
4.6 KiB
Go
Raw Normal View History

2018-11-21 10:47:06 -05:00
package quic_test
import (
"context"
"crypto/rand"
"testing"
"time"
2019-02-10 09:02:28 -05:00
"github.com/google/go-cmp/cmp"
2018-11-21 10:47:06 -05:00
"v2ray.com/core/common"
"v2ray.com/core/common/buf"
"v2ray.com/core/common/net"
2018-11-21 16:02:19 -05:00
"v2ray.com/core/common/protocol"
2018-11-21 10:47:06 -05:00
"v2ray.com/core/common/protocol/tls/cert"
2018-11-21 16:02:19 -05:00
"v2ray.com/core/common/serial"
2018-11-21 10:47:06 -05:00
"v2ray.com/core/testing/servers/udp"
"v2ray.com/core/transport/internet"
2018-11-21 16:02:19 -05:00
"v2ray.com/core/transport/internet/headers/wireguard"
2018-11-21 10:47:06 -05:00
"v2ray.com/core/transport/internet/quic"
"v2ray.com/core/transport/internet/tls"
)
func TestQuicConnection(t *testing.T) {
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.v2ray.com"), cert.CommonName("www.v2ray.com")))},
},
}, func(conn internet.Connection) {
go func() {
defer conn.Close()
b := buf.New()
defer b.Release()
for {
b.Clear()
if _, err := b.ReadFrom(conn); err != nil {
return
}
2019-02-10 09:02:28 -05:00
common.Must2(conn.Write(b.Bytes()))
2018-11-21 10:47:06 -05:00
}
}()
})
2019-02-02 16:19:30 -05:00
common.Must(err)
2018-11-21 10:47:06 -05:00
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.v2ray.com",
AllowInsecure: true,
},
})
2019-02-02 16:19:30 -05:00
common.Must(err)
2018-11-21 10:47:06 -05:00
defer conn.Close()
const N = 1024
b1 := make([]byte, N)
common.Must2(rand.Read(b1))
b2 := buf.New()
2019-02-10 09:02:28 -05:00
common.Must2(conn.Write(b1))
2018-11-21 10:47:06 -05:00
b2.Clear()
common.Must2(b2.ReadFullFrom(conn, N))
2019-02-10 09:02:28 -05:00
if r := cmp.Diff(b2.Bytes(), b1); r != "" {
t.Error(r)
}
2018-11-21 10:47:06 -05:00
2019-02-10 09:02:28 -05:00
common.Must2(conn.Write(b1))
2018-11-21 10:47:06 -05:00
b2.Clear()
common.Must2(b2.ReadFullFrom(conn, N))
2019-02-10 09:02:28 -05:00
if r := cmp.Diff(b2.Bytes(), b1); r != "" {
t.Error(r)
}
2018-11-21 10:47:06 -05:00
}
2018-11-21 16:02:19 -05:00
func TestQuicConnectionWithoutTLS(t *testing.T) {
port := udp.PickPort()
listener, err := quic.Listen(context.Background(), net.LocalHostIP, port, &internet.MemoryStreamConfig{
ProtocolName: "quic",
ProtocolSettings: &quic.Config{},
}, func(conn internet.Connection) {
go func() {
defer conn.Close()
b := buf.New()
defer b.Release()
for {
b.Clear()
if _, err := b.ReadFrom(conn); err != nil {
return
}
2019-02-10 09:02:28 -05:00
common.Must2(conn.Write(b.Bytes()))
2018-11-21 16:02:19 -05:00
}
}()
})
2019-02-02 16:19:30 -05:00
common.Must(err)
2018-11-21 16:02:19 -05:00
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{},
})
2019-02-02 16:19:30 -05:00
common.Must(err)
2018-11-21 16:02:19 -05:00
defer conn.Close()
const N = 1024
b1 := make([]byte, N)
common.Must2(rand.Read(b1))
b2 := buf.New()
2019-02-10 09:02:28 -05:00
common.Must2(conn.Write(b1))
2018-11-21 16:02:19 -05:00
b2.Clear()
common.Must2(b2.ReadFullFrom(conn, N))
2019-02-10 09:02:28 -05:00
if r := cmp.Diff(b2.Bytes(), b1); r != "" {
t.Error(r)
}
2018-11-21 16:02:19 -05:00
2019-02-10 09:02:28 -05:00
common.Must2(conn.Write(b1))
2018-11-21 16:02:19 -05:00
b2.Clear()
common.Must2(b2.ReadFullFrom(conn, N))
2019-02-10 09:02:28 -05:00
if r := cmp.Diff(b2.Bytes(), b1); r != "" {
t.Error(r)
}
2018-11-21 16:02:19 -05:00
}
func TestQuicConnectionAuthHeader(t *testing.T) {
port := udp.PickPort()
listener, err := quic.Listen(context.Background(), net.LocalHostIP, port, &internet.MemoryStreamConfig{
ProtocolName: "quic",
ProtocolSettings: &quic.Config{
Header: serial.ToTypedMessage(&wireguard.WireguardConfig{}),
Key: "abcd",
Security: &protocol.SecurityConfig{
Type: protocol.SecurityType_AES128_GCM,
},
},
}, func(conn internet.Connection) {
go func() {
defer conn.Close()
b := buf.New()
defer b.Release()
for {
b.Clear()
if _, err := b.ReadFrom(conn); err != nil {
return
}
2019-02-10 09:02:28 -05:00
common.Must2(conn.Write(b.Bytes()))
2018-11-21 16:02:19 -05:00
}
}()
})
2019-02-02 16:19:30 -05:00
common.Must(err)
2018-11-21 16:02:19 -05:00
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{
Header: serial.ToTypedMessage(&wireguard.WireguardConfig{}),
Key: "abcd",
Security: &protocol.SecurityConfig{
Type: protocol.SecurityType_AES128_GCM,
},
},
})
2019-02-02 16:19:30 -05:00
common.Must(err)
2018-11-21 16:02:19 -05:00
defer conn.Close()
const N = 1024
b1 := make([]byte, N)
common.Must2(rand.Read(b1))
b2 := buf.New()
2019-02-10 09:02:28 -05:00
common.Must2(conn.Write(b1))
2018-11-21 16:02:19 -05:00
b2.Clear()
common.Must2(b2.ReadFullFrom(conn, N))
2019-02-10 09:02:28 -05:00
if r := cmp.Diff(b2.Bytes(), b1); r != "" {
t.Error(r)
}
2018-11-21 16:02:19 -05:00
2019-02-10 09:02:28 -05:00
common.Must2(conn.Write(b1))
2018-11-21 16:02:19 -05:00
b2.Clear()
common.Must2(b2.ReadFullFrom(conn, N))
2019-02-10 09:02:28 -05:00
if r := cmp.Diff(b2.Bytes(), b1); r != "" {
t.Error(r)
}
2018-11-21 16:02:19 -05:00
}