1963f3792cff33187e8eb984f8fc30923c93acb4
[platform/kernel/u-boot.git] / test / cmd_ut.c
1 // SPDX-License-Identifier: GPL-2.0
2 /*
3  * (C) Copyright 2015
4  * Joe Hershberger, National Instruments, joe.hershberger@ni.com
5  */
6
7 #include <common.h>
8 #include <command.h>
9 #include <console.h>
10 #include <test/suites.h>
11 #include <test/test.h>
12
13 static int do_ut_all(struct cmd_tbl *cmdtp, int flag, int argc,
14                      char *const argv[]);
15
16 int cmd_ut_category(const char *name, const char *prefix,
17                     struct unit_test *tests, int n_ents,
18                     int argc, char *const argv[])
19 {
20         struct unit_test_state uts = { .fail_count = 0 };
21         struct unit_test *test;
22         int prefix_len = prefix ? strlen(prefix) : 0;
23
24         if (argc == 1)
25                 printf("Running %d %s tests\n", n_ents, name);
26
27         for (test = tests; test < tests + n_ents; test++) {
28                 const char *test_name = test->name;
29
30                 /* Remove the prefix */
31                 if (prefix && !strncmp(test_name, prefix, prefix_len))
32                         test_name += prefix_len;
33
34                 if (argc > 1 && strcmp(argv[1], test_name))
35                         continue;
36                 printf("Test: %s\n", test->name);
37
38                 if (test->flags & UT_TESTF_CONSOLE_REC) {
39                         int ret = console_record_reset_enable();
40
41                         if (ret) {
42                                 printf("Skipping: Console recording disabled\n");
43                                 continue;
44                         }
45                 }
46
47                 uts.start = mallinfo();
48
49                 test->func(&uts);
50         }
51
52         printf("Failures: %d\n", uts.fail_count);
53
54         return uts.fail_count ? CMD_RET_FAILURE : 0;
55 }
56
57 static struct cmd_tbl cmd_ut_sub[] = {
58         U_BOOT_CMD_MKENT(all, CONFIG_SYS_MAXARGS, 1, do_ut_all, "", ""),
59 #if defined(CONFIG_UT_DM)
60         U_BOOT_CMD_MKENT(dm, CONFIG_SYS_MAXARGS, 1, do_ut_dm, "", ""),
61 #endif
62 #if defined(CONFIG_UT_ENV)
63         U_BOOT_CMD_MKENT(env, CONFIG_SYS_MAXARGS, 1, do_ut_env, "", ""),
64 #endif
65 #ifdef CONFIG_UT_OPTEE
66         U_BOOT_CMD_MKENT(optee, CONFIG_SYS_MAXARGS, 1, do_ut_optee, "", ""),
67 #endif
68 #ifdef CONFIG_UT_OVERLAY
69         U_BOOT_CMD_MKENT(overlay, CONFIG_SYS_MAXARGS, 1, do_ut_overlay, "", ""),
70 #endif
71 #ifdef CONFIG_UT_LIB
72         U_BOOT_CMD_MKENT(lib, CONFIG_SYS_MAXARGS, 1, do_ut_lib, "", ""),
73 #endif
74 #ifdef CONFIG_UT_LOG
75         U_BOOT_CMD_MKENT(log, CONFIG_SYS_MAXARGS, 1, do_ut_log, "", ""),
76 #endif
77 #ifdef CONFIG_UT_TIME
78         U_BOOT_CMD_MKENT(time, CONFIG_SYS_MAXARGS, 1, do_ut_time, "", ""),
79 #endif
80 #if CONFIG_IS_ENABLED(UT_UNICODE) && !defined(API_BUILD)
81         U_BOOT_CMD_MKENT(unicode, CONFIG_SYS_MAXARGS, 1, do_ut_unicode, "", ""),
82 #endif
83 #ifdef CONFIG_SANDBOX
84         U_BOOT_CMD_MKENT(compression, CONFIG_SYS_MAXARGS, 1, do_ut_compression,
85                          "", ""),
86         U_BOOT_CMD_MKENT(bloblist, CONFIG_SYS_MAXARGS, 1, do_ut_bloblist,
87                          "", ""),
88         U_BOOT_CMD_MKENT(str, CONFIG_SYS_MAXARGS, 1, do_ut_str,
89                          "", ""),
90 #endif
91 };
92
93 static int do_ut_all(struct cmd_tbl *cmdtp, int flag, int argc,
94                      char *const argv[])
95 {
96         int i;
97         int retval;
98         int any_fail = 0;
99
100         for (i = 1; i < ARRAY_SIZE(cmd_ut_sub); i++) {
101                 printf("----Running %s tests----\n", cmd_ut_sub[i].name);
102                 retval = cmd_ut_sub[i].cmd(cmdtp, flag, 1, &cmd_ut_sub[i].name);
103                 if (!any_fail)
104                         any_fail = retval;
105         }
106
107         return any_fail;
108 }
109
110 static int do_ut(struct cmd_tbl *cmdtp, int flag, int argc, char *const argv[])
111 {
112         struct cmd_tbl *cp;
113
114         if (argc < 2)
115                 return CMD_RET_USAGE;
116
117         /* drop initial "ut" arg */
118         argc--;
119         argv++;
120
121         cp = find_cmd_tbl(argv[0], cmd_ut_sub, ARRAY_SIZE(cmd_ut_sub));
122
123         if (cp)
124                 return cp->cmd(cmdtp, flag, argc, argv);
125
126         return CMD_RET_USAGE;
127 }
128
129 #ifdef CONFIG_SYS_LONGHELP
130 static char ut_help_text[] =
131         "all - execute all enabled tests\n"
132 #ifdef CONFIG_SANDBOX
133         "ut bloblist - Test bloblist implementation\n"
134         "ut compression - Test compressors and bootm decompression\n"
135 #endif
136 #ifdef CONFIG_UT_DM
137         "ut dm [test-name]\n"
138 #endif
139 #ifdef CONFIG_UT_ENV
140         "ut env [test-name]\n"
141 #endif
142 #ifdef CONFIG_UT_LIB
143         "ut lib [test-name] - test library functions\n"
144 #endif
145 #ifdef CONFIG_UT_LOG
146         "ut log [test-name] - test logging functions\n"
147 #endif
148 #ifdef CONFIG_UT_OPTEE
149         "ut optee [test-name]\n"
150 #endif
151 #ifdef CONFIG_UT_OVERLAY
152         "ut overlay [test-name]\n"
153 #endif
154 #ifdef CONFIG_SANDBOX
155         "ut str - Basic test of string functions\n"
156 #endif
157 #ifdef CONFIG_UT_TIME
158         "ut time - Very basic test of time functions\n"
159 #endif
160 #if defined(CONFIG_UT_UNICODE) && \
161         !defined(CONFIG_SPL_BUILD) && !defined(API_BUILD)
162         "ut unicode [test-name] - test Unicode functions\n"
163 #endif
164         ;
165 #endif /* CONFIG_SYS_LONGHELP */
166
167 U_BOOT_CMD(
168         ut, CONFIG_SYS_MAXARGS, 1, do_ut,
169         "unit tests", ut_help_text
170 );