github.com/google/osv-scalibr@v0.4.1/converter/spdx/license_test.go (about) 1 // Copyright 2025 Google LLC 2 // 3 // Licensed under the Apache License, Version 2.0 (the "License"); 4 // you may not use this file except in compliance with the License. 5 // You may obtain a copy of the License at 6 // 7 // http://www.apache.org/licenses/LICENSE-2.0 8 // 9 // Unless required by applicable law or agreed to in writing, software 10 // distributed under the License is distributed on an "AS IS" BASIS, 11 // WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. 12 // See the License for the specific language governing permissions and 13 // limitations under the License. 14 15 package spdx_test 16 17 import ( 18 "testing" 19 20 "bitbucket.org/creachadair/stringset" 21 "github.com/google/go-cmp/cmp" 22 "github.com/google/go-cmp/cmp/cmpopts" 23 "github.com/google/osv-scalibr/converter/spdx" 24 "github.com/spdx/tools-golang/spdx/v2/v2_3" 25 ) 26 27 func TestLicenseExpression(t *testing.T) { 28 tests := []struct { 29 name string 30 licenses []string 31 wantExpr string 32 wantOtherLicenses stringset.Set 33 }{ 34 { 35 name: "empty licenses list", 36 licenses: []string{}, 37 wantExpr: spdx.NoAssertion, 38 wantOtherLicenses: stringset.New(), 39 }, 40 { 41 name: "just empty string licenses", 42 licenses: []string{"", "", ""}, 43 wantExpr: spdx.NoAssertion, 44 wantOtherLicenses: stringset.New(), 45 }, 46 { 47 name: "has non-standard", 48 licenses: []string{"NON-STANDARD", "MIT"}, 49 wantExpr: spdx.NoAssertion, 50 wantOtherLicenses: stringset.New(), 51 }, 52 { 53 name: "has unknown", 54 licenses: []string{"UNKNOWN", "MIT"}, 55 wantExpr: spdx.NoAssertion, 56 wantOtherLicenses: stringset.New(), 57 }, 58 { 59 name: "MIT", 60 licenses: []string{"MIT"}, 61 wantExpr: "MIT", 62 wantOtherLicenses: stringset.New(), 63 }, 64 { 65 name: "MIT in parens", 66 licenses: []string{"(MIT)"}, 67 wantExpr: "MIT", 68 wantOtherLicenses: stringset.New(), 69 }, 70 { 71 name: "Multiple values", 72 licenses: []string{"MIT", "LGPL-2.0-only"}, 73 wantExpr: "LGPL-2.0-only AND MIT", 74 wantOtherLicenses: stringset.New(), 75 }, 76 { 77 name: "OR value", 78 licenses: []string{"MIT OR LGPL-2.0-only"}, 79 wantExpr: "(MIT OR LGPL-2.0-only)", 80 wantOtherLicenses: stringset.New(), 81 }, 82 { 83 name: "non-spdx license", 84 licenses: []string{"MADE UP"}, 85 wantExpr: "LicenseRef-MADE-UP", 86 wantOtherLicenses: stringset.New("MADE UP"), 87 }, 88 { 89 name: "OR with non-spdx license", 90 licenses: []string{"MADE UP OR MIT"}, 91 wantExpr: "(LicenseRef-MADE-UP OR MIT)", 92 wantOtherLicenses: stringset.New("MADE UP"), 93 }, 94 { 95 name: "Complicated expression", 96 licenses: []string{"MADE UP OR MIT", "(CC0-1.0 and AGPL-1.0-only)", "(MIT or LGPL-2.0-only)", "??", "CC0-1.0"}, 97 wantExpr: "(LicenseRef-MADE-UP OR MIT) AND (MIT OR LGPL-2.0-only) AND AGPL-1.0-only AND CC0-1.0 AND LicenseRef---", 98 wantOtherLicenses: stringset.New("MADE UP", "??"), 99 }, 100 } 101 for _, tc := range tests { 102 t.Run(tc.name, func(t *testing.T) { 103 gotExpr, gotOtherLicenses := spdx.LicenseExpression(tc.licenses) 104 if gotExpr != tc.wantExpr { 105 t.Errorf("expr err - licenseExpression(%v) = %q, want %q", tc.licenses, gotExpr, tc.wantExpr) 106 } 107 if diff := cmp.Diff(tc.wantOtherLicenses, gotOtherLicenses, cmpopts.SortSlices(func(a, b *v2_3.OtherLicense) bool { return a.LicenseIdentifier < b.LicenseIdentifier })); diff != "" { 108 t.Errorf("custom err - licenseExpression(%v) returned unexpected diff (-want +got):\n%s", tc.licenses, diff) 109 } 110 }) 111 } 112 }