| 1 | package spdxexp |
| 2 | |
| 3 | import ( |
| 4 | "errors" |
| 5 | "testing" |
| 6 | |
| 7 | "github.com/stretchr/testify/assert" |
| 8 | "github.com/stretchr/testify/require" |
| 9 | ) |
| 10 | |
| 11 | func TestScan(t *testing.T) { |
| 12 | tests := []struct { |
| 13 | name string |
| 14 | expression string |
| 15 | tokens []token |
| 16 | err error |
| 17 | }{ |
| 18 | {"single license", "MIT", |
| 19 | []token{ |
| 20 | {role: licenseToken, value: "MIT"}, |
| 21 | }, nil}, |
| 22 | {"single license - diff case", "mit", |
| 23 | []token{ |
| 24 | {role: licenseToken, value: "MIT"}, |
| 25 | }, nil}, |
| 26 | {"empty expression", "", []token(nil), nil}, |
| 27 | {"invalid license", "NON-EXISTENT-LICENSE", []token(nil), |
| 28 | errors.New("unknown license 'NON-EXISTENT-LICENSE' at offset 0")}, |
| 29 | {"two licenses using AND", "MIT AND Apache-2.0", |
| 30 | []token{ |
| 31 | {role: licenseToken, value: "MIT"}, |
| 32 | {role: operatorToken, value: "AND"}, |
| 33 | {role: licenseToken, value: "Apache-2.0"}, |
| 34 | }, nil}, |
| 35 | {"two licenses using OR inside paren", "(MIT OR Apache-2.0)", |
| 36 | []token{ |
| 37 | {role: operatorToken, value: "("}, |
| 38 | {role: licenseToken, value: "MIT"}, |
| 39 | {role: operatorToken, value: "OR"}, |
| 40 | {role: licenseToken, value: "Apache-2.0"}, |
| 41 | {role: operatorToken, value: ")"}, |
| 42 | }, nil}, |
| 43 | {"kitchen sink", " (MIT AND Apache-1.0+) OR DocumentRef-spdx-tool-1.2:LicenseRef-MIT-Style-2 OR (GPL-2.0 WITH Bison-exception-2.2)", |
| 44 | []token{ |
| 45 | {role: operatorToken, value: "("}, |
| 46 | {role: licenseToken, value: "MIT"}, |
| 47 | {role: operatorToken, value: "AND"}, |
| 48 | {role: licenseToken, value: "Apache-1.0"}, |
| 49 | {role: operatorToken, value: "+"}, |
| 50 | {role: operatorToken, value: ")"}, |
| 51 | {role: operatorToken, value: "OR"}, |
| 52 | {role: documentRefToken, value: "spdx-tool-1.2"}, |
| 53 | {role: operatorToken, value: ":"}, |
| 54 | {role: licenseRefToken, value: "MIT-Style-2"}, |
| 55 | {role: operatorToken, value: "OR"}, |
| 56 | {role: operatorToken, value: "("}, |
| 57 | {role: licenseToken, value: "GPL-2.0"}, |
| 58 | {role: operatorToken, value: "WITH"}, |
| 59 | {role: exceptionToken, value: "Bison-exception-2.2"}, |
| 60 | {role: operatorToken, value: ")"}, |
| 61 | }, nil}, |
| 62 | } |
| 63 | |
| 64 | for _, test := range tests { |
| 65 | test := test |
| 66 | t.Run(test.name, func(t *testing.T) { |
| 67 | tokens, err := scan(test.expression) |
| 68 | |
| 69 | require.Equal(t, test.err, err) |
| 70 | assert.Equal(t, test.tokens, tokens) |
| 71 | }) |
| 72 | } |
| 73 | } |
| 74 | |
| 75 | func TestHasMoreSource(t *testing.T) { |
| 76 | tests := []struct { |
| 77 | name string |
| 78 | exp *expressionStream |
| 79 | result bool |
| 80 | }{ |
| 81 | {"at start", getExpressionStream("MIT OR Apache-2.0", 0), true}, |
| 82 | {"at middle", getExpressionStream("MIT OR Apache-2.0", 3), true}, |
| 83 | {"at end", getExpressionStream("MIT OR Apache-2.0", len("MIT OR Apache-2.0")), false}, |
| 84 | {"past end", getExpressionStream("MIT OR Apache-2.0", 50), false}, |
| 85 | } |
| 86 | |
| 87 | for _, test := range tests { |
| 88 | test := test |
| 89 | t.Run(test.name, func(t *testing.T) { |
| 90 | assert.Equal(t, test.result, test.exp.hasMore()) |
| 91 | }) |
| 92 | } |
| 93 | } |
| 94 | |
| 95 | func TestParseToken(t *testing.T) { |
| 96 | tests := []struct { |
| 97 | name string |
| 98 | exp *expressionStream |
| 99 | token *token |
| 100 | newIndex int |
| 101 | err error |
| 102 | }{ |
| 103 | {"operator found", getExpressionStream("MIT AND Apache-2.0", 4), |
| 104 | &token{role: operatorToken, value: "AND"}, 7, nil}, |
| 105 | {"operator error", getExpressionStream("Apache-1.0 + OR MIT", 11), |
| 106 | nil, 11, errors.New("unexpected space before +")}, |
| 107 | {"document ref found", getExpressionStream("DocumentRef-spdx-tool-1.2:LicenseRef-MIT-Style-2", 0), |
| 108 | &token{role: documentRefToken, value: "spdx-tool-1.2"}, 25, nil}, |
| 109 | {"document ref error", getExpressionStream("DocumentRef-!23", 0), |
| 110 | nil, 12, errors.New("expected id at offset 12")}, |
| 111 | {"license ref found", getExpressionStream("DocumentRef-spdx-tool-1.2:LicenseRef-MIT-Style-2", 26), |
| 112 | &token{role: licenseRefToken, value: "MIT-Style-2"}, 48, nil}, |
| 113 | {"license ref error", getExpressionStream("LicenseRef-!23", 0), |
| 114 | nil, 11, errors.New("expected id at offset 11")}, |
| 115 | {"identifier found", getExpressionStream("MIT AND Apache-2.0", 8), |
| 116 | &token{role: licenseToken, value: "Apache-2.0"}, 18, nil}, |
| 117 | {"identifier error", getExpressionStream("NON-EXISTENT-LICENSE", 0), |
| 118 | nil, 0, errors.New("unknown license 'NON-EXISTENT-LICENSE' at offset 0")}, |
| 119 | } |
| 120 | |
| 121 | for _, test := range tests { |
| 122 | test := test |
| 123 | t.Run(test.name, func(t *testing.T) { |
| 124 | tokn := test.exp.parseToken() |
| 125 | assert.Equal(t, test.newIndex, test.exp.index) |
| 126 | |
| 127 | require.Equal(t, test.err, test.exp.err) |
| 128 | if test.err != nil { |
| 129 | // token is nil when error occurs or token is not recognized |
| 130 | var nilToken *token |
| 131 | assert.Equal(t, nilToken, tokn) |
| 132 | return |
| 133 | } |
| 134 | |
| 135 | // token recognized, check token value |
| 136 | assert.Equal(t, test.token, tokn) |
| 137 | }) |
| 138 | } |
| 139 | } |
| 140 | |
| 141 | func TestReadRegex(t *testing.T) { |
| 142 | tests := []struct { |
| 143 | name string |
| 144 | exp *expressionStream |
| 145 | pattern string |
| 146 | match string |
| 147 | newIndex int |
| 148 | }{ |
| 149 | {"regex to skip leading blank in middle", getExpressionStream("MIT OR Apache-2.0", 3), |
| 150 | "[ ]*", " ", 4}, |
| 151 | {"regex for id", getExpressionStream("LicenseRef-MIT-Style-1", 11), |
| 152 | "[A-Za-z0-9-.]+", "MIT-Style-1", 22}, |
| 153 | } |
| 154 | |
| 155 | for _, test := range tests { |
| 156 | test := test |
| 157 | t.Run(test.name, func(t *testing.T) { |
| 158 | match := test.exp.readRegex(test.pattern) |
| 159 | assert.Equal(t, test.match, match) |
| 160 | assert.Equal(t, test.newIndex, test.exp.index) |
| 161 | }) |
| 162 | } |
| 163 | } |
| 164 | |
| 165 | func TestRead(t *testing.T) { |
| 166 | tests := []struct { |
| 167 | name string |
| 168 | exp *expressionStream |
| 169 | next string |
| 170 | match string |
| 171 | newIndex int |
| 172 | }{ |
| 173 | {"at first - match word", getExpressionStream("MIT OR Apache-2.0", 0), "MIT", "MIT", 3}, |
| 174 | {"at middle - match operator", getExpressionStream("MIT OR Apache-2.0", 4), "OR", "OR", 6}, |
| 175 | {"at middle - match last word", getExpressionStream("MIT OR Apache-2.0", 7), "Apache-2.0", "Apache-2.0", 17}, |
| 176 | {"at first - no match", getExpressionStream("MIT OR Apache-2.0", 0), "GPL", "", 0}, |
| 177 | {"at middle - no match for operator", getExpressionStream("MIT OR Apache-2.0", 4), "AND", "", 4}, |
| 178 | {"at middle - no match last word", getExpressionStream("MIT OR Apache-2.0", 7), "GPL", "", 7}, |
| 179 | } |
| 180 | |
| 181 | for _, test := range tests { |
| 182 | test := test |
| 183 | t.Run(test.name, func(t *testing.T) { |
| 184 | match := test.exp.read(test.next) |
| 185 | assert.Equal(t, test.match, match) |
| 186 | assert.Equal(t, test.newIndex, test.exp.index) |
| 187 | }) |
| 188 | } |
| 189 | } |
| 190 | |
| 191 | func TestSkipWhitespace(t *testing.T) { |
| 192 | tests := []struct { |
| 193 | name string |
| 194 | exp *expressionStream |
| 195 | newIndex int |
| 196 | }{ |
| 197 | {"at first - no blanks", getExpressionStream("MIT OR Apache-2.0", 0), 0}, |
| 198 | {"at first - with blanks", getExpressionStream(" MIT OR Apache-2 .0", 0), 2}, |
| 199 | {"at middle - no blanks", getExpressionStream("MIT OR Apache-2.0", 4), 4}, |
| 200 | {"at middle - with blanks", getExpressionStream("MIT OR Apache-2.0", 3), 4}, |
| 201 | {"at end - no blanks", getExpressionStream("MIT OR GPL", 10), 10}, |
| 202 | {"at end - with blanks", getExpressionStream("MIT OR GPL ", 10), 12}, |
| 203 | } |
| 204 | |
| 205 | for _, test := range tests { |
| 206 | test := test |
| 207 | t.Run(test.name, func(t *testing.T) { |
| 208 | test.exp.skipWhitespace() |
| 209 | assert.Equal(t, test.newIndex, test.exp.index) |
| 210 | }) |
| 211 | } |
| 212 | } |
| 213 | |
| 214 | func TestReadOperator(t *testing.T) { |
| 215 | tests := []struct { |
| 216 | name string |
| 217 | exp *expressionStream |
| 218 | operator *token |
| 219 | newIndex int |
| 220 | err error |
| 221 | }{ |
| 222 | {"WITH operator", getExpressionStream("MIT WITH Bison-exception-2.2", 4), |
| 223 | &token{role: operatorToken, value: "WITH"}, 8, nil}, |
| 224 | {"AND operator", getExpressionStream("MIT AND Apache-2.0", 4), |
| 225 | &token{role: operatorToken, value: "AND"}, 7, nil}, |
| 226 | {"OR operator", getExpressionStream("MIT OR Apache-2.0", 4), |
| 227 | &token{role: operatorToken, value: "OR"}, 6, nil}, |
| 228 | {"( operator", getExpressionStream("(MIT OR Apache-2.0)", 0), |
| 229 | &token{role: operatorToken, value: "("}, 1, nil}, |
| 230 | {") operator", getExpressionStream("(MIT OR Apache-2.0)", 18), |
| 231 | &token{role: operatorToken, value: ")"}, 19, nil}, |
| 232 | {": operator", getExpressionStream("DocumentRef-spdx-tool-1.2:LicenseRef-MIT-Style-2", 25), |
| 233 | &token{role: operatorToken, value: ":"}, 26, nil}, |
| 234 | {"plus operator - correctly used", getExpressionStream("Apache-1.0+ OR MIT", 10), |
| 235 | &token{role: operatorToken, value: "+"}, 11, nil}, |
| 236 | {"plus operator - with preceding space", getExpressionStream("Apache-1.0 + OR MIT", 11), |
| 237 | nil, 11, errors.New("unexpected space before +")}, |
| 238 | {"operator not found", getExpressionStream("MIT AND Apache-2.0", 8), |
| 239 | nil, 8, nil}, |
| 240 | } |
| 241 | |
| 242 | for _, test := range tests { |
| 243 | test := test |
| 244 | t.Run(test.name, func(t *testing.T) { |
| 245 | operator := test.exp.readOperator() |
| 246 | assert.Equal(t, test.newIndex, test.exp.index) |
| 247 | require.Equal(t, test.err, test.exp.err) |
| 248 | assert.Equal(t, test.operator, operator) |
| 249 | }) |
| 250 | } |
| 251 | } |
| 252 | |
| 253 | func TestReadId(t *testing.T) { |
| 254 | tests := []struct { |
| 255 | name string |
| 256 | exp *expressionStream |
| 257 | id string |
| 258 | newIndex int |
| 259 | }{ |
| 260 | {"valid numeric id", getExpressionStream("LicenseRef-23", 11), "23", 13}, |
| 261 | {"valid id with dashes", getExpressionStream("LicenseRef-MIT-Style-1", 11), "MIT-Style-1", 22}, |
| 262 | {"valid id with period", getExpressionStream("DocumentRef-spdx-tool-1.2:LicenseRef-MIT-Style-2", 12), "spdx-tool-1.2", 25}, |
| 263 | {"invalid starts with non-supported character", getExpressionStream("LicenseRef-!23", 11), "", 11}, |
| 264 | {"invalid non-supported character in middle", getExpressionStream("LicenseRef-2!3", 11), "2", 12}, |
| 265 | {"invalid ends with non-supported character", getExpressionStream("LicenseRef-23!", 11), "23", 13}, |
| 266 | } |
| 267 | |
| 268 | for _, test := range tests { |
| 269 | test := test |
| 270 | t.Run(test.name, func(t *testing.T) { |
| 271 | id := test.exp.readID() |
| 272 | // check values if there isn't an error |
| 273 | assert.Equal(t, test.id, id) |
| 274 | assert.Equal(t, test.newIndex, test.exp.index) |
| 275 | }) |
| 276 | } |
| 277 | } |
| 278 | |
| 279 | func TestReadDocumentRef(t *testing.T) { |
| 280 | tests := []struct { |
| 281 | name string |
| 282 | exp *expressionStream |
| 283 | ref *token |
| 284 | newIndex int |
| 285 | err error |
| 286 | }{ |
| 287 | {"valid document ref with id", getExpressionStream("DocumentRef-spdx-tool-1.2:LicenseRef-MIT-Style-2", 0), &token{role: documentRefToken, value: "spdx-tool-1.2"}, 25, nil}, |
| 288 | {"document ref not found", getExpressionStream("DocumentRef-spdx-tool-1.2:LicenseRef-MIT-Style-2", 26), nil, 26, nil}, |
| 289 | {"invalid document ref with bad id", getExpressionStream("DocumentRef-!23", 0), nil, 12, errors.New("expected id at offset 12")}, |
| 290 | } |
| 291 | |
| 292 | for _, test := range tests { |
| 293 | test := test |
| 294 | t.Run(test.name, func(t *testing.T) { |
| 295 | ref := test.exp.readDocumentRef() |
| 296 | assert.Equal(t, test.newIndex, test.exp.index) |
| 297 | |
| 298 | require.Equal(t, test.err, test.exp.err) |
| 299 | if test.err != nil { |
| 300 | // ref should be nil when error occurs or a ref is not found |
| 301 | var nilToken *token |
| 302 | assert.Equal(t, nilToken, ref, "Expected nil token when error occurs.") |
| 303 | return |
| 304 | } |
| 305 | |
| 306 | // ref found, check ref value |
| 307 | assert.Equal(t, test.ref, ref) |
| 308 | }) |
| 309 | } |
| 310 | } |
| 311 | |
| 312 | func TestReadLicenseRef(t *testing.T) { |
| 313 | tests := []struct { |
| 314 | name string |
| 315 | exp *expressionStream |
| 316 | ref *token |
| 317 | newIndex int |
| 318 | err error |
| 319 | }{ |
| 320 | {"valid license ref with id", getExpressionStream("DocumentRef-spdx-tool-1.2:LicenseRef-MIT-Style-2", 26), &token{role: licenseRefToken, value: "MIT-Style-2"}, 48, nil}, |
| 321 | {"license ref not found", getExpressionStream("DocumentRef-spdx-tool-1.2:LicenseRef-MIT-Style-2", 0), nil, 0, nil}, |
| 322 | {"invalid license ref with bad id", getExpressionStream("LicenseRef-!23", 0), nil, 11, errors.New("expected id at offset 11")}, |
| 323 | } |
| 324 | |
| 325 | for _, test := range tests { |
| 326 | test := test |
| 327 | t.Run(test.name, func(t *testing.T) { |
| 328 | ref := test.exp.readLicenseRef() |
| 329 | assert.Equal(t, test.newIndex, test.exp.index) |
| 330 | |
| 331 | require.Equal(t, test.err, test.exp.err) |
| 332 | if test.err != nil { |
| 333 | // ref should be nil when error occurs or a ref is not found |
| 334 | var nilToken *token |
| 335 | assert.Equal(t, nilToken, ref) |
| 336 | return |
| 337 | } |
| 338 | |
| 339 | // ref found, check ref value |
| 340 | assert.Equal(t, test.ref, ref) |
| 341 | }) |
| 342 | } |
| 343 | } |
| 344 | |
| 345 | func TestReadLicense(t *testing.T) { |
| 346 | tests := []struct { |
| 347 | name string |
| 348 | exp *expressionStream |
| 349 | license *token |
| 350 | newExpression string |
| 351 | newIndex int |
| 352 | err error |
| 353 | }{ |
| 354 | {"active license", getExpressionStream("MIT", 0), &token{role: licenseToken, value: "MIT"}, "MIT", 3, nil}, |
| 355 | {"active -or-later", getExpressionStream("AGPL-1.0-or-later", 0), &token{role: licenseToken, value: "AGPL-1.0-or-later"}, "AGPL-1.0-or-later", 17, nil}, |
| 356 | {"active -or-later using +", getExpressionStream("AGPL-1.0+", 0), &token{role: licenseToken, value: "AGPL-1.0-or-later"}, "AGPL-1.0+", 9, nil}, // no valid example for this; all that include -or-later have the base as a deprecated license |
| 357 | {"active -or-later not in list", getExpressionStream("Apache-1.0-or-later", 0), &token{role: licenseToken, value: "Apache-1.0"}, "Apache-1.0+", 10, nil}, |
| 358 | {"active -only", getExpressionStream("GPL-2.0-only", 0), &token{role: licenseToken, value: "GPL-2.0-only"}, "GPL-2.0-only", 12, nil}, |
| 359 | {"active -only not in list", getExpressionStream("ECL-1.0-only", 0), &token{role: licenseToken, value: "ECL-1.0"}, "ECL-1.0-only", 12, nil}, |
| 360 | {"deprecated license", getExpressionStream("LGPL-2.1", 0), &token{role: licenseToken, value: "LGPL-2.1"}, "LGPL-2.1", 8, nil}, |
| 361 | {"exception license", getExpressionStream("GPL-CC-1.0", 0), &token{role: exceptionToken, value: "GPL-CC-1.0"}, "GPL-CC-1.0", 10, nil}, |
| 362 | {"invalid license", getExpressionStream("NON-EXISTENT-LICENSE", 0), nil, "NON-EXISTENT-LICENSE", 0, errors.New("unknown license 'NON-EXISTENT-LICENSE' at offset 0")}, |
| 363 | } |
| 364 | |
| 365 | for _, test := range tests { |
| 366 | test := test |
| 367 | t.Run(test.name, func(t *testing.T) { |
| 368 | license := test.exp.readLicense() |
| 369 | assert.Equal(t, test.newIndex, test.exp.index) |
| 370 | |
| 371 | require.Equal(t, test.err, test.exp.err) |
| 372 | if test.err != nil { |
| 373 | // license should be nil when error occurs or a license is not found |
| 374 | var nilToken *token |
| 375 | assert.Equal(t, nilToken, license) |
| 376 | return |
| 377 | } |
| 378 | |
| 379 | // license found, check license value |
| 380 | assert.Equal(t, test.license, license) |
| 381 | assert.Equal(t, test.newExpression, test.exp.expression) |
| 382 | }) |
| 383 | } |
| 384 | } |
| 385 | |
| 386 | func getExpressionStream(expression string, index int) *expressionStream { |
| 387 | return &expressionStream{ |
| 388 | expression: expression, |
| 389 | index: index, |
| 390 | err: nil, |
| 391 | } |
| 392 | } |