1 // SPDX-License-Identifier: LGPL-2.1-or-later
2 /*
3 * libfdt - Flat Device Tree manipulation
4 * Testcase for fdt_parent_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_parent_len(const char * path)17 static int path_parent_len(const char *path)
18 {
19 const char *p = strrchr(path, '/');
20
21 if (!p)
22 TEST_BUG();
23 if (p == path)
24 return 1;
25 else
26 return p - path;
27 }
28
check_path(struct fdt_header * fdt,const char * path)29 static void check_path(struct fdt_header *fdt, const char *path)
30 {
31 char *parentpath;
32 int nodeoffset, parentoffset, parentpathoffset, pathparentlen;
33
34 pathparentlen = path_parent_len(path);
35 parentpath = alloca(pathparentlen + 1);
36 strncpy(parentpath, path, pathparentlen);
37 parentpath[pathparentlen] = '\0';
38
39 verbose_printf("Path: \"%s\"\tParent: \"%s\"\n", path, parentpath);
40
41 nodeoffset = fdt_path_offset(fdt, path);
42 if (nodeoffset < 0)
43 FAIL("fdt_path_offset(%s): %s", path, fdt_strerror(nodeoffset));
44
45 parentpathoffset = fdt_path_offset(fdt, parentpath);
46 if (parentpathoffset < 0)
47 FAIL("fdt_path_offset(%s): %s", parentpath,
48 fdt_strerror(parentpathoffset));
49
50 parentoffset = fdt_parent_offset(fdt, nodeoffset);
51 if (parentoffset < 0)
52 FAIL("fdt_parent_offset(): %s", fdt_strerror(parentoffset));
53
54 if (parentoffset != parentpathoffset)
55 FAIL("fdt_parent_offset() returns %d instead of %d",
56 parentoffset, parentpathoffset);
57 }
58
main(int argc,char * argv[])59 int main(int argc, char *argv[])
60 {
61 void *fdt;
62 int err;
63
64 test_init(argc, argv);
65 fdt = load_blob_arg(argc, argv);
66
67 check_path(fdt, "/subnode@1");
68 check_path(fdt, "/subnode@2");
69 check_path(fdt, "/subnode@1/subsubnode");
70 check_path(fdt, "/subnode@2/subsubnode@0");
71 err = fdt_parent_offset(fdt, 0);
72 if (err != -FDT_ERR_NOTFOUND)
73 FAIL("fdt_parent_offset(/) returns %d instead of "
74 "-FDT_ERR_NOTFOUND", err);
75
76 PASS();
77 }
78