From 435950348475b27b9040c03fc3c160fc482bce1a Mon Sep 17 00:00:00 2001 From: Kota Kanbe Date: Wed, 6 Jan 2021 13:33:08 +0900 Subject: [PATCH] fix(redhat): possibility of false positives on RHEL (#1115) --- oval/util.go | 13 ++++++++----- oval/util_test.go | 33 +++++++++++++++++++++++++++++++++ 2 files changed, 41 insertions(+), 5 deletions(-) diff --git a/oval/util.go b/oval/util.go index 4d995075..acbe82df 100644 --- a/oval/util.go +++ b/oval/util.go @@ -378,9 +378,6 @@ func isOvalDefAffected(def ovalmodels.Definition, req request, family string, ru return false, false, "" } -var centosVerPattern = regexp.MustCompile(`\.[es]l(\d+)(?:_\d+)?(?:\.centos)?`) -var esVerPattern = regexp.MustCompile(`\.el(\d+)(?:_\d+)?`) - func lessThan(family, newVer string, packInOVAL ovalmodels.Package) (bool, error) { switch family { case config.Debian, @@ -416,8 +413,8 @@ func lessThan(family, newVer string, packInOVAL ovalmodels.Package) (bool, error case config.RedHat, config.CentOS: - vera := rpmver.NewVersion(centosVerPattern.ReplaceAllString(newVer, ".el$1")) - verb := rpmver.NewVersion(esVerPattern.ReplaceAllString(packInOVAL.Version, ".el$1")) + vera := rpmver.NewVersion(centOSVersionToRHEL(newVer)) + verb := rpmver.NewVersion(packInOVAL.Version) return vera.LessThan(verb), nil default: @@ -425,3 +422,9 @@ func lessThan(family, newVer string, packInOVAL ovalmodels.Package) (bool, error } return false, xerrors.Errorf("Package version comparison not supported: %s", family) } + +var centosVerPattern = regexp.MustCompile(`\.[es]l(\d+)(?:_\d+)?(?:\.centos)?`) + +func centOSVersionToRHEL(ver string) string { + return centosVerPattern.ReplaceAllString(ver, ".el$1") +} diff --git a/oval/util_test.go b/oval/util_test.go index e0eeab67..c8fe4a7a 100644 --- a/oval/util_test.go +++ b/oval/util_test.go @@ -1193,3 +1193,36 @@ func Test_major(t *testing.T) { } } } + +func Test_centOSVersionToRHEL(t *testing.T) { + type args struct { + ver string + } + tests := []struct { + name string + args args + want string + }{ + { + name: "remove centos.", + args: args{ + ver: "grub2-tools-2.02-0.80.el7.centos.x86_64", + }, + want: "grub2-tools-2.02-0.80.el7.x86_64", + }, + { + name: "noop", + args: args{ + ver: "grub2-tools-2.02-0.80.el7.x86_64", + }, + want: "grub2-tools-2.02-0.80.el7.x86_64", + }, + } + for _, tt := range tests { + t.Run(tt.name, func(t *testing.T) { + if got := centOSVersionToRHEL(tt.args.ver); got != tt.want { + t.Errorf("centOSVersionToRHEL() = %v, want %v", got, tt.want) + } + }) + } +}