fix(debian,ubuntu): collect running kernel source package (#1935)

This commit is contained in:
MaineK00n
2024-06-06 21:20:16 +09:00
committed by GitHub
parent 5af1a22733
commit e1fab805af
9 changed files with 666 additions and 358 deletions

View File

@@ -1,7 +1,9 @@
package gost
import (
"cmp"
"reflect"
"slices"
"testing"
"time"
@@ -119,10 +121,9 @@ func TestUbuntuConvertToModel(t *testing.T) {
func Test_detect(t *testing.T) {
type args struct {
cves map[string]gostmodels.UbuntuCVE
fixed bool
srcPkg models.SrcPackage
runningKernelBinaryPkgName string
cves map[string]gostmodels.UbuntuCVE
fixed bool
srcPkg models.SrcPackage
}
tests := []struct {
name string
@@ -152,9 +153,8 @@ func Test_detect(t *testing.T) {
},
},
},
fixed: true,
srcPkg: models.SrcPackage{Name: "pkg", Version: "0.0.0-1", BinaryNames: []string{"pkg"}},
runningKernelBinaryPkgName: "",
fixed: true,
srcPkg: models.SrcPackage{Name: "pkg", Version: "0.0.0-1", BinaryNames: []string{"pkg"}},
},
want: []cveContent{
{
@@ -180,9 +180,8 @@ func Test_detect(t *testing.T) {
},
},
},
fixed: false,
srcPkg: models.SrcPackage{Name: "pkg", Version: "0.0.0-1", BinaryNames: []string{"pkg"}},
runningKernelBinaryPkgName: "",
fixed: false,
srcPkg: models.SrcPackage{Name: "pkg", Version: "0.0.0-1", BinaryNames: []string{"pkg"}},
},
want: []cveContent{
{
@@ -218,17 +217,22 @@ func Test_detect(t *testing.T) {
},
},
},
fixed: true,
srcPkg: models.SrcPackage{Name: "linux-signed", Version: "0.0.0-1", BinaryNames: []string{"linux-image-generic", "linux-headers-generic"}},
runningKernelBinaryPkgName: "linux-image-generic",
fixed: true,
srcPkg: models.SrcPackage{Name: "linux-signed", Version: "0.0.0-1", BinaryNames: []string{"linux-image-generic", "linux-headers-generic"}},
},
want: []cveContent{
{
cveContent: models.CveContent{Type: models.UbuntuAPI, CveID: "CVE-0000-0001", SourceLink: "https://ubuntu.com/security/CVE-0000-0001", References: []models.Reference{}},
fixStatuses: models.PackageFixStatuses{{
Name: "linux-image-generic",
FixedIn: "0.0.0-2",
}},
fixStatuses: models.PackageFixStatuses{
{
Name: "linux-image-generic",
FixedIn: "0.0.0-2",
},
{
Name: "linux-headers-generic",
FixedIn: "0.0.0-2",
},
},
},
},
},
@@ -255,77 +259,21 @@ func Test_detect(t *testing.T) {
},
},
},
fixed: true,
srcPkg: models.SrcPackage{Name: "linux-meta", Version: "0.0.0.1", BinaryNames: []string{"linux-image-generic", "linux-headers-generic"}},
runningKernelBinaryPkgName: "linux-image-generic",
},
want: []cveContent{
{
cveContent: models.CveContent{Type: models.UbuntuAPI, CveID: "CVE-0000-0001", SourceLink: "https://ubuntu.com/security/CVE-0000-0001", References: []models.Reference{}},
fixStatuses: models.PackageFixStatuses{{
Name: "linux-image-generic",
FixedIn: "0.0.0.2",
}},
},
fixed: true,
srcPkg: models.SrcPackage{Name: "linux-meta", Version: "0.0.0.1", BinaryNames: []string{"linux-image-generic", "linux-headers-generic"}},
},
want: nil,
},
}
for _, tt := range tests {
t.Run(tt.name, func(t *testing.T) {
if got := (Ubuntu{}).detect(tt.args.cves, tt.args.fixed, tt.args.srcPkg, tt.args.runningKernelBinaryPkgName); !reflect.DeepEqual(got, tt.want) {
got := (Ubuntu{}).detect(tt.args.cves, tt.args.fixed, tt.args.srcPkg)
for i := range got {
slices.SortFunc(got[i].fixStatuses, func(i, j models.PackageFixStatus) int { return cmp.Compare(j.Name, i.Name) })
}
if !reflect.DeepEqual(got, tt.want) {
t.Errorf("detect() = %#v, want %#v", got, tt.want)
}
})
}
}
func TestUbuntu_isKernelSourcePackage(t *testing.T) {
tests := []struct {
pkgname string
want bool
}{
{
pkgname: "linux",
want: true,
},
{
pkgname: "apt",
want: false,
},
{
pkgname: "linux-aws",
want: true,
},
{
pkgname: "linux-5.9",
want: true,
},
{
pkgname: "linux-base",
want: false,
},
{
pkgname: "apt-utils",
want: false,
},
{
pkgname: "linux-aws-edge",
want: true,
},
{
pkgname: "linux-aws-5.15",
want: true,
},
{
pkgname: "linux-lowlatency-hwe-5.15",
want: true,
},
}
for _, tt := range tests {
t.Run(tt.pkgname, func(t *testing.T) {
if got := (Ubuntu{}).isKernelSourcePackage(tt.pkgname); got != tt.want {
t.Errorf("Ubuntu.isKernelSourcePackage() = %v, want %v", got, tt.want)
}
})
}
}