1 /* SPDX-License-Identifier: GPL-2.0+ */
3 * Copyright (c) 2013 Google, Inc.
10 #include <linux/bitops.h>
13 * struct unit_test_state - Entire state of test system
15 * @fail_count: Number of tests that failed
16 * @skip_count: Number of tests that were skipped
17 * @start: Store the starting mallinfo when doing leak test
18 * @of_live: true to use livetree if available, false to use flattree
19 * @of_root: Record of the livetree root node (used for setting up tests)
21 * @testdev: Test device
22 * @force_fail_alloc: Force all memory allocs to fail
23 * @skip_post_probe: Skip uclass post-probe processing
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
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)
29 * @of_other: Live tree for the other FDT
30 * @runs_per_test: Number of times to run each test (typically 1)
31 * @force_run: true to run tests marked with the UT_TESTF_MANUAL flag
32 * @expect_str: Temporary string used to hold expected string value
33 * @actual_str: Temporary string used to hold actual string value
35 struct unit_test_state {
38 struct mallinfo start;
39 struct device_node *of_root;
42 struct udevice *testdev;
50 struct device_node *of_other;
57 /* Test flags for each test */
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 */
64 UT_TESTF_CONSOLE_REC = BIT(5), /* needs console recording */
65 /* do extra driver model init and uninit */
67 UT_TESTF_OTHER_FDT = BIT(7), /* read in other device tree */
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.
73 UT_TESTF_MANUAL = BIT(8),
74 UT_TESTF_ETH_BOOTDEV = BIT(9), /* enable Ethernet bootdevs */
78 * struct unit_test - Information about a unit test
81 * @func: Function to call to perform test
82 * @flags: Flags indicated pre-conditions for test
87 int (*func)(struct unit_test_state *state);
92 * UNIT_TEST() - create linker generated list entry for unit a unit test
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.
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.
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().
108 * @_name: concatenation of name of the test suite, "_test_", and the name
110 * @_flags: an integer field that can be evaluated by the test suite
112 * @_suite: name of the test suite concatenated with "_test"
114 #define UNIT_TEST(_name, _flags, _suite) \
115 ll_entry_declare(struct unit_test, _name, ut_ ## _suite) = { \
122 /* Get the start of a list of unit tests for a particular suite */
123 #define UNIT_TEST_SUITE_START(_suite) \
124 ll_entry_start(struct unit_test, ut_ ## _suite)
125 #define UNIT_TEST_SUITE_COUNT(_suite) \
126 ll_entry_count(struct unit_test, ut_ ## _suite)
128 /* 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())
133 /* Sizes for devres tests */
135 TEST_DEVRES_SIZE = 100,
136 TEST_DEVRES_COUNT = 10,
137 TEST_DEVRES_TOTAL = TEST_DEVRES_SIZE * TEST_DEVRES_COUNT,
139 /* A few different sizes */
140 TEST_DEVRES_SIZE2 = 15,
141 TEST_DEVRES_SIZE3 = 37,
145 * testbus_get_clear_removed() - Test function to obtain removed device
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.
151 struct udevice *testbus_get_clear_removed(void);
153 #ifdef CONFIG_SANDBOX
154 #include <asm/state.h>
155 #include <asm/test.h>
158 static inline void arch_reset_for_test(void)
160 #ifdef CONFIG_SANDBOX
161 state_reset_for_test(state_get_current());
164 static inline int test_load_other_fdt(struct unit_test_state *uts)
167 #ifdef CONFIG_SANDBOX
168 ret = sandbox_load_other_fdt(&uts->other_fdt, &uts->other_fdt_size);
174 * test_set_eth_enable() - Enable / disable Ethernet
176 * Allows control of whether Ethernet packets are actually send/received
178 * @enable: true to enable Ethernet, false to disable
180 static inline void test_set_eth_enable(bool enable)
182 #ifdef CONFIG_SANDBOX
183 sandbox_set_eth_enable(enable);
187 /* Allow ethernet to be disabled for testing purposes */
188 static inline bool test_eth_enabled(void)
192 #ifdef CONFIG_SANDBOX
193 enabled = sandbox_eth_enabled();
198 /* Allow ethernet bootdev to be ignored for testing purposes */
199 static inline bool test_eth_bootdev_enabled(void)
203 #ifdef CONFIG_SANDBOX
204 enabled = sandbox_eth_enabled();
209 #endif /* __TEST_TEST_H */