xref: /netbsd-src/external/gpl2/dtc/dist/tests/set_name.c (revision f3cfa6f6ce31685c6c4a758bc430e69eb99f50a4)
1 /*	$NetBSD: set_name.c,v 1.1.1.2 2017/06/08 15:59:27 skrll Exp $	*/
2 
3 /*
4  * libfdt - Flat Device Tree manipulation
5  *	Testcase for fdt_set_name()
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 #include <stdlib.h>
23 #include <stdio.h>
24 #include <string.h>
25 #include <stdint.h>
26 
27 #include <libfdt.h>
28 
29 #include "tests.h"
30 #include "testdata.h"
31 
32 static void check_set_name(void *fdt, const char *path, const char *newname)
33 {
34 	int offset;
35 	const char *getname, *oldname;
36 	int len, err;
37 
38 	oldname = strrchr(path, '/');
39 	if (!oldname)
40 		TEST_BUG();
41 	oldname += 1;
42 
43 	offset = fdt_path_offset(fdt, path);
44 	if (offset < 0)
45 		FAIL("Couldn't find %s", path);
46 
47 	getname = fdt_get_name(fdt, offset, &len);
48 	verbose_printf("fdt_get_name(%d) returns \"%s\" (len=%d)\n",
49 		       offset, getname, len);
50 	if (!getname)
51 		FAIL("fdt_get_name(%d): %s", offset, fdt_strerror(len));
52 
53 	if (strcmp(getname, oldname) != 0)
54 		FAIL("fdt_get_name(%s) returned \"%s\" instead of \"%s\"",
55 		     path, getname, oldname);
56 
57 	if (len != strlen(getname))
58 		FAIL("fdt_get_name(%s) returned length %d instead of %zd",
59 		     path, len, strlen(getname));
60 
61 	err = fdt_set_name(fdt, offset, newname);
62 	if (err)
63 		FAIL("fdt_set_name(%d, \"%s\"): %s", offset, newname,
64 		     fdt_strerror(err));
65 
66 	getname = fdt_get_name(fdt, offset, &len);
67 	if (!getname)
68 		FAIL("fdt_get_name(%d): %s", offset, fdt_strerror(len));
69 
70 	if (strcmp(getname, newname) != 0)
71 		FAIL("fdt_get_name(%s) returned \"%s\" instead of \"%s\"",
72 		     path, getname, newname);
73 
74 	if (len != strlen(getname))
75 		FAIL("fdt_get_name(%s) returned length %d instead of %zd",
76 		     path, len, strlen(getname));
77 }
78 
79 int main(int argc, char *argv[])
80 {
81 	void *fdt;
82 
83 	test_init(argc, argv);
84 	fdt = load_blob_arg(argc, argv);
85 	fdt = open_blob_rw(fdt);
86 
87 	check_set_name(fdt, "/subnode@1", "subnode@17");
88 	check_set_name(fdt, "/subnode@2/subsubnode@0", "fred@0");
89 	check_set_name(fdt, "/subnode@17/subsubnode", "something@0");
90 
91 	PASS();
92 }
93