-
Notifications
You must be signed in to change notification settings - Fork 95
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
kernel_features: fix tests for new KernelConfig
This is a preparation for a fix for: aquasecurity/tracee#851
- Loading branch information
1 parent
c3b9bd6
commit 7b38633
Showing
7 changed files
with
122 additions
and
125 deletions.
There are no files selected for viewing
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -1,98 +1,74 @@ | ||
package helpers | ||
|
||
import ( | ||
"errors" | ||
"testing" | ||
|
||
"github.com/stretchr/testify/assert" | ||
"testing" | ||
) | ||
|
||
func TestGetProcGZConfigByPath(t *testing.T) { | ||
testCases := []struct { | ||
name string | ||
goldenFilePath string | ||
expectedMap KernelConfig | ||
expectedError error | ||
}{ | ||
{ | ||
name: "non-existent", | ||
goldenFilePath: "foobarblahblahblah", | ||
expectedMap: KernelConfig{}, | ||
expectedError: errors.New("could not open foobarblahblahblah: open foobarblahblahblah: no such file or directory"), | ||
}, | ||
{ | ||
name: "invalid zip format", | ||
goldenFilePath: "testdata/tarred_config.tar", | ||
expectedMap: KernelConfig{}, | ||
expectedError: errors.New("gzip: invalid header"), | ||
}, | ||
{ | ||
name: "standard config", | ||
goldenFilePath: "testdata/config_standard.gz", | ||
expectedMap: KernelConfig{CONFIG_BPF: "y", CONFIG_BPF_JIT_ALWAYS_ON: "y", CONFIG_BPF_LSM: "y", CONFIG_BPF_PRELOAD: "y", CONFIG_BPF_PRELOAD_UMD: "m", CONFIG_BPF_SYSCALL: "y", CONFIG_IPV6_SEG6_BPF: "y", CONFIG_NETFILTER_XT_MATCH_BPF: "m"}, | ||
expectedError: nil, | ||
}, | ||
{ | ||
name: "config with comments in it", | ||
goldenFilePath: "testdata/comments_config.gz", | ||
expectedMap: KernelConfig{CONFIG_BPF: "y", CONFIG_BPF_SYSCALL: "y", CONFIG_BPF_PRELOAD_UMD: "m"}, | ||
expectedError: nil, | ||
}, | ||
} | ||
func TestGetKernelConfigValue(t *testing.T) { | ||
|
||
for _, tt := range testCases { | ||
t.Run(tt.name, func(t *testing.T) { | ||
var kconfig KernelConfig = make(map[uint32]string) | ||
err := kconfig.getProcGZConfigByPath(tt.goldenFilePath) | ||
assert.Equal(t, tt.expectedError, err) | ||
assert.Equal(t, tt.expectedMap, kconfig) | ||
}) | ||
} | ||
} | ||
allConfigFiles := []string{"testdata/config_standard.gz", "testdata/config_comments.gz", "testdata/config_comments"} | ||
|
||
func TestGetKernelConfigValue(t *testing.T) { | ||
testCases := []struct { | ||
name string | ||
key uint32 | ||
conf KernelConfig | ||
expectedError error | ||
expectedValue string | ||
testName string | ||
givenOptions []KernelConfigOption | ||
givenValues []interface{} // might either be KernelConfigOptionValue or String | ||
missingOptions []KernelConfigOption // options that will be missing from given config files | ||
}{ | ||
{ | ||
name: "Value present", | ||
key: CONFIG_BPF, | ||
conf: KernelConfig{CONFIG_BPF: "y"}, | ||
expectedError: nil, | ||
expectedValue: "y", | ||
testName: "option ok", | ||
givenOptions: []KernelConfigOption{CONFIG_BPF}, | ||
givenValues: []interface{}{BUILTIN}, | ||
missingOptions: []KernelConfigOption{}, | ||
}, | ||
{ | ||
name: "Value present", | ||
key: CONFIG_BPF, | ||
conf: KernelConfig{CONFIG_BPFILTER: "foo", CONFIG_BPF: "y"}, | ||
expectedError: nil, | ||
expectedValue: "y", | ||
testName: "multiple options ok", | ||
givenOptions: []KernelConfigOption{CONFIG_BPF, CONFIG_BPF_SYSCALL, CONFIG_TEST_BPF, CONFIG_HZ}, | ||
givenValues: []interface{}{BUILTIN, BUILTIN, MODULE, "250"}, | ||
missingOptions: []KernelConfigOption{}, | ||
}, | ||
{ | ||
name: "nil conf", | ||
key: CONFIG_BPF, | ||
conf: nil, | ||
expectedError: errors.New("kernel config value does not exist, it's possible this option is not present in your kernel version or the KernelConfig has not been initialized"), | ||
expectedValue: "", | ||
testName: "multiple options ok with single not ok", | ||
givenOptions: []KernelConfigOption{CONFIG_BPF, CONFIG_BPF_SYSCALL, CONFIG_TEST_BPF, CONFIG_HZ}, | ||
givenValues: []interface{}{MODULE, BUILTIN, MODULE, "250"}, | ||
missingOptions: []KernelConfigOption{CONFIG_BPF}, | ||
}, | ||
{ | ||
name: "Value not present", | ||
key: CONFIG_BPF_JIT, | ||
conf: KernelConfig{CONFIG_BPF: "y"}, | ||
expectedError: errors.New("kernel config value does not exist, it's possible this option is not present in your kernel version or the KernelConfig has not been initialized"), | ||
expectedValue: "", | ||
testName: "multiple options ok with multiple not ok", | ||
givenOptions: []KernelConfigOption{CONFIG_BPF, CONFIG_BPF_SYSCALL, CONFIG_TEST_BPF, CONFIG_HZ, CONFIG_HZ}, | ||
givenValues: []interface{}{MODULE, BUILTIN, MODULE, "250", "500"}, | ||
missingOptions: []KernelConfigOption{CONFIG_BPF, CONFIG_HZ}, | ||
}, | ||
{ | ||
testName: "undefined value", | ||
givenOptions: []KernelConfigOption{0xFFFFFFFF}, | ||
givenValues: []interface{}{UNDEFINED}, // non-existing values will be ignored | ||
missingOptions: []KernelConfigOption{}, | ||
}, | ||
} | ||
|
||
for _, tt := range testCases { | ||
t.Run(tt.name, func(t *testing.T) { | ||
v, err := tt.conf.GetKernelConfigValue(tt.key) | ||
assert.Equal(t, tt.expectedError, err) | ||
assert.Equal(t, tt.expectedValue, v) | ||
for _, tt := range testCases { // for each of the test cases run: | ||
t.Run(tt.testName, func(test *testing.T) { // a test named testName with the following func(): | ||
for _, configFile := range allConfigFiles { | ||
var err error | ||
|
||
// initialize the KernelConfig object | ||
config := KernelConfig{} | ||
err = config.initKernelConfig(configFile) | ||
assert.Equal(test, err, nil) | ||
|
||
// add needed KernelConfigOptions | ||
for pos, option := range tt.givenOptions { | ||
config.AddNeeded(option, tt.givenValues[pos]) | ||
} | ||
|
||
// check amount of missing KernelConfigOptions first | ||
missing := config.CheckMissing() | ||
assert.Equal(test, len(tt.missingOptions), len(missing)) | ||
|
||
// check if missing KernelConfigOptions are the correct ones | ||
assert.ElementsMatch(test, tt.missingOptions, missing) | ||
} | ||
}) | ||
} | ||
} |
Binary file not shown.
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,17 @@ | ||
# this is a comment in the beginning | ||
CONFIG_BPF=y | ||
CONFIG_BPF_LSM=y | ||
CONFIG_BPF_SYSCALL=y | ||
# this is comment in the middle | ||
CONFIG_ARCH_WANT_DEFAULT_BPF_JIT=y | ||
CONFIG_BPF_JIT_ALWAYS_ON=y | ||
CONFIG_BPF_JIT_DEFAULT_ON=y | ||
CONFIG_BPF_PRELOAD=y | ||
CONFIG_BPF_PRELOAD_UMD=m | ||
# CONFIG_BLAH is not set | ||
CONFIG_IPV6_SEG6_BPF=y | ||
CONFIG_NETFILTER_XT_MATCH_BPF=m | ||
CONFIG_HZ_250=y | ||
CONFIG_HZ=250 | ||
CONFIG_TEST_BPF=m | ||
# a comment at the end |
Binary file not shown.
Binary file not shown.
Binary file not shown.