xref: /netbsd-src/external/gpl2/dtc/dist/tests/set_name.c (revision 0a3071956a3a9fdebdbf7f338cf2d439b45fc728)
1 /*	$NetBSD: set_name.c,v 1.1.1.3 2019/12/22 12:34:07 skrll Exp $	*/
2 
3 // SPDX-License-Identifier: LGPL-2.1-or-later
4 /*
5  * libfdt - Flat Device Tree manipulation
6  *	Testcase for fdt_set_name()
7  * Copyright (C) 2006 David Gibson, IBM Corporation.
8  */
9 #include <stdlib.h>
10 #include <stdio.h>
11 #include <string.h>
12 #include <stdint.h>
13 
14 #include <libfdt.h>
15 
16 #include "tests.h"
17 #include "testdata.h"
18 
19 static void check_set_name(void *fdt, const char *path, const char *newname)
20 {
21 	int offset;
22 	const char *getname, *oldname;
23 	int len, err;
24 
25 	oldname = strrchr(path, '/');
26 	if (!oldname)
27 		TEST_BUG();
28 	oldname += 1;
29 
30 	offset = fdt_path_offset(fdt, path);
31 	if (offset < 0)
32 		FAIL("Couldn't find %s", path);
33 
34 	getname = fdt_get_name(fdt, offset, &len);
35 	verbose_printf("fdt_get_name(%d) returns \"%s\" (len=%d)\n",
36 		       offset, getname, len);
37 	if (!getname)
38 		FAIL("fdt_get_name(%d): %s", offset, fdt_strerror(len));
39 
40 	if (strcmp(getname, oldname) != 0)
41 		FAIL("fdt_get_name(%s) returned \"%s\" instead of \"%s\"",
42 		     path, getname, oldname);
43 
44 	if (len != strlen(getname))
45 		FAIL("fdt_get_name(%s) returned length %d instead of %zd",
46 		     path, len, strlen(getname));
47 
48 	err = fdt_set_name(fdt, offset, newname);
49 	if (err)
50 		FAIL("fdt_set_name(%d, \"%s\"): %s", offset, newname,
51 		     fdt_strerror(err));
52 
53 	getname = fdt_get_name(fdt, offset, &len);
54 	if (!getname)
55 		FAIL("fdt_get_name(%d): %s", offset, fdt_strerror(len));
56 
57 	if (strcmp(getname, newname) != 0)
58 		FAIL("fdt_get_name(%s) returned \"%s\" instead of \"%s\"",
59 		     path, getname, newname);
60 
61 	if (len != strlen(getname))
62 		FAIL("fdt_get_name(%s) returned length %d instead of %zd",
63 		     path, len, strlen(getname));
64 }
65 
66 int main(int argc, char *argv[])
67 {
68 	void *fdt;
69 
70 	test_init(argc, argv);
71 	fdt = load_blob_arg(argc, argv);
72 	fdt = open_blob_rw(fdt);
73 
74 	check_set_name(fdt, "/subnode@1", "subnode@17");
75 	check_set_name(fdt, "/subnode@2/subsubnode@0", "fred@0");
76 	check_set_name(fdt, "/subnode@17/subsubnode", "something@0");
77 
78 	PASS();
79 }
80