[PATCH v2 5/6] of: unittest: Unit-tests for target root overlays.

From: Pantelis Antoniou
Date: Mon May 16 2016 - 16:18:43 EST


Add unittests for target-root based overlays.

Signed-off-by: Pantelis Antoniou <pantelis.antoniou@xxxxxxxxxxxx>
---
drivers/of/unittest-data/testcases.dts | 5 +
drivers/of/unittest-data/tests-overlay.dtsi | 48 +++++++
drivers/of/unittest.c | 213 ++++++++++++++++++++++++++++
3 files changed, 266 insertions(+)

diff --git a/drivers/of/unittest-data/testcases.dts b/drivers/of/unittest-data/testcases.dts
index 74e8805..a6ded1b6 100644
--- a/drivers/of/unittest-data/testcases.dts
+++ b/drivers/of/unittest-data/testcases.dts
@@ -80,5 +80,10 @@
target = <0x00000000 0x00000004>;
};
};
+ overlay18 {
+ fragment@0 {
+ target = <0x00000000>;
+ };
+ };
};
}; };
diff --git a/drivers/of/unittest-data/tests-overlay.dtsi b/drivers/of/unittest-data/tests-overlay.dtsi
index ab32996..170b04d 100644
--- a/drivers/of/unittest-data/tests-overlay.dtsi
+++ b/drivers/of/unittest-data/tests-overlay.dtsi
@@ -116,6 +116,24 @@
status = "disabled";
reg = <16>;
};
+
+ unittest17: test-unittest17 {
+ compatible = "unittest";
+ status = "disabled";
+ reg = <17>;
+ };
+
+ unittest18: test-unittest18 {
+ compatible = "unittest";
+ status = "disabled";
+ reg = <18>;
+ };
+
+ unittest19: test-unittest19 {
+ compatible = "unittest";
+ status = "disabled";
+ reg = <19>;
+ };
};
};

@@ -340,5 +358,35 @@
};
};
};
+
+ /* test enable using target root (relative path) */
+ overlay17 {
+ fragment@0 {
+ target-path = "/";
+ __overlay__ {
+ status = "okay";
+ };
+ };
+ };
+
+ /* test enable using target phandle */
+ overlay18 {
+ fragment@0 {
+ target = <&unittest18>;
+ __overlay__ {
+ status = "okay";
+ };
+ };
+ };
+
+ /* test trying to enable out of root (should fail) */
+ overlay19 {
+ fragment@0 {
+ target = <&unittest19>;
+ __overlay__ {
+ status = "okay";
+ };
+ };
+ };
};
};
diff --git a/drivers/of/unittest.c b/drivers/of/unittest.c
index af4c571..7f288e6 100644
--- a/drivers/of/unittest.c
+++ b/drivers/of/unittest.c
@@ -1979,6 +1979,215 @@ out:
unittest(1, "overlay test %d passed\n", 16);
}

