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

112 lines
2.9 KiB
Go
Raw Normal View History

2016-12-22 23:30:46 +00:00
package websocket_test
2016-08-15 12:15:12 +00:00
import (
2017-01-04 14:34:11 +00:00
"bytes"
"context"
2017-02-24 00:05:16 +00:00
"testing"
"time"
2016-08-20 18:55:45 +00:00
v2net "v2ray.com/core/common/net"
"v2ray.com/core/testing/assert"
2017-01-12 15:10:03 +00:00
tlsgen "v2ray.com/core/testing/tls"
2016-09-30 14:53:40 +00:00
"v2ray.com/core/transport/internet"
2016-10-02 21:43:58 +00:00
v2tls "v2ray.com/core/transport/internet/tls"
2016-12-22 23:30:46 +00:00
. "v2ray.com/core/transport/internet/websocket"
2016-08-15 12:15:12 +00:00
)
2016-08-15 12:20:47 +00:00
func Test_listenWSAndDial(t *testing.T) {
assert := assert.On(t)
2017-02-24 00:05:16 +00:00
listen, err := ListenWS(internet.ContextWithTransportSettings(context.Background(), &Config{
Path: "ws",
}), v2net.DomainAddress("localhost"), 13146)
2016-08-15 12:20:47 +00:00
assert.Error(err).IsNil()
go func() {
2017-01-04 14:34:11 +00:00
for {
conn, err := listen.Accept()
if err != nil {
break
}
go func() {
defer conn.Close()
var b [1024]byte
n, err := conn.Read(b[:])
//assert.Error(err).IsNil()
if err != nil {
conn.SetReusable(false)
return
}
assert.Bool(bytes.HasPrefix(b[:n], []byte("Test connection"))).IsTrue()
_, err = conn.Write([]byte("Response"))
assert.Error(err).IsNil()
}()
}
2016-08-15 12:20:47 +00:00
}()
ctx := internet.ContextWithTransportSettings(context.Background(), &Config{Path: "ws"})
conn, err := Dial(ctx, v2net.TCPDestination(v2net.DomainAddress("localhost"), 13146))
2016-08-15 12:20:47 +00:00
assert.Error(err).IsNil()
2017-01-04 14:34:11 +00:00
_, err = conn.Write([]byte("Test connection 1"))
assert.Error(err).IsNil()
var b [1024]byte
n, err := conn.Read(b[:])
assert.Error(err).IsNil()
assert.String(string(b[:n])).Equals("Response")
assert.Error(conn.Close()).IsNil()
2016-08-15 13:29:15 +00:00
<-time.After(time.Second * 5)
conn, err = Dial(ctx, v2net.TCPDestination(v2net.DomainAddress("localhost"), 13146))
2016-08-15 13:29:15 +00:00
assert.Error(err).IsNil()
2017-01-04 14:34:11 +00:00
_, err = conn.Write([]byte("Test connection 2"))
assert.Error(err).IsNil()
n, err = conn.Read(b[:])
assert.Error(err).IsNil()
assert.String(string(b[:n])).Equals("Response")
assert.Error(conn.Close()).IsNil()
2016-08-15 13:29:15 +00:00
<-time.After(time.Second * 15)
conn, err = Dial(ctx, v2net.TCPDestination(v2net.DomainAddress("localhost"), 13146))
2016-08-15 13:29:15 +00:00
assert.Error(err).IsNil()
2017-01-04 14:34:11 +00:00
_, err = conn.Write([]byte("Test connection 3"))
assert.Error(err).IsNil()
n, err = conn.Read(b[:])
assert.Error(err).IsNil()
assert.String(string(b[:n])).Equals("Response")
assert.Error(conn.Close()).IsNil()
assert.Error(listen.Close()).IsNil()
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
ctx := internet.ContextWithTransportSettings(context.Background(), &Config{
Path: "wss",
ConnectionReuse: &ConnectionReuse{
Enable: true,
2016-09-30 14:53:40 +00:00
},
})
ctx = internet.ContextWithSecuritySettings(ctx, &v2tls.Config{
AllowInsecure: true,
2017-02-24 00:05:16 +00:00
Certificate: []*v2tls.Certificate{tlsgen.GenerateCertificateForTest()},
})
2017-02-24 00:05:16 +00:00
listen, err := ListenWS(ctx, v2net.DomainAddress("localhost"), 13143)
assert.Error(err).IsNil()
go func() {
conn, err := listen.Accept()
assert.Error(err).IsNil()
conn.Close()
listen.Close()
}()
conn, err := Dial(ctx, v2net.TCPDestination(v2net.DomainAddress("localhost"), 13143))
2016-08-15 13:19:53 +00:00
assert.Error(err).IsNil()
conn.Close()
}