From 89b26aa0a13510258d208ed038fe71cbb951deea Mon Sep 17 00:00:00 2001 From: Charan Ravela <124630918+c-ravela@users.noreply.github.com> Date: Mon, 26 Feb 2024 10:39:12 -0600 Subject: [PATCH 1/2] feat: add functional test cases --- tarian/tarian_test.go | 89 +++++++++++++++++++++++++++++++++++++++++++ 1 file changed, 89 insertions(+) create mode 100644 tarian/tarian_test.go diff --git a/tarian/tarian_test.go b/tarian/tarian_test.go new file mode 100644 index 0000000..6c3c876 --- /dev/null +++ b/tarian/tarian_test.go @@ -0,0 +1,89 @@ +// SPDX-License-Identifier: Apache-2.0 +// Copyright 2024 Authors of Tarian & the Organization created Tarian + +package tarian + +import ( + "fmt" + "os" + "testing" + + ebpf "github.com/intelops/tarian-detector/pkg/eBPF" +) + +func setup(major, minor, patch any) { + os.Setenv("LINUX_VERSION_MAJOR", fmt.Sprintf("%v", major)) + os.Setenv("LINUX_VERSION_MINOR", fmt.Sprintf("%v", minor)) + os.Setenv("LINUX_VERSION_PATCH", fmt.Sprintf("%v", patch)) +} + +func teardown() { + os.Unsetenv("LINUX_VERSION_MAJOR") + os.Unsetenv("LINUX_VERSION_MINOR") + os.Unsetenv("LINUX_VERSION_PATCH") +} + +// TestGetModule_Probe_count tests the GetModule function with a specific probe count. +func TestGetModule_Probe_count(t *testing.T) { + setup(5, 8, 0) + got, err := GetModule() + + if err != nil { + t.Errorf("GetModule() error = %v", err) + } + + probeCount := 16 * 2 + if len(got.GetPrograms()) != probeCount { + t.Errorf("GetModule() = %v, want %v", len(got.GetPrograms()), probeCount) + } + + teardown() +} + +// TestGetModule_Perf_Check tests the GetModule function for the map type PerfEventArray +func TestGetModule_Perf_Check(t *testing.T) { + setup(5, 6, 0) + got, err := GetModule() + + if err != nil { + t.Errorf("GetModule() error = %v", err) + } + + if got.GetMap().GetMapType() != ebpf.PerfEventArray { + t.Errorf("GetModule().ebpfMap = %v, want %v", got.GetMap().GetMapType(), ebpf.PerfEventArray) + } + + teardown() +} + +// TestGetModule_Ring_Check tests the GetModule function for the map type PerfEventArray +func TestGetModule_Ring_Check(t *testing.T) { + setup(5, 19, 0) + got, err := GetModule() + + if err != nil { + t.Errorf("GetModule() error = %v", err) + } + + // this is intended to be a perf event as we are currently only supporting perf events + // once we were able to create an array ring buffer directly then we would need to change this test + if got.GetMap().GetMapType() != ebpf.PerfEventArray { + t.Errorf("GetModule().ebpfMap = %v, want %v", got.GetMap().GetMapType(), ebpf.PerfEventArray) + } + + teardown() +} + +// TestGetModule_Kernel_Version_Err tests the GetModule function with invalid arguments +func TestGetModule_Kernel_Version_Err(t *testing.T) { + setup("ab", "cd", "ef") // invalid arguments + + _, err := GetModule() + + if err == nil { + t.Errorf("GetModule() error = %v, wantErr %v", err, "true") + + } + + teardown() +} From 913bd021b7647050f3b52d2e1d67ed35b666762a Mon Sep 17 00:00:00 2001 From: Charan Ravela <124630918+c-ravela@users.noreply.github.com> Date: Mon, 26 Feb 2024 10:40:07 -0600 Subject: [PATCH 2/2] feat: add docstrings and comments --- tarian/tarian.go | 4 +++- 1 file changed, 3 insertions(+), 1 deletion(-) diff --git a/tarian/tarian.go b/tarian/tarian.go index 90af60e..ee09a29 100644 --- a/tarian/tarian.go +++ b/tarian/tarian.go @@ -16,6 +16,8 @@ var tarianErr = err.New("tarian.tarian") //go:generate go run github.com/cilium/ebpf/cmd/bpf2go -cc clang -cflags $BPF_CFLAGS -target $CURR_ARCH tarian c/tarian.bpf.c -- -I../headers -I./c +// GetModule loads the ebpf specs like maps, programs and structures from a file.\ +// It returns a *ebpf.Module and an error, if any. func GetModule() (*ebpf.Module, error) { bpfObjs, err := getBpfObject() if err != nil { @@ -30,7 +32,7 @@ func GetModule() (*ebpf.Module, error) { tarianDetectorModule := ebpf.NewModule("tarian_detector") ckv, err := utils.CurrentKernelVersion() if err != nil { - return nil, tarianErr.Throwf("%v", err) + return nil, tarianErr.Throwf("failed to get current kernel version: %v", err) } if ckv >= utils.KernelVersion(5, 8, 0) && false {