|
1 | 1 | package detectors |
2 | 2 |
|
3 | 3 | import ( |
4 | | - "context" |
5 | 4 | _ "embed" |
6 | 5 | "testing" |
7 | 6 |
|
8 | 7 | "github.com/stretchr/testify/assert" |
9 | | - |
10 | | - logContext "github.com/trufflesecurity/trufflehog/v3/pkg/context" |
11 | | - "github.com/trufflesecurity/trufflehog/v3/pkg/pb/detectorspb" |
12 | 8 | ) |
13 | 9 |
|
14 | | -type fakeDetector struct{} |
15 | | -type customFalsePositiveChecker struct{ fakeDetector } |
16 | | - |
17 | | -func (d fakeDetector) FromData(ctx context.Context, verify bool, data []byte) ([]Result, error) { |
18 | | - return nil, nil |
19 | | -} |
20 | | - |
21 | | -func (d fakeDetector) Keywords() []string { |
22 | | - return nil |
23 | | -} |
24 | | - |
25 | | -func (d fakeDetector) Type() detectorspb.DetectorType { |
26 | | - return detectorspb.DetectorType(0) |
27 | | -} |
28 | | - |
29 | | -func (f fakeDetector) Description() string { return "" } |
30 | | - |
31 | | -func (d customFalsePositiveChecker) IsFalsePositive(result Result) (bool, string) { |
32 | | - return IsKnownFalsePositive(string(result.Raw), map[FalsePositive]struct{}{"a specific magic string": {}}, false) |
33 | | -} |
34 | | - |
35 | | -func TestFilterKnownFalsePositives_DefaultLogic(t *testing.T) { |
36 | | - results := []Result{ |
37 | | - {Raw: []byte("00000")}, // "default" false positive list |
38 | | - {Raw: []byte("number")}, // from wordlist |
39 | | - // from uuid list |
40 | | - {Raw: []byte("00000000-0000-0000-0000-000000000000")}, |
41 | | - {Raw: []byte("xxxxxxxx-xxxx-xxxx-xxxx-xxxxxxxxxxxx")}, |
42 | | - // real secrets |
43 | | - {Raw: []byte("hga8adshla3434g")}, |
44 | | - {Raw: []byte("f795f7db-2dfe-4095-96f3-8f8370c735f9")}, |
45 | | - } |
46 | | - expected := []Result{ |
47 | | - {Raw: []byte("hga8adshla3434g")}, |
48 | | - {Raw: []byte("f795f7db-2dfe-4095-96f3-8f8370c735f9")}, |
49 | | - } |
50 | | - filtered := FilterKnownFalsePositives(logContext.Background(), fakeDetector{}, results) |
51 | | - assert.ElementsMatch(t, expected, filtered) |
52 | | -} |
53 | | - |
54 | | -func TestFilterKnownFalsePositives_CustomLogic(t *testing.T) { |
55 | | - results := []Result{ |
56 | | - {Raw: []byte("a specific magic string")}, // specific target |
57 | | - {Raw: []byte("00000")}, // "default" false positive list |
58 | | - {Raw: []byte("number")}, // from wordlist |
59 | | - {Raw: []byte("hga8adshla3434g")}, // real secret |
60 | | - } |
61 | | - expected := []Result{ |
62 | | - {Raw: []byte("00000")}, |
63 | | - {Raw: []byte("number")}, |
64 | | - {Raw: []byte("hga8adshla3434g")}, |
65 | | - } |
66 | | - filtered := FilterKnownFalsePositives(logContext.Background(), customFalsePositiveChecker{}, results) |
67 | | - assert.ElementsMatch(t, expected, filtered) |
68 | | -} |
69 | | - |
70 | 10 | func TestIsFalsePositive(t *testing.T) { |
71 | 11 | type args struct { |
72 | 12 | match string |
|
0 commit comments