1*cc7d2833Sskrll /* $NetBSD: get_path.c,v 1.1.1.3 2019/12/22 12:34:07 skrll Exp $ */
2d89652e2Sskrll
3*cc7d2833Sskrll // SPDX-License-Identifier: LGPL-2.1-or-later
4b8ae3907Smacallan /*
5b8ae3907Smacallan * libfdt - Flat Device Tree manipulation
6b8ae3907Smacallan * Testcase for fdt_get_path()
7b8ae3907Smacallan * Copyright (C) 2006 David Gibson, IBM Corporation.
8b8ae3907Smacallan */
9b8ae3907Smacallan #include <stdlib.h>
10b8ae3907Smacallan #include <stdio.h>
11b8ae3907Smacallan #include <string.h>
12b8ae3907Smacallan #include <stdint.h>
13b8ae3907Smacallan
14b8ae3907Smacallan #include <libfdt.h>
15b8ae3907Smacallan
16b8ae3907Smacallan #include "tests.h"
17b8ae3907Smacallan #include "testdata.h"
18b8ae3907Smacallan
19b8ae3907Smacallan #define POISON ('\xff')
20b8ae3907Smacallan
check_path_buf(void * fdt,const char * path,int pathlen,int buflen)21b8ae3907Smacallan static void check_path_buf(void *fdt, const char *path, int pathlen, int buflen)
22b8ae3907Smacallan {
23b8ae3907Smacallan int offset;
24b8ae3907Smacallan char buf[buflen+1];
25b8ae3907Smacallan int len;
26b8ae3907Smacallan
27b8ae3907Smacallan offset = fdt_path_offset(fdt, path);
28b8ae3907Smacallan if (offset < 0)
29b8ae3907Smacallan FAIL("Couldn't find path \"%s\": %s", path, fdt_strerror(offset));
30b8ae3907Smacallan
31b8ae3907Smacallan memset(buf, POISON, sizeof(buf)); /* poison the buffer */
32b8ae3907Smacallan
33b8ae3907Smacallan len = fdt_get_path(fdt, offset, buf, buflen);
34*cc7d2833Sskrll verbose_printf("get_path() %s -> %d -> %s\n", path, offset,
35*cc7d2833Sskrll len >= 0 ? buf : "<error>");
36b8ae3907Smacallan
37b8ae3907Smacallan if (buflen <= pathlen) {
38b8ae3907Smacallan if (len != -FDT_ERR_NOSPACE)
39b8ae3907Smacallan FAIL("fdt_get_path([%d bytes]) returns %d with "
40b8ae3907Smacallan "insufficient buffer space", buflen, len);
41b8ae3907Smacallan } else {
42b8ae3907Smacallan if (len < 0)
43b8ae3907Smacallan FAIL("fdt_get_path([%d bytes]): %s", buflen,
44b8ae3907Smacallan fdt_strerror(len));
45b8ae3907Smacallan if (len != 0)
46b8ae3907Smacallan FAIL("fdt_get_path([%d bytes]) returns %d "
47b8ae3907Smacallan "instead of 0", buflen, len);
48b8ae3907Smacallan if (strcmp(buf, path) != 0)
49b8ae3907Smacallan FAIL("fdt_get_path([%d bytes]) returns \"%s\" "
50b8ae3907Smacallan "instead of \"%s\"", buflen, buf, path);
51b8ae3907Smacallan }
52b8ae3907Smacallan
53b8ae3907Smacallan if (buf[buflen] != POISON)
54b8ae3907Smacallan FAIL("fdt_get_path([%d bytes]) overran buffer", buflen);
55b8ae3907Smacallan }
56b8ae3907Smacallan
check_path(void * fdt,const char * path)57b8ae3907Smacallan static void check_path(void *fdt, const char *path)
58b8ae3907Smacallan {
59b8ae3907Smacallan int pathlen = strlen(path);
60b8ae3907Smacallan
61b8ae3907Smacallan check_path_buf(fdt, path, pathlen, 1024);
62b8ae3907Smacallan check_path_buf(fdt, path, pathlen, pathlen+1);
63b8ae3907Smacallan check_path_buf(fdt, path, pathlen, pathlen);
64b8ae3907Smacallan check_path_buf(fdt, path, pathlen, 0);
65b8ae3907Smacallan check_path_buf(fdt, path, pathlen, 2);
66b8ae3907Smacallan }
67b8ae3907Smacallan
main(int argc,char * argv[])68b8ae3907Smacallan int main(int argc, char *argv[])
69b8ae3907Smacallan {
70b8ae3907Smacallan void *fdt;
71b8ae3907Smacallan
72b8ae3907Smacallan test_init(argc, argv);
73b8ae3907Smacallan fdt = load_blob_arg(argc, argv);
74b8ae3907Smacallan
75b8ae3907Smacallan check_path(fdt, "/");
76b8ae3907Smacallan check_path(fdt, "/subnode@1");
77b8ae3907Smacallan check_path(fdt, "/subnode@2");
78b8ae3907Smacallan check_path(fdt, "/subnode@1/subsubnode");
79b8ae3907Smacallan check_path(fdt, "/subnode@2/subsubnode@0");
80b8ae3907Smacallan
81b8ae3907Smacallan PASS();
82b8ae3907Smacallan }
83