Skip to content

Commit 8897ed2

Browse files
added pattern test cases for all detectors starting with Alphabet a (#3539)
* added pattern test cases * some more test cases with some fixes too * improved apimetrics regex and added test cases * added more pattern test cases * added more pattern test cases * last brick of pattern test cases for a Alphabet
1 parent d073ad6 commit 8897ed2

File tree

104 files changed

+9547
-5229
lines changed

Some content is hidden

Large Commits have some content hidden by default. Use the searchbox below for content that may be hidden.

104 files changed

+9547
-5229
lines changed
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,120 @@
1+
//go:build detectors
2+
// +build detectors
3+
4+
package allsports
5+
6+
import (
7+
"context"
8+
"fmt"
9+
"testing"
10+
"time"
11+
12+
"github.com/kylelemons/godebug/pretty"
13+
14+
"github.com/trufflesecurity/trufflehog/v3/pkg/common"
15+
"github.com/trufflesecurity/trufflehog/v3/pkg/detectors"
16+
"github.com/trufflesecurity/trufflehog/v3/pkg/pb/detectorspb"
17+
)
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")
28+
29+
type args struct {
30+
ctx context.Context
31+
data []byte
32+
verify bool
33+
}
34+
tests := []struct {
35+
name string
36+
s Scanner
37+
args args
38+
want []detectors.Result
39+
wantErr bool
40+
}{
41+
{
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,
56+
},
57+
{
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,
72+
},
73+
{
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,
83+
},
84+
}
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)
91+
return
92+
}
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
98+
}
99+
if diff := pretty.Compare(got, tt.want); diff != "" {
100+
t.Errorf("Allsports.FromData() %s diff: (-got +want)\n%s", tt.name, diff)
101+
}
102+
})
103+
}
104+
}
105+
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)
116+
}
117+
}
118+
})
119+
}
120+
}
+55-89
Original file line numberDiff line numberDiff line change
@@ -1,120 +1,86 @@
1-
//go:build detectors
2-
// +build detectors
3-
41
package allsports
52

63
import (
74
"context"
85
"fmt"
96
"testing"
10-
"time"
117

12-
"github.com/kylelemons/godebug/pretty"
8+
"github.com/google/go-cmp/cmp"
9+
1310
"github.com/trufflesecurity/trufflehog/v3/pkg/detectors"
11+
"github.com/trufflesecurity/trufflehog/v3/pkg/engine/ahocorasick"
12+
)
1413

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"
1717
)
1818

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})
2822

29-
type args struct {
30-
ctx context.Context
31-
data []byte
32-
verify bool
33-
}
3423
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
4027
}{
4128
{
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},
5632
},
5733
{
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,
7237
},
7338
{
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,
8342
},
8443
}
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)
9150
return
9251
}
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
9857
}
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
10166
}
102-
})
103-
}
104-
}
10567

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{}{}
11674
}
11775
}
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+
}
11884
})
11985
}
12086
}

0 commit comments

Comments
 (0)