From caec1be706ac4e36d55e6343893a956f6ccf7464 Mon Sep 17 00:00:00 2001
From: Marek Vasut <marek.vasut+renesas@mailbox.org>
Date: Thu, 2 Mar 2023 04:08:38 +0100
Subject: [PATCH] test: cmd: fdt: Test fdt rm

Add 'fdt rm' test which works as follows:
- Create fuller FDT, map it to sysmem
- Selectively delete nodes or properties by both path and aliases
- Verify created nodes or properties using fdt print command

The test case can be triggered using:
"
./u-boot -Dc 'ut fdt'
"
To dump the full output from commands used during test, add '-v' flag.

Signed-off-by: Marek Vasut <marek.vasut+renesas@mailbox.org>
Reviewed-by: Simon Glass <sjg@chromium.org>
---
 test/cmd/fdt.c | 84 ++++++++++++++++++++++++++++++++++++++++++++++++++
 1 file changed, 84 insertions(+)

diff --git a/test/cmd/fdt.c b/test/cmd/fdt.c
index 4d45afaa29..cb86fc02a7 100644
--- a/test/cmd/fdt.c
+++ b/test/cmd/fdt.c
@@ -847,6 +847,90 @@ static int fdt_test_mknode(struct unit_test_state *uts)
 }
 FDT_TEST(fdt_test_mknode, UT_TESTF_CONSOLE_REC);
 
+static int fdt_test_rm(struct unit_test_state *uts)
+{
+	char fdt[4096];
+	ulong addr;
+
+	ut_assertok(make_fuller_fdt(uts, fdt, sizeof(fdt)));
+	addr = map_to_sysmem(fdt);
+	set_working_fdt_addr(addr);
+
+	/* Test removal of property in root node / */
+	ut_assertok(console_record_reset_enable());
+	ut_assertok(run_commandf("fdt print / compatible"));
+	ut_assert_nextline("compatible = \"u-boot,fdt-test\"");
+	ut_assertok(run_commandf("fdt rm / compatible"));
+	ut_asserteq(1, run_commandf("fdt print / compatible"));
+	ut_assert_nextline("libfdt fdt_getprop(): FDT_ERR_NOTFOUND");
+	ut_assertok(ut_check_console_end(uts));
+
+	/* Test removal of property clock-names in subnode /test-node@1234 */
+	ut_assertok(console_record_reset_enable());
+	ut_assertok(run_commandf("fdt print /test-node@1234 clock-names"));
+	ut_assert_nextline("clock-names = \"fixed\", \"i2c\", \"spi\", \"uart2\", \"uart1\"");
+	ut_assertok(run_commandf("fdt rm /test-node@1234 clock-names"));
+	ut_asserteq(1, run_commandf("fdt print /test-node@1234 clock-names"));
+	ut_assert_nextline("libfdt fdt_getprop(): FDT_ERR_NOTFOUND");
+	ut_assertok(ut_check_console_end(uts));
+
+	/* Test removal of property u-boot,empty-property in subnode /test-node@1234 by alias */
+	ut_assertok(console_record_reset_enable());
+	ut_assertok(run_commandf("fdt print testnodealias u-boot,empty-property"));
+	ut_assert_nextline("testnodealias u-boot,empty-property");
+	ut_assertok(run_commandf("fdt rm testnodealias u-boot,empty-property"));
+	ut_asserteq(1, run_commandf("fdt print testnodealias u-boot,empty-property"));
+	ut_assert_nextline("libfdt fdt_getprop(): FDT_ERR_NOTFOUND");
+	ut_assertok(ut_check_console_end(uts));
+
+	/* Test removal of non-existent property noprop in subnode /test-node@1234 */
+	ut_assertok(console_record_reset_enable());
+	ut_asserteq(1, run_commandf("fdt rm /test-node@1234 noprop"));
+	ut_assert_nextline("libfdt fdt_delprop(): FDT_ERR_NOTFOUND");
+	ut_assertok(ut_check_console_end(uts));
+
+	/* Test removal of non-existent node /no-node@5678 */
+	ut_assertok(console_record_reset_enable());
+	ut_asserteq(1, run_commandf("fdt rm /no-node@5678"));
+	ut_assert_nextline("libfdt fdt_path_offset() returned FDT_ERR_NOTFOUND");
+	ut_assertok(ut_check_console_end(uts));
+
+	/* Test removal of subnode /test-node@1234/subnode by alias */
+	ut_assertok(console_record_reset_enable());
+	ut_assertok(run_commandf("fdt rm subnodealias"));
+	ut_asserteq(1, run_commandf("fdt print /test-node@1234/subnode"));
+	ut_assert_nextline("libfdt fdt_path_offset() returned FDT_ERR_NOTFOUND");
+	ut_assertok(ut_check_console_end(uts));
+
+	/* Test removal of node by non-existent alias */
+	ut_assertok(console_record_reset_enable());
+	ut_asserteq(1, run_commandf("fdt rm noalias"));
+	ut_assert_nextline("libfdt fdt_path_offset() returned FDT_ERR_BADPATH");
+	ut_assertok(ut_check_console_end(uts));
+
+	/* Test removal of node by bad alias */
+	ut_assertok(console_record_reset_enable());
+	ut_asserteq(1, run_commandf("fdt rm noalias"));
+	ut_assert_nextline("libfdt fdt_path_offset() returned FDT_ERR_BADPATH");
+	ut_assertok(ut_check_console_end(uts));
+
+	/* Test removal of node /test-node@1234 */
+	ut_assertok(console_record_reset_enable());
+	ut_assertok(run_commandf("fdt rm /test-node@1234"));
+	ut_asserteq(1, run_commandf("fdt print /test-node@1234"));
+	ut_assert_nextline("libfdt fdt_path_offset() returned FDT_ERR_NOTFOUND");
+	ut_assertok(ut_check_console_end(uts));
+
+	/* Test removal of node / */
+	ut_assertok(console_record_reset_enable());
+	ut_assertok(run_commandf("fdt rm /"));
+	ut_asserteq(1, run_commandf("fdt print /"));
+	ut_assertok(ut_check_console_end(uts));
+
+	return 0;
+}
+FDT_TEST(fdt_test_rm, UT_TESTF_CONSOLE_REC);
+
 int do_ut_fdt(struct cmd_tbl *cmdtp, int flag, int argc, char *const argv[])
 {
 	struct unit_test *tests = UNIT_TEST_SUITE_START(fdt_test);
-- 
2.39.5