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