File: filter_test.go

package info (click to toggle)
golang-github-containers-common 0.50.1%2Bds1-4
  • links: PTS, VCS
  • area: main
  • in suites: bookworm
  • size: 4,440 kB
  • sloc: makefile: 118; sh: 46
file content (63 lines) | stat: -rw-r--r-- 1,458 bytes parent folder | download | duplicates (2)
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
//go:build seccomp
// +build seccomp

package seccomp

import (
	"testing"

	specs "github.com/opencontainers/runtime-spec/specs-go"
	libseccomp "github.com/seccomp/libseccomp-golang"
	"github.com/stretchr/testify/require"
)

func TestBuildFilter(t *testing.T) {
	for _, tc := range []struct {
		given  func() *specs.LinuxSeccomp
		expect func(*libseccomp.ScmpFilter, error)
	}{
		{ // Default profile
			given: func() *specs.LinuxSeccomp {
				sut, err := GetDefaultProfile(nil)
				require.Nil(t, err)
				return sut
			},
			expect: func(filter *libseccomp.ScmpFilter, err error) {
				require.Nil(t, err)
				require.NotNil(t, filter)
			},
		},
		{ // Spec nil
			given: func() *specs.LinuxSeccomp {
				return nil
			},
			expect: func(filter *libseccomp.ScmpFilter, err error) {
				require.Equal(t, ErrSpecNil, err)
				require.Nil(t, filter)
			},
		},
		{ // Spec empty
			given: func() *specs.LinuxSeccomp {
				return &specs.LinuxSeccomp{}
			},
			expect: func(filter *libseccomp.ScmpFilter, err error) {
				require.Equal(t, ErrSpecEmpty, err)
				require.Nil(t, filter)
			},
		},
		{ // Spec to seccomp failed
			given: func() *specs.LinuxSeccomp {
				sut, err := GetDefaultProfile(nil)
				require.Nil(t, err)
				sut.Syscalls[0].Action = "wrong"
				return sut
			},
			expect: func(filter *libseccomp.ScmpFilter, err error) {
				require.NotNil(t, err)
				require.Nil(t, filter)
			},
		},
	} {
		tc.expect(BuildFilter(tc.given()))
	}
}