|
| 1 | +//go:build detectors |
| 2 | +// +build detectors |
| 3 | + |
| 4 | +package salesforcerefreshtoken |
| 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 TestSalesforcerefreshtoken_FromChunk(t *testing.T) { |
| 21 | + ctx, cancel := context.WithTimeout(context.Background(), time.Second*5) |
| 22 | + defer cancel() |
| 23 | + |
| 24 | + testSecrets, err := common.GetSecret(ctx, "trufflehog-testing", "detectors6") |
| 25 | + if err != nil { |
| 26 | + t.Fatalf("could not get test secrets from GCP: %s", err) |
| 27 | + } |
| 28 | + refreshToken := testSecrets.MustGetField("SALESFORCE_REFRESH_TOKEN") |
| 29 | + consumerKey := testSecrets.MustGetField("SALESFORCE_REFRESH_TOKEN_KEY") |
| 30 | + consumerSecret := testSecrets.MustGetField("SALESFORCE_REFRESH_TOKEN_SECRET") |
| 31 | + inactiveSecret := testSecrets.MustGetField("SALESFORCE_REFRESH_TOKEN_INACTIVE_SECRET") |
| 32 | + |
| 33 | + type args struct { |
| 34 | + ctx context.Context |
| 35 | + data []byte |
| 36 | + verify bool |
| 37 | + } |
| 38 | + tests := []struct { |
| 39 | + name string |
| 40 | + s Scanner |
| 41 | + args args |
| 42 | + want []detectors.Result |
| 43 | + wantErr bool |
| 44 | + wantVerificationErr bool |
| 45 | + }{ |
| 46 | + { |
| 47 | + name: "found one valid trio, verified", |
| 48 | + s: Scanner{}, |
| 49 | + args: args{ |
| 50 | + ctx: context.Background(), |
| 51 | + data: []byte(fmt.Sprintf("refresh_token: %s, key: %s, secret: %s", refreshToken, consumerKey, consumerSecret)), |
| 52 | + verify: true, |
| 53 | + }, |
| 54 | + want: []detectors.Result{ |
| 55 | + { |
| 56 | + DetectorType: detectorspb.DetectorType_SalesforceRefreshToken, |
| 57 | + Verified: true, |
| 58 | + }, |
| 59 | + }, |
| 60 | + wantErr: false, |
| 61 | + wantVerificationErr: false, |
| 62 | + }, |
| 63 | + { |
| 64 | + name: "found one invalid trio, unverified", |
| 65 | + s: Scanner{}, |
| 66 | + args: args{ |
| 67 | + ctx: context.Background(), |
| 68 | + data: []byte(fmt.Sprintf("refresh_token: %s, key: %s, secret: %s", refreshToken, consumerKey, inactiveSecret)), |
| 69 | + verify: true, |
| 70 | + }, |
| 71 | + want: []detectors.Result{ |
| 72 | + { |
| 73 | + DetectorType: detectorspb.DetectorType_SalesforceRefreshToken, |
| 74 | + Verified: false, |
| 75 | + }, |
| 76 | + }, |
| 77 | + wantErr: false, |
| 78 | + wantVerificationErr: true, // Verification fails because the credentials are invalid and we can't verify the refresh token. |
| 79 | + }, |
| 80 | + { |
| 81 | + name: "multiple findings, one verified", |
| 82 | + s: Scanner{}, |
| 83 | + args: args{ |
| 84 | + ctx: context.Background(), |
| 85 | + data: []byte(fmt.Sprintf(` |
| 86 | + refresh_token: %s, key: %s, valid_secret: %s, |
| 87 | + invalid_refresh_token: 5Aep861eN26Sp9j0R5QPjh0AAAABBBBCCCCjcNqfo5kVBplkpP5tzyWXyVGAivx26AAAABBBBjYE133BBBBAAAA`, |
| 88 | + refreshToken, consumerKey, consumerSecret)), |
| 89 | + verify: true, |
| 90 | + }, |
| 91 | + want: []detectors.Result{ |
| 92 | + { |
| 93 | + DetectorType: detectorspb.DetectorType_SalesforceRefreshToken, |
| 94 | + Verified: true, // The valid refresh token combination |
| 95 | + }, |
| 96 | + { |
| 97 | + DetectorType: detectorspb.DetectorType_SalesforceRefreshToken, |
| 98 | + Verified: false, // The invalid refresh token combination |
| 99 | + }, |
| 100 | + }, |
| 101 | + wantErr: false, |
| 102 | + wantVerificationErr: false, |
| 103 | + }, |
| 104 | + { |
| 105 | + name: "not found (missing a component)", |
| 106 | + s: Scanner{}, |
| 107 | + args: args{ |
| 108 | + ctx: context.Background(), |
| 109 | + data: []byte(fmt.Sprintf("key: %s, secret: %s", consumerKey, consumerSecret)), // No refresh token |
| 110 | + verify: true, |
| 111 | + }, |
| 112 | + want: nil, |
| 113 | + wantErr: false, |
| 114 | + wantVerificationErr: false, |
| 115 | + }, |
| 116 | + { |
| 117 | + name: "found, would be verified if not for timeout", |
| 118 | + s: Scanner{client: common.SaneHttpClientTimeOut(1 * time.Microsecond)}, |
| 119 | + args: args{ |
| 120 | + ctx: context.Background(), |
| 121 | + data: []byte(fmt.Sprintf("refresh_token: %s, key: %s, secret: %s", refreshToken, consumerKey, consumerSecret)), |
| 122 | + verify: true, |
| 123 | + }, |
| 124 | + want: []detectors.Result{ |
| 125 | + { |
| 126 | + DetectorType: detectorspb.DetectorType_SalesforceRefreshToken, |
| 127 | + Verified: false, |
| 128 | + }, |
| 129 | + }, |
| 130 | + wantErr: false, |
| 131 | + wantVerificationErr: true, |
| 132 | + }, |
| 133 | + { |
| 134 | + name: "found, unexpected api response", |
| 135 | + s: Scanner{client: common.ConstantResponseHttpClient(404, "")}, |
| 136 | + args: args{ |
| 137 | + ctx: context.Background(), |
| 138 | + data: []byte(fmt.Sprintf("refresh_token: %s, key: %s, secret: %s", refreshToken, consumerKey, consumerSecret)), |
| 139 | + verify: true, |
| 140 | + }, |
| 141 | + want: []detectors.Result{ |
| 142 | + { |
| 143 | + DetectorType: detectorspb.DetectorType_SalesforceRefreshToken, |
| 144 | + Verified: false, |
| 145 | + }, |
| 146 | + }, |
| 147 | + wantErr: false, |
| 148 | + wantVerificationErr: true, |
| 149 | + }, |
| 150 | + } |
| 151 | + for _, tt := range tests { |
| 152 | + t.Run(tt.name, func(t *testing.T) { |
| 153 | + got, err := tt.s.FromData(tt.args.ctx, tt.args.verify, tt.args.data) |
| 154 | + if (err != nil) != tt.wantErr { |
| 155 | + t.Errorf("FromData() error = %v, wantErr %v", err, tt.wantErr) |
| 156 | + return |
| 157 | + } |
| 158 | + |
| 159 | + // Since the order of results can vary with maps, we use a more robust comparison. |
| 160 | + // This checks that for every `want` result, there is a matching `got` result. |
| 161 | + opts := []cmp.Option{ |
| 162 | + cmpopts.IgnoreFields(detectors.Result{}, "Raw", "RawV2", "verificationError", "ExtraData", "VerificationFromCache", "primarySecret"), |
| 163 | + cmpopts.SortSlices(func(a, b detectors.Result) bool { return a.Verified }), |
| 164 | + } |
| 165 | + if diff := cmp.Diff(tt.want, got, opts...); diff != "" { |
| 166 | + t.Errorf("FromData() results mismatch (-want +got):\n%s", diff) |
| 167 | + } |
| 168 | + |
| 169 | + // Also check that verification errors match expectations across all results. |
| 170 | + var gotErr bool |
| 171 | + for _, r := range got { |
| 172 | + if r.VerificationError() != nil { |
| 173 | + gotErr = true |
| 174 | + break |
| 175 | + } |
| 176 | + } |
| 177 | + if gotErr != tt.wantVerificationErr { |
| 178 | + t.Errorf("wantVerificationErr = %v, but got an error state of %v", tt.wantVerificationErr, gotErr) |
| 179 | + } |
| 180 | + }) |
| 181 | + } |
| 182 | +} |
| 183 | + |
| 184 | +func BenchmarkFromData(benchmark *testing.B) { |
| 185 | + ctx := context.Background() |
| 186 | + s := Scanner{} |
| 187 | + for name, data := range detectors.MustGetBenchmarkData() { |
| 188 | + benchmark.Run(name, func(b *testing.B) { |
| 189 | + b.ResetTimer() |
| 190 | + for n := 0; n < b.N; n++ { |
| 191 | + _, err := s.FromData(ctx, false, data) |
| 192 | + if err != nil { |
| 193 | + b.Fatal(err) |
| 194 | + } |
| 195 | + } |
| 196 | + }) |
| 197 | + } |
| 198 | +} |
0 commit comments