blob: c5df8b397b3717627bd8594f04659e76346312b2 [file] [log] [blame]
Tom Rini10e47792018-05-06 17:58:06 -04001// SPDX-License-Identifier: GPL-2.0
Joe Hershberger11dd7cc2015-05-20 14:27:28 -05002/*
3 * (C) Copyright 2015
4 * Joe Hershberger, National Instruments, joe.hershberger@ni.com
Joe Hershberger11dd7cc2015-05-20 14:27:28 -05005 */
6
Joe Hershberger11dd7cc2015-05-20 14:27:28 -05007#include <command.h>
Simon Glass6db8ea52020-07-28 19:41:13 -06008#include <console.h>
Tom Rinidec7ea02024-05-20 13:35:03 -06009#include <vsprintf.h>
Joe Hershberger11dd7cc2015-05-20 14:27:28 -050010#include <test/suites.h>
Simon Glass81cbe1c2017-11-25 11:57:29 -070011#include <test/test.h>
Simon Glass5722fb22021-03-07 17:34:47 -070012#include <test/ut.h>
Joe Hershberger11dd7cc2015-05-20 14:27:28 -050013
Simon Glass78fd76b2025-01-20 14:25:33 -070014/**
15 * struct suite - A set of tests for a certain topic
16 *
17 * All tests end up in a single 'struct unit_test' linker-list array, in order
18 * of the suite they are in
19 *
20 * @name: Name of suite
21 * @start: First test in suite
22 * @end: End test in suite (points to the first test in the next suite)
23 * @cmd: Command to use to run the suite
24 */
25struct suite {
26 const char *name;
27 struct unit_test *start;
28 struct unit_test *end;
29 ut_cmd_func cmd;
30};
31
Simon Glassed38aef2020-05-10 11:40:03 -060032static int do_ut_all(struct cmd_tbl *cmdtp, int flag, int argc,
33 char *const argv[]);
Joe Hershberger11dd7cc2015-05-20 14:27:28 -050034
Simon Glassfb998c22022-10-29 19:47:12 -060035static int do_ut_info(struct cmd_tbl *cmdtp, int flag, int argc,
36 char *const argv[]);
37
Philippe Reynes1f99f842019-12-17 19:07:04 +010038int cmd_ut_category(const char *name, const char *prefix,
39 struct unit_test *tests, int n_ents,
Simon Glassed38aef2020-05-10 11:40:03 -060040 int argc, char *const argv[])
Simon Glass81cbe1c2017-11-25 11:57:29 -070041{
Simon Glass3869eb52025-01-20 14:25:26 -070042 struct unit_test_state uts;
Simon Glass85ba7c32022-10-29 19:47:13 -060043 const char *test_insert = NULL;
Simon Glass91a187b2022-08-01 07:58:45 -060044 int runs_per_text = 1;
Simon Glass1f1614b2022-10-20 18:22:50 -060045 bool force_run = false;
Simon Glass5722fb22021-03-07 17:34:47 -070046 int ret;
Simon Glass81cbe1c2017-11-25 11:57:29 -070047
Simon Glass1f1614b2022-10-20 18:22:50 -060048 while (argc > 1 && *argv[1] == '-') {
49 const char *str = argv[1];
50
51 switch (str[1]) {
52 case 'r':
53 runs_per_text = dectoul(str + 2, NULL);
54 break;
55 case 'f':
56 force_run = true;
57 break;
Simon Glass85ba7c32022-10-29 19:47:13 -060058 case 'I':
59 test_insert = str + 2;
60 break;
Simon Glass1f1614b2022-10-20 18:22:50 -060061 }
Simon Glass91a187b2022-08-01 07:58:45 -060062 argv++;
Simon Glass85ba7c32022-10-29 19:47:13 -060063 argc--;
Simon Glass91a187b2022-08-01 07:58:45 -060064 }
65
Simon Glass3869eb52025-01-20 14:25:26 -070066 ut_init_state(&uts);
67 ret = ut_run_list(&uts, name, prefix, tests, n_ents,
Simon Glass793a98e2023-11-18 14:05:20 -070068 cmd_arg1(argc, argv), runs_per_text, force_run,
Simon Glass85ba7c32022-10-29 19:47:13 -060069 test_insert);
Simon Glass3869eb52025-01-20 14:25:26 -070070 ut_uninit_state(&uts);
Simon Glass81cbe1c2017-11-25 11:57:29 -070071
Simon Glass5722fb22021-03-07 17:34:47 -070072 return ret ? CMD_RET_FAILURE : 0;
Simon Glass81cbe1c2017-11-25 11:57:29 -070073}
74
Simon Glass78fd76b2025-01-20 14:25:33 -070075/* declare linker-list symbols for the start and end of a suite */
76#define SUITE_DECL(_name) \
77 ll_start_decl(suite_start_ ## _name, struct unit_test, ut_ ## _name); \
78 ll_end_decl(suite_end_ ## _name, struct unit_test, ut_ ## _name)
79
80/* declare a test suite which uses a subcommand to run */
81#define SUITE_CMD(_name, _cmd_func) { \
82 #_name, \
83 suite_start_ ## _name, \
84 suite_end_ ## _name, \
85 _cmd_func, \
86 }
87
88/* declare a test suite which can be run directly without a subcommand */
89#define SUITE(_name) { \
90 #_name, \
91 suite_start_ ## _name, \
92 suite_end_ ## _name, \
93 NULL, \
94 }
95
96SUITE_DECL(info);
97SUITE_DECL(bdinfo);
98SUITE_DECL(bootstd);
99SUITE_DECL(cmd);
100SUITE_DECL(common);
101SUITE_DECL(dm);
102SUITE_DECL(env);
103SUITE_DECL(exit);
104SUITE_DECL(fdt);
105SUITE_DECL(font);
106SUITE_DECL(optee);
107SUITE_DECL(overlay);
108SUITE_DECL(lib);
109SUITE_DECL(log);
110SUITE_DECL(mbr);
111SUITE_DECL(mem);
112SUITE_DECL(setexpr);
113SUITE_DECL(measurement);
114SUITE_DECL(bloblist);
115SUITE_DECL(bootm);
116SUITE_DECL(addrmap);
117SUITE_DECL(hush);
118SUITE_DECL(loadm);
119SUITE_DECL(pci_mps);
120SUITE_DECL(seama);
121SUITE_DECL(upl);
122
123static struct suite suites[] = {
124 SUITE_CMD(info, do_ut_info),
Marek Vasut2cd173c2023-05-31 03:03:58 +0200125#ifdef CONFIG_CMD_BDI
Simon Glass78fd76b2025-01-20 14:25:33 -0700126 SUITE_CMD(bdinfo, do_ut_bdinfo),
Marek Vasut2cd173c2023-05-31 03:03:58 +0200127#endif
Simon Glass804aa372023-10-01 19:15:15 -0600128#ifdef CONFIG_UT_BOOTSTD
Simon Glass78fd76b2025-01-20 14:25:33 -0700129 SUITE_CMD(bootstd, do_ut_bootstd),
Simon Glassb255efc2022-04-24 23:31:24 -0600130#endif
Simon Glass621a97e2023-10-01 19:15:12 -0600131#ifdef CONFIG_CMDLINE
Simon Glass78fd76b2025-01-20 14:25:33 -0700132 SUITE_CMD(cmd, do_ut_cmd),
Simon Glass621a97e2023-10-01 19:15:12 -0600133#endif
Simon Glass78fd76b2025-01-20 14:25:33 -0700134 SUITE_CMD(common, do_ut_common),
Joe Hershberger9dc1d712015-05-20 14:27:29 -0500135#if defined(CONFIG_UT_DM)
Simon Glass78fd76b2025-01-20 14:25:33 -0700136 SUITE_CMD(dm, do_ut_dm),
Joe Hershberger9dc1d712015-05-20 14:27:29 -0500137#endif
Joe Hershberger26e038f2015-05-20 14:27:36 -0500138#if defined(CONFIG_UT_ENV)
Simon Glass78fd76b2025-01-20 14:25:33 -0700139 SUITE_CMD(env, do_ut_env),
Joe Hershberger26e038f2015-05-20 14:27:36 -0500140#endif
Simon Glass78fd76b2025-01-20 14:25:33 -0700141 SUITE_CMD(exit, do_ut_exit),
Simon Glassf3c6a1d2022-07-13 06:06:59 -0600142#ifdef CONFIG_CMD_FDT
Simon Glass78fd76b2025-01-20 14:25:33 -0700143 SUITE_CMD(fdt, do_ut_fdt),
Simon Glassf3c6a1d2022-07-13 06:06:59 -0600144#endif
Simon Glass9e972c32022-10-06 08:36:16 -0600145#ifdef CONFIG_CONSOLE_TRUETYPE
Simon Glass78fd76b2025-01-20 14:25:33 -0700146 SUITE_CMD(font, do_ut_font),
Simon Glass9e972c32022-10-06 08:36:16 -0600147#endif
Heiko Stuebner1c9bb9b2019-10-23 16:46:41 +0200148#ifdef CONFIG_UT_OPTEE
Simon Glass78fd76b2025-01-20 14:25:33 -0700149 SUITE_CMD(optee, do_ut_optee),
Heiko Stuebner1c9bb9b2019-10-23 16:46:41 +0200150#endif
Maxime Ripard0e31a112016-07-05 10:26:46 +0200151#ifdef CONFIG_UT_OVERLAY
Simon Glass78fd76b2025-01-20 14:25:33 -0700152 SUITE_CMD(overlay, do_ut_overlay),
Maxime Ripard0e31a112016-07-05 10:26:46 +0200153#endif
Heinrich Schuchardtf77a6352019-01-30 07:53:31 +0100154#ifdef CONFIG_UT_LIB
Simon Glass78fd76b2025-01-20 14:25:33 -0700155 SUITE_CMD(lib, do_ut_lib),
Heinrich Schuchardtf77a6352019-01-30 07:53:31 +0100156#endif
Heinrich Schuchardtf433d502020-02-26 21:48:18 +0100157#ifdef CONFIG_UT_LOG
Simon Glass78fd76b2025-01-20 14:25:33 -0700158 SUITE_CMD(log, do_ut_log),
Heinrich Schuchardtf433d502020-02-26 21:48:18 +0100159#endif
Alexander Gendin038cb022023-10-09 01:24:36 +0000160#if defined(CONFIG_SANDBOX) && defined(CONFIG_CMD_MBR) && defined(CONFIG_CMD_MMC) \
161 && defined(CONFIG_MMC_SANDBOX) && defined(CONFIG_MMC_WRITE)
Simon Glass78fd76b2025-01-20 14:25:33 -0700162 SUITE_CMD(mbr, do_ut_mbr),
Alexander Gendin038cb022023-10-09 01:24:36 +0000163#endif
Simon Glass78fd76b2025-01-20 14:25:33 -0700164 SUITE_CMD(mem, do_ut_mem),
Heinrich Schuchardt90a082d2022-10-01 21:42:35 +0200165#if defined(CONFIG_SANDBOX) && defined(CONFIG_CMD_SETEXPR)
Simon Glass78fd76b2025-01-20 14:25:33 -0700166 SUITE_CMD(setexpr, do_ut_setexpr),
Heinrich Schuchardtc33f85e2021-02-17 12:58:14 +0100167#endif
Eddie James1a55a7a2023-10-24 10:43:51 -0500168#ifdef CONFIG_MEASURED_BOOT
Simon Glass78fd76b2025-01-20 14:25:33 -0700169 SUITE_CMD(measurement, do_ut_measurement),
Eddie James1a55a7a2023-10-24 10:43:51 -0500170#endif
Simon Glass1edaed02017-11-25 11:57:33 -0700171#ifdef CONFIG_SANDBOX
Evgeny Bachininfbdb85a2024-12-02 16:45:24 +0300172#if CONFIG_IS_ENABLED(BLOBLIST)
Simon Glass78fd76b2025-01-20 14:25:33 -0700173 SUITE_CMD(bloblist, do_ut_bloblist),
174 SUITE_CMD(bootm, do_ut_bootm),
Simon Glass1edaed02017-11-25 11:57:33 -0700175#endif
Evgeny Bachinin4b74ddd2024-12-02 16:45:25 +0300176#endif
Bin Meng261fc062021-02-25 17:22:35 +0800177#ifdef CONFIG_CMD_ADDRMAP
Simon Glass78fd76b2025-01-20 14:25:33 -0700178 SUITE_CMD(addrmap, do_ut_addrmap),
Bin Meng261fc062021-02-25 17:22:35 +0800179#endif
Francis Laniel2dba91a2023-12-22 22:02:21 +0100180#if CONFIG_IS_ENABLED(HUSH_PARSER)
Simon Glass78fd76b2025-01-20 14:25:33 -0700181 SUITE_CMD(hush, do_ut_hush),
Francis Laniel2dba91a2023-12-22 22:02:21 +0100182#endif
Rui Miguel Silva433f15a2022-05-11 10:55:40 +0100183#ifdef CONFIG_CMD_LOADM
Simon Glass78fd76b2025-01-20 14:25:33 -0700184 SUITE_CMD(loadm, do_ut_loadm),
Rui Miguel Silva433f15a2022-05-11 10:55:40 +0100185#endif
Stephen Carlsone5c05ae2023-03-10 11:07:15 -0800186#ifdef CONFIG_CMD_PCI_MPS
Simon Glass78fd76b2025-01-20 14:25:33 -0700187 SUITE_CMD(pci_mps, do_ut_pci_mps),
Stephen Carlsone5c05ae2023-03-10 11:07:15 -0800188#endif
Linus Walleijbef39252023-02-01 00:16:13 +0100189#ifdef CONFIG_CMD_SEAMA
Simon Glass78fd76b2025-01-20 14:25:33 -0700190 SUITE_CMD(seama, do_ut_seama),
Linus Walleijbef39252023-02-01 00:16:13 +0100191#endif
Simon Glassaac25692024-08-07 16:47:29 -0600192#ifdef CONFIG_CMD_UPL
Simon Glass78fd76b2025-01-20 14:25:33 -0700193 SUITE_CMD(upl, do_ut_upl),
Simon Glassaac25692024-08-07 16:47:29 -0600194#endif
Joe Hershberger11dd7cc2015-05-20 14:27:28 -0500195};
196
Simon Glass78fd76b2025-01-20 14:25:33 -0700197/** run_suite() - Run a suite of tests */
198static int run_suite(struct suite *ste, struct cmd_tbl *cmdtp, int flag,
199 int argc, char *const argv[])
200{
201 int ret;
202
203 if (ste->cmd) {
204 ret = ste->cmd(cmdtp, flag, argc, argv);
205 } else {
206 int n_ents = ste->end - ste->start;
207 char prefix[30];
208
209 /* use a standard prefix */
210 snprintf(prefix, sizeof(prefix), "%s_test", ste->name);
211 ret = cmd_ut_category(ste->name, prefix, ste->start, n_ents,
212 argc, argv);
213 }
214
215 return ret;
216}
217
Simon Glassed38aef2020-05-10 11:40:03 -0600218static int do_ut_all(struct cmd_tbl *cmdtp, int flag, int argc,
219 char *const argv[])
Joe Hershberger11dd7cc2015-05-20 14:27:28 -0500220{
221 int i;
222 int retval;
223 int any_fail = 0;
224
Simon Glass78fd76b2025-01-20 14:25:33 -0700225 for (i = 0; i < ARRAY_SIZE(suites); i++) {
226 struct suite *ste = &suites[i];
227 char *const argv[] = {(char *)ste->name, NULL};
228
229 printf("----Running %s tests----\n", ste->name);
230 retval = run_suite(ste, cmdtp, flag, 1, argv);
Joe Hershberger11dd7cc2015-05-20 14:27:28 -0500231 if (!any_fail)
232 any_fail = retval;
233 }
234
235 return any_fail;
236}
237
Simon Glassfb998c22022-10-29 19:47:12 -0600238static int do_ut_info(struct cmd_tbl *cmdtp, int flag, int argc,
239 char *const argv[])
240{
Simon Glasscfb38f82025-01-20 14:25:30 -0700241 const char *flags;
242
Simon Glass78fd76b2025-01-20 14:25:33 -0700243 printf("Test suites: %d\n", (int)ARRAY_SIZE(suites));
Simon Glassfb998c22022-10-29 19:47:12 -0600244 printf("Total tests: %d\n", (int)UNIT_TEST_ALL_COUNT());
245
Simon Glasscfb38f82025-01-20 14:25:30 -0700246 flags = cmd_arg1(argc, argv);
247 if (flags && !strcmp("-s", flags)) {
248 int i;
249
250 puts("\nTests Suite\n");
251 puts("----- -----\n");
Simon Glass78fd76b2025-01-20 14:25:33 -0700252 for (i = 1; i < ARRAY_SIZE(suites); i++) {
253 struct suite *ste = &suites[i];
254 long n_ent = ste->end - ste->start;
255
256 if (n_ent)
257 printf("%5ld %s\n", n_ent, ste->name);
258 else
259 printf("%5s %s\n", "?", ste->name);
260 }
Simon Glasscfb38f82025-01-20 14:25:30 -0700261 }
262
Simon Glassfb998c22022-10-29 19:47:12 -0600263 return 0;
264}
265
Simon Glass78fd76b2025-01-20 14:25:33 -0700266static struct suite *find_suite(const char *name)
267{
268 struct suite *ste;
269 int i;
270
271 for (i = 0, ste = suites; i < ARRAY_SIZE(suites); i++, ste++) {
272 if (!strcmp(ste->name, name))
273 return ste;
274 }
275
276 return NULL;
277}
278
Simon Glassed38aef2020-05-10 11:40:03 -0600279static int do_ut(struct cmd_tbl *cmdtp, int flag, int argc, char *const argv[])
Joe Hershberger11dd7cc2015-05-20 14:27:28 -0500280{
Simon Glass78fd76b2025-01-20 14:25:33 -0700281 struct suite *ste;
282 const char *name;
283 int ret;
Joe Hershberger11dd7cc2015-05-20 14:27:28 -0500284
285 if (argc < 2)
286 return CMD_RET_USAGE;
287
288 /* drop initial "ut" arg */
289 argc--;
290 argv++;
291
Simon Glass78fd76b2025-01-20 14:25:33 -0700292 name = argv[0];
293 if (!strcmp(name, "all")) {
294 ret = do_ut_all(cmdtp, flag, argc, argv);
295 } else if (!strcmp(name, "info")) {
296 ret = do_ut_info(cmdtp, flag, argc, argv);
297 } else {
298 ste = find_suite(argv[0]);
299 if (!ste) {
300 printf("Suite '%s' not found\n", argv[0]);
301 return CMD_RET_FAILURE;
302 }
Joe Hershberger11dd7cc2015-05-20 14:27:28 -0500303
Simon Glass78fd76b2025-01-20 14:25:33 -0700304 ret = run_suite(ste, cmdtp, flag, argc, argv);
305 }
306 if (ret)
307 return ret;
Joe Hershberger11dd7cc2015-05-20 14:27:28 -0500308
Simon Glass78fd76b2025-01-20 14:25:33 -0700309 return 0;
Joe Hershberger11dd7cc2015-05-20 14:27:28 -0500310}
311
Tom Rini03f146c2023-10-07 15:13:08 -0400312U_BOOT_LONGHELP(ut,
Simon Glass154bb8e2022-10-29 19:47:10 -0600313 "[-r] [-f] [<suite>] - run unit tests\n"
314 " -r<runs> Number of times to run each test\n"
315 " -f Force 'manual' tests to run as well\n"
316 " <suite> Test suite to run, or all\n"
317 "\n"
Simon Glassfb998c22022-10-29 19:47:12 -0600318 "\nOptions for <suite>:"
Simon Glass154bb8e2022-10-29 19:47:10 -0600319 "\nall - execute all enabled tests"
Simon Glasscfb38f82025-01-20 14:25:30 -0700320 "\ninfo [-s] - show info about tests [and suites]"
Simon Glass154bb8e2022-10-29 19:47:10 -0600321#ifdef CONFIG_CMD_ADDRMAP
322 "\naddrmap - very basic test of addrmap command"
323#endif
Marek Vasut2cd173c2023-05-31 03:03:58 +0200324#ifdef CONFIG_CMD_BDI
325 "\nbdinfo - bdinfo command"
326#endif
Heinrich Schuchardtb8b6c812018-08-31 21:31:28 +0200327#ifdef CONFIG_SANDBOX
Simon Glass154bb8e2022-10-29 19:47:10 -0600328 "\nbloblist - bloblist implementation"
Heinrich Schuchardtb8b6c812018-08-31 21:31:28 +0200329#endif
Simon Glassb255efc2022-04-24 23:31:24 -0600330#ifdef CONFIG_BOOTSTD
Simon Glass154bb8e2022-10-29 19:47:10 -0600331 "\nbootstd - standard boot implementation"
332#endif
Simon Glass621a97e2023-10-01 19:15:12 -0600333#ifdef CONFIG_CMDLINE
334 "\ncmd - test various commands"
335#endif
Simon Glass3394c3c2024-11-02 13:36:56 -0600336 "\ncommon - tests for common/ directory"
Joe Hershberger9dc1d712015-05-20 14:27:29 -0500337#ifdef CONFIG_UT_DM
Simon Glass154bb8e2022-10-29 19:47:10 -0600338 "\ndm - driver model"
Joe Hershberger9dc1d712015-05-20 14:27:29 -0500339#endif
Joe Hershberger26e038f2015-05-20 14:27:36 -0500340#ifdef CONFIG_UT_ENV
Simon Glass154bb8e2022-10-29 19:47:10 -0600341 "\nenv - environment"
Joe Hershberger26e038f2015-05-20 14:27:36 -0500342#endif
Simon Glassf3c6a1d2022-07-13 06:06:59 -0600343#ifdef CONFIG_CMD_FDT
Simon Glass154bb8e2022-10-29 19:47:10 -0600344 "\nfdt - fdt command"
Simon Glassf3c6a1d2022-07-13 06:06:59 -0600345#endif
Simon Glass9e972c32022-10-06 08:36:16 -0600346#ifdef CONFIG_CONSOLE_TRUETYPE
Marek Vasutef9eb6c2023-08-13 00:16:41 +0200347 "\nfont - font command"
Simon Glass154bb8e2022-10-29 19:47:10 -0600348#endif
Francis Laniel2dba91a2023-12-22 22:02:21 +0100349#if CONFIG_IS_ENABLED(HUSH_PARSER)
350 "\nhush - Test hush behavior"
351#endif
Simon Glass154bb8e2022-10-29 19:47:10 -0600352#ifdef CONFIG_CMD_LOADM
353 "\nloadm - loadm command parameters and loading memory blob"
Simon Glass9e972c32022-10-06 08:36:16 -0600354#endif
Heinrich Schuchardtf77a6352019-01-30 07:53:31 +0100355#ifdef CONFIG_UT_LIB
Simon Glass154bb8e2022-10-29 19:47:10 -0600356 "\nlib - library functions"
Heinrich Schuchardtf77a6352019-01-30 07:53:31 +0100357#endif
Heinrich Schuchardtf433d502020-02-26 21:48:18 +0100358#ifdef CONFIG_UT_LOG
Simon Glass154bb8e2022-10-29 19:47:10 -0600359 "\nlog - logging functions"
Heinrich Schuchardtf433d502020-02-26 21:48:18 +0100360#endif
Simon Glass154bb8e2022-10-29 19:47:10 -0600361 "\nmem - memory-related commands"
Heiko Stuebner1c9bb9b2019-10-23 16:46:41 +0200362#ifdef CONFIG_UT_OPTEE
Simon Glass154bb8e2022-10-29 19:47:10 -0600363 "\noptee - test OP-TEE"
Heiko Stuebner1c9bb9b2019-10-23 16:46:41 +0200364#endif
Maxime Ripard0e31a112016-07-05 10:26:46 +0200365#ifdef CONFIG_UT_OVERLAY
Simon Glass154bb8e2022-10-29 19:47:10 -0600366 "\noverlay - device tree overlays"
Maxime Ripard0e31a112016-07-05 10:26:46 +0200367#endif
Stephen Carlsone5c05ae2023-03-10 11:07:15 -0800368#ifdef CONFIG_CMD_PCI_MPS
369 "\npci_mps - PCI Express Maximum Payload Size"
370#endif
Simon Glass154bb8e2022-10-29 19:47:10 -0600371 "\nsetexpr - setexpr command"
Linus Walleijbef39252023-02-01 00:16:13 +0100372#ifdef CONFIG_CMD_SEAMA
373 "\nseama - seama command parameters loading and decoding"
374#endif
Tom Rini03f146c2023-10-07 15:13:08 -0400375 );
Joe Hershberger11dd7cc2015-05-20 14:27:28 -0500376
377U_BOOT_CMD(
378 ut, CONFIG_SYS_MAXARGS, 1, do_ut,
379 "unit tests", ut_help_text
380);