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 "try-catch-impl.h"
13 struct kunit_try_catch_test_context {
14 struct kunit_try_catch *try_catch;
18 static void kunit_test_successful_try(void *data)
20 struct kunit *test = data;
21 struct kunit_try_catch_test_context *ctx = test->priv;
23 ctx->function_called = true;
26 static void kunit_test_no_catch(void *data)
28 struct kunit *test = data;
30 KUNIT_FAIL(test, "Catch should not be called\n");
33 static void kunit_test_try_catch_successful_try_no_catch(struct kunit *test)
35 struct kunit_try_catch_test_context *ctx = test->priv;
36 struct kunit_try_catch *try_catch = ctx->try_catch;
38 kunit_try_catch_init(try_catch,
40 kunit_test_successful_try,
42 kunit_try_catch_run(try_catch, test);
44 KUNIT_EXPECT_TRUE(test, ctx->function_called);
47 static void kunit_test_unsuccessful_try(void *data)
49 struct kunit *test = data;
50 struct kunit_try_catch_test_context *ctx = test->priv;
51 struct kunit_try_catch *try_catch = ctx->try_catch;
53 kunit_try_catch_throw(try_catch);
54 KUNIT_FAIL(test, "This line should never be reached\n");
57 static void kunit_test_catch(void *data)
59 struct kunit *test = data;
60 struct kunit_try_catch_test_context *ctx = test->priv;
62 ctx->function_called = true;
65 static void kunit_test_try_catch_unsuccessful_try_does_catch(struct kunit *test)
67 struct kunit_try_catch_test_context *ctx = test->priv;
68 struct kunit_try_catch *try_catch = ctx->try_catch;
70 kunit_try_catch_init(try_catch,
72 kunit_test_unsuccessful_try,
74 kunit_try_catch_run(try_catch, test);
76 KUNIT_EXPECT_TRUE(test, ctx->function_called);
79 static int kunit_try_catch_test_init(struct kunit *test)
81 struct kunit_try_catch_test_context *ctx;
83 ctx = kunit_kzalloc(test, sizeof(*ctx), GFP_KERNEL);
84 KUNIT_ASSERT_NOT_ERR_OR_NULL(test, ctx);
87 ctx->try_catch = kunit_kmalloc(test,
88 sizeof(*ctx->try_catch),
90 KUNIT_ASSERT_NOT_ERR_OR_NULL(test, ctx->try_catch);
95 static struct kunit_case kunit_try_catch_test_cases[] = {
96 KUNIT_CASE(kunit_test_try_catch_successful_try_no_catch),
97 KUNIT_CASE(kunit_test_try_catch_unsuccessful_try_does_catch),
101 static struct kunit_suite kunit_try_catch_test_suite = {
102 .name = "kunit-try-catch-test",
103 .init = kunit_try_catch_test_init,
104 .test_cases = kunit_try_catch_test_cases,
108 * Context for testing test managed resources
109 * is_resource_initialized is used to test arbitrary resources
111 struct kunit_test_resource_context {
113 bool is_resource_initialized;
114 int allocate_order[2];
118 static int fake_resource_init(struct kunit_resource *res, void *context)
120 struct kunit_test_resource_context *ctx = context;
122 res->data = &ctx->is_resource_initialized;
123 ctx->is_resource_initialized = true;
127 static void fake_resource_free(struct kunit_resource *res)
129 bool *is_resource_initialized = res->data;
131 *is_resource_initialized = false;
134 static void kunit_resource_test_init_resources(struct kunit *test)
136 struct kunit_test_resource_context *ctx = test->priv;
138 kunit_init_test(&ctx->test, "testing_test_init_test", NULL);
140 KUNIT_EXPECT_TRUE(test, list_empty(&ctx->test.resources));
143 static void kunit_resource_test_alloc_resource(struct kunit *test)
145 struct kunit_test_resource_context *ctx = test->priv;
146 struct kunit_resource *res;
147 kunit_resource_free_t free = fake_resource_free;
149 res = kunit_alloc_and_get_resource(&ctx->test,
155 KUNIT_ASSERT_NOT_ERR_OR_NULL(test, res);
156 KUNIT_EXPECT_PTR_EQ(test,
157 &ctx->is_resource_initialized,
159 KUNIT_EXPECT_TRUE(test, list_is_last(&res->node, &ctx->test.resources));
160 KUNIT_EXPECT_PTR_EQ(test, free, res->free);
162 kunit_put_resource(res);
165 static inline bool kunit_resource_instance_match(struct kunit *test,
166 struct kunit_resource *res,
169 return res->data == match_data;
173 * Note: tests below use kunit_alloc_and_get_resource(), so as a consequence
174 * they have a reference to the associated resource that they must release
175 * via kunit_put_resource(). In normal operation, users will only
176 * have to do this for cases where they use kunit_find_resource(), and the
177 * kunit_alloc_resource() function will be used (which does not take a
178 * resource reference).
180 static void kunit_resource_test_destroy_resource(struct kunit *test)
182 struct kunit_test_resource_context *ctx = test->priv;
183 struct kunit_resource *res = kunit_alloc_and_get_resource(
190 kunit_put_resource(res);
192 KUNIT_ASSERT_FALSE(test,
193 kunit_destroy_resource(&ctx->test,
194 kunit_resource_instance_match,
197 KUNIT_EXPECT_FALSE(test, ctx->is_resource_initialized);
198 KUNIT_EXPECT_TRUE(test, list_empty(&ctx->test.resources));
201 static void kunit_resource_test_remove_resource(struct kunit *test)
203 struct kunit_test_resource_context *ctx = test->priv;
204 struct kunit_resource *res = kunit_alloc_and_get_resource(
211 /* The resource is in the list */
212 KUNIT_EXPECT_FALSE(test, list_empty(&ctx->test.resources));
214 /* Remove the resource. The pointer is still valid, but it can't be
217 kunit_remove_resource(test, res);
218 KUNIT_EXPECT_TRUE(test, list_empty(&ctx->test.resources));
219 /* We haven't been freed yet. */
220 KUNIT_EXPECT_TRUE(test, ctx->is_resource_initialized);
222 /* Removing the resource multiple times is valid. */
223 kunit_remove_resource(test, res);
224 KUNIT_EXPECT_TRUE(test, list_empty(&ctx->test.resources));
225 /* Despite having been removed twice (from only one reference), the
226 * resource still has not been freed.
228 KUNIT_EXPECT_TRUE(test, ctx->is_resource_initialized);
230 /* Free the resource. */
231 kunit_put_resource(res);
232 KUNIT_EXPECT_FALSE(test, ctx->is_resource_initialized);
235 static void kunit_resource_test_cleanup_resources(struct kunit *test)
238 struct kunit_test_resource_context *ctx = test->priv;
239 struct kunit_resource *resources[5];
241 for (i = 0; i < ARRAY_SIZE(resources); i++) {
242 resources[i] = kunit_alloc_and_get_resource(&ctx->test,
247 kunit_put_resource(resources[i]);
250 kunit_cleanup(&ctx->test);
252 KUNIT_EXPECT_TRUE(test, list_empty(&ctx->test.resources));
255 static void kunit_resource_test_mark_order(int order_array[],
261 for (i = 0; i < order_size && order_array[i]; i++)
264 order_array[i] = key;
267 #define KUNIT_RESOURCE_TEST_MARK_ORDER(ctx, order_field, key) \
268 kunit_resource_test_mark_order(ctx->order_field, \
269 ARRAY_SIZE(ctx->order_field), \
272 static int fake_resource_2_init(struct kunit_resource *res, void *context)
274 struct kunit_test_resource_context *ctx = context;
276 KUNIT_RESOURCE_TEST_MARK_ORDER(ctx, allocate_order, 2);
283 static void fake_resource_2_free(struct kunit_resource *res)
285 struct kunit_test_resource_context *ctx = res->data;
287 KUNIT_RESOURCE_TEST_MARK_ORDER(ctx, free_order, 2);
290 static int fake_resource_1_init(struct kunit_resource *res, void *context)
292 struct kunit_test_resource_context *ctx = context;
293 struct kunit_resource *res2;
295 res2 = kunit_alloc_and_get_resource(&ctx->test,
296 fake_resource_2_init,
297 fake_resource_2_free,
301 KUNIT_RESOURCE_TEST_MARK_ORDER(ctx, allocate_order, 1);
305 kunit_put_resource(res2);
310 static void fake_resource_1_free(struct kunit_resource *res)
312 struct kunit_test_resource_context *ctx = res->data;
314 KUNIT_RESOURCE_TEST_MARK_ORDER(ctx, free_order, 1);
318 * TODO(brendanhiggins@google.com): replace the arrays that keep track of the
319 * order of allocation and freeing with strict mocks using the IN_SEQUENCE macro
320 * to assert allocation and freeing order when the feature becomes available.
322 static void kunit_resource_test_proper_free_ordering(struct kunit *test)
324 struct kunit_test_resource_context *ctx = test->priv;
325 struct kunit_resource *res;
327 /* fake_resource_1 allocates a fake_resource_2 in its init. */
328 res = kunit_alloc_and_get_resource(&ctx->test,
329 fake_resource_1_init,
330 fake_resource_1_free,
335 * Since fake_resource_2_init calls KUNIT_RESOURCE_TEST_MARK_ORDER
336 * before returning to fake_resource_1_init, it should be the first to
337 * put its key in the allocate_order array.
339 KUNIT_EXPECT_EQ(test, ctx->allocate_order[0], 2);
340 KUNIT_EXPECT_EQ(test, ctx->allocate_order[1], 1);
342 kunit_put_resource(res);
344 kunit_cleanup(&ctx->test);
347 * Because fake_resource_2 finishes allocation before fake_resource_1,
348 * fake_resource_1 should be freed first since it could depend on
351 KUNIT_EXPECT_EQ(test, ctx->free_order[0], 1);
352 KUNIT_EXPECT_EQ(test, ctx->free_order[1], 2);
355 static void kunit_resource_test_static(struct kunit *test)
357 struct kunit_test_resource_context ctx;
358 struct kunit_resource res;
360 KUNIT_EXPECT_EQ(test, kunit_add_resource(test, NULL, NULL, &res, &ctx),
363 KUNIT_EXPECT_PTR_EQ(test, res.data, (void *)&ctx);
367 KUNIT_EXPECT_TRUE(test, list_empty(&test->resources));
370 static void kunit_resource_test_named(struct kunit *test)
372 struct kunit_resource res1, res2, *found = NULL;
373 struct kunit_test_resource_context ctx;
375 KUNIT_EXPECT_EQ(test,
376 kunit_add_named_resource(test, NULL, NULL, &res1,
379 KUNIT_EXPECT_PTR_EQ(test, res1.data, (void *)&ctx);
381 KUNIT_EXPECT_EQ(test,
382 kunit_add_named_resource(test, NULL, NULL, &res1,
386 KUNIT_EXPECT_EQ(test,
387 kunit_add_named_resource(test, NULL, NULL, &res2,
391 found = kunit_find_named_resource(test, "resource_1");
393 KUNIT_EXPECT_PTR_EQ(test, found, &res1);
396 kunit_put_resource(&res1);
398 KUNIT_EXPECT_EQ(test, kunit_destroy_named_resource(test, "resource_2"),
403 KUNIT_EXPECT_TRUE(test, list_empty(&test->resources));
406 static int kunit_resource_test_init(struct kunit *test)
408 struct kunit_test_resource_context *ctx =
409 kzalloc(sizeof(*ctx), GFP_KERNEL);
411 KUNIT_ASSERT_NOT_ERR_OR_NULL(test, ctx);
415 kunit_init_test(&ctx->test, "test_test_context", NULL);
420 static void kunit_resource_test_exit(struct kunit *test)
422 struct kunit_test_resource_context *ctx = test->priv;
424 kunit_cleanup(&ctx->test);
428 static struct kunit_case kunit_resource_test_cases[] = {
429 KUNIT_CASE(kunit_resource_test_init_resources),
430 KUNIT_CASE(kunit_resource_test_alloc_resource),
431 KUNIT_CASE(kunit_resource_test_destroy_resource),
432 KUNIT_CASE(kunit_resource_test_remove_resource),
433 KUNIT_CASE(kunit_resource_test_cleanup_resources),
434 KUNIT_CASE(kunit_resource_test_proper_free_ordering),
435 KUNIT_CASE(kunit_resource_test_static),
436 KUNIT_CASE(kunit_resource_test_named),
440 static struct kunit_suite kunit_resource_test_suite = {
441 .name = "kunit-resource-test",
442 .init = kunit_resource_test_init,
443 .exit = kunit_resource_test_exit,
444 .test_cases = kunit_resource_test_cases,
447 static void kunit_log_test(struct kunit *test)
449 struct kunit_suite suite;
451 suite.log = kunit_kzalloc(test, KUNIT_LOG_SIZE, GFP_KERNEL);
452 KUNIT_ASSERT_NOT_ERR_OR_NULL(test, suite.log);
454 kunit_log(KERN_INFO, test, "put this in log.");
455 kunit_log(KERN_INFO, test, "this too.");
456 kunit_log(KERN_INFO, &suite, "add to suite log.");
457 kunit_log(KERN_INFO, &suite, "along with this.");
459 #ifdef CONFIG_KUNIT_DEBUGFS
460 KUNIT_EXPECT_NOT_ERR_OR_NULL(test,
461 strstr(test->log, "put this in log."));
462 KUNIT_EXPECT_NOT_ERR_OR_NULL(test,
463 strstr(test->log, "this too."));
464 KUNIT_EXPECT_NOT_ERR_OR_NULL(test,
465 strstr(suite.log, "add to suite log."));
466 KUNIT_EXPECT_NOT_ERR_OR_NULL(test,
467 strstr(suite.log, "along with this."));
469 KUNIT_EXPECT_NULL(test, test->log);
473 static void kunit_log_newline_test(struct kunit *test)
475 kunit_info(test, "Add newline\n");
477 KUNIT_ASSERT_NOT_NULL_MSG(test, strstr(test->log, "Add newline\n"),
478 "Missing log line, full log:\n%s", test->log);
479 KUNIT_EXPECT_NULL(test, strstr(test->log, "Add newline\n\n"));
481 kunit_skip(test, "only useful when debugfs is enabled");
485 static struct kunit_case kunit_log_test_cases[] = {
486 KUNIT_CASE(kunit_log_test),
487 KUNIT_CASE(kunit_log_newline_test),
491 static struct kunit_suite kunit_log_test_suite = {
492 .name = "kunit-log-test",
493 .test_cases = kunit_log_test_cases,
496 static void kunit_status_set_failure_test(struct kunit *test)
500 kunit_init_test(&fake, "fake test", NULL);
502 KUNIT_EXPECT_EQ(test, fake.status, (enum kunit_status)KUNIT_SUCCESS);
503 kunit_set_failure(&fake);
504 KUNIT_EXPECT_EQ(test, fake.status, (enum kunit_status)KUNIT_FAILURE);
507 static void kunit_status_mark_skipped_test(struct kunit *test)
511 kunit_init_test(&fake, "fake test", NULL);
513 /* Before: Should be SUCCESS with no comment. */
514 KUNIT_EXPECT_EQ(test, fake.status, KUNIT_SUCCESS);
515 KUNIT_EXPECT_STREQ(test, fake.status_comment, "");
517 /* Mark the test as skipped. */
518 kunit_mark_skipped(&fake, "Accepts format string: %s", "YES");
520 /* After: Should be SKIPPED with our comment. */
521 KUNIT_EXPECT_EQ(test, fake.status, (enum kunit_status)KUNIT_SKIPPED);
522 KUNIT_EXPECT_STREQ(test, fake.status_comment, "Accepts format string: YES");
525 static struct kunit_case kunit_status_test_cases[] = {
526 KUNIT_CASE(kunit_status_set_failure_test),
527 KUNIT_CASE(kunit_status_mark_skipped_test),
531 static struct kunit_suite kunit_status_test_suite = {
532 .name = "kunit_status",
533 .test_cases = kunit_status_test_cases,
536 static void kunit_current_test(struct kunit *test)
538 /* Check results of both current->kunit_test and
539 * kunit_get_current_test() are equivalent to current test.
541 KUNIT_EXPECT_PTR_EQ(test, test, current->kunit_test);
542 KUNIT_EXPECT_PTR_EQ(test, test, kunit_get_current_test());
545 static void kunit_current_fail_test(struct kunit *test)
549 kunit_init_test(&fake, "fake test", NULL);
550 KUNIT_EXPECT_EQ(test, fake.status, KUNIT_SUCCESS);
552 /* Set current->kunit_test to fake test. */
553 current->kunit_test = &fake;
555 kunit_fail_current_test("This should make `fake` test fail.");
556 KUNIT_EXPECT_EQ(test, fake.status, (enum kunit_status)KUNIT_FAILURE);
557 kunit_cleanup(&fake);
559 /* Reset current->kunit_test to current test. */
560 current->kunit_test = test;
563 static struct kunit_case kunit_current_test_cases[] = {
564 KUNIT_CASE(kunit_current_test),
565 KUNIT_CASE(kunit_current_fail_test),
569 static struct kunit_suite kunit_current_test_suite = {
570 .name = "kunit_current",
571 .test_cases = kunit_current_test_cases,
574 kunit_test_suites(&kunit_try_catch_test_suite, &kunit_resource_test_suite,
575 &kunit_log_test_suite, &kunit_status_test_suite,
576 &kunit_current_test_suite);
578 MODULE_LICENSE("GPL v2");