Skip to content

Commit ea61703

Browse files
committed
test: provide kernel version as a parameter
Signed-off-by: Andrea Terzolo <andrea.terzolo@suse.com>
1 parent f7e385f commit ea61703

3 files changed

Lines changed: 54 additions & 33 deletions

File tree

internal/bpf/policy_values.go

Lines changed: 12 additions & 15 deletions
Original file line numberDiff line numberDiff line change
@@ -88,22 +88,19 @@ func stringPaddedLen(s int) int {
8888
if s <= stringMapSize6 {
8989
return stringMapSize6
9090
}
91-
if kernels.CurrVersionIsGreaterOrEqualThan("5.11") {
92-
if s <= stringMapSize7 {
93-
return stringMapSize7
94-
}
95-
if s <= stringMapSize8 {
96-
return stringMapSize8
97-
}
98-
if s <= stringMapSize9 {
99-
return stringMapSize9
100-
}
101-
return stringMapSize10
91+
if s <= stringMapSize7 {
92+
return stringMapSize7
93+
}
94+
if s <= stringMapSize8 {
95+
return stringMapSize8
96+
}
97+
if s <= stringMapSize9 {
98+
return stringMapSize9
10299
}
103-
return stringMapSize7
100+
return stringMapSize10
104101
}
105102

106-
func argStringSelectorValue(v string, removeNul bool) ([MaxStringMapsSize]byte, int, error) {
103+
func argStringSelectorValue(v string, removeNul bool, currKernelVer int) ([MaxStringMapsSize]byte, int, error) {
107104
if removeNul {
108105
// Remove any trailing nul characters ("\0" or 0x00)
109106
for v[len(v)-1] == 0 {
@@ -119,7 +116,7 @@ func argStringSelectorValue(v string, removeNul bool) ([MaxStringMapsSize]byte,
119116
}
120117

121118
switch {
122-
case kernels.CurrVersionIsLowerThan("5.11"):
119+
case kernels.VersionIsLowerThan(currKernelVer, "5.11"):
123120
// Until 5.11 we have max size of 512
124121
if s > stringMapSize7 {
125122
return ret, 0, errors.New("string is too long")
@@ -137,7 +134,7 @@ func argStringSelectorValue(v string, removeNul bool) ([MaxStringMapsSize]byte,
137134
}
138135

139136
func putValueInMap(m SelectorStringMaps, v string) error {
140-
value, size, err := argStringSelectorValue(v, false)
137+
value, size, err := argStringSelectorValue(v, false, kernels.GetCurrKernelVersion())
141138
if err != nil {
142139
return fmt.Errorf("value %s invalid: %w", v, err)
143140
}

internal/bpf/policy_values_test.go

Lines changed: 33 additions & 18 deletions
Original file line numberDiff line numberDiff line change
@@ -6,6 +6,7 @@ import (
66
"strings"
77
"testing"
88

9+
"github.com/neuvector/runtime-enforcer/internal/kernels"
910
"github.com/stretchr/testify/require"
1011
)
1112

@@ -44,7 +45,7 @@ func TestStringPaddedLen(t *testing.T) {
4445
expected: stringMapSize10,
4546
},
4647
{
47-
// this method has no limits
48+
// this method has no limits in input size
4849
in: stringMapSize10 * 10,
4950
expected: stringMapSize10,
5051
},
@@ -57,32 +58,46 @@ func TestStringPaddedLen(t *testing.T) {
5758
}
5859

5960
func TestArgStringSelectorValue(t *testing.T) {
60-
getExpectedValue := func(v string) [MaxStringMapsSize]byte {
61-
ret := [MaxStringMapsSize]byte{}
62-
copy(ret[:], []byte(v))
63-
return ret
64-
}
6561

6662
tests := []struct {
67-
name string
68-
in string
63+
kernelVer string
64+
len int
65+
hasError bool
6966
}{
7067
{
71-
// example input taken from a kind cluster with cri-containerd
72-
name: "less than 256 bytes",
73-
in: "/usr/bin/cri-containerd",
68+
// this is ok
69+
kernelVer: "5.9",
70+
len: stringMapSize7,
71+
hasError: false,
72+
},
73+
{
74+
kernelVer: "5.9",
75+
len: stringMapSize7 + 1,
76+
hasError: true,
77+
},
78+
{
79+
kernelVer: "5.11",
80+
len: MaxStringMapsSize,
81+
hasError: false,
7482
},
7583
{
76-
// example input taken from a kind cluster with cri-containerd
77-
name: "more than 256 bytes",
78-
in: strings.Repeat("AB", 500),
84+
kernelVer: "5.11",
85+
len: MaxStringMapsSize + 1,
86+
hasError: true,
7987
},
8088
}
8189
for _, tt := range tests {
82-
t.Run(tt.name, func(t *testing.T) {
83-
outBytes, _, err := argStringSelectorValue(tt.in, false)
84-
require.NoError(t, err)
85-
require.Equal(t, getExpectedValue(tt.in), outBytes)
90+
t.Run(fmt.Sprintf("kernel_%s_len_%d", tt.kernelVer, tt.len), func(t *testing.T) {
91+
_, _, err := argStringSelectorValue(
92+
strings.Repeat("a", tt.len),
93+
false,
94+
int(kernels.KernelStringToNumeric(tt.kernelVer)),
95+
)
96+
if tt.hasError {
97+
require.Error(t, err)
98+
} else {
99+
require.NoError(t, err)
100+
}
86101
})
87102
}
88103
}

internal/kernels/kernels.go

Lines changed: 9 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -37,6 +37,15 @@ func getKernelVersionFromSystem() (int, error) {
3737
return int(KernelStringToNumeric(release)), nil
3838
}
3939

40+
func GetCurrKernelVersion() int {
41+
return currKernelVersion
42+
}
43+
44+
func VersionIsLowerThan(curr int, kernel string) bool {
45+
intVersion := int(KernelStringToNumeric(kernel))
46+
return curr < intVersion
47+
}
48+
4049
func CurrVersionIsLowerThan(kernel string) bool {
4150
intVersion := int(KernelStringToNumeric(kernel))
4251
return currKernelVersion < intVersion

0 commit comments

Comments
 (0)