Иван authored and GitHub committed 2026-04-15 14:17:51 +00:00
1 parent 6780045550
commit 175502d807
16 files changed
+2275 -150

No files matched your search

+134
View File
@@ -2,10 +2,12 @@ package conf_test
import (
"encoding/json"
"strings"
"testing"
. "github.com/xtls/xray-core/infra/conf"
"github.com/xtls/xray-core/transport/internet"
finalmaskcustom "github.com/xtls/xray-core/transport/internet/finalmask/header/custom"
"google.golang.org/protobuf/proto"
)
@@ -156,3 +158,135 @@ func TestSocketConfig(t *testing.T) {
t.Fatalf("unexpected parsed TFO value, which should be -1")
}
}
func TestHeaderCustomUDPBuild(t *testing.T) {
parser := loadJSON(func() Buildable { return new(HeaderCustomUDP) })
runMultiTestCase(t, []TestCase{
{
Input: `{
"client": [
{
"type": "hex",
"packet": "aabb"
},
{
"rand": 2,
"capture": "seed",
"randRange": "16-32"
}
],
"server": [
{
"capture": "txid",
"transform": {
"op": "concat",
"args": [
{"reuse": "seed"},
{"u64": 258},
{"type": "hex", "bytes": "c0de"}
]
}
},
{
"reuse": "txid"
}
]
}`,
Parser: parser,
Output: &finalmaskcustom.UDPConfig{
Client: []*finalmaskcustom.UDPItem{
{
RandMax: 255,
Packet: []byte{0xAA, 0xBB},
},
{
Rand: 2,
RandMin: 16,
RandMax: 32,
Save: "seed",
},
},
Server: []*finalmaskcustom.UDPItem{
{
RandMax: 255,
Save: "txid",
Expr: &finalmaskcustom.Expr{
Op: "concat",
Args: []*finalmaskcustom.ExprArg{
{
Value: &finalmaskcustom.ExprArg_Var{
Var: "seed",
},
},
{
Value: &finalmaskcustom.ExprArg_U64{
U64: 258,
},
},
{
Value: &finalmaskcustom.ExprArg_Bytes{
Bytes: []byte{0xC0, 0xDE},
},
},
},
},
},
{
RandMax: 255,
Var: "txid",
},
},
},
},
})
}
func TestHeaderCustomTCPBuildRejectsMixedItemKinds(t *testing.T) {
parser := loadJSON(func() Buildable { return new(HeaderCustomTCP) })
_, err := parser(`{
"clients": [[
{
"packet": [1, 2],
"reuse": "txid"
}
]]
}`)
if err == nil || !strings.Contains(err.Error(), "exactly one item kind") {
t.Fatalf("expected mixed item kind rejection, got %v", err)
}
}
func TestHeaderCustomUDPBuildRejectsInvalidVariableNames(t *testing.T) {
parser := loadJSON(func() Buildable { return new(HeaderCustomUDP) })
_, err := parser(`{
"client": [
{
"capture": "bad-name",
"rand": 4
}
]
}`)
if err == nil || !strings.Contains(err.Error(), "invalid variable name") {
t.Fatalf("expected invalid variable name rejection, got %v", err)
}
}
func TestHeaderCustomUDPBuildRejectsExprWithoutArgs(t *testing.T) {
parser := loadJSON(func() Buildable { return new(HeaderCustomUDP) })
_, err := parser(`{
"client": [
{
"transform": {
"op": "concat"
}
}
]
}`)
if err == nil || !strings.Contains(err.Error(), "transform args") {
t.Fatalf("expected transform arg rejection, got %v", err)
}
}