Files
hakurei/ext/ext_test.go
Ophestra e292031624
All checks were successful
Test / Create distribution (push) Successful in 1m4s
Test / Sandbox (push) Successful in 2m44s
Test / Hakurei (push) Successful in 3m41s
Test / ShareFS (push) Successful in 3m42s
Test / Sandbox (race detector) (push) Successful in 5m4s
Test / Hakurei (race detector) (push) Successful in 6m13s
Test / Flake checks (push) Successful in 1m31s
ext: move lookup test
This was kept in-place to reduce patch size in the previous patch.

Signed-off-by: Ophestra <cat@gensokyo.uk>
2026-03-17 13:44:59 +09:00

77 lines
1.8 KiB
Go

package ext_test
import (
"encoding/json"
"errors"
"math"
"reflect"
"testing"
"hakurei.app/ext"
)
func TestSyscall(t *testing.T) {
t.Parallel()
testCases := []struct {
name string
data string
want ext.SyscallNum
err error
}{
{"epoll_create1", `"epoll_create1"`, ext.SNR_EPOLL_CREATE1, nil},
{"clone3", `"clone3"`, ext.SNR_CLONE3, nil},
{"oob", `-2147483647`, -math.MaxInt32,
&json.UnmarshalTypeError{Value: "number", Type: reflect.TypeFor[string](), Offset: 11}},
{"name", `"nonexistent_syscall"`, -math.MaxInt32,
ext.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 ext.SyscallNum
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(ext.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 := ext.SyscallNameError("\x00").Error(); got != want {
t.Fatalf("Error: %q, want %q", got, want)
}
})
}
func TestSyscallResolveName(t *testing.T) {
t.Parallel()
for name, want := range ext.Syscalls() {
t.Run(name, func(t *testing.T) {
t.Parallel()
if got, ok := ext.SyscallResolveName(name); !ok || got != want {
t.Errorf("SyscallResolveName(%q) = %d, want %d", name, got, want)
}
})
}
}