1
0
mirror of https://github.com/v2fly/v2ray-core.git synced 2024-11-09 03:37:37 -05:00
v2fly/transport/internet/ws/ws_test.go

161 lines
4.1 KiB
Go
Raw Normal View History

2016-08-15 08:15:12 -04:00
package ws_test
import (
2016-10-02 17:43:58 -04:00
"io/ioutil"
2016-08-15 08:15:12 -04:00
"testing"
"time"
2016-10-17 08:35:13 -04:00
"v2ray.com/core/common/loader"
2016-08-20 14:55:45 -04:00
v2net "v2ray.com/core/common/net"
"v2ray.com/core/testing/assert"
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-08-20 14:55:45 -04:00
. "v2ray.com/core/transport/internet/ws"
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)
2016-10-17 08:35:13 -04:00
listen, err := ListenWS(v2net.DomainAddress("localhost"), 13146, internet.ListenOptions{
2016-10-02 17:43:58 -04:00
Stream: &internet.StreamConfig{
Network: v2net.Network_WebSocket,
NetworkSettings: []*internet.NetworkSettings{
{
Network: v2net.Network_WebSocket,
2016-10-17 08:35:13 -04:00
Settings: loader.NewTypedSettings(&Config{
2016-10-02 17:43:58 -04:00
Path: "ws",
2016-10-17 08:35:13 -04:00
}),
2016-10-02 17:43:58 -04:00
},
},
},
})
2016-08-15 08:20:47 -04:00
assert.Error(err).IsNil()
go func() {
conn, err := listen.Accept()
assert.Error(err).IsNil()
conn.Close()
2016-08-15 09:29:15 -04:00
conn, err = listen.Accept()
assert.Error(err).IsNil()
conn.Close()
conn, err = listen.Accept()
assert.Error(err).IsNil()
conn.Close()
2016-08-15 09:19:53 -04:00
listen.Close()
2016-08-15 08:20:47 -04:00
}()
2016-10-17 08:35:13 -04:00
conn, err := Dial(v2net.AnyIP, v2net.TCPDestination(v2net.DomainAddress("localhost"), 13146), internet.DialerOptions{
2016-10-02 17:43:58 -04:00
Stream: &internet.StreamConfig{
Network: v2net.Network_WebSocket,
NetworkSettings: []*internet.NetworkSettings{
{
Network: v2net.Network_WebSocket,
2016-10-17 08:35:13 -04:00
Settings: loader.NewTypedSettings(&Config{
2016-10-02 17:43:58 -04:00
Path: "ws",
2016-10-17 08:35:13 -04:00
}),
2016-10-02 17:43:58 -04:00
},
},
},
})
2016-08-15 08:20:47 -04:00
assert.Error(err).IsNil()
conn.Close()
2016-08-15 09:29:15 -04:00
<-time.After(time.Second * 5)
2016-10-17 08:35:13 -04:00
conn, err = Dial(v2net.AnyIP, v2net.TCPDestination(v2net.DomainAddress("localhost"), 13146), internet.DialerOptions{
2016-10-02 17:43:58 -04:00
Stream: &internet.StreamConfig{
Network: v2net.Network_WebSocket,
NetworkSettings: []*internet.NetworkSettings{
{
Network: v2net.Network_WebSocket,
2016-10-17 08:35:13 -04:00
Settings: loader.NewTypedSettings(&Config{
2016-10-02 17:43:58 -04:00
Path: "ws",
2016-10-17 08:35:13 -04:00
}),
2016-10-02 17:43:58 -04:00
},
},
},
})
2016-08-15 09:29:15 -04:00
assert.Error(err).IsNil()
conn.Close()
<-time.After(time.Second * 15)
2016-10-17 08:35:13 -04:00
conn, err = Dial(v2net.AnyIP, v2net.TCPDestination(v2net.DomainAddress("localhost"), 13146), internet.DialerOptions{
2016-10-02 17:43:58 -04:00
Stream: &internet.StreamConfig{
Network: v2net.Network_WebSocket,
NetworkSettings: []*internet.NetworkSettings{
{
Network: v2net.Network_WebSocket,
2016-10-17 08:35:13 -04:00
Settings: loader.NewTypedSettings(&Config{
2016-10-02 17:43:58 -04:00
Path: "ws",
2016-10-17 08:35:13 -04:00
}),
2016-10-02 17:43:58 -04:00
},
},
},
})
2016-08-15 09:29:15 -04:00
assert.Error(err).IsNil()
conn.Close()
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-10-02 17:43:58 -04:00
tlsSettings := &v2tls.Config{
AllowInsecure: true,
Certificate: []*v2tls.Certificate{
{
Certificate: ReadFile("./../../../testing/tls/cert.pem", assert),
Key: ReadFile("./../../../testing/tls/key.pem", assert),
},
},
}
2016-09-30 10:53:40 -04:00
listen, err := ListenWS(v2net.DomainAddress("localhost"), 13143, internet.ListenOptions{
2016-10-02 17:43:58 -04:00
Stream: &internet.StreamConfig{
2016-10-17 08:35:13 -04:00
SecurityType: loader.GetType(new(v2tls.Config)),
SecuritySettings: []*loader.TypedSettings{loader.NewTypedSettings(tlsSettings)},
Network: v2net.Network_WebSocket,
2016-10-02 17:43:58 -04:00
NetworkSettings: []*internet.NetworkSettings{
{
Network: v2net.Network_WebSocket,
2016-10-17 08:35:13 -04:00
Settings: loader.NewTypedSettings(&Config{
Path: "wss",
ConnectionReuse: &ConnectionReuse{
Enable: true,
},
}),
2016-10-02 17:43:58 -04:00
},
2016-09-30 10:53:40 -04:00
},
},
})
2016-08-15 09:19:53 -04:00
assert.Error(err).IsNil()
go func() {
conn, err := listen.Accept()
assert.Error(err).IsNil()
conn.Close()
listen.Close()
}()
2016-09-30 10:53:40 -04:00
conn, err := Dial(v2net.AnyIP, v2net.TCPDestination(v2net.DomainAddress("localhost"), 13143), internet.DialerOptions{
2016-10-02 17:43:58 -04:00
Stream: &internet.StreamConfig{
2016-10-17 08:35:13 -04:00
SecurityType: loader.GetType(new(v2tls.Config)),
SecuritySettings: []*loader.TypedSettings{loader.NewTypedSettings(tlsSettings)},
Network: v2net.Network_WebSocket,
2016-10-02 17:43:58 -04:00
NetworkSettings: []*internet.NetworkSettings{
{
Network: v2net.Network_WebSocket,
2016-10-17 08:35:13 -04:00
Settings: loader.NewTypedSettings(&Config{
Path: "wss",
ConnectionReuse: &ConnectionReuse{
Enable: true,
},
}),
2016-10-02 17:43:58 -04:00
},
2016-09-30 10:53:40 -04:00
},
},
})
2016-08-15 09:19:53 -04:00
assert.Error(err).IsNil()
conn.Close()
}
2016-09-30 10:53:40 -04:00
2016-10-02 17:43:58 -04:00
func ReadFile(file string, assert *assert.Assert) []byte {
b, err := ioutil.ReadFile(file)
2016-09-30 10:53:40 -04:00
assert.Error(err).IsNil()
2016-10-02 17:43:58 -04:00
return b
2016-09-30 10:53:40 -04:00
}