files_test.go 1.8 KB

1234567891011121314151617181920212223242526272829303132333435363738394041424344454647484950515253545556575859606162636465666768697071727374757677787980
  1. package detect
  2. import (
  3. "path/filepath"
  4. "testing"
  5. "github.com/spf13/viper"
  6. "github.com/stretchr/testify/assert"
  7. "github.com/zricethezav/gitleaks/v8/config"
  8. "github.com/zricethezav/gitleaks/v8/report"
  9. )
  10. // TestFromGit tests the FromGit function
  11. func TestFromFiles(t *testing.T) {
  12. tests := []struct {
  13. cfgName string
  14. opts Options
  15. source string
  16. expectedFindings []*report.Finding
  17. }{
  18. {
  19. source: filepath.Join(repoBasePath, "nogit"),
  20. cfgName: "simple",
  21. expectedFindings: []*report.Finding{
  22. {
  23. Description: "AWS Access Key",
  24. StartLine: 19,
  25. EndLine: 19,
  26. StartColumn: 16,
  27. EndColumn: 35,
  28. Match: "AKIALALEMEL33243OLIA",
  29. Secret: "AKIALALEMEL33243OLIA",
  30. File: "../testdata/repos/nogit/main.go",
  31. RuleID: "aws-access-key",
  32. Tags: []string{"key", "AWS"},
  33. },
  34. },
  35. },
  36. {
  37. source: filepath.Join(repoBasePath, "nogit", "main.go"),
  38. cfgName: "simple",
  39. expectedFindings: []*report.Finding{
  40. {
  41. Description: "AWS Access Key",
  42. StartLine: 19,
  43. EndLine: 19,
  44. StartColumn: 16,
  45. EndColumn: 35,
  46. Match: "AKIALALEMEL33243OLIA",
  47. Secret: "AKIALALEMEL33243OLIA",
  48. File: "../testdata/repos/nogit/main.go",
  49. RuleID: "aws-access-key",
  50. Tags: []string{"key", "AWS"},
  51. },
  52. },
  53. },
  54. }
  55. for _, tt := range tests {
  56. viper.AddConfigPath(configPath)
  57. viper.SetConfigName("simple")
  58. viper.SetConfigType("toml")
  59. err := viper.ReadInConfig()
  60. if err != nil {
  61. t.Error(err)
  62. }
  63. var vc config.ViperConfig
  64. viper.Unmarshal(&vc)
  65. cfg, _ := vc.Translate()
  66. findings, err := FromFiles(tt.source, cfg, tt.opts)
  67. if err != nil {
  68. t.Error(err)
  69. }
  70. assert.ElementsMatch(t, tt.expectedFindings, findings)
  71. }
  72. }