summaryrefslogtreecommitdiffstats
path: root/commands
diff options
context:
space:
mode:
authorAhmad Fatoum <a.fatoum@pengutronix.de>2021-06-04 10:46:58 +0200
committerSascha Hauer <s.hauer@pengutronix.de>2021-06-09 12:20:05 +0200
commit7457c3d1d457922772e8bb7e07e3e9fe7dd0e09d (patch)
treea18ab27a128c0e99c153c16b64e19ded98557ca5 /commands
parentff10a9966637f65efe2c82f65ad2b76e34f3774c (diff)
downloadbarebox-7457c3d1d457922772e8bb7e07e3e9fe7dd0e09d.tar.gz
barebox-7457c3d1d457922772e8bb7e07e3e9fe7dd0e09d.tar.xz
test: add basic barebox self-test infrastructure
Self tests is code written to run within barebox to exercise functionality. They offer flexibility to test specific units of barebox instead of the program as a whole. Add a very simple infrastructure for registering and executing self-tests. This is based on the Linux kselftest modules. We don't utilize modules for this, however, because we only have module support on ARM, but we need a generic solution. Selftests can be enabled individually and even tested without shell support to allow tests to happen for size-restricted barebox images as well. Acked-by: Rouven Czerwinski <r.czerwinski@pengutronix.de> Signed-off-by: Ahmad Fatoum <a.fatoum@pengutronix.de> Link: https://lore.barebox.org/20210604084704.17410-8-a.fatoum@pengutronix.de Signed-off-by: Sascha Hauer <s.hauer@pengutronix.de>
Diffstat (limited to 'commands')
-rw-r--r--commands/Makefile1
-rw-r--r--commands/selftest.c88
2 files changed, 89 insertions, 0 deletions
diff --git a/commands/Makefile b/commands/Makefile
index 447349fd15..4b45d266fd 100644
--- a/commands/Makefile
+++ b/commands/Makefile
@@ -130,5 +130,6 @@ obj-$(CONFIG_CMD_SEED) += seed.o
obj-$(CONFIG_CMD_IP_ROUTE_GET) += ip-route-get.o
obj-$(CONFIG_CMD_BTHREAD) += bthread.o
obj-$(CONFIG_CMD_UBSAN) += ubsan.o
+obj-$(CONFIG_CMD_SELFTEST) += selftest.o
UBSAN_SANITIZE_ubsan.o := y
diff --git a/commands/selftest.c b/commands/selftest.c
new file mode 100644
index 0000000000..a10f1467fe
--- /dev/null
+++ b/commands/selftest.c
@@ -0,0 +1,88 @@
+/* SPDX-License-Identifier: GPL-2.0 */
+
+#define pr_fmt(fmt) "selftest: " fmt
+
+#include <common.h>
+#include <command.h>
+#include <getopt.h>
+#include <bselftest.h>
+#include <complete.h>
+
+static int run_selftest(const char *match, bool list)
+{
+ struct selftest *test;
+ int matches = 0;
+ int err = 0;
+
+ list_for_each_entry(test, &selftests, list) {
+ if (list) {
+ printf("%s\n", test->name);
+ matches++;
+ continue;
+ }
+
+ if (match && strcmp(test->name, match))
+ continue;
+
+ err |= test->func();
+ matches++;
+ }
+
+ if (!matches) {
+ if (match) {
+ printf("No tests matching '%s' found.\n", match);
+ return -EINVAL;
+ }
+
+ printf("No tests found.\n");
+ }
+
+ return err;
+}
+
+static int do_selftest(int argc, char *argv[])
+{
+ bool list = false;
+ int i, err = 0;
+ int opt;
+
+ while((opt = getopt(argc, argv, "l")) > 0) {
+ switch(opt) {
+ case 'l':
+ list = true;
+ break;
+ default:
+ return COMMAND_ERROR_USAGE;
+ }
+ }
+
+ if (optind == argc) {
+ err = run_selftest(NULL, list);
+ } else {
+ for (i = optind; i < argc; i++) {
+ err = run_selftest(argv[i], list);
+ if (err)
+ goto out;
+ }
+ }
+
+out:
+ return err ? COMMAND_ERROR : COMMAND_SUCCESS;
+}
+
+BAREBOX_CMD_HELP_START(selftest)
+BAREBOX_CMD_HELP_TEXT("Run enabled barebox self-tests")
+BAREBOX_CMD_HELP_TEXT("If run without arguments, all tests are run")
+BAREBOX_CMD_HELP_TEXT("")
+BAREBOX_CMD_HELP_TEXT("Options:")
+BAREBOX_CMD_HELP_OPT ("-l", "list available tests")
+BAREBOX_CMD_HELP_END
+
+BAREBOX_CMD_START(selftest)
+ .cmd = do_selftest,
+ BAREBOX_CMD_DESC("Run selftests")
+ BAREBOX_CMD_OPTS("[-l] [tests..]")
+ BAREBOX_CMD_GROUP(CMD_GRP_MISC)
+ BAREBOX_CMD_COMPLETE(empty_complete)
+ BAREBOX_CMD_HELP(cmd_selftest_help)
+BAREBOX_CMD_END