-
Notifications
You must be signed in to change notification settings - Fork 0
/
filter_element_test.go
125 lines (116 loc) · 2.42 KB
/
filter_element_test.go
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
// SPDX-Licence-Identifier: MIT
package goseccomp
import (
"reflect"
"testing"
"golang.org/x/net/bpf"
)
type TestCasesFilterElementKeep struct {
Orig FilterElement
Expected FilterElement
}
func TestFilterElementKeepLeastPreciseMatch(t *testing.T) {
cases := []TestCasesFilterElementKeep{
{
Orig: FilterElement{},
Expected: FilterElement{},
},
{
Orig: FilterElement{
Match: []SyscallCallFilter{
{
Args: [6]SyscallArgument{
Any(), Any(), Any(), Any(), Any(), Any(),
},
Number: 0,
},
{
Args: [6]SyscallArgument{
{4, false}, Any(), Any(), Any(), Any(), Any(),
},
Number: 1,
},
{
Args: [6]SyscallArgument{
Any(), Any(), Any(), Any(), Any(), Any(),
},
Number: 1,
},
{
Args: [6]SyscallArgument{
Any(), {1, false}, Any(), Any(), Any(), Any(),
},
Number: 0,
},
},
},
Expected: FilterElement{
Match: []SyscallCallFilter{
{
Args: [6]SyscallArgument{
Any(), Any(), Any(), Any(), Any(), Any(),
},
Number: 0,
},
{
Args: [6]SyscallArgument{
Any(), Any(), Any(), Any(), Any(), Any(),
},
Number: 1,
},
},
},
},
}
for i, tc := range cases {
tc.Orig.keepLeastPreciseMatch()
if !reflect.DeepEqual(tc.Orig, tc.Expected) {
t.Errorf(
"[%d/%d] Expected: %+v Got: %+v",
i+1, len(cases),
tc.Expected,
tc.Orig,
)
}
}
}
type TestCasesFilterElementCompile struct {
arch string
elem FilterElement
expected []bpf.Instruction
}
func TestFilterElementCompile(t *testing.T) {
cases := []TestCasesFilterElementCompile{
{
arch: "386",
elem: FilterElement{},
expected: nil,
},
{
arch: "386",
elem: FilterElement{
Decision: Decision{Type: KillThread, Data: 0},
Match: []SyscallCallFilter{
{0, [6]SyscallArgument{Any(), Any(), Any(), Any(), Any(), Any()}},
{1, [6]SyscallArgument{Any(), Any(), Any(), Any(), Any(), Any()}},
},
},
expected: []bpf.Instruction{
bpf.JumpIf{Cond: bpf.JumpEqual, Val: 1, SkipTrue: 1},
bpf.JumpIf{Cond: bpf.JumpEqual, Val: 0, SkipFalse: 1},
bpf.RetConstant{Val: 0},
},
},
}
for i, tc := range cases {
got := tc.elem.compile(tc.arch)
if !reflect.DeepEqual(got, tc.expected) {
t.Errorf(
"[%d/%d] Expected: %#v Got: %#v",
i+1, len(cases),
tc.expected,
got,
)
}
}
}