|
1 |
| -//go:build detectors |
2 |
| -// +build detectors |
3 |
| - |
4 | 1 | package allsports
|
5 | 2 |
|
6 | 3 | import (
|
7 | 4 | "context"
|
8 | 5 | "fmt"
|
9 | 6 | "testing"
|
10 |
| - "time" |
11 | 7 |
|
12 |
| - "github.com/kylelemons/godebug/pretty" |
| 8 | + "github.com/google/go-cmp/cmp" |
| 9 | + |
13 | 10 | "github.com/trufflesecurity/trufflehog/v3/pkg/detectors"
|
| 11 | + "github.com/trufflesecurity/trufflehog/v3/pkg/engine/ahocorasick" |
| 12 | +) |
14 | 13 |
|
15 |
| - "github.com/trufflesecurity/trufflehog/v3/pkg/common" |
16 |
| - "github.com/trufflesecurity/trufflehog/v3/pkg/pb/detectorspb" |
| 14 | +var ( |
| 15 | + validPattern = "d1f2e3c4b5a6d7e8f9g0h1i2j3k4l5m6n7o8p9q0r1s2t3u4v5w6x7y8z9a0b1ce" |
| 16 | + invalidPattern = "Ad1f2e3c4b5a6d7e8f9g0h1i2j3k4l5m6n7o8p9q0r1sRt3u4v5w6x7y8z9a0b1cE" |
17 | 17 | )
|
18 | 18 |
|
19 |
| -func TestAllsports_FromChunk(t *testing.T) { |
20 |
| - ctx, cancel := context.WithTimeout(context.Background(), time.Second*5) |
21 |
| - defer cancel() |
22 |
| - testSecrets, err := common.GetSecret(ctx, "trufflehog-testing", "detectors1") |
23 |
| - if err != nil { |
24 |
| - t.Fatalf("could not get test secrets from GCP: %s", err) |
25 |
| - } |
26 |
| - secret := testSecrets.MustGetField("ALLSPORTS") |
27 |
| - inactiveSecret := testSecrets.MustGetField("ALLSPORTS_INACTIVE") |
| 19 | +func TestAllSports_Pattern(t *testing.T) { |
| 20 | + d := Scanner{} |
| 21 | + ahoCorasickCore := ahocorasick.NewAhoCorasickCore([]detectors.Detector{d}) |
28 | 22 |
|
29 |
| - type args struct { |
30 |
| - ctx context.Context |
31 |
| - data []byte |
32 |
| - verify bool |
33 |
| - } |
34 | 23 | tests := []struct {
|
35 |
| - name string |
36 |
| - s Scanner |
37 |
| - args args |
38 |
| - want []detectors.Result |
39 |
| - wantErr bool |
| 24 | + name string |
| 25 | + input string |
| 26 | + want []string |
40 | 27 | }{
|
41 | 28 | {
|
42 |
| - name: "found, verified", |
43 |
| - s: Scanner{}, |
44 |
| - args: args{ |
45 |
| - ctx: context.Background(), |
46 |
| - data: []byte(fmt.Sprintf("You can find a allsports secret %s within", secret)), |
47 |
| - verify: true, |
48 |
| - }, |
49 |
| - want: []detectors.Result{ |
50 |
| - { |
51 |
| - DetectorType: detectorspb.DetectorType_Allsports, |
52 |
| - Verified: true, |
53 |
| - }, |
54 |
| - }, |
55 |
| - wantErr: false, |
| 29 | + name: "valid pattern", |
| 30 | + input: fmt.Sprintf("allsports: '%s'", validPattern), |
| 31 | + want: []string{validPattern}, |
56 | 32 | },
|
57 | 33 | {
|
58 |
| - name: "found, unverified", |
59 |
| - s: Scanner{}, |
60 |
| - args: args{ |
61 |
| - ctx: context.Background(), |
62 |
| - data: []byte(fmt.Sprintf("You can find a allsports secret %s within but not valid", inactiveSecret)), // the secret would satisfy the regex but not pass validation |
63 |
| - verify: true, |
64 |
| - }, |
65 |
| - want: []detectors.Result{ |
66 |
| - { |
67 |
| - DetectorType: detectorspb.DetectorType_Allsports, |
68 |
| - Verified: false, |
69 |
| - }, |
70 |
| - }, |
71 |
| - wantErr: false, |
| 34 | + name: "valid pattern - key out of prefix range", |
| 35 | + input: fmt.Sprintf("allsports keyword is not close to the real key in the data\n = '%s'", validPattern), |
| 36 | + want: nil, |
72 | 37 | },
|
73 | 38 | {
|
74 |
| - name: "not found", |
75 |
| - s: Scanner{}, |
76 |
| - args: args{ |
77 |
| - ctx: context.Background(), |
78 |
| - data: []byte("You cannot find the secret within"), |
79 |
| - verify: true, |
80 |
| - }, |
81 |
| - want: nil, |
82 |
| - wantErr: false, |
| 39 | + name: "invalid pattern", |
| 40 | + input: fmt.Sprintf("allsports: '%s'", invalidPattern), |
| 41 | + want: nil, |
83 | 42 | },
|
84 | 43 | }
|
85 |
| - for _, tt := range tests { |
86 |
| - t.Run(tt.name, func(t *testing.T) { |
87 |
| - s := Scanner{} |
88 |
| - got, err := s.FromData(tt.args.ctx, tt.args.verify, tt.args.data) |
89 |
| - if (err != nil) != tt.wantErr { |
90 |
| - t.Errorf("Allsports.FromData() error = %v, wantErr %v", err, tt.wantErr) |
| 44 | + |
| 45 | + for _, test := range tests { |
| 46 | + t.Run(test.name, func(t *testing.T) { |
| 47 | + matchedDetectors := ahoCorasickCore.FindDetectorMatches([]byte(test.input)) |
| 48 | + if len(matchedDetectors) == 0 { |
| 49 | + t.Errorf("keywords '%v' not matched by: %s", d.Keywords(), test.input) |
91 | 50 | return
|
92 | 51 | }
|
93 |
| - for i := range got { |
94 |
| - if len(got[i].Raw) == 0 { |
95 |
| - t.Fatalf("no raw secret present: \n %+v", got[i]) |
96 |
| - } |
97 |
| - got[i].Raw = nil |
| 52 | + |
| 53 | + results, err := d.FromData(context.Background(), false, []byte(test.input)) |
| 54 | + if err != nil { |
| 55 | + t.Errorf("error = %v", err) |
| 56 | + return |
98 | 57 | }
|
99 |
| - if diff := pretty.Compare(got, tt.want); diff != "" { |
100 |
| - t.Errorf("Allsports.FromData() %s diff: (-got +want)\n%s", tt.name, diff) |
| 58 | + |
| 59 | + if len(results) != len(test.want) { |
| 60 | + if len(results) == 0 { |
| 61 | + t.Errorf("did not receive result") |
| 62 | + } else { |
| 63 | + t.Errorf("expected %d results, only received %d", len(test.want), len(results)) |
| 64 | + } |
| 65 | + return |
101 | 66 | }
|
102 |
| - }) |
103 |
| - } |
104 |
| -} |
105 | 67 |
|
106 |
| -func BenchmarkFromData(benchmark *testing.B) { |
107 |
| - ctx := context.Background() |
108 |
| - s := Scanner{} |
109 |
| - for name, data := range detectors.MustGetBenchmarkData() { |
110 |
| - benchmark.Run(name, func(b *testing.B) { |
111 |
| - b.ResetTimer() |
112 |
| - for n := 0; n < b.N; n++ { |
113 |
| - _, err := s.FromData(ctx, false, data) |
114 |
| - if err != nil { |
115 |
| - b.Fatal(err) |
| 68 | + actual := make(map[string]struct{}, len(results)) |
| 69 | + for _, r := range results { |
| 70 | + if len(r.RawV2) > 0 { |
| 71 | + actual[string(r.RawV2)] = struct{}{} |
| 72 | + } else { |
| 73 | + actual[string(r.Raw)] = struct{}{} |
116 | 74 | }
|
117 | 75 | }
|
| 76 | + expected := make(map[string]struct{}, len(test.want)) |
| 77 | + for _, v := range test.want { |
| 78 | + expected[v] = struct{}{} |
| 79 | + } |
| 80 | + |
| 81 | + if diff := cmp.Diff(expected, actual); diff != "" { |
| 82 | + t.Errorf("%s diff: (-want +got)\n%s", test.name, diff) |
| 83 | + } |
118 | 84 | })
|
119 | 85 | }
|
120 | 86 | }
|
0 commit comments