From: Jonas Rebmann <jre@pengutronix.de>
To: Sascha Hauer <s.hauer@pengutronix.de>,
BAREBOX <barebox@lists.infradead.org>
Cc: Jonas Rebmann <jre@pengutronix.de>
Subject: [PATCH 2/2] test: self: add base64 selftests
Date: Mon, 01 Dec 2025 15:09:19 +0100 [thread overview]
Message-ID: <20251201-base64-bounds-v1-2-3ae2b2e8b7cb@pengutronix.de> (raw)
In-Reply-To: <20251201-base64-bounds-v1-0-3ae2b2e8b7cb@pengutronix.de>
These tests are specifically tailored around respecting the dst_len
parameter.
Signed-off-by: Jonas Rebmann <jre@pengutronix.de>
---
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..aedd5c6b58
--- /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_streq(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_streq(__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
prev parent reply other threads:[~2025-12-01 14:10 UTC|newest]
Thread overview: 3+ messages / expand[flat|nested] mbox.gz Atom feed top
2025-12-01 14:09 [PATCH 0/2] Fix out-of-bounds potential in decode_base64 and add regression tests Jonas Rebmann
2025-12-01 14:09 ` [PATCH 1/2] lib: base64: Fix out-of-bounds potential by respecting dst_len Jonas Rebmann
2025-12-01 14:09 ` Jonas Rebmann [this message]
Reply instructions:
You may reply publicly to this message via plain-text email
using any one of the following methods:
* Save the following mbox file, import it into your mail client,
and reply-to-all from there: mbox
Avoid top-posting and favor interleaved quoting:
https://en.wikipedia.org/wiki/Posting_style#Interleaved_style
* Reply using the --to, --cc, and --in-reply-to
switches of git-send-email(1):
git send-email \
--in-reply-to=20251201-base64-bounds-v1-2-3ae2b2e8b7cb@pengutronix.de \
--to=jre@pengutronix.de \
--cc=barebox@lists.infradead.org \
--cc=s.hauer@pengutronix.de \
/path/to/YOUR_REPLY
https://kernel.org/pub/software/scm/git/docs/git-send-email.html
* If your mail client supports setting the In-Reply-To header
via mailto: links, try the mailto: link
Be sure your reply has a Subject: header at the top and a blank line
before the message body.
This is a public inbox, see mirroring instructions
for how to clone and mirror all data and code used for this inbox