From 40ec84e22264af6bc8ef0b7584f9f426884f3944 Mon Sep 17 00:00:00 2001 From: Shelikhoo Date: Mon, 15 Aug 2016 20:15:12 +0800 Subject: [PATCH] Dial test --- transport/internet/ws/ws_test.go | 109 +++++++++++++++++++++++++++++++ 1 file changed, 109 insertions(+) create mode 100644 transport/internet/ws/ws_test.go diff --git a/transport/internet/ws/ws_test.go b/transport/internet/ws/ws_test.go new file mode 100644 index 000000000..f72c0f996 --- /dev/null +++ b/transport/internet/ws/ws_test.go @@ -0,0 +1,109 @@ +package ws_test + +import ( + "testing" + "time" + + "github.com/v2ray/v2ray-core/testing/assert" + + . "github.com/v2ray/v2ray-core/transport/internet/ws" + + v2net "github.com/v2ray/v2ray-core/common/net" +) + +func Test_Connect_ws(t *testing.T) { + assert := assert.On(t) + (&Config{Pto: "ws", Path: ""}).Apply() + conn, err := Dial(v2net.AnyIP, v2net.TCPDestination(v2net.DomainAddress("echo.websocket.org"), 80)) + assert.Error(err).IsNil() + conn.Write([]byte("echo")) + s := make(chan int) + go func() { + buf := make([]byte, 4) + conn.Read(buf) + s <- 0 + }() + <-s + conn.Close() +} + +func Test_Connect_wss(t *testing.T) { + assert := assert.On(t) + (&Config{Pto: "wss", Path: ""}).Apply() + conn, err := Dial(v2net.AnyIP, v2net.TCPDestination(v2net.DomainAddress("echo.websocket.org"), 443)) + assert.Error(err).IsNil() + conn.Write([]byte("echo")) + s := make(chan int) + go func() { + buf := make([]byte, 4) + conn.Read(buf) + s <- 0 + }() + <-s + conn.Close() +} + +func Test_Connect_ws_guess(t *testing.T) { + assert := assert.On(t) + (&Config{Pto: "", Path: ""}).Apply() + conn, err := Dial(v2net.AnyIP, v2net.TCPDestination(v2net.DomainAddress("echo.websocket.org"), 80)) + assert.Error(err).IsNil() + conn.Write([]byte("echo")) + s := make(chan int) + go func() { + buf := make([]byte, 4) + conn.Read(buf) + s <- 0 + }() + <-s + conn.Close() +} + +func Test_Connect_wss_guess(t *testing.T) { + assert := assert.On(t) + (&Config{Pto: "", Path: ""}).Apply() + conn, err := Dial(v2net.AnyIP, v2net.TCPDestination(v2net.DomainAddress("echo.websocket.org"), 443)) + assert.Error(err).IsNil() + conn.Write([]byte("echo")) + s := make(chan int) + go func() { + buf := make([]byte, 4) + conn.Read(buf) + s <- 0 + }() + <-s + conn.Close() +} + +func Test_Connect_wss_guess_fail(t *testing.T) { + assert := assert.On(t) + (&Config{Pto: "", Path: ""}).Apply() + _, err := Dial(v2net.AnyIP, v2net.TCPDestination(v2net.DomainAddress("static.kkdev.org"), 443)) + assert.Error(err).IsNotNil() +} + +func Test_Connect_wss_guess_reuse(t *testing.T) { + assert := assert.On(t) + (&Config{Pto: "", Path: "", ConnectionReuse: true}).Apply() + i := 3 + for i != 0 { + conn, err := Dial(v2net.AnyIP, v2net.TCPDestination(v2net.DomainAddress("echo.websocket.org"), 443)) + assert.Error(err).IsNil() + conn.Write([]byte("echo")) + s := make(chan int) + go func() { + buf := make([]byte, 4) + conn.Read(buf) + s <- 0 + }() + <-s + if i == 0 { + conn.SetDeadline(time.Now()) + conn.SetReadDeadline(time.Now()) + conn.SetWriteDeadline(time.Now()) + conn.SetReusable(false) + } + conn.Close() + i-- + } +}