trufflehog/pkg/engine/gcs_test.go
Brendan Shaklovitz da5301ea1e
Exit with non-zero exit code on chunk source error (#1286)
* Exit with non-zero exit code on chunk source error

* Exit with a non-zero exit code whenever we hit an error getting
  chunks. Previously the error would be logged but trufflehog would exit
  with a 0 (success) status code.

* fix gcs test

---------

Co-authored-by: Dustin Decker <dustin@trufflesec.com>
Co-authored-by: ahrav <ahravdutta02@gmail.com>
2023-06-26 11:39:57 -05:00

89 lines
2 KiB
Go

package engine
import (
"strings"
"testing"
"github.com/trufflesecurity/trufflehog/v3/pkg/context"
"github.com/trufflesecurity/trufflehog/v3/pkg/decoders"
"github.com/trufflesecurity/trufflehog/v3/pkg/sources"
)
func TestScanGCS(t *testing.T) {
tests := []struct {
name string
gcsConfig sources.GCSConfig
wantErr bool
}{
{
name: "scanned GCS",
gcsConfig: sources.GCSConfig{
ApiKey: "abc123",
ProjectID: "test-project",
CloudCred: false,
WithoutAuth: false,
ServiceAccount: "",
},
},
{
name: "missing project ID, with auth",
gcsConfig: sources.GCSConfig{ApiKey: "abc123"},
wantErr: true,
},
{
name: "missing project ID, without auth, public scan",
gcsConfig: sources.GCSConfig{WithoutAuth: true},
},
{
name: "multiple selected auth methods",
gcsConfig: sources.GCSConfig{
ApiKey: "abc123",
ProjectID: "test-project",
CloudCred: true,
WithoutAuth: false,
ServiceAccount: "",
},
wantErr: true,
},
{
name: "no auth method selected",
gcsConfig: sources.GCSConfig{
ProjectID: "test-project",
MaxObjectSize: 10 * 1024 * 1024,
},
},
}
for _, test := range tests {
t.Run(test.name, func(t *testing.T) {
ctx, cancel := context.WithCancel(context.TODO())
defer cancel()
e := Start(ctx,
WithConcurrency(1),
WithDecoders(decoders.DefaultDecoders()...),
WithDetectors(false, DefaultDetectors()...),
)
go func() {
resultCount := 0
for range e.ResultsChan() {
resultCount++
}
}()
err := e.ScanGCS(ctx, test.gcsConfig)
if err != nil && !test.wantErr && !strings.Contains(err.Error(), "googleapi: Error 400: Bad Request") {
t.Errorf("ScanGCS() got: %v, want: %v", err, nil)
return
}
logFatalFunc := func(_ error, _ string, _ ...any) {
t.Fatalf("error logging function should not have been called")
}
e.Finish(ctx, logFatalFunc)
if err == nil && test.wantErr {
t.Errorf("ScanGCS() got: %v, want: %v", err, "error")
}
})
}
}