1
0
Fork 0
mirror of https://github.com/moby/moby.git synced 2022-11-09 12:21:53 -05:00
moby--moby/profiles/seccomp/kernel_linux_test.go

122 lines
3.8 KiB
Go
Raw Normal View History

seccomp: remove dependency on pkg/parsers/kernel This removes the dependency on the `pkg/parsers/kernel` package, because secomp only needs to consider Linux (and no parsing is needed for Windows or Darwin kernel versions). This patch implements the minimum requirements for this implementation: - only `kernel` and `major` versions are considered - `minor` version, `flavor`, and `-rcXX` suffixes are ignored So, for example: - `3.4.54.longterm-1` => `kernel: 3`, `major: 4` - `3.8.0-19-generic` => `kernel: 3`, `major: 8` - `3.10.0-862.2.3.el7.x86_64` => `kernel: 3`, `major: 10` Some systems also omit the `minor` and/or have odd-formatted versions. In context of generating seccomp profiles, both versions below are considered equal; - `3.12.25-gentoo` => `kernel: 3`, `major: 12` - `3.12-1-amd64` => `kernel: 3`, `major: 12` Note that `-rcX` suffixes are also not considered, and thus (e.g.) kernel `5.9-rc1`, `5.9-rc6` and `5.9` are all considered equal. The motivation for ignoring "minor" versions and "flavors" is that; - The upstream kernel only does "kernel.major" releases - While release-candidates exists for kernel (e.g. 5.9-rc5), we don't expect users to write profiles that target a specific release-candidate, and therefore consider (e.g.) kernel `5.9-rc1`, `5.9-rc6` and `5.9` to be equal. - Generally, a seccomp-profile should either be portable, or written for a specific infrastructure (in which case the writer of the profile would know if the kernel-flavors used does/does not support certain things. Signed-off-by: Sebastiaan van Stijn <github@gone.nl>
2020-09-25 09:06:25 -04:00
package seccomp
import (
"fmt"
"testing"
)
func TestGetKernelVersion(t *testing.T) {
version, err := getKernelVersion()
if err != nil {
t.Fatal(err)
}
if version == nil {
t.Fatal("version is nil")
}
if version.kernel == 0 {
t.Fatal("no kernel version")
}
}
// TestParseRelease tests the ParseRelease() function
func TestParseRelease(t *testing.T) {
tests := []struct {
in string
out kernelVersion
expectedErr error
}{
{in: "3.8", out: kernelVersion{kernel: 3, major: 8}},
{in: "3.8.0", out: kernelVersion{kernel: 3, major: 8}},
{in: "3.8.0-19-generic", out: kernelVersion{kernel: 3, major: 8}},
{in: "3.4.54.longterm-1", out: kernelVersion{kernel: 3, major: 4}},
{in: "3.10.0-862.2.3.el7.x86_64", out: kernelVersion{kernel: 3, major: 10}},
{in: "3.12.8tag", out: kernelVersion{kernel: 3, major: 12}},
{in: "3.12-1-amd64", out: kernelVersion{kernel: 3, major: 12}},
{in: "3.12foobar", out: kernelVersion{kernel: 3, major: 12}},
{in: "99.999.999-19-generic", out: kernelVersion{kernel: 99, major: 999}},
{in: "3", expectedErr: fmt.Errorf(`failed to parse kernel version "3": unexpected EOF`)},
{in: "3.", expectedErr: fmt.Errorf(`failed to parse kernel version "3.": EOF`)},
{in: "3a", expectedErr: fmt.Errorf(`failed to parse kernel version "3a": input does not match format`)},
{in: "3.a", expectedErr: fmt.Errorf(`failed to parse kernel version "3.a": expected integer`)},
{in: "a", expectedErr: fmt.Errorf(`failed to parse kernel version "a": expected integer`)},
{in: "a.a", expectedErr: fmt.Errorf(`failed to parse kernel version "a.a": expected integer`)},
{in: "a.a.a-a", expectedErr: fmt.Errorf(`failed to parse kernel version "a.a.a-a": expected integer`)},
{in: "-3", expectedErr: fmt.Errorf(`failed to parse kernel version "-3": expected integer`)},
{in: "-3.", expectedErr: fmt.Errorf(`failed to parse kernel version "-3.": expected integer`)},
{in: "-3.8", expectedErr: fmt.Errorf(`failed to parse kernel version "-3.8": expected integer`)},
{in: "-3.-8", expectedErr: fmt.Errorf(`failed to parse kernel version "-3.-8": expected integer`)},
{in: "3.-8", expectedErr: fmt.Errorf(`failed to parse kernel version "3.-8": expected integer`)},
}
for _, tc := range tests {
tc := tc
t.Run(tc.in, func(t *testing.T) {
version, err := parseRelease(tc.in)
if tc.expectedErr != nil {
if err == nil {
t.Fatal("expected an error")
}
if err.Error() != tc.expectedErr.Error() {
t.Fatalf("expected: %s, got: %s", tc.expectedErr, err)
}
return
}
if err != nil {
t.Fatal("unexpected error:", err)
}
if version == nil {
t.Fatal("version is nil")
}
if version.kernel != tc.out.kernel || version.major != tc.out.major {
t.Fatalf("expected: %d.%d, got: %d.%d", tc.out.kernel, tc.out.major, version.kernel, version.major)
}
})
}
}
func TestKernelGreaterEqualThan(t *testing.T) {
// Get the current kernel version, so that we can make test relative to that
v, err := getKernelVersion()
if err != nil {
t.Fatal(err)
}
tests := []struct {
doc string
in string
expected bool
}{
{
doc: "same version",
in: fmt.Sprintf("%d.%d", v.kernel, v.major),
expected: true,
},
{
doc: "kernel minus one",
in: fmt.Sprintf("%d.%d", v.kernel-1, v.major),
expected: true,
},
{
doc: "kernel plus one",
in: fmt.Sprintf("%d.%d", v.kernel+1, v.major),
expected: false,
},
{
doc: "major plus one",
in: fmt.Sprintf("%d.%d", v.kernel, v.major+1),
expected: false,
},
}
for _, tc := range tests {
tc := tc
t.Run(tc.doc+": "+tc.in, func(t *testing.T) {
ok, err := kernelGreaterEqualThan(tc.in)
if err != nil {
t.Fatal("unexpected error:", err)
}
if ok != tc.expected {
t.Fatalf("expected: %v, got: %v", tc.expected, ok)
}
})
}
}