Skip to content

Commit

Permalink
more tests
Browse files Browse the repository at this point in the history
  • Loading branch information
attiasas committed Sep 14, 2023
1 parent deaa75f commit 0b26f04
Show file tree
Hide file tree
Showing 2 changed files with 90 additions and 0 deletions.
16 changes: 16 additions & 0 deletions xray/utils/sarifutils.go
Original file line number Diff line number Diff line change
Expand Up @@ -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
}
74 changes: 74 additions & 0 deletions xray/utils/sarifutils_test.go
Original file line number Diff line number Diff line change
Expand Up @@ -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) {
Expand Down Expand Up @@ -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: "",
Expand Down

0 comments on commit 0b26f04

Please sign in to comment.