transport_test.go 4.4 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174
  1. package v4_test
  2. import (
  3. "encoding/json"
  4. "testing"
  5. "github.com/golang/protobuf/proto"
  6. "github.com/v2fly/v2ray-core/v4/common/protocol"
  7. "github.com/v2fly/v2ray-core/v4/common/serial"
  8. "github.com/v2fly/v2ray-core/v4/infra/conf/cfgcommon/socketcfg"
  9. "github.com/v2fly/v2ray-core/v4/infra/conf/cfgcommon/testassist"
  10. "github.com/v2fly/v2ray-core/v4/infra/conf/v4"
  11. "github.com/v2fly/v2ray-core/v4/transport"
  12. "github.com/v2fly/v2ray-core/v4/transport/internet"
  13. "github.com/v2fly/v2ray-core/v4/transport/internet/headers/http"
  14. "github.com/v2fly/v2ray-core/v4/transport/internet/headers/noop"
  15. "github.com/v2fly/v2ray-core/v4/transport/internet/headers/tls"
  16. "github.com/v2fly/v2ray-core/v4/transport/internet/kcp"
  17. "github.com/v2fly/v2ray-core/v4/transport/internet/quic"
  18. "github.com/v2fly/v2ray-core/v4/transport/internet/tcp"
  19. "github.com/v2fly/v2ray-core/v4/transport/internet/websocket"
  20. )
  21. func TestSocketConfig(t *testing.T) {
  22. createParser := func() func(string) (proto.Message, error) {
  23. return func(s string) (proto.Message, error) {
  24. config := new(socketcfg.SocketConfig)
  25. if err := json.Unmarshal([]byte(s), config); err != nil {
  26. return nil, err
  27. }
  28. return config.Build()
  29. }
  30. }
  31. testassist.RunMultiTestCase(t, []testassist.TestCase{
  32. {
  33. Input: `{
  34. "mark": 1,
  35. "tcpFastOpen": true,
  36. "tcpFastOpenQueueLength": 1024
  37. }`,
  38. Parser: createParser(),
  39. Output: &internet.SocketConfig{
  40. Mark: 1,
  41. Tfo: internet.SocketConfig_Enable,
  42. TfoQueueLength: 1024,
  43. },
  44. },
  45. })
  46. }
  47. func TestTransportConfig(t *testing.T) {
  48. createParser := func() func(string) (proto.Message, error) {
  49. return func(s string) (proto.Message, error) {
  50. config := new(v4.TransportConfig)
  51. if err := json.Unmarshal([]byte(s), config); err != nil {
  52. return nil, err
  53. }
  54. return config.Build()
  55. }
  56. }
  57. testassist.RunMultiTestCase(t, []testassist.TestCase{
  58. {
  59. Input: `{
  60. "tcpSettings": {
  61. "header": {
  62. "type": "http",
  63. "request": {
  64. "version": "1.1",
  65. "method": "GET",
  66. "path": "/b",
  67. "headers": {
  68. "a": "b",
  69. "c": "d"
  70. }
  71. },
  72. "response": {
  73. "version": "1.0",
  74. "status": "404",
  75. "reason": "Not Found"
  76. }
  77. }
  78. },
  79. "kcpSettings": {
  80. "mtu": 1200,
  81. "header": {
  82. "type": "none"
  83. }
  84. },
  85. "wsSettings": {
  86. "path": "/t"
  87. },
  88. "quicSettings": {
  89. "key": "abcd",
  90. "header": {
  91. "type": "dtls"
  92. }
  93. }
  94. }`,
  95. Parser: createParser(),
  96. Output: &transport.Config{
  97. TransportSettings: []*internet.TransportConfig{
  98. {
  99. ProtocolName: "tcp",
  100. Settings: serial.ToTypedMessage(&tcp.Config{
  101. HeaderSettings: serial.ToTypedMessage(&http.Config{
  102. Request: &http.RequestConfig{
  103. Version: &http.Version{Value: "1.1"},
  104. Method: &http.Method{Value: "GET"},
  105. Uri: []string{"/b"},
  106. Header: []*http.Header{
  107. {Name: "a", Value: []string{"b"}},
  108. {Name: "c", Value: []string{"d"}},
  109. },
  110. },
  111. Response: &http.ResponseConfig{
  112. Version: &http.Version{Value: "1.0"},
  113. Status: &http.Status{Code: "404", Reason: "Not Found"},
  114. Header: []*http.Header{
  115. {
  116. Name: "Content-Type",
  117. Value: []string{"application/octet-stream", "video/mpeg"},
  118. },
  119. {
  120. Name: "Transfer-Encoding",
  121. Value: []string{"chunked"},
  122. },
  123. {
  124. Name: "Connection",
  125. Value: []string{"keep-alive"},
  126. },
  127. {
  128. Name: "Pragma",
  129. Value: []string{"no-cache"},
  130. },
  131. {
  132. Name: "Cache-Control",
  133. Value: []string{"private", "no-cache"},
  134. },
  135. },
  136. },
  137. }),
  138. }),
  139. },
  140. {
  141. ProtocolName: "mkcp",
  142. Settings: serial.ToTypedMessage(&kcp.Config{
  143. Mtu: &kcp.MTU{Value: 1200},
  144. HeaderConfig: serial.ToTypedMessage(&noop.Config{}),
  145. }),
  146. },
  147. {
  148. ProtocolName: "websocket",
  149. Settings: serial.ToTypedMessage(&websocket.Config{
  150. Path: "/t",
  151. }),
  152. },
  153. {
  154. ProtocolName: "quic",
  155. Settings: serial.ToTypedMessage(&quic.Config{
  156. Key: "abcd",
  157. Security: &protocol.SecurityConfig{
  158. Type: protocol.SecurityType_NONE,
  159. },
  160. Header: serial.ToTypedMessage(&tls.PacketConfig{}),
  161. }),
  162. },
  163. },
  164. },
  165. },
  166. })
  167. }