config_test.go 3.2 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116
  1. package config
  2. import (
  3. "fmt"
  4. "github.com/zricethezav/gitleaks/options"
  5. "regexp"
  6. "testing"
  7. )
  8. func TestParse(t *testing.T) {
  9. tests := []struct {
  10. description string
  11. opts options.Options
  12. wantErr error
  13. wantFileRegex *regexp.Regexp
  14. wantMessages *regexp.Regexp
  15. wantWhitelist Whitelist
  16. }{
  17. {
  18. description: "default config",
  19. opts: options.Options{},
  20. },
  21. {
  22. description: "test successful load",
  23. opts: options.Options{
  24. Config: "../test_data/test_configs/aws_key.toml",
  25. },
  26. },
  27. {
  28. description: "test bad toml",
  29. opts: options.Options{
  30. Config: "../test_data/test_configs/bad_aws_key.toml",
  31. },
  32. wantErr: fmt.Errorf("Near line 7 (last key parsed 'rules.description'): expected value but found \"AWS\" instead"),
  33. },
  34. {
  35. description: "test bad regex",
  36. opts: options.Options{
  37. Config: "../test_data/test_configs/bad_regex_aws_key.toml",
  38. },
  39. wantErr: fmt.Errorf("problem loading config: error parsing regexp: invalid nested repetition operator: `???`"),
  40. },
  41. {
  42. description: "test bad global whitelist file regex",
  43. opts: options.Options{
  44. Config: "../test_data/test_configs/bad_aws_key_global_whitelist_file.toml",
  45. },
  46. wantErr: fmt.Errorf("problem loading config: error parsing regexp: missing argument to repetition operator: `??`"),
  47. },
  48. {
  49. description: "test bad global file regex",
  50. opts: options.Options{
  51. Config: "../test_data/test_configs/bad_aws_key_file_regex.toml",
  52. },
  53. wantErr: fmt.Errorf("problem loading config: error parsing regexp: missing argument to repetition operator: `??`"),
  54. },
  55. {
  56. description: "test bad global message regex",
  57. opts: options.Options{
  58. Config: "../test_data/test_configs/bad_aws_key_message_regex.toml",
  59. },
  60. wantErr: fmt.Errorf("problem loading config: error parsing regexp: missing argument to repetition operator: `??`"),
  61. },
  62. {
  63. description: "test successful load big ol thing",
  64. opts: options.Options{
  65. Config: "../test_data/test_configs/large.toml",
  66. },
  67. },
  68. {
  69. description: "test load entropy",
  70. opts: options.Options{
  71. Config: "../test_data/test_configs/entropy.toml",
  72. },
  73. },
  74. {
  75. description: "test entropy bad range",
  76. opts: options.Options{
  77. Config: "../test_data/test_configs/bad_entropy_1.toml",
  78. },
  79. wantErr: fmt.Errorf("entropy range must be ascending"),
  80. },
  81. {
  82. description: "test entropy value p2",
  83. opts: options.Options{
  84. Config: "../test_data/test_configs/bad_entropy_2.toml",
  85. },
  86. wantErr: fmt.Errorf("strconv.ParseFloat: parsing \"x\": invalid syntax"),
  87. },
  88. {
  89. description: "test entropy value p1",
  90. opts: options.Options{
  91. Config: "../test_data/test_configs/bad_entropy_3.toml",
  92. },
  93. wantErr: fmt.Errorf("strconv.ParseFloat: parsing \"x\": invalid syntax"),
  94. },
  95. {
  96. description: "test entropy value p1",
  97. opts: options.Options{
  98. Config: "../test_data/test_configs/bad_entropy_4.toml",
  99. },
  100. wantErr: fmt.Errorf("invalid entropy ranges, must be within 0.0-8.0"),
  101. },
  102. }
  103. for _, test := range tests {
  104. _, err := NewConfig(test.opts)
  105. if err != nil {
  106. if test.wantErr == nil {
  107. t.Error(err)
  108. } else if test.wantErr.Error() != err.Error() {
  109. t.Errorf("expected err: %s, got %s", test.wantErr, err)
  110. }
  111. }
  112. }
  113. }