Lines Matching refs:test

3  * Base unit test (KUnit) API.
10 #include <kunit/test.h>
11 #include <kunit/test-bug.h>
31 * Hook to fail the current test and print an error message to the log.
81 "Print test stats: never (0), only for multiple subtests (1), or always (2)");
101 static void kunit_print_test_stats(struct kunit *test,
107 kunit_log(KERN_INFO, test,
110 test->name,
143 /* Currently supported test levels */
153 * We do not log the test suite header as doing so would
154 * mean debugfs display would consist of the test suite
155 * header prior to individual test results.
168 static void kunit_print_ok_not_ok(struct kunit *test,
179 * When test is NULL assume that results are from the suite
182 WARN(!test && test_level, "suite test level can't be %u!\n", test_level);
185 * We do not log the test suite results as doing so would
186 * mean debugfs display would consist of an incorrect test
191 if (!test)
197 kunit_log(KERN_INFO, test,
251 static void kunit_print_string_stream(struct kunit *test,
262 kunit_err(test,
265 kunit_err(test, "%s", fragment->fragment);
267 kunit_err(test, "\n");
269 kunit_err(test, "%s", buf);
274 static void kunit_fail(struct kunit *test, const struct kunit_loc *loc,
280 kunit_set_failure(test);
282 stream = kunit_alloc_string_stream(test, GFP_KERNEL);
294 kunit_print_string_stream(test, stream);
296 kunit_free_string_stream(test, stream);
299 void __noreturn __kunit_abort(struct kunit *test)
301 kunit_try_catch_throw(&test->try_catch); /* Does not return. */
304 * Throw could not abort from test.
309 WARN_ONCE(true, "Throw could not abort from test!\n");
313 void __kunit_do_failed_assertion(struct kunit *test,
327 kunit_fail(test, loc, type, assert, assert_format, &message);
333 void kunit_init_test(struct kunit *test, const char *name, struct string_stream *log)
335 spin_lock_init(&test->lock);
336 INIT_LIST_HEAD(&test->resources);
337 test->name = name;
338 test->log = log;
339 if (test->log)
341 test->status = KUNIT_SUCCESS;
342 test->status_comment[0] = '\0';
346 /* Only warn when a test takes more than twice the threshold */
357 static void kunit_run_case_check_speed(struct kunit *test,
371 kunit_warn(test,
377 * Initializes and runs test case. Does not clean up or do post validations.
379 static void kunit_run_case_internal(struct kunit *test,
388 ret = suite->init(test);
390 kunit_err(test, "failed to initialize: %d\n", ret);
391 kunit_set_failure(test);
398 test_case->run_case(test);
402 kunit_run_case_check_speed(test, test_case, timespec64_sub(end, start));
405 static void kunit_case_internal_cleanup(struct kunit *test)
407 kunit_cleanup(test);
411 * Performs post validations and cleanup after a test case was run.
414 static void kunit_run_case_cleanup(struct kunit *test,
418 suite->exit(test);
420 kunit_case_internal_cleanup(test);
424 struct kunit *test;
432 struct kunit *test = ctx->test;
436 current->kunit_test = test;
443 kunit_run_case_internal(test, suite, test_case);
449 struct kunit *test = ctx->test;
452 current->kunit_test = test;
454 kunit_run_case_cleanup(test, suite);
460 struct kunit *test = ctx->test;
461 int try_exit_code = kunit_try_catch_get_result(&test->try_catch);
464 kunit_set_failure(test);
472 kunit_err(test, "test case cleanup timed out\n");
474 * Unknown internal error occurred preventing test case from
478 kunit_err(test, "internal error occurred during test case cleanup: %d\n",
484 kunit_err(test, "test aborted during cleanup. continuing without cleaning up\n");
491 struct kunit *test = ctx->test;
492 int try_exit_code = kunit_try_catch_get_result(&test->try_catch);
495 kunit_set_failure(test);
501 kunit_err(test, "test case timed out\n");
503 * Unknown internal error occurred preventing test case from
507 kunit_err(test, "internal error occurred preventing test case from running: %d\n",
515 * Performs all logic to run a test case. It also catches most errors that
516 * occur in a test case and reports them as failures.
520 struct kunit *test)
525 try_catch = &test->try_catch;
528 test,
531 context.test = test;
538 test,
543 /* Propagate the parameter result to the test case. */
544 if (test->status == KUNIT_FAILURE)
546 else if (test_case->status != KUNIT_FAILURE && test->status == KUNIT_SUCCESS)
623 struct kunit test = { .param_value = NULL, .param_index = 0 };
626 kunit_init_test(&test, test_case->name, test_case->log);
629 test.status = KUNIT_SKIPPED;
630 kunit_update_stats(&param_stats, test.status);
632 /* Non-parameterised test. */
634 kunit_run_case_catch_errors(suite, test_case, &test);
635 kunit_update_stats(&param_stats, test.status);
639 test.param_value = test_case->generate_params(NULL, param_desc);
641 kunit_log(KERN_INFO, &test, KUNIT_SUBTEST_INDENT KUNIT_SUBTEST_INDENT
643 kunit_log(KERN_INFO, &test, KUNIT_SUBTEST_INDENT KUNIT_SUBTEST_INDENT
646 while (test.param_value) {
647 kunit_run_case_catch_errors(suite, test_case, &test);
651 "param-%d", test.param_index);
654 kunit_print_ok_not_ok(&test, KUNIT_LEVEL_CASE_PARAM,
655 test.status,
656 test.param_index + 1,
658 test.status_comment);
660 kunit_update_stats(&param_stats, test.status);
664 test.param_value = test_case->generate_params(test.param_value, param_desc);
665 test.param_index++;
666 test.status = KUNIT_SUCCESS;
667 test.status_comment[0] = '\0';
668 test.priv = NULL;
674 kunit_print_test_stats(&test, param_stats);
676 kunit_print_ok_not_ok(&test, KUNIT_LEVEL_CASE, test_case->status,
679 test.status_comment);
727 pr_err("kunit: test interrupted\n");
852 void *kunit_kmalloc_array(struct kunit *test, size_t n, size_t size, gfp_t gfp)
861 if (kunit_add_action_or_reset(test, kfree_action_wrapper, data) != 0)
868 void kunit_kfree(struct kunit *test, const void *ptr)
873 kunit_release_action(test, kfree_action_wrapper, (void *)ptr);
877 void kunit_cleanup(struct kunit *test)
883 * test->resources is a stack - each allocation must be freed in the
892 spin_lock_irqsave(&test->lock, flags);
893 if (list_empty(&test->resources)) {
894 spin_unlock_irqrestore(&test->lock, flags);
897 res = list_last_entry(&test->resources,
902 * resource, and this can't happen if the test->lock
905 spin_unlock_irqrestore(&test->lock, flags);
906 kunit_remove_resource(test, res);