blob: 3bbd77c38b553f8a0e110c992d88286c9dbedbd7 [file] [log] [blame]
Tom Rini83d290c2018-05-06 17:58:06 -04001/* SPDX-License-Identifier: GPL-2.0+ */
Joe Hershbergere721b882015-05-20 14:27:27 -05002/*
3 * Copyright (c) 2013 Google, Inc.
Joe Hershbergere721b882015-05-20 14:27:27 -05004 */
5
6#ifndef __TEST_TEST_H
7#define __TEST_TEST_H
8
9#include <malloc.h>
Simon Glasse180c2b2020-07-28 19:41:12 -060010#include <linux/bitops.h>
Joe Hershbergere721b882015-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 Glass72b524c2021-03-07 17:34:56 -070017 * @of_live: true to use livetree if available, false to use flattree
Simon Glass6fb2f572017-05-18 20:09:17 -060018 * @of_root: Record of the livetree root node (used for setting up tests)
Simon Glass4a467c62021-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 Glass0e4b6972022-09-06 20:27:05 -060023 * @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
Simon Glass756c0142022-09-06 20:27:10 -060026 * @other_fdt: Buffer for the other FDT (UT_TESTF_OTHER_FDT)
27 * @other_fdt_size: Size of the other FDT (UT_TESTF_OTHER_FDT)
Simon Glass8d468a12022-09-06 20:27:11 -060028 * @of_other: Live tree for the other FDT
Simon Glassea94d052022-08-01 07:58:45 -060029 * @runs_per_test: Number of times to run each test (typically 1)
Simon Glass400175b2020-01-27 08:49:56 -070030 * @expect_str: Temporary string used to hold expected string value
31 * @actual_str: Temporary string used to hold actual string value
Joe Hershbergere721b882015-05-20 14:27:27 -050032 */
33struct unit_test_state {
34 int fail_count;
35 struct mallinfo start;
Simon Glass6fb2f572017-05-18 20:09:17 -060036 struct device_node *of_root;
Simon Glass72b524c2021-03-07 17:34:56 -070037 bool of_live;
Simon Glass4a467c62021-03-07 17:34:57 -070038 struct udevice *root;
39 struct udevice *testdev;
40 int force_fail_alloc;
41 int skip_post_probe;
Simon Glass0e4b6972022-09-06 20:27:05 -060042 uint fdt_chksum;
43 void *fdt_copy;
44 uint fdt_size;
Simon Glass756c0142022-09-06 20:27:10 -060045 void *other_fdt;
46 int other_fdt_size;
Simon Glass8d468a12022-09-06 20:27:11 -060047 struct device_node *of_other;
Simon Glassea94d052022-08-01 07:58:45 -060048 int runs_per_test;
Simon Glassc614ddf2021-05-08 06:59:59 -060049 char expect_str[512];
50 char actual_str[512];
Joe Hershbergere721b882015-05-20 14:27:27 -050051};
52
Simon Glasse180c2b2020-07-28 19:41:12 -060053/* Test flags for each test */
54enum {
55 UT_TESTF_SCAN_PDATA = BIT(0), /* test needs platform data */
56 UT_TESTF_PROBE_TEST = BIT(1), /* probe test uclass */
57 UT_TESTF_SCAN_FDT = BIT(2), /* scan device tree */
58 UT_TESTF_FLAT_TREE = BIT(3), /* test needs flat DT */
59 UT_TESTF_LIVE_TREE = BIT(4), /* needs live device tree */
Simon Glass132644f2020-07-28 19:41:13 -060060 UT_TESTF_CONSOLE_REC = BIT(5), /* needs console recording */
Simon Glass4bc639e2021-03-07 17:34:45 -070061 /* do extra driver model init and uninit */
62 UT_TESTF_DM = BIT(6),
Simon Glass8d468a12022-09-06 20:27:11 -060063 UT_TESTF_OTHER_FDT = BIT(7), /* read in other device tree */
Simon Glasse180c2b2020-07-28 19:41:12 -060064};
65
Joe Hershbergere721b882015-05-20 14:27:27 -050066/**
67 * struct unit_test - Information about a unit test
68 *
69 * @name: Name of test
70 * @func: Function to call to perform test
71 * @flags: Flags indicated pre-conditions for test
72 */
73struct unit_test {
Simon Glass801587b2017-05-18 20:09:15 -060074 const char *file;
Joe Hershbergere721b882015-05-20 14:27:27 -050075 const char *name;
76 int (*func)(struct unit_test_state *state);
77 int flags;
78};
79
Heinrich Schuchardtd0ba0262020-05-06 18:26:07 +020080/**
81 * UNIT_TEST() - create linker generated list entry for unit a unit test
82 *
83 * The macro UNIT_TEST() is used to create a linker generated list entry. These
84 * list entries are enumerate tests that can be execute using the ut command.
85 * The list entries are used both by the implementation of the ut command as
86 * well as in a related Python test.
87 *
88 * For Python testing the subtests are collected in Python function
89 * generate_ut_subtest() by applying a regular expression to the lines of file
90 * u-boot.sym. The list entries have to follow strict naming conventions to be
91 * matched by the expression.
92 *
93 * Use UNIT_TEST(foo_test_bar, _flags, foo_test) for a test bar in test suite
94 * foo that can be executed via command 'ut foo bar' and is implemented in
95 * function foo_test_bar().
96 *
97 * @_name: concatenation of name of the test suite, "_test_", and the name
98 * of the test
99 * @_flags: an integer field that can be evaluated by the test suite
100 * implementation
101 * @_suite: name of the test suite concatenated with "_test"
102 */
Joe Hershbergere721b882015-05-20 14:27:27 -0500103#define UNIT_TEST(_name, _flags, _suite) \
Simon Glass2a2814d2021-03-07 17:35:11 -0700104 ll_entry_declare(struct unit_test, _name, ut_ ## _suite) = { \
Simon Glass801587b2017-05-18 20:09:15 -0600105 .file = __FILE__, \
Joe Hershbergere721b882015-05-20 14:27:27 -0500106 .name = #_name, \
107 .flags = _flags, \
108 .func = _name, \
109 }
110
Simon Glass2a2814d2021-03-07 17:35:11 -0700111/* Get the start of a list of unit tests for a particular suite */
Simon Glassa7a98752021-03-07 17:35:10 -0700112#define UNIT_TEST_SUITE_START(_suite) \
Simon Glass2a2814d2021-03-07 17:35:11 -0700113 ll_entry_start(struct unit_test, ut_ ## _suite)
Simon Glassa7a98752021-03-07 17:35:10 -0700114#define UNIT_TEST_SUITE_COUNT(_suite) \
Simon Glass2a2814d2021-03-07 17:35:11 -0700115 ll_entry_count(struct unit_test, ut_ ## _suite)
Simon Glassa7a98752021-03-07 17:35:10 -0700116
Simon Glass84823562021-03-07 17:35:12 -0700117/* Use ! and ~ so that all tests will be sorted between these two values */
118#define UNIT_TEST_ALL_START() ll_entry_start(struct unit_test, ut_!)
119#define UNIT_TEST_ALL_END() ll_entry_start(struct unit_test, ut_~)
120#define UNIT_TEST_ALL_COUNT() (UNIT_TEST_ALL_END() - UNIT_TEST_ALL_START())
121
Simon Glassdc12ebb2019-12-29 21:19:25 -0700122/* Sizes for devres tests */
123enum {
124 TEST_DEVRES_SIZE = 100,
125 TEST_DEVRES_COUNT = 10,
126 TEST_DEVRES_TOTAL = TEST_DEVRES_SIZE * TEST_DEVRES_COUNT,
127
Simon Glass42a0ce52019-12-29 21:19:28 -0700128 /* A few different sizes */
Simon Glassdc12ebb2019-12-29 21:19:25 -0700129 TEST_DEVRES_SIZE2 = 15,
Simon Glass42a0ce52019-12-29 21:19:28 -0700130 TEST_DEVRES_SIZE3 = 37,
Simon Glassdc12ebb2019-12-29 21:19:25 -0700131};
Joe Hershbergere721b882015-05-20 14:27:27 -0500132
Simon Glassb25ff5c2020-10-25 20:38:28 -0600133/**
Simon Glass079ac592020-12-23 08:11:18 -0700134 * testbus_get_clear_removed() - Test function to obtain removed device
135 *
136 * This is used in testbus to find out which device was removed. Calling this
137 * function returns a pointer to the device and then clears it back to NULL, so
138 * that a future test can check it.
139 */
140struct udevice *testbus_get_clear_removed(void);
141
Simon Glass756c0142022-09-06 20:27:10 -0600142#ifdef CONFIG_SANDBOX
143#include <asm/state.h>
144#include <asm/test.h>
145#endif
146
Simon Glassd4a15922021-03-25 10:44:33 +1300147static inline void arch_reset_for_test(void)
148{
149#ifdef CONFIG_SANDBOX
Simon Glassd4a15922021-03-25 10:44:33 +1300150 state_reset_for_test(state_get_current());
151#endif
152}
Simon Glass756c0142022-09-06 20:27:10 -0600153static inline int test_load_other_fdt(struct unit_test_state *uts)
154{
155 int ret = 0;
156#ifdef CONFIG_SANDBOX
157 ret = sandbox_load_other_fdt(&uts->other_fdt, &uts->other_fdt_size);
158#endif
159 return ret;
160}
Simon Glassd4a15922021-03-25 10:44:33 +1300161
Joe Hershbergere721b882015-05-20 14:27:27 -0500162#endif /* __TEST_TEST_H */