xref: /netbsd-src/external/gpl2/dtc/dist/tests/parent_offset.c (revision cc7d2833ecf67da5a5ddc470841931eb9f6723e4)
1 /*	$NetBSD: parent_offset.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_parent_offset()
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 
path_parent_len(const char * path)19 static int path_parent_len(const char *path)
20 {
21 	const char *p = strrchr(path, '/');
22 
23 	if (!p)
24 		TEST_BUG();
25 	if (p == path)
26 		return 1;
27 	else
28 		return p - path;
29 }
30 
check_path(struct fdt_header * fdt,const char * path)31 static void check_path(struct fdt_header *fdt, const char *path)
32 {
33 	char *parentpath;
34 	int nodeoffset, parentoffset, parentpathoffset, pathparentlen;
35 
36 	pathparentlen = path_parent_len(path);
37 	parentpath = alloca(pathparentlen + 1);
38 	strncpy(parentpath, path, pathparentlen);
39 	parentpath[pathparentlen] = '\0';
40 
41 	verbose_printf("Path: \"%s\"\tParent: \"%s\"\n", path, parentpath);
42 
43 	nodeoffset = fdt_path_offset(fdt, path);
44 	if (nodeoffset < 0)
45 		FAIL("fdt_path_offset(%s): %s", path, fdt_strerror(nodeoffset));
46 
47 	parentpathoffset = fdt_path_offset(fdt, parentpath);
48 	if (parentpathoffset < 0)
49 		FAIL("fdt_path_offset(%s): %s", parentpath,
50 		     fdt_strerror(parentpathoffset));
51 
52 	parentoffset = fdt_parent_offset(fdt, nodeoffset);
53 	if (parentoffset < 0)
54 		FAIL("fdt_parent_offset(): %s", fdt_strerror(parentoffset));
55 
56 	if (parentoffset != parentpathoffset)
57 		FAIL("fdt_parent_offset() returns %d instead of %d",
58 		     parentoffset, parentpathoffset);
59 }
60 
main(int argc,char * argv[])61 int main(int argc, char *argv[])
62 {
63 	void *fdt;
64 	int err;
65 
66 	test_init(argc, argv);
67 	fdt = load_blob_arg(argc, argv);
68 
69 	check_path(fdt, "/subnode@1");
70 	check_path(fdt, "/subnode@2");
71 	check_path(fdt, "/subnode@1/subsubnode");
72 	check_path(fdt, "/subnode@2/subsubnode@0");
73 	err = fdt_parent_offset(fdt, 0);
74 	if (err != -FDT_ERR_NOTFOUND)
75 		FAIL("fdt_parent_offset(/) returns %d instead of "
76 		     "-FDT_ERR_NOTFOUND", err);
77 
78 	PASS();
79 }
80