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