Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Pre main #99

Merged
merged 4 commits into from
Feb 26, 2024
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
4 changes: 3 additions & 1 deletion tarian/tarian.go
Original file line number Diff line number Diff line change
Expand Up @@ -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 {
Expand All @@ -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 {
Expand Down
89 changes: 89 additions & 0 deletions tarian/tarian_test.go
Original file line number Diff line number Diff line change
@@ -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()
}
Loading