v2ray-core/transport/internet/ws/ws_test.go

243 lines
6.0 KiB
Go
Raw Normal View History

2016-08-15 12:15:12 +00:00
package ws_test
import (
2016-09-30 14:53:40 +00:00
"crypto/tls"
2016-08-15 12:15:12 +00:00
"testing"
"time"
2016-08-20 18:55:45 +00:00
v2net "v2ray.com/core/common/net"
"v2ray.com/core/testing/assert"
2016-09-30 14:53:40 +00:00
"v2ray.com/core/transport/internet"
2016-08-20 18:55:45 +00:00
. "v2ray.com/core/transport/internet/ws"
2016-08-15 12:15:12 +00:00
)
func Test_Connect_ws(t *testing.T) {
assert := assert.On(t)
2016-09-30 14:53:40 +00:00
(&Config{Path: ""}).Apply()
conn, err := Dial(v2net.AnyIP, v2net.TCPDestination(v2net.DomainAddress("echo.websocket.org"), 80), internet.DialerOptions{})
2016-08-15 12:15:12 +00:00
assert.Error(err).IsNil()
conn.Write([]byte("echo"))
s := make(chan int)
go func() {
buf := make([]byte, 4)
conn.Read(buf)
2016-08-15 13:55:43 +00:00
str := string(buf)
if str != "echo" {
assert.Fail("Data mismatch")
}
2016-08-15 12:15:12 +00:00
s <- 0
}()
<-s
conn.Close()
}
func Test_Connect_wss(t *testing.T) {
assert := assert.On(t)
2016-09-30 14:53:40 +00:00
(&Config{Path: ""}).Apply()
conn, err := Dial(v2net.AnyIP, v2net.TCPDestination(v2net.DomainAddress("echo.websocket.org"), 443), internet.DialerOptions{
Stream: &internet.StreamSettings{
Security: internet.StreamSecurityTypeTLS,
},
})
2016-08-15 12:15:12 +00:00
assert.Error(err).IsNil()
conn.Write([]byte("echo"))
s := make(chan int)
go func() {
buf := make([]byte, 4)
conn.Read(buf)
2016-08-15 13:55:43 +00:00
str := string(buf)
if str != "echo" {
assert.Fail("Data mismatch")
}
2016-08-15 12:15:12 +00:00
s <- 0
}()
<-s
conn.Close()
}
2016-08-15 13:29:15 +00:00
func Test_Connect_wss_1_nil(t *testing.T) {
assert := assert.On(t)
2016-09-30 14:53:40 +00:00
(&Config{Path: ""}).Apply()
conn, err := Dial(nil, v2net.TCPDestination(v2net.DomainAddress("echo.websocket.org"), 443), internet.DialerOptions{
Stream: &internet.StreamSettings{
Security: internet.StreamSecurityTypeTLS,
},
})
2016-08-15 13:29:15 +00:00
assert.Error(err).IsNil()
conn.Write([]byte("echo"))
s := make(chan int)
go func() {
buf := make([]byte, 4)
conn.Read(buf)
2016-08-15 13:55:43 +00:00
str := string(buf)
if str != "echo" {
assert.Fail("Data mismatch")
}
2016-08-15 13:29:15 +00:00
s <- 0
}()
<-s
conn.Close()
}
2016-08-15 12:15:12 +00:00
func Test_Connect_ws_guess(t *testing.T) {
assert := assert.On(t)
2016-09-30 14:53:40 +00:00
(&Config{Path: ""}).Apply()
conn, err := Dial(v2net.AnyIP, v2net.TCPDestination(v2net.DomainAddress("echo.websocket.org"), 80), internet.DialerOptions{})
2016-08-15 12:15:12 +00:00
assert.Error(err).IsNil()
conn.Write([]byte("echo"))
s := make(chan int)
go func() {
buf := make([]byte, 4)
conn.Read(buf)
2016-08-15 13:55:43 +00:00
str := string(buf)
if str != "echo" {
assert.Fail("Data mismatch")
}
2016-08-15 12:15:12 +00:00
s <- 0
}()
<-s
conn.Close()
}
func Test_Connect_wss_guess(t *testing.T) {
assert := assert.On(t)
2016-09-30 14:53:40 +00:00
(&Config{Path: ""}).Apply()
conn, err := Dial(v2net.AnyIP, v2net.TCPDestination(v2net.DomainAddress("echo.websocket.org"), 443), internet.DialerOptions{
Stream: &internet.StreamSettings{
Security: internet.StreamSecurityTypeTLS,
},
})
2016-08-15 12:15:12 +00:00
assert.Error(err).IsNil()
conn.Write([]byte("echo"))
s := make(chan int)
go func() {
buf := make([]byte, 4)
conn.Read(buf)
2016-08-15 13:55:43 +00:00
str := string(buf)
if str != "echo" {
assert.Fail("Data mismatch")
}
2016-08-15 12:15:12 +00:00
s <- 0
}()
<-s
conn.Close()
}
func Test_Connect_wss_guess_fail(t *testing.T) {
assert := assert.On(t)
2016-09-30 14:53:40 +00:00
(&Config{Path: ""}).Apply()
_, err := Dial(v2net.AnyIP, v2net.TCPDestination(v2net.DomainAddress("static.kkdev.org"), 443), internet.DialerOptions{
Stream: &internet.StreamSettings{
Security: internet.StreamSecurityTypeTLS,
},
})
2016-08-15 13:29:15 +00:00
assert.Error(err).IsNotNil()
}
2016-08-15 12:15:12 +00:00
func Test_Connect_wss_guess_reuse(t *testing.T) {
assert := assert.On(t)
2016-09-30 14:53:40 +00:00
(&Config{Path: "", ConnectionReuse: true}).Apply()
2016-08-15 12:15:12 +00:00
i := 3
for i != 0 {
2016-09-30 14:53:40 +00:00
conn, err := Dial(v2net.AnyIP, v2net.TCPDestination(v2net.DomainAddress("echo.websocket.org"), 443), internet.DialerOptions{
Stream: &internet.StreamSettings{
Security: internet.StreamSecurityTypeTLS,
},
})
2016-08-15 12:15:12 +00:00
assert.Error(err).IsNil()
conn.Write([]byte("echo"))
s := make(chan int)
go func() {
buf := make([]byte, 4)
conn.Read(buf)
2016-08-15 13:55:43 +00:00
str := string(buf)
if str != "echo" {
assert.Fail("Data mismatch")
}
2016-08-15 12:15:12 +00:00
s <- 0
}()
<-s
if i == 0 {
conn.SetDeadline(time.Now())
conn.SetReadDeadline(time.Now())
conn.SetWriteDeadline(time.Now())
conn.SetReusable(false)
}
conn.Close()
i--
}
}
2016-08-15 12:20:47 +00:00
func Test_listenWSAndDial(t *testing.T) {
assert := assert.On(t)
2016-09-30 14:53:40 +00:00
(&Config{Path: "ws"}).Apply()
listen, err := ListenWS(v2net.DomainAddress("localhost"), 13142, internet.ListenOptions{})
2016-08-15 12:20:47 +00:00
assert.Error(err).IsNil()
go func() {
conn, err := listen.Accept()
assert.Error(err).IsNil()
conn.Close()
2016-08-15 13:29:15 +00:00
conn, err = listen.Accept()
assert.Error(err).IsNil()
conn.Close()
conn, err = listen.Accept()
assert.Error(err).IsNil()
conn.Close()
2016-08-15 13:19:53 +00:00
listen.Close()
2016-08-15 12:20:47 +00:00
}()
2016-09-30 14:53:40 +00:00
conn, err := Dial(v2net.AnyIP, v2net.TCPDestination(v2net.DomainAddress("localhost"), 13142), internet.DialerOptions{})
2016-08-15 12:20:47 +00:00
assert.Error(err).IsNil()
conn.Close()
2016-08-15 13:29:15 +00:00
<-time.After(time.Second * 5)
2016-09-30 14:53:40 +00:00
conn, err = Dial(v2net.AnyIP, v2net.TCPDestination(v2net.DomainAddress("localhost"), 13142), internet.DialerOptions{})
2016-08-15 13:29:15 +00:00
assert.Error(err).IsNil()
conn.Close()
<-time.After(time.Second * 15)
2016-09-30 14:53:40 +00:00
conn, err = Dial(v2net.AnyIP, v2net.TCPDestination(v2net.DomainAddress("localhost"), 13142), internet.DialerOptions{})
2016-08-15 13:29:15 +00:00
assert.Error(err).IsNil()
conn.Close()
2016-08-15 12:20:47 +00:00
}
2016-08-15 13:19:53 +00:00
func Test_listenWSAndDial_TLS(t *testing.T) {
assert := assert.On(t)
go func() {
<-time.After(time.Second * 5)
assert.Fail("Too slow")
}()
2016-09-30 14:53:40 +00:00
(&Config{Path: "wss", ConnectionReuse: true}).Apply()
listen, err := ListenWS(v2net.DomainAddress("localhost"), 13143, internet.ListenOptions{
Stream: &internet.StreamSettings{
Security: internet.StreamSecurityTypeTLS,
TLSSettings: &internet.TLSSettings{
AllowInsecure: true,
Certs: LoadTestCert(assert),
},
},
})
2016-08-15 13:19:53 +00:00
assert.Error(err).IsNil()
go func() {
conn, err := listen.Accept()
assert.Error(err).IsNil()
conn.Close()
listen.Close()
}()
2016-09-30 14:53:40 +00:00
conn, err := Dial(v2net.AnyIP, v2net.TCPDestination(v2net.DomainAddress("localhost"), 13143), internet.DialerOptions{
Stream: &internet.StreamSettings{
Security: internet.StreamSecurityTypeTLS,
TLSSettings: &internet.TLSSettings{
AllowInsecure: true,
Certs: LoadTestCert(assert),
},
},
})
2016-08-15 13:19:53 +00:00
assert.Error(err).IsNil()
conn.Close()
}
2016-09-30 14:53:40 +00:00
func LoadTestCert(assert *assert.Assert) []tls.Certificate {
cert, err := tls.LoadX509KeyPair("./../../../testing/tls/cert.pem", "./../../../testing/tls/key.pem")
assert.Error(err).IsNil()
return []tls.Certificate{cert}
}