forked from containers/common
-
Notifications
You must be signed in to change notification settings - Fork 1
/
conversion_test.go
213 lines (200 loc) · 4.82 KB
/
conversion_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
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
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
package seccomp
import (
"testing"
"github.com/opencontainers/runtime-spec/specs-go"
"github.com/stretchr/testify/require"
)
func TestGoArchToSeccompArchSuccess(t *testing.T) {
for goArch, seccompArch := range goArchToSeccompArchMap {
res, err := GoArchToSeccompArch(goArch)
require.Nil(t, err)
require.Equal(t, seccompArch, res)
}
}
func TestGoArchToSeccompArchFailure(t *testing.T) {
res, err := GoArchToSeccompArch("wrong")
require.NotNil(t, err)
require.Empty(t, res)
}
func TestSpecArchToSeccompArchSuccess(t *testing.T) {
for specArch, seccompArch := range specArchToSeccompArchMap {
res, err := specArchToSeccompArch(specArch)
require.Nil(t, err)
require.Equal(t, seccompArch, res)
}
}
func TestSpecArchToSeccompArchFailure(t *testing.T) {
res, err := specArchToSeccompArch("wrong")
require.NotNil(t, err)
require.Empty(t, res)
}
func TestSpecArchToLibseccompArchSuccess(t *testing.T) {
for specArch, libseccompArch := range specArchToLibseccompArchMap {
res, err := specArchToLibseccompArch(specArch)
require.Nil(t, err)
require.Equal(t, libseccompArch, res)
}
}
func TestSpecArchToLibseccompArchFailure(t *testing.T) {
res, err := specArchToLibseccompArch("wrong")
require.NotNil(t, err)
require.Empty(t, res)
}
func TestSpecActionToSeccompActionSuccess(t *testing.T) {
for specAction, seccompAction := range specActionToSeccompActionMap {
res, err := specActionToSeccompAction(specAction)
require.Nil(t, err)
require.Equal(t, seccompAction, res)
}
}
func TestSpecActionToSeccompActionFailure(t *testing.T) {
res, err := specActionToSeccompAction("wrong")
require.NotNil(t, err)
require.Empty(t, res)
}
func TestSpecOperatorToSeccompOperatorSuccess(t *testing.T) {
for specOperator, seccompOperator := range specOperatorToSeccompOperatorMap {
res, err := specOperatorToSeccompOperator(specOperator)
require.Nil(t, err)
require.Equal(t, seccompOperator, res)
}
}
func TestSpecOperatorToSeccompOperatorFailure(t *testing.T) {
res, err := specOperatorToSeccompOperator("wrong")
require.NotNil(t, err)
require.Empty(t, res)
}
func TestSpecToSeccomp(t *testing.T) {
var ret uint = 1
for _, tc := range []struct {
input *specs.LinuxSeccomp
expected func(*Seccomp, error)
}{
{ // success
input: &specs.LinuxSeccomp{
DefaultAction: specs.ActKill,
Architectures: []specs.Arch{
specs.ArchX32,
specs.ArchX86,
},
Syscalls: []specs.LinuxSyscall{
{
Names: []string{"open", "rmdir"},
Action: specs.ActTrap,
ErrnoRet: &ret,
Args: []specs.LinuxSeccompArg{
{
Index: 0,
Value: 20,
ValueTwo: 10,
Op: specs.OpLessThan,
},
{
Index: 1,
Value: 10,
ValueTwo: 12,
Op: specs.OpEqualTo,
},
},
},
{
Names: []string{"bind"},
Action: specs.ActTrap,
ErrnoRet: &ret,
},
},
},
expected: func(profile *Seccomp, err error) {
require.Nil(t, err)
require.Equal(t, &Seccomp{
DefaultAction: ActKill,
Architectures: []Arch{ArchX32, ArchX86},
Syscalls: []*Syscall{
{
Name: "open",
Action: ActTrap,
ErrnoRet: &ret,
Args: []*Arg{
{
Index: 0,
Value: 20,
ValueTwo: 10,
Op: OpLessThan,
},
{
Index: 1,
Value: 10,
ValueTwo: 12,
Op: OpEqualTo,
},
},
},
{
Name: "rmdir",
Action: ActTrap,
ErrnoRet: &ret,
Args: []*Arg{
{
Index: 0,
Value: 20,
ValueTwo: 10,
Op: OpLessThan,
},
{
Index: 1,
Value: 10,
ValueTwo: 12,
Op: OpEqualTo,
},
},
},
{
Name: "bind",
Action: ActTrap,
ErrnoRet: &ret,
Args: []*Arg{},
},
},
}, profile)
},
},
{ // wrong arch
input: &specs.LinuxSeccomp{
DefaultAction: specs.ActKill,
Architectures: []specs.Arch{"wrong"},
},
expected: func(profile *Seccomp, err error) {
require.NotNil(t, err)
require.Nil(t, profile)
},
},
{ // wrong op
input: &specs.LinuxSeccomp{
DefaultAction: specs.ActKill,
Syscalls: []specs.LinuxSyscall{
{
Names: []string{"rmdir"},
Action: specs.ActTrap,
ErrnoRet: &ret,
Args: []specs.LinuxSeccompArg{
{Op: "wrong"},
},
},
},
},
expected: func(profile *Seccomp, err error) {
require.NotNil(t, err)
require.Nil(t, profile)
},
},
{ // wrong default action
input: &specs.LinuxSeccomp{},
expected: func(profile *Seccomp, err error) {
require.NotNil(t, err)
require.Nil(t, profile)
},
},
} {
tc.expected(specToSeccomp(tc.input))
}
}