1/*
2 * libfdt - Flat Device Tree manipulation
3 *	Testcase for fdt_parent_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 path_parent_len(const char *path)
31{
32	const char *p = strrchr(path, '/');
33
34	if (!p)
35		TEST_BUG();
36	if (p == path)
37		return 1;
38	else
39		return p - path;
40}
41
42static void check_path(struct fdt_header *fdt, const char *path)
43{
44	char *parentpath;
45	int nodeoffset, parentoffset, parentpathoffset, pathparentlen;
46
47	pathparentlen = path_parent_len(path);
48	parentpath = alloca(pathparentlen + 1);
49	strncpy(parentpath, path, pathparentlen);
50	parentpath[pathparentlen] = '\0';
51
52	verbose_printf("Path: \"%s\"\tParent: \"%s\"\n", path, parentpath);
53
54	nodeoffset = fdt_path_offset(fdt, path);
55	if (nodeoffset < 0)
56		FAIL("fdt_path_offset(%s): %s", path, fdt_strerror(nodeoffset));
57
58	parentpathoffset = fdt_path_offset(fdt, parentpath);
59	if (parentpathoffset < 0)
60		FAIL("fdt_path_offset(%s): %s", parentpath,
61		     fdt_strerror(parentpathoffset));
62
63	parentoffset = fdt_parent_offset(fdt, nodeoffset);
64	if (parentoffset < 0)
65		FAIL("fdt_parent_offset(): %s", fdt_strerror(parentoffset));
66
67	if (parentoffset != parentpathoffset)
68		FAIL("fdt_parent_offset() returns %d instead of %d",
69		     parentoffset, parentpathoffset);
70}
71
72int main(int argc, char *argv[])
73{
74	void *fdt;
75	int err;
76
77	test_init(argc, argv);
78	fdt = load_blob_arg(argc, argv);
79
80	check_path(fdt, "/subnode@1");
81	check_path(fdt, "/subnode@2");
82	check_path(fdt, "/subnode@1/subsubnode");
83	check_path(fdt, "/subnode@2/subsubnode@0");
84	err = fdt_parent_offset(fdt, 0);
85	if (err != -FDT_ERR_NOTFOUND)
86		FAIL("fdt_parent_offset(/) returns %d instead of "
87		     "-FDT_ERR_NOTFOUND", err);
88
89	PASS();
90}
91