Simon Glass | 5722fb2 | 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 | d18f739 | 2021-03-07 17:34:50 -0700 | [diff] [blame] | 9 | #include <dm.h> |
Simon Glass | 2dba476 | 2021-03-07 17:34:58 -0700 | [diff] [blame] | 10 | #include <asm/state.h> |
Simon Glass | d18f739 | 2021-03-07 17:34:50 -0700 | [diff] [blame] | 11 | #include <dm/root.h> |
Simon Glass | 2dba476 | 2021-03-07 17:34:58 -0700 | [diff] [blame] | 12 | #include <dm/test.h> |
Simon Glass | 5b7e55b | 2021-03-07 17:34:59 -0700 | [diff] [blame] | 13 | #include <dm/uclass-internal.h> |
Simon Glass | 5722fb2 | 2021-03-07 17:34:47 -0700 | [diff] [blame] | 14 | #include <test/test.h> |
Simon Glass | d18f739 | 2021-03-07 17:34:50 -0700 | [diff] [blame] | 15 | #include <test/ut.h> |
Simon Glass | 5722fb2 | 2021-03-07 17:34:47 -0700 | [diff] [blame] | 16 | |
Simon Glass | 0f8f677 | 2021-03-07 17:34:49 -0700 | [diff] [blame] | 17 | DECLARE_GLOBAL_DATA_PTR; |
| 18 | |
Simon Glass | 2dba476 | 2021-03-07 17:34:58 -0700 | [diff] [blame] | 19 | /** |
| 20 | * dm_test_pre_run() - Get ready to run a driver model test |
| 21 | * |
| 22 | * This clears out the driver model data structures. For sandbox it resets the |
| 23 | * state structure |
| 24 | * |
| 25 | * @uts: Test state |
| 26 | */ |
| 27 | static int dm_test_pre_run(struct unit_test_state *uts) |
| 28 | { |
| 29 | bool of_live = uts->of_live; |
| 30 | |
| 31 | uts->root = NULL; |
| 32 | uts->testdev = NULL; |
| 33 | uts->force_fail_alloc = false; |
| 34 | uts->skip_post_probe = false; |
| 35 | gd->dm_root = NULL; |
| 36 | if (!CONFIG_IS_ENABLED(OF_PLATDATA)) |
| 37 | memset(dm_testdrv_op_count, '\0', sizeof(dm_testdrv_op_count)); |
| 38 | state_reset_for_test(state_get_current()); |
| 39 | |
| 40 | /* Determine whether to make the live tree available */ |
| 41 | gd_set_of_root(of_live ? uts->of_root : NULL); |
| 42 | ut_assertok(dm_init(of_live)); |
| 43 | uts->root = dm_root(); |
| 44 | |
| 45 | return 0; |
| 46 | } |
| 47 | |
Simon Glass | 5b7e55b | 2021-03-07 17:34:59 -0700 | [diff] [blame] | 48 | static int dm_test_post_run(struct unit_test_state *uts) |
| 49 | { |
| 50 | int id; |
| 51 | |
| 52 | for (id = 0; id < UCLASS_COUNT; id++) { |
| 53 | struct uclass *uc; |
| 54 | |
| 55 | /* |
| 56 | * If the uclass doesn't exist we don't want to create it. So |
| 57 | * check that here before we call uclass_find_device(). |
| 58 | */ |
| 59 | uc = uclass_find(id); |
| 60 | if (!uc) |
| 61 | continue; |
| 62 | ut_assertok(uclass_destroy(uc)); |
| 63 | } |
| 64 | |
| 65 | return 0; |
| 66 | } |
| 67 | |
Simon Glass | 242357c | 2021-03-07 17:34:51 -0700 | [diff] [blame] | 68 | /* Ensure all the test devices are probed */ |
| 69 | static int do_autoprobe(struct unit_test_state *uts) |
| 70 | { |
| 71 | struct udevice *dev; |
| 72 | int ret; |
| 73 | |
| 74 | /* Scanning the uclass is enough to probe all the devices */ |
| 75 | for (ret = uclass_first_device(UCLASS_TEST, &dev); |
| 76 | dev; |
| 77 | ret = uclass_next_device(&dev)) |
| 78 | ; |
| 79 | |
| 80 | return ret; |
| 81 | } |
| 82 | |
Simon Glass | 436687e | 2021-03-07 17:35:01 -0700 | [diff] [blame^] | 83 | /** |
| 84 | * test_pre_run() - Handle any preparation needed to run a test |
| 85 | * |
| 86 | * @uts: Test state |
| 87 | * @test: Test to prepare for |
| 88 | * @return 0 if OK, -EAGAIN to skip this test since some required feature is not |
| 89 | * available, other -ve on error (meaning that testing cannot likely |
| 90 | * continue) |
| 91 | */ |
| 92 | static int test_pre_run(struct unit_test_state *uts, struct unit_test *test) |
Simon Glass | d93dc7d | 2021-03-07 17:34:48 -0700 | [diff] [blame] | 93 | { |
Simon Glass | b2890a1 | 2021-03-07 17:34:56 -0700 | [diff] [blame] | 94 | if (test->flags & UT_TESTF_DM) |
Simon Glass | 2dba476 | 2021-03-07 17:34:58 -0700 | [diff] [blame] | 95 | ut_assertok(dm_test_pre_run(uts)); |
Simon Glass | b2890a1 | 2021-03-07 17:34:56 -0700 | [diff] [blame] | 96 | |
Simon Glass | 59cad96 | 2021-03-07 17:34:55 -0700 | [diff] [blame] | 97 | ut_set_skip_delays(uts, false); |
| 98 | |
Simon Glass | 86a5bd0 | 2021-03-07 17:34:53 -0700 | [diff] [blame] | 99 | uts->start = mallinfo(); |
Simon Glass | d93dc7d | 2021-03-07 17:34:48 -0700 | [diff] [blame] | 100 | |
Simon Glass | 177e0fd | 2021-03-07 17:34:52 -0700 | [diff] [blame] | 101 | if (test->flags & UT_TESTF_SCAN_PDATA) |
| 102 | ut_assertok(dm_scan_plat(false)); |
| 103 | |
Simon Glass | 242357c | 2021-03-07 17:34:51 -0700 | [diff] [blame] | 104 | if (test->flags & UT_TESTF_PROBE_TEST) |
| 105 | ut_assertok(do_autoprobe(uts)); |
| 106 | |
Simon Glass | d18f739 | 2021-03-07 17:34:50 -0700 | [diff] [blame] | 107 | if (!CONFIG_IS_ENABLED(OF_PLATDATA) && |
| 108 | (test->flags & UT_TESTF_SCAN_FDT)) |
| 109 | ut_assertok(dm_extended_scan(false)); |
| 110 | |
Simon Glass | d93dc7d | 2021-03-07 17:34:48 -0700 | [diff] [blame] | 111 | if (test->flags & UT_TESTF_CONSOLE_REC) { |
| 112 | int ret = console_record_reset_enable(); |
| 113 | |
| 114 | if (ret) { |
| 115 | printf("Skipping: Console recording disabled\n"); |
| 116 | return -EAGAIN; |
| 117 | } |
| 118 | } |
Simon Glass | 2b566b9 | 2021-03-07 17:34:54 -0700 | [diff] [blame] | 119 | ut_silence_console(uts); |
Simon Glass | d93dc7d | 2021-03-07 17:34:48 -0700 | [diff] [blame] | 120 | |
| 121 | return 0; |
| 122 | } |
| 123 | |
Simon Glass | 436687e | 2021-03-07 17:35:01 -0700 | [diff] [blame^] | 124 | /** |
| 125 | * test_post_run() - Handle cleaning up after a test |
| 126 | * |
| 127 | * @uts: Test state |
| 128 | * @test: Test to clean up after |
| 129 | * @return 0 if OK, -ve on error (meaning that testing cannot likely continue) |
| 130 | */ |
| 131 | static int test_post_run(struct unit_test_state *uts, struct unit_test *test) |
Simon Glass | d93dc7d | 2021-03-07 17:34:48 -0700 | [diff] [blame] | 132 | { |
Simon Glass | 2b566b9 | 2021-03-07 17:34:54 -0700 | [diff] [blame] | 133 | ut_unsilence_console(uts); |
Simon Glass | 5b7e55b | 2021-03-07 17:34:59 -0700 | [diff] [blame] | 134 | if (test->flags & UT_TESTF_DM) |
| 135 | ut_assertok(dm_test_post_run(uts)); |
Simon Glass | 0f8f677 | 2021-03-07 17:34:49 -0700 | [diff] [blame] | 136 | |
Simon Glass | d93dc7d | 2021-03-07 17:34:48 -0700 | [diff] [blame] | 137 | return 0; |
| 138 | } |
| 139 | |
Simon Glass | 5517edb | 2021-03-07 17:35:00 -0700 | [diff] [blame] | 140 | int ut_run_test(struct unit_test_state *uts, struct unit_test *test, |
| 141 | const char *test_name) |
| 142 | { |
Simon Glass | 436687e | 2021-03-07 17:35:01 -0700 | [diff] [blame^] | 143 | const char *fname = strrchr(test->file, '/') + 1; |
| 144 | const char *note = ""; |
Simon Glass | 5517edb | 2021-03-07 17:35:00 -0700 | [diff] [blame] | 145 | int ret; |
| 146 | |
Simon Glass | 436687e | 2021-03-07 17:35:01 -0700 | [diff] [blame^] | 147 | if ((test->flags & UT_TESTF_DM) && !uts->of_live) |
| 148 | note = " (flat tree)"; |
| 149 | printf("Test: %s: %s%s\n", test_name, fname, note); |
Simon Glass | 5517edb | 2021-03-07 17:35:00 -0700 | [diff] [blame] | 150 | |
| 151 | ret = test_pre_run(uts, test); |
| 152 | if (ret == -EAGAIN) |
| 153 | return -EAGAIN; |
| 154 | if (ret) |
| 155 | return ret; |
| 156 | |
| 157 | test->func(uts); |
| 158 | |
| 159 | ret = test_post_run(uts, test); |
| 160 | if (ret) |
| 161 | return ret; |
| 162 | |
| 163 | return 0; |
| 164 | } |
| 165 | |
Simon Glass | 5722fb2 | 2021-03-07 17:34:47 -0700 | [diff] [blame] | 166 | int ut_run_tests(struct unit_test_state *uts, const char *prefix, |
| 167 | struct unit_test *tests, int count, const char *select_name) |
| 168 | { |
| 169 | struct unit_test *test; |
| 170 | int prefix_len = prefix ? strlen(prefix) : 0; |
| 171 | int found = 0; |
| 172 | |
| 173 | for (test = tests; test < tests + count; test++) { |
| 174 | const char *test_name = test->name; |
Simon Glass | d93dc7d | 2021-03-07 17:34:48 -0700 | [diff] [blame] | 175 | int ret; |
Simon Glass | 5722fb2 | 2021-03-07 17:34:47 -0700 | [diff] [blame] | 176 | |
| 177 | /* Remove the prefix */ |
| 178 | if (prefix && !strncmp(test_name, prefix, prefix_len)) |
| 179 | test_name += prefix_len; |
| 180 | |
| 181 | if (select_name && strcmp(select_name, test_name)) |
| 182 | continue; |
Simon Glass | 5517edb | 2021-03-07 17:35:00 -0700 | [diff] [blame] | 183 | ret = ut_run_test(uts, test, test_name); |
Simon Glass | 5722fb2 | 2021-03-07 17:34:47 -0700 | [diff] [blame] | 184 | found++; |
Simon Glass | d93dc7d | 2021-03-07 17:34:48 -0700 | [diff] [blame] | 185 | if (ret == -EAGAIN) |
| 186 | continue; |
| 187 | if (ret) |
| 188 | return ret; |
Simon Glass | 5722fb2 | 2021-03-07 17:34:47 -0700 | [diff] [blame] | 189 | } |
| 190 | if (select_name && !found) |
| 191 | return -ENOENT; |
| 192 | |
| 193 | return uts->fail_count ? -EBADF : 0; |
| 194 | } |
| 195 | |
| 196 | int ut_run_list(const char *category, const char *prefix, |
| 197 | struct unit_test *tests, int count, const char *select_name) |
| 198 | { |
| 199 | struct unit_test_state uts = { .fail_count = 0 }; |
| 200 | int ret; |
| 201 | |
| 202 | if (!select_name) |
| 203 | printf("Running %d %s tests\n", count, category); |
| 204 | |
| 205 | ret = ut_run_tests(&uts, prefix, tests, count, select_name); |
| 206 | |
| 207 | if (ret == -ENOENT) |
| 208 | printf("Test '%s' not found\n", select_name); |
| 209 | else |
| 210 | printf("Failures: %d\n", uts.fail_count); |
| 211 | |
| 212 | return ret; |
| 213 | } |