allowlist_test.go 3.4 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156
  1. package config
  2. import (
  3. "errors"
  4. "testing"
  5. "github.com/google/go-cmp/cmp"
  6. "github.com/google/go-cmp/cmp/cmpopts"
  7. "github.com/stretchr/testify/assert"
  8. "github.com/zricethezav/gitleaks/v8/regexp"
  9. )
  10. func TestCommitAllowed(t *testing.T) {
  11. tests := []struct {
  12. allowlist Allowlist
  13. commit string
  14. commitAllowed bool
  15. }{
  16. {
  17. allowlist: Allowlist{
  18. Commits: []string{"commitA"},
  19. },
  20. commit: "commitA",
  21. commitAllowed: true,
  22. },
  23. {
  24. allowlist: Allowlist{
  25. Commits: []string{"commitB"},
  26. },
  27. commit: "commitA",
  28. commitAllowed: false,
  29. },
  30. {
  31. allowlist: Allowlist{
  32. Commits: []string{"commitB"},
  33. },
  34. commit: "",
  35. commitAllowed: false,
  36. },
  37. }
  38. for _, tt := range tests {
  39. assert.Equal(t, tt.commitAllowed, tt.allowlist.CommitAllowed(tt.commit))
  40. }
  41. }
  42. func TestRegexAllowed(t *testing.T) {
  43. tests := []struct {
  44. allowlist Allowlist
  45. secret string
  46. regexAllowed bool
  47. }{
  48. {
  49. allowlist: Allowlist{
  50. Regexes: []*regexp.Regexp{regexp.MustCompile("matchthis")},
  51. },
  52. secret: "a secret: matchthis, done",
  53. regexAllowed: true,
  54. },
  55. {
  56. allowlist: Allowlist{
  57. Regexes: []*regexp.Regexp{regexp.MustCompile("matchthis")},
  58. },
  59. secret: "a secret",
  60. regexAllowed: false,
  61. },
  62. }
  63. for _, tt := range tests {
  64. assert.Equal(t, tt.regexAllowed, tt.allowlist.RegexAllowed(tt.secret))
  65. }
  66. }
  67. func TestPathAllowed(t *testing.T) {
  68. tests := []struct {
  69. allowlist Allowlist
  70. path string
  71. pathAllowed bool
  72. }{
  73. {
  74. allowlist: Allowlist{
  75. Paths: []*regexp.Regexp{regexp.MustCompile("path")},
  76. },
  77. path: "a path",
  78. pathAllowed: true,
  79. },
  80. {
  81. allowlist: Allowlist{
  82. Paths: []*regexp.Regexp{regexp.MustCompile("path")},
  83. },
  84. path: "a ???",
  85. pathAllowed: false,
  86. },
  87. }
  88. for _, tt := range tests {
  89. assert.Equal(t, tt.pathAllowed, tt.allowlist.PathAllowed(tt.path))
  90. }
  91. }
  92. func TestValidate(t *testing.T) {
  93. tests := map[string]struct {
  94. input Allowlist
  95. expected Allowlist
  96. wantErr error
  97. }{
  98. "empty conditions": {
  99. input: Allowlist{},
  100. wantErr: errors.New("[[rules.allowlists]] must contain at least one check for: commits, paths, regexes, or stopwords"),
  101. },
  102. "deduplicated commits and stopwords": {
  103. input: Allowlist{
  104. Commits: []string{"commitA", "commitB", "commitA"},
  105. StopWords: []string{"stopwordA", "stopwordB", "stopwordA"},
  106. },
  107. expected: Allowlist{
  108. Commits: []string{"commitA", "commitB"},
  109. StopWords: []string{"stopwordA", "stopwordB"},
  110. },
  111. },
  112. }
  113. for _, tt := range tests {
  114. // Expected an error.
  115. err := tt.input.Validate()
  116. if err != nil {
  117. if tt.wantErr == nil {
  118. t.Fatalf("Received unexpected error: %v", err)
  119. } else if !assert.EqualError(t, err, tt.wantErr.Error()) {
  120. t.Fatalf("Received unexpected error, expected '%v', got '%v'", tt.wantErr, err)
  121. }
  122. } else {
  123. if tt.wantErr != nil {
  124. t.Fatalf("Did not receive expected error: %v", tt.wantErr)
  125. }
  126. }
  127. var (
  128. regexComparer = func(x, y *regexp.Regexp) bool {
  129. // Compare the string representation of the regex patterns.
  130. if x == nil || y == nil {
  131. return x == y
  132. }
  133. return x.String() == y.String()
  134. }
  135. arrayComparer = func(a, b string) bool {
  136. return a < b
  137. }
  138. opts = cmp.Options{
  139. cmp.Comparer(regexComparer),
  140. cmpopts.SortSlices(arrayComparer),
  141. }
  142. )
  143. if diff := cmp.Diff(tt.input, tt.expected, opts); diff != "" {
  144. t.Errorf("diff: (-want +got)\n%s", diff)
  145. }
  146. }
  147. }