[PATCH 5/5] test: add tests for default environment

Rasmus Villemoes rasmus.villemoes at prevas.dk
Tue Nov 10 21:26:03 CET 2020


Signed-off-by: Rasmus Villemoes <rasmus.villemoes at prevas.dk>
---
 configs/sandbox64_defconfig |   1 +
 configs/sandbox_defconfig   |   1 +
 include/configs/sandbox.h   |   8 ++-
 test/env/Makefile           |   1 +
 test/env/default.c          | 102 ++++++++++++++++++++++++++++++++++++
 5 files changed, 112 insertions(+), 1 deletion(-)
 create mode 100644 test/env/default.c

diff --git a/configs/sandbox64_defconfig b/configs/sandbox64_defconfig
index dc993cd13a..fa8f5190ed 100644
--- a/configs/sandbox64_defconfig
+++ b/configs/sandbox64_defconfig
@@ -91,6 +91,7 @@ CONFIG_ENV_IS_NOWHERE=y
 CONFIG_ENV_IS_IN_EXT4=y
 CONFIG_ENV_EXT4_INTERFACE="host"
 CONFIG_ENV_EXT4_DEVICE_AND_PART="0:0"
+CONFIG_ENV_AMEND_DEFAULT_FROM_FDT=y
 CONFIG_BOOTP_SEND_HOSTNAME=y
 CONFIG_NETCONSOLE=y
 CONFIG_IP_DEFRAG=y
diff --git a/configs/sandbox_defconfig b/configs/sandbox_defconfig
index f2a767a4cd..f1a4ecbf01 100644
--- a/configs/sandbox_defconfig
+++ b/configs/sandbox_defconfig
@@ -105,6 +105,7 @@ CONFIG_ENV_IS_NOWHERE=y
 CONFIG_ENV_IS_IN_EXT4=y
 CONFIG_ENV_EXT4_INTERFACE="host"
 CONFIG_ENV_EXT4_DEVICE_AND_PART="0:0"
+CONFIG_ENV_AMEND_DEFAULT_FROM_FDT=y
 CONFIG_BOOTP_SEND_HOSTNAME=y
 CONFIG_NETCONSOLE=y
 CONFIG_IP_DEFRAG=y
diff --git a/include/configs/sandbox.h b/include/configs/sandbox.h
index e0708fe573..928a80d872 100644
--- a/include/configs/sandbox.h
+++ b/include/configs/sandbox.h
@@ -106,11 +106,17 @@
 	"scriptaddr=0x1000\0" \
 	"pxefile_addr_r=0x2000\0"
 
+#define TEST_ENV_SETTINGS \
+	"test0=a\0"	  \
+	"test1=b\0"	  \
+	"test2=c\0"
+
 #define CONFIG_EXTRA_ENV_SETTINGS \
 	SANDBOX_SERIAL_SETTINGS \
 	SANDBOX_ETH_SETTINGS \
 	BOOTENV \
-	MEM_LAYOUT_ENV_SETTINGS
+	MEM_LAYOUT_ENV_SETTINGS \
+	TEST_ENV_SETTINGS
 
 #ifndef CONFIG_SPL_BUILD
 #define CONFIG_SYS_IDE_MAXBUS		1
diff --git a/test/env/Makefile b/test/env/Makefile
index 5c8eae31b0..740b9c522e 100644
--- a/test/env/Makefile
+++ b/test/env/Makefile
@@ -5,3 +5,4 @@
 obj-y += cmd_ut_env.o
 obj-y += attr.o
 obj-y += hashtable.o
+obj-y += default.o
diff --git a/test/env/default.c b/test/env/default.c
new file mode 100644
index 0000000000..36fae5d783
--- /dev/null
+++ b/test/env/default.c
@@ -0,0 +1,102 @@
+// SPDX-License-Identifier: GPL-2.0
+
+#include <common.h>
+#include <command.h>
+#include <env_attr.h>
+#include <test/env.h>
+#include <test/ut.h>
+#include <fdt_support.h>
+
+DECLARE_GLOBAL_DATA_PTR;
+
+static char *testvars[] = {"test0", "test1", "test2", "test3"};
+
+static int test_amend_default_from_fdt(struct unit_test_state *uts)
+{
+	static const char def_env[] = "test1=x\0test2=\0test3=y";
+	void *blob = (void*)gd->fdt_blob;
+	const char *val;
+
+	ut_assert(fdt_find_or_add_subnode(blob, 0, "config") >= 0);
+	ut_assertok(fdt_find_and_setprop(blob, "/config", "default-environment",
+					  def_env, sizeof(def_env), 1));
+
+	env_set("test0", NULL);
+	env_set_default_vars(ARRAY_SIZE(testvars), testvars, 0);
+
+	val = env_get("test0");
+	ut_assertnonnull(val);
+	ut_asserteq_str("a", val);
+
+	val = env_get("test1");
+	ut_assertnonnull(val);
+	ut_asserteq_str("x", val);
+
+	val = env_get("test2");
+	ut_assertnull(val);
+
+	val = env_get("test3");
+	ut_assertnonnull(val);
+	ut_asserteq_str("y", val);
+
+	return 0;
+}
+
+static int env_test_default(struct unit_test_state *uts)
+{
+	const char *val;
+	int ret = 0;
+	void *blob;
+	const void *orig_blob;
+	int blob_sz;
+	int i;
+
+	for (i = 0; i < ARRAY_SIZE(testvars); ++i) {
+		ut_assertok(env_set(testvars[i], "ggg"));
+		val = env_get(testvars[i]);
+		ut_assertnonnull(val);
+		ut_asserteq_str(val, "ggg");
+	}
+
+	env_set_default_vars(ARRAY_SIZE(testvars), testvars, 0);
+
+	val = env_get("test0");
+	ut_assertnonnull(val);
+	ut_asserteq_str("a", val);
+
+	val = env_get("test1");
+	ut_assertnonnull(val);
+	ut_asserteq_str("b", val);
+
+	val = env_get("test2");
+	ut_assertnonnull(val);
+	ut_asserteq_str("c", val);
+
+	/*
+	 * env_set_default_vars() leaves existing variables alone that
+	 * are not defined in the default environment.
+	 */
+	val = env_get("test3");
+	ut_assertnonnull(val);
+	ut_asserteq_str("ggg", val);
+
+	if (!IS_ENABLED(CONFIG_ENV_AMEND_DEFAULT_FROM_FDT))
+		return ret;
+
+	blob_sz = fdt_totalsize(gd->fdt_blob) + 256;
+	blob = malloc(blob_sz);
+	ut_assertnonnull(blob);
+
+	/* Make a writable copy of the fdt blob */
+	ut_assertok(fdt_open_into(gd->fdt_blob, blob, blob_sz));
+	orig_blob = gd->fdt_blob;
+	gd->fdt_blob = blob;
+
+	ret = test_amend_default_from_fdt(uts);
+
+	gd->fdt_blob = orig_blob;
+	free(blob);
+
+	return ret;
+}
+ENV_TEST(env_test_default, 0);
-- 
2.23.0



More information about the U-Boot mailing list