Skip to content

Commit

Permalink
Add error context
Browse files Browse the repository at this point in the history
Signed-off-by: Miaha <[email protected]>
  • Loading branch information
Miaha Cybersec committed May 22, 2024
1 parent c9b0c2c commit 5bf917f
Show file tree
Hide file tree
Showing 2 changed files with 51 additions and 19 deletions.
6 changes: 3 additions & 3 deletions pkg/pkgmgr/rpm.go
Original file line number Diff line number Diff line change
Expand Up @@ -82,19 +82,19 @@ func (st rpmDBType) String() string {

// Depending on go-rpm-version lib for RPM version comparison rules.
func isValidRPMVersion(v string) bool { // nolint:revive
err := isValidPackage(v)
err := isValidVersion(v)
return err == nil
}

func isValidPackage(ver string) error {
func isValidVersion(ver string) error {
if !unicode.IsDigit(rune(ver[0])) {
return errors.New("upstream_version must start with digit")
}

allowedSymbols := ".-+~:_"
for _, s := range ver {
if !unicode.IsDigit(s) && !unicode.IsLetter(s) && !strings.ContainsRune(allowedSymbols, s) {
return errors.New("upstream_version includes invalid character")
return fmt.Errorf("upstream_version %s includes invalid character %q", ver, s)
}
}
return nil
Expand Down
64 changes: 48 additions & 16 deletions pkg/pkgmgr/rpm_test.go
Original file line number Diff line number Diff line change
Expand Up @@ -359,27 +359,59 @@ func Test_rpmManager_GetPackageType(t *testing.T) {
}
}

func TestIsValidPackage(t *testing.T) {
func TestIsValidVersion(t *testing.T) {
tests := []struct {
name string
ver string
errMsg string
testName string
pkgVersion string
expectedErr string
}{
{name: "Valid version - numbers and dot", ver: "1.2.3.4", errMsg: ""},
{name: "Valid version - with hyphen", ver: "1.2.3-beta", errMsg: ""},
{name: "Valid version - with underscore", ver: "2_0_0", errMsg: ""},
{name: "Valid version - with tilde", ver: "3.0.1~rc1", errMsg: ""},
{name: "Valid version - with colon", ver: "2:9.0.1314-1.amzn2.0.1", errMsg: ""},
{name: "Invalid version - starts with letter", ver: "a1.2.3", errMsg: "upstream_version must start with digit"},
{name: "Invalid version - with space", ver: "1.2.3 with fix", errMsg: "upstream_version includes invalid character"},
{name: "Invalid version - with special character", ver: "1.2.3@", errMsg: "upstream_version includes invalid character"},
{
testName: "Valid version, numbers and dot",
pkgVersion: "1.2.3.4",
expectedErr: "",
},
{
testName: "Valid version, with hyphen",
pkgVersion: "1.2.3-beta",
expectedErr: "",
},
{
testName: "Valid version, with underscore",
pkgVersion: "2_0_0",
expectedErr: "",
},
{
testName: "Valid version, with tilde",
pkgVersion: "3.0.1~rc1",
expectedErr: "",
},
{
testName: "Valid version, with colon",
pkgVersion: "2:9.0.1314-1.amzn2.0.1",
expectedErr: "",
},
{
testName: "Invalid version, starts with letter",
pkgVersion: "a1.2.3",
expectedErr: "upstream_version must start with digit",
},
{
testName: "Invalid version, has spaces",
pkgVersion: "1.2.3 with fix",
expectedErr: "upstream_version 1.2.3 with fix includes invalid character ' '",
},
{
testName: "Invalid version, has special character",
pkgVersion: "1.2.3@",
expectedErr: "upstream_version 1.2.3@ includes invalid character '@'",
},
}

for _, tt := range tests {
t.Run(tt.name, func(t *testing.T) {
err := isValidPackage(tt.ver)
if (err != nil && err.Error() != tt.errMsg) || (err == nil && tt.errMsg != "") {
t.Errorf("isValidPackage(%q) error = %v, want %v", tt.ver, err, tt.errMsg)
t.Run(tt.testName, func(t *testing.T) {
err := isValidVersion(tt.pkgVersion)
if (err != nil && err.Error() != tt.expectedErr) || (err == nil && tt.expectedErr != "") {
t.Errorf("isValidPackage(%q) error = %v, want %v", tt.pkgVersion, err, tt.expectedErr)
}
})
}
Expand Down

0 comments on commit 5bf917f

Please sign in to comment.