blob: 3806c2ad89cb16340bf83ebbdd76f84847ae9cee [file] [log] [blame]
Simon Glass5722fb22021-03-07 17:34:47 -07001// SPDX-License-Identifier: GPL-2.0+
2/*
3 * Copyright 2021 Google LLC
4 * Written by Simon Glass <sjg@chromium.org>
5 */
6
7#include <common.h>
8#include <console.h>
Simon Glassd18f7392021-03-07 17:34:50 -07009#include <dm.h>
Simon Glass2dba4762021-03-07 17:34:58 -070010#include <asm/state.h>
Simon Glassd18f7392021-03-07 17:34:50 -070011#include <dm/root.h>
Simon Glass2dba4762021-03-07 17:34:58 -070012#include <dm/test.h>
Simon Glass5b7e55b2021-03-07 17:34:59 -070013#include <dm/uclass-internal.h>
Simon Glass5722fb22021-03-07 17:34:47 -070014#include <test/test.h>
Simon Glassd18f7392021-03-07 17:34:50 -070015#include <test/ut.h>
Simon Glass5722fb22021-03-07 17:34:47 -070016
Simon Glass0f8f6772021-03-07 17:34:49 -070017DECLARE_GLOBAL_DATA_PTR;
18
Simon Glass2dba4762021-03-07 17:34:58 -070019/**
20 * dm_test_pre_run() - Get ready to run a driver model test
21 *
22 * This clears out the driver model data structures. For sandbox it resets the
23 * state structure
24 *
25 * @uts: Test state
26 */
27static int dm_test_pre_run(struct unit_test_state *uts)
28{
29 bool of_live = uts->of_live;
30
31 uts->root = NULL;
32 uts->testdev = NULL;
33 uts->force_fail_alloc = false;
34 uts->skip_post_probe = false;
35 gd->dm_root = NULL;
36 if (!CONFIG_IS_ENABLED(OF_PLATDATA))
37 memset(dm_testdrv_op_count, '\0', sizeof(dm_testdrv_op_count));
38 state_reset_for_test(state_get_current());
39
40 /* Determine whether to make the live tree available */
41 gd_set_of_root(of_live ? uts->of_root : NULL);
42 ut_assertok(dm_init(of_live));
43 uts->root = dm_root();
44
45 return 0;
46}
47
Simon Glass5b7e55b2021-03-07 17:34:59 -070048static int dm_test_post_run(struct unit_test_state *uts)
49{
50 int id;
51
52 for (id = 0; id < UCLASS_COUNT; id++) {
53 struct uclass *uc;
54
55 /*
56 * If the uclass doesn't exist we don't want to create it. So
57 * check that here before we call uclass_find_device().
58 */
59 uc = uclass_find(id);
60 if (!uc)
61 continue;
62 ut_assertok(uclass_destroy(uc));
63 }
64
65 return 0;
66}
67
Simon Glass242357c2021-03-07 17:34:51 -070068/* Ensure all the test devices are probed */
69static int do_autoprobe(struct unit_test_state *uts)
70{
71 struct udevice *dev;
72 int ret;
73
74 /* Scanning the uclass is enough to probe all the devices */
75 for (ret = uclass_first_device(UCLASS_TEST, &dev);
76 dev;
77 ret = uclass_next_device(&dev))
78 ;
79
80 return ret;
81}
82
Simon Glassd93dc7d2021-03-07 17:34:48 -070083int test_pre_run(struct unit_test_state *uts, struct unit_test *test)
84{
Simon Glassb2890a12021-03-07 17:34:56 -070085 if (test->flags & UT_TESTF_DM)
Simon Glass2dba4762021-03-07 17:34:58 -070086 ut_assertok(dm_test_pre_run(uts));
Simon Glassb2890a12021-03-07 17:34:56 -070087
Simon Glass59cad962021-03-07 17:34:55 -070088 ut_set_skip_delays(uts, false);
89
Simon Glass86a5bd02021-03-07 17:34:53 -070090 uts->start = mallinfo();
Simon Glassd93dc7d2021-03-07 17:34:48 -070091
Simon Glass177e0fd2021-03-07 17:34:52 -070092 if (test->flags & UT_TESTF_SCAN_PDATA)
93 ut_assertok(dm_scan_plat(false));
94
Simon Glass242357c2021-03-07 17:34:51 -070095 if (test->flags & UT_TESTF_PROBE_TEST)
96 ut_assertok(do_autoprobe(uts));
97
Simon Glassd18f7392021-03-07 17:34:50 -070098 if (!CONFIG_IS_ENABLED(OF_PLATDATA) &&
99 (test->flags & UT_TESTF_SCAN_FDT))
100 ut_assertok(dm_extended_scan(false));
101
Simon Glassd93dc7d2021-03-07 17:34:48 -0700102 if (test->flags & UT_TESTF_CONSOLE_REC) {
103 int ret = console_record_reset_enable();
104
105 if (ret) {
106 printf("Skipping: Console recording disabled\n");
107 return -EAGAIN;
108 }
109 }
Simon Glass2b566b92021-03-07 17:34:54 -0700110 ut_silence_console(uts);
Simon Glassd93dc7d2021-03-07 17:34:48 -0700111
112 return 0;
113}
114
115int test_post_run(struct unit_test_state *uts, struct unit_test *test)
116{
Simon Glass2b566b92021-03-07 17:34:54 -0700117 ut_unsilence_console(uts);
Simon Glass5b7e55b2021-03-07 17:34:59 -0700118 if (test->flags & UT_TESTF_DM)
119 ut_assertok(dm_test_post_run(uts));
Simon Glass0f8f6772021-03-07 17:34:49 -0700120
Simon Glassd93dc7d2021-03-07 17:34:48 -0700121 return 0;
122}
123
Simon Glass5722fb22021-03-07 17:34:47 -0700124int ut_run_tests(struct unit_test_state *uts, const char *prefix,
125 struct unit_test *tests, int count, const char *select_name)
126{
127 struct unit_test *test;
128 int prefix_len = prefix ? strlen(prefix) : 0;
129 int found = 0;
130
131 for (test = tests; test < tests + count; test++) {
132 const char *test_name = test->name;
Simon Glassd93dc7d2021-03-07 17:34:48 -0700133 int ret;
Simon Glass5722fb22021-03-07 17:34:47 -0700134
135 /* Remove the prefix */
136 if (prefix && !strncmp(test_name, prefix, prefix_len))
137 test_name += prefix_len;
138
139 if (select_name && strcmp(select_name, test_name))
140 continue;
141 printf("Test: %s\n", test_name);
142 found++;
143
Simon Glassd93dc7d2021-03-07 17:34:48 -0700144 ret = test_pre_run(uts, test);
145 if (ret == -EAGAIN)
146 continue;
147 if (ret)
148 return ret;
Simon Glass5722fb22021-03-07 17:34:47 -0700149
150 test->func(uts);
Simon Glassd93dc7d2021-03-07 17:34:48 -0700151
152 ret = test_post_run(uts, test);
153 if (ret)
154 return ret;
Simon Glass5722fb22021-03-07 17:34:47 -0700155 }
156 if (select_name && !found)
157 return -ENOENT;
158
159 return uts->fail_count ? -EBADF : 0;
160}
161
162int ut_run_list(const char *category, const char *prefix,
163 struct unit_test *tests, int count, const char *select_name)
164{
165 struct unit_test_state uts = { .fail_count = 0 };
166 int ret;
167
168 if (!select_name)
169 printf("Running %d %s tests\n", count, category);
170
171 ret = ut_run_tests(&uts, prefix, tests, count, select_name);
172
173 if (ret == -ENOENT)
174 printf("Test '%s' not found\n", select_name);
175 else
176 printf("Failures: %d\n", uts.fail_count);
177
178 return ret;
179}