1 /* SPDX-License-Identifier: GPL-2.0+ */
2 /*
3  * Copyright (c) 2013 Google, Inc.
4  */
5 
6 #ifndef __TEST_TEST_H
7 #define __TEST_TEST_H
8 
9 #include <malloc.h>
10 #include <linux/bitops.h>
11 
12 /*
13  * struct unit_test_state - Entire state of test system
14  *
15  * @fail_count: Number of tests that failed
16  * @start: Store the starting mallinfo when doing leak test
17  * @of_live: true to use livetree if available, false to use flattree
18  * @of_root: Record of the livetree root node (used for setting up tests)
19  * @root: Root device
20  * @testdev: Test device
21  * @force_fail_alloc: Force all memory allocs to fail
22  * @skip_post_probe: Skip uclass post-probe processing
23  * @expect_str: Temporary string used to hold expected string value
24  * @actual_str: Temporary string used to hold actual string value
25  */
26 struct unit_test_state {
27 	int fail_count;
28 	struct mallinfo start;
29 	struct device_node *of_root;
30 	bool of_live;
31 	struct udevice *root;
32 	struct udevice *testdev;
33 	int force_fail_alloc;
34 	int skip_post_probe;
35 	char expect_str[256];
36 	char actual_str[256];
37 };
38 
39 /* Test flags for each test */
40 enum {
41 	UT_TESTF_SCAN_PDATA	= BIT(0),	/* test needs platform data */
42 	UT_TESTF_PROBE_TEST	= BIT(1),	/* probe test uclass */
43 	UT_TESTF_SCAN_FDT	= BIT(2),	/* scan device tree */
44 	UT_TESTF_FLAT_TREE	= BIT(3),	/* test needs flat DT */
45 	UT_TESTF_LIVE_TREE	= BIT(4),	/* needs live device tree */
46 	UT_TESTF_CONSOLE_REC	= BIT(5),	/* needs console recording */
47 	/* do extra driver model init and uninit */
48 	UT_TESTF_DM		= BIT(6),
49 };
50 
51 /**
52  * struct unit_test - Information about a unit test
53  *
54  * @name: Name of test
55  * @func: Function to call to perform test
56  * @flags: Flags indicated pre-conditions for test
57  */
58 struct unit_test {
59 	const char *file;
60 	const char *name;
61 	int (*func)(struct unit_test_state *state);
62 	int flags;
63 };
64 
65 /**
66  * UNIT_TEST() - create linker generated list entry for unit a unit test
67  *
68  * The macro UNIT_TEST() is used to create a linker generated list entry. These
69  * list entries are enumerate tests that can be execute using the ut command.
70  * The list entries are used both by the implementation of the ut command as
71  * well as in a related Python test.
72  *
73  * For Python testing the subtests are collected in Python function
74  * generate_ut_subtest() by applying a regular expression to the lines of file
75  * u-boot.sym. The list entries have to follow strict naming conventions to be
76  * matched by the expression.
77  *
78  * Use UNIT_TEST(foo_test_bar, _flags, foo_test) for a test bar in test suite
79  * foo that can be executed via command 'ut foo bar' and is implemented in
80  * function foo_test_bar().
81  *
82  * @_name:	concatenation of name of the test suite, "_test_", and the name
83  *		of the test
84  * @_flags:	an integer field that can be evaluated by the test suite
85  *		implementation
86  * @_suite:	name of the test suite concatenated with "_test"
87  */
88 #define UNIT_TEST(_name, _flags, _suite)				\
89 	ll_entry_declare(struct unit_test, _name, ut_ ## _suite) = {	\
90 		.file = __FILE__,					\
91 		.name = #_name,						\
92 		.flags = _flags,					\
93 		.func = _name,						\
94 	}
95 
96 /* Get the start of a list of unit tests for a particular suite */
97 #define UNIT_TEST_SUITE_START(_suite) \
98 	ll_entry_start(struct unit_test, ut_ ## _suite)
99 #define UNIT_TEST_SUITE_COUNT(_suite) \
100 	ll_entry_count(struct unit_test, ut_ ## _suite)
101 
102 /* Use ! and ~ so that all tests will be sorted between these two values */
103 #define UNIT_TEST_ALL_START()	ll_entry_start(struct unit_test, ut_!)
104 #define UNIT_TEST_ALL_END()	ll_entry_start(struct unit_test, ut_~)
105 #define UNIT_TEST_ALL_COUNT()	(UNIT_TEST_ALL_END() - UNIT_TEST_ALL_START())
106 
107 /* Sizes for devres tests */
108 enum {
109 	TEST_DEVRES_SIZE	= 100,
110 	TEST_DEVRES_COUNT	= 10,
111 	TEST_DEVRES_TOTAL	= TEST_DEVRES_SIZE * TEST_DEVRES_COUNT,
112 
113 	/* A few different sizes */
114 	TEST_DEVRES_SIZE2	= 15,
115 	TEST_DEVRES_SIZE3	= 37,
116 };
117 
118 /**
119  * testbus_get_clear_removed() - Test function to obtain removed device
120  *
121  * This is used in testbus to find out which device was removed. Calling this
122  * function returns a pointer to the device and then clears it back to NULL, so
123  * that a future test can check it.
124  */
125 struct udevice *testbus_get_clear_removed(void);
126 
arch_reset_for_test(void)127 static inline void arch_reset_for_test(void)
128 {
129 #ifdef CONFIG_SANDBOX
130 #include <asm/state.h>
131 
132 	state_reset_for_test(state_get_current());
133 #endif
134 }
135 
136 #endif /* __TEST_TEST_H */
137