github.com/anchore/syft@v1.38.2/syft/pkg/cataloger/r/package_test.go (about)

     1  package r
     2  
     3  import (
     4  	"context"
     5  	"testing"
     6  
     7  	"github.com/anchore/syft/syft/pkg"
     8  )
     9  
    10  func Test_NewPackageLicenses(t *testing.T) {
    11  	ctx := context.TODO()
    12  	testCases := []struct {
    13  		name string
    14  		pd   parseData
    15  		want []pkg.License
    16  	}{
    17  		{
    18  			"License field with single valid spdx",
    19  			parseData{
    20  				Package: "Foo",
    21  				Version: "1",
    22  				License: "MIT",
    23  			},
    24  			[]pkg.License{
    25  				pkg.NewLicenseWithContext(ctx, "MIT"),
    26  			},
    27  		},
    28  		{
    29  			"License field with single version separator no +",
    30  			parseData{
    31  				Package: "Bar",
    32  				Version: "2",
    33  				License: "LGPL (== 2.0)",
    34  			},
    35  			[]pkg.License{
    36  				pkg.NewLicenseWithContext(ctx, "LGPL2.0"),
    37  			},
    38  		},
    39  		{
    40  			"License field with multiple version separator",
    41  			parseData{
    42  				Package: "Bar",
    43  				Version: "2",
    44  				License: "LGPL (>= 2.0, < 3)",
    45  			},
    46  			[]pkg.License{
    47  				pkg.NewLicenseWithContext(ctx, "LGPL2.0+"),
    48  			},
    49  		},
    50  		{
    51  			"License field with file reference",
    52  			parseData{
    53  				Package: "Baz",
    54  				Version: "3",
    55  				License: "GPL-2 + file LICENSE",
    56  			},
    57  			[]pkg.License{
    58  				pkg.NewLicenseWithContext(ctx, "GPL-2"),
    59  			},
    60  		},
    61  		{
    62  			"License field which covers no case",
    63  			parseData{
    64  				Package: "Baz",
    65  				Version: "3",
    66  				License: "Mozilla Public License",
    67  			},
    68  			[]pkg.License{
    69  				pkg.NewLicenseWithContext(ctx, "Mozilla Public License"),
    70  			},
    71  		},
    72  		{
    73  			"License field with multiple cases",
    74  			parseData{
    75  				Package: "Baz",
    76  				Version: "3",
    77  				License: "GPL-2 | file LICENSE | LGPL (>= 2.0)",
    78  			},
    79  			[]pkg.License{
    80  				pkg.NewLicenseWithContext(ctx, "GPL-2"),
    81  				pkg.NewLicenseWithContext(ctx, "LGPL2.0+"),
    82  			},
    83  		},
    84  	}
    85  
    86  	for _, tt := range testCases {
    87  		t.Run(tt.name, func(t *testing.T) {
    88  			got := parseLicenseData(ctx, tt.pd.License)
    89  			if len(got) != len(tt.want) {
    90  				t.Errorf("unexpected number of licenses: got=%d, want=%d", len(got), len(tt.want))
    91  			}
    92  
    93  			for _, wantLicense := range tt.want {
    94  				found := false
    95  				for _, gotLicense := range got {
    96  					if wantLicense.Type == gotLicense.Type &&
    97  						wantLicense.SPDXExpression == gotLicense.SPDXExpression &&
    98  						wantLicense.Value == gotLicense.Value {
    99  						found = true
   100  					}
   101  				}
   102  				if !found {
   103  					t.Errorf("could not find expected license: %+v; got: %+v", wantLicense, got)
   104  				}
   105  			}
   106  		})
   107  	}
   108  }