@@ -56,5 +56,59 @@ var _ = Describe("Sarif Formatter", func() {
56
56
hasSuppressions , _ := regexp .MatchString (`"suppressions": \[(\s*){` , result )
57
57
Expect (hasSuppressions ).To (BeTrue ())
58
58
})
59
+ It ("sarif formatted report should contain the formatted one line code snippet" , func () {
60
+ ruleID := "G101"
61
+ cwe := gosec .GetCweByRule (ruleID )
62
+ code := "68: \t \t }\n 69: \t \t var data = template.HTML(v.TmplFile)\n 70: \t \t isTmpl := true\n "
63
+ expectedCode := "var data = template.HTML(v.TmplFile)"
64
+ issue := gosec.Issue {
65
+ File : "/home/src/project/test.go" ,
66
+ Line : "69" ,
67
+ Col : "14" ,
68
+ RuleID : ruleID ,
69
+ What : "test" ,
70
+ Confidence : gosec .High ,
71
+ Severity : gosec .High ,
72
+ Code : code ,
73
+ Cwe : cwe ,
74
+ Suppressions : []gosec.SuppressionInfo {
75
+ {
76
+ Kind : "kind" ,
77
+ Justification : "justification" ,
78
+ },
79
+ },
80
+ }
81
+ reportInfo := gosec .NewReportInfo ([]* gosec.Issue {& issue }, & gosec.Metrics {}, map [string ][]gosec.Error {}).WithVersion ("v2.7.0" )
82
+ sarifReport , err := sarif .GenerateReport ([]string {}, reportInfo )
83
+ Expect (err ).ShouldNot (HaveOccurred ())
84
+ Expect (sarifReport .Runs [0 ].Results [0 ].Locations [0 ].PhysicalLocation .Region .Snippet .Text ).Should (Equal (expectedCode ))
85
+ })
86
+ It ("sarif formatted report should contain the formatted multiple line code snippet" , func () {
87
+ ruleID := "G101"
88
+ cwe := gosec .GetCweByRule (ruleID )
89
+ code := "68: }\n 69: var data = template.HTML(v.TmplFile)\n 70: isTmpl := true\n "
90
+ expectedCode := "var data = template.HTML(v.TmplFile)\n isTmpl := true\n "
91
+ issue := gosec.Issue {
92
+ File : "/home/src/project/test.go" ,
93
+ Line : "69-70" ,
94
+ Col : "14" ,
95
+ RuleID : ruleID ,
96
+ What : "test" ,
97
+ Confidence : gosec .High ,
98
+ Severity : gosec .High ,
99
+ Code : code ,
100
+ Cwe : cwe ,
101
+ Suppressions : []gosec.SuppressionInfo {
102
+ {
103
+ Kind : "kind" ,
104
+ Justification : "justification" ,
105
+ },
106
+ },
107
+ }
108
+ reportInfo := gosec .NewReportInfo ([]* gosec.Issue {& issue }, & gosec.Metrics {}, map [string ][]gosec.Error {}).WithVersion ("v2.7.0" )
109
+ sarifReport , err := sarif .GenerateReport ([]string {}, reportInfo )
110
+ Expect (err ).ShouldNot (HaveOccurred ())
111
+ Expect (sarifReport .Runs [0 ].Results [0 ].Locations [0 ].PhysicalLocation .Region .Snippet .Text ).Should (Equal (expectedCode ))
112
+ })
59
113
})
60
114
})
0 commit comments