File size: 4,750 Bytes
dde4052 | 1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 22 23 24 25 26 27 28 29 30 31 32 33 34 35 36 37 38 39 40 41 42 43 44 45 46 47 48 49 50 51 52 53 54 55 56 57 58 59 60 61 62 63 64 65 66 67 68 69 70 71 72 73 74 75 76 77 78 79 80 81 82 83 84 85 86 87 88 89 90 91 92 93 94 95 96 97 98 99 100 101 102 103 104 105 106 107 108 109 110 111 112 113 114 115 116 117 118 119 120 121 122 123 124 125 126 127 128 129 130 131 132 133 134 135 136 137 138 139 140 141 142 143 144 145 146 147 148 149 150 151 152 153 154 155 156 157 158 159 160 161 162 163 164 165 166 167 168 169 170 171 172 173 174 175 176 177 178 179 180 181 182 183 184 185 186 187 188 189 190 191 192 193 194 | package api
import (
"testing"
)
func TestMaskSensitiveString(t *testing.T) {
tests := []struct {
name string
input string
expected string
}{
{
name: "空字符串",
input: "",
expected: "",
},
{
name: "短字符串(小于等于8位)",
input: "short",
expected: "****",
},
{
name: "正常API key",
input: "sk-1234567890abcdefghijklmnopqrstuvwxyz",
expected: "sk-1****wxyz",
},
{
name: "正常私钥",
input: "0x1234567890abcdef1234567890abcdef12345678",
expected: "0x12****5678",
},
{
name: "刚好9位",
input: "123456789",
expected: "1234****6789",
},
}
for _, tt := range tests {
t.Run(tt.name, func(t *testing.T) {
result := MaskSensitiveString(tt.input)
if result != tt.expected {
t.Errorf("MaskSensitiveString(%q) = %q, want %q", tt.input, result, tt.expected)
}
})
}
}
func TestSanitizeModelConfigForLog(t *testing.T) {
models := map[string]struct {
Enabled bool `json:"enabled"`
APIKey string `json:"api_key"`
CustomAPIURL string `json:"custom_api_url"`
CustomModelName string `json:"custom_model_name"`
}{
"deepseek": {
Enabled: true,
APIKey: "sk-1234567890abcdefghijklmnopqrstuvwxyz",
CustomAPIURL: "https://api.deepseek.com",
CustomModelName: "deepseek-chat",
},
}
result := SanitizeModelConfigForLog(models)
deepseekConfig, ok := result["deepseek"].(map[string]interface{})
if !ok {
t.Fatal("deepseek config not found or wrong type")
}
if deepseekConfig["enabled"] != true {
t.Errorf("expected enabled=true, got %v", deepseekConfig["enabled"])
}
maskedKey, ok := deepseekConfig["api_key"].(string)
if !ok {
t.Fatal("api_key not found or wrong type")
}
if maskedKey != "sk-1****wxyz" {
t.Errorf("expected masked api_key='sk-1****wxyz', got %q", maskedKey)
}
if deepseekConfig["custom_api_url"] != "https://api.deepseek.com" {
t.Errorf("custom_api_url should not be masked")
}
}
func TestSanitizeExchangeConfigForLog(t *testing.T) {
exchanges := map[string]struct {
Enabled bool `json:"enabled"`
APIKey string `json:"api_key"`
SecretKey string `json:"secret_key"`
Testnet bool `json:"testnet"`
HyperliquidWalletAddr string `json:"hyperliquid_wallet_addr"`
AsterUser string `json:"aster_user"`
AsterSigner string `json:"aster_signer"`
AsterPrivateKey string `json:"aster_private_key"`
}{
"binance": {
Enabled: true,
APIKey: "binance_api_key_1234567890abcdef",
SecretKey: "binance_secret_key_1234567890abcdef",
Testnet: false,
},
"hyperliquid": {
Enabled: true,
HyperliquidWalletAddr: "0x1234567890abcdef1234567890abcdef12345678",
Testnet: false,
},
}
result := SanitizeExchangeConfigForLog(exchanges)
// 检查币安配置
binanceConfig, ok := result["binance"].(map[string]interface{})
if !ok {
t.Fatal("binance config not found or wrong type")
}
maskedAPIKey, ok := binanceConfig["api_key"].(string)
if !ok {
t.Fatal("binance api_key not found or wrong type")
}
if maskedAPIKey != "bina****cdef" {
t.Errorf("expected masked api_key='bina****cdef', got %q", maskedAPIKey)
}
maskedSecretKey, ok := binanceConfig["secret_key"].(string)
if !ok {
t.Fatal("binance secret_key not found or wrong type")
}
if maskedSecretKey != "bina****cdef" {
t.Errorf("expected masked secret_key='bina****cdef', got %q", maskedSecretKey)
}
// 检查 Hyperliquid 配置
hlConfig, ok := result["hyperliquid"].(map[string]interface{})
if !ok {
t.Fatal("hyperliquid config not found or wrong type")
}
walletAddr, ok := hlConfig["hyperliquid_wallet_addr"].(string)
if !ok {
t.Fatal("hyperliquid_wallet_addr not found or wrong type")
}
// 钱包地址不应该被脱敏
if walletAddr != "0x1234567890abcdef1234567890abcdef12345678" {
t.Errorf("wallet address should not be masked, got %q", walletAddr)
}
}
func TestMaskEmail(t *testing.T) {
tests := []struct {
name string
input string
expected string
}{
{
name: "空邮箱",
input: "",
expected: "",
},
{
name: "格式错误",
input: "notanemail",
expected: "****",
},
{
name: "正常邮箱",
input: "user@example.com",
expected: "us****@example.com",
},
{
name: "短用户名",
input: "a@example.com",
expected: "**@example.com",
},
}
for _, tt := range tests {
t.Run(tt.name, func(t *testing.T) {
result := MaskEmail(tt.input)
if result != tt.expected {
t.Errorf("MaskEmail(%q) = %q, want %q", tt.input, result, tt.expected)
}
})
}
}
|