blob: db0d82e36c32ad82364d62c4c9f2de1b12970258 [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 Glass86a5bd02021-03-07 17:34:53 -070033 uts->start = mallinfo();
Simon Glassd93dc7d2021-03-07 17:34:48 -070034
Simon Glass177e0fd2021-03-07 17:34:52 -070035 if (test->flags & UT_TESTF_SCAN_PDATA)
36 ut_assertok(dm_scan_plat(false));
37
Simon Glass242357c2021-03-07 17:34:51 -070038 if (test->flags & UT_TESTF_PROBE_TEST)
39 ut_assertok(do_autoprobe(uts));
40
Simon Glassd18f7392021-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 Glassd93dc7d2021-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 }
53
54 return 0;
55}
56
57int test_post_run(struct unit_test_state *uts, struct unit_test *test)
58{
Simon Glass0f8f6772021-03-07 17:34:49 -070059 gd->flags &= ~GD_FLG_RECORD;
60
Simon Glassd93dc7d2021-03-07 17:34:48 -070061 return 0;
62}
63
Simon Glass5722fb22021-03-07 17:34:47 -070064int ut_run_tests(struct unit_test_state *uts, const char *prefix,
65 struct unit_test *tests, int count, const char *select_name)
66{
67 struct unit_test *test;
68 int prefix_len = prefix ? strlen(prefix) : 0;
69 int found = 0;
70
71 for (test = tests; test < tests + count; test++) {
72 const char *test_name = test->name;
Simon Glassd93dc7d2021-03-07 17:34:48 -070073 int ret;
Simon Glass5722fb22021-03-07 17:34:47 -070074
75 /* Remove the prefix */
76 if (prefix && !strncmp(test_name, prefix, prefix_len))
77 test_name += prefix_len;
78
79 if (select_name && strcmp(select_name, test_name))
80 continue;
81 printf("Test: %s\n", test_name);
82 found++;
83
Simon Glassd93dc7d2021-03-07 17:34:48 -070084 ret = test_pre_run(uts, test);
85 if (ret == -EAGAIN)
86 continue;
87 if (ret)
88 return ret;
Simon Glass5722fb22021-03-07 17:34:47 -070089
90 test->func(uts);
Simon Glassd93dc7d2021-03-07 17:34:48 -070091
92 ret = test_post_run(uts, test);
93 if (ret)
94 return ret;
Simon Glass5722fb22021-03-07 17:34:47 -070095 }
96 if (select_name && !found)
97 return -ENOENT;
98
99 return uts->fail_count ? -EBADF : 0;
100}
101
102int ut_run_list(const char *category, const char *prefix,
103 struct unit_test *tests, int count, const char *select_name)
104{
105 struct unit_test_state uts = { .fail_count = 0 };
106 int ret;
107
108 if (!select_name)
109 printf("Running %d %s tests\n", count, category);
110
111 ret = ut_run_tests(&uts, prefix, tests, count, select_name);
112
113 if (ret == -ENOENT)
114 printf("Test '%s' not found\n", select_name);
115 else
116 printf("Failures: %d\n", uts.fail_count);
117
118 return ret;
119}