Received: by 2002:a17:90a:8582:0:0:0:0 with SMTP id m2csp2328090pjn; Tue, 2 Apr 2019 14:28:50 -0700 (PDT) X-Google-Smtp-Source: APXvYqz+QG8UZAqHY6FQgsd2p79P15apXJDjNDFzRjj+z23IeHakb1e4dDDkkHg2nmdUVsiAR4bH X-Received: by 2002:a65:50cc:: with SMTP id s12mr67981313pgp.130.1554240530338; Tue, 02 Apr 2019 14:28:50 -0700 (PDT) ARC-Seal: i=1; a=rsa-sha256; t=1554240530; cv=none; d=google.com; s=arc-20160816; b=oHkuhsmlLnP3twMgLhMS+at0UFrovxTahcumealfuShKjTkmtM69GXbinaRlQUx5h2 5Q5ITsNH5HiAcgN5RJpilE29j0IWzQwKTQzblcTm6PeFmIYzcBaghGmQhkaDyC2a/0eO Mfo2bzvnEyUna5hfi3LZtYZFm7tuoStZXRbaHY+rupQS98f8EmpKwdYof1ejLOmpEUXo DFj1+rS8pX/USnRacQNp9VRIJccWNhMIsZOuBFbMVapWrQyPbA2BwTRejSJtPh3uoDMp mqAQZy+/I1j5Z6pPWDhwJnfYPQCdDbAag3/Y/udJ4UIDFa35Qfkp3JqX4seISDJWfFy9 MFhg== ARC-Message-Signature: i=1; a=rsa-sha256; c=relaxed/relaxed; d=google.com; s=arc-20160816; h=list-id:precedence:sender:cc:to:subject:message-id:date:from :in-reply-to:references:mime-version:dkim-signature; bh=qiIybSas6l3Go+QGXlovOiyqOyWhoq4kX0QAlAr0REw=; b=DKslbSvziQPS0eVbivQxVKPNNnU7vnQDS/WuoeLAQuX4NTMhj6f/kwwkbe/GXoLxD/ mVEkxALZzgeJyzBaNKpW00QMF3eb9JuC6I+GNz7eW1uLxhw/HhqpxEZxmIMUNFCwAszH 9fCD1eLYCnoTGqbue9AdKS4mGW4vIMe/kQLojfBFUukE1aBFngO+ZWZa7ukuTcyTwoGy 75pDrrpKYI31tO6C7zmyWo2neakr65FLVuRobQyzrxCEkpplGfJhKaeHFx+YXJwDmsID AhOnB8GpFtfGV25C1AbX5UTxXygu1JBe38W2aejH5gf1SE8U3n2ir8Rk0xHJo6pF5E41 jX6g== ARC-Authentication-Results: i=1; mx.google.com; dkim=pass header.i=@chromium.org header.s=google header.b=NO0CC482; spf=pass (google.com: best guess record for domain of linux-kernel-owner@vger.kernel.org designates 209.132.180.67 as permitted sender) smtp.mailfrom=linux-kernel-owner@vger.kernel.org; dmarc=pass (p=NONE sp=NONE dis=NONE) header.from=chromium.org Return-Path: Received: from vger.kernel.org (vger.kernel.org. [209.132.180.67]) by mx.google.com with ESMTP id 91si12103642pla.14.2019.04.02.14.28.34; Tue, 02 Apr 2019 14:28:50 -0700 (PDT) Received-SPF: pass (google.com: best guess record for domain of linux-kernel-owner@vger.kernel.org designates 209.132.180.67 as permitted sender) client-ip=209.132.180.67; Authentication-Results: mx.google.com; dkim=pass header.i=@chromium.org header.s=google header.b=NO0CC482; spf=pass (google.com: best guess record for domain of linux-kernel-owner@vger.kernel.org designates 209.132.180.67 as permitted sender) smtp.mailfrom=linux-kernel-owner@vger.kernel.org; dmarc=pass (p=NONE sp=NONE dis=NONE) header.from=chromium.org Received: (majordomo@vger.kernel.org) by vger.kernel.org via listexpand id S1726316AbfDBV1i (ORCPT + 99 others); Tue, 2 Apr 2019 17:27:38 -0400 Received: from mail-vs1-f66.google.com ([209.85.217.66]:46007 "EHLO mail-vs1-f66.google.com" rhost-flags-OK-OK-OK-OK) by vger.kernel.org with ESMTP id S1725963AbfDBV1i (ORCPT ); Tue, 2 Apr 2019 17:27:38 -0400 Received: by mail-vs1-f66.google.com with SMTP id n14so8651094vsp.12 for ; Tue, 02 Apr 2019 14:27:37 -0700 (PDT) DKIM-Signature: v=1; a=rsa-sha256; c=relaxed/relaxed; d=chromium.org; s=google; h=mime-version:references:in-reply-to:from:date:message-id:subject:to :cc; bh=qiIybSas6l3Go+QGXlovOiyqOyWhoq4kX0QAlAr0REw=; b=NO0CC48215QJX+LvelRD6HW7H3fHFnoJuYQuFV5NlHIHgBVXT9OUGd0Pk9EBuK3AhX U3wniD5FUh4a326ZPT0EiC+1czgqvNkwhBWmswFLo081atSBOhBKJJMiIf5XKZ+r3uHf 15b7iODK9dlFQsLopQUgPOornurFJG9r2UBEk= X-Google-DKIM-Signature: v=1; a=rsa-sha256; c=relaxed/relaxed; d=1e100.net; s=20161025; h=x-gm-message-state:mime-version:references:in-reply-to:from:date :message-id:subject:to:cc; bh=qiIybSas6l3Go+QGXlovOiyqOyWhoq4kX0QAlAr0REw=; b=psumDR0sGPLfV1r3W1zk0poOdWoHOZrN4jYqNXWEZX3KIhUndxeXn2lqVAS0kSrrwq vW2xnMbLPXPp4m6QpoBY6wZwdv58IXiZVrrsFYV6s2RB66b1SFLh+fZnWpZ+bNLXfOu6 YT2xXVH8Gc/b7LT146WyIx3mI8REa//t21v+7yWMDntbJtm5c70Mtf90QkyL53j3LiF3 8Rs41i7JXQl+pqpiuUIBwV+aSz+OFC+AcsX6TWFBICIr+Y/KNPaWcWdqTvVcJf6fVXPS Ifj/PdQPo3W8dX5Fd+5jlCtXuL9tOSmV49sSI//Vd0rLwtkXOOvsC1eNBZy25Yi1XnyA x9DQ== X-Gm-Message-State: APjAAAWn7d1gvO6AMxE0MWRYKejPnKkju1QDG1GIb1GzdudaMVbdU4XE 9yPGPSMYzypEn5q35JjfgEL/eRhVsew= X-Received: by 2002:a67:828f:: with SMTP id e137mr43363765vsd.131.1554240456537; Tue, 02 Apr 2019 14:27:36 -0700 (PDT) Received: from mail-vk1-f176.google.com (mail-vk1-f176.google.com. [209.85.221.176]) by smtp.gmail.com with ESMTPSA id t201sm5129232vke.15.2019.04.02.14.27.33 for (version=TLS1_2 cipher=ECDHE-RSA-AES128-GCM-SHA256 bits=128/128); Tue, 02 Apr 2019 14:27:33 -0700 (PDT) Received: by mail-vk1-f176.google.com with SMTP id h71so3335909vkf.5 for ; Tue, 02 Apr 2019 14:27:33 -0700 (PDT) X-Received: by 2002:a1f:29c5:: with SMTP id p188mr42395945vkp.24.1554240452727; Tue, 02 Apr 2019 14:27:32 -0700 (PDT) MIME-Version: 1.0 References: <20190306214226.14598-1-tobin@kernel.org> <20190306214226.14598-3-tobin@kernel.org> In-Reply-To: <20190306214226.14598-3-tobin@kernel.org> From: Kees Cook Date: Tue, 2 Apr 2019 14:27:21 -0700 X-Gmail-Original-Message-ID: Message-ID: Subject: Re: [PATCH v3 2/7] kselftest: Add test runner creation script To: "Tobin C. Harding" Cc: Shuah Khan , 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 , "open list:KERNEL SELFTEST FRAMEWORK" , LKML Content-Type: text/plain; charset="UTF-8" Sender: linux-kernel-owner@vger.kernel.org Precedence: bulk List-ID: X-Mailing-List: linux-kernel@vger.kernel.org On Wed, Mar 6, 2019 at 1:43 PM Tobin C. Harding wrote: > > 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 > --- > 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 Probably will need a way to pass arguments into the modprobe here, but otherwise looks fine. > + 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 $@ > -- > 2.20.1 > Reviewed-by: Kees Cook -- Kees Cook