github.com/anchore/syft@v1.4.2-0.20240516191711-1bec1fc5d397/syft/pkg/type_test.go (about) 1 package pkg 2 3 import ( 4 "testing" 5 6 "github.com/scylladb/go-set/strset" 7 "github.com/stretchr/testify/assert" 8 ) 9 10 func TestTypeFromPURL(t *testing.T) { 11 12 tests := []struct { 13 name string 14 purl string 15 expected Type 16 }{ 17 { 18 purl: "pkg:rpm/fedora/util-linux@2.32.1-27.el8-?arch=amd64", 19 expected: RpmPkg, 20 }, 21 { 22 purl: "pkg:apk/alpine/util-linux@2.32.1", 23 expected: ApkPkg, 24 }, 25 { 26 purl: "pkg:deb/debian/curl@7.50.3-1?arch=i386&distro=jessie", 27 expected: DebPkg, 28 }, 29 { 30 purl: "pkg:npm/util@2.32", 31 expected: NpmPkg, 32 }, 33 { 34 purl: "pkg:pypi/util-linux@2.32.1-27.el8", 35 expected: PythonPkg, 36 }, 37 { 38 purl: "pkg:gem/ruby-advisory-db-check@0.12.4", 39 expected: GemPkg, 40 }, 41 { 42 purl: "pkg:golang/github.com/gorilla/context@234fd47e07d1004f0aed9c", 43 expected: GoModulePkg, 44 }, 45 { 46 purl: "pkg:cargo/clap@2.33.0", 47 expected: RustPkg, 48 }, 49 { 50 purl: "pkg:pub/util@1.2.34?hosted_url=pub.hosted.org", 51 expected: DartPubPkg, 52 }, 53 54 { 55 purl: "pkg:dotnet/Microsoft.CodeAnalysis.Razor@2.2.0", 56 expected: DotnetPkg, 57 }, 58 { 59 purl: "pkg:composer/laravel/laravel@5.5.0", 60 expected: PhpComposerPkg, 61 }, 62 { 63 purl: "pkg:pecl/memcached@3.2.0", 64 expected: PhpPeclPkg, 65 }, 66 { 67 purl: "pkg:maven/org.apache.xmlgraphics/batik-anim@1.9.1?type=zip&classifier=dist", 68 expected: JavaPkg, 69 }, 70 { 71 purl: "pkg:alpm/arch/linux@5.10.0?arch=x86_64&distro=arch", 72 expected: AlpmPkg, 73 }, 74 { 75 purl: "pkg:cocoapods/GlossButtonNode@3.1.2", 76 expected: CocoapodsPkg, 77 }, 78 { 79 purl: "pkg:conan/catch2@2.13.8", 80 expected: ConanPkg, 81 }, 82 { 83 purl: "pkg:hackage/HTTP@4000.3.16", 84 expected: HackagePkg, 85 }, 86 { 87 purl: "pkg:hex/hpax/hpax@0.1.1", 88 expected: HexPkg, 89 }, 90 { 91 purl: "pkg:otp/accept@0.3.5", 92 expected: ErlangOTPPkg, 93 }, 94 { 95 purl: "pkg:generic/linux-kernel@5.10.15", 96 expected: LinuxKernelPkg, 97 }, 98 { 99 purl: "pkg:nix/glibc@2.34?hash=h0cnbmfcn93xm5dg2x27ixhag1cwndga", 100 expected: NixPkg, 101 }, 102 { 103 purl: "pkg:cran/base@4.3.0", 104 expected: Rpkg, 105 }, 106 { 107 purl: "pkg:swift/github.com/apple/swift-numerics/swift-numerics@1.0.2", 108 expected: SwiftPkg, 109 }, 110 } 111 112 var pkgTypes []string 113 var expectedTypes = strset.New() 114 for _, ty := range AllPkgs { 115 expectedTypes.Add(string(ty)) 116 } 117 118 // testing microsoft packages and jenkins-plugins and custom binary type 119 // is not valid for purl at this time 120 expectedTypes.Remove(string(KbPkg)) 121 expectedTypes.Remove(string(JenkinsPluginPkg)) 122 expectedTypes.Remove(string(PortagePkg)) 123 expectedTypes.Remove(string(BinaryPkg)) 124 expectedTypes.Remove(string(LinuxKernelModulePkg)) 125 expectedTypes.Remove(string(GithubActionPkg), string(GithubActionWorkflowPkg)) 126 expectedTypes.Remove(string(WordpressPluginPkg)) 127 128 for _, test := range tests { 129 t.Run(string(test.expected), func(t *testing.T) { 130 actual := TypeFromPURL(test.purl) 131 132 if actual != "" { 133 pkgTypes = append(pkgTypes, string(actual)) 134 } 135 136 assert.Equal(t, test.expected, actual) 137 }) 138 } 139 140 assert.ElementsMatch(t, expectedTypes.List(), pkgTypes, "missing one or more package types to test against (maybe a package type was added?)") 141 142 }