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
|
/*
* @Author: EnderByEndera
* @Date: 2020-12-08 11:28:49
* @LastEditTime: 2021-02-01 06:17:11
* @LastEditors: Please set LastEditors
* @Description: Test UnmarshalSetting and MarshalSetting
* @FilePath: /commdetection/rules/rulestype_test.go
*/
package rules
import (
"commdetection/comm"
"commdetection/model"
"log"
"testing"
)
func TestEvaluateCommandRule(t *testing.T) {
t.Run("Test Normal Command Rule Evaluation", func(t *testing.T) {
cs := model.CommScore{
Command: model.Command{
CommName: "wget",
Args: []string{"https://127.0.0.1:8080"},
},
Score: 100,
}
cs = EvaluateCommandRule(cs)
if cs.Score != 80 {
t.Errorf("score is not as predicted")
}
})
}
func TestEvaluatePathRule(t *testing.T) {
t.Run("Test Normal Path Rule Evaluation", func(t *testing.T) {
cs := model.CommScore{
Command: model.Command{
CommName: "wget",
Args: []string{"rules.json"},
},
Score: 100,
}
cs = EvaluatePathRule(cs)
if cs.Score >= 0 && cs.Score < 100 {
log.Printf("result score is %f", cs.Score)
} else {
t.Errorf("score is not as predicted")
}
})
}
func TestEvaluateWebsiteRule(t *testing.T) {
t.Run("Test Evaluating Website Rule", func(t *testing.T) {
cs := model.CommScore{
Command: model.Command{
CommName: "wget",
Args: []string{"https://golang.org/"},
Flags: []string{},
},
Score: 100.0,
}
cs = EvaluateWebsiteRule(cs)
if cs.Score == 100 {
return
}
t.Errorf("Wrong score: %.2f", cs.Score)
})
}
func BenchmarkEvaluateCommandRule(b *testing.B) {
comms := comm.GetCommands()
comms = comm.FlushCommands(comms, []comm.Filter{comm.WhichCommandFilter})
css := InitCommScores(comms)
b.ResetTimer()
for _, cs := range css {
cs = EvaluateCommandRule(cs)
}
}
func BenchmarkEvaluatePathRule(b *testing.B) {
comms := comm.GetCommands()
comms = comm.FlushCommands(comms, []comm.Filter{comm.WhichCommandFilter})
css := InitCommScores(comms)
b.ResetTimer()
for _, cs := range css {
cs = EvaluatePathRule(cs)
}
}
func BenchmarkEvaluateWebsiteRule(b *testing.B) {
comms := comm.GetCommands()
comms = comm.FlushCommands(comms, []comm.Filter{comm.WhichCommandFilter})
css := InitCommScores(comms)
b.ResetTimer()
for _, cs := range css {
cs = EvaluateWebsiteRule(cs)
}
}
|