ws_test.go 2.7 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102
  1. package websocket_test
  2. import (
  3. "bytes"
  4. "context"
  5. "testing"
  6. "time"
  7. "v2ray.com/core/common/net"
  8. tlsgen "v2ray.com/core/testing/tls"
  9. "v2ray.com/core/transport/internet"
  10. v2tls "v2ray.com/core/transport/internet/tls"
  11. . "v2ray.com/core/transport/internet/websocket"
  12. . "v2ray.com/ext/assert"
  13. )
  14. func Test_listenWSAndDial(t *testing.T) {
  15. assert := With(t)
  16. listen, err := ListenWS(internet.ContextWithTransportSettings(context.Background(), &Config{
  17. Path: "ws",
  18. }), net.DomainAddress("localhost"), 13146, func(ctx context.Context, conn internet.Connection) bool {
  19. go func(c internet.Connection) {
  20. defer c.Close()
  21. var b [1024]byte
  22. n, err := c.Read(b[:])
  23. //assert(err, IsNil)
  24. if err != nil {
  25. return
  26. }
  27. assert(bytes.HasPrefix(b[:n], []byte("Test connection")), IsTrue)
  28. _, err = c.Write([]byte("Response"))
  29. assert(err, IsNil)
  30. }(conn)
  31. return true
  32. })
  33. assert(err, IsNil)
  34. ctx := internet.ContextWithTransportSettings(context.Background(), &Config{Path: "ws"})
  35. conn, err := Dial(ctx, net.TCPDestination(net.DomainAddress("localhost"), 13146))
  36. assert(err, IsNil)
  37. _, err = conn.Write([]byte("Test connection 1"))
  38. assert(err, IsNil)
  39. var b [1024]byte
  40. n, err := conn.Read(b[:])
  41. assert(err, IsNil)
  42. assert(string(b[:n]), Equals, "Response")
  43. assert(conn.Close(), IsNil)
  44. <-time.After(time.Second * 5)
  45. conn, err = Dial(ctx, net.TCPDestination(net.DomainAddress("localhost"), 13146))
  46. assert(err, IsNil)
  47. _, err = conn.Write([]byte("Test connection 2"))
  48. assert(err, IsNil)
  49. n, err = conn.Read(b[:])
  50. assert(err, IsNil)
  51. assert(string(b[:n]), Equals, "Response")
  52. assert(conn.Close(), IsNil)
  53. <-time.After(time.Second * 15)
  54. conn, err = Dial(ctx, net.TCPDestination(net.DomainAddress("localhost"), 13146))
  55. assert(err, IsNil)
  56. _, err = conn.Write([]byte("Test connection 3"))
  57. assert(err, IsNil)
  58. n, err = conn.Read(b[:])
  59. assert(err, IsNil)
  60. assert(string(b[:n]), Equals, "Response")
  61. assert(conn.Close(), IsNil)
  62. assert(listen.Close(), IsNil)
  63. }
  64. func Test_listenWSAndDial_TLS(t *testing.T) {
  65. assert := With(t)
  66. start := time.Now()
  67. ctx := internet.ContextWithTransportSettings(context.Background(), &Config{
  68. Path: "wss",
  69. })
  70. ctx = internet.ContextWithSecuritySettings(ctx, &v2tls.Config{
  71. AllowInsecure: true,
  72. Certificate: []*v2tls.Certificate{tlsgen.GenerateCertificateForTest()},
  73. })
  74. listen, err := ListenWS(ctx, net.DomainAddress("localhost"), 13143, func(ctx context.Context, conn internet.Connection) bool {
  75. go func() {
  76. _ = conn.Close()
  77. }()
  78. return true
  79. })
  80. assert(err, IsNil)
  81. defer listen.Close()
  82. conn, err := Dial(ctx, net.TCPDestination(net.DomainAddress("localhost"), 13143))
  83. assert(err, IsNil)
  84. _ = conn.Close()
  85. end := time.Now()
  86. assert(end.Before(start.Add(time.Second*5)), IsTrue)
  87. }