Skip to content

Commit 5fb1a8c

Browse files
rfvirgilshuahkh
authored andcommitted
kunit: Add example of kunit_activate_static_stub() with pointer-to-function
Adds a variant of example_static_stub_test() that shows use of a pointer-to-function with kunit_activate_static_stub(). A const pointer to the add_one() function is declared. This pointer-to-function is passed to kunit_activate_static_stub() and kunit_deactivate_static_stub() instead of passing add_one directly. Signed-off-by: Richard Fitzgerald <rf@opensource.cirrus.com> Reviewed-by: David Gow <davidgow@google.com> Signed-off-by: Shuah Khan <skhan@linuxfoundation.org>
1 parent fcbac39 commit 5fb1a8c

1 file changed

Lines changed: 35 additions & 0 deletions

File tree

lib/kunit/kunit-example-test.c

Lines changed: 35 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -168,6 +168,16 @@ static int subtract_one(int i)
168168
return i - 1;
169169
}
170170

171+
/*
172+
* If the function to be replaced is static within a module it is
173+
* useful to export a pointer to that function instead of having
174+
* to change the static function to a non-static exported function.
175+
*
176+
* This pointer simulates a module exporting a pointer to a static
177+
* function.
178+
*/
179+
static int (* const add_one_fn_ptr)(int i) = add_one;
180+
171181
/*
172182
* This test shows the use of static stubs.
173183
*/
@@ -187,6 +197,30 @@ static void example_static_stub_test(struct kunit *test)
187197
KUNIT_EXPECT_EQ(test, add_one(1), 2);
188198
}
189199

200+
/*
201+
* This test shows the use of static stubs when the function being
202+
* replaced is provided as a pointer-to-function instead of the
203+
* actual function. This is useful for providing access to static
204+
* functions in a module by exporting a pointer to that function
205+
* instead of having to change the static function to a non-static
206+
* exported function.
207+
*/
208+
static void example_static_stub_using_fn_ptr_test(struct kunit *test)
209+
{
210+
/* By default, function is not stubbed. */
211+
KUNIT_EXPECT_EQ(test, add_one(1), 2);
212+
213+
/* Replace add_one() with subtract_one(). */
214+
kunit_activate_static_stub(test, add_one_fn_ptr, subtract_one);
215+
216+
/* add_one() is now replaced. */
217+
KUNIT_EXPECT_EQ(test, add_one(1), 0);
218+
219+
/* Return add_one() to normal. */
220+
kunit_deactivate_static_stub(test, add_one_fn_ptr);
221+
KUNIT_EXPECT_EQ(test, add_one(1), 2);
222+
}
223+
190224
static const struct example_param {
191225
int value;
192226
} example_params_array[] = {
@@ -259,6 +293,7 @@ static struct kunit_case example_test_cases[] = {
259293
KUNIT_CASE(example_mark_skipped_test),
260294
KUNIT_CASE(example_all_expect_macros_test),
261295
KUNIT_CASE(example_static_stub_test),
296+
KUNIT_CASE(example_static_stub_using_fn_ptr_test),
262297
KUNIT_CASE(example_priv_test),
263298
KUNIT_CASE_PARAM(example_params_test, example_gen_params),
264299
KUNIT_CASE_SLOW(example_slow_test),

0 commit comments

Comments
 (0)