From patchwork Tue Jan 31 16:02:30 2023 Content-Type: text/plain; charset="utf-8" MIME-Version: 1.0 Content-Transfer-Encoding: 7bit X-Patchwork-Submitter: Roberto Sassu X-Patchwork-Id: 13123136 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 4B79BC38142 for ; Tue, 31 Jan 2023 16:03:34 +0000 (UTC) Received: (majordomo@vger.kernel.org) by vger.kernel.org via listexpand id S230060AbjAaQDc (ORCPT ); Tue, 31 Jan 2023 11:03:32 -0500 Received: from lindbergh.monkeyblade.net ([23.128.96.19]:42548 "EHLO lindbergh.monkeyblade.net" rhost-flags-OK-OK-OK-OK) by vger.kernel.org with ESMTP id S229962AbjAaQDb (ORCPT ); Tue, 31 Jan 2023 11:03:31 -0500 Received: from frasgout13.his.huawei.com (frasgout13.his.huawei.com [14.137.139.46]) by lindbergh.monkeyblade.net (Postfix) with ESMTPS id 318CA3A597 for ; Tue, 31 Jan 2023 08:03:19 -0800 (PST) Received: from mail02.huawei.com (unknown [172.18.147.227]) by frasgout13.his.huawei.com (SkyGuard) with ESMTP id 4P5qPq5W3vz9xFGX for ; Tue, 31 Jan 2023 23:55:11 +0800 (CST) Received: from huaweicloud.com (unknown [10.204.63.22]) by APP2 (Coremail) with SMTP id GxC2BwAn+2GzO9ljT5LfAA--.11028S2; Tue, 31 Jan 2023 17:03:05 +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 v2] Introduce expect_pass_if() and expect_fail_if() Date: Tue, 31 Jan 2023 17:02:30 +0100 Message-Id: <20230131160230.2327345-1-roberto.sassu@huaweicloud.com> X-Mailer: git-send-email 2.25.1 MIME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UUU= X-CM-SenderInfo: purev21wro2thvvxqx5xdzvxpfor3voofrz/1tbiAQAOBF1jj4hY2gAAsw 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 --- Changelog v1: - Declare idx variables as local tests/functions.sh | 38 ++++++++++++++++++++++++++++++++++++++ 1 file changed, 38 insertions(+) diff --git a/tests/functions.sh b/tests/functions.sh index 2c4d20536316..ed06040b394d 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 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 ]