1 // SPDX-License-Identifier: GPL-2.0
3 * KUnit test for core test infrastructure.
5 * Copyright (C) 2019, Google LLC.
6 * Author: Brendan Higgins <brendanhiggins@google.com>
8 #include <kunit/test.h>
9 #include <kunit/test-bug.h>
11 #include "string-stream.h"
12 #include "try-catch-impl.h"
14 struct kunit_try_catch_test_context {
15 struct kunit_try_catch *try_catch;
19 static void kunit_test_successful_try(void *data)
21 struct kunit *test = data;
22 struct kunit_try_catch_test_context *ctx = test->priv;
24 ctx->function_called = true;
27 static void kunit_test_no_catch(void *data)
29 struct kunit *test = data;
31 KUNIT_FAIL(test, "Catch should not be called\n");
34 static void kunit_test_try_catch_successful_try_no_catch(struct kunit *test)
36 struct kunit_try_catch_test_context *ctx = test->priv;
37 struct kunit_try_catch *try_catch = ctx->try_catch;
39 kunit_try_catch_init(try_catch,
41 kunit_test_successful_try,
43 kunit_try_catch_run(try_catch, test);
45 KUNIT_EXPECT_TRUE(test, ctx->function_called);
48 static void kunit_test_unsuccessful_try(void *data)
50 struct kunit *test = data;
51 struct kunit_try_catch_test_context *ctx = test->priv;
52 struct kunit_try_catch *try_catch = ctx->try_catch;
54 kunit_try_catch_throw(try_catch);
55 KUNIT_FAIL(test, "This line should never be reached\n");
58 static void kunit_test_catch(void *data)
60 struct kunit *test = data;
61 struct kunit_try_catch_test_context *ctx = test->priv;
63 ctx->function_called = true;
66 static void kunit_test_try_catch_unsuccessful_try_does_catch(struct kunit *test)
68 struct kunit_try_catch_test_context *ctx = test->priv;
69 struct kunit_try_catch *try_catch = ctx->try_catch;
71 kunit_try_catch_init(try_catch,
73 kunit_test_unsuccessful_try,
75 kunit_try_catch_run(try_catch, test);
77 KUNIT_EXPECT_TRUE(test, ctx->function_called);
80 static int kunit_try_catch_test_init(struct kunit *test)
82 struct kunit_try_catch_test_context *ctx;
84 ctx = kunit_kzalloc(test, sizeof(*ctx), GFP_KERNEL);
85 KUNIT_ASSERT_NOT_ERR_OR_NULL(test, ctx);
88 ctx->try_catch = kunit_kmalloc(test,
89 sizeof(*ctx->try_catch),
91 KUNIT_ASSERT_NOT_ERR_OR_NULL(test, ctx->try_catch);
96 static struct kunit_case kunit_try_catch_test_cases[] = {
97 KUNIT_CASE(kunit_test_try_catch_successful_try_no_catch),
98 KUNIT_CASE(kunit_test_try_catch_unsuccessful_try_does_catch),
102 static struct kunit_suite kunit_try_catch_test_suite = {
103 .name = "kunit-try-catch-test",
104 .init = kunit_try_catch_test_init,
105 .test_cases = kunit_try_catch_test_cases,
109 * Context for testing test managed resources
110 * is_resource_initialized is used to test arbitrary resources
112 struct kunit_test_resource_context {
114 bool is_resource_initialized;
115 int allocate_order[2];
119 static int fake_resource_init(struct kunit_resource *res, void *context)
121 struct kunit_test_resource_context *ctx = context;
123 res->data = &ctx->is_resource_initialized;
124 ctx->is_resource_initialized = true;
128 static void fake_resource_free(struct kunit_resource *res)
130 bool *is_resource_initialized = res->data;
132 *is_resource_initialized = false;
135 static void kunit_resource_test_init_resources(struct kunit *test)
137 struct kunit_test_resource_context *ctx = test->priv;
139 kunit_init_test(&ctx->test, "testing_test_init_test", NULL);
141 KUNIT_EXPECT_TRUE(test, list_empty(&ctx->test.resources));
144 static void kunit_resource_test_alloc_resource(struct kunit *test)
146 struct kunit_test_resource_context *ctx = test->priv;
147 struct kunit_resource *res;
148 kunit_resource_free_t free = fake_resource_free;
150 res = kunit_alloc_and_get_resource(&ctx->test,
156 KUNIT_ASSERT_NOT_ERR_OR_NULL(test, res);
157 KUNIT_EXPECT_PTR_EQ(test,
158 &ctx->is_resource_initialized,
160 KUNIT_EXPECT_TRUE(test, list_is_last(&res->node, &ctx->test.resources));
161 KUNIT_EXPECT_PTR_EQ(test, free, res->free);
163 kunit_put_resource(res);
166 static inline bool kunit_resource_instance_match(struct kunit *test,
167 struct kunit_resource *res,
170 return res->data == match_data;
174 * Note: tests below use kunit_alloc_and_get_resource(), so as a consequence
175 * they have a reference to the associated resource that they must release
176 * via kunit_put_resource(). In normal operation, users will only
177 * have to do this for cases where they use kunit_find_resource(), and the
178 * kunit_alloc_resource() function will be used (which does not take a
179 * resource reference).
181 static void kunit_resource_test_destroy_resource(struct kunit *test)
183 struct kunit_test_resource_context *ctx = test->priv;
184 struct kunit_resource *res = kunit_alloc_and_get_resource(
191 kunit_put_resource(res);
193 KUNIT_ASSERT_FALSE(test,
194 kunit_destroy_resource(&ctx->test,
195 kunit_resource_instance_match,
198 KUNIT_EXPECT_FALSE(test, ctx->is_resource_initialized);
199 KUNIT_EXPECT_TRUE(test, list_empty(&ctx->test.resources));
202 static void kunit_resource_test_remove_resource(struct kunit *test)
204 struct kunit_test_resource_context *ctx = test->priv;
205 struct kunit_resource *res = kunit_alloc_and_get_resource(
212 /* The resource is in the list */
213 KUNIT_EXPECT_FALSE(test, list_empty(&ctx->test.resources));
215 /* Remove the resource. The pointer is still valid, but it can't be
218 kunit_remove_resource(test, res);
219 KUNIT_EXPECT_TRUE(test, list_empty(&ctx->test.resources));
220 /* We haven't been freed yet. */
221 KUNIT_EXPECT_TRUE(test, ctx->is_resource_initialized);
223 /* Removing the resource multiple times is valid. */
224 kunit_remove_resource(test, res);
225 KUNIT_EXPECT_TRUE(test, list_empty(&ctx->test.resources));
226 /* Despite having been removed twice (from only one reference), the
227 * resource still has not been freed.
229 KUNIT_EXPECT_TRUE(test, ctx->is_resource_initialized);
231 /* Free the resource. */
232 kunit_put_resource(res);
233 KUNIT_EXPECT_FALSE(test, ctx->is_resource_initialized);
236 static void kunit_resource_test_cleanup_resources(struct kunit *test)
239 struct kunit_test_resource_context *ctx = test->priv;
240 struct kunit_resource *resources[5];
242 for (i = 0; i < ARRAY_SIZE(resources); i++) {
243 resources[i] = kunit_alloc_and_get_resource(&ctx->test,
248 kunit_put_resource(resources[i]);
251 kunit_cleanup(&ctx->test);
253 KUNIT_EXPECT_TRUE(test, list_empty(&ctx->test.resources));
256 static void kunit_resource_test_mark_order(int order_array[],
262 for (i = 0; i < order_size && order_array[i]; i++)
265 order_array[i] = key;
268 #define KUNIT_RESOURCE_TEST_MARK_ORDER(ctx, order_field, key) \
269 kunit_resource_test_mark_order(ctx->order_field, \
270 ARRAY_SIZE(ctx->order_field), \
273 static int fake_resource_2_init(struct kunit_resource *res, void *context)
275 struct kunit_test_resource_context *ctx = context;
277 KUNIT_RESOURCE_TEST_MARK_ORDER(ctx, allocate_order, 2);
284 static void fake_resource_2_free(struct kunit_resource *res)
286 struct kunit_test_resource_context *ctx = res->data;
288 KUNIT_RESOURCE_TEST_MARK_ORDER(ctx, free_order, 2);
291 static int fake_resource_1_init(struct kunit_resource *res, void *context)
293 struct kunit_test_resource_context *ctx = context;
294 struct kunit_resource *res2;
296 res2 = kunit_alloc_and_get_resource(&ctx->test,
297 fake_resource_2_init,
298 fake_resource_2_free,
302 KUNIT_RESOURCE_TEST_MARK_ORDER(ctx, allocate_order, 1);
306 kunit_put_resource(res2);
311 static void fake_resource_1_free(struct kunit_resource *res)
313 struct kunit_test_resource_context *ctx = res->data;
315 KUNIT_RESOURCE_TEST_MARK_ORDER(ctx, free_order, 1);
319 * TODO(brendanhiggins@google.com): replace the arrays that keep track of the
320 * order of allocation and freeing with strict mocks using the IN_SEQUENCE macro
321 * to assert allocation and freeing order when the feature becomes available.
323 static void kunit_resource_test_proper_free_ordering(struct kunit *test)
325 struct kunit_test_resource_context *ctx = test->priv;
326 struct kunit_resource *res;
328 /* fake_resource_1 allocates a fake_resource_2 in its init. */
329 res = kunit_alloc_and_get_resource(&ctx->test,
330 fake_resource_1_init,
331 fake_resource_1_free,
336 * Since fake_resource_2_init calls KUNIT_RESOURCE_TEST_MARK_ORDER
337 * before returning to fake_resource_1_init, it should be the first to
338 * put its key in the allocate_order array.
340 KUNIT_EXPECT_EQ(test, ctx->allocate_order[0], 2);
341 KUNIT_EXPECT_EQ(test, ctx->allocate_order[1], 1);
343 kunit_put_resource(res);
345 kunit_cleanup(&ctx->test);
348 * Because fake_resource_2 finishes allocation before fake_resource_1,
349 * fake_resource_1 should be freed first since it could depend on
352 KUNIT_EXPECT_EQ(test, ctx->free_order[0], 1);
353 KUNIT_EXPECT_EQ(test, ctx->free_order[1], 2);
356 static void kunit_resource_test_static(struct kunit *test)
358 struct kunit_test_resource_context ctx;
359 struct kunit_resource res;
361 KUNIT_EXPECT_EQ(test, kunit_add_resource(test, NULL, NULL, &res, &ctx),
364 KUNIT_EXPECT_PTR_EQ(test, res.data, (void *)&ctx);
368 KUNIT_EXPECT_TRUE(test, list_empty(&test->resources));
371 static void kunit_resource_test_named(struct kunit *test)
373 struct kunit_resource res1, res2, *found = NULL;
374 struct kunit_test_resource_context ctx;
376 KUNIT_EXPECT_EQ(test,
377 kunit_add_named_resource(test, NULL, NULL, &res1,
380 KUNIT_EXPECT_PTR_EQ(test, res1.data, (void *)&ctx);
382 KUNIT_EXPECT_EQ(test,
383 kunit_add_named_resource(test, NULL, NULL, &res1,
387 KUNIT_EXPECT_EQ(test,
388 kunit_add_named_resource(test, NULL, NULL, &res2,
392 found = kunit_find_named_resource(test, "resource_1");
394 KUNIT_EXPECT_PTR_EQ(test, found, &res1);
397 kunit_put_resource(&res1);
399 KUNIT_EXPECT_EQ(test, kunit_destroy_named_resource(test, "resource_2"),
404 KUNIT_EXPECT_TRUE(test, list_empty(&test->resources));
407 static void increment_int(void *ctx)
413 static void kunit_resource_test_action(struct kunit *test)
417 kunit_add_action(test, increment_int, &num_actions);
418 KUNIT_EXPECT_EQ(test, num_actions, 0);
420 KUNIT_EXPECT_EQ(test, num_actions, 1);
422 /* Once we've cleaned up, the action queue is empty. */
424 KUNIT_EXPECT_EQ(test, num_actions, 1);
426 /* Check the same function can be deferred multiple times. */
427 kunit_add_action(test, increment_int, &num_actions);
428 kunit_add_action(test, increment_int, &num_actions);
430 KUNIT_EXPECT_EQ(test, num_actions, 3);
432 static void kunit_resource_test_remove_action(struct kunit *test)
436 kunit_add_action(test, increment_int, &num_actions);
437 KUNIT_EXPECT_EQ(test, num_actions, 0);
439 kunit_remove_action(test, increment_int, &num_actions);
441 KUNIT_EXPECT_EQ(test, num_actions, 0);
443 static void kunit_resource_test_release_action(struct kunit *test)
447 kunit_add_action(test, increment_int, &num_actions);
448 KUNIT_EXPECT_EQ(test, num_actions, 0);
449 /* Runs immediately on trigger. */
450 kunit_release_action(test, increment_int, &num_actions);
451 KUNIT_EXPECT_EQ(test, num_actions, 1);
453 /* Doesn't run again on test exit. */
455 KUNIT_EXPECT_EQ(test, num_actions, 1);
457 static void action_order_1(void *ctx)
459 struct kunit_test_resource_context *res_ctx = (struct kunit_test_resource_context *)ctx;
461 KUNIT_RESOURCE_TEST_MARK_ORDER(res_ctx, free_order, 1);
462 kunit_log(KERN_INFO, current->kunit_test, "action_order_1");
464 static void action_order_2(void *ctx)
466 struct kunit_test_resource_context *res_ctx = (struct kunit_test_resource_context *)ctx;
468 KUNIT_RESOURCE_TEST_MARK_ORDER(res_ctx, free_order, 2);
469 kunit_log(KERN_INFO, current->kunit_test, "action_order_2");
471 static void kunit_resource_test_action_ordering(struct kunit *test)
473 struct kunit_test_resource_context *ctx = test->priv;
475 kunit_add_action(test, action_order_1, ctx);
476 kunit_add_action(test, action_order_2, ctx);
477 kunit_add_action(test, action_order_1, ctx);
478 kunit_add_action(test, action_order_2, ctx);
479 kunit_remove_action(test, action_order_1, ctx);
480 kunit_release_action(test, action_order_2, ctx);
483 /* [2 is triggered] [2], [(1 is cancelled)] [1] */
484 KUNIT_EXPECT_EQ(test, ctx->free_order[0], 2);
485 KUNIT_EXPECT_EQ(test, ctx->free_order[1], 2);
486 KUNIT_EXPECT_EQ(test, ctx->free_order[2], 1);
489 static int kunit_resource_test_init(struct kunit *test)
491 struct kunit_test_resource_context *ctx =
492 kzalloc(sizeof(*ctx), GFP_KERNEL);
494 KUNIT_ASSERT_NOT_ERR_OR_NULL(test, ctx);
498 kunit_init_test(&ctx->test, "test_test_context", NULL);
503 static void kunit_resource_test_exit(struct kunit *test)
505 struct kunit_test_resource_context *ctx = test->priv;
507 kunit_cleanup(&ctx->test);
511 static struct kunit_case kunit_resource_test_cases[] = {
512 KUNIT_CASE(kunit_resource_test_init_resources),
513 KUNIT_CASE(kunit_resource_test_alloc_resource),
514 KUNIT_CASE(kunit_resource_test_destroy_resource),
515 KUNIT_CASE(kunit_resource_test_remove_resource),
516 KUNIT_CASE(kunit_resource_test_cleanup_resources),
517 KUNIT_CASE(kunit_resource_test_proper_free_ordering),
518 KUNIT_CASE(kunit_resource_test_static),
519 KUNIT_CASE(kunit_resource_test_named),
520 KUNIT_CASE(kunit_resource_test_action),
521 KUNIT_CASE(kunit_resource_test_remove_action),
522 KUNIT_CASE(kunit_resource_test_release_action),
523 KUNIT_CASE(kunit_resource_test_action_ordering),
527 static struct kunit_suite kunit_resource_test_suite = {
528 .name = "kunit-resource-test",
529 .init = kunit_resource_test_init,
530 .exit = kunit_resource_test_exit,
531 .test_cases = kunit_resource_test_cases,
535 * Log tests call string_stream functions, which aren't exported. So only
536 * build this code if this test is built-in.
538 #if IS_BUILTIN(CONFIG_KUNIT_TEST)
540 /* This avoids a cast warning if kfree() is passed direct to kunit_add_action(). */
541 static void kfree_wrapper(void *p)
546 static void kunit_log_test(struct kunit *test)
548 struct kunit_suite suite;
549 #ifdef CONFIG_KUNIT_DEBUGFS
552 suite.log = kunit_alloc_string_stream(test, GFP_KERNEL);
553 KUNIT_ASSERT_NOT_ERR_OR_NULL(test, suite.log);
554 string_stream_set_append_newlines(suite.log, true);
556 kunit_log(KERN_INFO, test, "put this in log.");
557 kunit_log(KERN_INFO, test, "this too.");
558 kunit_log(KERN_INFO, &suite, "add to suite log.");
559 kunit_log(KERN_INFO, &suite, "along with this.");
561 #ifdef CONFIG_KUNIT_DEBUGFS
562 KUNIT_EXPECT_TRUE(test, test->log->append_newlines);
564 full_log = string_stream_get_string(test->log);
565 kunit_add_action(test, (kunit_action_t *)kfree, full_log);
566 KUNIT_EXPECT_NOT_ERR_OR_NULL(test,
567 strstr(full_log, "put this in log."));
568 KUNIT_EXPECT_NOT_ERR_OR_NULL(test,
569 strstr(full_log, "this too."));
571 full_log = string_stream_get_string(suite.log);
572 kunit_add_action(test, kfree_wrapper, full_log);
573 KUNIT_EXPECT_NOT_ERR_OR_NULL(test,
574 strstr(full_log, "add to suite log."));
575 KUNIT_EXPECT_NOT_ERR_OR_NULL(test,
576 strstr(full_log, "along with this."));
578 KUNIT_EXPECT_NULL(test, test->log);
582 static void kunit_log_newline_test(struct kunit *test)
586 kunit_info(test, "Add newline\n");
588 full_log = string_stream_get_string(test->log);
589 kunit_add_action(test, kfree_wrapper, full_log);
590 KUNIT_ASSERT_NOT_NULL_MSG(test, strstr(full_log, "Add newline\n"),
591 "Missing log line, full log:\n%s", full_log);
592 KUNIT_EXPECT_NULL(test, strstr(full_log, "Add newline\n\n"));
594 kunit_skip(test, "only useful when debugfs is enabled");
598 static void kunit_log_test(struct kunit *test)
600 kunit_skip(test, "Log tests only run when built-in");
603 static void kunit_log_newline_test(struct kunit *test)
605 kunit_skip(test, "Log tests only run when built-in");
607 #endif /* IS_BUILTIN(CONFIG_KUNIT_TEST) */
609 static struct kunit_case kunit_log_test_cases[] = {
610 KUNIT_CASE(kunit_log_test),
611 KUNIT_CASE(kunit_log_newline_test),
615 static struct kunit_suite kunit_log_test_suite = {
616 .name = "kunit-log-test",
617 .test_cases = kunit_log_test_cases,
620 static void kunit_status_set_failure_test(struct kunit *test)
624 kunit_init_test(&fake, "fake test", NULL);
626 KUNIT_EXPECT_EQ(test, fake.status, (enum kunit_status)KUNIT_SUCCESS);
627 kunit_set_failure(&fake);
628 KUNIT_EXPECT_EQ(test, fake.status, (enum kunit_status)KUNIT_FAILURE);
631 static void kunit_status_mark_skipped_test(struct kunit *test)
635 kunit_init_test(&fake, "fake test", NULL);
637 /* Before: Should be SUCCESS with no comment. */
638 KUNIT_EXPECT_EQ(test, fake.status, KUNIT_SUCCESS);
639 KUNIT_EXPECT_STREQ(test, fake.status_comment, "");
641 /* Mark the test as skipped. */
642 kunit_mark_skipped(&fake, "Accepts format string: %s", "YES");
644 /* After: Should be SKIPPED with our comment. */
645 KUNIT_EXPECT_EQ(test, fake.status, (enum kunit_status)KUNIT_SKIPPED);
646 KUNIT_EXPECT_STREQ(test, fake.status_comment, "Accepts format string: YES");
649 static struct kunit_case kunit_status_test_cases[] = {
650 KUNIT_CASE(kunit_status_set_failure_test),
651 KUNIT_CASE(kunit_status_mark_skipped_test),
655 static struct kunit_suite kunit_status_test_suite = {
656 .name = "kunit_status",
657 .test_cases = kunit_status_test_cases,
660 static void kunit_current_test(struct kunit *test)
662 /* Check results of both current->kunit_test and
663 * kunit_get_current_test() are equivalent to current test.
665 KUNIT_EXPECT_PTR_EQ(test, test, current->kunit_test);
666 KUNIT_EXPECT_PTR_EQ(test, test, kunit_get_current_test());
669 static void kunit_current_fail_test(struct kunit *test)
673 kunit_init_test(&fake, "fake test", NULL);
674 KUNIT_EXPECT_EQ(test, fake.status, KUNIT_SUCCESS);
676 /* Set current->kunit_test to fake test. */
677 current->kunit_test = &fake;
679 kunit_fail_current_test("This should make `fake` test fail.");
680 KUNIT_EXPECT_EQ(test, fake.status, (enum kunit_status)KUNIT_FAILURE);
681 kunit_cleanup(&fake);
683 /* Reset current->kunit_test to current test. */
684 current->kunit_test = test;
687 static struct kunit_case kunit_current_test_cases[] = {
688 KUNIT_CASE(kunit_current_test),
689 KUNIT_CASE(kunit_current_fail_test),
693 static struct kunit_suite kunit_current_test_suite = {
694 .name = "kunit_current",
695 .test_cases = kunit_current_test_cases,
698 kunit_test_suites(&kunit_try_catch_test_suite, &kunit_resource_test_suite,
699 &kunit_log_test_suite, &kunit_status_test_suite,
700 &kunit_current_test_suite);
702 MODULE_LICENSE("GPL v2");