node_test.go 3.1 KB

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