Skip to content

Commit

Permalink
SyscallVal: add some simple tests
Browse files Browse the repository at this point in the history
Signed-off-by: Kornilios Kourtis <[email protected]>
  • Loading branch information
kkourt committed Sep 10, 2024
1 parent b751ed7 commit fcdee32
Showing 1 changed file with 83 additions and 0 deletions.
83 changes: 83 additions & 0 deletions pkg/sensors/tracing/syscall_list_test.go
Original file line number Diff line number Diff line change
@@ -0,0 +1,83 @@
package tracing

import (
"runtime"
"testing"

"github.com/stretchr/testify/assert"
"github.com/stretchr/testify/require"
)

func defABI(t *testing.T) string {
abi, err := defaultABI()
assert.Nil(t, err)
return abi
}

func defSysCallPrefix(t *testing.T) string {
switch a := runtime.GOARCH; a {
case "amd64":
return "__x64_"
case "arm64":
return "__arm64_"
default:
t.Fatalf("unsupported arch: %s", a)
return ""
}
}

func TestParseSyscallValue(t *testing.T) {
type testCase struct {
val string
expError bool
expABI, expName string
}

tcs := []testCase{
{val: "sys_dup", expError: false, expABI: defABI(t), expName: "sys_dup"},
{val: "i386/sys_dup", expError: false, expABI: "i386", expName: "sys_dup"},
{val: "__ia32_sys_dup", expError: false, expABI: "i386", expName: "sys_dup"},
{val: "x64/sys_dup", expError: false, expABI: "x64", expName: "sys_dup"},
{val: "__x64_sys_dup", expError: false, expABI: "x64", expName: "sys_dup"},
{val: "__foo_sys_dup", expError: false, expABI: defABI(t), expName: "__foo_sys_dup"},
}

for _, tc := range tcs {
abi, name, err := parseSyscallValue(SyscallVal(tc.val))
if tc.expError {
require.NotNil(t, err)
} else {
require.Nil(t, err)
require.Equal(t, tc.expABI, abi)
require.Equal(t, tc.expName, name)
}
}
}

func TestSyscallValSymbol(t *testing.T) {
type testCase struct {
val string
symExpError bool
symExpVal string
}

tcs := []testCase{
{val: "sys_dup", symExpError: false, symExpVal: defSysCallPrefix(t) + "sys_dup"},
{val: "i386/sys_dup", symExpError: false, symExpVal: "__ia32_sys_dup"},
{val: "__ia32_sys_dup", symExpError: false, symExpVal: "__ia32_sys_dup"},
{val: "x64/sys_dup", symExpError: false, symExpVal: "__x64_sys_dup"},
{val: "__foo_dup", symExpError: false, symExpVal: "__foo_dup"},
{val: "arm64/sys_dup", symExpError: false, symExpVal: "__arm64_sys_dup"},
}

for _, tc := range tcs {
v := SyscallVal(tc.val)
sym, err := v.Symbol()
if tc.symExpError {
require.NotNil(t, err)
} else {
require.Nil(t, err)
require.Equal(t, tc.symExpVal, sym)
}
}
}

0 comments on commit fcdee32

Please sign in to comment.