With spec-conformant sorting added for bootloader specification entries,
add a simple self test that verifies that the sort order is as one would
expect.

Signed-off-by: Ahmad Fatoum <[email protected]>
---
 include/bselftest.h                           | 20 ++++++++
 test/self/Kconfig                             |  5 ++
 test/self/Makefile                            |  2 +
 test/self/blspec.c                            | 46 +++++++++++++++++++
 .../data/test/boot/boot.sh                    |  3 ++
 .../data/test/loader/entries/boarda.conf      |  6 +++
 .../data/test/loader/entries/boardb.conf      |  6 +++
 .../data/test/loader/entries/boardc.conf      |  6 +++
 .../data/test/loader/entries/boardd.conf      |  5 ++
 9 files changed, 99 insertions(+)
 create mode 100644 test/self/blspec.c
 create mode 100755 test/self/defaultenv-blspec-test/data/test/boot/boot.sh
 create mode 100644 
test/self/defaultenv-blspec-test/data/test/loader/entries/boarda.conf
 create mode 100644 
test/self/defaultenv-blspec-test/data/test/loader/entries/boardb.conf
 create mode 100644 
test/self/defaultenv-blspec-test/data/test/loader/entries/boardc.conf
 create mode 100644 
test/self/defaultenv-blspec-test/data/test/loader/entries/boardd.conf

diff --git a/include/bselftest.h b/include/bselftest.h
index c3f323864358..fc698d7c5c0c 100644
--- a/include/bselftest.h
+++ b/include/bselftest.h
@@ -78,4 +78,24 @@ static inline void selftests_run(void)
 int selftest_run(struct selftest *test);
 bool selftest_is_running(struct selftest *test);
 
+#define __assert_cond(cond) ({ total_tests++; cond || (failed_tests++, false); 
})
+
+#define assert_cond(cond) ({ \
+       bool __cond = __assert_cond(cond); \
+       if (!__cond) \
+               pr_warn("%s:%d: condition %s unexpectedly false\n", \
+                       __func__, __LINE__, #cond); \
+       __cond; \
+})
+
+#define assert_inteq(a, b) assert_cond(a == b)
+
+#define assert_streq(a, b) ({ \
+       bool __cond = __assert_cond(strcmp(a, b) == 0); \
+       if (!__cond) \
+               pr_warn("%s:%d: %s and %s unexpectedly unequal\n", \
+                       __func__, __LINE__, a, b); \
+       __cond; \
+})
+
 #endif
diff --git a/test/self/Kconfig b/test/self/Kconfig
index 2ccdfe621821..85a3ef790116 100644
--- a/test/self/Kconfig
+++ b/test/self/Kconfig
@@ -51,6 +51,7 @@ config SELFTEST_ENABLE_ALL
        select SELFTEST_TLV
        select SELFTEST_DM
        select SELFTEST_TALLOC
+       select SELFTEST_BLSPEC if BLSPEC && DEFAULT_ENVIRONMENT
        help
          Selects all self-tests compatible with current configuration
 
@@ -127,6 +128,10 @@ config SELFTEST_SETJMP
        bool "setjmp/longjmp library selftest"
        depends on ARCH_HAS_SJLJ
 
+config SELFTEST_BLSPEC
+       bool "bootloader spec selftest"
+       depends on BLSPEC && DEFAULT_ENVIRONMENT
+
 config SELFTEST_REGULATOR
        bool "Regulator selftest"
        depends on REGULATOR_FIXED
diff --git a/test/self/Makefile b/test/self/Makefile
index 9f839fc0d07a..2bfdbb9949df 100644
--- a/test/self/Makefile
+++ b/test/self/Makefile
@@ -25,6 +25,8 @@ obj-$(CONFIG_SELFTEST_TEST_COMMAND) += test_command.o
 obj-$(CONFIG_SELFTEST_IDR) += idr.o
 obj-$(CONFIG_SELFTEST_TLV) += tlv.o tlv.dtb.o
 obj-$(CONFIG_SELFTEST_DM) += dm.o
+obj-$(CONFIG_SELFTEST_BLSPEC) += blspec.o
+bbenv-$(CONFIG_SELFTEST_BLSPEC) += defaultenv-blspec-test
 
 ifdef REGENERATE_KEYTOC
 
