public inbox for linux-kernel@vger.kernel.org
 help / color / mirror / Atom feed
From: Rae Moar <rmoar@google.com>
To: brendanhiggins@google.com, davidgow@google.com, dlatypov@google.com
Cc: skhan@linuxfoundation.org, tales.aparecida@gmail.com,
	john.johansen@canonical.com, kunit-dev@googlegroups.com,
	linux-kernel@vger.kernel.org, linux-kselftest@vger.kernel.org,
	apparmor@lists.ubuntu.com, Rae Moar <rmoar@google.com>
Subject: [PATCH v1 0/2] kunit: add macro to allow conditionally exposing static symbols to tests
Date: Wed,  2 Nov 2022 17:59:57 +0000	[thread overview]
Message-ID: <20221102175959.2921063-1-rmoar@google.com> (raw)

Currently in order to test a static function, tests must be included in the
same translation unit as the function. However, this can cause issues with
including implementation and test code in the same file. As an alternative,
the first patch in this series creates a macro that will set a function to
be static or not depending on whether CONFIG_KUNIT is enabled. This allows
the function to be visible during testing and static otherwise.

As an example, the current status quo to test static functions is:

=== test.c ===

static void test_case(struct kunit *test)
{
  KUNIT_EXPECT_EQ(test, my_func_to_test(), 2);
}

Then the tests are included in the implementation file as a workaround to
the issue of testing static functions:

=== implementation.c ===

static int my_func_to_test() {...}
...
#include "test.c"

Instead, the function could be defined with this new macro:

=== implementation.c ===

VISIBLE_IF_KUNIT int my_func_to_test() {...}

The first patch also creates a macro that will export a symbol into a kunit
testing namespace only if CONFIG_KUNIT is enabled. This follows the logic
above and allows symbols to be conditionally exported based on the testing
status.

The second patch in the series updates the policy_unpack test in AppArmor
to show an example of how to use both of these macros in order to address
the issue of testing static functions.

Rae Moar (2):
  kunit: add macro to allow conditionally exposing static symbols to
    tests
  apparmor: test: make static symbols visible during kunit testing

 include/kunit/visibility.h                | 32 ++++++++++
 security/apparmor/Kconfig                 |  4 +-
 security/apparmor/Makefile                |  2 +
 security/apparmor/include/policy_unpack.h | 50 ++++++++++++++++
 security/apparmor/policy_unpack.c         | 72 +++++++----------------
 security/apparmor/policy_unpack_test.c    |  5 ++
 6 files changed, 112 insertions(+), 53 deletions(-)
 create mode 100644 include/kunit/visibility.h


base-commit: 11e76194937b506caf1b49512c42d5c2588681d7
-- 
2.38.1.273.g43a17bfeac-goog


             reply	other threads:[~2022-11-02 18:03 UTC|newest]

Thread overview: 10+ messages / expand[flat|nested]  mbox.gz  Atom feed  top
2022-11-02 17:59 Rae Moar [this message]
2022-11-02 17:59 ` [PATCH v1 1/2] kunit: add macro to allow conditionally exposing static symbols to tests Rae Moar
2022-11-22  6:13   ` John Johansen
2022-11-23  9:20   ` David Gow
2022-11-02 17:59 ` [PATCH v1 2/2] apparmor: test: make static symbols visible during kunit testing Rae Moar
2022-11-22  6:20   ` John Johansen
2022-11-23  9:19     ` David Gow
2022-11-24  4:49       ` John Johansen
2022-12-05 17:23         ` Rae Moar
2022-11-22  6:13 ` [PATCH v1 0/2] kunit: add macro to allow conditionally exposing static symbols to tests John Johansen

Reply instructions:

You may reply publicly to this message via plain-text email
using any one of the following methods:

* Save the following mbox file, import it into your mail client,
  and reply-to-all from there: mbox

  Avoid top-posting and favor interleaved quoting:
  https://en.wikipedia.org/wiki/Posting_style#Interleaved_style

* Reply using the --to, --cc, and --in-reply-to
  switches of git-send-email(1):

  git send-email \
    --in-reply-to=20221102175959.2921063-1-rmoar@google.com \
    --to=rmoar@google.com \
    --cc=apparmor@lists.ubuntu.com \
    --cc=brendanhiggins@google.com \
    --cc=davidgow@google.com \
    --cc=dlatypov@google.com \
    --cc=john.johansen@canonical.com \
    --cc=kunit-dev@googlegroups.com \
    --cc=linux-kernel@vger.kernel.org \
    --cc=linux-kselftest@vger.kernel.org \
    --cc=skhan@linuxfoundation.org \
    --cc=tales.aparecida@gmail.com \
    /path/to/YOUR_REPLY

  https://kernel.org/pub/software/scm/git/docs/git-send-email.html

* If your mail client supports setting the In-Reply-To header
  via mailto: links, try the mailto: link
Be sure your reply has a Subject: header at the top and a blank line before the message body.
This is a public inbox, see mirroring instructions
for how to clone and mirror all data and code used for this inbox