-
Notifications
You must be signed in to change notification settings - Fork 20
/
value_matcher_test.go
421 lines (391 loc) · 10.2 KB
/
value_matcher_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
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
307
308
309
310
311
312
313
314
315
316
317
318
319
320
321
322
323
324
325
326
327
328
329
330
331
332
333
334
335
336
337
338
339
340
341
342
343
344
345
346
347
348
349
350
351
352
353
354
355
356
357
358
359
360
361
362
363
364
365
366
367
368
369
370
371
372
373
374
375
376
377
378
379
380
381
382
383
384
385
386
387
388
389
390
391
392
393
394
395
396
397
398
399
400
401
402
403
404
405
406
407
408
409
410
411
412
413
414
415
416
417
418
419
420
421
package quamina
import (
"fmt"
"math/rand"
"strings"
"testing"
)
func TestInvalidValueTypes(t *testing.T) {
var before []typedVal
addInvalid(t, before)
before = append(before, typedVal{vType: stringType, val: "foo"})
addInvalid(t, before)
before = append(before, typedVal{vType: stringType, val: "bar"})
addInvalid(t, before)
}
func addInvalid(t *testing.T, before []typedVal) {
t.Helper()
defer func() {
if recover() == nil {
t.Errorf("TestAddInvalidTransition should have panicked")
}
}()
panicType := valType(999)
// empty value matcher
m := newValueMatcher()
invalidField := typedVal{
vType: panicType,
val: "one",
}
for _, addBefore := range before {
m.addTransition(addBefore, &nullPrinter{})
}
m.addTransition(invalidField, &nullPrinter{})
}
func TestNoOpTransition(t *testing.T) {
vm := newValueMatcher()
tr := vm.transitionOn(&Field{Val: []byte("foo")}, &bufpair{})
if len(tr) != 0 {
t.Error("matched on empty valuematcher")
}
}
func TestAddTransition(t *testing.T) {
m := newValueMatcher()
v1 := typedVal{
vType: stringType,
val: "one",
}
t1 := m.addTransition(v1, &nullPrinter{})
if t1 == nil {
t.Error("nil addTrans")
}
t1x := m.transitionOn(&Field{Val: []byte("one")}, &bufpair{})
if len(t1x) != 1 || t1x[0] != t1 {
t.Error("Retrieve failed")
}
tXtra := m.addTransition(v1, &nullPrinter{})
if tXtra != t1 {
t.Error("dupe trans missed")
}
v2 := typedVal{
vType: stringType,
val: "two",
}
t2 := m.addTransition(v2, &nullPrinter{})
t2x := m.transitionOn(&Field{Val: []byte("two")}, &bufpair{})
if len(t2x) != 1 || t2x[0] != t2 {
t.Error("trans failed T2")
}
t1x = m.transitionOn(&Field{Val: []byte("one")}, &bufpair{})
if len(t1x) != 1 || t1x[0] != t1 {
t.Error("Retrieve failed")
}
v3 := typedVal{
vType: stringType,
val: "three",
}
t3 := m.addTransition(v3, &nullPrinter{})
t3x := m.transitionOn(&Field{Val: []byte("three")}, &bufpair{})
if len(t3x) != 1 || t3x[0] != t3 {
t.Error("Match failed T3")
}
t2x = m.transitionOn(&Field{Val: []byte("two")}, &bufpair{})
if len(t2x) != 1 || t2x[0] != t2 {
t.Error("trans failed T2")
}
t1x = m.transitionOn(&Field{Val: []byte("one")}, &bufpair{})
if len(t1x) != 1 || t1x[0] != t1 {
t.Error("Retrieve failed")
}
}
func TestMultiTransitions(t *testing.T) {
patX := `{"foo": [ { "shellstyle": "*x*b" } ]}`
patY := `{"foo": [ { "shellstyle": "*y*b" } ]}`
m := newCoreMatcher()
if m.addPattern("X", patX) != nil {
t.Error("add patX")
}
if m.addPattern("Y", patY) != nil {
t.Error("add patY")
}
e := `{"foo": "axyb"}`
matches, err := m.matchesForJSONEvent([]byte(e))
if err != nil {
t.Error("m4: " + err.Error())
}
if len(matches) != 2 {
t.Error("just one")
}
}
func TestAY(t *testing.T) {
m := newCoreMatcher()
pat := `{"x": [ { "shellstyle": "*ay*"} ] }`
err := m.addPattern("AY", pat)
if err != nil {
t.Error("AY: " + err.Error())
}
shouldMatch := []string{"ay", "aay", "aaaayyyyy", "xyzay", "ayxxxx"}
e := `{"x": "X"}`
for _, sm := range shouldMatch {
p := strings.ReplaceAll(e, "X", sm)
matches, err := m.matchesForJSONEvent([]byte(p))
if err != nil {
t.Error("bad JSON: " + err.Error())
}
if len(matches) != 1 || matches[0] != "AY" {
t.Errorf("%s didn't match", sm)
}
}
}
func TestOverlappingValues(t *testing.T) {
m := newCoreMatcher()
p1 := `{"a": ["foo"]}`
p2 := `{"a": ["football"]}`
p3 := `{"a": ["footballer"]}`
var err error
var wantP1 X = "p1"
err = m.addPattern(wantP1, p1)
if err != nil {
t.Error("Ouch p1")
}
var wantP2 X = "p2"
err = m.addPattern(wantP2, p2)
if err != nil {
t.Error("Ouch p2")
}
var wantP3 X = "p3"
err = m.addPattern(wantP3, p3)
if err != nil {
t.Error("Ouch p3")
}
e1 := `{"x": 3, "a": "foo"}`
e2 := `{"x": 3, "a": "football"}`
e3 := `{"x": 3, "a": "footballer"}`
matches, err := m.matchesForJSONEvent([]byte(e1))
if err != nil {
t.Error("Error on e1: " + err.Error())
}
if len(matches) != 1 {
t.Errorf("bad len %d", len(matches))
} else if matches[0] != wantP1 {
t.Errorf("Failure on e1 - want %v got %v", wantP1, matches[0])
}
matches, err = m.matchesForJSONEvent([]byte(e2))
if err != nil {
t.Error("Error on e2: " + err.Error())
}
if len(matches) != 1 || matches[0] != wantP2 {
t.Error("Failure on e2")
}
matches, err = m.matchesForJSONEvent([]byte(e3))
if err != nil {
t.Error("Error on e3: " + err.Error())
}
if len(matches) != 1 || matches[0] != wantP3 {
t.Error("Failure on e3")
}
}
func TestFuzzValueMatcher(t *testing.T) {
source := rand.NewSource(98543)
m := newCoreMatcher()
var pNames []X
bytes := "abcdefghijklmnopqrstuvwxyz"
lb := int64(len(bytes))
strLen := 12
used := make(map[X]bool)
// make ten thousand 12-char strings, randomized
for i := 0; i < 10000; i++ {
str := ""
for j := 0; j < strLen; j++ {
//nolint:gosec
ch := bytes[source.Int63()%lb]
str += string([]byte{ch})
}
pNames = append(pNames, str)
used[str] = true
}
// add a pattern for each string
pBase := `{"a": [ "999" ]}`
for _, pName := range pNames {
err := m.addPattern(pName, strings.ReplaceAll(pBase, "999", pName.(string)))
if err != nil {
t.Errorf("addPattern %s: %s", pName, err.Error())
}
}
// make sure all the patterns match
eBase := `{"a": "999"}`
for _, pName := range pNames {
event := strings.ReplaceAll(eBase, "999", pName.(string))
matches, err := m.matchesForJSONEvent([]byte(event))
if err != nil {
t.Errorf("m4J botch on %s: %s", event, err.Error())
}
if len(matches) != 1 {
t.Errorf("M=%d for %s", len(matches), pName)
} else {
if matches[0] != pName {
t.Errorf("wanted %s got %s", pName, matches[0])
}
}
}
// now let's run 1000 more random strings that shouldn't match
shouldNot := 0
for shouldNot < len(pNames) {
str := ""
for j := 0; j < strLen; j++ {
//nolint:gosec
ch := bytes[source.Int63()%lb]
str += string([]byte{ch})
}
_, ok := used[str]
if ok {
continue
}
shouldNot++
event := strings.ReplaceAll(eBase, "999", str)
matches, err := m.matchesForJSONEvent([]byte(event))
if err != nil {
t.Errorf("shouldNot botch on %s: %s", event, err.Error())
}
if len(matches) != 0 {
t.Errorf("OUCH %d matches to %s", len(matches), str)
}
}
}
func TestFuzzWithNumbers(t *testing.T) {
source := rand.NewSource(98543)
m := newCoreMatcher()
var pNames []X
used := make(map[X]bool)
// make ten thousand random numbers between 1 and 100K. There are probably dupes?
for i := 0; i < 10000; i++ {
//nolint:gosec
n := source.Int63()
ns := fmt.Sprintf("%d", n)
pNames = append(pNames, ns)
used[ns] = true
}
// add a pattern for each number
pBase := `{"a": [ 999 ]}`
for _, pName := range pNames {
err := m.addPattern(pName, strings.ReplaceAll(pBase, "999", pName.(string)))
if err != nil {
t.Errorf("addPattern %s: %s", pName, err.Error())
}
}
// make sure all the patterns match
eBase := `{"a": 999}`
for _, pName := range pNames {
event := strings.ReplaceAll(eBase, "999", pName.(string))
matches, err := m.matchesForJSONEvent([]byte(event))
if err != nil {
t.Errorf("m4J botch on %s: %s", event, err.Error())
}
if len(matches) != 1 {
t.Errorf("M=%d for %s", len(matches), pName)
} else {
if matches[0] != pName {
t.Errorf("wanted %s got %s", pName, matches[0])
}
}
}
// now let's run 1000 more random numbers that shouldn't match
shouldNot := 0
for shouldNot < len(pNames) {
//nolint:gosec
n := rand.Int63n(1000000)
ns := fmt.Sprintf("%d", n)
_, ok := used[ns]
if ok {
continue
}
shouldNot++
event := strings.ReplaceAll(eBase, "999", ns)
// breaks on 98463
// fmt.Println("Event: " + event)
matches, err := m.matchesForJSONEvent([]byte(event))
if err != nil {
t.Errorf("shouldNot botch on %s: %s", event, err.Error())
}
if len(matches) != 0 {
t.Errorf("OUCH %d matches to %s", len(matches), ns)
}
}
}
func TestMakeFAFragment(t *testing.T) {
data := []string{"ca", "cat", "longer"}
targetFA := &fieldMatcher{}
targetState := &faState{table: newSmallTable(), fieldTransitions: []*fieldMatcher{targetFA}}
targetStep := &faNext{states: []*faState{targetState}}
pp := newPrettyPrinter(3234)
for _, datum := range data {
frag := makeFAFragment([]byte(datum), targetStep, pp)
startTable := frag.states[0].table
var transIn []*fieldMatcher
transOut := traverseDFA(startTable, []byte(datum)[1:], transIn)
if len(transOut) != 1 || transOut[0] != targetFA {
t.Error("fail on ", datum)
}
}
}
func TestExerciseSingletonReplacement(t *testing.T) {
cm := newCoreMatcher()
err := cm.addPattern("x", `{"x": [ "a"]}`)
if err != nil {
t.Error("AP: " + err.Error())
}
err = cm.addPattern("x", `{"x": [1]}`)
if err != nil {
t.Error("AP: " + err.Error())
}
events := []string{`{"x": 1}`, `{"x": "a"}`}
for _, e := range events {
matches, err := cm.matchesForJSONEvent([]byte(e))
if err != nil {
t.Error("m4: " + err.Error())
}
if len(matches) != 1 || matches[0] != "x" {
t.Error("match failed on: " + e)
}
}
events = []string{`{"x": 1}`, `{"x": "a"}`}
for _, e := range events {
matches, err := cm.matchesForJSONEvent([]byte(e))
if err != nil {
t.Error("m4: " + err.Error())
}
if len(matches) != 1 || matches[0] != "x" {
t.Error("match failed on: " + e)
}
}
cm = newCoreMatcher()
err = cm.addPattern("x", `{"x": ["x"]}`)
if err != nil {
t.Error("AP: " + err.Error())
}
err = cm.addPattern("x", `{"x": [ {"wildcard": "x*y"}]}`)
if err != nil {
t.Error("AP: " + err.Error())
}
events = []string{`{"x": "x"}`, `{"x": "x..y"}`}
for _, e := range events {
matches, err := cm.matchesForJSONEvent([]byte(e))
if err != nil {
t.Error("m4: " + err.Error())
}
if len(matches) != 1 || matches[0] != "x" {
t.Error("match failed on: " + e)
}
}
}
func TestMergeNfaAndNumeric(t *testing.T) {
cm := newCoreMatcher()
err := cm.addPattern("x", `{"x": [{"wildcard":"x*y"}]}`)
if err != nil {
t.Error("AP: " + err.Error())
}
err = cm.addPattern("x", `{"x": [3]}`)
if err != nil {
t.Error("AP: " + err.Error())
}
events := []string{`{"x": 3}`, `{"x": "xasdfy"}`}
for _, e := range events {
matches, err := cm.matchesForJSONEvent([]byte(e))
if err != nil {
t.Error("M4: " + err.Error())
}
if len(matches) != 1 || matches[0] != "x" {
t.Error("Match failed on " + e)
}
}
}