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