1 /*
2 * libfdt - Flat Device Tree manipulation
3 * Testcase for string handling
4 * Copyright (C) 2015 NVIDIA 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
21 #include <stdlib.h>
22 #include <stdio.h>
23 #include <string.h>
24 #include <stdint.h>
25
26 #include <libfdt.h>
27
28 #include "tests.h"
29 #include "testdata.h"
30
check_expected_failure(const void * fdt,const char * path,const char * property)31 static void check_expected_failure(const void *fdt, const char *path,
32 const char *property)
33 {
34 int offset, err;
35
36 offset = fdt_path_offset(fdt, "/");
37 if (offset < 0)
38 FAIL("Couldn't find path %s", path);
39
40 err = fdt_stringlist_count(fdt, offset, "#address-cells");
41 if (err != -FDT_ERR_BADVALUE)
42 FAIL("unexpectedly succeeded in parsing #address-cells\n");
43
44 err = fdt_stringlist_search(fdt, offset, "#address-cells", "foo");
45 if (err != -FDT_ERR_BADVALUE)
46 FAIL("found string in #address-cells: %d\n", err);
47
48 /*
49 * Note that the #address-cells property contains a small 32-bit
50 * unsigned integer, hence some bytes will be zero, and searching for
51 * the empty string will succeed.
52 *
53 * The reason for this oddity is that the function will exit when the
54 * first occurrence of the string is found, but in order to determine
55 * that the property does not contain a valid string list it would
56 * need to process the whole value.
57 */
58 err = fdt_stringlist_search(fdt, offset, "#address-cells", "");
59 if (err != 0)
60 FAIL("empty string not found in #address-cells: %d\n", err);
61
62 /*
63 * fdt_get_string() can successfully extract strings from non-string
64 * properties. This is because it doesn't necessarily parse the whole
65 * property value, which would be necessary for it to determine if a
66 * valid string or string list is present.
67 */
68 }
69
check_string_count(const void * fdt,const char * path,const char * property,int count)70 static void check_string_count(const void *fdt, const char *path,
71 const char *property, int count)
72 {
73 int offset, err;
74
75 offset = fdt_path_offset(fdt, path);
76 if (offset < 0)
77 FAIL("Couldn't find path %s", path);
78
79 err = fdt_stringlist_count(fdt, offset, property);
80 if (err < 0)
81 FAIL("Couldn't count strings in property %s of node %s: %d\n",
82 property, path, err);
83
84 if (err != count)
85 FAIL("String count for property %s of node %s is %d instead of %d\n",
86 path, property, err, count);
87 }
88
check_string_index(const void * fdt,const char * path,const char * property,const char * string,int idx)89 static void check_string_index(const void *fdt, const char *path,
90 const char *property, const char *string,
91 int idx)
92 {
93 int offset, err;
94
95 offset = fdt_path_offset(fdt, path);
96 if (offset < 0)
97 FAIL("Couldn't find path %s", path);
98
99 err = fdt_stringlist_search(fdt, offset, property, string);
100
101 if (err != idx)
102 FAIL("Index of %s in property %s of node %s is %d, expected %d\n",
103 string, property, path, err, idx);
104 }
105
check_string(const void * fdt,const char * path,const char * property,int idx,const char * string)106 static void check_string(const void *fdt, const char *path,
107 const char *property, int idx,
108 const char *string)
109 {
110 const char *result;
111 int offset, len;
112
113 offset = fdt_path_offset(fdt, path);
114 if (offset < 0)
115 FAIL("Couldn't find path %s", path);
116
117 result = fdt_stringlist_get(fdt, offset, property, idx, &len);
118 if (!result)
119 FAIL("Couldn't extract string %d from property %s of node %s: %d\n",
120 idx, property, path, len);
121
122 if (strcmp(string, result) != 0)
123 FAIL("String %d in property %s of node %s is %s, expected %s\n",
124 idx, property, path, result, string);
125 }
126
main(int argc,char * argv[])127 int main(int argc, char *argv[])
128 {
129 void *fdt;
130
131 if (argc != 2)
132 CONFIG("Usage: %s <dtb file>\n", argv[0]);
133
134 test_init(argc, argv);
135 fdt = load_blob(argv[1]);
136
137 check_expected_failure(fdt, "/", "#address-cells");
138 check_expected_failure(fdt, "/", "#size-cells");
139
140 check_string_count(fdt, "/", "compatible", 1);
141 check_string_count(fdt, "/device", "compatible", 2);
142 check_string_count(fdt, "/device", "big-endian", 0);
143
144 check_string_index(fdt, "/", "compatible", "test-strings", 0);
145 check_string_index(fdt, "/device", "compatible", "foo", 0);
146 check_string_index(fdt, "/device", "compatible", "bar", 1);
147 check_string_index(fdt, "/device", "big-endian", "baz", -1);
148
149 check_string(fdt, "/", "compatible", 0, "test-strings");
150 check_string(fdt, "/device", "compatible", 0, "foo");
151 check_string(fdt, "/device", "compatible", 1, "bar");
152
153 PASS();
154 }
155