Lines Matching full:test

3  * KUnit test for core test infrastructure.
9 #include <kunit/test.h>
10 #include <kunit/test-bug.h>
25 struct kunit *test = data; in kunit_test_successful_try() local
26 struct kunit_try_catch_test_context *ctx = test->priv; in kunit_test_successful_try()
33 struct kunit *test = data; in kunit_test_no_catch() local
35 KUNIT_FAIL(test, "Catch should not be called\n"); in kunit_test_no_catch()
38 static void kunit_test_try_catch_successful_try_no_catch(struct kunit *test) in kunit_test_try_catch_successful_try_no_catch() argument
40 struct kunit_try_catch_test_context *ctx = test->priv; in kunit_test_try_catch_successful_try_no_catch()
44 test, in kunit_test_try_catch_successful_try_no_catch()
47 kunit_try_catch_run(try_catch, test); in kunit_test_try_catch_successful_try_no_catch()
49 KUNIT_EXPECT_TRUE(test, ctx->function_called); in kunit_test_try_catch_successful_try_no_catch()
54 struct kunit *test = data; in kunit_test_unsuccessful_try() local
55 struct kunit_try_catch_test_context *ctx = test->priv; in kunit_test_unsuccessful_try()
59 KUNIT_FAIL(test, "This line should never be reached\n"); in kunit_test_unsuccessful_try()
64 struct kunit *test = data; in kunit_test_catch() local
65 struct kunit_try_catch_test_context *ctx = test->priv; in kunit_test_catch()
70 static void kunit_test_try_catch_unsuccessful_try_does_catch(struct kunit *test) in kunit_test_try_catch_unsuccessful_try_does_catch() argument
72 struct kunit_try_catch_test_context *ctx = test->priv; in kunit_test_try_catch_unsuccessful_try_does_catch()
76 test, in kunit_test_try_catch_unsuccessful_try_does_catch()
79 kunit_try_catch_run(try_catch, test); in kunit_test_try_catch_unsuccessful_try_does_catch()
81 KUNIT_EXPECT_TRUE(test, ctx->function_called); in kunit_test_try_catch_unsuccessful_try_does_catch()
84 static int kunit_try_catch_test_init(struct kunit *test) in kunit_try_catch_test_init() argument
88 ctx = kunit_kzalloc(test, sizeof(*ctx), GFP_KERNEL); in kunit_try_catch_test_init()
89 KUNIT_ASSERT_NOT_ERR_OR_NULL(test, ctx); in kunit_try_catch_test_init()
90 test->priv = ctx; in kunit_try_catch_test_init()
92 ctx->try_catch = kunit_kmalloc(test, in kunit_try_catch_test_init()
95 KUNIT_ASSERT_NOT_ERR_OR_NULL(test, ctx->try_catch); in kunit_try_catch_test_init()
107 .name = "kunit-try-catch-test",
116 struct kunit *test = data; in kunit_test_null_dereference() local
121 KUNIT_FAIL(test, "This line should never be reached\n"); in kunit_test_null_dereference()
124 static void kunit_test_fault_null_dereference(struct kunit *test) in kunit_test_fault_null_dereference() argument
126 struct kunit_try_catch_test_context *ctx = test->priv; in kunit_test_fault_null_dereference()
130 test, in kunit_test_fault_null_dereference()
133 kunit_try_catch_run(try_catch, test); in kunit_test_fault_null_dereference()
135 KUNIT_EXPECT_EQ(test, try_catch->try_result, -EINTR); in kunit_test_fault_null_dereference()
136 KUNIT_EXPECT_TRUE(test, ctx->function_called); in kunit_test_fault_null_dereference()
155 * Context for testing test managed resources
156 * is_resource_initialized is used to test arbitrary resources
159 struct kunit test; member
181 static void kunit_resource_test_init_resources(struct kunit *test) in kunit_resource_test_init_resources() argument
183 struct kunit_test_resource_context *ctx = test->priv; in kunit_resource_test_init_resources()
185 kunit_init_test(&ctx->test, "testing_test_init_test", NULL); in kunit_resource_test_init_resources()
187 KUNIT_EXPECT_TRUE(test, list_empty(&ctx->test.resources)); in kunit_resource_test_init_resources()
190 static void kunit_resource_test_alloc_resource(struct kunit *test) in kunit_resource_test_alloc_resource() argument
192 struct kunit_test_resource_context *ctx = test->priv; in kunit_resource_test_alloc_resource()
196 res = kunit_alloc_and_get_resource(&ctx->test, in kunit_resource_test_alloc_resource()
202 KUNIT_ASSERT_NOT_ERR_OR_NULL(test, res); in kunit_resource_test_alloc_resource()
203 KUNIT_EXPECT_PTR_EQ(test, in kunit_resource_test_alloc_resource()
206 KUNIT_EXPECT_TRUE(test, list_is_last(&res->node, &ctx->test.resources)); in kunit_resource_test_alloc_resource()
207 KUNIT_EXPECT_PTR_EQ(test, free, res->free); in kunit_resource_test_alloc_resource()
212 static inline bool kunit_resource_instance_match(struct kunit *test, in kunit_resource_instance_match() argument
227 static void kunit_resource_test_destroy_resource(struct kunit *test) in kunit_resource_test_destroy_resource() argument
229 struct kunit_test_resource_context *ctx = test->priv; in kunit_resource_test_destroy_resource()
231 &ctx->test, in kunit_resource_test_destroy_resource()
239 KUNIT_ASSERT_FALSE(test, in kunit_resource_test_destroy_resource()
240 kunit_destroy_resource(&ctx->test, in kunit_resource_test_destroy_resource()
244 KUNIT_EXPECT_FALSE(test, ctx->is_resource_initialized); in kunit_resource_test_destroy_resource()
245 KUNIT_EXPECT_TRUE(test, list_empty(&ctx->test.resources)); in kunit_resource_test_destroy_resource()
248 static void kunit_resource_test_remove_resource(struct kunit *test) in kunit_resource_test_remove_resource() argument
250 struct kunit_test_resource_context *ctx = test->priv; in kunit_resource_test_remove_resource()
252 &ctx->test, in kunit_resource_test_remove_resource()
259 KUNIT_EXPECT_FALSE(test, list_empty(&ctx->test.resources)); in kunit_resource_test_remove_resource()
264 kunit_remove_resource(test, res); in kunit_resource_test_remove_resource()
265 KUNIT_EXPECT_TRUE(test, list_empty(&ctx->test.resources)); in kunit_resource_test_remove_resource()
267 KUNIT_EXPECT_TRUE(test, ctx->is_resource_initialized); in kunit_resource_test_remove_resource()
270 kunit_remove_resource(test, res); in kunit_resource_test_remove_resource()
271 KUNIT_EXPECT_TRUE(test, list_empty(&ctx->test.resources)); in kunit_resource_test_remove_resource()
275 KUNIT_EXPECT_TRUE(test, ctx->is_resource_initialized); in kunit_resource_test_remove_resource()
279 KUNIT_EXPECT_FALSE(test, ctx->is_resource_initialized); in kunit_resource_test_remove_resource()
282 static void kunit_resource_test_cleanup_resources(struct kunit *test) in kunit_resource_test_cleanup_resources() argument
285 struct kunit_test_resource_context *ctx = test->priv; in kunit_resource_test_cleanup_resources()
289 resources[i] = kunit_alloc_and_get_resource(&ctx->test, in kunit_resource_test_cleanup_resources()
297 kunit_cleanup(&ctx->test); in kunit_resource_test_cleanup_resources()
299 KUNIT_EXPECT_TRUE(test, list_empty(&ctx->test.resources)); in kunit_resource_test_cleanup_resources()
342 res2 = kunit_alloc_and_get_resource(&ctx->test, in fake_resource_1_init()
369 static void kunit_resource_test_proper_free_ordering(struct kunit *test) in kunit_resource_test_proper_free_ordering() argument
371 struct kunit_test_resource_context *ctx = test->priv; in kunit_resource_test_proper_free_ordering()
375 res = kunit_alloc_and_get_resource(&ctx->test, in kunit_resource_test_proper_free_ordering()
386 KUNIT_EXPECT_EQ(test, ctx->allocate_order[0], 2); in kunit_resource_test_proper_free_ordering()
387 KUNIT_EXPECT_EQ(test, ctx->allocate_order[1], 1); in kunit_resource_test_proper_free_ordering()
391 kunit_cleanup(&ctx->test); in kunit_resource_test_proper_free_ordering()
398 KUNIT_EXPECT_EQ(test, ctx->free_order[0], 1); in kunit_resource_test_proper_free_ordering()
399 KUNIT_EXPECT_EQ(test, ctx->free_order[1], 2); in kunit_resource_test_proper_free_ordering()
402 static void kunit_resource_test_static(struct kunit *test) in kunit_resource_test_static() argument
407 KUNIT_EXPECT_EQ(test, kunit_add_resource(test, NULL, NULL, &res, &ctx), in kunit_resource_test_static()
410 KUNIT_EXPECT_PTR_EQ(test, res.data, (void *)&ctx); in kunit_resource_test_static()
412 kunit_cleanup(test); in kunit_resource_test_static()
414 KUNIT_EXPECT_TRUE(test, list_empty(&test->resources)); in kunit_resource_test_static()
417 static void kunit_resource_test_named(struct kunit *test) in kunit_resource_test_named() argument
422 KUNIT_EXPECT_EQ(test, in kunit_resource_test_named()
423 kunit_add_named_resource(test, NULL, NULL, &res1, in kunit_resource_test_named()
426 KUNIT_EXPECT_PTR_EQ(test, res1.data, (void *)&ctx); in kunit_resource_test_named()
428 KUNIT_EXPECT_EQ(test, in kunit_resource_test_named()
429 kunit_add_named_resource(test, NULL, NULL, &res1, in kunit_resource_test_named()
433 KUNIT_EXPECT_EQ(test, in kunit_resource_test_named()
434 kunit_add_named_resource(test, NULL, NULL, &res2, in kunit_resource_test_named()
438 found = kunit_find_named_resource(test, "resource_1"); in kunit_resource_test_named()
440 KUNIT_EXPECT_PTR_EQ(test, found, &res1); in kunit_resource_test_named()
445 KUNIT_EXPECT_EQ(test, kunit_destroy_named_resource(test, "resource_2"), in kunit_resource_test_named()
448 kunit_cleanup(test); in kunit_resource_test_named()
450 KUNIT_EXPECT_TRUE(test, list_empty(&test->resources)); in kunit_resource_test_named()
459 static void kunit_resource_test_action(struct kunit *test) in kunit_resource_test_action() argument
463 kunit_add_action(test, increment_int, &num_actions); in kunit_resource_test_action()
464 KUNIT_EXPECT_EQ(test, num_actions, 0); in kunit_resource_test_action()
465 kunit_cleanup(test); in kunit_resource_test_action()
466 KUNIT_EXPECT_EQ(test, num_actions, 1); in kunit_resource_test_action()
469 kunit_cleanup(test); in kunit_resource_test_action()
470 KUNIT_EXPECT_EQ(test, num_actions, 1); in kunit_resource_test_action()
473 kunit_add_action(test, increment_int, &num_actions); in kunit_resource_test_action()
474 kunit_add_action(test, increment_int, &num_actions); in kunit_resource_test_action()
475 kunit_cleanup(test); in kunit_resource_test_action()
476 KUNIT_EXPECT_EQ(test, num_actions, 3); in kunit_resource_test_action()
478 static void kunit_resource_test_remove_action(struct kunit *test) in kunit_resource_test_remove_action() argument
482 kunit_add_action(test, increment_int, &num_actions); in kunit_resource_test_remove_action()
483 KUNIT_EXPECT_EQ(test, num_actions, 0); in kunit_resource_test_remove_action()
485 kunit_remove_action(test, increment_int, &num_actions); in kunit_resource_test_remove_action()
486 kunit_cleanup(test); in kunit_resource_test_remove_action()
487 KUNIT_EXPECT_EQ(test, num_actions, 0); in kunit_resource_test_remove_action()
489 static void kunit_resource_test_release_action(struct kunit *test) in kunit_resource_test_release_action() argument
493 kunit_add_action(test, increment_int, &num_actions); in kunit_resource_test_release_action()
494 KUNIT_EXPECT_EQ(test, num_actions, 0); in kunit_resource_test_release_action()
496 kunit_release_action(test, increment_int, &num_actions); in kunit_resource_test_release_action()
497 KUNIT_EXPECT_EQ(test, num_actions, 1); in kunit_resource_test_release_action()
499 /* Doesn't run again on test exit. */ in kunit_resource_test_release_action()
500 kunit_cleanup(test); in kunit_resource_test_release_action()
501 KUNIT_EXPECT_EQ(test, num_actions, 1); in kunit_resource_test_release_action()
517 static void kunit_resource_test_action_ordering(struct kunit *test) in kunit_resource_test_action_ordering() argument
519 struct kunit_test_resource_context *ctx = test->priv; in kunit_resource_test_action_ordering()
521 kunit_add_action(test, action_order_1, ctx); in kunit_resource_test_action_ordering()
522 kunit_add_action(test, action_order_2, ctx); in kunit_resource_test_action_ordering()
523 kunit_add_action(test, action_order_1, ctx); in kunit_resource_test_action_ordering()
524 kunit_add_action(test, action_order_2, ctx); in kunit_resource_test_action_ordering()
525 kunit_remove_action(test, action_order_1, ctx); in kunit_resource_test_action_ordering()
526 kunit_release_action(test, action_order_2, ctx); in kunit_resource_test_action_ordering()
527 kunit_cleanup(test); in kunit_resource_test_action_ordering()
530 KUNIT_EXPECT_EQ(test, ctx->free_order[0], 2); in kunit_resource_test_action_ordering()
531 KUNIT_EXPECT_EQ(test, ctx->free_order[1], 2); in kunit_resource_test_action_ordering()
532 KUNIT_EXPECT_EQ(test, ctx->free_order[2], 1); in kunit_resource_test_action_ordering()
535 static int kunit_resource_test_init(struct kunit *test) in kunit_resource_test_init() argument
540 KUNIT_ASSERT_NOT_ERR_OR_NULL(test, ctx); in kunit_resource_test_init()
542 test->priv = ctx; in kunit_resource_test_init()
544 kunit_init_test(&ctx->test, "test_test_context", NULL); in kunit_resource_test_init()
549 static void kunit_resource_test_exit(struct kunit *test) in kunit_resource_test_exit() argument
551 struct kunit_test_resource_context *ctx = test->priv; in kunit_resource_test_exit()
553 kunit_cleanup(&ctx->test); in kunit_resource_test_exit()
574 .name = "kunit-resource-test",
582 * build this code if this test is built-in.
589 static void kunit_log_test(struct kunit *test) in kunit_log_test() argument
595 suite.log = kunit_alloc_string_stream(test, GFP_KERNEL); in kunit_log_test()
596 KUNIT_ASSERT_NOT_ERR_OR_NULL(test, suite.log); in kunit_log_test()
599 kunit_log(KERN_INFO, test, "put this in log."); in kunit_log_test()
600 kunit_log(KERN_INFO, test, "this too."); in kunit_log_test()
605 KUNIT_EXPECT_TRUE(test, test->log->append_newlines); in kunit_log_test()
607 full_log = string_stream_get_string(test->log); in kunit_log_test()
608 kunit_add_action(test, kfree_wrapper, full_log); in kunit_log_test()
609 KUNIT_EXPECT_NOT_ERR_OR_NULL(test, in kunit_log_test()
611 KUNIT_EXPECT_NOT_ERR_OR_NULL(test, in kunit_log_test()
615 kunit_add_action(test, kfree_wrapper, full_log); in kunit_log_test()
616 KUNIT_EXPECT_NOT_ERR_OR_NULL(test, in kunit_log_test()
618 KUNIT_EXPECT_NOT_ERR_OR_NULL(test, in kunit_log_test()
621 KUNIT_EXPECT_NULL(test, test->log); in kunit_log_test()
625 static void kunit_log_newline_test(struct kunit *test) in kunit_log_newline_test() argument
629 kunit_info(test, "Add newline\n"); in kunit_log_newline_test()
630 if (test->log) { in kunit_log_newline_test()
631 full_log = string_stream_get_string(test->log); in kunit_log_newline_test()
632 kunit_add_action(test, kfree_wrapper, full_log); in kunit_log_newline_test()
633 KUNIT_ASSERT_NOT_NULL_MSG(test, strstr(full_log, "Add newline\n"), in kunit_log_newline_test()
635 KUNIT_EXPECT_NULL(test, strstr(full_log, "Add newline\n\n")); in kunit_log_newline_test()
637 kunit_skip(test, "only useful when debugfs is enabled"); in kunit_log_newline_test()
641 static void kunit_log_test(struct kunit *test) in kunit_log_test() argument
643 kunit_skip(test, "Log tests only run when built-in"); in kunit_log_test()
646 static void kunit_log_newline_test(struct kunit *test) in kunit_log_newline_test() argument
648 kunit_skip(test, "Log tests only run when built-in"); in kunit_log_newline_test()
659 .name = "kunit-log-test",
663 static void kunit_status_set_failure_test(struct kunit *test) in kunit_status_set_failure_test() argument
667 kunit_init_test(&fake, "fake test", NULL); in kunit_status_set_failure_test()
669 KUNIT_EXPECT_EQ(test, fake.status, (enum kunit_status)KUNIT_SUCCESS); in kunit_status_set_failure_test()
671 KUNIT_EXPECT_EQ(test, fake.status, (enum kunit_status)KUNIT_FAILURE); in kunit_status_set_failure_test()
674 static void kunit_status_mark_skipped_test(struct kunit *test) in kunit_status_mark_skipped_test() argument
678 kunit_init_test(&fake, "fake test", NULL); in kunit_status_mark_skipped_test()
681 KUNIT_EXPECT_EQ(test, fake.status, KUNIT_SUCCESS); in kunit_status_mark_skipped_test()
682 KUNIT_EXPECT_STREQ(test, fake.status_comment, ""); in kunit_status_mark_skipped_test()
684 /* Mark the test as skipped. */ in kunit_status_mark_skipped_test()
688 KUNIT_EXPECT_EQ(test, fake.status, (enum kunit_status)KUNIT_SKIPPED); in kunit_status_mark_skipped_test()
689 KUNIT_EXPECT_STREQ(test, fake.status_comment, "Accepts format string: YES"); in kunit_status_mark_skipped_test()
703 static void kunit_current_test(struct kunit *test) in kunit_current_test() argument
706 * kunit_get_current_test() are equivalent to current test. in kunit_current_test()
708 KUNIT_EXPECT_PTR_EQ(test, test, current->kunit_test); in kunit_current_test()
709 KUNIT_EXPECT_PTR_EQ(test, test, kunit_get_current_test()); in kunit_current_test()
712 static void kunit_current_fail_test(struct kunit *test) in kunit_current_fail_test() argument
716 kunit_init_test(&fake, "fake test", NULL); in kunit_current_fail_test()
717 KUNIT_EXPECT_EQ(test, fake.status, KUNIT_SUCCESS); in kunit_current_fail_test()
719 /* Set current->kunit_test to fake test. */ in kunit_current_fail_test()
722 kunit_fail_current_test("This should make `fake` test fail."); in kunit_current_fail_test()
723 KUNIT_EXPECT_EQ(test, fake.status, (enum kunit_status)KUNIT_FAILURE); in kunit_current_fail_test()
726 /* Reset current->kunit_test to current test. */ in kunit_current_fail_test()
727 current->kunit_test = test; in kunit_current_fail_test()
741 static void kunit_device_test(struct kunit *test) in kunit_device_test() argument
746 test_device = kunit_device_register(test, "my_device"); in kunit_device_test()
747 KUNIT_ASSERT_NOT_ERR_OR_NULL(test, test_device); in kunit_device_test()
752 KUNIT_EXPECT_EQ(test, action_was_run, 0); in kunit_device_test()
754 kunit_device_unregister(test, test_device); in kunit_device_test()
756 KUNIT_EXPECT_EQ(test, action_was_run, 1); in kunit_device_test()
759 static void kunit_device_cleanup_test(struct kunit *test) in kunit_device_cleanup_test() argument
764 test_device = kunit_device_register(test, "my_device"); in kunit_device_cleanup_test()
765 KUNIT_ASSERT_NOT_ERR_OR_NULL(test, test_device); in kunit_device_cleanup_test()
770 KUNIT_EXPECT_EQ(test, action_was_run, 0); in kunit_device_cleanup_test()
773 kunit_cleanup(test); in kunit_device_cleanup_test()
775 KUNIT_EXPECT_EQ(test, action_was_run, 1); in kunit_device_cleanup_test()
786 struct kunit *test = kunit_get_current_test(); in driver_probe_hook() local
787 struct driver_test_state *state = (struct driver_test_state *)test->priv; in driver_probe_hook()
795 struct kunit *test = kunit_get_current_test(); in driver_remove_hook() local
796 struct driver_test_state *state = (struct driver_test_state *)test->priv; in driver_remove_hook()
802 static void kunit_device_driver_test(struct kunit *test) in kunit_device_driver_test() argument
806 struct driver_test_state *test_state = kunit_kzalloc(test, sizeof(*test_state), GFP_KERNEL); in kunit_device_driver_test()
808 test->priv = test_state; in kunit_device_driver_test()
809 test_driver = kunit_driver_create(test, "my_driver"); in kunit_device_driver_test()
812 KUNIT_ASSERT_NOT_ERR_OR_NULL(test, test_driver); in kunit_device_driver_test()
817 test_device = kunit_device_register_with_driver(test, "my_device", test_driver); in kunit_device_driver_test()
820 KUNIT_ASSERT_NOT_ERR_OR_NULL(test, test_device); in kunit_device_driver_test()
823 KUNIT_ASSERT_TRUE(test, test_state->driver_device_probed); in kunit_device_driver_test()
828 KUNIT_EXPECT_EQ(test, test_state->action_was_run, 0); in kunit_device_driver_test()
830 kunit_device_unregister(test, test_device); in kunit_device_driver_test()
834 KUNIT_ASSERT_TRUE(test, test_state->driver_device_removed); in kunit_device_driver_test()
836 // We're going to test this again. in kunit_device_driver_test()
841 test_device = kunit_device_register_with_driver(test, "my_device", test_driver); in kunit_device_driver_test()
844 KUNIT_ASSERT_NOT_ERR_OR_NULL(test, test_device); in kunit_device_driver_test()
847 KUNIT_ASSERT_TRUE(test, test_state->driver_device_probed); in kunit_device_driver_test()
874 MODULE_DESCRIPTION("KUnit test for core test infrastructure");