@@ -17,14 +17,22 @@ import (
1717 "github.com/aquasecurity/trivy/pkg/types"
1818)
1919
20+ // TestReportWriter_Sarif reuses report.PathToFileURI to compute expected URIs.
21+ // The correctness of PathToFileURI itself (including Windows path handling) is verified in TestWrite_Sarif.
2022func TestReportWriter_Sarif (t * testing.T ) {
23+ tmpScanURI := report .PathToFileURI ("/tmp/scan" )
24+
2125 tests := []struct {
22- name string
23- input types.Report
24- want * sarif.Report
26+ name string
27+ target string
28+ input types.Report
29+ want * sarif.Report
2530 }{
2631 {
2732 name : "report with vulnerabilities" ,
33+ // Container images don't have a local filesystem path, so target is empty
34+ // and OriginalUriBaseIDs is omitted from the SARIF output.
35+ target : "" ,
2836 input : types.Report {
2937 ArtifactName : "debian:9" ,
3038 ArtifactType : ftypes .TypeContainerImage ,
@@ -170,11 +178,6 @@ func TestReportWriter_Sarif(t *testing.T) {
170178 },
171179 },
172180 ColumnKind : "utf16CodeUnits" ,
173- OriginalUriBaseIDs : map [string ]* sarif.ArtifactLocation {
174- "ROOTPATH" : {
175- URI : lo .ToPtr ("file:///" ),
176- },
177- },
178181 PropertyBag : sarif.PropertyBag {
179182 Properties : map [string ]any {
180183 "imageName" : "debian:9" ,
@@ -188,7 +191,8 @@ func TestReportWriter_Sarif(t *testing.T) {
188191 },
189192 },
190193 {
191- name : "report with misconfigurations" ,
194+ name : "report with misconfigurations" ,
195+ target : "/tmp/scan" ,
192196 input : types.Report {
193197 Results : types.Results {
194198 {
@@ -329,15 +333,16 @@ func TestReportWriter_Sarif(t *testing.T) {
329333 ColumnKind : "utf16CodeUnits" ,
330334 OriginalUriBaseIDs : map [string ]* sarif.ArtifactLocation {
331335 "ROOTPATH" : {
332- URI : lo .ToPtr ("file:///" ),
336+ URI : lo .ToPtr (tmpScanURI ),
333337 },
334338 },
335339 },
336340 },
337341 },
338342 },
339343 {
340- name : "report with secrets" ,
344+ name : "report with secrets" ,
345+ target : "/tmp/scan" ,
341346 input : types.Report {
342347 Results : types.Results {
343348 {
@@ -423,15 +428,16 @@ func TestReportWriter_Sarif(t *testing.T) {
423428 ColumnKind : "utf16CodeUnits" ,
424429 OriginalUriBaseIDs : map [string ]* sarif.ArtifactLocation {
425430 "ROOTPATH" : {
426- URI : lo .ToPtr ("file:///" ),
431+ URI : lo .ToPtr (tmpScanURI ),
427432 },
428433 },
429434 },
430435 },
431436 },
432437 },
433438 {
434- name : "report with licenses" ,
439+ name : "report with licenses" ,
440+ target : "/tmp/scan" ,
435441 input : types.Report {
436442 Results : types.Results {
437443 {
@@ -512,15 +518,16 @@ func TestReportWriter_Sarif(t *testing.T) {
512518 ColumnKind : "utf16CodeUnits" ,
513519 OriginalUriBaseIDs : map [string ]* sarif.ArtifactLocation {
514520 "ROOTPATH" : {
515- URI : lo .ToPtr ("file:///" ),
521+ URI : lo .ToPtr (tmpScanURI ),
516522 },
517523 },
518524 },
519525 },
520526 },
521527 },
522528 {
523- name : "no vulns" ,
529+ name : "no vulns" ,
530+ target : "/tmp/scan" ,
524531 want : & sarif.Report {
525532 Version : "2.1.0" ,
526533 Schema : "https://raw.githubusercontent.com/oasis-tcs/sarif-spec/main/sarif-2.1/schema/sarif-schema-2.1.0.json" ,
@@ -539,7 +546,7 @@ func TestReportWriter_Sarif(t *testing.T) {
539546 ColumnKind : "utf16CodeUnits" ,
540547 OriginalUriBaseIDs : map [string ]* sarif.ArtifactLocation {
541548 "ROOTPATH" : {
542- URI : lo .ToPtr ("file:///" ),
549+ URI : lo .ToPtr (tmpScanURI ),
543550 },
544551 },
545552 },
@@ -721,11 +728,6 @@ func TestReportWriter_Sarif(t *testing.T) {
721728 },
722729 },
723730 ColumnKind : "utf16CodeUnits" ,
724- OriginalUriBaseIDs : map [string ]* sarif.ArtifactLocation {
725- "ROOTPATH" : {
726- URI : lo .ToPtr ("file:///" ),
727- },
728- },
729731 },
730732 },
731733 },
@@ -737,6 +739,7 @@ func TestReportWriter_Sarif(t *testing.T) {
737739 sarifWritten := bytes .NewBuffer (nil )
738740 w := report.SarifWriter {
739741 Output : sarifWritten ,
742+ Target : tt .target ,
740743 }
741744 err := w .Write (t .Context (), tt .input )
742745 require .NoError (t , err )
@@ -749,6 +752,25 @@ func TestReportWriter_Sarif(t *testing.T) {
749752 }
750753}
751754
755+ func TestReportWriter_toSarifErrorLevel (t * testing.T ) {
756+ tests := []struct {
757+ severity string
758+ sarifErrorLevel string
759+ }{
760+ {severity : "CRITICAL" , sarifErrorLevel : "error" },
761+ {severity : "HIGH" , sarifErrorLevel : "error" },
762+ {severity : "MEDIUM" , sarifErrorLevel : "warning" },
763+ {severity : "LOW" , sarifErrorLevel : "note" },
764+ {severity : "UNKNOWN" , sarifErrorLevel : "note" },
765+ {severity : "OTHER" , sarifErrorLevel : "none" },
766+ }
767+ for _ , tc := range tests {
768+ t .Run (tc .severity , func (t * testing.T ) {
769+ assert .Equal (t , tc .sarifErrorLevel , report .ToSarifErrorLevel (tc .severity ), tc .severity )
770+ })
771+ }
772+ }
773+
752774func TestToPathUri (t * testing.T ) {
753775 tests := []struct {
754776 input string
0 commit comments