mirror of
https://github.com/shadow1ng/fscan.git
synced 2025-09-14 05:56:46 +08:00

主要变更: - 移除ParseIP.go和ParsePort.go包装层,统一使用parsers模块 - 精简i18n.go国际化系统,移除日俄语言支持,减少79%代码量 - 简化Variables.go配置同步机制,移除未使用的SyncToConfig函数 - 优化LegacyParser.go兼容层,移除扩展功能函数 - 修复结构体字面量和测试用例,提升代码质量 性能优化: - 减少总代码量约2000行,提升维护性 - 保持100%API兼容性,现有调用无需修改 - 优化系统启动速度和内存使用 - 统一解析逻辑,消除功能重复 测试验证: - 全项目编译通过,无错误或警告 - 所有核心功能正常工作 - 单元测试和回归测试通过 - IP/端口解析功能完整保留
142 lines
3.0 KiB
Go
142 lines
3.0 KiB
Go
package parsers
|
|
|
|
import (
|
|
"reflect"
|
|
"testing"
|
|
)
|
|
|
|
// TestParsePort 测试端口解析功能
|
|
func TestParsePort(t *testing.T) {
|
|
tests := []struct {
|
|
name string
|
|
input string
|
|
expected []int
|
|
}{
|
|
{
|
|
name: "Simple ports",
|
|
input: "80,443,22",
|
|
expected: []int{22, 80, 443}, // sorted
|
|
},
|
|
{
|
|
name: "Port range",
|
|
input: "80-85",
|
|
expected: []int{80, 81, 82, 83, 84, 85},
|
|
},
|
|
{
|
|
name: "Mixed ports and ranges",
|
|
input: "22,80-82,443",
|
|
expected: []int{22, 80, 81, 82, 443}, // sorted
|
|
},
|
|
{
|
|
name: "Predefined group",
|
|
input: "web",
|
|
expected: []int{80, 443, 8080, 8443}, // subset of web ports
|
|
},
|
|
{
|
|
name: "Empty string",
|
|
input: "",
|
|
expected: nil,
|
|
},
|
|
}
|
|
|
|
for _, tt := range tests {
|
|
t.Run(tt.name, func(t *testing.T) {
|
|
result := ParsePort(tt.input)
|
|
|
|
// For predefined groups, we just check if common ports are included
|
|
if tt.name == "Predefined group" {
|
|
hasExpectedPorts := false
|
|
for _, expectedPort := range tt.expected {
|
|
for _, resultPort := range result {
|
|
if resultPort == expectedPort {
|
|
hasExpectedPorts = true
|
|
break
|
|
}
|
|
}
|
|
if !hasExpectedPorts {
|
|
break
|
|
}
|
|
}
|
|
if !hasExpectedPorts {
|
|
t.Errorf("ParsePort(%s) does not contain expected common web ports %v", tt.input, tt.expected)
|
|
}
|
|
} else {
|
|
if !reflect.DeepEqual(result, tt.expected) {
|
|
t.Errorf("ParsePort(%s) = %v, expected %v", tt.input, result, tt.expected)
|
|
}
|
|
}
|
|
})
|
|
}
|
|
}
|
|
|
|
// TestParseIP 测试IP解析功能
|
|
func TestParseIP(t *testing.T) {
|
|
tests := []struct {
|
|
name string
|
|
input string
|
|
expected int // expected number of IPs
|
|
}{
|
|
{
|
|
name: "Single IP",
|
|
input: "192.168.1.1",
|
|
expected: 1,
|
|
},
|
|
{
|
|
name: "CIDR notation",
|
|
input: "192.168.1.0/30",
|
|
expected: 4, // 192.168.1.0-3
|
|
},
|
|
{
|
|
name: "IP range",
|
|
input: "192.168.1.1-192.168.1.3",
|
|
expected: 3,
|
|
},
|
|
}
|
|
|
|
for _, tt := range tests {
|
|
t.Run(tt.name, func(t *testing.T) {
|
|
result, err := ParseIP(tt.input, "", "")
|
|
if err != nil {
|
|
t.Errorf("ParseIP(%s) returned error: %v", tt.input, err)
|
|
return
|
|
}
|
|
if len(result) != tt.expected {
|
|
t.Errorf("ParseIP(%s) returned %d IPs, expected %d", tt.input, len(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
|
|
},
|
|
{
|
|
name: "Empty string",
|
|
input: "",
|
|
expected: []int{},
|
|
},
|
|
{
|
|
name: "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)
|
|
}
|
|
})
|
|
}
|
|
} |