From da814d7f818f955d659af39c5df433d50f1abae1 Mon Sep 17 00:00:00 2001 From: Michael Imamura Date: Wed, 18 Dec 2024 19:30:56 -0500 Subject: [PATCH] Add unit test for SBOM. --- .../plugin_runner/toolbox/lint_test.go | 44 +++++++++++++++++++ 1 file changed, 44 insertions(+) diff --git a/backend/utilities/plugin_runner/toolbox/lint_test.go b/backend/utilities/plugin_runner/toolbox/lint_test.go index 44a6b43e..6008ca22 100644 --- a/backend/utilities/plugin_runner/toolbox/lint_test.go +++ b/backend/utilities/plugin_runner/toolbox/lint_test.go @@ -176,6 +176,50 @@ func TestValidInventory(t *testing.T) { } } +func TestValidSBOM(t *testing.T) { + actual := lint("sbom", []byte(`{ + "success": true, + "truncated": false, + "details": [ + [ + {"sbom": "foo"} + ], + [ + { + "bom-ref": "pkg:golang/cloud.google.com/go/datastore@1.1.0", + "type": "gomod", + "name": "cloud.google.com/go/datastore", + "version": "1.1.0", + "licenses": [ + { + "id": "Apache-2.0", + "name": "Apache-2.0" + } + ] + } + ] + ], + "errors": ["failed to scan"] + }`)) + if actual != nil { + t.Fatalf("expected no errors, got %v", actual) + } +} + +func TestSBOMMissingValue(t *testing.T) { + actual := lint("sbom", []byte(`{ + "success": true, + "truncated": false, + "details": [ + [] + ], + "errors": ["failed to scan"] + }`)) + if !containsValidationError(actual, "/details", "minItems: got 1, want 2") { + t.Fatalf("expected required error, got %v", actual) + } +} + func TestUnknownType(t *testing.T) { actual := lint("foo", []byte("{}")) if actual == nil || !strings.Contains(actual.Error(), "unknown plugin type") {