summaryrefslogtreecommitdiff
path: root/rules/rules_test.go
blob: 468a5f6c275492a647901bce979afebe82f68bf7 (plain)
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
/*
 * @Author: EnderByEndera
 * @Date: 2020-12-04 15:03:09
 * @LastEditTime: 2021-01-06 09:59:08
 * @LastEditors: Please set LastEditors
 * @Description: Test commrules.go
 * @FilePath: /commdetection/rules/commrules_test.go
 */
package rules

import (
	"commdetection/comm"
	"commdetection/model"
	"fmt"
	"testing"
)

func TestRule(t *testing.T) {
	t.Run("Test AddRule", func(t *testing.T) {
		r := model.Rules{
			{
				Name:     "RuleA",
				RuleFunc: "EvaluateCommandRule",
			},
			{
				Name:     "RuleB",
				RuleFunc: "EvaluatePathRule",
			},
		}
		r = AddRule(r, model.Rule{
			Name:     "RuleC",
			RuleFunc: "EvaluateCommandRule",
		})
		predict := model.Rules{
			{
				Name:     "RuleA",
				RuleFunc: "EvaluateCommandRule",
			},
			{
				Name:     "RuleB",
				RuleFunc: "EvaluatePathRule",
			},
			{
				Name:     "RuleC",
				RuleFunc: "EvaluateCommandRule",
			},
		}
		if len(r) != len(predict) {
			fmt.Println(r)
			t.Errorf("results are not as predicted")
			return
		}
		for i := range r {
			if r[i].Name != predict[i].Name {
				fmt.Println(r)
				t.Errorf("results are not as predicted")
				return
			}
		}
		cs := model.CommScore{
			Command: model.Command{CommName: "wget"},
			Score:   100,
		}
		for _, rule := range r {
			cs = RuleFuncMap[rule.RuleFunc](cs)
		}
		if cs.Score != 100*0.8*0.8 {
			t.Errorf("results are not as predicted")
		}
	})

	t.Run("Test AddRule with the same name", func(t *testing.T) {
		r := model.Rules{
			{
				Name:     "RuleA",
				RuleFunc: "EvaluateCommandRule",
			},
			{
				Name:     "RuleB",
				RuleFunc: "EvaluatePathRule",
			},
		}
		r = AddRule(r, model.Rule{
			Name:     "RuleA",
			RuleFunc: "EvaluateCommandRule",
		})
		if len(r) == 3 {
			t.Errorf("results are not as predicted")
		}
	})

	t.Run("Test DeleteRuleByName", func(t *testing.T) {
		r := model.Rules{
			{
				Name:     "RuleA",
				RuleFunc: "EvaluateCommandRule",
			},
			{
				Name:     "RuleB",
				RuleFunc: "EvaluatePathRule",
			},
		}
		r = DeleteRuleByName(r, "RuleB")
		if len(r) != 1 {
			fmt.Println(r)
			t.Errorf("results are not as predicted")
		}
		r = DeleteRuleByName(r, "RuleB")
		if len(r) != 1 {
			fmt.Println(r)
			t.Errorf("results are not as predicted")
		}
	})

	t.Run("Test MarshallEvaluationRules", func(t *testing.T) {
		rs := model.Rules{
			{
				Name:     "RuleA",
				RuleFunc: "EvaluateCommandRule",
			},
		}
		err := model.MarshalEvaluationRules(rs)
		if err != nil {
			t.Error(err)
		}
	})
}

func TestInitCommScores(t *testing.T) {
	comms := comm.GetCommands()
	comms = comm.FlushCommands(comms, []comm.Filter{comm.WhichCommandFilter})
	css := InitCommScores(comms)
	fmt.Println(css)
}

func TestEvaluateCommScore(t *testing.T) {
	css := EvaluateCommScore([]model.CommScore{
		{
			Command: model.Command{
				CommName: "wget",
				Args:     []string{""},
			},
			Score: 100,
		},
	},
		model.Rules{
			{
				Name:     "RuleA",
				RuleFunc: "EvaluateCommandRule",
			},
		})
	if css[0].Score != 80.0 {
		t.Errorf("Wrong Score")
	}
}