xref: /netbsd-src/external/gpl2/dtc/dist/tests/path-references.c (revision bdc22b2e01993381dcefeff2bc9b56ca75a4235c)
1 /*	$NetBSD: path-references.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 string references in dtc
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_ref(const void *fdt, int node, const char *checkpath)
33 {
34 	const char *p;
35 	int len;
36 
37 	p = fdt_getprop(fdt, node, "ref", &len);
38 	if (!p)
39 		FAIL("fdt_getprop(%d, \"ref\"): %s", node, fdt_strerror(len));
40 	if (!streq(p, checkpath))
41 		FAIL("'ref' in node at %d has value \"%s\" instead of \"%s\"",
42 		     node, p, checkpath);
43 
44 	p = fdt_getprop(fdt, node, "lref", &len);
45 	if (!p)
46 		FAIL("fdt_getprop(%d, \"lref\"): %s", node, fdt_strerror(len));
47 	if (!streq(p, checkpath))
48 		FAIL("'lref' in node at %d has value \"%s\" instead of \"%s\"",
49 		     node, p, checkpath);
50 }
51 
52 static void check_rref(const void *fdt)
53 {
54 	const char *p;
55 	int len;
56 
57 	/* Check reference to root node */
58 	p = fdt_getprop(fdt, 0, "rref", &len);
59 	if (!p)
60 		FAIL("fdt_getprop(0, \"rref\"): %s", fdt_strerror(len));
61 	if (!streq(p, "/"))
62 		FAIL("'rref' in root node has value \"%s\" instead of \"/\"",
63 		     p);
64 }
65 
66 int main(int argc, char *argv[])
67 {
68 	void *fdt;
69 	const char *p;
70 	int len, multilen;
71 	int n1, n2;
72 
73 	test_init(argc, argv);
74 	fdt = load_blob_arg(argc, argv);
75 
76 	n1 = fdt_path_offset(fdt, "/node1");
77 	if (n1 < 0)
78 		FAIL("fdt_path_offset(/node1): %s", fdt_strerror(n1));
79 	n2 = fdt_path_offset(fdt, "/node2");
80 	if (n2 < 0)
81 		FAIL("fdt_path_offset(/node2): %s", fdt_strerror(n2));
82 
83 	check_ref(fdt, n1, "/node2");
84 	check_ref(fdt, n2, "/node1");
85 
86 	/* Check multiple reference */
87 	multilen = strlen("/node1") + strlen("/node2") + 2;
88 	p = fdt_getprop(fdt, 0, "multiref", &len);
89 	if (!p)
90 		FAIL("fdt_getprop(0, \"multiref\"): %s", fdt_strerror(len));
91 	if (len != multilen)
92 		FAIL("multiref has wrong length, %d instead of %d",
93 		     len, multilen);
94 	if ((!streq(p, "/node1") || !streq(p + strlen("/node1") + 1, "/node2")))
95 		FAIL("multiref has wrong value");
96 
97 	check_rref(fdt);
98 
99 	PASS();
100 }
101