bitbucket.org/Aishee/synsec@v0.0.0-20210414005726-236fc01a153d/pkg/parser/node_test.go (about) 1 package parser 2 3 import ( 4 "testing" 5 6 "bitbucket.org/Aishee/synsec/pkg/types" 7 yaml "gopkg.in/yaml.v2" 8 ) 9 10 func TestParserConfigs(t *testing.T) { 11 pctx, err := Init(map[string]interface{}{"patterns": "../../config/patterns/", "data": "./tests/"}) 12 if err != nil { 13 t.Fatalf("unable to load patterns : %s", err) 14 } 15 16 /*the actual tests*/ 17 var CfgTests = []struct { 18 NodeCfg *Node 19 Compiles bool 20 Valid bool 21 }{ 22 //valid node with grok pattern 23 {&Node{Debug: true, Stage: "s00", Grok: types.GrokPattern{RegexpValue: "^x%{DATA:extr}$", TargetField: "t"}}, true, true}, 24 //bad filter 25 {&Node{Debug: true, Stage: "s00", Filter: "ratata"}, false, false}, 26 //empty node 27 {&Node{Debug: true, Stage: "s00", Filter: "true"}, false, false}, 28 //bad subgrok 29 {&Node{Debug: true, Stage: "s00", SubGroks: yaml.MapSlice{{Key: string("FOOBAR"), Value: string("[a-$")}}}, false, true}, 30 //valid node with grok pattern 31 {&Node{Debug: true, Stage: "s00", SubGroks: yaml.MapSlice{{Key: string("FOOBAR"), Value: string("[a-z]")}}, Grok: types.GrokPattern{RegexpValue: "^x%{FOOBAR:extr}$", TargetField: "t"}}, true, true}, 32 //bad node success 33 {&Node{Debug: true, Stage: "s00", OnSuccess: "ratat", Grok: types.GrokPattern{RegexpValue: "^x%{DATA:extr}$", TargetField: "t"}}, false, false}, 34 //ok node success 35 {&Node{Debug: true, Stage: "s00", OnSuccess: "continue", Grok: types.GrokPattern{RegexpValue: "^x%{DATA:extr}$", TargetField: "t"}}, true, true}, 36 //valid node with grok sub-pattern used by name 37 {&Node{Debug: true, Stage: "s00", SubGroks: yaml.MapSlice{{Key: string("FOOBARx"), Value: string("[a-z] %{DATA:lol}$")}}, Grok: types.GrokPattern{RegexpName: "FOOBARx", TargetField: "t"}}, true, true}, 38 //node with unexisting grok pattern 39 {&Node{Debug: true, Stage: "s00", Grok: types.GrokPattern{RegexpName: "RATATA", TargetField: "t"}}, false, true}, 40 //node with grok pattern dependencies 41 {&Node{Debug: true, Stage: "s00", SubGroks: yaml.MapSlice{ 42 {Key: string("SUBGROK"), Value: string("[a-z]")}, 43 {Key: string("MYGROK"), Value: string("[a-z]%{SUBGROK}")}, 44 }, Grok: types.GrokPattern{RegexpValue: "^x%{MYGROK:extr}$", TargetField: "t"}}, true, true}, 45 //node with broken grok pattern dependencies 46 {&Node{Debug: true, Stage: "s00", SubGroks: yaml.MapSlice{ 47 {Key: string("SUBGROKBIS"), Value: string("[a-z]%{MYGROKBIS}")}, 48 {Key: string("MYGROKBIS"), Value: string("[a-z]")}, 49 }, Grok: types.GrokPattern{RegexpValue: "^x%{MYGROKBIS:extr}$", TargetField: "t"}}, false, true}, 50 } 51 for idx := range CfgTests { 52 err := CfgTests[idx].NodeCfg.compile(pctx, []EnricherCtx{}) 53 if CfgTests[idx].Compiles == true && err != nil { 54 t.Fatalf("Compile: (%d/%d) expected valid, got : %s", idx+1, len(CfgTests), err) 55 } 56 if CfgTests[idx].Compiles == false && err == nil { 57 t.Fatalf("Compile: (%d/%d) expected errror", idx+1, len(CfgTests)) 58 } 59 60 err = CfgTests[idx].NodeCfg.validate(pctx, []EnricherCtx{}) 61 if CfgTests[idx].Valid == true && err != nil { 62 t.Fatalf("Valid: (%d/%d) expected valid, got : %s", idx+1, len(CfgTests), err) 63 } 64 if CfgTests[idx].Valid == false && err == nil { 65 t.Fatalf("Valid: (%d/%d) expected error", idx+1, len(CfgTests)) 66 } 67 68 } 69 }