blob: 2b68331b5468fa5a21ca3d89f1eaff69c44d1273 [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
16 * @start: Store the starting mallinfo when doing leak test
Simon Glassb2890a12021-03-07 17:34:56 -070017 * @of_live: true to use livetree if available, false to use flattree
Simon Glass017886b2017-05-18 20:09:17 -060018 * @of_root: Record of the livetree root node (used for setting up tests)
Simon Glassb98bfbc2021-03-07 17:34:57 -070019 * @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
Simon Glass91a187b2022-08-01 07:58:45 -060023 * @runs_per_test: Number of times to run each test (typically 1)
Simon Glassd856e912020-01-27 08:49:56 -070024 * @expect_str: Temporary string used to hold expected string value
25 * @actual_str: Temporary string used to hold actual string value
Joe Hershberger3a77be52015-05-20 14:27:27 -050026 */
27struct unit_test_state {
28 int fail_count;
29 struct mallinfo start;
Simon Glass017886b2017-05-18 20:09:17 -060030 struct device_node *of_root;
Simon Glassb2890a12021-03-07 17:34:56 -070031 bool of_live;
Simon Glassb98bfbc2021-03-07 17:34:57 -070032 struct udevice *root;
33 struct udevice *testdev;
34 int force_fail_alloc;
35 int skip_post_probe;
Simon Glass91a187b2022-08-01 07:58:45 -060036 int runs_per_test;
Simon Glass4a249ba2021-05-08 06:59:59 -060037 char expect_str[512];
38 char actual_str[512];
Joe Hershberger3a77be52015-05-20 14:27:27 -050039};
40
Simon Glass974dccd2020-07-28 19:41:12 -060041/* Test flags for each test */
42enum {
43 UT_TESTF_SCAN_PDATA = BIT(0), /* test needs platform data */
44 UT_TESTF_PROBE_TEST = BIT(1), /* probe test uclass */
45 UT_TESTF_SCAN_FDT = BIT(2), /* scan device tree */
46 UT_TESTF_FLAT_TREE = BIT(3), /* test needs flat DT */
47 UT_TESTF_LIVE_TREE = BIT(4), /* needs live device tree */
Simon Glass6db8ea52020-07-28 19:41:13 -060048 UT_TESTF_CONSOLE_REC = BIT(5), /* needs console recording */
Simon Glassab1fca02021-03-07 17:34:45 -070049 /* do extra driver model init and uninit */
50 UT_TESTF_DM = BIT(6),
Simon Glass2f0cb8f2022-07-30 15:52:12 -060051 /* live or flat device tree, but not both in the same executable */
52 UT_TESTF_LIVE_OR_FLAT = BIT(4),
Simon Glass974dccd2020-07-28 19:41:12 -060053};
54
Joe Hershberger3a77be52015-05-20 14:27:27 -050055/**
56 * struct unit_test - Information about a unit test
57 *
58 * @name: Name of test
59 * @func: Function to call to perform test
60 * @flags: Flags indicated pre-conditions for test
61 */
62struct unit_test {
Simon Glassbe408ee2017-05-18 20:09:15 -060063 const char *file;
Joe Hershberger3a77be52015-05-20 14:27:27 -050064 const char *name;
65 int (*func)(struct unit_test_state *state);
66 int flags;
67};
68
Heinrich Schuchardtdf6c36c2020-05-06 18:26:07 +020069/**
70 * UNIT_TEST() - create linker generated list entry for unit a unit test
71 *
72 * The macro UNIT_TEST() is used to create a linker generated list entry. These
73 * list entries are enumerate tests that can be execute using the ut command.
74 * The list entries are used both by the implementation of the ut command as
75 * well as in a related Python test.
76 *
77 * For Python testing the subtests are collected in Python function
78 * generate_ut_subtest() by applying a regular expression to the lines of file
79 * u-boot.sym. The list entries have to follow strict naming conventions to be
80 * matched by the expression.
81 *
82 * Use UNIT_TEST(foo_test_bar, _flags, foo_test) for a test bar in test suite
83 * foo that can be executed via command 'ut foo bar' and is implemented in
84 * function foo_test_bar().
85 *
86 * @_name: concatenation of name of the test suite, "_test_", and the name
87 * of the test
88 * @_flags: an integer field that can be evaluated by the test suite
89 * implementation
90 * @_suite: name of the test suite concatenated with "_test"
91 */
Joe Hershberger3a77be52015-05-20 14:27:27 -050092#define UNIT_TEST(_name, _flags, _suite) \
Simon Glass16386932021-03-07 17:35:11 -070093 ll_entry_declare(struct unit_test, _name, ut_ ## _suite) = { \
Simon Glassbe408ee2017-05-18 20:09:15 -060094 .file = __FILE__, \
Joe Hershberger3a77be52015-05-20 14:27:27 -050095 .name = #_name, \
96 .flags = _flags, \
97 .func = _name, \
98 }
99
Simon Glass16386932021-03-07 17:35:11 -0700100/* Get the start of a list of unit tests for a particular suite */
Simon Glassb50211f2021-03-07 17:35:10 -0700101#define UNIT_TEST_SUITE_START(_suite) \
Simon Glass16386932021-03-07 17:35:11 -0700102 ll_entry_start(struct unit_test, ut_ ## _suite)
Simon Glassb50211f2021-03-07 17:35:10 -0700103#define UNIT_TEST_SUITE_COUNT(_suite) \
Simon Glass16386932021-03-07 17:35:11 -0700104 ll_entry_count(struct unit_test, ut_ ## _suite)
Simon Glassb50211f2021-03-07 17:35:10 -0700105
Simon Glass1ef74ab2021-03-07 17:35:12 -0700106/* Use ! and ~ so that all tests will be sorted between these two values */
107#define UNIT_TEST_ALL_START() ll_entry_start(struct unit_test, ut_!)
108#define UNIT_TEST_ALL_END() ll_entry_start(struct unit_test, ut_~)
109#define UNIT_TEST_ALL_COUNT() (UNIT_TEST_ALL_END() - UNIT_TEST_ALL_START())
110
Simon Glass204675c2019-12-29 21:19:25 -0700111/* Sizes for devres tests */
112enum {
113 TEST_DEVRES_SIZE = 100,
114 TEST_DEVRES_COUNT = 10,
115 TEST_DEVRES_TOTAL = TEST_DEVRES_SIZE * TEST_DEVRES_COUNT,
116
Simon Glass0fe15372019-12-29 21:19:28 -0700117 /* A few different sizes */
Simon Glass204675c2019-12-29 21:19:25 -0700118 TEST_DEVRES_SIZE2 = 15,
Simon Glass0fe15372019-12-29 21:19:28 -0700119 TEST_DEVRES_SIZE3 = 37,
Simon Glass204675c2019-12-29 21:19:25 -0700120};
Joe Hershberger3a77be52015-05-20 14:27:27 -0500121
Simon Glassa4e289b2020-10-25 20:38:28 -0600122/**
Simon Glass4bf89722020-12-23 08:11:18 -0700123 * testbus_get_clear_removed() - Test function to obtain removed device
124 *
125 * This is used in testbus to find out which device was removed. Calling this
126 * function returns a pointer to the device and then clears it back to NULL, so
127 * that a future test can check it.
128 */
129struct udevice *testbus_get_clear_removed(void);
130
Simon Glass45b807d2021-03-25 10:44:33 +1300131static inline void arch_reset_for_test(void)
132{
133#ifdef CONFIG_SANDBOX
134#include <asm/state.h>
135
136 state_reset_for_test(state_get_current());
137#endif
138}
139
Joe Hershberger3a77be52015-05-20 14:27:27 -0500140#endif /* __TEST_TEST_H */