1 /*
2  * libfdt - Flat Device Tree manipulation
3  *	Testcase for fdt_set_name()
4  * Copyright (C) 2006 David Gibson, IBM Corporation.
5  *
6  * This library is free software; you can redistribute it and/or
7  * modify it under the terms of the GNU Lesser General Public License
8  * as published by the Free Software Foundation; either version 2.1 of
9  * the License, or (at your option) any later version.
10  *
11  * This library is distributed in the hope that it will be useful, but
12  * WITHOUT ANY WARRANTY; without even the implied warranty of
13  * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the GNU
14  * Lesser General Public License for more details.
15  *
16  * You should have received a copy of the GNU Lesser General Public
17  * License along with this library; if not, write to the Free Software
18  * Foundation, Inc., 51 Franklin St, Fifth Floor, Boston, MA 02110-1301 USA
19  */
20 #include <stdlib.h>
21 #include <stdio.h>
22 #include <string.h>
23 #include <stdint.h>
24 
25 #include <libfdt.h>
26 
27 #include "tests.h"
28 #include "testdata.h"
29 
check_set_name(void * fdt,const char * path,const char * newname)30 static void check_set_name(void *fdt, const char *path, const char *newname)
31 {
32 	int offset;
33 	const char *getname, *oldname;
34 	int len, err;
35 
36 	oldname = strrchr(path, '/');
37 	if (!oldname)
38 		TEST_BUG();
39 	oldname += 1;
40 
41 	offset = fdt_path_offset(fdt, path);
42 	if (offset < 0)
43 		FAIL("Couldn't find %s", path);
44 
45 	getname = fdt_get_name(fdt, offset, &len);
46 	verbose_printf("fdt_get_name(%d) returns \"%s\" (len=%d)\n",
47 		       offset, getname, len);
48 	if (!getname)
49 		FAIL("fdt_get_name(%d): %s", offset, fdt_strerror(len));
50 
51 	if (strcmp(getname, oldname) != 0)
52 		FAIL("fdt_get_name(%s) returned \"%s\" instead of \"%s\"",
53 		     path, getname, oldname);
54 
55 	if (len != strlen(getname))
56 		FAIL("fdt_get_name(%s) returned length %d instead of %zd",
57 		     path, len, strlen(getname));
58 
59 	err = fdt_set_name(fdt, offset, newname);
60 	if (err)
61 		FAIL("fdt_set_name(%d, \"%s\"): %s", offset, newname,
62 		     fdt_strerror(err));
63 
64 	getname = fdt_get_name(fdt, offset, &len);
65 	if (!getname)
66 		FAIL("fdt_get_name(%d): %s", offset, fdt_strerror(len));
67 
68 	if (strcmp(getname, newname) != 0)
69 		FAIL("fdt_get_name(%s) returned \"%s\" instead of \"%s\"",
70 		     path, getname, newname);
71 
72 	if (len != strlen(getname))
73 		FAIL("fdt_get_name(%s) returned length %d instead of %zd",
74 		     path, len, strlen(getname));
75 }
76 
main(int argc,char * argv[])77 int main(int argc, char *argv[])
78 {
79 	void *fdt;
80 
81 	test_init(argc, argv);
82 	fdt = load_blob_arg(argc, argv);
83 	fdt = open_blob_rw(fdt);
84 
85 	check_set_name(fdt, "/subnode@1", "subnode@17");
86 	check_set_name(fdt, "/subnode@2/subsubnode@0", "fred@0");
87 	check_set_name(fdt, "/subnode@17/subsubnode", "something@0");
88 
89 	PASS();
90 }
91