test: Make a copy of the device tree before running a test
[platform/kernel/u-boot.git] / include / test / test.h
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  * @fdt_chksum: crc8 of the device tree contents
24  * @fdt_copy: Copy of the device tree
25  * @fdt_size: Size of the device-tree copy
26  * @runs_per_test: Number of times to run each test (typically 1)
27  * @expect_str: Temporary string used to hold expected string value
28  * @actual_str: Temporary string used to hold actual string value
29  */
30 struct unit_test_state {
31         int fail_count;
32         struct mallinfo start;
33         struct device_node *of_root;
34         bool of_live;
35         struct udevice *root;
36         struct udevice *testdev;
37         int force_fail_alloc;
38         int skip_post_probe;
39         uint fdt_chksum;
40         void *fdt_copy;
41         uint fdt_size;
42         int runs_per_test;
43         char expect_str[512];
44         char actual_str[512];
45 };
46
47 /* Test flags for each test */
48 enum {
49         UT_TESTF_SCAN_PDATA     = BIT(0),       /* test needs platform data */
50         UT_TESTF_PROBE_TEST     = BIT(1),       /* probe test uclass */
51         UT_TESTF_SCAN_FDT       = BIT(2),       /* scan device tree */
52         UT_TESTF_FLAT_TREE      = BIT(3),       /* test needs flat DT */
53         UT_TESTF_LIVE_TREE      = BIT(4),       /* needs live device tree */
54         UT_TESTF_CONSOLE_REC    = BIT(5),       /* needs console recording */
55         /* do extra driver model init and uninit */
56         UT_TESTF_DM             = BIT(6),
57         /* live or flat device tree, but not both in the same executable */
58         UT_TESTF_LIVE_OR_FLAT   = BIT(7),
59 };
60
61 /**
62  * struct unit_test - Information about a unit test
63  *
64  * @name: Name of test
65  * @func: Function to call to perform test
66  * @flags: Flags indicated pre-conditions for test
67  */
68 struct unit_test {
69         const char *file;
70         const char *name;
71         int (*func)(struct unit_test_state *state);
72         int flags;
73 };
74
75 /**
76  * UNIT_TEST() - create linker generated list entry for unit a unit test
77  *
78  * The macro UNIT_TEST() is used to create a linker generated list entry. These
79  * list entries are enumerate tests that can be execute using the ut command.
80  * The list entries are used both by the implementation of the ut command as
81  * well as in a related Python test.
82  *
83  * For Python testing the subtests are collected in Python function
84  * generate_ut_subtest() by applying a regular expression to the lines of file
85  * u-boot.sym. The list entries have to follow strict naming conventions to be
86  * matched by the expression.
87  *
88  * Use UNIT_TEST(foo_test_bar, _flags, foo_test) for a test bar in test suite
89  * foo that can be executed via command 'ut foo bar' and is implemented in
90  * function foo_test_bar().
91  *
92  * @_name:      concatenation of name of the test suite, "_test_", and the name
93  *              of the test
94  * @_flags:     an integer field that can be evaluated by the test suite
95  *              implementation
96  * @_suite:     name of the test suite concatenated with "_test"
97  */
98 #define UNIT_TEST(_name, _flags, _suite)                                \
99         ll_entry_declare(struct unit_test, _name, ut_ ## _suite) = {    \
100                 .file = __FILE__,                                       \
101                 .name = #_name,                                         \
102                 .flags = _flags,                                        \
103                 .func = _name,                                          \
104         }
105
106 /* Get the start of a list of unit tests for a particular suite */
107 #define UNIT_TEST_SUITE_START(_suite) \
108         ll_entry_start(struct unit_test, ut_ ## _suite)
109 #define UNIT_TEST_SUITE_COUNT(_suite) \
110         ll_entry_count(struct unit_test, ut_ ## _suite)
111
112 /* Use ! and ~ so that all tests will be sorted between these two values */
113 #define UNIT_TEST_ALL_START()   ll_entry_start(struct unit_test, ut_!)
114 #define UNIT_TEST_ALL_END()     ll_entry_start(struct unit_test, ut_~)
115 #define UNIT_TEST_ALL_COUNT()   (UNIT_TEST_ALL_END() - UNIT_TEST_ALL_START())
116
117 /* Sizes for devres tests */
118 enum {
119         TEST_DEVRES_SIZE        = 100,
120         TEST_DEVRES_COUNT       = 10,
121         TEST_DEVRES_TOTAL       = TEST_DEVRES_SIZE * TEST_DEVRES_COUNT,
122
123         /* A few different sizes */
124         TEST_DEVRES_SIZE2       = 15,
125         TEST_DEVRES_SIZE3       = 37,
126 };
127
128 /**
129  * testbus_get_clear_removed() - Test function to obtain removed device
130  *
131  * This is used in testbus to find out which device was removed. Calling this
132  * function returns a pointer to the device and then clears it back to NULL, so
133  * that a future test can check it.
134  */
135 struct udevice *testbus_get_clear_removed(void);
136
137 static inline void arch_reset_for_test(void)
138 {
139 #ifdef CONFIG_SANDBOX
140 #include <asm/state.h>
141
142         state_reset_for_test(state_get_current());
143 #endif
144 }
145
146 #endif /* __TEST_TEST_H */