@@ -1515,3 +1515,111 @@ third_token: ghp_abcdefghijklmnopqrstuvwxyz1234567890
15151515 })
15161516 }
15171517}
1518+
1519+ func TestMaxFindingsWarning (t * testing.T ) {
1520+ // Content with multiple secrets in single fragment
1521+ multipleSecrets := `
1522+ github_token: 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
1523+ another_token: ghp_1234567890abcdefghijklmnopqrstuvwxyz
1524+ third_token: ghp_abcdefghijklmnopqrstuvwxyz1234567890
1525+ fourth_token: ghp_9876543210zyxwvutsrqponmlkjihgfedcba
1526+ fifth_token: ghp_aB3cD4eF5gH6iJ7kL8mN9oP0qR1sT2uV3wX4
1527+ `
1528+
1529+ testCases := []struct {
1530+ name string
1531+ limit uint64
1532+ fragments []string
1533+ expectedCount int
1534+ shouldLogWarning bool
1535+ }{
1536+ {
1537+ name : "no limit - no warning" ,
1538+ limit : 0 ,
1539+ fragments : []string {multipleSecrets },
1540+ expectedCount : 5 ,
1541+ shouldLogWarning : false ,
1542+ },
1543+ {
1544+ name : "limit of 3 - warning logged when limit reached" ,
1545+ limit : 3 ,
1546+ fragments : []string {multipleSecrets },
1547+ expectedCount : 3 ,
1548+ shouldLogWarning : true ,
1549+ },
1550+ {
1551+ name : "limit of 2 across multiple fragments - warning logged" ,
1552+ limit : 2 ,
1553+ fragments : []string {
1554+ "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" ,
1555+ "ghp_1234567890abcdefghijklmnopqrstuvwxyz" ,
1556+ "ghp_abcdefghijklmnopqrstuvwxyz1234567890" ,
1557+ },
1558+ expectedCount : 2 ,
1559+ shouldLogWarning : true ,
1560+ },
1561+ {
1562+ name : "limit of 1 - warning logged immediately" ,
1563+ limit : 1 ,
1564+ fragments : []string {multipleSecrets },
1565+ expectedCount : 1 ,
1566+ shouldLogWarning : true ,
1567+ },
1568+ {
1569+ name : "limit higher than findings - no warning" ,
1570+ limit : 10 ,
1571+ fragments : []string {multipleSecrets },
1572+ expectedCount : 5 ,
1573+ shouldLogWarning : false ,
1574+ },
1575+ }
1576+
1577+ for _ , tc := range testCases {
1578+ t .Run (tc .name , func (t * testing.T ) {
1579+ // Capture log output
1580+ var logsBuffer bytes.Buffer
1581+ log .Logger = log .Output (zerolog.ConsoleWriter {
1582+ Out : & logsBuffer ,
1583+ NoColor : true ,
1584+ TimeFormat : "" ,
1585+ }).Level (zerolog .WarnLevel )
1586+
1587+ eng , err := initEngine (& EngineConfig {
1588+ DetectorWorkerPoolSize : 1 ,
1589+ MaxFindings : tc .limit ,
1590+ })
1591+ require .NoError (t , err )
1592+ defer eng .Shutdown ()
1593+
1594+ secretsChan := make (chan * secrets.Secret , 10 )
1595+ fsPlugin := & plugins.FileSystemPlugin {}
1596+
1597+ for _ , fragment := range tc .fragments {
1598+ err = eng .DetectFragment (item {content : & fragment }, secretsChan , fsPlugin .GetName ())
1599+ require .NoError (t , err )
1600+ }
1601+
1602+ close (secretsChan )
1603+
1604+ count := 0
1605+ for range secretsChan {
1606+ count ++
1607+ }
1608+
1609+ // Verify findings count
1610+ assert .Equal (t , tc .expectedCount , count )
1611+
1612+ // Verify warning message
1613+ loggedMessage := logsBuffer .String ()
1614+ if tc .shouldLogWarning {
1615+ assert .Contains (t , loggedMessage , "Maximum findings limit reached" ,
1616+ "Expected warning message to be logged when limit is reached" )
1617+ assert .Contains (t , loggedMessage , fmt .Sprintf ("max_findings=%d" , tc .limit ),
1618+ "Expected max_findings value in log message" )
1619+ } else {
1620+ assert .NotContains (t , loggedMessage , "Maximum findings limit reached" ,
1621+ "Warning message should not be logged when limit is not reached" )
1622+ }
1623+ })
1624+ }
1625+ }
0 commit comments