Skip to content

Commit c64aaf0

Browse files
mauriciovasquezbernalrata
authored andcommitted
libcontainer/specconv: extend SetupSeccomp tests
Extend the SetupSeccomp tests by adding the following cases: - Test nil config - Test empty config - Test bad action and architecture - Test all possible actions Signed-off-by: Mauricio Vásquez <[email protected]>
1 parent 2b025c0 commit c64aaf0

File tree

1 file changed

+130
-20
lines changed

1 file changed

+130
-20
lines changed

libcontainer/specconv/spec_linux_test.go

Lines changed: 130 additions & 20 deletions
Original file line numberDiff line numberDiff line change
@@ -139,10 +139,59 @@ func TestCreateHooks(t *testing.T) {
139139
}
140140
}
141141

142+
func TestSetupSeccompNil(t *testing.T) {
143+
seccomp, err := SetupSeccomp(nil)
144+
if err != nil {
145+
t.Error("Expected error to be nil")
146+
}
147+
148+
if seccomp != nil {
149+
t.Error("Expected seccomp to be nil")
150+
}
151+
}
152+
153+
func TestSetupSeccompEmpty(t *testing.T) {
154+
conf := &specs.LinuxSeccomp{}
155+
seccomp, err := SetupSeccomp(conf)
156+
if err != nil {
157+
t.Error("Expected error to be nil")
158+
}
159+
160+
if seccomp != nil {
161+
t.Error("Expected seccomp to be nil")
162+
}
163+
}
164+
165+
// TestSetupSeccompWrongAction tests that a wrong action triggers an error
166+
func TestSetupSeccompWrongAction(t *testing.T) {
167+
conf := &specs.LinuxSeccomp{
168+
DefaultAction: "SCMP_ACT_NON_EXIXTENT_ACTION",
169+
}
170+
_, err := SetupSeccomp(conf)
171+
if err == nil {
172+
t.Error("Expected error")
173+
}
174+
}
175+
176+
// TestSetupSeccompWrongArchitecture tests that a wrong architecture triggers an error
177+
func TestSetupSeccompWrongArchitecture(t *testing.T) {
178+
conf := &specs.LinuxSeccomp{
179+
DefaultAction: "SCMP_ACT_ALLOW",
180+
Architectures: []specs.Arch{"SCMP_ARCH_NON_EXISTENT_ARCH"},
181+
}
182+
_, err := SetupSeccomp(conf)
183+
if err == nil {
184+
t.Error("Expected error")
185+
}
186+
}
187+
142188
func TestSetupSeccomp(t *testing.T) {
189+
errnoRet := uint(55)
143190
conf := &specs.LinuxSeccomp{
144-
DefaultAction: "SCMP_ACT_ERRNO",
145-
Architectures: []specs.Arch{specs.ArchX86_64, specs.ArchARM},
191+
DefaultAction: "SCMP_ACT_ERRNO",
192+
Architectures: []specs.Arch{specs.ArchX86_64, specs.ArchARM},
193+
ListenerPath: "/var/run/mysocket",
194+
ListenerMetadata: "mymetadatastring",
146195
Syscalls: []specs.LinuxSyscall{
147196
{
148197
Names: []string{"clone"},
@@ -157,16 +206,33 @@ func TestSetupSeccomp(t *testing.T) {
157206
},
158207
},
159208
{
160-
Names: []string{
161-
"select",
162-
"semctl",
163-
"semget",
164-
"semop",
165-
"semtimedop",
166-
"send",
167-
"sendfile",
168-
},
169-
Action: "SCMP_ACT_ALLOW",
209+
Names: []string{"semctl"},
210+
Action: "SCMP_ACT_KILL",
211+
},
212+
{
213+
Names: []string{"semget"},
214+
Action: "SCMP_ACT_ERRNO",
215+
},
216+
{
217+
Names: []string{"send"},
218+
Action: "SCMP_ACT_ERRNO",
219+
ErrnoRet: &errnoRet,
220+
},
221+
{
222+
Names: []string{"lchown"},
223+
Action: "SCMP_ACT_TRAP",
224+
},
225+
{
226+
Names: []string{"lremovexattr"},
227+
Action: "SCMP_ACT_TRACE",
228+
},
229+
{
230+
Names: []string{"mbind"},
231+
Action: "SCMP_ACT_LOG",
232+
},
233+
{
234+
Names: []string{"mknod"},
235+
Action: "SCMP_ACT_NOTIFY",
170236
},
171237
},
172238
}
@@ -175,7 +241,7 @@ func TestSetupSeccomp(t *testing.T) {
175241
t.Errorf("Couldn't create Seccomp config: %v", err)
176242
}
177243

178-
if seccomp.DefaultAction != 2 { // SCMP_ACT_ERRNO
244+
if seccomp.DefaultAction != configs.Errno {
179245
t.Error("Wrong conversion for DefaultAction")
180246
}
181247

@@ -187,29 +253,73 @@ func TestSetupSeccomp(t *testing.T) {
187253
t.Error("Expected architectures are not found")
188254
}
189255

256+
if seccomp.ListenerPath != "/var/run/mysocket" {
257+
t.Error("Expected ListenerPath is wrong")
258+
}
259+
260+
if seccomp.ListenerMetadata != "mymetadatastring" {
261+
t.Error("Expected ListenerMetadata is wrong")
262+
}
263+
190264
calls := seccomp.Syscalls
191265

192266
callsLength := len(calls)
193267
if callsLength != 8 {
194268
t.Errorf("Expected 8 syscalls, got :%d", callsLength)
195269
}
196270

197-
for i, call := range calls {
198-
if i == 0 {
271+
for _, call := range calls {
272+
switch call.Name {
273+
case "clone":
274+
if call.Action != configs.Allow {
275+
t.Error("Wrong conversion for the clone syscall action")
276+
}
199277
expectedCloneSyscallArgs := configs.Arg{
200278
Index: 0,
201-
Op: 7, // SCMP_CMP_MASKED_EQ
279+
Op: configs.MaskEqualTo,
202280
Value: unix.CLONE_NEWNS | unix.CLONE_NEWUTS | unix.CLONE_NEWIPC | unix.CLONE_NEWUSER | unix.CLONE_NEWPID | unix.CLONE_NEWNET | unix.CLONE_NEWCGROUP,
203281
ValueTwo: 0,
204282
}
205283
if expectedCloneSyscallArgs != *call.Args[0] {
206284
t.Errorf("Wrong arguments conversion for the clone syscall under test")
207285
}
286+
case "semctl":
287+
if call.Action != configs.Kill {
288+
t.Errorf("Wrong conversion for the %s syscall action", call.Name)
289+
}
290+
case "semget":
291+
if call.Action != configs.Errno {
292+
t.Errorf("Wrong conversion for the %s syscall action", call.Name)
293+
}
294+
if call.ErrnoRet != nil {
295+
t.Errorf("Wrong error ret for the %s syscall", call.Name)
296+
}
297+
case "send":
298+
if call.Action != configs.Errno {
299+
t.Errorf("Wrong conversion for the %s syscall action", call.Name)
300+
}
301+
if *call.ErrnoRet != errnoRet {
302+
t.Errorf("Wrong error ret for the %s syscall", call.Name)
303+
}
304+
case "lchown":
305+
if call.Action != configs.Trap {
306+
t.Errorf("Wrong conversion for the %s syscall action", call.Name)
307+
}
308+
case "lremovexattr":
309+
if call.Action != configs.Trace {
310+
t.Errorf("Wrong conversion for the %s syscall action", call.Name)
311+
}
312+
case "mbind":
313+
if call.Action != configs.Log {
314+
t.Errorf("Wrong conversion for the %s syscall action", call.Name)
315+
}
316+
case "mknod":
317+
if call.Action != configs.Notify {
318+
t.Errorf("Wrong conversion for the %s syscall action", call.Name)
319+
}
320+
default:
321+
t.Errorf("Unexpected syscall %s found", call.Name)
208322
}
209-
if call.Action != 4 {
210-
t.Error("Wrong conversion for the clone syscall action")
211-
}
212-
213323
}
214324
}
215325

0 commit comments

Comments
 (0)