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