+static void of_unittest_overlay_17(void)
+{
+ int ret;
+ int overlay_nr = 17;
+ int unittest_nr = 17;
+ enum overlay_type ovtype = PDEV_OVERLAY;
+ int before = 0;
+ int after = 1;
+ const char *root_path;
+ struct device_node *np = NULL, *target_root = NULL;
+ int id = -1;
+
+ /* unittest device must not be in before state */
+ if (of_unittest_device_exists(unittest_nr, ovtype) != before) {
+ unittest(0, "overlay @\"%s\" with device @\"%s\" %s\n",
+ overlay_path(overlay_nr),
+ unittest_path(unittest_nr, ovtype),
+ !before ? "enabled" : "disabled");
+ return;
+ }
+
+ np = of_find_node_by_path(overlay_path(overlay_nr));
+ if (np == NULL) {
+ unittest(0, "could not find overlay node @\"%s\"\n",
+ overlay_path(overlay_nr));
+ ret = -EINVAL;
+ goto out;
+ }
+
+ root_path = "/testcase-data/overlay-node/test-bus/test-unittest17";
+ target_root = of_find_node_by_path(root_path);
+ if (!target_root) {
+ unittest(0, "could not find target_root node @\"%s\"\n",
+ root_path);
+ ret = -EINVAL;
+ goto out;
+ }
+
+ ret = of_overlay_create_target_root(np, target_root);
+ of_node_put(target_root);
+
+ if (ret < 0) {
+ unittest(0, "could not create overlay from \"%s\"\n",
+ overlay_path(overlay_nr));
+ goto out;
+ }
+ id = ret;
+ of_unittest_track_overlay(id);
+
+ ret = 0;
+
+out:
+ of_node_put(np);
+
+ if (ret)
+ return;
+
+ /* unittest device must be to set to after state */
+ if (of_unittest_device_exists(unittest_nr, ovtype) != after) {
+ unittest(0, "overlay @\"%s\" failed to create @\"%s\" %s\n",
+ overlay_path(overlay_nr),
+ unittest_path(unittest_nr, ovtype),
+ !after ? "enabled" : "disabled");
+ return;
+ }
+
+ unittest(1, "overlay test %d passed\n", 17);
+}
+
+static void of_unittest_overlay_18(void)
+{
+ int ret;
+ int overlay_nr = 18;
+ int unittest_nr = 18;
+ enum overlay_type ovtype = PDEV_OVERLAY;
+ int before = 0;
+ int after = 1;
+ const char *root_path;
+ struct device_node *np = NULL, *target_root = NULL;
+ int id = -1;
+
+ /* unittest device must not be in before state */
+ if (of_unittest_device_exists(unittest_nr, ovtype) != before) {
+ unittest(0, "overlay @\"%s\" with device @\"%s\" %s\n",
+ overlay_path(overlay_nr),
+ unittest_path(unittest_nr, ovtype),
+ !before ? "enabled" : "disabled");
+ return;
+ }
+
+ np = of_find_node_by_path(overlay_path(overlay_nr));
+ if (np == NULL) {
+ unittest(0, "could not find overlay node @\"%s\"\n",
+ overlay_path(overlay_nr));
+ ret = -EINVAL;
+ goto out;
+ }
+
+ root_path = "/testcase-data/overlay-node/test-bus/test-unittest18";
+ target_root = of_find_node_by_path(root_path);
+ if (!target_root) {
+ unittest(0, "could not find target_root node @\"%s\"\n",
+ root_path);
+ ret = -EINVAL;
+ goto out;
+ }
+
+ ret = of_overlay_create_target_root(np, target_root);
+ of_node_put(target_root);
+
+ if (ret < 0) {
+ unittest(0, "could not create overlay from \"%s\"\n",
+ overlay_path(overlay_nr));
+ goto out;
+ }
+ id = ret;
+ of_unittest_track_overlay(id);
+
+ ret = 0;
+
+out:
+ of_node_put(np);
+
+ if (ret)
+ return;
+
+ /* unittest device must be to set to after state */
+ if (of_unittest_device_exists(unittest_nr, ovtype) != after) {
+ unittest(0, "overlay @\"%s\" failed to create @\"%s\" %s\n",
+ overlay_path(overlay_nr),
+ unittest_path(unittest_nr, ovtype),
+ !after ? "enabled" : "disabled");
+ return;
+ }
+
+ unittest(1, "overlay test %d passed\n", 18);
+}
+
+static void of_unittest_overlay_19(void)
+{
+ int ret;
+ int overlay_nr = 19;
+ int unittest_nr = 19;
+ enum overlay_type ovtype = PDEV_OVERLAY;
+ int before = 0;
+ int after = 0;
+ const char *root_path;
+ struct device_node *np = NULL, *target_root = NULL;
+ int id = -1;
+
+ /* unittest device must not be in before state */
+ if (of_unittest_device_exists(unittest_nr, ovtype) != before) {
+ unittest(0, "overlay @\"%s\" with device @\"%s\" %s\n",
+ overlay_path(overlay_nr),
+ unittest_path(unittest_nr, ovtype),
+ !before ? "enabled" : "disabled");
+ return;
+ }
+
+ np = of_find_node_by_path(overlay_path(overlay_nr));
+ if (np == NULL) {
+ unittest(0, "could not find overlay node @\"%s\"\n",
+ overlay_path(overlay_nr));
+ ret = -EINVAL;
+ goto out;
+ }
+
+ root_path = "/testcase-data/overlay-node/test-bus/test-unittest19";
+ target_root = of_find_node_by_path(root_path);
+ if (!target_root) {
+ unittest(0, "could not find target_root node @\"%s\"\n",
+ root_path);
+ ret = -EINVAL;
+ goto out;
+ }
+
+ ret = of_overlay_create_target_root(np, target_root);
+ of_node_put(target_root);
+
+ if (ret >= 0) {
+ unittest(0, "created overlay from \"%s\" while we shouldn't\n",
+ overlay_path(overlay_nr));
+ id = ret;
+ of_unittest_track_overlay(id);
+ ret = -EINVAL;
+ goto out;
+ }
+
+ ret = 0;
+
+out:
+ of_node_put(np);
+
+ if (ret)
+ return;
+
+ /* unittest device must be to set to after state */
+ if (of_unittest_device_exists(unittest_nr, ovtype) != after) {
+ unittest(0, "overlay @\"%s\" failed to create @\"%s\" %s\n",
+ overlay_path(overlay_nr),
+ unittest_path(unittest_nr, ovtype),
+ !after ? "enabled" : "disabled");
+ return;
+ }
+
+ unittest(1, "overlay test %d passed\n", 16);
+}
+
+
static void __init of_unittest_overlay(void)
{
struct device_node *bus_np = NULL;
@@ -2032,6 +2241,10 @@ static void __init of_unittest_overlay(void)

of_unittest_overlay_16();

+ of_unittest_overlay_17();
+ of_unittest_overlay_18();
+ of_unittest_overlay_19();
+
#if IS_BUILTIN(CONFIG_I2C)
if (unittest(of_unittest_overlay_i2c_init() == 0, "i2c init failed\n"))
goto out;
--
1.7.12