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

131 lines
3.4 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"
2017-08-29 12:32:54 +00:00
"v2ray.com/core/common/net"
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"
2017-10-24 14:15:35 +00:00
. "v2ray.com/ext/assert"
2016-08-15 12:15:12 +00:00
)
2016-08-15 12:20:47 +00:00
func Test_listenWSAndDial(t *testing.T) {
2017-10-24 14:15:35 +00:00
assert := With(t)
2017-02-24 00:05:16 +00:00
listen, err := ListenWS(internet.ContextWithTransportSettings(context.Background(), &Config{
Path: "ws",
2018-02-08 15:39:18 +00:00
}), net.DomainAddress("localhost"), 13146, func(conn internet.Connection) {
2017-05-08 22:01:15 +00:00
go func(c internet.Connection) {
defer c.Close()
2017-01-04 14:34:11 +00:00
2017-05-08 22:01:15 +00:00
var b [1024]byte
n, err := c.Read(b[:])
2017-10-24 14:15:35 +00:00
//assert(err, IsNil)
2017-05-08 22:01:15 +00:00
if err != nil {
return
}
2017-10-24 14:15:35 +00:00
assert(bytes.HasPrefix(b[:n], []byte("Test connection")), IsTrue)
2017-01-04 14:34:11 +00:00
2017-05-08 22:01:15 +00:00
_, err = c.Write([]byte("Response"))
2017-10-24 14:15:35 +00:00
assert(err, IsNil)
2017-05-08 22:01:15 +00:00
}(conn)
})
2017-10-24 14:15:35 +00:00
assert(err, IsNil)
ctx := internet.ContextWithTransportSettings(context.Background(), &Config{Path: "ws"})
2017-08-29 12:32:54 +00:00
conn, err := Dial(ctx, net.TCPDestination(net.DomainAddress("localhost"), 13146))
2017-10-24 14:15:35 +00:00
assert(err, IsNil)
2017-01-04 14:34:11 +00:00
_, err = conn.Write([]byte("Test connection 1"))
2017-10-24 14:15:35 +00:00
assert(err, IsNil)
2017-01-04 14:34:11 +00:00
var b [1024]byte
n, err := conn.Read(b[:])
2017-10-24 14:15:35 +00:00
assert(err, IsNil)
assert(string(b[:n]), Equals, "Response")
2017-01-04 14:34:11 +00:00
2017-10-24 14:15:35 +00:00
assert(conn.Close(), IsNil)
2016-08-15 13:29:15 +00:00
<-time.After(time.Second * 5)
2017-08-29 12:32:54 +00:00
conn, err = Dial(ctx, net.TCPDestination(net.DomainAddress("localhost"), 13146))
2017-10-24 14:15:35 +00:00
assert(err, IsNil)
2017-01-04 14:34:11 +00:00
_, err = conn.Write([]byte("Test connection 2"))
2017-10-24 14:15:35 +00:00
assert(err, IsNil)
2017-01-04 14:34:11 +00:00
n, err = conn.Read(b[:])
2017-10-24 14:15:35 +00:00
assert(err, IsNil)
assert(string(b[:n]), Equals, "Response")
assert(conn.Close(), IsNil)
assert(listen.Close(), IsNil)
}
func TestDialWithRemoteAddr(t *testing.T) {
assert := With(t)
listen, err := ListenWS(internet.ContextWithTransportSettings(context.Background(), &Config{
Path: "ws",
2018-02-08 15:39:18 +00:00
}), net.DomainAddress("localhost"), 13148, func(conn internet.Connection) {
go func(c internet.Connection) {
defer c.Close()
assert(c.RemoteAddr().String(), HasPrefix, "1.1.1.1")
var b [1024]byte
n, err := c.Read(b[:])
//assert(err, IsNil)
if err != nil {
return
}
assert(bytes.HasPrefix(b[:n], []byte("Test connection")), IsTrue)
_, err = c.Write([]byte("Response"))
assert(err, IsNil)
}(conn)
})
assert(err, IsNil)
ctx := internet.ContextWithTransportSettings(context.Background(), &Config{Path: "ws", Header: []*Header{{Key: "X-Forwarded-For", Value: "1.1.1.1"}}})
conn, err := Dial(ctx, net.TCPDestination(net.DomainAddress("localhost"), 13148))
2017-10-24 14:15:35 +00:00
assert(err, IsNil)
_, err = conn.Write([]byte("Test connection 1"))
2017-10-24 14:15:35 +00:00
assert(err, IsNil)
var b [1024]byte
n, err := conn.Read(b[:])
2017-10-24 14:15:35 +00:00
assert(err, IsNil)
assert(string(b[:n]), Equals, "Response")
2017-01-04 14:34:11 +00:00
2017-10-24 14:15:35 +00:00
assert(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) {
2017-10-24 14:15:35 +00:00
assert := With(t)
start := time.Now()
2016-09-30 14:53:40 +00:00
ctx := internet.ContextWithTransportSettings(context.Background(), &Config{
Path: "wss",
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()},
})
2018-02-08 15:39:18 +00:00
listen, err := ListenWS(ctx, net.DomainAddress("localhost"), 13143, func(conn internet.Connection) {
2017-05-08 22:01:15 +00:00
go func() {
2017-10-22 14:04:39 +00:00
_ = conn.Close()
2017-05-08 22:01:15 +00:00
}()
})
2017-10-24 14:15:35 +00:00
assert(err, IsNil)
2017-05-08 22:01:15 +00:00
defer listen.Close()
2017-02-24 00:05:16 +00:00
2017-08-29 12:32:54 +00:00
conn, err := Dial(ctx, net.TCPDestination(net.DomainAddress("localhost"), 13143))
2017-10-24 14:15:35 +00:00
assert(err, IsNil)
2017-10-22 14:04:39 +00:00
_ = conn.Close()
2017-10-24 14:15:35 +00:00
end := time.Now()
assert(end.Before(start.Add(time.Second*5)), IsTrue)
2016-08-15 13:19:53 +00:00
}