diff options
| author | Ophestra <cat@gensokyo.uk> | 2025-03-12 15:52:48 +0900 |
|---|---|---|
| committer | Ophestra <cat@gensokyo.uk> | 2025-03-12 15:52:48 +0900 |
| commit | 29c3f8becb875de308be9238f5ca0ba9f35eb7bd (patch) | |
| tree | a0e59b8f753a2024b51f21d4079c9a56fb015834 /helper/seccomp/seccomp_test.go | |
| parent | be16970e770554b3c2191da41eb9bd4472b323e4 (diff) | |
helper/seccomp: improve error handling
This passes both errno and libseccomp return value.
Signed-off-by: Ophestra <cat@gensokyo.uk>
Diffstat (limited to 'helper/seccomp/seccomp_test.go')
| -rw-r--r-- | helper/seccomp/seccomp_test.go | 65 |
1 files changed, 65 insertions, 0 deletions
diff --git a/helper/seccomp/seccomp_test.go b/helper/seccomp/seccomp_test.go new file mode 100644 index 00000000..c04179c1 --- /dev/null +++ b/helper/seccomp/seccomp_test.go @@ -0,0 +1,65 @@ +package seccomp_test + +import ( + "errors" + "runtime" + "syscall" + "testing" + + "git.gensokyo.uk/security/fortify/helper/seccomp" +) + +func TestLibraryError(t *testing.T) { + testCases := []struct { + name string + sample *seccomp.LibraryError + want string + wantIs bool + compare error + }{ + { + "full", + &seccomp.LibraryError{Prefix: "seccomp_export_bpf failed", Seccomp: syscall.ECANCELED, Errno: syscall.EBADF}, + "seccomp_export_bpf failed: operation canceled (bad file descriptor)", + true, + &seccomp.LibraryError{Prefix: "seccomp_export_bpf failed", Seccomp: syscall.ECANCELED, Errno: syscall.EBADF}, + }, + { + "errno only", + &seccomp.LibraryError{Prefix: "seccomp_init failed", Errno: syscall.ENOMEM}, + "seccomp_init failed: cannot allocate memory", + false, + nil, + }, + { + "seccomp only", + &seccomp.LibraryError{Prefix: "internal libseccomp failure", Seccomp: syscall.EFAULT}, + "internal libseccomp failure: bad address", + true, + syscall.EFAULT, + }, + } + for _, tc := range testCases { + t.Run(tc.name, func(t *testing.T) { + if errors.Is(tc.sample, tc.compare) != tc.wantIs { + t.Errorf("errors.Is(%#v, %#v) did not return %v", + tc.sample, tc.compare, tc.wantIs) + } + + if got := tc.sample.Error(); got != tc.want { + t.Errorf("Error: %q, want %q", + got, tc.want) + } + }) + } + + t.Run("invalid", func(t *testing.T) { + wantPanic := "invalid libseccomp error" + defer func() { + if r := recover(); r != wantPanic { + t.Errorf("panic: %q, want %q", r, wantPanic) + } + }() + runtime.KeepAlive(new(seccomp.LibraryError).Error()) + }) +} |
