blob: fe96d739dc417c3b842e2d4ed71a0297d1513c87 [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 Glass30a0d202021-03-07 17:34:49 -070033 /* DM tests have already done this */
34 if (!(test->flags & UT_TESTF_DM))
35 uts->start = mallinfo();
Simon Glassd002a272021-03-07 17:34:48 -070036
Simon Glass5a986f32021-03-07 17:34:52 -070037 if (test->flags & UT_TESTF_SCAN_PDATA)
38 ut_assertok(dm_scan_plat(false));
39
Simon Glass4b8b27e2021-03-07 17:34:51 -070040 if (test->flags & UT_TESTF_PROBE_TEST)
41 ut_assertok(do_autoprobe(uts));
42
Simon Glassd8ed2342021-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 Glassd002a272021-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 }
55
56 return 0;
57}
58
59int test_post_run(struct unit_test_state *uts, struct unit_test *test)
60{
Simon Glass30a0d202021-03-07 17:34:49 -070061 gd->flags &= ~GD_FLG_RECORD;
62
Simon Glassd002a272021-03-07 17:34:48 -070063 return 0;
64}
65
Simon Glass1c721752021-03-07 17:34:47 -070066int ut_run_tests(struct unit_test_state *uts, const char *prefix,
67 struct unit_test *tests, int count, const char *select_name)
68{
69 struct unit_test *test;
70 int prefix_len = prefix ? strlen(prefix) : 0;
71 int found = 0;
72
73 for (test = tests; test < tests + count; test++) {
74 const char *test_name = test->name;
Simon Glassd002a272021-03-07 17:34:48 -070075 int ret;
Simon Glass1c721752021-03-07 17:34:47 -070076
77 /* Remove the prefix */
78 if (prefix && !strncmp(test_name, prefix, prefix_len))
79 test_name += prefix_len;
80
81 if (select_name && strcmp(select_name, test_name))
82 continue;
83 printf("Test: %s\n", test_name);
84 found++;
85
Simon Glassd002a272021-03-07 17:34:48 -070086 ret = test_pre_run(uts, test);
87 if (ret == -EAGAIN)
88 continue;
89 if (ret)
90 return ret;
Simon Glass1c721752021-03-07 17:34:47 -070091
92 test->func(uts);
Simon Glassd002a272021-03-07 17:34:48 -070093
94 ret = test_post_run(uts, test);
95 if (ret)
96 return ret;
Simon Glass1c721752021-03-07 17:34:47 -070097 }
98 if (select_name && !found)
99 return -ENOENT;
100
101 return uts->fail_count ? -EBADF : 0;
102}
103
104int ut_run_list(const char *category, const char *prefix,
105 struct unit_test *tests, int count, const char *select_name)
106{
107 struct unit_test_state uts = { .fail_count = 0 };
108 int ret;
109
110 if (!select_name)
111 printf("Running %d %s tests\n", count, category);
112
113 ret = ut_run_tests(&uts, prefix, tests, count, select_name);
114
115 if (ret == -ENOENT)
116 printf("Test '%s' not found\n", select_name);
117 else
118 printf("Failures: %d\n", uts.fail_count);
119
120 return ret;
121}