|
| 1 | +package enrich |
| 2 | + |
| 3 | +import ( |
| 4 | + "bytes" |
| 5 | + "strings" |
| 6 | + "testing" |
| 7 | + |
| 8 | + coreformat "github.com/jfrog/jfrog-cli-core/v2/common/format" |
| 9 | + "github.com/jfrog/jfrog-cli-security/utils" |
| 10 | + "github.com/jfrog/jfrog-cli-security/utils/results" |
| 11 | + "github.com/jfrog/jfrog-client-go/xray/services" |
| 12 | + "github.com/stretchr/testify/assert" |
| 13 | + "github.com/stretchr/testify/require" |
| 14 | +) |
| 15 | + |
| 16 | +func makeCmdResultsWithVulns(vulns []services.Vulnerability) *results.SecurityCommandResults { |
| 17 | + cmdResults := results.NewCommandResults(utils.SBOM) |
| 18 | + target := cmdResults.NewScanResults(results.ScanTarget{Target: "test.json", Name: "test.json"}) |
| 19 | + target.ScaScanResults(0, services.ScanResponse{Vulnerabilities: vulns}) |
| 20 | + return cmdResults |
| 21 | +} |
| 22 | + |
| 23 | +func TestPrintVulnerabilitiesTable_WithFindings(t *testing.T) { |
| 24 | + cmdResults := makeCmdResultsWithVulns([]services.Vulnerability{ |
| 25 | + { |
| 26 | + Cves: []services.Cve{{Id: "CVE-2021-1234"}}, |
| 27 | + Components: map[string]services.Component{"pkg:npm/lodash@4.17.11": {}}, |
| 28 | + }, |
| 29 | + { |
| 30 | + Cves: []services.Cve{{Id: "CVE-2020-9999"}}, |
| 31 | + Components: map[string]services.Component{"pkg:npm/minimist@1.2.5": {}}, |
| 32 | + }, |
| 33 | + }) |
| 34 | + |
| 35 | + cmd := &EnrichCommand{outputFormat: coreformat.Table} |
| 36 | + var buf bytes.Buffer |
| 37 | + err := cmd.printVulnerabilitiesTable(cmdResults, &buf) |
| 38 | + require.NoError(t, err) |
| 39 | + |
| 40 | + out := buf.String() |
| 41 | + assert.Contains(t, out, "COMPONENT") |
| 42 | + assert.Contains(t, out, "CVE-ID") |
| 43 | + assert.Contains(t, out, "pkg:npm/lodash@4.17.11") |
| 44 | + assert.Contains(t, out, "CVE-2021-1234") |
| 45 | + assert.Contains(t, out, "pkg:npm/minimist@1.2.5") |
| 46 | + assert.Contains(t, out, "CVE-2020-9999") |
| 47 | +} |
| 48 | + |
| 49 | +func TestPrintVulnerabilitiesTable_Empty(t *testing.T) { |
| 50 | + cmdResults := makeCmdResultsWithVulns(nil) |
| 51 | + |
| 52 | + cmd := &EnrichCommand{outputFormat: coreformat.Table} |
| 53 | + var buf bytes.Buffer |
| 54 | + err := cmd.printVulnerabilitiesTable(cmdResults, &buf) |
| 55 | + require.NoError(t, err) |
| 56 | + |
| 57 | + out := buf.String() |
| 58 | + assert.Contains(t, out, "COMPONENT") |
| 59 | + assert.Contains(t, out, "CVE-ID") |
| 60 | + // no data rows |
| 61 | + lines := strings.Split(strings.TrimSpace(out), "\n") |
| 62 | + assert.Len(t, lines, 1) |
| 63 | +} |
| 64 | + |
| 65 | +func TestPrintVulnerabilitiesTable_NoCves(t *testing.T) { |
| 66 | + cmdResults := makeCmdResultsWithVulns([]services.Vulnerability{ |
| 67 | + { |
| 68 | + Cves: nil, |
| 69 | + Components: map[string]services.Component{"pkg:go/golang.org/x/net@v0.0.0-20210226": {}}, |
| 70 | + }, |
| 71 | + }) |
| 72 | + |
| 73 | + cmd := &EnrichCommand{outputFormat: coreformat.Table} |
| 74 | + var buf bytes.Buffer |
| 75 | + err := cmd.printVulnerabilitiesTable(cmdResults, &buf) |
| 76 | + require.NoError(t, err) |
| 77 | + |
| 78 | + out := buf.String() |
| 79 | + assert.Contains(t, out, "pkg:go/golang.org/x/net@v0.0.0-20210226") |
| 80 | + // CVE-ID column is blank but row is present |
| 81 | + assert.True(t, strings.Count(out, "\n") >= 2) |
| 82 | +} |
0 commit comments