Files
trufflehog/pkg/detectors/bitbucketapppassword/bitbucketapppassword_integration_test.go
Casey Tran 47e7b7cd74
Lint / golangci-lint (push) Waiting to run
Lint / semgrep (push) Waiting to run
Scan for secrets / test (push) Waiting to run
Test / test (push) Waiting to run
Test / test-community (push) Waiting to run
Release / mark-latest (push) Canceled after 0s
Release / Release (push) Canceled after 0s
Split out detector types into separate proto file in order to narrow CODEOWNERS scope (#4871)
We want to move the detector types out of the Scanning team purview. So I split off detector types into its own proto file (so that file detector_type.proto can be owned by the Integrations team), regenerated the pb files with "make protos", and made the detector files use the new generated detector_type.pb.go. Included the new detector_type.proto file in CODEOWNERS and made CODEOWNERS categories that contain larger teams be towards the top so that more fine grained ownership is filtered properly.
2026-04-07 16:16:31 -05:00

104 lines
2.8 KiB
Go

//go:build detectors
// +build detectors
package bitbucketapppassword
import (
"context"
"fmt"
"testing"
"time"
"github.com/google/go-cmp/cmp"
"github.com/trufflesecurity/trufflehog/v3/pkg/common"
"github.com/trufflesecurity/trufflehog/v3/pkg/detectors"
"github.com/trufflesecurity/trufflehog/v3/pkg/pb/detector_typepb"
)
func TestBitbucketAppPassword_FromData_Integration(t *testing.T) {
ctx, cancel := context.WithTimeout(context.Background(), time.Second*10)
defer cancel()
testSecrets, err := common.GetSecret(ctx, "trufflehog-testing", "detectors2")
if err != nil {
t.Fatalf("could not get test secrets from GCP: %s", err)
}
username := testSecrets.MustGetField("USERNAME")
validPassword := testSecrets.MustGetField("BITBUCKETAPPPASSWORD")
invalidPassword := "ATBB123abcDEF456ghiJKL789mnoPQR" // An invalid but correctly formatted password
tests := []struct {
name string
input string
want []detectors.Result
wantErr bool
}{
{
name: "valid credential",
input: fmt.Sprintf("https://%s:%[email protected]", username, validPassword),
want: []detectors.Result{
{
DetectorType: detector_typepb.DetectorType_BitbucketAppPassword,
Verified: true,
Raw: []byte(fmt.Sprintf("%s:%s", username, validPassword)),
},
},
},
{
name: "invalid credential",
input: fmt.Sprintf("https://%s:%[email protected]", username, invalidPassword),
want: []detectors.Result{
{
DetectorType: detector_typepb.DetectorType_BitbucketAppPassword,
Verified: false,
Raw: []byte(fmt.Sprintf("%s:%s", username, invalidPassword)),
},
},
},
{
name: "no credential found",
input: "this string has no credentials",
want: nil,
},
}
for _, tc := range tests {
t.Run(tc.name, func(t *testing.T) {
s := &Scanner{}
got, err := s.FromData(ctx, true, []byte(tc.input))
if (err != nil) != tc.wantErr {
t.Fatalf("FromData() error = %v, wantErr %v", err, tc.wantErr)
}
// Normalizing results for comparison by removing fields that are not relevant for the test
for i := range got {
if got[i].VerificationError() != nil {
t.Logf("verification error: %s", got[i].VerificationError())
}
}
if diff := cmp.Diff(tc.want, got, cmp.Comparer(func(x, y detectors.Result) bool {
return x.Verified == y.Verified && string(x.Raw) == string(y.Raw) && x.DetectorType == y.DetectorType
})); diff != "" {
t.Errorf("FromData() mismatch (-want +got):\n%s", diff)
}
})
}
}
func BenchmarkFromData(benchmark *testing.B) {
ctx := context.Background()
s := &Scanner{}
for name, data := range detectors.MustGetBenchmarkData() {
benchmark.Run(name, func(b *testing.B) {
for n := 0; n < b.N; n++ {
_, err := s.FromData(ctx, false, data)
if err != nil {
b.Fatal(err)
}
}
})
}
}