blob: c1853ce471b24b7f29dde5f24b077255fa14bbb0 [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
12/*
13 * struct unit_test_state - Entire state of test system
14 *
15 * @fail_count: Number of tests that failed
Simon Glass816fe6c2022-10-20 18:22:48 -060016 * @skip_count: Number of tests that were skipped
Joe Hershberger3a77be52015-05-20 14:27:27 -050017 * @start: Store the starting mallinfo when doing leak test
Simon Glassb2890a12021-03-07 17:34:56 -070018 * @of_live: true to use livetree if available, false to use flattree
Simon Glass017886b2017-05-18 20:09:17 -060019 * @of_root: Record of the livetree root node (used for setting up tests)
Simon Glassb98bfbc2021-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 Glass3ba76752022-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 Glass57b00a92022-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 Glassb9958072022-09-06 20:27:11 -060029 * @of_other: Live tree for the other FDT
Simon Glass91a187b2022-08-01 07:58:45 -060030 * @runs_per_test: Number of times to run each test (typically 1)
Simon Glassd856e912020-01-27 08:49:56 -070031 * @expect_str: Temporary string used to hold expected string value
32 * @actual_str: Temporary string used to hold actual string value
Joe Hershberger3a77be52015-05-20 14:27:27 -050033 */
34struct unit_test_state {
35 int fail_count;
Simon Glass816fe6c2022-10-20 18:22:48 -060036 int skip_count;
Joe Hershberger3a77be52015-05-20 14:27:27 -050037 struct mallinfo start;
Simon Glass017886b2017-05-18 20:09:17 -060038 struct device_node *of_root;
Simon Glassb2890a12021-03-07 17:34:56 -070039 bool of_live;
Simon Glassb98bfbc2021-03-07 17:34:57 -070040 struct udevice *root;
41 struct udevice *testdev;
42 int force_fail_alloc;
43 int skip_post_probe;
Simon Glass3ba76752022-09-06 20:27:05 -060044 uint fdt_chksum;
45 void *fdt_copy;
46 uint fdt_size;
Simon Glass57b00a92022-09-06 20:27:10 -060047 void *other_fdt;
48 int other_fdt_size;
Simon Glassb9958072022-09-06 20:27:11 -060049 struct device_node *of_other;
Simon Glass91a187b2022-08-01 07:58:45 -060050 int runs_per_test;
Simon Glass4a249ba2021-05-08 06:59:59 -060051 char expect_str[512];
52 char actual_str[512];
Joe Hershberger3a77be52015-05-20 14:27:27 -050053};
54
Simon Glass974dccd2020-07-28 19:41:12 -060055/* Test flags for each test */
56enum {
57 UT_TESTF_SCAN_PDATA = BIT(0), /* test needs platform data */
58 UT_TESTF_PROBE_TEST = BIT(1), /* probe test uclass */
59 UT_TESTF_SCAN_FDT = BIT(2), /* scan device tree */
60 UT_TESTF_FLAT_TREE = BIT(3), /* test needs flat DT */
61 UT_TESTF_LIVE_TREE = BIT(4), /* needs live device tree */
Simon Glass6db8ea52020-07-28 19:41:13 -060062 UT_TESTF_CONSOLE_REC = BIT(5), /* needs console recording */
Simon Glassab1fca02021-03-07 17:34:45 -070063 /* do extra driver model init and uninit */
64 UT_TESTF_DM = BIT(6),
Simon Glassb9958072022-09-06 20:27:11 -060065 UT_TESTF_OTHER_FDT = BIT(7), /* read in other device tree */
Simon Glass974dccd2020-07-28 19:41:12 -060066};
67
Joe Hershberger3a77be52015-05-20 14:27:27 -050068/**
69 * struct unit_test - Information about a unit test
70 *
71 * @name: Name of test
72 * @func: Function to call to perform test
73 * @flags: Flags indicated pre-conditions for test
74 */
75struct unit_test {
Simon Glassbe408ee2017-05-18 20:09:15 -060076 const char *file;
Joe Hershberger3a77be52015-05-20 14:27:27 -050077 const char *name;
78 int (*func)(struct unit_test_state *state);
79 int flags;
80};
81
Heinrich Schuchardtdf6c36c2020-05-06 18:26:07 +020082/**
83 * UNIT_TEST() - create linker generated list entry for unit a unit test
84 *
85 * The macro UNIT_TEST() is used to create a linker generated list entry. These
86 * list entries are enumerate tests that can be execute using the ut command.
87 * The list entries are used both by the implementation of the ut command as
88 * well as in a related Python test.
89 *
90 * For Python testing the subtests are collected in Python function
91 * generate_ut_subtest() by applying a regular expression to the lines of file
92 * u-boot.sym. The list entries have to follow strict naming conventions to be
93 * matched by the expression.
94 *
95 * Use UNIT_TEST(foo_test_bar, _flags, foo_test) for a test bar in test suite
96 * foo that can be executed via command 'ut foo bar' and is implemented in
97 * function foo_test_bar().
98 *
99 * @_name: concatenation of name of the test suite, "_test_", and the name
100 * of the test
101 * @_flags: an integer field that can be evaluated by the test suite
102 * implementation
103 * @_suite: name of the test suite concatenated with "_test"
104 */
Joe Hershberger3a77be52015-05-20 14:27:27 -0500105#define UNIT_TEST(_name, _flags, _suite) \
Simon Glass16386932021-03-07 17:35:11 -0700106 ll_entry_declare(struct unit_test, _name, ut_ ## _suite) = { \
Simon Glassbe408ee2017-05-18 20:09:15 -0600107 .file = __FILE__, \
Joe Hershberger3a77be52015-05-20 14:27:27 -0500108 .name = #_name, \
109 .flags = _flags, \
110 .func = _name, \
111 }
112
Simon Glass16386932021-03-07 17:35:11 -0700113/* Get the start of a list of unit tests for a particular suite */
Simon Glassb50211f2021-03-07 17:35:10 -0700114#define UNIT_TEST_SUITE_START(_suite) \
Simon Glass16386932021-03-07 17:35:11 -0700115 ll_entry_start(struct unit_test, ut_ ## _suite)
Simon Glassb50211f2021-03-07 17:35:10 -0700116#define UNIT_TEST_SUITE_COUNT(_suite) \
Simon Glass16386932021-03-07 17:35:11 -0700117 ll_entry_count(struct unit_test, ut_ ## _suite)
Simon Glassb50211f2021-03-07 17:35:10 -0700118
Simon Glass1ef74ab2021-03-07 17:35:12 -0700119/* Use ! and ~ so that all tests will be sorted between these two values */
120#define UNIT_TEST_ALL_START() ll_entry_start(struct unit_test, ut_!)
121#define UNIT_TEST_ALL_END() ll_entry_start(struct unit_test, ut_~)
122#define UNIT_TEST_ALL_COUNT() (UNIT_TEST_ALL_END() - UNIT_TEST_ALL_START())
123
Simon Glass204675c2019-12-29 21:19:25 -0700124/* Sizes for devres tests */
125enum {
126 TEST_DEVRES_SIZE = 100,
127 TEST_DEVRES_COUNT = 10,
128 TEST_DEVRES_TOTAL = TEST_DEVRES_SIZE * TEST_DEVRES_COUNT,
129
Simon Glass0fe15372019-12-29 21:19:28 -0700130 /* A few different sizes */
Simon Glass204675c2019-12-29 21:19:25 -0700131 TEST_DEVRES_SIZE2 = 15,
Simon Glass0fe15372019-12-29 21:19:28 -0700132 TEST_DEVRES_SIZE3 = 37,
Simon Glass204675c2019-12-29 21:19:25 -0700133};
Joe Hershberger3a77be52015-05-20 14:27:27 -0500134
Simon Glassa4e289b2020-10-25 20:38:28 -0600135/**
Simon Glass4bf89722020-12-23 08:11:18 -0700136 * testbus_get_clear_removed() - Test function to obtain removed device
137 *
138 * This is used in testbus to find out which device was removed. Calling this
139 * function returns a pointer to the device and then clears it back to NULL, so
140 * that a future test can check it.
141 */
142struct udevice *testbus_get_clear_removed(void);
143
Simon Glass45b807d2021-03-25 10:44:33 +1300144#ifdef CONFIG_SANDBOX
145#include <asm/state.h>
Simon Glass57b00a92022-09-06 20:27:10 -0600146#include <asm/test.h>
147#endif
Simon Glass45b807d2021-03-25 10:44:33 +1300148
Simon Glass57b00a92022-09-06 20:27:10 -0600149static inline void arch_reset_for_test(void)
150{
151#ifdef CONFIG_SANDBOX
Simon Glass45b807d2021-03-25 10:44:33 +1300152 state_reset_for_test(state_get_current());
153#endif
Simon Glass57b00a92022-09-06 20:27:10 -0600154}
155static inline int test_load_other_fdt(struct unit_test_state *uts)
156{
157 int ret = 0;
158#ifdef CONFIG_SANDBOX
159 ret = sandbox_load_other_fdt(&uts->other_fdt, &uts->other_fdt_size);
160#endif
161 return ret;
Simon Glass45b807d2021-03-25 10:44:33 +1300162}
163
Joe Hershberger3a77be52015-05-20 14:27:27 -0500164#endif /* __TEST_TEST_H */