kunit: Add macros to help write more complex tests
v2 cover kunit: Add macros to help write more complex tests
v1: https://groups.google.com/g/kunit-dev/c/f4LIMLyofj8
v2: make it more complex and attempt to be thread safe
s/FIXED_STUB/GLOBAL_STUB (David, Lucas)
make it little more thread safe (Rae, David) wait until stub call finishes before test end (David) wait until stub call finishes before changing stub (David) allow stub deactivation (Rae)
prefer kunit log (David)
add simple selftest (Michal)
also introduce ONLY_IF_KUNIT macro (Michal)
Sample output from the tests:
$ tools/testing/kunit/kunit.py run *example*.*global* \
--kunitconfig lib/kunit/.kunitconfig --raw_output KTAP version 1
1..1
# example: initializing suite
KTAP version 1
# Subtest: example
# module: kunit_example_test
1..1
# example_global_stub_test: initializing
# example_global_stub_test: add_two: redirecting to subtract_one # example_global_stub_test: add_two: redirecting to subtract_one # example_global_stub_test: cleaning up
ok 1 example_global_stub_test
# example: exiting suite
ok 1 example
$ tools/testing/kunit/kunit.py run *global*.*global* \
--kunitconfig lib/kunit/.kunitconfig --raw_output
KTAP version 1
1..1
KTAP version 1
# Subtest: kunit_global_stub
# module: kunit_test
1..4
# kunit_global_stub_test_activate: real_void_func: redirecting to replacement_void_func # kunit_global_stub_test_activate: real_func: redirecting to replacement_func # kunit_global_stub_test_activate: real_func: redirecting to replacement_func # kunit_global_stub_test_activate: real_func: redirecting to other_replacement_func # kunit_global_stub_test_activate: real_func: redirecting to other_replacement_func # kunit_global_stub_test_activate: real_func: redirecting to super_replacement_func # kunit_global_stub_test_activate: real_func: redirecting to super_replacement_func ok 1 kunit_global_stub_test_activate
ok 2 kunit_global_stub_test_deactivate
# kunit_global_stub_test_slow_deactivate: real_func: redirecting to slow_replacement_func # kunit_global_stub_test_slow_deactivate: real_func: redirecting to slow_replacement_func # kunit_global_stub_test_slow_deactivate: waiting for slow_replacement_func # kunit_global_stub_test_slow_deactivate.speed: slow
ok 3 kunit_global_stub_test_slow_deactivate
# kunit_global_stub_test_slow_replace: real_func: redirecting to slow_replacement_func # kunit_global_stub_test_slow_replace: real_func: redirecting to slow_replacement_func # kunit_global_stub_test_slow_replace: waiting for slow_replacement_func # kunit_global_stub_test_slow_replace: real_func: redirecting to other_replacement_func # kunit_global_stub_test_slow_replace.speed: slow
ok 4 kunit_global_stub_test_slow_replace
# kunit_global_stub: pass:4 fail:0 skip:0 total:4
# Totals: pass:4 fail:0 skip:0 total:4
ok 1 kunit_global_stub
Michal Wajdeczko (6):
kunit: Introduce kunit_is_running()
kunit: Add macro to conditionally expose declarations to tests
kunit: Add macro to conditionally expose expressions to tests
kunit: Allow function redirection outside of the KUnit thread
kunit: Add example with alternate function redirection method
kunit: Add some selftests for global stub redirection macros
include/kunit/static_stub.h | 158 ++++++++++++++++++++ include/kunit/test-bug.h | 12 +- include/kunit/visibility.h | 16 +++ lib/kunit/kunit-example-test.c | 67 +++++++++ lib/kunit/kunit-test.c | 254 ++++++++++++++++++++++++++++++++- lib/kunit/static_stub.c | 49 +++++++ 6 files changed, 553 insertions(+), 3 deletions(-)
Signature · 1 line
2.43.0
PATCH v2 1/6 kunit: Introduce kunit_is_running()
Wrap uses of the static key 'kunit_running' into a helper macro to allow future checks to be placed in the code residing outside of the CONFIG_KUNIT. We will start using this in upcoming patch.
--- include/kunit/test-bug.h | 12 ++++++++++-- 1 file changed, 10 insertions(+), 2 deletions(-)
include/kunit/test-bug.h +10 -2
@@ -25,6 +25,13 @@ extern struct kunit_hooks_table {void *(*get_static_stub_address)(struct kunit *test,void *real_fn_addr); } kunit_hooks; +/** +* kunit_is_running() - True, if KUnit test is currently running. +* +* If CONFIG_KUNIT is not enabled, it will compile down to a false. +*/ +#define kunit_is_running () static_branch_unlikely(&kunit_running) +/** * kunit_get_current_test() - Return a pointer to the currently running * KUnit test.
@@ -40,7 +47,7 @@ extern struct kunit_hooks_table {*/ static inline struct kunit *kunit_get_current_test (void ) { -if (!static_branch_unlikely (&kunit_running)) +if (!kunit_is_running ())return NULL ;return current->kunit_test;
@@ -53,7 +60,7 @@ static inline struct kunit *kunit_get_current_test(void)* If a KUnit test is running in the current task, mark that test as failed. */ #define kunit_fail_current_test(fmt, ...) do { \ - if (static_branch_unlikely(&kunit_running)) { \ + if (kunit_is_running()) { \ /* Guaranteed to be non-NULL when kunit_running true*/ \ kunit_hooks.fail_current_test(__FILE__, __LINE__, \ fmt, ##__VA_ARGS__); \
@@ -64,6 +71,7 @@ static inline struct kunit *kunit_get_current_test(void)static inline struct kunit *kunit_get_current_test (void ) {return NULL ; } +#define kunit_is_running () false#define kunit_fail_current_test (fmt, ...) do {} while (0)#endif
Signature · 1 line
2.43.0
PATCH v2 2/6 kunit: Add macro to conditionally expose declarations to tests
In reply to Michal WajdeczkoThe DECLARE_IF_KUNIT macro will introduces identifiers only if CONFIG_KUNIT is enabled. Otherwise if CONFIG_KUNIT is not enabled no identifiers from the param list will be defined.
---
Cc: Lucas De Marchi <[email protected]>
--- include/kunit/visibility.h | 8 ++++++++ 1 file changed, 8 insertions(+)
include/kunit/visibility.h +8 -0
@@ -11,6 +11,13 @@#define _KUNIT_VISIBILITY_H#if IS_ENABLED (CONFIG_KUNIT ) +/** +* DECLARE_IF_KUNIT - A macro that introduces identifiers only if +* CONFIG_KUNIT is enabled. Otherwise if CONFIG_KUNIT is not enabled +* no identifiers will be defined. +* @body: identifiers to be introduced conditionally +*/ + #define DECLARE_IF_KUNIT(body...) body/** * VISIBLE_IF_KUNIT - A macro that sets symbols to be static if * CONFIG_KUNIT is not enabled. Otherwise if CONFIG_KUNIT is enabled
@@ -26,6 +33,7 @@#define EXPORT_SYMBOL_IF_KUNIT (symbol) EXPORT_SYMBOL_NS(symbol, \ EXPORTED_FOR_KUNIT_TESTING)#else + #define DECLARE_IF_KUNIT(body...)#define VISIBLE_IF_KUNIT static#define EXPORT_SYMBOL_IF_KUNIT (symbol)#endif
Signature · 1 line
2.43.0
Re: [PATCH v2 2/6] kunit: Add macro to conditionally expose declarations to tests
Quoted 9 lines · Michal Wajdeczko
The
DECLARE_IF_KUNITmacro will introduces identifiers only ifCONFIG_KUNITis enabled. Otherwise ifCONFIG_KUNITis not enabled no identifiers from the param list will be defined.Signed-off-by Michal Wajdeczko <[email protected]>Reviewed-by Rae Moar <[email protected]>Reviewed-by David Gow <[email protected]>---
Cc: Lucas De Marchi <[email protected]>
up to kunit maintainers, but it doesn't seem the word "DECLARE" is entirely correct. What it's doing is expanding arg, and it doesn't matter if it's an expression, definition, declaration.
Looking at patch 3, I think it would be more obvious to the caller if we have:
IF_KUNIT_ELSE_EMPTY()IF_KUNIT_ELSE_ZERO()
Quoted 9 lines
---
include/kunit/visibility.h| 8 ++++++++1 file changed, 8 insertions(+)include/kunit/visibility.h +0
-0
include/kunit/visibility.h line 18
Quoted 8 lines
include/kunit/visibility.h +5
-0@@ -11,6 +11,13 @@#define _KUNIT_VISIBILITY_H#if IS_ENABLED (CONFIG_KUNIT ) +/** +* DECLARE_IF_KUNIT - A macro that introduces identifiers only if +* CONFIG_KUNIT is enabled. Otherwise if CONFIG_KUNIT is not enabled +* no identifiers will be defined. +* @body: identifiers to be introduced conditionally
missing an example here with fields inside a struct
Lucas De Marchi
Quoted 16 lines
include/kunit/visibility.h +3
-0+*/ + #define DECLARE_IF_KUNIT(body...) body/** * VISIBLE_IF_KUNIT - A macro that sets symbols to be static if * CONFIG_KUNIT is not enabled. Otherwise if CONFIG_KUNIT is enabled @@ -26,6 +33,7 @@#define EXPORT_SYMBOL_IF_KUNIT (symbol) EXPORT_SYMBOL_NS(symbol, \ EXPORTED_FOR_KUNIT_TESTING)#else + #define DECLARE_IF_KUNIT(body...)#define VISIBLE_IF_KUNIT static#define EXPORT_SYMBOL_IF_KUNIT (symbol)#endif Signature · 1 line
2.43.0
Re: [PATCH v2 2/6] kunit: Add macro to conditionally expose declarations to tests
Quoted 11 lines · Lucas De Marchi
Quoted 9 lines · Michal Wajdeczko
The
DECLARE_IF_KUNITmacro will introduces identifiers only ifCONFIG_KUNITis enabled. Otherwise ifCONFIG_KUNITis not enabled no identifiers from the param list will be defined.Signed-off-by Michal Wajdeczko <[email protected]>Reviewed-by Rae Moar <[email protected]>Reviewed-by David Gow <[email protected]>---
Cc: Lucas De Marchi <[email protected]>
Quoted 3 lines
up to kunit maintainers, but it doesn't seem the word "DECLARE" is entirely correct. What it's doing is expanding arg, and it doesn't matter if it's an expression, definition, declaration.
hmm, while this is true for statement & declarations (as both have similar notation) but not sure about the expression (that's why we have patch 3)
Quoted 6 lines
Looking at patch 3, I think it would be more obvious to the caller if we have:
IF_KUNIT_ELSE_EMPTY()IF_KUNIT_ELSE_ZERO()
existing macros in this file are named as xxx_IF_KUNIT so maybe we should try to follow that pattern...
so maybe (like BUILD_BUG_ON_ZERO)
ONLY_IF_KUNIT(body...) ONLY_IF_KUNIT_ZERO(expr...)
Quoted 12 lines
Quoted 12 lines
---
include/kunit/visibility.h| 8 ++++++++1 file changed, 8 insertions(+)include/kunit/visibility.h +0
-0@@ -11,6 +11,13 @@#define _KUNIT_VISIBILITY_H
Quoted 8 lines
include/kunit/visibility.h line 18
Quoted 6 lines
include/kunit/visibility.h +5
-0#if IS_ENABLED (CONFIG_KUNIT ) + /** +* DECLARE_IF_KUNIT - A macro that introduces identifiers only if +* CONFIG_KUNIT is enabled. Otherwise if CONFIG_KUNIT is not enabled +* no identifiers will be defined. +* @body: identifiers to be introduced conditionally missing an example here with fields inside a struct
would this work?
Example:
struct example {
int foo;
/* private: test only */
DECLARE_IF_KUNIT(int bar);
};Quoted 19 lines
Lucas De Marchi
Quoted 16 lines
include/kunit/visibility.h +3
-0+*/ + #define DECLARE_IF_KUNIT(body...) body /*** VISIBLE_IF_KUNIT - A macro that sets symbols to be static if * CONFIG_KUNIT is not enabled. Otherwise if CONFIG_KUNIT is enabled @@ -26,6 +33,7 @@ #define EXPORT_SYMBOL_IF_KUNIT(symbol) EXPORT_SYMBOL_NS(symbol, \ EXPORTED_FOR_KUNIT_TESTING)#else + #define DECLARE_IF_KUNIT(body...) #define VISIBLE_IF_KUNIT static #define EXPORT_SYMBOL_IF_KUNIT(symbol)#endif --
2.43.0
PATCH v2 3/6 kunit: Add macro to conditionally expose expressions to tests
In reply to Michal WajdeczkoThe ONLY_IF_KUNIT macro will add expression statement only if the CONFIG_KUNIT is enabled. Otherwise if CONFIG_KUNIT is not enabled it will evaluate always to 0.
---
Cc: Rae Moar <[email protected]>
Cc: David Gow <[email protected]>
Cc: Lucas De Marchi <[email protected]>
--- include/kunit/visibility.h | 8 ++++++++ 1 file changed, 8 insertions(+)
include/kunit/visibility.h +8 -0
@@ -18,6 +18,13 @@* @body: identifiers to be introduced conditionally */ #define DECLARE_IF_KUNIT(body...) body +/** +* ONLY_IF_KUNIT - A macro that adds expression statement only if +* CONFIG_KUNIT is enabled. Otherwise if CONFIG_KUNIT is not enabled +* it will evaluate always to 0. +* @expr: expression to be introduced conditionally +*/ + #define ONLY_IF_KUNIT(expr...) expr/** * VISIBLE_IF_KUNIT - A macro that sets symbols to be static if * CONFIG_KUNIT is not enabled. Otherwise if CONFIG_KUNIT is enabled
@@ -34,6 +41,7 @@ EXPORTED_FOR_KUNIT_TESTING)#else #define DECLARE_IF_KUNIT(body...) + #define ONLY_IF_KUNIT(expr...) 0#define VISIBLE_IF_KUNIT static#define EXPORT_SYMBOL_IF_KUNIT (symbol)#endif
Signature · 1 line
2.43.0
Re: [PATCH v2 3/6] kunit: Add macro to conditionally expose expressions to tests
Quoted 6 lines · Michal Wajdeczko
The
ONLY_IF_KUNITmacro will add expression statement only if theCONFIG_KUNITis enabled. Otherwise ifCONFIG_KUNITis not enabled it will evaluate always to 0.Signed-off-by Michal Wajdeczko <[email protected]>
Hello!
Thanks for the second version of this patch series!
I definitely could see this new macro as being useful but I currently don't see an example of its use in the rest of the patch series. How do you see this macro as being used or do you have a current use case for this macro?
I would be fine adding this macro without being used as long as examples on how and why to use it are clearly documented.
Thanks!
-Rae
Quoted 37 lines
---
Cc: Rae Moar <[email protected]>
Cc: David Gow <[email protected]>
Cc: Lucas De Marchi <[email protected]>--- include/kunit/visibility.h | 8 ++++++++ 1 file changed, 8 insertions(+)include/kunit/visibility.h +8
-1@@ -18,6 +18,13 @@* @body: identifiers to be introduced conditionally */ #define DECLARE_IF_KUNIT(body...) body +/** +* ONLY_IF_KUNIT - A macro that adds expression statement only if +* CONFIG_KUNIT is enabled. Otherwise if CONFIG_KUNIT is not enabled +* it will evaluate always to 0. +* @expr: expression to be introduced conditionally +*/ + #define ONLY_IF_KUNIT(expr...) expr/** * VISIBLE_IF_KUNIT - A macro that sets symbols to be static if * CONFIG_KUNIT is not enabled. Otherwise if CONFIG_KUNIT is enabled @@ -34,6 +41,7 @@ EXPORTED_FOR_KUNIT_TESTING)#else #define DECLARE_IF_KUNIT(body...) + #define ONLY_IF_KUNIT(expr...) 0#define VISIBLE_IF_KUNIT static#define EXPORT_SYMBOL_IF_KUNIT (symbol)#endif --2.43.0
Re: [PATCH v2 3/6] kunit: Add macro to conditionally expose expressions to tests
Quoted 9 lines · Rae Moar
Quoted 6 lines · Michal Wajdeczko
The
ONLY_IF_KUNITmacro will add expression statement only if theCONFIG_KUNITis enabled. Otherwise ifCONFIG_KUNITis not enabled it will evaluate always to 0.Signed-off-by Michal Wajdeczko <[email protected]>
Quoted 8 lines
Hello!
Thanks for the second version of this patch series!
I definitely could see this new macro as being useful but I currently don't see an example of its use in the rest of the patch series. How do you see this macro as being used or do you have a current use case for this macro?
in Xe driver we have this macro defined as XE_TEST_ONLY [1]
[1] https://elixir.bootlin.com/linux/v6.11-rc5/A/ident/XE_TEST_ONLY
Quoted 3 lines
I would be fine adding this macro without being used as long as examples on how and why to use it are clearly documented.
sure, I'll try to add some usage in the example patch 5/6
Quoted 41 lines
Thanks!
-RaeQuoted 37 lines
---
Cc: Rae Moar <[email protected]>
Cc: David Gow <[email protected]>
Cc: Lucas De Marchi <[email protected]>--- include/kunit/visibility.h | 8 ++++++++ 1 file changed, 8 insertions(+)include/kunit/visibility.h +8
-1@@ -18,6 +18,13 @@* @body: identifiers to be introduced conditionally */ #define DECLARE_IF_KUNIT(body...) body +/** +* ONLY_IF_KUNIT - A macro that adds expression statement only if +* CONFIG_KUNIT is enabled. Otherwise if CONFIG_KUNIT is not enabled +* it will evaluate always to 0. +* @expr: expression to be introduced conditionally +*/ + #define ONLY_IF_KUNIT(expr...) expr/** * VISIBLE_IF_KUNIT - A macro that sets symbols to be static if * CONFIG_KUNIT is not enabled. Otherwise if CONFIG_KUNIT is enabled @@ -34,6 +41,7 @@ EXPORTED_FOR_KUNIT_TESTING)#else #define DECLARE_IF_KUNIT(body...) + #define ONLY_IF_KUNIT(expr...) 0#define VISIBLE_IF_KUNIT static#define EXPORT_SYMBOL_IF_KUNIT (symbol)#endif --2.43.0
PATCH v2 4/6 kunit: Allow function redirection outside of the KUnit thread
In reply to Michal WajdeczkoCurrently, the 'static stub' API only allows function redirection for calls made from the kthread of the current test, which prevents the use of this functionality when the tested code is also used by other threads, outside of the KUnit test, like from the workqueue.
Add another set of macros to allow redirection to the replacement functions, which, unlike the KUNIT_STATIC_STUB_REDIRECT, will affect all calls done during the test execution.
These new stubs, named 'global', must be declared using dedicated KUNIT_DECLARE_GLOBAL_STUB() macro and then can be placed either as global static variables or as part of the other structures.
To properly maintain stubs lifecycle, they can be activated only from the main KUnit context. Some precaution is taken to avoid changing or deactivating replacement functions if one is still used by other thread.
---
Cc: Rae Moar <[email protected]>
Cc: David Gow <[email protected]>
Cc: Daniel Latypov <[email protected]>
Cc: Lucas De Marchi <[email protected]>
---
v2: s/FIXED_STUB/GLOBAL_STUB (David, Lucas)
make it little more thread safe (Rae, David) wait until stub call finishes before test end (David) wait until stub call finishes before changing stub (David) allow stub deactivation (Rae)
prefer kunit log (David)
--- include/kunit/static_stub.h | 158 ++++++++++++++++++++++++++++++++++++ lib/kunit/static_stub.c | 49 +++++++++++ 2 files changed, 207 insertions(+)
include/kunit/static_stub.h +158 -0
@@ -12,12 +12,15 @@/* If CONFIG_KUNIT is not enabled, these stubs quietly disappear. */ #define KUNIT_STATIC_STUB_REDIRECT(real_fn_name, args...) do {} while (0) +#define KUNIT_GLOBAL_STUB_REDIRECT(stub_name, args...) do {} while (0) +#define KUNIT_DECLARE_GLOBAL_STUB (stub_name, stub_type)#else #include <kunit/test.h> #include <kunit/test-bug.h> +#include <linux/cleanup.h> /* for CLASS */ #include <linux/compiler.h> /* for {un,}likely() */ #include <linux/sched.h> /* for task_struct */
@@ -109,5 +112,160 @@ void __kunit_activate_static_stub(struct kunit *test,*/ void kunit_deactivate_static_stub (struct kunit *test,void *real_fn_addr); +/** +* struct kunit_global_stub - Represents a context of global function stub. +* @replacement: The address of replacement function. +* @owner: The KUnit test that owns the stub, valid only when @busy > 0. +* @busy: The stub busyness counter incremented on entry to the replacement +* function, decremented on exit, used to signal if the stub is idle. +* @idle: The completion state to indicate when the stub is idle again. +* +* This structure is for KUnit internal use only. +* See KUNIT_DECLARE_GLOBAL_STUB(). +*/ +struct kunit_global_stub { +void *replacement; +struct kunit *owner; +atomic_t busy; +struct completion idle; +}; + +/** +* KUNIT_DECLARE_GLOBAL_STUB() - Declare a global function stub. +* @stub_name: The name of the stub, must be a valid identifier +* @stub_type: The type of the function that this stub will replace +* +* This macro will declare new identifier of an anonymous type that will +* represent global stub function that could be used by KUnit. It can be stored +* outside of the KUnit code. If the CONFIG_KUNIT is not enabled this will +* be evaluated to an empty statement. +* +* The anonymous type introduced by this macro is mostly a wrapper to generic +* struct kunit_global_stub but with additional dummy member, that is never +* used directly, but is needed to maintain the type of the stub function. +*/ +#define KUNIT_DECLARE_GLOBAL_STUB (stub_name, stub_type) \ +union { \ + struct kunit_global_stub base; \ + typeof(stub_type) dummy; \ +} stub_name + +/* Internal struct to define guard class */ +struct kunit_global_stub_guard { +struct kunit_global_stub *stub; +void *active_replacement; +}; + +/* Internal class used to guard stub calls */ +DEFINE_CLASS (kunit_global_stub_guard, + struct kunit_global_stub_guard, + ({ +struct kunit_global_stub *stub = _T.stub; +bool active = !!_T.active_replacement; + +if (active && !atomic_dec_return (&stub->busy)) +complete_all (&stub->idle); + }), + ({ +class_kunit_global_stub_guard_t guard; +bool active = !!atomic_inc_not_zero (&stub->busy); + + guard.stub = stub; + guard.active_replacement = active ?READ_ONCE (stub->replacement) :NULL ; + + guard; + }), + struct kunit_global_stub *stub) + +/** +* KUNIT_GLOBAL_STUB_REDIRECT() - Call a fixed function stub if activated. +* @stub: The function stub declared using KUNIT_DECLARE_GLOBAL_STUB() +* @args: All of the arguments passed to this stub +* +* This is a function prologue which is used to allow calls to the current +* function to be redirected if a KUnit is running. If the KUnit is not +* running or stub is not yet activated the function will continue execution +* as normal. +* +* The function stub must be declared with KUNIT_DECLARE_GLOBAL_STUB() that is +* stored in a place that is accessible from both the test code, which will +* activate this stub using kunit_activate_global_stub(), and from the function, +* where we will do this redirection using KUNIT_GLOBAL_STUB_REDIRECT(). +* +* Unlike the KUNIT_STATIC_STUB_REDIRECT(), this redirection will work +* even if the caller is not in a KUnit context (like a worker thread). +* +* Example: +* +* .. code-block:: c +* +* KUNIT_DECLARE_GLOBAL_STUB(func_stub, int (*)(int n)); +* +* int real_func(int n) +* { +* KUNIT_GLOBAL_STUB_REDIRECT(func_stub, n); +* return n + 1; +* } +* +* int replacement_func(int n) +* { +* return n + 100; +* } +* +* void example_test(struct kunit *test) +* { +* KUNIT_EXPECT_EQ(test, real_func(1), 2); +* kunit_activate_global_stub(test, func_stub, replacement_func); +* KUNIT_EXPECT_EQ(test, real_func(1), 101); +* } + */ +#define KUNIT_GLOBAL_STUB_REDIRECT(stub, args...) do { \ + if (kunit_is_running()) { \ + typeof(stub) *__stub = &(stub); \ + CLASS(kunit_global_stub_guard, guard)(&__stub->base); \ + typeof(__stub->dummy) replacement = guard.active_replacement; \ + if (unlikely(replacement)) { \ + kunit_info(__stub->base.owner, "%s: redirecting to %ps\n", \ + __func__, replacement); \ + return replacement(args); \ + } \ + } \ +} while (0) + +void __kunit_activate_global_stub (struct kunit *test,struct kunit_global_stub *stub, +void *replacement_addr); + +/** +* kunit_activate_global_stub() - Setup a fixed function stub. +* @test: Test case that wants to activate a fixed function stub +* @stub: The location of the function stub pointer +* @replacement: The replacement function +* +* This helper setups a function stub with the replacement function. +* It will also automatically deactivate the stub at the test end. +* +* The redirection can be disabled with kunit_deactivate_global_stub(). +* The stub must be declared using KUNIT_DECLARE_GLOBAL_STUB(). +*/ +#define kunit_activate_global_stub (test, stub, replacement) do { \ + typeof(stub) *__stub = &(stub); \ + typecheck_fn(typeof(__stub->dummy), (replacement)); \ + __kunit_activate_global_stub((test), &__stub->base, (replacement)); \ +} while (0) + +void __kunit_deactivate_global_stub (struct kunit *test,struct kunit_global_stub *stub); + +/** +* kunit_deactivate_global_stub() - Disable a fixed function stub. +* @test: Test case that wants to deactivate a fixed function stub +* @stub: The location of the function stub pointer +* +* The stub must be declared using KUNIT_DECLARE_GLOBAL_STUB(). +*/ +#define kunit_deactivate_global_stub (test, stub) do { \ + typeof(stub) *__stub = &(stub); \ + __kunit_deactivate_global_stub((test), &__stub->base); \ +} while (0) +#endif #endif
lib/kunit/static_stub.c +49 -0
52 lines not on this page · raw message
Signature · 1 line
2.43.0
Re: [PATCH v2 4/6] kunit: Allow function redirection outside of the KUnit thread
Quoted 100 lines · Michal Wajdeczko
Currently, the 'static stub' API only allows function redirection for calls made from the kthread of the current test, which prevents the use of this functionality when the tested code is also used by other threads, outside of the KUnit test, like from the workqueue.
Add another set of macros to allow redirection to the replacement functions, which, unlike the
KUNIT_STATIC_STUB_REDIRECT, will affect all calls done during the test execution.These new stubs, named 'global', must be declared using dedicated
KUNIT_DECLARE_GLOBAL_STUB()macro and then can be placed either as global static variables or as part of the other structures.To properly maintain stubs lifecycle, they can be activated only from the main KUnit context. Some precaution is taken to avoid changing or deactivating replacement functions if one is still used by other thread.
Signed-off-by Michal Wajdeczko <[email protected]>---
Cc: Rae Moar <[email protected]>
Cc: David Gow <[email protected]>
Cc: Daniel Latypov <[email protected]>
Cc: Lucas De Marchi <[email protected]>
---
v2: s/FIXED_STUB/GLOBAL_STUB (David, Lucas)
make it little more thread safe (Rae, David)
wait until stub call finishes before test end (David) wait until stub call finishes before changing stub (David) allow stub deactivation (Rae)
prefer kunit log (David)
---include/kunit/static_stub.h| 158 ++++++++++++++++++++++++++++++++++++lib/kunit/static_stub.c| 49 +++++++++++2 files changed, 207 insertions(+)include/kunit/static_stub.h +43
-0@@ -12,12 +12,15 @@/* If CONFIG_KUNIT is not enabled, these stubs quietly disappear. */ #define KUNIT_STATIC_STUB_REDIRECT(real_fn_name, args...) do {} while (0) +#define KUNIT_GLOBAL_STUB_REDIRECT(stub_name, args...) do {} while (0) +#define KUNIT_DECLARE_GLOBAL_STUB (stub_name, stub_type)#else #include <kunit/test.h> #include <kunit/test-bug.h> +#include <linux/cleanup.h> /* for CLASS */ #include <linux/compiler.h> /* for {un,}likely() */ #include <linux/sched.h> /* for task_struct */ @@ -109,5 +112,160 @@ void __kunit_activate_static_stub(struct kunit *test,*/ void kunit_deactivate_static_stub (struct kunit *test,void *real_fn_addr); +/** +* struct kunit_global_stub - Represents a context of global function stub. +* @replacement: The address of replacement function. +* @owner: The KUnit test that owns the stub, valid only when @busy > 0. +* @busy: The stub busyness counter incremented on entry to the replacement +* function, decremented on exit, used to signal if the stub is idle. +* @idle: The completion state to indicate when the stub is idle again. +* +* This structure is for KUnit internal use only. +* See KUNIT_DECLARE_GLOBAL_STUB(). +*/ +struct kunit_global_stub { +void *replacement; +struct kunit *owner; +atomic_t busy; +struct completion idle; +}; + +/** +* KUNIT_DECLARE_GLOBAL_STUB() - Declare a global function stub. +* @stub_name: The name of the stub, must be a valid identifier +* @stub_type: The type of the function that this stub will replace +* +* This macro will declare new identifier of an anonymous type that will +* represent global stub function that could be used by KUnit. It can be stored +* outside of the KUnit code. If the CONFIG_KUNIT is not enabled this will +* be evaluated to an empty statement. +* +* The anonymous type introduced by this macro is mostly a wrapper to generic +* struct kunit_global_stub but with additional dummy member, that is never +* used directly, but is needed to maintain the type of the stub function. +*/ +#define KUNIT_DECLARE_GLOBAL_STUB (stub_name, stub_type) \ +union { \ + struct kunit_global_stub base; \ + typeof(stub_type) dummy; \ +} stub_name + +/* Internal struct to define guard class */ +struct kunit_global_stub_guard {
include/kunit/static_stub.h void __kunit_activate_static_stub(struct kunit *test, · line 164
Quoted 10 lines
include/kunit/static_stub.h +10
-0+struct kunit_global_stub *stub; +void *active_replacement; +}; + +/* Internal class used to guard stub calls */ +DEFINE_CLASS(kunit_global_stub_guard, + struct kunit_global_stub_guard, + ({ + struct kunit_global_stub *stub = _T.stub; + bool active = !!_T.active_replacement;
I'd call this bool active_replacement as it's not the same thing as the active below.
Quoted 65 lines
include/kunit/static_stub.h +65
-0+ +if (active && !atomic_dec_return (&stub->busy)) +complete_all (&stub->idle); + }), + ({ + class_kunit_global_stub_guard_t guard; + bool active = !!atomic_inc_not_zero(&stub->busy); + + guard.stub = stub; + guard.active_replacement = active ? READ_ONCE(stub->replacement) : NULL; + + guard; + }), + struct kunit_global_stub *stub) + +/** +* KUNIT_GLOBAL_STUB_REDIRECT() - Call a fixed function stub if activated. +* @stub: The function stub declared using KUNIT_DECLARE_GLOBAL_STUB() +* @args: All of the arguments passed to this stub +* +* This is a function prologue which is used to allow calls to the current +* function to be redirected if a KUnit is running. If the KUnit is not +* running or stub is not yet activated the function will continue execution +* as normal. +* +* The function stub must be declared with KUNIT_DECLARE_GLOBAL_STUB() that is +* stored in a place that is accessible from both the test code, which will +* activate this stub using kunit_activate_global_stub(), and from the function, +* where we will do this redirection using KUNIT_GLOBAL_STUB_REDIRECT(). +* +* Unlike the KUNIT_STATIC_STUB_REDIRECT(), this redirection will work +* even if the caller is not in a KUnit context (like a worker thread). +* +* Example: +* +* .. code-block:: c +* +* KUNIT_DECLARE_GLOBAL_STUB(func_stub, int (*)(int n)); +* +* int real_func(int n) +* { +* KUNIT_GLOBAL_STUB_REDIRECT(func_stub, n); +* return n + 1; +* } +* +* int replacement_func(int n) +* { +* return n + 100; +* } +* +* void example_test(struct kunit *test) +* { +* KUNIT_EXPECT_EQ(test, real_func(1), 2); +* kunit_activate_global_stub(test, func_stub, replacement_func); +* KUNIT_EXPECT_EQ(test, real_func(1), 101); +* } +*/ +#define KUNIT_GLOBAL_STUB_REDIRECT(stub, args...) do { \ + if (kunit_is_running()) { \ + typeof(stub) *__stub = &(stub); \ + CLASS(kunit_global_stub_guard, guard)(&__stub->base); \ + typeof(__stub->dummy) replacement = guard.active_replacement; \ + if (unlikely(replacement)) { \ + kunit_info(__stub->base.owner, "%s: redirecting to %ps\n", \ + __func__, replacement); \
include/kunit/static_stub.h void __kunit_activate_static_stub(struct kunit *test, · line 239
Quoted 10 lines
include/kunit/static_stub.h +10
-0+return replacement (args); \ + } \ + } \ +}while (0 ) + +void __kunit_activate_global_stub(struct kunit *test, struct kunit_global_stub *stub, + void *replacement_addr); + +/** +* kunit_activate_global_stub() - Setup a fixed function stub.
s/fixed/global/ here and every where else below
Quoted 37 lines
include/kunit/static_stub.h +30
-0+* @test: Test case that wants to activate a fixed function stub +* @stub: The location of the function stub pointer +* @replacement: The replacement function +* +* This helper setups a function stub with the replacement function. +* It will also automatically deactivate the stub at the test end. +* +* The redirection can be disabled with kunit_deactivate_global_stub(). +* The stub must be declared using KUNIT_DECLARE_GLOBAL_STUB(). +*/ +#define kunit_activate_global_stub (test, stub, replacement) do { \ + typeof(stub) *__stub = &(stub); \ + typecheck_fn(typeof(__stub->dummy), (replacement)); \ + __kunit_activate_global_stub((test), &__stub->base, (replacement)); \ +} while (0) + +void __kunit_deactivate_global_stub (struct kunit *test,struct kunit_global_stub *stub); + +/** +* kunit_deactivate_global_stub() - Disable a fixed function stub. +* @test: Test case that wants to deactivate a fixed function stub +* @stub: The location of the function stub pointer +* +* The stub must be declared using KUNIT_DECLARE_GLOBAL_STUB(). +*/ +#define kunit_deactivate_global_stub (test, stub) do { \ + typeof(stub) *__stub = &(stub); \ + __kunit_deactivate_global_stub((test), &__stub->base); \ +} while (0) +#endif #endif lib/kunit/static_stub.c +0
-0
lib/kunit/static_stub.c void __kunit_activate_static_stub(struct kunit *test, · line 130
Quoted 10 lines
lib/kunit/static_stub.c +7
-0@@ -121,3 +121,52 @@ void __kunit_activate_static_stub(struct kunit *test, } }EXPORT_SYMBOL_GPL (__kunit_activate_static_stub); + +static void sanitize_global_stub (void *data) +{ +struct kunit *test =kunit_get_current_test (); +struct kunit_global_stub *stub = data; + +KUNIT_EXPECT_NE (test,0 ,atomic_read (&stub->busy));
shouldn't sanitize_ be unconditional and do nothing in this case?
lib/kunit/static_stub.c void __kunit_activate_static_stub(struct kunit *test, · line 136
Quoted 6 lines
lib/kunit/static_stub.c +6
-0+KUNIT_EXPECT_PTR_EQ (test, test,READ_ONCE (stub->owner)); + +reinit_completion (&stub->idle); +if (!atomic_dec_and_test (&stub->busy)) { +kunit_info (test,"waiting for %ps\n" , stub->replacement); +KUNIT_EXPECT_EQ (test,0 ,wait_for_completion_interruptible (&stub->idle));
what's preventing stub->busy going to 1 again after this?
Lucas De Marchi
Quoted 39 lines
lib/kunit/static_stub.c +36
-0+ } + +WRITE_ONCE (stub->owner,NULL ); +WRITE_ONCE (stub->replacement,NULL ); +} + +/* +* Helper function for kunit_activate_global_stub(). The macro does +* typechecking, so use it instead. +*/ +void __kunit_activate_global_stub (struct kunit *test, +struct kunit_global_stub *stub, +void *replacement_addr) +{ +KUNIT_ASSERT_NOT_ERR_OR_NULL (test, stub); +KUNIT_ASSERT_NOT_ERR_OR_NULL (test, replacement_addr); +if (atomic_read (&stub->busy)) +kunit_release_action (test, sanitize_global_stub, stub); +else +init_completion (&stub->idle); +WRITE_ONCE (stub->owner, test); +WRITE_ONCE (stub->replacement, replacement_addr); +KUNIT_ASSERT_EQ (test,1 ,atomic_inc_return (&stub->busy)); +KUNIT_ASSERT_EQ (test,0 ,kunit_add_action_or_reset (test, sanitize_global_stub, stub)); +} +EXPORT_SYMBOL_GPL (__kunit_activate_global_stub); + +/* +* Helper function for kunit_deactivate_global_stub(). Use it instead. +*/ +void __kunit_deactivate_global_stub (struct kunit *test,struct kunit_global_stub *stub) +{ +if (atomic_read (&stub->busy)) +kunit_release_action (test, sanitize_global_stub, stub); +} +EXPORT_SYMBOL_GPL (__kunit_deactivate_global_stub);Signature · 1 line
2.43.0
Re: [PATCH v2 4/6] kunit: Allow function redirection outside of the KUnit thread
Quoted 116 lines · Lucas De Marchi
Quoted 115 lines · Michal Wajdeczko
Currently, the 'static stub' API only allows function redirection for calls made from the kthread of the current test, which prevents the use of this functionality when the tested code is also used by other threads, outside of the KUnit test, like from the workqueue.
Add another set of macros to allow redirection to the replacement functions, which, unlike the
KUNIT_STATIC_STUB_REDIRECT, will affect all calls done during the test execution.These new stubs, named 'global', must be declared using dedicated
KUNIT_DECLARE_GLOBAL_STUB()macro and then can be placed either as global static variables or as part of the other structures.To properly maintain stubs lifecycle, they can be activated only from the main KUnit context. Some precaution is taken to avoid changing or deactivating replacement functions if one is still used by other thread.
Signed-off-by Michal Wajdeczko <[email protected]>---
Cc: Rae Moar <[email protected]>
Cc: David Gow <[email protected]>
Cc: Daniel Latypov <[email protected]>
Cc: Lucas De Marchi <[email protected]>
---
v2: s/FIXED_STUB/GLOBAL_STUB (David, Lucas)
make it little more thread safe (Rae, David)
wait until stub call finishes before test end (David) wait until stub call finishes before changing stub (David) allow stub deactivation (Rae)
prefer kunit log (David)
---include/kunit/static_stub.h| 158 ++++++++++++++++++++++++++++++++++++ lib/kunit/static_stub.c | 49 +++++++++++2 files changed, 207 insertions(+)include/kunit/static_stub.h +3
-0@@ -12,12 +12,15 @@/* If CONFIG_KUNIT is not enabled, these stubs quietly disappear. */ #define KUNIT_STATIC_STUB_REDIRECT(real_fn_name, args...) do {} while (0) +#define KUNIT_GLOBAL_STUB_REDIRECT(stub_name, args...) do {} while (0) +#define KUNIT_DECLARE_GLOBAL_STUB (stub_name, stub_type)#else #include <kunit/test.h> #include <kunit/test-bug.h> +#include <linux/cleanup.h> /* for CLASS */ #include <linux/compiler.h> /* for {un,}likely() */ #include <linux/sched.h> /* for task_struct */ @@ -109,5 +112,160 @@ void __kunit_activate_static_stub(struct kunit*test, */ void
kunit_deactivate_static_stub(struct kunit *test, void *real_fn_addr);+/**
+ * structkunit_global_stub- Represents a context of global function stub.
+ * @replacement: The address of replacement function.
+ * @owner: The KUnit test that owns the stub, valid only when @busy > 0.
+ * @busy: The stub busyness counter incremented on entry to the replacement
+ * function, decremented on exit, used to signal if the stub is idle.
+ * @idle: The completion state to indicate when the stub is idle again.
+ *
+ * This structure is for KUnit internal use only.
+ * SeeKUNIT_DECLARE_GLOBAL_STUB().
+ */include/kunit/static_stub.h +14
-0+struct kunit_global_stub { + void *replacement; + struct kunit *owner; + atomic_t busy; + struct completion idle; +}; + +/** +* KUNIT_DECLARE_GLOBAL_STUB() - Declare a global function stub. +* @stub_name: The name of the stub, must be a valid identifier +* @stub_type: The type of the function that this stub will replace +* +* This macro will declare new identifier of an anonymous type that will +* represent global stub function that could be used by KUnit. It can be stored
+ * outside of the KUnit code. If theCONFIG_KUNITis not enabled this will
+ * be evaluated to an empty statement.
+ *
+ * The anonymous type introduced by this macro is mostly a wrapper to generic
+ * structkunit_global_stubbut with additional dummy member, that is never
+ * used directly, but is needed to maintain the type of the stub function.
+ */include/kunit/static_stub.h +13
-0+#define KUNIT_DECLARE_GLOBAL_STUB (stub_name, stub_type) \ +union { \ + struct kunit_global_stub base; \ + typeof(stub_type) dummy; \ +} stub_name + +/* Internal struct to define guard class */ +struct kunit_global_stub_guard { + struct kunit_global_stub *stub; + void *active_replacement; +}; + +/* Internal class used to guard stub calls */
Quoted 8 lines
Quoted 5 lines
+DEFINE_CLASS(kunit_global_stub_guard,
+ structkunit_global_stub_guard, + ({
+ structkunit_global_stub*stub = _T.stub; + bool active = !!_T.active_replacement;I'd call this
bool active_replacementas it's not the same thing as the active below.
IMO 'active_replacement' would be even more confusing as by that name we identify the address and here it's a flag
OTOH the 'active' in both places means more/less the same (in init below it mean stub was 'activated' and in exit here that we used 'activated' replacement function)
Quoted 82 lines
Quoted 82 lines
include/kunit/static_stub.h +10
-0+ + if (active && !atomic_dec_return(&stub->busy)) + complete_all(&stub->idle); + }), + ({ + class_kunit_global_stub_guard_t guard; + bool active = !!atomic_inc_not_zero(&stub->busy); + + guard.stub = stub; + guard.active_replacement = active ?
READ_ONCE(stub->replacement): NULL;include/kunit/static_stub.h +7
-0+ + guard; + }), + struct kunit_global_stub *stub) + +/** +* KUNIT_GLOBAL_STUB_REDIRECT() - Call a fixed function stub if activated.
+ * @stub: The function stub declared usingKUNIT_DECLARE_GLOBAL_STUB()
+ * @args: All of the arguments passed to this stub
+ *
+ * This is a function prologue which is used to allow calls to the current
+ * function to be redirected if a KUnit is running. If the KUnit is not
+ * running or stub is not yet activated the function will continue execution
+ * as normal.
+ *
+ * The function stub must be declared withKUNIT_DECLARE_GLOBAL_STUB()that is
+ * stored in a place that is accessible from both the test code, which will
+ * activate this stub usingkunit_activate_global_stub(), and from the function,
+ * where we will do this redirection usingKUNIT_GLOBAL_STUB_REDIRECT().
+ *
+ * Unlike theKUNIT_STATIC_STUB_REDIRECT(), this redirection will work
+ * even if the caller is not in a KUnit context (like a worker thread).
+ *
+ * Example:
+ *
+ * .. code-block:: c
+ *
+ *KUNIT_DECLARE_GLOBAL_STUB(func_stub, int (*)(int n));
+ *
+ * intreal_func(int n)
+ * {
+ *KUNIT_GLOBAL_STUB_REDIRECT(func_stub, n);
+ * return n + 1;
+ * }
+ *
+ * intreplacement_func(int n)
+ * {
+ * return n + 100;
+ * }
+ *
+ * voidexample_test(struct kunit *test)
+ * {
+ *KUNIT_EXPECT_EQ(test, real_func(1), 2);
+ *kunit_activate_global_stub(test, func_stub, replacement_func);
+ *KUNIT_EXPECT_EQ(test, real_func(1), 101);
+ * }
+ */
+#defineKUNIT_GLOBAL_STUB_REDIRECT(stub, args...)do
{ \
+ if (kunit_is_running()) { \
+ typeof(stub)*__stub= &(stub); \
+CLASS(kunit_global_stub_guard, guard)(&__stub->base); \
+typeof(__stub->dummy)replacement = guard.active_replacement; \
+ if (unlikely(replacement)) { \
+ kunit_info(__stub->base.owner, "%s: redirecting to %ps\n", \
+__func__, replacement); \
+ return replacement(args); \
+ } \
+ } \include/kunit/static_stub.h +2
-0+}while (0 ) +
Quoted 8 lines
include/kunit/static_stub.h void __kunit_activate_static_stub(struct kunit *test, · line 239
Quoted 6 lines
+void __kunit_activate_global_stub(struct kunit *test, struct
kunit_global_stub*stub,
+ void*replacement_addr);include/kunit/static_stub.h +3
-0+ +/** +* kunit_activate_global_stub() - Setup a fixed function stub. s/fixed/global/ here and every where else below
oops
Quoted 46 lines
Quoted 46 lines
+ * @test: Test case that wants to activate a fixed function stub
+ * @stub: The location of the function stub pointer
+ * @replacement: The replacement function
+ *
+ * This helper setups a function stub with the replacement function.
+ * It will also automatically deactivate the stub at the test end.
+ *
+ * The redirection can be disabled withkunit_deactivate_global_stub().
+ * The stub must be declared usingKUNIT_DECLARE_GLOBAL_STUB().
+ */
+#definekunit_activate_global_stub(test, stub, replacement)do { \
+ typeof(stub)*__stub= &(stub); \
+typecheck_fn(typeof(__stub->dummy), (replacement)); \
+__kunit_activate_global_stub((test), &__stub->base, (replacement)); \include/kunit/static_stub.h +3
-0+}while (0 ) + +void __kunit_deactivate_global_stub(struct kunit *test, struct
kunit_global_stub*stub);include/kunit/static_stub.h +13
-0+ +/** +* kunit_deactivate_global_stub() - Disable a fixed function stub. +* @test: Test case that wants to deactivate a fixed function stub +* @stub: The location of the function stub pointer +* +* The stub must be declared using KUNIT_DECLARE_GLOBAL_STUB(). +*/ +#define kunit_deactivate_global_stub (test, stub) do { \ + typeof(stub) *__stub = &(stub); \ + __kunit_deactivate_global_stub((test), &__stub->base); \ +} while (0) +#endif
#endiflib/kunit/static_stub.c +0
-0@@ -121,3 +121,52 @@ void __kunit_activate_static_stub(struct kunit *test, } }
EXPORT_SYMBOL_GPL(__kunit_activate_static_stub);
+
Quoted 8 lines
lib/kunit/static_stub.c void __kunit_activate_static_stub(struct kunit *test, · line 130
Quoted 6 lines
lib/kunit/static_stub.c +6
-0+static void sanitize_global_stub (void *data) +{ + struct kunit *test = kunit_get_current_test(); + struct kunit_global_stub *stub = data; + + KUNIT_EXPECT_NE(test, 0, atomic_read(&stub->busy));shouldn't
sanitize_be unconditional and do nothing in this case?
I just didn't like early return here, but maybe it's more correct
Quoted 10 lines
Quoted 8 lines
+
KUNIT_EXPECT_PTR_EQ(test, test, READ_ONCE(stub->owner)); +
+reinit_completion(&stub->idle);
+ if (!atomic_dec_and_test(&stub->busy)) {
+kunit_info(test, "waiting for %ps\n", stub->replacement); +KUNIT_EXPECT_EQ(test, 0, wait_for_completion_interruptible(&stub->idle));what's preventing
stub->busygoing to 1 again after this?
at the redirection point in kunit_global_stub_guard we have
atomic_inc_not_zero(&stub->busy);
and the activation/deactivation can only be done from the main KUnit thread (which is here)
Quoted 44 lines
Lucas De Marchi
Quoted 41 lines
+ }
lib/kunit/static_stub.c +23
-0+ + WRITE_ONCE(stub->owner, NULL); + WRITE_ONCE(stub->replacement, NULL); +} + +/* +* Helper function for kunit_activate_global_stub(). The macro does +* typechecking, so use it instead. +*/ +void __kunit_activate_global_stub (struct kunit *test, + struct kunit_global_stub *stub, + void *replacement_addr) +{ + KUNIT_ASSERT_NOT_ERR_OR_NULL(test, stub); + KUNIT_ASSERT_NOT_ERR_OR_NULL(test, replacement_addr); + if (atomic_read(&stub->busy)) + kunit_release_action(test, sanitize_global_stub, stub); + else + init_completion(&stub->idle); + WRITE_ONCE(stub->owner, test); + WRITE_ONCE(stub->replacement, replacement_addr); + KUNIT_ASSERT_EQ(test, 1, atomic_inc_return(&stub->busy)); + KUNIT_ASSERT_EQ(test, 0, kunit_add_action_or_reset(test,
sanitize_global_stub, stub));lib/kunit/static_stub.c +7
-0+} +EXPORT_SYMBOL_GPL (__kunit_activate_global_stub); + +/* +* Helper function for kunit_deactivate_global_stub(). Use it instead. +*/ +void __kunit_deactivate_global_stub(struct kunit *test, struct
kunit_global_stub*stub)lib/kunit/static_stub.c +5
-0+{ + if (atomic_read(&stub->busy)) + kunit_release_action(test, sanitize_global_stub, stub); +} +EXPORT_SYMBOL_GPL (__kunit_deactivate_global_stub);--
2.43.0
Re: [PATCH v2 4/6] kunit: Allow function redirection outside of the KUnit thread
Quoted 283 lines · Michal Wajdeczko
Quoted 116 lines · Lucas De Marchi
Quoted 115 lines · Michal Wajdeczko
Currently, the 'static stub' API only allows function redirection for calls made from the kthread of the current test, which prevents the use of this functionality when the tested code is also used by other threads, outside of the KUnit test, like from the workqueue.
Add another set of macros to allow redirection to the replacement functions, which, unlike the
KUNIT_STATIC_STUB_REDIRECT, will affect all calls done during the test execution.These new stubs, named 'global', must be declared using dedicated
KUNIT_DECLARE_GLOBAL_STUB()macro and then can be placed either as global static variables or as part of the other structures.To properly maintain stubs lifecycle, they can be activated only from the main KUnit context. Some precaution is taken to avoid changing or deactivating replacement functions if one is still used by other thread.
Signed-off-by Michal Wajdeczko <[email protected]>---
Cc: Rae Moar <[email protected]>
Cc: David Gow <[email protected]>
Cc: Daniel Latypov <[email protected]>
Cc: Lucas De Marchi <[email protected]>
---
v2: s/FIXED_STUB/GLOBAL_STUB (David, Lucas)
make it little more thread safe (Rae, David)
wait until stub call finishes before test end (David) wait until stub call finishes before changing stub (David) allow stub deactivation (Rae)
prefer kunit log (David)
---include/kunit/static_stub.h| 158 ++++++++++++++++++++++++++++++++++++ lib/kunit/static_stub.c | 49 +++++++++++2 files changed, 207 insertions(+)include/kunit/static_stub.h +3
-0@@ -12,12 +12,15 @@/* If CONFIG_KUNIT is not enabled, these stubs quietly disappear. */ #define KUNIT_STATIC_STUB_REDIRECT(real_fn_name, args...) do {} while (0) +#define KUNIT_GLOBAL_STUB_REDIRECT(stub_name, args...) do {} while (0) +#define KUNIT_DECLARE_GLOBAL_STUB (stub_name, stub_type)#else #include <kunit/test.h> #include <kunit/test-bug.h> +#include <linux/cleanup.h> /* for CLASS */ #include <linux/compiler.h> /* for {un,}likely() */ #include <linux/sched.h> /* for task_struct */ @@ -109,5 +112,160 @@ void __kunit_activate_static_stub(struct kunit*test, */ void
kunit_deactivate_static_stub(struct kunit *test, void *real_fn_addr);+/**
+ * structkunit_global_stub- Represents a context of global function stub.
+ * @replacement: The address of replacement function.
+ * @owner: The KUnit test that owns the stub, valid only when @busy > 0.
+ * @busy: The stub busyness counter incremented on entry to the replacement
+ * function, decremented on exit, used to signal if the stub is idle.
+ * @idle: The completion state to indicate when the stub is idle again.
+ *
+ * This structure is for KUnit internal use only.
+ * SeeKUNIT_DECLARE_GLOBAL_STUB().
+ */include/kunit/static_stub.h +14
-0+struct kunit_global_stub { + void *replacement; + struct kunit *owner; + atomic_t busy; + struct completion idle; +}; + +/** +* KUNIT_DECLARE_GLOBAL_STUB() - Declare a global function stub. +* @stub_name: The name of the stub, must be a valid identifier +* @stub_type: The type of the function that this stub will replace +* +* This macro will declare new identifier of an anonymous type that will +* represent global stub function that could be used by KUnit. It can be stored
+ * outside of the KUnit code. If theCONFIG_KUNITis not enabled this will
+ * be evaluated to an empty statement.
+ *
+ * The anonymous type introduced by this macro is mostly a wrapper to generic
+ * structkunit_global_stubbut with additional dummy member, that is never
+ * used directly, but is needed to maintain the type of the stub function.
+ */include/kunit/static_stub.h +13
-0+#define KUNIT_DECLARE_GLOBAL_STUB (stub_name, stub_type) \ +union { \ + struct kunit_global_stub base; \ + typeof(stub_type) dummy; \ +} stub_name + +/* Internal struct to define guard class */ +struct kunit_global_stub_guard { + struct kunit_global_stub *stub; + void *active_replacement; +}; + +/* Internal class used to guard stub calls */ Quoted 8 lines
Quoted 5 lines
+DEFINE_CLASS(kunit_global_stub_guard,
+ structkunit_global_stub_guard, + ({
+ structkunit_global_stub*stub = _T.stub; + bool active = !!_T.active_replacement;I'd call this
bool active_replacementas it's not the same thing as the active below.IMO '
active_replacement' would be even more confusing as by that name we identify the address and here it's a flagOTOH the 'active' in both places means more/less the same (in init below it mean stub was 'activated' and in exit here that we used 'activated' replacement function)
Quoted 82 lines
Quoted 82 lines
include/kunit/static_stub.h +10
-0+ + if (active && !atomic_dec_return(&stub->busy)) + complete_all(&stub->idle); + }), + ({ + class_kunit_global_stub_guard_t guard; + bool active = !!atomic_inc_not_zero(&stub->busy); + + guard.stub = stub; + guard.active_replacement = active ?
READ_ONCE(stub->replacement): NULL;include/kunit/static_stub.h +7
-0+ + guard; + }), + struct kunit_global_stub *stub) + +/** +* KUNIT_GLOBAL_STUB_REDIRECT() - Call a fixed function stub if activated.
+ * @stub: The function stub declared usingKUNIT_DECLARE_GLOBAL_STUB()
+ * @args: All of the arguments passed to this stub
+ *
+ * This is a function prologue which is used to allow calls to the current
+ * function to be redirected if a KUnit is running. If the KUnit is not
+ * running or stub is not yet activated the function will continue execution
+ * as normal.
+ *
+ * The function stub must be declared withKUNIT_DECLARE_GLOBAL_STUB()that is
+ * stored in a place that is accessible from both the test code, which will
+ * activate this stub usingkunit_activate_global_stub(), and from the function,
+ * where we will do this redirection usingKUNIT_GLOBAL_STUB_REDIRECT().
+ *
+ * Unlike theKUNIT_STATIC_STUB_REDIRECT(), this redirection will work
+ * even if the caller is not in a KUnit context (like a worker thread).
+ *
+ * Example:
+ *
+ * .. code-block:: c
+ *
+ *KUNIT_DECLARE_GLOBAL_STUB(func_stub, int (*)(int n));
+ *
+ * intreal_func(int n)
+ * {
+ *KUNIT_GLOBAL_STUB_REDIRECT(func_stub, n);
+ * return n + 1;
+ * }
+ *
+ * intreplacement_func(int n)
+ * {
+ * return n + 100;
+ * }
+ *
+ * voidexample_test(struct kunit *test)
+ * {
+ *KUNIT_EXPECT_EQ(test, real_func(1), 2);
+ *kunit_activate_global_stub(test, func_stub, replacement_func);
+ *KUNIT_EXPECT_EQ(test, real_func(1), 101);
+ * }
+ */
+#defineKUNIT_GLOBAL_STUB_REDIRECT(stub, args...)do
{ \
+ if (kunit_is_running()) { \
+ typeof(stub)*__stub= &(stub); \
+CLASS(kunit_global_stub_guard, guard)(&__stub->base); \
+typeof(__stub->dummy)replacement = guard.active_replacement; \
+ if (unlikely(replacement)) { \
+ kunit_info(__stub->base.owner, "%s: redirecting to %ps\n", \
+__func__, replacement); \
+ return replacement(args); \
+ } \
+ } \include/kunit/static_stub.h +2
-0+}while (0 ) +Quoted 8 lines
include/kunit/static_stub.h void __kunit_activate_static_stub(struct kunit *test, · line 239
Quoted 6 lines
+void __kunit_activate_global_stub(struct kunit *test, struct
kunit_global_stub*stub,
+ void*replacement_addr);include/kunit/static_stub.h +3
-0+ +/** +* kunit_activate_global_stub() - Setup a fixed function stub. s/fixed/global/ here and every where else below
oops
Quoted 46 lines
Quoted 46 lines
+ * @test: Test case that wants to activate a fixed function stub
+ * @stub: The location of the function stub pointer
+ * @replacement: The replacement function
+ *
+ * This helper setups a function stub with the replacement function.
+ * It will also automatically deactivate the stub at the test end.
+ *
+ * The redirection can be disabled withkunit_deactivate_global_stub().
+ * The stub must be declared usingKUNIT_DECLARE_GLOBAL_STUB().
+ */
+#definekunit_activate_global_stub(test, stub, replacement)do { \
+ typeof(stub)*__stub= &(stub); \
+typecheck_fn(typeof(__stub->dummy), (replacement)); \
+__kunit_activate_global_stub((test), &__stub->base, (replacement)); \include/kunit/static_stub.h +3
-0+}while (0 ) + +void __kunit_deactivate_global_stub(struct kunit *test, struct
kunit_global_stub*stub);include/kunit/static_stub.h +13
-0+ +/** +* kunit_deactivate_global_stub() - Disable a fixed function stub. +* @test: Test case that wants to deactivate a fixed function stub +* @stub: The location of the function stub pointer +* +* The stub must be declared using KUNIT_DECLARE_GLOBAL_STUB(). +*/ +#define kunit_deactivate_global_stub (test, stub) do { \ + typeof(stub) *__stub = &(stub); \ + __kunit_deactivate_global_stub((test), &__stub->base); \ +} while (0) +#endif
#endiflib/kunit/static_stub.c +0
-0@@ -121,3 +121,52 @@ void __kunit_activate_static_stub(struct kunit *test, } }
EXPORT_SYMBOL_GPL(__kunit_activate_static_stub);
+Quoted 8 lines
lib/kunit/static_stub.c void __kunit_activate_static_stub(struct kunit *test, · line 130
Quoted 6 lines
lib/kunit/static_stub.c +6
-0+static void sanitize_global_stub (void *data) +{ + struct kunit *test = kunit_get_current_test(); + struct kunit_global_stub *stub = data; + + KUNIT_EXPECT_NE(test, 0, atomic_read(&stub->busy));shouldn't
sanitize_be unconditional and do nothing in this case?
Quoted 1 line
I just didn't like early return here, but maybe it's more correct
hmm, in fact we don't need to check stub->busy prior to calling kunit_release_action() since our goal is to detect whether stub was activated, but this action will be released/called only if we have added this action after the stub activation, so we can just rely on the action management code and just keep the EXPECT_NE here as a guard
Quoted 63 lines
Quoted 11 lines
Quoted 9 lines
+
KUNIT_EXPECT_PTR_EQ(test, test, READ_ONCE(stub->owner)); +
+reinit_completion(&stub->idle);
+ if (!atomic_dec_and_test(&stub->busy)) {
+kunit_info(test, "waiting for %ps\n", stub->replacement); +KUNIT_EXPECT_EQ(test, 0, wait_for_completion_interruptible(&stub->idle));what's preventing
stub->busygoing to 1 again after this?at the redirection point in
kunit_global_stub_guardwe haveatomic_inc_not_zero(&stub->busy);and the activation/deactivation can only be done from the main KUnit thread (which is here)
Quoted 44 lines
Lucas De Marchi
Quoted 41 lines
+ }
lib/kunit/static_stub.c +23
-0+ + WRITE_ONCE(stub->owner, NULL); + WRITE_ONCE(stub->replacement, NULL); +} + +/* +* Helper function for kunit_activate_global_stub(). The macro does +* typechecking, so use it instead. +*/ +void __kunit_activate_global_stub (struct kunit *test, + struct kunit_global_stub *stub, + void *replacement_addr) +{ + KUNIT_ASSERT_NOT_ERR_OR_NULL(test, stub); + KUNIT_ASSERT_NOT_ERR_OR_NULL(test, replacement_addr); + if (atomic_read(&stub->busy)) + kunit_release_action(test, sanitize_global_stub, stub); + else + init_completion(&stub->idle); + WRITE_ONCE(stub->owner, test); + WRITE_ONCE(stub->replacement, replacement_addr); + KUNIT_ASSERT_EQ(test, 1, atomic_inc_return(&stub->busy)); + KUNIT_ASSERT_EQ(test, 0, kunit_add_action_or_reset(test,
sanitize_global_stub, stub));lib/kunit/static_stub.c +7
-0+} +EXPORT_SYMBOL_GPL (__kunit_activate_global_stub); + +/* +* Helper function for kunit_deactivate_global_stub(). Use it instead. +*/ +void __kunit_deactivate_global_stub(struct kunit *test, struct
kunit_global_stub*stub)lib/kunit/static_stub.c +5
-0+{ + if (atomic_read(&stub->busy)) + kunit_release_action(test, sanitize_global_stub, stub); +} +EXPORT_SYMBOL_GPL (__kunit_deactivate_global_stub);--
2.43.0
PATCH v2 5/6 kunit: Add example with alternate function redirection method
In reply to Michal WajdeczkoAdd example how to use KUNIT_FIXED_STUB_REDIRECT and compare its usage with the KUNIT_STATIC_STUB_REDIRECT. Also show how the DECLARE_IF_KUNIT macro could be helpful in declaring test data in the non-test data structures.
---
Cc: Daniel Latypov <[email protected]>
Cc: Lucas De Marchi <[email protected]>
---
v2: add missing testcase description (Rae) and rebase
--- lib/kunit/kunit-example-test.c | 67 ++++++++++++++++++++++++++++++++++ 1 file changed, 67 insertions(+)
lib/kunit/kunit-example-test.c +67 -0
@@ -6,8 +6,10 @@* Author: Brendan Higgins <[email protected]> */ +#include <linux/workqueue.h> #include <kunit/test.h> #include <kunit/static_stub.h> +#include <kunit/visibility.h> /* * This is the most fundamental element of KUnit, the test case. A test case
@@ -221,6 +223,70 @@ static void example_static_stub_using_fn_ptr_test(struct kunit *test)KUNIT_EXPECT_EQ (test,add_one (1 ),2 ); } +/* This could be a location of various global stub functions. */ +static struct { + KUNIT_DECLARE_GLOBAL_STUB(add_two, int (*)(int i)); +} stubs; + +/* This is a function we'll replace with stubs. */ +static int add_two (int i) +{ +/* This will trigger the stub if active. */ +KUNIT_STATIC_STUB_REDIRECT (add_two, i); +KUNIT_GLOBAL_STUB_REDIRECT (stubs.add_two, i); + +return i +2 ; +} + +struct add_two_async_work { +struct work_struct work; +int param; +int result; +}; + +static void add_two_async_func (struct work_struct *work) +{ +struct add_two_async_work *w =container_of (work,typeof (*w), work); + + w->result =add_two (w->param); +} + +static int add_two_async (int i) +{ +struct add_two_async_work w = { .param = i }; + +INIT_WORK_ONSTACK (&w.work, add_two_async_func); +schedule_work (&w.work); +flush_work (&w.work); +destroy_work_on_stack (&w.work); + +return w.result; +} + +/* +* This test shows how to use KUNIT_GLOBAL_STUB_REDIRECT and compares its +* usage with the KUNIT_STATIC_STUB_REDIRECT. +*/ +static void example_global_stub_test (struct kunit *test) +{ +/* static stub redirection works only for KUnit thread */ +kunit_activate_static_stub (test, add_two, subtract_one); +KUNIT_EXPECT_EQ (test,add_two (1 ),subtract_one (1 )); +KUNIT_EXPECT_NE_MSG (test,add_two_async (1 ),subtract_one (1 ), +"stub shouldn't be active outside KUnit thread!" ); + +kunit_deactivate_static_stub (test, add_two); +KUNIT_EXPECT_EQ (test,add_two (1 ),add_two (1 )); + +/* fixed stub redirection works for KUnit and other threads */ +kunit_activate_global_stub (test, stubs.add_two, subtract_one); +KUNIT_EXPECT_EQ (test,add_two (1 ),subtract_one (1 )); +KUNIT_EXPECT_EQ (test,add_two_async (1 ),subtract_one (1 )); + +kunit_deactivate_global_stub (test, stubs.add_two); +KUNIT_EXPECT_EQ (test,add_two (1 ),add_two (1 )); +} +static const struct example_param {int value; } example_params_array[] = {
@@ -294,6 +360,7 @@ static struct kunit_case example_test_cases[] = {KUNIT_CASE (example_all_expect_macros_test),KUNIT_CASE (example_static_stub_test),KUNIT_CASE (example_static_stub_using_fn_ptr_test), +KUNIT_CASE (example_global_stub_test),KUNIT_CASE (example_priv_test),KUNIT_CASE_PARAM (example_params_test, example_gen_params), KUNIT_CASE_SLOW(example_slow_test),
Signature · 1 line
2.43.0
Re: [PATCH v2 5/6] kunit: Add example with alternate function redirection method
Quoted 1 line · Michal Wajdeczko
Add example how to use
KUNIT_FIXED_STUB_REDIRECTand compare its
s/FIXED/GLOBAL/
Quoted 36 lines
usage with the
KUNIT_STATIC_STUB_REDIRECT. Also show how theDECLARE_IF_KUNITmacro could be helpful in declaring test data in the non-test data structures.Signed-off-by Michal Wajdeczko <[email protected]>Reviewed-by Rae Moar <[email protected]> #v1Reviewed-by David Gow <[email protected]> #v1---
Cc: Daniel Latypov <[email protected]>
Cc: Lucas De Marchi <[email protected]>
---
v2: add missing testcase description (Rae) and rebase
---lib/kunit/kunit-example-test.c| 67 ++++++++++++++++++++++++++++++++++1 file changed, 67 insertions(+)lib/kunit/kunit-example-test.c +3
-0@@ -6,8 +6,10 @@* Author: Brendan Higgins <[email protected]> */ +#include <linux/workqueue.h> #include <kunit/test.h> #include <kunit/static_stub.h> +#include <kunit/visibility.h> /* * This is the most fundamental element of KUnit, the test case. A test case @@ -221,6 +223,70 @@ static void example_static_stub_using_fn_ptr_test(struct kunit *test)KUNIT_EXPECT_EQ (test,add_one (1 ),2 ); } +/* This could be a location of various global stub functions. */
lib/kunit/kunit-example-test.c static void example_static_stub_using_fn_ptr_test(struct kunit *test) · line 236
Quoted 10 lines
lib/kunit/kunit-example-test.c +10
-0+static struct { + KUNIT_DECLARE_GLOBAL_STUB(add_two, int (*)(int i)); +} stubs; + +/* This is a function we'll replace with stubs. */ +static int add_two (int i) +{ +/* This will trigger the stub if active. */ +KUNIT_STATIC_STUB_REDIRECT (add_two, i); +KUNIT_GLOBAL_STUB_REDIRECT (stubs.add_two, i);
Nice use of an outer storage so we can give it the same name. IMO this should be encouraged.
Quoted 35 lines
lib/kunit/kunit-example-test.c +35
-0+ +return i +2 ; +} + +struct add_two_async_work { +struct work_struct work; +int param; +int result; +}; + +static void add_two_async_func (struct work_struct *work) +{ +struct add_two_async_work *w =container_of (work,typeof (*w), work); + + w->result =add_two (w->param); +} + +static int add_two_async (int i) +{ +struct add_two_async_work w = { .param = i }; + +INIT_WORK_ONSTACK (&w.work, add_two_async_func); +schedule_work (&w.work); +flush_work (&w.work); +destroy_work_on_stack (&w.work); + +return w.result; +} + +/* +* This test shows how to use KUNIT_GLOBAL_STUB_REDIRECT and compares its +* usage with the KUNIT_STATIC_STUB_REDIRECT. +*/ +static void example_global_stub_test (struct kunit *test) +{
lib/kunit/kunit-example-test.c static void example_static_stub_using_fn_ptr_test(struct kunit *test) · line 281
Quoted 10 lines
lib/kunit/kunit-example-test.c +10
-0+/* static stub redirection works only for KUnit thread */ +kunit_activate_static_stub (test, add_two, subtract_one); +KUNIT_EXPECT_EQ (test,add_two (1 ),subtract_one (1 )); +KUNIT_EXPECT_NE_MSG (test,add_two_async (1 ),subtract_one (1 ), +"stub shouldn't be active outside KUnit thread!" ); + +kunit_deactivate_static_stub (test, add_two); +KUNIT_EXPECT_EQ (test,add_two (1 ),add_two (1 )); + +/* fixed stub redirection works for KUnit and other threads */
s/fixed/global/
with those fixes,
thanks
Lucas De Marchi
Quoted 22 lines
lib/kunit/kunit-example-test.c +9
-0+kunit_activate_global_stub (test, stubs.add_two, subtract_one); +KUNIT_EXPECT_EQ (test,add_two (1 ),subtract_one (1 )); +KUNIT_EXPECT_EQ (test,add_two_async (1 ),subtract_one (1 )); + +kunit_deactivate_global_stub (test, stubs.add_two); +KUNIT_EXPECT_EQ (test,add_two (1 ),add_two (1 )); +} +static const struct example_param {int value; } example_params_array[] = {@@ -294,6 +360,7 @@ static struct kunit_case example_test_cases[] = {KUNIT_CASE (example_all_expect_macros_test),KUNIT_CASE (example_static_stub_test),KUNIT_CASE (example_static_stub_using_fn_ptr_test), +KUNIT_CASE (example_global_stub_test),KUNIT_CASE (example_priv_test),KUNIT_CASE_PARAM (example_params_test, example_gen_params), KUNIT_CASE_SLOW(example_slow_test),Signature · 1 line
2.43.0
PATCH v2 6/6 kunit: Add some selftests for global stub redirection macros
In reply to Michal WajdeczkoWhile we already have few ASSERT() within the implementation, it's always better to have dedicated test cases. Add tests for:
- automatic deactivation of the stubs at the test end
- blocked deactivation until all active stub calls finish
- blocked stub change until all active stub calls finish
- safe abuse (deactivation without activation)
---
Cc: Rae Moar <[email protected]>
Cc: David Gow <[email protected]>
Cc: Lucas De Marchi <[email protected]>
--- lib/kunit/kunit-test.c | 254 ++++++++++++++++++++++++++++++++++++++++- 1 file changed, 253 insertions(+), 1 deletion(-)
lib/kunit/kunit-test.c +253 -1
@@ -6,8 +6,10 @@* Author: Brendan Higgins <[email protected]> */ #include "linux/gfp_types.h" +#include <kunit/static_stub.h> #include <kunit/test.h> #include <kunit/test-bug.h> +#include <kunit/visibility.h> #include <linux/device.h> #include <kunit/device.h>
@@ -866,10 +868,260 @@ static struct kunit_suite kunit_current_test_suite = { .test_cases = kunit_current_test_cases, }; +static struct { +/* this stub matches the real function */ + KUNIT_DECLARE_GLOBAL_STUB(first_stub, int (*)(int i)); +/* this stub matches only return type of the real function */ + KUNIT_DECLARE_GLOBAL_STUB(second_stub, int (*)(int bit, int data)); +/* this is an example stub that returns void */ + KUNIT_DECLARE_GLOBAL_STUB(void_stub, void (*)(void)); +/* this is an example how to store additional data for use by stubs */ + DECLARE_IF_KUNIT(int data); + DECLARE_IF_KUNIT(int counter); +} stubs = { + DECLARE_IF_KUNIT(.data = 3), +}; + +static int real_func (int i) +{ +KUNIT_GLOBAL_STUB_REDIRECT (stubs.first_stub, i); +KUNIT_GLOBAL_STUB_REDIRECT (stubs.second_stub,BIT (i), stubs.data); + +return i; +} + +struct real_work { +struct work_struct work; +int param; +int result; +}; + +static void real_work_func (struct work_struct *work) +{ +struct real_work *w =container_of (work,typeof (*w), work); + + w->result =real_func (w->param); +} + +static int real_func_async (int i) +{ +struct real_work w = { .param = i, .result = -EINPROGRESS }; + +INIT_WORK_ONSTACK (&w.work, real_work_func); +schedule_work (&w.work); +flush_work (&w.work); +destroy_work_on_stack (&w.work); + +return w.result; +} + +static int replacement_func (int i) +{ +return i +1 ; +} + +static int other_replacement_func (int i) +{ +return i +10 ; +} + +static int super_replacement_func (int bit,int data) +{ +return bit * data; +} + +static int slow_replacement_func (int i) +{ +schedule_timeout_interruptible (HZ /20 ); +return replacement_func (i); +} + +static void real_void_func (void ) +{ +KUNIT_GLOBAL_STUB_REDIRECT (stubs.void_stub); +DECLARE_IF_KUNIT (stubs.counter++); +} + +static void replacement_void_func (void ) +{ + stubs.counter--; +} + +static void expect_deactivated (void *data) +{ +struct kunit *test =kunit_get_current_test (); + +KUNIT_EXPECT_NULL (test, stubs.first_stub.base.owner); +KUNIT_EXPECT_NULL (test, stubs.first_stub.base.replacement); +KUNIT_EXPECT_NULL (test, stubs.second_stub.base.owner); +KUNIT_EXPECT_NULL (test, stubs.second_stub.base.replacement); +KUNIT_EXPECT_NULL (test, stubs.void_stub.base.owner); +KUNIT_EXPECT_NULL (test, stubs.void_stub.base.replacement); +} + +static void kunit_global_stub_test_deactivate (struct kunit *test) +{ +/* make sure everything will be deactivated */ +KUNIT_ASSERT_EQ (test,0 ,kunit_add_action_or_reset (test, expect_deactivated, test)); + +/* deactivate without activate */ +kunit_deactivate_global_stub (test, stubs.first_stub); + +/* deactivate twice */ +kunit_deactivate_global_stub (test, stubs.first_stub); + +/* allow to skip deactivation (will be tested by expect_deactivated action) */ +kunit_activate_global_stub (test, stubs.first_stub, replacement_func); +} + +static void kunit_global_stub_test_activate (struct kunit *test) +{ +int real, replacement, other, super, i =2 ; + +/* prerequisites */ +real_void_func (); +KUNIT_ASSERT_EQ (test, stubs.counter,1 ); +replacement_void_func (); +KUNIT_ASSERT_EQ (test, stubs.counter,0 ); + +/* prerequisites cont'd */ +KUNIT_ASSERT_EQ (test,real_func (i), real =real_func_async (i)); +KUNIT_ASSERT_NE (test,real_func (i), replacement =replacement_func (i)); +KUNIT_ASSERT_NE (test,real_func (i), other =other_replacement_func (i)); +KUNIT_ASSERT_NE (test,real_func (i), super =super_replacement_func (BIT (i), stubs.data)); + +/* make sure everything will be deactivated */ +KUNIT_ASSERT_EQ (test,0 ,kunit_add_action_or_reset (test, expect_deactivated, test)); + +/* allow to activate replacement */ +kunit_activate_global_stub (test, stubs.void_stub, replacement_void_func); +real_void_func (); +KUNIT_ASSERT_EQ (test, stubs.counter,-1 ); + +/* allow to activate replacement */ +kunit_activate_global_stub (test, stubs.first_stub, replacement_func); +KUNIT_EXPECT_EQ (test,real_func (i), replacement); +KUNIT_EXPECT_EQ (test,real_func_async (i), replacement); + +/* allow to change replacement */ +kunit_activate_global_stub (test, stubs.first_stub, other_replacement_func); +KUNIT_EXPECT_EQ (test,real_func (i), other); +KUNIT_EXPECT_EQ (test,real_func_async (i), other); + +/* allow to deactivate replacement */ +kunit_deactivate_global_stub (test, stubs.first_stub); +KUNIT_EXPECT_EQ (test,real_func (i), real); +KUNIT_EXPECT_EQ (test,real_func_async (i), real); + +/* allow to activate replacement with different arguments */ +kunit_activate_global_stub (test, stubs.second_stub, super_replacement_func); +KUNIT_EXPECT_EQ (test,real_func (i), super); +KUNIT_EXPECT_EQ (test,real_func_async (i), super); + +/* allow to deactivate twice */ +kunit_deactivate_global_stub (test, stubs.second_stub); +kunit_deactivate_global_stub (test, stubs.second_stub); +KUNIT_EXPECT_EQ (test,real_func_async (i), real); +KUNIT_EXPECT_EQ (test,real_func (i), real); +} + +static void flush_real_work (void *data) +{ +struct real_work *w = data; + +flush_work (&w->work); +} + +static void __kunit_global_stub_test_slow (struct kunit *test,bool replace) +{ +int real, replacement, other, i = replace ?3 :5 ; +struct real_work *w; + +/* prerequisites */ +KUNIT_ASSERT_EQ (test,real_func (i), real =real_func_async (i)); +KUNIT_ASSERT_NE (test,real_func (i), replacement =slow_replacement_func (i)); +KUNIT_ASSERT_NE (test,real_func (i), other =other_replacement_func (i)); +KUNIT_ASSERT_NOT_ERR_OR_NULL (test, w =kunit_kzalloc (test,sizeof (*w),GFP_KERNEL )); +INIT_WORK (&w->work, real_work_func); +KUNIT_ASSERT_EQ (test,0 ,kunit_add_action_or_reset (test, flush_real_work, w)); +KUNIT_ASSERT_EQ (test,0 ,kunit_add_action_or_reset (test, expect_deactivated, test)); + +/* allow to activate replacement */ +kunit_activate_global_stub (test, stubs.first_stub, slow_replacement_func); +KUNIT_EXPECT_EQ (test,real_func_async (i), replacement); + + w->param = i; + w->result =0 ; +queue_work (system_long_wq, &w->work); + +/* wait until work starts */ +while (work_pending (&w->work)) +schedule_timeout_interruptible (HZ /100 ); +KUNIT_EXPECT_NE (test,work_busy (&w->work),0 ); + +/* wait until work enters the stub */ +while (atomic_read (&stubs.first_stub.base.busy) <2 ) +schedule_timeout_interruptible (HZ /100 ); + +/* stub should be still busy(2) at this point */ +KUNIT_EXPECT_EQ (test,2 ,atomic_read (&stubs.first_stub.base.busy)); +KUNIT_EXPECT_EQ (test, w->result,0 ); + +if (replace) { +/* try replace the stub, it should be just activated(1) */ +kunit_activate_global_stub (test, stubs.first_stub, other_replacement_func); +KUNIT_EXPECT_EQ (test,1 ,atomic_read (&stubs.first_stub.base.busy)); + }else { +/* try to deactivate the stub, it should be disabled(0) */ +kunit_deactivate_global_stub (test, stubs.first_stub); +KUNIT_EXPECT_EQ (test,0 ,atomic_read (&stubs.first_stub.base.busy)); + } + +/* and results from the worker should be available */ +KUNIT_EXPECT_EQ (test, w->result, replacement); +KUNIT_EXPECT_NE (test, w->result, real); +KUNIT_EXPECT_NE (test, w->result, other); + +if (replace) +KUNIT_EXPECT_EQ (test,real_func_async (i), other); +else +KUNIT_EXPECT_EQ (test,real_func_async (i), real); +} + +static void kunit_global_stub_test_slow_deactivate (struct kunit *test) +{ +__kunit_global_stub_test_slow (test, false); +} + +static void kunit_global_stub_test_slow_replace (struct kunit *test) +{ +__kunit_global_stub_test_slow (test, true); +} + +static int kunit_global_stub_test_init (struct kunit *test) +{ + stubs.counter =0 ; +return 0 ; +} + +static struct kunit_case kunit_global_stub_test_cases[] = { +KUNIT_CASE (kunit_global_stub_test_activate), +KUNIT_CASE (kunit_global_stub_test_deactivate), +KUNIT_CASE_SLOW (kunit_global_stub_test_slow_deactivate), +KUNIT_CASE_SLOW (kunit_global_stub_test_slow_replace), + {} +}; + +static struct kunit_suite kunit_global_stub_suite = { + .name ="kunit_global_stub" , + .init = kunit_global_stub_test_init, + .test_cases = kunit_global_stub_test_cases, +}; +kunit_test_suites (&kunit_try_catch_test_suite, &kunit_resource_test_suite, &kunit_log_test_suite, &kunit_status_test_suite, &kunit_current_test_suite, &kunit_device_test_suite, - &kunit_fault_test_suite); + &kunit_fault_test_suite, &kunit_global_stub_suite);MODULE_DESCRIPTION ("KUnit test for core test infrastructure" );MODULE_LICENSE ("GPL v2" );
Signature · 1 line
2.43.0