transport_test.go 4.3 KB

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