1/*
2 * libfdt - Flat Device Tree manipulation
3 *	Testcase for fdt_path_offset()
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
30static int check_subnode(void *fdt, int parent, const char *name)
31{
32	int offset;
33	const struct fdt_node_header *nh;
34	uint32_t tag;
35
36	verbose_printf("Checking subnode \"%s\" of %d...", name, parent);
37	offset = fdt_subnode_offset(fdt, parent, name);
38	verbose_printf("offset %d...", offset);
39	if (offset < 0)
40		FAIL("fdt_subnode_offset(\"%s\"): %s", name, fdt_strerror(offset));
41	nh = fdt_offset_ptr(fdt, offset, sizeof(*nh));
42	verbose_printf("pointer %p\n", nh);
43	if (! nh)
44		FAIL("NULL retrieving subnode \"%s\"", name);
45
46	tag = fdt32_to_cpu(nh->tag);
47
48	if (tag != FDT_BEGIN_NODE)
49		FAIL("Incorrect tag 0x%08x on property \"%s\"", tag, name);
50	if (!nodename_eq(nh->name, name))
51		FAIL("Subnode name mismatch \"%s\" instead of \"%s\"",
52		     nh->name, name);
53
54	return offset;
55}
56
57static void check_path_offset(void *fdt, char *path, int offset)
58{
59	int rc;
60
61	verbose_printf("Checking offset of \"%s\" is %d...\n", path, offset);
62
63	rc = fdt_path_offset(fdt, path);
64	if (rc < 0)
65		FAIL("fdt_path_offset(\"%s\") failed: %s",
66		     path,  fdt_strerror(rc));
67	if (rc != offset)
68		FAIL("fdt_path_offset(\"%s\") returned incorrect offset"
69		     " %d instead of %d", path, rc, offset);
70}
71
72static void check_path_offset_namelen(void *fdt, char *path, int namelen,
73				      int offset)
74{
75	int rc;
76
77	verbose_printf("Checking offset of \"%s\" [first %d characters]"
78		       " is %d...\n", path, namelen, offset);
79
80	rc = fdt_path_offset_namelen(fdt, path, namelen);
81	if (rc == offset)
82		return;
83
84	if (rc < 0)
85		FAIL("fdt_path_offset_namelen(\"%s\", %d) failed: %s",
86		     path, namelen, fdt_strerror(rc));
87	else
88		FAIL("fdt_path_offset_namelen(\"%s\", %d) returned incorrect"
89		     " offset %d instead of %d", path, namelen, rc, offset);
90}
91
92int main(int argc, char *argv[])
93{
94	void *fdt;
95	int subnode1_offset, subnode2_offset;
96	int subsubnode1_offset, subsubnode2_offset, subsubnode2_offset2;
97
98	test_init(argc, argv);
99	fdt = load_blob_arg(argc, argv);
100
101	check_path_offset(fdt, "/", 0);
102
103	subnode1_offset = check_subnode(fdt, 0, "subnode@1");
104	subnode2_offset = check_subnode(fdt, 0, "subnode@2");
105
106	check_path_offset(fdt, "/subnode@1", subnode1_offset);
107	check_path_offset(fdt, "/subnode@2", subnode2_offset);
108
109	subsubnode1_offset = check_subnode(fdt, subnode1_offset, "subsubnode");
110	subsubnode2_offset = check_subnode(fdt, subnode2_offset, "subsubnode@0");
111	subsubnode2_offset2 = check_subnode(fdt, subnode2_offset, "subsubnode");
112
113	check_path_offset(fdt, "/subnode@1/subsubnode", subsubnode1_offset);
114	check_path_offset(fdt, "/subnode@2/subsubnode@0", subsubnode2_offset);
115	check_path_offset(fdt, "/subnode@2/subsubnode", subsubnode2_offset2);
116
117	/* Test paths with extraneous separators */
118	check_path_offset(fdt, "//", 0);
119	check_path_offset(fdt, "///", 0);
120	check_path_offset(fdt, "//subnode@1", subnode1_offset);
121	check_path_offset(fdt, "/subnode@1/", subnode1_offset);
122	check_path_offset(fdt, "//subnode@1///", subnode1_offset);
123	check_path_offset(fdt, "/subnode@2////subsubnode", subsubnode2_offset2);
124
125	/* Test fdt_path_offset_namelen() */
126	check_path_offset_namelen(fdt, "/subnode@1", 1, 0);
127	check_path_offset_namelen(fdt, "/subnode@1/subsubnode", 10, subnode1_offset);
128	check_path_offset_namelen(fdt, "/subnode@1/subsubnode", 11, subnode1_offset);
129	check_path_offset_namelen(fdt, "/subnode@2TRAILINGGARBAGE", 10, subnode2_offset);
130	check_path_offset_namelen(fdt, "/subnode@2TRAILINGGARBAGE", 11, -FDT_ERR_NOTFOUND);
131	check_path_offset_namelen(fdt, "/subnode@2/subsubnode@0/more", 23, subsubnode2_offset2);
132	check_path_offset_namelen(fdt, "/subnode@2/subsubnode@0/more", 22, -FDT_ERR_NOTFOUND);
133	check_path_offset_namelen(fdt, "/subnode@2/subsubnode@0/more", 24, subsubnode2_offset2);
134	check_path_offset_namelen(fdt, "/subnode@2/subsubnode@0/more", 25, -FDT_ERR_NOTFOUND);
135
136	PASS();
137}
138