ws_test.go 6.0 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242
  1. package ws_test
  2. import (
  3. "crypto/tls"
  4. "testing"
  5. "time"
  6. v2net "v2ray.com/core/common/net"
  7. "v2ray.com/core/testing/assert"
  8. "v2ray.com/core/transport/internet"
  9. . "v2ray.com/core/transport/internet/ws"
  10. )
  11. func Test_Connect_ws(t *testing.T) {
  12. assert := assert.On(t)
  13. (&Config{Path: ""}).Apply()
  14. conn, err := Dial(v2net.AnyIP, v2net.TCPDestination(v2net.DomainAddress("echo.websocket.org"), 80), internet.DialerOptions{})
  15. assert.Error(err).IsNil()
  16. conn.Write([]byte("echo"))
  17. s := make(chan int)
  18. go func() {
  19. buf := make([]byte, 4)
  20. conn.Read(buf)
  21. str := string(buf)
  22. if str != "echo" {
  23. assert.Fail("Data mismatch")
  24. }
  25. s <- 0
  26. }()
  27. <-s
  28. conn.Close()
  29. }
  30. func Test_Connect_wss(t *testing.T) {
  31. assert := assert.On(t)
  32. (&Config{Path: ""}).Apply()
  33. conn, err := Dial(v2net.AnyIP, v2net.TCPDestination(v2net.DomainAddress("echo.websocket.org"), 443), internet.DialerOptions{
  34. Stream: &internet.StreamSettings{
  35. Security: internet.StreamSecurityTypeTLS,
  36. },
  37. })
  38. assert.Error(err).IsNil()
  39. conn.Write([]byte("echo"))
  40. s := make(chan int)
  41. go func() {
  42. buf := make([]byte, 4)
  43. conn.Read(buf)
  44. str := string(buf)
  45. if str != "echo" {
  46. assert.Fail("Data mismatch")
  47. }
  48. s <- 0
  49. }()
  50. <-s
  51. conn.Close()
  52. }
  53. func Test_Connect_wss_1_nil(t *testing.T) {
  54. assert := assert.On(t)
  55. (&Config{Path: ""}).Apply()
  56. conn, err := Dial(nil, v2net.TCPDestination(v2net.DomainAddress("echo.websocket.org"), 443), internet.DialerOptions{
  57. Stream: &internet.StreamSettings{
  58. Security: internet.StreamSecurityTypeTLS,
  59. },
  60. })
  61. assert.Error(err).IsNil()
  62. conn.Write([]byte("echo"))
  63. s := make(chan int)
  64. go func() {
  65. buf := make([]byte, 4)
  66. conn.Read(buf)
  67. str := string(buf)
  68. if str != "echo" {
  69. assert.Fail("Data mismatch")
  70. }
  71. s <- 0
  72. }()
  73. <-s
  74. conn.Close()
  75. }
  76. func Test_Connect_ws_guess(t *testing.T) {
  77. assert := assert.On(t)
  78. (&Config{Path: ""}).Apply()
  79. conn, err := Dial(v2net.AnyIP, v2net.TCPDestination(v2net.DomainAddress("echo.websocket.org"), 80), internet.DialerOptions{})
  80. assert.Error(err).IsNil()
  81. conn.Write([]byte("echo"))
  82. s := make(chan int)
  83. go func() {
  84. buf := make([]byte, 4)
  85. conn.Read(buf)
  86. str := string(buf)
  87. if str != "echo" {
  88. assert.Fail("Data mismatch")
  89. }
  90. s <- 0
  91. }()
  92. <-s
  93. conn.Close()
  94. }
  95. func Test_Connect_wss_guess(t *testing.T) {
  96. assert := assert.On(t)
  97. (&Config{Path: ""}).Apply()
  98. conn, err := Dial(v2net.AnyIP, v2net.TCPDestination(v2net.DomainAddress("echo.websocket.org"), 443), internet.DialerOptions{
  99. Stream: &internet.StreamSettings{
  100. Security: internet.StreamSecurityTypeTLS,
  101. },
  102. })
  103. assert.Error(err).IsNil()
  104. conn.Write([]byte("echo"))
  105. s := make(chan int)
  106. go func() {
  107. buf := make([]byte, 4)
  108. conn.Read(buf)
  109. str := string(buf)
  110. if str != "echo" {
  111. assert.Fail("Data mismatch")
  112. }
  113. s <- 0
  114. }()
  115. <-s
  116. conn.Close()
  117. }
  118. func Test_Connect_wss_guess_fail(t *testing.T) {
  119. assert := assert.On(t)
  120. (&Config{Path: ""}).Apply()
  121. _, err := Dial(v2net.AnyIP, v2net.TCPDestination(v2net.DomainAddress("static.kkdev.org"), 443), internet.DialerOptions{
  122. Stream: &internet.StreamSettings{
  123. Security: internet.StreamSecurityTypeTLS,
  124. },
  125. })
  126. assert.Error(err).IsNotNil()
  127. }
  128. func Test_Connect_wss_guess_reuse(t *testing.T) {
  129. assert := assert.On(t)
  130. (&Config{Path: "", ConnectionReuse: true}).Apply()
  131. i := 3
  132. for i != 0 {
  133. conn, err := Dial(v2net.AnyIP, v2net.TCPDestination(v2net.DomainAddress("echo.websocket.org"), 443), internet.DialerOptions{
  134. Stream: &internet.StreamSettings{
  135. Security: internet.StreamSecurityTypeTLS,
  136. },
  137. })
  138. assert.Error(err).IsNil()
  139. conn.Write([]byte("echo"))
  140. s := make(chan int)
  141. go func() {
  142. buf := make([]byte, 4)
  143. conn.Read(buf)
  144. str := string(buf)
  145. if str != "echo" {
  146. assert.Fail("Data mismatch")
  147. }
  148. s <- 0
  149. }()
  150. <-s
  151. if i == 0 {
  152. conn.SetDeadline(time.Now())
  153. conn.SetReadDeadline(time.Now())
  154. conn.SetWriteDeadline(time.Now())
  155. conn.SetReusable(false)
  156. }
  157. conn.Close()
  158. i--
  159. }
  160. }
  161. func Test_listenWSAndDial(t *testing.T) {
  162. assert := assert.On(t)
  163. (&Config{Path: "ws"}).Apply()
  164. listen, err := ListenWS(v2net.DomainAddress("localhost"), 13142, internet.ListenOptions{})
  165. assert.Error(err).IsNil()
  166. go func() {
  167. conn, err := listen.Accept()
  168. assert.Error(err).IsNil()
  169. conn.Close()
  170. conn, err = listen.Accept()
  171. assert.Error(err).IsNil()
  172. conn.Close()
  173. conn, err = listen.Accept()
  174. assert.Error(err).IsNil()
  175. conn.Close()
  176. listen.Close()
  177. }()
  178. conn, err := Dial(v2net.AnyIP, v2net.TCPDestination(v2net.DomainAddress("localhost"), 13142), internet.DialerOptions{})
  179. assert.Error(err).IsNil()
  180. conn.Close()
  181. <-time.After(time.Second * 5)
  182. conn, err = Dial(v2net.AnyIP, v2net.TCPDestination(v2net.DomainAddress("localhost"), 13142), internet.DialerOptions{})
  183. assert.Error(err).IsNil()
  184. conn.Close()
  185. <-time.After(time.Second * 15)
  186. conn, err = Dial(v2net.AnyIP, v2net.TCPDestination(v2net.DomainAddress("localhost"), 13142), internet.DialerOptions{})
  187. assert.Error(err).IsNil()
  188. conn.Close()
  189. }
  190. func Test_listenWSAndDial_TLS(t *testing.T) {
  191. assert := assert.On(t)
  192. go func() {
  193. <-time.After(time.Second * 5)
  194. assert.Fail("Too slow")
  195. }()
  196. (&Config{Path: "wss", ConnectionReuse: true}).Apply()
  197. listen, err := ListenWS(v2net.DomainAddress("localhost"), 13143, internet.ListenOptions{
  198. Stream: &internet.StreamSettings{
  199. Security: internet.StreamSecurityTypeTLS,
  200. TLSSettings: &internet.TLSSettings{
  201. AllowInsecure: true,
  202. Certs: LoadTestCert(assert),
  203. },
  204. },
  205. })
  206. assert.Error(err).IsNil()
  207. go func() {
  208. conn, err := listen.Accept()
  209. assert.Error(err).IsNil()
  210. conn.Close()
  211. listen.Close()
  212. }()
  213. conn, err := Dial(v2net.AnyIP, v2net.TCPDestination(v2net.DomainAddress("localhost"), 13143), internet.DialerOptions{
  214. Stream: &internet.StreamSettings{
  215. Security: internet.StreamSecurityTypeTLS,
  216. TLSSettings: &internet.TLSSettings{
  217. AllowInsecure: true,
  218. Certs: LoadTestCert(assert),
  219. },
  220. },
  221. })
  222. assert.Error(err).IsNil()
  223. conn.Close()
  224. }
  225. func LoadTestCert(assert *assert.Assert) []tls.Certificate {
  226. cert, err := tls.LoadX509KeyPair("./../../../testing/tls/cert.pem", "./../../../testing/tls/key.pem")
  227. assert.Error(err).IsNil()
  228. return []tls.Certificate{cert}
  229. }