File: node_test.go

package info (click to toggle)
crowdsec 1.4.6-10.1
  • links: PTS, VCS
  • area: main
  • in suites: forky, sid
  • size: 18,500 kB
  • sloc: sh: 2,870; makefile: 386; python: 74
file content (69 lines) | stat: -rw-r--r-- 3,191 bytes parent folder | download | duplicates (3)
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
package parser

import (
	"testing"

	"github.com/crowdsecurity/crowdsec/pkg/types"
	yaml "gopkg.in/yaml.v2"
)

func TestParserConfigs(t *testing.T) {
	pctx, err := Init(map[string]interface{}{"patterns": "../../config/patterns/", "data": "./tests/"})
	if err != nil {
		t.Fatalf("unable to load patterns : %s", err)
	}

	/*the actual tests*/
	var CfgTests = []struct {
		NodeCfg  *Node
		Compiles bool
		Valid    bool
	}{
		//valid node with grok pattern
		{&Node{Debug: true, Stage: "s00", Grok: types.GrokPattern{RegexpValue: "^x%{DATA:extr}$", TargetField: "t"}}, true, true},
		//bad filter
		{&Node{Debug: true, Stage: "s00", Filter: "ratata"}, false, false},
		//empty node
		{&Node{Debug: true, Stage: "s00", Filter: "true"}, false, false},
		//bad subgrok
		{&Node{Debug: true, Stage: "s00", SubGroks: yaml.MapSlice{{Key: string("FOOBAR"), Value: string("[a-$")}}}, false, true},
		//valid node with grok pattern
		{&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},
		//bad node success
		{&Node{Debug: true, Stage: "s00", OnSuccess: "ratat", Grok: types.GrokPattern{RegexpValue: "^x%{DATA:extr}$", TargetField: "t"}}, false, false},
		//ok node success
		{&Node{Debug: true, Stage: "s00", OnSuccess: "continue", Grok: types.GrokPattern{RegexpValue: "^x%{DATA:extr}$", TargetField: "t"}}, true, true},
		//valid node with grok sub-pattern used by name
		{&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},
		//node with unexisting grok pattern
		{&Node{Debug: true, Stage: "s00", Grok: types.GrokPattern{RegexpName: "RATATA", TargetField: "t"}}, false, true},
		//node with grok pattern dependencies
		{&Node{Debug: true, Stage: "s00", SubGroks: yaml.MapSlice{
			{Key: string("SUBGROK"), Value: string("[a-z]")},
			{Key: string("MYGROK"), Value: string("[a-z]%{SUBGROK}")},
		}, Grok: types.GrokPattern{RegexpValue: "^x%{MYGROK:extr}$", TargetField: "t"}}, true, true},
		//node with broken grok pattern dependencies
		{&Node{Debug: true, Stage: "s00", SubGroks: yaml.MapSlice{
			{Key: string("SUBGROKBIS"), Value: string("[a-z]%{MYGROKBIS}")},
			{Key: string("MYGROKBIS"), Value: string("[a-z]")},
		}, Grok: types.GrokPattern{RegexpValue: "^x%{MYGROKBIS:extr}$", TargetField: "t"}}, false, true},
	}
	for idx := range CfgTests {
		err := CfgTests[idx].NodeCfg.compile(pctx, EnricherCtx{})
		if CfgTests[idx].Compiles == true && err != nil {
			t.Fatalf("Compile: (%d/%d) expected valid, got : %s", idx+1, len(CfgTests), err)
		}
		if CfgTests[idx].Compiles == false && err == nil {
			t.Fatalf("Compile: (%d/%d) expected error", idx+1, len(CfgTests))
		}

		err = CfgTests[idx].NodeCfg.validate(pctx, EnricherCtx{})
		if CfgTests[idx].Valid == true && err != nil {
			t.Fatalf("Valid: (%d/%d) expected valid, got : %s", idx+1, len(CfgTests), err)
		}
		if CfgTests[idx].Valid == false && err == nil {
			t.Fatalf("Valid: (%d/%d) expected error", idx+1, len(CfgTests))
		}

	}
}