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 "linux/gfp_types.h"
9 #include <kunit/test.h>
10 #include <kunit/test-bug.h>
12 #include <linux/device.h>
13 #include <kunit/device.h>
15 #include "string-stream.h"
16 #include "try-catch-impl.h"
18 struct kunit_try_catch_test_context
{
19 struct kunit_try_catch
*try_catch
;
23 static void kunit_test_successful_try(void *data
)
25 struct kunit
*test
= data
;
26 struct kunit_try_catch_test_context
*ctx
= test
->priv
;
28 ctx
->function_called
= true;
31 static void kunit_test_no_catch(void *data
)
33 struct kunit
*test
= data
;
35 KUNIT_FAIL(test
, "Catch should not be called\n");
38 static void kunit_test_try_catch_successful_try_no_catch(struct kunit
*test
)
40 struct kunit_try_catch_test_context
*ctx
= test
->priv
;
41 struct kunit_try_catch
*try_catch
= ctx
->try_catch
;
43 kunit_try_catch_init(try_catch
,
45 kunit_test_successful_try
,
47 kunit_try_catch_run(try_catch
, test
);
49 KUNIT_EXPECT_TRUE(test
, ctx
->function_called
);
52 static void kunit_test_unsuccessful_try(void *data
)
54 struct kunit
*test
= data
;
55 struct kunit_try_catch_test_context
*ctx
= test
->priv
;
56 struct kunit_try_catch
*try_catch
= ctx
->try_catch
;
58 kunit_try_catch_throw(try_catch
);
59 KUNIT_FAIL(test
, "This line should never be reached\n");
62 static void kunit_test_catch(void *data
)
64 struct kunit
*test
= data
;
65 struct kunit_try_catch_test_context
*ctx
= test
->priv
;
67 ctx
->function_called
= true;
70 static void kunit_test_try_catch_unsuccessful_try_does_catch(struct kunit
*test
)
72 struct kunit_try_catch_test_context
*ctx
= test
->priv
;
73 struct kunit_try_catch
*try_catch
= ctx
->try_catch
;
75 kunit_try_catch_init(try_catch
,
77 kunit_test_unsuccessful_try
,
79 kunit_try_catch_run(try_catch
, test
);
81 KUNIT_EXPECT_TRUE(test
, ctx
->function_called
);
84 static int kunit_try_catch_test_init(struct kunit
*test
)
86 struct kunit_try_catch_test_context
*ctx
;
88 ctx
= kunit_kzalloc(test
, sizeof(*ctx
), GFP_KERNEL
);
89 KUNIT_ASSERT_NOT_ERR_OR_NULL(test
, ctx
);
92 ctx
->try_catch
= kunit_kmalloc(test
,
93 sizeof(*ctx
->try_catch
),
95 KUNIT_ASSERT_NOT_ERR_OR_NULL(test
, ctx
->try_catch
);
100 static struct kunit_case kunit_try_catch_test_cases
[] = {
101 KUNIT_CASE(kunit_test_try_catch_successful_try_no_catch
),
102 KUNIT_CASE(kunit_test_try_catch_unsuccessful_try_does_catch
),
106 static struct kunit_suite kunit_try_catch_test_suite
= {
107 .name
= "kunit-try-catch-test",
108 .init
= kunit_try_catch_test_init
,
109 .test_cases
= kunit_try_catch_test_cases
,
112 #if IS_ENABLED(CONFIG_KUNIT_FAULT_TEST)
114 static void kunit_test_null_dereference(void *data
)
116 struct kunit
*test
= data
;
121 KUNIT_FAIL(test
, "This line should never be reached\n");
124 static void kunit_test_fault_null_dereference(struct kunit
*test
)
126 struct kunit_try_catch_test_context
*ctx
= test
->priv
;
127 struct kunit_try_catch
*try_catch
= ctx
->try_catch
;
129 kunit_try_catch_init(try_catch
,
131 kunit_test_null_dereference
,
133 kunit_try_catch_run(try_catch
, test
);
135 KUNIT_EXPECT_EQ(test
, try_catch
->try_result
, -EINTR
);
136 KUNIT_EXPECT_TRUE(test
, ctx
->function_called
);
139 #endif /* CONFIG_KUNIT_FAULT_TEST */
141 static struct kunit_case kunit_fault_test_cases
[] = {
142 #if IS_ENABLED(CONFIG_KUNIT_FAULT_TEST)
143 KUNIT_CASE(kunit_test_fault_null_dereference
),
144 #endif /* CONFIG_KUNIT_FAULT_TEST */
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
,
155 * Context for testing test managed resources
156 * is_resource_initialized is used to test arbitrary resources
158 struct kunit_test_resource_context
{
160 bool is_resource_initialized
;
161 int allocate_order
[2];
165 static int fake_resource_init(struct kunit_resource
*res
, void *context
)
167 struct kunit_test_resource_context
*ctx
= context
;
169 res
->data
= &ctx
->is_resource_initialized
;
170 ctx
->is_resource_initialized
= true;
174 static void fake_resource_free(struct kunit_resource
*res
)
176 bool *is_resource_initialized
= res
->data
;
178 *is_resource_initialized
= false;
181 static void kunit_resource_test_init_resources(struct kunit
*test
)
183 struct kunit_test_resource_context
*ctx
= test
->priv
;
185 kunit_init_test(&ctx
->test
, "testing_test_init_test", NULL
);
187 KUNIT_EXPECT_TRUE(test
, list_empty(&ctx
->test
.resources
));
190 static void kunit_resource_test_alloc_resource(struct kunit
*test
)
192 struct kunit_test_resource_context
*ctx
= test
->priv
;
193 struct kunit_resource
*res
;
194 kunit_resource_free_t free
= fake_resource_free
;
196 res
= kunit_alloc_and_get_resource(&ctx
->test
,
202 KUNIT_ASSERT_NOT_ERR_OR_NULL(test
, res
);
203 KUNIT_EXPECT_PTR_EQ(test
,
204 &ctx
->is_resource_initialized
,
206 KUNIT_EXPECT_TRUE(test
, list_is_last(&res
->node
, &ctx
->test
.resources
));
207 KUNIT_EXPECT_PTR_EQ(test
, free
, res
->free
);
209 kunit_put_resource(res
);
212 static inline bool kunit_resource_instance_match(struct kunit
*test
,
213 struct kunit_resource
*res
,
216 return res
->data
== match_data
;
220 * Note: tests below use kunit_alloc_and_get_resource(), so as a consequence
221 * they have a reference to the associated resource that they must release
222 * via kunit_put_resource(). In normal operation, users will only
223 * have to do this for cases where they use kunit_find_resource(), and the
224 * kunit_alloc_resource() function will be used (which does not take a
225 * resource reference).
227 static void kunit_resource_test_destroy_resource(struct kunit
*test
)
229 struct kunit_test_resource_context
*ctx
= test
->priv
;
230 struct kunit_resource
*res
= kunit_alloc_and_get_resource(
237 kunit_put_resource(res
);
239 KUNIT_ASSERT_FALSE(test
,
240 kunit_destroy_resource(&ctx
->test
,
241 kunit_resource_instance_match
,
244 KUNIT_EXPECT_FALSE(test
, ctx
->is_resource_initialized
);
245 KUNIT_EXPECT_TRUE(test
, list_empty(&ctx
->test
.resources
));
248 static void kunit_resource_test_remove_resource(struct kunit
*test
)
250 struct kunit_test_resource_context
*ctx
= test
->priv
;
251 struct kunit_resource
*res
= kunit_alloc_and_get_resource(
258 /* The resource is in the list */
259 KUNIT_EXPECT_FALSE(test
, list_empty(&ctx
->test
.resources
));
261 /* Remove the resource. The pointer is still valid, but it can't be
264 kunit_remove_resource(test
, res
);
265 KUNIT_EXPECT_TRUE(test
, list_empty(&ctx
->test
.resources
));
266 /* We haven't been freed yet. */
267 KUNIT_EXPECT_TRUE(test
, ctx
->is_resource_initialized
);
269 /* Removing the resource multiple times is valid. */
270 kunit_remove_resource(test
, res
);
271 KUNIT_EXPECT_TRUE(test
, list_empty(&ctx
->test
.resources
));
272 /* Despite having been removed twice (from only one reference), the
273 * resource still has not been freed.
275 KUNIT_EXPECT_TRUE(test
, ctx
->is_resource_initialized
);
277 /* Free the resource. */
278 kunit_put_resource(res
);
279 KUNIT_EXPECT_FALSE(test
, ctx
->is_resource_initialized
);
282 static void kunit_resource_test_cleanup_resources(struct kunit
*test
)
285 struct kunit_test_resource_context
*ctx
= test
->priv
;
286 struct kunit_resource
*resources
[5];
288 for (i
= 0; i
< ARRAY_SIZE(resources
); i
++) {
289 resources
[i
] = kunit_alloc_and_get_resource(&ctx
->test
,
294 kunit_put_resource(resources
[i
]);
297 kunit_cleanup(&ctx
->test
);
299 KUNIT_EXPECT_TRUE(test
, list_empty(&ctx
->test
.resources
));
302 static void kunit_resource_test_mark_order(int order_array
[],
308 for (i
= 0; i
< order_size
&& order_array
[i
]; i
++)
311 order_array
[i
] = key
;
314 #define KUNIT_RESOURCE_TEST_MARK_ORDER(ctx, order_field, key) \
315 kunit_resource_test_mark_order(ctx->order_field, \
316 ARRAY_SIZE(ctx->order_field), \
319 static int fake_resource_2_init(struct kunit_resource
*res
, void *context
)
321 struct kunit_test_resource_context
*ctx
= context
;
323 KUNIT_RESOURCE_TEST_MARK_ORDER(ctx
, allocate_order
, 2);
330 static void fake_resource_2_free(struct kunit_resource
*res
)
332 struct kunit_test_resource_context
*ctx
= res
->data
;
334 KUNIT_RESOURCE_TEST_MARK_ORDER(ctx
, free_order
, 2);
337 static int fake_resource_1_init(struct kunit_resource
*res
, void *context
)
339 struct kunit_test_resource_context
*ctx
= context
;
340 struct kunit_resource
*res2
;
342 res2
= kunit_alloc_and_get_resource(&ctx
->test
,
343 fake_resource_2_init
,
344 fake_resource_2_free
,
348 KUNIT_RESOURCE_TEST_MARK_ORDER(ctx
, allocate_order
, 1);
352 kunit_put_resource(res2
);
357 static void fake_resource_1_free(struct kunit_resource
*res
)
359 struct kunit_test_resource_context
*ctx
= res
->data
;
361 KUNIT_RESOURCE_TEST_MARK_ORDER(ctx
, free_order
, 1);
365 * TODO(brendanhiggins@google.com): replace the arrays that keep track of the
366 * order of allocation and freeing with strict mocks using the IN_SEQUENCE macro
367 * to assert allocation and freeing order when the feature becomes available.
369 static void kunit_resource_test_proper_free_ordering(struct kunit
*test
)
371 struct kunit_test_resource_context
*ctx
= test
->priv
;
372 struct kunit_resource
*res
;
374 /* fake_resource_1 allocates a fake_resource_2 in its init. */
375 res
= kunit_alloc_and_get_resource(&ctx
->test
,
376 fake_resource_1_init
,
377 fake_resource_1_free
,
382 * Since fake_resource_2_init calls KUNIT_RESOURCE_TEST_MARK_ORDER
383 * before returning to fake_resource_1_init, it should be the first to
384 * put its key in the allocate_order array.
386 KUNIT_EXPECT_EQ(test
, ctx
->allocate_order
[0], 2);
387 KUNIT_EXPECT_EQ(test
, ctx
->allocate_order
[1], 1);
389 kunit_put_resource(res
);
391 kunit_cleanup(&ctx
->test
);
394 * Because fake_resource_2 finishes allocation before fake_resource_1,
395 * fake_resource_1 should be freed first since it could depend on
398 KUNIT_EXPECT_EQ(test
, ctx
->free_order
[0], 1);
399 KUNIT_EXPECT_EQ(test
, ctx
->free_order
[1], 2);
402 static void kunit_resource_test_static(struct kunit
*test
)
404 struct kunit_test_resource_context ctx
;
405 struct kunit_resource res
;
407 KUNIT_EXPECT_EQ(test
, kunit_add_resource(test
, NULL
, NULL
, &res
, &ctx
),
410 KUNIT_EXPECT_PTR_EQ(test
, res
.data
, (void *)&ctx
);
414 KUNIT_EXPECT_TRUE(test
, list_empty(&test
->resources
));
417 static void kunit_resource_test_named(struct kunit
*test
)
419 struct kunit_resource res1
, res2
, *found
= NULL
;
420 struct kunit_test_resource_context ctx
;
422 KUNIT_EXPECT_EQ(test
,
423 kunit_add_named_resource(test
, NULL
, NULL
, &res1
,
426 KUNIT_EXPECT_PTR_EQ(test
, res1
.data
, (void *)&ctx
);
428 KUNIT_EXPECT_EQ(test
,
429 kunit_add_named_resource(test
, NULL
, NULL
, &res1
,
433 KUNIT_EXPECT_EQ(test
,
434 kunit_add_named_resource(test
, NULL
, NULL
, &res2
,
438 found
= kunit_find_named_resource(test
, "resource_1");
440 KUNIT_EXPECT_PTR_EQ(test
, found
, &res1
);
443 kunit_put_resource(&res1
);
445 KUNIT_EXPECT_EQ(test
, kunit_destroy_named_resource(test
, "resource_2"),
450 KUNIT_EXPECT_TRUE(test
, list_empty(&test
->resources
));
453 static void increment_int(void *ctx
)
459 static void kunit_resource_test_action(struct kunit
*test
)
463 kunit_add_action(test
, increment_int
, &num_actions
);
464 KUNIT_EXPECT_EQ(test
, num_actions
, 0);
466 KUNIT_EXPECT_EQ(test
, num_actions
, 1);
468 /* Once we've cleaned up, the action queue is empty. */
470 KUNIT_EXPECT_EQ(test
, num_actions
, 1);
472 /* Check the same function can be deferred multiple times. */
473 kunit_add_action(test
, increment_int
, &num_actions
);
474 kunit_add_action(test
, increment_int
, &num_actions
);
476 KUNIT_EXPECT_EQ(test
, num_actions
, 3);
478 static void kunit_resource_test_remove_action(struct kunit
*test
)
482 kunit_add_action(test
, increment_int
, &num_actions
);
483 KUNIT_EXPECT_EQ(test
, num_actions
, 0);
485 kunit_remove_action(test
, increment_int
, &num_actions
);
487 KUNIT_EXPECT_EQ(test
, num_actions
, 0);
489 static void kunit_resource_test_release_action(struct kunit
*test
)
493 kunit_add_action(test
, increment_int
, &num_actions
);
494 KUNIT_EXPECT_EQ(test
, num_actions
, 0);
495 /* Runs immediately on trigger. */
496 kunit_release_action(test
, increment_int
, &num_actions
);
497 KUNIT_EXPECT_EQ(test
, num_actions
, 1);
499 /* Doesn't run again on test exit. */
501 KUNIT_EXPECT_EQ(test
, num_actions
, 1);
503 static void action_order_1(void *ctx
)
505 struct kunit_test_resource_context
*res_ctx
= (struct kunit_test_resource_context
*)ctx
;
507 KUNIT_RESOURCE_TEST_MARK_ORDER(res_ctx
, free_order
, 1);
508 kunit_log(KERN_INFO
, current
->kunit_test
, "action_order_1");
510 static void action_order_2(void *ctx
)
512 struct kunit_test_resource_context
*res_ctx
= (struct kunit_test_resource_context
*)ctx
;
514 KUNIT_RESOURCE_TEST_MARK_ORDER(res_ctx
, free_order
, 2);
515 kunit_log(KERN_INFO
, current
->kunit_test
, "action_order_2");
517 static void kunit_resource_test_action_ordering(struct kunit
*test
)
519 struct kunit_test_resource_context
*ctx
= test
->priv
;
521 kunit_add_action(test
, action_order_1
, ctx
);
522 kunit_add_action(test
, action_order_2
, ctx
);
523 kunit_add_action(test
, action_order_1
, ctx
);
524 kunit_add_action(test
, action_order_2
, ctx
);
525 kunit_remove_action(test
, action_order_1
, ctx
);
526 kunit_release_action(test
, action_order_2
, ctx
);
529 /* [2 is triggered] [2], [(1 is cancelled)] [1] */
530 KUNIT_EXPECT_EQ(test
, ctx
->free_order
[0], 2);
531 KUNIT_EXPECT_EQ(test
, ctx
->free_order
[1], 2);
532 KUNIT_EXPECT_EQ(test
, ctx
->free_order
[2], 1);
535 static int kunit_resource_test_init(struct kunit
*test
)
537 struct kunit_test_resource_context
*ctx
=
538 kzalloc(sizeof(*ctx
), GFP_KERNEL
);
540 KUNIT_ASSERT_NOT_ERR_OR_NULL(test
, ctx
);
544 kunit_init_test(&ctx
->test
, "test_test_context", NULL
);
549 static void kunit_resource_test_exit(struct kunit
*test
)
551 struct kunit_test_resource_context
*ctx
= test
->priv
;
553 kunit_cleanup(&ctx
->test
);
557 static struct kunit_case kunit_resource_test_cases
[] = {
558 KUNIT_CASE(kunit_resource_test_init_resources
),
559 KUNIT_CASE(kunit_resource_test_alloc_resource
),
560 KUNIT_CASE(kunit_resource_test_destroy_resource
),
561 KUNIT_CASE(kunit_resource_test_remove_resource
),
562 KUNIT_CASE(kunit_resource_test_cleanup_resources
),
563 KUNIT_CASE(kunit_resource_test_proper_free_ordering
),
564 KUNIT_CASE(kunit_resource_test_static
),
565 KUNIT_CASE(kunit_resource_test_named
),
566 KUNIT_CASE(kunit_resource_test_action
),
567 KUNIT_CASE(kunit_resource_test_remove_action
),
568 KUNIT_CASE(kunit_resource_test_release_action
),
569 KUNIT_CASE(kunit_resource_test_action_ordering
),
573 static struct kunit_suite kunit_resource_test_suite
= {
574 .name
= "kunit-resource-test",
575 .init
= kunit_resource_test_init
,
576 .exit
= kunit_resource_test_exit
,
577 .test_cases
= kunit_resource_test_cases
,
581 * Log tests call string_stream functions, which aren't exported. So only
582 * build this code if this test is built-in.
584 #if IS_BUILTIN(CONFIG_KUNIT_TEST)
586 /* This avoids a cast warning if kfree() is passed direct to kunit_add_action(). */
587 KUNIT_DEFINE_ACTION_WRAPPER(kfree_wrapper
, kfree
, const void *);
589 static void kunit_log_test(struct kunit
*test
)
591 struct kunit_suite suite
;
592 #ifdef CONFIG_KUNIT_DEBUGFS
595 suite
.log
= kunit_alloc_string_stream(test
, GFP_KERNEL
);
596 KUNIT_ASSERT_NOT_ERR_OR_NULL(test
, suite
.log
);
597 string_stream_set_append_newlines(suite
.log
, true);
599 kunit_log(KERN_INFO
, test
, "put this in log.");
600 kunit_log(KERN_INFO
, test
, "this too.");
601 kunit_log(KERN_INFO
, &suite
, "add to suite log.");
602 kunit_log(KERN_INFO
, &suite
, "along with this.");
604 #ifdef CONFIG_KUNIT_DEBUGFS
605 KUNIT_EXPECT_TRUE(test
, test
->log
->append_newlines
);
607 full_log
= string_stream_get_string(test
->log
);
608 kunit_add_action(test
, kfree_wrapper
, full_log
);
609 KUNIT_EXPECT_NOT_ERR_OR_NULL(test
,
610 strstr(full_log
, "put this in log."));
611 KUNIT_EXPECT_NOT_ERR_OR_NULL(test
,
612 strstr(full_log
, "this too."));
614 full_log
= string_stream_get_string(suite
.log
);
615 kunit_add_action(test
, kfree_wrapper
, full_log
);
616 KUNIT_EXPECT_NOT_ERR_OR_NULL(test
,
617 strstr(full_log
, "add to suite log."));
618 KUNIT_EXPECT_NOT_ERR_OR_NULL(test
,
619 strstr(full_log
, "along with this."));
621 KUNIT_EXPECT_NULL(test
, test
->log
);
625 static void kunit_log_newline_test(struct kunit
*test
)
629 kunit_info(test
, "Add newline\n");
631 full_log
= string_stream_get_string(test
->log
);
632 kunit_add_action(test
, kfree_wrapper
, full_log
);
633 KUNIT_ASSERT_NOT_NULL_MSG(test
, strstr(full_log
, "Add newline\n"),
634 "Missing log line, full log:\n%s", full_log
);
635 KUNIT_EXPECT_NULL(test
, strstr(full_log
, "Add newline\n\n"));
637 kunit_skip(test
, "only useful when debugfs is enabled");
641 static void kunit_log_test(struct kunit
*test
)
643 kunit_skip(test
, "Log tests only run when built-in");
646 static void kunit_log_newline_test(struct kunit
*test
)
648 kunit_skip(test
, "Log tests only run when built-in");
650 #endif /* IS_BUILTIN(CONFIG_KUNIT_TEST) */
652 static struct kunit_case kunit_log_test_cases
[] = {
653 KUNIT_CASE(kunit_log_test
),
654 KUNIT_CASE(kunit_log_newline_test
),
658 static struct kunit_suite kunit_log_test_suite
= {
659 .name
= "kunit-log-test",
660 .test_cases
= kunit_log_test_cases
,
663 static void kunit_status_set_failure_test(struct kunit
*test
)
667 kunit_init_test(&fake
, "fake test", NULL
);
669 KUNIT_EXPECT_EQ(test
, fake
.status
, (enum kunit_status
)KUNIT_SUCCESS
);
670 kunit_set_failure(&fake
);
671 KUNIT_EXPECT_EQ(test
, fake
.status
, (enum kunit_status
)KUNIT_FAILURE
);
674 static void kunit_status_mark_skipped_test(struct kunit
*test
)
678 kunit_init_test(&fake
, "fake test", NULL
);
680 /* Before: Should be SUCCESS with no comment. */
681 KUNIT_EXPECT_EQ(test
, fake
.status
, KUNIT_SUCCESS
);
682 KUNIT_EXPECT_STREQ(test
, fake
.status_comment
, "");
684 /* Mark the test as skipped. */
685 kunit_mark_skipped(&fake
, "Accepts format string: %s", "YES");
687 /* After: Should be SKIPPED with our comment. */
688 KUNIT_EXPECT_EQ(test
, fake
.status
, (enum kunit_status
)KUNIT_SKIPPED
);
689 KUNIT_EXPECT_STREQ(test
, fake
.status_comment
, "Accepts format string: YES");
692 static struct kunit_case kunit_status_test_cases
[] = {
693 KUNIT_CASE(kunit_status_set_failure_test
),
694 KUNIT_CASE(kunit_status_mark_skipped_test
),
698 static struct kunit_suite kunit_status_test_suite
= {
699 .name
= "kunit_status",
700 .test_cases
= kunit_status_test_cases
,
703 static void kunit_current_test(struct kunit
*test
)
705 /* Check results of both current->kunit_test and
706 * kunit_get_current_test() are equivalent to current test.
708 KUNIT_EXPECT_PTR_EQ(test
, test
, current
->kunit_test
);
709 KUNIT_EXPECT_PTR_EQ(test
, test
, kunit_get_current_test());
712 static void kunit_current_fail_test(struct kunit
*test
)
716 kunit_init_test(&fake
, "fake test", NULL
);
717 KUNIT_EXPECT_EQ(test
, fake
.status
, KUNIT_SUCCESS
);
719 /* Set current->kunit_test to fake test. */
720 current
->kunit_test
= &fake
;
722 kunit_fail_current_test("This should make `fake` test fail.");
723 KUNIT_EXPECT_EQ(test
, fake
.status
, (enum kunit_status
)KUNIT_FAILURE
);
724 kunit_cleanup(&fake
);
726 /* Reset current->kunit_test to current test. */
727 current
->kunit_test
= test
;
730 static struct kunit_case kunit_current_test_cases
[] = {
731 KUNIT_CASE(kunit_current_test
),
732 KUNIT_CASE(kunit_current_fail_test
),
736 static void test_dev_action(void *priv
)
738 *(void **)priv
= (void *)1;
741 static void kunit_device_test(struct kunit
*test
)
743 struct device
*test_device
;
744 long action_was_run
= 0;
746 test_device
= kunit_device_register(test
, "my_device");
747 KUNIT_ASSERT_NOT_ERR_OR_NULL(test
, test_device
);
749 // Add an action to verify cleanup.
750 devm_add_action(test_device
, test_dev_action
, &action_was_run
);
752 KUNIT_EXPECT_EQ(test
, action_was_run
, 0);
754 kunit_device_unregister(test
, test_device
);
756 KUNIT_EXPECT_EQ(test
, action_was_run
, 1);
759 static void kunit_device_cleanup_test(struct kunit
*test
)
761 struct device
*test_device
;
762 long action_was_run
= 0;
764 test_device
= kunit_device_register(test
, "my_device");
765 KUNIT_ASSERT_NOT_ERR_OR_NULL(test
, test_device
);
767 /* Add an action to verify cleanup. */
768 devm_add_action(test_device
, test_dev_action
, &action_was_run
);
770 KUNIT_EXPECT_EQ(test
, action_was_run
, 0);
772 /* Force KUnit to run cleanup early. */
775 KUNIT_EXPECT_EQ(test
, action_was_run
, 1);
778 struct driver_test_state
{
779 bool driver_device_probed
;
780 bool driver_device_removed
;
784 static int driver_probe_hook(struct device
*dev
)
786 struct kunit
*test
= kunit_get_current_test();
787 struct driver_test_state
*state
= (struct driver_test_state
*)test
->priv
;
789 state
->driver_device_probed
= true;
793 static int driver_remove_hook(struct device
*dev
)
795 struct kunit
*test
= kunit_get_current_test();
796 struct driver_test_state
*state
= (struct driver_test_state
*)test
->priv
;
798 state
->driver_device_removed
= true;
802 static void kunit_device_driver_test(struct kunit
*test
)
804 struct device_driver
*test_driver
;
805 struct device
*test_device
;
806 struct driver_test_state
*test_state
= kunit_kzalloc(test
, sizeof(*test_state
), GFP_KERNEL
);
808 KUNIT_ASSERT_NOT_ERR_OR_NULL(test
, test_state
);
810 test
->priv
= test_state
;
811 test_driver
= kunit_driver_create(test
, "my_driver");
813 // This can fail with an error pointer.
814 KUNIT_ASSERT_NOT_ERR_OR_NULL(test
, test_driver
);
816 test_driver
->probe
= driver_probe_hook
;
817 test_driver
->remove
= driver_remove_hook
;
819 test_device
= kunit_device_register_with_driver(test
, "my_device", test_driver
);
821 // This can fail with an error pointer.
822 KUNIT_ASSERT_NOT_ERR_OR_NULL(test
, test_device
);
824 // Make sure the probe function was called.
825 KUNIT_ASSERT_TRUE(test
, test_state
->driver_device_probed
);
827 // Add an action to verify cleanup.
828 devm_add_action(test_device
, test_dev_action
, &test_state
->action_was_run
);
830 KUNIT_EXPECT_EQ(test
, test_state
->action_was_run
, 0);
832 kunit_device_unregister(test
, test_device
);
835 // Make sure the remove hook was called.
836 KUNIT_ASSERT_TRUE(test
, test_state
->driver_device_removed
);
838 // We're going to test this again.
839 test_state
->driver_device_probed
= false;
841 // The driver should not automatically be destroyed by
842 // kunit_device_unregister, so we can re-use it.
843 test_device
= kunit_device_register_with_driver(test
, "my_device", test_driver
);
845 // This can fail with an error pointer.
846 KUNIT_ASSERT_NOT_ERR_OR_NULL(test
, test_device
);
848 // Probe was called again.
849 KUNIT_ASSERT_TRUE(test
, test_state
->driver_device_probed
);
851 // Everything is automatically freed here.
854 static struct kunit_case kunit_device_test_cases
[] = {
855 KUNIT_CASE(kunit_device_test
),
856 KUNIT_CASE(kunit_device_cleanup_test
),
857 KUNIT_CASE(kunit_device_driver_test
),
861 static struct kunit_suite kunit_device_test_suite
= {
862 .name
= "kunit_device",
863 .test_cases
= kunit_device_test_cases
,
866 static struct kunit_suite kunit_current_test_suite
= {
867 .name
= "kunit_current",
868 .test_cases
= kunit_current_test_cases
,
871 kunit_test_suites(&kunit_try_catch_test_suite
, &kunit_resource_test_suite
,
872 &kunit_log_test_suite
, &kunit_status_test_suite
,
873 &kunit_current_test_suite
, &kunit_device_test_suite
,
874 &kunit_fault_test_suite
);
876 MODULE_DESCRIPTION("KUnit test for core test infrastructure");
877 MODULE_LICENSE("GPL v2");