Tom Rini | 10e4779 | 2018-05-06 17:58:06 -0400 | [diff] [blame] | 1 | /* SPDX-License-Identifier: GPL-2.0+ */ |
Joe Hershberger | 3a77be5 | 2015-05-20 14:27:27 -0500 | [diff] [blame] | 2 | /* |
| 3 | * Copyright (c) 2013 Google, Inc. |
Joe Hershberger | 3a77be5 | 2015-05-20 14:27:27 -0500 | [diff] [blame] | 4 | */ |
| 5 | |
| 6 | #ifndef __TEST_TEST_H |
| 7 | #define __TEST_TEST_H |
| 8 | |
| 9 | #include <malloc.h> |
Simon Glass | 974dccd | 2020-07-28 19:41:12 -0600 | [diff] [blame] | 10 | #include <linux/bitops.h> |
Joe Hershberger | 3a77be5 | 2015-05-20 14:27:27 -0500 | [diff] [blame] | 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 |
Simon Glass | b2890a1 | 2021-03-07 17:34:56 -0700 | [diff] [blame] | 17 | * @of_live: true to use livetree if available, false to use flattree |
Simon Glass | 017886b | 2017-05-18 20:09:17 -0600 | [diff] [blame] | 18 | * @of_root: Record of the livetree root node (used for setting up tests) |
Simon Glass | b98bfbc | 2021-03-07 17:34:57 -0700 | [diff] [blame^] | 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 |
Simon Glass | d856e91 | 2020-01-27 08:49:56 -0700 | [diff] [blame] | 23 | * @expect_str: Temporary string used to hold expected string value |
| 24 | * @actual_str: Temporary string used to hold actual string value |
Joe Hershberger | 3a77be5 | 2015-05-20 14:27:27 -0500 | [diff] [blame] | 25 | */ |
| 26 | struct unit_test_state { |
| 27 | int fail_count; |
| 28 | struct mallinfo start; |
Simon Glass | 017886b | 2017-05-18 20:09:17 -0600 | [diff] [blame] | 29 | struct device_node *of_root; |
Simon Glass | b2890a1 | 2021-03-07 17:34:56 -0700 | [diff] [blame] | 30 | bool of_live; |
Simon Glass | b98bfbc | 2021-03-07 17:34:57 -0700 | [diff] [blame^] | 31 | struct udevice *root; |
| 32 | struct udevice *testdev; |
| 33 | int force_fail_alloc; |
| 34 | int skip_post_probe; |
Simon Glass | d856e91 | 2020-01-27 08:49:56 -0700 | [diff] [blame] | 35 | char expect_str[256]; |
| 36 | char actual_str[256]; |
Joe Hershberger | 3a77be5 | 2015-05-20 14:27:27 -0500 | [diff] [blame] | 37 | }; |
| 38 | |
Simon Glass | 974dccd | 2020-07-28 19:41:12 -0600 | [diff] [blame] | 39 | /* Test flags for each test */ |
| 40 | enum { |
| 41 | UT_TESTF_SCAN_PDATA = BIT(0), /* test needs platform data */ |
| 42 | UT_TESTF_PROBE_TEST = BIT(1), /* probe test uclass */ |
| 43 | UT_TESTF_SCAN_FDT = BIT(2), /* scan device tree */ |
| 44 | UT_TESTF_FLAT_TREE = BIT(3), /* test needs flat DT */ |
| 45 | UT_TESTF_LIVE_TREE = BIT(4), /* needs live device tree */ |
Simon Glass | 6db8ea5 | 2020-07-28 19:41:13 -0600 | [diff] [blame] | 46 | UT_TESTF_CONSOLE_REC = BIT(5), /* needs console recording */ |
Simon Glass | ab1fca0 | 2021-03-07 17:34:45 -0700 | [diff] [blame] | 47 | /* do extra driver model init and uninit */ |
| 48 | UT_TESTF_DM = BIT(6), |
Simon Glass | 974dccd | 2020-07-28 19:41:12 -0600 | [diff] [blame] | 49 | }; |
| 50 | |
Joe Hershberger | 3a77be5 | 2015-05-20 14:27:27 -0500 | [diff] [blame] | 51 | /** |
| 52 | * struct unit_test - Information about a unit test |
| 53 | * |
| 54 | * @name: Name of test |
| 55 | * @func: Function to call to perform test |
| 56 | * @flags: Flags indicated pre-conditions for test |
| 57 | */ |
| 58 | struct unit_test { |
Simon Glass | be408ee | 2017-05-18 20:09:15 -0600 | [diff] [blame] | 59 | const char *file; |
Joe Hershberger | 3a77be5 | 2015-05-20 14:27:27 -0500 | [diff] [blame] | 60 | const char *name; |
| 61 | int (*func)(struct unit_test_state *state); |
| 62 | int flags; |
| 63 | }; |
| 64 | |
Heinrich Schuchardt | df6c36c | 2020-05-06 18:26:07 +0200 | [diff] [blame] | 65 | /** |
| 66 | * UNIT_TEST() - create linker generated list entry for unit a unit test |
| 67 | * |
| 68 | * The macro UNIT_TEST() is used to create a linker generated list entry. These |
| 69 | * list entries are enumerate tests that can be execute using the ut command. |
| 70 | * The list entries are used both by the implementation of the ut command as |
| 71 | * well as in a related Python test. |
| 72 | * |
| 73 | * For Python testing the subtests are collected in Python function |
| 74 | * generate_ut_subtest() by applying a regular expression to the lines of file |
| 75 | * u-boot.sym. The list entries have to follow strict naming conventions to be |
| 76 | * matched by the expression. |
| 77 | * |
| 78 | * Use UNIT_TEST(foo_test_bar, _flags, foo_test) for a test bar in test suite |
| 79 | * foo that can be executed via command 'ut foo bar' and is implemented in |
| 80 | * function foo_test_bar(). |
| 81 | * |
| 82 | * @_name: concatenation of name of the test suite, "_test_", and the name |
| 83 | * of the test |
| 84 | * @_flags: an integer field that can be evaluated by the test suite |
| 85 | * implementation |
| 86 | * @_suite: name of the test suite concatenated with "_test" |
| 87 | */ |
Joe Hershberger | 3a77be5 | 2015-05-20 14:27:27 -0500 | [diff] [blame] | 88 | #define UNIT_TEST(_name, _flags, _suite) \ |
| 89 | ll_entry_declare(struct unit_test, _name, _suite) = { \ |
Simon Glass | be408ee | 2017-05-18 20:09:15 -0600 | [diff] [blame] | 90 | .file = __FILE__, \ |
Joe Hershberger | 3a77be5 | 2015-05-20 14:27:27 -0500 | [diff] [blame] | 91 | .name = #_name, \ |
| 92 | .flags = _flags, \ |
| 93 | .func = _name, \ |
| 94 | } |
| 95 | |
Simon Glass | 204675c | 2019-12-29 21:19:25 -0700 | [diff] [blame] | 96 | /* Sizes for devres tests */ |
| 97 | enum { |
| 98 | TEST_DEVRES_SIZE = 100, |
| 99 | TEST_DEVRES_COUNT = 10, |
| 100 | TEST_DEVRES_TOTAL = TEST_DEVRES_SIZE * TEST_DEVRES_COUNT, |
| 101 | |
Simon Glass | 0fe1537 | 2019-12-29 21:19:28 -0700 | [diff] [blame] | 102 | /* A few different sizes */ |
Simon Glass | 204675c | 2019-12-29 21:19:25 -0700 | [diff] [blame] | 103 | TEST_DEVRES_SIZE2 = 15, |
Simon Glass | 0fe1537 | 2019-12-29 21:19:28 -0700 | [diff] [blame] | 104 | TEST_DEVRES_SIZE3 = 37, |
Simon Glass | 204675c | 2019-12-29 21:19:25 -0700 | [diff] [blame] | 105 | }; |
Joe Hershberger | 3a77be5 | 2015-05-20 14:27:27 -0500 | [diff] [blame] | 106 | |
Simon Glass | a4e289b | 2020-10-25 20:38:28 -0600 | [diff] [blame] | 107 | /** |
Simon Glass | 4bf8972 | 2020-12-23 08:11:18 -0700 | [diff] [blame] | 108 | * testbus_get_clear_removed() - Test function to obtain removed device |
| 109 | * |
| 110 | * This is used in testbus to find out which device was removed. Calling this |
| 111 | * function returns a pointer to the device and then clears it back to NULL, so |
| 112 | * that a future test can check it. |
| 113 | */ |
| 114 | struct udevice *testbus_get_clear_removed(void); |
| 115 | |
| 116 | /** |
Simon Glass | a1add9d | 2021-03-07 17:34:46 -0700 | [diff] [blame] | 117 | * dm_test_run() - Run driver model tests |
Simon Glass | a4e289b | 2020-10-25 20:38:28 -0600 | [diff] [blame] | 118 | * |
| 119 | * Run all the available driver model tests, or a selection |
| 120 | * |
| 121 | * @test_name: Name of single test to run (e.g. "dm_test_fdt_pre_reloc" or just |
| 122 | * "fdt_pre_reloc"), or NULL to run all |
| 123 | * @return 0 if all tests passed, 1 if not |
| 124 | */ |
Simon Glass | a1add9d | 2021-03-07 17:34:46 -0700 | [diff] [blame] | 125 | int dm_test_run(const char *test_name); |
Simon Glass | a4e289b | 2020-10-25 20:38:28 -0600 | [diff] [blame] | 126 | |
Joe Hershberger | 3a77be5 | 2015-05-20 14:27:27 -0500 | [diff] [blame] | 127 | #endif /* __TEST_TEST_H */ |