blob: a971fe0e9c86771698b2153b9a5729af84624c57 [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 Glassd002a272021-03-07 17:34:48 -070016int test_pre_run(struct unit_test_state *uts, struct unit_test *test)
17{
Simon Glass30a0d202021-03-07 17:34:49 -070018 /* DM tests have already done this */
19 if (!(test->flags & UT_TESTF_DM))
20 uts->start = mallinfo();
Simon Glassd002a272021-03-07 17:34:48 -070021
Simon Glassd8ed2342021-03-07 17:34:50 -070022 if (!CONFIG_IS_ENABLED(OF_PLATDATA) &&
23 (test->flags & UT_TESTF_SCAN_FDT))
24 ut_assertok(dm_extended_scan(false));
25
Simon Glassd002a272021-03-07 17:34:48 -070026 if (test->flags & UT_TESTF_CONSOLE_REC) {
27 int ret = console_record_reset_enable();
28
29 if (ret) {
30 printf("Skipping: Console recording disabled\n");
31 return -EAGAIN;
32 }
33 }
34
35 return 0;
36}
37
38int test_post_run(struct unit_test_state *uts, struct unit_test *test)
39{
Simon Glass30a0d202021-03-07 17:34:49 -070040 gd->flags &= ~GD_FLG_RECORD;
41
Simon Glassd002a272021-03-07 17:34:48 -070042 return 0;
43}
44
Simon Glass1c721752021-03-07 17:34:47 -070045int ut_run_tests(struct unit_test_state *uts, const char *prefix,
46 struct unit_test *tests, int count, const char *select_name)
47{
48 struct unit_test *test;
49 int prefix_len = prefix ? strlen(prefix) : 0;
50 int found = 0;
51
52 for (test = tests; test < tests + count; test++) {
53 const char *test_name = test->name;
Simon Glassd002a272021-03-07 17:34:48 -070054 int ret;
Simon Glass1c721752021-03-07 17:34:47 -070055
56 /* Remove the prefix */
57 if (prefix && !strncmp(test_name, prefix, prefix_len))
58 test_name += prefix_len;
59
60 if (select_name && strcmp(select_name, test_name))
61 continue;
62 printf("Test: %s\n", test_name);
63 found++;
64
Simon Glassd002a272021-03-07 17:34:48 -070065 ret = test_pre_run(uts, test);
66 if (ret == -EAGAIN)
67 continue;
68 if (ret)
69 return ret;
Simon Glass1c721752021-03-07 17:34:47 -070070
71 test->func(uts);
Simon Glassd002a272021-03-07 17:34:48 -070072
73 ret = test_post_run(uts, test);
74 if (ret)
75 return ret;
Simon Glass1c721752021-03-07 17:34:47 -070076 }
77 if (select_name && !found)
78 return -ENOENT;
79
80 return uts->fail_count ? -EBADF : 0;
81}
82
83int ut_run_list(const char *category, const char *prefix,
84 struct unit_test *tests, int count, const char *select_name)
85{
86 struct unit_test_state uts = { .fail_count = 0 };
87 int ret;
88
89 if (!select_name)
90 printf("Running %d %s tests\n", count, category);
91
92 ret = ut_run_tests(&uts, prefix, tests, count, select_name);
93
94 if (ret == -ENOENT)
95 printf("Test '%s' not found\n", select_name);
96 else
97 printf("Failures: %d\n", uts.fail_count);
98
99 return ret;
100}