github.com/lineaje-labs/syft@v0.98.1-0.20231227153149-9e393f60ff1b/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 "gpl3.0.0+", 98 "gpl3.0+", 99 "gpl3+", 100 }, 101 }, 102 { 103 "abc-1.1", 104 []string{ 105 "abc1", 106 "abc1.1", 107 "abc1.1.0", 108 }, 109 }, 110 { 111 "oldap-2.0", 112 []string{ 113 "oldap2", 114 "oldap2.0", 115 "oldap2.0.0", 116 }, 117 }, 118 } 119 120 for _, test := range tests { 121 t.Run(test.shortName, func(t *testing.T) { 122 cleanID := cleanLicenseID(test.shortName) 123 perms := buildLicenseIDPermutations(cleanID) 124 assert.ElementsMatch(t, test.permutations, perms) 125 }) 126 } 127 } 128 129 func TestVersionPermutations(t *testing.T) { 130 var tests = []struct { 131 version []string 132 permutations []string 133 }{ 134 { 135 []string{"1", "0"}, 136 []string{"1", "1.0", "1.0.0"}, 137 }, 138 { 139 []string{"2"}, 140 []string{"2", "2.0", "2.0.0"}, 141 }, 142 { 143 []string{"2", "0"}, 144 []string{"2", "2.0", "2.0.0"}, 145 }, 146 147 { 148 []string{"3", "0", "0"}, 149 []string{"3", "3.0", "3.0.0"}, 150 }, 151 { 152 []string{"0", "3"}, 153 []string{"0.3", "0.3.0"}, 154 }, 155 { 156 []string{"0", "0", "3"}, 157 []string{"0.0.3"}, 158 }, 159 } 160 161 for _, test := range tests { 162 t.Run(strings.Join(test.version, "."), func(t *testing.T) { 163 got := versionPermutations(test.version) 164 assert.ElementsMatch(t, test.permutations, got) 165 }) 166 } 167 } 168 169 func TestFindLicenseVersion(t *testing.T) { 170 var tests = []struct { 171 license string 172 version []string 173 }{ 174 { 175 "GPL-1.0-only", 176 []string{"1", "0"}, 177 }, 178 { 179 "GPL-2.0", 180 []string{"2", "0"}, 181 }, 182 { 183 "GPL-2.0.0", 184 []string{"2", "0", "0"}, 185 }, 186 { 187 "GPL-2", 188 []string{"2"}, 189 }, 190 { 191 "php-3.01", 192 []string{"3", "01"}, 193 }, 194 { 195 "oldap-2.0", 196 []string{"2", "0"}, 197 }, 198 } 199 200 for _, test := range tests { 201 t.Run(test.license, func(t *testing.T) { 202 got := findLicenseVersion(test.license) 203 assert.Equal(t, test.version, got) 204 }) 205 } 206 } 207 208 func Test_findReplacementLicense(t *testing.T) { 209 tests := []struct { 210 l License 211 expected *License 212 licenses LicenseList 213 }{ 214 {license1, nil, LicenseList{}}, 215 {license1, nil, LicenseList{Licenses: []License{license3}}}, 216 {license1, &license2, LicenseList{Licenses: []License{license2, license3}}}, 217 {license1, &license2, LicenseList{Licenses: []License{license2, license3, license4, license5}}}, 218 } 219 220 for _, tt := range tests { 221 assert.Equal(t, tt.expected, tt.licenses.findReplacementLicense(tt.l)) 222 } 223 }