xref: /netbsd-src/external/gpl2/dtc/dist/tests/del_node.c (revision bdc22b2e01993381dcefeff2bc9b56ca75a4235c)
1 /*	$NetBSD: del_node.c,v 1.1.1.2 2017/06/08 15:59:26 skrll Exp $	*/
2 
3 /*
4  * libfdt - Flat Device Tree manipulation
5  *	Testcase for fdt_nop_node()
6  * Copyright (C) 2006 David Gibson, IBM Corporation.
7  *
8  * This library is free software; you can redistribute it and/or
9  * modify it under the terms of the GNU Lesser General Public License
10  * as published by the Free Software Foundation; either version 2.1 of
11  * the License, or (at your option) any later version.
12  *
13  * This library is distributed in the hope that it will be useful, but
14  * WITHOUT ANY WARRANTY; without even the implied warranty of
15  * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the GNU
16  * Lesser General Public License for more details.
17  *
18  * You should have received a copy of the GNU Lesser General Public
19  * License along with this library; if not, write to the Free Software
20  * Foundation, Inc., 51 Franklin St, Fifth Floor, Boston, MA 02110-1301 USA
21  */
22 
23 #include <stdlib.h>
24 #include <stdio.h>
25 #include <string.h>
26 #include <ctype.h>
27 #include <stdint.h>
28 
29 #include <libfdt.h>
30 
31 #include "tests.h"
32 #include "testdata.h"
33 
34 int main(int argc, char *argv[])
35 {
36 	void *fdt;
37 	int subnode1_offset, subnode2_offset, subsubnode2_offset;
38 	int err;
39 	int oldsize, delsize, newsize;
40 
41 	test_init(argc, argv);
42 	fdt = load_blob_arg(argc, argv);
43 
44 	fdt = open_blob_rw(fdt);
45 
46 	oldsize = fdt_totalsize(fdt);
47 
48 	subnode1_offset = fdt_path_offset(fdt, "/subnode@1");
49 	if (subnode1_offset < 0)
50 		FAIL("Couldn't find \"/subnode@1\": %s",
51 		     fdt_strerror(subnode1_offset));
52 	check_getprop_cell(fdt, subnode1_offset, "prop-int", TEST_VALUE_1);
53 
54 	subnode2_offset = fdt_path_offset(fdt, "/subnode@2");
55 	if (subnode2_offset < 0)
56 		FAIL("Couldn't find \"/subnode@2\": %s",
57 		     fdt_strerror(subnode2_offset));
58 	check_getprop_cell(fdt, subnode2_offset, "prop-int", TEST_VALUE_2);
59 
60 	subsubnode2_offset = fdt_path_offset(fdt, "/subnode@2/subsubnode");
61 	if (subsubnode2_offset < 0)
62 		FAIL("Couldn't find \"/subnode@2/subsubnode\": %s",
63 		     fdt_strerror(subsubnode2_offset));
64 	check_getprop_cell(fdt, subsubnode2_offset, "prop-int", TEST_VALUE_2);
65 
66 	err = fdt_del_node(fdt, subnode1_offset);
67 	if (err)
68 		FAIL("fdt_del_node(subnode1): %s", fdt_strerror(err));
69 
70 	subnode1_offset = fdt_path_offset(fdt, "/subnode@1");
71 	if (subnode1_offset != -FDT_ERR_NOTFOUND)
72 		FAIL("fdt_path_offset(subnode1) returned \"%s\" instead of \"%s\"",
73 		     fdt_strerror(subnode1_offset),
74 		     fdt_strerror(-FDT_ERR_NOTFOUND));
75 
76 	subnode2_offset = fdt_path_offset(fdt, "/subnode@2");
77 	if (subnode2_offset < 0)
78 		FAIL("Couldn't find \"/subnode2\": %s",
79 		     fdt_strerror(subnode2_offset));
80 	check_getprop_cell(fdt, subnode2_offset, "prop-int", TEST_VALUE_2);
81 
82 	subsubnode2_offset = fdt_path_offset(fdt, "/subnode@2/subsubnode");
83 	if (subsubnode2_offset < 0)
84 		FAIL("Couldn't find \"/subnode@2/subsubnode\": %s",
85 		     fdt_strerror(subsubnode2_offset));
86 	check_getprop_cell(fdt, subsubnode2_offset, "prop-int", TEST_VALUE_2);
87 
88 	err = fdt_del_node(fdt, subnode2_offset);
89 	if (err)
90 		FAIL("fdt_del_node(subnode2): %s", fdt_strerror(err));
91 
92 	subnode1_offset = fdt_path_offset(fdt, "/subnode@1");
93 	if (subnode1_offset != -FDT_ERR_NOTFOUND)
94 		FAIL("fdt_path_offset(subnode1) returned \"%s\" instead of \"%s\"",
95 		     fdt_strerror(subnode1_offset),
96 		     fdt_strerror(-FDT_ERR_NOTFOUND));
97 
98 	subnode2_offset = fdt_path_offset(fdt, "/subnode@2");
99 	if (subnode2_offset != -FDT_ERR_NOTFOUND)
100 		FAIL("fdt_path_offset(subnode2) returned \"%s\" instead of \"%s\"",
101 		     fdt_strerror(subnode2_offset),
102 		     fdt_strerror(-FDT_ERR_NOTFOUND));
103 
104 	subsubnode2_offset = fdt_path_offset(fdt, "/subnode@2/subsubnode");
105 	if (subsubnode2_offset != -FDT_ERR_NOTFOUND)
106 		FAIL("fdt_path_offset(subsubnode2) returned \"%s\" instead of \"%s\"",
107 		     fdt_strerror(subsubnode2_offset),
108 		     fdt_strerror(-FDT_ERR_NOTFOUND));
109 
110 	delsize = fdt_totalsize(fdt);
111 
112 	err = fdt_pack(fdt);
113 	if (err)
114 		FAIL("fdt_pack(): %s", fdt_strerror(err));
115 
116 	newsize = fdt_totalsize(fdt);
117 
118 	verbose_printf("oldsize = %d, delsize = %d, newsize = %d\n",
119 		       oldsize, delsize, newsize);
120 
121 	if (newsize >= oldsize)
122 		FAIL("Tree failed to shrink after deletions");
123 
124 	PASS();
125 }
126