|
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 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 if (buflen <= pathlen) { |
|
47 if (len != -FDT_ERR_NOSPACE) |
|
48 FAIL("fdt_get_path([%d bytes]) returns %d with " |
|
49 "insufficient buffer space", buflen, len); |
|
50 } else { |
|
51 if (len < 0) |
|
52 FAIL("fdt_get_path([%d bytes]): %s", buflen, |
|
53 fdt_strerror(len)); |
|
54 if (len != pathlen) |
|
55 FAIL("fdt_get_path([%d bytes]) reports length %d " |
|
56 "instead of %d", buflen, len, pathlen); |
|
57 if (strcmp(buf, path) != 0) |
|
58 FAIL("fdt_get_path([%d bytes]) returns \"%s\" " |
|
59 "instead of \"%s\"", buflen, buf, path); |
|
60 } |
|
61 |
|
62 if (buf[buflen] != POISON) |
|
63 FAIL("fdt_get_path([%d bytes]) overran buffer", buflen); |
|
64 } |
|
65 |
|
66 void check_path(void *fdt, const char *path) |
|
67 { |
|
68 int pathlen = strlen(path); |
|
69 |
|
70 check_path_buf(fdt, path, pathlen, 1024); |
|
71 check_path_buf(fdt, path, pathlen, pathlen+1); |
|
72 check_path_buf(fdt, path, pathlen, pathlen); |
|
73 } |
|
74 |
|
75 int main(int argc, char *argv[]) |
|
76 { |
|
77 void *fdt; |
|
78 |
|
79 test_init(argc, argv); |
|
80 fdt = load_blob_arg(argc, argv); |
|
81 |
|
82 check_path(fdt, "/"); |
|
83 check_path(fdt, "/subnode@1"); |
|
84 check_path(fdt, "/subnode@2"); |
|
85 check_path(fdt, "/subnode@1/subsubnode"); |
|
86 check_path(fdt, "/subnode@2/subsubnode@0"); |
|
87 |
|
88 PASS(); |
|
89 } |