blob: bc8f0bbe501eaadf48422fbb63277b173526b030 [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 Glassef69dbe2025-01-20 14:25:59 -070017 */
18struct ut_stats {
19 int fail_count;
20 int skip_count;
21};
22
23/*
24 * struct unit_test_state - Entire state of test system
25 *
26 * @cur: Statistics for the current run
Joe Hershberger3a77be52015-05-20 14:27:27 -050027 * @start: Store the starting mallinfo when doing leak test
Simon Glassb2890a12021-03-07 17:34:56 -070028 * @of_live: true to use livetree if available, false to use flattree
Simon Glass017886b2017-05-18 20:09:17 -060029 * @of_root: Record of the livetree root node (used for setting up tests)
Simon Glassb98bfbc2021-03-07 17:34:57 -070030 * @root: Root device
31 * @testdev: Test device
32 * @force_fail_alloc: Force all memory allocs to fail
33 * @skip_post_probe: Skip uclass post-probe processing
Simon Glass3ba76752022-09-06 20:27:05 -060034 * @fdt_chksum: crc8 of the device tree contents
35 * @fdt_copy: Copy of the device tree
36 * @fdt_size: Size of the device-tree copy
Simon Glass1a92f832024-08-22 07:57:48 -060037 * @other_fdt: Buffer for the other FDT (UTF_OTHER_FDT)
38 * @other_fdt_size: Size of the other FDT (UTF_OTHER_FDT)
Simon Glassb9958072022-09-06 20:27:11 -060039 * @of_other: Live tree for the other FDT
Simon Glass91a187b2022-08-01 07:58:45 -060040 * @runs_per_test: Number of times to run each test (typically 1)
Simon Glass1a92f832024-08-22 07:57:48 -060041 * @force_run: true to run tests marked with the UTF_MANUAL flag
Simon Glassbe277ee2024-10-28 13:47:53 +010042 * @old_bloblist: stores the old gd->bloblist pointer
Simon Glassd856e912020-01-27 08:49:56 -070043 * @expect_str: Temporary string used to hold expected string value
44 * @actual_str: Temporary string used to hold actual string value
Joe Hershberger3a77be52015-05-20 14:27:27 -050045 */
46struct unit_test_state {
Simon Glassef69dbe2025-01-20 14:25:59 -070047 struct ut_stats cur;
Joe Hershberger3a77be52015-05-20 14:27:27 -050048 struct mallinfo start;
Simon Glass017886b2017-05-18 20:09:17 -060049 struct device_node *of_root;
Simon Glassb2890a12021-03-07 17:34:56 -070050 bool of_live;
Simon Glassb98bfbc2021-03-07 17:34:57 -070051 struct udevice *root;
52 struct udevice *testdev;
53 int force_fail_alloc;
54 int skip_post_probe;
Simon Glass3ba76752022-09-06 20:27:05 -060055 uint fdt_chksum;
56 void *fdt_copy;
57 uint fdt_size;
Simon Glass57b00a92022-09-06 20:27:10 -060058 void *other_fdt;
59 int other_fdt_size;
Simon Glassb9958072022-09-06 20:27:11 -060060 struct device_node *of_other;
Simon Glass91a187b2022-08-01 07:58:45 -060061 int runs_per_test;
Simon Glass1f1614b2022-10-20 18:22:50 -060062 bool force_run;
Simon Glassbe277ee2024-10-28 13:47:53 +010063 void *old_bloblist;
Simon Glass4a249ba2021-05-08 06:59:59 -060064 char expect_str[512];
65 char actual_str[512];
Joe Hershberger3a77be52015-05-20 14:27:27 -050066};
67
Simon Glass974dccd2020-07-28 19:41:12 -060068/* Test flags for each test */
Simon Glass1a92f832024-08-22 07:57:48 -060069enum ut_flags {
70 UTF_SCAN_PDATA = BIT(0), /* test needs platform data */
71 UTF_PROBE_TEST = BIT(1), /* probe test uclass */
72 UTF_SCAN_FDT = BIT(2), /* scan device tree */
73 UTF_FLAT_TREE = BIT(3), /* test needs flat DT */
74 UTF_LIVE_TREE = BIT(4), /* needs live device tree */
Simon Glass11fcfa32024-08-22 07:57:50 -060075 UTF_CONSOLE = BIT(5), /* needs console recording */
Simon Glassab1fca02021-03-07 17:34:45 -070076 /* do extra driver model init and uninit */
Simon Glass1a92f832024-08-22 07:57:48 -060077 UTF_DM = BIT(6),
78 UTF_OTHER_FDT = BIT(7), /* read in other device tree */
Simon Glass1f1614b2022-10-20 18:22:50 -060079 /*
80 * Only run if explicitly requested with 'ut -f <suite> <test>'. The
81 * test name must end in "_norun" so that pytest detects this also,
82 * since it cannot access the flags.
83 */
Simon Glass1a92f832024-08-22 07:57:48 -060084 UTF_MANUAL = BIT(8),
85 UTF_ETH_BOOTDEV = BIT(9), /* enable Ethernet bootdevs */
86 UTF_SF_BOOTDEV = BIT(10), /* enable SPI flash bootdevs */
Simon Glassbe277ee2024-10-28 13:47:53 +010087 UFT_BLOBLIST = BIT(11), /* test changes gd->bloblist */
Simon Glass974dccd2020-07-28 19:41:12 -060088};
89
Joe Hershberger3a77be52015-05-20 14:27:27 -050090/**
91 * struct unit_test - Information about a unit test
92 *
93 * @name: Name of test
94 * @func: Function to call to perform test
95 * @flags: Flags indicated pre-conditions for test
96 */
97struct unit_test {
Simon Glassbe408ee2017-05-18 20:09:15 -060098 const char *file;
Joe Hershberger3a77be52015-05-20 14:27:27 -050099 const char *name;
100 int (*func)(struct unit_test_state *state);
101 int flags;
102};
103
Heinrich Schuchardtdf6c36c2020-05-06 18:26:07 +0200104/**
105 * UNIT_TEST() - create linker generated list entry for unit a unit test
106 *
107 * The macro UNIT_TEST() is used to create a linker generated list entry. These
108 * list entries are enumerate tests that can be execute using the ut command.
109 * The list entries are used both by the implementation of the ut command as
110 * well as in a related Python test.
111 *
112 * For Python testing the subtests are collected in Python function
113 * generate_ut_subtest() by applying a regular expression to the lines of file
114 * u-boot.sym. The list entries have to follow strict naming conventions to be
115 * matched by the expression.
116 *
117 * Use UNIT_TEST(foo_test_bar, _flags, foo_test) for a test bar in test suite
118 * foo that can be executed via command 'ut foo bar' and is implemented in
119 * function foo_test_bar().
120 *
121 * @_name: concatenation of name of the test suite, "_test_", and the name
122 * of the test
123 * @_flags: an integer field that can be evaluated by the test suite
Simon Glass1a92f832024-08-22 07:57:48 -0600124 * implementation (see enum ut_flags)
Heinrich Schuchardtdf6c36c2020-05-06 18:26:07 +0200125 * @_suite: name of the test suite concatenated with "_test"
126 */
Joe Hershberger3a77be52015-05-20 14:27:27 -0500127#define UNIT_TEST(_name, _flags, _suite) \
Simon Glass16386932021-03-07 17:35:11 -0700128 ll_entry_declare(struct unit_test, _name, ut_ ## _suite) = { \
Simon Glassbe408ee2017-05-18 20:09:15 -0600129 .file = __FILE__, \
Joe Hershberger3a77be52015-05-20 14:27:27 -0500130 .name = #_name, \
131 .flags = _flags, \
132 .func = _name, \
133 }
134
Simon Glass16386932021-03-07 17:35:11 -0700135/* Get the start of a list of unit tests for a particular suite */
Simon Glassb50211f2021-03-07 17:35:10 -0700136#define UNIT_TEST_SUITE_START(_suite) \
Simon Glass16386932021-03-07 17:35:11 -0700137 ll_entry_start(struct unit_test, ut_ ## _suite)
Simon Glassb50211f2021-03-07 17:35:10 -0700138#define UNIT_TEST_SUITE_COUNT(_suite) \
Simon Glass16386932021-03-07 17:35:11 -0700139 ll_entry_count(struct unit_test, ut_ ## _suite)
Simon Glassb50211f2021-03-07 17:35:10 -0700140
Simon Glass1ef74ab2021-03-07 17:35:12 -0700141/* Use ! and ~ so that all tests will be sorted between these two values */
142#define UNIT_TEST_ALL_START() ll_entry_start(struct unit_test, ut_!)
143#define UNIT_TEST_ALL_END() ll_entry_start(struct unit_test, ut_~)
144#define UNIT_TEST_ALL_COUNT() (UNIT_TEST_ALL_END() - UNIT_TEST_ALL_START())
145
Simon Glass204675c2019-12-29 21:19:25 -0700146/* Sizes for devres tests */
147enum {
148 TEST_DEVRES_SIZE = 100,
149 TEST_DEVRES_COUNT = 10,
150 TEST_DEVRES_TOTAL = TEST_DEVRES_SIZE * TEST_DEVRES_COUNT,
151
Simon Glass0fe15372019-12-29 21:19:28 -0700152 /* A few different sizes */
Simon Glass204675c2019-12-29 21:19:25 -0700153 TEST_DEVRES_SIZE2 = 15,
Simon Glass0fe15372019-12-29 21:19:28 -0700154 TEST_DEVRES_SIZE3 = 37,
Simon Glass204675c2019-12-29 21:19:25 -0700155};
Joe Hershberger3a77be52015-05-20 14:27:27 -0500156
Simon Glassa4e289b2020-10-25 20:38:28 -0600157/**
Simon Glass4bf89722020-12-23 08:11:18 -0700158 * testbus_get_clear_removed() - Test function to obtain removed device
159 *
160 * This is used in testbus to find out which device was removed. Calling this
161 * function returns a pointer to the device and then clears it back to NULL, so
162 * that a future test can check it.
163 */
164struct udevice *testbus_get_clear_removed(void);
165
Simon Glass45b807d2021-03-25 10:44:33 +1300166#ifdef CONFIG_SANDBOX
167#include <asm/state.h>
Simon Glass57b00a92022-09-06 20:27:10 -0600168#include <asm/test.h>
169#endif
Simon Glass45b807d2021-03-25 10:44:33 +1300170
Simon Glass57b00a92022-09-06 20:27:10 -0600171static inline void arch_reset_for_test(void)
172{
173#ifdef CONFIG_SANDBOX
Simon Glass45b807d2021-03-25 10:44:33 +1300174 state_reset_for_test(state_get_current());
175#endif
Simon Glass57b00a92022-09-06 20:27:10 -0600176}
177static inline int test_load_other_fdt(struct unit_test_state *uts)
178{
179 int ret = 0;
180#ifdef CONFIG_SANDBOX
181 ret = sandbox_load_other_fdt(&uts->other_fdt, &uts->other_fdt_size);
182#endif
183 return ret;
Simon Glass45b807d2021-03-25 10:44:33 +1300184}
185
Simon Glass66014cc2023-01-17 10:47:27 -0700186/**
Simon Glassffc49482023-01-17 10:47:36 -0700187 * Control skipping of time delays
188 *
189 * Some tests have unnecessay time delays (e.g. USB). Allow these to be
190 * skipped to speed up testing
191 *
192 * @param skip_delays true to skip delays from now on, false to honour delay
193 * requests
194 */
195static inline void test_set_skip_delays(bool skip_delays)
196{
197#ifdef CONFIG_SANDBOX
198 state_set_skip_delays(skip_delays);
199#endif
200}
201
202/**
Simon Glass66014cc2023-01-17 10:47:27 -0700203 * test_set_eth_enable() - Enable / disable Ethernet
204 *
205 * Allows control of whether Ethernet packets are actually send/received
206 *
207 * @enable: true to enable Ethernet, false to disable
208 */
209static inline void test_set_eth_enable(bool enable)
210{
211#ifdef CONFIG_SANDBOX
212 sandbox_set_eth_enable(enable);
213#endif
214}
215
216/* Allow ethernet to be disabled for testing purposes */
217static inline bool test_eth_enabled(void)
218{
219 bool enabled = true;
220
221#ifdef CONFIG_SANDBOX
222 enabled = sandbox_eth_enabled();
223#endif
224 return enabled;
225}
226
Simon Glassb490f5f2023-01-17 10:47:28 -0700227/* Allow ethernet bootdev to be ignored for testing purposes */
228static inline bool test_eth_bootdev_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 Glass4937be02023-01-17 10:48:02 -0700238/* Allow SPI flash bootdev to be ignored for testing purposes */
239static inline bool test_sf_bootdev_enabled(void)
240{
241 bool enabled = true;
242
243#ifdef CONFIG_SANDBOX
244 enabled = sandbox_sf_bootdev_enabled();
245#endif
246 return enabled;
247}
248
249static inline void test_sf_set_enable_bootdevs(bool enable)
250{
251#ifdef CONFIG_SANDBOX
252 sandbox_sf_set_enable_bootdevs(enable);
253#endif
254}
255
Joe Hershberger3a77be52015-05-20 14:27:27 -0500256#endif /* __TEST_TEST_H */