github.com/v2fly/v2ray-core/v4@v4.45.2/infra/conf/transport_test.go (about)

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