Skip to content

Commit 170c317

Browse files
l0kodshuahkh
authored andcommitted
kunit: Add tests for fault
Add a test case to check NULL pointer dereference and make sure it would result as a failed test. The full kunit_fault test suite is marked as skipped when run on UML because it would result to a kernel panic. Tested with: ./tools/testing/kunit/kunit.py run --arch x86_64 kunit_fault ./tools/testing/kunit/kunit.py run --arch arm64 \ --cross_compile=aarch64-linux-gnu- kunit_fault Cc: Brendan Higgins <brendanhiggins@google.com> Cc: Rae Moar <rmoar@google.com> Cc: Shuah Khan <skhan@linuxfoundation.org> Reviewed-by: David Gow <davidgow@google.com> Signed-off-by: Mickaël Salaün <mic@digikod.net> Link: https://lore.kernel.org/r/20240408074625.65017-8-mic@digikod.net Signed-off-by: Shuah Khan <skhan@linuxfoundation.org>
1 parent 8bd5d74 commit 170c317

File tree

1 file changed

+44
-1
lines changed

1 file changed

+44
-1
lines changed

lib/kunit/kunit-test.c

Lines changed: 44 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -109,6 +109,48 @@ static struct kunit_suite kunit_try_catch_test_suite = {
109109
.test_cases = kunit_try_catch_test_cases,
110110
};
111111

112+
#ifndef CONFIG_UML
113+
114+
static void kunit_test_null_dereference(void *data)
115+
{
116+
struct kunit *test = data;
117+
int *null = NULL;
118+
119+
*null = 0;
120+
121+
KUNIT_FAIL(test, "This line should never be reached\n");
122+
}
123+
124+
static void kunit_test_fault_null_dereference(struct kunit *test)
125+
{
126+
struct kunit_try_catch_test_context *ctx = test->priv;
127+
struct kunit_try_catch *try_catch = ctx->try_catch;
128+
129+
kunit_try_catch_init(try_catch,
130+
test,
131+
kunit_test_null_dereference,
132+
kunit_test_catch);
133+
kunit_try_catch_run(try_catch, test);
134+
135+
KUNIT_EXPECT_EQ(test, try_catch->try_result, -EINTR);
136+
KUNIT_EXPECT_TRUE(test, ctx->function_called);
137+
}
138+
139+
#endif /* !CONFIG_UML */
140+
141+
static struct kunit_case kunit_fault_test_cases[] = {
142+
#ifndef CONFIG_UML
143+
KUNIT_CASE(kunit_test_fault_null_dereference),
144+
#endif /* !CONFIG_UML */
145+
{}
146+
};
147+
148+
static struct kunit_suite kunit_fault_test_suite = {
149+
.name = "kunit_fault",
150+
.init = kunit_try_catch_test_init,
151+
.test_cases = kunit_fault_test_cases,
152+
};
153+
112154
/*
113155
* Context for testing test managed resources
114156
* is_resource_initialized is used to test arbitrary resources
@@ -826,6 +868,7 @@ static struct kunit_suite kunit_current_test_suite = {
826868

827869
kunit_test_suites(&kunit_try_catch_test_suite, &kunit_resource_test_suite,
828870
&kunit_log_test_suite, &kunit_status_test_suite,
829-
&kunit_current_test_suite, &kunit_device_test_suite);
871+
&kunit_current_test_suite, &kunit_device_test_suite,
872+
&kunit_fault_test_suite);
830873

831874
MODULE_LICENSE("GPL v2");

0 commit comments

Comments
 (0)