package diffscan import ( "amadan.net/oficina/equipo/internal/wire" "amadan.net/rastrillo/rastrillo/budget" "context" "encoding/base64" "errors" "github.com/zricethezav/gitleaks/v8/config" "os" "strings" "testing" ) func TestMain(m *testing.M) { os.Exit(budget.Main(m)) } func TestScanClosed(t *testing.T) { secret := "ghp_" + "0123456789abcdefghijklmnopqrstuvwxyz" for i, s := range []string{"rk_live_" + "0123456789abcdefghijklmnop", "rk_live_" + "0123456789abcdefghijklmnop # gitleaks:allow", secret, "-token = '" + secret + "' # gitleaks:allow", base64.StdEncoding.EncodeToString([]byte(secret)), "context\n" + secret + "\nend", "-----BEGIN RSA PRIVATE KEY-----\nMIIBOgIBAAJBALlongprivatekey\n-----END RSA PRIVATE KEY-----"} { if got := Check(context.Background(), s, nil); got != wire.DiffWithheld { t.Fatalf("case %d secret not withheld: %q", i, got) } } if Check(context.Background(), "let x = 1\n", nil) != "" { t.Fatal("safe refused") } if Check(context.Background(), "harmless opaque value", []string{"opaque value"}) != wire.DiffWithheld { t.Fatal("known secret") } ctx, cancel := context.WithCancel(context.Background()) cancel() if Check(ctx, "let x = 1\n", nil) != wire.DiffUnavailable { t.Fatal("cancelled scan") } if Check(context.Background(), strings.Repeat("x", wire.MaxDiffBlobBytes+1), nil) != wire.DiffTooLarge { t.Fatal("oversize scan") } } func TestEmbeddedPasswordURL(t *testing.T) { for _, value := range []string{"https://reader:tiny@repo.example.test/path", "postgres://app:p%40ss@db.example.test/main", "custom+scheme://user:password@host", "redis://:secret@host"} { if Check(context.Background(), value, nil) != wire.DiffWithheld { t.Fatal("embedded password accepted") } } for _, value := range []string{"https://reader@repo.example.test/path", "https://repo.example.test/path"} { if Check(context.Background(), value, nil) != "" { t.Fatal("URL without password refused") } } } func TestScannerFailureClosed(t *testing.T) { original := rules t.Cleanup(func() { rules = original }) rules = func() (config.Config, error) { panic("scanner failed") } if Check(context.Background(), "ordinary text", nil) != wire.DiffUnavailable { t.Fatal("panic accepted") } rules = func() (config.Config, error) { return config.Config{}, errors.New("scanner failed") } if Check(context.Background(), "ordinary text", nil) != wire.DiffUnavailable { t.Fatal("error accepted") } rules = original originalScan := scanText t.Cleanup(func() { scanText = originalScan }) scanText = func(context.Context, config.Config, string) (bool, error) { panic("detector failed") } if Check(context.Background(), "ordinary text", nil) != wire.DiffUnavailable { t.Fatal("detector panic accepted") } scanText = func(context.Context, config.Config, string) (bool, error) { return false, errors.New("detector failed") } if Check(context.Background(), "ordinary text", nil) != wire.DiffUnavailable { t.Fatal("detector error accepted") } }