forked from jfrog/jfrog-cli-security
-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathxsc_test.go
More file actions
115 lines (101 loc) · 3.98 KB
/
xsc_test.go
File metadata and controls
115 lines (101 loc) · 3.98 KB
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
package main
import (
"encoding/json"
"errors"
"testing"
"github.com/stretchr/testify/assert"
"github.com/stretchr/testify/require"
"github.com/jfrog/jfrog-cli-core/v2/common/format"
"github.com/jfrog/jfrog-cli-security/tests/validations"
"github.com/jfrog/jfrog-cli-security/utils/formats"
"github.com/jfrog/jfrog-cli-security/utils/xsc"
"github.com/jfrog/jfrog-cli-security/tests"
securityTestUtils "github.com/jfrog/jfrog-cli-security/tests/utils"
"github.com/jfrog/jfrog-cli-security/tests/utils/integration"
xscservices "github.com/jfrog/jfrog-client-go/xsc/services"
)
func TestReportError(t *testing.T) {
xrayVersion, xscVersion, cleanUp := integration.InitXscTest(t)
securityTestUtils.ValidateXscVersion(t, xscVersion, xsc.MinXscVersionForErrorReport)
defer cleanUp()
errorToReport := errors.New("THIS IS NOT A REAL ERROR! This Error is posted as part of TestReportError test")
assert.NoError(t, xsc.ReportError(xrayVersion, xscVersion, tests.XscDetails, errorToReport, "cli", ""))
}
// In the npm tests we use a watch flag, so we would get only violations
func TestXscAuditNpmJsonWithWatch(t *testing.T) {
_, _, cleanUp := integration.InitXscTest(t)
defer cleanUp()
testCases := []struct {
name string
format format.OutputFormat
withVuln bool
}{
{
name: "No violations (JSON)",
format: format.Json,
},
{
name: "No violations (Simple JSON)",
format: format.SimpleJson,
withVuln: true,
},
// SARIF format is not supported for validations (not implemented)
}
for _, tc := range testCases {
t.Run(tc.name, func(t *testing.T) {
validationsParams := validations.ValidationParams{
Total: &validations.TotalCount{Licenses: 1, Violations: 1},
Violations: &validations.ViolationCount{ValidateType: &validations.ScaViolationCount{Security: 1}},
}
if tc.withVuln {
validationsParams.Total.Vulnerabilities = 1
validationsParams.Vulnerabilities = &validations.VulnerabilityCount{ValidateScan: &validations.ScanCount{Sca: 1}}
}
validations.ValidateCommandOutput(t, testAuditNpm(t, tc.format, "xsc-", tc.withVuln), tc.format, validationsParams)
})
}
}
func TestXscAuditMaven(t *testing.T) {
_, _, cleanUp := integration.InitXscTest(t)
defer cleanUp()
for _, format := range []format.OutputFormat{format.Json, format.SimpleJson} {
t.Run(string(format), func(t *testing.T) {
validations.ValidateCommandOutput(t, testAuditMaven(t, format), format, validations.ValidationParams{
Total: &validations.TotalCount{Licenses: 1, Vulnerabilities: 1},
})
})
}
}
func TestXscAnalyticsForAudit(t *testing.T) {
xrayVersion, xscVersion, cleanUp := integration.InitXscTest(t)
defer cleanUp()
// Scan npm project and verify that analytics general event were sent to XSC.
output := testAuditNpm(t, format.SimpleJson, "xsc-analytics", false)
validateAnalyticsBasicEvent(t, xrayVersion, xscVersion, output)
}
func validateAnalyticsBasicEvent(t *testing.T, xrayVersion, xscVersion, output string) {
// Get MSI.
var results formats.SimpleJsonResults
err := json.Unmarshal([]byte(output), &results)
require.NoError(t, err)
// Verify analytics metrics.
event, err := xsc.GetScanEvent(xrayVersion, xscVersion, results.MultiScanId, tests.XscDetails, "")
require.NoError(t, err)
assert.NotNil(t, event)
assert.NotEmpty(t, results.MultiScanId)
// Event creation and addition information.
assert.Equal(t, xscservices.CliProduct, event.Product)
assert.Equal(t, xscservices.CliEventType, event.EventType)
assert.NotEmpty(t, event.AnalyzerManagerVersion)
assert.NotEmpty(t, event.EventStatus)
// The information that was added after updating the event with the scan's results.
assert.NotEmpty(t, event.TotalScanDuration)
assert.True(t, event.TotalFindings > 0)
}
func TestAdvancedSecurityDockerScanWithXsc(t *testing.T) {
testCli, cleanupDocker := integration.InitNativeDockerTest(t)
defer cleanupDocker()
cleanUpXsc := integration.PrepareXscForTest(t)
defer cleanUpXsc()
runAdvancedSecurityDockerScan(t, testCli, "jfrog/demo-security:latest")
}