@@ -17,8 +17,12 @@ limitations under the License.
1717package allowdenylist
1818
1919import (
20- "regexp"
20+ "fmt"
21+ "strings"
2122 "testing"
23+ "time"
24+
25+ regexp "github.com/dlclark/regexp2"
2226)
2327
2428func TestNew (t * testing.T ) {
@@ -76,7 +80,11 @@ func TestInclude(t *testing.T) {
7680 t .Fatal ("expected Parse() to not fail" )
7781 }
7882
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 {
8088 t .Fatal ("expected included item to be included" )
8189 }
8290 })
@@ -93,7 +101,11 @@ func TestInclude(t *testing.T) {
93101 t .Fatalf ("expected Parse() to not fail, but got error : %v" , err )
94102 }
95103
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 {
97109 t .Fatal ("expected included item to be included" )
98110 }
99111 })
@@ -109,7 +121,11 @@ func TestInclude(t *testing.T) {
109121 t .Fatalf ("expected Parse() to not fail, but got error : %v" , err )
110122 }
111123
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 {
113129 t .Fatal ("expected included item to be included" )
114130 }
115131 })
@@ -130,16 +146,32 @@ func TestInclude(t *testing.T) {
130146 t .Fatalf ("expected Parse() to not fail, but got error : %v" , err )
131147 }
132148
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 {
134154 t .Fatalf ("expected included %s to be included" , item1 )
135155 }
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 {
137161 t .Fatalf ("expected included %s to be excluded" , item2 )
138162 }
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 {
140168 t .Fatalf ("expected included %s to be excluded" , item3 )
141169 }
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 {
143175 t .Fatalf ("expected included %s to be included" , item4 )
144176 }
145177 })
@@ -159,7 +191,11 @@ func TestExclude(t *testing.T) {
159191 t .Fatalf ("expected Parse() to not fail, but got error : %v" , err )
160192 }
161193
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 {
163199 t .Fatal ("expected excluded item to be excluded" )
164200 }
165201 })
@@ -176,7 +212,11 @@ func TestExclude(t *testing.T) {
176212 t .Fatalf ("expected Parse() to not fail, but got error : %v" , err )
177213 }
178214
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 {
180220 t .Fatal ("expected excluded item to be excluded" )
181221 }
182222 })
@@ -224,7 +264,8 @@ func TestStatus(t *testing.T) {
224264 allowlist , _ := New (map [string ]struct {}{item1 : {}, item2 : {}}, map [string ]struct {}{})
225265 actualStatusString := allowlist .Status ()
226266 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 )
228269 if ! matched {
229270 t .Errorf ("expected status %q but got %q" , expectedRegexPattern , actualStatusString )
230271 }
@@ -244,9 +285,34 @@ func TestStatus(t *testing.T) {
244285 denylist , _ := New (map [string ]struct {}{}, map [string ]struct {}{item1 : {}, item2 : {}})
245286 actualStatusString := denylist .Status ()
246287 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 )
248290 if ! matched {
249291 t .Errorf ("expected status %q but got %q" , expectedRegexPattern , actualStatusString )
250292 }
251293 })
252294}
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 := time .Second
305+ buffer := 500 * time .Millisecond
306+ t .Run (fmt .Sprint (timeout ), func (t * testing.T ) {
307+ r .MatchTimeout = timeout
308+ start := time .Now ()
309+ _ , err = r .FindStringMatch (exp )
310+ if err != nil && ! strings .HasPrefix (err .Error (), "match timeout" ) {
311+ t .Fatal (err )
312+ }
313+ elapsed := time .Since (start )
314+ if elapsed > timeout + buffer {
315+ t .Fatalf ("timeout %v exceeded: %v" , timeout , elapsed )
316+ }
317+ })
318+ }
0 commit comments