From 0b26f041b3940befc483379bb29baa3b4f86cf0f Mon Sep 17 00:00:00 2001 From: attiasas Date: Thu, 14 Sep 2023 18:18:43 +0300 Subject: [PATCH] more tests --- xray/utils/sarifutils.go | 16 ++++++++ xray/utils/sarifutils_test.go | 74 +++++++++++++++++++++++++++++++++++ 2 files changed, 90 insertions(+) diff --git a/xray/utils/sarifutils.go b/xray/utils/sarifutils.go index 57effb331..03314c087 100644 --- a/xray/utils/sarifutils.go +++ b/xray/utils/sarifutils.go @@ -305,3 +305,19 @@ func CreateDummyResultWithOneLocation(fileName string, startLine, startCol, endL RuleID: &ruleId, } } + +func CreateDummyCodeFlow(threadFlows ...*sarif.ThreadFlow) *sarif.CodeFlow { + flow := sarif.NewCodeFlow() + for _, threadFlow := range threadFlows { + flow.AddThreadFlow(threadFlow) + } + return flow +} + +func CreateDummyThreadFlow(locations ...*sarif.Location) *sarif.ThreadFlow { + stackStrace := sarif.NewThreadFlow() + for _, location := range locations { + stackStrace.AddLocation(sarif.NewThreadFlowLocation().WithLocation(location)) + } + return stackStrace +} diff --git a/xray/utils/sarifutils_test.go b/xray/utils/sarifutils_test.go index c9182c4a6..44e2f9893 100644 --- a/xray/utils/sarifutils_test.go +++ b/xray/utils/sarifutils_test.go @@ -8,11 +8,83 @@ import ( ) func TestAggregateMultipleRunsIntoSingle(t *testing.T) { + tests := []struct { + runs []*sarif.Run + expectedOutput *sarif.Run + }{ + { + runs: []*sarif.Run{}, + expectedOutput: sarif.NewRunWithInformationURI("tool", "url"), + }, + } + for _, test := range tests { + result := sarif.NewRunWithInformationURI("tool", "url") + AggregateMultipleRunsIntoSingle(test.runs, result) + assert.Equal(t, test.expectedOutput, result) + } } func TestGetLocationRelatedCodeFlowsFromResult(t *testing.T) { + tests := []struct { + result *sarif.Result + location *sarif.Location + expectedOutput []*sarif.CodeFlow + }{ + { + result: CreateDummyPassingResult("rule"), + location: CreateDummyLocation("file", 0, 0, 0, 0, "snippet"), + expectedOutput: nil, + }, + { + result: CreateDummyResultWithOneLocation("file", 0, 0, 0, 0, "snippet", "rule", "level"), + location: CreateDummyLocation("file", 0, 0, 0, 0, "snippet"), + expectedOutput: nil, + }, + { + result: CreateDummyResultWithOneLocation("file", 0, 0, 0, 0, "snippet", "rule", "level").WithCodeFlows([]*sarif.CodeFlow{CreateDummyCodeFlow(CreateDummyThreadFlow(CreateDummyLocation("file", 0, 0, 0, 0, "snippet")))}), + location: CreateDummyLocation("file2", 0, 0, 0, 0, "snippet"), + expectedOutput: nil, + }, + { + result: CreateDummyResultWithOneLocation("file", 0, 0, 0, 0, "snippet", "rule", "level").WithCodeFlows([]*sarif.CodeFlow{CreateDummyCodeFlow(CreateDummyThreadFlow(CreateDummyLocation("file", 0, 0, 0, 0, "snippet")))}), + location: CreateDummyLocation("file", 0, 0, 0, 0, "snippet"), + expectedOutput: []*sarif.CodeFlow{CreateDummyCodeFlow(CreateDummyThreadFlow(CreateDummyLocation("file", 0, 0, 0, 0, "snippet")))}, + }, + { + result: CreateDummyResultWithOneLocation("file", 0, 0, 0, 0, "snippet", "rule", "level").WithCodeFlows([]*sarif.CodeFlow{ + CreateDummyCodeFlow(CreateDummyThreadFlow( + CreateDummyLocation("file4", 2, 0, 2, 0, "snippetB"), + CreateDummyLocation("file2", 0, 2, 0, 2, "snippetA"), + CreateDummyLocation("file", 0, 0, 0, 0, "snippet"), + )), + CreateDummyCodeFlow(CreateDummyThreadFlow( + CreateDummyLocation("file", 0, 0, 0, 0, "snippet"), + CreateDummyLocation("file2", 1, 0, 1, 0, "snippet"), + )), + CreateDummyCodeFlow(CreateDummyThreadFlow( + CreateDummyLocation("fileC", 1, 1, 1, 1, "snippetC"), + CreateDummyLocation("file", 0, 0, 0, 0, "snippet"), + )), + }), + location: CreateDummyLocation("file", 0, 0, 0, 0, "snippet"), + expectedOutput: []*sarif.CodeFlow{ + CreateDummyCodeFlow(CreateDummyThreadFlow( + CreateDummyLocation("file4", 2, 0, 2, 0, "snippetB"), + CreateDummyLocation("file2", 0, 2, 0, 2, "snippetA"), + CreateDummyLocation("file", 0, 0, 0, 0, "snippet"), + )), + CreateDummyCodeFlow(CreateDummyThreadFlow( + CreateDummyLocation("fileC", 1, 1, 1, 1, "snippetC"), + CreateDummyLocation("file", 0, 0, 0, 0, "snippet"), + )), + }, + }, + } + for _, test := range tests { + assert.Equal(t, test.expectedOutput, GetLocationRelatedCodeFlowsFromResult(test.location, test.result)) + } } func TestGetResultsLocationCount(t *testing.T) { @@ -304,6 +376,8 @@ func TestExtractRelativePath(t *testing.T) { }{ {fullPath: "file:///Users/user/Desktop/secrets_scanner/tests/req.nodejs/file.js", projectPath: "Users/user/Desktop/secrets_scanner/", expectedResult: "tests/req.nodejs/file.js"}, + {fullPath: "file:///private/Users/user/Desktop/secrets_scanner/tests/req.nodejs/file.js", + projectPath: "Users/user/Desktop/secrets_scanner/", expectedResult: "tests/req.nodejs/file.js"}, {fullPath: "invalidFullPath", projectPath: "Users/user/Desktop/secrets_scanner/", expectedResult: "invalidFullPath"}, {fullPath: "",