From: Ahmad Fatoum <a.fatoum@pengutronix.de> To: barebox@lists.infradead.org Cc: Ahmad Fatoum <a.fatoum@pengutronix.de> Subject: [PATCH 1/2] test: self: add simple environment variable test Date: Mon, 20 Jun 2022 09:23:39 +0200 [thread overview] Message-ID: <20220620072340.1461618-1-a.fatoum@pengutronix.de> (raw) setenv can now accept a format string. Add some tests for the old and new usage. Signed-off-by: Ahmad Fatoum <a.fatoum@pengutronix.de> --- test/self/Kconfig | 4 +++ test/self/Makefile | 1 + test/self/envvar.c | 82 ++++++++++++++++++++++++++++++++++++++++++++++ 3 files changed, 87 insertions(+) create mode 100644 test/self/envvar.c diff --git a/test/self/Kconfig b/test/self/Kconfig index cf11efe54486..a5eac85646b7 100644 --- a/test/self/Kconfig +++ b/test/self/Kconfig @@ -29,6 +29,7 @@ config SELFTEST_ENABLE_ALL bool "Enable all self-tests" select SELFTEST_PRINTF select SELFTEST_PROGRESS_NOTIFIER + select SELFTEST_ENVIRONMENT_VARIABLES if ENVIRONMENT_VARIABLES help Selects all self-tests compatible with current configuration @@ -51,4 +52,7 @@ config SELFTEST_OF_MANIPULATION config SELFTEST_PROGRESS_NOTIFIER bool "progress notifier selftest" +config SELFTEST_ENVIRONMENT_VARIABLES + bool "environment variable selftest" + endif diff --git a/test/self/Makefile b/test/self/Makefile index 65d01596b806..ca9f9c34d1e5 100644 --- a/test/self/Makefile +++ b/test/self/Makefile @@ -5,3 +5,4 @@ obj-$(CONFIG_SELFTEST_MALLOC) += malloc.o obj-$(CONFIG_SELFTEST_PRINTF) += printf.o obj-$(CONFIG_SELFTEST_PROGRESS_NOTIFIER) += progress-notifier.o obj-$(CONFIG_SELFTEST_OF_MANIPULATION) += of_manipulation.o of_manipulation.dtb.o +obj-$(CONFIG_SELFTEST_ENVIRONMENT_VARIABLES) += envvar.o diff --git a/test/self/envvar.c b/test/self/envvar.c new file mode 100644 index 000000000000..a686c6c2e206 --- /dev/null +++ b/test/self/envvar.c @@ -0,0 +1,82 @@ +// SPDX-License-Identifier: GPL-2.0-only + +#define pr_fmt(fmt) KBUILD_MODNAME ": " fmt + +#include <common.h> +#include <environment.h> +#include <bselftest.h> +#include <linux/sizes.h> + +BSELFTEST_GLOBALS(); + +static int strequal(const char *a, const char *b) +{ + if (!a || !b) + return a == b; + + return !strcmp(a, b); +} + +static void __expect_getenv(const char *var, const char *expect, + const char *func, int line) +{ + const char *val; + + total_tests++; + + val = getenv(var); + if (!IS_ENABLED(CONFIG_ENVIRONMENT_VARIABLES)) { + if (val == NULL) { + skipped_tests++; + return; + } + } + + if (!strequal(val, expect)) { + failed_tests++; + printf("%s:%d: failure: getenv(%s) == \"%s\", but \"%s\" expected\n", + func, line, var, val ?: "<NULL>", expect ?: "<NULL>"); + } +} + +#define expect_getenv(v, e) __expect_getenv(v, e, __func__, __LINE__) + +static void test_envvar(void) +{ + + if (IS_ENABLED(CONFIG_ENVIRONMENT_VARIABLES)) + pr_info("built without environment variable support: Skipping tests\n"); + + expect_getenv("__TEST_VAR1", NULL); + + setenv("__TEST_VAR1", "VALUE1"); + + expect_getenv("__TEST_VAR1", "VALUE1"); + + unsetenv("__TEST_VAR1"); + + expect_getenv("__TEST_VAR1", NULL); + + setenv("__TEST_VAR1", "VALUE1"); + + expect_getenv("__TEST_VAR1", "VALUE1"); + + setenv("__TEST_VAR1", "VALUE2"); + + expect_getenv("__TEST_VAR1", "VALUE2"); + + setenv("__TEST_VAR1", "1337"); + + expect_getenv("__TEST_VAR1", "1337"); + + setenv("__TEST_VAR1", "0x%s", "1337"); + + expect_getenv("__TEST_VAR1", "0x1337"); + + setenv("__TEST_VAR1", "%ux1%c%x", 0, '3', 0x37); + + expect_getenv("__TEST_VAR1", "0x1337"); + + unsetenv("__TEST_VAR1"); +} +bselftest(core, test_envvar); -- 2.30.2
next reply other threads:[~2022-06-20 7:25 UTC|newest] Thread overview: 2+ messages / expand[flat|nested] mbox.gz Atom feed top 2022-06-20 7:23 Ahmad Fatoum [this message] 2022-06-20 7:23 ` [PATCH 2/2] test: self: include new tests in CONFIG_SELFTEST_ENABLE_ALL Ahmad Fatoum
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=20220620072340.1461618-1-a.fatoum@pengutronix.de \ --to=a.fatoum@pengutronix.de \ --cc=barebox@lists.infradead.org \ --subject='Re: [PATCH 1/2] test: self: add simple environment variable test' \ /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
This is a public inbox, see mirroring instructions for how to clone and mirror all data and code used for this inbox