[PATCH v4 13/13] test: Add some tests for kconfig.h

Simon Glass sjg at chromium.org
Sun Jan 16 21:19:37 CET 2022


The macros in this file are a little confusing and we currently have no
tests to check that they work as expected.

Add some tests which check the macros in C code. Add a few tests which
check that the build errors are generated correctly too, using buildman's
-a option.

Signed-off-by: Simon Glass <sjg at chromium.org>
---

Changes in v4:
- Expand the series with tests and buildman changes

 test/Kconfig                  |  1 +
 test/Makefile                 |  2 +-
 test/lib/Kconfig              | 23 ++++++++++++++
 test/lib/Makefile             |  5 +++
 test/lib/kconfig.c            | 58 +++++++++++++++++++++++++++++++++++
 test/lib/kconfig_spl.c        | 44 ++++++++++++++++++++++++++
 test/py/tests/test_kconfig.py | 34 ++++++++++++++++++++
 7 files changed, 166 insertions(+), 1 deletion(-)
 create mode 100644 test/lib/Kconfig
 create mode 100644 test/lib/kconfig.c
 create mode 100644 test/lib/kconfig_spl.c
 create mode 100644 test/py/tests/test_kconfig.py

diff --git a/test/Kconfig b/test/Kconfig
index e15ba239eb3..4137e7eabf5 100644
--- a/test/Kconfig
+++ b/test/Kconfig
@@ -97,5 +97,6 @@ config UT_UNICODE
 
 source "test/dm/Kconfig"
 source "test/env/Kconfig"
+source "test/lib/Kconfig"
 source "test/optee/Kconfig"
 source "test/overlay/Kconfig"
diff --git a/test/Makefile b/test/Makefile
index b3b2902e2e7..379f1e9fda7 100644
--- a/test/Makefile
+++ b/test/Makefile
@@ -16,6 +16,7 @@ obj-$(CONFIG_$(SPL_)CMDLINE) += cmd_ut.o
 obj-$(CONFIG_$(SPL_)CMDLINE) += command_ut.o
 obj-$(CONFIG_$(SPL_)UT_COMPRESSION) += compression.o
 obj-y += dm/
+obj-$(CONFIG_UNIT_TEST) += lib/
 obj-$(CONFIG_$(SPL_)CMDLINE) += print_ut.o
 obj-$(CONFIG_$(SPL_)CMDLINE) += str_ut.o
 obj-$(CONFIG_UT_TIME) += time_ut.o
@@ -23,7 +24,6 @@ obj-y += ut.o
 
 ifeq ($(CONFIG_SPL_BUILD),)
 obj-$(CONFIG_UNIT_TEST) += common/
-obj-$(CONFIG_UNIT_TEST) += lib/
 obj-y += log/
 obj-$(CONFIG_$(SPL_)UT_UNICODE) += unicode_ut.o
 endif
diff --git a/test/lib/Kconfig b/test/lib/Kconfig
new file mode 100644
index 00000000000..dbb03e4a36f
--- /dev/null
+++ b/test/lib/Kconfig
@@ -0,0 +1,23 @@
+# SPDX-License-Identifier: GPL-2.0+
+# Copyright 2022 Google LLC
+
+if SANDBOX
+
+config TEST_KCONFIG
+	bool "Enable detection of Kconfig macro errors"
+	help
+	  This is used to test that the IF_ENABLED_INT() macro causes a build error
+	  if the value is used when the CONFIG Is not enabled.
+
+config TEST_KCONFIG_ENABLE
+	bool "Option to enable"
+	help
+	  This is the option that controls whether the value is present.
+
+config TEST_KCONFIG_VALUE
+	int "Value associated with the option"
+	depends on TEST_KCONFIG_ENABLE
+	help
+	  This is the value whgch is present if TEST_KCONFIG_ENABLE is enabled.
+
+endif # SANDBOX
diff --git a/test/lib/Makefile b/test/lib/Makefile
index d244bb431d4..7e7922fe3b4 100644
--- a/test/lib/Makefile
+++ b/test/lib/Makefile
@@ -2,11 +2,13 @@
 #
 # (C) Copyright 2018
 # Mario Six, Guntermann & Drunck GmbH, mario.six at gdsys.cc
