github.com/anchore/syft@v1.38.2/syft/pkg/cataloger/ocaml/parse_opam_test.go (about)

     1  package ocaml
     2  
     3  import (
     4  	"context"
     5  	"testing"
     6  
     7  	"github.com/stretchr/testify/assert"
     8  
     9  	"github.com/anchore/syft/syft/artifact"
    10  	"github.com/anchore/syft/syft/file"
    11  	"github.com/anchore/syft/syft/pkg"
    12  	"github.com/anchore/syft/syft/pkg/cataloger/internal/pkgtest"
    13  )
    14  
    15  func TestParseOpamPackage(t *testing.T) {
    16  	fixture1 := "test-fixtures/ocaml-base-compiler.4.14.0/opam"
    17  	location1 := file.NewLocation(fixture1)
    18  
    19  	fixture2 := "test-fixtures/alcotest.opam"
    20  	location2 := file.NewLocation(fixture2)
    21  	ctx := context.TODO()
    22  	tests := []struct {
    23  		fixture string
    24  		want    []pkg.Package
    25  	}{
    26  		{
    27  			fixture: fixture1,
    28  			want: []pkg.Package{
    29  				{
    30  					Name:      "ocaml-base-compiler",
    31  					Version:   "4.14.0",
    32  					PURL:      "pkg:opam/ocaml-base-compiler@4.14.0",
    33  					Locations: file.NewLocationSet(location1),
    34  					Licenses: pkg.NewLicenseSet(
    35  						pkg.NewLicensesFromLocationWithContext(ctx, location1, "LGPL-2.1-or-later WITH OCaml-LGPL-linking-exception")...,
    36  					),
    37  					Language: pkg.OCaml,
    38  					Type:     pkg.OpamPkg,
    39  					Metadata: pkg.OpamPackage{
    40  						Name:     "ocaml-base-compiler",
    41  						Version:  "4.14.0",
    42  						Licenses: []string{"LGPL-2.1-or-later WITH OCaml-LGPL-linking-exception"},
    43  						URL:      "https://github.com/ocaml/ocaml/archive/4.14.0.tar.gz",
    44  						Checksums: []string{
    45  							"sha256=39f44260382f28d1054c5f9d8bf4753cb7ad64027da792f7938344544da155e8",
    46  						},
    47  						Homepage: "https://ocaml.org",
    48  					},
    49  				},
    50  			},
    51  		},
    52  		{
    53  			fixture: fixture2,
    54  			want: []pkg.Package{
    55  				{
    56  					Name:      "alcotest",
    57  					Version:   "1.5.0",
    58  					PURL:      "pkg:opam/alcotest@1.5.0",
    59  					Locations: file.NewLocationSet(location2),
    60  					Licenses: pkg.NewLicenseSet(
    61  						pkg.NewLicensesFromLocationWithContext(
    62  							ctx,
    63  							location2,
    64  							"ISC",
    65  						)...,
    66  					),
    67  					Language: pkg.OCaml,
    68  					Type:     pkg.OpamPkg,
    69  					Metadata: pkg.OpamPackage{
    70  						Name:     "alcotest",
    71  						Version:  "1.5.0",
    72  						Licenses: []string{"ISC"},
    73  						Homepage: "https://github.com/mirage/alcotest",
    74  					},
    75  				},
    76  			},
    77  		},
    78  	}
    79  	for _, tt := range tests {
    80  		t.Run(tt.fixture, func(t *testing.T) {
    81  			// TODO: no relationships are under test yet
    82  			var expectedRelationships []artifact.Relationship
    83  
    84  			pkgtest.TestFileParser(t, tt.fixture, parseOpamPackage, tt.want, expectedRelationships)
    85  		})
    86  	}
    87  }
    88  
    89  func TestParseLicense(t *testing.T) {
    90  	tests := []struct {
    91  		name  string
    92  		input string
    93  		want  []string
    94  	}{
    95  		{
    96  			name:  "single license",
    97  			input: `"MIT"`,
    98  			want: []string{
    99  				"MIT",
   100  			},
   101  		},
   102  		{
   103  			name: "multiple license",
   104  			input: `[
   105  			"MIT", "IST"
   106  			]`,
   107  			want: []string{
   108  				"MIT",
   109  				"IST",
   110  			},
   111  		},
   112  	}
   113  	for _, tt := range tests {
   114  		t.Run(tt.name, func(t *testing.T) {
   115  			assert.Equal(t, tt.want, parseLicenses(tt.input))
   116  		})
   117  	}
   118  }
   119  
   120  func TestParseUrl(t *testing.T) {
   121  	tests := []struct {
   122  		name          string
   123  		input         string
   124  		wantUrl       string
   125  		wantChecksums []string
   126  	}{
   127  		{
   128  			name: "single checksums",
   129  			input: `
   130  src:
   131      "https://github.com/mirage/mirage-clock/releases/download/v4.2.0/mirage-clock-4.2.0.tbz"
   132    checksum:
   133      "sha256=fa17d15d5be23c79ba741f5f7cb88ed7112de16a4410cea81c71b98086889847"
   134  			`,
   135  			wantUrl: "https://github.com/mirage/mirage-clock/releases/download/v4.2.0/mirage-clock-4.2.0.tbz",
   136  			wantChecksums: []string{
   137  				"sha256=fa17d15d5be23c79ba741f5f7cb88ed7112de16a4410cea81c71b98086889847",
   138  			},
   139  		},
   140  		{
   141  			name: "multiple checksums",
   142  			input: `
   143  src:
   144      "https://github.com/mirage/mirage-clock/releases/download/v4.2.0/mirage-clock-4.2.0.tbz"
   145    checksum: [
   146      "sha256=fa17d15d5be23c79ba741f5f7cb88ed7112de16a4410cea81c71b98086889847"
   147      "sha512=05a359dc8400d4ca200ff255dbd030acd33d2c4acb5020838f772c02cdb5f243f3dbafbc43a8cd51e6b5923a140f84c9e7ea25b2c0fa277bb68b996190d36e3b"
   148  	"sha1024=05a359dc8400d4ca200ff255dbd030acd33d2c4acb5020838f772c02cdb5f243f3dbafbc43a8cd51e6b5923a140f84c9e7ea25b2c0fa277bb68b996190d36e3b"
   149    ]
   150  			`,
   151  			wantUrl: "https://github.com/mirage/mirage-clock/releases/download/v4.2.0/mirage-clock-4.2.0.tbz",
   152  			wantChecksums: []string{
   153  				"sha256=fa17d15d5be23c79ba741f5f7cb88ed7112de16a4410cea81c71b98086889847",
   154  				"sha512=05a359dc8400d4ca200ff255dbd030acd33d2c4acb5020838f772c02cdb5f243f3dbafbc43a8cd51e6b5923a140f84c9e7ea25b2c0fa277bb68b996190d36e3b",
   155  				"sha1024=05a359dc8400d4ca200ff255dbd030acd33d2c4acb5020838f772c02cdb5f243f3dbafbc43a8cd51e6b5923a140f84c9e7ea25b2c0fa277bb68b996190d36e3b",
   156  			},
   157  		},
   158  	}
   159  	for _, tt := range tests {
   160  		t.Run(tt.name, func(t *testing.T) {
   161  			url, checksums := parseURL([]byte(tt.input))
   162  			assert.Equal(t, tt.wantUrl, url)
   163  			assert.Equal(t, tt.wantChecksums, checksums)
   164  		})
   165  	}
   166  }