mirror of
https://github.com/securego/gosec.git
synced 2024-12-25 12:05:52 +00:00
Handle all errors in the formatter tests (#431)
Signed-off-by: Cosmin Cojocar <cosmin.cojocar@gmx.ch>
This commit is contained in:
parent
9cb83e10af
commit
a1969e208c
1 changed files with 36 additions and 24 deletions
|
@ -4,11 +4,12 @@ import (
|
||||||
"bytes"
|
"bytes"
|
||||||
"encoding/json"
|
"encoding/json"
|
||||||
"fmt"
|
"fmt"
|
||||||
|
"strings"
|
||||||
|
|
||||||
. "github.com/onsi/ginkgo"
|
. "github.com/onsi/ginkgo"
|
||||||
. "github.com/onsi/gomega"
|
. "github.com/onsi/gomega"
|
||||||
"github.com/securego/gosec"
|
"github.com/securego/gosec"
|
||||||
"gopkg.in/yaml.v2"
|
"gopkg.in/yaml.v2"
|
||||||
"strings"
|
|
||||||
)
|
)
|
||||||
|
|
||||||
func createIssue(ruleID string, cwe gosec.Cwe) gosec.Issue {
|
func createIssue(ruleID string, cwe gosec.Cwe) gosec.Issue {
|
||||||
|
@ -261,10 +262,11 @@ var _ = Describe("Formatter", func() {
|
||||||
error := map[string][]gosec.Error{}
|
error := map[string][]gosec.Error{}
|
||||||
|
|
||||||
buf := new(bytes.Buffer)
|
buf := new(bytes.Buffer)
|
||||||
CreateReport(buf, "csv", []string{}, []*gosec.Issue{&issue}, &gosec.Metrics{}, error)
|
err := CreateReport(buf, "csv", []string{}, []*gosec.Issue{&issue}, &gosec.Metrics{}, error)
|
||||||
|
Expect(err).ShouldNot(HaveOccurred())
|
||||||
pattern := "/home/src/project/test.go,1,test,HIGH,HIGH,testcode,CWE-%s\n"
|
pattern := "/home/src/project/test.go,1,test,HIGH,HIGH,testcode,CWE-%s\n"
|
||||||
expect := fmt.Sprintf(pattern, cwe.ID)
|
expect := fmt.Sprintf(pattern, cwe.ID)
|
||||||
Expect(string(buf.Bytes())).To(Equal(expect))
|
Expect(string(buf.String())).To(Equal(expect))
|
||||||
}
|
}
|
||||||
})
|
})
|
||||||
It("xml formatted report should contain the CWE mapping", func() {
|
It("xml formatted report should contain the CWE mapping", func() {
|
||||||
|
@ -274,10 +276,11 @@ var _ = Describe("Formatter", func() {
|
||||||
error := map[string][]gosec.Error{}
|
error := map[string][]gosec.Error{}
|
||||||
|
|
||||||
buf := new(bytes.Buffer)
|
buf := new(bytes.Buffer)
|
||||||
CreateReport(buf, "xml", []string{}, []*gosec.Issue{&issue}, &gosec.Metrics{NumFiles: 0, NumLines: 0, NumNosec: 0, NumFound: 0}, error)
|
err := CreateReport(buf, "xml", []string{}, []*gosec.Issue{&issue}, &gosec.Metrics{NumFiles: 0, NumLines: 0, NumNosec: 0, NumFound: 0}, error)
|
||||||
|
Expect(err).ShouldNot(HaveOccurred())
|
||||||
pattern := "Results:\n\n\n[/home/src/project/test.go:1] - %s (CWE-%s): test (Confidence: HIGH, Severity: HIGH)\n > testcode\n\n\nSummary:\n Files: 0\n Lines: 0\n Nosec: 0\n Issues: 0\n\n"
|
pattern := "Results:\n\n\n[/home/src/project/test.go:1] - %s (CWE-%s): test (Confidence: HIGH, Severity: HIGH)\n > testcode\n\n\nSummary:\n Files: 0\n Lines: 0\n Nosec: 0\n Issues: 0\n\n"
|
||||||
expect := fmt.Sprintf(pattern, rule, cwe.ID)
|
expect := fmt.Sprintf(pattern, rule, cwe.ID)
|
||||||
Expect(string(buf.Bytes())).To(Equal(expect))
|
Expect(string(buf.String())).To(Equal(expect))
|
||||||
}
|
}
|
||||||
})
|
})
|
||||||
It("json formatted report should contain the CWE mapping", func() {
|
It("json formatted report should contain the CWE mapping", func() {
|
||||||
|
@ -290,10 +293,11 @@ var _ = Describe("Formatter", func() {
|
||||||
|
|
||||||
expect := new(bytes.Buffer)
|
expect := new(bytes.Buffer)
|
||||||
enc := json.NewEncoder(expect)
|
enc := json.NewEncoder(expect)
|
||||||
enc.Encode(data)
|
err := enc.Encode(data)
|
||||||
|
Expect(err).ShouldNot(HaveOccurred())
|
||||||
buf := new(bytes.Buffer)
|
buf := new(bytes.Buffer)
|
||||||
CreateReport(buf, "json", []string{}, []*gosec.Issue{&issue}, &gosec.Metrics{}, error)
|
err = CreateReport(buf, "json", []string{}, []*gosec.Issue{&issue}, &gosec.Metrics{}, error)
|
||||||
|
Expect(err).ShouldNot(HaveOccurred())
|
||||||
result := stripString(buf.String())
|
result := stripString(buf.String())
|
||||||
expectation := stripString(expect.String())
|
expectation := stripString(expect.String())
|
||||||
Expect(result).To(Equal(expectation))
|
Expect(result).To(Equal(expectation))
|
||||||
|
@ -309,10 +313,11 @@ var _ = Describe("Formatter", func() {
|
||||||
|
|
||||||
expect := new(bytes.Buffer)
|
expect := new(bytes.Buffer)
|
||||||
enc := json.NewEncoder(expect)
|
enc := json.NewEncoder(expect)
|
||||||
enc.Encode(data)
|
err := enc.Encode(data)
|
||||||
|
Expect(err).ShouldNot(HaveOccurred())
|
||||||
buf := new(bytes.Buffer)
|
buf := new(bytes.Buffer)
|
||||||
CreateReport(buf, "html", []string{}, []*gosec.Issue{&issue}, &gosec.Metrics{}, error)
|
err = CreateReport(buf, "html", []string{}, []*gosec.Issue{&issue}, &gosec.Metrics{}, error)
|
||||||
|
Expect(err).ShouldNot(HaveOccurred())
|
||||||
result := stripString(buf.String())
|
result := stripString(buf.String())
|
||||||
expectation := stripString(expect.String())
|
expectation := stripString(expect.String())
|
||||||
Expect(result).To(ContainSubstring(expectation))
|
Expect(result).To(ContainSubstring(expectation))
|
||||||
|
@ -328,10 +333,11 @@ var _ = Describe("Formatter", func() {
|
||||||
|
|
||||||
expect := new(bytes.Buffer)
|
expect := new(bytes.Buffer)
|
||||||
enc := yaml.NewEncoder(expect)
|
enc := yaml.NewEncoder(expect)
|
||||||
enc.Encode(data)
|
err := enc.Encode(data)
|
||||||
|
Expect(err).ShouldNot(HaveOccurred())
|
||||||
buf := new(bytes.Buffer)
|
buf := new(bytes.Buffer)
|
||||||
CreateReport(buf, "yaml", []string{}, []*gosec.Issue{&issue}, &gosec.Metrics{}, error)
|
err = CreateReport(buf, "yaml", []string{}, []*gosec.Issue{&issue}, &gosec.Metrics{}, error)
|
||||||
|
Expect(err).ShouldNot(HaveOccurred())
|
||||||
result := stripString(buf.String())
|
result := stripString(buf.String())
|
||||||
expectation := stripString(expect.String())
|
expectation := stripString(expect.String())
|
||||||
Expect(result).To(ContainSubstring(expectation))
|
Expect(result).To(ContainSubstring(expectation))
|
||||||
|
@ -347,10 +353,11 @@ var _ = Describe("Formatter", func() {
|
||||||
|
|
||||||
expect := new(bytes.Buffer)
|
expect := new(bytes.Buffer)
|
||||||
enc := yaml.NewEncoder(expect)
|
enc := yaml.NewEncoder(expect)
|
||||||
enc.Encode(data)
|
err := enc.Encode(data)
|
||||||
|
Expect(err).ShouldNot(HaveOccurred())
|
||||||
buf := new(bytes.Buffer)
|
buf := new(bytes.Buffer)
|
||||||
CreateReport(buf, "junit-xml", []string{}, []*gosec.Issue{&issue}, &gosec.Metrics{}, error)
|
err = CreateReport(buf, "junit-xml", []string{}, []*gosec.Issue{&issue}, &gosec.Metrics{}, error)
|
||||||
|
Expect(err).ShouldNot(HaveOccurred())
|
||||||
expectation := stripString(fmt.Sprintf("[/home/src/project/test.go:1] - test (Confidence: 2, Severity: 2, CWE: %s)", cwe.ID))
|
expectation := stripString(fmt.Sprintf("[/home/src/project/test.go:1] - test (Confidence: 2, Severity: 2, CWE: %s)", cwe.ID))
|
||||||
result := stripString(buf.String())
|
result := stripString(buf.String())
|
||||||
Expect(result).To(ContainSubstring(expectation))
|
Expect(result).To(ContainSubstring(expectation))
|
||||||
|
@ -366,10 +373,11 @@ var _ = Describe("Formatter", func() {
|
||||||
|
|
||||||
expect := new(bytes.Buffer)
|
expect := new(bytes.Buffer)
|
||||||
enc := yaml.NewEncoder(expect)
|
enc := yaml.NewEncoder(expect)
|
||||||
enc.Encode(data)
|
err := enc.Encode(data)
|
||||||
|
Expect(err).ShouldNot(HaveOccurred())
|
||||||
buf := new(bytes.Buffer)
|
buf := new(bytes.Buffer)
|
||||||
CreateReport(buf, "text", []string{}, []*gosec.Issue{&issue}, &gosec.Metrics{}, error)
|
err = CreateReport(buf, "text", []string{}, []*gosec.Issue{&issue}, &gosec.Metrics{}, error)
|
||||||
|
Expect(err).ShouldNot(HaveOccurred())
|
||||||
expectation := stripString(fmt.Sprintf("[/home/src/project/test.go:1] - %s (CWE-%s): test (Confidence: HIGH, Severity: HIGH)", rule, cwe.ID))
|
expectation := stripString(fmt.Sprintf("[/home/src/project/test.go:1] - %s (CWE-%s): test (Confidence: HIGH, Severity: HIGH)", rule, cwe.ID))
|
||||||
result := stripString(buf.String())
|
result := stripString(buf.String())
|
||||||
Expect(result).To(ContainSubstring(expectation))
|
Expect(result).To(ContainSubstring(expectation))
|
||||||
|
@ -381,12 +389,15 @@ var _ = Describe("Formatter", func() {
|
||||||
issue := createIssue(rule, cwe)
|
issue := createIssue(rule, cwe)
|
||||||
error := map[string][]gosec.Error{}
|
error := map[string][]gosec.Error{}
|
||||||
buf := new(bytes.Buffer)
|
buf := new(bytes.Buffer)
|
||||||
CreateReport(buf, "sonarqube", []string{"/home/src/project"}, []*gosec.Issue{&issue}, &gosec.Metrics{}, error)
|
err := CreateReport(buf, "sonarqube", []string{"/home/src/project"}, []*gosec.Issue{&issue}, &gosec.Metrics{}, error)
|
||||||
|
Expect(err).ShouldNot(HaveOccurred())
|
||||||
|
|
||||||
result := stripString(buf.String())
|
result := stripString(buf.String())
|
||||||
|
|
||||||
expect := new(bytes.Buffer)
|
expect := new(bytes.Buffer)
|
||||||
enc := json.NewEncoder(expect)
|
enc := json.NewEncoder(expect)
|
||||||
enc.Encode(cwe)
|
err = enc.Encode(cwe)
|
||||||
|
Expect(err).ShouldNot(HaveOccurred())
|
||||||
|
|
||||||
expectation := stripString(expect.String())
|
expectation := stripString(expect.String())
|
||||||
Expect(result).To(ContainSubstring(expectation))
|
Expect(result).To(ContainSubstring(expectation))
|
||||||
|
@ -399,10 +410,11 @@ var _ = Describe("Formatter", func() {
|
||||||
error := map[string][]gosec.Error{}
|
error := map[string][]gosec.Error{}
|
||||||
|
|
||||||
buf := new(bytes.Buffer)
|
buf := new(bytes.Buffer)
|
||||||
CreateReport(buf, "golint", []string{}, []*gosec.Issue{&issue}, &gosec.Metrics{}, error)
|
err := CreateReport(buf, "golint", []string{}, []*gosec.Issue{&issue}, &gosec.Metrics{}, error)
|
||||||
|
Expect(err).ShouldNot(HaveOccurred())
|
||||||
pattern := "/home/src/project/test.go:1:1: [CWE-%s] test (Rule:%s, Severity:HIGH, Confidence:HIGH)\n"
|
pattern := "/home/src/project/test.go:1:1: [CWE-%s] test (Rule:%s, Severity:HIGH, Confidence:HIGH)\n"
|
||||||
expect := fmt.Sprintf(pattern, cwe.ID, rule)
|
expect := fmt.Sprintf(pattern, cwe.ID, rule)
|
||||||
Expect(string(buf.Bytes())).To(Equal(expect))
|
Expect(string(buf.String())).To(Equal(expect))
|
||||||
}
|
}
|
||||||
})
|
})
|
||||||
})
|
})
|
||||||
|
|
Loading…
Reference in a new issue