|
1 | 1 | package main |
2 | 2 |
|
3 | 3 | import ( |
4 | | -// "strconv" |
5 | | -// "strings" |
6 | | -// "testing" |
| 4 | + "strconv" |
| 5 | + "strings" |
| 6 | + "testing" |
7 | 7 |
|
8 | | -// "path/filepath" |
| 8 | + "path/filepath" |
9 | 9 |
|
10 | | -// "github.com/stretchr/testify/assert" |
11 | | -// securityTestUtils "github.com/jfrog/jfrog-cli-security/tests/utils" |
12 | | -// "github.com/jfrog/jfrog-cli-security/tests/validations" |
| 10 | + "github.com/jfrog/jfrog-cli-security/commands/maliciousscan" |
| 11 | + securityTestUtils "github.com/jfrog/jfrog-cli-security/tests/utils" |
| 12 | + "github.com/jfrog/jfrog-cli-security/tests/validations" |
| 13 | + "github.com/stretchr/testify/assert" |
13 | 14 |
|
14 | | -// "github.com/jfrog/jfrog-cli-core/v2/common/format" |
15 | | -// securityTests "github.com/jfrog/jfrog-cli-security/tests" |
16 | | -// securityIntegrationTestUtils "github.com/jfrog/jfrog-cli-security/tests/utils/integration" |
| 15 | + "github.com/jfrog/jfrog-cli-core/v2/common/format" |
| 16 | + securityTests "github.com/jfrog/jfrog-cli-security/tests" |
| 17 | + securityIntegrationTestUtils "github.com/jfrog/jfrog-cli-security/tests/utils/integration" |
17 | 18 | ) |
18 | 19 |
|
19 | | -// type maliciousScanCommandTestParams struct { |
20 | | -// WorkingDirsToScan []string |
21 | | -// Format format.OutputFormat |
22 | | -// Threads int |
23 | | -// MinSeverity string |
24 | | -// } |
| 20 | +type maliciousScanCommandTestParams struct { |
| 21 | + WorkingDirsToScan []string |
| 22 | + Format format.OutputFormat |
| 23 | + Threads int |
| 24 | + MinSeverity string |
| 25 | +} |
25 | 26 |
|
26 | | -// func getMaliciousScanCmdArgs(params maliciousScanCommandTestParams) (args []string) { |
27 | | -// args = []string{"malicious-scan"} |
28 | | -// if len(params.WorkingDirsToScan) > 0 { |
29 | | -// args = append(args, "--working-dirs="+strings.Join(params.WorkingDirsToScan, ",")) |
30 | | -// } |
31 | | -// if params.Format != "" { |
32 | | -// args = append(args, "--format="+string(params.Format)) |
33 | | -// } |
34 | | -// if params.Threads > 0 { |
35 | | -// args = append(args, "--threads="+strconv.Itoa(params.Threads)) |
36 | | -// } |
37 | | -// if params.MinSeverity != "" { |
38 | | -// args = append(args, "--min-severity="+params.MinSeverity) |
39 | | -// } |
40 | | -// return args |
41 | | -// } |
| 27 | +func getMaliciousScanCmdArgs(params maliciousScanCommandTestParams) (args []string) { |
| 28 | + args = []string{"malicious-scan"} |
| 29 | + if len(params.WorkingDirsToScan) > 0 { |
| 30 | + args = append(args, "--working-dirs="+strings.Join(params.WorkingDirsToScan, ",")) |
| 31 | + } |
| 32 | + if params.Format != "" { |
| 33 | + args = append(args, "--format="+string(params.Format)) |
| 34 | + } |
| 35 | + if params.Threads > 0 { |
| 36 | + args = append(args, "--threads="+strconv.Itoa(params.Threads)) |
| 37 | + } |
| 38 | + if params.MinSeverity != "" { |
| 39 | + args = append(args, "--min-severity="+params.MinSeverity) |
| 40 | + } |
| 41 | + return args |
| 42 | +} |
42 | 43 |
|
43 | | -// func runMaliciousScan(t *testing.T, params maliciousScanCommandTestParams) (string, error) { |
44 | | -// cleanUp := securityIntegrationTestUtils.UseTestHomeWithDefaultXrayConfig(t) |
45 | | -// defer cleanUp() |
46 | | -// return securityTests.PlatformCli.RunCliCmdWithOutputs(t, getMaliciousScanCmdArgs(params)...) |
47 | | -// } |
| 44 | +func runMaliciousScan(t *testing.T, params maliciousScanCommandTestParams) (string, error) { |
| 45 | + cleanUp := securityIntegrationTestUtils.UseTestHomeWithDefaultXrayConfig(t) |
| 46 | + defer cleanUp() |
| 47 | + return securityTests.PlatformCli.RunCliCmdWithOutputs(t, getMaliciousScanCmdArgs(params)...) |
| 48 | +} |
48 | 49 |
|
49 | | -// func TestMaliciousScan(t *testing.T) { |
50 | | -// testCases := []struct { |
51 | | -// name string |
52 | | -// format format.OutputFormat |
53 | | -// projectPath string |
54 | | -// expectedIssues int |
55 | | -// }{ |
56 | | -// { |
57 | | -// name: "Malicious scan with findings (Simple JSON)", |
58 | | -// format: format.SimpleJson, |
59 | | -// projectPath: filepath.Join("projects", "jas", "jas", "malicious"), |
60 | | -// expectedIssues: 1, |
61 | | -// }, |
62 | | -// { |
63 | | -// name: "Malicious scan without findings (Simple JSON)", |
64 | | -// format: format.SimpleJson, |
65 | | -// projectPath: filepath.Join("projects", "empty_project", "python_project_with_no_deps"), |
66 | | -// expectedIssues: 0, |
67 | | -// }, |
68 | | -// } |
69 | | -// for _, tc := range testCases { |
70 | | -// t.Run(tc.name, func(t *testing.T) { |
71 | | -// fullProjectPath := filepath.Join(filepath.FromSlash(securityTests.GetTestResourcesPath()), tc.projectPath) |
72 | | -// _, cleanUp := securityTestUtils.CreateTestProjectEnvAndChdir(t, fullProjectPath) |
73 | | -// defer cleanUp() |
| 50 | +func TestMaliciousScan(t *testing.T) { |
| 51 | + securityIntegrationTestUtils.InitMaliciousScanTest(t, maliciousscan.MinimumXrayVersionForMaliciousScan) |
| 52 | + testCases := []struct { |
| 53 | + name string |
| 54 | + format format.OutputFormat |
| 55 | + projectPath string |
| 56 | + expectedIssues int |
| 57 | + }{ |
| 58 | + { |
| 59 | + name: "Malicious scan with findings (Simple JSON)", |
| 60 | + format: format.SimpleJson, |
| 61 | + projectPath: filepath.Join("projects", "jas", "jas", "malicious"), |
| 62 | + expectedIssues: 1, |
| 63 | + }, |
| 64 | + { |
| 65 | + name: "Malicious scan without findings (Simple JSON)", |
| 66 | + format: format.SimpleJson, |
| 67 | + projectPath: filepath.Join("projects", "empty_project", "python_project_with_no_deps"), |
| 68 | + expectedIssues: 0, |
| 69 | + }, |
| 70 | + } |
| 71 | + for _, tc := range testCases { |
| 72 | + t.Run(tc.name, func(t *testing.T) { |
| 73 | + fullProjectPath := filepath.Join(filepath.FromSlash(securityTests.GetTestResourcesPath()), tc.projectPath) |
| 74 | + _, cleanUp := securityTestUtils.CreateTestProjectEnvAndChdir(t, fullProjectPath) |
| 75 | + defer cleanUp() |
74 | 76 |
|
75 | | -// params := maliciousScanCommandTestParams{ |
76 | | -// Format: tc.format, |
77 | | -// } |
78 | | -// output, err := runMaliciousScan(t, params) |
79 | | -// assert.NoError(t, err) |
| 77 | + params := maliciousScanCommandTestParams{ |
| 78 | + Format: tc.format, |
| 79 | + } |
| 80 | + output, err := runMaliciousScan(t, params) |
| 81 | + assert.NoError(t, err) |
80 | 82 |
|
81 | | -// validationsParams := validations.ValidationParams{ |
82 | | -// Vulnerabilities: &validations.VulnerabilityCount{ |
83 | | -// ValidateScan: &validations.ScanCount{MaliciousCode: tc.expectedIssues}, |
84 | | -// }, |
85 | | -// } |
86 | | -// if tc.expectedIssues == 0 { |
87 | | -// validationsParams.ExactResultsMatch = true |
88 | | -// } |
89 | | -// validations.ValidateCommandOutput(t, output, tc.format, validationsParams) |
90 | | -// }) |
91 | | -// } |
92 | | -// } |
| 83 | + validationsParams := validations.ValidationParams{ |
| 84 | + Vulnerabilities: &validations.VulnerabilityCount{ |
| 85 | + ValidateScan: &validations.ScanCount{MaliciousCode: tc.expectedIssues}, |
| 86 | + }, |
| 87 | + } |
| 88 | + if tc.expectedIssues == 0 { |
| 89 | + validationsParams.ExactResultsMatch = true |
| 90 | + } |
| 91 | + validations.ValidateCommandOutput(t, output, tc.format, validationsParams) |
| 92 | + }) |
| 93 | + } |
| 94 | +} |
93 | 95 |
|
94 | | -// func TestMaliciousScanWithWorkingDirs(t *testing.T) { |
95 | | -// maliciousProjectPath := filepath.Join(filepath.FromSlash(securityTests.GetTestResourcesPath()), "projects", "jas", "jas", "malicious") |
96 | | -// _, cleanUp := securityTestUtils.CreateTestProjectEnvAndChdir(t, maliciousProjectPath) |
97 | | -// defer cleanUp() |
| 96 | +func TestMaliciousScanWithWorkingDirs(t *testing.T) { |
| 97 | + securityIntegrationTestUtils.InitMaliciousScanTest(t, maliciousscan.MinimumXrayVersionForMaliciousScan) |
98 | 98 |
|
99 | | -// params := maliciousScanCommandTestParams{ |
100 | | -// WorkingDirsToScan: []string{"."}, |
101 | | -// Format: format.SimpleJson, |
102 | | -// } |
103 | | -// output, err := runMaliciousScan(t, params) |
104 | | -// assert.NoError(t, err) |
| 99 | + maliciousProjectPath := filepath.Join(filepath.FromSlash(securityTests.GetTestResourcesPath()), "projects", "jas", "jas", "malicious") |
| 100 | + _, cleanUp := securityTestUtils.CreateTestProjectEnvAndChdir(t, maliciousProjectPath) |
| 101 | + defer cleanUp() |
105 | 102 |
|
106 | | -// validationsParams := validations.ValidationParams{ |
107 | | -// Vulnerabilities: &validations.VulnerabilityCount{ |
108 | | -// ValidateScan: &validations.ScanCount{MaliciousCode: 1}, |
109 | | -// }, |
110 | | -// } |
111 | | -// validations.ValidateCommandOutput(t, output, format.SimpleJson, validationsParams) |
112 | | -// } |
| 103 | + params := maliciousScanCommandTestParams{ |
| 104 | + WorkingDirsToScan: []string{"."}, |
| 105 | + Format: format.SimpleJson, |
| 106 | + } |
| 107 | + output, err := runMaliciousScan(t, params) |
| 108 | + assert.NoError(t, err) |
| 109 | + |
| 110 | + validationsParams := validations.ValidationParams{ |
| 111 | + Vulnerabilities: &validations.VulnerabilityCount{ |
| 112 | + ValidateScan: &validations.ScanCount{MaliciousCode: 1}, |
| 113 | + }, |
| 114 | + } |
| 115 | + validations.ValidateCommandOutput(t, output, format.SimpleJson, validationsParams) |
| 116 | +} |
0 commit comments