diff options
author | Ahmad Fatoum <a.fatoum@pengutronix.de> | 2022-02-07 09:28:01 +0100 |
---|---|---|
committer | Sascha Hauer <s.hauer@pengutronix.de> | 2022-02-07 09:35:41 +0100 |
commit | 54d8d7cc75d28a9dae17ee96e389e0848c530ae8 (patch) | |
tree | 941740ce3d16cc40dc8f6953e720bcc82f36c547 /test | |
parent | 66715eb4e93e94b47fca865f9190c51b8fe218d7 (diff) | |
download | barebox-54d8d7cc75d28a9dae17ee96e389e0848c530ae8.tar.gz barebox-54d8d7cc75d28a9dae17ee96e389e0848c530ae8.tar.xz |
test: self: add device tree manipulation test
We had recently gained support for two extra helpers:
of_property_sprintf and of_property_write_strings. Add tests for them as
well as for general OF creation and unflattening.
Signed-off-by: Ahmad Fatoum <a.fatoum@pengutronix.de>
Link: https://lore.barebox.org/20220207082801.1052894-5-a.fatoum@pengutronix.de
Signed-off-by: Sascha Hauer <s.hauer@pengutronix.de>
Diffstat (limited to 'test')
-rw-r--r-- | test/self/Kconfig | 6 | ||||
-rw-r--r-- | test/self/Makefile | 1 | ||||
-rw-r--r-- | test/self/of_manipulation.c | 111 | ||||
-rw-r--r-- | test/self/of_manipulation.dts | 30 |
4 files changed, 148 insertions, 0 deletions
diff --git a/test/self/Kconfig b/test/self/Kconfig index dfaa32dda0..3340a9146e 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 e78ccc3cfb..05a2a6a236 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 0000000000..1bcd593c86 --- /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 0000000000..3b690bb7f0 --- /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"; + }; +}; |