From 5d77188898b6d489d311610ba91ebbb1896e5db7 Mon Sep 17 00:00:00 2001 From: Andrei Fedotov Date: Tue, 23 Jul 2024 19:08:47 +0300 Subject: [PATCH] Add TestKprobeMatchBinariesPrefixLargePath Adding test that has Prefix operator in matchBinaries selector. The file path of test binary (nop) being executed is larger than 256 bytes. Co-authored-by: Mahe Tardy Signed-off-by: Andrei Fedotov --- pkg/sensors/tracing/kprobe_test.go | 44 ++++++++++++++++++++++++++++++ 1 file changed, 44 insertions(+) diff --git a/pkg/sensors/tracing/kprobe_test.go b/pkg/sensors/tracing/kprobe_test.go index 5ecc0d2902b..95d17fae961 100644 --- a/pkg/sensors/tracing/kprobe_test.go +++ b/pkg/sensors/tracing/kprobe_test.go @@ -3886,6 +3886,50 @@ func TestKprobeMatchBinaries(t *testing.T) { }) } +func TestKprobeMatchBinariesPrefixLargePath(t *testing.T) { + if !kernels.EnableLargeProgs() { + t.Skip() + } + repoBinPath := testutils.RepoRootPath("contrib/tester-progs/nop") + tmpDir, err := os.MkdirTemp("/tmp", "tetragon-matchbinlarge-*") + assert.NoError(t, err) + defer os.RemoveAll(tmpDir) + + tmpDirLarge := tmpDir + "/" + strings.Repeat("a", 250) + err = os.Mkdir(tmpDirLarge, 0755) + assert.NoError(t, err) + + tmpBinaryPath := tmpDirLarge + "/nop" + + err = exec.Command("cp", repoBinPath, tmpBinaryPath).Run() + assert.NoError(t, err) + + var doneWG, readyWG sync.WaitGroup + defer doneWG.Wait() + + ctx, cancel := context.WithTimeout(context.Background(), tus.Conf().CmdWaitTime) + defer cancel() + + createCrdFile(t, getMatchBinariesCrd("Prefix", []string{tmpDir})) + + obs, err := observertesthelper.GetDefaultObserverWithFile(t, ctx, testConfigFile, tus.Conf().TetragonLib, observertesthelper.WithMyPid()) + if err != nil { + t.Fatalf("GetDefaultObserverWithFile error: %s", err) + } + observertesthelper.LoopEvents(ctx, t, &doneWG, &readyWG, obs) + readyWG.Wait() + + if err := exec.Command(tmpBinaryPath).Run(); err != nil { + t.Fatalf("failed to run nop: %s", err) + } + + checker := ec.NewUnorderedEventChecker(ec.NewProcessKprobeChecker(""). + WithProcess(ec.NewProcessChecker().WithBinary(sm.Full(tmpBinaryPath))). + WithFunctionName(sm.Full("fd_install"))) + err = jsonchecker.JsonTestCheck(t, checker) + assert.NoError(t, err) +} + // matchBinariesPerfringTest checks that the matchBinaries do correctly // filter the events i.e. it checks that no other events appear. func matchBinariesPerfringTest(t *testing.T, operator string, values []string) {