| 123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242 |
- package ws_test
- import (
- "crypto/tls"
- "testing"
- "time"
- v2net "v2ray.com/core/common/net"
- "v2ray.com/core/testing/assert"
- "v2ray.com/core/transport/internet"
- . "v2ray.com/core/transport/internet/ws"
- )
- func Test_Connect_ws(t *testing.T) {
- assert := assert.On(t)
- (&Config{Path: ""}).Apply()
- conn, err := Dial(v2net.AnyIP, v2net.TCPDestination(v2net.DomainAddress("echo.websocket.org"), 80), internet.DialerOptions{})
- assert.Error(err).IsNil()
- conn.Write([]byte("echo"))
- s := make(chan int)
- go func() {
- buf := make([]byte, 4)
- conn.Read(buf)
- str := string(buf)
- if str != "echo" {
- assert.Fail("Data mismatch")
- }
- s <- 0
- }()
- <-s
- conn.Close()
- }
- func Test_Connect_wss(t *testing.T) {
- assert := assert.On(t)
- (&Config{Path: ""}).Apply()
- conn, err := Dial(v2net.AnyIP, v2net.TCPDestination(v2net.DomainAddress("echo.websocket.org"), 443), internet.DialerOptions{
- Stream: &internet.StreamSettings{
- Security: internet.StreamSecurityTypeTLS,
- },
- })
- assert.Error(err).IsNil()
- conn.Write([]byte("echo"))
- s := make(chan int)
- go func() {
- buf := make([]byte, 4)
- conn.Read(buf)
- str := string(buf)
- if str != "echo" {
- assert.Fail("Data mismatch")
- }
- s <- 0
- }()
- <-s
- conn.Close()
- }
- func Test_Connect_wss_1_nil(t *testing.T) {
- assert := assert.On(t)
- (&Config{Path: ""}).Apply()
- conn, err := Dial(nil, v2net.TCPDestination(v2net.DomainAddress("echo.websocket.org"), 443), internet.DialerOptions{
- Stream: &internet.StreamSettings{
- Security: internet.StreamSecurityTypeTLS,
- },
- })
- assert.Error(err).IsNil()
- conn.Write([]byte("echo"))
- s := make(chan int)
- go func() {
- buf := make([]byte, 4)
- conn.Read(buf)
- str := string(buf)
- if str != "echo" {
- assert.Fail("Data mismatch")
- }
- s <- 0
- }()
- <-s
- conn.Close()
- }
- func Test_Connect_ws_guess(t *testing.T) {
- assert := assert.On(t)
- (&Config{Path: ""}).Apply()
- conn, err := Dial(v2net.AnyIP, v2net.TCPDestination(v2net.DomainAddress("echo.websocket.org"), 80), internet.DialerOptions{})
- assert.Error(err).IsNil()
- conn.Write([]byte("echo"))
- s := make(chan int)
- go func() {
- buf := make([]byte, 4)
- conn.Read(buf)
- str := string(buf)
- if str != "echo" {
- assert.Fail("Data mismatch")
- }
- s <- 0
- }()
- <-s
- conn.Close()
- }
- func Test_Connect_wss_guess(t *testing.T) {
- assert := assert.On(t)
- (&Config{Path: ""}).Apply()
- conn, err := Dial(v2net.AnyIP, v2net.TCPDestination(v2net.DomainAddress("echo.websocket.org"), 443), internet.DialerOptions{
- Stream: &internet.StreamSettings{
- Security: internet.StreamSecurityTypeTLS,
- },
- })
- assert.Error(err).IsNil()
- conn.Write([]byte("echo"))
- s := make(chan int)
- go func() {
- buf := make([]byte, 4)
- conn.Read(buf)
- str := string(buf)
- if str != "echo" {
- assert.Fail("Data mismatch")
- }
- s <- 0
- }()
- <-s
- conn.Close()
- }
- func Test_Connect_wss_guess_fail(t *testing.T) {
- assert := assert.On(t)
- (&Config{Path: ""}).Apply()
- _, err := Dial(v2net.AnyIP, v2net.TCPDestination(v2net.DomainAddress("static.kkdev.org"), 443), internet.DialerOptions{
- Stream: &internet.StreamSettings{
- Security: internet.StreamSecurityTypeTLS,
- },
- })
- assert.Error(err).IsNotNil()
- }
- func Test_Connect_wss_guess_reuse(t *testing.T) {
- assert := assert.On(t)
- (&Config{Path: "", ConnectionReuse: true}).Apply()
- i := 3
- for i != 0 {
- conn, err := Dial(v2net.AnyIP, v2net.TCPDestination(v2net.DomainAddress("echo.websocket.org"), 443), internet.DialerOptions{
- Stream: &internet.StreamSettings{
- Security: internet.StreamSecurityTypeTLS,
- },
- })
- assert.Error(err).IsNil()
- conn.Write([]byte("echo"))
- s := make(chan int)
- go func() {
- buf := make([]byte, 4)
- conn.Read(buf)
- str := string(buf)
- if str != "echo" {
- assert.Fail("Data mismatch")
- }
- s <- 0
- }()
- <-s
- if i == 0 {
- conn.SetDeadline(time.Now())
- conn.SetReadDeadline(time.Now())
- conn.SetWriteDeadline(time.Now())
- conn.SetReusable(false)
- }
- conn.Close()
- i--
- }
- }
- func Test_listenWSAndDial(t *testing.T) {
- assert := assert.On(t)
- (&Config{Path: "ws"}).Apply()
- listen, err := ListenWS(v2net.DomainAddress("localhost"), 13142, internet.ListenOptions{})
- assert.Error(err).IsNil()
- go func() {
- conn, err := listen.Accept()
- assert.Error(err).IsNil()
- conn.Close()
- conn, err = listen.Accept()
- assert.Error(err).IsNil()
- conn.Close()
- conn, err = listen.Accept()
- assert.Error(err).IsNil()
- conn.Close()
- listen.Close()
- }()
- conn, err := Dial(v2net.AnyIP, v2net.TCPDestination(v2net.DomainAddress("localhost"), 13142), internet.DialerOptions{})
- assert.Error(err).IsNil()
- conn.Close()
- <-time.After(time.Second * 5)
- conn, err = Dial(v2net.AnyIP, v2net.TCPDestination(v2net.DomainAddress("localhost"), 13142), internet.DialerOptions{})
- assert.Error(err).IsNil()
- conn.Close()
- <-time.After(time.Second * 15)
- conn, err = Dial(v2net.AnyIP, v2net.TCPDestination(v2net.DomainAddress("localhost"), 13142), internet.DialerOptions{})
- assert.Error(err).IsNil()
- conn.Close()
- }
- func Test_listenWSAndDial_TLS(t *testing.T) {
- assert := assert.On(t)
- go func() {
- <-time.After(time.Second * 5)
- assert.Fail("Too slow")
- }()
- (&Config{Path: "wss", ConnectionReuse: true}).Apply()
- listen, err := ListenWS(v2net.DomainAddress("localhost"), 13143, internet.ListenOptions{
- Stream: &internet.StreamSettings{
- Security: internet.StreamSecurityTypeTLS,
- TLSSettings: &internet.TLSSettings{
- AllowInsecure: true,
- Certs: LoadTestCert(assert),
- },
- },
- })
- assert.Error(err).IsNil()
- go func() {
- conn, err := listen.Accept()
- assert.Error(err).IsNil()
- conn.Close()
- listen.Close()
- }()
- conn, err := Dial(v2net.AnyIP, v2net.TCPDestination(v2net.DomainAddress("localhost"), 13143), internet.DialerOptions{
- Stream: &internet.StreamSettings{
- Security: internet.StreamSecurityTypeTLS,
- TLSSettings: &internet.TLSSettings{
- AllowInsecure: true,
- Certs: LoadTestCert(assert),
- },
- },
- })
- assert.Error(err).IsNil()
- conn.Close()
- }
- func LoadTestCert(assert *assert.Assert) []tls.Certificate {
- cert, err := tls.LoadX509KeyPair("./../../../testing/tls/cert.pem", "./../../../testing/tls/key.pem")
- assert.Error(err).IsNil()
- return []tls.Certificate{cert}
- }
|