| 1 | package spdxexp |
| 2 | |
| 3 | import ( |
| 4 | "context" |
| 5 | "os" |
| 6 | "os/exec" |
| 7 | "testing" |
| 8 | "time" |
| 9 | |
| 10 | "github.com/stretchr/testify/assert" |
| 11 | ) |
| 12 | |
| 13 | const kernelHeadersLicense = `(GPL-2.0-only WITH Linux-syscall-note OR BSD-2-Clause) AND (GPL-2.0-only WITH Linux-syscall-note OR BSD-3-Clause) AND (GPL-2.0-only WITH Linux-syscall-note OR CDDL-1.0) AND (GPL-2.0-only WITH Linux-syscall-note OR Linux-OpenIB) AND (GPL-2.0-only WITH Linux-syscall-note OR MIT) AND (GPL-2.0-or-later WITH Linux-syscall-note OR BSD-3-Clause) AND (GPL-2.0-or-later WITH Linux-syscall-note OR MIT) AND Apache-2.0 AND BSD-2-Clause AND BSD-3-Clause AND BSD-3-Clause-Clear AND GFDL-1.1-no-invariants-or-later AND GPL-1.0-or-later AND (GPL-1.0-or-later OR BSD-3-Clause) AND GPL-1.0-or-later WITH Linux-syscall-note AND GPL-2.0-only AND (GPL-2.0-only OR Apache-2.0) AND (GPL-2.0-only OR BSD-2-Clause) AND (GPL-2.0-only OR BSD-3-Clause) AND (GPL-2.0-only OR CDDL-1.0) AND (GPL-2.0-only OR GFDL-1.1-no-invariants-or-later) AND (GPL-2.0-only OR GFDL-1.2-no-invariants-only) AND GPL-2.0-only WITH Linux-syscall-note AND GPL-2.0-or-later AND (GPL-2.0-or-later OR BSD-2-Clause) AND (GPL-2.0-or-later OR BSD-3-Clause) AND (GPL-2.0-or-later OR CC-BY-4.0) AND GPL-2.0-or-later WITH GCC-exception-2.0 AND GPL-2.0-or-later WITH Linux-syscall-note AND ISC AND LGPL-2.0-or-later AND (LGPL-2.0-or-later OR BSD-2-Clause) AND LGPL-2.0-or-later WITH Linux-syscall-note AND LGPL-2.1-only AND (LGPL-2.1-only OR BSD-2-Clause) AND LGPL-2.1-only WITH Linux-syscall-note AND LGPL-2.1-or-later AND LGPL-2.1-or-later WITH Linux-syscall-note AND (Linux-OpenIB OR GPL-2.0-only) AND (Linux-OpenIB OR GPL-2.0-only OR BSD-2-Clause) AND Linux-man-pages-copyleft AND MIT AND (MIT OR GPL-2.0-only) AND (MIT OR GPL-2.0-or-later) AND (MIT OR LGPL-2.1-only) AND (MPL-1.1 OR GPL-2.0-only) AND (X11 OR GPL-2.0-only) AND (X11 OR GPL-2.0-or-later) AND Zlib AND (copyleft-next-0.3.1 OR GPL-2.0-or-later)` |
| 14 | |
| 15 | var expectedKernelHeadersLicenses = []string{ |
| 16 | "GPL-2.0-only WITH Linux-syscall-note", |
| 17 | "BSD-2-Clause", |
| 18 | "BSD-3-Clause", |
| 19 | "CDDL-1.0", |
| 20 | "Linux-OpenIB", |
| 21 | "MIT", |
| 22 | "GPL-2.0-or-later WITH Linux-syscall-note", |
| 23 | "Apache-2.0", |
| 24 | "BSD-3-Clause-Clear", |
| 25 | "GFDL-1.1-no-invariants-or-later", |
| 26 | "GPL-1.0-or-later", |
| 27 | "GPL-1.0-or-later WITH Linux-syscall-note", |
| 28 | "GPL-2.0-only", |
| 29 | "GFDL-1.2-no-invariants-only", |
| 30 | "GPL-2.0-or-later", |
| 31 | "CC-BY-4.0", |
| 32 | "GPL-2.0-or-later WITH GCC-exception-2.0", |
| 33 | "ISC", |
| 34 | "LGPL-2.0-or-later", |
| 35 | "LGPL-2.0-or-later WITH Linux-syscall-note", |
| 36 | "LGPL-2.1-only", |
| 37 | "LGPL-2.1-only WITH Linux-syscall-note", |
| 38 | "LGPL-2.1-or-later", |
| 39 | "LGPL-2.1-or-later WITH Linux-syscall-note", |
| 40 | "Linux-man-pages-copyleft", |
| 41 | "MPL-1.1", |
| 42 | "X11", |
| 43 | "Zlib", |
| 44 | "copyleft-next-0.3.1", |
| 45 | } |
| 46 | |
| 47 | func TestExtractLicenses(t *testing.T) { |
| 48 | tests := []struct { |
| 49 | name string |
| 50 | inputExpression string |
| 51 | extractedLicenses []string |
| 52 | }{ |
| 53 | {"Single license", "MIT", []string{"MIT"}}, |
| 54 | {"AND'ed licenses", "MIT AND Apache-2.0", []string{"MIT", "Apache-2.0"}}, |
| 55 | {"AND'ed & OR'ed licenses", "(MIT AND Apache-2.0) OR GPL-3.0", []string{"GPL-3.0", "MIT", "Apache-2.0"}}, |
| 56 | {"ONLY modifiers", "LGPL-2.1-only OR MIT OR BSD-3-Clause", []string{"MIT", "BSD-3-Clause", "LGPL-2.1-only"}}, |
| 57 | {"WITH modifiers", "GPL-2.0-or-later WITH Bison-exception-2.2", []string{"GPL-2.0-or-later WITH Bison-exception-2.2"}}, |
| 58 | {"Invalid SPDX expression", "MIT OR INVALID", nil}, |
| 59 | {"-or-later suffix with mixed case", "GPL-2.0-Or-later", []string{"GPL-2.0-or-later"}}, |
| 60 | {"+ operator in Apache", "APACHE-2.0+", []string{"Apache-2.0+"}}, |
| 61 | {"+ operator in GPL", "GPL-2.0+", []string{"GPL-2.0-or-later"}}, |
| 62 | } |
| 63 | |
| 64 | for _, test := range tests { |
| 65 | t.Run(test.name, func(t *testing.T) { |
| 66 | licenses, err := ExtractLicenses(test.inputExpression) |
| 67 | assert.ElementsMatch(t, test.extractedLicenses, licenses) |
| 68 | if test.extractedLicenses == nil { |
| 69 | assert.Error(t, err) |
| 70 | } else { |
| 71 | assert.NoError(t, err) |
| 72 | } |
| 73 | }) |
| 74 | } |
| 75 | } |
| 76 | |
| 77 | func TestExtractLicensesLicenseRefAndDedup(t *testing.T) { |
| 78 | licenses, err := ExtractLicenses("(LicenseRef-custom OR LicenseRef-custom) AND (DocumentRef-spdx-tool-1.2:LicenseRef-custom OR MIT)") |
| 79 | assert.NoError(t, err) |
| 80 | assert.ElementsMatch(t, []string{"LicenseRef-custom", "DocumentRef-spdx-tool-1.2:LicenseRef-custom", "MIT"}, licenses) |
| 81 | } |
| 82 | |
| 83 | func TestExtractLicensesLongExpressionDoesNotHang(t *testing.T) { |
| 84 | if os.Getenv("GO_SPDX_EXTRACT_LICENSES_LONG_CHILD") == "1" { |
| 85 | licenses, err := ExtractLicenses(kernelHeadersLicense) |
| 86 | assert.NoError(t, err) |
| 87 | assert.ElementsMatch(t, expectedKernelHeadersLicenses, licenses) |
| 88 | return |
| 89 | } |
| 90 | |
| 91 | ctx, cancel := context.WithTimeout(context.Background(), 5*time.Second) |
| 92 | defer cancel() |
| 93 | |
| 94 | // #nosec G204 G702 -- safe in tests: re-executes current test binary with fixed arg |
| 95 | cmd := exec.CommandContext(ctx, os.Args[0], "-test.run", "^TestExtractLicensesLongExpressionDoesNotHang$") |
| 96 | cmd.Env = append(os.Environ(), "GO_SPDX_EXTRACT_LICENSES_LONG_CHILD=1") |
| 97 | output, err := cmd.CombinedOutput() |
| 98 | if ctx.Err() == context.DeadlineExceeded { |
| 99 | t.Fatalf("ExtractLicenses timed out on long expression: %s", output) |
| 100 | } |
| 101 | if err != nil { |
| 102 | t.Fatalf("child process failed: %v\n%s", err, output) |
| 103 | } |
| 104 | } |