[RFC v1] software: node: test: add test to exercise parent child add/remove

From: Brendan Higgins
Date: Tue Apr 14 2020 - 17:05:07 EST


Add test which adds a tree of software_nodes, checks that the nodes
exist, and then removes the tree. This exercises a bug reported by
Naresh Kamboju <naresh.kamboju@xxxxxxxxxx>, and pretty much just takes a
test case from the test_printf Kselftest module and refocusses it on
adding and then removing a tree of software_nodes.

Signed-off-by: Brendan Higgins <brendanhiggins@xxxxxxxxxx>
---

I am not sure if this should be rolled into the property entry test, or
should be moved somewhere else; nevertheless, testing the software node
API seems like a good idea and this seems like a good place to start.

---
drivers/base/test/Kconfig | 14 ++++++++
drivers/base/test/Makefile | 2 ++
drivers/base/test/software-node-test.c | 46 ++++++++++++++++++++++++++
3 files changed, 62 insertions(+)
create mode 100644 drivers/base/test/software-node-test.c

diff --git a/drivers/base/test/Kconfig b/drivers/base/test/Kconfig
index 305c7751184a..b42f385fe233 100644
--- a/drivers/base/test/Kconfig
+++ b/drivers/base/test/Kconfig
@@ -11,3 +11,17 @@ config TEST_ASYNC_DRIVER_PROBE
config KUNIT_DRIVER_PE_TEST
bool "KUnit Tests for property entry API"
depends on KUNIT=y
+config KUNIT_DRIVER_SOFTWARE_NODE_TEST
+ bool "KUnit Tests for software node API"
+ depends on KUNIT=y
+ help
+ This builds the software node API tests.
+
+ KUnit tests run during boot and output the results to the debug log
+ in TAP format (http://testanything.org/). Only useful for kernel devs
+ and are not for inclusion into a production build.
+
+ For more information on KUnit and unit tests in general please refer
+ to the KUnit documentation in Documentation/dev-tools/kunit/.
+
+ If unsure, say N.
diff --git a/drivers/base/test/Makefile b/drivers/base/test/Makefile
index 3ca56367c84b..63325e8a5288 100644
--- a/drivers/base/test/Makefile
+++ b/drivers/base/test/Makefile
@@ -2,3 +2,5 @@
obj-$(CONFIG_TEST_ASYNC_DRIVER_PROBE) += test_async_driver_probe.o

obj-$(CONFIG_KUNIT_DRIVER_PE_TEST) += property-entry-test.o
+
+obj-$(CONFIG_KUNIT_DRIVER_SOFTWARE_NODE_TEST) += software-node-test.o
diff --git a/drivers/base/test/software-node-test.c b/drivers/base/test/software-node-test.c
new file mode 100644
index 000000000000..0609cbd9aa0a
--- /dev/null
+++ b/drivers/base/test/software-node-test.c
@@ -0,0 +1,46 @@
+// SPDX-License-Identifier: GPL-2.0
+// Unit tests for software node API
+//
+// Copyright 2020 Google LLC.
+
+#include <kunit/test.h>
+#include <linux/property.h>
+#include <linux/types.h>
+
+static void software_node_test_register_nodes(struct kunit *test)
+{
+ const struct software_node softnodes[] = {
+ { .name = "first", },
+ { .name = "second", .parent = &softnodes[0], },
+ { .name = "third", .parent = &softnodes[1], },
+ {}
+ };
+ const char * const full_name = "first/second/third";
+ char *buf;
+
+ buf = kunit_kzalloc(test, strlen(full_name), GFP_KERNEL);
+ KUNIT_ASSERT_NOT_ERR_OR_NULL(test, buf);
+
+ KUNIT_ASSERT_EQ(test, software_node_register_nodes(softnodes), 0);
+
+ /* Check that all the nodes exist. */
+ KUNIT_ASSERT_EQ(test,
+ (size_t)sprintf(buf, "%pfw",
+ software_node_fwnode(&softnodes[2])),
+ strlen(full_name));
+ KUNIT_EXPECT_STREQ(test, buf, full_name);
+
+ software_node_unregister_nodes(softnodes);
+}
+
+static struct kunit_case software_node_test_cases[] = {
+ KUNIT_CASE(software_node_test_register_nodes),
+ {}
+};
+
+static struct kunit_suite software_node_test_suite = {
+ .name = "software-node",
+ .test_cases = software_node_test_cases,
+};
+
+kunit_test_suite(software_node_test_suite);

base-commit: 8632e9b5645bbc2331d21d892b0d6961c1a08429
--
2.26.0.110.g2183baf09c-goog