+ifeq ($(CONFIG_SPL_BUILD),)
 obj-y += cmd_ut_lib.o
 obj-y += abuf.o
 obj-$(CONFIG_EFI_LOADER) += efi_device_path.o
 obj-$(CONFIG_EFI_SECURE_BOOT) += efi_image_region.o
 obj-y += hexdump.o
+obj-$(CONFIG_SANDBOX) += kconfig.o
 obj-y += lmb.o
 obj-y += longjmp.o
 obj-$(CONFIG_CONSOLE_RECORD) += test_print.o
@@ -19,3 +21,6 @@ obj-$(CONFIG_UT_LIB_RSA) += rsa.o
 obj-$(CONFIG_AES) += test_aes.o
 obj-$(CONFIG_GETOPT) += getopt.o
 obj-$(CONFIG_UT_LIB_CRYPT) += test_crypt.o
+else
+obj-$(CONFIG_SANDBOX) += kconfig_spl.o
+endif
diff --git a/test/lib/kconfig.c b/test/lib/kconfig.c
new file mode 100644
index 00000000000..472d2c57280
--- /dev/null
+++ b/test/lib/kconfig.c
@@ -0,0 +1,58 @@
+// SPDX-License-Identifier: GPL-2.0+
+/*
+ * Test of linux/kconfig.h macros
+ *
+ * Copyright 2022 Google LLC
+ * Written by Simon Glass <sjg at chromium.org>
+ */
+
+#include <common.h>
+#include <test/lib.h>
+#include <test/test.h>
+#include <test/ut.h>
+
+static int lib_test_is_enabled(struct unit_test_state *uts)
+{
+	ulong val;
+
+	ut_asserteq(1, IS_ENABLED(CONFIG_CMDLINE))
+	ut_asserteq(0, IS_ENABLED(CONFIG__UNDEFINED))
+
+	ut_asserteq(1, CONFIG_IS_ENABLED(CMDLINE))
+	ut_asserteq(0, CONFIG_IS_ENABLED(OF_PLATDATA))
+	ut_asserteq(0, CONFIG_IS_ENABLED(_UNDEFINED))
+
+	ut_asserteq(0xc000,
+		    IF_ENABLED_INT(CONFIG_BLOBLIST_FIXED, CONFIG_BLOBLIST_ADDR));
+	ut_asserteq(0xc000,
+		    CONFIG_IF_ENABLED_INT(BLOBLIST_FIXED, BLOBLIST_ADDR));
+
+	/*
+	 * This fails if CONFIG_TEST_KCONFIG_ENABLE is not enabled, since the
+	 * value is used. Disable for SPL so that the errors in kconfig_spl.c
+	 * are detected, since otherwise a build error when building U-Boot may
+	 * cause SPL to not be built.
+	 */
+	if (!IS_ENABLED(CONFIG_SANDBOX_SPL) &&
+	    IS_ENABLED(CONFIG_TEST_KCONFIG)) {
+		val = IF_ENABLED_INT(CONFIG_TEST_KCONFIG_ENABLE,
+				     CONFIG_TEST_KCONFIG_VALUE);
+		printf("value %ld\n", val);
+	}
+
+	/*
+	 * This fails if CONFIG_TEST_KCONFIG_ENABLE is not enabled, since the
+	 * value is used. Disable for SPL so that the errors in kconfig_spl.c
+	 * are detected, since otherwise a build error when building U-Boot may
+	 * cause SPL to not be built.
+	 */
+	if (!IS_ENABLED(CONFIG_SANDBOX_SPL) &&
+	    CONFIG_IS_ENABLED(TEST_KCONFIG)) {
+		val = CONFIG_IF_ENABLED_INT(TEST_KCONFIG_ENABLE,
+					    TEST_KCONFIG_VALUE);
+		printf("value2 %ld\n", val);
+	}
+
+	return 0;
+}
+LIB_TEST(lib_test_is_enabled, 0);
diff --git a/test/lib/kconfig_spl.c b/test/lib/kconfig_spl.c
new file mode 100644
index 00000000000..c89ceaec66f
--- /dev/null
+++ b/test/lib/kconfig_spl.c
@@ -0,0 +1,44 @@
+// SPDX-License-Identifier: GPL-2.0+
+/*
+ * Test of linux/kconfig.h macros for SPL
+ *
+ * Copyright 2022 Google LLC
+ * Written by Simon Glass <sjg at chromium.org>
+ */
+
+#include <common.h>
+#include <test/lib.h>
+#include <test/test.h>
+#include <test/ut.h>
+
+static int lib_test_spl_is_enabled(struct unit_test_state *uts)
+{
+	ulong val;
+
+	ut_asserteq(0, CONFIG_IS_ENABLED(CMDLINE))
+	ut_asserteq(1, CONFIG_IS_ENABLED(OF_PLATDATA))
+	ut_asserteq(0, CONFIG_IS_ENABLED(_UNDEFINED))
+
+	/*
+	 * This fails if CONFIG_TEST_KCONFIG_ENABLE is not enabled, since the
+	 * value is used.
+	 */
+	if (IS_ENABLED(CONFIG_TEST_KCONFIG)) {
+		val = IF_ENABLED_INT(CONFIG_TEST_KCONFIG_ENABLE,
+				     CONFIG_TEST_KCONFIG_VALUE);
+		printf("value %ld\n", val);
+	}
+
+	/*
+	 * This fails if CONFIG_TEST_KCONFIG_ENABLE is not enabled, since the
+	 * value is used.
+	 */
+	if (CONFIG_IS_ENABLED(TEST_KCONFIG)) {
+		val = CONFIG_IF_ENABLED_INT(TEST_KCONFIG_ENABLE,
+					    TEST_KCONFIG_VALUE);
+		printf("value2 %ld\n", val);
+	}
+
+	return 0;
+}
+LIB_TEST(lib_test_spl_is_enabled, 0);
diff --git a/test/py/tests/test_kconfig.py b/test/py/tests/test_kconfig.py
new file mode 100644
index 00000000000..43ab4824334
--- /dev/null
+++ b/test/py/tests/test_kconfig.py
@@ -0,0 +1,34 @@
+# SPDX-License-Identifier: GPL-2.0
+# Copyright 2022 Google LLC
+# Written by Simon Glass <sjg at chromium.org>
+
+import pytest
+
+import u_boot_utils as util
+
+ at pytest.mark.slow
+def test_kconfig(u_boot_console):
+    """Test build failures when IF_ENABLED_INT() option is not enabled"""
+    cons = u_boot_console
+
+    # This detects build errors in test/lib/kconfig.c
+    out = util.run_and_log(
+        cons, ['./tools/buildman/buildman', '--board', 'sandbox',
+               '-a', 'TEST_KCONFIG'], ignore_errors=True)
+    assert 'invalid_use_of_IF_ENABLED_INT' in out
+    assert 'invalid_use_of_CONFIG_IF_ENABLED_INT' in out
+
+ at pytest.mark.slow
+def test_kconfig_spl(u_boot_console):
+    """Test build failures when IF_ENABLED_INT() option is not enabled"""
+    cons = u_boot_console
+
+    # This detects build errors in test/lib/kconfig_spl.c
+    out = util.run_and_log(
+        cons, ['./tools/buildman/buildman', '--board', 'sandbox_spl',
+               '-a', 'TEST_KCONFIG'], ignore_errors=True)
+    assert 'invalid_use_of_IF_ENABLED_INT' in out
+
+    # There is no CONFIG_SPL_TEST_KCONFIG, so the CONFIG_IF_ENABLED_INT()
+    # line should not generate an error
+    assert 'invalid_use_of_CONFIG_IF_ENABLED_INT' not in out
-- 
2.34.1.703.g22d0c6ccf7-goog



More information about the U-Boot mailing list