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