-
-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathschema_test.go
More file actions
976 lines (839 loc) · 26.6 KB
/
schema_test.go
File metadata and controls
976 lines (839 loc) · 26.6 KB
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
422
423
424
425
426
427
428
429
430
431
432
433
434
435
436
437
438
439
440
441
442
443
444
445
446
447
448
449
450
451
452
453
454
455
456
457
458
459
460
461
462
463
464
465
466
467
468
469
470
471
472
473
474
475
476
477
478
479
480
481
482
483
484
485
486
487
488
489
490
491
492
493
494
495
496
497
498
499
500
501
502
503
504
505
506
507
508
509
510
511
512
513
514
515
516
517
518
519
520
521
522
523
524
525
526
527
528
529
530
531
532
533
534
535
536
537
538
539
540
541
542
543
544
545
546
547
548
549
550
551
552
553
554
555
556
557
558
559
560
561
562
563
564
565
566
567
568
569
570
571
572
573
574
575
576
577
578
579
580
581
582
583
584
585
586
587
588
589
590
591
592
593
594
595
596
597
598
599
600
601
602
603
604
605
606
607
608
609
610
611
612
613
614
615
616
617
618
619
620
621
622
623
624
625
626
627
628
629
630
631
632
633
634
635
636
637
638
639
640
641
642
643
644
645
646
647
648
649
650
651
652
653
654
655
656
657
658
659
660
661
662
663
664
665
666
667
668
669
670
671
672
673
674
675
676
677
678
679
680
681
682
683
684
685
686
687
688
689
690
691
692
693
694
695
696
697
698
699
700
701
702
703
704
705
706
707
708
709
710
711
712
713
714
715
716
717
718
719
720
721
722
723
724
725
726
727
728
729
730
731
732
733
734
735
736
737
738
739
740
741
742
743
744
745
746
747
748
749
750
751
752
753
754
755
756
757
758
759
760
761
762
763
764
765
766
767
768
769
770
771
772
773
774
775
776
777
778
779
780
781
782
783
784
785
786
787
788
789
790
791
792
793
794
795
796
797
798
799
800
801
802
803
804
805
806
807
808
809
810
811
812
813
814
815
816
817
818
819
820
821
822
823
824
825
826
827
828
829
830
831
832
833
834
835
836
837
838
839
840
841
842
843
844
845
846
847
848
849
850
851
852
853
854
855
856
857
858
859
860
861
862
863
864
865
866
867
868
869
870
871
872
873
874
875
876
877
878
879
880
881
882
883
884
885
886
887
888
889
890
891
892
893
894
895
896
897
898
899
900
901
902
903
904
905
906
907
908
909
910
911
912
913
914
915
916
917
918
919
920
921
922
923
924
925
926
927
928
929
930
931
932
933
934
935
936
937
938
939
940
941
942
943
944
945
946
947
948
949
950
951
952
953
954
955
956
957
958
959
960
961
962
963
964
965
966
967
968
969
970
971
972
973
974
975
976
package wirefilter
import (
"testing"
"github.com/stretchr/testify/assert"
)
func TestSchemaFunctionControl(t *testing.T) {
t.Run("default mode is blocklist - all functions allowed", func(t *testing.T) {
schema := NewSchema().AddField("name", TypeString)
_, err := Compile(`lower(name) == "test"`, schema)
assert.NoError(t, err)
_, err = Compile(`upper(name) == "TEST"`, schema)
assert.NoError(t, err)
_, err = Compile(`len(name) > 0`, schema)
assert.NoError(t, err)
})
t.Run("blocklist mode - disable specific functions", func(t *testing.T) {
schema := NewSchema().
AddField("name", TypeString).
DisableFunctions("lower")
_, err := Compile(`lower(name) == "test"`, schema)
assert.Error(t, err)
assert.Contains(t, err.Error(), "function not allowed: lower")
// Other functions still work
_, err = Compile(`upper(name) == "TEST"`, schema)
assert.NoError(t, err)
})
t.Run("blocklist mode - disable multiple functions", func(t *testing.T) {
schema := NewSchema().
AddField("name", TypeString).
DisableFunctions("lower", "upper", "len")
_, err := Compile(`lower(name) == "test"`, schema)
assert.Error(t, err)
_, err = Compile(`upper(name) == "TEST"`, schema)
assert.Error(t, err)
_, err = Compile(`len(name) > 0`, schema)
assert.Error(t, err)
// starts_with still works
_, err = Compile(`starts_with(name, "test")`, schema)
assert.NoError(t, err)
})
t.Run("allowlist mode - only enabled functions work", func(t *testing.T) {
schema := NewSchema().
AddField("name", TypeString).
SetFunctionMode(FunctionModeAllowlist).
EnableFunctions("lower")
_, err := Compile(`lower(name) == "test"`, schema)
assert.NoError(t, err)
_, err = Compile(`upper(name) == "TEST"`, schema)
assert.Error(t, err)
assert.Contains(t, err.Error(), "function not allowed: upper")
})
t.Run("allowlist mode - enable multiple functions", func(t *testing.T) {
schema := NewSchema().
AddField("name", TypeString).
SetFunctionMode(FunctionModeAllowlist).
EnableFunctions("lower", "upper", "len")
_, err := Compile(`lower(name) == "test"`, schema)
assert.NoError(t, err)
_, err = Compile(`upper(name) == "TEST"`, schema)
assert.NoError(t, err)
_, err = Compile(`len(name) > 0`, schema)
assert.NoError(t, err)
// starts_with is not enabled
_, err = Compile(`starts_with(name, "test")`, schema)
assert.Error(t, err)
})
t.Run("function names are case-insensitive", func(t *testing.T) {
schema := NewSchema().
AddField("name", TypeString).
DisableFunctions("LOWER")
_, err := Compile(`lower(name) == "test"`, schema)
assert.Error(t, err)
_, err = Compile(`LOWER(name) == "test"`, schema)
assert.Error(t, err)
_, err = Compile(`Lower(name) == "test"`, schema)
assert.Error(t, err)
})
t.Run("enable after disable re-enables function", func(t *testing.T) {
schema := NewSchema().
AddField("name", TypeString).
DisableFunctions("lower").
EnableFunctions("lower")
_, err := Compile(`lower(name) == "test"`, schema)
assert.NoError(t, err)
})
t.Run("disable after enable disables function", func(t *testing.T) {
schema := NewSchema().
AddField("name", TypeString).
SetFunctionMode(FunctionModeAllowlist).
EnableFunctions("lower").
DisableFunctions("lower")
_, err := Compile(`lower(name) == "test"`, schema)
assert.Error(t, err)
})
t.Run("allowlist mode with no functions enabled", func(t *testing.T) {
schema := NewSchema().
AddField("name", TypeString).
SetFunctionMode(FunctionModeAllowlist)
_, err := Compile(`lower(name) == "test"`, schema)
assert.Error(t, err)
// Non-function expressions still work
_, err = Compile(`name == "test"`, schema)
assert.NoError(t, err)
})
t.Run("IsFunctionAllowed - blocklist mode", func(t *testing.T) {
schema := NewSchema().DisableFunctions("lower")
assert.False(t, schema.IsFunctionAllowed("lower"))
assert.False(t, schema.IsFunctionAllowed("LOWER"))
assert.True(t, schema.IsFunctionAllowed("upper"))
})
t.Run("IsFunctionAllowed - allowlist mode", func(t *testing.T) {
schema := NewSchema().
SetFunctionMode(FunctionModeAllowlist).
EnableFunctions("lower")
assert.True(t, schema.IsFunctionAllowed("lower"))
assert.True(t, schema.IsFunctionAllowed("LOWER"))
assert.False(t, schema.IsFunctionAllowed("upper"))
})
t.Run("nested function calls respect rules", func(t *testing.T) {
schema := NewSchema().
AddField("name", TypeString).
DisableFunctions("lower")
// len(lower(name)) should fail because lower is disabled
_, err := Compile(`len(lower(name)) > 0`, schema)
assert.Error(t, err)
assert.Contains(t, err.Error(), "function not allowed: lower")
// len(upper(name)) should work
_, err = Compile(`len(upper(name)) > 0`, schema)
assert.NoError(t, err)
})
t.Run("nil schema allows all functions", func(t *testing.T) {
_, err := Compile(`lower(name) == "test"`, nil)
assert.NoError(t, err)
})
t.Run("blocklist with enabled function", func(t *testing.T) {
schema := NewSchema().
SetFunctionMode(FunctionModeBlocklist).
DisableFunctions("lower").
EnableFunctions("lower") // re-enable
assert.True(t, schema.IsFunctionAllowed("lower"))
})
t.Run("validate unpack expression", func(t *testing.T) {
schema := NewSchema().AddField("tags", TypeArray)
_, err := Compile(`tags[*] == "a"`, schema)
assert.NoError(t, err)
})
t.Run("validate unpack with unknown field", func(t *testing.T) {
schema := NewSchema()
_, err := Compile(`unknown[*] == "a"`, schema)
assert.Error(t, err)
})
t.Run("validate index expression", func(t *testing.T) {
schema := NewSchema().AddField("data", TypeMap)
_, err := Compile(`data["key"] == "val"`, schema)
assert.NoError(t, err)
})
t.Run("validate index with unknown field", func(t *testing.T) {
schema := NewSchema()
_, err := Compile(`unknown["key"] == "val"`, schema)
assert.Error(t, err)
})
t.Run("validate index with unknown field as index key", func(t *testing.T) {
schema := NewSchema().AddField("data", TypeMap)
_, err := Compile(`data[unknown_field] == "val"`, schema)
assert.Error(t, err)
})
t.Run("validate list ref expression", func(t *testing.T) {
schema := NewSchema().AddField("ip", TypeIP)
_, err := Compile(`ip in $blocked`, schema)
assert.NoError(t, err)
})
t.Run("validate range expression", func(t *testing.T) {
schema := NewSchema().AddField("x", TypeInt)
_, err := Compile(`x in {1..10}`, schema)
assert.NoError(t, err)
})
t.Run("validate function args with unknown field", func(t *testing.T) {
schema := NewSchema()
_, err := Compile(`lower(unknown) == "test"`, schema)
assert.Error(t, err)
})
t.Run("schema with field map constructor", func(t *testing.T) {
schema := NewSchema(map[string]Type{
"name": TypeString,
"age": TypeInt,
})
_, ok := schema.GetField("name")
assert.True(t, ok)
_, ok = schema.GetField("age")
assert.True(t, ok)
})
}
func TestSchemaTypeValidation(t *testing.T) {
schema := NewSchema().
AddField("name", TypeString).
AddField("status", TypeInt).
AddField("active", TypeBool).
AddField("ip", TypeIP).
AddField("tags", TypeArray).
AddField("data", TypeMap).
AddField("body", TypeBytes)
t.Run("string valid operators", func(t *testing.T) {
valid := []string{
`name == "test"`,
`name != "test"`,
`name contains "test"`,
`name matches "^test"`,
`name in {"a", "b"}`,
`name wildcard "*.com"`,
`name strict wildcard "*.COM"`,
}
for _, expr := range valid {
_, err := Compile(expr, schema)
assert.NoError(t, err, "should be valid: %s", expr)
}
})
t.Run("string invalid operators", func(t *testing.T) {
invalid := []string{
`name > "test"`,
`name < "test"`,
`name >= "test"`,
`name <= "test"`,
`name === "test"`,
`name !== "test"`,
}
for _, expr := range invalid {
_, err := Compile(expr, schema)
assert.Error(t, err, "should be invalid: %s", expr)
assert.Contains(t, err.Error(), "not valid for field type")
}
})
t.Run("int valid operators", func(t *testing.T) {
valid := []string{
`status == 200`,
`status != 404`,
`status > 400`,
`status < 500`,
`status >= 200`,
`status <= 299`,
`status in {200, 301, 404}`,
}
for _, expr := range valid {
_, err := Compile(expr, schema)
assert.NoError(t, err, "should be valid: %s", expr)
}
})
t.Run("int invalid operators", func(t *testing.T) {
invalid := []string{
`status contains 200`,
`status matches "200"`,
`status wildcard "2*"`,
`status === 200`,
}
for _, expr := range invalid {
_, err := Compile(expr, schema)
assert.Error(t, err, "should be invalid: %s", expr)
}
})
t.Run("bool valid operators", func(t *testing.T) {
valid := []string{
`active == true`,
`active != false`,
}
for _, expr := range valid {
_, err := Compile(expr, schema)
assert.NoError(t, err, "should be valid: %s", expr)
}
})
t.Run("bool invalid operators", func(t *testing.T) {
invalid := []string{
`active > true`,
`active contains true`,
`active in {true}`,
}
for _, expr := range invalid {
_, err := Compile(expr, schema)
assert.Error(t, err, "should be invalid: %s", expr)
}
})
t.Run("IP valid operators", func(t *testing.T) {
valid := []string{
`ip == 10.0.0.1`,
`ip != 10.0.0.1`,
`ip in "10.0.0.0/8"`,
`ip in {10.0.0.1, 192.168.0.0/16}`,
}
for _, expr := range valid {
_, err := Compile(expr, schema)
assert.NoError(t, err, "should be valid: %s", expr)
}
})
t.Run("IP invalid operators", func(t *testing.T) {
invalid := []string{
`ip > 10.0.0.1`,
`ip contains "10"`,
`ip matches "10\\..*"`,
`ip wildcard "10.*"`,
}
for _, expr := range invalid {
_, err := Compile(expr, schema)
assert.Error(t, err, "should be invalid: %s", expr)
}
})
t.Run("array valid operators", func(t *testing.T) {
valid := []string{
`tags == tags`,
`tags contains "admin"`,
`tags in {"a", "b"}`,
`tags === "admin"`,
`tags !== "admin"`,
}
for _, expr := range valid {
_, err := Compile(expr, schema)
assert.NoError(t, err, "should be valid: %s", expr)
}
})
t.Run("array invalid operators", func(t *testing.T) {
invalid := []string{
`tags > "admin"`,
`tags matches "admin"`,
`tags wildcard "admin*"`,
}
for _, expr := range invalid {
_, err := Compile(expr, schema)
assert.Error(t, err, "should be invalid: %s", expr)
}
})
t.Run("map valid operators", func(t *testing.T) {
valid := []string{
`data == data`,
`data != data`,
}
for _, expr := range valid {
_, err := Compile(expr, schema)
assert.NoError(t, err, "should be valid: %s", expr)
}
})
t.Run("map invalid operators", func(t *testing.T) {
invalid := []string{
`data > data`,
`data contains "key"`,
}
for _, expr := range invalid {
_, err := Compile(expr, schema)
assert.Error(t, err, "should be invalid: %s", expr)
}
})
t.Run("no schema skips type validation", func(t *testing.T) {
_, err := Compile(`name > "test"`, nil)
assert.NoError(t, err)
})
t.Run("logical operators always valid", func(t *testing.T) {
valid := []string{
`name == "test" and status > 200`,
`name == "test" or active == true`,
`active xor active`,
}
for _, expr := range valid {
_, err := Compile(expr, schema)
assert.NoError(t, err, "should be valid: %s", expr)
}
})
t.Run("unpack skips type validation", func(t *testing.T) {
// tags[*] unpacks array elements - operator applies to elements, not array
valid := []string{
`tags[*] matches "^admin"`,
`tags[*] > 5`,
`tags[*] wildcard "*.com"`,
}
for _, expr := range valid {
_, err := Compile(expr, schema)
assert.NoError(t, err, "should be valid (unpacked): %s", expr)
}
})
t.Run("index skips element type validation", func(t *testing.T) {
// data["key"] accesses map element - type of element unknown at schema level
_, err := Compile(`data["key"] matches "^test"`, schema)
assert.NoError(t, err)
})
}
func TestSchemaComplexityLimits(t *testing.T) {
t.Run("max depth - within limit", func(t *testing.T) {
schema := NewSchema().
AddField("a", TypeBool).
SetMaxDepth(10)
_, err := Compile(`a and a and a`, schema)
assert.NoError(t, err)
})
t.Run("max depth - exceeds limit", func(t *testing.T) {
schema := NewSchema().
AddField("a", TypeBool).
SetMaxDepth(3)
// "a and a and a and a" creates nested binary exprs > depth 3
_, err := Compile(`a and (a and (a and a))`, schema)
assert.Error(t, err)
assert.Contains(t, err.Error(), "exceeds maximum depth")
})
t.Run("max depth - exact limit", func(t *testing.T) {
schema := NewSchema().
AddField("a", TypeInt).
SetMaxDepth(5)
_, err := Compile(`a == 1`, schema)
assert.NoError(t, err)
})
t.Run("max nodes - within limit", func(t *testing.T) {
schema := NewSchema().
AddField("a", TypeBool).
SetMaxNodes(20)
_, err := Compile(`a and a`, schema)
assert.NoError(t, err)
})
t.Run("max nodes - exceeds limit", func(t *testing.T) {
schema := NewSchema().
AddField("x", TypeInt).
SetMaxNodes(5)
_, err := Compile(`x == 1 and x == 2 and x == 3`, schema)
assert.Error(t, err)
assert.Contains(t, err.Error(), "exceeds maximum node count")
})
t.Run("zero limits means unlimited", func(t *testing.T) {
schema := NewSchema().
AddField("x", TypeInt).
SetMaxDepth(0).
SetMaxNodes(0)
_, err := Compile(`x == 1 and x == 2 and x == 3 and x == 4`, schema)
assert.NoError(t, err)
})
t.Run("depth with nested functions", func(t *testing.T) {
schema := NewSchema().
AddField("name", TypeString).
SetMaxDepth(3)
// lower(name) == "test" has depth: BinaryExpr > FunctionCallExpr > FieldExpr = 3
_, err := Compile(`lower(name) == "test"`, schema)
assert.NoError(t, err)
})
t.Run("depth with deeply nested functions", func(t *testing.T) {
schema := NewSchema().
AddField("name", TypeString).
SetMaxDepth(3)
// nested: and > BinaryExpr > FunctionCallExpr > FieldExpr = 4
_, err := Compile(`lower(name) == "test" and name == "x"`, schema)
assert.Error(t, err)
assert.Contains(t, err.Error(), "exceeds maximum depth")
})
t.Run("nodes with array", func(t *testing.T) {
schema := NewSchema().
AddField("x", TypeInt).
SetMaxNodes(10)
// x in {1, 2, 3, 4, 5, 6, 7, 8} = BinaryExpr + FieldExpr + ArrayExpr + 8 literals = 11
_, err := Compile(`x in {1, 2, 3, 4, 5, 6, 7, 8}`, schema)
assert.Error(t, err)
assert.Contains(t, err.Error(), "exceeds maximum node count")
})
t.Run("combined depth and nodes", func(t *testing.T) {
schema := NewSchema().
AddField("a", TypeBool).
SetMaxDepth(100).
SetMaxNodes(5)
_, err := Compile(`a and a and a and a`, schema)
assert.Error(t, err)
assert.Contains(t, err.Error(), "exceeds maximum node count")
})
}
func TestSchemaExport(t *testing.T) {
t.Run("fields", func(t *testing.T) {
schema := NewSchema().
AddField("http.host", TypeString).
AddField("http.status", TypeInt).
AddField("ip.src", TypeIP).
AddField("active", TypeBool)
exported := schema.Export()
assert.Equal(t, TypeString, exported["http.host"])
assert.Equal(t, TypeInt, exported["http.status"])
assert.Equal(t, TypeIP, exported["ip.src"])
assert.Equal(t, TypeBool, exported["active"])
assert.Len(t, exported, 4)
})
t.Run("empty schema", func(t *testing.T) {
schema := NewSchema()
exported := schema.Export()
assert.Empty(t, exported)
})
}
func TestSchemaValidationEdgeCases(t *testing.T) {
t.Run("range start validation error", func(t *testing.T) {
schema := NewSchema().AddField("x", TypeInt)
// Hand-build AST with unknown field inside RangeExpr.Start
expr := &BinaryExpr{
Left: &FieldExpr{Name: "x"},
Operator: TokenIn,
Right: &ArrayExpr{Elements: []Expression{
&RangeExpr{
Start: &FieldExpr{Name: "unknown"},
End: &LiteralExpr{Value: IntValue(10)},
},
}},
}
err := schema.Validate(expr)
assert.Error(t, err)
})
t.Run("range end validation error", func(t *testing.T) {
schema := NewSchema().AddField("x", TypeInt)
expr := &BinaryExpr{
Left: &FieldExpr{Name: "x"},
Operator: TokenIn,
Right: &ArrayExpr{Elements: []Expression{
&RangeExpr{
Start: &LiteralExpr{Value: IntValue(1)},
End: &FieldExpr{Name: "unknown"},
},
}},
}
err := schema.Validate(expr)
assert.Error(t, err)
})
t.Run("validate func args builtin with custom funcs registered", func(t *testing.T) {
schema := NewSchema().
AddField("name", TypeString).
RegisterFunction("custom_fn", TypeBool, nil)
// lower() is a built-in, not in customFuncs - hits the !ok return
_, err := Compile(`lower(name) == "test"`, schema)
assert.NoError(t, err)
})
t.Run("validate func args no custom funcs registered", func(t *testing.T) {
schema := NewSchema().AddField("name", TypeString)
_, err := Compile(`lower(name) == "test"`, schema)
assert.NoError(t, err)
})
}
func TestTypedArrayField(t *testing.T) {
t.Run("valid unpack operations on typed array", func(t *testing.T) {
schema := NewSchema().
AddArrayField("tags", TypeString).
AddArrayField("ports", TypeInt)
valid := []string{
`tags[*] == "admin"`,
`tags[*] != "blocked"`,
`tags[*] contains "prod"`,
`tags[*] matches "^admin"`,
`tags[*] in {"a", "b"}`,
`tags[*] wildcard "*.com"`,
`ports[*] >= 1024`,
`ports[*] < 65535`,
`ports[*] == 443`,
`ports[*] in {80, 443}`,
}
for _, expr := range valid {
_, err := Compile(expr, schema)
assert.NoError(t, err, "should be valid: %s", expr)
}
})
t.Run("invalid unpack operations on typed array", func(t *testing.T) {
schema := NewSchema().
AddArrayField("tags", TypeString).
AddArrayField("ports", TypeInt)
invalid := []string{
`tags[*] > 10`,
`tags[*] >= 5`,
`ports[*] contains "x"`,
`ports[*] matches "^[0-9]+"`,
`ports[*] wildcard "80*"`,
}
for _, expr := range invalid {
_, err := Compile(expr, schema)
assert.Error(t, err, "should be invalid: %s", expr)
assert.Contains(t, err.Error(), "not valid for field type")
}
})
t.Run("untyped array still skips element validation", func(t *testing.T) {
schema := NewSchema().AddField("tags", TypeArray)
_, err := Compile(`tags[*] > 10`, schema)
assert.NoError(t, err)
})
t.Run("array field type is TypeArray", func(t *testing.T) {
schema := NewSchema().AddArrayField("tags", TypeString)
field, ok := schema.GetField("tags")
assert.True(t, ok)
assert.Equal(t, TypeArray, field.Type)
assert.Equal(t, TypeString, field.ElemType)
})
}
func TestTypedMapField(t *testing.T) {
t.Run("valid index operations on typed map", func(t *testing.T) {
schema := NewSchema().
AddMapField("headers", TypeString).
AddMapField("scores", TypeFloat)
valid := []string{
`headers["x-env"] == "prod"`,
`headers["host"] contains "example"`,
`headers["ua"] matches "^Mozilla"`,
`scores["risk"] > 0.8`,
`scores["risk"] >= 0.5`,
`scores["risk"] == 1.0`,
}
for _, expr := range valid {
_, err := Compile(expr, schema)
assert.NoError(t, err, "should be valid: %s", expr)
}
})
t.Run("invalid index operations on typed map", func(t *testing.T) {
schema := NewSchema().
AddMapField("headers", TypeString).
AddMapField("scores", TypeFloat)
invalid := []string{
`headers["x-env"] > 10`,
`headers["host"] >= 5`,
`scores["risk"] contains "x"`,
`scores["risk"] matches "^[0-9]+"`,
}
for _, expr := range invalid {
_, err := Compile(expr, schema)
assert.Error(t, err, "should be invalid: %s", expr)
assert.Contains(t, err.Error(), "not valid for field type")
}
})
t.Run("untyped map still skips value validation", func(t *testing.T) {
schema := NewSchema().AddField("data", TypeMap)
_, err := Compile(`data["key"] > 10`, schema)
assert.NoError(t, err)
})
t.Run("map field type is TypeMap", func(t *testing.T) {
schema := NewSchema().AddMapField("headers", TypeString)
field, ok := schema.GetField("headers")
assert.True(t, ok)
assert.Equal(t, TypeMap, field.Type)
assert.Equal(t, TypeString, field.ElemType)
})
}
func TestTypedFieldsCombined(t *testing.T) {
t.Run("mixed typed and untyped fields", func(t *testing.T) {
schema := NewSchema().
AddField("name", TypeString).
AddArrayField("tags", TypeString).
AddArrayField("ports", TypeInt).
AddMapField("headers", TypeString).
AddMapField("scores", TypeFloat).
AddField("data", TypeMap)
valid := []string{
`name == "test" and tags[*] contains "prod"`,
`ports[*] >= 1024 and headers["host"] == "example.com"`,
`scores["risk"] > 0.5 or name contains "admin"`,
`data["key"] > 10`,
}
for _, expr := range valid {
_, err := Compile(expr, schema)
assert.NoError(t, err, "should be valid: %s", expr)
}
invalid := []string{
`tags[*] > 10 and name == "test"`,
`scores["risk"] contains "x"`,
}
for _, expr := range invalid {
_, err := Compile(expr, schema)
assert.Error(t, err, "should be invalid: %s", expr)
}
})
t.Run("export includes typed fields", func(t *testing.T) {
schema := NewSchema().
AddArrayField("tags", TypeString).
AddMapField("headers", TypeString)
exported := schema.Export()
assert.Equal(t, TypeArray, exported["tags"])
assert.Equal(t, TypeMap, exported["headers"])
})
}
func TestFunctionReturnTypeInference(t *testing.T) {
schema := NewSchema().
AddMapField("user", TypeString).
RegisterFunction("get_score", TypeFloat, []Type{TypeString}).
RegisterFunction("get_name", TypeString, []Type{TypeString})
t.Run("valid operations on function return type", func(t *testing.T) {
valid := []string{
`get_score(user["email"]) > 0.5`,
`get_score(user["email"]) >= 0.0`,
`get_score(user["email"]) == 1.0`,
`get_score(user["email"]) in {0.5, 1.0}`,
`get_name(user["email"]) == "admin"`,
`get_name(user["email"]) contains "admin"`,
`get_name(user["email"]) matches "^admin"`,
}
for _, expr := range valid {
_, err := Compile(expr, schema)
assert.NoError(t, err, "should be valid: %s", expr)
}
})
t.Run("invalid operations on function return type", func(t *testing.T) {
invalid := []string{
`get_score(user["email"]) contains "x"`,
`get_score(user["email"]) matches "^[0-9]"`,
`get_name(user["email"]) > 5`,
`get_name(user["email"]) >= 0`,
}
for _, expr := range invalid {
_, err := Compile(expr, schema)
assert.Error(t, err, "should be invalid: %s", expr)
assert.Contains(t, err.Error(), "not valid for field type")
}
})
t.Run("unregistered function skips validation", func(t *testing.T) {
_, err := Compile(`unknown_func() > 5`, schema)
assert.NoError(t, err)
})
t.Run("full chain inference", func(t *testing.T) {
_, err := Compile(`get_score(user["email"]) > 0.5 and get_name(user["email"]) contains "admin"`, schema)
assert.NoError(t, err)
})
}
func TestSchemaTimeAndDuration(t *testing.T) {
schema := NewSchema().
AddField("created_at", TypeTime).
AddField("ttl", TypeDuration).
AddField("name", TypeString)
t.Run("valid time operators", func(t *testing.T) {
tests := []string{
`created_at == 2026-03-19T10:00:00Z`,
`created_at != 2026-03-19T10:00:00Z`,
`created_at < 2026-03-19T10:00:00Z`,
`created_at > 2026-03-19T10:00:00Z`,
`created_at <= 2026-03-19T10:00:00Z`,
`created_at >= 2026-03-19T10:00:00Z`,
`created_at + 1h >= 2026-03-19T10:00:00Z`,
`created_at - 1h <= 2026-03-19T10:00:00Z`,
}
for _, expr := range tests {
_, err := Compile(expr, schema)
assert.NoError(t, err, "expression: %s", expr)
}
})
t.Run("invalid time operators", func(t *testing.T) {
tests := []string{
`created_at contains "test"`,
`created_at matches "pattern"`,
`created_at * 2`,
}
for _, expr := range tests {
_, err := Compile(expr, schema)
assert.Error(t, err, "expression: %s", expr)
}
})
t.Run("valid duration operators", func(t *testing.T) {
tests := []string{
`ttl == 30m`,
`ttl != 30m`,
`ttl < 1h`,
`ttl > 5m`,
`ttl <= 1h`,
`ttl >= 5m`,
`ttl + 30m > 1h`,
`ttl - 5m < 1h`,
`ttl * 2 > 1h`,
`ttl / 2 < 1h`,
`ttl % 1h == 30m`,
}
for _, expr := range tests {
_, err := Compile(expr, schema)
assert.NoError(t, err, "expression: %s", expr)
}
})
t.Run("invalid duration operators", func(t *testing.T) {
tests := []string{
`ttl contains "test"`,
`ttl matches "pattern"`,
}
for _, expr := range tests {
_, err := Compile(expr, schema)
assert.Error(t, err, "expression: %s", expr)
}
})
t.Run("now resolves to TypeTime", func(t *testing.T) {
_, err := Compile(`created_at >= now() - 1h`, schema)
assert.NoError(t, err)
})
t.Run("time in set", func(t *testing.T) {
_, err := Compile(`created_at in {2026-03-19T10:00:00Z, 2026-03-20T10:00:00Z}`, schema)
assert.NoError(t, err)
})
t.Run("duration in set", func(t *testing.T) {
_, err := Compile(`ttl in {30m, 1h, 2h}`, schema)
assert.NoError(t, err)
})
}
func TestDisableRegex(t *testing.T) {
schema := NewSchema().
AddField("name", TypeString).
AddField("path", TypeString).
DisableRegex()
t.Run("matches operator blocked", func(t *testing.T) {
_, err := Compile(`name matches "^test"`, schema)
assert.Error(t, err)
assert.Contains(t, err.Error(), "regex is disabled")
})
t.Run("tilde alias blocked", func(t *testing.T) {
_, err := Compile(`name ~ "^test"`, schema)
assert.Error(t, err)
assert.Contains(t, err.Error(), "regex is disabled")
})
t.Run("regex_replace blocked", func(t *testing.T) {
_, err := Compile(`regex_replace(name, "[0-9]+", "X") == "X"`, schema)
assert.Error(t, err)
assert.Contains(t, err.Error(), "regex is disabled")
})
t.Run("regex_extract blocked", func(t *testing.T) {
_, err := Compile(`regex_extract(path, "/v[0-9]+/") == ""`, schema)
assert.Error(t, err)
assert.Contains(t, err.Error(), "regex is disabled")
})
t.Run("contains_word blocked", func(t *testing.T) {
_, err := Compile(`contains_word(name, "admin")`, schema)
assert.Error(t, err)
assert.Contains(t, err.Error(), "regex is disabled")
})
t.Run("wildcard still allowed", func(t *testing.T) {
_, err := Compile(`name wildcard "*.example.com"`, schema)
assert.NoError(t, err)
})
t.Run("strict wildcard still allowed", func(t *testing.T) {
_, err := Compile(`name strict wildcard "*.Example.com"`, schema)
assert.NoError(t, err)
})
t.Run("contains still allowed", func(t *testing.T) {
_, err := Compile(`name contains "test"`, schema)
assert.NoError(t, err)
})
t.Run("equality still allowed", func(t *testing.T) {
_, err := Compile(`name == "test"`, schema)
assert.NoError(t, err)
})
t.Run("other functions still allowed", func(t *testing.T) {
_, err := Compile(`lower(name) == "test"`, schema)
assert.NoError(t, err)
})
t.Run("regex allowed without flag", func(t *testing.T) {
noFlag := NewSchema().AddField("name", TypeString)
_, err := Compile(`name matches "^test"`, noFlag)
assert.NoError(t, err)
_, err = Compile(`regex_replace(name, "a", "b") == "x"`, noFlag)
assert.NoError(t, err)
})
}