blob: 6997568cc0762a4fa6f39c9b84e36b21c8acdd4b [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
17 * @priv: A pointer to some other info some suites want to track
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 Glassd856e912020-01-27 08:49:56 -070020 * @expect_str: Temporary string used to hold expected string value
21 * @actual_str: Temporary string used to hold actual string value
Joe Hershberger3a77be52015-05-20 14:27:27 -050022 */
23struct unit_test_state {
24 int fail_count;
25 struct mallinfo start;
26 void *priv;
Simon Glass017886b2017-05-18 20:09:17 -060027 struct device_node *of_root;
Simon Glassb2890a12021-03-07 17:34:56 -070028 bool of_live;
Simon Glassd856e912020-01-27 08:49:56 -070029 char expect_str[256];
30 char actual_str[256];
Joe Hershberger3a77be52015-05-20 14:27:27 -050031};
32
Simon Glass974dccd2020-07-28 19:41:12 -060033/* Test flags for each test */
34enum {
35 UT_TESTF_SCAN_PDATA = BIT(0), /* test needs platform data */
36 UT_TESTF_PROBE_TEST = BIT(1), /* probe test uclass */
37 UT_TESTF_SCAN_FDT = BIT(2), /* scan device tree */
38 UT_TESTF_FLAT_TREE = BIT(3), /* test needs flat DT */
39 UT_TESTF_LIVE_TREE = BIT(4), /* needs live device tree */
Simon Glass6db8ea52020-07-28 19:41:13 -060040 UT_TESTF_CONSOLE_REC = BIT(5), /* needs console recording */
Simon Glassab1fca02021-03-07 17:34:45 -070041 /* do extra driver model init and uninit */
42 UT_TESTF_DM = BIT(6),
Simon Glass974dccd2020-07-28 19:41:12 -060043};
44
Joe Hershberger3a77be52015-05-20 14:27:27 -050045/**
46 * struct unit_test - Information about a unit test
47 *
48 * @name: Name of test
49 * @func: Function to call to perform test
50 * @flags: Flags indicated pre-conditions for test
51 */
52struct unit_test {
Simon Glassbe408ee2017-05-18 20:09:15 -060053 const char *file;
Joe Hershberger3a77be52015-05-20 14:27:27 -050054 const char *name;
55 int (*func)(struct unit_test_state *state);
56 int flags;
57};
58
Heinrich Schuchardtdf6c36c2020-05-06 18:26:07 +020059/**
60 * UNIT_TEST() - create linker generated list entry for unit a unit test
61 *
62 * The macro UNIT_TEST() is used to create a linker generated list entry. These
63 * list entries are enumerate tests that can be execute using the ut command.
64 * The list entries are used both by the implementation of the ut command as
65 * well as in a related Python test.
66 *
67 * For Python testing the subtests are collected in Python function
68 * generate_ut_subtest() by applying a regular expression to the lines of file
69 * u-boot.sym. The list entries have to follow strict naming conventions to be
70 * matched by the expression.
71 *
72 * Use UNIT_TEST(foo_test_bar, _flags, foo_test) for a test bar in test suite
73 * foo that can be executed via command 'ut foo bar' and is implemented in
74 * function foo_test_bar().
75 *
76 * @_name: concatenation of name of the test suite, "_test_", and the name
77 * of the test
78 * @_flags: an integer field that can be evaluated by the test suite
79 * implementation
80 * @_suite: name of the test suite concatenated with "_test"
81 */
Joe Hershberger3a77be52015-05-20 14:27:27 -050082#define UNIT_TEST(_name, _flags, _suite) \
83 ll_entry_declare(struct unit_test, _name, _suite) = { \
Simon Glassbe408ee2017-05-18 20:09:15 -060084 .file = __FILE__, \
Joe Hershberger3a77be52015-05-20 14:27:27 -050085 .name = #_name, \
86 .flags = _flags, \
87 .func = _name, \
88 }
89
Simon Glass204675c2019-12-29 21:19:25 -070090/* Sizes for devres tests */
91enum {
92 TEST_DEVRES_SIZE = 100,
93 TEST_DEVRES_COUNT = 10,
94 TEST_DEVRES_TOTAL = TEST_DEVRES_SIZE * TEST_DEVRES_COUNT,
95
Simon Glass0fe15372019-12-29 21:19:28 -070096 /* A few different sizes */
Simon Glass204675c2019-12-29 21:19:25 -070097 TEST_DEVRES_SIZE2 = 15,
Simon Glass0fe15372019-12-29 21:19:28 -070098 TEST_DEVRES_SIZE3 = 37,
Simon Glass204675c2019-12-29 21:19:25 -070099};
Joe Hershberger3a77be52015-05-20 14:27:27 -0500100
Simon Glassa4e289b2020-10-25 20:38:28 -0600101/**
Simon Glass4bf89722020-12-23 08:11:18 -0700102 * testbus_get_clear_removed() - Test function to obtain removed device
103 *
104 * This is used in testbus to find out which device was removed. Calling this
105 * function returns a pointer to the device and then clears it back to NULL, so
106 * that a future test can check it.
107 */
108struct udevice *testbus_get_clear_removed(void);
109
110/**
Simon Glassa1add9d2021-03-07 17:34:46 -0700111 * dm_test_run() - Run driver model tests
Simon Glassa4e289b2020-10-25 20:38:28 -0600112 *
113 * Run all the available driver model tests, or a selection
114 *
115 * @test_name: Name of single test to run (e.g. "dm_test_fdt_pre_reloc" or just
116 * "fdt_pre_reloc"), or NULL to run all
117 * @return 0 if all tests passed, 1 if not
118 */
Simon Glassa1add9d2021-03-07 17:34:46 -0700119int dm_test_run(const char *test_name);
Simon Glassa4e289b2020-10-25 20:38:28 -0600120
Joe Hershberger3a77be52015-05-20 14:27:27 -0500121#endif /* __TEST_TEST_H */