mirror of
https://github.com/anchore/syft.git
synced 2026-10-31 06:26:30 +01:00
Syft can get CPEs from several source, including generating them based on package data, finding them in the NVD CPE dictionary, or finding them declared in a manifest or existing SBOM. Record where Syft got CPEs so that consumers of SBOMs can reason about how trustworthy they are. Signed-off-by: Will Murphy <will.murphy@anchore.com>
99 lines
3.6 KiB
Go
99 lines
3.6 KiB
Go
package cpe
|
|
|
|
import (
|
|
"sort"
|
|
"testing"
|
|
|
|
"github.com/stretchr/testify/assert"
|
|
)
|
|
|
|
func Test_BySpecificity(t *testing.T) {
|
|
tests := []struct {
|
|
name string
|
|
input []Attributes
|
|
expected []Attributes
|
|
}{
|
|
{
|
|
name: "sort strictly by wfn *",
|
|
input: []Attributes{
|
|
MustAttributes("cpe:2.3:a:*:package:1:*:*:*:*:*:*:*"),
|
|
MustAttributes("cpe:2.3:a:some:package:1:*:*:*:*:*:*:*"),
|
|
MustAttributes("cpe:2.3:a:*:package:1:*:*:*:*:some:*:*"),
|
|
MustAttributes("cpe:2.3:a:some:package:1:*:*:*:*:some:*:*"),
|
|
MustAttributes("cpe:2.3:a:some:package:*:*:*:*:*:*:*:*"),
|
|
},
|
|
expected: []Attributes{
|
|
MustAttributes("cpe:2.3:a:some:package:1:*:*:*:*:some:*:*"),
|
|
MustAttributes("cpe:2.3:a:some:package:1:*:*:*:*:*:*:*"),
|
|
MustAttributes("cpe:2.3:a:some:package:*:*:*:*:*:*:*:*"),
|
|
MustAttributes("cpe:2.3:a:*:package:1:*:*:*:*:some:*:*"),
|
|
MustAttributes("cpe:2.3:a:*:package:1:*:*:*:*:*:*:*"),
|
|
},
|
|
},
|
|
{
|
|
name: "sort strictly by field length",
|
|
input: []Attributes{
|
|
MustAttributes("cpe:2.3:a:1:22:1:*:*:*:*:1:*:*"),
|
|
MustAttributes("cpe:2.3:a:55555:1:1:*:*:*:*:1:*:*"),
|
|
MustAttributes("cpe:2.3:a:1:1:333:*:*:*:*:1:*:*"),
|
|
MustAttributes("cpe:2.3:a:1:666666:1:*:*:*:*:1:*:*"),
|
|
MustAttributes("cpe:2.3:a:1:1:1:*:*:*:*:1:*:*"),
|
|
MustAttributes("cpe:2.3:a:1:1:1:*:*:*:*:4444:*:*"),
|
|
},
|
|
expected: []Attributes{
|
|
MustAttributes("cpe:2.3:a:1:666666:1:*:*:*:*:1:*:*"),
|
|
MustAttributes("cpe:2.3:a:55555:1:1:*:*:*:*:1:*:*"),
|
|
MustAttributes("cpe:2.3:a:1:1:1:*:*:*:*:4444:*:*"),
|
|
MustAttributes("cpe:2.3:a:1:1:333:*:*:*:*:1:*:*"),
|
|
MustAttributes("cpe:2.3:a:1:22:1:*:*:*:*:1:*:*"),
|
|
MustAttributes("cpe:2.3:a:1:1:1:*:*:*:*:1:*:*"),
|
|
},
|
|
},
|
|
{
|
|
name: "sort by mix of field length and specificity",
|
|
input: []Attributes{
|
|
MustAttributes("cpe:2.3:a:1:666666:*:*:*:*:*:1:*:*"),
|
|
MustAttributes("cpe:2.3:a:*:1:1:*:*:*:*:4444:*:*"),
|
|
MustAttributes("cpe:2.3:a:1:*:333:*:*:*:*:*:*:*"),
|
|
MustAttributes("cpe:2.3:a:1:1:1:*:*:*:*:1:*:*"),
|
|
MustAttributes("cpe:2.3:a:1:22:1:*:*:*:*:1:*:*"),
|
|
MustAttributes("cpe:2.3:a:55555:1:1:*:*:*:*:1:*:*"),
|
|
},
|
|
expected: []Attributes{
|
|
MustAttributes("cpe:2.3:a:55555:1:1:*:*:*:*:1:*:*"),
|
|
MustAttributes("cpe:2.3:a:1:22:1:*:*:*:*:1:*:*"),
|
|
MustAttributes("cpe:2.3:a:1:1:1:*:*:*:*:1:*:*"),
|
|
MustAttributes("cpe:2.3:a:1:666666:*:*:*:*:*:1:*:*"),
|
|
MustAttributes("cpe:2.3:a:*:1:1:*:*:*:*:4444:*:*"),
|
|
MustAttributes("cpe:2.3:a:1:*:333:*:*:*:*:*:*:*"),
|
|
},
|
|
},
|
|
{
|
|
name: "sort by mix of field length, specificity, dash",
|
|
input: []Attributes{
|
|
MustAttributes("cpe:2.3:a:alpine:alpine_keys:2.3-r1:*:*:*:*:*:*:*"),
|
|
MustAttributes("cpe:2.3:a:alpine_keys:alpine_keys:2.3-r1:*:*:*:*:*:*:*"),
|
|
MustAttributes("cpe:2.3:a:alpine-keys:alpine_keys:2.3-r1:*:*:*:*:*:*:*"),
|
|
MustAttributes("cpe:2.3:a:alpine:alpine-keys:2.3-r1:*:*:*:*:*:*:*"),
|
|
MustAttributes("cpe:2.3:a:alpine-keys:alpine-keys:2.3-r1:*:*:*:*:*:*:*"),
|
|
MustAttributes("cpe:2.3:a:alpine_keys:alpine-keys:2.3-r1:*:*:*:*:*:*:*"),
|
|
},
|
|
expected: []Attributes{
|
|
MustAttributes("cpe:2.3:a:alpine-keys:alpine-keys:2.3-r1:*:*:*:*:*:*:*"),
|
|
MustAttributes("cpe:2.3:a:alpine-keys:alpine_keys:2.3-r1:*:*:*:*:*:*:*"),
|
|
MustAttributes("cpe:2.3:a:alpine_keys:alpine-keys:2.3-r1:*:*:*:*:*:*:*"),
|
|
MustAttributes("cpe:2.3:a:alpine_keys:alpine_keys:2.3-r1:*:*:*:*:*:*:*"),
|
|
MustAttributes("cpe:2.3:a:alpine:alpine-keys:2.3-r1:*:*:*:*:*:*:*"),
|
|
MustAttributes("cpe:2.3:a:alpine:alpine_keys:2.3-r1:*:*:*:*:*:*:*"),
|
|
},
|
|
},
|
|
}
|
|
|
|
for _, test := range tests {
|
|
t.Run(test.name, func(t *testing.T) {
|
|
sort.Sort(BySpecificity(test.input))
|
|
assert.Equal(t, test.expected, test.input)
|
|
})
|
|
}
|
|
}
|