github.com/nextlinux/gosbom@v0.81.1-0.20230627115839-1ff50c281391/gosbom/formats/common/spdxhelpers/license_test.go (about)

     1  package spdxhelpers
     2  
     3  import (
     4  	"testing"
     5  
     6  	"github.com/nextlinux/gosbom/gosbom/pkg"
     7  	"github.com/stretchr/testify/assert"
     8  )
     9  
    10  func Test_License(t *testing.T) {
    11  	type expected struct {
    12  		concluded string
    13  		declared  string
    14  	}
    15  	tests := []struct {
    16  		name     string
    17  		input    pkg.Package
    18  		expected expected
    19  	}{
    20  		{
    21  			name:  "no licenses",
    22  			input: pkg.Package{},
    23  			expected: expected{
    24  				concluded: "NOASSERTION",
    25  				declared:  "NOASSERTION",
    26  			},
    27  		},
    28  		{
    29  			name: "no SPDX licenses",
    30  			input: pkg.Package{
    31  				Licenses: pkg.NewLicenseSet(pkg.NewLicense("made-up")),
    32  			},
    33  			expected: expected{
    34  				concluded: "NOASSERTION",
    35  				declared:  "LicenseRef-made-up",
    36  			},
    37  		},
    38  		{
    39  			name: "with SPDX license",
    40  			input: pkg.Package{
    41  				Licenses: pkg.NewLicenseSet(pkg.NewLicense("MIT")),
    42  			},
    43  			expected: struct {
    44  				concluded string
    45  				declared  string
    46  			}{
    47  				concluded: "NOASSERTION",
    48  				declared:  "MIT",
    49  			},
    50  		},
    51  		{
    52  			name: "with SPDX license expression",
    53  			input: pkg.Package{
    54  				Licenses: pkg.NewLicenseSet(
    55  					pkg.NewLicense("MIT"),
    56  					pkg.NewLicense("GPL-3.0-only"),
    57  				),
    58  			},
    59  			expected: expected{
    60  				concluded: "NOASSERTION",
    61  				// because we sort licenses alphabetically GPL ends up at the start
    62  				declared: "GPL-3.0-only AND MIT",
    63  			},
    64  		},
    65  		{
    66  			name: "includes valid LicenseRef-",
    67  			input: pkg.Package{
    68  				Licenses: pkg.NewLicenseSet(
    69  					pkg.NewLicense("one thing first"),
    70  					pkg.NewLicense("two things/#$^second"),
    71  					pkg.NewLicense("MIT"),
    72  				),
    73  			},
    74  			expected: expected{
    75  				concluded: "NOASSERTION",
    76  				// because we separate licenses between valid SPDX and non valid, valid ID always end at the front
    77  				declared: "MIT AND LicenseRef-one-thing-first AND LicenseRef-two-things----second",
    78  			},
    79  		},
    80  		{
    81  			name: "join parentheses correctly",
    82  			input: pkg.Package{
    83  				Licenses: pkg.NewLicenseSet(
    84  					pkg.NewLicense("one thing first"),
    85  					pkg.NewLicense("MIT AND GPL-3.0-only"),
    86  					pkg.NewLicense("MIT OR APACHE-2.0"),
    87  				),
    88  			},
    89  			expected: expected{
    90  				concluded: "NOASSERTION",
    91  				// because we separate licenses between valid SPDX and non valid, valid ID always end at the front
    92  				declared: "(MIT AND GPL-3.0-only) AND (MIT OR APACHE-2.0) AND LicenseRef-one-thing-first",
    93  			},
    94  		},
    95  	}
    96  	for _, test := range tests {
    97  		t.Run(test.name, func(t *testing.T) {
    98  			c, d := License(test.input)
    99  			assert.Equal(t, test.expected.concluded, c)
   100  			assert.Equal(t, test.expected.declared, d)
   101  		})
   102  	}
   103  }
   104  
   105  func Test_joinLicenses(t *testing.T) {
   106  	tests := []struct {
   107  		name string
   108  		args []string
   109  		want string
   110  	}{
   111  		{
   112  			name: "multiple licenses",
   113  			args: []string{"MIT", "GPL-3.0-only"},
   114  			want: "MIT AND GPL-3.0-only",
   115  		},
   116  		{
   117  			name: "multiple licenses with complex expressions",
   118  			args: []string{"MIT AND Apache", "GPL-3.0-only"},
   119  			want: "(MIT AND Apache) AND GPL-3.0-only",
   120  		},
   121  	}
   122  	for _, tt := range tests {
   123  		t.Run(tt.name, func(t *testing.T) {
   124  			assert.Equalf(t, tt.want, joinLicenses(tt.args), "joinLicenses(%v)", tt.args)
   125  		})
   126  	}
   127  }