185 lines
4.9 KiB
Go
185 lines
4.9 KiB
Go
package xboard
|
|
|
|
import (
|
|
"testing"
|
|
|
|
"github.com/sagernet/sing-box/option"
|
|
)
|
|
|
|
func TestXUserResolveKeyPrefersPasswordFields(t *testing.T) {
|
|
user := XUser{
|
|
UUID: "uuid-value",
|
|
Passwd: "passwd-value",
|
|
Password: "password-value",
|
|
Token: "token-value",
|
|
}
|
|
|
|
if got := user.ResolveKey(); got != "passwd-value" {
|
|
t.Fatalf("ResolveKey() = %q, want %q", got, "passwd-value")
|
|
}
|
|
}
|
|
|
|
func TestXUserIdentifierPrefersUUID(t *testing.T) {
|
|
user := XUser{
|
|
ID: 7,
|
|
UUID: "uuid-value",
|
|
Email: "user@example.com",
|
|
}
|
|
|
|
if got := user.Identifier(); got != "uuid-value" {
|
|
t.Fatalf("Identifier() = %q, want %q", got, "uuid-value")
|
|
}
|
|
}
|
|
|
|
func TestResolveUserKeyForSS2022CombinedPassword(t *testing.T) {
|
|
service := &Service{ssServerKey: "master-key"}
|
|
user := XUser{
|
|
ID: 1,
|
|
Password: "master-key:user-key",
|
|
UUID: "uuid-value",
|
|
}
|
|
|
|
if got := service.resolveUserKey(user, true); got != "user-key" {
|
|
t.Fatalf("resolveUserKey() = %q, want %q", got, "user-key")
|
|
}
|
|
}
|
|
|
|
func TestResolveUserKeyForNonSS2022UsesResolvedKey(t *testing.T) {
|
|
service := &Service{}
|
|
user := XUser{
|
|
UUID: "uuid-value",
|
|
Passwd: "passwd-value",
|
|
}
|
|
|
|
if got := service.resolveUserKey(user, false); got != "passwd-value" {
|
|
t.Fatalf("resolveUserKey() = %q, want %q", got, "passwd-value")
|
|
}
|
|
}
|
|
|
|
func TestXUserIdentifierFallsBackToEmailThenID(t *testing.T) {
|
|
userWithEmail := XUser{
|
|
ID: 8,
|
|
Email: "user@example.com",
|
|
}
|
|
if got := userWithEmail.Identifier(); got != "user@example.com" {
|
|
t.Fatalf("Identifier() = %q, want %q", got, "user@example.com")
|
|
}
|
|
|
|
userWithID := XUser{ID: 9}
|
|
if got := userWithID.Identifier(); got != "9" {
|
|
t.Fatalf("Identifier() = %q, want %q", got, "9")
|
|
}
|
|
}
|
|
|
|
func TestExpandNodeOptions(t *testing.T) {
|
|
base := option.XBoardServiceOptions{
|
|
PanelURL: "https://panel.example",
|
|
Key: "shared-token",
|
|
NodeType: "vless",
|
|
Nodes: []option.XBoardNodeOptions{
|
|
{NodeID: 1},
|
|
{NodeID: 2, NodeType: "anytls"},
|
|
},
|
|
}
|
|
|
|
nodes := expandNodeOptions(base)
|
|
if len(nodes) != 2 {
|
|
t.Fatalf("expandNodeOptions() len = %d, want 2", len(nodes))
|
|
}
|
|
if nodes[0].NodeID != 1 || nodes[0].NodeType != "vless" {
|
|
t.Fatalf("first node = %+v", nodes[0])
|
|
}
|
|
if nodes[1].NodeID != 2 || nodes[1].NodeType != "anytls" {
|
|
t.Fatalf("second node = %+v", nodes[1])
|
|
}
|
|
}
|
|
|
|
func TestExpandNodeOptionsMinimalInstallConfig(t *testing.T) {
|
|
base := option.XBoardServiceOptions{
|
|
PanelURL: "https://panel.example",
|
|
Key: "shared-token",
|
|
SyncInterval: 60,
|
|
ReportInterval: 60,
|
|
Nodes: []option.XBoardNodeOptions{
|
|
{NodeID: 286},
|
|
{NodeID: 774},
|
|
},
|
|
}
|
|
|
|
nodes := expandNodeOptions(base)
|
|
if len(nodes) != 2 {
|
|
t.Fatalf("expandNodeOptions() len = %d, want 2", len(nodes))
|
|
}
|
|
for index, node := range nodes {
|
|
if node.PanelURL != base.PanelURL {
|
|
t.Fatalf("node %d PanelURL = %q, want %q", index, node.PanelURL, base.PanelURL)
|
|
}
|
|
if node.Key != base.Key {
|
|
t.Fatalf("node %d Key = %q, want %q", index, node.Key, base.Key)
|
|
}
|
|
if node.SyncInterval != base.SyncInterval {
|
|
t.Fatalf("node %d SyncInterval = %v, want %v", index, node.SyncInterval, base.SyncInterval)
|
|
}
|
|
if node.ReportInterval != base.ReportInterval {
|
|
t.Fatalf("node %d ReportInterval = %v, want %v", index, node.ReportInterval, base.ReportInterval)
|
|
}
|
|
if node.ConfigPanelURL != "" || node.UserPanelURL != "" {
|
|
t.Fatalf("node %d unexpected panel overrides: config=%q user=%q", index, node.ConfigPanelURL, node.UserPanelURL)
|
|
}
|
|
if node.ConfigNodeID != 0 || node.UserNodeID != 0 {
|
|
t.Fatalf("node %d unexpected node overrides: config=%d user=%d", index, node.ConfigNodeID, node.UserNodeID)
|
|
}
|
|
}
|
|
if nodes[0].NodeID != 286 {
|
|
t.Fatalf("first node NodeID = %d, want 286", nodes[0].NodeID)
|
|
}
|
|
if nodes[1].NodeID != 774 {
|
|
t.Fatalf("second node NodeID = %d, want 774", nodes[1].NodeID)
|
|
}
|
|
}
|
|
|
|
func TestExpandedNodeTagFallsBackToNodeID(t *testing.T) {
|
|
tag := expandedNodeTag("", 0, option.XBoardNodeOptions{NodeID: 286}, option.XBoardServiceOptions{NodeID: 286})
|
|
if tag != "xboard-286" {
|
|
t.Fatalf("expandedNodeTag() = %q, want %q", tag, "xboard-286")
|
|
}
|
|
}
|
|
|
|
func TestBuildInboundMultiplex(t *testing.T) {
|
|
config := &XMultiplexConfig{
|
|
Enabled: true,
|
|
Padding: true,
|
|
Brutal: &XBrutalConfig{
|
|
Enabled: true,
|
|
UpMbps: 100,
|
|
DownMbps: 200,
|
|
},
|
|
}
|
|
|
|
got := buildInboundMultiplex(config)
|
|
if got == nil {
|
|
t.Fatal("buildInboundMultiplex() returned nil")
|
|
}
|
|
if !got.Enabled || !got.Padding {
|
|
t.Fatalf("buildInboundMultiplex() = %+v", got)
|
|
}
|
|
if got.Brutal == nil || !got.Brutal.Enabled || got.Brutal.UpMbps != 100 || got.Brutal.DownMbps != 200 {
|
|
t.Fatalf("buildInboundMultiplex() brutal = %+v", got.Brutal)
|
|
}
|
|
}
|
|
|
|
func TestNormalizePanelNodeType(t *testing.T) {
|
|
tests := map[string]string{
|
|
"v2ray": "vmess",
|
|
"hysteria2": "hysteria",
|
|
"vless": "vless",
|
|
"": "",
|
|
}
|
|
|
|
for input, want := range tests {
|
|
if got := normalizePanelNodeType(input); got != want {
|
|
t.Fatalf("normalizePanelNodeType(%q) = %q, want %q", input, got, want)
|
|
}
|
|
}
|
|
}
|