Tom Rini | 10e4779 | 2018-05-06 17:58:06 -0400 | [diff] [blame] | 1 | /* SPDX-License-Identifier: GPL-2.0+ */ |
Joe Hershberger | 3a77be5 | 2015-05-20 14:27:27 -0500 | [diff] [blame] | 2 | /* |
| 3 | * Copyright (c) 2013 Google, Inc. |
Joe Hershberger | 3a77be5 | 2015-05-20 14:27:27 -0500 | [diff] [blame] | 4 | */ |
| 5 | |
| 6 | #ifndef __TEST_TEST_H |
| 7 | #define __TEST_TEST_H |
| 8 | |
| 9 | #include <malloc.h> |
Simon Glass | 974dccd | 2020-07-28 19:41:12 -0600 | [diff] [blame] | 10 | #include <linux/bitops.h> |
Joe Hershberger | 3a77be5 | 2015-05-20 14:27:27 -0500 | [diff] [blame] | 11 | |
Simon Glass | ef69dbe | 2025-01-20 14:25:59 -0700 | [diff] [blame] | 12 | /** |
| 13 | * struct ut_stats - Statistics about tests run |
Joe Hershberger | 3a77be5 | 2015-05-20 14:27:27 -0500 | [diff] [blame] | 14 | * |
| 15 | * @fail_count: Number of tests that failed |
Simon Glass | 816fe6c | 2022-10-20 18:22:48 -0600 | [diff] [blame] | 16 | * @skip_count: Number of tests that were skipped |
Simon Glass | 4f621e5 | 2025-01-20 14:26:00 -0700 | [diff] [blame] | 17 | * @test_count: Number of tests run. If a test is run muiltiple times, only one |
| 18 | * is counted |
Simon Glass | 5fa92d6 | 2025-02-07 11:30:35 -0700 | [diff] [blame] | 19 | * @start: Timer value when test started |
| 20 | * @duration_ms: Suite duration in milliseconds |
Simon Glass | ef69dbe | 2025-01-20 14:25:59 -0700 | [diff] [blame] | 21 | */ |
| 22 | struct ut_stats { |
| 23 | int fail_count; |
| 24 | int skip_count; |
Simon Glass | 4f621e5 | 2025-01-20 14:26:00 -0700 | [diff] [blame] | 25 | int test_count; |
Simon Glass | 5fa92d6 | 2025-02-07 11:30:35 -0700 | [diff] [blame] | 26 | ulong start; |
| 27 | ulong duration_ms; |
Simon Glass | ef69dbe | 2025-01-20 14:25:59 -0700 | [diff] [blame] | 28 | }; |
| 29 | |
| 30 | /* |
| 31 | * struct unit_test_state - Entire state of test system |
| 32 | * |
| 33 | * @cur: Statistics for the current run |
Simon Glass | fb82a04 | 2025-01-20 14:26:02 -0700 | [diff] [blame] | 34 | * @total: Statistics for all test runs |
| 35 | * @run_count: Number of times ut_run_list() has been called |
Simon Glass | bd9b151 | 2025-02-07 11:30:36 -0700 | [diff] [blame] | 36 | * @worst: Sute which had the first per-text run time |
| 37 | * @worst_ms: Time taken by that test |
Joe Hershberger | 3a77be5 | 2015-05-20 14:27:27 -0500 | [diff] [blame] | 38 | * @start: Store the starting mallinfo when doing leak test |
Simon Glass | b2890a1 | 2021-03-07 17:34:56 -0700 | [diff] [blame] | 39 | * @of_live: true to use livetree if available, false to use flattree |
Simon Glass | 017886b | 2017-05-18 20:09:17 -0600 | [diff] [blame] | 40 | * @of_root: Record of the livetree root node (used for setting up tests) |
Simon Glass | b98bfbc | 2021-03-07 17:34:57 -0700 | [diff] [blame] | 41 | * @root: Root device |
| 42 | * @testdev: Test device |
| 43 | * @force_fail_alloc: Force all memory allocs to fail |
| 44 | * @skip_post_probe: Skip uclass post-probe processing |
Simon Glass | 3ba7675 | 2022-09-06 20:27:05 -0600 | [diff] [blame] | 45 | * @fdt_chksum: crc8 of the device tree contents |
| 46 | * @fdt_copy: Copy of the device tree |
| 47 | * @fdt_size: Size of the device-tree copy |
Simon Glass | 1a92f83 | 2024-08-22 07:57:48 -0600 | [diff] [blame] | 48 | * @other_fdt: Buffer for the other FDT (UTF_OTHER_FDT) |
| 49 | * @other_fdt_size: Size of the other FDT (UTF_OTHER_FDT) |
Simon Glass | b995807 | 2022-09-06 20:27:11 -0600 | [diff] [blame] | 50 | * @of_other: Live tree for the other FDT |
Simon Glass | 91a187b | 2022-08-01 07:58:45 -0600 | [diff] [blame] | 51 | * @runs_per_test: Number of times to run each test (typically 1) |
Simon Glass | 1a92f83 | 2024-08-22 07:57:48 -0600 | [diff] [blame] | 52 | * @force_run: true to run tests marked with the UTF_MANUAL flag |
Simon Glass | be277ee | 2024-10-28 13:47:53 +0100 | [diff] [blame] | 53 | * @old_bloblist: stores the old gd->bloblist pointer |
Simon Glass | d856e91 | 2020-01-27 08:49:56 -0700 | [diff] [blame] | 54 | * @expect_str: Temporary string used to hold expected string value |
| 55 | * @actual_str: Temporary string used to hold actual string value |
Joe Hershberger | 3a77be5 | 2015-05-20 14:27:27 -0500 | [diff] [blame] | 56 | */ |
| 57 | struct unit_test_state { |
Simon Glass | ef69dbe | 2025-01-20 14:25:59 -0700 | [diff] [blame] | 58 | struct ut_stats cur; |
Simon Glass | fb82a04 | 2025-01-20 14:26:02 -0700 | [diff] [blame] | 59 | struct ut_stats total; |
| 60 | int run_count; |
Simon Glass | bd9b151 | 2025-02-07 11:30:36 -0700 | [diff] [blame] | 61 | const struct suite *worst; |
| 62 | int worst_ms; |
Joe Hershberger | 3a77be5 | 2015-05-20 14:27:27 -0500 | [diff] [blame] | 63 | struct mallinfo start; |
Simon Glass | 017886b | 2017-05-18 20:09:17 -0600 | [diff] [blame] | 64 | struct device_node *of_root; |
Simon Glass | b2890a1 | 2021-03-07 17:34:56 -0700 | [diff] [blame] | 65 | bool of_live; |
Simon Glass | b98bfbc | 2021-03-07 17:34:57 -0700 | [diff] [blame] | 66 | struct udevice *root; |
| 67 | struct udevice *testdev; |
| 68 | int force_fail_alloc; |
| 69 | int skip_post_probe; |
Simon Glass | 3ba7675 | 2022-09-06 20:27:05 -0600 | [diff] [blame] | 70 | uint fdt_chksum; |
| 71 | void *fdt_copy; |
| 72 | uint fdt_size; |
Simon Glass | 57b00a9 | 2022-09-06 20:27:10 -0600 | [diff] [blame] | 73 | void *other_fdt; |
| 74 | int other_fdt_size; |
Simon Glass | b995807 | 2022-09-06 20:27:11 -0600 | [diff] [blame] | 75 | struct device_node *of_other; |
Simon Glass | 91a187b | 2022-08-01 07:58:45 -0600 | [diff] [blame] | 76 | int runs_per_test; |
Simon Glass | 1f1614b | 2022-10-20 18:22:50 -0600 | [diff] [blame] | 77 | bool force_run; |
Simon Glass | be277ee | 2024-10-28 13:47:53 +0100 | [diff] [blame] | 78 | void *old_bloblist; |
Simon Glass | 4a249ba | 2021-05-08 06:59:59 -0600 | [diff] [blame] | 79 | char expect_str[512]; |
| 80 | char actual_str[512]; |
Joe Hershberger | 3a77be5 | 2015-05-20 14:27:27 -0500 | [diff] [blame] | 81 | }; |
| 82 | |
Simon Glass | 974dccd | 2020-07-28 19:41:12 -0600 | [diff] [blame] | 83 | /* Test flags for each test */ |
Simon Glass | 1a92f83 | 2024-08-22 07:57:48 -0600 | [diff] [blame] | 84 | enum ut_flags { |
| 85 | UTF_SCAN_PDATA = BIT(0), /* test needs platform data */ |
| 86 | UTF_PROBE_TEST = BIT(1), /* probe test uclass */ |
| 87 | UTF_SCAN_FDT = BIT(2), /* scan device tree */ |
| 88 | UTF_FLAT_TREE = BIT(3), /* test needs flat DT */ |
| 89 | UTF_LIVE_TREE = BIT(4), /* needs live device tree */ |
Simon Glass | 11fcfa3 | 2024-08-22 07:57:50 -0600 | [diff] [blame] | 90 | UTF_CONSOLE = BIT(5), /* needs console recording */ |
Simon Glass | ab1fca0 | 2021-03-07 17:34:45 -0700 | [diff] [blame] | 91 | /* do extra driver model init and uninit */ |
Simon Glass | 1a92f83 | 2024-08-22 07:57:48 -0600 | [diff] [blame] | 92 | UTF_DM = BIT(6), |
| 93 | UTF_OTHER_FDT = BIT(7), /* read in other device tree */ |
Simon Glass | 1f1614b | 2022-10-20 18:22:50 -0600 | [diff] [blame] | 94 | /* |
| 95 | * Only run if explicitly requested with 'ut -f <suite> <test>'. The |
| 96 | * test name must end in "_norun" so that pytest detects this also, |
| 97 | * since it cannot access the flags. |
| 98 | */ |
Simon Glass | 1a92f83 | 2024-08-22 07:57:48 -0600 | [diff] [blame] | 99 | UTF_MANUAL = BIT(8), |
| 100 | UTF_ETH_BOOTDEV = BIT(9), /* enable Ethernet bootdevs */ |
| 101 | UTF_SF_BOOTDEV = BIT(10), /* enable SPI flash bootdevs */ |
Simon Glass | be277ee | 2024-10-28 13:47:53 +0100 | [diff] [blame] | 102 | UFT_BLOBLIST = BIT(11), /* test changes gd->bloblist */ |
Simon Glass | 5c9a124 | 2025-02-07 11:30:38 -0700 | [diff] [blame^] | 103 | UTF_INIT = BIT(12), /* test inits a suite */ |
| 104 | UTF_UNINIT = BIT(13), /* test uninits a suite */ |
Simon Glass | 974dccd | 2020-07-28 19:41:12 -0600 | [diff] [blame] | 105 | }; |
| 106 | |
Joe Hershberger | 3a77be5 | 2015-05-20 14:27:27 -0500 | [diff] [blame] | 107 | /** |
| 108 | * struct unit_test - Information about a unit test |
| 109 | * |
| 110 | * @name: Name of test |
| 111 | * @func: Function to call to perform test |
| 112 | * @flags: Flags indicated pre-conditions for test |
| 113 | */ |
| 114 | struct unit_test { |
Simon Glass | be408ee | 2017-05-18 20:09:15 -0600 | [diff] [blame] | 115 | const char *file; |
Joe Hershberger | 3a77be5 | 2015-05-20 14:27:27 -0500 | [diff] [blame] | 116 | const char *name; |
| 117 | int (*func)(struct unit_test_state *state); |
| 118 | int flags; |
| 119 | }; |
| 120 | |
Heinrich Schuchardt | df6c36c | 2020-05-06 18:26:07 +0200 | [diff] [blame] | 121 | /** |
| 122 | * UNIT_TEST() - create linker generated list entry for unit a unit test |
| 123 | * |
| 124 | * The macro UNIT_TEST() is used to create a linker generated list entry. These |
| 125 | * list entries are enumerate tests that can be execute using the ut command. |
| 126 | * The list entries are used both by the implementation of the ut command as |
| 127 | * well as in a related Python test. |
| 128 | * |
| 129 | * For Python testing the subtests are collected in Python function |
| 130 | * generate_ut_subtest() by applying a regular expression to the lines of file |
| 131 | * u-boot.sym. The list entries have to follow strict naming conventions to be |
| 132 | * matched by the expression. |
| 133 | * |
| 134 | * Use UNIT_TEST(foo_test_bar, _flags, foo_test) for a test bar in test suite |
| 135 | * foo that can be executed via command 'ut foo bar' and is implemented in |
| 136 | * function foo_test_bar(). |
| 137 | * |
| 138 | * @_name: concatenation of name of the test suite, "_test_", and the name |
| 139 | * of the test |
| 140 | * @_flags: an integer field that can be evaluated by the test suite |
Simon Glass | 1a92f83 | 2024-08-22 07:57:48 -0600 | [diff] [blame] | 141 | * implementation (see enum ut_flags) |
Heinrich Schuchardt | df6c36c | 2020-05-06 18:26:07 +0200 | [diff] [blame] | 142 | * @_suite: name of the test suite concatenated with "_test" |
| 143 | */ |
Joe Hershberger | 3a77be5 | 2015-05-20 14:27:27 -0500 | [diff] [blame] | 144 | #define UNIT_TEST(_name, _flags, _suite) \ |
Simon Glass | 1638693 | 2021-03-07 17:35:11 -0700 | [diff] [blame] | 145 | ll_entry_declare(struct unit_test, _name, ut_ ## _suite) = { \ |
Simon Glass | be408ee | 2017-05-18 20:09:15 -0600 | [diff] [blame] | 146 | .file = __FILE__, \ |
Joe Hershberger | 3a77be5 | 2015-05-20 14:27:27 -0500 | [diff] [blame] | 147 | .name = #_name, \ |
| 148 | .flags = _flags, \ |
| 149 | .func = _name, \ |
| 150 | } |
| 151 | |
Simon Glass | 5c9a124 | 2025-02-07 11:30:38 -0700 | [diff] [blame^] | 152 | /* init function for unit-test suite (the 'A' makes it first) */ |
| 153 | #define UNIT_TEST_INIT(_name, _flags, _suite) \ |
| 154 | ll_entry_declare(struct unit_test, A ## _name, ut_ ## _suite) = { \ |
| 155 | .file = __FILE__, \ |
| 156 | .name = #_name, \ |
| 157 | .flags = (_flags) | UTF_INIT, \ |
| 158 | .func = _name, \ |
| 159 | } |
| 160 | |
| 161 | /* uninit function for unit-test suite (the 'aaa' makes it last) */ |
| 162 | #define UNIT_TEST_UNINIT(_name, _flags, _suite) \ |
| 163 | ll_entry_declare(struct unit_test, zzz ## _name, ut_ ## _suite) = { \ |
| 164 | .file = __FILE__, \ |
| 165 | .name = #_name, \ |
| 166 | .flags = (_flags) | UTF_UNINIT, \ |
| 167 | .func = _name, \ |
| 168 | } |
| 169 | |
Simon Glass | 1638693 | 2021-03-07 17:35:11 -0700 | [diff] [blame] | 170 | /* Get the start of a list of unit tests for a particular suite */ |
Simon Glass | b50211f | 2021-03-07 17:35:10 -0700 | [diff] [blame] | 171 | #define UNIT_TEST_SUITE_START(_suite) \ |
Simon Glass | 1638693 | 2021-03-07 17:35:11 -0700 | [diff] [blame] | 172 | ll_entry_start(struct unit_test, ut_ ## _suite) |
Simon Glass | b50211f | 2021-03-07 17:35:10 -0700 | [diff] [blame] | 173 | #define UNIT_TEST_SUITE_COUNT(_suite) \ |
Simon Glass | 1638693 | 2021-03-07 17:35:11 -0700 | [diff] [blame] | 174 | ll_entry_count(struct unit_test, ut_ ## _suite) |
Simon Glass | b50211f | 2021-03-07 17:35:10 -0700 | [diff] [blame] | 175 | |
Simon Glass | 1ef74ab | 2021-03-07 17:35:12 -0700 | [diff] [blame] | 176 | /* Use ! and ~ so that all tests will be sorted between these two values */ |
| 177 | #define UNIT_TEST_ALL_START() ll_entry_start(struct unit_test, ut_!) |
| 178 | #define UNIT_TEST_ALL_END() ll_entry_start(struct unit_test, ut_~) |
| 179 | #define UNIT_TEST_ALL_COUNT() (UNIT_TEST_ALL_END() - UNIT_TEST_ALL_START()) |
| 180 | |
Simon Glass | 204675c | 2019-12-29 21:19:25 -0700 | [diff] [blame] | 181 | /* Sizes for devres tests */ |
| 182 | enum { |
| 183 | TEST_DEVRES_SIZE = 100, |
| 184 | TEST_DEVRES_COUNT = 10, |
| 185 | TEST_DEVRES_TOTAL = TEST_DEVRES_SIZE * TEST_DEVRES_COUNT, |
| 186 | |
Simon Glass | 0fe1537 | 2019-12-29 21:19:28 -0700 | [diff] [blame] | 187 | /* A few different sizes */ |
Simon Glass | 204675c | 2019-12-29 21:19:25 -0700 | [diff] [blame] | 188 | TEST_DEVRES_SIZE2 = 15, |
Simon Glass | 0fe1537 | 2019-12-29 21:19:28 -0700 | [diff] [blame] | 189 | TEST_DEVRES_SIZE3 = 37, |
Simon Glass | 204675c | 2019-12-29 21:19:25 -0700 | [diff] [blame] | 190 | }; |
Joe Hershberger | 3a77be5 | 2015-05-20 14:27:27 -0500 | [diff] [blame] | 191 | |
Simon Glass | a4e289b | 2020-10-25 20:38:28 -0600 | [diff] [blame] | 192 | /** |
Simon Glass | 4bf8972 | 2020-12-23 08:11:18 -0700 | [diff] [blame] | 193 | * testbus_get_clear_removed() - Test function to obtain removed device |
| 194 | * |
| 195 | * This is used in testbus to find out which device was removed. Calling this |
| 196 | * function returns a pointer to the device and then clears it back to NULL, so |
| 197 | * that a future test can check it. |
| 198 | */ |
| 199 | struct udevice *testbus_get_clear_removed(void); |
| 200 | |
Simon Glass | 45b807d | 2021-03-25 10:44:33 +1300 | [diff] [blame] | 201 | #ifdef CONFIG_SANDBOX |
| 202 | #include <asm/state.h> |
Simon Glass | 57b00a9 | 2022-09-06 20:27:10 -0600 | [diff] [blame] | 203 | #include <asm/test.h> |
| 204 | #endif |
Simon Glass | 45b807d | 2021-03-25 10:44:33 +1300 | [diff] [blame] | 205 | |
Simon Glass | 57b00a9 | 2022-09-06 20:27:10 -0600 | [diff] [blame] | 206 | static inline void arch_reset_for_test(void) |
| 207 | { |
| 208 | #ifdef CONFIG_SANDBOX |
Simon Glass | 45b807d | 2021-03-25 10:44:33 +1300 | [diff] [blame] | 209 | state_reset_for_test(state_get_current()); |
| 210 | #endif |
Simon Glass | 57b00a9 | 2022-09-06 20:27:10 -0600 | [diff] [blame] | 211 | } |
| 212 | static inline int test_load_other_fdt(struct unit_test_state *uts) |
| 213 | { |
| 214 | int ret = 0; |
| 215 | #ifdef CONFIG_SANDBOX |
| 216 | ret = sandbox_load_other_fdt(&uts->other_fdt, &uts->other_fdt_size); |
| 217 | #endif |
| 218 | return ret; |
Simon Glass | 45b807d | 2021-03-25 10:44:33 +1300 | [diff] [blame] | 219 | } |
| 220 | |
Simon Glass | 66014cc | 2023-01-17 10:47:27 -0700 | [diff] [blame] | 221 | /** |
Simon Glass | ffc4948 | 2023-01-17 10:47:36 -0700 | [diff] [blame] | 222 | * Control skipping of time delays |
| 223 | * |
| 224 | * Some tests have unnecessay time delays (e.g. USB). Allow these to be |
| 225 | * skipped to speed up testing |
| 226 | * |
| 227 | * @param skip_delays true to skip delays from now on, false to honour delay |
| 228 | * requests |
| 229 | */ |
| 230 | static inline void test_set_skip_delays(bool skip_delays) |
| 231 | { |
| 232 | #ifdef CONFIG_SANDBOX |
| 233 | state_set_skip_delays(skip_delays); |
| 234 | #endif |
| 235 | } |
| 236 | |
| 237 | /** |
Simon Glass | 66014cc | 2023-01-17 10:47:27 -0700 | [diff] [blame] | 238 | * test_set_eth_enable() - Enable / disable Ethernet |
| 239 | * |
| 240 | * Allows control of whether Ethernet packets are actually send/received |
| 241 | * |
| 242 | * @enable: true to enable Ethernet, false to disable |
| 243 | */ |
| 244 | static inline void test_set_eth_enable(bool enable) |
| 245 | { |
| 246 | #ifdef CONFIG_SANDBOX |
| 247 | sandbox_set_eth_enable(enable); |
| 248 | #endif |
| 249 | } |
| 250 | |
| 251 | /* Allow ethernet to be disabled for testing purposes */ |
| 252 | static inline bool test_eth_enabled(void) |
| 253 | { |
| 254 | bool enabled = true; |
| 255 | |
| 256 | #ifdef CONFIG_SANDBOX |
| 257 | enabled = sandbox_eth_enabled(); |
| 258 | #endif |
| 259 | return enabled; |
| 260 | } |
| 261 | |
Simon Glass | b490f5f | 2023-01-17 10:47:28 -0700 | [diff] [blame] | 262 | /* Allow ethernet bootdev to be ignored for testing purposes */ |
| 263 | static inline bool test_eth_bootdev_enabled(void) |
| 264 | { |
| 265 | bool enabled = true; |
| 266 | |
| 267 | #ifdef CONFIG_SANDBOX |
| 268 | enabled = sandbox_eth_enabled(); |
| 269 | #endif |
| 270 | return enabled; |
| 271 | } |
| 272 | |
Simon Glass | 4937be0 | 2023-01-17 10:48:02 -0700 | [diff] [blame] | 273 | /* Allow SPI flash bootdev to be ignored for testing purposes */ |
| 274 | static inline bool test_sf_bootdev_enabled(void) |
| 275 | { |
| 276 | bool enabled = true; |
| 277 | |
| 278 | #ifdef CONFIG_SANDBOX |
| 279 | enabled = sandbox_sf_bootdev_enabled(); |
| 280 | #endif |
| 281 | return enabled; |
| 282 | } |
| 283 | |
| 284 | static inline void test_sf_set_enable_bootdevs(bool enable) |
| 285 | { |
| 286 | #ifdef CONFIG_SANDBOX |
| 287 | sandbox_sf_set_enable_bootdevs(enable); |
| 288 | #endif |
| 289 | } |
| 290 | |
Joe Hershberger | 3a77be5 | 2015-05-20 14:27:27 -0500 | [diff] [blame] | 291 | #endif /* __TEST_TEST_H */ |