blob: 25c7712d160fdb8e40e2cfffe38263ee5b4ae981 [file] [log] [blame]
Tom Rini10e47792018-05-06 17:58:06 -04001/* SPDX-License-Identifier: GPL-2.0+ */
Joe Hershberger3a77be52015-05-20 14:27:27 -05002/*
3 * Copyright (c) 2013 Google, Inc.
Joe Hershberger3a77be52015-05-20 14:27:27 -05004 */
5
6#ifndef __TEST_TEST_H
7#define __TEST_TEST_H
8
9#include <malloc.h>
Simon Glass974dccd2020-07-28 19:41:12 -060010#include <linux/bitops.h>
Joe Hershberger3a77be52015-05-20 14:27:27 -050011
Simon Glassef69dbe2025-01-20 14:25:59 -070012/**
13 * struct ut_stats - Statistics about tests run
Joe Hershberger3a77be52015-05-20 14:27:27 -050014 *
15 * @fail_count: Number of tests that failed
Simon Glass816fe6c2022-10-20 18:22:48 -060016 * @skip_count: Number of tests that were skipped
Simon Glass4f621e52025-01-20 14:26:00 -070017 * @test_count: Number of tests run. If a test is run muiltiple times, only one
18 * is counted
Simon Glass5fa92d62025-02-07 11:30:35 -070019 * @start: Timer value when test started
20 * @duration_ms: Suite duration in milliseconds
Simon Glassef69dbe2025-01-20 14:25:59 -070021 */
22struct ut_stats {
23 int fail_count;
24 int skip_count;
Simon Glass4f621e52025-01-20 14:26:00 -070025 int test_count;
Simon Glass5fa92d62025-02-07 11:30:35 -070026 ulong start;
27 ulong duration_ms;
Simon Glassef69dbe2025-01-20 14:25:59 -070028};
29
30/*
31 * struct unit_test_state - Entire state of test system
32 *
33 * @cur: Statistics for the current run
Simon Glassfb82a042025-01-20 14:26:02 -070034 * @total: Statistics for all test runs
35 * @run_count: Number of times ut_run_list() has been called
Joe Hershberger3a77be52015-05-20 14:27:27 -050036 * @start: Store the starting mallinfo when doing leak test
Simon Glassb2890a12021-03-07 17:34:56 -070037 * @of_live: true to use livetree if available, false to use flattree
Simon Glass017886b2017-05-18 20:09:17 -060038 * @of_root: Record of the livetree root node (used for setting up tests)
Simon Glassb98bfbc2021-03-07 17:34:57 -070039 * @root: Root device
40 * @testdev: Test device
41 * @force_fail_alloc: Force all memory allocs to fail
42 * @skip_post_probe: Skip uclass post-probe processing
Simon Glass3ba76752022-09-06 20:27:05 -060043 * @fdt_chksum: crc8 of the device tree contents
44 * @fdt_copy: Copy of the device tree
45 * @fdt_size: Size of the device-tree copy
Simon Glass1a92f832024-08-22 07:57:48 -060046 * @other_fdt: Buffer for the other FDT (UTF_OTHER_FDT)
47 * @other_fdt_size: Size of the other FDT (UTF_OTHER_FDT)
Simon Glassb9958072022-09-06 20:27:11 -060048 * @of_other: Live tree for the other FDT
Simon Glass91a187b2022-08-01 07:58:45 -060049 * @runs_per_test: Number of times to run each test (typically 1)
Simon Glass1a92f832024-08-22 07:57:48 -060050 * @force_run: true to run tests marked with the UTF_MANUAL flag
Simon Glassbe277ee2024-10-28 13:47:53 +010051 * @old_bloblist: stores the old gd->bloblist pointer
Simon Glassd856e912020-01-27 08:49:56 -070052 * @expect_str: Temporary string used to hold expected string value
53 * @actual_str: Temporary string used to hold actual string value
Joe Hershberger3a77be52015-05-20 14:27:27 -050054 */
55struct unit_test_state {
Simon Glassef69dbe2025-01-20 14:25:59 -070056 struct ut_stats cur;
Simon Glassfb82a042025-01-20 14:26:02 -070057 struct ut_stats total;
58 int run_count;
Joe Hershberger3a77be52015-05-20 14:27:27 -050059 struct mallinfo start;
Simon Glass017886b2017-05-18 20:09:17 -060060 struct device_node *of_root;
Simon Glassb2890a12021-03-07 17:34:56 -070061 bool of_live;
Simon Glassb98bfbc2021-03-07 17:34:57 -070062 struct udevice *root;
63 struct udevice *testdev;
64 int force_fail_alloc;
65 int skip_post_probe;
Simon Glass3ba76752022-09-06 20:27:05 -060066 uint fdt_chksum;
67 void *fdt_copy;
68 uint fdt_size;
Simon Glass57b00a92022-09-06 20:27:10 -060069 void *other_fdt;
70 int other_fdt_size;
Simon Glassb9958072022-09-06 20:27:11 -060071 struct device_node *of_other;
Simon Glass91a187b2022-08-01 07:58:45 -060072 int runs_per_test;
Simon Glass1f1614b2022-10-20 18:22:50 -060073 bool force_run;
Simon Glassbe277ee2024-10-28 13:47:53 +010074 void *old_bloblist;
Simon Glass4a249ba2021-05-08 06:59:59 -060075 char expect_str[512];
76 char actual_str[512];
Joe Hershberger3a77be52015-05-20 14:27:27 -050077};
78
Simon Glass974dccd2020-07-28 19:41:12 -060079/* Test flags for each test */
Simon Glass1a92f832024-08-22 07:57:48 -060080enum ut_flags {
81 UTF_SCAN_PDATA = BIT(0), /* test needs platform data */
82 UTF_PROBE_TEST = BIT(1), /* probe test uclass */
83 UTF_SCAN_FDT = BIT(2), /* scan device tree */
84 UTF_FLAT_TREE = BIT(3), /* test needs flat DT */
85 UTF_LIVE_TREE = BIT(4), /* needs live device tree */
Simon Glass11fcfa32024-08-22 07:57:50 -060086 UTF_CONSOLE = BIT(5), /* needs console recording */
Simon Glassab1fca02021-03-07 17:34:45 -070087 /* do extra driver model init and uninit */
Simon Glass1a92f832024-08-22 07:57:48 -060088 UTF_DM = BIT(6),
89 UTF_OTHER_FDT = BIT(7), /* read in other device tree */
Simon Glass1f1614b2022-10-20 18:22:50 -060090 /*
91 * Only run if explicitly requested with 'ut -f <suite> <test>'. The
92 * test name must end in "_norun" so that pytest detects this also,
93 * since it cannot access the flags.
94 */
Simon Glass1a92f832024-08-22 07:57:48 -060095 UTF_MANUAL = BIT(8),
96 UTF_ETH_BOOTDEV = BIT(9), /* enable Ethernet bootdevs */
97 UTF_SF_BOOTDEV = BIT(10), /* enable SPI flash bootdevs */
Simon Glassbe277ee2024-10-28 13:47:53 +010098 UFT_BLOBLIST = BIT(11), /* test changes gd->bloblist */
Simon Glass974dccd2020-07-28 19:41:12 -060099};
100
Joe Hershberger3a77be52015-05-20 14:27:27 -0500101/**
102 * struct unit_test - Information about a unit test
103 *
104 * @name: Name of test
105 * @func: Function to call to perform test
106 * @flags: Flags indicated pre-conditions for test
107 */
108struct unit_test {
Simon Glassbe408ee2017-05-18 20:09:15 -0600109 const char *file;
Joe Hershberger3a77be52015-05-20 14:27:27 -0500110 const char *name;
111 int (*func)(struct unit_test_state *state);
112 int flags;
113};
114
Heinrich Schuchardtdf6c36c2020-05-06 18:26:07 +0200115/**
116 * UNIT_TEST() - create linker generated list entry for unit a unit test
117 *
118 * The macro UNIT_TEST() is used to create a linker generated list entry. These
119 * list entries are enumerate tests that can be execute using the ut command.
120 * The list entries are used both by the implementation of the ut command as
121 * well as in a related Python test.
122 *
123 * For Python testing the subtests are collected in Python function
124 * generate_ut_subtest() by applying a regular expression to the lines of file
125 * u-boot.sym. The list entries have to follow strict naming conventions to be
126 * matched by the expression.
127 *
128 * Use UNIT_TEST(foo_test_bar, _flags, foo_test) for a test bar in test suite
129 * foo that can be executed via command 'ut foo bar' and is implemented in
130 * function foo_test_bar().
131 *
132 * @_name: concatenation of name of the test suite, "_test_", and the name
133 * of the test
134 * @_flags: an integer field that can be evaluated by the test suite
Simon Glass1a92f832024-08-22 07:57:48 -0600135 * implementation (see enum ut_flags)
Heinrich Schuchardtdf6c36c2020-05-06 18:26:07 +0200136 * @_suite: name of the test suite concatenated with "_test"
137 */
Joe Hershberger3a77be52015-05-20 14:27:27 -0500138#define UNIT_TEST(_name, _flags, _suite) \
Simon Glass16386932021-03-07 17:35:11 -0700139 ll_entry_declare(struct unit_test, _name, ut_ ## _suite) = { \
Simon Glassbe408ee2017-05-18 20:09:15 -0600140 .file = __FILE__, \
Joe Hershberger3a77be52015-05-20 14:27:27 -0500141 .name = #_name, \
142 .flags = _flags, \
143 .func = _name, \
144 }
145
Simon Glass16386932021-03-07 17:35:11 -0700146/* Get the start of a list of unit tests for a particular suite */
Simon Glassb50211f2021-03-07 17:35:10 -0700147#define UNIT_TEST_SUITE_START(_suite) \
Simon Glass16386932021-03-07 17:35:11 -0700148 ll_entry_start(struct unit_test, ut_ ## _suite)
Simon Glassb50211f2021-03-07 17:35:10 -0700149#define UNIT_TEST_SUITE_COUNT(_suite) \
Simon Glass16386932021-03-07 17:35:11 -0700150 ll_entry_count(struct unit_test, ut_ ## _suite)
Simon Glassb50211f2021-03-07 17:35:10 -0700151
Simon Glass1ef74ab2021-03-07 17:35:12 -0700152/* Use ! and ~ so that all tests will be sorted between these two values */
153#define UNIT_TEST_ALL_START() ll_entry_start(struct unit_test, ut_!)
154#define UNIT_TEST_ALL_END() ll_entry_start(struct unit_test, ut_~)
155#define UNIT_TEST_ALL_COUNT() (UNIT_TEST_ALL_END() - UNIT_TEST_ALL_START())
156
Simon Glass204675c2019-12-29 21:19:25 -0700157/* Sizes for devres tests */
158enum {
159 TEST_DEVRES_SIZE = 100,
160 TEST_DEVRES_COUNT = 10,
161 TEST_DEVRES_TOTAL = TEST_DEVRES_SIZE * TEST_DEVRES_COUNT,
162
Simon Glass0fe15372019-12-29 21:19:28 -0700163 /* A few different sizes */
Simon Glass204675c2019-12-29 21:19:25 -0700164 TEST_DEVRES_SIZE2 = 15,
Simon Glass0fe15372019-12-29 21:19:28 -0700165 TEST_DEVRES_SIZE3 = 37,
Simon Glass204675c2019-12-29 21:19:25 -0700166};
Joe Hershberger3a77be52015-05-20 14:27:27 -0500167
Simon Glassa4e289b2020-10-25 20:38:28 -0600168/**
Simon Glass4bf89722020-12-23 08:11:18 -0700169 * testbus_get_clear_removed() - Test function to obtain removed device
170 *
171 * This is used in testbus to find out which device was removed. Calling this
172 * function returns a pointer to the device and then clears it back to NULL, so
173 * that a future test can check it.
174 */
175struct udevice *testbus_get_clear_removed(void);
176
Simon Glass45b807d2021-03-25 10:44:33 +1300177#ifdef CONFIG_SANDBOX
178#include <asm/state.h>
Simon Glass57b00a92022-09-06 20:27:10 -0600179#include <asm/test.h>
180#endif
Simon Glass45b807d2021-03-25 10:44:33 +1300181
Simon Glass57b00a92022-09-06 20:27:10 -0600182static inline void arch_reset_for_test(void)
183{
184#ifdef CONFIG_SANDBOX
Simon Glass45b807d2021-03-25 10:44:33 +1300185 state_reset_for_test(state_get_current());
186#endif
Simon Glass57b00a92022-09-06 20:27:10 -0600187}
188static inline int test_load_other_fdt(struct unit_test_state *uts)
189{
190 int ret = 0;
191#ifdef CONFIG_SANDBOX
192 ret = sandbox_load_other_fdt(&uts->other_fdt, &uts->other_fdt_size);
193#endif
194 return ret;
Simon Glass45b807d2021-03-25 10:44:33 +1300195}
196
Simon Glass66014cc2023-01-17 10:47:27 -0700197/**
Simon Glassffc49482023-01-17 10:47:36 -0700198 * Control skipping of time delays
199 *
200 * Some tests have unnecessay time delays (e.g. USB). Allow these to be
201 * skipped to speed up testing
202 *
203 * @param skip_delays true to skip delays from now on, false to honour delay
204 * requests
205 */
206static inline void test_set_skip_delays(bool skip_delays)
207{
208#ifdef CONFIG_SANDBOX
209 state_set_skip_delays(skip_delays);
210#endif
211}
212
213/**
Simon Glass66014cc2023-01-17 10:47:27 -0700214 * test_set_eth_enable() - Enable / disable Ethernet
215 *
216 * Allows control of whether Ethernet packets are actually send/received
217 *
218 * @enable: true to enable Ethernet, false to disable
219 */
220static inline void test_set_eth_enable(bool enable)
221{
222#ifdef CONFIG_SANDBOX
223 sandbox_set_eth_enable(enable);
224#endif
225}
226
227/* Allow ethernet to be disabled for testing purposes */
228static inline bool test_eth_enabled(void)
229{
230 bool enabled = true;
231
232#ifdef CONFIG_SANDBOX
233 enabled = sandbox_eth_enabled();
234#endif
235 return enabled;
236}
237
Simon Glassb490f5f2023-01-17 10:47:28 -0700238/* Allow ethernet bootdev to be ignored for testing purposes */
239static inline bool test_eth_bootdev_enabled(void)
240{
241 bool enabled = true;
242
243#ifdef CONFIG_SANDBOX
244 enabled = sandbox_eth_enabled();
245#endif
246 return enabled;
247}
248
Simon Glass4937be02023-01-17 10:48:02 -0700249/* Allow SPI flash bootdev to be ignored for testing purposes */
250static inline bool test_sf_bootdev_enabled(void)
251{
252 bool enabled = true;
253
254#ifdef CONFIG_SANDBOX
255 enabled = sandbox_sf_bootdev_enabled();
256#endif
257 return enabled;
258}
259
260static inline void test_sf_set_enable_bootdevs(bool enable)
261{
262#ifdef CONFIG_SANDBOX
263 sandbox_sf_set_enable_bootdevs(enable);
264#endif
265}
266
Joe Hershberger3a77be52015-05-20 14:27:27 -0500267#endif /* __TEST_TEST_H */