104 lines
3.2 KiB
Go
104 lines
3.2 KiB
Go
package main
|
|
|
|
import (
|
|
"os"
|
|
"testing"
|
|
)
|
|
|
|
func TestConfig_LoadSave(t *testing.T) {
|
|
// 创建临时配置文件
|
|
configFile := "test_config.json"
|
|
defer os.Remove(configFile)
|
|
|
|
// 创建新配置
|
|
config := NewConfig()
|
|
config.ConfigFile = configFile
|
|
config.LogLevel = LogLevelDebug
|
|
config.CaptureInterface = "eth0"
|
|
config.ForwardEnabled = true
|
|
config.MaxPacketSize = 4096
|
|
|
|
// 测试保存配置
|
|
if err := config.Save(); err != nil {
|
|
t.Fatalf("Failed to save config: %v", err)
|
|
}
|
|
|
|
// 创建新配置实例加载保存的配置
|
|
loadedConfig := NewConfig()
|
|
loadedConfig.ConfigFile = configFile
|
|
if err := loadedConfig.Load(); err != nil {
|
|
t.Fatalf("Failed to load config: %v", err)
|
|
}
|
|
|
|
// 验证加载的配置是否与保存的一致
|
|
if loadedConfig.LogLevel != config.LogLevel {
|
|
t.Errorf("Expected LogLevel %v, got %v", config.LogLevel, loadedConfig.LogLevel)
|
|
}
|
|
if loadedConfig.CaptureInterface != config.CaptureInterface {
|
|
t.Errorf("Expected CaptureInterface %s, got %s", config.CaptureInterface, loadedConfig.CaptureInterface)
|
|
}
|
|
if loadedConfig.ForwardEnabled != config.ForwardEnabled {
|
|
t.Errorf("Expected ForwardEnabled %v, got %v", config.ForwardEnabled, loadedConfig.ForwardEnabled)
|
|
}
|
|
if loadedConfig.MaxPacketSize != config.MaxPacketSize {
|
|
t.Errorf("Expected MaxPacketSize %d, got %d", config.MaxPacketSize, loadedConfig.MaxPacketSize)
|
|
}
|
|
}
|
|
|
|
func TestConfig_LoadDefault(t *testing.T) {
|
|
// 创建临时配置文件
|
|
configFile := "test_default_config.json"
|
|
defer os.Remove(configFile)
|
|
|
|
// 创建新配置并加载不存在的文件(应该创建默认配置)
|
|
config := NewConfig()
|
|
config.ConfigFile = configFile
|
|
|
|
if err := config.Load(); err != nil {
|
|
t.Fatalf("Failed to load default config: %v", err)
|
|
}
|
|
|
|
// 验证默认值
|
|
if config.LogLevel != LogLevelInfo {
|
|
t.Errorf("Expected default LogLevel %v, got %v", LogLevelInfo, config.LogLevel)
|
|
}
|
|
if config.CaptureInterface != "" {
|
|
t.Errorf("Expected empty default CaptureInterface, got %s", config.CaptureInterface)
|
|
}
|
|
if config.ForwardEnabled != false {
|
|
t.Errorf("Expected default ForwardEnabled false, got %v", config.ForwardEnabled)
|
|
}
|
|
if config.MaxPacketSize != 65536 {
|
|
t.Errorf("Expected default MaxPacketSize 65536, got %d", config.MaxPacketSize)
|
|
}
|
|
|
|
// 验证是否创建了配置文件
|
|
if _, err := os.Stat(configFile); os.IsNotExist(err) {
|
|
t.Error("Config file was not created")
|
|
}
|
|
}
|
|
|
|
func TestConfig_Update(t *testing.T) {
|
|
config := NewConfig()
|
|
newConfig := &Config{
|
|
LogLevel: LogLevelWarn,
|
|
CaptureInterface: "wlan0",
|
|
ForwardEnabled: true,
|
|
MaxPacketSize: 8192,
|
|
}
|
|
|
|
config.Update(newConfig)
|
|
|
|
if config.LogLevel != newConfig.LogLevel {
|
|
t.Errorf("Expected LogLevel %v, got %v", newConfig.LogLevel, config.LogLevel)
|
|
}
|
|
if config.CaptureInterface != newConfig.CaptureInterface {
|
|
t.Errorf("Expected CaptureInterface %s, got %s", newConfig.CaptureInterface, config.CaptureInterface)
|
|
}
|
|
if config.ForwardEnabled != newConfig.ForwardEnabled {
|
|
t.Errorf("Expected ForwardEnabled %v, got %v", newConfig.ForwardEnabled, config.ForwardEnabled)
|
|
}
|
|
if config.MaxPacketSize != newConfig.MaxPacketSize {
|
|
t.Errorf("Expected MaxPacketSize %d, got %d", newConfig.MaxPacketSize, config.MaxPacketSize)
|
|
}
|
|
} |