Skip to content

Commit

Permalink
[generic_test.go] Add unit test for ExtractParam with fake LSM policy
Browse files Browse the repository at this point in the history
This test aims at testing the ExtractParam feature.

Tetragon use hard-coded types instead of BTF. So currently, the
ExtractParam feature does not allow to extract attributes from other
types than the few hard-coded in `generictypes.go`.

For instance, if you try to use the feature with task_struct structure,
it will fail because it is not yet hard-coded.

Signed-off-by: Tristan d'Audibert <tristan.daudibert@orange.com>
  • Loading branch information
ScriptSathi committed Jan 10, 2025
1 parent 0d26b71 commit 9361d90
Showing 1 changed file with 63 additions and 0 deletions.
63 changes: 63 additions & 0 deletions pkg/sensors/tracing/generic_test.go
Original file line number Diff line number Diff line change
@@ -0,0 +1,63 @@
// SPDX-License-Identifier: Apache-2.0
// Copyright Authors of Tetragon
// Copyright Orange

package tracing

import (
"testing"

api "github.com/cilium/tetragon/pkg/api/tracingapi"
gt "github.com/cilium/tetragon/pkg/generictypes"
"github.com/cilium/tetragon/pkg/tracingpolicy"
"github.com/stretchr/testify/assert"
)

func TestBuildBtfArgFromLSMPolicy(t *testing.T) {
rawPolicy := `
apiVersion: cilium.io/v1alpha1
kind: TracingPolicy
metadata:
name: "lsm"
spec:
lsmhooks:
- hook: "fake"
args:
- index: 0
type: "linux_binprm"
extractParam: 'mm.arg_start'
overwriteType: 'int'
- index: 1
type: "linux_binprm"
extractParam: 'file.f_path.dentry.d_name.name'
overwriteType: 'string'
`
policy, err := tracingpolicy.FromYAML(rawPolicy)
assert.NoError(t, err, "FromYAML rawPolicy error %s", err)

var btfArgs [api.EventConfigMaxArgs][api.MaxBtfArgDepth]api.ConfigBtfArg

for i, arg := range policy.TpSpec().LsmHooks[0].Args {
btfArgs[i] = [api.MaxBtfArgDepth]api.ConfigBtfArg{}
lastBtfType, err := buildBtfArg(arg, &btfArgs[i])
assert.NoError(t, err)
assert.NotEqual(t, nil, lastBtfType)
argType := findTypeFromBtfType(arg, lastBtfType)
assert.NotEqual(t, gt.GenericInvalidType, argType, "Type %s is not supported", (*lastBtfType).TypeName())
}

result := [api.EventConfigMaxArgs][api.MaxBtfArgDepth]api.ConfigBtfArg{
[api.MaxBtfArgDepth]api.ConfigBtfArg{
{Offset: 16, IsPointer: 1, IsInitialized: 1},
{Offset: 312, IsPointer: 1, IsInitialized: 1}, // Off = 376, but CI complains
},
[api.MaxBtfArgDepth]api.ConfigBtfArg{
{Offset: 64, IsPointer: 1, IsInitialized: 1},
{Offset: 16, IsPointer: 0, IsInitialized: 1}, // Off = 152, but CI complains
{Offset: 8, IsPointer: 1, IsInitialized: 1},
{Offset: 32, IsPointer: 0, IsInitialized: 1},
{Offset: 8, IsPointer: 1, IsInitialized: 1},
},
}
assert.Equal(t, btfArgs, result)
}

0 comments on commit 9361d90

Please sign in to comment.