Skip to content

Commit 913b9fc

Browse files
committed
add integrity extractor test
1 parent 3c28e4c commit 913b9fc

File tree

4 files changed

+297
-24
lines changed

4 files changed

+297
-24
lines changed

pkg/scorer/v2/engine/farmulas.go

Lines changed: 9 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -14,12 +14,20 @@
1414

1515
package engine
1616

17-
import "github.com/interlynk-io/sbomqs/pkg/scorer/v2/config"
17+
import (
18+
"fmt"
1819

20+
"github.com/interlynk-io/sbomqs/pkg/scorer/v2/config"
21+
)
22+
23+
func NoComponentsNA() string { return "N/A (no components)" }
1924
func MissingField(field string) string { return "missing " + field }
2025
func PresentField(field string) string { return "present " + field }
2126
func NonSupportedSPDXField() string { return "N/A (SPDX)" }
2227
func UnknownSpec() string { return "N/A (unknown spec)" }
28+
func CompDescription(have, total int, field string) string {
29+
return fmt.Sprintf("%d/%d have %s", have, total, field)
30+
}
2331

2432
// perComponentScore returns 10 × (have/total)
2533
func PerComponentScore(have, total int) float64 {

pkg/scorer/v2/extractors/identification.go

Lines changed: 8 additions & 7 deletions
Original file line numberDiff line numberDiff line change
@@ -30,7 +30,7 @@ func CompWithName(doc sbom.Document) config.FeatureScore {
3030
if total == 0 {
3131
return config.FeatureScore{
3232
Score: engine.PerComponentScore(0, total),
33-
Desc: "N/A (no components)",
33+
Desc: engine.NoComponentsNA(),
3434
Ignore: true,
3535
}
3636
}
@@ -40,8 +40,9 @@ func CompWithName(doc sbom.Document) config.FeatureScore {
4040
})
4141

4242
return config.FeatureScore{
43-
Score: engine.PerComponentScore(have, total),
44-
Desc: fmt.Sprintf("%d/%d have names", have, total),
43+
Score: engine.PerComponentScore(have, total),
44+
Desc: engine.CompDescription(have, total, "names"),
45+
4546
Ignore: false,
4647
}
4748
}
@@ -52,7 +53,7 @@ func CompWithVersion(doc sbom.Document) config.FeatureScore {
5253
if total == 0 {
5354
return config.FeatureScore{
5455
Score: engine.PerComponentScore(0, total),
55-
Desc: "N/A (no components)",
56+
Desc: engine.NoComponentsNA(),
5657
Ignore: true,
5758
}
5859
}
@@ -63,7 +64,7 @@ func CompWithVersion(doc sbom.Document) config.FeatureScore {
6364

6465
return config.FeatureScore{
6566
Score: engine.PerComponentScore(have, total),
66-
Desc: fmt.Sprintf("%d/%d have versions", have, total),
67+
Desc: engine.CompDescription(have, total, "versions"),
6768
Ignore: false,
6869
}
6970
}
@@ -74,7 +75,7 @@ func CompWithUniqLocalIDs(doc sbom.Document) config.FeatureScore {
7475
if total == 0 {
7576
return config.FeatureScore{
7677
Score: engine.PerComponentScore(0, total),
77-
Desc: "N/A (no components)",
78+
Desc: engine.NoComponentsNA(),
7879
Ignore: true,
7980
}
8081
}
@@ -90,7 +91,7 @@ func CompWithUniqLocalIDs(doc sbom.Document) config.FeatureScore {
9091

9192
return config.FeatureScore{
9293
Score: engine.PerComponentScore(len(have), total),
93-
Desc: fmt.Sprintf("%d/%d have unique IDs", len(have), total),
94+
Desc: engine.CompDescription(len(have), total, "unique IDs"),
9495
Ignore: false,
9596
}
9697
}

pkg/scorer/v2/extractors/integrity.go

Lines changed: 16 additions & 16 deletions
Original file line numberDiff line numberDiff line change
@@ -33,21 +33,21 @@ func CompWithSHA1Plus(doc sbom.Document) config.FeatureScore {
3333
if total == 0 {
3434
return config.FeatureScore{
3535
Score: engine.PerComponentScore(0, 0),
36-
Desc: "N/A (no components)",
36+
Desc: engine.NoComponentsNA(),
3737
Ignore: true,
3838
}
3939
}
4040

41-
withSHA1p := 0
41+
have := 0
4242
for _, comp := range comps {
4343
if hasSHA1Plus(comp) {
44-
withSHA1p++
44+
have++
4545
}
4646
}
4747

4848
return config.FeatureScore{
49-
Score: engine.PerComponentScore(withSHA1p, total),
50-
Desc: fmt.Sprintf("%d/%d have SHA-1+", withSHA1p, total),
49+
Score: engine.PerComponentScore(have, total),
50+
Desc: engine.CompDescription(have, total, "SHA-1+"),
5151
Ignore: false,
5252
}
5353
}
@@ -59,21 +59,21 @@ func CompWithSHA256Plus(doc sbom.Document) config.FeatureScore {
5959
if total == 0 {
6060
return config.FeatureScore{
6161
Score: engine.PerComponentScore(0, 0),
62-
Desc: "N/A (no components)",
62+
Desc: engine.NoComponentsNA(),
6363
Ignore: true,
6464
}
6565
}
6666

67-
with256p := 0
67+
have := 0
6868
for _, c := range comps {
6969
if hasSHA256Plus(c) {
70-
with256p++
70+
have++
7171
}
7272
}
7373

7474
return config.FeatureScore{
75-
Score: engine.PerComponentScore(with256p, total),
76-
Desc: fmt.Sprintf("%d/%d have SHA-256+", with256p, total),
75+
Score: engine.PerComponentScore(have, total),
76+
Desc: engine.CompDescription(have, total, "SHA-256+"),
7777
Ignore: false,
7878
}
7979
}
@@ -88,18 +88,18 @@ var verifySignature = common.VerifySignature
8888
// 5 = signature present but verification failed
8989
// 0 = no signature / incomplete bundle
9090
func SBOMSignature(doc sbom.Document) config.FeatureScore {
91-
s := doc.Signature()
92-
if s == nil {
91+
sig := doc.Signature()
92+
if sig == nil {
9393
return config.FeatureScore{
9494
Score: 0,
95-
Desc: "no signature",
95+
Desc: engine.MissingField("signature"),
9696
Ignore: false,
9797
}
9898
}
9999

100-
pubKeyPath := strings.TrimSpace(s.GetPublicKey())
101-
blobPath := strings.TrimSpace(s.GetBlob())
102-
sigPath := strings.TrimSpace(s.GetSigValue())
100+
pubKeyPath := strings.TrimSpace(sig.GetPublicKey())
101+
blobPath := strings.TrimSpace(sig.GetBlob())
102+
sigPath := strings.TrimSpace(sig.GetSigValue())
103103

104104
// Incomplete bundle → treat as missing
105105
if pubKeyPath == "" || blobPath == "" || sigPath == "" {

0 commit comments

Comments
 (0)