All of lore.kernel.org
 help / color / mirror / Atom feed
* [PATCH ima-evm-utils] Introduce expect_pass_if() and expect_fail_if()
@ 2023-01-31  8:59 Roberto Sassu
  2023-01-31 15:22 ` Mimi Zohar
  0 siblings, 1 reply; 2+ messages in thread
From: Roberto Sassu @ 2023-01-31  8:59 UTC (permalink / raw)
  To: zohar, dmitry.kasatkin
  Cc: linux-integrity, vt, pvorel, stefanb, Roberto Sassu

From: Roberto Sassu <roberto.sassu@huawei.com>

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>
---
 tests/functions.sh | 38 ++++++++++++++++++++++++++++++++++++++
 1 file changed, 38 insertions(+)

diff --git a/tests/functions.sh b/tests/functions.sh
index 2c4d20536316..3cc5a792a5e7 100755
--- a/tests/functions.sh
+++ b/tests/functions.sh
@@ -100,6 +100,25 @@ expect_pass() {
   return $ret
 }
 
+expect_pass_if() {
+  local indexes="$1"
+  local ret
+
+  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
+
+  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 ]
-- 
2.25.1


^ permalink raw reply related	[flat|nested] 2+ messages in thread

* Re: [PATCH ima-evm-utils] Introduce expect_pass_if() and expect_fail_if()
  2023-01-31  8:59 [PATCH ima-evm-utils] Introduce expect_pass_if() and expect_fail_if() Roberto Sassu
@ 2023-01-31 15:22 ` Mimi Zohar
  0 siblings, 0 replies; 2+ messages in thread
From: Mimi Zohar @ 2023-01-31 15:22 UTC (permalink / raw)
  To: Roberto Sassu, dmitry.kasatkin
  Cc: linux-integrity, vt, pvorel, stefanb, Roberto Sassu

On Tue, 2023-01-31 at 09:59 +0100, Roberto Sassu wrote:
> From: Roberto Sassu <roberto.sassu@huawei.com>
> 
> 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>

Keeping it simple like this works for now.   Based on Stefan's previous
comments, the variable 'idx' should probably be defined as local.

Otherwise,
Reviewed-by: Mimi Zohar <zohar@linux.ibm.com>


^ permalink raw reply	[flat|nested] 2+ messages in thread

end of thread, other threads:[~2023-01-31 15:25 UTC | newest]

Thread overview: 2+ messages (download: mbox.gz / follow: Atom feed)
-- links below jump to the message on this page --
2023-01-31  8:59 [PATCH ima-evm-utils] Introduce expect_pass_if() and expect_fail_if() Roberto Sassu
2023-01-31 15:22 ` Mimi Zohar

This is an external index of several public inboxes,
see mirroring instructions on how to clone and mirror
all data and code used by this external index.