blob: 6f0d32f7e2762b406b1028d8bc214eb9b93659cd [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 Glass59cad962021-03-07 17:34:55 -070033 ut_set_skip_delays(uts, false);
34
Simon Glass86a5bd02021-03-07 17:34:53 -070035 uts->start = mallinfo();
Simon Glassd93dc7d2021-03-07 17:34:48 -070036
Simon Glass177e0fd2021-03-07 17:34:52 -070037 if (test->flags & UT_TESTF_SCAN_PDATA)
38 ut_assertok(dm_scan_plat(false));
39
Simon Glass242357c2021-03-07 17:34:51 -070040 if (test->flags & UT_TESTF_PROBE_TEST)
41 ut_assertok(do_autoprobe(uts));
42
Simon Glassd18f7392021-03-07 17:34:50 -070043 if (!CONFIG_IS_ENABLED(OF_PLATDATA) &&
44 (test->flags & UT_TESTF_SCAN_FDT))
45 ut_assertok(dm_extended_scan(false));
46
Simon Glassd93dc7d2021-03-07 17:34:48 -070047 if (test->flags & UT_TESTF_CONSOLE_REC) {
48 int ret = console_record_reset_enable();
49
50 if (ret) {
51 printf("Skipping: Console recording disabled\n");
52 return -EAGAIN;
53 }
54 }
Simon Glass2b566b92021-03-07 17:34:54 -070055 ut_silence_console(uts);
Simon Glassd93dc7d2021-03-07 17:34:48 -070056
57 return 0;
58}
59
60int test_post_run(struct unit_test_state *uts, struct unit_test *test)
61{
Simon Glass2b566b92021-03-07 17:34:54 -070062 ut_unsilence_console(uts);
Simon Glass0f8f6772021-03-07 17:34:49 -070063
Simon Glassd93dc7d2021-03-07 17:34:48 -070064 return 0;
65}
66
Simon Glass5722fb22021-03-07 17:34:47 -070067int ut_run_tests(struct unit_test_state *uts, const char *prefix,
68 struct unit_test *tests, int count, const char *select_name)
69{
70 struct unit_test *test;
71 int prefix_len = prefix ? strlen(prefix) : 0;
72 int found = 0;
73
74 for (test = tests; test < tests + count; test++) {
75 const char *test_name = test->name;
Simon Glassd93dc7d2021-03-07 17:34:48 -070076 int ret;
Simon Glass5722fb22021-03-07 17:34:47 -070077
78 /* Remove the prefix */
79 if (prefix && !strncmp(test_name, prefix, prefix_len))
80 test_name += prefix_len;
81
82 if (select_name && strcmp(select_name, test_name))
83 continue;
84 printf("Test: %s\n", test_name);
85 found++;
86
Simon Glassd93dc7d2021-03-07 17:34:48 -070087 ret = test_pre_run(uts, test);
88 if (ret == -EAGAIN)
89 continue;
90 if (ret)
91 return ret;
Simon Glass5722fb22021-03-07 17:34:47 -070092
93 test->func(uts);
Simon Glassd93dc7d2021-03-07 17:34:48 -070094
95 ret = test_post_run(uts, test);
96 if (ret)
97 return ret;
Simon Glass5722fb22021-03-07 17:34:47 -070098 }
99 if (select_name && !found)
100 return -ENOENT;
101
102 return uts->fail_count ? -EBADF : 0;
103}
104
105int ut_run_list(const char *category, const char *prefix,
106 struct unit_test *tests, int count, const char *select_name)
107{
108 struct unit_test_state uts = { .fail_count = 0 };
109 int ret;
110
111 if (!select_name)
112 printf("Running %d %s tests\n", count, category);
113
114 ret = ut_run_tests(&uts, prefix, tests, count, select_name);
115
116 if (ret == -ENOENT)
117 printf("Test '%s' not found\n", select_name);
118 else
119 printf("Failures: %d\n", uts.fail_count);
120
121 return ret;
122}