ws_test.go 4.6 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182
  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_wss_1_nil(t *testing.T) {
  40. assert := assert.On(t)
  41. (&Config{Pto: "wss", Path: ""}).Apply()
  42. conn, err := Dial(nil, v2net.TCPDestination(v2net.DomainAddress("echo.websocket.org"), 443))
  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_ws_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"), 80))
  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(t *testing.T) {
  70. assert := assert.On(t)
  71. (&Config{Pto: "", Path: ""}).Apply()
  72. conn, err := Dial(v2net.AnyIP, v2net.TCPDestination(v2net.DomainAddress("echo.websocket.org"), 443))
  73. assert.Error(err).IsNil()
  74. conn.Write([]byte("echo"))
  75. s := make(chan int)
  76. go func() {
  77. buf := make([]byte, 4)
  78. conn.Read(buf)
  79. s <- 0
  80. }()
  81. <-s
  82. conn.Close()
  83. }
  84. func Test_Connect_wss_guess_fail(t *testing.T) {
  85. assert := assert.On(t)
  86. (&Config{Pto: "", Path: ""}).Apply()
  87. _, err := Dial(v2net.AnyIP, v2net.TCPDestination(v2net.DomainAddress("static.kkdev.org"), 443))
  88. assert.Error(err).IsNotNil()
  89. }
  90. func Test_Connect_wss_guess_fail_port(t *testing.T) {
  91. assert := assert.On(t)
  92. (&Config{Pto: "", Path: ""}).Apply()
  93. _, err := Dial(v2net.AnyIP, v2net.TCPDestination(v2net.DomainAddress("static.kkdev.org"), 179))
  94. assert.Error(err).IsNotNil()
  95. }
  96. func Test_Connect_wss_guess_reuse(t *testing.T) {
  97. assert := assert.On(t)
  98. (&Config{Pto: "", Path: "", ConnectionReuse: true}).Apply()
  99. i := 3
  100. for i != 0 {
  101. conn, err := Dial(v2net.AnyIP, v2net.TCPDestination(v2net.DomainAddress("echo.websocket.org"), 443))
  102. assert.Error(err).IsNil()
  103. conn.Write([]byte("echo"))
  104. s := make(chan int)
  105. go func() {
  106. buf := make([]byte, 4)
  107. conn.Read(buf)
  108. s <- 0
  109. }()
  110. <-s
  111. if i == 0 {
  112. conn.SetDeadline(time.Now())
  113. conn.SetReadDeadline(time.Now())
  114. conn.SetWriteDeadline(time.Now())
  115. conn.SetReusable(false)
  116. }
  117. conn.Close()
  118. i--
  119. }
  120. }
  121. func Test_listenWSAndDial(t *testing.T) {
  122. assert := assert.On(t)
  123. (&Config{Pto: "ws", Path: "ws"}).Apply()
  124. listen, err := ListenWS(v2net.DomainAddress("localhost"), 13142)
  125. assert.Error(err).IsNil()
  126. go func() {
  127. conn, err := listen.Accept()
  128. assert.Error(err).IsNil()
  129. conn.Close()
  130. conn, err = listen.Accept()
  131. assert.Error(err).IsNil()
  132. conn.Close()
  133. conn, err = listen.Accept()
  134. assert.Error(err).IsNil()
  135. conn.Close()
  136. listen.Close()
  137. }()
  138. conn, err := Dial(v2net.AnyIP, v2net.TCPDestination(v2net.DomainAddress("localhost"), 13142))
  139. assert.Error(err).IsNil()
  140. conn.Close()
  141. <-time.After(time.Second * 5)
  142. conn, err = Dial(v2net.AnyIP, v2net.TCPDestination(v2net.DomainAddress("localhost"), 13142))
  143. assert.Error(err).IsNil()
  144. conn.Close()
  145. <-time.After(time.Second * 15)
  146. conn, err = Dial(v2net.AnyIP, v2net.TCPDestination(v2net.DomainAddress("localhost"), 13142))
  147. assert.Error(err).IsNil()
  148. conn.Close()
  149. }
  150. func Test_listenWSAndDial_TLS(t *testing.T) {
  151. assert := assert.On(t)
  152. go func() {
  153. <-time.After(time.Second * 5)
  154. assert.Fail("Too slow")
  155. }()
  156. (&Config{Pto: "wss", Path: "wss", ConnectionReuse: true, DeveloperInsecureSkipVerify: true, PrivKey: "./../../../testing/tls/key.pem", Cert: "./../../../testing/tls/cert.pem"}).Apply()
  157. listen, err := ListenWS(v2net.DomainAddress("localhost"), 13143)
  158. assert.Error(err).IsNil()
  159. go func() {
  160. conn, err := listen.Accept()
  161. assert.Error(err).IsNil()
  162. conn.Close()
  163. listen.Close()
  164. }()
  165. conn, err := Dial(v2net.AnyIP, v2net.TCPDestination(v2net.DomainAddress("localhost"), 13143))
  166. assert.Error(err).IsNil()
  167. conn.Close()
  168. }