transport_test.go 4.2 KB

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