Files
trivy/pkg/detector/library/comparer/compare_test.go
Teppei Fukuda b6d5b82c48 fix: version comparison (#740)
* feat: add comparer

* refactor: rename lang with ecosystem

* feat(bundler): add comparer

* feat(node): add comparer

* feat(bundler): integrate comparer

* feat(cargo): integrate comparer

* feat(composer): add comparer

* feat(ghsa): integrate comparer

* feat(node): integrate comparer

* feat(python): integrate comparer

* test(bundler): add tests

* test(cargo): add tests

* test(composer): add tests

* test(ghsa): add tests

* test(node): add tests

* test(python): add tests

* refactor(utils): remove unnecessary functions

* test(utils): add tests

* test: rename bucket prefixes

* fix(detect): use string

* chore: update dependencies

* docs: add comments

* fix(cargo): handle unpatched vulnerability

* test(db): update trivy-db for integration tests

* test(integration): update a golden file

* test(cargo): Add a case for missing patched version

Signed-off-by: Simarpreet Singh <simar@linux.com>

* refactor(advisory): update comments

* refactor(node/advisory): change the receiver

* chore(mod): update dependencies

* refactor(comparer): unexport MatchVersion

* refactor: fix maligned structs

* test(node): add empty value

* refactor

* refactor: sort imports

* chore(mod): update trivy-db

Co-authored-by: Simarpreet Singh <simar@linux.com>
2020-11-17 11:38:58 +02:00

97 lines
2.0 KiB
Go

package comparer_test
import (
"testing"
"github.com/stretchr/testify/assert"
"github.com/aquasecurity/trivy-db/pkg/types"
"github.com/aquasecurity/trivy/pkg/detector/library/comparer"
"github.com/aquasecurity/trivy/pkg/log"
)
func TestGenericComparer_IsVulnerable(t *testing.T) {
type args struct {
ver string
advisory types.Advisory
}
tests := []struct {
name string
args args
want bool
}{
{
name: "happy path",
args: args{
ver: "1.2.3",
advisory: types.Advisory{
VulnerableVersions: []string{"<=1.0"},
PatchedVersions: []string{">=1.1"},
},
},
},
{
name: "no patch",
args: args{
ver: "1.2.3",
advisory: types.Advisory{
VulnerableVersions: []string{"<=99.999.99999"},
PatchedVersions: []string{"<0.0.0"},
},
},
want: true,
},
{
name: "pre-release",
args: args{
ver: "1.2.2-alpha",
advisory: types.Advisory{
VulnerableVersions: []string{"<=1.2.2"},
PatchedVersions: []string{">=1.2.2"},
},
},
want: true,
},
{
name: "multiple constraints",
args: args{
ver: "2.0.0",
advisory: types.Advisory{
VulnerableVersions: []string{">=1.7.0 <1.7.16", ">=1.8.0 <1.8.8", ">=2.0.0 <2.0.8", ">=3.0.0-beta.1 <3.0.0-beta.7"},
PatchedVersions: []string{">=3.0.0-beta.7", ">=2.0.8 <3.0.0-beta.1", ">=1.8.8 <2.0.0", ">=1.7.16 <1.8.0"},
},
},
want: true,
},
{
name: "invalid version",
args: args{
ver: "1.2..4",
advisory: types.Advisory{
VulnerableVersions: []string{"<1.0.0"},
},
},
want: false,
},
{
name: "improper constraint",
args: args{
ver: "1.2.3",
advisory: types.Advisory{
VulnerableVersions: []string{"*"},
PatchedVersions: nil,
},
},
want: false,
},
}
log.InitLogger(false, false)
for _, tt := range tests {
t.Run(tt.name, func(t *testing.T) {
v := comparer.GenericComparer{}
got := v.IsVulnerable(tt.args.ver, tt.args.advisory)
assert.Equal(t, tt.want, got)
})
}
}