Simon Glass | 1c72175 | 2021-03-07 17:34:47 -0700 | [diff] [blame] | 1 | // SPDX-License-Identifier: GPL-2.0+ |
| 2 | /* |
| 3 | * Copyright 2021 Google LLC |
| 4 | * Written by Simon Glass <sjg@chromium.org> |
| 5 | */ |
| 6 | |
| 7 | #include <common.h> |
| 8 | #include <console.h> |
Simon Glass | d8ed234 | 2021-03-07 17:34:50 -0700 | [diff] [blame] | 9 | #include <dm.h> |
| 10 | #include <dm/root.h> |
Simon Glass | 1c72175 | 2021-03-07 17:34:47 -0700 | [diff] [blame] | 11 | #include <test/test.h> |
Simon Glass | d8ed234 | 2021-03-07 17:34:50 -0700 | [diff] [blame] | 12 | #include <test/ut.h> |
Simon Glass | 1c72175 | 2021-03-07 17:34:47 -0700 | [diff] [blame] | 13 | |
Simon Glass | 30a0d20 | 2021-03-07 17:34:49 -0700 | [diff] [blame] | 14 | DECLARE_GLOBAL_DATA_PTR; |
| 15 | |
Simon Glass | 4b8b27e | 2021-03-07 17:34:51 -0700 | [diff] [blame^] | 16 | /* Ensure all the test devices are probed */ |
| 17 | static int do_autoprobe(struct unit_test_state *uts) |
| 18 | { |
| 19 | struct udevice *dev; |
| 20 | int ret; |
| 21 | |
| 22 | /* Scanning the uclass is enough to probe all the devices */ |
| 23 | for (ret = uclass_first_device(UCLASS_TEST, &dev); |
| 24 | dev; |
| 25 | ret = uclass_next_device(&dev)) |
| 26 | ; |
| 27 | |
| 28 | return ret; |
| 29 | } |
| 30 | |
Simon Glass | d002a27 | 2021-03-07 17:34:48 -0700 | [diff] [blame] | 31 | int test_pre_run(struct unit_test_state *uts, struct unit_test *test) |
| 32 | { |
Simon Glass | 30a0d20 | 2021-03-07 17:34:49 -0700 | [diff] [blame] | 33 | /* DM tests have already done this */ |
| 34 | if (!(test->flags & UT_TESTF_DM)) |
| 35 | uts->start = mallinfo(); |
Simon Glass | d002a27 | 2021-03-07 17:34:48 -0700 | [diff] [blame] | 36 | |
Simon Glass | 4b8b27e | 2021-03-07 17:34:51 -0700 | [diff] [blame^] | 37 | if (test->flags & UT_TESTF_PROBE_TEST) |
| 38 | ut_assertok(do_autoprobe(uts)); |
| 39 | |
Simon Glass | d8ed234 | 2021-03-07 17:34:50 -0700 | [diff] [blame] | 40 | if (!CONFIG_IS_ENABLED(OF_PLATDATA) && |
| 41 | (test->flags & UT_TESTF_SCAN_FDT)) |
| 42 | ut_assertok(dm_extended_scan(false)); |
| 43 | |
Simon Glass | d002a27 | 2021-03-07 17:34:48 -0700 | [diff] [blame] | 44 | if (test->flags & UT_TESTF_CONSOLE_REC) { |
| 45 | int ret = console_record_reset_enable(); |
| 46 | |
| 47 | if (ret) { |
| 48 | printf("Skipping: Console recording disabled\n"); |
| 49 | return -EAGAIN; |
| 50 | } |
| 51 | } |
| 52 | |
| 53 | return 0; |
| 54 | } |
| 55 | |
| 56 | int test_post_run(struct unit_test_state *uts, struct unit_test *test) |
| 57 | { |
Simon Glass | 30a0d20 | 2021-03-07 17:34:49 -0700 | [diff] [blame] | 58 | gd->flags &= ~GD_FLG_RECORD; |
| 59 | |
Simon Glass | d002a27 | 2021-03-07 17:34:48 -0700 | [diff] [blame] | 60 | return 0; |
| 61 | } |
| 62 | |
Simon Glass | 1c72175 | 2021-03-07 17:34:47 -0700 | [diff] [blame] | 63 | int ut_run_tests(struct unit_test_state *uts, const char *prefix, |
| 64 | struct unit_test *tests, int count, const char *select_name) |
| 65 | { |
| 66 | struct unit_test *test; |
| 67 | int prefix_len = prefix ? strlen(prefix) : 0; |
| 68 | int found = 0; |
| 69 | |
| 70 | for (test = tests; test < tests + count; test++) { |
| 71 | const char *test_name = test->name; |
Simon Glass | d002a27 | 2021-03-07 17:34:48 -0700 | [diff] [blame] | 72 | int ret; |
Simon Glass | 1c72175 | 2021-03-07 17:34:47 -0700 | [diff] [blame] | 73 | |
| 74 | /* Remove the prefix */ |
| 75 | if (prefix && !strncmp(test_name, prefix, prefix_len)) |
| 76 | test_name += prefix_len; |
| 77 | |
| 78 | if (select_name && strcmp(select_name, test_name)) |
| 79 | continue; |
| 80 | printf("Test: %s\n", test_name); |
| 81 | found++; |
| 82 | |
Simon Glass | d002a27 | 2021-03-07 17:34:48 -0700 | [diff] [blame] | 83 | ret = test_pre_run(uts, test); |
| 84 | if (ret == -EAGAIN) |
| 85 | continue; |
| 86 | if (ret) |
| 87 | return ret; |
Simon Glass | 1c72175 | 2021-03-07 17:34:47 -0700 | [diff] [blame] | 88 | |
| 89 | test->func(uts); |
Simon Glass | d002a27 | 2021-03-07 17:34:48 -0700 | [diff] [blame] | 90 | |
| 91 | ret = test_post_run(uts, test); |
| 92 | if (ret) |
| 93 | return ret; |
Simon Glass | 1c72175 | 2021-03-07 17:34:47 -0700 | [diff] [blame] | 94 | } |
| 95 | if (select_name && !found) |
| 96 | return -ENOENT; |
| 97 | |
| 98 | return uts->fail_count ? -EBADF : 0; |
| 99 | } |
| 100 | |
| 101 | int ut_run_list(const char *category, const char *prefix, |
| 102 | struct unit_test *tests, int count, const char *select_name) |
| 103 | { |
| 104 | struct unit_test_state uts = { .fail_count = 0 }; |
| 105 | int ret; |
| 106 | |
| 107 | if (!select_name) |
| 108 | printf("Running %d %s tests\n", count, category); |
| 109 | |
| 110 | ret = ut_run_tests(&uts, prefix, tests, count, select_name); |
| 111 | |
| 112 | if (ret == -ENOENT) |
| 113 | printf("Test '%s' not found\n", select_name); |
| 114 | else |
| 115 | printf("Failures: %d\n", uts.fail_count); |
| 116 | |
| 117 | return ret; |
| 118 | } |