github.com/noqcks/syft@v0.0.0-20230920222752-a9e2c4e288e5/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:maven/org.apache.xmlgraphics/batik-anim@1.9.1?type=zip&classifier=dist",
    64  			expected: JavaPkg,
    65  		},
    66  		{
    67  			purl:     "pkg:alpm/arch/linux@5.10.0?arch=x86_64&distro=arch",
    68  			expected: AlpmPkg,
    69  		},
    70  		{
    71  			purl:     "pkg:cocoapods/GlossButtonNode@3.1.2",
    72  			expected: CocoapodsPkg,
    73  		},
    74  		{
    75  			purl:     "pkg:conan/catch2@2.13.8",
    76  			expected: ConanPkg,
    77  		},
    78  		{
    79  			purl:     "pkg:hackage/HTTP@4000.3.16",
    80  			expected: HackagePkg,
    81  		},
    82  		{
    83  			purl:     "pkg:hex/hpax/hpax@0.1.1",
    84  			expected: HexPkg,
    85  		},
    86  		{
    87  			purl:     "pkg:generic/linux-kernel@5.10.15",
    88  			expected: LinuxKernelPkg,
    89  		},
    90  		{
    91  			purl:     "pkg:nix/glibc@2.34?hash=h0cnbmfcn93xm5dg2x27ixhag1cwndga",
    92  			expected: NixPkg,
    93  		},
    94  		{
    95  			purl:     "pkg:cran/base@4.3.0",
    96  			expected: Rpkg,
    97  		},
    98  		{
    99  			purl:     "pkg:swift/github.com/apple/swift-numerics/swift-numerics@1.0.2",
   100  			expected: SwiftPkg,
   101  		},
   102  	}
   103  
   104  	var pkgTypes []string
   105  	var expectedTypes = strset.New()
   106  	for _, ty := range AllPkgs {
   107  		expectedTypes.Add(string(ty))
   108  	}
   109  
   110  	// testing microsoft packages and jenkins-plugins and custom binary type
   111  	// is not valid for purl at this time
   112  	expectedTypes.Remove(string(KbPkg))
   113  	expectedTypes.Remove(string(JenkinsPluginPkg))
   114  	expectedTypes.Remove(string(PortagePkg))
   115  	expectedTypes.Remove(string(BinaryPkg))
   116  	expectedTypes.Remove(string(LinuxKernelModulePkg))
   117  	expectedTypes.Remove(string(GithubActionPkg), string(GithubActionWorkflowPkg))
   118  
   119  	for _, test := range tests {
   120  		t.Run(string(test.expected), func(t *testing.T) {
   121  			actual := TypeFromPURL(test.purl)
   122  
   123  			if actual != "" {
   124  				pkgTypes = append(pkgTypes, string(actual))
   125  			}
   126  
   127  			assert.Equal(t, test.expected, actual)
   128  		})
   129  	}
   130  
   131  	assert.ElementsMatch(t, expectedTypes.List(), pkgTypes, "missing one or more package types to test against (maybe a package type was added?)")
   132  
   133  }