kunit: Add tests for fault
authorMickaël Salaün <mic@digikod.net>
Mon, 8 Apr 2024 07:46:25 +0000 (09:46 +0200)
committerShuah Khan <skhan@linuxfoundation.org>
Mon, 6 May 2024 20:22:02 +0000 (14:22 -0600)
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>
lib/kunit/kunit-test.c

index f7980ef236a38bdefd8e0e7b53915f6057348617..0fdca5fffaecd198a60d21c6d0d74d88da794c5c 100644 (file)
@@ -109,6 +109,48 @@ static struct kunit_suite kunit_try_catch_test_suite = {
        .test_cases = kunit_try_catch_test_cases,
 };
 
+#ifndef CONFIG_UML
+
+static void kunit_test_null_dereference(void *data)
+{
+       struct kunit *test = data;
+       int *null = NULL;
+
+       *null = 0;
+
+       KUNIT_FAIL(test, "This line should never be reached\n");
+}
+
+static void kunit_test_fault_null_dereference(struct kunit *test)
+{
+       struct kunit_try_catch_test_context *ctx = test->priv;
+       struct kunit_try_catch *try_catch = ctx->try_catch;
+
+       kunit_try_catch_init(try_catch,
+                            test,
+                            kunit_test_null_dereference,
+                            kunit_test_catch);
+       kunit_try_catch_run(try_catch, test);
+
+       KUNIT_EXPECT_EQ(test, try_catch->try_result, -EINTR);
+       KUNIT_EXPECT_TRUE(test, ctx->function_called);
+}
+
+#endif /* !CONFIG_UML */
+
+static struct kunit_case kunit_fault_test_cases[] = {
+#ifndef CONFIG_UML
+       KUNIT_CASE(kunit_test_fault_null_dereference),
+#endif /* !CONFIG_UML */
+       {}
+};
+
+static struct kunit_suite kunit_fault_test_suite = {
+       .name = "kunit_fault",
+       .init = kunit_try_catch_test_init,
+       .test_cases = kunit_fault_test_cases,
+};
+
 /*
  * Context for testing test managed resources
  * is_resource_initialized is used to test arbitrary resources
@@ -826,6 +868,7 @@ static struct kunit_suite kunit_current_test_suite = {
 
 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_current_test_suite, &kunit_device_test_suite,
+                 &kunit_fault_test_suite);
 
 MODULE_LICENSE("GPL v2");