From 042013bb04819f931e5ebd3d784e7282821833bd Mon Sep 17 00:00:00 2001 From: Ophestra Date: Thu, 6 Nov 2025 00:57:32 +0900 Subject: container/std: syscall JSON adapter This provides cross-platform JSON adapter for syscall number. Signed-off-by: Ophestra --- container/std/seccomp_test.go | 63 +++++++++++++++++++++++++++++++++++++++++++ 1 file changed, 63 insertions(+) create mode 100644 container/std/seccomp_test.go (limited to 'container/std/seccomp_test.go') diff --git a/container/std/seccomp_test.go b/container/std/seccomp_test.go new file mode 100644 index 00000000..91a718d3 --- /dev/null +++ b/container/std/seccomp_test.go @@ -0,0 +1,63 @@ +package std_test + +import ( + "encoding/json" + "errors" + "math" + "reflect" + "syscall" + "testing" + + "hakurei.app/container/std" +) + +func TestScmpSyscall(t *testing.T) { + t.Parallel() + + testCases := []struct { + name string + data string + want std.ScmpSyscall + err error + }{ + {"select", `"select"`, syscall.SYS_SELECT, nil}, + {"clone3", `"clone3"`, std.SYS_CLONE3, nil}, + + {"oob", `-2147483647`, -math.MaxInt32, + &json.UnmarshalTypeError{Value: "number", Type: reflect.TypeFor[string](), Offset: 11}}, + {"name", `"nonexistent_syscall"`, -math.MaxInt32, + std.SyscallNameError("nonexistent_syscall")}, + } + for _, tc := range testCases { + t.Run(tc.name, func(t *testing.T) { + t.Parallel() + + t.Run("decode", func(t *testing.T) { + var got std.ScmpSyscall + if err := json.Unmarshal([]byte(tc.data), &got); !reflect.DeepEqual(err, tc.err) { + t.Fatalf("Unmarshal: error = %#v, want %#v", err, tc.err) + } else if err == nil && got != tc.want { + t.Errorf("Unmarshal: %v, want %v", got, tc.want) + } + }) + if errors.As(tc.err, new(std.SyscallNameError)) { + return + } + + t.Run("encode", func(t *testing.T) { + if got, err := json.Marshal(&tc.want); err != nil { + t.Fatalf("Marshal: error = %v", err) + } else if string(got) != tc.data { + t.Errorf("Marshal: %s, want %s", string(got), tc.data) + } + }) + }) + } + + t.Run("error", func(t *testing.T) { + const want = `invalid syscall name "\x00"` + if got := std.SyscallNameError("\x00").Error(); got != want { + t.Fatalf("Error: %q, want %q", got, want) + } + }) +} -- cgit v1.3.1