-
Notifications
You must be signed in to change notification settings - Fork 17
/
tree_test.go
119 lines (101 loc) · 3.27 KB
/
tree_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
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
package sigma
import (
"encoding/json"
"testing"
"github.com/markuskont/datamodels"
"gopkg.in/yaml.v2"
)
func TestTreeParse(t *testing.T) {
for _, c := range parseTestCases {
var rule Rule
if err := yaml.Unmarshal([]byte(c.Rule), &rule); err != nil {
t.Fatalf("tree parse case %d failed to unmarshal yaml, %s", c.ID, err)
}
p, err := NewTree(RuleHandle{Rule: rule, NoCollapseWS: c.noCollapseWSNeg})
if err != nil {
t.Fatalf("tree parse case %d failed: %s", c.ID, err)
}
// Positive cases
for i, c2 := range c.Pos {
var obj datamodels.Map
if err := json.Unmarshal([]byte(c2), &obj); err != nil {
t.Fatalf("rule parser case %d positive case %d json unmarshal error %s", c.ID, i, err)
}
m, _ := p.Match(obj)
if !m {
t.Fatalf("rule parser case %d positive case %d did not match", c.ID, i)
}
}
// Negative cases
for i, c2 := range c.Neg {
var obj datamodels.Map
if err := json.Unmarshal([]byte(c2), &obj); err != nil {
t.Fatalf("rule parser case %d positive case %d json unmarshal error %s", c.ID, i, err)
}
m, _ := p.Match(obj)
if m {
t.Fatalf("rule parser case %d negative case %d matched", c.ID, i)
}
}
}
}
// we should probably add an alternative to this benchmark to include noCollapseWS on or off (we collapse by default now)
func benchmarkCase(b *testing.B, rawRule, rawEvent string) {
var rule Rule
if err := yaml.Unmarshal([]byte(parseTestCases[0].Rule), &rule); err != nil {
b.Fail()
}
p, err := NewTree(RuleHandle{Rule: rule})
if err != nil {
b.Fail()
}
var event datamodels.Map
if err := json.Unmarshal([]byte(parseTestCases[0].Pos[0]), &event); err != nil {
b.Fail()
}
for i := 0; i < b.N; i++ {
p.Match(event)
}
}
func BenchmarkTreePositive0(b *testing.B) {
benchmarkCase(b, parseTestCases[0].Rule, parseTestCases[0].Pos[0])
}
func BenchmarkTreePositive1(b *testing.B) {
benchmarkCase(b, parseTestCases[1].Rule, parseTestCases[1].Pos[0])
}
func BenchmarkTreePositive2(b *testing.B) {
benchmarkCase(b, parseTestCases[2].Rule, parseTestCases[2].Pos[0])
}
func BenchmarkTreePositive3(b *testing.B) {
benchmarkCase(b, parseTestCases[3].Rule, parseTestCases[3].Pos[0])
}
func BenchmarkTreePositive4(b *testing.B) {
benchmarkCase(b, parseTestCases[4].Rule, parseTestCases[4].Pos[0])
}
func BenchmarkTreePositive5(b *testing.B) {
benchmarkCase(b, parseTestCases[5].Rule, parseTestCases[6].Pos[0])
}
func BenchmarkTreePositive6(b *testing.B) {
benchmarkCase(b, parseTestCases[6].Rule, parseTestCases[6].Pos[0])
}
func BenchmarkTreeNegative0(b *testing.B) {
benchmarkCase(b, parseTestCases[0].Rule, parseTestCases[0].Neg[0])
}
func BenchmarkTreeNegative1(b *testing.B) {
benchmarkCase(b, parseTestCases[1].Rule, parseTestCases[1].Neg[0])
}
func BenchmarkTreeNegative2(b *testing.B) {
benchmarkCase(b, parseTestCases[2].Rule, parseTestCases[2].Neg[0])
}
func BenchmarkTreeNegative3(b *testing.B) {
benchmarkCase(b, parseTestCases[3].Rule, parseTestCases[3].Neg[0])
}
func BenchmarkTreeNegative4(b *testing.B) {
benchmarkCase(b, parseTestCases[4].Rule, parseTestCases[4].Neg[0])
}
func BenchmarkTreeNegative5(b *testing.B) {
benchmarkCase(b, parseTestCases[5].Rule, parseTestCases[6].Neg[0])
}
func BenchmarkTreeNegative6(b *testing.B) {
benchmarkCase(b, parseTestCases[6].Rule, parseTestCases[6].Neg[0])
}