@@ -16,10 +16,11 @@ import (
1616)
1717
1818const (
19- githubPatPath = "testData/secrets/github-pat.txt"
20- jwtPath = "testData/secrets/jwt.txt"
21- expectedReportPath = "testData/expectedReport.json"
22- expectedReportResultsIgnoredPath = "testData/expectedReportWithIgnoredResults.json"
19+ githubPatPath = "testData/secrets/github-pat.txt"
20+ jwtPath = "testData/secrets/jwt.txt"
21+ expectedReportPath = "testData/expectedReport.json"
22+ expectedReportResultsIgnoredResultsPath = "testData/expectedReportWithIgnoredResults.json"
23+ expectedReportResultsIgnoredRulePath = "testData/expectedReportWithIgnoredRule.json"
2324)
2425
2526func TestScan (t * testing.T ) {
@@ -135,7 +136,74 @@ func TestScan(t *testing.T) {
135136 })
136137 assert .NoError (t , err , "scanner encountered an error" )
137138
138- expectedReportBytes , err := os .ReadFile (expectedReportResultsIgnoredPath )
139+ expectedReportBytes , err := os .ReadFile (expectedReportResultsIgnoredResultsPath )
140+ assert .NoError (t , err , "failed to read expected report file" )
141+
142+ var expectedReport , actualReportMap map [string ]interface {}
143+
144+ err = json .Unmarshal (expectedReportBytes , & expectedReport )
145+ assert .NoError (t , err , "failed to unmarshal expected report JSON" )
146+
147+ actualReportBytes , err := json .Marshal (actualReport )
148+ assert .NoError (t , err , "failed to marshal actual report to JSON" )
149+ err = json .Unmarshal (actualReportBytes , & actualReportMap )
150+ assert .NoError (t , err , "failed to unmarshal actual report JSON" )
151+
152+ normalizedExpectedReport , err := utils .NormalizeReportData (expectedReport )
153+ assert .NoError (t , err , "Failed to normalize actual report" )
154+
155+ normalizedActualReport , err := utils .NormalizeReportData (actualReportMap )
156+ assert .NoError (t , err , "Failed to normalize actual report" )
157+
158+ assert .EqualValues (t , normalizedExpectedReport , normalizedActualReport )
159+ })
160+ t .Run ("Successful scan with multiple items and ignored rule" , func (t * testing.T ) {
161+ cmd .Report = reporting .Init ()
162+ cmd .SecretsChan = make (chan * secrets.Secret )
163+ cmd .SecretsExtrasChan = make (chan * secrets.Secret )
164+ cmd .ValidationChan = make (chan * secrets.Secret )
165+ cmd .CvssScoreWithoutValidationChan = make (chan * secrets.Secret )
166+ cmd .Channels .Items = make (chan plugins.ISourceItem )
167+ cmd .Channels .Errors = make (chan error )
168+
169+ githubPatBytes , err := os .ReadFile (githubPatPath )
170+ assert .NoError (t , err , "failed to read github-pat file" )
171+ githubPatContent := string (githubPatBytes )
172+
173+ jwtBytes , err := os .ReadFile (jwtPath )
174+ assert .NoError (t , err , "failed to read jwt file" )
175+ jwtContent := string (jwtBytes )
176+
177+ emptyContent := ""
178+ emptyMockPath := "mockPath"
179+
180+ scanItems := []ScanItem {
181+ {
182+ Content : & githubPatContent ,
183+ ID : fmt .Sprintf ("mock-%s" , githubPatPath ),
184+ Source : githubPatPath ,
185+ },
186+ {
187+ Content : & emptyContent ,
188+ ID : fmt .Sprintf ("mock-%s" , emptyMockPath ),
189+ Source : emptyMockPath ,
190+ },
191+ {
192+ Content : & jwtContent ,
193+ ID : fmt .Sprintf ("mock-%s" , jwtPath ),
194+ Source : jwtPath ,
195+ },
196+ }
197+
198+ testScanner := NewScanner ()
199+ actualReport , err := testScanner .Scan (scanItems , ScanConfig {
200+ IgnoreRules : []string {
201+ "github-pat" ,
202+ },
203+ })
204+ assert .NoError (t , err , "scanner encountered an error" )
205+
206+ expectedReportBytes , err := os .ReadFile (expectedReportResultsIgnoredRulePath )
139207 assert .NoError (t , err , "failed to read expected report file" )
140208
141209 var expectedReport , actualReportMap map [string ]interface {}
@@ -352,7 +420,7 @@ func TestScanDynamic(t *testing.T) {
352420 })
353421 assert .NoError (t , err , "scanner encountered an error" )
354422
355- expectedReportBytes , err := os .ReadFile (expectedReportResultsIgnoredPath )
423+ expectedReportBytes , err := os .ReadFile (expectedReportResultsIgnoredResultsPath )
356424 assert .NoError (t , err , "failed to read expected report file" )
357425
358426 var expectedReport , actualReportMap map [string ]interface {}
@@ -374,7 +442,81 @@ func TestScanDynamic(t *testing.T) {
374442
375443 assert .EqualValues (t , normalizedExpectedReport , normalizedActualReport )
376444 })
445+ t .Run ("Successful ScanDynamic with Multiple Items and Ignored Rule" , func (t * testing.T ) {
446+ cmd .Report = reporting .Init ()
447+ cmd .SecretsChan = make (chan * secrets.Secret )
448+ cmd .SecretsExtrasChan = make (chan * secrets.Secret )
449+ cmd .ValidationChan = make (chan * secrets.Secret )
450+ cmd .CvssScoreWithoutValidationChan = make (chan * secrets.Secret )
451+ cmd .Channels .Items = make (chan plugins.ISourceItem )
452+ cmd .Channels .Errors = make (chan error )
453+ cmd .Channels .WaitGroup = & sync.WaitGroup {}
454+
455+ githubPatBytes , err := os .ReadFile (githubPatPath )
456+ assert .NoError (t , err , "failed to read github-pat file" )
457+ githubPatContent := string (githubPatBytes )
458+
459+ jwtBytes , err := os .ReadFile (jwtPath )
460+ assert .NoError (t , err , "failed to read jwt file" )
461+ jwtContent := string (jwtBytes )
462+
463+ emptyContent := ""
464+ emptyMockPath := "mockPath"
465+
466+ scanItems := []ScanItem {
467+ {
468+ Content : & githubPatContent ,
469+ ID : fmt .Sprintf ("mock-%s" , githubPatPath ),
470+ Source : githubPatPath ,
471+ },
472+ {
473+ Content : & emptyContent ,
474+ ID : fmt .Sprintf ("mock-%s" , emptyMockPath ),
475+ Source : emptyMockPath ,
476+ },
477+ {
478+ Content : & jwtContent ,
479+ ID : fmt .Sprintf ("mock-%s" , jwtPath ),
480+ Source : jwtPath ,
481+ },
482+ }
377483
484+ itemsIn := make (chan ScanItem , len (scanItems ))
485+ for _ , item := range scanItems {
486+ itemsIn <- item
487+ }
488+ close (itemsIn )
489+
490+ testScanner := NewScanner ()
491+ actualReport , err := testScanner .ScanDynamic (itemsIn , ScanConfig {
492+ IgnoreRules : []string {
493+ "github-pat" ,
494+ },
495+ })
496+ assert .NoError (t , err , "scanner encountered an error" )
497+
498+ expectedReportBytes , err := os .ReadFile (expectedReportResultsIgnoredRulePath )
499+ assert .NoError (t , err , "failed to read expected report file" )
500+
501+ var expectedReport , actualReportMap map [string ]interface {}
502+
503+ err = json .Unmarshal (expectedReportBytes , & expectedReport )
504+ assert .NoError (t , err , "failed to unmarshal expected report JSON" )
505+
506+ actualReportBytes , err := json .Marshal (actualReport )
507+ assert .NoError (t , err , "failed to marshal actual report to JSON" )
508+ err = json .Unmarshal (actualReportBytes , & actualReportMap )
509+ assert .NoError (t , err , "failed to unmarshal actual report JSON" )
510+
511+ // Normalize both maps.
512+ normalizedExpectedReport , err := utils .NormalizeReportData (expectedReport )
513+ assert .NoError (t , err , "Failed to normalize actual report" )
514+
515+ normalizedActualReport , err := utils .NormalizeReportData (actualReportMap )
516+ assert .NoError (t , err , "Failed to normalize actual report" )
517+
518+ assert .EqualValues (t , normalizedExpectedReport , normalizedActualReport )
519+ })
378520 t .Run ("error handling should work" , func (t * testing.T ) {
379521 cmd .Report = reporting .Init ()
380522 cmd .SecretsChan = make (chan * secrets.Secret )
0 commit comments