kunit: add macro to allow conditionally exposing static symbols to tests
Subsystems (4)
v3 cover kunit: add macro to allow conditionally exposing static symbols to tests
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. Additionally, the patch allows the policy_unpack test to be built as a module.
Changes since v2:
- Add mention of namespacing symbols to the commit message of the second patch.
- Change module name in the second patch from policy_unpack_test to apparmor_policy_unpack_test.
Changes since v1:
- Changed the namespace of exported symbols for the apparmor policy_unpack_test by adding the aa_ prefix.
- Separated the documentation comments for macros in include/kunit/visibility.h.
- Changed copyright date and author for include/kunit/visibility.h.
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 | 33 +++ security/apparmor/Kconfig | 4 +- security/apparmor/Makefile | 3 + security/apparmor/include/policy_unpack.h | 50 +++++ security/apparmor/policy_unpack.c | 238 ++++++++++------------ security/apparmor/policy_unpack_test.c | 69 ++++--- 6 files changed, 229 insertions(+), 168 deletions(-) create mode 100644 include/kunit/visibility.h
base-commit: 0f08f3e2a0186dfb8e33cb46105228eb18448a0e
Signature · 1 line
2.39.0.rc0.267.gcb52ba06e7-goog
PATCH v3 1/2 kunit: add macro to allow conditionally exposing static symbols to tests
Create two macros:
VISIBLE_IF_KUNIT - A macro that sets symbols to be static if CONFIG_KUNIT is not enabled. Otherwise if CONFIG_KUNIT is enabled there is no change to the symbol definition.
EXPORT_SYMBOL_IF_KUNIT(symbol) - Exports symbol into EXPORTED_FOR_KUNIT_TESTING namespace only if CONFIG_KUNIT is enabled. Must use MODULE_IMPORT_NS(EXPORTED_FOR_KUNIT_TESTING) in test file in order to use symbols.
---
Changes since v2:
- No changes to this patch.
Changes since v1:
- Separated the documentation comments for macros.
- Changed copyright date and author for new header file.
include/kunit/visibility.h | 33 +++++++++++++++++++++++++++++++++ 1 file changed, 33 insertions(+) create mode 100644 include/kunit/visibility.h
include/kunit/visibility.h new file +33 -0
@@ -0,0 +1,33 @@ +/* SPDX-License-Identifier: GPL-2.0 */ +/* +* KUnit API to allow symbols to be conditionally visible during KUnit +* testing +* +* Copyright (C) 2022, Google LLC. +* Author: Rae Moar <[email protected]> +*/ + +#ifndef _KUNIT_VISIBILITY_H +#define _KUNIT_VISIBILITY_H + +#if IS_ENABLED (CONFIG_KUNIT ) +/** +* VISIBLE_IF_KUNIT - A macro that sets symbols to be static if +* CONFIG_KUNIT is not enabled. Otherwise if CONFIG_KUNIT is enabled +* there is no change to the symbol definition. +*/ +#define VISIBLE_IF_KUNIT +/** +* EXPORT_SYMBOL_IF_KUNIT(symbol) - Exports symbol into +* EXPORTED_FOR_KUNIT_TESTING namespace only if CONFIG_KUNIT is +* enabled. Must use MODULE_IMPORT_NS(EXPORTED_FOR_KUNIT_TESTING) +* in test file in order to use symbols. +*/ +#define EXPORT_SYMBOL_IF_KUNIT (symbol) EXPORT_SYMBOL_NS(symbol, \ + EXPORTED_FOR_KUNIT_TESTING) +#else +#define VISIBLE_IF_KUNIT static +#define EXPORT_SYMBOL_IF_KUNIT (symbol) +#endif + +#endif /* _KUNIT_VISIBILITY_H */
Signature · 1 line
2.39.0.rc0.267.gcb52ba06e7-goog
PATCH v3 2/2 apparmor: test: make static symbols visible during kunit testing
In reply to Rae MoarUse macros, VISIBLE_IF_KUNIT and EXPORT_SYMBOL_IF_KUNIT, to allow static symbols to be conditionally set to be visible during apparmor_policy_unpack_test, which removes the need to include the testing file in the implementation file.
Change the namespace of the symbols that are now conditionally visible (by adding the prefix aa_) to avoid confusion with symbols of the same name.
Allow the test to be built as a module and namespace the module name from policy_unpack_test to apparmor_policy_unpack_test to improve clarity of the module name.
Provide an example of how static symbols can be dealt with in testing.
---
Changes since v2:
- Add mention of namespacing symbols to the commit message.
- Change module name from policy_unpack_test to apparmor_policy_unpack_test.
Changes since v1:
- Changed the namespace of exported symbols for the apparmor policy_unpack_test by adding the aa_ prefix.
security/apparmor/Kconfig | 4 +- security/apparmor/Makefile | 3 + security/apparmor/include/policy_unpack.h | 50 +++++ security/apparmor/policy_unpack.c | 238 ++++++++++------------ security/apparmor/policy_unpack_test.c | 69 ++++--- 5 files changed, 196 insertions(+), 168 deletions(-)
security/apparmor/Kconfig +2 -2
@@ -106,8 +106,8 @@ config SECURITY_APPARMOR_PARANOID_LOAD Disabling the check will speed up policy loads. config SECURITY_APPARMOR_KUNIT_TEST - bool "Build KUnit tests for policy_unpack.c" if !KUNIT_ALL_TESTS - depends on KUNIT=y && SECURITY_APPARMOR + tristate "Build KUnit tests for policy_unpack.c" if !KUNIT_ALL_TESTS + depends on KUNIT && SECURITY_APPARMOR default KUNIT_ALL_TESTS help This builds the AppArmor KUnit tests.
security/apparmor/Makefile +3 -0
@@ -8,6 +8,9 @@ apparmor-y := apparmorfs.o audit.o capability.o task.o ipc.o lib.o match.o \ resource.o secid.o file.o policy_ns.o label.o mount.o net.o apparmor-$(CONFIG_SECURITY_APPARMOR_HASH) += crypto.o +obj-$(CONFIG_SECURITY_APPARMOR_KUNIT_TEST) += apparmor_policy_unpack_test.o +apparmor_policy_unpack_test-objs += policy_unpack_test.o + clean-files := capability_names.h rlim_names.h net_names.h # Build a lower case string table of address family names
security/apparmor/include/policy_unpack.h +50 -0
60 lines not on this page · raw message
security/apparmor/policy_unpack.c +104 -134
624 lines not on this page · raw message
security/apparmor/policy_unpack_test.c +37 -32
264 lines not on this page · raw message
Signature · 1 line
2.39.0.rc0.267.gcb52ba06e7-goog
John Johansen
maintainerQuoted 66 lines · Rae Moar
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_KUNITis 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_KUNITintmy_func_to_test(){...}The first patch also creates a macro that will export a symbol into a kunit testing namespace only if
CONFIG_KUNITis 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_unpacktest in AppArmor to show an example of how to use both of these macros in order to address the issue of testing static functions. Additionally, the patch allows thepolicy_unpacktest to be built as a module.Changes since v2:
- Add mention of namespacing symbols to the commit message of the second patch.
- Change module name in the second patch frompolicy_unpack_testtoapparmor_policy_unpack_test.Changes since v1:
- Changed the namespace of exported symbols for the apparmorpolicy_unpack_testby adding theaa_prefix.
- Separated the documentation comments for macros ininclude/kunit/visibility.h.
- Changed copyright date and author forinclude/kunit/visibility.h.Rae Moar (2):
kunit: add macro to allow conditionally exposing static symbols to
tests
apparmor: test: make static symbols visible during kunit testinginclude/kunit/visibility.h | 33 +++ security/apparmor/Kconfig | 4 +- security/apparmor/Makefile | 3 + security/apparmor/include/policy_unpack.h | 50 +++++ security/apparmor/policy_unpack.c | 238 ++++++++++------------ security/apparmor/policy_unpack_test.c | 69 ++++--- 6 files changed, 229 insertions(+), 168 deletions(-)
Quoted 4 lines
create mode 100644
include/kunit/visibility.hbase-commit:
0f08f3e2a0186dfb8e33cb46105228eb18448a0e
thanks Rae,
looks good to me, David unless you tell me otherwise I assume this is still going in via the kselftest/kunit tree.
Quoted 71 lines · John Johansen
Quoted 69 lines · Rae Moar
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_KUNITis 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_KUNITintmy_func_to_test(){...}The first patch also creates a macro that will export a symbol into a kunit testing namespace only if
CONFIG_KUNITis 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_unpacktest in AppArmor to show an example of how to use both of these macros in order to address the issue of testing static functions. Additionally, the patch allows thepolicy_unpacktest to be built as a module.Changes since v2:
- Add mention of namespacing symbols to the commit message of the second patch.
- Change module name in the second patch frompolicy_unpack_testtoapparmor_policy_unpack_test.Changes since v1:
- Changed the namespace of exported symbols for the apparmorpolicy_unpack_testby adding theaa_prefix.
- Separated the documentation comments for macros ininclude/kunit/visibility.h.
- Changed copyright date and author forinclude/kunit/visibility.h.Rae Moar (2):
kunit: add macro to allow conditionally exposing static symbols to
tests
apparmor: test: make static symbols visible during kunit testinginclude/kunit/visibility.h | 33 +++ security/apparmor/Kconfig | 4 +- security/apparmor/Makefile | 3 + security/apparmor/include/policy_unpack.h | 50 +++++ security/apparmor/policy_unpack.c | 238 ++++++++++------------ security/apparmor/policy_unpack_test.c | 69 ++++--- 6 files changed, 229 insertions(+), 168 deletions(-)create mode 100644
include/kunit/visibility.h
Quoted 7 lines
Quoted 1 line
base-commit:
0f08f3e2a0186dfb8e33cb46105228eb18448a0ethanks Rae,
looks good to me, David unless you tell me otherwise I assume this is still going in via the kselftest/kunit tree.
Yup, both of these are looking good to me (and work fine on my machine).
I've added these to the queue for the kselftest/kunit tree. There's a chance they won't make it into 6.2, but if not, they should be the first things in the 6.3 branch.
Thanks very much,
-- David