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