@@ -17,8 +17,12 @@ limitations under the License.
17
17
package allowdenylist
18
18
19
19
import (
20
- "regexp"
20
+ "fmt"
21
+ "strings"
21
22
"testing"
23
+ "time"
24
+
25
+ regexp "github.com/dlclark/regexp2"
22
26
)
23
27
24
28
func TestNew (t * testing.T ) {
@@ -76,7 +80,11 @@ func TestInclude(t *testing.T) {
76
80
t .Fatal ("expected Parse() to not fail" )
77
81
}
78
82
79
- if ! allowlist .IsIncluded ("item1" ) {
83
+ isIncluded , err := allowlist .IsIncluded ("item1" )
84
+ if err != nil {
85
+ t .Fatal ("expected IsIncluded() to not fail" )
86
+ }
87
+ if ! isIncluded {
80
88
t .Fatal ("expected included item to be included" )
81
89
}
82
90
})
@@ -93,7 +101,11 @@ func TestInclude(t *testing.T) {
93
101
t .Fatalf ("expected Parse() to not fail, but got error : %v" , err )
94
102
}
95
103
96
- if ! denylist .IsIncluded (item1 ) {
104
+ isIncluded , err := denylist .IsIncluded (item1 )
105
+ if err != nil {
106
+ t .Fatal ("expected IsIncluded() to not fail" )
107
+ }
108
+ if ! isIncluded {
97
109
t .Fatal ("expected included item to be included" )
98
110
}
99
111
})
@@ -109,7 +121,11 @@ func TestInclude(t *testing.T) {
109
121
t .Fatalf ("expected Parse() to not fail, but got error : %v" , err )
110
122
}
111
123
112
- if ! allowlist .IsIncluded ("kube_secret_info" ) {
124
+ isIncluded , err := allowlist .IsIncluded ("kube_secret_info" )
125
+ if err != nil {
126
+ t .Fatal ("expected IsIncluded() to not fail" )
127
+ }
128
+ if ! isIncluded {
113
129
t .Fatal ("expected included item to be included" )
114
130
}
115
131
})
@@ -130,16 +146,32 @@ func TestInclude(t *testing.T) {
130
146
t .Fatalf ("expected Parse() to not fail, but got error : %v" , err )
131
147
}
132
148
133
- if denylist .IsExcluded (item1 ) {
149
+ isExcluded , err := denylist .IsExcluded (item1 )
150
+ if err != nil {
151
+ t .Fatal ("expected IsExcluded() to not fail" )
152
+ }
153
+ if isExcluded {
134
154
t .Fatalf ("expected included %s to be included" , item1 )
135
155
}
136
- if denylist .IsIncluded (item2 ) {
156
+ isIncluded , err := denylist .IsIncluded (item2 )
157
+ if err != nil {
158
+ t .Fatal ("expected IsIncluded() to not fail" )
159
+ }
160
+ if isIncluded {
137
161
t .Fatalf ("expected included %s to be excluded" , item2 )
138
162
}
139
- if denylist .IsIncluded (item3 ) {
163
+ isIncluded , err = denylist .IsIncluded (item3 )
164
+ if err != nil {
165
+ t .Fatal ("expected IsIncluded() to not fail" )
166
+ }
167
+ if isIncluded {
140
168
t .Fatalf ("expected included %s to be excluded" , item3 )
141
169
}
142
- if denylist .IsExcluded (item4 ) {
170
+ isExcluded , err = denylist .IsExcluded (item4 )
171
+ if err != nil {
172
+ t .Fatal ("expected IsExcluded() to not fail" )
173
+ }
174
+ if isExcluded {
143
175
t .Fatalf ("expected included %s to be included" , item4 )
144
176
}
145
177
})
@@ -159,7 +191,11 @@ func TestExclude(t *testing.T) {
159
191
t .Fatalf ("expected Parse() to not fail, but got error : %v" , err )
160
192
}
161
193
162
- if allowlist .IsIncluded (item1 ) {
194
+ isIncluded , err := allowlist .IsIncluded (item1 )
195
+ if err != nil {
196
+ t .Fatal ("expected IsIncluded() to not fail" )
197
+ }
198
+ if isIncluded {
163
199
t .Fatal ("expected excluded item to be excluded" )
164
200
}
165
201
})
@@ -176,7 +212,11 @@ func TestExclude(t *testing.T) {
176
212
t .Fatalf ("expected Parse() to not fail, but got error : %v" , err )
177
213
}
178
214
179
- if denylist .IsIncluded (item1 ) {
215
+ isIncluded , err := denylist .IsIncluded (item1 )
216
+ if err != nil {
217
+ t .Fatal ("expected IsIncluded() to not fail" )
218
+ }
219
+ if isIncluded {
180
220
t .Fatal ("expected excluded item to be excluded" )
181
221
}
182
222
})
@@ -224,7 +264,8 @@ func TestStatus(t *testing.T) {
224
264
allowlist , _ := New (map [string ]struct {}{item1 : {}, item2 : {}}, map [string ]struct {}{})
225
265
actualStatusString := allowlist .Status ()
226
266
expectedRegexPattern := `^Including the following lists that were on allowlist: (item1|item2), (item2|item1)$`
227
- matched , _ := regexp .MatchString (expectedRegexPattern , actualStatusString )
267
+ re := regexp .MustCompile (expectedRegexPattern , regexpDefaultSpec )
268
+ matched , _ := re .MatchString (actualStatusString )
228
269
if ! matched {
229
270
t .Errorf ("expected status %q but got %q" , expectedRegexPattern , actualStatusString )
230
271
}
@@ -244,9 +285,38 @@ func TestStatus(t *testing.T) {
244
285
denylist , _ := New (map [string ]struct {}{}, map [string ]struct {}{item1 : {}, item2 : {}})
245
286
actualStatusString := denylist .Status ()
246
287
expectedRegexPattern := `^Excluding the following lists that were on denylist: (item1|item2), (item2|item1)$`
247
- matched , _ := regexp .MatchString (expectedRegexPattern , actualStatusString )
288
+ re := regexp .MustCompile (expectedRegexPattern , regexpDefaultSpec )
289
+ matched , _ := re .MatchString (actualStatusString )
248
290
if ! matched {
249
291
t .Errorf ("expected status %q but got %q" , expectedRegexPattern , actualStatusString )
250
292
}
251
293
})
252
294
}
295
+
296
+ func TestCatastrophicBacktrackTimeout (t * testing.T ) {
297
+ r , err := regexp .Compile ("(.+)*\\ ?" , 0 )
298
+ if err != nil {
299
+ t .Fatal (err )
300
+ }
301
+ var exp = "Lorem ipsum dolor sit amet, consectetur adipiscing elit"
302
+ exp = strings .Repeat (exp , 2 ^ 10 )
303
+
304
+ timeout := regexp .DefaultMatchTimeout
305
+ t .Logf ("regexp.DefaultMatchTimeout set to: %v" , timeout )
306
+ buffer := 500 * time .Millisecond
307
+ t .Run (fmt .Sprint (timeout ), func (t * testing.T ) {
308
+ r .MatchTimeout = timeout
309
+ start := time .Now ()
310
+ _ , err = r .FindStringMatch (exp )
311
+ if err != nil && ! strings .HasPrefix (err .Error (), "match timeout" ) {
312
+ t .Fatal (err )
313
+ }
314
+ if err == nil {
315
+ t .Fatal ("expected catastrophic backtracking error" )
316
+ }
317
+ elapsed := time .Since (start )
318
+ if elapsed > timeout + buffer {
319
+ t .Fatalf ("timeout %v exceeded: %v" , timeout , elapsed )
320
+ }
321
+ })
322
+ }
0 commit comments