Skip to content

Commit

Permalink
bpf: fix prepend_name unit tests for new buffer length
Browse files Browse the repository at this point in the history
Signed-off-by: Mahe Tardy <mahe.tardy@gmail.com>
  • Loading branch information
mtardy committed Aug 2, 2024
1 parent c47bb12 commit b28ad1e
Show file tree
Hide file tree
Showing 3 changed files with 46 additions and 21 deletions.
4 changes: 3 additions & 1 deletion Makefile
Original file line number Diff line number Diff line change
Expand Up @@ -264,8 +264,10 @@ test: tester-progs tetragon-bpf ## Run Go tests.
tester-progs: ## Compile helper programs for unit testing.
$(MAKE) -C $(TESTER_PROGS_DIR)

## bpf-test: ## run BPF tests.
## bpf-test BPFGOTESTFLAGS="-v": ## run BPF tests with verbose.
.PHONY: bpf-test
bpf-test: ## Run BPF tests.
bpf-test:
$(MAKE) -C ./bpf run-test

.PHONY: verify
Expand Down
2 changes: 1 addition & 1 deletion bpf/tests/prepend_name_test.c
Original file line number Diff line number Diff line change
Expand Up @@ -13,7 +13,7 @@

char _license[] __attribute__((section("license"), used)) = "Dual BSD/GPL";

#define TEST_MAX_BUF_LEN 256
#define TEST_MAX_BUF_LEN 4096
#define NAME_MAX 255

struct test_prepend_name_state_map_value {
Expand Down
61 changes: 42 additions & 19 deletions bpf/tests/prepend_name_test.go
Original file line number Diff line number Diff line change
Expand Up @@ -18,7 +18,7 @@ import (

const (
// those constants must be synchronized with the BPF code
MAX_BUF_LEN = 256
MAX_BUF_LEN = 4096
NAME_MAX = 255
testPrependNameStateMapName = "test_prepend_name_state_map"
programName = "test_prepend_name"
Expand Down Expand Up @@ -232,44 +232,67 @@ func Test_PrependName(t *testing.T) {
assert.Equal(t, "sr/bin/cat", state.BufferToString())
})

SetupLongDentry := func() string {
// length is 239
const longDentry = "pizza_tomato_mozzarella_basil_pizza_tomato_mozzarella_basil_pizza_tomato_mozzarella_basil_pizza_tomato_mozzarella_basil_pizza_tomato_mozzarella_basil_pizza_tomato_mozzarella_basil_pizza_tomato_mozzarella_basil_pizza_tomato_mozzarella_basil"
// length is 239
const longDentry = "pizza_tomato_mozzarella_basil_pizza_tomato_mozzarella_basil_pizza_tomato_mozzarella_basil_pizza_tomato_mozzarella_basil_pizza_tomato_mozzarella_basil_pizza_tomato_mozzarella_basil_pizza_tomato_mozzarella_basil_pizza_tomato_mozzarella_basil"

t.Run("MaxSizeBufMedium", func(t *testing.T) {
const bufsize = 256
state.ResetStateWithBuflen(bufsize)

err = state.UpdateDentry(longDentry)
assert.NoError(t, err)
code := runPrependName()
assert.Equal(t, 0, code)
assert.Equal(t, "/"+longDentry, state.BufferToString())
return longDentry
}

t.Run("MaxSizeBufFull", func(t *testing.T) {
state.ResetStateWithBuflen(MAX_BUF_LEN)

longDentry := SetupLongDentry()

// length is 15, so 239 + 15 + 2 slash chars = 256
err = state.UpdateDentry("favorite_recipe")
assert.NoError(t, err)
code := runPrependName()
code = runPrependName()
assert.Equal(t, 0, code)
assert.Equal(t, "/favorite_recipe"+"/"+longDentry, state.BufferToString())
assert.Equal(t, MAX_BUF_LEN, len(state.BufferToString()))
assert.Equal(t, bufsize, len(state.BufferToString()))
})

t.Run("MaxSizeBufTooSmall", func(t *testing.T) {
t.Run("MaxSizeBufFull", func(t *testing.T) {
maxDentry := strings.Repeat("a", NAME_MAX)
state.ResetStateWithBuflen(MAX_BUF_LEN)

longDentry := SetupLongDentry()
var expectedState string
// (len("/") + 255) * 16 = 4096
for range 16 {
err = state.UpdateDentry(maxDentry)
assert.NoError(t, err)
code := runPrependName()
assert.Equal(t, 0, code)
expectedState += "/" + maxDentry
assert.Equal(t, expectedState, state.BufferToString())
}
})

t.Run("MaxSizeBufTooSmall", func(t *testing.T) {
largeDentry := strings.Repeat("a", 240)
state.ResetStateWithBuflen(MAX_BUF_LEN)

// length is 16 with the "s" of "recipes", so 240 + 15 + 2 slash chars = 257
err = state.UpdateDentry("favorite_recipes")
var expectedState string
// (len("/") + 240) * 16 = 3856
for range 16 {
err = state.UpdateDentry(largeDentry)
assert.NoError(t, err)
code := runPrependName()
assert.Equal(t, 0, code)
expectedState = "/" + largeDentry + expectedState
assert.Equal(t, expectedState, state.BufferToString())
}
// at this stage, there should be 240 chars left in the buf which leaves
// no space for the remaining root slash character
err = state.UpdateDentry(largeDentry)
assert.NoError(t, err)
code := runPrependName()
assert.Equal(t, -int(unix.ENAMETOOLONG), code)
assert.Equal(t, "favorite_recipes"+"/"+longDentry, state.BufferToString())
assert.Equal(t, MAX_BUF_LEN, len(state.BufferToString()))
// note that I intentionally don't add the '/' char
expectedState = largeDentry + expectedState
assert.Equal(t, expectedState, state.BufferToString())
})

t.Run("MaxSizeBufNormalUse", func(t *testing.T) {
Expand Down

0 comments on commit b28ad1e

Please sign in to comment.