You can not select more than 25 topics
Topics must start with a letter or number, can include dashes ('-') and can be up to 35 characters long.
205 lines
3.9 KiB
205 lines
3.9 KiB
package config
|
|
|
|
import (
|
|
"os"
|
|
"testing"
|
|
"time"
|
|
)
|
|
|
|
func TestLoadConfig(t *testing.T) {
|
|
// 创建临时配置文件
|
|
tmpFile, err := os.CreateTemp("", "test_config_*.yaml")
|
|
if err != nil {
|
|
t.Fatal(err)
|
|
}
|
|
defer os.Remove(tmpFile.Name())
|
|
|
|
configContent := `
|
|
serviceType: client
|
|
server:
|
|
address: 127.0.0.1
|
|
port: 1080
|
|
username: testuser
|
|
password: testpass
|
|
proxy:
|
|
mode: http
|
|
localPort: 8080
|
|
globalProxy:
|
|
enabled: true
|
|
dnsProxy: true
|
|
dnsPort: 5353
|
|
logLevel: debug
|
|
timeout: 60s
|
|
`
|
|
|
|
if _, err := tmpFile.WriteString(configContent); err != nil {
|
|
t.Fatal(err)
|
|
}
|
|
tmpFile.Close()
|
|
|
|
// 测试加载配置
|
|
config, err := LoadConfig(tmpFile.Name())
|
|
if err != nil {
|
|
t.Fatalf("LoadConfig failed: %v", err)
|
|
}
|
|
|
|
// 验证配置值
|
|
if config.ServiceType != "client" {
|
|
t.Errorf("Expected ServiceType 'client', got '%s'", config.ServiceType)
|
|
}
|
|
|
|
if config.Server.Address != "127.0.0.1" {
|
|
t.Errorf("Expected Server.Address '127.0.0.1', got '%s'", config.Server.Address)
|
|
}
|
|
|
|
if config.Server.Port != 1080 {
|
|
t.Errorf("Expected Server.Port 1080, got %d", config.Server.Port)
|
|
}
|
|
|
|
if config.Server.Username != "testuser" {
|
|
t.Errorf("Expected Server.Username 'testuser', got '%s'", config.Server.Username)
|
|
}
|
|
|
|
if config.Server.Password != "testpass" {
|
|
t.Errorf("Expected Server.Password 'testpass', got '%s'", config.Server.Password)
|
|
}
|
|
|
|
if config.Proxy.LocalPort != 8080 {
|
|
t.Errorf("Expected Proxy.LocalPort 8080, got %d", config.Proxy.LocalPort)
|
|
}
|
|
|
|
if config.LogLevel != "debug" {
|
|
t.Errorf("Expected LogLevel 'debug', got '%s'", config.LogLevel)
|
|
}
|
|
|
|
if config.Timeout != 60*time.Second {
|
|
t.Errorf("Expected Timeout 60s, got %v", config.Timeout)
|
|
}
|
|
}
|
|
|
|
func TestLoadConfigDefaults(t *testing.T) {
|
|
// 创建最小配置
|
|
tmpFile, err := os.CreateTemp("", "test_config_min_*.yaml")
|
|
if err != nil {
|
|
t.Fatal(err)
|
|
}
|
|
defer os.Remove(tmpFile.Name())
|
|
|
|
minimalConfig := `
|
|
serviceType: client
|
|
server:
|
|
address: 127.0.0.1
|
|
port: 1080
|
|
username: user
|
|
password: pass
|
|
`
|
|
|
|
if _, err := tmpFile.WriteString(minimalConfig); err != nil {
|
|
t.Fatal(err)
|
|
}
|
|
tmpFile.Close()
|
|
|
|
config, err := LoadConfig(tmpFile.Name())
|
|
if err != nil {
|
|
t.Fatalf("LoadConfig failed: %v", err)
|
|
}
|
|
|
|
// 验证默认值
|
|
if config.LogLevel != "info" {
|
|
t.Errorf("Expected default LogLevel 'info', got '%s'", config.LogLevel)
|
|
}
|
|
|
|
if config.Timeout != 30*time.Second {
|
|
t.Errorf("Expected default Timeout 30s, got %v", config.Timeout)
|
|
}
|
|
|
|
if config.Proxy.LocalPort != 8080 {
|
|
t.Errorf("Expected default Proxy.LocalPort 8080, got %d", config.Proxy.LocalPort)
|
|
}
|
|
}
|
|
|
|
func TestGetServerAddr(t *testing.T) {
|
|
config := &Config{
|
|
Server: Server{
|
|
Address: "example.com",
|
|
Port: 1080,
|
|
},
|
|
}
|
|
|
|
expected := "example.com:1080"
|
|
result := config.GetServerAddr()
|
|
|
|
if result != expected {
|
|
t.Errorf("Expected '%s', got '%s'", expected, result)
|
|
}
|
|
}
|
|
|
|
func TestValidate(t *testing.T) {
|
|
tests := []struct {
|
|
name string
|
|
config *Config
|
|
wantErr bool
|
|
}{
|
|
{
|
|
name: "valid config",
|
|
config: &Config{
|
|
Server: Server{
|
|
Address: "127.0.0.1",
|
|
Port: 1080,
|
|
},
|
|
Proxy: Proxy{
|
|
Mode: "http",
|
|
},
|
|
},
|
|
wantErr: false,
|
|
},
|
|
{
|
|
name: "empty address",
|
|
config: &Config{
|
|
Server: Server{
|
|
Address: "",
|
|
Port: 1080,
|
|
},
|
|
Proxy: Proxy{
|
|
Mode: "http",
|
|
},
|
|
},
|
|
wantErr: true,
|
|
},
|
|
{
|
|
name: "invalid port",
|
|
config: &Config{
|
|
Server: Server{
|
|
Address: "127.0.0.1",
|
|
Port: -1,
|
|
},
|
|
Proxy: Proxy{
|
|
Mode: "http",
|
|
},
|
|
},
|
|
wantErr: true,
|
|
},
|
|
{
|
|
name: "invalid mode",
|
|
config: &Config{
|
|
Server: Server{
|
|
Address: "127.0.0.1",
|
|
Port: 1080,
|
|
},
|
|
Proxy: Proxy{
|
|
Mode: "invalid",
|
|
},
|
|
},
|
|
wantErr: true,
|
|
},
|
|
}
|
|
|
|
for _, tt := range tests {
|
|
t.Run(tt.name, func(t *testing.T) {
|
|
err := tt.config.Validate()
|
|
if (err != nil) != tt.wantErr {
|
|
t.Errorf("Validate() error = %v, wantErr %v", err, tt.wantErr)
|
|
}
|
|
})
|
|
}
|
|
}
|
|
|