Add some false positive tests #4703

There are automated tests that validate the behavior of IsKnownFalsePositive and FilterKnownFalsePositives, but none that validate GetFalsePositiveCheck. I hope to soon delete FilterKnownFalsePositives, which would leave this area of the code under-tested. This commit adds some tests for GetFalsePositiveCheck so that we can eventually remove FilterKnownFalsePositives without losing overall test coverage.
This commit is contained in:
Cody Rose
2026-01-28 15:09:04 -05:00
committed by GitHub
parent 5032470af1
commit 7c7facf032
+44
View File
@@ -32,6 +32,50 @@ func (d customFalsePositiveChecker) IsFalsePositive(result Result) (bool, string
return IsKnownFalsePositive(string(result.Raw), map[FalsePositive]struct{}{"a specific magic string": {}}, false)
}
// This test validates that GetFalsePositiveCheck, when invoked on a detector that does not implement
// CustomFalsePositiveChecker, returns a predicate that behaves as expected.
func TestGetFalsePositiveCheck_DefaultLogic(t *testing.T) {
testCases := []struct {
raw string
isFalsePositive bool
}{
{"00000", true}, // "default" false positive list
{"number", true}, // from wordlist
{"00000000-0000-0000-0000-000000000000", true}, // from uuid list
{"xxxxxxxx-xxxx-xxxx-xxxx-xxxxxxxxxxxx", true}, // from uuid list
{"hga8adshla3434g", false},
{"f795f7db-2dfe-4095-96f3-8f8370c735f9", false},
}
for _, tt := range testCases {
isFalsePositive, _ := GetFalsePositiveCheck(fakeDetector{})(Result{Raw: []byte(tt.raw)})
assert.Equal(t, tt.isFalsePositive, isFalsePositive, "secret %q had unexpected false positive status", tt.raw)
}
}
// This test validates that GetFalsePositiveCheck, when invoked on a detector that implements
// CustomFalsePositiveChecker, returns a predicate that behaves as expected. (Specifically, the predicate should not
// flag secrets that are present in the standard false positive lists.)
func TestGetFalsePositiveCheck_CustomLogic(t *testing.T) {
testCases := []struct {
raw string
isFalsePositive bool
}{
{"a specific magic string", true}, // the specific value the custom checker is looking for
{"00000", false},
{"number", false},
{"00000000-0000-0000-0000-000000000000", false},
{"xxxxxxxx-xxxx-xxxx-xxxx-xxxxxxxxxxxx", false},
{"hga8adshla3434g", false},
{"f795f7db-2dfe-4095-96f3-8f8370c735f9", false},
}
for _, tt := range testCases {
isFalsePositive, _ := GetFalsePositiveCheck(customFalsePositiveChecker{})(Result{Raw: []byte(tt.raw)})
assert.Equal(t, tt.isFalsePositive, isFalsePositive, "secret %q had unexpected false positive status", tt.raw)
}
}
func TestFilterKnownFalsePositives_DefaultLogic(t *testing.T) {
results := []Result{
{Raw: []byte("00000")}, // "default" false positive list