-
Notifications
You must be signed in to change notification settings - Fork 2
/
Copy pathrx_test.go
92 lines (87 loc) · 1.77 KB
/
rx_test.go
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
// Copyright 2022 Juan Pablo Tosso and the OWASP Coraza contributors
// SPDX-License-Identifier: Apache-2.0
package wasilibs
import (
"fmt"
"testing"
"github.com/corazawaf/coraza/v3"
"github.com/corazawaf/coraza/v3/experimental/plugins/plugintypes"
)
func TestRx(t *testing.T) {
tests := []struct {
pattern string
input string
want bool
}{
{
pattern: "som(.*)ta",
input: "somedata",
want: true,
},
{
pattern: "som(.*)ta",
input: "notdata",
want: false,
},
{
pattern: "ハロー",
input: "ハローワールド",
want: true,
},
{
pattern: "ハロー",
input: "グッバイワールド",
want: false,
},
{
pattern: `\xac\xed\x00\x05`,
input: "\xac\xed\x00\x05t\x00\x04test",
want: true,
},
{
pattern: `\xac\xed\x00\x05`,
input: "\xac\xed\x00t\x00\x04test",
want: false,
},
{
// Requires dotall
pattern: `hello.*world`,
input: "hello\nworld",
want: true,
},
{
// Requires multiline
pattern: `^hello.*world`,
input: "test\nhello\nworld",
want: true,
},
{
// Makes sure, (?sm) passed by the user does not
// break the regex.
pattern: `(?sm)hello.*world`,
input: "hello\nworld",
want: true,
},
}
for _, tc := range tests {
tt := tc
t.Run(fmt.Sprintf("%s/%s", tt.pattern, tt.input), func(t *testing.T) {
opts := plugintypes.OperatorOptions{
Arguments: tt.pattern,
}
rx, err := newRX(opts)
if err != nil {
t.Fatal(err)
}
waf, err := coraza.NewWAF(coraza.NewWAFConfig())
if err != nil {
t.Error(err)
}
tx := waf.NewTransaction()
res := rx.Evaluate(tx.(plugintypes.TransactionState), tt.input)
if res != tt.want {
t.Errorf("want %v, got %v", tt.want, res)
}
})
}
}