Repository navigation
Expand file tree
/
Copy pathparse_syntax_test.go
More file actions
255 lines (236 loc) · 6.93 KB
/
Copy pathparse_syntax_test.go
File metadata and controls
255 lines (236 loc) · 6.93 KB
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
package spdx
import (
"errors"
"reflect"
"strings"
"testing"
)
func TestParseSyntaxAllowsUnknownIdentifiers(t *testing.T) {
t.Parallel()
tests := []struct {
name string
input string
want string
err error
}{
{
name: "license",
input: "Future-License-9.9 OR MIT",
want: "Future-License-9.9 OR MIT",
err: ErrInvalidLicenseID,
},
{
name: "license with interior punctuation",
input: "Future.License-9.9 OR MIT",
want: "Future.License-9.9 OR MIT",
err: ErrInvalidLicenseID,
},
{
name: "exception",
input: "GPL-2.0-only WITH Future-exception-9.9",
want: "GPL-2.0-only WITH Future-exception-9.9",
err: ErrInvalidException,
},
{
name: "ScanCode exception reference",
input: "GPL-2.0-only WITH LicenseRef-scancode-cygwin-exception-2.0",
want: "GPL-2.0-only WITH LicenseRef-scancode-cygwin-exception-2.0",
err: ErrMissingOperand,
},
{
name: "custom exception reference",
input: "gpl-2.0-ONLY WITH licenseref-Custom.1-2",
want: "GPL-2.0-only WITH licenseref-Custom.1-2",
err: ErrMissingOperand,
},
{
name: "license with edge punctuation",
input: "-Future-License. OR - OR .",
want: "-Future-License. OR - OR .",
err: ErrInvalidLicenseID,
},
{
name: "exception with edge punctuation",
input: "GPL-2.0-only WITH .Future-exception-",
want: "GPL-2.0-only WITH .Future-exception-",
err: ErrInvalidException,
},
{
name: "exception reference with edge punctuation",
input: "GPL-2.0-only WITH LicenseRef--custom.",
want: "GPL-2.0-only WITH LicenseRef--custom.",
err: ErrMissingOperand,
},
{
name: "references with edge punctuation",
input: "LicenseRef-scancode-gradle-enterprise-sla-2022-11- OR DocumentRef--vendor.:LicenseRef--",
want: "LicenseRef-scancode-gradle-enterprise-sla-2022-11- OR DocumentRef--vendor.:LicenseRef--",
},
{
name: "exception reference with plus modifier",
input: "GPL-2.0+ WITH LicenseRef-custom",
want: "GPL-2.0+ WITH LicenseRef-custom",
err: ErrMissingOperand,
},
}
for _, test := range tests {
t.Run(test.name, func(t *testing.T) {
t.Parallel()
expression, err := ParseSyntax(test.input)
if err != nil {
t.Fatalf("ParseSyntax(%q): %v", test.input, err)
}
if got := expression.String(); got != test.want {
t.Errorf("ParseSyntax(%q) = %q, want %q", test.input, got, test.want)
}
_, err = ParseStrict(test.input)
if !errors.Is(err, test.err) {
t.Errorf("ParseStrict(%q) error = %v, want %v", test.input, err, test.err)
}
})
}
}
func TestParseSyntaxExceptionReferenceRewrite(t *testing.T) {
t.Parallel()
exception := "LicenseRef-scancode-cygwin-exception-2.0"
input := "MIT OR GPL-2.0-only WITH " + exception + " AND Apache-2.0"
expression, err := ParseSyntax(input)
if err != nil {
t.Fatal(err)
}
want := "MIT OR (GPL-2.0-only WITH " + exception + " AND Apache-2.0)"
if got := expression.String(); got != want {
t.Errorf("String() = %q, want %q", got, want)
}
wantAST := &OrExpression{
Left: &License{ID: "MIT"},
Right: &AndExpression{
Left: &License{ID: "GPL-2.0-only", Exception: exception},
Right: &License{ID: "Apache-2.0"},
},
}
if !reflect.DeepEqual(expression, wantAST) {
t.Errorf("AST = %#v, want %#v", expression, wantAST)
}
var identifiers []string
rewritten := RewriteIdentifiers(expression, func(id string) string {
identifiers = append(identifiers, id)
if id == exception {
return "custom-exception"
}
return id
})
wantIdentifiers := []string{"MIT", "GPL-2.0-only", exception, "Apache-2.0"}
if !reflect.DeepEqual(identifiers, wantIdentifiers) {
t.Errorf("identifiers = %q, want %q", identifiers, wantIdentifiers)
}
wantRewritten := "MIT OR (GPL-2.0-only WITH custom-exception AND Apache-2.0)"
if rewritten != wantRewritten {
t.Errorf("RewriteIdentifiers() = %q, want %q", rewritten, wantRewritten)
}
if got := expression.String(); got != want {
t.Errorf("String() after rewrite = %q, want %q", got, want)
}
if _, err := ParseStrict(input); err == nil {
t.Error("ParseStrict() accepted an unknown exception reference")
}
if Valid(input) {
t.Error("Valid() accepted an unknown exception reference")
}
}
func TestParseSyntaxMatchesStrictAST(t *testing.T) {
t.Parallel()
inputs := []string{
"mit",
"MIT OR Apache-2.0 AND BSD-3-Clause",
"(MIT OR Apache-2.0) AND GPL-2.0-only",
"GPL-2.0-only WITH Classpath-exception-2.0",
"GPL-2.0+",
"LicenseRef-custom AND DocumentRef-vendor:LicenseRef-proprietary",
"LicenseRef-custom.1-2 AND DocumentRef-vendor.1:LicenseRef-proprietary-2",
"NONE",
"NOASSERTION",
}
for _, input := range inputs {
t.Run(input, func(t *testing.T) {
t.Parallel()
syntaxExpression, err := ParseSyntax(input)
if err != nil {
t.Fatalf("ParseSyntax(%q): %v", input, err)
}
strictExpression, err := ParseStrict(input)
if err != nil {
t.Fatalf("ParseStrict(%q): %v", input, err)
}
if !reflect.DeepEqual(syntaxExpression, strictExpression) {
t.Errorf(
"ASTs differ:\nParseSyntax: %#v\nParseStrict: %#v",
syntaxExpression,
strictExpression,
)
}
})
}
}
func TestParseSyntaxRejectsMalformedExpressions(t *testing.T) {
t.Parallel()
inputs := []string{
"",
"MIT OR",
"AND MIT",
"(MIT OR Apache-2.0",
"MIT OR Apache-2.0)",
"MIT WITH",
"MIT/Apache-2.0",
"Future_License-1.0",
"MIT WITH Future/exception",
"MIT WITH LicenseRef-",
"MIT WITH LicenseRef-invalid/value",
"MIT WITH LicenseRef-custom+",
"MIT WITH LicenseRef-custom WITH LicenseRef-other",
"LicenseRef-",
"LicenseRef-invalid/value",
"DocumentRef-vendor",
"DocumentRef-:LicenseRef-custom",
"DocumentRef-vendor:LicenseRef-",
"DocumentRef-vendor:LicenseRef-invalid/value",
}
for _, input := range inputs {
t.Run(input, func(t *testing.T) {
t.Parallel()
if _, err := ParseSyntax(input); err == nil {
t.Errorf("ParseSyntax(%q) succeeded", input)
}
if _, err := ParseStrict(input); err == nil {
t.Errorf("ParseStrict(%q) succeeded", input)
}
})
}
}
func TestParseSyntaxEnforcesLimits(t *testing.T) {
t.Parallel()
oversized := strings.Repeat("X", maxParseLength+1)
if _, err := ParseSyntax(oversized); !errors.Is(err, ErrExpressionTooLarge) {
t.Errorf("oversized expression error = %v, want %v", err, ErrExpressionTooLarge)
}
deeplyNested := strings.Repeat("(", maxParseDepth+1) +
"Future-License-9.9" +
strings.Repeat(")", maxParseDepth+1)
if _, err := ParseSyntax(deeplyNested); !errors.Is(err, ErrExpressionTooLarge) {
t.Errorf("deeply nested expression error = %v, want %v", err, ErrExpressionTooLarge)
}
}
func TestLicenseRefWithTrailingHyphen(t *testing.T) {
t.Parallel()
const input = "MIT OR LicenseRef-scancode-gradle-enterprise-sla-2022-11-"
if !Valid(input) {
t.Errorf("Valid(%q) = false", input)
}
got, err := NormalizeExpression(input)
if err != nil {
t.Fatalf("NormalizeExpression(%q): %v", input, err)
}
if got != input {
t.Errorf("NormalizeExpression(%q) = %q", input, got)
}
}