-
Notifications
You must be signed in to change notification settings - Fork 20
/
generic_machine_test.go
207 lines (188 loc) · 4.24 KB
/
generic_machine_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
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
package quamina
import (
"fmt"
"os"
"testing"
)
/* This test adopted, with thanks, from aws/event-ruler */
func TestRulerArraysBug(t *testing.T) {
event := "{\n" +
" \"requestContext\": { \"obfuscatedCustomerId\": \"AIDACKCEVSQ6C2EXAMPLE\" },\n" +
" \"hypotheses\": [\n" +
" { \"isBluePrint\": true, \"creator\": \"A123\" },\n" +
" { \"isBluePrint\": false, \"creator\": \"A234\" }\n" +
" ]\n" +
"}"
r1 := "{\n" +
" \"hypotheses\": {\n" +
" \"isBluePrint\": [ false ],\n" +
" \"creator\": [ \"A123\" ]\n" +
" }\n" +
"}"
r2 := "{\n" +
" \"hypotheses\": {\n" +
" \"isBluePrint\": [ true ],\n" +
" \"creator\": [ \"A234\" ]\n" +
" }\n" +
"}"
q, _ := New()
err := q.AddPattern("r1", r1)
if err != nil {
t.Error("add r1")
}
err = q.AddPattern("r2", r2)
if err != nil {
t.Error("add r2")
}
matches, err := q.MatchesForEvent([]byte(event))
if err != nil {
t.Errorf("MatchesForEvent: %s", err)
}
if len(matches) != 0 {
t.Error("Nonzero matches")
}
}
func readTestData(t *testing.T, fname string) []byte {
t.Helper()
bytes, err := os.ReadFile("testdata/" + fname)
if err != nil {
t.Error("couldn't read: " + fname + ": " + err.Error())
}
return bytes
}
func TestRulerNestedArrays(t *testing.T) {
event1 := readTestData(t, "arrayEvent1.json")
event2 := readTestData(t, "arrayEvent2.json")
event3 := readTestData(t, "arrayEvent3.json")
event4 := readTestData(t, "arrayEvent4.json")
rule1 := string(readTestData(t, "arrayRule1.json"))
rule2 := string(readTestData(t, "arrayRule2.json"))
rule3 := string(readTestData(t, "arrayRule3.json"))
q, _ := New()
for i, rule := range []string{rule1, rule2, rule3} {
err := q.AddPattern(fmt.Sprintf("rule%d", i+1), rule)
if err != nil {
t.Errorf("add rule%d", i)
}
}
r1, err := q.MatchesForEvent(event1)
if err != nil {
t.Error("Matches " + err.Error())
}
if len(r1) != 2 {
t.Errorf("r1 len %d", len(r1))
}
r2, err := q.MatchesForEvent(event2)
if err != nil {
t.Error("Matches " + err.Error())
}
if len(r2) != 0 {
t.Errorf("r2 matchd %d", len(r2))
}
r3, err := q.MatchesForEvent(event3)
if err != nil {
t.Error("Matches " + err.Error())
}
if len(r3) != 0 {
t.Errorf("r3 matchd %d", len(r2))
}
r4, err := q.MatchesForEvent(event4)
if err != nil {
t.Error("Matches " + err.Error())
}
if len(r4) != 1 || r4[0] != "rule3" {
var msg string
if len(r4) == 1 {
msg += "match: " + r4[0].(string)
} else {
msg = fmt.Sprintf("r4 matches %d", len(r4))
}
t.Error(msg)
}
}
func TestRulerSimplestPossibleMachine(t *testing.T) {
rule1 := "{ \"a\" : [ 1 ] }"
rule2 := "{ \"b\" : [ 2 ] }"
rule3 := "{ \"c\" : [ 3 ] }"
q, _ := New()
_ = q.AddPattern("r1", rule1)
_ = q.AddPattern("r2", rule2)
_ = q.AddPattern("r3", rule3)
event1 := "{ \"a\" : 1 }"
event2 := "{ \"b\" : 2 }"
event4 := "{ \"x\" : true }"
event5 := "{ \"a\" : 1, \"b\": 2, \"c\" : 3 }"
var val []X
var err error
val, err = q.MatchesForEvent([]byte(event1))
if err != nil {
t.Error("e1: " + err.Error())
}
if len(val) != 1 || val[0] != "r1" {
t.Error("event1 fail")
}
val, err = q.MatchesForEvent([]byte(event2))
if err != nil {
t.Error("e2: " + err.Error())
}
if len(val) != 1 || val[0] != "r2" {
t.Error("event2 fail")
}
val, err = q.MatchesForEvent([]byte(event4))
if err != nil {
t.Error("e2: " + err.Error())
}
if len(val) != 0 {
t.Error("event4 fail")
}
val, err = q.MatchesForEvent([]byte(event5))
if err != nil {
t.Error("e2: " + err.Error())
}
if len(val) != 3 {
t.Error("event4 fail")
}
matched := 0
for _, v := range val {
if v == "r1" || v == "r2" || v == "r3" {
matched++
}
}
if matched != 3 {
t.Error("missing match")
}
}
func TestRulerEmptyInput(t *testing.T) {
rule1 := `{
"detail": {
"c-count": [
{
"exists": false
}
]
},
"d-count": [
{
"exists": false
}
],
"e-count": [
{
"exists": false
}
]
}`
event := "{}"
q, _ := New()
err := q.AddPattern("r", rule1)
if err != nil {
t.Error("Empty input add pattern" + err.Error())
}
matches, err := q.MatchesForEvent([]byte(event))
if err != nil {
t.Error("Empty input matches: " + err.Error())
}
if len(matches) != 1 || matches[0] != "r" {
t.Error("Empty input match botch")
}
}