mail archive of the barebox mailing list
 help / color / mirror / Atom feed
From: Ahmad Fatoum <a.fatoum@pengutronix.de>
To: barebox@lists.infradead.org
Cc: Ahmad Fatoum <a.fatoum@pengutronix.de>
Subject: [PATCH 5/5] test: self: add device tree manipulation test
Date: Mon,  7 Feb 2022 08:56:21 +0100	[thread overview]
Message-ID: <20220207075621.1014381-5-a.fatoum@pengutronix.de> (raw)
In-Reply-To: <20220207075621.1014381-1-a.fatoum@pengutronix.de>

We had recently gained support for two extra helpers:

of_property_sprintf and of_property_write_string. Add tests for them as
well as for general OF creation and unflattening.

Signed-off-by: Ahmad Fatoum <a.fatoum@pengutronix.de>
---
 test/self/Kconfig             |   6 ++
 test/self/Makefile            |   1 +
 test/self/of_manipulation.c   | 111 ++++++++++++++++++++++++++++++++++
 test/self/of_manipulation.dts |  30 +++++++++
 4 files changed, 148 insertions(+)
 create mode 100644 test/self/of_manipulation.c
 create mode 100644 test/self/of_manipulation.dts

diff --git a/test/self/Kconfig b/test/self/Kconfig
index dfaa32dda009..3340a9146ee2 100644
--- a/test/self/Kconfig
+++ b/test/self/Kconfig
@@ -37,6 +37,12 @@ config SELFTEST_PRINTF
 	help
 	  Tests barebox vsnprintf() functionality
 
+config SELFTEST_OF_MANIPULATION
+	bool "OF manipulation selftest"
+	select OFTREE
+	help
+	  Tests barebox device tree manipulation functionality
+
 config SELFTEST_PROGRESS_NOTIFIER
 	bool "progress notifier selftest"
 
diff --git a/test/self/Makefile b/test/self/Makefile
index e78ccc3cfb90..05a2a6a236ec 100644
--- a/test/self/Makefile
+++ b/test/self/Makefile
@@ -3,3 +3,4 @@
 obj-$(CONFIG_SELFTEST) += core.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
