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

161 lines
4.1 KiB
Go
Raw Normal View History

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