github.com/imannamdari/v2ray-core/v5@v5.0.5/infra/conf/v4/transport_test.go (about)

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