Skip to content

Commit

Permalink
Merge pull request #99 from intelops/pre-main
Browse files Browse the repository at this point in the history
Pre main
  • Loading branch information
c-ravela authored Feb 26, 2024
2 parents dc0a9e2 + bba8c22 commit 7701e4e
Show file tree
Hide file tree
Showing 2 changed files with 92 additions and 1 deletion.
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()
}

0 comments on commit 7701e4e

Please sign in to comment.