|
| 1 | +package twitch |
| 2 | + |
| 3 | +import ( |
| 4 | + "context" |
| 5 | + "fmt" |
| 6 | + "testing" |
| 7 | + "time" |
| 8 | + |
| 9 | + "github.com/kylelemons/godebug/pretty" |
| 10 | + "github.com/trufflesecurity/trufflehog/v3/pkg/detectors" |
| 11 | + |
| 12 | + "github.com/trufflesecurity/trufflehog/v3/pkg/common" |
| 13 | + "github.com/trufflesecurity/trufflehog/v3/pkg/pb/detectorspb" |
| 14 | +) |
| 15 | + |
| 16 | +func TestTwitch_FromChunk(t *testing.T) { |
| 17 | + ctx, cancel := context.WithTimeout(context.Background(), time.Second*5) |
| 18 | + defer cancel() |
| 19 | + testSecrets, err := common.GetSecret(ctx, "trufflehog-testing", "detectors3") |
| 20 | + if err != nil { |
| 21 | + t.Fatalf("could not get test secrets from GCP: %s", err) |
| 22 | + } |
| 23 | + secret := testSecrets.MustGetField("TWITCH") |
| 24 | + id := testSecrets.MustGetField("TWITCH_ID") |
| 25 | + inactiveSecret := testSecrets.MustGetField("TWITCH_INACTIVE") |
| 26 | + |
| 27 | + type args struct { |
| 28 | + ctx context.Context |
| 29 | + data []byte |
| 30 | + verify bool |
| 31 | + } |
| 32 | + tests := []struct { |
| 33 | + name string |
| 34 | + s Scanner |
| 35 | + args args |
| 36 | + want []detectors.Result |
| 37 | + wantErr bool |
| 38 | + }{ |
| 39 | + { |
| 40 | + name: "found, verified", |
| 41 | + s: Scanner{}, |
| 42 | + args: args{ |
| 43 | + ctx: context.Background(), |
| 44 | + data: []byte(fmt.Sprintf("You can find a twitch secret %s within twitch %s", secret, id)), |
| 45 | + verify: true, |
| 46 | + }, |
| 47 | + want: []detectors.Result{ |
| 48 | + { |
| 49 | + DetectorType: detectorspb.DetectorType_Twitch, |
| 50 | + Verified: true, |
| 51 | + }, |
| 52 | + }, |
| 53 | + wantErr: false, |
| 54 | + }, |
| 55 | + { |
| 56 | + name: "found, unverified", |
| 57 | + s: Scanner{}, |
| 58 | + args: args{ |
| 59 | + ctx: context.Background(), |
| 60 | + data: []byte(fmt.Sprintf("You can find a twitch secret %s within twitch %s but not valid", inactiveSecret, id)), // the secret would satisfy the regex but not pass validation |
| 61 | + verify: true, |
| 62 | + }, |
| 63 | + want: []detectors.Result{ |
| 64 | + { |
| 65 | + DetectorType: detectorspb.DetectorType_Twitch, |
| 66 | + Verified: false, |
| 67 | + }, |
| 68 | + }, |
| 69 | + wantErr: false, |
| 70 | + }, |
| 71 | + { |
| 72 | + name: "not found", |
| 73 | + s: Scanner{}, |
| 74 | + args: args{ |
| 75 | + ctx: context.Background(), |
| 76 | + data: []byte("You cannot find the secret within"), |
| 77 | + verify: true, |
| 78 | + }, |
| 79 | + want: nil, |
| 80 | + wantErr: false, |
| 81 | + }, |
| 82 | + } |
| 83 | + for _, tt := range tests { |
| 84 | + t.Run(tt.name, func(t *testing.T) { |
| 85 | + s := Scanner{} |
| 86 | + got, err := s.FromData(tt.args.ctx, tt.args.verify, tt.args.data) |
| 87 | + if (err != nil) != tt.wantErr { |
| 88 | + t.Errorf("Twitch.FromData() error = %v, wantErr %v", err, tt.wantErr) |
| 89 | + return |
| 90 | + } |
| 91 | + for i := range got { |
| 92 | + if len(got[i].Raw) == 0 { |
| 93 | + t.Fatalf("no raw secret present: \n %+v", got[i]) |
| 94 | + } |
| 95 | + got[i].Raw = nil |
| 96 | + } |
| 97 | + if diff := pretty.Compare(got, tt.want); diff != "" { |
| 98 | + t.Errorf("Twitch.FromData() %s diff: (-got +want)\n%s", tt.name, diff) |
| 99 | + } |
| 100 | + }) |
| 101 | + } |
| 102 | +} |
| 103 | + |
| 104 | +func BenchmarkFromData(benchmark *testing.B) { |
| 105 | + ctx := context.Background() |
| 106 | + s := Scanner{} |
| 107 | + for name, data := range detectors.MustGetBenchmarkData() { |
| 108 | + benchmark.Run(name, func(b *testing.B) { |
| 109 | + for n := 0; n < b.N; n++ { |
| 110 | + _, err := s.FromData(ctx, false, data) |
| 111 | + if err != nil { |
| 112 | + b.Fatal(err) |
| 113 | + } |
| 114 | + } |
| 115 | + }) |
| 116 | + } |
| 117 | +} |
0 commit comments