These tests are specifically tailored around respecting the dst_len
parameter.

Signed-off-by: Jonas Rebmann <[email protected]>
---
 test/self/Kconfig  |  7 +++++++
 test/self/Makefile |  1 +
 test/self/base64.c | 45 +++++++++++++++++++++++++++++++++++++++++++++
 3 files changed, 53 insertions(+)

diff --git a/test/self/Kconfig b/test/self/Kconfig
index 936b12072e..adef8609ef 100644
--- a/test/self/Kconfig
+++ b/test/self/Kconfig
@@ -28,6 +28,7 @@ config SELFTEST_AUTORUN
 
 config SELFTEST_ENABLE_ALL
        bool "Enable all self-tests"
+       select SELFTEST_BASE64
        select SELFTEST_RANGE
        select SELFTEST_PRINTF
        select SELFTEST_MALLOC
@@ -52,6 +53,12 @@ config SELFTEST_ENABLE_ALL
        help
          Selects all self-tests compatible with current configuration
 
+config SELFTEST_BASE64
+       bool "base64 selftest"
+       select BASE64
+       help
+         Tests base64 implementation
+
 config SELFTEST_RANGE
        bool "range.h selftest"
        help
diff --git a/test/self/Makefile b/test/self/Makefile
index 0bd947928a..d244c19052 100644
--- a/test/self/Makefile
+++ b/test/self/Makefile
@@ -1,6 +1,7 @@
 # SPDX-License-Identifier: GPL-2.0
 
 obj-$(CONFIG_SELFTEST) += core.o
+obj-$(CONFIG_SELFTEST_BASE64) += base64.o
 obj-$(CONFIG_SELFTEST_RANGE) += range.o
 obj-$(CONFIG_SELFTEST_MALLOC) += malloc.o
 obj-$(CONFIG_SELFTEST_TALLOC) += talloc.o
diff --git a/test/self/base64.c b/test/self/base64.c
new file mode 100644
index 0000000000..c9140aedb9
--- /dev/null
+++ b/test/self/base64.c
@@ -0,0 +1,45 @@
+// SPDX-License-Identifier: GPL-2.0-only
+
+#define pr_fmt(fmt) KBUILD_MODNAME ": " fmt
+
+#include <common.h>
+#include <bselftest.h>
+#include <base64.h>
+#include <string.h>
+
+BSELFTEST_GLOBALS();
+
+static void __expect_base64(const char *func, int line, int dst_len,
+                          const char *src, int expect_len, const char *expect)
+{
+       int ret;
+       char *buf = strdup("canary");
+       bool fail = false;
+
+       total_tests++;
+       ret = decode_base64(buf, dst_len, src);
+       if (!streq_ptr(buf, expect)) {
+               fail = true;
+               printf("%s:%d: got '%s', but '%s' expected\n", func, line, buf,
+                      expect);
+       }
+       if (ret != expect_len) {
+               fail = true;
+               printf("%s:%d: got length %i, but %i expected\n", func, line,
+                      ret, expect_len);
+       }
+       if (fail)
+               failed_tests++;
+       free(buf);
+}
+
+#define expect_base64(dst_len, src, expect_len, expect) \
+       __expect_base64(__func__, __LINE__, dst_len, src, expect_len, expect)
+
+static void test_base64(void)
+{
+       expect_base64(1, "QUJD", 1, "Aanary");
+       expect_base64(5, "QUJD", 3, "ABCary");
+       expect_base64(5, "$UJD", 0, "canary");
+}
+bselftest(parser, test_base64);

-- 
2.51.2.535.g419c72cb8a


Reply via email to