]>
Commit | Line | Data |
---|---|---|
83d290c5 | 1 | /* SPDX-License-Identifier: GPL-2.0+ */ |
e721b882 JH |
2 | /* |
3 | * Copyright (c) 2013 Google, Inc. | |
e721b882 JH |
4 | */ |
5 | ||
6 | #ifndef __TEST_TEST_H | |
7 | #define __TEST_TEST_H | |
8 | ||
9 | #include <malloc.h> | |
e180c2b1 | 10 | #include <linux/bitops.h> |
e721b882 | 11 | |
0925659a SG |
12 | /** |
13 | * struct ut_stats - Statistics about tests run | |
e721b882 JH |
14 | * |
15 | * @fail_count: Number of tests that failed | |
1facaade | 16 | * @skip_count: Number of tests that were skipped |
561320be SG |
17 | * @test_count: Number of tests run. If a test is run muiltiple times, only one |
18 | * is counted | |
0925659a SG |
19 | */ |
20 | struct ut_stats { | |
21 | int fail_count; | |
22 | int skip_count; | |
561320be | 23 | int test_count; |
0925659a SG |
24 | }; |
25 | ||
26 | /* | |
27 | * struct unit_test_state - Entire state of test system | |
28 | * | |
29 | * @cur: Statistics for the current run | |
6d97c980 SG |
30 | * @total: Statistics for all test runs |
31 | * @run_count: Number of times ut_run_list() has been called | |
e721b882 | 32 | * @start: Store the starting mallinfo when doing leak test |
72b524cf | 33 | * @of_live: true to use livetree if available, false to use flattree |
6fb2f579 | 34 | * @of_root: Record of the livetree root node (used for setting up tests) |
4a467c6d SG |
35 | * @root: Root device |
36 | * @testdev: Test device | |
37 | * @force_fail_alloc: Force all memory allocs to fail | |
38 | * @skip_post_probe: Skip uclass post-probe processing | |
0e4b697f SG |
39 | * @fdt_chksum: crc8 of the device tree contents |
40 | * @fdt_copy: Copy of the device tree | |
41 | * @fdt_size: Size of the device-tree copy | |
725c438c SG |
42 | * @other_fdt: Buffer for the other FDT (UTF_OTHER_FDT) |
43 | * @other_fdt_size: Size of the other FDT (UTF_OTHER_FDT) | |
8d468a18 | 44 | * @of_other: Live tree for the other FDT |
ea94d053 | 45 | * @runs_per_test: Number of times to run each test (typically 1) |
725c438c | 46 | * @force_run: true to run tests marked with the UTF_MANUAL flag |
3f1d7993 | 47 | * @old_bloblist: stores the old gd->bloblist pointer |
400175b0 SG |
48 | * @expect_str: Temporary string used to hold expected string value |
49 | * @actual_str: Temporary string used to hold actual string value | |
e721b882 JH |
50 | */ |
51 | struct unit_test_state { | |
0925659a | 52 | struct ut_stats cur; |
6d97c980 SG |
53 | struct ut_stats total; |
54 | int run_count; | |
e721b882 | 55 | struct mallinfo start; |
6fb2f579 | 56 | struct device_node *of_root; |
72b524cf | 57 | bool of_live; |
4a467c6d SG |
58 | struct udevice *root; |
59 | struct udevice *testdev; | |
60 | int force_fail_alloc; | |
61 | int skip_post_probe; | |
0e4b697f SG |
62 | uint fdt_chksum; |
63 | void *fdt_copy; | |
64 | uint fdt_size; | |
756c0142 SG |
65 | void *other_fdt; |
66 | int other_fdt_size; | |
8d468a18 | 67 | struct device_node *of_other; |
ea94d053 | 68 | int runs_per_test; |
cbd71fad | 69 | bool force_run; |
3f1d7993 | 70 | void *old_bloblist; |
c614ddf2 SG |
71 | char expect_str[512]; |
72 | char actual_str[512]; | |
e721b882 JH |
73 | }; |
74 | ||
e180c2b1 | 75 | /* Test flags for each test */ |
725c438c SG |
76 | enum ut_flags { |
77 | UTF_SCAN_PDATA = BIT(0), /* test needs platform data */ | |
78 | UTF_PROBE_TEST = BIT(1), /* probe test uclass */ | |
79 | UTF_SCAN_FDT = BIT(2), /* scan device tree */ | |
80 | UTF_FLAT_TREE = BIT(3), /* test needs flat DT */ | |
81 | UTF_LIVE_TREE = BIT(4), /* needs live device tree */ | |
9b99762e | 82 | UTF_CONSOLE = BIT(5), /* needs console recording */ |
4bc639ee | 83 | /* do extra driver model init and uninit */ |
725c438c SG |
84 | UTF_DM = BIT(6), |
85 | UTF_OTHER_FDT = BIT(7), /* read in other device tree */ | |
cbd71fad SG |
86 | /* |
87 | * Only run if explicitly requested with 'ut -f <suite> <test>'. The | |
88 | * test name must end in "_norun" so that pytest detects this also, | |
89 | * since it cannot access the flags. | |
90 | */ | |
725c438c SG |
91 | UTF_MANUAL = BIT(8), |
92 | UTF_ETH_BOOTDEV = BIT(9), /* enable Ethernet bootdevs */ | |
93 | UTF_SF_BOOTDEV = BIT(10), /* enable SPI flash bootdevs */ | |
3f1d7993 | 94 | UFT_BLOBLIST = BIT(11), /* test changes gd->bloblist */ |
e180c2b1 SG |
95 | }; |
96 | ||
e721b882 JH |
97 | /** |
98 | * struct unit_test - Information about a unit test | |
99 | * | |
100 | * @name: Name of test | |
101 | * @func: Function to call to perform test | |
102 | * @flags: Flags indicated pre-conditions for test | |
103 | */ | |
104 | struct unit_test { | |
801587bd | 105 | const char *file; |
e721b882 JH |
106 | const char *name; |
107 | int (*func)(struct unit_test_state *state); | |
108 | int flags; | |
109 | }; | |
110 | ||
d0ba026b HS |
111 | /** |
112 | * UNIT_TEST() - create linker generated list entry for unit a unit test | |
113 | * | |
114 | * The macro UNIT_TEST() is used to create a linker generated list entry. These | |
115 | * list entries are enumerate tests that can be execute using the ut command. | |
116 | * The list entries are used both by the implementation of the ut command as | |
117 | * well as in a related Python test. | |
118 | * | |
119 | * For Python testing the subtests are collected in Python function | |
120 | * generate_ut_subtest() by applying a regular expression to the lines of file | |
121 | * u-boot.sym. The list entries have to follow strict naming conventions to be | |
122 | * matched by the expression. | |
123 | * | |
124 | * Use UNIT_TEST(foo_test_bar, _flags, foo_test) for a test bar in test suite | |
125 | * foo that can be executed via command 'ut foo bar' and is implemented in | |
126 | * function foo_test_bar(). | |
127 | * | |
128 | * @_name: concatenation of name of the test suite, "_test_", and the name | |
129 | * of the test | |
130 | * @_flags: an integer field that can be evaluated by the test suite | |
725c438c | 131 | * implementation (see enum ut_flags) |
d0ba026b HS |
132 | * @_suite: name of the test suite concatenated with "_test" |
133 | */ | |
e721b882 | 134 | #define UNIT_TEST(_name, _flags, _suite) \ |
2a2814d5 | 135 | ll_entry_declare(struct unit_test, _name, ut_ ## _suite) = { \ |
801587bd | 136 | .file = __FILE__, \ |
e721b882 JH |
137 | .name = #_name, \ |
138 | .flags = _flags, \ | |
139 | .func = _name, \ | |
140 | } | |
141 | ||
2a2814d5 | 142 | /* Get the start of a list of unit tests for a particular suite */ |
a7a98755 | 143 | #define UNIT_TEST_SUITE_START(_suite) \ |
2a2814d5 | 144 | ll_entry_start(struct unit_test, ut_ ## _suite) |
a7a98755 | 145 | #define UNIT_TEST_SUITE_COUNT(_suite) \ |
2a2814d5 | 146 | ll_entry_count(struct unit_test, ut_ ## _suite) |
a7a98755 | 147 | |
8482356f SG |
148 | /* Use ! and ~ so that all tests will be sorted between these two values */ |
149 | #define UNIT_TEST_ALL_START() ll_entry_start(struct unit_test, ut_!) | |
150 | #define UNIT_TEST_ALL_END() ll_entry_start(struct unit_test, ut_~) | |
151 | #define UNIT_TEST_ALL_COUNT() (UNIT_TEST_ALL_END() - UNIT_TEST_ALL_START()) | |
152 | ||
dc12ebbb SG |
153 | /* Sizes for devres tests */ |
154 | enum { | |
155 | TEST_DEVRES_SIZE = 100, | |
156 | TEST_DEVRES_COUNT = 10, | |
157 | TEST_DEVRES_TOTAL = TEST_DEVRES_SIZE * TEST_DEVRES_COUNT, | |
158 | ||
42a0ce57 | 159 | /* A few different sizes */ |
dc12ebbb | 160 | TEST_DEVRES_SIZE2 = 15, |
42a0ce57 | 161 | TEST_DEVRES_SIZE3 = 37, |
dc12ebbb | 162 | }; |
e721b882 | 163 | |
079ac595 SG |
164 | /** |
165 | * testbus_get_clear_removed() - Test function to obtain removed device | |
166 | * | |
167 | * This is used in testbus to find out which device was removed. Calling this | |
168 | * function returns a pointer to the device and then clears it back to NULL, so | |
169 | * that a future test can check it. | |
170 | */ | |
171 | struct udevice *testbus_get_clear_removed(void); | |
172 | ||
d4a1592a SG |
173 | #ifdef CONFIG_SANDBOX |
174 | #include <asm/state.h> | |
756c0142 SG |
175 | #include <asm/test.h> |
176 | #endif | |
d4a1592a | 177 | |
756c0142 SG |
178 | static inline void arch_reset_for_test(void) |
179 | { | |
180 | #ifdef CONFIG_SANDBOX | |
d4a1592a SG |
181 | state_reset_for_test(state_get_current()); |
182 | #endif | |
183 | } | |
756c0142 SG |
184 | static inline int test_load_other_fdt(struct unit_test_state *uts) |
185 | { | |
186 | int ret = 0; | |
187 | #ifdef CONFIG_SANDBOX | |
188 | ret = sandbox_load_other_fdt(&uts->other_fdt, &uts->other_fdt_size); | |
189 | #endif | |
190 | return ret; | |
191 | } | |
d4a1592a | 192 | |
8b031871 SG |
193 | /** |
194 | * Control skipping of time delays | |
195 | * | |
196 | * Some tests have unnecessay time delays (e.g. USB). Allow these to be | |
197 | * skipped to speed up testing | |
198 | * | |
199 | * @param skip_delays true to skip delays from now on, false to honour delay | |
200 | * requests | |
201 | */ | |
202 | static inline void test_set_skip_delays(bool skip_delays) | |
203 | { | |
204 | #ifdef CONFIG_SANDBOX | |
205 | state_set_skip_delays(skip_delays); | |
206 | #endif | |
207 | } | |
208 | ||
f43b2df3 SG |
209 | /** |
210 | * test_set_eth_enable() - Enable / disable Ethernet | |
211 | * | |
212 | * Allows control of whether Ethernet packets are actually send/received | |
213 | * | |
214 | * @enable: true to enable Ethernet, false to disable | |
215 | */ | |
216 | static inline void test_set_eth_enable(bool enable) | |
217 | { | |
218 | #ifdef CONFIG_SANDBOX | |
219 | sandbox_set_eth_enable(enable); | |
220 | #endif | |
221 | } | |
222 | ||
223 | /* Allow ethernet to be disabled for testing purposes */ | |
224 | static inline bool test_eth_enabled(void) | |
225 | { | |
226 | bool enabled = true; | |
227 | ||
228 | #ifdef CONFIG_SANDBOX | |
229 | enabled = sandbox_eth_enabled(); | |
230 | #endif | |
231 | return enabled; | |
232 | } | |
233 | ||
70dd8865 SG |
234 | /* Allow ethernet bootdev to be ignored for testing purposes */ |
235 | static inline bool test_eth_bootdev_enabled(void) | |
236 | { | |
237 | bool enabled = true; | |
238 | ||
239 | #ifdef CONFIG_SANDBOX | |
240 | enabled = sandbox_eth_enabled(); | |
241 | #endif | |
242 | return enabled; | |
243 | } | |
244 | ||
081bdc52 SG |
245 | /* Allow SPI flash bootdev to be ignored for testing purposes */ |
246 | static inline bool test_sf_bootdev_enabled(void) | |
247 | { | |
248 | bool enabled = true; | |
249 | ||
250 | #ifdef CONFIG_SANDBOX | |
251 | enabled = sandbox_sf_bootdev_enabled(); | |
252 | #endif | |
253 | return enabled; | |
254 | } | |
255 | ||
256 | static inline void test_sf_set_enable_bootdevs(bool enable) | |
257 | { | |
258 | #ifdef CONFIG_SANDBOX | |
259 | sandbox_sf_set_enable_bootdevs(enable); | |
260 | #endif | |
261 | } | |
262 | ||
e721b882 | 263 | #endif /* __TEST_TEST_H */ |