diff --git a/test/self/of_manipulation.c b/test/self/of_manipulation.c
new file mode 100644
index 000000000000..1bcd593c8628
--- /dev/null
+++ b/test/self/of_manipulation.c
@@ -0,0 +1,111 @@
+// SPDX-License-Identifier: GPL-2.0-only
+
+#define pr_fmt(fmt) KBUILD_MODNAME ": " fmt
+
+#include <common.h>
+#include <bselftest.h>
+#include <linux/kernel.h>
+#include <module.h>
+#include <stdio.h>
+#include <stdlib.h>
+#include <linux/string.h>
+#include <errno.h>
+#include <of.h>
+
+BSELFTEST_GLOBALS();
+
+static void assert_different(struct device_node *a, struct device_node *b, int expect)
+{
+	int ret;
+
+	total_tests++;
+
+	ret = of_diff(a, b, -1);
+	if (ret == expect)
+		return;
+
+	pr_warn("comparison of %s and %s failed: %u differences expected, %u found.\n",
+		a->full_name, b->full_name, expect, ret);
+	of_diff(a, b, 1);
+	failed_tests++;
+}
+
+#define assert_equal(a, b) assert_different(a, b, 0)
+
+static void test_of_basics(struct device_node *root)
+{
+	struct device_node *node1, *node2, *node21;
+
+	node1 = of_new_node(root, "node1");
+	node2 = of_new_node(root, "node2");
+
+	assert_equal(node1, node2);
+
+	of_property_write_bool(node2, "property1", true);
+
+	assert_different(node1, node2, 1);
+
+	node21 = of_new_node(node2, "node21");
+
+	assert_different(node1, node2, 2);
+	assert_equal(node1, node21);
+
+	of_new_node(node1, "node21");
+
+	assert_different(node1, node2, 1);
+
+	of_property_write_bool(node1, "property1", true);
+
+	assert_equal(node1, node2);
+}
+
+static void test_of_property_strings(struct device_node *root)
+{
+	struct device_node *np1, *np2, *np3;
+	char properties[] = "ayy\0bee\0sea";
+
+	np1 = of_new_node(root, "np1");
+	np2 = of_new_node(root, "np2");
+	np3 = of_new_node(root, "np3");
+
+	of_property_sprintf(np1, "property-single", "%c%c%c", 'a', 'y', 'y');
+
+	of_property_write_string(np2, "property-single", "ayy");
+
+	assert_equal(np1, np2);
+
+	of_set_property(np2, "property-multi", properties, sizeof(properties), 1);
+
+	of_property_write_strings(np3, "property-single", "ayy", NULL);
+	of_property_write_strings(np3, "property-multi",
+				  "ayy", "bee", "sea", NULL);
+
+	assert_equal(np2, np3);
+
+	of_set_property(np1, "property-multi", properties, sizeof(properties), 1);
+
+	assert_equal(np1, np2);
+
+	of_set_property(np1, "property-multi", properties, sizeof(properties) - 1, 0);
+
+	assert_different(np1, np2, 1);
+}
+
+static void __init test_of_manipulation(void)
+{
+	extern char __dtb_of_manipulation_start[], __dtb_of_manipulation_end[];
+	struct device_node *root = of_new_node(NULL, NULL);
+	struct device_node *expected;
+
+	test_of_basics(root);
+	test_of_property_strings(root);
+
+	expected = of_unflatten_dtb(__dtb_of_manipulation_start,
+				    __dtb_of_manipulation_end - __dtb_of_manipulation_start);
+
+	assert_equal(root, expected);
+
+	of_delete_node(root);
+	of_delete_node(expected);
+}
+bselftest(core, test_of_manipulation);
diff --git a/test/self/of_manipulation.dts b/test/self/of_manipulation.dts
new file mode 100644
index 000000000000..3b690bb7f0fe
--- /dev/null
+++ b/test/self/of_manipulation.dts
@@ -0,0 +1,30 @@
+/* SPDX-License-Identifier: GPL-2.0-only */
+
+/dts-v1/;
+
+/ {
+	node1 {
+		property1;
+		node21 { };
+	};
+
+	node2 {
+		property1;
+		node21 { };
+	};
+
+	np1 {
+		property-single = "ayy";
+		property-multi = [61 79 79 00 62 65 65 00 73 65 61];
+	};
+
+	np2 {
+		property-single = "ayy";
+		property-multi = "ayy", "bee", "sea";
+	};
+
+	np3 {
+		property-single = "ayy";
+		property-multi = "ayy", "bee", "sea";
+	};
+};
-- 
2.30.2


_______________________________________________
barebox mailing list
barebox@lists.infradead.org
http://lists.infradead.org/mailman/listinfo/barebox


      parent reply	other threads:[~2022-02-07  7:58 UTC|newest]

Thread overview: 8+ messages / expand[flat|nested]  mbox.gz  Atom feed  top
2022-02-07  7:56 [PATCH 1/5] test: self: printf: log skipped tests Ahmad Fatoum
2022-02-07  7:56 ` [PATCH 2/5] test: self: printf: add tests for %*ph prints Ahmad Fatoum
2022-02-07  7:56 ` [PATCH 3/5] of: report whether of_diff found differences in return code Ahmad Fatoum
2022-02-07  7:56 ` [PATCH 4/5] of: silence of_diff output for negative indents Ahmad Fatoum
2022-02-07  8:17   ` Sascha Hauer
2022-02-07  8:22     ` Sascha Hauer
2022-02-07  8:23       ` Ahmad Fatoum
2022-02-07  7:56 ` Ahmad Fatoum [this message]

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=20220207075621.1014381-5-a.fatoum@pengutronix.de \
    --to=a.fatoum@pengutronix.de \
    --cc=barebox@lists.infradead.org \
    /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
Be sure your reply has a Subject: header at the top and a blank line before the message body.
This is a public inbox, see mirroring instructions
for how to clone and mirror all data and code used for this inbox