-
-
Notifications
You must be signed in to change notification settings - Fork 14
/
Copy pathvalidate_test.go
467 lines (389 loc) · 17.3 KB
/
validate_test.go
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
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
307
308
309
310
311
312
313
314
315
316
317
318
319
320
321
322
323
324
325
326
327
328
329
330
331
332
333
334
335
336
337
338
339
340
341
342
343
344
345
346
347
348
349
350
351
352
353
354
355
356
357
358
359
360
361
362
363
364
365
366
367
368
369
370
371
372
373
374
375
376
377
378
379
380
381
382
383
384
385
386
387
388
389
390
391
392
393
394
395
396
397
398
399
400
401
402
403
404
405
406
407
408
409
410
411
412
413
414
415
416
417
418
419
420
421
422
423
424
425
426
427
428
429
430
431
432
433
434
435
436
437
438
439
440
441
442
443
444
445
446
447
448
449
450
451
452
453
454
455
456
457
458
459
460
461
462
463
464
465
466
467
// SPDX-License-Identifier: Apache-2.0
/*
* Licensed to the Apache Software Foundation (ASF) under one or more
* contributor license agreements. See the NOTICE file distributed with
* this work for additional information regarding copyright ownership.
* The ASF licenses this file to You under the Apache License, Version 2.0
* (the "License"); you may not use this file except in compliance with
* the License. You may obtain a copy of the License at
*
* http://www.apache.org/licenses/LICENSE-2.0
*
* Unless required by applicable law or agreed to in writing, software
* distributed under the License is distributed on an "AS IS" BASIS,
* WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
* See the License for the specific language governing permissions and
* limitations under the License.
*/
package cmd
import (
"bufio"
"bytes"
"encoding/json"
"fmt"
"io/fs"
"log"
"os"
"testing"
"github.com/CycloneDX/sbom-utility/schema"
"github.com/CycloneDX/sbom-utility/utils"
"github.com/xeipuuv/gojsonschema"
)
const (
SCHEMA_VARIANT_NONE = ""
SCHEMA_VARIANT_DEVELOPMENT = "development"
SCHEMA_VARIANT_CUSTOM = "custom"
)
// JSON SBOM files containing syntax errors for testing
const (
TEST_CDX_1_3_SYNTAX_ERR_1 = "test/cyclonedx/cdx-1-3-syntax-err-1.json"
TEST_CDX_1_3_SYNTAX_ERR_2 = "test/cyclonedx/cdx-1-3-syntax-err-2.json"
)
const (
TEST_CDX_1_4_VALIDATE_ERR_COMPONENTS_UNIQUE = "test/validation/cdx-1-4-validate-err-components-unique-items-1.json"
TEST_CDX_1_4_VALIDATE_ERR_FORMAT_IRI_REFERENCE = "test/validation/cdx-1-4-validate-err-components-format-iri-reference.json"
)
type ValidateTestInfo struct {
CommonTestInfo
SchemaVariant string
CustomSchema string
SchemaErrorExpectedLineNum int
SchemaErrorExpectedCharNum int
}
func (ti *ValidateTestInfo) String() string {
buffer, _ := utils.EncodeAnyToDefaultIndentedJSONStr(ti)
return buffer.String()
}
func NewValidateTestInfoMinimum(inputFile string) *ValidateTestInfo {
var ti = new(ValidateTestInfo)
var pCommon = &ti.CommonTestInfo
pCommon.InputFile = inputFile
pCommon.OutputFormat = FORMAT_TEXT
ti.SchemaVariant = SCHEMA_VARIANT_NONE
return ti
}
func NewValidateTestInfoBasic(inputFile string, outputFormat string, expectedError error) *ValidateTestInfo {
var ti = new(ValidateTestInfo)
var pCommon = &ti.CommonTestInfo
pCommon.InputFile = inputFile
pCommon.OutputFormat = outputFormat
pCommon.ResultExpectedError = expectedError
ti.SchemaVariant = SCHEMA_VARIANT_NONE
return ti
}
func NewValidateTestInfo(inputFile string, outputFormat string, schemaVariant string, expectedError error) *ValidateTestInfo {
var ti = new(ValidateTestInfo)
var pCommon = &ti.CommonTestInfo
pCommon.InputFile = inputFile
pCommon.OutputFormat = outputFormat
pCommon.ResultExpectedError = expectedError
ti.SchemaVariant = schemaVariant
return ti
}
func innerTestValidate(t *testing.T, vti ValidateTestInfo) (document *schema.BOM, schemaErrors []gojsonschema.ResultError, actualError error) {
getLogger().Enter()
defer getLogger().Exit()
// Copy the test filename to the command line flags where the code looks for it
utils.GlobalFlags.PersistentFlags.InputFile = vti.InputFile
// Set the err result format
utils.GlobalFlags.PersistentFlags.OutputFormat = vti.OutputFormat
// Set the schema variant where the command line flag would
utils.GlobalFlags.ValidateFlags.SchemaVariant = vti.SchemaVariant
// Mock stdin if requested
if vti.MockStdin == true {
utils.GlobalFlags.PersistentFlags.InputFile = INPUT_TYPE_STDIN
file, err := os.Open(vti.InputFile) // For read access.
if err != nil {
log.Fatal(err)
}
// convert byte slice to io.Reader
savedStdIn := os.Stdin
// !!!Important restore stdin
defer func() { os.Stdin = savedStdIn }()
os.Stdin = file
}
// Invoke the actual validate function
var isValid bool
var outputBuffer bytes.Buffer
// TODO: support additional tests on output buffer (e.g., format==valid JSON)
isValid, document, schemaErrors, outputBuffer, actualError = innerValidateErrorBuffered(
utils.GlobalFlags.PersistentFlags,
utils.GlobalFlags.ValidateFlags,
)
getLogger().Tracef("document: '%s', isValid=`%t`, actualError=`%T`", document.GetFilename(), isValid, actualError)
// Always compare actual against expected error (even if it is `nil`)
expectedError := vti.ResultExpectedError
if !ErrorTypesMatch(actualError, expectedError) {
if len(schemaErrors) > 0 {
getLogger().Debugf("schemaErrors='%s'", schemaErrors)
}
switch t := actualError.(type) {
default:
fmt.Printf("unhandled error type: '%v'\n", t)
fmt.Printf(">> value: '%v'\n", t)
getLogger().Error(actualError)
}
t.Errorf("expected error type: `%T`, actual type: `%T`", expectedError, actualError)
}
// ANY error returned from Validate() SHOULD mark the input file as "invalid"
if actualError != nil && isValid {
t.Errorf("Validate() returned error (`%T`); however, input file still valid (%t)", actualError, isValid)
}
// ALWAYS make sure the if error was NOT expected that input file is marked "valid"
if expectedError == nil && !isValid {
t.Errorf("Input file invalid (%t); expected valid (no error)", isValid)
}
// Assure it is valid JSON output
if vti.OutputFormat == FORMAT_JSON {
if outputBuffer.Len() == 0 {
if expectedError == nil {
getLogger().Tracef("output data empty as expected (nil).")
} else {
t.Error(fmt.Errorf("output data empty; expected error text: %s", expectedError.Error()))
t.Logf("%s", outputBuffer.String())
}
} else if !utils.IsValidJsonRaw(outputBuffer.Bytes()) {
err := getLogger().Errorf("output did not contain valid format data; expected: '%s'", FORMAT_JSON)
t.Error(err.Error())
t.Logf("%s", outputBuffer.String())
return
}
}
return
}
func innerValidateErrorBuffered(persistentFlags utils.PersistentCommandFlags, validationFlags utils.ValidateCommandFlags) (isValid bool, document *schema.BOM, schemaErrors []gojsonschema.ResultError, outputBuffer bytes.Buffer, err error) {
// Declare an output outputBuffer/outputWriter to use used during tests
var outputWriter = bufio.NewWriter(&outputBuffer)
// ensure all data is written to buffer before further validation
defer outputWriter.Flush()
// Invoke the actual command (API)
isValid, document, schemaErrors, err = Validate(outputWriter, persistentFlags, utils.GlobalFlags.ValidateFlags)
getLogger().Tracef("document: '%s', isValid=`%t`, err=`%T`", document.GetFilename(), isValid, err)
return
}
func innerValidateForcedSchema(t *testing.T, filename string, forcedSchema string, outputFormat string, expectedError error) (document *schema.BOM, schemaErrors []gojsonschema.ResultError, actualError error) {
getLogger().Enter()
defer getLogger().Exit()
utils.GlobalFlags.ValidateFlags.ForcedJsonSchemaFile = forcedSchema
// !!!Important!!! Must reset this global flag and use the default schema for subsequent tests
defer func() { utils.GlobalFlags.ValidateFlags.ForcedJsonSchemaFile = "" }()
vti := NewValidateTestInfo(filename, outputFormat, SCHEMA_VARIANT_NONE, expectedError)
innerTestValidate(t, *vti)
return
}
// Tests *ErrorInvalidSBOM error types and any (lower-level) errors they "wrapped"
func innerValidateInvalidSBOMInnerError(t *testing.T, filename string, variant string, innerError error) (document *schema.BOM, schemaErrors []gojsonschema.ResultError, actualError error) {
getLogger().Enter()
defer getLogger().Exit()
vti := NewValidateTestInfo(filename, FORMAT_TEXT, variant, &InvalidSBOMError{})
document, schemaErrors, actualError = innerTestValidate(t, *vti)
invalidSBOMError, ok := actualError.(*InvalidSBOMError)
if !ok {
t.Errorf("Unable to cast actual error type (%T) to `InvalidSBOMError`: (%T)", actualError, &InvalidSBOMError{})
} else if !ErrorTypesMatch(invalidSBOMError.InnerError, innerError) {
t.Errorf("expected wrapped error type: `%T`, actual type: `%T`", innerError, invalidSBOMError.InnerError)
}
return
}
// Tests for *json.SyntaxErrors "wrapped" in *ErrorInvalidSBOM error types
// It also tests that the syntax error occurred at the expected line number and character offset
func innerValidateSyntaxError(t *testing.T, filename string, variant string, expectedLineNum int, expectedCharNum int) (document *schema.BOM, actualError error) {
getLogger().Enter()
defer getLogger().Exit()
vti := NewValidateTestInfo(filename, FORMAT_TEXT, variant, &json.SyntaxError{})
document, _, actualError = innerTestValidate(t, *vti)
syntaxError, ok := actualError.(*json.SyntaxError)
if !ok {
t.Errorf("Unable to cast inner error type (%T) to *json.SyntaxError: (%T)", actualError, syntaxError)
return
}
// Now make sure we correctly report the line/char offsets of the actual syntax error
// within the (test) input file
// Note: Uses the offset from JSON syntax errors return "encoding/json.SyntaxError"
rawBytes := document.GetRawBytes()
actualLineNum, actualCharNum := utils.CalcLineAndCharacterPos(rawBytes, syntaxError.Offset)
if actualLineNum != expectedLineNum || actualCharNum != expectedCharNum {
t.Errorf("syntax error found at line,char=[%d,%d], expected=[%d,%d]", actualLineNum, actualCharNum, expectedLineNum, expectedCharNum)
}
return
}
func innerTestSchemaErrorAndErrorResults(t *testing.T,
filename string, variant string,
schemaErrorType string, schemaErrorField string, schemaErrorValue string) {
vti := NewValidateTestInfo(filename, FORMAT_TEXT, variant, &InvalidSBOMError{})
document, results, _ := innerTestValidate(t, *vti)
getLogger().Debugf("filename: '%s', results:\n%v", document.GetFilename(), results)
// See ResultType struct fields (and values) in the `gojsonschema` package
exists := schemaErrorExists(results, schemaErrorType, schemaErrorField, schemaErrorValue)
if !exists {
t.Errorf("expected schema error: Type='%s', Field='%s', Value='%s'",
schemaErrorType,
schemaErrorField,
schemaErrorValue)
}
}
func schemaErrorExists(schemaErrors []gojsonschema.ResultError,
expectedType string, expectedField string, expectedValue interface{}) bool {
for i, resultError := range schemaErrors {
// Some descriptions include very long enums; in those cases,
// truncate to a reasonable length using an intelligent separator
getLogger().Tracef(">> %d. Type: [%s], Field: [%s], Value: [%v]",
i+1,
resultError.Type(),
resultError.Field(),
resultError.Value())
actualType := resultError.Type()
actualField := resultError.Field()
actualValue := resultError.Value()
if actualType == expectedType {
// we have matched on the type (key) field, continue to match other fields
if expectedField != "" &&
actualField != expectedField {
getLogger().Tracef("expected Field: '%s'; actual Field: '%s'", expectedField, actualField)
return false
}
if expectedValue != "" &&
actualValue != expectedValue {
getLogger().Tracef("expected Value: '%s'; actual Value: '%s'", actualValue, expectedValue)
return false
}
return true
} else {
getLogger().Debugf("Skipping result[%d]: expected Type: '%s'; actual Type: '%s'", i, expectedType, actualType)
}
}
return false
}
// -----------------------------------------------------------
// Command tests
// -----------------------------------------------------------
// Test for invalid input file provided on the `-i` flag
func TestValidateInvalidInputFileLoad(t *testing.T) {
// Assure we return path error
vti := NewValidateTestInfoBasic(TEST_INPUT_FILE_NON_EXISTENT, FORMAT_TEXT, &fs.PathError{})
innerTestValidate(t, *vti)
}
// -----------------------------------------------------------
// JSON Syntax error tests
// -----------------------------------------------------------
// Syntax error tests SHOULD return error type `encoding/json.SyntaxError`
// -----------------------------------------------------------
// "invalid character": Missing closing `}` bracket on `metadata` property
func TestValidateSyntaxErrorCdx13Test1(t *testing.T) {
filename := TEST_CDX_1_3_SYNTAX_ERR_1
LINE_NUM := 6
OFFSET := 18
innerValidateSyntaxError(t, filename, SCHEMA_VARIANT_NONE, LINE_NUM, OFFSET)
}
// "invalid character": Missing `:` separating `"properties"` key from array value `[`
func TestValidateSyntaxErrorCdx13Test2(t *testing.T) {
filename := TEST_CDX_1_3_SYNTAX_ERR_2
LINE_NUM := 123
OFFSET := 28
innerValidateSyntaxError(t, filename, SCHEMA_VARIANT_NONE, LINE_NUM, OFFSET)
}
// -----------------------------------------------------------
// Custom schema tests (i.e., `--force` flag) tests
// -----------------------------------------------------------
// NOTE: None of these tests actually test an SBOM against custom schema;
// those tests are instead run in "validate_custom_test.go"
// Force validation against a "custom" schema with compatible format (CDX) and version (1.3)
func TestValidateForceCustomSchemaCdx13(t *testing.T) {
innerValidateForcedSchema(t,
TEST_CDX_1_3_MATURE_EXAMPLE_1_BASE,
TEST_SCHEMA_CDX_1_3_CUSTOM,
FORMAT_TEXT,
nil)
}
// Force validation against a "custom" schema with compatible format (CDX) and version (1.4)
func TestValidateForceCustomSchemaCdx14(t *testing.T) {
innerValidateForcedSchema(t,
TEST_CDX_1_4_MATURE_EXAMPLE_1_BASE,
TEST_SCHEMA_CDX_1_4_CUSTOM,
FORMAT_TEXT,
nil)
}
// Force validation using schema with compatible format, but older version than the SBOM version
func TestValidateForceCustomSchemaCdxSchemaOlder(t *testing.T) {
innerValidateForcedSchema(t,
TEST_CDX_1_4_MATURE_EXAMPLE_1_BASE,
TEST_SCHEMA_CDX_1_3_CUSTOM,
FORMAT_TEXT,
nil)
}
// TODO: add additional checks on the buffered output
func TestValidateCdx14ErrorResultsUniqueComponentsText(t *testing.T) {
vti := NewValidateTestInfo(TEST_CDX_1_4_VALIDATE_ERR_COMPONENTS_UNIQUE, FORMAT_TEXT, SCHEMA_VARIANT_NONE, &InvalidSBOMError{})
innerTestValidate(t, *vti)
}
// TODO: add additional checks on the buffered output
func TestValidateCdx14ErrorResultsFormatIriReferencesText(t *testing.T) {
vti := NewValidateTestInfo(TEST_CDX_1_4_VALIDATE_ERR_FORMAT_IRI_REFERENCE, FORMAT_TEXT, SCHEMA_VARIANT_NONE, &InvalidSBOMError{})
innerTestValidate(t, *vti)
}
func TestValidateCdx14ErrorResultsUniqueComponentsCsv(t *testing.T) {
vti := NewValidateTestInfo(TEST_CDX_1_4_VALIDATE_ERR_COMPONENTS_UNIQUE, FORMAT_CSV, SCHEMA_VARIANT_NONE, &InvalidSBOMError{})
innerTestValidate(t, *vti)
}
// TODO: add additional checks on the buffered output
func TestValidateCdx14ErrorResultsFormatIriReferencesCsv(t *testing.T) {
vti := NewValidateTestInfo(TEST_CDX_1_4_VALIDATE_ERR_FORMAT_IRI_REFERENCE, FORMAT_CSV, SCHEMA_VARIANT_NONE, &InvalidSBOMError{})
innerTestValidate(t, *vti)
}
func TestValidateCdx14ErrorResultsUniqueComponentsJson(t *testing.T) {
var EXPECTED_ERROR_NUM = 2
var EXPECTED_ERROR_CONTEXT = "(root).components"
vti := NewValidateTestInfo(TEST_CDX_1_4_VALIDATE_ERR_COMPONENTS_UNIQUE, FORMAT_JSON, SCHEMA_VARIANT_NONE, &InvalidSBOMError{})
_, schemaErrors, _ := innerTestValidate(t, *vti)
if len(schemaErrors) != EXPECTED_ERROR_NUM {
t.Errorf("invalid schema error count: expected '%v'; actual: '%v')", EXPECTED_ERROR_NUM, len(schemaErrors))
}
if schemaErrors[0].Context().String() != EXPECTED_ERROR_CONTEXT {
t.Errorf("invalid schema error context: expected '%v'; actual: '%v')", EXPECTED_ERROR_CONTEXT, schemaErrors[0].Context().String())
}
}
// TODO: add additional checks on the buffered output
func TestValidateCdx14ErrorResultsFormatIriReferencesJson(t *testing.T) {
var EXPECTED_ERROR_NUM = 1
var EXPECTED_ERROR_CONTEXT = "(root).components.2.externalReferences.0.url"
vti := NewValidateTestInfo(TEST_CDX_1_4_VALIDATE_ERR_FORMAT_IRI_REFERENCE, FORMAT_JSON, SCHEMA_VARIANT_NONE, &InvalidSBOMError{})
_, schemaErrors, _ := innerTestValidate(t, *vti)
if len(schemaErrors) != EXPECTED_ERROR_NUM {
t.Errorf("invalid schema error count: expected '%v'; actual: '%v')", EXPECTED_ERROR_NUM, len(schemaErrors))
}
if schemaErrors[0].Context().String() != EXPECTED_ERROR_CONTEXT {
t.Errorf("invalid schema error context: expected '%v'; actual: '%v')", EXPECTED_ERROR_CONTEXT, schemaErrors[0].Context().String())
}
}
// -----------------------------------------------------------
// Test custom config.json (i.e., `--config-schema` flag)
// -----------------------------------------------------------
func loadCustomSchemaConfig(filename string) (err error) {
// Do not pass a default file, it should fail if custom policy cannot be loaded
err = SupportedFormatConfig.InnerLoadSchemaConfigFile(filename, DEFAULT_SCHEMA_CONFIG)
if err != nil {
return
}
return
}
func restoreEmbeddedDefaultSchemaConfig() (err error) {
return loadCustomSchemaConfig("")
}
func innerValidateCustomSchemaConfig(t *testing.T, filename string, configFile string, variant string, format string, expectedError error) (document *schema.BOM, schemaErrors []gojsonschema.ResultError, actualError error) {
getLogger().Enter()
defer getLogger().Exit()
loadCustomSchemaConfig(configFile)
// !!!Important!!! MUST restore the embedded `config.json` to be used for all other tests
defer restoreEmbeddedDefaultSchemaConfig()
vti := NewValidateTestInfo(TEST_CDX_1_4_MIN_REQUIRED, FORMAT_TEXT, variant, nil)
document, schemaErrors, actualError = innerTestValidate(t, *vti)
return
}
func TestValidateWithCustomSchemaConfiguration(t *testing.T) {
innerValidateCustomSchemaConfig(t, TEST_CDX_1_4_MIN_REQUIRED, DEFAULT_SCHEMA_CONFIG, SCHEMA_VARIANT_NONE, FORMAT_TEXT, nil)
}
func TestValidateUsingStdin(t *testing.T) {
vti := NewValidateTestInfo(TEST_CDX_1_4_MIN_REQUIRED, FORMAT_JSON, SCHEMA_VARIANT_NONE, nil)
vti.MockStdin = true
innerTestValidate(t, *vti)
}