1 // SPDX-License-Identifier: LGPL-2.1-or-later
2 /*
3 * libfdt - Flat Device Tree manipulation
4 * Testcase for fdt_get_path()
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
17 #define POISON ('\xff')
18
check_path_buf(void * fdt,const char * path,int pathlen,int buflen)19 static void check_path_buf(void *fdt, const char *path, int pathlen, int buflen)
20 {
21 int offset;
22 char buf[buflen+1];
23 int len;
24
25 offset = fdt_path_offset(fdt, path);
26 if (offset < 0)
27 FAIL("Couldn't find path \"%s\": %s", path, fdt_strerror(offset));
28
29 memset(buf, POISON, sizeof(buf)); /* poison the buffer */
30
31 len = fdt_get_path(fdt, offset, buf, buflen);
32 verbose_printf("get_path() %s -> %d -> %s\n", path, offset,
33 len >= 0 ? buf : "<error>");
34
35 if (buflen <= pathlen) {
36 if (len != -FDT_ERR_NOSPACE)
37 FAIL("fdt_get_path([%d bytes]) returns %d with "
38 "insufficient buffer space", buflen, len);
39 } else {
40 if (len < 0)
41 FAIL("fdt_get_path([%d bytes]): %s", buflen,
42 fdt_strerror(len));
43 if (len != 0)
44 FAIL("fdt_get_path([%d bytes]) returns %d "
45 "instead of 0", buflen, len);
46 if (strcmp(buf, path) != 0)
47 FAIL("fdt_get_path([%d bytes]) returns \"%s\" "
48 "instead of \"%s\"", buflen, buf, path);
49 }
50
51 if (buf[buflen] != POISON)
52 FAIL("fdt_get_path([%d bytes]) overran buffer", buflen);
53 }
54
check_path(void * fdt,const char * path)55 static void check_path(void *fdt, const char *path)
56 {
57 int pathlen = strlen(path);
58
59 check_path_buf(fdt, path, pathlen, 1024);
60 check_path_buf(fdt, path, pathlen, pathlen+1);
61 check_path_buf(fdt, path, pathlen, pathlen);
62 check_path_buf(fdt, path, pathlen, 0);
63 check_path_buf(fdt, path, pathlen, 2);
64 }
65
main(int argc,char * argv[])66 int main(int argc, char *argv[])
67 {
68 void *fdt;
69
70 test_init(argc, argv);
71 fdt = load_blob_arg(argc, argv);
72
73 check_path(fdt, "/");
74 check_path(fdt, "/subnode@1");
75 check_path(fdt, "/subnode@2");
76 check_path(fdt, "/subnode@1/subsubnode");
77 check_path(fdt, "/subnode@2/subsubnode@0");
78
79 PASS();
80 }
81