2020-11-25 14:01:53 +03:00
|
|
|
package conf_test
|
|
|
|
|
|
|
|
import (
|
|
|
|
"encoding/json"
|
|
|
|
"testing"
|
|
|
|
|
2020-12-04 04:36:16 +03:00
|
|
|
. "github.com/xtls/xray-core/infra/conf"
|
|
|
|
"github.com/xtls/xray-core/transport/internet"
|
2023-08-10 07:43:34 +03:00
|
|
|
"google.golang.org/protobuf/proto"
|
2020-11-25 14:01:53 +03:00
|
|
|
)
|
|
|
|
|
|
|
|
func TestSocketConfig(t *testing.T) {
|
|
|
|
createParser := func() func(string) (proto.Message, error) {
|
|
|
|
return func(s string) (proto.Message, error) {
|
|
|
|
config := new(SocketConfig)
|
|
|
|
if err := json.Unmarshal([]byte(s), config); err != nil {
|
|
|
|
return nil, err
|
|
|
|
}
|
|
|
|
return config.Build()
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
2021-03-30 19:42:02 +03:00
|
|
|
// test "tcpFastOpen": true, queue length 256 is expected. other parameters are tested here too
|
|
|
|
expectedOutput := &internet.SocketConfig{
|
|
|
|
Mark: 1,
|
|
|
|
Tfo: 256,
|
|
|
|
DomainStrategy: internet.DomainStrategy_USE_IP,
|
|
|
|
DialerProxy: "tag",
|
|
|
|
}
|
2020-11-25 14:01:53 +03:00
|
|
|
runMultiTestCase(t, []TestCase{
|
|
|
|
{
|
|
|
|
Input: `{
|
|
|
|
"mark": 1,
|
2021-03-06 19:29:17 +03:00
|
|
|
"tcpFastOpen": true,
|
|
|
|
"domainStrategy": "UseIP",
|
|
|
|
"dialerProxy": "tag"
|
2020-11-25 14:01:53 +03:00
|
|
|
}`,
|
|
|
|
Parser: createParser(),
|
2021-03-30 19:42:02 +03:00
|
|
|
Output: expectedOutput,
|
2021-03-06 17:45:12 +03:00
|
|
|
},
|
|
|
|
})
|
2021-03-30 19:42:02 +03:00
|
|
|
if expectedOutput.ParseTFOValue() != 256 {
|
|
|
|
t.Fatalf("unexpected parsed TFO value, which should be 256")
|
|
|
|
}
|
|
|
|
|
|
|
|
// test "tcpFastOpen": false, disabled TFO is expected
|
|
|
|
expectedOutput = &internet.SocketConfig{
|
|
|
|
Mark: 0,
|
|
|
|
Tfo: -1,
|
|
|
|
}
|
2021-03-06 17:45:12 +03:00
|
|
|
runMultiTestCase(t, []TestCase{
|
|
|
|
{
|
|
|
|
Input: `{
|
|
|
|
"tcpFastOpen": false
|
|
|
|
}`,
|
|
|
|
Parser: createParser(),
|
2021-03-30 19:42:02 +03:00
|
|
|
Output: expectedOutput,
|
2021-03-06 17:45:12 +03:00
|
|
|
},
|
|
|
|
})
|
2021-03-30 19:42:02 +03:00
|
|
|
if expectedOutput.ParseTFOValue() != 0 {
|
|
|
|
t.Fatalf("unexpected parsed TFO value, which should be 0")
|
|
|
|
}
|
|
|
|
|
|
|
|
// test "tcpFastOpen": 65535, queue length 65535 is expected
|
|
|
|
expectedOutput = &internet.SocketConfig{
|
|
|
|
Mark: 0,
|
|
|
|
Tfo: 65535,
|
|
|
|
}
|
2021-03-06 17:45:12 +03:00
|
|
|
runMultiTestCase(t, []TestCase{
|
|
|
|
{
|
|
|
|
Input: `{
|
|
|
|
"tcpFastOpen": 65535
|
|
|
|
}`,
|
|
|
|
Parser: createParser(),
|
2021-03-30 19:42:02 +03:00
|
|
|
Output: expectedOutput,
|
|
|
|
},
|
|
|
|
})
|
|
|
|
if expectedOutput.ParseTFOValue() != 65535 {
|
|
|
|
t.Fatalf("unexpected parsed TFO value, which should be 65535")
|
|
|
|
}
|
|
|
|
|
|
|
|
// test "tcpFastOpen": -65535, disable TFO is expected
|
|
|
|
expectedOutput = &internet.SocketConfig{
|
|
|
|
Mark: 0,
|
|
|
|
Tfo: -65535,
|
|
|
|
}
|
|
|
|
runMultiTestCase(t, []TestCase{
|
|
|
|
{
|
|
|
|
Input: `{
|
|
|
|
"tcpFastOpen": -65535
|
|
|
|
}`,
|
|
|
|
Parser: createParser(),
|
|
|
|
Output: expectedOutput,
|
|
|
|
},
|
|
|
|
})
|
|
|
|
if expectedOutput.ParseTFOValue() != 0 {
|
|
|
|
t.Fatalf("unexpected parsed TFO value, which should be 0")
|
|
|
|
}
|
|
|
|
|
|
|
|
// test "tcpFastOpen": 0, no operation is expected
|
|
|
|
expectedOutput = &internet.SocketConfig{
|
|
|
|
Mark: 0,
|
|
|
|
Tfo: 0,
|
|
|
|
}
|
|
|
|
runMultiTestCase(t, []TestCase{
|
|
|
|
{
|
|
|
|
Input: `{
|
|
|
|
"tcpFastOpen": 0
|
|
|
|
}`,
|
|
|
|
Parser: createParser(),
|
|
|
|
Output: expectedOutput,
|
|
|
|
},
|
|
|
|
})
|
|
|
|
if expectedOutput.ParseTFOValue() != -1 {
|
|
|
|
t.Fatalf("unexpected parsed TFO value, which should be -1")
|
|
|
|
}
|
|
|
|
|
|
|
|
// test omit "tcpFastOpen", no operation is expected
|
|
|
|
expectedOutput = &internet.SocketConfig{
|
|
|
|
Mark: 0,
|
|
|
|
Tfo: 0,
|
|
|
|
}
|
|
|
|
runMultiTestCase(t, []TestCase{
|
|
|
|
{
|
|
|
|
Input: `{}`,
|
|
|
|
Parser: createParser(),
|
|
|
|
Output: expectedOutput,
|
2020-11-25 14:01:53 +03:00
|
|
|
},
|
|
|
|
})
|
2021-03-30 19:42:02 +03:00
|
|
|
if expectedOutput.ParseTFOValue() != -1 {
|
|
|
|
t.Fatalf("unexpected parsed TFO value, which should be -1")
|
|
|
|
}
|
|
|
|
|
|
|
|
// test "tcpFastOpen": null, no operation is expected
|
|
|
|
expectedOutput = &internet.SocketConfig{
|
|
|
|
Mark: 0,
|
|
|
|
Tfo: 0,
|
|
|
|
}
|
|
|
|
runMultiTestCase(t, []TestCase{
|
|
|
|
{
|
|
|
|
Input: `{
|
|
|
|
"tcpFastOpen": null
|
|
|
|
}`,
|
|
|
|
Parser: createParser(),
|
|
|
|
Output: expectedOutput,
|
|
|
|
},
|
|
|
|
})
|
|
|
|
if expectedOutput.ParseTFOValue() != -1 {
|
|
|
|
t.Fatalf("unexpected parsed TFO value, which should be -1")
|
|
|
|
}
|
2020-11-25 14:01:53 +03:00
|
|
|
}
|