diff --git a/xray/commands/audit/jas/common_test.go b/xray/commands/audit/jas/common_test.go index 33ef7a60d..94e270195 100644 --- a/xray/commands/audit/jas/common_test.go +++ b/xray/commands/audit/jas/common_test.go @@ -1,10 +1,11 @@ package jas import ( + "testing" + "github.com/jfrog/jfrog-cli-core/v2/xray/utils" "github.com/owenrumney/go-sarif/v2/sarif" "github.com/stretchr/testify/assert" - "testing" ) func TestExcludeSuppressResults(t *testing.T) { @@ -15,27 +16,27 @@ func TestExcludeSuppressResults(t *testing.T) { }{ { sarifResults: []*sarif.Result{ - utils.GetDummyResultWithOneLocation("", 0, 0, 0, 0, "snippet1", "ruleId1", "level1"), - utils.GetDummyResultWithOneLocation("", 0, 0, 0, 0, "snippet2", "ruleId2", "level2"), + utils.CreateDummyResultWithOneLocation("", 0, 0, 0, 0, "snippet1", "ruleId1", "level1"), + utils.CreateDummyResultWithOneLocation("", 0, 0, 0, 0, "snippet2", "ruleId2", "level2"), }, expectedOutput: []*sarif.Result{ - utils.GetDummyResultWithOneLocation("", 0, 0, 0, 0, "snippet1", "ruleId1", "level1"), - utils.GetDummyResultWithOneLocation("", 0, 0, 0, 0, "snippet2", "ruleId2", "level2"), + utils.CreateDummyResultWithOneLocation("", 0, 0, 0, 0, "snippet1", "ruleId1", "level1"), + utils.CreateDummyResultWithOneLocation("", 0, 0, 0, 0, "snippet2", "ruleId2", "level2"), }, }, { sarifResults: []*sarif.Result{ - utils.GetDummyResultWithOneLocation("", 0, 0, 0, 0, "snippet1", "ruleId1", "level1").WithSuppression([]*sarif.Suppression{sarif.NewSuppression("")}), - utils.GetDummyResultWithOneLocation("", 0, 0, 0, 0, "snippet2", "ruleId2", "level2"), + utils.CreateDummyResultWithOneLocation("", 0, 0, 0, 0, "snippet1", "ruleId1", "level1").WithSuppression([]*sarif.Suppression{sarif.NewSuppression("")}), + utils.CreateDummyResultWithOneLocation("", 0, 0, 0, 0, "snippet2", "ruleId2", "level2"), }, expectedOutput: []*sarif.Result{ - utils.GetDummyResultWithOneLocation("", 0, 0, 0, 0, "snippet2", "ruleId2", "level2"), + utils.CreateDummyResultWithOneLocation("", 0, 0, 0, 0, "snippet2", "ruleId2", "level2"), }, }, { sarifResults: []*sarif.Result{ - utils.GetDummyResultWithOneLocation("", 0, 0, 0, 0, "snippet1", "ruleId1", "level1").WithSuppression([]*sarif.Suppression{sarif.NewSuppression("")}), - utils.GetDummyResultWithOneLocation("", 0, 0, 0, 0, "snippet2", "ruleId2", "level2").WithSuppression([]*sarif.Suppression{sarif.NewSuppression("")}), + utils.CreateDummyResultWithOneLocation("", 0, 0, 0, 0, "snippet1", "ruleId1", "level1").WithSuppression([]*sarif.Suppression{sarif.NewSuppression("")}), + utils.CreateDummyResultWithOneLocation("", 0, 0, 0, 0, "snippet2", "ruleId2", "level2").WithSuppression([]*sarif.Suppression{sarif.NewSuppression("")}), }, expectedOutput: []*sarif.Result{}, }, @@ -54,10 +55,10 @@ func TestAddScoreToRunRules(t *testing.T) { expectedOutput []*sarif.ReportingDescriptor }{ { - sarifRun: utils.GetRunWithDummyResults( - utils.GetDummyResultWithOneLocation("file1", 0, 0, 0, 0, "snippet", "rule1", "info"), - utils.GetDummyResultWithOneLocation("file2", 0, 0, 0, 0, "snippet", "rule1", "info"), - utils.GetDummyResultWithOneLocation("file", 0, 0, 0, 0, "snippet", "rule2", "warning"), + sarifRun: utils.CreateRunWithDummyResults( + utils.CreateDummyResultWithOneLocation("file1", 0, 0, 0, 0, "snippet", "rule1", "info"), + utils.CreateDummyResultWithOneLocation("file2", 0, 0, 0, 0, "snippet", "rule1", "info"), + utils.CreateDummyResultWithOneLocation("file", 0, 0, 0, 0, "snippet", "rule2", "warning"), ), expectedOutput: []*sarif.ReportingDescriptor{ sarif.NewRule("rule1").WithProperties(sarif.Properties{"security-severity": "6.9"}), @@ -65,12 +66,12 @@ func TestAddScoreToRunRules(t *testing.T) { }, }, { - sarifRun: utils.GetRunWithDummyResults( - utils.GetDummyResultWithOneLocation("file", 0, 0, 0, 0, "snippet", "rule1", "none"), - utils.GetDummyResultWithOneLocation("file", 0, 0, 0, 0, "snippet", "rule2", "note"), - utils.GetDummyResultWithOneLocation("file", 0, 0, 0, 0, "snippet", "rule3", "info"), - utils.GetDummyResultWithOneLocation("file", 0, 0, 0, 0, "snippet", "rule4", "warning"), - utils.GetDummyResultWithOneLocation("file", 0, 0, 0, 0, "snippet", "rule5", "error"), + sarifRun: utils.CreateRunWithDummyResults( + utils.CreateDummyResultWithOneLocation("file", 0, 0, 0, 0, "snippet", "rule1", "none"), + utils.CreateDummyResultWithOneLocation("file", 0, 0, 0, 0, "snippet", "rule2", "note"), + utils.CreateDummyResultWithOneLocation("file", 0, 0, 0, 0, "snippet", "rule3", "info"), + utils.CreateDummyResultWithOneLocation("file", 0, 0, 0, 0, "snippet", "rule4", "warning"), + utils.CreateDummyResultWithOneLocation("file", 0, 0, 0, 0, "snippet", "rule5", "error"), ), expectedOutput: []*sarif.ReportingDescriptor{ sarif.NewRule("rule1").WithProperties(sarif.Properties{"security-severity": "0.0"}), diff --git a/xray/utils/resultstable_test.go b/xray/utils/resultstable_test.go index 476dea718..ca1b2a026 100644 --- a/xray/utils/resultstable_test.go +++ b/xray/utils/resultstable_test.go @@ -439,9 +439,9 @@ func TestGetApplicableCveValue(t *testing.T) { { scanResults: &ExtendedScanResults{ ApplicabilityScanResults: []*sarif.Run{ - GetRunWithDummyResults( - GetDummyResultWithOneLocation("fileName1", 0, 1, 0, 0, "snippet1", "applic_testCve1", "info"), - GetDummyPassingResult("applic_testCve2"), + CreateRunWithDummyResults( + CreateDummyResultWithOneLocation("fileName1", 0, 1, 0, 0, "snippet1", "applic_testCve1", "info"), + CreateDummyPassingResult("applic_testCve2"), ), }, EntitledForJas: true, @@ -453,9 +453,9 @@ func TestGetApplicableCveValue(t *testing.T) { { scanResults: &ExtendedScanResults{ ApplicabilityScanResults: []*sarif.Run{ - GetRunWithDummyResults( - GetDummyPassingResult("applic_testCve1"), - GetDummyResultWithOneLocation("fileName2", 1, 0, 0, 0, "snippet2", "applic_testCve2", "warning"), + CreateRunWithDummyResults( + CreateDummyPassingResult("applic_testCve1"), + CreateDummyResultWithOneLocation("fileName2", 1, 0, 0, 0, "snippet2", "applic_testCve2", "warning"), ), }, EntitledForJas: true, @@ -467,9 +467,9 @@ func TestGetApplicableCveValue(t *testing.T) { { scanResults: &ExtendedScanResults{ ApplicabilityScanResults: []*sarif.Run{ - GetRunWithDummyResults( - GetDummyPassingResult("applic_testCve1"), - GetDummyResultWithOneLocation("fileName3", 0, 1, 0, 0, "snippet3", "applic_testCve2", "info"), + CreateRunWithDummyResults( + CreateDummyPassingResult("applic_testCve1"), + CreateDummyResultWithOneLocation("fileName3", 0, 1, 0, 0, "snippet3", "applic_testCve2", "info"), ), }, EntitledForJas: true, @@ -481,9 +481,9 @@ func TestGetApplicableCveValue(t *testing.T) { { scanResults: &ExtendedScanResults{ ApplicabilityScanResults: []*sarif.Run{ - GetRunWithDummyResults( - GetDummyPassingResult("applic_testCve1"), - GetDummyPassingResult("applic_testCve2"), + CreateRunWithDummyResults( + CreateDummyPassingResult("applic_testCve1"), + CreateDummyPassingResult("applic_testCve2"), ), }, EntitledForJas: true, @@ -495,9 +495,9 @@ func TestGetApplicableCveValue(t *testing.T) { { scanResults: &ExtendedScanResults{ ApplicabilityScanResults: []*sarif.Run{ - GetRunWithDummyResults( - GetDummyPassingResult("applic_testCve1"), - GetDummyResultWithOneLocation("fileName4", 1, 0, 0, 0, "snippet", "applic_testCve2", "warning"), + CreateRunWithDummyResults( + CreateDummyPassingResult("applic_testCve1"), + CreateDummyResultWithOneLocation("fileName4", 1, 0, 0, 0, "snippet", "applic_testCve2", "warning"), ), }, EntitledForJas: true, @@ -509,7 +509,7 @@ func TestGetApplicableCveValue(t *testing.T) { { scanResults: &ExtendedScanResults{ ApplicabilityScanResults: []*sarif.Run{ - GetRunWithDummyResults(GetDummyPassingResult("applic_testCve1")), + CreateRunWithDummyResults(CreateDummyPassingResult("applic_testCve1")), }, EntitledForJas: true}, cves: []services.Cve{{Id: "testCve1"}, {Id: "testCve2"}}, diff --git a/xray/utils/sarifutils.go b/xray/utils/sarifutils.go index 4ae8fd4a2..57effb331 100644 --- a/xray/utils/sarifutils.go +++ b/xray/utils/sarifutils.go @@ -260,7 +260,7 @@ func GetInvocationWorkingDirectory(invocation *sarif.Invocation) string { return "" } -func GetRunWithDummyResults(results ...*sarif.Result) *sarif.Run { +func CreateRunWithDummyResults(results ...*sarif.Result) *sarif.Run { run := sarif.NewRunWithInformationURI("", "") for _, result := range results { run.AddResult(result) @@ -268,7 +268,7 @@ func GetRunWithDummyResults(results ...*sarif.Result) *sarif.Run { return run } -func GetDummyResultWithLocations(msg, ruleId, level string, locations ...*sarif.Location) *sarif.Result { +func CreateDummyResultWithLocations(msg, ruleId, level string, locations ...*sarif.Location) *sarif.Result { return &sarif.Result{ Message: *sarif.NewTextMessage(msg), Locations: locations, @@ -277,7 +277,7 @@ func GetDummyResultWithLocations(msg, ruleId, level string, locations ...*sarif. } } -func GetDummyLocation(fileName string, startLine, startCol, endLine, endCol int, snippet string) *sarif.Location { +func CreateDummyLocation(fileName string, startLine, startCol, endLine, endCol int, snippet string) *sarif.Location { return &sarif.Location{ PhysicalLocation: &sarif.PhysicalLocation{ ArtifactLocation: &sarif.ArtifactLocation{URI: &fileName}, @@ -290,7 +290,7 @@ func GetDummyLocation(fileName string, startLine, startCol, endLine, endCol int, } } -func GetDummyPassingResult(ruleId string) *sarif.Result { +func CreateDummyPassingResult(ruleId string) *sarif.Result { kind := "pass" return &sarif.Result{ Kind: &kind, @@ -298,9 +298,9 @@ func GetDummyPassingResult(ruleId string) *sarif.Result { } } -func GetDummyResultWithOneLocation(fileName string, startLine, startCol, endLine, endCol int, snippet, ruleId, level string) *sarif.Result { +func CreateDummyResultWithOneLocation(fileName string, startLine, startCol, endLine, endCol int, snippet, ruleId, level string) *sarif.Result { return &sarif.Result{ - Locations: []*sarif.Location{GetDummyLocation(fileName, startCol, startCol, endLine, endCol, snippet)}, + Locations: []*sarif.Location{CreateDummyLocation(fileName, startCol, startCol, endLine, endCol, snippet)}, Level: &level, RuleID: &ruleId, } diff --git a/xray/utils/sarifutils_test.go b/xray/utils/sarifutils_test.go index 6dc4f21bb..c9182c4a6 100644 --- a/xray/utils/sarifutils_test.go +++ b/xray/utils/sarifutils_test.go @@ -25,30 +25,30 @@ func TestGetResultsLocationCount(t *testing.T) { expectedOutput: 0, }, { - runs: []*sarif.Run{GetRunWithDummyResults()}, + runs: []*sarif.Run{CreateRunWithDummyResults()}, expectedOutput: 0, }, { - runs: []*sarif.Run{GetRunWithDummyResults( - GetDummyPassingResult("rule"), - GetDummyResultWithOneLocation("file", 0, 0, 0, 0, "snippet", "rule", "level"), + runs: []*sarif.Run{CreateRunWithDummyResults( + CreateDummyPassingResult("rule"), + CreateDummyResultWithOneLocation("file", 0, 0, 0, 0, "snippet", "rule", "level"), )}, expectedOutput: 1, }, { runs: []*sarif.Run{ - GetRunWithDummyResults( - GetDummyPassingResult("rule"), - GetDummyResultWithOneLocation("file", 0, 0, 0, 0, "snippet", "rule", "level"), + CreateRunWithDummyResults( + CreateDummyPassingResult("rule"), + CreateDummyResultWithOneLocation("file", 0, 0, 0, 0, "snippet", "rule", "level"), ), - GetRunWithDummyResults( - GetDummyResultWithLocations( + CreateRunWithDummyResults( + CreateDummyResultWithLocations( "msg", "rule", "level", - GetDummyLocation("file", 0, 0, 0, 0, "snippet"), - GetDummyLocation("file", 0, 0, 0, 0, "snippet"), - GetDummyLocation("file", 0, 0, 0, 0, "snippet"), + CreateDummyLocation("file", 0, 0, 0, 0, "snippet"), + CreateDummyLocation("file", 0, 0, 0, 0, "snippet"), + CreateDummyLocation("file", 0, 0, 0, 0, "snippet"), ), ), }, @@ -71,7 +71,7 @@ func TestGetResultMsgText(t *testing.T) { expectedOutput: "", }, { - result: GetDummyResultWithLocations("msg", "rule", "level"), + result: CreateDummyResultWithLocations("msg", "rule", "level"), expectedOutput: "msg", }, } @@ -91,7 +91,7 @@ func TestGetLocationSnippet(t *testing.T) { expectedOutput: "", }, { - location: GetDummyLocation("filename", 1, 2, 3, 4, "snippet"), + location: CreateDummyLocation("filename", 1, 2, 3, 4, "snippet"), expectedOutput: "snippet", }, } @@ -111,7 +111,7 @@ func TestSetLocationSnippet(t *testing.T) { expectedOutput: "", }, { - location: GetDummyLocation("filename", 1, 2, 3, 4, "snippet"), + location: CreateDummyLocation("filename", 1, 2, 3, 4, "snippet"), expectedOutput: "changedSnippet", }, } @@ -132,7 +132,7 @@ func TestGetLocationFileName(t *testing.T) { expectedOutput: "", }, { - location: GetDummyLocation("filename", 1, 2, 3, 4, "snippet"), + location: CreateDummyLocation("filename", 1, 2, 3, 4, "snippet"), expectedOutput: "filename", }, } @@ -149,17 +149,17 @@ func TestGetRelativeLocationFileName(t *testing.T) { expectedOutput string }{ { - location: GetDummyLocation("file:///root/someDir/another/file", 1, 2, 3, 4, "snippet"), + location: CreateDummyLocation("file:///root/someDir/another/file", 1, 2, 3, 4, "snippet"), invocations: []*sarif.Invocation{}, expectedOutput: "root/someDir/another/file", }, { - location: GetDummyLocation("file:///root/someDir/another/file", 1, 2, 3, 4, "snippet"), + location: CreateDummyLocation("file:///root/someDir/another/file", 1, 2, 3, 4, "snippet"), invocations: []*sarif.Invocation{{WorkingDirectory: sarif.NewSimpleArtifactLocation("/not/relevant")}}, expectedOutput: "root/someDir/another/file", }, { - location: GetDummyLocation("file:///root/someDir/another/file", 1, 2, 3, 4, "snippet"), + location: CreateDummyLocation("file:///root/someDir/another/file", 1, 2, 3, 4, "snippet"), invocations: []*sarif.Invocation{{WorkingDirectory: sarif.NewSimpleArtifactLocation("/root/someDir/")}}, expectedOutput: "another/file", }, @@ -180,7 +180,7 @@ func TestSetLocationFileName(t *testing.T) { expectedOutput: "", }, { - location: GetDummyLocation("filename", 1, 2, 3, 4, "snippet"), + location: CreateDummyLocation("filename", 1, 2, 3, 4, "snippet"), expectedOutput: "changedFilename", }, } @@ -205,7 +205,7 @@ func TestGetLocationRegion(t *testing.T) { expectedOutput: nil, }, { - location: GetDummyLocation("filename", 1, 2, 3, 4, "snippet"), + location: CreateDummyLocation("filename", 1, 2, 3, 4, "snippet"), expectedOutput: sarif.NewRegion().WithStartLine(1).WithStartColumn(2).WithEndLine(3).WithEndColumn(4). WithSnippet(sarif.NewArtifactContent().WithText("snippet")), }, @@ -226,7 +226,7 @@ func TestGetLocationStartLine(t *testing.T) { expectedOutput: 0, }, { - location: GetDummyLocation("filename", 1, 2, 3, 4, "snippet"), + location: CreateDummyLocation("filename", 1, 2, 3, 4, "snippet"), expectedOutput: 1, }, } @@ -246,7 +246,7 @@ func TestGetLocationStartColumn(t *testing.T) { expectedOutput: 0, }, { - location: GetDummyLocation("filename", 1, 2, 3, 4, "snippet"), + location: CreateDummyLocation("filename", 1, 2, 3, 4, "snippet"), expectedOutput: 2, }, } @@ -266,7 +266,7 @@ func TestGetLocationEndLine(t *testing.T) { expectedOutput: 0, }, { - location: GetDummyLocation("filename", 1, 2, 3, 4, "snippet"), + location: CreateDummyLocation("filename", 1, 2, 3, 4, "snippet"), expectedOutput: 3, }, } @@ -286,7 +286,7 @@ func TestGetLocationEndColumn(t *testing.T) { expectedOutput: 0, }, { - location: GetDummyLocation("filename", 1, 2, 3, 4, "snippet"), + location: CreateDummyLocation("filename", 1, 2, 3, 4, "snippet"), expectedOutput: 4, }, } @@ -388,11 +388,11 @@ func TestIsApplicableResult(t *testing.T) { expectedOutput bool }{ { - sarifResult: GetDummyPassingResult("rule"), + sarifResult: CreateDummyPassingResult("rule"), expectedOutput: false, }, { - sarifResult: GetDummyResultWithOneLocation("file", 0, 0, 0, 0, "snippet1", "ruleId1", "level1"), + sarifResult: CreateDummyResultWithOneLocation("file", 0, 0, 0, 0, "snippet1", "ruleId1", "level1"), expectedOutput: true, }, } @@ -460,22 +460,22 @@ func TestGetRunRules(t *testing.T) { expectedOutput: []*sarif.ReportingDescriptor{}, }, { - run: GetRunWithDummyResults(), + run: CreateRunWithDummyResults(), expectedOutput: []*sarif.ReportingDescriptor{}, }, { - run: GetRunWithDummyResults( - GetDummyPassingResult("rule1"), + run: CreateRunWithDummyResults( + CreateDummyPassingResult("rule1"), ), expectedOutput: []*sarif.ReportingDescriptor{sarif.NewRule("rule1")}, }, { - run: GetRunWithDummyResults( - GetDummyPassingResult("rule1"), - GetDummyPassingResult("rule1"), - GetDummyPassingResult("rule2"), - GetDummyPassingResult("rule3"), - GetDummyPassingResult("rule2"), + run: CreateRunWithDummyResults( + CreateDummyPassingResult("rule1"), + CreateDummyPassingResult("rule1"), + CreateDummyPassingResult("rule2"), + CreateDummyPassingResult("rule3"), + CreateDummyPassingResult("rule2"), ), expectedOutput: []*sarif.ReportingDescriptor{sarif.NewRule("rule1"), sarif.NewRule("rule2"), sarif.NewRule("rule3")}, },