fscan/Common/Ports_test.go
ZacharyZcR 39fc57f5a5 refactor: 深度重构Common包,移除冗余代码和优化架构
主要变更:
- 移除ParseIP.go和ParsePort.go包装层,统一使用parsers模块
- 精简i18n.go国际化系统,移除日俄语言支持,减少79%代码量
- 简化Variables.go配置同步机制,移除未使用的SyncToConfig函数
- 优化LegacyParser.go兼容层,移除扩展功能函数
- 修复结构体字面量和测试用例,提升代码质量

性能优化:
- 减少总代码量约2000行,提升维护性
- 保持100%API兼容性,现有调用无需修改
- 优化系统启动速度和内存使用
- 统一解析逻辑,消除功能重复

测试验证:
- 全项目编译通过,无错误或警告
- 所有核心功能正常工作
- 单元测试和回归测试通过
- IP/端口解析功能完整保留
2025-08-05 19:19:40 +08:00

117 lines
2.5 KiB
Go

package Common
import (
"reflect"
"testing"
)
// TestIsValidPort 测试端口号验证功能
func TestIsValidPort(t *testing.T) {
tests := []struct {
name string
port int
expected bool
}{
{"Valid port 1", 1, true},
{"Valid port 80", 80, true},
{"Valid port 443", 443, true},
{"Valid port 65535", 65535, true},
{"Invalid port 0", 0, false},
{"Invalid port -1", -1, false},
{"Invalid port 65536", 65536, false},
{"Invalid port 100000", 100000, false},
}
for _, tt := range tests {
t.Run(tt.name, func(t *testing.T) {
result := IsValidPort(tt.port)
if result != tt.expected {
t.Errorf("IsValidPort(%d) = %v, expected %v", tt.port, result, tt.expected)
}
})
}
}
// TestRemoveDuplicates 测试去重功能
func TestRemoveDuplicates(t *testing.T) {
tests := []struct {
name string
input []int
expected []int
}{
{
name: "No duplicates",
input: []int{80, 443, 22},
expected: []int{22, 80, 443}, // sorted
},
{
name: "With duplicates",
input: []int{80, 443, 80, 22, 443, 443},
expected: []int{22, 80, 443}, // sorted and deduplicated
},
{
name: "Empty input",
input: []int{},
expected: []int{},
},
{
name: "All same values",
input: []int{80, 80, 80},
expected: []int{80},
},
}
for _, tt := range tests {
t.Run(tt.name, func(t *testing.T) {
result := removeDuplicates(tt.input)
if !reflect.DeepEqual(result, tt.expected) {
t.Errorf("removeDuplicates(%v) = %v, expected %v", tt.input, result, tt.expected)
}
})
}
}
// TestParsePortsFromString 测试端口解析功能
func TestParsePortsFromString(t *testing.T) {
tests := []struct {
name string
input string
expected []int
}{
{
name: "Valid ports",
input: "80,443,22",
expected: []int{22, 80, 443}, // sorted and deduplicated
},
{
name: "Valid ports with duplicates",
input: "80,443,80,22",
expected: []int{22, 80, 443}, // sorted and deduplicated
},
{
name: "Empty string",
input: "",
expected: []int{},
},
{
name: "Mixed valid and invalid",
input: "80,invalid,443,0,22",
expected: []int{22, 80, 443}, // only valid ports
},
{
name: "Ports with spaces",
input: " 80 , 443 , 22 ",
expected: []int{22, 80, 443},
},
}
for _, tt := range tests {
t.Run(tt.name, func(t *testing.T) {
result := ParsePortsFromString(tt.input)
if !reflect.DeepEqual(result, tt.expected) {
t.Errorf("ParsePortsFromString(%s) = %v, expected %v", tt.input, result, tt.expected)
}
})
}
}