blob: beabe9333dc0ba667b59b66f3de92587c5bef15f [file] [log] [blame]
Tom Rini83d290c2018-05-06 17:58:06 -04001/* SPDX-License-Identifier: GPL-2.0+ */
Joe Hershbergere721b882015-05-20 14:27:27 -05002/*
3 * Copyright (c) 2013 Google, Inc.
Joe Hershbergere721b882015-05-20 14:27:27 -05004 */
5
6#ifndef __TEST_TEST_H
7#define __TEST_TEST_H
8
9#include <malloc.h>
Simon Glasse180c2b2020-07-28 19:41:12 -060010#include <linux/bitops.h>
Joe Hershbergere721b882015-05-20 14:27:27 -050011
12/*
13 * struct unit_test_state - Entire state of test system
14 *
15 * @fail_count: Number of tests that failed
Simon Glass1facaad2022-10-20 18:22:48 -060016 * @skip_count: Number of tests that were skipped
Joe Hershbergere721b882015-05-20 14:27:27 -050017 * @start: Store the starting mallinfo when doing leak test
Simon Glass72b524c2021-03-07 17:34:56 -070018 * @of_live: true to use livetree if available, false to use flattree
Simon Glass6fb2f572017-05-18 20:09:17 -060019 * @of_root: Record of the livetree root node (used for setting up tests)
Simon Glass4a467c62021-03-07 17:34:57 -070020 * @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 Glass0e4b6972022-09-06 20:27:05 -060024 * @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 Glass756c0142022-09-06 20:27:10 -060027 * @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 Glass8d468a12022-09-06 20:27:11 -060029 * @of_other: Live tree for the other FDT
Simon Glassea94d052022-08-01 07:58:45 -060030 * @runs_per_test: Number of times to run each test (typically 1)
Simon Glasscbd71fa2022-10-20 18:22:50 -060031 * @force_run: true to run tests marked with the UT_TESTF_MANUAL flag
Simon Glass400175b2020-01-27 08:49:56 -070032 * @expect_str: Temporary string used to hold expected string value
33 * @actual_str: Temporary string used to hold actual string value
Joe Hershbergere721b882015-05-20 14:27:27 -050034 */
35struct unit_test_state {
36 int fail_count;
Simon Glass1facaad2022-10-20 18:22:48 -060037 int skip_count;
Joe Hershbergere721b882015-05-20 14:27:27 -050038 struct mallinfo start;
Simon Glass6fb2f572017-05-18 20:09:17 -060039 struct device_node *of_root;
Simon Glass72b524c2021-03-07 17:34:56 -070040 bool of_live;
Simon Glass4a467c62021-03-07 17:34:57 -070041 struct udevice *root;
42 struct udevice *testdev;
43 int force_fail_alloc;
44 int skip_post_probe;
Simon Glass0e4b6972022-09-06 20:27:05 -060045 uint fdt_chksum;
46 void *fdt_copy;
47 uint fdt_size;
Simon Glass756c0142022-09-06 20:27:10 -060048 void *other_fdt;
49 int other_fdt_size;
Simon Glass8d468a12022-09-06 20:27:11 -060050 struct device_node *of_other;
Simon Glassea94d052022-08-01 07:58:45 -060051 int runs_per_test;
Simon Glasscbd71fa2022-10-20 18:22:50 -060052 bool force_run;
Simon Glassc614ddf2021-05-08 06:59:59 -060053 char expect_str[512];
54 char actual_str[512];
Joe Hershbergere721b882015-05-20 14:27:27 -050055};
56
Simon Glasse180c2b2020-07-28 19:41:12 -060057/* Test flags for each test */
58enum {
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 Glass132644f2020-07-28 19:41:13 -060064 UT_TESTF_CONSOLE_REC = BIT(5), /* needs console recording */
Simon Glass4bc639e2021-03-07 17:34:45 -070065 /* do extra driver model init and uninit */
66 UT_TESTF_DM = BIT(6),
Simon Glass8d468a12022-09-06 20:27:11 -060067 UT_TESTF_OTHER_FDT = BIT(7), /* read in other device tree */
Simon Glasscbd71fa2022-10-20 18:22:50 -060068 /*
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 Glass70dd8862023-01-17 10:47:28 -070074 UT_TESTF_ETH_BOOTDEV = BIT(9), /* enable Ethernet bootdevs */
Simon Glasse180c2b2020-07-28 19:41:12 -060075};
76
Joe Hershbergere721b882015-05-20 14:27:27 -050077/**
78 * struct unit_test - Information about a unit test
79 *
80 * @name: Name of test
81 * @func: Function to call to perform test
82 * @flags: Flags indicated pre-conditions for test
83 */
84struct unit_test {
Simon Glass801587b2017-05-18 20:09:15 -060085 const char *file;
Joe Hershbergere721b882015-05-20 14:27:27 -050086 const char *name;
87 int (*func)(struct unit_test_state *state);
88 int flags;
89};
90
Heinrich Schuchardtd0ba0262020-05-06 18:26:07 +020091/**
92 * UNIT_TEST() - create linker generated list entry for unit a unit test
93 *
94 * The macro UNIT_TEST() is used to create a linker generated list entry. These
95 * list entries are enumerate tests that can be execute using the ut command.
96 * The list entries are used both by the implementation of the ut command as
97 * well as in a related Python test.
98 *
99 * For Python testing the subtests are collected in Python function
100 * generate_ut_subtest() by applying a regular expression to the lines of file
101 * u-boot.sym. The list entries have to follow strict naming conventions to be
102 * matched by the expression.
103 *
104 * Use UNIT_TEST(foo_test_bar, _flags, foo_test) for a test bar in test suite
105 * foo that can be executed via command 'ut foo bar' and is implemented in
106 * function foo_test_bar().
107 *
108 * @_name: concatenation of name of the test suite, "_test_", and the name
109 * of the test
110 * @_flags: an integer field that can be evaluated by the test suite
111 * implementation
112 * @_suite: name of the test suite concatenated with "_test"
113 */
Joe Hershbergere721b882015-05-20 14:27:27 -0500114#define UNIT_TEST(_name, _flags, _suite) \
Simon Glass2a2814d2021-03-07 17:35:11 -0700115 ll_entry_declare(struct unit_test, _name, ut_ ## _suite) = { \
Simon Glass801587b2017-05-18 20:09:15 -0600116 .file = __FILE__, \
Joe Hershbergere721b882015-05-20 14:27:27 -0500117 .name = #_name, \
118 .flags = _flags, \
119 .func = _name, \
120 }
121
Simon Glass2a2814d2021-03-07 17:35:11 -0700122/* Get the start of a list of unit tests for a particular suite */
Simon Glassa7a98752021-03-07 17:35:10 -0700123#define UNIT_TEST_SUITE_START(_suite) \
Simon Glass2a2814d2021-03-07 17:35:11 -0700124 ll_entry_start(struct unit_test, ut_ ## _suite)
Simon Glassa7a98752021-03-07 17:35:10 -0700125#define UNIT_TEST_SUITE_COUNT(_suite) \
Simon Glass2a2814d2021-03-07 17:35:11 -0700126 ll_entry_count(struct unit_test, ut_ ## _suite)
Simon Glassa7a98752021-03-07 17:35:10 -0700127
Simon Glass84823562021-03-07 17:35:12 -0700128/* Use ! and ~ so that all tests will be sorted between these two values */
129#define UNIT_TEST_ALL_START() ll_entry_start(struct unit_test, ut_!)
130#define UNIT_TEST_ALL_END() ll_entry_start(struct unit_test, ut_~)
131#define UNIT_TEST_ALL_COUNT() (UNIT_TEST_ALL_END() - UNIT_TEST_ALL_START())
132
Simon Glassdc12ebb2019-12-29 21:19:25 -0700133/* Sizes for devres tests */
134enum {
135 TEST_DEVRES_SIZE = 100,
136 TEST_DEVRES_COUNT = 10,
137 TEST_DEVRES_TOTAL = TEST_DEVRES_SIZE * TEST_DEVRES_COUNT,
138
Simon Glass42a0ce52019-12-29 21:19:28 -0700139 /* A few different sizes */
Simon Glassdc12ebb2019-12-29 21:19:25 -0700140 TEST_DEVRES_SIZE2 = 15,
Simon Glass42a0ce52019-12-29 21:19:28 -0700141 TEST_DEVRES_SIZE3 = 37,
Simon Glassdc12ebb2019-12-29 21:19:25 -0700142};
Joe Hershbergere721b882015-05-20 14:27:27 -0500143
Simon Glassb25ff5c2020-10-25 20:38:28 -0600144/**
Simon Glass079ac592020-12-23 08:11:18 -0700145 * testbus_get_clear_removed() - Test function to obtain removed device
146 *
147 * This is used in testbus to find out which device was removed. Calling this
148 * function returns a pointer to the device and then clears it back to NULL, so
149 * that a future test can check it.
150 */
151struct udevice *testbus_get_clear_removed(void);
152
Simon Glass756c0142022-09-06 20:27:10 -0600153#ifdef CONFIG_SANDBOX
154#include <asm/state.h>
155#include <asm/test.h>
156#endif
157
Simon Glassd4a15922021-03-25 10:44:33 +1300158static inline void arch_reset_for_test(void)
159{
160#ifdef CONFIG_SANDBOX
Simon Glassd4a15922021-03-25 10:44:33 +1300161 state_reset_for_test(state_get_current());
162#endif
163}
Simon Glass756c0142022-09-06 20:27:10 -0600164static inline int test_load_other_fdt(struct unit_test_state *uts)
165{
166 int ret = 0;
167#ifdef CONFIG_SANDBOX
168 ret = sandbox_load_other_fdt(&uts->other_fdt, &uts->other_fdt_size);
169#endif
170 return ret;
171}
Simon Glassd4a15922021-03-25 10:44:33 +1300172
Simon Glassf43b2df2023-01-17 10:47:27 -0700173/**
174 * test_set_eth_enable() - Enable / disable Ethernet
175 *
176 * Allows control of whether Ethernet packets are actually send/received
177 *
178 * @enable: true to enable Ethernet, false to disable
179 */
180static inline void test_set_eth_enable(bool enable)
181{
182#ifdef CONFIG_SANDBOX
183 sandbox_set_eth_enable(enable);
184#endif
185}
186
187/* Allow ethernet to be disabled for testing purposes */
188static inline bool test_eth_enabled(void)
189{
190 bool enabled = true;
191
192#ifdef CONFIG_SANDBOX
193 enabled = sandbox_eth_enabled();
194#endif
195 return enabled;
196}
197
Simon Glass70dd8862023-01-17 10:47:28 -0700198/* Allow ethernet bootdev to be ignored for testing purposes */
199static inline bool test_eth_bootdev_enabled(void)
200{
201 bool enabled = true;
202
203#ifdef CONFIG_SANDBOX
204 enabled = sandbox_eth_enabled();
205#endif
206 return enabled;
207}
208
Joe Hershbergere721b882015-05-20 14:27:27 -0500209#endif /* __TEST_TEST_H */