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