]> CyberLeo.Net >> Repos - FreeBSD/FreeBSD.git/blob - tests/get_path.c
Update DTC to git d75b33af676d0beac8398651a7f09037555a550b.
[FreeBSD/FreeBSD.git] / tests / get_path.c
1 /*
2  * libfdt - Flat Device Tree manipulation
3  *      Testcase for fdt_get_path()
4  * Copyright (C) 2006 David Gibson, IBM Corporation.
5  *
6  * This library is free software; you can redistribute it and/or
7  * modify it under the terms of the GNU Lesser General Public License
8  * as published by the Free Software Foundation; either version 2.1 of
9  * the License, or (at your option) any later version.
10  *
11  * This library is distributed in the hope that it will be useful, but
12  * WITHOUT ANY WARRANTY; without even the implied warranty of
13  * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the GNU
14  * Lesser General Public License for more details.
15  *
16  * You should have received a copy of the GNU Lesser General Public
17  * License along with this library; if not, write to the Free Software
18  * Foundation, Inc., 51 Franklin St, Fifth Floor, Boston, MA 02110-1301 USA
19  */
20 #include <stdlib.h>
21 #include <stdio.h>
22 #include <string.h>
23 #include <stdint.h>
24
25 #include <fdt.h>
26 #include <libfdt.h>
27
28 #include "tests.h"
29 #include "testdata.h"
30
31 #define POISON  ('\xff')
32
33 static void check_path_buf(void *fdt, const char *path, int pathlen, int buflen)
34 {
35         int offset;
36         char buf[buflen+1];
37         int len;
38
39         offset = fdt_path_offset(fdt, path);
40         if (offset < 0)
41                 FAIL("Couldn't find path \"%s\": %s", path, fdt_strerror(offset));
42
43         memset(buf, POISON, sizeof(buf)); /* poison the buffer */
44
45         len = fdt_get_path(fdt, offset, buf, buflen);
46         verbose_printf("get_path() %s -> %d -> %s\n", path, offset, buf);
47
48         if (buflen <= pathlen) {
49                 if (len != -FDT_ERR_NOSPACE)
50                         FAIL("fdt_get_path([%d bytes]) returns %d with "
51                              "insufficient buffer space", buflen, len);
52         } else {
53                 if (len < 0)
54                         FAIL("fdt_get_path([%d bytes]): %s", buflen,
55                              fdt_strerror(len));
56                 if (len != 0)
57                         FAIL("fdt_get_path([%d bytes]) returns %d "
58                              "instead of 0", buflen, len);
59                 if (strcmp(buf, path) != 0)
60                         FAIL("fdt_get_path([%d bytes]) returns \"%s\" "
61                              "instead of \"%s\"", buflen, buf, path);
62         }
63
64         if (buf[buflen] != POISON)
65                 FAIL("fdt_get_path([%d bytes]) overran buffer", buflen);
66 }
67
68 static void check_path(void *fdt, const char *path)
69 {
70         int pathlen = strlen(path);
71
72         check_path_buf(fdt, path, pathlen, 1024);
73         check_path_buf(fdt, path, pathlen, pathlen+1);
74         check_path_buf(fdt, path, pathlen, pathlen);
75         check_path_buf(fdt, path, pathlen, 0);
76         check_path_buf(fdt, path, pathlen, 2);
77 }
78
79 int main(int argc, char *argv[])
80 {
81         void *fdt;
82
83         test_init(argc, argv);
84         fdt = load_blob_arg(argc, argv);
85
86         check_path(fdt, "/");
87         check_path(fdt, "/subnode@1");
88         check_path(fdt, "/subnode@2");
89         check_path(fdt, "/subnode@1/subsubnode");
90         check_path(fdt, "/subnode@2/subsubnode@0");
91
92         PASS();
93 }