|
| 1 | +//go:build detectors |
| 2 | +// +build detectors |
| 3 | + |
| 4 | +package sentryorgtoken |
| 5 | + |
| 6 | +import ( |
| 7 | + "context" |
| 8 | + "fmt" |
| 9 | + "testing" |
| 10 | + "time" |
| 11 | + |
| 12 | + "github.com/google/go-cmp/cmp" |
| 13 | + "github.com/google/go-cmp/cmp/cmpopts" |
| 14 | + |
| 15 | + "github.com/trufflesecurity/trufflehog/v3/pkg/common" |
| 16 | + "github.com/trufflesecurity/trufflehog/v3/pkg/detectors" |
| 17 | + "github.com/trufflesecurity/trufflehog/v3/pkg/pb/detectorspb" |
| 18 | +) |
| 19 | + |
| 20 | +func TestSentryOrgToken_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", "detectors5") |
| 24 | + if err != nil { |
| 25 | + t.Fatalf("could not get test secrets from GCP: %s", err) |
| 26 | + } |
| 27 | + secret := testSecrets.MustGetField("SENTRY_ORG_TOKEN") |
| 28 | + inactiveSecret := testSecrets.MustGetField("SENTRY_ORG_TOKEN_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 | + wantVerificationErr bool |
| 42 | + }{ |
| 43 | + { |
| 44 | + name: "found, verified", |
| 45 | + s: Scanner{}, |
| 46 | + args: args{ |
| 47 | + ctx: context.Background(), |
| 48 | + data: []byte(fmt.Sprintf("You can find a sentry secret %s within", secret)), |
| 49 | + verify: true, |
| 50 | + }, |
| 51 | + want: []detectors.Result{ |
| 52 | + { |
| 53 | + DetectorType: detectorspb.DetectorType_SentryOrgToken, |
| 54 | + Verified: true, |
| 55 | + }, |
| 56 | + }, |
| 57 | + wantErr: false, |
| 58 | + }, |
| 59 | + { |
| 60 | + name: "found, unverified", |
| 61 | + s: Scanner{}, |
| 62 | + args: args{ |
| 63 | + ctx: context.Background(), |
| 64 | + data: []byte(fmt.Sprintf("You can find a sentry secret %s within but not valid", inactiveSecret)), // the secret would satisfy the regex but not pass validation |
| 65 | + verify: true, |
| 66 | + }, |
| 67 | + want: []detectors.Result{ |
| 68 | + { |
| 69 | + DetectorType: detectorspb.DetectorType_SentryOrgToken, |
| 70 | + Verified: false, |
| 71 | + }, |
| 72 | + }, |
| 73 | + wantErr: false, |
| 74 | + }, |
| 75 | + { |
| 76 | + name: "not found", |
| 77 | + s: Scanner{}, |
| 78 | + args: args{ |
| 79 | + ctx: context.Background(), |
| 80 | + data: []byte("You cannot find the secret within"), |
| 81 | + verify: true, |
| 82 | + }, |
| 83 | + want: nil, |
| 84 | + wantErr: false, |
| 85 | + }, |
| 86 | + } |
| 87 | + |
| 88 | + for _, tt := range tests { |
| 89 | + t.Run(tt.name, func(t *testing.T) { |
| 90 | + got, err := tt.s.FromData(tt.args.ctx, tt.args.verify, tt.args.data) |
| 91 | + if (err != nil) != tt.wantErr { |
| 92 | + t.Errorf("SentryOrgToken.FromData() error = %v, wantErr %v", err, tt.wantErr) |
| 93 | + return |
| 94 | + } |
| 95 | + for i := range got { |
| 96 | + if len(got[i].Raw) == 0 { |
| 97 | + t.Fatal("no raw secret present") |
| 98 | + } |
| 99 | + if (got[i].VerificationError() != nil) != tt.wantVerificationErr { |
| 100 | + t.Fatalf("wantVerificationError = %v, verification error = %v,", tt.wantVerificationErr, got[i].VerificationError()) |
| 101 | + } |
| 102 | + } |
| 103 | + opts := cmpopts.IgnoreFields(detectors.Result{}, "Raw", "verificationError") |
| 104 | + if diff := cmp.Diff(got, tt.want, opts); diff != "" { |
| 105 | + t.Errorf("SentryOrgToken.FromData() %s diff: (-got +want)\n%s", tt.name, diff) |
| 106 | + } |
| 107 | + }) |
| 108 | + } |
| 109 | +} |
| 110 | + |
| 111 | +func BenchmarkFromData(benchmark *testing.B) { |
| 112 | + ctx := context.Background() |
| 113 | + s := Scanner{} |
| 114 | + for name, data := range detectors.MustGetBenchmarkData() { |
| 115 | + benchmark.Run(name, func(b *testing.B) { |
| 116 | + b.ResetTimer() |
| 117 | + for n := 0; n < b.N; n++ { |
| 118 | + _, err := s.FromData(ctx, false, data) |
| 119 | + if err != nil { |
| 120 | + b.Fatal(err) |
| 121 | + } |
| 122 | + } |
| 123 | + }) |
| 124 | + } |
| 125 | +} |
0 commit comments