Skip to content

Commit bd1c660

Browse files
Improved and fixed A&B detectors pattern test cases (#4359)
* first commit * Improved and Fixed A&B detectors pattern test cases --------- Co-authored-by: Shahzad Haider <[email protected]>
1 parent 2f1baea commit bd1c660

File tree

127 files changed

+5657
-4080
lines changed

Some content is hidden

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

127 files changed

+5657
-4080
lines changed

pkg/detectors/abstract/abstract_test.go

Lines changed: 53 additions & 25 deletions
Original file line numberDiff line numberDiff line change
@@ -2,20 +2,15 @@ package abstract
22

33
import (
44
"context"
5-
"fmt"
65
"testing"
76

87
"github.com/google/go-cmp/cmp"
8+
"github.com/stretchr/testify/require"
99

1010
"github.com/trufflesecurity/trufflehog/v3/pkg/detectors"
1111
"github.com/trufflesecurity/trufflehog/v3/pkg/engine/ahocorasick"
1212
)
1313

14-
var (
15-
validPattern = "qwerty12345iugt67s7a7sa0akhsxz82"
16-
invalidPattern = "zxcvbr12345iugt67s7a7sa0akhsXz820"
17-
)
18-
1914
func TestAbstract_Pattern(t *testing.T) {
2015
d := Scanner{}
2116
ahoCorasickCore := ahocorasick.NewAhoCorasickCore([]detectors.Detector{d})
@@ -26,42 +21,74 @@ func TestAbstract_Pattern(t *testing.T) {
2621
want []string
2722
}{
2823
{
29-
name: "valid pattern",
30-
input: fmt.Sprintf("abstract token = '%s'", validPattern),
31-
want: []string{validPattern},
24+
name: "valid pattern",
25+
input: `
26+
[INFO] Sending request to abstract API
27+
[DEBUG] Using API_KEY=oxpf4a93fjovt0v1z6lltcbcizlrml98
28+
[INFO] Response received: 200 OK
29+
`,
30+
want: []string{"oxpf4a93fjovt0v1z6lltcbcizlrml98"},
31+
},
32+
{
33+
name: "valid pattern - xml",
34+
input: `
35+
<com.cloudbees.plugins.credentials.impl.StringCredentialsImpl>
36+
<scope>GLOBAL</scope>
37+
<id>{abstract}</id>
38+
<secret>{abstract AQAAABAAA 5422358j60yxo9nc0dbpxby602tsxd6j}</secret>
39+
<description>configuration for production</description>
40+
<creationDate>2023-05-18T14:32:10Z</creationDate>
41+
<owner>jenkins-admin</owner>
42+
</com.cloudbees.plugins.credentials.impl.StringCredentialsImpl>
43+
`,
44+
want: []string{"5422358j60yxo9nc0dbpxby602tsxd6j"},
45+
},
46+
{
47+
name: "valid pattern - two keys",
48+
input: `
49+
[INFO] Sending request to abstract API
50+
[DEBUG] Using API_KEY=oxpf4a93fjovt0v1z6lltcbcizlrml98
51+
[Error] Response received: 401 UnAuthorized
52+
[INFO] Sending request to abstract API
53+
[DEBUG] Using API_KEY=muytrs09876iugt67s7a7sa0akhsxz82
54+
[INFO] Response received: 200 OK
55+
`,
56+
want: []string{"oxpf4a93fjovt0v1z6lltcbcizlrml98", "muytrs09876iugt67s7a7sa0akhsxz82"},
3257
},
3358
{
34-
name: "valid pattern - out of prefix range",
35-
input: fmt.Sprintf("abstract token keyword is not close to the real token = '%s'", validPattern),
36-
want: nil,
59+
name: "valid pattern - out of prefix range",
60+
input: `
61+
[INFO] Sending request to abstract API
62+
[INFO] Processing request
63+
[Info] Response received: 200 OK
64+
[DEBUG] Used API_KEY=oxpf4a93fjovt0v1z6lltcbcizlrml98
65+
`,
66+
want: nil,
3767
},
3868
{
39-
name: "invalid pattern",
40-
input: fmt.Sprintf("abstract = '%s'", invalidPattern),
41-
want: nil,
69+
name: "invalid pattern",
70+
input: `
71+
[INFO] Sending request to abstract API
72+
[DEBUG] Using API_KEY=zxcvbr12345iugt67s7a7sa0akhsXz820
73+
[ERROR] Response received: 400 BadRequest
74+
`,
75+
want: nil,
4276
},
4377
}
4478

4579
for _, test := range tests {
4680
t.Run(test.name, func(t *testing.T) {
4781
matchedDetectors := ahoCorasickCore.FindDetectorMatches([]byte(test.input))
4882
if len(matchedDetectors) == 0 {
49-
t.Errorf("keywords '%v' not matched by: %s", d.Keywords(), test.input)
83+
t.Errorf("test %q failed: expected keywords %v to be found in the input", test.name, d.Keywords())
5084
return
5185
}
5286

5387
results, err := d.FromData(context.Background(), false, []byte(test.input))
54-
if err != nil {
55-
t.Errorf("error = %v", err)
56-
return
57-
}
88+
require.NoError(t, err)
5889

5990
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-
}
91+
t.Errorf("mismatch in result count: expected %d, got %d", len(test.want), len(results))
6592
return
6693
}
6794

@@ -73,6 +100,7 @@ func TestAbstract_Pattern(t *testing.T) {
73100
actual[string(r.Raw)] = struct{}{}
74101
}
75102
}
103+
76104
expected := make(map[string]struct{}, len(test.want))
77105
for _, v := range test.want {
78106
expected[v] = struct{}{}

pkg/detectors/abuseipdb/abuseipdb_test.go

Lines changed: 41 additions & 25 deletions
Original file line numberDiff line numberDiff line change
@@ -2,20 +2,15 @@ package abuseipdb
22

33
import (
44
"context"
5-
"fmt"
65
"testing"
76

87
"github.com/google/go-cmp/cmp"
8+
"github.com/stretchr/testify/require"
99

1010
"github.com/trufflesecurity/trufflehog/v3/pkg/detectors"
1111
"github.com/trufflesecurity/trufflehog/v3/pkg/engine/ahocorasick"
1212
)
1313

14-
var (
15-
validPattern = "123abcdef456ghijkl789mnopqr012stuvwx3455123abcdef456ghijkl789mnopqr012stuvwx3455"
16-
invalidPattern = "123abcdef456Ghijkl789mnopqr012stuvwx3455123abcdef456ghijkl789mnopqr012stuvwX"
17-
)
18-
1914
func TestAbuseipdb_Pattern(t *testing.T) {
2015
d := Scanner{}
2116
ahoCorasickCore := ahocorasick.NewAhoCorasickCore([]detectors.Detector{d})
@@ -26,42 +21,62 @@ func TestAbuseipdb_Pattern(t *testing.T) {
2621
want []string
2722
}{
2823
{
29-
name: "valid pattern",
30-
input: fmt.Sprintf("abuseipdb token = '%s'", validPattern),
31-
want: []string{validPattern},
24+
name: "valid pattern",
25+
input: `
26+
[INFO] Sending request to abuseipdb API
27+
[DEBUG] Using API_KEY=o8oqti3tghu2xic76ii4t7jb9bxuzd4200j1yrkdjl6s8834hx4dgz1wwo90diqraakjd13sljcjkfnf
28+
[INFO] Response received: 200 OK
29+
`,
30+
want: []string{"o8oqti3tghu2xic76ii4t7jb9bxuzd4200j1yrkdjl6s8834hx4dgz1wwo90diqraakjd13sljcjkfnf"},
3231
},
3332
{
34-
name: "valid pattern - out of prefix range",
35-
input: fmt.Sprintf("abuseipdb token keyword is not close to the real token = '%s'", validPattern),
36-
want: nil,
33+
name: "valid pattern - xml",
34+
input: `
35+
<com.cloudbees.plugins.credentials.impl.StringCredentialsImpl>
36+
<scope>GLOBAL</scope>
37+
<id>{abuseipdb}</id>
38+
<secret>{abuseipdb AQAAABAAA zgtj0q3v38u4pthc6nmy02n60bj244u5o9j47ln1jlue5mxzaasfi29x4dzcbxroawvkm26thtr61066}</secret>
39+
<description>configuration for production</description>
40+
<creationDate>2023-05-18T14:32:10Z</creationDate>
41+
<owner>jenkins-admin</owner>
42+
</com.cloudbees.plugins.credentials.impl.StringCredentialsImpl>
43+
`,
44+
want: []string{"zgtj0q3v38u4pthc6nmy02n60bj244u5o9j47ln1jlue5mxzaasfi29x4dzcbxroawvkm26thtr61066"},
3745
},
3846
{
39-
name: "invalid pattern",
40-
input: fmt.Sprintf("abuseipdb = '%s'", invalidPattern),
41-
want: nil,
47+
name: "valid pattern - out of prefix range",
48+
input: `
49+
[INFO] Sending request to abuseipdb API
50+
[INFO] Processing request
51+
[Info] Response received: 200 OK
52+
[DEBUG] Used API_KEY=o8oqti3tghu2xic76ii4t7jb9bxuzd4200j1yrkdjl6s8834hx4dgz1wwo90diqraakjd13sljcjkfnf
53+
`,
54+
want: nil,
55+
},
56+
{
57+
name: "invalid pattern",
58+
input: `
59+
[INFO] Sending request to abuseipdb API
60+
[DEBUG] Using API_KEY=7e4abcdef456Ghijkl789mnopqr012stuvwx3455123abcdef456ghijkl789mnopqr012stuvwX
61+
[ERROR] Response received: 400 BadRequest
62+
`,
63+
want: nil,
4264
},
4365
}
4466

4567
for _, test := range tests {
4668
t.Run(test.name, func(t *testing.T) {
4769
matchedDetectors := ahoCorasickCore.FindDetectorMatches([]byte(test.input))
4870
if len(matchedDetectors) == 0 {
49-
t.Errorf("keywords '%v' not matched by: %s", d.Keywords(), test.input)
71+
t.Errorf("test %q failed: expected keywords %v to be found in the input", test.name, d.Keywords())
5072
return
5173
}
5274

5375
results, err := d.FromData(context.Background(), false, []byte(test.input))
54-
if err != nil {
55-
t.Errorf("error = %v", err)
56-
return
57-
}
76+
require.NoError(t, err)
5877

5978
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-
}
79+
t.Errorf("mismatch in result count: expected %d, got %d", len(test.want), len(results))
6580
return
6681
}
6782

@@ -73,6 +88,7 @@ func TestAbuseipdb_Pattern(t *testing.T) {
7388
actual[string(r.Raw)] = struct{}{}
7489
}
7590
}
91+
7692
expected := make(map[string]struct{}, len(test.want))
7793
for _, v := range test.want {
7894
expected[v] = struct{}{}

pkg/detectors/abyssale/abyssale_test.go

Lines changed: 41 additions & 25 deletions
Original file line numberDiff line numberDiff line change
@@ -2,20 +2,15 @@ package abyssale
22

33
import (
44
"context"
5-
"fmt"
65
"testing"
76

87
"github.com/google/go-cmp/cmp"
8+
"github.com/stretchr/testify/require"
99

1010
"github.com/trufflesecurity/trufflehog/v3/pkg/detectors"
1111
"github.com/trufflesecurity/trufflehog/v3/pkg/engine/ahocorasick"
1212
)
1313

14-
var (
15-
validPattern = "123abcDEF456ghiJKL789mnoPQR012stuVWX3456"
16-
invalidPattern = "123abcDEF456ghiJKL789mnoPQR012stuVWX"
17-
)
18-
1914
func TestAbyssale_Pattern(t *testing.T) {
2015
d := Scanner{}
2116
ahoCorasickCore := ahocorasick.NewAhoCorasickCore([]detectors.Detector{d})
@@ -26,42 +21,62 @@ func TestAbyssale_Pattern(t *testing.T) {
2621
want []string
2722
}{
2823
{
29-
name: "valid pattern",
30-
input: fmt.Sprintf("abyssale token = '%s'", validPattern),
31-
want: []string{validPattern},
24+
name: "valid pattern",
25+
input: `
26+
[INFO] Sending request to abyssale API
27+
[DEBUG] Using API_KEY=rWE8I0axy6Fvw40RE8tsNS3L7zBU5vAhEnW4hq9G
28+
[INFO] Response received: 200 OK
29+
`,
30+
want: []string{"rWE8I0axy6Fvw40RE8tsNS3L7zBU5vAhEnW4hq9G"},
3231
},
3332
{
34-
name: "valid pattern - out of prefix range",
35-
input: fmt.Sprintf("abyssale token keyword is not close to the real token = '%s'", validPattern),
36-
want: nil,
33+
name: "valid pattern - xml",
34+
input: `
35+
<com.cloudbees.plugins.credentials.impl.StringCredentialsImpl>
36+
<scope>GLOBAL</scope>
37+
<id>{abyssale}</id>
38+
<secret>{abyssale AQAAABAAA xTiPNSDg6JjzG8fWoLb8JlE8SBcMKkCx2fZLZD91}</secret>
39+
<description>configuration for production</description>
40+
<creationDate>2023-05-18T14:32:10Z</creationDate>
41+
<owner>jenkins-admin</owner>
42+
</com.cloudbees.plugins.credentials.impl.StringCredentialsImpl>
43+
`,
44+
want: []string{"xTiPNSDg6JjzG8fWoLb8JlE8SBcMKkCx2fZLZD91"},
3745
},
3846
{
39-
name: "invalid pattern",
40-
input: fmt.Sprintf("abyssale = '%s'", invalidPattern),
41-
want: nil,
47+
name: "valid pattern - out of prefix range",
48+
input: `
49+
[INFO] Sending request to abyssale API
50+
[INFO] Processing request
51+
[Info] Response received: 200 OK
52+
[DEBUG] Used API_KEY=rWE8I0axy6Fvw40RE8tsNS3L7zBU5vAhEnW4hq9G
53+
`,
54+
want: nil,
55+
},
56+
{
57+
name: "invalid pattern",
58+
input: `
59+
[INFO] Sending request to abyssale API
60+
[DEBUG] Using API_KEY=rWE8_0axy6Fvw40RE8tsNS3L7zBU5vAhEnW4hq9G
61+
[ERROR] Response received: 400 BadRequest
62+
`,
63+
want: nil,
4264
},
4365
}
4466

4567
for _, test := range tests {
4668
t.Run(test.name, func(t *testing.T) {
4769
matchedDetectors := ahoCorasickCore.FindDetectorMatches([]byte(test.input))
4870
if len(matchedDetectors) == 0 {
49-
t.Errorf("keywords '%v' not matched by: %s", d.Keywords(), test.input)
71+
t.Errorf("test %q failed: expected keywords %v to be found in the input", test.name, d.Keywords())
5072
return
5173
}
5274

5375
results, err := d.FromData(context.Background(), false, []byte(test.input))
54-
if err != nil {
55-
t.Errorf("error = %v", err)
56-
return
57-
}
76+
require.NoError(t, err)
5877

5978
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-
}
79+
t.Errorf("mismatch in result count: expected %d, got %d", len(test.want), len(results))
6580
return
6681
}
6782

@@ -73,6 +88,7 @@ func TestAbyssale_Pattern(t *testing.T) {
7388
actual[string(r.Raw)] = struct{}{}
7489
}
7590
}
91+
7692
expected := make(map[string]struct{}, len(test.want))
7793
for _, v := range test.want {
7894
expected[v] = struct{}{}

0 commit comments

Comments
 (0)