From patchwork Tue Jan 31 08:59:55 2023 Content-Type: text/plain; charset="utf-8" MIME-Version: 1.0 Content-Transfer-Encoding: 7bit X-Patchwork-Submitter: Roberto Sassu X-Patchwork-Id: 13122583 Return-Path: X-Spam-Checker-Version: SpamAssassin 3.4.0 (2014-02-07) on aws-us-west-2-korg-lkml-1.web.codeaurora.org Received: from vger.kernel.org (vger.kernel.org [23.128.96.18]) by smtp.lore.kernel.org (Postfix) with ESMTP id 954EEC38142 for ; Tue, 31 Jan 2023 09:21:55 +0000 (UTC) Received: (majordomo@vger.kernel.org) by vger.kernel.org via listexpand id S232079AbjAaJVy (ORCPT ); Tue, 31 Jan 2023 04:21:54 -0500 Received: from lindbergh.monkeyblade.net ([23.128.96.19]:50306 "EHLO lindbergh.monkeyblade.net" rhost-flags-OK-OK-OK-OK) by vger.kernel.org with ESMTP id S231201AbjAaJVy (ORCPT ); Tue, 31 Jan 2023 04:21:54 -0500 Received: from frasgout13.his.huawei.com (frasgout13.his.huawei.com [14.137.139.46]) by lindbergh.monkeyblade.net (Postfix) with ESMTPS id 38B5892 for ; Tue, 31 Jan 2023 01:21:52 -0800 (PST) Received: from mail02.huawei.com (unknown [172.18.147.229]) by frasgout13.his.huawei.com (SkyGuard) with ESMTP id 4P5f7d1f0bz9xFg3 for ; Tue, 31 Jan 2023 16:57:17 +0800 (CST) Received: from huaweicloud.com (unknown [10.204.63.22]) by APP2 (Coremail) with SMTP id GxC2BwD3wlnB2dhj9TPeAA--.10220S2; Tue, 31 Jan 2023 10:05:10 +0100 (CET) From: Roberto Sassu To: zohar@linux.ibm.com, dmitry.kasatkin@gmail.com Cc: linux-integrity@vger.kernel.org, vt@altlinux.org, pvorel@suse.cz, stefanb@linux.ibm.com, Roberto Sassu Subject: [PATCH ima-evm-utils] Introduce expect_pass_if() and expect_fail_if() Date: Tue, 31 Jan 2023 09:59:55 +0100 Message-Id: <20230131085955.2312243-1-roberto.sassu@huaweicloud.com> X-Mailer: git-send-email 2.25.1 MIME-Version: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yTuYvjxU4NB_UUUUU X-CM-SenderInfo: purev21wro2thvvxqx5xdzvxpfor3voofrz/1tbiAgAOBF1jj4RRhgABsr X-CFilter-Loop: Reflected Precedence: bulk List-ID: X-Mailing-List: linux-integrity@vger.kernel.org From: Roberto Sassu 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 Reviewed-by: Mimi Zohar --- 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 ]