Skip to content

Commit 9095845

Browse files
added pattern test cases for detectors starting with b (#3559)
* added pattern test cases for detectors starting with b * last brick
1 parent 8897ed2 commit 9095845

File tree

96 files changed

+7046
-3183
lines changed

Some content is hidden

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

96 files changed

+7046
-3183
lines changed

pkg/detectors/bannerbear/bannerbear.go

+2-1
Original file line numberDiff line numberDiff line change
@@ -3,10 +3,11 @@ package bannerbear
33
import (
44
"context"
55
"fmt"
6-
regexp "github.com/wasilibs/go-re2"
76
"net/http"
87
"strings"
98

9+
regexp "github.com/wasilibs/go-re2"
10+
1011
"github.com/trufflesecurity/trufflehog/v3/pkg/common"
1112
"github.com/trufflesecurity/trufflehog/v3/pkg/detectors"
1213
"github.com/trufflesecurity/trufflehog/v3/pkg/pb/detectorspb"
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,120 @@
1+
//go:build detectors
2+
// +build detectors
3+
4+
package bannerbear
5+
6+
import (
7+
"context"
8+
"fmt"
9+
"testing"
10+
"time"
11+
12+
"github.com/kylelemons/godebug/pretty"
13+
"github.com/trufflesecurity/trufflehog/v3/pkg/detectors"
14+
15+
"github.com/trufflesecurity/trufflehog/v3/pkg/common"
16+
"github.com/trufflesecurity/trufflehog/v3/pkg/pb/detectorspb"
17+
)
18+
19+
func TestBannerbear_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("BANNERBEAR")
27+
inactiveSecret := testSecrets.MustGetField("BANNERBEAR_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 bannerbear secret %s within", secret)),
47+
verify: true,
48+
},
49+
want: []detectors.Result{
50+
{
51+
DetectorType: detectorspb.DetectorType_Bannerbear,
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 bannerbear 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_Bannerbear,
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("Bannerbear.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("Bannerbear.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+
}
+82-89
Original file line numberDiff line numberDiff line change
@@ -1,120 +1,113 @@
1-
//go:build detectors
2-
// +build detectors
3-
41
package bannerbear
52

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

12-
"github.com/kylelemons/godebug/pretty"
13-
"github.com/trufflesecurity/trufflehog/v3/pkg/detectors"
8+
"github.com/google/go-cmp/cmp"
149

15-
"github.com/trufflesecurity/trufflehog/v3/pkg/common"
16-
"github.com/trufflesecurity/trufflehog/v3/pkg/pb/detectorspb"
10+
"github.com/trufflesecurity/trufflehog/v3/pkg/detectors"
11+
"github.com/trufflesecurity/trufflehog/v3/pkg/engine/ahocorasick"
1712
)
1813

19-
func TestBannerbear_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("BANNERBEAR")
27-
inactiveSecret := testSecrets.MustGetField("BANNERBEAR_INACTIVE")
14+
var (
15+
validPattern = "yvxpthLIcYpZweFpPOVeCOtt"
16+
complexPattern = `
17+
func main() {
18+
url := "https://api.example.com/v1/resource"
19+
20+
// Create a new request with the secret as a header
21+
req, err := http.NewRequest("POST", url, bytes.NewBuffer([]byte("{}")))
22+
if err != nil {
23+
fmt.Println("Error creating request:", err)
24+
return
25+
}
26+
27+
bannerBearToken := "Bearer yvxpthLIcYpZweFpPOVeCOtt"
28+
req.Header.Set("Authorization", bannerBearToken)
29+
30+
// Perform the request
31+
client := &http.Client{}
32+
resp, _ := client.Do(req)
33+
defer resp.Body.Close()
2834
29-
type args struct {
30-
ctx context.Context
31-
data []byte
32-
verify bool
35+
// Check response status
36+
if resp.StatusCode == http.StatusOK {
37+
fmt.Println("Request successful!")
38+
} else {
39+
fmt.Println("Request failed with status:", resp.Status)
40+
}
3341
}
42+
`
43+
invalidPattern = "yvxpthLIcYpZweFpPOVeCOtot"
44+
)
45+
46+
func TestBannerBear_Pattern(t *testing.T) {
47+
d := Scanner{}
48+
ahoCorasickCore := ahocorasick.NewAhoCorasickCore([]detectors.Detector{d})
49+
3450
tests := []struct {
35-
name string
36-
s Scanner
37-
args args
38-
want []detectors.Result
39-
wantErr bool
51+
name string
52+
input string
53+
want []string
4054
}{
4155
{
42-
name: "found, verified",
43-
s: Scanner{},
44-
args: args{
45-
ctx: context.Background(),
46-
data: []byte(fmt.Sprintf("You can find a bannerbear secret %s within", secret)),
47-
verify: true,
48-
},
49-
want: []detectors.Result{
50-
{
51-
DetectorType: detectorspb.DetectorType_Bannerbear,
52-
Verified: true,
53-
},
54-
},
55-
wantErr: false,
56+
name: "valid pattern",
57+
input: fmt.Sprintf("bannerbear credentials: %s", validPattern),
58+
want: []string{"yvxpthLIcYpZweFpPOVeCOtt"},
5659
},
5760
{
58-
name: "found, unverified",
59-
s: Scanner{},
60-
args: args{
61-
ctx: context.Background(),
62-
data: []byte(fmt.Sprintf("You can find a bannerbear 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_Bannerbear,
68-
Verified: false,
69-
},
70-
},
71-
wantErr: false,
61+
name: "valid pattern - complex",
62+
input: complexPattern,
63+
want: []string{"yvxpthLIcYpZweFpPOVeCOtt"},
7264
},
7365
{
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,
66+
name: "invalid pattern",
67+
input: fmt.Sprintf("bannerbear credentials: %s", invalidPattern),
68+
want: nil,
8369
},
8470
}
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("Bannerbear.FromData() error = %v, wantErr %v", err, tt.wantErr)
71+
72+
for _, test := range tests {
73+
t.Run(test.name, func(t *testing.T) {
74+
matchedDetectors := ahoCorasickCore.FindDetectorMatches([]byte(test.input))
75+
if len(matchedDetectors) == 0 {
76+
t.Errorf("keywords '%v' not matched by: %s", d.Keywords(), test.input)
9177
return
9278
}
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
79+
80+
results, err := d.FromData(context.Background(), false, []byte(test.input))
81+
if err != nil {
82+
t.Errorf("error = %v", err)
83+
return
9884
}
99-
if diff := pretty.Compare(got, tt.want); diff != "" {
100-
t.Errorf("Bannerbear.FromData() %s diff: (-got +want)\n%s", tt.name, diff)
85+
86+
if len(results) != len(test.want) {
87+
if len(results) == 0 {
88+
t.Errorf("did not receive result")
89+
} else {
90+
t.Errorf("expected %d results, only received %d", len(test.want), len(results))
91+
}
92+
return
10193
}
102-
})
103-
}
104-
}
10594

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)
95+
actual := make(map[string]struct{}, len(results))
96+
for _, r := range results {
97+
if len(r.RawV2) > 0 {
98+
actual[string(r.RawV2)] = struct{}{}
99+
} else {
100+
actual[string(r.Raw)] = struct{}{}
116101
}
117102
}
103+
expected := make(map[string]struct{}, len(test.want))
104+
for _, v := range test.want {
105+
expected[v] = struct{}{}
106+
}
107+
108+
if diff := cmp.Diff(expected, actual); diff != "" {
109+
t.Errorf("%s diff: (-want +got)\n%s", test.name, diff)
110+
}
118111
})
119112
}
120113
}

pkg/detectors/baremetrics/baremetrics.go

+2-1
Original file line numberDiff line numberDiff line change
@@ -3,10 +3,11 @@ package baremetrics
33
import (
44
"context"
55
"fmt"
6-
regexp "github.com/wasilibs/go-re2"
76
"net/http"
87
"strings"
98

9+
regexp "github.com/wasilibs/go-re2"
10+
1011
"github.com/trufflesecurity/trufflehog/v3/pkg/common"
1112
"github.com/trufflesecurity/trufflehog/v3/pkg/detectors"
1213
"github.com/trufflesecurity/trufflehog/v3/pkg/pb/detectorspb"

0 commit comments

Comments
 (0)