1
0
mirror of https://git.code.sf.net/p/linux-ima/ima-evm-utils synced 2025-04-27 22:32:31 +02:00

Introduce expect_pass_if() and expect_fail_if()

Introduce these functions to let the developer specify which kernel patches
are required for the tests to be successful (either pass or fail). If a
test is not successful, print those patches in the test result summary.

First, the developer should declare an array, named PATCHES, with the list
of all kernel patches that are required by the tests. For example:

PATCHES=(
'patch 1 title'
...
'patch N title'
)

Second, the developer could replace the existing expect_pass() and
expect_fail() respectively with expect_pass_if() and expect_fail_if(), and
add the indexes in the PATCHES array as the first argument, enclosed with
quotes. The other parameters of expect_pass() and expect_fail() remain the
same.

In the following example, the PATCHES array has been added to a new test
script, tests/mmap_check.test:

PATCHES=(
'ima: Align ima_file_mmap() parameters with mmap_file LSM hook'
'ima: Introduce MMAP_CHECK_REQPROT hook'
)

Then, expect_pass() has been replaced with expect_pass_if():

expect_pass_if '0' check_mmap "MMAP_CHECK" "read_implies_exec"

The resulting output when a test fails (if the required patch is not
applied) is:

Test: check_mmap (hook="MMAP_CHECK", test_mmap arg: "read_implies_exec")
Result (expect found): not found
Possibly missing patches:
 - ima: Align ima_file_mmap() parameters with mmap_file LSM hook

Signed-off-by: Roberto Sassu <roberto.sassu@huawei.com>
Signed-off-by: Mimi Zohar <zohar@linux.ibm.com>
This commit is contained in:
Roberto Sassu 2023-01-31 17:02:30 +01:00 committed by Mimi Zohar
parent 8f6ba073a0
commit 1d3a0b6923

View File

@ -100,6 +100,25 @@ expect_pass() {
return $ret
}
expect_pass_if() {
local indexes="$1"
local ret idx
shift
expect_pass "$@"
ret=$?
if [ $ret -ne 0 ] && [ $ret -ne 77 ] && [ -n "$PATCHES" ]; then
echo $YELLOW"Possibly missing patches:"$NORM
for idx in $indexes; do
echo $YELLOW" - ${PATCHES[$((idx))]}"$NORM
done
fi
return $ret
}
# Eval negative test (one that should fail) and account its result
expect_fail() {
local ret
@ -137,6 +156,25 @@ expect_fail() {
return $ret
}
expect_fail_if() {
local indexes="$1"
local ret idx
shift
expect_fail "$@"
ret=$?
if { [ $ret -eq 0 ] || [ $ret -eq 99 ]; } && [ -n "$PATCHES" ]; then
echo $YELLOW"Possibly missing patches:"$NORM
for idx in $indexes; do
echo $YELLOW" - ${PATCHES[$((idx))]}"$NORM
done
fi
return $ret
}
# return true if current test is positive
_test_expected_to_pass() {
[ ! $TFAIL ]