Debian Codegraph @

spdxexp/satisfies_test.go

raw symbols SCIP
1 package spdxexp
2
3 import (
4 "errors"
5 "fmt"
6 "testing"
7
8 "github.com/stretchr/testify/assert"
9 )
10
11 func TestValidateLicenses(t *testing.T) {
12 tests := []struct {
13 name string
14 inputLicenses []string
15 allValid bool
16 invalidLicenses []string
17 }{
18 {"MIT shortcut test", []string{"MIT"}, true, []string{}},
19 {"mit shortcut test", []string{"mit"}, true, []string{}},
20 {"Apache-2.0 active shortcut test", []string{"Apache-2.0"}, true, []string{}},
21 {"All invalid", []string{"MTI", "Apche-2.0", "0xDEADBEEF", ""}, false, []string{"MTI", "Apche-2.0", "0xDEADBEEF", ""}},
22 {"All valid", []string{"MIT", "Apache-2.0", "GPL-2.0"}, true, []string{}},
23 {"Some invalid", []string{"MTI", "Apche-2.0", "GPL-2.0"}, false, []string{"MTI", "Apche-2.0"}},
24 {"GPL-2.0", []string{"GPL-2.0"}, true, []string{}},
25 {"GPL-2.0-only", []string{"GPL-2.0-only"}, true, []string{}},
26 {"SPDX Expressions are valid", []string{
27 "MIT AND APACHE-2.0",
28 "MIT AND APCHE-2.0",
29 "LGPL-2.1-only OR MIT OR BSD-3-Clause",
30 "GPL-2.0-or-later WITH Bison-exception-2.2",
31 }, false, []string{"MIT AND APCHE-2.0"}},
32 {"Empty string is invalid", []string{""}, false, []string{""}},
33 }
34
35 for _, test := range tests {
36 t.Run(test.name, func(t *testing.T) {
37 valid, invalidLicenses := ValidateLicenses(test.inputLicenses)
38 assert.EqualValues(t, test.invalidLicenses, invalidLicenses)
39 assert.Equal(t, test.allValid, valid)
40 })
41 }
42 }
43
44 func TestValidateLicensesWithOptions_FailComplexExpressions(t *testing.T) {
45 tests := []struct {
46 name string
47 inputLicenses []string
48 options ValidateLicensesOptions
49 allValid bool
50 invalidLicenses []string
51 }{
52 {
53 name: "Expressions rejected",
54 inputLicenses: []string{"MIT AND Apache-2.0"},
55 options: ValidateLicensesOptions{FailComplexExpressions: true},
56 allValid: false,
57 invalidLicenses: []string{
58 "MIT AND Apache-2.0",
59 },
60 },
61 {
62 name: "Mixed list rejects only expressions",
63 inputLicenses: []string{"MIT", "Apache-2.0", "LGPL-2.1-only OR MIT"},
64 options: ValidateLicensesOptions{FailComplexExpressions: true},
65 allValid: false,
66 invalidLicenses: []string{
67 "LGPL-2.1-only OR MIT",
68 },
69 },
70 {
71 name: "WITH exception is not treated as complex expression",
72 inputLicenses: []string{"GPL-2.0-or-later WITH Bison-exception-2.2"},
73 options: ValidateLicensesOptions{FailComplexExpressions: true},
74 allValid: true,
75 invalidLicenses: []string{},
76 },
77 }
78
79 for _, test := range tests {
80 t.Run(test.name, func(t *testing.T) {
81 valid, invalidLicenses := ValidateLicensesWithOptions(test.inputLicenses, test.options)
82 assert.EqualValues(t, test.invalidLicenses, invalidLicenses)
83 assert.Equal(t, test.allValid, valid)
84 })
85 }
86 }
87
88 func TestValidateLicensesWithOptions_FailDeprecatedLicenses(t *testing.T) {
89 // eCos-2.0 is a known deprecated SPDX license ID (see TestDeprecatedLicense).
90 deprecatedLicense := "eCos-2.0"
91
92 tests := []struct {
93 name string
94 inputLicenses []string
95 options ValidateLicensesOptions
96 allValid bool
97 invalidLicenses []string
98 }{
99 {
100 name: "Deprecated license rejected",
101 inputLicenses: []string{deprecatedLicense},
102 options: ValidateLicensesOptions{FailDeprecatedLicenses: true},
103 allValid: false,
104 invalidLicenses: []string{
105 deprecatedLicense,
106 },
107 },
108 {
109 name: "Mixed list rejects only deprecated licenses",
110 inputLicenses: []string{"MIT", "Apache-2.0", deprecatedLicense},
111 options: ValidateLicensesOptions{FailDeprecatedLicenses: true},
112 allValid: false,
113 invalidLicenses: []string{
114 deprecatedLicense,
115 },
116 },
117 {
118 name: "WITH exception rejects deprecated license if FailDeprecatedLicenses is true",
119 inputLicenses: []string{deprecatedLicense + " WITH Bison-exception-2.2"},
120 options: ValidateLicensesOptions{FailDeprecatedLicenses: true},
121 allValid: false,
122 invalidLicenses: []string{
123 deprecatedLicense + " WITH Bison-exception-2.2",
124 },
125 },
126 {
127 name: "WITH exception allows deprecated license if FailDeprecatedLicenses is false",
128 inputLicenses: []string{deprecatedLicense + " WITH Bison-exception-2.2"},
129 options: ValidateLicensesOptions{FailDeprecatedLicenses: false},
130 allValid: true,
131 invalidLicenses: []string{},
132 },
133 }
134
135 for _, test := range tests {
136 t.Run(test.name, func(t *testing.T) {
137 valid, invalidLicenses := ValidateLicensesWithOptions(test.inputLicenses, test.options)
138 assert.EqualValues(t, test.invalidLicenses, invalidLicenses)
139 assert.Equal(t, test.allValid, valid)
140 })
141 }
142 }
143
144 func TestValidateLicensesWithOptions_AllOptions(t *testing.T) {
145 documentRef := "DocumentRef-spdx-tool-1.2:LicenseRef-MIT-Style-2"
146 licenseRef := "LicenseRef-MIT-Style-1"
147 deprecated := "eCos-2.0"
148 expression := "MIT AND Apache-2.0"
149 licenseWithException := "GPL-2.0-or-later WITH Bison-exception-2.2"
150
151 tests := []struct {
152 name string
153 licenses []string
154 options ValidateLicensesOptions
155 valid bool
156 invalidLicenses []string
157 }{
158 {
159 name: "Defaults allow deprecated, refs, and expressions",
160 licenses: []string{" MIT ", deprecated, licenseRef, documentRef, expression, licenseWithException},
161 options: ValidateLicensesOptions{},
162 valid: true,
163 invalidLicenses: []string{},
164 },
165 {
166 name: "FailDeprecatedLicenses rejects deprecated IDs",
167 licenses: []string{deprecated, "Apache-2.0"},
168 options: ValidateLicensesOptions{FailDeprecatedLicenses: true},
169 valid: false,
170 invalidLicenses: []string{deprecated},
171 },
172 {
173 name: "FailComplexExpressions rejects conjunction expressions",
174 licenses: []string{expression, licenseWithException},
175 options: ValidateLicensesOptions{FailComplexExpressions: true},
176 valid: false,
177 invalidLicenses: []string{expression},
178 },
179 {
180 name: "FailComplexExpressions does not duplicate invalid entries",
181 licenses: []string{"MIT AND APCHE-2.0"},
182 options: ValidateLicensesOptions{FailComplexExpressions: true},
183 valid: false,
184 invalidLicenses: []string{"MIT AND APCHE-2.0"},
185 },
186 {
187 name: "FailAllLicenseRefs rejects LicenseRef but allows DocumentRef",
188 licenses: []string{licenseRef, documentRef},
189 options: ValidateLicensesOptions{FailAllLicenseRefs: true},
190 valid: false,
191 invalidLicenses: []string{licenseRef},
192 },
193 {
194 name: "FailAllDocumentRefs rejects DocumentRef but allows LicenseRef",
195 licenses: []string{documentRef, licenseRef},
196 options: ValidateLicensesOptions{FailAllDocumentRefs: true},
197 valid: false,
198 invalidLicenses: []string{documentRef},
199 },
200 {
201 name: "FailAllLicenseRefs and FailAllDocumentRefs rejects any non-active atomic ref",
202 licenses: []string{licenseRef, documentRef, "CustomRef-foo"},
203 options: ValidateLicensesOptions{FailAllLicenseRefs: true, FailAllDocumentRefs: true},
204 valid: false,
205 invalidLicenses: []string{licenseRef, documentRef, "CustomRef-foo"},
206 },
207 {
208 name: "All flags together",
209 licenses: []string{deprecated, licenseRef, documentRef, expression, licenseWithException, "Apache-2.0"},
210 options: ValidateLicensesOptions{
211 FailComplexExpressions: true,
212 FailDeprecatedLicenses: true,
213 FailAllLicenseRefs: true,
214 FailAllDocumentRefs: true,
215 },
216 valid: false,
217 invalidLicenses: []string{deprecated, licenseRef, documentRef, expression},
218 },
219 }
220
221 for _, test := range tests {
222 test := test
223 t.Run(test.name, func(t *testing.T) {
224 valid, invalidLicenses := ValidateLicensesWithOptions(test.licenses, test.options)
225 assert.Equal(t, test.valid, valid)
226 assert.EqualValues(t, test.invalidLicenses, invalidLicenses)
227 })
228 }
229 }
230
231 func TestValidateAndNormalizeLicensesWithOptions_FailComplexExpressions(t *testing.T) {
232 tests := []struct {
233 name string
234 inputLicenses []string
235 options ValidateLicensesOptions
236 invalidLicenses []string
237 normalizedLicenses []string
238 }{
239 {
240 name: "Expressions rejected",
241 inputLicenses: []string{"MIT AND Apache-2.0"},
242 options: ValidateLicensesOptions{FailComplexExpressions: true},
243 invalidLicenses: []string{
244 "MIT AND Apache-2.0",
245 },
246 normalizedLicenses: []string{},
247 },
248 {
249 name: "Mixed list rejects only expressions",
250 inputLicenses: []string{"mit", "apache-2.0", "LGPL-2.1-only OR MIT"},
251 options: ValidateLicensesOptions{FailComplexExpressions: true},
252 invalidLicenses: []string{
253 "LGPL-2.1-only OR MIT",
254 },
255 normalizedLicenses: []string{"MIT", "Apache-2.0"},
256 },
257 {
258 name: "WITH exception is not treated as complex expression",
259 inputLicenses: []string{"gpl-2.0-or-later WITH Bison-exception-2.2"},
260 options: ValidateLicensesOptions{FailComplexExpressions: true},
261 invalidLicenses: []string{},
262 normalizedLicenses: []string{"GPL-2.0-or-later WITH Bison-exception-2.2"},
263 },
264 }
265
266 for _, test := range tests {
267 t.Run(test.name, func(t *testing.T) {
268 normalizedLicenses, invalidLicenses := ValidateAndNormalizeLicensesWithOptions(test.inputLicenses, test.options)
269 assert.EqualValues(t, test.invalidLicenses, invalidLicenses, "invalid licenses should match expected")
270 assert.EqualValues(t, test.normalizedLicenses, normalizedLicenses, "normalized licenses should match expected")
271 })
272 }
273 }
274
275 func TestValidateAndNormalizeLicensesWithOptions_FailDeprecatedLicenses(t *testing.T) {
276 // eCos-2.0 is a known deprecated SPDX license ID (see TestDeprecatedLicense).
277 deprecatedLicense := "eCos-2.0"
278
279 tests := []struct {
280 name string
281 inputLicenses []string
282 options ValidateLicensesOptions
283 invalidLicenses []string
284 normalizedLicenses []string
285 }{
286 {
287 name: "Deprecated license rejected",
288 inputLicenses: []string{deprecatedLicense},
289 options: ValidateLicensesOptions{FailDeprecatedLicenses: true},
290 invalidLicenses: []string{
291 deprecatedLicense,
292 },
293 normalizedLicenses: []string{},
294 },
295 {
296 name: "Mixed list rejects only deprecated licenses",
297 inputLicenses: []string{"MIT", "Apache-2.0", deprecatedLicense},
298 options: ValidateLicensesOptions{FailDeprecatedLicenses: true},
299 invalidLicenses: []string{
300 deprecatedLicense,
301 },
302 normalizedLicenses: []string{"MIT", "Apache-2.0"},
303 },
304 {
305 name: "WITH exception rejects deprecated license if FailDeprecatedLicenses is true",
306 inputLicenses: []string{deprecatedLicense + " WITH Bison-exception-2.2"},
307 options: ValidateLicensesOptions{FailDeprecatedLicenses: true},
308 invalidLicenses: []string{
309 deprecatedLicense + " WITH Bison-exception-2.2",
310 },
311 normalizedLicenses: []string{},
312 },
313 {
314 name: "WITH exception allows deprecated license if FailDeprecatedLicenses is false",
315 inputLicenses: []string{deprecatedLicense + " WITH Bison-exception-2.2"},
316 options: ValidateLicensesOptions{FailDeprecatedLicenses: false},
317 invalidLicenses: []string{},
318 normalizedLicenses: []string{deprecatedLicense + " WITH Bison-exception-2.2"},
319 },
320 }
321
322 for _, test := range tests {
323 t.Run(test.name, func(t *testing.T) {
324 normalizedLicenses, invalidLicenses := ValidateAndNormalizeLicensesWithOptions(test.inputLicenses, test.options)
325 assert.EqualValues(t, test.invalidLicenses, invalidLicenses, "invalid licenses should match expected")
326 assert.EqualValues(t, test.normalizedLicenses, normalizedLicenses, "normalized licenses should match expected")
327 })
328 }
329 }
330
331 func TestValidateAndNormalizeLicensesWithOptions_AllOptions(t *testing.T) {
332 documentRef := "DocumentRef-spdx-tool-1.2:LicenseRef-MIT-Style-2"
333 licenseRef := "LicenseRef-MIT-Style-1"
334 deprecated := "eCos-2.0"
335 expression := "MIT AND Apache-2.0"
336 licenseWithException := "GPL-2.0-or-later WITH Bison-exception-2.2"
337
338 tests := []struct {
339 name string
340 inputLicenses []string
341 options ValidateLicensesOptions
342 invalidLicenses []string
343 normalizedLicenses []string
344 }{
345 {
346 name: "Dedups",
347 inputLicenses: []string{" MIT ", "MIT", "mit"},
348 options: ValidateLicensesOptions{},
349 invalidLicenses: []string{},
350 normalizedLicenses: []string{"MIT"},
351 },
352 {
353 name: "Defaults allow deprecated, refs, and expressions",
354 inputLicenses: []string{" MIT ", deprecated, "licenseref-mine-MyLicense", licenseRef, documentRef, expression, licenseWithException},
355 options: ValidateLicensesOptions{},
356 invalidLicenses: []string{"licenseref-mine-MyLicense"},
357 normalizedLicenses: []string{"MIT", deprecated, licenseRef, documentRef, expression, licenseWithException},
358 },
359 {
360 name: "FailDeprecatedLicenses rejects deprecated IDs",
361 inputLicenses: []string{deprecated, "Apache-2.0"},
362 options: ValidateLicensesOptions{FailDeprecatedLicenses: true},
363 invalidLicenses: []string{deprecated},
364 normalizedLicenses: []string{"Apache-2.0"},
365 },
366 {
367 name: "FailComplexExpressions rejects conjunction expressions",
368 inputLicenses: []string{expression, licenseWithException},
369 options: ValidateLicensesOptions{FailComplexExpressions: true},
370 invalidLicenses: []string{expression},
371 normalizedLicenses: []string{licenseWithException},
372 },
373 {
374 name: "FailComplexExpressions does not duplicate invalid entries",
375 inputLicenses: []string{"MIT AND APCHE-2.0"},
376 options: ValidateLicensesOptions{FailComplexExpressions: true},
377 invalidLicenses: []string{"MIT AND APCHE-2.0"},
378 normalizedLicenses: []string{},
379 },
380 {
381 name: "FailAllLicenseRefs rejects LicenseRef but allows DocumentRef",
382 inputLicenses: []string{licenseRef, documentRef},
383 options: ValidateLicensesOptions{FailAllLicenseRefs: true},
384 invalidLicenses: []string{licenseRef},
385 normalizedLicenses: []string{documentRef},
386 },
387 {
388 name: "FailAllDocumentRefs rejects DocumentRef but allows LicenseRef",
389 inputLicenses: []string{documentRef, licenseRef},
390 options: ValidateLicensesOptions{FailAllDocumentRefs: true},
391 invalidLicenses: []string{documentRef},
392 normalizedLicenses: []string{licenseRef},
393 },
394 {
395 name: "FailAllLicenseRefs and FailAllDocumentRefs rejects any non-active atomic ref",
396 inputLicenses: []string{licenseRef, documentRef, "CustomRef-foo"},
397 options: ValidateLicensesOptions{FailAllLicenseRefs: true, FailAllDocumentRefs: true},
398 invalidLicenses: []string{licenseRef, documentRef, "CustomRef-foo"},
399 normalizedLicenses: []string{},
400 },
401 {
402 name: "All flags together",
403 inputLicenses: []string{deprecated, licenseRef, documentRef, expression, licenseWithException, "Apache-2.0"},
404 options: ValidateLicensesOptions{
405 FailComplexExpressions: true,
406 FailDeprecatedLicenses: true,
407 FailAllLicenseRefs: true,
408 FailAllDocumentRefs: true,
409 },
410 invalidLicenses: []string{deprecated, licenseRef, documentRef, expression},
411 normalizedLicenses: []string{licenseWithException, "Apache-2.0"},
412 },
413 }
414
415 for _, test := range tests {
416 test := test
417 t.Run(test.name, func(t *testing.T) {
418 normalizedLicenses, invalidLicenses := ValidateAndNormalizeLicensesWithOptions(test.inputLicenses, test.options)
419 assert.EqualValues(t, test.invalidLicenses, invalidLicenses, "invalid licenses should match expected")
420 assert.EqualValues(t, test.normalizedLicenses, normalizedLicenses, "normalized licenses should match expected")
421 })
422 }
423 }
424
425 // TestSatisfiesSingle lets you quickly test a single call to Satisfies with a specific license expression and allowed list of licenses.
426 // To test a different expression, change the expression, allowed licenses, and expected result in the function body.
427 // TO RUN: go test ./expression -run TestSatisfiesSingle
428 func TestSatisfiesSingle(t *testing.T) {
429 // Update these to test a different expression.
430 expression := "BSD-3-Clause AND GPL-2.0"
431 allowedList := []string{"BSD-3-Clause", "GPL-2.0"}
432 expectedResult := true
433
434 // Run the test.
435 actualResult, err := Satisfies(expression, allowedList)
436 assert.NoError(t, err)
437 assert.Equal(t, expectedResult, actualResult)
438 }
439
440 func TestSatisfies_FastPathValidation(t *testing.T) {
441 tests := []struct {
442 name string
443 repoExpression string
444 allowedList []string
445 satisfied bool
446 expectErr bool
447 expectedErr string
448 }{
449 {
450 name: "MIT trims whitespace",
451 repoExpression: " MIT \t",
452 allowedList: []string{"MIT"},
453 satisfied: true,
454 },
455 {
456 name: "Atomic deprecated license matches allowed list",
457 repoExpression: " eCos-2.0 ",
458 allowedList: []string{"ECOS-2.0"},
459 satisfied: true,
460 },
461 {
462 name: "Atomic deprecated license not in allowed list",
463 repoExpression: "eCos-2.0",
464 allowedList: []string{"MIT"},
465 satisfied: false,
466 },
467 {
468 name: "Single license WITH exception exact match",
469 repoExpression: "GPL-2.0-or-later WITH Bison-exception-2.2",
470 allowedList: []string{"GPL-2.0-or-later WITH Bison-exception-2.2"},
471 satisfied: true,
472 },
473 {
474 name: "Single license WITH exception not in allowed list",
475 repoExpression: "GPL-2.0-or-later WITH Bison-exception-2.2",
476 allowedList: []string{"MIT"},
477 satisfied: false,
478 },
479 {
480 name: "Single license WITH exception matches allow list ignoring case",
481 repoExpression: "gpl-2.0-or-later with bison-exception-2.2",
482 allowedList: []string{"GPL-2.0-or-later WITH Bison-exception-2.2"},
483 satisfied: true,
484 },
485 {
486 name: "Single license WITH invalid exception returns error",
487 repoExpression: "GPL-2.0-or-later WITH NOT-A-REAL-EXCEPTION",
488 allowedList: []string{"GPL-2.0-or-later WITH NOT-A-REAL-EXCEPTION"},
489 expectErr: true,
490 expectedErr: "unknown license 'NOT-A-REAL-EXCEPTION' at offset 22",
491 },
492 {
493 name: "Single license WITH invalid license part returns error",
494 repoExpression: "NOT-A-REAL-LICENSE WITH Bison-exception-2.2",
495 allowedList: []string{"NOT-A-REAL-LICENSE WITH Bison-exception-2.2"},
496 expectErr: true,
497 expectedErr: "unknown license 'NOT-A-REAL-LICENSE' at offset 0",
498 },
499 }
500
501 for _, test := range tests {
502 test := test
503 t.Run(test.name, func(t *testing.T) {
504 actualResult, err := Satisfies(test.repoExpression, test.allowedList)
505 if test.expectErr {
506 assert.EqualError(t, err, test.expectedErr)
507 return
508 }
509 assert.NoError(t, err)
510 assert.Equal(t, test.satisfied, actualResult)
511 })
512 }
513 }
514
515 func TestSatisfies(t *testing.T) {
516 tests := []struct {
517 name string
518 repoExpression string
519 allowedList []string
520 satisfied bool
521 err error
522 }{
523 // TODO: Test error conditions (e.g. GPL is an invalid license, Apachie + has invalid + operator)
524 // regression tests from spdx-satisfies.js - comments for satisfies function
525 {"MIT satisfies [MIT]", "MIT", []string{"MIT"}, true, nil},
526 {"miT satisfies [MIT]", "miT", []string{"MIT"}, true, nil},
527 {"MIT satisfies [mit]", "MIT", []string{"mit"}, true, nil},
528 {"! MIT satisfies [Apache-2.0]", "MIT", []string{"Apache-2.0"}, false, nil},
529 {"err - <empty expression> satisfies MIT", "", []string{"MIT"}, false,
530 errors.New("parse error - cannot parse empty string")},
531 {"err - MIT satisfies <empty allow list>", "MIT", []string{}, false,
532 errors.New("allowedList requires at least one element, but is empty")},
533 {"err - invalid license", "NON-EXISTENT-LICENSE", []string{"MIT", "Apache-2.0"}, false,
534 errors.New("unknown license 'NON-EXISTENT-LICENSE' at offset 0")},
535 {"err - invalid license in allowed list", "Apache-1.0", []string{"NON-EXISTENT-LICENSE", "Apache-2.0"}, false,
536 errors.New("unknown license 'NON-EXISTENT-LICENSE' at offset 0")},
537
538 {"MIT satisfies [MIT, Apache-2.0]", "MIT", []string{"MIT", "Apache-2.0"}, true, nil},
539 {"MIT OR Apache-2.0 satisfies [MIT]", "MIT OR Apache-2.0", []string{"MIT"}, true, nil},
540 {"! GPL-2.0 satisfies [MIT, Apache-2.0]", "GPL-2.0", []string{"MIT", "Apache-2.0"}, false, nil},
541 {"! MIT OR Apache-2.0 satisfies [GPL-2.0]", "MIT OR Apache-2.0", []string{"GPL-2.0"}, false, nil},
542
543 {"Apache-2.0 AND MIT satisfies [MIT, APACHE-2.0]", "Apache-2.0 AND MIT", []string{"MIT", "APACHE-2.0"}, true, nil},
544 {"apache-2.0 AND mit satisfies [MIT, APACHE-2.0]", "apache-2.0 AND mit", []string{"MIT", "APACHE-2.0"}, true, nil},
545 {"Apache-2.0 AND MIT satisfies [MIT, Apache-2.0]", "Apache-2.0 AND MIT", []string{"MIT", "Apache-2.0"}, true, nil},
546 {"MIT AND Apache-2.0 satisfies [MIT, Apache-2.0]", "MIT AND Apache-2.0", []string{"MIT", "Apache-2.0"}, true, nil},
547 {"! MIT AND Apache-2.0 satisfies [MIT]", "MIT AND Apache-2.0", []string{"MIT"}, false, nil},
548 {"! GPL-2.0 satisfies [MIT, Apache-2.0]", "GPL-2.0", []string{"MIT", "Apache-2.0"}, false, nil},
549
550 {"MIT AND Apache-2.0 satisfies [MIT, Apache-1.0, Apache-2.0]", "MIT AND Apache-2.0", []string{"MIT", "Apache-1.0", "Apache-2.0"}, true, nil},
551
552 {"Apache-1.0+ satisfies [Apache-2.0]", "Apache-1.0+", []string{"Apache-2.0"}, true, nil},
553 {"Apache-1.0+ satisfies [Apache-2.0+]", "Apache-1.0+", []string{"Apache-2.0+"}, true, nil}, // TODO: Fails here but passes js
554 {"! Apache-1.0 satisfies [Apache-2.0+]", "Apache-1.0", []string{"Apache-2.0+"}, false, nil},
555 {"Apache-2.0 satisfies [Apache-2.0+]", "Apache-2.0", []string{"Apache-2.0+"}, true, nil},
556 {"! Apache-3.0 satisfies [Apache-2.0+]", "Apache-3.0", []string{"Apache-2.0+"}, false, errors.New("unknown license 'Apache-3.0' at offset 0")},
557
558 {"! Apache-1.0 satisfies [Apache-2.0-or-later]", "Apache-1.0", []string{"Apache-2.0-or-later"}, false, nil},
559 {"Apache-2.0 satisfies [Apache-2.0-or-later]", "Apache-2.0", []string{"Apache-2.0-or-later"}, true, nil},
560 {"! Apache-3.0 satisfies [Apache-2.0-or-later]", "Apache-3.0", []string{"Apache-2.0-or-later"}, false, errors.New("unknown license 'Apache-3.0' at offset 0")},
561
562 {"! Apache-1.0 satisfies [Apache-2.0-only]", "Apache-1.0", []string{"Apache-2.0-only"}, false, nil},
563 {"Apache-2.0 satisfies [Apache-2.0-only]", "Apache-2.0", []string{"Apache-2.0-only"}, true, nil},
564 {"! Apache-3.0 satisfies [Apache-2.0-only]", "Apache-3.0", []string{"Apache-2.0-only"}, false, errors.New("unknown license 'Apache-3.0' at offset 0")},
565
566 // regression tests from spdx-satisfies.js - assert statements in README
567 {"MIT satisfies [MIT]", "MIT", []string{"MIT"}, true, nil},
568
569 {"MIT satisfies [ISC, MIT]", "MIT", []string{"ISC", "MIT"}, true, nil},
570 {"Zlib satisfies [ISC, MIT, Zlib]", "Zlib", []string{"ISC", "MIT", "Zlib"}, true, nil},
571 {"! GPL-3.0 satisfies [ISC, MIT]", "GPL-3.0", []string{"ISC", "MIT"}, false, nil},
572 {"GPL-2.0 satisfies [GPL-2.0+]", "GPL-2.0", []string{"GPL-2.0+"}, true, nil}, // TODO: Fails here but passes js
573 {"GPL-2.0 satisfies [GPL-2.0-or-later]", "GPL-2.0", []string{"GPL-2.0-or-later"}, true, nil}, // TODO: Fails here and js
574 {"GPL-3.0 satisfies [GPL-2.0+]", "GPL-3.0", []string{"GPL-2.0+"}, true, nil},
575 {"GPL-1.0-or-later satisfies [GPL-2.0-or-later]", "GPL-1.0-or-later", []string{"GPL-2.0-or-later"}, true, nil},
576 {"GPL-1.0+ satisfies [GPL-2.0+]", "GPL-1.0+", []string{"GPL-2.0+"}, true, nil},
577 {"! GPL-1.0 satisfies [GPL-2.0+]", "GPL-1.0", []string{"GPL-2.0+"}, false, nil},
578 {"GPL-2.0-only satisfies [GPL-2.0-only]", "GPL-2.0-only", []string{"GPL-2.0-only"}, true, nil},
579 {"GPL-2.0 satisfies [GPL-2.0-only]", "GPL-2.0", []string{"GPL-2.0-only"}, true, nil},
580 {"GPL-2.0 AND GPL-2.0-only satisfies [GPL-2.0-only]", "GPL-2.0 AND GPL-2.0-only", []string{"GPL-2.0-only"}, true, nil},
581 {"GPL-3.0-only satisfies [GPL-2.0+]", "GPL-3.0-only", []string{"GPL-2.0+"}, true, nil},
582
583 {"! GPL-2.0 satisfies [GPL-2.0+ WITH Bison-exception-2.2]",
584 "GPL-2.0", []string{"GPL-2.0+ WITH Bison-exception-2.2"}, false, nil},
585 {"GPL-3.0 WITH Bison-exception-2.2 satisfies [GPL-2.0+ WITH Bison-exception-2.2]",
586 "GPL-3.0 WITH Bison-exception-2.2", []string{"GPL-2.0+ WITH Bison-exception-2.2"}, true, nil},
587
588 {"(MIT OR GPL-2.0) satisfies [ISC, MIT]", "(MIT OR GPL-2.0)", []string{"ISC", "MIT"}, true, nil},
589 {"(MIT AND GPL-2.0) satisfies [MIT, GPL-2.0]", "(MIT AND GPL-2.0)", []string{"MIT", "GPL-2.0"}, true, nil},
590 {"MIT AND GPL-2.0 AND ISC satisfies [MIT, GPL-2.0, ISC]",
591 "MIT AND GPL-2.0 AND ISC", []string{"MIT", "GPL-2.0", "ISC"}, true, nil},
592 {"MIT AND GPL-2.0 AND ISC satisfies [ISC, GPL-2.0, MIT]",
593 "MIT AND GPL-2.0 AND ISC", []string{"ISC", "GPL-2.0", "MIT"}, true, nil},
594 {"(MIT OR GPL-2.0) AND ISC satisfies [MIT, ISC]",
595 "(MIT OR GPL-2.0) AND ISC", []string{"MIT", "ISC"}, true, nil},
596 {"MIT AND ISC satisfies [MIT, GPL-2.0, ISC]",
597 "MIT AND ISC", []string{"MIT", "GPL-2.0", "ISC"}, true, nil},
598 {"(MIT OR Apache-2.0) AND (ISC OR GPL-2.0) satisfies [Apache-2.0, ISC]",
599 "(MIT OR Apache-2.0) AND (ISC OR GPL-2.0)", []string{"Apache-2.0", "ISC"}, true, nil},
600 {"(MIT AND GPL-2.0) satisfies [MIT, GPL-2.0]",
601 "(MIT AND GPL-2.0)", []string{"MIT", "GPL-2.0"}, true, nil},
602 {"(MIT AND GPL-2.0) satisfies [GPL-2.0, MIT]",
603 "(MIT AND GPL-2.0)", []string{"GPL-2.0", "MIT"}, true, nil},
604 {"MIT satisfies [GPL-2.0, MIT, MIT, ISC]",
605 "MIT", []string{"GPL-2.0", "MIT", "MIT", "ISC"}, true, nil},
606 {"MIT AND ICU satisfies [MIT, GPL-2.0, ISC, Apache-2.0, ICU]",
607 "MIT AND ICU", []string{"MIT", "GPL-2.0", "ISC", "Apache-2.0", "ICU"}, true, nil}, // TODO: This says true and the js version returns true, but it shouldn't.
608 {"! (MIT AND GPL-2.0) satisfies [ISC, GPL-2.0]",
609 "(MIT AND GPL-2.0)", []string{"ISC", "GPL-2.0"}, false, nil},
610 {"! MIT AND (GPL-2.0 OR ISC) satisfies [MIT]",
611 "MIT AND (GPL-2.0 OR ISC)", []string{"MIT"}, false, nil},
612 {"! (MIT OR Apache-2.0) AND (ISC OR GPL-2.0) satisfies [MIT]",
613 "(MIT OR Apache-2.0) AND (ISC OR GPL-2.0)", []string{"MIT"}, false, nil},
614 {"licenseRef is expression",
615 "LicenseRef-X-BSD-3-Clause-Golang", []string{"MIT", "Apache-2.0", "LicenseRef-X-BSD-3-Clause-Golang"}, true, nil},
616 {"licenseRef in expression",
617 "MIT AND LicenseRef-X-BSD-3-Clause-Golang", []string{"MIT", "Apache-2.0", "LicenseRef-X-BSD-3-Clause-Golang"}, true, nil},
618 {"licenseRef not in expression",
619 "MIT AND Apache-2.0", []string{"MIT", "Apache-2.0", "LicenseRef-X-BSD-3-Clause-Golang"}, true, nil},
620 {"licenseRef not allowed",
621 "MIT AND LicenseRef-X-BSD-3-Clause-Golang", []string{"MIT", "Apache-2.0"}, false, nil},
622 {"licenseRef allowed, but OTHER is not allowed",
623 "(BSD-3-Clause AND OTHER) OR (BSD-3-Clause AND LicenseRef-X-BSD-3-Clause-Golang)",
624 []string{"MIT", "Apache-2.0", "LicenseRef-X-BSD-3-Clause-Golang"}, false,
625 errors.New("unknown license 'OTHER' at offset 18")},
626 {"licenseRef with documentRef is expression",
627 "DocumentRef-spdx-tool-1.2:LicenseRef-X-BSD-3-Clause-Golang",
628 []string{"MIT", "Apache-2.0", "DocumentRef-spdx-tool-1.2:LicenseRef-X-BSD-3-Clause-Golang"}, true, nil},
629 {"licenseRef with documentRef in expression",
630 "MIT AND DocumentRef-spdx-tool-1.2:LicenseRef-X-BSD-3-Clause-Golang",
631 []string{"MIT", "Apache-2.0", "DocumentRef-spdx-tool-1.2:LicenseRef-X-BSD-3-Clause-Golang"}, true, nil},
632 {"licenseRef with documentRef not in expression",
633 "MIT AND Apache-2.0",
634 []string{"MIT", "Apache-2.0", "DocumentRef-spdx-tool-1.2:LicenseRef-X-BSD-3-Clause-Golang"}, true, nil},
635 {"licenseRef with documentRef not allowed",
636 "MIT AND DocumentRef-spdx-tool-1.2:LicenseRef-X-BSD-3-Clause-Golang",
637 []string{"MIT", "Apache-2.0"}, false, nil},
638 {"licenseRef allowed, but documentRef not allowed",
639 "MIT AND DocumentRef-spdx-tool-1.2:LicenseRef-X-BSD-3-Clause-Golang",
640 []string{"MIT", "Apache-2.0", "LicenseRef-X-BSD-3-Clause-Golang"}, false, nil},
641 {"licenseRef alone not allowed, but with documentRef allowed",
642 "MIT AND LicenseRef-X-BSD-3-Clause-Golang",
643 []string{"MIT", "Apache-2.0", "DocumentRef-spdx-tool-1.2:LicenseRef-X-BSD-3-Clause-Golang"}, false, nil},
644 }
645
646 for _, test := range tests {
647 test := test
648 t.Run(test.name, func(t *testing.T) {
649 satisfied, err := Satisfies(test.repoExpression, test.allowedList)
650 assert.Equal(t, test.err, err)
651 assert.Equal(t, test.satisfied, satisfied)
652 })
653 }
654 }
655
656 func TestExpand(t *testing.T) {
657 // TODO: Add tests for licenses that include plus and/or exception.
658 // TODO: Add tests for license ref and document ref.
659 tests := []struct {
660 name string
661 node *node
662 result [][]*node
663 }{
664 {singleLicense().name, singleLicense().node, singleLicense().sorted},
665 {orExpression().name, orExpression().node, orExpression().sorted},
666 {orAndExpression().name, orAndExpression().node, orAndExpression().sorted},
667 {orOPAndCPExpression().name, orOPAndCPExpression().node, orOPAndCPExpression().sorted},
668 {andOrExpression().name, andOrExpression().node, andOrExpression().sorted},
669 {orAndOrExpression().name, orAndOrExpression().node, orAndOrExpression().sorted},
670 {orOPAndCPOrExpression().name, orOPAndCPOrExpression().node, orOPAndCPOrExpression().sorted},
671 {orOrOrExpression().name, orOrOrExpression().node, orOrOrExpression().sorted},
672 {andOrAndExpression().name, andOrAndExpression().node, andOrAndExpression().sorted},
673 {oPAndCPOrOPAndCPExpression().name, oPAndCPOrOPAndCPExpression().node, oPAndCPOrOPAndCPExpression().sorted},
674 {andExpression().name, andExpression().node, andExpression().sorted},
675 {andOPOrCPExpression().name, andOPOrCPExpression().node, andOPOrCPExpression().sorted},
676 {oPOrCPAndOPOrCPExpression().name, oPOrCPAndOPOrCPExpression().node, oPOrCPAndOPOrCPExpression().sorted},
677 {andOPOrCPAndExpression().name, andOPOrCPAndExpression().node, andOPOrCPAndExpression().sorted},
678 {andAndAndExpression().name, andAndAndExpression().node, andAndAndExpression().sorted},
679 }
680
681 for _, test := range tests {
682 test := test
683 t.Run(test.name, func(t *testing.T) {
684 expandResult := test.node.expand(true)
685 assert.Equal(t, test.result, expandResult)
686 })
687 }
688 }
689
690 func TestExpandOr(t *testing.T) {
691 tests := []struct {
692 name string
693 node *node
694 expanded [][]*node
695 }{
696 {orExpression().name, orExpression().node, orExpression().expanded},
697 {orAndExpression().name, orAndExpression().node, orAndExpression().expanded},
698 {orOPAndCPExpression().name, orOPAndCPExpression().node, orOPAndCPExpression().expanded},
699 {andOrExpression().name, andOrExpression().node, andOrExpression().expanded},
700 {orAndOrExpression().name, orAndOrExpression().node, orAndOrExpression().expanded},
701 {orOPAndCPOrExpression().name, orOPAndCPOrExpression().node, orOPAndCPOrExpression().expanded},
702 {orOrOrExpression().name, orOrOrExpression().node, orOrOrExpression().expanded},
703 {andOrAndExpression().name, andOrAndExpression().node, andOrAndExpression().expanded},
704 {oPAndCPOrOPAndCPExpression().name, oPAndCPOrOPAndCPExpression().node, oPAndCPOrOPAndCPExpression().expanded},
705
706 // TODO: Uncomment kitchen sink test when license plus, exception, license ref, and document ref are supported.
707 // {"kitchen sink",
708 // // " (MIT AND Apache-1.0+) OR DocumentRef-spdx-tool-1.2:LicenseRef-MIT-Style-2 OR (GPL-2.0 WITH Bison-exception-2.2)",
709 // &node{
710 // role: expressionNode,
711 // exp: &expressionNodePartial{
712 // left: &node{
713 // role: expressionNode,
714 // exp: &expressionNodePartial{
715 // left: &node{
716 // role: licenseNode,
717 // exp: nil,
718 // lic: &licenseNodePartial{
719 // license: "MIT",
720 // hasPlus: false,
721 // hasException: false,
722 // exception: "",
723 // },
724 // ref: nil,
725 // },
726 // conjunction: "and",
727 // right: &node{
728 // role: licenseNode,
729 // exp: nil,
730 // lic: &licenseNodePartial{
731 // license: "Apache-1.0",
732 // hasPlus: true,
733 // hasException: false,
734 // exception: "",
735 // },
736 // ref: nil,
737 // },
738 // },
739 // lic: nil,
740 // ref: nil,
741 // },
742 // conjunction: "or",
743 // right: &node{
744 // role: expressionNode,
745 // exp: &expressionNodePartial{
746 // left: &node{
747 // role: licenseRefNode,
748 // exp: nil,
749 // lic: nil,
750 // ref: &referenceNodePartial{
751 // hasDocumentRef: true,
752 // documentRef: "spdx-tool-1.2",
753 // licenseRef: "MIT-Style-2",
754 // },
755 // },
756 // conjunction: "or",
757 // right: &node{
758 // role: licenseNode,
759 // exp: nil,
760 // lic: &licenseNodePartial{
761 // license: "GPL-2.0",
762 // hasPlus: false,
763 // hasException: true,
764 // exception: "Bison-exception-2.2",
765 // },
766 // ref: nil,
767 // },
768 // },
769 // lic: nil,
770 // ref: nil,
771 // },
772 // },
773 // lic: nil,
774 // ref: nil,
775 // },
776 // // [][]string{{"MIT", "Apache-1.0+"}, {"DocumentRef-spdx-tool-1.2:LicenseRef-MIT-Style-2"}, {"GPL-2.0 with Bison-exception-2.2"}}},
777 // [][]*node{
778 // {
779 // {
780 // role: licenseNode,
781 // exp: nil,
782 // lic: &licenseNodePartial{
783 // license: "MIT", hasPlus: false,
784 // hasException: false, exception: ""},
785 // ref: nil,
786 // },
787 // {
788 // role: licenseNode,
789 // exp: nil,
790 // lic: &licenseNodePartial{
791 // license: "Apache-1.0", hasPlus: true,
792 // hasException: false, exception: ""},
793 // ref: nil,
794 // },
795 // },
796 // {
797 // {
798 // role: licenseRefNode,
799 // exp: nil,
800 // lic: nil,
801 // ref: &referenceNodePartial{
802 // hasDocumentRef: true,
803 // documentRef: "spdx-tool-1.2",
804 // licenseRef: "MIT-Style-2",
805 // },
806 // },
807 // {
808 // role: licenseNode,
809 // exp: nil,
810 // lic: &licenseNodePartial{
811 // license: "GPL-2.0",
812 // hasPlus: false,
813 // hasException: true,
814 // exception: "Bison-exception-2.2",
815 // },
816 // ref: nil,
817 // },
818 // },
819 // },
820 // },
821 }
822
823 for _, test := range tests {
824 test := test
825 t.Run(test.name, func(t *testing.T) {
826 expanded := test.node.expandOr()
827 assert.Equal(t, test.expanded, expanded)
828 })
829 }
830 }
831
832 func TestExpandAnd(t *testing.T) {
833 tests := []struct {
834 name string
835 node *node
836 expanded [][]*node
837 }{
838 {andExpression().name, andExpression().node, andExpression().expanded},
839 {andOPOrCPExpression().name, andOPOrCPExpression().node, andOPOrCPExpression().expanded},
840 {oPOrCPAndOPOrCPExpression().name, oPOrCPAndOPOrCPExpression().node, oPOrCPAndOPOrCPExpression().expanded},
841 {andOPOrCPAndExpression().name, andOPOrCPAndExpression().node, andOPOrCPAndExpression().expanded},
842 {andAndAndExpression().name, andAndAndExpression().node, andAndAndExpression().expanded},
843 }
844
845 for _, test := range tests {
846 test := test
847 t.Run(test.name, func(t *testing.T) {
848 expandAndResult := test.node.expandAnd()
849 assert.Equal(t, test.expanded, expandAndResult)
850 })
851 }
852 }
853
854 type testCaseData struct {
855 name string
856 expression string
857 node *node
858 expanded [][]*node
859 sorted [][]*node
860 }
861
862 func singleLicense() testCaseData {
863 return testCaseData{
864 name: "Single License",
865 expression: "MIT",
866 node: &node{
867 role: licenseNode,
868 exp: nil,
869 lic: &licenseNodePartial{
870 license: "MIT", hasPlus: false,
871 hasException: false, exception: ""},
872 ref: nil,
873 },
874 expanded: [][]*node{
875 {
876 {
877 role: licenseNode,
878 exp: nil,
879 lic: &licenseNodePartial{
880 license: "MIT", hasPlus: false,
881 hasException: false, exception: ""},
882 ref: nil,
883 },
884 },
885 },
886 sorted: [][]*node{
887 {
888 {
889 role: licenseNode,
890 exp: nil,
891 lic: &licenseNodePartial{
892 license: "MIT", hasPlus: false,
893 hasException: false, exception: ""},
894 ref: nil,
895 },
896 },
897 },
898 }
899 }
900 func orExpression() testCaseData {
901 return testCaseData{
902 name: "OR Expression",
903 expression: "MIT OR Apache-2.0",
904 node: &node{
905 role: expressionNode,
906 exp: &expressionNodePartial{
907 left: &node{
908 role: licenseNode,
909 exp: nil,
910 lic: &licenseNodePartial{
911 license: "MIT", hasPlus: false,
912 hasException: false, exception: ""},
913 ref: nil,
914 },
915 conjunction: "or",
916 right: &node{
917 role: licenseNode,
918 exp: nil,
919 lic: &licenseNodePartial{
920 license: "Apache-2.0", hasPlus: false,
921 hasException: false, exception: ""},
922 ref: nil,
923 },
924 },
925 lic: nil,
926 ref: nil,
927 },
928 expanded: [][]*node{
929 {
930 {
931 role: licenseNode,
932 exp: nil,
933 lic: &licenseNodePartial{
934 license: "MIT", hasPlus: false,
935 hasException: false, exception: ""},
936 ref: nil,
937 },
938 },
939 {
940 {
941 role: licenseNode,
942 exp: nil,
943 lic: &licenseNodePartial{
944 license: "Apache-2.0", hasPlus: false,
945 hasException: false, exception: ""},
946 ref: nil,
947 },
948 },
949 },
950 sorted: [][]*node{
951 {
952 {
953 role: licenseNode,
954 exp: nil,
955 lic: &licenseNodePartial{
956 license: "Apache-2.0", hasPlus: false,
957 hasException: false, exception: ""},
958 ref: nil,
959 },
960 },
961 {
962 {
963 role: licenseNode,
964 exp: nil,
965 lic: &licenseNodePartial{
966 license: "MIT", hasPlus: false,
967 hasException: false, exception: ""},
968 ref: nil,
969 },
970 },
971 },
972 }
973 }
974
975 func orAndExpression() testCaseData {
976 return testCaseData{
977 name: "OR-AND Expression",
978 expression: "MIT OR Apache-2.0 AND GPL-2.0",
979 node: &node{
980 role: expressionNode,
981 exp: &expressionNodePartial{
982 left: &node{
983 role: licenseNode,
984 exp: nil,
985 lic: &licenseNodePartial{
986 license: "MIT", hasPlus: false,
987 hasException: false, exception: ""},
988 ref: nil,
989 },
990 conjunction: "or",
991 right: &node{
992 exp: &expressionNodePartial{
993 left: &node{
994 role: licenseNode,
995 exp: nil,
996 lic: &licenseNodePartial{
997 license: "Apache-2.0",
998 hasPlus: false,
999 hasException: false,
1000 exception: "",
1001 },
1002 ref: nil,
1003 },
1004 conjunction: "and",
1005 right: &node{
1006 role: licenseNode,
1007 exp: nil,
1008 lic: &licenseNodePartial{
1009 license: "GPL-2.0",
1010 hasPlus: false,
1011 hasException: false,
1012 exception: "",
1013 },
1014 ref: nil,
1015 },
1016 },
1017 lic: nil,
1018 ref: nil,
1019 },
1020 },
1021 lic: nil,
1022 ref: nil,
1023 },
1024 expanded: [][]*node{
1025 {
1026 {
1027 role: licenseNode,
1028 exp: nil,
1029 lic: &licenseNodePartial{
1030 license: "MIT", hasPlus: false,
1031 hasException: false, exception: ""},
1032 ref: nil,
1033 },
1034 },
1035 {
1036 {
1037 role: licenseNode,
1038 exp: nil,
1039 lic: &licenseNodePartial{
1040 license: "Apache-2.0", hasPlus: false,
1041 hasException: false, exception: ""},
1042 ref: nil,
1043 },
1044 {
1045 role: licenseNode,
1046 exp: nil,
1047 lic: &licenseNodePartial{
1048 license: "GPL-2.0", hasPlus: false,
1049 hasException: false, exception: ""},
1050 ref: nil,
1051 },
1052 },
1053 },
1054 sorted: [][]*node{
1055 {
1056 {
1057 role: licenseNode,
1058 exp: nil,
1059 lic: &licenseNodePartial{
1060 license: "Apache-2.0", hasPlus: false,
1061 hasException: false, exception: ""},
1062 ref: nil,
1063 },
1064 {
1065 role: licenseNode,
1066 exp: nil,
1067 lic: &licenseNodePartial{
1068 license: "GPL-2.0", hasPlus: false,
1069 hasException: false, exception: ""},
1070 ref: nil,
1071 },
1072 },
1073 {
1074 {
1075 role: licenseNode,
1076 exp: nil,
1077 lic: &licenseNodePartial{
1078 license: "MIT", hasPlus: false,
1079 hasException: false, exception: ""},
1080 ref: nil,
1081 },
1082 },
1083 },
1084 }
1085 }
1086
1087 func orOPAndCPExpression() testCaseData {
1088 return testCaseData{
1089 name: "OR(AND) Expression",
1090 expression: "MIT OR (Apache-2.0 AND GPL-2.0)",
1091 node: &node{
1092 role: expressionNode,
1093 exp: &expressionNodePartial{
1094 left: &node{
1095 role: licenseNode,
1096 exp: nil,
1097 lic: &licenseNodePartial{
1098 license: "MIT", hasPlus: false,
1099 hasException: false, exception: ""},
1100 ref: nil,
1101 },
1102 conjunction: "or",
1103 right: &node{
1104 exp: &expressionNodePartial{
1105 left: &node{
1106 role: licenseNode,
1107 exp: nil,
1108 lic: &licenseNodePartial{
1109 license: "Apache-2.0",
1110 hasPlus: false,
1111 hasException: false,
1112 exception: "",
1113 },
1114 ref: nil,
1115 },
1116 conjunction: "and",
1117 right: &node{
1118 role: licenseNode,
1119 exp: nil,
1120 lic: &licenseNodePartial{
1121 license: "GPL-2.0",
1122 hasPlus: false,
1123 hasException: false,
1124 exception: "",
1125 },
1126 ref: nil,
1127 },
1128 },
1129 lic: nil,
1130 ref: nil,
1131 },
1132 },
1133 lic: nil,
1134 ref: nil,
1135 },
1136 expanded: [][]*node{
1137 {
1138 {
1139 role: licenseNode,
1140 exp: nil,
1141 lic: &licenseNodePartial{
1142 license: "MIT", hasPlus: false,
1143 hasException: false, exception: ""},
1144 ref: nil,
1145 },
1146 },
1147 {
1148 {
1149 role: licenseNode,
1150 exp: nil,
1151 lic: &licenseNodePartial{
1152 license: "Apache-2.0", hasPlus: false,
1153 hasException: false, exception: ""},
1154 ref: nil,
1155 },
1156 {
1157 role: licenseNode,
1158 exp: nil,
1159 lic: &licenseNodePartial{
1160 license: "GPL-2.0", hasPlus: false,
1161 hasException: false, exception: ""},
1162 ref: nil,
1163 },
1164 },
1165 },
1166 sorted: [][]*node{
1167 {
1168 {
1169 role: licenseNode,
1170 exp: nil,
1171 lic: &licenseNodePartial{
1172 license: "Apache-2.0", hasPlus: false,
1173 hasException: false, exception: ""},
1174 ref: nil,
1175 },
1176 {
1177 role: licenseNode,
1178 exp: nil,
1179 lic: &licenseNodePartial{
1180 license: "GPL-2.0", hasPlus: false,
1181 hasException: false, exception: ""},
1182 ref: nil,
1183 },
1184 },
1185 {
1186 {
1187 role: licenseNode,
1188 exp: nil,
1189 lic: &licenseNodePartial{
1190 license: "MIT", hasPlus: false,
1191 hasException: false, exception: ""},
1192 ref: nil,
1193 },
1194 },
1195 },
1196 }
1197 }
1198
1199 func andOrExpression() testCaseData {
1200 return testCaseData{
1201 name: "AND-OR Expression",
1202 expression: "MIT AND Apache-2.0 OR GPL-2.0",
1203 node: &node{
1204 role: expressionNode,
1205 exp: &expressionNodePartial{
1206 left: &node{
1207 exp: &expressionNodePartial{
1208 left: &node{
1209 role: licenseNode,
1210 exp: nil,
1211 lic: &licenseNodePartial{
1212 license: "MIT",
1213 hasPlus: false,
1214 hasException: false,
1215 exception: "",
1216 },
1217 ref: nil,
1218 },
1219 conjunction: "and",
1220 right: &node{
1221 role: licenseNode,
1222 exp: nil,
1223 lic: &licenseNodePartial{
1224 license: "Apache-2.0",
1225 hasPlus: false,
1226 hasException: false,
1227 exception: "",
1228 },
1229 ref: nil,
1230 },
1231 },
1232 lic: nil,
1233 ref: nil,
1234 },
1235 conjunction: "or",
1236 right: &node{
1237 role: licenseNode,
1238 exp: nil,
1239 lic: &licenseNodePartial{
1240 license: "GPL-2.0", hasPlus: false,
1241 hasException: false, exception: ""},
1242 ref: nil,
1243 },
1244 },
1245 lic: nil,
1246 ref: nil,
1247 },
1248 expanded: [][]*node{
1249 {
1250 {
1251 role: licenseNode,
1252 exp: nil,
1253 lic: &licenseNodePartial{
1254 license: "MIT", hasPlus: false,
1255 hasException: false, exception: ""},
1256 ref: nil,
1257 },
1258 {
1259 role: licenseNode,
1260 exp: nil,
1261 lic: &licenseNodePartial{
1262 license: "Apache-2.0", hasPlus: false,
1263 hasException: false, exception: ""},
1264 ref: nil,
1265 },
1266 },
1267 {
1268 {
1269 role: licenseNode,
1270 exp: nil,
1271 lic: &licenseNodePartial{
1272 license: "GPL-2.0", hasPlus: false,
1273 hasException: false, exception: ""},
1274 ref: nil,
1275 },
1276 },
1277 },
1278 sorted: [][]*node{
1279 {
1280 {
1281 role: licenseNode,
1282 exp: nil,
1283 lic: &licenseNodePartial{
1284 license: "Apache-2.0", hasPlus: false,
1285 hasException: false, exception: ""},
1286 ref: nil,
1287 },
1288 {
1289 role: licenseNode,
1290 exp: nil,
1291 lic: &licenseNodePartial{
1292 license: "MIT", hasPlus: false,
1293 hasException: false, exception: ""},
1294 ref: nil,
1295 },
1296 },
1297 {
1298 {
1299 role: licenseNode,
1300 exp: nil,
1301 lic: &licenseNodePartial{
1302 license: "GPL-2.0", hasPlus: false,
1303 hasException: false, exception: ""},
1304 ref: nil,
1305 },
1306 },
1307 },
1308 }
1309 }
1310
1311 func orAndOrExpression() testCaseData {
1312 return testCaseData{
1313 name: "OR-AND-OR Expression",
1314 expression: "MIT OR ISC AND Apache-2.0 OR GPL-2.0",
1315 node: &node{
1316 role: expressionNode,
1317 exp: &expressionNodePartial{
1318 left: &node{
1319 role: licenseNode,
1320 exp: nil,
1321 lic: &licenseNodePartial{
1322 license: "MIT",
1323 hasPlus: false,
1324 hasException: false,
1325 exception: "",
1326 },
1327 ref: nil,
1328 },
1329 conjunction: "or",
1330 right: &node{
1331 exp: &expressionNodePartial{
1332 left: &node{
1333 role: expressionNode,
1334 exp: &expressionNodePartial{
1335 left: &node{
1336 role: licenseNode,
1337 exp: nil,
1338 lic: &licenseNodePartial{
1339 license: "ISC",
1340 hasPlus: false,
1341 hasException: false,
1342 exception: "",
1343 },
1344 ref: nil,
1345 },
1346 conjunction: "and",
1347 right: &node{
1348 role: licenseNode,
1349 exp: nil,
1350 lic: &licenseNodePartial{
1351 license: "Apache-2.0",
1352 hasPlus: false,
1353 hasException: false,
1354 exception: "",
1355 },
1356 ref: nil,
1357 },
1358 },
1359 lic: nil,
1360 ref: nil,
1361 },
1362 conjunction: "or",
1363 right: &node{
1364 role: licenseNode,
1365 exp: nil,
1366 lic: &licenseNodePartial{
1367 license: "GPL-2.0",
1368 hasPlus: false,
1369 hasException: false,
1370 exception: "",
1371 },
1372 ref: nil,
1373 },
1374 },
1375 lic: nil,
1376 ref: nil,
1377 },
1378 },
1379 lic: nil,
1380 ref: nil,
1381 },
1382 expanded: [][]*node{
1383 {
1384 {
1385 role: licenseNode,
1386 exp: nil,
1387 lic: &licenseNodePartial{
1388 license: "MIT", hasPlus: false,
1389 hasException: false, exception: ""},
1390 ref: nil,
1391 },
1392 },
1393 {
1394 {
1395 role: licenseNode,
1396 exp: nil,
1397 lic: &licenseNodePartial{
1398 license: "ISC", hasPlus: false,
1399 hasException: false, exception: ""},
1400 ref: nil,
1401 },
1402 {
1403 role: licenseNode,
1404 exp: nil,
1405 lic: &licenseNodePartial{
1406 license: "Apache-2.0", hasPlus: false,
1407 hasException: false, exception: ""},
1408 ref: nil,
1409 },
1410 },
1411 {
1412 {
1413 role: licenseNode,
1414 exp: nil,
1415 lic: &licenseNodePartial{
1416 license: "GPL-2.0", hasPlus: false,
1417 hasException: false, exception: ""},
1418 ref: nil,
1419 },
1420 },
1421 },
1422 sorted: [][]*node{
1423 {
1424 {
1425 role: licenseNode,
1426 exp: nil,
1427 lic: &licenseNodePartial{
1428 license: "Apache-2.0", hasPlus: false,
1429 hasException: false, exception: ""},
1430 ref: nil,
1431 },
1432 {
1433 role: licenseNode,
1434 exp: nil,
1435 lic: &licenseNodePartial{
1436 license: "ISC", hasPlus: false,
1437 hasException: false, exception: ""},
1438 ref: nil,
1439 },
1440 },
1441 {
1442 {
1443 role: licenseNode,
1444 exp: nil,
1445 lic: &licenseNodePartial{
1446 license: "GPL-2.0", hasPlus: false,
1447 hasException: false, exception: ""},
1448 ref: nil,
1449 },
1450 },
1451 {
1452 {
1453 role: licenseNode,
1454 exp: nil,
1455 lic: &licenseNodePartial{
1456 license: "MIT", hasPlus: false,
1457 hasException: false, exception: ""},
1458 ref: nil,
1459 },
1460 },
1461 },
1462 }
1463 }
1464
1465 func orOPAndCPOrExpression() testCaseData {
1466 return testCaseData{
1467 name: "OR(AND)OR Expression",
1468 expression: "MIT OR (ISC AND Apache-2.0) OR GPL-2.0",
1469 node: &node{
1470 role: expressionNode,
1471 exp: &expressionNodePartial{
1472 left: &node{
1473 role: licenseNode,
1474 exp: nil,
1475 lic: &licenseNodePartial{
1476 license: "MIT",
1477 hasPlus: false,
1478 hasException: false,
1479 exception: "",
1480 },
1481 ref: nil,
1482 },
1483 conjunction: "or",
1484 right: &node{
1485 exp: &expressionNodePartial{
1486 left: &node{
1487 role: expressionNode,
1488 exp: &expressionNodePartial{
1489 left: &node{
1490 role: licenseNode,
1491 exp: nil,
1492 lic: &licenseNodePartial{
1493 license: "ISC",
1494 hasPlus: false,
1495 hasException: false,
1496 exception: "",
1497 },
1498 ref: nil,
1499 },
1500 conjunction: "and",
1501 right: &node{
1502 role: licenseNode,
1503 exp: nil,
1504 lic: &licenseNodePartial{
1505 license: "Apache-2.0",
1506 hasPlus: false,
1507 hasException: false,
1508 exception: "",
1509 },
1510 ref: nil,
1511 },
1512 },
1513 lic: nil,
1514 ref: nil,
1515 },
1516 conjunction: "or",
1517 right: &node{
1518 role: licenseNode,
1519 exp: nil,
1520 lic: &licenseNodePartial{
1521 license: "GPL-2.0",
1522 hasPlus: false,
1523 hasException: false,
1524 exception: "",
1525 },
1526 ref: nil,
1527 },
1528 },
1529 lic: nil,
1530 ref: nil,
1531 },
1532 },
1533 lic: nil,
1534 ref: nil,
1535 },
1536 expanded: [][]*node{
1537 {
1538 {
1539 role: licenseNode,
1540 exp: nil,
1541 lic: &licenseNodePartial{
1542 license: "MIT", hasPlus: false,
1543 hasException: false, exception: ""},
1544 ref: nil,
1545 },
1546 },
1547 {
1548 {
1549 role: licenseNode,
1550 exp: nil,
1551 lic: &licenseNodePartial{
1552 license: "ISC", hasPlus: false,
1553 hasException: false, exception: ""},
1554 ref: nil,
1555 },
1556 {
1557 role: licenseNode,
1558 exp: nil,
1559 lic: &licenseNodePartial{
1560 license: "Apache-2.0", hasPlus: false,
1561 hasException: false, exception: ""},
1562 ref: nil,
1563 },
1564 },
1565 {
1566 {
1567 role: licenseNode,
1568 exp: nil,
1569 lic: &licenseNodePartial{
1570 license: "GPL-2.0", hasPlus: false,
1571 hasException: false, exception: ""},
1572 ref: nil,
1573 },
1574 },
1575 },
1576
1577 sorted: [][]*node{
1578 {
1579 {
1580 role: licenseNode,
1581 exp: nil,
1582 lic: &licenseNodePartial{
1583 license: "Apache-2.0", hasPlus: false,
1584 hasException: false, exception: ""},
1585 ref: nil,
1586 },
1587 {
1588 role: licenseNode,
1589 exp: nil,
1590 lic: &licenseNodePartial{
1591 license: "ISC", hasPlus: false,
1592 hasException: false, exception: ""},
1593 ref: nil,
1594 },
1595 },
1596 {
1597 {
1598 role: licenseNode,
1599 exp: nil,
1600 lic: &licenseNodePartial{
1601 license: "GPL-2.0", hasPlus: false,
1602 hasException: false, exception: ""},
1603 ref: nil,
1604 },
1605 },
1606 {
1607 {
1608 role: licenseNode,
1609 exp: nil,
1610 lic: &licenseNodePartial{
1611 license: "MIT", hasPlus: false,
1612 hasException: false, exception: ""},
1613 ref: nil,
1614 },
1615 },
1616 },
1617 }
1618 }
1619
1620 func orOrOrExpression() testCaseData {
1621 return testCaseData{
1622 name: "OR-OR-OR Expression",
1623 expression: "MIT OR ISC OR Apache-2.0 OR GPL-2.0",
1624 node: &node{
1625 role: expressionNode,
1626 exp: &expressionNodePartial{
1627 left: &node{
1628 role: licenseNode,
1629 exp: nil,
1630 lic: &licenseNodePartial{
1631 license: "MIT",
1632 hasPlus: false,
1633 hasException: false,
1634 exception: "",
1635 },
1636 ref: nil,
1637 },
1638 conjunction: "or",
1639 right: &node{
1640 exp: &expressionNodePartial{
1641 left: &node{
1642 role: licenseNode,
1643 exp: nil,
1644 lic: &licenseNodePartial{
1645 license: "ISC",
1646 hasPlus: false,
1647 hasException: false,
1648 exception: "",
1649 },
1650 ref: nil,
1651 },
1652 conjunction: "or",
1653 right: &node{
1654 exp: &expressionNodePartial{
1655 left: &node{
1656 role: licenseNode,
1657 exp: nil,
1658 lic: &licenseNodePartial{
1659 license: "Apache-2.0",
1660 hasPlus: false,
1661 hasException: false,
1662 exception: "",
1663 },
1664 ref: nil,
1665 },
1666 conjunction: "or",
1667 right: &node{
1668 role: licenseNode,
1669 exp: nil,
1670 lic: &licenseNodePartial{
1671 license: "GPL-2.0",
1672 hasPlus: false,
1673 hasException: false,
1674 exception: "",
1675 },
1676 ref: nil,
1677 },
1678 },
1679 lic: nil,
1680 ref: nil,
1681 },
1682 },
1683 lic: nil,
1684 ref: nil,
1685 },
1686 },
1687 lic: nil,
1688 ref: nil,
1689 },
1690 expanded: [][]*node{
1691 {
1692 {
1693 role: licenseNode,
1694 exp: nil,
1695 lic: &licenseNodePartial{
1696 license: "MIT", hasPlus: false,
1697 hasException: false, exception: ""},
1698 ref: nil,
1699 },
1700 },
1701 {
1702 {
1703 role: licenseNode,
1704 exp: nil,
1705 lic: &licenseNodePartial{
1706 license: "ISC", hasPlus: false,
1707 hasException: false, exception: ""},
1708 ref: nil,
1709 },
1710 },
1711 {
1712 {
1713 role: licenseNode,
1714 exp: nil,
1715 lic: &licenseNodePartial{
1716 license: "Apache-2.0", hasPlus: false,
1717 hasException: false, exception: ""},
1718 ref: nil,
1719 },
1720 },
1721 {
1722 {
1723 role: licenseNode,
1724 exp: nil,
1725 lic: &licenseNodePartial{
1726 license: "GPL-2.0", hasPlus: false,
1727 hasException: false, exception: ""},
1728 ref: nil,
1729 },
1730 },
1731 },
1732
1733 sorted: [][]*node{
1734 {
1735 {
1736 role: licenseNode,
1737 exp: nil,
1738 lic: &licenseNodePartial{
1739 license: "Apache-2.0", hasPlus: false,
1740 hasException: false, exception: ""},
1741 ref: nil,
1742 },
1743 },
1744 {
1745 {
1746 role: licenseNode,
1747 exp: nil,
1748 lic: &licenseNodePartial{
1749 license: "GPL-2.0", hasPlus: false,
1750 hasException: false, exception: ""},
1751 ref: nil,
1752 },
1753 },
1754 {
1755 {
1756 role: licenseNode,
1757 exp: nil,
1758 lic: &licenseNodePartial{
1759 license: "ISC", hasPlus: false,
1760 hasException: false, exception: ""},
1761 ref: nil,
1762 },
1763 },
1764 {
1765 {
1766 role: licenseNode,
1767 exp: nil,
1768 lic: &licenseNodePartial{
1769 license: "MIT", hasPlus: false,
1770 hasException: false, exception: ""},
1771 ref: nil,
1772 },
1773 },
1774 },
1775 }
1776 }
1777
1778 func andOrAndExpression() testCaseData {
1779 return testCaseData{
1780 name: "AND-OR-AND Expression",
1781 expression: "MIT AND ISC OR Apache-2.0 AND GPL-2.0",
1782 node: &node{
1783 role: expressionNode,
1784 exp: &expressionNodePartial{
1785 left: &node{
1786 exp: &expressionNodePartial{
1787 left: &node{
1788 role: licenseNode,
1789 exp: nil,
1790 lic: &licenseNodePartial{
1791 license: "MIT",
1792 hasPlus: false,
1793 hasException: false,
1794 exception: "",
1795 },
1796 ref: nil,
1797 },
1798 conjunction: "and",
1799 right: &node{
1800 role: licenseNode,
1801 exp: nil,
1802 lic: &licenseNodePartial{
1803 license: "ISC",
1804 hasPlus: false,
1805 hasException: false,
1806 exception: "",
1807 },
1808 ref: nil,
1809 },
1810 },
1811 lic: nil,
1812 ref: nil,
1813 },
1814 conjunction: "or",
1815 right: &node{
1816 exp: &expressionNodePartial{
1817 left: &node{
1818 role: licenseNode,
1819 exp: nil,
1820 lic: &licenseNodePartial{
1821 license: "Apache-2.0",
1822 hasPlus: false,
1823 hasException: false,
1824 exception: "",
1825 },
1826 ref: nil,
1827 },
1828 conjunction: "and",
1829 right: &node{
1830 role: licenseNode,
1831 exp: nil,
1832 lic: &licenseNodePartial{
1833 license: "GPL-2.0",
1834 hasPlus: false,
1835 hasException: false,
1836 exception: "",
1837 },
1838 ref: nil,
1839 },
1840 },
1841 lic: nil,
1842 ref: nil,
1843 },
1844 },
1845 lic: nil,
1846 ref: nil,
1847 },
1848 expanded: [][]*node{
1849 {
1850 {
1851 role: licenseNode,
1852 exp: nil,
1853 lic: &licenseNodePartial{
1854 license: "MIT", hasPlus: false,
1855 hasException: false, exception: ""},
1856 ref: nil,
1857 },
1858 {
1859 role: licenseNode,
1860 exp: nil,
1861 lic: &licenseNodePartial{
1862 license: "ISC", hasPlus: false,
1863 hasException: false, exception: ""},
1864 ref: nil,
1865 },
1866 },
1867 {
1868 {
1869 role: licenseNode,
1870 exp: nil,
1871 lic: &licenseNodePartial{
1872 license: "Apache-2.0", hasPlus: false,
1873 hasException: false, exception: ""},
1874 ref: nil,
1875 },
1876 {
1877 role: licenseNode,
1878 exp: nil,
1879 lic: &licenseNodePartial{
1880 license: "GPL-2.0", hasPlus: false,
1881 hasException: false, exception: ""},
1882 ref: nil,
1883 },
1884 },
1885 },
1886
1887 sorted: [][]*node{
1888 {
1889 {
1890 role: licenseNode,
1891 exp: nil,
1892 lic: &licenseNodePartial{
1893 license: "Apache-2.0", hasPlus: false,
1894 hasException: false, exception: ""},
1895 ref: nil,
1896 },
1897 {
1898 role: licenseNode,
1899 exp: nil,
1900 lic: &licenseNodePartial{
1901 license: "GPL-2.0", hasPlus: false,
1902 hasException: false, exception: ""},
1903 ref: nil,
1904 },
1905 },
1906 {
1907 {
1908 role: licenseNode,
1909 exp: nil,
1910 lic: &licenseNodePartial{
1911 license: "ISC", hasPlus: false,
1912 hasException: false, exception: ""},
1913 ref: nil,
1914 },
1915 {
1916 role: licenseNode,
1917 exp: nil,
1918 lic: &licenseNodePartial{
1919 license: "MIT", hasPlus: false,
1920 hasException: false, exception: ""},
1921 ref: nil,
1922 },
1923 },
1924 },
1925 }
1926 }
1927
1928 func oPAndCPOrOPAndCPExpression() testCaseData {
1929 return testCaseData{
1930 name: "(AND)OR(AND) Expression",
1931 expression: "(MIT AND ISC) OR (Apache-2.0 AND GPL-2.0)",
1932 node: &node{
1933 role: expressionNode,
1934 exp: &expressionNodePartial{
1935 left: &node{
1936 exp: &expressionNodePartial{
1937 left: &node{
1938 role: licenseNode,
1939 exp: nil,
1940 lic: &licenseNodePartial{
1941 license: "MIT",
1942 hasPlus: false,
1943 hasException: false,
1944 exception: "",
1945 },
1946 ref: nil,
1947 },
1948 conjunction: "and",
1949 right: &node{
1950 role: licenseNode,
1951 exp: nil,
1952 lic: &licenseNodePartial{
1953 license: "ISC",
1954 hasPlus: false,
1955 hasException: false,
1956 exception: "",
1957 },
1958 ref: nil,
1959 },
1960 },
1961 lic: nil,
1962 ref: nil,
1963 },
1964 conjunction: "or",
1965 right: &node{
1966 exp: &expressionNodePartial{
1967 left: &node{
1968 role: licenseNode,
1969 exp: nil,
1970 lic: &licenseNodePartial{
1971 license: "Apache-2.0",
1972 hasPlus: false,
1973 hasException: false,
1974 exception: "",
1975 },
1976 ref: nil,
1977 },
1978 conjunction: "and",
1979 right: &node{
1980 role: licenseNode,
1981 exp: nil,
1982 lic: &licenseNodePartial{
1983 license: "GPL-2.0",
1984 hasPlus: false,
1985 hasException: false,
1986 exception: "",
1987 },
1988 ref: nil,
1989 },
1990 },
1991 lic: nil,
1992 ref: nil,
1993 },
1994 },
1995 lic: nil,
1996 ref: nil,
1997 },
1998 expanded: [][]*node{
1999 {
2000 {
2001 role: licenseNode,
2002 exp: nil,
2003 lic: &licenseNodePartial{
2004 license: "MIT", hasPlus: false,
2005 hasException: false, exception: ""},
2006 ref: nil,
2007 },
2008 {
2009 role: licenseNode,
2010 exp: nil,
2011 lic: &licenseNodePartial{
2012 license: "ISC", hasPlus: false,
2013 hasException: false, exception: ""},
2014 ref: nil,
2015 },
2016 },
2017 {
2018 {
2019 role: licenseNode,
2020 exp: nil,
2021 lic: &licenseNodePartial{
2022 license: "Apache-2.0", hasPlus: false,
2023 hasException: false, exception: ""},
2024 ref: nil,
2025 },
2026 {
2027 role: licenseNode,
2028 exp: nil,
2029 lic: &licenseNodePartial{
2030 license: "GPL-2.0", hasPlus: false,
2031 hasException: false, exception: ""},
2032 ref: nil,
2033 },
2034 },
2035 },
2036
2037 sorted: [][]*node{
2038 {
2039 {
2040 role: licenseNode,
2041 exp: nil,
2042 lic: &licenseNodePartial{
2043 license: "Apache-2.0", hasPlus: false,
2044 hasException: false, exception: ""},
2045 ref: nil,
2046 },
2047 {
2048 role: licenseNode,
2049 exp: nil,
2050 lic: &licenseNodePartial{
2051 license: "GPL-2.0", hasPlus: false,
2052 hasException: false, exception: ""},
2053 ref: nil,
2054 },
2055 },
2056 {
2057 {
2058 role: licenseNode,
2059 exp: nil,
2060 lic: &licenseNodePartial{
2061 license: "ISC", hasPlus: false,
2062 hasException: false, exception: ""},
2063 ref: nil,
2064 },
2065 {
2066 role: licenseNode,
2067 exp: nil,
2068 lic: &licenseNodePartial{
2069 license: "MIT", hasPlus: false,
2070 hasException: false, exception: ""},
2071 ref: nil,
2072 },
2073 },
2074 },
2075 }
2076 }
2077
2078 func andExpression() testCaseData {
2079 return testCaseData{
2080 name: "AND Expression",
2081 expression: "MIT AND Apache-2.0",
2082 node: &node{
2083 role: expressionNode,
2084 exp: &expressionNodePartial{
2085 left: &node{
2086 role: licenseNode,
2087 exp: nil,
2088 lic: &licenseNodePartial{
2089 license: "MIT", hasPlus: false,
2090 hasException: false, exception: ""},
2091 ref: nil,
2092 },
2093 conjunction: "and",
2094 right: &node{
2095 role: licenseNode,
2096 exp: nil,
2097 lic: &licenseNodePartial{
2098 license: "Apache-2.0", hasPlus: false,
2099 hasException: false, exception: ""},
2100 ref: nil,
2101 },
2102 },
2103 lic: nil,
2104 ref: nil,
2105 },
2106 expanded: [][]*node{
2107 {
2108 {
2109 role: licenseNode,
2110 exp: nil,
2111 lic: &licenseNodePartial{
2112 license: "MIT", hasPlus: false,
2113 hasException: false, exception: ""},
2114 ref: nil,
2115 },
2116 {
2117 role: licenseNode,
2118 exp: nil,
2119 lic: &licenseNodePartial{
2120 license: "Apache-2.0", hasPlus: false,
2121 hasException: false, exception: ""},
2122 ref: nil,
2123 },
2124 },
2125 },
2126
2127 sorted: [][]*node{
2128 {
2129 {
2130 role: licenseNode,
2131 exp: nil,
2132 lic: &licenseNodePartial{
2133 license: "Apache-2.0", hasPlus: false,
2134 hasException: false, exception: ""},
2135 ref: nil,
2136 },
2137 {
2138 role: licenseNode,
2139 exp: nil,
2140 lic: &licenseNodePartial{
2141 license: "MIT", hasPlus: false,
2142 hasException: false, exception: ""},
2143 ref: nil,
2144 },
2145 },
2146 },
2147 }
2148 }
2149
2150 func andOPOrCPExpression() testCaseData {
2151 return testCaseData{
2152 name: "AND(OR) Expression",
2153 expression: "MIT AND (Apache-2.0 OR GPL-2.0)",
2154 node: &node{
2155 role: expressionNode,
2156 exp: &expressionNodePartial{
2157 left: &node{
2158 role: licenseNode,
2159 exp: nil,
2160 lic: &licenseNodePartial{
2161 license: "MIT",
2162 hasPlus: false,
2163 hasException: false,
2164 exception: "",
2165 },
2166 ref: nil,
2167 },
2168 conjunction: "and",
2169 right: &node{
2170 exp: &expressionNodePartial{
2171 left: &node{
2172 role: licenseNode,
2173 exp: nil,
2174 lic: &licenseNodePartial{
2175 license: "Apache-2.0",
2176 hasPlus: false,
2177 hasException: false,
2178 exception: "",
2179 },
2180 ref: nil,
2181 },
2182 conjunction: "or",
2183 right: &node{
2184 role: licenseNode,
2185 exp: nil,
2186 lic: &licenseNodePartial{
2187 license: "GPL-2.0",
2188 hasPlus: false,
2189 hasException: false,
2190 exception: "",
2191 },
2192 ref: nil,
2193 },
2194 },
2195 lic: nil,
2196 ref: nil,
2197 },
2198 },
2199 lic: nil,
2200 ref: nil,
2201 },
2202 expanded: [][]*node{
2203 {
2204 {
2205 role: licenseNode,
2206 exp: nil,
2207 lic: &licenseNodePartial{
2208 license: "MIT", hasPlus: false,
2209 hasException: false, exception: ""},
2210 ref: nil,
2211 },
2212 {
2213 role: licenseNode,
2214 exp: nil,
2215 lic: &licenseNodePartial{
2216 license: "Apache-2.0", hasPlus: false,
2217 hasException: false, exception: ""},
2218 ref: nil,
2219 },
2220 },
2221 {
2222 {
2223 role: licenseNode,
2224 exp: nil,
2225 lic: &licenseNodePartial{
2226 license: "MIT", hasPlus: false,
2227 hasException: false, exception: ""},
2228 ref: nil,
2229 },
2230 {
2231 role: licenseNode,
2232 exp: nil,
2233 lic: &licenseNodePartial{
2234 license: "GPL-2.0", hasPlus: false,
2235 hasException: false, exception: ""},
2236 ref: nil,
2237 },
2238 },
2239 },
2240
2241 sorted: [][]*node{
2242 {
2243 {
2244 role: licenseNode,
2245 exp: nil,
2246 lic: &licenseNodePartial{
2247 license: "Apache-2.0", hasPlus: false,
2248 hasException: false, exception: ""},
2249 ref: nil,
2250 },
2251 {
2252 role: licenseNode,
2253 exp: nil,
2254 lic: &licenseNodePartial{
2255 license: "MIT", hasPlus: false,
2256 hasException: false, exception: ""},
2257 ref: nil,
2258 },
2259 },
2260 {
2261 {
2262 role: licenseNode,
2263 exp: nil,
2264 lic: &licenseNodePartial{
2265 license: "GPL-2.0", hasPlus: false,
2266 hasException: false, exception: ""},
2267 ref: nil,
2268 },
2269 {
2270 role: licenseNode,
2271 exp: nil,
2272 lic: &licenseNodePartial{
2273 license: "MIT", hasPlus: false,
2274 hasException: false, exception: ""},
2275 ref: nil,
2276 },
2277 },
2278 },
2279 }
2280 }
2281
2282 func oPOrCPAndOPOrCPExpression() testCaseData {
2283 return testCaseData{
2284 name: "(OR)AND(OR) Expression",
2285 expression: "(MIT OR ISC) AND (Apache-2.0 OR GPL-2.0)",
2286 node: &node{
2287 role: expressionNode,
2288 exp: &expressionNodePartial{
2289 left: &node{
2290 exp: &expressionNodePartial{
2291 left: &node{
2292 role: licenseNode,
2293 exp: nil,
2294 lic: &licenseNodePartial{
2295 license: "MIT",
2296 hasPlus: false,
2297 hasException: false,
2298 exception: "",
2299 },
2300 ref: nil,
2301 },
2302 conjunction: "or",
2303 right: &node{
2304 role: licenseNode,
2305 exp: nil,
2306 lic: &licenseNodePartial{
2307 license: "ISC",
2308 hasPlus: false,
2309 hasException: false,
2310 exception: "",
2311 },
2312 ref: nil,
2313 },
2314 },
2315 lic: nil,
2316 ref: nil,
2317 },
2318 conjunction: "and",
2319 right: &node{
2320 exp: &expressionNodePartial{
2321 left: &node{
2322 role: licenseNode,
2323 exp: nil,
2324 lic: &licenseNodePartial{
2325 license: "Apache-2.0",
2326 hasPlus: false,
2327 hasException: false,
2328 exception: "",
2329 },
2330 ref: nil,
2331 },
2332 conjunction: "or",
2333 right: &node{
2334 role: licenseNode,
2335 exp: nil,
2336 lic: &licenseNodePartial{
2337 license: "GPL-2.0",
2338 hasPlus: false,
2339 hasException: false,
2340 exception: "",
2341 },
2342 ref: nil,
2343 },
2344 },
2345 lic: nil,
2346 ref: nil,
2347 },
2348 },
2349 lic: nil,
2350 ref: nil,
2351 },
2352 expanded: [][]*node{
2353 {
2354 {
2355 role: licenseNode,
2356 exp: nil,
2357 lic: &licenseNodePartial{
2358 license: "MIT", hasPlus: false,
2359 hasException: false, exception: ""},
2360 ref: nil,
2361 },
2362 {
2363 role: licenseNode,
2364 exp: nil,
2365 lic: &licenseNodePartial{
2366 license: "Apache-2.0", hasPlus: false,
2367 hasException: false, exception: ""},
2368 ref: nil,
2369 },
2370 },
2371 {
2372 {
2373 role: licenseNode,
2374 exp: nil,
2375 lic: &licenseNodePartial{
2376 license: "ISC", hasPlus: false,
2377 hasException: false, exception: ""},
2378 ref: nil,
2379 },
2380 {
2381 role: licenseNode,
2382 exp: nil,
2383 lic: &licenseNodePartial{
2384 license: "Apache-2.0", hasPlus: false,
2385 hasException: false, exception: ""},
2386 ref: nil,
2387 },
2388 },
2389 {
2390 {
2391 role: licenseNode,
2392 exp: nil,
2393 lic: &licenseNodePartial{
2394 license: "MIT", hasPlus: false,
2395 hasException: false, exception: ""},
2396 ref: nil,
2397 },
2398 {
2399 role: licenseNode,
2400 exp: nil,
2401 lic: &licenseNodePartial{
2402 license: "GPL-2.0", hasPlus: false,
2403 hasException: false, exception: ""},
2404 ref: nil,
2405 },
2406 },
2407 {
2408 {
2409 role: licenseNode,
2410 exp: nil,
2411 lic: &licenseNodePartial{
2412 license: "ISC", hasPlus: false,
2413 hasException: false, exception: ""},
2414 ref: nil,
2415 },
2416 {
2417 role: licenseNode,
2418 exp: nil,
2419 lic: &licenseNodePartial{
2420 license: "GPL-2.0", hasPlus: false,
2421 hasException: false, exception: ""},
2422 ref: nil,
2423 },
2424 },
2425 },
2426
2427 sorted: [][]*node{
2428 {
2429 {
2430 role: licenseNode,
2431 exp: nil,
2432 lic: &licenseNodePartial{
2433 license: "Apache-2.0", hasPlus: false,
2434 hasException: false, exception: ""},
2435 ref: nil,
2436 },
2437 {
2438 role: licenseNode,
2439 exp: nil,
2440 lic: &licenseNodePartial{
2441 license: "ISC", hasPlus: false,
2442 hasException: false, exception: ""},
2443 ref: nil,
2444 },
2445 },
2446 {
2447 {
2448 role: licenseNode,
2449 exp: nil,
2450 lic: &licenseNodePartial{
2451 license: "Apache-2.0", hasPlus: false,
2452 hasException: false, exception: ""},
2453 ref: nil,
2454 },
2455 {
2456 role: licenseNode,
2457 exp: nil,
2458 lic: &licenseNodePartial{
2459 license: "MIT", hasPlus: false,
2460 hasException: false, exception: ""},
2461 ref: nil,
2462 },
2463 },
2464 {
2465 {
2466 role: licenseNode,
2467 exp: nil,
2468 lic: &licenseNodePartial{
2469 license: "GPL-2.0", hasPlus: false,
2470 hasException: false, exception: ""},
2471 ref: nil,
2472 },
2473 {
2474 role: licenseNode,
2475 exp: nil,
2476 lic: &licenseNodePartial{
2477 license: "ISC", hasPlus: false,
2478 hasException: false, exception: ""},
2479 ref: nil,
2480 },
2481 },
2482 {
2483 {
2484 role: licenseNode,
2485 exp: nil,
2486 lic: &licenseNodePartial{
2487 license: "GPL-2.0", hasPlus: false,
2488 hasException: false, exception: ""},
2489 ref: nil,
2490 },
2491 {
2492 role: licenseNode,
2493 exp: nil,
2494 lic: &licenseNodePartial{
2495 license: "MIT", hasPlus: false,
2496 hasException: false, exception: ""},
2497 ref: nil,
2498 },
2499 },
2500 },
2501 }
2502 }
2503
2504 func andOPOrCPAndExpression() testCaseData {
2505 return testCaseData{
2506 name: "AND(OR)AND Expression",
2507 expression: "MIT AND (ISC OR Apache-2.0) AND GPL-2.0",
2508 node: &node{
2509 role: expressionNode,
2510 exp: &expressionNodePartial{
2511 left: &node{
2512 role: licenseNode,
2513 exp: nil,
2514 lic: &licenseNodePartial{
2515 license: "MIT",
2516 hasPlus: false,
2517 hasException: false,
2518 exception: "",
2519 },
2520 ref: nil,
2521 },
2522 conjunction: "and",
2523 right: &node{
2524 role: expressionNode,
2525 exp: &expressionNodePartial{
2526 left: &node{
2527 role: expressionNode,
2528 exp: &expressionNodePartial{
2529 left: &node{
2530 role: licenseNode,
2531 exp: nil,
2532 lic: &licenseNodePartial{
2533 license: "ISC",
2534 hasPlus: false,
2535 hasException: false,
2536 exception: "",
2537 },
2538 ref: nil,
2539 },
2540 conjunction: "or",
2541 right: &node{
2542 role: licenseNode,
2543 exp: nil,
2544 lic: &licenseNodePartial{
2545 license: "Apache-2.0",
2546 hasPlus: false,
2547 hasException: false,
2548 exception: "",
2549 },
2550 ref: nil,
2551 },
2552 },
2553 lic: nil,
2554 ref: nil,
2555 },
2556 conjunction: "and",
2557 right: &node{
2558 role: licenseNode,
2559 exp: nil,
2560 lic: &licenseNodePartial{
2561 license: "GPL-2.0",
2562 hasPlus: false,
2563 hasException: false,
2564 exception: "",
2565 },
2566 ref: nil,
2567 },
2568 },
2569 lic: nil,
2570 ref: nil,
2571 },
2572 },
2573 lic: nil,
2574 ref: nil,
2575 },
2576 expanded: [][]*node{
2577 {
2578 {
2579 role: licenseNode,
2580 exp: nil,
2581 lic: &licenseNodePartial{
2582 license: "MIT", hasPlus: false,
2583 hasException: false, exception: ""},
2584 ref: nil,
2585 },
2586 {
2587 role: licenseNode,
2588 exp: nil,
2589 lic: &licenseNodePartial{
2590 license: "ISC", hasPlus: false,
2591 hasException: false, exception: ""},
2592 ref: nil,
2593 },
2594 {
2595 role: licenseNode,
2596 exp: nil,
2597 lic: &licenseNodePartial{
2598 license: "GPL-2.0", hasPlus: false,
2599 hasException: false, exception: ""},
2600 ref: nil,
2601 },
2602 },
2603 {
2604 {
2605 role: licenseNode,
2606 exp: nil,
2607 lic: &licenseNodePartial{
2608 license: "MIT", hasPlus: false,
2609 hasException: false, exception: ""},
2610 ref: nil,
2611 },
2612 {
2613 role: licenseNode,
2614 exp: nil,
2615 lic: &licenseNodePartial{
2616 license: "Apache-2.0", hasPlus: false,
2617 hasException: false, exception: ""},
2618 ref: nil,
2619 },
2620 {
2621 role: licenseNode,
2622 exp: nil,
2623 lic: &licenseNodePartial{
2624 license: "GPL-2.0", hasPlus: false,
2625 hasException: false, exception: ""},
2626 ref: nil,
2627 },
2628 },
2629 },
2630
2631 sorted: [][]*node{
2632 {
2633 {
2634 role: licenseNode,
2635 exp: nil,
2636 lic: &licenseNodePartial{
2637 license: "Apache-2.0", hasPlus: false,
2638 hasException: false, exception: ""},
2639 ref: nil,
2640 },
2641 {
2642 role: licenseNode,
2643 exp: nil,
2644 lic: &licenseNodePartial{
2645 license: "GPL-2.0", hasPlus: false,
2646 hasException: false, exception: ""},
2647 ref: nil,
2648 },
2649 {
2650 role: licenseNode,
2651 exp: nil,
2652 lic: &licenseNodePartial{
2653 license: "MIT", hasPlus: false,
2654 hasException: false, exception: ""},
2655 ref: nil,
2656 },
2657 },
2658 {
2659 {
2660 role: licenseNode,
2661 exp: nil,
2662 lic: &licenseNodePartial{
2663 license: "GPL-2.0", hasPlus: false,
2664 hasException: false, exception: ""},
2665 ref: nil,
2666 },
2667 {
2668 role: licenseNode,
2669 exp: nil,
2670 lic: &licenseNodePartial{
2671 license: "ISC", hasPlus: false,
2672 hasException: false, exception: ""},
2673 ref: nil,
2674 },
2675 {
2676 role: licenseNode,
2677 exp: nil,
2678 lic: &licenseNodePartial{
2679 license: "MIT", hasPlus: false,
2680 hasException: false, exception: ""},
2681 ref: nil,
2682 },
2683 },
2684 },
2685 }
2686 }
2687
2688 func andAndAndExpression() testCaseData {
2689 return testCaseData{
2690 name: "AND-AND-AND Expression",
2691 expression: "MIT AND ISC AND Apache-2.0 AND GPL-2.0",
2692 node: &node{
2693 role: expressionNode,
2694 exp: &expressionNodePartial{
2695 left: &node{
2696 role: licenseNode,
2697 exp: nil,
2698 lic: &licenseNodePartial{
2699 license: "MIT",
2700 hasPlus: false,
2701 hasException: false,
2702 exception: "",
2703 },
2704 ref: nil,
2705 },
2706 conjunction: "and",
2707 right: &node{
2708 role: expressionNode,
2709 exp: &expressionNodePartial{
2710 left: &node{
2711 role: licenseNode,
2712 exp: nil,
2713 lic: &licenseNodePartial{
2714 license: "ISC",
2715 hasPlus: false,
2716 hasException: false,
2717 exception: "",
2718 },
2719 ref: nil,
2720 },
2721 conjunction: "and",
2722 right: &node{
2723 role: expressionNode,
2724 exp: &expressionNodePartial{
2725 left: &node{
2726 role: licenseNode,
2727 exp: nil,
2728 lic: &licenseNodePartial{
2729 license: "Apache-2.0",
2730 hasPlus: false,
2731 hasException: false,
2732 exception: "",
2733 },
2734 ref: nil,
2735 },
2736 conjunction: "and",
2737 right: &node{
2738 role: licenseNode,
2739 exp: nil,
2740 lic: &licenseNodePartial{
2741 license: "GPL-2.0",
2742 hasPlus: false,
2743 hasException: false,
2744 exception: "",
2745 },
2746 ref: nil,
2747 },
2748 },
2749 lic: nil,
2750 ref: nil,
2751 },
2752 },
2753 lic: nil,
2754 ref: nil,
2755 },
2756 },
2757 lic: nil,
2758 ref: nil,
2759 },
2760 expanded: [][]*node{
2761 {
2762 {
2763 role: licenseNode,
2764 exp: nil,
2765 lic: &licenseNodePartial{
2766 license: "MIT", hasPlus: false,
2767 hasException: false, exception: ""},
2768 ref: nil,
2769 },
2770 {
2771 role: licenseNode,
2772 exp: nil,
2773 lic: &licenseNodePartial{
2774 license: "ISC", hasPlus: false,
2775 hasException: false, exception: ""},
2776 ref: nil,
2777 },
2778 {
2779 role: licenseNode,
2780 exp: nil,
2781 lic: &licenseNodePartial{
2782 license: "Apache-2.0", hasPlus: false,
2783 hasException: false, exception: ""},
2784 ref: nil,
2785 },
2786 {
2787 role: licenseNode,
2788 exp: nil,
2789 lic: &licenseNodePartial{
2790 license: "GPL-2.0", hasPlus: false,
2791 hasException: false, exception: ""},
2792 ref: nil,
2793 },
2794 },
2795 },
2796
2797 sorted: [][]*node{
2798 {
2799 {
2800 role: licenseNode,
2801 exp: nil,
2802 lic: &licenseNodePartial{
2803 license: "Apache-2.0", hasPlus: false,
2804 hasException: false, exception: ""},
2805 ref: nil,
2806 },
2807 {
2808 role: licenseNode,
2809 exp: nil,
2810 lic: &licenseNodePartial{
2811 license: "GPL-2.0", hasPlus: false,
2812 hasException: false, exception: ""},
2813 ref: nil,
2814 },
2815 {
2816 role: licenseNode,
2817 exp: nil,
2818 lic: &licenseNodePartial{
2819 license: "ISC", hasPlus: false,
2820 hasException: false, exception: ""},
2821 ref: nil,
2822 },
2823 {
2824 role: licenseNode,
2825 exp: nil,
2826 lic: &licenseNodePartial{
2827 license: "MIT", hasPlus: false,
2828 hasException: false, exception: ""},
2829 ref: nil,
2830 },
2831 },
2832 },
2833 }
2834 }
2835
2836 func ExampleSatisfies_singleLicense() {
2837 fmt.Println(Satisfies("MIT", []string{"MIT"}))
2838 // Output: true <nil>
2839 }
2840
2841 func ExampleSatisfies_or() {
2842 fmt.Println(Satisfies("MIT OR Apache-2.0", []string{"MIT"}))
2843 // Output: true <nil>
2844 }
2845
2846 func ExampleSatisfies_orNotFound() {
2847 fmt.Println(Satisfies("MIT OR Apache-2.0", []string{"GPL-2.0"}))
2848 // Output: false <nil>
2849 }
2850
2851 func ExampleSatisfies_and() {
2852 fmt.Println(Satisfies("Apache-2.0 AND MIT", []string{"MIT", "Apache-2.0"}))
2853 // Output: true <nil>
2854 }
2855
2856 func ExampleSatisfies_andNotFound() {
2857 fmt.Println(Satisfies("MIT AND Apache-2.0", []string{"MIT"}))
2858 // Output: false <nil>
2859 }
2860
2861 func ExampleSatisfies_plus() {
2862 fmt.Println(Satisfies("Apache-2.0", []string{"Apache-1.0+"}))
2863 // Output: true <nil>
2864 }
2865
2866 func ExampleSatisfies_plusNotFound() {
2867 fmt.Println(Satisfies("Apache-1.0", []string{"Apache-2.0+"}))
2868 // Output: false <nil>
2869 }
2870
2871 func ExampleSatisfies_orLater() {
2872 fmt.Println(Satisfies("Apache-2.0", []string{"Apache-1.0-or-later"}))
2873 // Output: true <nil>
2874 }
2875
2876 func ExampleSatisfies_orLaterNotFound() {
2877 fmt.Println(Satisfies("Apache-1.0", []string{"Apache-2.0-or-later"}))
2878 // Output: false <nil>
2879 }
2880
2881 func ExampleSatisfies_only() {
2882 fmt.Println(Satisfies("Apache-1.0", []string{"Apache-1.0-only"}))
2883 // Output: true <nil>
2884 }
2885
2886 func ExampleSatisfies_onlyNotFound() {
2887 fmt.Println(Satisfies("Apache-2.0", []string{"Apache-1.0-only"}))
2888 // Output: false <nil>
2889 }
2890
2891 func ExampleSatisfies_extraAllowedLicense() {
2892 fmt.Println(Satisfies("MIT AND Apache-2.0", []string{"MIT", "Apache-1.0", "Apache-2.0"}))
2893 // Output: true <nil>
2894 }
2895
2896 func ExampleSatisfies_errorUnknownLicense() {
2897 fmt.Println(Satisfies("GPL", []string{"GPL"}))
2898 // Output: false unknown license 'GPL' at offset 0
2899 }
2900
2901 func ExampleValidateLicenses_allGood() {
2902 fmt.Println(ValidateLicenses([]string{"MIT", "Apache-2.0", "GPL-2.0"}))
2903 // Output: true []
2904 }
2905
2906 func ExampleValidateLicenses_oneBad() {
2907 fmt.Println(ValidateLicenses([]string{"MIT", "Apache-2.0", "GPL"}))
2908 // Output: false [GPL]
2909 }
2910
2911 func ExampleValidateLicenses_allBad() {
2912 fmt.Println(ValidateLicenses([]string{"MTI", "Apache--2.0", "GPL"}))
2913 // Output: false [MTI Apache--2.0 GPL]
2914 }
2915
2916 // TestValidateLicenses_BenchmarkExamples is a safety check to ensure benchmark improvements are not due
2917 // to changes in behavior of the ValidateLicenses function.
2918 func TestValidateLicenses_BenchmarkExamples(t *testing.T) {
2919 // This test is used to verify that the test expressions used in the benchmarks return expected results.
2920 // If any of the test expressions are invalid, then there is likely an issue with the benchmark results and investigation would be needed.
2921 for _, test := range validateLicensesBenchmarkScenarios {
2922 t.Run(test.name, func(t *testing.T) {
2923 valid, invalidLicenses := ValidateLicenses(test.testLicenses)
2924 assert.True(t, valid, "Expected licenses to be valid for scenario: %s", test.name)
2925 assert.Empty(t, invalidLicenses, "Expected no invalid licenses for scenario: %s", test.name)
2926 })
2927 }
2928 }