1
0
mirror of https://github.com/SAP/jenkins-library.git synced 2024-12-12 10:55:20 +02:00
sap-jenkins-library/pkg/protecode/analysis_test.go
Jk1484 ffc931aad1
feat(golangBuild): use 'unit' build tag to include tests during test execution (#4345)
* Added unit tag as argument. Added description to runTests command. Changed code generator to have unit build tag in generated unit test files.

* Added unit build tag to all unit test files.

* added to new unit test unit build tag

* Update verify-go.yml

* small fix

---------

Co-authored-by: Muhammadali Nazarov <Muhammadali.Nazarov@acronis.com>
Co-authored-by: Christopher Fenner <26137398+CCFenner@users.noreply.github.com>
2023-05-03 21:02:11 +05:00

131 lines
4.0 KiB
Go

//go:build unit
// +build unit
package protecode
import (
"testing"
"github.com/stretchr/testify/assert"
)
func TestIsSevere(t *testing.T) {
t.Run("with severe cvss v3 vulnerability", func(t *testing.T) {
// init
vulnerability := Vulnerability{
Exact: true,
Triage: []Triage{},
Vuln: Vuln{
Cve: "Cve2",
Cvss: "8.0",
Cvss3Score: "7.3",
},
}
// test && assert
assert.True(t, isSevere(vulnerability))
})
t.Run("with severe cvss v2 vulnerability", func(t *testing.T) {
// init
vulnerability := Vulnerability{
Exact: true,
Triage: []Triage{},
Vuln: Vuln{
Cve: "Cve2",
Cvss: "8.0",
Cvss3Score: "0.0",
},
}
// test && assert
assert.True(t, isSevere(vulnerability))
})
t.Run("with non-severe cvss v3 vulnerability", func(t *testing.T) {
// init
vulnerability := Vulnerability{
Exact: true,
Triage: []Triage{},
Vuln: Vuln{
Cve: "Cve2",
Cvss: "4.0",
Cvss3Score: "4.0",
},
}
// test && assert
assert.False(t, isSevere(vulnerability))
})
t.Run("with non-severe cvss v2 vulnerability", func(t *testing.T) {
// init
vulnerability := Vulnerability{
Exact: true,
Triage: []Triage{},
Vuln: Vuln{
Cve: "Cve2",
Cvss: "4.0",
Cvss3Score: "0.0",
},
}
// test && assert
assert.False(t, isSevere(vulnerability))
})
t.Run("with non-severe vulnerability with missing cvss v3 rating", func(t *testing.T) {
// init
vulnerability := Vulnerability{
Exact: true,
Triage: []Triage{},
Vuln: Vuln{
Cve: "Cve2",
Cvss: "4.0",
Cvss3Score: "",
},
}
// test && assert
assert.False(t, isSevere(vulnerability))
})
}
func TestHasSevereVulnerabilities(t *testing.T) {
severeV3 := Vulnerability{Exact: true, Triage: []Triage{}, Vuln: Vuln{Cve: "Cve1", Cvss: "4.0", Cvss3Score: "8.0"}}
severeV2 := Vulnerability{Exact: true, Triage: []Triage{}, Vuln: Vuln{Cve: "Cve2", Cvss: "8.0", Cvss3Score: "0.0"}}
nonSevere1 := Vulnerability{Exact: true, Triage: []Triage{}, Vuln: Vuln{Cve: "Cve3", Cvss: "4.0", Cvss3Score: "4.0"}}
nonSevere2 := Vulnerability{Exact: true, Triage: []Triage{}, Vuln: Vuln{Cve: "Cve4", Cvss: "4.0", Cvss3Score: "4.0"}}
excluded := Vulnerability{Exact: true, Triage: []Triage{}, Vuln: Vuln{Cve: "Cve5", Cvss: "8.0", Cvss3Score: "8.0"}}
triaged := Vulnerability{Exact: true, Triage: []Triage{{ID: 1}}, Vuln: Vuln{Cve: "Cve6", Cvss: "8.0", Cvss3Score: "8.0"}}
historic := Vulnerability{Exact: false, Triage: []Triage{}, Vuln: Vuln{Cve: "Cve7", Cvss: "8.0", Cvss3Score: "8.0"}}
t.Run("with severe v3 vulnerabilities", func(t *testing.T) {
// init
data := Result{Components: []Component{{Vulns: []Vulnerability{nonSevere1, severeV3}}}}
// test && assert
assert.True(t, HasSevereVulnerabilities(data, ""))
})
t.Run("with severe v2 vulnerabilities", func(t *testing.T) {
// init
data := Result{Components: []Component{{Vulns: []Vulnerability{nonSevere1, severeV2}}}}
// test && assert
assert.True(t, HasSevereVulnerabilities(data, ""))
})
t.Run("without severe vulnerabilities", func(t *testing.T) {
// init
data := Result{Components: []Component{{Vulns: []Vulnerability{nonSevere1, nonSevere2}}}}
// test && assert
assert.False(t, HasSevereVulnerabilities(data, ""))
})
t.Run("with historic vulnerabilities", func(t *testing.T) {
// init
data := Result{Components: []Component{{Vulns: []Vulnerability{nonSevere1, triaged}}}}
// test && assert
assert.False(t, HasSevereVulnerabilities(data, ""))
})
t.Run("with excluded vulnerabilities", func(t *testing.T) {
// init
data := Result{Components: []Component{{Vulns: []Vulnerability{nonSevere1, excluded}}}}
// test && assert
assert.False(t, HasSevereVulnerabilities(data, "Cve5,Cve14"))
})
t.Run("with historic vulnerabilities", func(t *testing.T) {
// init
data := Result{Components: []Component{{Vulns: []Vulnerability{nonSevere1, historic}}}}
// test && assert
assert.False(t, HasSevereVulnerabilities(data, ""))
})
}