ws_test.go 2.9 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124
  1. package ws_test
  2. import (
  3. "testing"
  4. "time"
  5. "github.com/v2ray/v2ray-core/testing/assert"
  6. . "github.com/v2ray/v2ray-core/transport/internet/ws"
  7. v2net "github.com/v2ray/v2ray-core/common/net"
  8. )
  9. func Test_Connect_ws(t *testing.T) {
  10. assert := assert.On(t)
  11. (&Config{Pto: "ws", Path: ""}).Apply()
  12. conn, err := Dial(v2net.AnyIP, v2net.TCPDestination(v2net.DomainAddress("echo.websocket.org"), 80))
  13. assert.Error(err).IsNil()
  14. conn.Write([]byte("echo"))
  15. s := make(chan int)
  16. go func() {
  17. buf := make([]byte, 4)
  18. conn.Read(buf)
  19. s <- 0
  20. }()
  21. <-s
  22. conn.Close()
  23. }
  24. func Test_Connect_wss(t *testing.T) {
  25. assert := assert.On(t)
  26. (&Config{Pto: "wss", Path: ""}).Apply()
  27. conn, err := Dial(v2net.AnyIP, v2net.TCPDestination(v2net.DomainAddress("echo.websocket.org"), 443))
  28. assert.Error(err).IsNil()
  29. conn.Write([]byte("echo"))
  30. s := make(chan int)
  31. go func() {
  32. buf := make([]byte, 4)
  33. conn.Read(buf)
  34. s <- 0
  35. }()
  36. <-s
  37. conn.Close()
  38. }
  39. func Test_Connect_ws_guess(t *testing.T) {
  40. assert := assert.On(t)
  41. (&Config{Pto: "", Path: ""}).Apply()
  42. conn, err := Dial(v2net.AnyIP, v2net.TCPDestination(v2net.DomainAddress("echo.websocket.org"), 80))
  43. assert.Error(err).IsNil()
  44. conn.Write([]byte("echo"))
  45. s := make(chan int)
  46. go func() {
  47. buf := make([]byte, 4)
  48. conn.Read(buf)
  49. s <- 0
  50. }()
  51. <-s
  52. conn.Close()
  53. }
  54. func Test_Connect_wss_guess(t *testing.T) {
  55. assert := assert.On(t)
  56. (&Config{Pto: "", Path: ""}).Apply()
  57. conn, err := Dial(v2net.AnyIP, v2net.TCPDestination(v2net.DomainAddress("echo.websocket.org"), 443))
  58. assert.Error(err).IsNil()
  59. conn.Write([]byte("echo"))
  60. s := make(chan int)
  61. go func() {
  62. buf := make([]byte, 4)
  63. conn.Read(buf)
  64. s <- 0
  65. }()
  66. <-s
  67. conn.Close()
  68. }
  69. func Test_Connect_wss_guess_fail(t *testing.T) {
  70. assert := assert.On(t)
  71. (&Config{Pto: "", Path: ""}).Apply()
  72. _, err := Dial(v2net.AnyIP, v2net.TCPDestination(v2net.DomainAddress("static.kkdev.org"), 443))
  73. assert.Error(err).IsNotNil()
  74. }
  75. func Test_Connect_wss_guess_reuse(t *testing.T) {
  76. assert := assert.On(t)
  77. (&Config{Pto: "", Path: "", ConnectionReuse: true}).Apply()
  78. i := 3
  79. for i != 0 {
  80. conn, err := Dial(v2net.AnyIP, v2net.TCPDestination(v2net.DomainAddress("echo.websocket.org"), 443))
  81. assert.Error(err).IsNil()
  82. conn.Write([]byte("echo"))
  83. s := make(chan int)
  84. go func() {
  85. buf := make([]byte, 4)
  86. conn.Read(buf)
  87. s <- 0
  88. }()
  89. <-s
  90. if i == 0 {
  91. conn.SetDeadline(time.Now())
  92. conn.SetReadDeadline(time.Now())
  93. conn.SetWriteDeadline(time.Now())
  94. conn.SetReusable(false)
  95. }
  96. conn.Close()
  97. i--
  98. }
  99. }
  100. func Test_listenWSAndDial(t *testing.T) {
  101. assert := assert.On(t)
  102. (&Config{Pto: "ws", Path: ""}).Apply()
  103. listen, err := ListenWS(v2net.DomainAddress("localhost"), 13142)
  104. assert.Error(err).IsNil()
  105. go func() {
  106. conn, err := listen.Accept()
  107. assert.Error(err).IsNil()
  108. conn.Close()
  109. }()
  110. conn, err := Dial(v2net.AnyIP, v2net.TCPDestination(v2net.DomainAddress("localhost"), 13142))
  111. assert.Error(err).IsNil()
  112. conn.Close()
  113. }