github.com/nextlinux/gosbom@v0.81.1-0.20230627115839-1ff50c281391/internal/spdxlicense/generate/license_test.go (about) 1 package main 2 3 import ( 4 "strings" 5 "testing" 6 7 "github.com/stretchr/testify/assert" 8 ) 9 10 var ( 11 license1 = License{ 12 ID: "ABC-1.0+", 13 Name: "The ABC License 1.0", 14 Deprecated: true, 15 } 16 17 license2 = License{ 18 ID: "ABC-1.0-or-later", 19 Name: "The ABC License 1.0", 20 } 21 22 license3 = License{ 23 ID: "ABC-1.0", 24 Name: "The ABC License 1.0 Only", 25 Deprecated: true, 26 } 27 28 license4 = License{ 29 ID: "ABC-1.0-only", 30 Name: "The ABC License 1.0 Only", 31 } 32 license5 = License{ 33 ID: "Duh-1.0", 34 Name: "The Duh License 1.0", 35 Deprecated: true, 36 } 37 license6 = License{ 38 ID: "Duh-1.0-duh", 39 Name: "The Duh License 1.0", 40 Deprecated: true, 41 } 42 ) 43 44 func TestLicense_canReplace(t *testing.T) { 45 tests := []struct { 46 l1, l2 License 47 expected bool 48 }{ 49 {license1, license2, false}, 50 {license2, license1, true}, 51 {license2, license3, false}, 52 {license3, license2, false}, 53 } 54 55 for _, tt := range tests { 56 t.Run(tt.l1.ID+" - "+tt.l2.ID, func(t *testing.T) { 57 assert.Equal(t, tt.expected, tt.l1.canReplace(tt.l2)) 58 }) 59 } 60 } 61 62 func TestLicensePermutations(t *testing.T) { 63 var tests = []struct { 64 shortName string 65 permutations []string 66 }{ 67 { 68 "GPL-1-only", 69 []string{ 70 "gpl1only", 71 "gpl1.0only", 72 "gpl1.0.0only", 73 }, 74 }, 75 { 76 "GPL-2", 77 []string{ 78 "gpl2", 79 "gpl2.0", 80 "gpl2.0.0", 81 }, 82 }, 83 { 84 "GPL-2.0+", 85 []string{ 86 "gpl2+", 87 "gpl2.0+", 88 "gpl2.0.0+", 89 }, 90 }, 91 { 92 "GPL-3.0.0-or-later", 93 []string{ 94 "gpl3orlater", 95 "gpl3.0orlater", 96 "gpl3.0.0orlater", 97 }, 98 }, 99 { 100 "abc-1.1", 101 []string{ 102 "abc1", 103 "abc1.1", 104 "abc1.1.0", 105 }, 106 }, 107 { 108 "oldap-2.0", 109 []string{ 110 "oldap2", 111 "oldap2.0", 112 "oldap2.0.0", 113 }, 114 }, 115 } 116 117 for _, test := range tests { 118 t.Run(test.shortName, func(t *testing.T) { 119 cleanID := cleanLicenseID(test.shortName) 120 perms := buildLicenseIDPermutations(cleanID) 121 assert.ElementsMatch(t, test.permutations, perms) 122 }) 123 } 124 } 125 126 func TestVersionPermutations(t *testing.T) { 127 var tests = []struct { 128 version []string 129 permutations []string 130 }{ 131 { 132 []string{"1", "0"}, 133 []string{"1", "1.0", "1.0.0"}, 134 }, 135 { 136 []string{"2"}, 137 []string{"2", "2.0", "2.0.0"}, 138 }, 139 { 140 []string{"2", "0"}, 141 []string{"2", "2.0", "2.0.0"}, 142 }, 143 144 { 145 []string{"3", "0", "0"}, 146 []string{"3", "3.0", "3.0.0"}, 147 }, 148 { 149 []string{"0", "3"}, 150 []string{"0.3", "0.3.0"}, 151 }, 152 { 153 []string{"0", "0", "3"}, 154 []string{"0.0.3"}, 155 }, 156 } 157 158 for _, test := range tests { 159 t.Run(strings.Join(test.version, "."), func(t *testing.T) { 160 got := versionPermutations(test.version) 161 assert.ElementsMatch(t, test.permutations, got) 162 }) 163 } 164 } 165 166 func TestFindLicenseVersion(t *testing.T) { 167 var tests = []struct { 168 license string 169 version []string 170 }{ 171 { 172 "GPL-1.0-only", 173 []string{"1", "0"}, 174 }, 175 { 176 "GPL-2.0", 177 []string{"2", "0"}, 178 }, 179 { 180 "GPL-2.0.0", 181 []string{"2", "0", "0"}, 182 }, 183 { 184 "GPL-2", 185 []string{"2"}, 186 }, 187 { 188 "php-3.01", 189 []string{"3", "01"}, 190 }, 191 { 192 "oldap-2.0", 193 []string{"2", "0"}, 194 }, 195 } 196 197 for _, test := range tests { 198 t.Run(test.license, func(t *testing.T) { 199 got := findLicenseVersion(test.license) 200 assert.Equal(t, test.version, got) 201 }) 202 } 203 } 204 205 func Test_findReplacementLicense(t *testing.T) { 206 tests := []struct { 207 l License 208 expected *License 209 licenses LicenseList 210 }{ 211 {license1, nil, LicenseList{}}, 212 {license1, nil, LicenseList{Licenses: []License{license3}}}, 213 {license1, &license2, LicenseList{Licenses: []License{license2, license3}}}, 214 {license1, &license2, LicenseList{Licenses: []License{license2, license3, license4, license5}}}, 215 } 216 217 for _, tt := range tests { 218 assert.Equal(t, tt.expected, tt.licenses.findReplacementLicense(tt.l)) 219 } 220 }