diff --git a/test/self/blspec.c b/test/self/blspec.c
new file mode 100644
index 000000000000..0e9eca4d06e9
--- /dev/null
+++ b/test/self/blspec.c
@@ -0,0 +1,46 @@
+// SPDX-License-Identifier: GPL-2.0-only
+
+#define pr_fmt(fmt) KBUILD_MODNAME ": " fmt
+
+#include <common.h>
+#include <bselftest.h>
+#include <boot.h>
+#include <envfs.h>
+#include <init.h>
+
+BSELFTEST_GLOBALS();
+
+static void test_blspec(void)
+{
+       struct bootentries *entries;
+       struct bootentry *entry;
+       int ret, i = 0;
+       const char *expected[] = {
+               "/env/data/test/loader/entries/boardb.conf",
+               "/env/data/test/loader/entries/boardc.conf",
+               "/env/data/test/loader/entries/boarda.conf",
+               "/env/data/test/loader/entries/boardd.conf",
+       };
+
+       entries = bootentries_alloc();
+
+       ret = bootentry_create_from_name(entries, "/env/data/test");
+       if (!assert_inteq(ret, 4))
+               return;
+
+       if (!assert_cond(!list_empty(&entries->entries)))
+               return;
+
+       bootentries_for_each_entry(entries, entry) {
+               assert_streq(expected[i], entry->path);
+               i++;
+       }
+}
+bselftest(parser, test_blspec);
+
+static int test_blspec_env_init(void)
+{
+       defaultenv_append_directory(defaultenv_blspec_test);
+       return 0;
+}
+late_initcall(test_blspec_env_init);
diff --git a/test/self/defaultenv-blspec-test/data/test/boot/boot.sh 
b/test/self/defaultenv-blspec-test/data/test/boot/boot.sh
new file mode 100755
index 000000000000..19ba0024098b
--- /dev/null
+++ b/test/self/defaultenv-blspec-test/data/test/boot/boot.sh
@@ -0,0 +1,3 @@
+#!/bin/sh
+
+echo boot test
diff --git 
a/test/self/defaultenv-blspec-test/data/test/loader/entries/boarda.conf 
b/test/self/defaultenv-blspec-test/data/test/loader/entries/boarda.conf
new file mode 100644
index 000000000000..a0b8356476be
--- /dev/null
+++ b/test/self/defaultenv-blspec-test/data/test/loader/entries/boarda.conf
@@ -0,0 +1,6 @@
+title          barebox - Test Image - v6.1-rc2 A
+version                Dummy v6.1-rc2
+options                rootwait
+linux          /boot/boot.sh
+sort-key        blspec-test
+linux-appendroot       true
diff --git 
a/test/self/defaultenv-blspec-test/data/test/loader/entries/boardb.conf 
b/test/self/defaultenv-blspec-test/data/test/loader/entries/boardb.conf
new file mode 100644
index 000000000000..93d5ab5f0fac
--- /dev/null
+++ b/test/self/defaultenv-blspec-test/data/test/loader/entries/boardb.conf
@@ -0,0 +1,6 @@
+title          barebox - Test Image - v6.19 B
+version                Dummy v6.19
+options                rootwait
+linux          /boot/boot.sh
+sort-key        blspec-test
+linux-appendroot       true
diff --git 
a/test/self/defaultenv-blspec-test/data/test/loader/entries/boardc.conf 
b/test/self/defaultenv-blspec-test/data/test/loader/entries/boardc.conf
new file mode 100644
index 000000000000..8df597d4f251
--- /dev/null
+++ b/test/self/defaultenv-blspec-test/data/test/loader/entries/boardc.conf
@@ -0,0 +1,6 @@
+title          barebox - Test Image - v6.9 C
+version                Dummy v6.9
+options                rootwait
+linux          /boot/boot.sh
+sort-key        blspec-test
+linux-appendroot       true
diff --git 
a/test/self/defaultenv-blspec-test/data/test/loader/entries/boardd.conf 
b/test/self/defaultenv-blspec-test/data/test/loader/entries/boardd.conf
new file mode 100644
index 000000000000..b2581147c3d7
--- /dev/null
+++ b/test/self/defaultenv-blspec-test/data/test/loader/entries/boardd.conf
@@ -0,0 +1,5 @@
+title          barebox - Test Image - v9999 (no sort-key) D
+version                v9999
+options                rootwait
+linux          /boot/boot.sh
+linux-appendroot       true
-- 
2.47.3


Reply via email to