From 0b0cd4c4736e557a86611bdd38372291e045f817 Mon Sep 17 00:00:00 2001 From: dragon Date: Thu, 19 Dec 2024 15:10:13 +0800 Subject: [PATCH 1/5] fix arch package ver regex --- modules/packages/arch/metadata.go | 5 +++-- 1 file changed, 3 insertions(+), 2 deletions(-) diff --git a/modules/packages/arch/metadata.go b/modules/packages/arch/metadata.go index 06a2206a36218..0c016ebd045c4 100644 --- a/modules/packages/arch/metadata.go +++ b/modules/packages/arch/metadata.go @@ -43,8 +43,9 @@ var ( ErrInvalidArchitecture = util.NewInvalidArgumentErrorf("package architecture is invalid") // https://man.archlinux.org/man/PKGBUILD.5 - namePattern = regexp.MustCompile(`\A[a-zA-Z0-9@._+-]+\z`) - versionPattern = regexp.MustCompile(`\A(?:[0-9]:)?[a-zA-Z0-9.+~]+(?:-[a-zA-Z0-9.+-~]+)?\z`) + namePattern = regexp.MustCompile(`\A[a-zA-Z0-9@._+-]+\z`) + // (epoch:pkgver-pkgrel) + versionPattern = regexp.MustCompile(`\A(?:[0-9]:)?[a-zA-Z0-9._+~]+(?:-[a-zA-Z0-9.+-_~]+)?\z`) ) type Package struct { From 1c45dc282d1a98105b9faccbcb94990c40a67bf5 Mon Sep 17 00:00:00 2001 From: Exploding Dragon Date: Thu, 19 Dec 2024 15:51:16 +0800 Subject: [PATCH 2/5] Update modules/packages/arch/metadata.go Co-authored-by: wxiaoguang --- modules/packages/arch/metadata.go | 2 +- 1 file changed, 1 insertion(+), 1 deletion(-) diff --git a/modules/packages/arch/metadata.go b/modules/packages/arch/metadata.go index 0c016ebd045c4..1b26e7273450f 100644 --- a/modules/packages/arch/metadata.go +++ b/modules/packages/arch/metadata.go @@ -45,7 +45,7 @@ var ( // https://man.archlinux.org/man/PKGBUILD.5 namePattern = regexp.MustCompile(`\A[a-zA-Z0-9@._+-]+\z`) // (epoch:pkgver-pkgrel) - versionPattern = regexp.MustCompile(`\A(?:[0-9]:)?[a-zA-Z0-9._+~]+(?:-[a-zA-Z0-9.+-_~]+)?\z`) + versionPattern = regexp.MustCompile(`\A(?:[\d]:)?[\w.+~]+(?:-[-\w.+~]+)?\z`) ) type Package struct { From 6cf07ce69930fea35e08ae3376b34ead18206854 Mon Sep 17 00:00:00 2001 From: dragon Date: Thu, 19 Dec 2024 15:58:44 +0800 Subject: [PATCH 3/5] add some tests. --- modules/packages/arch/metadata.go | 2 +- modules/packages/arch/metadata_test.go | 16 +++++++++------- 2 files changed, 10 insertions(+), 8 deletions(-) diff --git a/modules/packages/arch/metadata.go b/modules/packages/arch/metadata.go index 1b26e7273450f..3d7bdf3997f74 100644 --- a/modules/packages/arch/metadata.go +++ b/modules/packages/arch/metadata.go @@ -45,7 +45,7 @@ var ( // https://man.archlinux.org/man/PKGBUILD.5 namePattern = regexp.MustCompile(`\A[a-zA-Z0-9@._+-]+\z`) // (epoch:pkgver-pkgrel) - versionPattern = regexp.MustCompile(`\A(?:[\d]:)?[\w.+~]+(?:-[-\w.+~]+)?\z`) + versionPattern = regexp.MustCompile(`\A(?:\d:)?[\w.+~]+(?:-[-\w.+~]+)?\z`) ) type Package struct { diff --git a/modules/packages/arch/metadata_test.go b/modules/packages/arch/metadata_test.go index 37c0a553b8423..681d8efe50452 100644 --- a/modules/packages/arch/metadata_test.go +++ b/modules/packages/arch/metadata_test.go @@ -7,6 +7,7 @@ import ( "archive/tar" "bytes" "compress/gzip" + "fmt" "io" "testing" @@ -122,13 +123,14 @@ func TestParsePackageInfo(t *testing.T) { assert.ErrorIs(t, err, ErrInvalidName) }) - t.Run("InvalidVersion", func(t *testing.T) { - data := createPKGINFOContent(packageName, "") - - p, err := ParsePackageInfo(bytes.NewReader(data)) - assert.Nil(t, p) - assert.ErrorIs(t, err, ErrInvalidVersion) - }) + for _, v := range []string{"", "a:1.0.0-1", "0.0.1/1-1", "1.0.0 -1"} { + t.Run(fmt.Sprintf("InvalidVersion[%s]", v), func(t *testing.T) { + data := createPKGINFOContent(packageName, v) + p, err := ParsePackageInfo(bytes.NewReader(data)) + assert.Nil(t, p) + assert.ErrorIs(t, err, ErrInvalidVersion) + }) + } t.Run("Valid", func(t *testing.T) { data := createPKGINFOContent(packageName, packageVersion) From 846a70e113ac58650bd28c3a82f57f0d4f465927 Mon Sep 17 00:00:00 2001 From: wxiaoguang Date: Thu, 19 Dec 2024 16:19:12 +0800 Subject: [PATCH 4/5] more tests --- modules/packages/arch/metadata_test.go | 16 +++++++--------- 1 file changed, 7 insertions(+), 9 deletions(-) diff --git a/modules/packages/arch/metadata_test.go b/modules/packages/arch/metadata_test.go index 681d8efe50452..15ab8d90c3a47 100644 --- a/modules/packages/arch/metadata_test.go +++ b/modules/packages/arch/metadata_test.go @@ -7,7 +7,6 @@ import ( "archive/tar" "bytes" "compress/gzip" - "fmt" "io" "testing" @@ -123,14 +122,13 @@ func TestParsePackageInfo(t *testing.T) { assert.ErrorIs(t, err, ErrInvalidName) }) - for _, v := range []string{"", "a:1.0.0-1", "0.0.1/1-1", "1.0.0 -1"} { - t.Run(fmt.Sprintf("InvalidVersion[%s]", v), func(t *testing.T) { - data := createPKGINFOContent(packageName, v) - p, err := ParsePackageInfo(bytes.NewReader(data)) - assert.Nil(t, p) - assert.ErrorIs(t, err, ErrInvalidVersion) - }) - } + t.Run("Regexp", func(t *testing.T) { + assert.Regexp(t, versionPattern, "1.2_3~4+5") + assert.Regexp(t, versionPattern, "1:2_3~4+5") + assert.NotRegexp(t, versionPattern, "a:1.0.0-1") + assert.NotRegexp(t, versionPattern, "0.0.1/1-1") + assert.NotRegexp(t, versionPattern, "1.0.0 -1") + }) t.Run("Valid", func(t *testing.T) { data := createPKGINFOContent(packageName, packageVersion) From 78059de310ddcc9d359bcdac9e75ad2f3c5bd225 Mon Sep 17 00:00:00 2001 From: wxiaoguang Date: Thu, 19 Dec 2024 16:22:59 +0800 Subject: [PATCH 5/5] revert old package "invalid version" test --- modules/packages/arch/metadata_test.go | 8 ++++++++ 1 file changed, 8 insertions(+) diff --git a/modules/packages/arch/metadata_test.go b/modules/packages/arch/metadata_test.go index 15ab8d90c3a47..5bdf63a3ee33e 100644 --- a/modules/packages/arch/metadata_test.go +++ b/modules/packages/arch/metadata_test.go @@ -130,6 +130,14 @@ func TestParsePackageInfo(t *testing.T) { assert.NotRegexp(t, versionPattern, "1.0.0 -1") }) + t.Run("InvalidVersion", func(t *testing.T) { + data := createPKGINFOContent(packageName, "") + + p, err := ParsePackageInfo(bytes.NewReader(data)) + assert.Nil(t, p) + assert.ErrorIs(t, err, ErrInvalidVersion) + }) + t.Run("Valid", func(t *testing.T) { data := createPKGINFOContent(packageName, packageVersion)