blob: e273777b6e204069cb8dee766ce3c8f8d97de68f [file] [log] [blame]
Simon Glass1c721752021-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 Glassd8ed2342021-03-07 17:34:50 -07009#include <dm.h>
10#include <dm/root.h>
Simon Glass1c721752021-03-07 17:34:47 -070011#include <test/test.h>
Simon Glassd8ed2342021-03-07 17:34:50 -070012#include <test/ut.h>
Simon Glass1c721752021-03-07 17:34:47 -070013
Simon Glass30a0d202021-03-07 17:34:49 -070014DECLARE_GLOBAL_DATA_PTR;
15
Simon Glass4b8b27e2021-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 Glassd002a272021-03-07 17:34:48 -070031int test_pre_run(struct unit_test_state *uts, struct unit_test *test)
32{
Simon Glass19fb3db2021-03-07 17:34:53 -070033 uts->start = mallinfo();
Simon Glassd002a272021-03-07 17:34:48 -070034
Simon Glass5a986f32021-03-07 17:34:52 -070035 if (test->flags & UT_TESTF_SCAN_PDATA)
36 ut_assertok(dm_scan_plat(false));
37
Simon Glass4b8b27e2021-03-07 17:34:51 -070038 if (test->flags & UT_TESTF_PROBE_TEST)
39 ut_assertok(do_autoprobe(uts));
40
Simon Glassd8ed2342021-03-07 17:34:50 -070041 if (!CONFIG_IS_ENABLED(OF_PLATDATA) &&
42 (test->flags & UT_TESTF_SCAN_FDT))
43 ut_assertok(dm_extended_scan(false));
44
Simon Glassd002a272021-03-07 17:34:48 -070045 if (test->flags & UT_TESTF_CONSOLE_REC) {
46 int ret = console_record_reset_enable();
47
48 if (ret) {
49 printf("Skipping: Console recording disabled\n");
50 return -EAGAIN;
51 }
52 }
Simon Glass74524712021-03-07 17:34:54 -070053 ut_silence_console(uts);
Simon Glassd002a272021-03-07 17:34:48 -070054
55 return 0;
56}
57
58int test_post_run(struct unit_test_state *uts, struct unit_test *test)
59{
Simon Glass74524712021-03-07 17:34:54 -070060 ut_unsilence_console(uts);
Simon Glass30a0d202021-03-07 17:34:49 -070061
Simon Glassd002a272021-03-07 17:34:48 -070062 return 0;
63}
64
Simon Glass1c721752021-03-07 17:34:47 -070065int ut_run_tests(struct unit_test_state *uts, const char *prefix,
66 struct unit_test *tests, int count, const char *select_name)
67{
68 struct unit_test *test;
69 int prefix_len = prefix ? strlen(prefix) : 0;
70 int found = 0;
71
72 for (test = tests; test < tests + count; test++) {
73 const char *test_name = test->name;
Simon Glassd002a272021-03-07 17:34:48 -070074 int ret;
Simon Glass1c721752021-03-07 17:34:47 -070075
76 /* Remove the prefix */
77 if (prefix && !strncmp(test_name, prefix, prefix_len))
78 test_name += prefix_len;
79
80 if (select_name && strcmp(select_name, test_name))
81 continue;
82 printf("Test: %s\n", test_name);
83 found++;
84
Simon Glassd002a272021-03-07 17:34:48 -070085 ret = test_pre_run(uts, test);
86 if (ret == -EAGAIN)
87 continue;
88 if (ret)
89 return ret;
Simon Glass1c721752021-03-07 17:34:47 -070090
91 test->func(uts);
Simon Glassd002a272021-03-07 17:34:48 -070092
93 ret = test_post_run(uts, test);
94 if (ret)
95 return ret;
Simon Glass1c721752021-03-07 17:34:47 -070096 }
97 if (select_name && !found)
98 return -ENOENT;
99
100 return uts->fail_count ? -EBADF : 0;
101}
102
103int ut_run_list(const char *category, const char *prefix,
104 struct unit_test *tests, int count, const char *select_name)
105{
106 struct unit_test_state uts = { .fail_count = 0 };
107 int ret;
108
109 if (!select_name)
110 printf("Running %d %s tests\n", count, category);
111
112 ret = ut_run_tests(&uts, prefix, tests, count, select_name);
113
114 if (ret == -ENOENT)
115 printf("Test '%s' not found\n", select_name);
116 else
117 printf("Failures: %d\n", uts.fail_count);
118
119 return ret;
120}