github.com/anchore/syft@v1.38.2/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:bitnami/apache@2.4.62-3?arch=arm64&distro=debian-12",
    27  			expected: BitnamiPkg,
    28  		},
    29  		{
    30  			purl:     "pkg:deb/debian/curl@7.50.3-1?arch=i386&distro=jessie",
    31  			expected: DebPkg,
    32  		},
    33  		{
    34  			purl:     "pkg:npm/util@2.32",
    35  			expected: NpmPkg,
    36  		},
    37  		{
    38  			purl:     "pkg:pypi/util-linux@2.32.1-27.el8",
    39  			expected: PythonPkg,
    40  		},
    41  		{
    42  			purl:     "pkg:gem/ruby-advisory-db-check@0.12.4",
    43  			expected: GemPkg,
    44  		},
    45  		{
    46  			purl:     "pkg:golang/github.com/gorilla/context@234fd47e07d1004f0aed9c",
    47  			expected: GoModulePkg,
    48  		},
    49  		{
    50  			purl:     "pkg:cargo/clap@2.33.0",
    51  			expected: RustPkg,
    52  		},
    53  		{
    54  			purl:     "pkg:pub/util@1.2.34?hosted_url=pub.hosted.org",
    55  			expected: DartPubPkg,
    56  		},
    57  		{
    58  			purl:     "pkg:dotnet/Microsoft.CodeAnalysis.Razor@2.2.0",
    59  			expected: DotnetPkg,
    60  		},
    61  		{
    62  			purl:     "pkg:composer/laravel/laravel@5.5.0",
    63  			expected: PhpComposerPkg,
    64  		},
    65  		{
    66  			purl:     "pkg:pear/pecl.php.net/memcached@3.2.0", // pecl namespace
    67  			expected: PhpPearPkg,
    68  		},
    69  		{
    70  			purl:     "pkg:pear/pear.php.net/memcached@3.2.0", // pear namespace
    71  			expected: PhpPearPkg,
    72  		},
    73  		{
    74  			purl:     "pkg:pecl/pecl.php.net/memcached@3.2.0", // note: this is an invalid purl, but we will handle it anyway in case folks created the type pre-emptively
    75  			expected: PhpPearPkg,                              // we should still consider this a pear package
    76  		},
    77  		{
    78  			purl:     "pkg:maven/org.apache.xmlgraphics/batik-anim@1.9.1?type=zip&classifier=dist",
    79  			expected: JavaPkg,
    80  		},
    81  		{
    82  			purl:     "pkg:alpm/arch/linux@5.10.0?arch=x86_64&distro=arch",
    83  			expected: AlpmPkg,
    84  		},
    85  		{
    86  			purl:     "pkg:cocoapods/GlossButtonNode@3.1.2",
    87  			expected: CocoapodsPkg,
    88  		},
    89  		{
    90  			purl:     "pkg:conan/catch2@2.13.8",
    91  			expected: ConanPkg,
    92  		},
    93  		{
    94  			purl:     "pkg:hackage/HTTP@4000.3.16",
    95  			expected: HackagePkg,
    96  		},
    97  		{
    98  			purl:     "pkg:hex/hpax/hpax@0.1.1",
    99  			expected: HexPkg,
   100  		},
   101  		{
   102  			purl:     "pkg:otp/accept@0.3.5",
   103  			expected: ErlangOTPPkg,
   104  		},
   105  		{
   106  			purl:     "pkg:generic/linux-kernel@5.10.15",
   107  			expected: LinuxKernelPkg,
   108  		},
   109  		{
   110  			purl:     "pkg:nix/glibc@2.34?hash=h0cnbmfcn93xm5dg2x27ixhag1cwndga",
   111  			expected: NixPkg,
   112  		},
   113  		{
   114  			purl:     "pkg:cran/base@4.3.0",
   115  			expected: Rpkg,
   116  		},
   117  		{
   118  			purl:     "pkg:luarocks/kong@3.7.0",
   119  			expected: LuaRocksPkg,
   120  		},
   121  		{
   122  			purl:     "pkg:swift/github.com/apple/swift-numerics/swift-numerics@1.0.2",
   123  			expected: SwiftPkg,
   124  		},
   125  		{
   126  			purl:     "pkg:swiplpack/condition@0.1.1",
   127  			expected: SwiplPackPkg,
   128  		},
   129  		{
   130  			purl:     "pkg:opam/ocaml-base-compiler@5.2.0",
   131  			expected: OpamPkg,
   132  		},
   133  		{
   134  			name:     "conda",
   135  			purl:     "pkg:generic/conda@1.2.3",
   136  			expected: CondaPkg,
   137  		},
   138  	}
   139  
   140  	var pkgTypes = strset.New()
   141  	var expectedTypes = strset.New()
   142  	for _, ty := range AllPkgs {
   143  		expectedTypes.Add(string(ty))
   144  	}
   145  
   146  	// testing microsoft packages and jenkins-plugins and custom binary type
   147  	// and terraform types is not valid for purl at this time
   148  	expectedTypes.Remove(string(KbPkg))
   149  	expectedTypes.Remove(string(JenkinsPluginPkg))
   150  	expectedTypes.Remove(string(PortagePkg))
   151  	expectedTypes.Remove(string(BinaryPkg))
   152  	expectedTypes.Remove(string(LinuxKernelModulePkg))
   153  	expectedTypes.Remove(string(GithubActionPkg), string(GithubActionWorkflowPkg))
   154  	expectedTypes.Remove(string(WordpressPluginPkg))
   155  	expectedTypes.Remove(string(HomebrewPkg))
   156  	expectedTypes.Remove(string(TerraformPkg))
   157  	expectedTypes.Remove(string(GraalVMNativeImagePkg))
   158  	expectedTypes.Remove(string(ModelPkg))   // no valid purl for ai artifacts currently
   159  	expectedTypes.Remove(string(PhpPeclPkg)) // we should always consider this a pear package
   160  
   161  	for _, test := range tests {
   162  		t.Run(string(test.expected), func(t *testing.T) {
   163  			actual := TypeFromPURL(test.purl)
   164  
   165  			if actual != "" {
   166  				pkgTypes.Add(string(actual))
   167  			}
   168  
   169  			assert.Equal(t, test.expected, actual)
   170  		})
   171  	}
   172  
   173  	assert.ElementsMatch(t, expectedTypes.List(), pkgTypes.List(), "missing one or more package types to test against (maybe a package type was added?)")
   174  }