allowlist_test.go 3.4 KB

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