test: 补充单元测试覆盖率 29.9% → 36.6%

新建 18 个测试文件,追加 30 个已有测试文件,覆盖协议解析、
错误分类、CEL 表达式求值、YAML 反序列化、字节编码等纯函数。
This commit is contained in:
ZacharyZcR
2026-06-17 12:51:41 +08:00
parent 6eff1d5ccf
commit 0612255893
48 changed files with 7556 additions and 1 deletions
+69
View File
@@ -529,3 +529,72 @@ func TestExtras_ToMap_EmptyStringFiltering(t *testing.T) {
}
})
}
// =============================================================================
// ParseVersionInfo 测试
// =============================================================================
func TestParseVersionInfo(t *testing.T) {
tests := []struct {
name string
versionInfo string
foundItems []string
wantVP string // VendorProduct
wantVer string // Version
wantCPE string
}{
{
name: "只有product-斜线分隔符",
versionInfo: " p/Apache/",
wantVP: "Apache",
},
{
name: "product和version-斜线分隔符",
versionInfo: " p/nginx/ v/1.18.0/",
wantVP: "nginx",
wantVer: "1.18.0",
},
{
name: "pipe分隔符",
versionInfo: " p|OpenSSH| v|8.2p1|",
wantVP: "OpenSSH",
wantVer: "8.2p1",
},
{
name: "含$1占位符替换后解析",
versionInfo: " p/OpenSSH/ v/$1/",
foundItems: []string{"8.2p1"},
wantVP: "OpenSSH",
wantVer: "8.2p1",
},
{
name: "CPE解析",
versionInfo: " cpe:/a:apache:httpd:2.4.41",
wantCPE: "a:apache:httpd:2.4.41",
},
{
name: "空VersionInfo返回全空Extras",
versionInfo: "",
},
}
for _, tt := range tests {
t.Run(tt.name, func(t *testing.T) {
m := &Match{
VersionInfo: tt.versionInfo,
FoundItems: tt.foundItems,
}
got := m.ParseVersionInfo(nil)
if got.VendorProduct != tt.wantVP {
t.Errorf("VendorProduct = %q, want %q", got.VendorProduct, tt.wantVP)
}
if got.Version != tt.wantVer {
t.Errorf("Version = %q, want %q", got.Version, tt.wantVer)
}
if got.CPE != tt.wantCPE {
t.Errorf("CPE = %q, want %q", got.CPE, tt.wantCPE)
}
})
}
}