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