1*cc7d2833Sskrll /* $NetBSD: del_node.c,v 1.1.1.3 2019/12/22 12:34:05 skrll Exp $ */
2d89652e2Sskrll
3*cc7d2833Sskrll // SPDX-License-Identifier: LGPL-2.1-or-later
4b8ae3907Smacallan /*
5b8ae3907Smacallan * libfdt - Flat Device Tree manipulation
6b8ae3907Smacallan * Testcase for fdt_nop_node()
7b8ae3907Smacallan * Copyright (C) 2006 David Gibson, IBM Corporation.
8b8ae3907Smacallan */
9b8ae3907Smacallan
10b8ae3907Smacallan #include <stdlib.h>
11b8ae3907Smacallan #include <stdio.h>
12b8ae3907Smacallan #include <string.h>
13b8ae3907Smacallan #include <ctype.h>
14b8ae3907Smacallan #include <stdint.h>
15b8ae3907Smacallan
16b8ae3907Smacallan #include <libfdt.h>
17b8ae3907Smacallan
18b8ae3907Smacallan #include "tests.h"
19b8ae3907Smacallan #include "testdata.h"
20b8ae3907Smacallan
main(int argc,char * argv[])21b8ae3907Smacallan int main(int argc, char *argv[])
22b8ae3907Smacallan {
23b8ae3907Smacallan void *fdt;
24b8ae3907Smacallan int subnode1_offset, subnode2_offset, subsubnode2_offset;
25b8ae3907Smacallan int err;
26b8ae3907Smacallan int oldsize, delsize, newsize;
27b8ae3907Smacallan
28b8ae3907Smacallan test_init(argc, argv);
29b8ae3907Smacallan fdt = load_blob_arg(argc, argv);
30b8ae3907Smacallan
31b8ae3907Smacallan fdt = open_blob_rw(fdt);
32b8ae3907Smacallan
33b8ae3907Smacallan oldsize = fdt_totalsize(fdt);
34b8ae3907Smacallan
35b8ae3907Smacallan subnode1_offset = fdt_path_offset(fdt, "/subnode@1");
36b8ae3907Smacallan if (subnode1_offset < 0)
37b8ae3907Smacallan FAIL("Couldn't find \"/subnode@1\": %s",
38b8ae3907Smacallan fdt_strerror(subnode1_offset));
39b8ae3907Smacallan check_getprop_cell(fdt, subnode1_offset, "prop-int", TEST_VALUE_1);
40b8ae3907Smacallan
41b8ae3907Smacallan subnode2_offset = fdt_path_offset(fdt, "/subnode@2");
42b8ae3907Smacallan if (subnode2_offset < 0)
43b8ae3907Smacallan FAIL("Couldn't find \"/subnode@2\": %s",
44b8ae3907Smacallan fdt_strerror(subnode2_offset));
45b8ae3907Smacallan check_getprop_cell(fdt, subnode2_offset, "prop-int", TEST_VALUE_2);
46b8ae3907Smacallan
47b8ae3907Smacallan subsubnode2_offset = fdt_path_offset(fdt, "/subnode@2/subsubnode");
48b8ae3907Smacallan if (subsubnode2_offset < 0)
49b8ae3907Smacallan FAIL("Couldn't find \"/subnode@2/subsubnode\": %s",
50b8ae3907Smacallan fdt_strerror(subsubnode2_offset));
51b8ae3907Smacallan check_getprop_cell(fdt, subsubnode2_offset, "prop-int", TEST_VALUE_2);
52b8ae3907Smacallan
53b8ae3907Smacallan err = fdt_del_node(fdt, subnode1_offset);
54b8ae3907Smacallan if (err)
55b8ae3907Smacallan FAIL("fdt_del_node(subnode1): %s", fdt_strerror(err));
56b8ae3907Smacallan
57b8ae3907Smacallan subnode1_offset = fdt_path_offset(fdt, "/subnode@1");
58b8ae3907Smacallan if (subnode1_offset != -FDT_ERR_NOTFOUND)
59b8ae3907Smacallan FAIL("fdt_path_offset(subnode1) returned \"%s\" instead of \"%s\"",
60b8ae3907Smacallan fdt_strerror(subnode1_offset),
61b8ae3907Smacallan fdt_strerror(-FDT_ERR_NOTFOUND));
62b8ae3907Smacallan
63b8ae3907Smacallan subnode2_offset = fdt_path_offset(fdt, "/subnode@2");
64b8ae3907Smacallan if (subnode2_offset < 0)
65b8ae3907Smacallan FAIL("Couldn't find \"/subnode2\": %s",
66b8ae3907Smacallan fdt_strerror(subnode2_offset));
67b8ae3907Smacallan check_getprop_cell(fdt, subnode2_offset, "prop-int", TEST_VALUE_2);
68b8ae3907Smacallan
69b8ae3907Smacallan subsubnode2_offset = fdt_path_offset(fdt, "/subnode@2/subsubnode");
70b8ae3907Smacallan if (subsubnode2_offset < 0)
71b8ae3907Smacallan FAIL("Couldn't find \"/subnode@2/subsubnode\": %s",
72b8ae3907Smacallan fdt_strerror(subsubnode2_offset));
73b8ae3907Smacallan check_getprop_cell(fdt, subsubnode2_offset, "prop-int", TEST_VALUE_2);
74b8ae3907Smacallan
75b8ae3907Smacallan err = fdt_del_node(fdt, subnode2_offset);
76b8ae3907Smacallan if (err)
77b8ae3907Smacallan FAIL("fdt_del_node(subnode2): %s", fdt_strerror(err));
78b8ae3907Smacallan
79b8ae3907Smacallan subnode1_offset = fdt_path_offset(fdt, "/subnode@1");
80b8ae3907Smacallan if (subnode1_offset != -FDT_ERR_NOTFOUND)
81b8ae3907Smacallan FAIL("fdt_path_offset(subnode1) returned \"%s\" instead of \"%s\"",
82b8ae3907Smacallan fdt_strerror(subnode1_offset),
83b8ae3907Smacallan fdt_strerror(-FDT_ERR_NOTFOUND));
84b8ae3907Smacallan
85b8ae3907Smacallan subnode2_offset = fdt_path_offset(fdt, "/subnode@2");
86b8ae3907Smacallan if (subnode2_offset != -FDT_ERR_NOTFOUND)
87b8ae3907Smacallan FAIL("fdt_path_offset(subnode2) returned \"%s\" instead of \"%s\"",
88b8ae3907Smacallan fdt_strerror(subnode2_offset),
89b8ae3907Smacallan fdt_strerror(-FDT_ERR_NOTFOUND));
90b8ae3907Smacallan
91b8ae3907Smacallan subsubnode2_offset = fdt_path_offset(fdt, "/subnode@2/subsubnode");
92b8ae3907Smacallan if (subsubnode2_offset != -FDT_ERR_NOTFOUND)
93b8ae3907Smacallan FAIL("fdt_path_offset(subsubnode2) returned \"%s\" instead of \"%s\"",
94b8ae3907Smacallan fdt_strerror(subsubnode2_offset),
95b8ae3907Smacallan fdt_strerror(-FDT_ERR_NOTFOUND));
96b8ae3907Smacallan
97b8ae3907Smacallan delsize = fdt_totalsize(fdt);
98b8ae3907Smacallan
99b8ae3907Smacallan err = fdt_pack(fdt);
100b8ae3907Smacallan if (err)
101b8ae3907Smacallan FAIL("fdt_pack(): %s", fdt_strerror(err));
102b8ae3907Smacallan
103b8ae3907Smacallan newsize = fdt_totalsize(fdt);
104b8ae3907Smacallan
105b8ae3907Smacallan verbose_printf("oldsize = %d, delsize = %d, newsize = %d\n",
106b8ae3907Smacallan oldsize, delsize, newsize);
107b8ae3907Smacallan
108b8ae3907Smacallan if (newsize >= oldsize)
109b8ae3907Smacallan FAIL("Tree failed to shrink after deletions");
110b8ae3907Smacallan
111b8ae3907Smacallan PASS();
112b8ae3907Smacallan }
113