• Home
  • Line#
  • Scopes#
  • Navigate#
  • Raw
  • Download
1 // SPDX-License-Identifier: LGPL-2.1-or-later
2 /*
3  * libfdt - Flat Device Tree manipulation
4  *	Testcase for fdt_supernode_atdepth_offset()
5  * Copyright (C) 2006 David Gibson, IBM Corporation.
6  */
7 #include <stdlib.h>
8 #include <stdio.h>
9 #include <string.h>
10 #include <stdint.h>
11 
12 #include <libfdt.h>
13 
14 #include "tests.h"
15 #include "testdata.h"
16 
path_depth(const char * path)17 static int path_depth(const char *path)
18 {
19 	const char *p;
20 	int depth = 0;
21 
22 	if (path[0] != '/')
23 		TEST_BUG();
24 
25 	if (strcmp(path, "/") == 0)
26 		return 0;
27 	for (p = path; *p; p++)
28 		if (*p == '/')
29 			depth++;
30 
31 	/* Special case for path == "/" */
32 	if (p == (path + 1))
33 		return 0;
34 	else
35 		return depth;
36 }
37 
path_prefix(const char * path,int depth)38 static int path_prefix(const char *path, int depth)
39 {
40 	const char *p;
41 	int i;
42 
43 	if (path[0] != '/')
44 		TEST_BUG();
45 
46 	if (depth == 0)
47 		return 1;
48 
49 	p = path;
50 	for (i = 0; i < depth; i++)
51 		p = p+1 + strcspn(p+1, "/");
52 
53 	return p - path;
54 }
55 
check_supernode_atdepth(struct fdt_header * fdt,const char * path,int depth)56 static void check_supernode_atdepth(struct fdt_header *fdt, const char *path,
57 			     int depth)
58 {
59 	int pdepth = path_depth(path);
60 	char *superpath;
61 	int nodeoffset, supernodeoffset, superpathoffset, pathprefixlen;
62 	int nodedepth;
63 
64 	pathprefixlen = path_prefix(path, depth);
65 	superpath = alloca(pathprefixlen + 1);
66 	strncpy(superpath, path, pathprefixlen);
67 	superpath[pathprefixlen] = '\0';
68 
69 	verbose_printf("Path %s (%d), depth %d, supernode is %s\n",
70 		       path, pdepth, depth, superpath);
71 
72 	nodeoffset = fdt_path_offset(fdt, path);
73 	if (nodeoffset < 0)
74 		FAIL("fdt_path_offset(%s): %s", path, fdt_strerror(nodeoffset));
75 	superpathoffset = fdt_path_offset(fdt, superpath);
76 	if (superpathoffset < 0)
77 		FAIL("fdt_path_offset(%s): %s", superpath,
78 		     fdt_strerror(superpathoffset));
79 
80 	supernodeoffset = fdt_supernode_atdepth_offset(fdt, nodeoffset,
81 						       depth, &nodedepth);
82 	if (supernodeoffset < 0)
83 		FAIL("fdt_supernode_atdepth_offset(): %s",
84 		     fdt_strerror(supernodeoffset));
85 
86 	if (supernodeoffset != superpathoffset)
87 		FAIL("fdt_supernode_atdepth_offset() returns %d instead of %d",
88 		     supernodeoffset, superpathoffset);
89 
90 	if (nodedepth != pdepth)
91 		FAIL("fdt_supernode_atdept_offset() returns node depth %d "
92 		     "instead of %d", nodedepth, pdepth);
93 }
94 
check_supernode_overdepth(struct fdt_header * fdt,const char * path)95 static void check_supernode_overdepth(struct fdt_header *fdt, const char *path)
96 {
97 	int pdepth = path_depth(path);
98 	int nodeoffset, err;
99 
100 	nodeoffset = fdt_path_offset(fdt, path);
101 	if (nodeoffset < 0)
102 		FAIL("fdt_path_offset(%s): %s", path, fdt_strerror(nodeoffset));
103 
104 	err = fdt_supernode_atdepth_offset(fdt, nodeoffset, pdepth + 1, NULL);
105 	if (err != -FDT_ERR_NOTFOUND)
106 		FAIL("fdt_supernode_atdept_offset(%s, %d) returns %d instead "
107 		     "of FDT_ERR_NOTFOUND", path, pdepth+1, err);
108 }
109 
check_path(struct fdt_header * fdt,const char * path)110 static void check_path(struct fdt_header *fdt, const char *path)
111 {
112 	int i;
113 
114 	for (i = 0; i <= path_depth(path); i++)
115 		check_supernode_atdepth(fdt, path, i);
116 	check_supernode_overdepth(fdt, path);
117 }
main(int argc,char * argv[])118 int main(int argc, char *argv[])
119 {
120 	void *fdt;
121 
122 	test_init(argc, argv);
123 	fdt = load_blob_arg(argc, argv);
124 
125 	check_path(fdt, "/");
126 	check_path(fdt, "/subnode@1");
127 	check_path(fdt, "/subnode@2");
128 	check_path(fdt, "/subnode@1/subsubnode");
129 	check_path(fdt, "/subnode@2/subsubnode@0");
130 
131 	PASS();
132 }
133