Skip to content

Commit

Permalink
fix unit test
Browse files Browse the repository at this point in the history
  • Loading branch information
orz25 committed Mar 25, 2024
1 parent 61d4b9f commit d2395f2
Show file tree
Hide file tree
Showing 2 changed files with 11 additions and 7 deletions.
8 changes: 4 additions & 4 deletions audit_test.go
Original file line number Diff line number Diff line change
Expand Up @@ -326,7 +326,7 @@ func TestXrayAuditMultiProjects(t *testing.T) {
defer securityTestUtils.CleanTestsHomeEnv()
output := securityTests.PlatformCli.WithoutCredentials().RunCliCmdWithOutput(t, "audit", "--format="+string(format.SimpleJson), workingDirsFlag)
securityTestUtils.VerifySimpleJsonScanResults(t, output, 35, 0)
securityTestUtils.VerifySimpleJsonJasResults(t, output, 1, 9, 7, 6, 25)
securityTestUtils.VerifySimpleJsonJasResults(t, output, 1, 9, 7, 6, 0, 25, 1)
}

func TestXrayAuditPipJson(t *testing.T) {
Expand Down Expand Up @@ -430,18 +430,18 @@ func addDummyPackageDescriptor(t *testing.T, hasPackageJson bool) {

func TestXrayAuditJasSimpleJson(t *testing.T) {
output := testXrayAuditJas(t, string(format.SimpleJson), filepath.Join("jas", "jas-test"))
securityTestUtils.VerifySimpleJsonJasResults(t, output, 1, 9, 7, 3, 3)
securityTestUtils.VerifySimpleJsonJasResults(t, output, 1, 9, 7, 3, 0, 3, 1)
}

func TestXrayAuditJasSimpleJsonWithConfig(t *testing.T) {
output := testXrayAuditJas(t, string(format.SimpleJson), filepath.Join("jas", "jas-config"))
securityTestUtils.VerifySimpleJsonJasResults(t, output, 0, 0, 1, 3, 3)
securityTestUtils.VerifySimpleJsonJasResults(t, output, 0, 0, 1, 3, 0, 3, 1)
}

func TestXrayAuditJasNoViolationsSimpleJson(t *testing.T) {
output := testXrayAuditJas(t, string(format.SimpleJson), filepath.Join("package-managers", "npm", "npm"))
securityTestUtils.VerifySimpleJsonScanResults(t, output, 1, 0)
securityTestUtils.VerifySimpleJsonJasResults(t, output, 0, 0, 0, 0, 0)
securityTestUtils.VerifySimpleJsonJasResults(t, output, 0, 0, 0, 0, 0, 0, 1)
}

func testXrayAuditJas(t *testing.T, format string, project string) string {
Expand Down
10 changes: 7 additions & 3 deletions tests/utils/test_validation.go
Original file line number Diff line number Diff line change
Expand Up @@ -53,14 +53,15 @@ func VerifySimpleJsonScanResults(t *testing.T, content string, minVulnerabilitie
}
}

func VerifySimpleJsonJasResults(t *testing.T, content string, minSastViolations, minIacViolations, minSecrets, minApplicable, minNotCovered int) {
func VerifySimpleJsonJasResults(t *testing.T, content string, minSastViolations, minIacViolations, minSecrets,
minApplicable, minUndetermined, minNotCovered, minNotApplicable int) {
var results formats.SimpleJsonResults
err := json.Unmarshal([]byte(content), &results)
if assert.NoError(t, err) {
assert.GreaterOrEqual(t, len(results.Sast), minSastViolations, "Found less sast then expected")
assert.GreaterOrEqual(t, len(results.Secrets), minSecrets, "Found less secrets then expected")
assert.GreaterOrEqual(t, len(results.Iacs), minIacViolations, "Found less IaC then expected")
var applicableResults, notApplicableResults, notCoveredResults int
var applicableResults, undeterminedResults, notCoveredResults, notApplicableResults int
for _, vuln := range results.Vulnerabilities {
switch vuln.Applicable {
case string(utils.NotApplicable):
Expand All @@ -69,10 +70,13 @@ func VerifySimpleJsonJasResults(t *testing.T, content string, minSastViolations,
applicableResults++
case string(utils.NotCovered):
notCoveredResults++
case string(utils.ApplicabilityUndetermined):
undeterminedResults++
}
}
assert.GreaterOrEqual(t, applicableResults, minApplicable, "Found less applicableResults then expected")
assert.GreaterOrEqual(t, notApplicableResults, 1, "Found less notApplicableResults then expected")
assert.GreaterOrEqual(t, undeterminedResults, minUndetermined, "Found less undeterminedResults then expected")
assert.GreaterOrEqual(t, notCoveredResults, minNotCovered, "Found less notCoveredResults then expected")
assert.GreaterOrEqual(t, notApplicableResults, minNotApplicable, "Found less notApplicableResults then expected")
}
}

0 comments on commit d2395f2

Please sign in to comment.