|
| 1 | +//go:build detectors |
| 2 | +// +build detectors |
| 3 | + |
| 4 | +package flyio |
| 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 TestFlyio_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", "detectors6") |
| 24 | + if err != nil { |
| 25 | + t.Fatalf("could not get test secrets from GCP: %s", err) |
| 26 | + } |
| 27 | + secret := testSecrets.MustGetField("FLYIO") |
| 28 | + inactiveSecret := testSecrets.MustGetField("FLYIO_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 flyio secret %s within", secret)), |
| 49 | + verify: true, |
| 50 | + }, |
| 51 | + want: []detectors.Result{ |
| 52 | + { |
| 53 | + DetectorType: detectorspb.DetectorType_FlyIO, |
| 54 | + Verified: true, |
| 55 | + }, |
| 56 | + }, |
| 57 | + wantErr: false, |
| 58 | + wantVerificationErr: false, |
| 59 | + }, |
| 60 | + { |
| 61 | + name: "found, unverified", |
| 62 | + s: Scanner{}, |
| 63 | + args: args{ |
| 64 | + ctx: context.Background(), |
| 65 | + data: []byte(fmt.Sprintf("You can find a flyio secret %s within but not valid", inactiveSecret)), // the secret would satisfy the regex but not pass validation |
| 66 | + verify: true, |
| 67 | + }, |
| 68 | + want: []detectors.Result{ |
| 69 | + { |
| 70 | + DetectorType: detectorspb.DetectorType_FlyIO, |
| 71 | + Verified: false, |
| 72 | + }, |
| 73 | + }, |
| 74 | + wantErr: false, |
| 75 | + wantVerificationErr: false, |
| 76 | + }, |
| 77 | + { |
| 78 | + name: "not found", |
| 79 | + s: Scanner{}, |
| 80 | + args: args{ |
| 81 | + ctx: context.Background(), |
| 82 | + data: []byte("You cannot find the secret within"), |
| 83 | + verify: true, |
| 84 | + }, |
| 85 | + want: nil, |
| 86 | + wantErr: false, |
| 87 | + wantVerificationErr: false, |
| 88 | + }, |
| 89 | + { |
| 90 | + name: "found, would be verified if not for timeout", |
| 91 | + s: Scanner{client: common.SaneHttpClientTimeOut(1 * time.Microsecond)}, |
| 92 | + args: args{ |
| 93 | + ctx: context.Background(), |
| 94 | + data: []byte(fmt.Sprintf("You can find a flyio secret %s within", secret)), |
| 95 | + verify: true, |
| 96 | + }, |
| 97 | + want: []detectors.Result{ |
| 98 | + { |
| 99 | + DetectorType: detectorspb.DetectorType_FlyIO, |
| 100 | + Verified: false, |
| 101 | + }, |
| 102 | + }, |
| 103 | + wantErr: false, |
| 104 | + wantVerificationErr: true, |
| 105 | + }, |
| 106 | + { |
| 107 | + name: "found, verified but unexpected api surface", |
| 108 | + s: Scanner{client: common.ConstantResponseHttpClient(404, "")}, |
| 109 | + args: args{ |
| 110 | + ctx: context.Background(), |
| 111 | + data: []byte(fmt.Sprintf("You can find a flyio secret %s within", secret)), |
| 112 | + verify: true, |
| 113 | + }, |
| 114 | + want: func() []detectors.Result { |
| 115 | + r := detectors.Result{ |
| 116 | + DetectorType: detectorspb.DetectorType_FlyIO, |
| 117 | + Verified: false, |
| 118 | + } |
| 119 | + r.SetVerificationError(fmt.Errorf("unexpected HTTP response status 404")) |
| 120 | + return []detectors.Result{r} |
| 121 | + }(), |
| 122 | + wantErr: false, |
| 123 | + wantVerificationErr: true, |
| 124 | + }, |
| 125 | + } |
| 126 | + for _, tt := range tests { |
| 127 | + t.Run(tt.name, func(t *testing.T) { |
| 128 | + got, err := tt.s.FromData(tt.args.ctx, tt.args.verify, tt.args.data) |
| 129 | + if (err != nil) != tt.wantErr { |
| 130 | + t.Errorf("Flyio.FromData() error = %v, wantErr %v", err, tt.wantErr) |
| 131 | + return |
| 132 | + } |
| 133 | + for i := range got { |
| 134 | + if len(got[i].Raw) == 0 { |
| 135 | + t.Fatalf("no raw secret present: \n %+v", got[i]) |
| 136 | + } |
| 137 | + if (got[i].VerificationError() != nil) != tt.wantVerificationErr { |
| 138 | + t.Fatalf("wantVerificationError = %v, verification error = %v", tt.wantVerificationErr, got[i].VerificationError()) |
| 139 | + } |
| 140 | + } |
| 141 | + ignoreOpts := cmpopts.IgnoreFields(detectors.Result{}, "Raw", "verificationError", "AnalysisInfo") |
| 142 | + ignoreUnexported := cmpopts.IgnoreUnexported(detectors.Result{}) |
| 143 | + if diff := cmp.Diff(got, tt.want, ignoreOpts, ignoreUnexported); diff != "" { |
| 144 | + t.Errorf("Flyio.FromData() %s diff: (-got +want)\n%s", tt.name, diff) |
| 145 | + } |
| 146 | + }) |
| 147 | + } |
| 148 | +} |
0 commit comments