Add qemu 2.4.0
[kvmfornfv.git] / qemu / dtc / 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 <libfdt.h>
26
27 #include "tests.h"
28 #include "testdata.h"
29
30 #define POISON  ('\xff')
31
32 static void check_path_buf(void *fdt, const char *path, int pathlen, int buflen)
33 {
34         int offset;
35         char buf[buflen+1];
36         int len;
37
38         offset = fdt_path_offset(fdt, path);
39         if (offset < 0)
40                 FAIL("Couldn't find path \"%s\": %s", path, fdt_strerror(offset));
41
42         memset(buf, POISON, sizeof(buf)); /* poison the buffer */
43
44         len = fdt_get_path(fdt, offset, buf, buflen);
45         verbose_printf("get_path() %s -> %d -> %s\n", path, offset, buf);
46
47         if (buflen <= pathlen) {
48                 if (len != -FDT_ERR_NOSPACE)
49                         FAIL("fdt_get_path([%d bytes]) returns %d with "
50                              "insufficient buffer space", buflen, len);
51         } else {
52                 if (len < 0)
53                         FAIL("fdt_get_path([%d bytes]): %s", buflen,
54                              fdt_strerror(len));
55                 if (len != 0)
56                         FAIL("fdt_get_path([%d bytes]) returns %d "
57                              "instead of 0", buflen, len);
58                 if (strcmp(buf, path) != 0)
59                         FAIL("fdt_get_path([%d bytes]) returns \"%s\" "
60                              "instead of \"%s\"", buflen, buf, path);
61         }
62
63         if (buf[buflen] != POISON)
64                 FAIL("fdt_get_path([%d bytes]) overran buffer", buflen);
65 }
66
67 static void check_path(void *fdt, const char *path)
68 {
69         int pathlen = strlen(path);
70
71         check_path_buf(fdt, path, pathlen, 1024);
72         check_path_buf(fdt, path, pathlen, pathlen+1);
73         check_path_buf(fdt, path, pathlen, pathlen);
74         check_path_buf(fdt, path, pathlen, 0);
75         check_path_buf(fdt, path, pathlen, 2);
76 }
77
78 int main(int argc, char *argv[])
79 {
80         void *fdt;
81
82         test_init(argc, argv);
83         fdt = load_blob_arg(argc, argv);
84
85         check_path(fdt, "/");
86         check_path(fdt, "/subnode@1");
87         check_path(fdt, "/subnode@2");
88         check_path(fdt, "/subnode@1/subsubnode");
89         check_path(fdt, "/subnode@2/subsubnode@0");
90
91         PASS();
92 }