blob: f14b7b09f79f08c340c34304c463025a71a33338 [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>
10#include <dm/root.h>
Simon Glass5722fb22021-03-07 17:34:47 -070011#include <test/test.h>
Simon Glassd18f7392021-03-07 17:34:50 -070012#include <test/ut.h>
Simon Glass5722fb22021-03-07 17:34:47 -070013
Simon Glass0f8f6772021-03-07 17:34:49 -070014DECLARE_GLOBAL_DATA_PTR;
15
Simon Glass242357c2021-03-07 17:34:51 -070016/* Ensure all the test devices are probed */
17static int do_autoprobe(struct unit_test_state *uts)
18{
19 struct udevice *dev;
20 int ret;
21
22 /* Scanning the uclass is enough to probe all the devices */
23 for (ret = uclass_first_device(UCLASS_TEST, &dev);
24 dev;
25 ret = uclass_next_device(&dev))
26 ;
27
28 return ret;
29}
30
Simon Glassd93dc7d2021-03-07 17:34:48 -070031int test_pre_run(struct unit_test_state *uts, struct unit_test *test)
32{
Simon Glassb2890a12021-03-07 17:34:56 -070033 if (test->flags & UT_TESTF_DM)
34 ut_assertok(dm_test_init(uts));
35
Simon Glass59cad962021-03-07 17:34:55 -070036 ut_set_skip_delays(uts, false);
37
Simon Glass86a5bd02021-03-07 17:34:53 -070038 uts->start = mallinfo();
Simon Glassd93dc7d2021-03-07 17:34:48 -070039
Simon Glass177e0fd2021-03-07 17:34:52 -070040 if (test->flags & UT_TESTF_SCAN_PDATA)
41 ut_assertok(dm_scan_plat(false));
42
Simon Glass242357c2021-03-07 17:34:51 -070043 if (test->flags & UT_TESTF_PROBE_TEST)
44 ut_assertok(do_autoprobe(uts));
45
Simon Glassd18f7392021-03-07 17:34:50 -070046 if (!CONFIG_IS_ENABLED(OF_PLATDATA) &&
47 (test->flags & UT_TESTF_SCAN_FDT))
48 ut_assertok(dm_extended_scan(false));
49
Simon Glassd93dc7d2021-03-07 17:34:48 -070050 if (test->flags & UT_TESTF_CONSOLE_REC) {
51 int ret = console_record_reset_enable();
52
53 if (ret) {
54 printf("Skipping: Console recording disabled\n");
55 return -EAGAIN;
56 }
57 }
Simon Glass2b566b92021-03-07 17:34:54 -070058 ut_silence_console(uts);
Simon Glassd93dc7d2021-03-07 17:34:48 -070059
60 return 0;
61}
62
63int test_post_run(struct unit_test_state *uts, struct unit_test *test)
64{
Simon Glass2b566b92021-03-07 17:34:54 -070065 ut_unsilence_console(uts);
Simon Glass0f8f6772021-03-07 17:34:49 -070066
Simon Glassd93dc7d2021-03-07 17:34:48 -070067 return 0;
68}
69
Simon Glass5722fb22021-03-07 17:34:47 -070070int ut_run_tests(struct unit_test_state *uts, const char *prefix,
71 struct unit_test *tests, int count, const char *select_name)
72{
73 struct unit_test *test;
74 int prefix_len = prefix ? strlen(prefix) : 0;
75 int found = 0;
76
77 for (test = tests; test < tests + count; test++) {
78 const char *test_name = test->name;
Simon Glassd93dc7d2021-03-07 17:34:48 -070079 int ret;
Simon Glass5722fb22021-03-07 17:34:47 -070080
81 /* Remove the prefix */
82 if (prefix && !strncmp(test_name, prefix, prefix_len))
83 test_name += prefix_len;
84
85 if (select_name && strcmp(select_name, test_name))
86 continue;
87 printf("Test: %s\n", test_name);
88 found++;
89
Simon Glassd93dc7d2021-03-07 17:34:48 -070090 ret = test_pre_run(uts, test);
91 if (ret == -EAGAIN)
92 continue;
93 if (ret)
94 return ret;
Simon Glass5722fb22021-03-07 17:34:47 -070095
96 test->func(uts);
Simon Glassd93dc7d2021-03-07 17:34:48 -070097
98 ret = test_post_run(uts, test);
99 if (ret)
100 return ret;
Simon Glass5722fb22021-03-07 17:34:47 -0700101 }
102 if (select_name && !found)
103 return -ENOENT;
104
105 return uts->fail_count ? -EBADF : 0;
106}
107
108int ut_run_list(const char *category, const char *prefix,
109 struct unit_test *tests, int count, const char *select_name)
110{
111 struct unit_test_state uts = { .fail_count = 0 };
112 int ret;
113
114 if (!select_name)
115 printf("Running %d %s tests\n", count, category);
116
117 ret = ut_run_tests(&uts, prefix, tests, count, select_name);
118
119 if (ret == -ENOENT)
120 printf("Test '%s' not found\n", select_name);
121 else
122 printf("Failures: %d\n", uts.fail_count);
123
124 return ret;
125}