blob: 181cdb43152d7909d3766c3d5ed63c62c906acf4 [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
Simon Glass78fd76b2025-01-20 14:25:33 -070096SUITE_DECL(bdinfo);
97SUITE_DECL(bootstd);
98SUITE_DECL(cmd);
99SUITE_DECL(common);
100SUITE_DECL(dm);
101SUITE_DECL(env);
102SUITE_DECL(exit);
103SUITE_DECL(fdt);
104SUITE_DECL(font);
105SUITE_DECL(optee);
106SUITE_DECL(overlay);
107SUITE_DECL(lib);
108SUITE_DECL(log);
109SUITE_DECL(mbr);
110SUITE_DECL(mem);
111SUITE_DECL(setexpr);
112SUITE_DECL(measurement);
113SUITE_DECL(bloblist);
114SUITE_DECL(bootm);
115SUITE_DECL(addrmap);
116SUITE_DECL(hush);
117SUITE_DECL(loadm);
118SUITE_DECL(pci_mps);
119SUITE_DECL(seama);
120SUITE_DECL(upl);
121
122static struct suite suites[] = {
Marek Vasut2cd173c2023-05-31 03:03:58 +0200123#ifdef CONFIG_CMD_BDI
Simon Glasse1dc0502025-01-20 14:25:34 -0700124 SUITE(bdinfo),
Marek Vasut2cd173c2023-05-31 03:03:58 +0200125#endif
Simon Glass804aa372023-10-01 19:15:15 -0600126#ifdef CONFIG_UT_BOOTSTD
Simon Glass78fd76b2025-01-20 14:25:33 -0700127 SUITE_CMD(bootstd, do_ut_bootstd),
Simon Glassb255efc2022-04-24 23:31:24 -0600128#endif
Simon Glass621a97e2023-10-01 19:15:12 -0600129#ifdef CONFIG_CMDLINE
Simon Glassc2356572025-01-20 14:25:35 -0700130 SUITE(cmd),
Simon Glass621a97e2023-10-01 19:15:12 -0600131#endif
Simon Glassbc39468a2025-01-20 14:25:36 -0700132 SUITE(common),
Joe Hershberger9dc1d712015-05-20 14:27:29 -0500133#if defined(CONFIG_UT_DM)
Simon Glass82f00f22025-01-20 14:25:37 -0700134 SUITE(dm),
Joe Hershberger9dc1d712015-05-20 14:27:29 -0500135#endif
Joe Hershberger26e038f2015-05-20 14:27:36 -0500136#if defined(CONFIG_UT_ENV)
Simon Glass2ff779d2025-01-20 14:25:38 -0700137 SUITE(env),
Joe Hershberger26e038f2015-05-20 14:27:36 -0500138#endif
Simon Glass3e5f32d2025-01-20 14:25:39 -0700139 SUITE(exit),
Simon Glassf3c6a1d2022-07-13 06:06:59 -0600140#ifdef CONFIG_CMD_FDT
Simon Glass936b69e2025-01-20 14:25:40 -0700141 SUITE(fdt),
Simon Glassf3c6a1d2022-07-13 06:06:59 -0600142#endif
Simon Glass9e972c32022-10-06 08:36:16 -0600143#ifdef CONFIG_CONSOLE_TRUETYPE
Simon Glass1b0b3f52025-01-20 14:25:41 -0700144 SUITE(font),
Simon Glass9e972c32022-10-06 08:36:16 -0600145#endif
Heiko Stuebner1c9bb9b2019-10-23 16:46:41 +0200146#ifdef CONFIG_UT_OPTEE
Simon Glass78fd76b2025-01-20 14:25:33 -0700147 SUITE_CMD(optee, do_ut_optee),
Heiko Stuebner1c9bb9b2019-10-23 16:46:41 +0200148#endif
Maxime Ripard0e31a112016-07-05 10:26:46 +0200149#ifdef CONFIG_UT_OVERLAY
Simon Glass78fd76b2025-01-20 14:25:33 -0700150 SUITE_CMD(overlay, do_ut_overlay),
Maxime Ripard0e31a112016-07-05 10:26:46 +0200151#endif
Heinrich Schuchardtf77a6352019-01-30 07:53:31 +0100152#ifdef CONFIG_UT_LIB
Simon Glassb315ffb2025-01-20 14:25:42 -0700153 SUITE(lib),
Heinrich Schuchardtf77a6352019-01-30 07:53:31 +0100154#endif
Heinrich Schuchardtf433d502020-02-26 21:48:18 +0100155#ifdef CONFIG_UT_LOG
Simon Glassf7ad5092025-01-20 14:25:43 -0700156 SUITE(log),
Heinrich Schuchardtf433d502020-02-26 21:48:18 +0100157#endif
Alexander Gendin038cb022023-10-09 01:24:36 +0000158#if defined(CONFIG_SANDBOX) && defined(CONFIG_CMD_MBR) && defined(CONFIG_CMD_MMC) \
159 && defined(CONFIG_MMC_SANDBOX) && defined(CONFIG_MMC_WRITE)
Simon Glassc16ffb22025-01-20 14:25:44 -0700160 SUITE(mbr),
Alexander Gendin038cb022023-10-09 01:24:36 +0000161#endif
Simon Glass05512d62025-01-20 14:25:45 -0700162 SUITE(mem),
Heinrich Schuchardt90a082d2022-10-01 21:42:35 +0200163#if defined(CONFIG_SANDBOX) && defined(CONFIG_CMD_SETEXPR)
Simon Glass8feb5ee2025-01-20 14:25:46 -0700164 SUITE(setexpr),
Heinrich Schuchardtc33f85e2021-02-17 12:58:14 +0100165#endif
Eddie James1a55a7a2023-10-24 10:43:51 -0500166#ifdef CONFIG_MEASURED_BOOT
Simon Glasscc9d1de2025-01-20 14:25:47 -0700167 SUITE(measurement),
Eddie James1a55a7a2023-10-24 10:43:51 -0500168#endif
Simon Glass1edaed02017-11-25 11:57:33 -0700169#ifdef CONFIG_SANDBOX
Evgeny Bachininfbdb85a2024-12-02 16:45:24 +0300170#if CONFIG_IS_ENABLED(BLOBLIST)
Simon Glassacec4be2025-01-20 14:25:48 -0700171 SUITE(bloblist),
Simon Glass4060c362025-01-20 14:25:49 -0700172 SUITE(bootm),
Simon Glass1edaed02017-11-25 11:57:33 -0700173#endif
Evgeny Bachinin4b74ddd2024-12-02 16:45:25 +0300174#endif
Bin Meng261fc062021-02-25 17:22:35 +0800175#ifdef CONFIG_CMD_ADDRMAP
Simon Glass03bf0042025-01-20 14:25:50 -0700176 SUITE(addrmap),
Bin Meng261fc062021-02-25 17:22:35 +0800177#endif
Francis Laniel2dba91a2023-12-22 22:02:21 +0100178#if CONFIG_IS_ENABLED(HUSH_PARSER)
Simon Glassb5f92742025-01-20 14:25:51 -0700179 SUITE(hush),
Francis Laniel2dba91a2023-12-22 22:02:21 +0100180#endif
Rui Miguel Silva433f15a2022-05-11 10:55:40 +0100181#ifdef CONFIG_CMD_LOADM
Simon Glass401c2cf2025-01-20 14:25:52 -0700182 SUITE(loadm),
Rui Miguel Silva433f15a2022-05-11 10:55:40 +0100183#endif
Stephen Carlsone5c05ae2023-03-10 11:07:15 -0800184#ifdef CONFIG_CMD_PCI_MPS
Simon Glassc6cede32025-01-20 14:25:53 -0700185 SUITE(pci_mps),
Stephen Carlsone5c05ae2023-03-10 11:07:15 -0800186#endif
Linus Walleijbef39252023-02-01 00:16:13 +0100187#ifdef CONFIG_CMD_SEAMA
Simon Glassf0764dd2025-01-20 14:25:54 -0700188 SUITE(seama),
Linus Walleijbef39252023-02-01 00:16:13 +0100189#endif
Simon Glassaac25692024-08-07 16:47:29 -0600190#ifdef CONFIG_CMD_UPL
Simon Glass0cf497c2025-01-20 14:25:55 -0700191 SUITE(upl),
Simon Glassaac25692024-08-07 16:47:29 -0600192#endif
Joe Hershberger11dd7cc2015-05-20 14:27:28 -0500193};
194
Simon Glass78fd76b2025-01-20 14:25:33 -0700195/** run_suite() - Run a suite of tests */
196static int run_suite(struct suite *ste, struct cmd_tbl *cmdtp, int flag,
197 int argc, char *const argv[])
198{
199 int ret;
200
201 if (ste->cmd) {
202 ret = ste->cmd(cmdtp, flag, argc, argv);
203 } else {
204 int n_ents = ste->end - ste->start;
205 char prefix[30];
206
207 /* use a standard prefix */
208 snprintf(prefix, sizeof(prefix), "%s_test", ste->name);
209 ret = cmd_ut_category(ste->name, prefix, ste->start, n_ents,
210 argc, argv);
211 }
212
213 return ret;
214}
215
Simon Glassed38aef2020-05-10 11:40:03 -0600216static int do_ut_all(struct cmd_tbl *cmdtp, int flag, int argc,
217 char *const argv[])
Joe Hershberger11dd7cc2015-05-20 14:27:28 -0500218{
219 int i;
220 int retval;
221 int any_fail = 0;
222
Simon Glass78fd76b2025-01-20 14:25:33 -0700223 for (i = 0; i < ARRAY_SIZE(suites); i++) {
224 struct suite *ste = &suites[i];
225 char *const argv[] = {(char *)ste->name, NULL};
226
227 printf("----Running %s tests----\n", ste->name);
228 retval = run_suite(ste, cmdtp, flag, 1, argv);
Joe Hershberger11dd7cc2015-05-20 14:27:28 -0500229 if (!any_fail)
230 any_fail = retval;
231 }
232
233 return any_fail;
234}
235
Simon Glassfb998c22022-10-29 19:47:12 -0600236static int do_ut_info(struct cmd_tbl *cmdtp, int flag, int argc,
237 char *const argv[])
238{
Simon Glasscfb38f82025-01-20 14:25:30 -0700239 const char *flags;
240
Simon Glass78fd76b2025-01-20 14:25:33 -0700241 printf("Test suites: %d\n", (int)ARRAY_SIZE(suites));
Simon Glassfb998c22022-10-29 19:47:12 -0600242 printf("Total tests: %d\n", (int)UNIT_TEST_ALL_COUNT());
243
Simon Glasscfb38f82025-01-20 14:25:30 -0700244 flags = cmd_arg1(argc, argv);
245 if (flags && !strcmp("-s", flags)) {
246 int i;
247
248 puts("\nTests Suite\n");
249 puts("----- -----\n");
Simon Glass7d4ae012025-01-20 14:25:56 -0700250 for (i = 0; i < ARRAY_SIZE(suites); i++) {
Simon Glass78fd76b2025-01-20 14:25:33 -0700251 struct suite *ste = &suites[i];
252 long n_ent = ste->end - ste->start;
253
254 if (n_ent)
255 printf("%5ld %s\n", n_ent, ste->name);
256 else
257 printf("%5s %s\n", "?", ste->name);
258 }
Simon Glasscfb38f82025-01-20 14:25:30 -0700259 }
260
Simon Glassfb998c22022-10-29 19:47:12 -0600261 return 0;
262}
263
Simon Glass78fd76b2025-01-20 14:25:33 -0700264static struct suite *find_suite(const char *name)
265{
266 struct suite *ste;
267 int i;
268
269 for (i = 0, ste = suites; i < ARRAY_SIZE(suites); i++, ste++) {
270 if (!strcmp(ste->name, name))
271 return ste;
272 }
273
274 return NULL;
275}
276
Simon Glassed38aef2020-05-10 11:40:03 -0600277static int do_ut(struct cmd_tbl *cmdtp, int flag, int argc, char *const argv[])
Joe Hershberger11dd7cc2015-05-20 14:27:28 -0500278{
Simon Glass78fd76b2025-01-20 14:25:33 -0700279 struct suite *ste;
280 const char *name;
281 int ret;
Joe Hershberger11dd7cc2015-05-20 14:27:28 -0500282
283 if (argc < 2)
284 return CMD_RET_USAGE;
285
286 /* drop initial "ut" arg */
287 argc--;
288 argv++;
289
Simon Glass78fd76b2025-01-20 14:25:33 -0700290 name = argv[0];
291 if (!strcmp(name, "all")) {
292 ret = do_ut_all(cmdtp, flag, argc, argv);
293 } else if (!strcmp(name, "info")) {
294 ret = do_ut_info(cmdtp, flag, argc, argv);
295 } else {
296 ste = find_suite(argv[0]);
297 if (!ste) {
298 printf("Suite '%s' not found\n", argv[0]);
299 return CMD_RET_FAILURE;
300 }
Joe Hershberger11dd7cc2015-05-20 14:27:28 -0500301
Simon Glass78fd76b2025-01-20 14:25:33 -0700302 ret = run_suite(ste, cmdtp, flag, argc, argv);
303 }
304 if (ret)
305 return ret;
Joe Hershberger11dd7cc2015-05-20 14:27:28 -0500306
Simon Glass78fd76b2025-01-20 14:25:33 -0700307 return 0;
Joe Hershberger11dd7cc2015-05-20 14:27:28 -0500308}
309
Tom Rini03f146c2023-10-07 15:13:08 -0400310U_BOOT_LONGHELP(ut,
Simon Glass154bb8e2022-10-29 19:47:10 -0600311 "[-r] [-f] [<suite>] - run unit tests\n"
312 " -r<runs> Number of times to run each test\n"
313 " -f Force 'manual' tests to run as well\n"
314 " <suite> Test suite to run, or all\n"
315 "\n"
Simon Glassfb998c22022-10-29 19:47:12 -0600316 "\nOptions for <suite>:"
Simon Glass154bb8e2022-10-29 19:47:10 -0600317 "\nall - execute all enabled tests"
Simon Glasscfb38f82025-01-20 14:25:30 -0700318 "\ninfo [-s] - show info about tests [and suites]"
Simon Glass154bb8e2022-10-29 19:47:10 -0600319#ifdef CONFIG_CMD_ADDRMAP
320 "\naddrmap - very basic test of addrmap command"
321#endif
Marek Vasut2cd173c2023-05-31 03:03:58 +0200322#ifdef CONFIG_CMD_BDI
323 "\nbdinfo - bdinfo command"
324#endif
Heinrich Schuchardtb8b6c812018-08-31 21:31:28 +0200325#ifdef CONFIG_SANDBOX
Simon Glass154bb8e2022-10-29 19:47:10 -0600326 "\nbloblist - bloblist implementation"
Heinrich Schuchardtb8b6c812018-08-31 21:31:28 +0200327#endif
Simon Glassb255efc2022-04-24 23:31:24 -0600328#ifdef CONFIG_BOOTSTD
Simon Glass154bb8e2022-10-29 19:47:10 -0600329 "\nbootstd - standard boot implementation"
330#endif
Simon Glass621a97e2023-10-01 19:15:12 -0600331#ifdef CONFIG_CMDLINE
332 "\ncmd - test various commands"
333#endif
Simon Glass3394c3c2024-11-02 13:36:56 -0600334 "\ncommon - tests for common/ directory"
Joe Hershberger9dc1d712015-05-20 14:27:29 -0500335#ifdef CONFIG_UT_DM
Simon Glass154bb8e2022-10-29 19:47:10 -0600336 "\ndm - driver model"
Joe Hershberger9dc1d712015-05-20 14:27:29 -0500337#endif
Joe Hershberger26e038f2015-05-20 14:27:36 -0500338#ifdef CONFIG_UT_ENV
Simon Glass154bb8e2022-10-29 19:47:10 -0600339 "\nenv - environment"
Joe Hershberger26e038f2015-05-20 14:27:36 -0500340#endif
Simon Glassf3c6a1d2022-07-13 06:06:59 -0600341#ifdef CONFIG_CMD_FDT
Simon Glass154bb8e2022-10-29 19:47:10 -0600342 "\nfdt - fdt command"
Simon Glassf3c6a1d2022-07-13 06:06:59 -0600343#endif
Simon Glass9e972c32022-10-06 08:36:16 -0600344#ifdef CONFIG_CONSOLE_TRUETYPE
Marek Vasutef9eb6c2023-08-13 00:16:41 +0200345 "\nfont - font command"
Simon Glass154bb8e2022-10-29 19:47:10 -0600346#endif
Francis Laniel2dba91a2023-12-22 22:02:21 +0100347#if CONFIG_IS_ENABLED(HUSH_PARSER)
348 "\nhush - Test hush behavior"
349#endif
Simon Glass154bb8e2022-10-29 19:47:10 -0600350#ifdef CONFIG_CMD_LOADM
351 "\nloadm - loadm command parameters and loading memory blob"
Simon Glass9e972c32022-10-06 08:36:16 -0600352#endif
Heinrich Schuchardtf77a6352019-01-30 07:53:31 +0100353#ifdef CONFIG_UT_LIB
Simon Glass154bb8e2022-10-29 19:47:10 -0600354 "\nlib - library functions"
Heinrich Schuchardtf77a6352019-01-30 07:53:31 +0100355#endif
Heinrich Schuchardtf433d502020-02-26 21:48:18 +0100356#ifdef CONFIG_UT_LOG
Simon Glass154bb8e2022-10-29 19:47:10 -0600357 "\nlog - logging functions"
Heinrich Schuchardtf433d502020-02-26 21:48:18 +0100358#endif
Simon Glass154bb8e2022-10-29 19:47:10 -0600359 "\nmem - memory-related commands"
Heiko Stuebner1c9bb9b2019-10-23 16:46:41 +0200360#ifdef CONFIG_UT_OPTEE
Simon Glass154bb8e2022-10-29 19:47:10 -0600361 "\noptee - test OP-TEE"
Heiko Stuebner1c9bb9b2019-10-23 16:46:41 +0200362#endif
Maxime Ripard0e31a112016-07-05 10:26:46 +0200363#ifdef CONFIG_UT_OVERLAY
Simon Glass154bb8e2022-10-29 19:47:10 -0600364 "\noverlay - device tree overlays"
Maxime Ripard0e31a112016-07-05 10:26:46 +0200365#endif
Stephen Carlsone5c05ae2023-03-10 11:07:15 -0800366#ifdef CONFIG_CMD_PCI_MPS
367 "\npci_mps - PCI Express Maximum Payload Size"
368#endif
Simon Glass154bb8e2022-10-29 19:47:10 -0600369 "\nsetexpr - setexpr command"
Linus Walleijbef39252023-02-01 00:16:13 +0100370#ifdef CONFIG_CMD_SEAMA
371 "\nseama - seama command parameters loading and decoding"
372#endif
Tom Rini03f146c2023-10-07 15:13:08 -0400373 );
Joe Hershberger11dd7cc2015-05-20 14:27:28 -0500374
375U_BOOT_CMD(
376 ut, CONFIG_SYS_MAXARGS, 1, do_ut,
377 "unit tests", ut_help_text
378);