mirror of
https://github.com/aquasecurity/trivy.git
synced 2025-12-21 23:00:42 -08:00
* refactor(docker_conf): rename and remove unnecessary options * feat(rpc): define new API * fix(cli): change default timeout * fix(import): fix package names * refactor(vulnerability): remove old mock * refactor(utils): remove un-needed functions * feat(cache): implement cache communicating with a server * refactor(scan): separate scan function as local scanner * test(scanner): add tests for ScanImage * refactor(scan): remove unused options * test(vulnerability): generate mock * refactor(server): split a file * feat(server): implement new RPC server * feat(client): implement new RPC client * fix(cache): use new cache interface * fix(standalone): use new scanner * fix(client): use new scanner * fix(server): pass cache * test(integration): make sure an error is not nil before calling the method * fix(mod): update dependencies * test(integration): ensure the image load finishes * feat(docker): support DOCKER_HOST and DOCKER_CERT_PATH * chore(mod): update dependencies * refactor(rpc): remove old client * feat(server): support old API for backward compatibility * fix(server): check a schema version of JSON cache * fix(rpc): add a version to packages * feat(rpc): add PutImage * test: rename expectations * refactor(cache): rename LayerCache to ImageCache * refactor: rename ImageInfo to ImageReference * fix(applier): pass image_id to ApplyLayer * feat(cache): handle image cache * chore(mod): update dependencies * refactor(server): pass only config * feat(cli): add -removed-pkgs option * refactor(err): wrap errors
312 lines
6.5 KiB
Go
312 lines
6.5 KiB
Go
package rpc
|
|
|
|
import (
|
|
"os"
|
|
"testing"
|
|
|
|
"github.com/aquasecurity/trivy/rpc/common"
|
|
|
|
ftypes "github.com/aquasecurity/fanal/types"
|
|
|
|
"github.com/aquasecurity/trivy/pkg/log"
|
|
|
|
ptypes "github.com/aquasecurity/go-dep-parser/pkg/types"
|
|
dbTypes "github.com/aquasecurity/trivy-db/pkg/types"
|
|
"github.com/aquasecurity/trivy/pkg/types"
|
|
"github.com/stretchr/testify/assert"
|
|
)
|
|
|
|
func TestMain(m *testing.M) {
|
|
log.InitLogger(false, false)
|
|
code := m.Run()
|
|
os.Exit(code)
|
|
}
|
|
|
|
func TestConvertToRpcPkgs(t *testing.T) {
|
|
type args struct {
|
|
pkgs []ftypes.Package
|
|
}
|
|
tests := []struct {
|
|
name string
|
|
args args
|
|
want []*common.Package
|
|
}{
|
|
{
|
|
name: "happy path",
|
|
args: args{
|
|
pkgs: []ftypes.Package{
|
|
{
|
|
Name: "binary",
|
|
Version: "1.2.3",
|
|
Release: "1",
|
|
Epoch: 2,
|
|
Arch: "x86_64",
|
|
SrcName: "src",
|
|
SrcVersion: "1.2.3",
|
|
SrcRelease: "1",
|
|
SrcEpoch: 2,
|
|
},
|
|
},
|
|
},
|
|
want: []*common.Package{
|
|
{
|
|
Name: "binary",
|
|
Version: "1.2.3",
|
|
Release: "1",
|
|
Epoch: 2,
|
|
Arch: "x86_64",
|
|
SrcName: "src",
|
|
SrcVersion: "1.2.3",
|
|
SrcRelease: "1",
|
|
SrcEpoch: 2,
|
|
},
|
|
},
|
|
},
|
|
}
|
|
for _, tt := range tests {
|
|
t.Run(tt.name, func(t *testing.T) {
|
|
got := ConvertToRpcPkgs(tt.args.pkgs)
|
|
assert.Equal(t, tt.want, got, tt.name)
|
|
})
|
|
}
|
|
}
|
|
|
|
func TestConvertFromRpcPkgs(t *testing.T) {
|
|
type args struct {
|
|
rpcPkgs []*common.Package
|
|
}
|
|
tests := []struct {
|
|
name string
|
|
args args
|
|
want []ftypes.Package
|
|
}{
|
|
{
|
|
args: args{
|
|
rpcPkgs: []*common.Package{
|
|
{
|
|
Name: "binary",
|
|
Version: "1.2.3",
|
|
Release: "1",
|
|
Epoch: 2,
|
|
Arch: "x86_64",
|
|
SrcName: "src",
|
|
SrcVersion: "1.2.3",
|
|
SrcRelease: "1",
|
|
SrcEpoch: 2,
|
|
},
|
|
},
|
|
},
|
|
want: []ftypes.Package{
|
|
{
|
|
Name: "binary",
|
|
Version: "1.2.3",
|
|
Release: "1",
|
|
Epoch: 2,
|
|
Arch: "x86_64",
|
|
SrcName: "src",
|
|
SrcVersion: "1.2.3",
|
|
SrcRelease: "1",
|
|
SrcEpoch: 2,
|
|
},
|
|
},
|
|
},
|
|
}
|
|
for _, tt := range tests {
|
|
t.Run(tt.name, func(t *testing.T) {
|
|
got := ConvertFromRpcPkgs(tt.args.rpcPkgs)
|
|
assert.Equal(t, tt.want, got, tt.name)
|
|
})
|
|
}
|
|
}
|
|
|
|
func TestConvertFromRpcLibraries(t *testing.T) {
|
|
type args struct {
|
|
rpcLibs []*common.Library
|
|
}
|
|
tests := []struct {
|
|
name string
|
|
args args
|
|
want []ptypes.Library
|
|
}{
|
|
{
|
|
name: "happy path",
|
|
args: args{
|
|
rpcLibs: []*common.Library{
|
|
{Name: "foo", Version: "1.2.3"},
|
|
{Name: "bar", Version: "4.5.6"},
|
|
},
|
|
},
|
|
want: []ptypes.Library{
|
|
{Name: "foo", Version: "1.2.3"},
|
|
{Name: "bar", Version: "4.5.6"},
|
|
},
|
|
},
|
|
}
|
|
for _, tt := range tests {
|
|
t.Run(tt.name, func(t *testing.T) {
|
|
got := ConvertFromRpcLibraries(tt.args.rpcLibs)
|
|
assert.Equal(t, got, tt.want, tt.name)
|
|
})
|
|
}
|
|
}
|
|
|
|
func TestConvertToRpcLibraries(t *testing.T) {
|
|
type args struct {
|
|
libs []ptypes.Library
|
|
}
|
|
tests := []struct {
|
|
name string
|
|
args args
|
|
want []*common.Library
|
|
}{
|
|
{
|
|
name: "happy path",
|
|
args: args{
|
|
libs: []ptypes.Library{
|
|
{Name: "foo", Version: "1.2.3"},
|
|
{Name: "bar", Version: "4.5.6"},
|
|
},
|
|
},
|
|
want: []*common.Library{
|
|
{Name: "foo", Version: "1.2.3"},
|
|
{Name: "bar", Version: "4.5.6"},
|
|
},
|
|
},
|
|
}
|
|
for _, tt := range tests {
|
|
t.Run(tt.name, func(t *testing.T) {
|
|
got := ConvertToRpcLibraries(tt.args.libs)
|
|
assert.Equal(t, got, tt.want, tt.name)
|
|
})
|
|
}
|
|
}
|
|
|
|
func TestConvertFromRpcVulns(t *testing.T) {
|
|
type args struct {
|
|
rpcVulns []*common.Vulnerability
|
|
}
|
|
tests := []struct {
|
|
name string
|
|
args args
|
|
want []types.DetectedVulnerability
|
|
}{
|
|
{
|
|
name: "happy path",
|
|
args: args{
|
|
rpcVulns: []*common.Vulnerability{
|
|
{
|
|
VulnerabilityId: "CVE-2019-0001",
|
|
PkgName: "foo",
|
|
InstalledVersion: "1.2.3",
|
|
FixedVersion: "1.2.4",
|
|
Title: "DoS",
|
|
Description: "Denial of Service",
|
|
Severity: common.Severity_CRITICAL,
|
|
References: []string{"http://example.com"},
|
|
},
|
|
},
|
|
},
|
|
want: []types.DetectedVulnerability{
|
|
{
|
|
VulnerabilityID: "CVE-2019-0001",
|
|
PkgName: "foo",
|
|
InstalledVersion: "1.2.3",
|
|
FixedVersion: "1.2.4",
|
|
Vulnerability: dbTypes.Vulnerability{
|
|
Title: "DoS",
|
|
Description: "Denial of Service",
|
|
Severity: "CRITICAL",
|
|
References: []string{"http://example.com"},
|
|
},
|
|
},
|
|
},
|
|
},
|
|
}
|
|
for _, tt := range tests {
|
|
t.Run(tt.name, func(t *testing.T) {
|
|
got := ConvertFromRpcVulns(tt.args.rpcVulns)
|
|
assert.Equal(t, got, tt.want, tt.name)
|
|
})
|
|
}
|
|
}
|
|
|
|
func TestConvertToRpcVulns(t *testing.T) {
|
|
type args struct {
|
|
vulns []types.DetectedVulnerability
|
|
}
|
|
tests := []struct {
|
|
name string
|
|
args args
|
|
want []*common.Vulnerability
|
|
}{
|
|
{
|
|
name: "happy path",
|
|
args: args{
|
|
vulns: []types.DetectedVulnerability{
|
|
{
|
|
VulnerabilityID: "CVE-2019-0001",
|
|
PkgName: "foo",
|
|
InstalledVersion: "1.2.3",
|
|
FixedVersion: "1.2.4",
|
|
Vulnerability: dbTypes.Vulnerability{
|
|
Title: "DoS",
|
|
Description: "Denial of Service",
|
|
Severity: "MEDIUM",
|
|
References: []string{"http://example.com"},
|
|
},
|
|
},
|
|
},
|
|
},
|
|
want: []*common.Vulnerability{
|
|
{
|
|
VulnerabilityId: "CVE-2019-0001",
|
|
PkgName: "foo",
|
|
InstalledVersion: "1.2.3",
|
|
FixedVersion: "1.2.4",
|
|
Title: "DoS",
|
|
Description: "Denial of Service",
|
|
Severity: common.Severity_MEDIUM,
|
|
References: []string{"http://example.com"},
|
|
},
|
|
},
|
|
},
|
|
{
|
|
name: "invalid severity",
|
|
args: args{
|
|
vulns: []types.DetectedVulnerability{
|
|
{
|
|
VulnerabilityID: "CVE-2019-0002",
|
|
PkgName: "bar",
|
|
InstalledVersion: "1.2.3",
|
|
FixedVersion: "1.2.4",
|
|
Vulnerability: dbTypes.Vulnerability{
|
|
Title: "DoS",
|
|
Description: "Denial of Service",
|
|
Severity: "INVALID",
|
|
References: []string{"http://example.com"},
|
|
},
|
|
},
|
|
},
|
|
},
|
|
want: []*common.Vulnerability{
|
|
{
|
|
VulnerabilityId: "CVE-2019-0002",
|
|
PkgName: "bar",
|
|
InstalledVersion: "1.2.3",
|
|
FixedVersion: "1.2.4",
|
|
Title: "DoS",
|
|
Description: "Denial of Service",
|
|
Severity: common.Severity_UNKNOWN,
|
|
References: []string{"http://example.com"},
|
|
},
|
|
},
|
|
},
|
|
}
|
|
for _, tt := range tests {
|
|
t.Run(tt.name, func(t *testing.T) {
|
|
got := ConvertToRpcVulns(tt.args.vulns)
|
|
assert.Equal(t, got, tt.want, tt.name)
|
|
})
|
|
}
|
|
}
|