From patchwork Wed Mar 6 21:42:21 2019 Content-Type: text/plain; charset="utf-8" MIME-Version: 1.0 Content-Transfer-Encoding: 7bit X-Patchwork-Submitter: "Tobin C. Harding" X-Patchwork-Id: 10841821 Return-Path: Received: from mail.wl.linuxfoundation.org (pdx-wl-mail.web.codeaurora.org [172.30.200.125]) by pdx-korg-patchwork-2.web.codeaurora.org (Postfix) with ESMTP id 065871575 for ; Wed, 6 Mar 2019 21:43:49 +0000 (UTC) Received: from mail.wl.linuxfoundation.org (localhost [127.0.0.1]) by mail.wl.linuxfoundation.org (Postfix) with ESMTP id E9C972ED0C for ; Wed, 6 Mar 2019 21:43:48 +0000 (UTC) Received: by mail.wl.linuxfoundation.org (Postfix, from userid 486) id DDE0C2EDDF; Wed, 6 Mar 2019 21:43:48 +0000 (UTC) X-Spam-Checker-Version: SpamAssassin 3.3.1 (2010-03-16) on pdx-wl-mail.web.codeaurora.org X-Spam-Level: X-Spam-Status: No, score=-5.2 required=2.0 tests=BAYES_00,DKIM_SIGNED, DKIM_VALID,MAILING_LIST_MULTI,RCVD_IN_DNSWL_MED autolearn=ham version=3.3.1 Received: from mother.openwall.net (mother.openwall.net [195.42.179.200]) by mail.wl.linuxfoundation.org (Postfix) with SMTP id 12F352ED0C for ; Wed, 6 Mar 2019 21:43:47 +0000 (UTC) Received: (qmail 7484 invoked by uid 550); 6 Mar 2019 21:43:37 -0000 Mailing-List: contact kernel-hardening-help@lists.openwall.com; run by ezmlm Precedence: bulk List-Post: List-Help: List-Unsubscribe: List-Subscribe: List-ID: Delivered-To: mailing list kernel-hardening@lists.openwall.com Received: (qmail 7364 invoked from network); 6 Mar 2019 21:43:36 -0000 DKIM-Signature: v=1; a=rsa-sha256; c=relaxed/relaxed; d= messagingengine.com; h=cc:content-transfer-encoding:date:from :in-reply-to:message-id:mime-version:references:subject:to :x-me-proxy:x-me-proxy:x-me-sender:x-me-sender:x-sasl-enc; s= fm2; bh=q01YGeCoIbHEEIB//S+2XygDhqyH/9qJaPDW3fWo93c=; b=8JzgRdlI wlbeEOegIMH/s8GRQ+Yr/us5es7S6oCHaLVTp6BapMgZeLbIwpphXfUyKUJBn6+b SYPsf6gTkFsrG00zG6vabqdSe20TA6bM02pHFrW4yR3eMlVNQcpsspAPhaVJnnTX b1M8Q0ofUtvdD0nXjRPXlDd0EUWMSbvDE7x9gSHS/4gZzzg2abcnPS2a7ETWhkoC yj7zBKabjnau99JyIB2ptNFOftAP05N6Wq9GYL9NMTwA7OoCxejxlpV62RW8ebpb 3L6AReYf0nmbjIsPB7ZQDHakiG3a4h7moAChbyOwhzIUKUtPU0DZxjzQOJ8aT34k 9TTvxX+4g0cjsA== X-ME-Sender: X-ME-Proxy-Cause: gggruggvucftvghtrhhoucdtuddrgedutddrfeeigdekjecutefuodetggdotefrodftvf curfhrohhfihhlvgemucfhrghsthforghilhdpqfgfvfdpuffrtefokffrpgfnqfghnecu uegrihhlohhuthemuceftddtnecusecvtfgvtghiphhivghnthhsucdlqddutddtmdenuc fjughrpefhvffufffkofgjfhgggfestdekredtredttdenucfhrhhomhepfdfvohgsihhn ucevrdcujfgrrhguihhnghdfuceothhosghinheskhgvrhhnvghlrdhorhhgqeenucfkph epuddvgedrudeiledrhedrudehkeenucfrrghrrghmpehmrghilhhfrhhomhepthhosghi nheskhgvrhhnvghlrdhorhhgnecuvehluhhsthgvrhfuihiivgepud X-ME-Proxy: From: "Tobin C. Harding" To: Kees Cook , Shuah Khan Cc: "Tobin C. Harding" , Jann Horn , Andy Shevchenko , Randy Dunlap , Rasmus Villemoes , Stephen Rothwell , Andy Lutomirski , Daniel Micay , Arnd Bergmann , Miguel Ojeda , "Gustavo A. R. Silva" , Greg Kroah-Hartman , Alexander Shishkin , kernel-hardening@lists.openwall.com, linux-kselftest@vger.kernel.org, linux-kernel@vger.kernel.org Subject: [PATCH v3 2/7] kselftest: Add test runner creation script Date: Thu, 7 Mar 2019 08:42:21 +1100 Message-Id: <20190306214226.14598-3-tobin@kernel.org> X-Mailer: git-send-email 2.20.1 In-Reply-To: <20190306214226.14598-1-tobin@kernel.org> References: <20190306214226.14598-1-tobin@kernel.org> MIME-Version: 1.0 X-Virus-Scanned: ClamAV using ClamSMTP Currently if we wish to use kselftest to run tests within a kernel module we write a small script to load/unload and do error reporting. There are a bunch of these under tools/testing/selftests/lib/ that are all identical except for the test name. We can reduce code duplication and improve maintainability if we have one version of this. However kselftest requires an executable for each test. We can move all the script logic to a central script then have each individual test script set the module name and call the main script. There is a little bit of boilerplate left in each script to handle building/running tests with the O=/path/to/out make option. Add test runner creation script. Signed-off-by: Tobin C. Harding Reviewed-by: Kees Cook --- tools/testing/selftests/kselftest_module.sh | 75 +++++++++++++++++++++ 1 file changed, 75 insertions(+) create mode 100755 tools/testing/selftests/kselftest_module.sh diff --git a/tools/testing/selftests/kselftest_module.sh b/tools/testing/selftests/kselftest_module.sh new file mode 100755 index 000000000000..b5d446738614 --- /dev/null +++ b/tools/testing/selftests/kselftest_module.sh @@ -0,0 +1,75 @@ +#!/bin/sh +# SPDX-License-Identifier: GPL-2.0+ + +# +# Runs an individual test module. kselftest expects a separate +# executable for each test. So test should each have an individial +# script that can call this script. +# + +# Individual test scrits should define these: +module="" # filename (without the .ko). +desc="" # Output prefix. + +modprobe="/sbin/modprobe" + +main() { + parse_args $@ + assert_root + assert_have_module + run_module +} + +parse_args() { + script=${0##*/} + + if [[ ! $# -eq 2 ]]; then + echo "Usage: $script [FAIL]" + exit 1 + fi + + module=$1 + desc=$2 +} + +assert_root() { + if [[ $EUID -ne 0 ]]; then + skip "please run as root" + fi +} + +assert_have_module() { + if ! $modprobe -q -n $module; then + skip "module $module is not found" + fi +} + +run_module() { + if $modprobe -q $module; then + $modprobe -q -r $module + say "ok" + else + fail "" + fi +} + +say() { + echo "$desc: $1" +} + + +fail() { + say "$1 [FAIL]" >&2 + exit 1 +} + +skip() { + say "$1 [SKIP]" >&2 + # Kselftest framework requirement - SKIP code is 4. + exit 4 +} + +# +# Main script +# +main $@