setenv can now accept a format string. Add some tests for the old and new usage. Signed-off-by: Ahmad Fatoum <a.fatoum@xxxxxxxxxxxxxx> --- v1 -> v2: - use pr_setenv that made it upstream instead of varargs setenv --- 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..a4620f0437f0 --- /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"); + + pr_setenv("__TEST_VAR1", "0x%s", "1337"); + + expect_getenv("__TEST_VAR1", "0x1337"); + + pr_setenv("__TEST_VAR1", "%ux1%c%x", 0, '3', 0x37); + + expect_getenv("__TEST_VAR1", "0x1337"); + + unsetenv("__TEST_VAR1"); +} +bselftest(core, test_envvar); -- 2.30.2