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>
10 #include "try-catch-impl.h"
12 struct kunit_try_catch_test_context {
13 struct kunit_try_catch *try_catch;
17 static void kunit_test_successful_try(void *data)
19 struct kunit *test = data;
20 struct kunit_try_catch_test_context *ctx = test->priv;
22 ctx->function_called = true;
25 static void kunit_test_no_catch(void *data)
27 struct kunit *test = data;
29 KUNIT_FAIL(test, "Catch should not be called\n");
32 static void kunit_test_try_catch_successful_try_no_catch(struct kunit *test)
34 struct kunit_try_catch_test_context *ctx = test->priv;
35 struct kunit_try_catch *try_catch = ctx->try_catch;
37 kunit_try_catch_init(try_catch,
39 kunit_test_successful_try,
41 kunit_try_catch_run(try_catch, test);
43 KUNIT_EXPECT_TRUE(test, ctx->function_called);
46 static void kunit_test_unsuccessful_try(void *data)
48 struct kunit *test = data;
49 struct kunit_try_catch_test_context *ctx = test->priv;
50 struct kunit_try_catch *try_catch = ctx->try_catch;
52 kunit_try_catch_throw(try_catch);
53 KUNIT_FAIL(test, "This line should never be reached\n");
56 static void kunit_test_catch(void *data)
58 struct kunit *test = data;
59 struct kunit_try_catch_test_context *ctx = test->priv;
61 ctx->function_called = true;
64 static void kunit_test_try_catch_unsuccessful_try_does_catch(struct kunit *test)
66 struct kunit_try_catch_test_context *ctx = test->priv;
67 struct kunit_try_catch *try_catch = ctx->try_catch;
69 kunit_try_catch_init(try_catch,
71 kunit_test_unsuccessful_try,
73 kunit_try_catch_run(try_catch, test);
75 KUNIT_EXPECT_TRUE(test, ctx->function_called);
78 static int kunit_try_catch_test_init(struct kunit *test)
80 struct kunit_try_catch_test_context *ctx;
82 ctx = kunit_kzalloc(test, sizeof(*ctx), GFP_KERNEL);
83 KUNIT_ASSERT_NOT_ERR_OR_NULL(test, ctx);
86 ctx->try_catch = kunit_kmalloc(test,
87 sizeof(*ctx->try_catch),
89 KUNIT_ASSERT_NOT_ERR_OR_NULL(test, ctx->try_catch);
94 static struct kunit_case kunit_try_catch_test_cases[] = {
95 KUNIT_CASE(kunit_test_try_catch_successful_try_no_catch),
96 KUNIT_CASE(kunit_test_try_catch_unsuccessful_try_does_catch),
100 static struct kunit_suite kunit_try_catch_test_suite = {
101 .name = "kunit-try-catch-test",
102 .init = kunit_try_catch_test_init,
103 .test_cases = kunit_try_catch_test_cases,
107 * Context for testing test managed resources
108 * is_resource_initialized is used to test arbitrary resources
110 struct kunit_test_resource_context {
112 bool is_resource_initialized;
113 int allocate_order[2];
117 static int fake_resource_init(struct kunit_resource *res, void *context)
119 struct kunit_test_resource_context *ctx = context;
121 res->data = &ctx->is_resource_initialized;
122 ctx->is_resource_initialized = true;
126 static void fake_resource_free(struct kunit_resource *res)
128 bool *is_resource_initialized = res->data;
130 *is_resource_initialized = false;
133 static void kunit_resource_test_init_resources(struct kunit *test)
135 struct kunit_test_resource_context *ctx = test->priv;
137 kunit_init_test(&ctx->test, "testing_test_init_test", NULL);
139 KUNIT_EXPECT_TRUE(test, list_empty(&ctx->test.resources));
142 static void kunit_resource_test_alloc_resource(struct kunit *test)
144 struct kunit_test_resource_context *ctx = test->priv;
145 struct kunit_resource *res;
146 kunit_resource_free_t free = fake_resource_free;
148 res = kunit_alloc_and_get_resource(&ctx->test,
154 KUNIT_ASSERT_NOT_ERR_OR_NULL(test, res);
155 KUNIT_EXPECT_PTR_EQ(test,
156 &ctx->is_resource_initialized,
158 KUNIT_EXPECT_TRUE(test, list_is_last(&res->node, &ctx->test.resources));
159 KUNIT_EXPECT_PTR_EQ(test, free, res->free);
161 kunit_put_resource(res);
164 static inline bool kunit_resource_instance_match(struct kunit *test,
165 struct kunit_resource *res,
168 return res->data == match_data;
172 * Note: tests below use kunit_alloc_and_get_resource(), so as a consequence
173 * they have a reference to the associated resource that they must release
174 * via kunit_put_resource(). In normal operation, users will only
175 * have to do this for cases where they use kunit_find_resource(), and the
176 * kunit_alloc_resource() function will be used (which does not take a
177 * resource reference).
179 static void kunit_resource_test_destroy_resource(struct kunit *test)
181 struct kunit_test_resource_context *ctx = test->priv;
182 struct kunit_resource *res = kunit_alloc_and_get_resource(
189 kunit_put_resource(res);
191 KUNIT_ASSERT_FALSE(test,
192 kunit_destroy_resource(&ctx->test,
193 kunit_resource_instance_match,
196 KUNIT_EXPECT_FALSE(test, ctx->is_resource_initialized);
197 KUNIT_EXPECT_TRUE(test, list_empty(&ctx->test.resources));
200 static void kunit_resource_test_remove_resource(struct kunit *test)
202 struct kunit_test_resource_context *ctx = test->priv;
203 struct kunit_resource *res = kunit_alloc_and_get_resource(
210 /* The resource is in the list */
211 KUNIT_EXPECT_FALSE(test, list_empty(&ctx->test.resources));
213 /* Remove the resource. The pointer is still valid, but it can't be
216 kunit_remove_resource(test, res);
217 KUNIT_EXPECT_TRUE(test, list_empty(&ctx->test.resources));
218 /* We haven't been freed yet. */
219 KUNIT_EXPECT_TRUE(test, ctx->is_resource_initialized);
221 /* Removing the resource multiple times is valid. */
222 kunit_remove_resource(test, res);
223 KUNIT_EXPECT_TRUE(test, list_empty(&ctx->test.resources));
224 /* Despite having been removed twice (from only one reference), the
225 * resource still has not been freed.
227 KUNIT_EXPECT_TRUE(test, ctx->is_resource_initialized);
229 /* Free the resource. */
230 kunit_put_resource(res);
231 KUNIT_EXPECT_FALSE(test, ctx->is_resource_initialized);
234 static void kunit_resource_test_cleanup_resources(struct kunit *test)
237 struct kunit_test_resource_context *ctx = test->priv;
238 struct kunit_resource *resources[5];
240 for (i = 0; i < ARRAY_SIZE(resources); i++) {
241 resources[i] = kunit_alloc_and_get_resource(&ctx->test,
246 kunit_put_resource(resources[i]);
249 kunit_cleanup(&ctx->test);
251 KUNIT_EXPECT_TRUE(test, list_empty(&ctx->test.resources));
254 static void kunit_resource_test_mark_order(int order_array[],
260 for (i = 0; i < order_size && order_array[i]; i++)
263 order_array[i] = key;
266 #define KUNIT_RESOURCE_TEST_MARK_ORDER(ctx, order_field, key) \
267 kunit_resource_test_mark_order(ctx->order_field, \
268 ARRAY_SIZE(ctx->order_field), \
271 static int fake_resource_2_init(struct kunit_resource *res, void *context)
273 struct kunit_test_resource_context *ctx = context;
275 KUNIT_RESOURCE_TEST_MARK_ORDER(ctx, allocate_order, 2);
282 static void fake_resource_2_free(struct kunit_resource *res)
284 struct kunit_test_resource_context *ctx = res->data;
286 KUNIT_RESOURCE_TEST_MARK_ORDER(ctx, free_order, 2);
289 static int fake_resource_1_init(struct kunit_resource *res, void *context)
291 struct kunit_test_resource_context *ctx = context;
292 struct kunit_resource *res2;
294 res2 = kunit_alloc_and_get_resource(&ctx->test,
295 fake_resource_2_init,
296 fake_resource_2_free,
300 KUNIT_RESOURCE_TEST_MARK_ORDER(ctx, allocate_order, 1);
304 kunit_put_resource(res2);
309 static void fake_resource_1_free(struct kunit_resource *res)
311 struct kunit_test_resource_context *ctx = res->data;
313 KUNIT_RESOURCE_TEST_MARK_ORDER(ctx, free_order, 1);
317 * TODO(brendanhiggins@google.com): replace the arrays that keep track of the
318 * order of allocation and freeing with strict mocks using the IN_SEQUENCE macro
319 * to assert allocation and freeing order when the feature becomes available.
321 static void kunit_resource_test_proper_free_ordering(struct kunit *test)
323 struct kunit_test_resource_context *ctx = test->priv;
324 struct kunit_resource *res;
326 /* fake_resource_1 allocates a fake_resource_2 in its init. */
327 res = kunit_alloc_and_get_resource(&ctx->test,
328 fake_resource_1_init,
329 fake_resource_1_free,
334 * Since fake_resource_2_init calls KUNIT_RESOURCE_TEST_MARK_ORDER
335 * before returning to fake_resource_1_init, it should be the first to
336 * put its key in the allocate_order array.
338 KUNIT_EXPECT_EQ(test, ctx->allocate_order[0], 2);
339 KUNIT_EXPECT_EQ(test, ctx->allocate_order[1], 1);
341 kunit_put_resource(res);
343 kunit_cleanup(&ctx->test);
346 * Because fake_resource_2 finishes allocation before fake_resource_1,
347 * fake_resource_1 should be freed first since it could depend on
350 KUNIT_EXPECT_EQ(test, ctx->free_order[0], 1);
351 KUNIT_EXPECT_EQ(test, ctx->free_order[1], 2);
354 static void kunit_resource_test_static(struct kunit *test)
356 struct kunit_test_resource_context ctx;
357 struct kunit_resource res;
359 KUNIT_EXPECT_EQ(test, kunit_add_resource(test, NULL, NULL, &res, &ctx),
362 KUNIT_EXPECT_PTR_EQ(test, res.data, (void *)&ctx);
366 KUNIT_EXPECT_TRUE(test, list_empty(&test->resources));
369 static void kunit_resource_test_named(struct kunit *test)
371 struct kunit_resource res1, res2, *found = NULL;
372 struct kunit_test_resource_context ctx;
374 KUNIT_EXPECT_EQ(test,
375 kunit_add_named_resource(test, NULL, NULL, &res1,
378 KUNIT_EXPECT_PTR_EQ(test, res1.data, (void *)&ctx);
380 KUNIT_EXPECT_EQ(test,
381 kunit_add_named_resource(test, NULL, NULL, &res1,
385 KUNIT_EXPECT_EQ(test,
386 kunit_add_named_resource(test, NULL, NULL, &res2,
390 found = kunit_find_named_resource(test, "resource_1");
392 KUNIT_EXPECT_PTR_EQ(test, found, &res1);
395 kunit_put_resource(&res1);
397 KUNIT_EXPECT_EQ(test, kunit_destroy_named_resource(test, "resource_2"),
402 KUNIT_EXPECT_TRUE(test, list_empty(&test->resources));
405 static int kunit_resource_test_init(struct kunit *test)
407 struct kunit_test_resource_context *ctx =
408 kzalloc(sizeof(*ctx), GFP_KERNEL);
410 KUNIT_ASSERT_NOT_ERR_OR_NULL(test, ctx);
414 kunit_init_test(&ctx->test, "test_test_context", NULL);
419 static void kunit_resource_test_exit(struct kunit *test)
421 struct kunit_test_resource_context *ctx = test->priv;
423 kunit_cleanup(&ctx->test);
427 static struct kunit_case kunit_resource_test_cases[] = {
428 KUNIT_CASE(kunit_resource_test_init_resources),
429 KUNIT_CASE(kunit_resource_test_alloc_resource),
430 KUNIT_CASE(kunit_resource_test_destroy_resource),
431 KUNIT_CASE(kunit_resource_test_remove_resource),
432 KUNIT_CASE(kunit_resource_test_cleanup_resources),
433 KUNIT_CASE(kunit_resource_test_proper_free_ordering),
434 KUNIT_CASE(kunit_resource_test_static),
435 KUNIT_CASE(kunit_resource_test_named),
439 static struct kunit_suite kunit_resource_test_suite = {
440 .name = "kunit-resource-test",
441 .init = kunit_resource_test_init,
442 .exit = kunit_resource_test_exit,
443 .test_cases = kunit_resource_test_cases,
446 static void kunit_log_test(struct kunit *test);
448 static struct kunit_case kunit_log_test_cases[] = {
449 KUNIT_CASE(kunit_log_test),
453 static struct kunit_suite kunit_log_test_suite = {
454 .name = "kunit-log-test",
455 .test_cases = kunit_log_test_cases,
458 static void kunit_log_test(struct kunit *test)
460 struct kunit_suite suite;
462 suite.log = kunit_kzalloc(test, KUNIT_LOG_SIZE, GFP_KERNEL);
463 KUNIT_ASSERT_NOT_ERR_OR_NULL(test, suite.log);
465 kunit_log(KERN_INFO, test, "put this in log.");
466 kunit_log(KERN_INFO, test, "this too.");
467 kunit_log(KERN_INFO, &suite, "add to suite log.");
468 kunit_log(KERN_INFO, &suite, "along with this.");
470 #ifdef CONFIG_KUNIT_DEBUGFS
471 KUNIT_EXPECT_NOT_ERR_OR_NULL(test,
472 strstr(test->log, "put this in log."));
473 KUNIT_EXPECT_NOT_ERR_OR_NULL(test,
474 strstr(test->log, "this too."));
475 KUNIT_EXPECT_NOT_ERR_OR_NULL(test,
476 strstr(suite.log, "add to suite log."));
477 KUNIT_EXPECT_NOT_ERR_OR_NULL(test,
478 strstr(suite.log, "along with this."));
480 KUNIT_EXPECT_NULL(test, test->log);
484 static void kunit_status_set_failure_test(struct kunit *test)
488 kunit_init_test(&fake, "fake test", NULL);
490 KUNIT_EXPECT_EQ(test, fake.status, (enum kunit_status)KUNIT_SUCCESS);
491 kunit_set_failure(&fake);
492 KUNIT_EXPECT_EQ(test, fake.status, (enum kunit_status)KUNIT_FAILURE);
495 static void kunit_status_mark_skipped_test(struct kunit *test)
499 kunit_init_test(&fake, "fake test", NULL);
501 /* Before: Should be SUCCESS with no comment. */
502 KUNIT_EXPECT_EQ(test, fake.status, KUNIT_SUCCESS);
503 KUNIT_EXPECT_STREQ(test, fake.status_comment, "");
505 /* Mark the test as skipped. */
506 kunit_mark_skipped(&fake, "Accepts format string: %s", "YES");
508 /* After: Should be SKIPPED with our comment. */
509 KUNIT_EXPECT_EQ(test, fake.status, (enum kunit_status)KUNIT_SKIPPED);
510 KUNIT_EXPECT_STREQ(test, fake.status_comment, "Accepts format string: YES");
513 static struct kunit_case kunit_status_test_cases[] = {
514 KUNIT_CASE(kunit_status_set_failure_test),
515 KUNIT_CASE(kunit_status_mark_skipped_test),
519 static struct kunit_suite kunit_status_test_suite = {
520 .name = "kunit_status",
521 .test_cases = kunit_status_test_cases,
524 kunit_test_suites(&kunit_try_catch_test_suite, &kunit_resource_test_suite,
525 &kunit_log_test_suite, &kunit_status_test_suite);
527 MODULE_LICENSE("GPL v2");