Skip to content

Commit 2aa1a1a

Browse files
authored
updated and added pattern tests for detectors t-v (#3753)
1 parent 415701e commit 2aa1a1a

File tree

160 files changed

+14577
-7464
lines changed

Some content is hidden

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

160 files changed

+14577
-7464
lines changed
Lines changed: 120 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,120 @@
1+
//go:build detectors
2+
// +build detectors
3+
4+
package tailscale
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/detectors"
15+
16+
"github.com/trufflesecurity/trufflehog/v3/pkg/common"
17+
"github.com/trufflesecurity/trufflehog/v3/pkg/pb/detectorspb"
18+
)
19+
20+
func TestTailscaleapi_FromChunk(t *testing.T) {
21+
ctx, cancel := context.WithTimeout(context.Background(), time.Second*5)
22+
defer cancel()
23+
testSecrets, err := common.GetSecret(ctx, "trufflehog-testing", "detectors4")
24+
if err != nil {
25+
t.Fatalf("could not get test secrets from GCP: %s", err)
26+
}
27+
secret := testSecrets.MustGetField("TAILSCALEAPI")
28+
inactiveSecret := testSecrets.MustGetField("TAILSCALEAPI_INACTIVE")
29+
30+
type args struct {
31+
ctx context.Context
32+
data []byte
33+
verify bool
34+
}
35+
tests := []struct {
36+
name string
37+
s Scanner
38+
args args
39+
want []detectors.Result
40+
wantErr bool
41+
}{
42+
{
43+
name: "found, verified",
44+
s: Scanner{},
45+
args: args{
46+
ctx: context.Background(),
47+
data: []byte(fmt.Sprintf("You can find a tailscaleapi secret %s within", secret)),
48+
verify: true,
49+
},
50+
want: []detectors.Result{
51+
{
52+
DetectorType: detectorspb.DetectorType_Tailscale,
53+
Verified: true,
54+
},
55+
},
56+
wantErr: false,
57+
},
58+
{
59+
name: "found, unverified",
60+
s: Scanner{},
61+
args: args{
62+
ctx: context.Background(),
63+
data: []byte(fmt.Sprintf("You can find a tailscaleapi secret %s within but not valid", inactiveSecret)), // the secret would satisfy the regex but not pass validation
64+
verify: true,
65+
},
66+
want: []detectors.Result{
67+
{
68+
DetectorType: detectorspb.DetectorType_Tailscale,
69+
Verified: false,
70+
},
71+
},
72+
wantErr: false,
73+
},
74+
{
75+
name: "not found",
76+
s: Scanner{},
77+
args: args{
78+
ctx: context.Background(),
79+
data: []byte("You cannot find the secret within"),
80+
verify: true,
81+
},
82+
want: nil,
83+
wantErr: false,
84+
},
85+
}
86+
for _, tt := range tests {
87+
t.Run(tt.name, func(t *testing.T) {
88+
s := Scanner{}
89+
got, err := s.FromData(tt.args.ctx, tt.args.verify, tt.args.data)
90+
if (err != nil) != tt.wantErr {
91+
t.Errorf("Tailscaleapi.FromData() error = %v, wantErr %v", err, tt.wantErr)
92+
return
93+
}
94+
for i := range got {
95+
if len(got[i].Raw) == 0 {
96+
t.Fatalf("no raw secret present: \n %+v", got[i])
97+
}
98+
got[i].Raw = nil
99+
}
100+
if diff := pretty.Compare(got, tt.want); diff != "" {
101+
t.Errorf("Tailscaleapi.FromData() %s diff: (-got +want)\n%s", tt.name, diff)
102+
}
103+
})
104+
}
105+
}
106+
107+
func BenchmarkFromData(benchmark *testing.B) {
108+
ctx := context.Background()
109+
s := Scanner{}
110+
for name, data := range detectors.MustGetBenchmarkData() {
111+
benchmark.Run(name, func(b *testing.B) {
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+
}
Lines changed: 52 additions & 91 deletions
Original file line numberDiff line numberDiff line change
@@ -1,120 +1,81 @@
1-
//go:build detectors
2-
// +build detectors
3-
41
package tailscale
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"
139

1410
"github.com/trufflesecurity/trufflehog/v3/pkg/detectors"
15-
16-
"github.com/trufflesecurity/trufflehog/v3/pkg/common"
17-
"github.com/trufflesecurity/trufflehog/v3/pkg/pb/detectorspb"
11+
"github.com/trufflesecurity/trufflehog/v3/pkg/engine/ahocorasick"
1812
)
1913

20-
func TestTailscaleapi_FromChunk(t *testing.T) {
21-
ctx, cancel := context.WithTimeout(context.Background(), time.Second*5)
22-
defer cancel()
23-
testSecrets, err := common.GetSecret(ctx, "trufflehog-testing", "detectors4")
24-
if err != nil {
25-
t.Fatalf("could not get test secrets from GCP: %s", err)
26-
}
27-
secret := testSecrets.MustGetField("TAILSCALEAPI")
28-
inactiveSecret := testSecrets.MustGetField("TAILSCALEAPI_INACTIVE")
14+
var (
15+
validPattern = "tskey-rtzgayeq-RLL0xBAIXBhkYRhir0gmxHNoARkj0CqpNTj_xM2Zpm4lDxEIGsYbwO_kzlNSwrQrAOL4yacZGlBfj37e3WRRlmYfKtrgC-xmk0NNQFLQGCTPwcwQT7d6YipCXS1ScCVL8So"
16+
invalidPattern = "tskey-rtzgayeq-RLL0xBAIXBhkYRhir0gmxH?oARkj0CqpNTj_xM2Zpm4lDxEIGsYbwO_kzlNSwrQrAOL4yacZGlBfj37e3WRRlmYfKtrgC-xmk0NNQFLQGCTPwcwQT7d6YipCXS1ScCVL8So"
17+
keyword = "tailscale"
18+
)
2919

30-
type args struct {
31-
ctx context.Context
32-
data []byte
33-
verify bool
34-
}
20+
func TestTailscaleapi_Pattern(t *testing.T) {
21+
d := Scanner{}
22+
ahoCorasickCore := ahocorasick.NewAhoCorasickCore([]detectors.Detector{d})
3523
tests := []struct {
36-
name string
37-
s Scanner
38-
args args
39-
want []detectors.Result
40-
wantErr bool
24+
name string
25+
input string
26+
want []string
4127
}{
4228
{
43-
name: "found, verified",
44-
s: Scanner{},
45-
args: args{
46-
ctx: context.Background(),
47-
data: []byte(fmt.Sprintf("You can find a tailscaleapi secret %s within", secret)),
48-
verify: true,
49-
},
50-
want: []detectors.Result{
51-
{
52-
DetectorType: detectorspb.DetectorType_Tailscale,
53-
Verified: true,
54-
},
55-
},
56-
wantErr: false,
29+
name: "valid pattern - with keyword tailscale",
30+
input: fmt.Sprintf("%s token = '%s'", keyword, validPattern),
31+
want: []string{validPattern},
5732
},
5833
{
59-
name: "found, unverified",
60-
s: Scanner{},
61-
args: args{
62-
ctx: context.Background(),
63-
data: []byte(fmt.Sprintf("You can find a tailscaleapi secret %s within but not valid", inactiveSecret)), // the secret would satisfy the regex but not pass validation
64-
verify: true,
65-
},
66-
want: []detectors.Result{
67-
{
68-
DetectorType: detectorspb.DetectorType_Tailscale,
69-
Verified: false,
70-
},
71-
},
72-
wantErr: false,
73-
},
74-
{
75-
name: "not found",
76-
s: Scanner{},
77-
args: args{
78-
ctx: context.Background(),
79-
data: []byte("You cannot find the secret within"),
80-
verify: true,
81-
},
82-
want: nil,
83-
wantErr: false,
34+
name: "invalid pattern",
35+
input: fmt.Sprintf("%s = '%s'", keyword, invalidPattern),
36+
want: []string{},
8437
},
8538
}
86-
for _, tt := range tests {
87-
t.Run(tt.name, func(t *testing.T) {
88-
s := Scanner{}
89-
got, err := s.FromData(tt.args.ctx, tt.args.verify, tt.args.data)
90-
if (err != nil) != tt.wantErr {
91-
t.Errorf("Tailscaleapi.FromData() error = %v, wantErr %v", err, tt.wantErr)
39+
40+
for _, test := range tests {
41+
t.Run(test.name, func(t *testing.T) {
42+
matchedDetectors := ahoCorasickCore.FindDetectorMatches([]byte(test.input))
43+
if len(matchedDetectors) == 0 {
44+
t.Errorf("keywords '%v' not matched by: %s", d.Keywords(), test.input)
9245
return
9346
}
94-
for i := range got {
95-
if len(got[i].Raw) == 0 {
96-
t.Fatalf("no raw secret present: \n %+v", got[i])
97-
}
98-
got[i].Raw = nil
47+
48+
results, err := d.FromData(context.Background(), false, []byte(test.input))
49+
if err != nil {
50+
t.Errorf("error = %v", err)
51+
return
9952
}
100-
if diff := pretty.Compare(got, tt.want); diff != "" {
101-
t.Errorf("Tailscaleapi.FromData() %s diff: (-got +want)\n%s", tt.name, diff)
53+
54+
if len(results) != len(test.want) {
55+
if len(results) == 0 {
56+
t.Errorf("did not receive result")
57+
} else {
58+
t.Errorf("expected %d results, only received %d", len(test.want), len(results))
59+
}
60+
return
10261
}
103-
})
104-
}
105-
}
10662

107-
func BenchmarkFromData(benchmark *testing.B) {
108-
ctx := context.Background()
109-
s := Scanner{}
110-
for name, data := range detectors.MustGetBenchmarkData() {
111-
benchmark.Run(name, func(b *testing.B) {
112-
for n := 0; n < b.N; n++ {
113-
_, err := s.FromData(ctx, false, data)
114-
if err != nil {
115-
b.Fatal(err)
63+
actual := make(map[string]struct{}, len(results))
64+
for _, r := range results {
65+
if len(r.RawV2) > 0 {
66+
actual[string(r.RawV2)] = struct{}{}
67+
} else {
68+
actual[string(r.Raw)] = struct{}{}
11669
}
11770
}
71+
expected := make(map[string]struct{}, len(test.want))
72+
for _, v := range test.want {
73+
expected[v] = struct{}{}
74+
}
75+
76+
if diff := cmp.Diff(expected, actual); diff != "" {
77+
t.Errorf("%s diff: (-want +got)\n%s", test.name, diff)
78+
}
11879
})
11980
}
12081
}

0 commit comments

Comments
 (0)