2 * Self tests for device tree subsystem
5 #define pr_fmt(fmt) "### dt-test ### " fmt
9 #include <linux/errno.h>
10 #include <linux/module.h>
12 #include <linux/list.h>
13 #include <linux/mutex.h>
14 #include <linux/slab.h>
15 #include <linux/device.h>
17 static bool selftest_passed
= true;
18 #define selftest(result, fmt, ...) { \
20 pr_err("FAIL %s:%i " fmt, __FILE__, __LINE__, ##__VA_ARGS__); \
21 selftest_passed = false; \
23 pr_info("pass %s:%i\n", __FILE__, __LINE__); \
27 static void __init
of_selftest_parse_phandle_with_args(void)
29 struct device_node
*np
;
30 struct of_phandle_args args
;
33 np
= of_find_node_by_path("/testcase-data/phandle-tests/consumer-a");
35 pr_err("missing testcase data\n");
39 rc
= of_count_phandle_with_args(np
, "phandle-list", "#phandle-cells");
40 selftest(rc
== 7, "of_count_phandle_with_args() returned %i, expected 7\n", rc
);
42 for (i
= 0; i
< 8; i
++) {
44 rc
= of_parse_phandle_with_args(np
, "phandle-list",
45 "#phandle-cells", i
, &args
);
47 /* Test the values from tests-phandle.dtsi */
51 passed
&= (args
.args_count
== 1);
52 passed
&= (args
.args
[0] == (i
+ 1));
56 passed
&= (args
.args_count
== 2);
57 passed
&= (args
.args
[0] == (i
+ 1));
58 passed
&= (args
.args
[1] == 0);
61 passed
&= (rc
== -ENOENT
);
65 passed
&= (args
.args_count
== 3);
66 passed
&= (args
.args
[0] == (i
+ 1));
67 passed
&= (args
.args
[1] == 4);
68 passed
&= (args
.args
[2] == 3);
72 passed
&= (args
.args_count
== 2);
73 passed
&= (args
.args
[0] == (i
+ 1));
74 passed
&= (args
.args
[1] == 100);
78 passed
&= (args
.args_count
== 0);
82 passed
&= (args
.args_count
== 1);
83 passed
&= (args
.args
[0] == (i
+ 1));
86 passed
&= (rc
== -ENOENT
);
92 selftest(passed
, "index %i - data error on node %s rc=%i\n",
93 i
, args
.np
->full_name
, rc
);
96 /* Check for missing list property */
97 rc
= of_parse_phandle_with_args(np
, "phandle-list-missing",
98 "#phandle-cells", 0, &args
);
99 selftest(rc
== -ENOENT
, "expected:%i got:%i\n", -ENOENT
, rc
);
100 rc
= of_count_phandle_with_args(np
, "phandle-list-missing",
102 selftest(rc
== -ENOENT
, "expected:%i got:%i\n", -ENOENT
, rc
);
104 /* Check for missing cells property */
105 rc
= of_parse_phandle_with_args(np
, "phandle-list",
106 "#phandle-cells-missing", 0, &args
);
107 selftest(rc
== -EINVAL
, "expected:%i got:%i\n", -EINVAL
, rc
);
108 rc
= of_count_phandle_with_args(np
, "phandle-list",
109 "#phandle-cells-missing");
110 selftest(rc
== -EINVAL
, "expected:%i got:%i\n", -EINVAL
, rc
);
112 /* Check for bad phandle in list */
113 rc
= of_parse_phandle_with_args(np
, "phandle-list-bad-phandle",
114 "#phandle-cells", 0, &args
);
115 selftest(rc
== -EINVAL
, "expected:%i got:%i\n", -EINVAL
, rc
);
116 rc
= of_count_phandle_with_args(np
, "phandle-list-bad-phandle",
118 selftest(rc
== -EINVAL
, "expected:%i got:%i\n", -EINVAL
, rc
);
120 /* Check for incorrectly formed argument list */
121 rc
= of_parse_phandle_with_args(np
, "phandle-list-bad-args",
122 "#phandle-cells", 1, &args
);
123 selftest(rc
== -EINVAL
, "expected:%i got:%i\n", -EINVAL
, rc
);
124 rc
= of_count_phandle_with_args(np
, "phandle-list-bad-args",
126 selftest(rc
== -EINVAL
, "expected:%i got:%i\n", -EINVAL
, rc
);
129 static void __init
of_selftest_property_string(void)
131 const char *strings
[4];
132 struct device_node
*np
;
136 np
= of_find_node_by_path("/testcase-data/phandle-tests/consumer-a");
138 pr_err("No testcase data in device tree\n");
142 rc
= of_property_match_string(np
, "phandle-list-names", "first");
143 selftest(rc
== 0, "first expected:0 got:%i\n", rc
);
144 rc
= of_property_match_string(np
, "phandle-list-names", "second");
145 selftest(rc
== 1, "second expected:0 got:%i\n", rc
);
146 rc
= of_property_match_string(np
, "phandle-list-names", "third");
147 selftest(rc
== 2, "third expected:0 got:%i\n", rc
);
148 rc
= of_property_match_string(np
, "phandle-list-names", "fourth");
149 selftest(rc
== -ENODATA
, "unmatched string; rc=%i\n", rc
);
150 rc
= of_property_match_string(np
, "missing-property", "blah");
151 selftest(rc
== -EINVAL
, "missing property; rc=%i\n", rc
);
152 rc
= of_property_match_string(np
, "empty-property", "blah");
153 selftest(rc
== -ENODATA
, "empty property; rc=%i\n", rc
);
154 rc
= of_property_match_string(np
, "unterminated-string", "blah");
155 selftest(rc
== -EILSEQ
, "unterminated string; rc=%i\n", rc
);
157 /* of_property_count_strings() tests */
158 rc
= of_property_count_strings(np
, "string-property");
159 selftest(rc
== 1, "Incorrect string count; rc=%i\n", rc
);
160 rc
= of_property_count_strings(np
, "phandle-list-names");
161 selftest(rc
== 3, "Incorrect string count; rc=%i\n", rc
);
162 rc
= of_property_count_strings(np
, "unterminated-string");
163 selftest(rc
== -EILSEQ
, "unterminated string; rc=%i\n", rc
);
164 rc
= of_property_count_strings(np
, "unterminated-string-list");
165 selftest(rc
== -EILSEQ
, "unterminated string array; rc=%i\n", rc
);
167 /* of_property_read_string_index() tests */
168 rc
= of_property_read_string_index(np
, "string-property", 0, strings
);
169 selftest(rc
== 0 && !strcmp(strings
[0], "foobar"), "of_property_read_string_index() failure; rc=%i\n", rc
);
171 rc
= of_property_read_string_index(np
, "string-property", 1, strings
);
172 selftest(rc
== -ENODATA
&& strings
[0] == NULL
, "of_property_read_string_index() failure; rc=%i\n", rc
);
173 rc
= of_property_read_string_index(np
, "phandle-list-names", 0, strings
);
174 selftest(rc
== 0 && !strcmp(strings
[0], "first"), "of_property_read_string_index() failure; rc=%i\n", rc
);
175 rc
= of_property_read_string_index(np
, "phandle-list-names", 1, strings
);
176 selftest(rc
== 0 && !strcmp(strings
[0], "second"), "of_property_read_string_index() failure; rc=%i\n", rc
);
177 rc
= of_property_read_string_index(np
, "phandle-list-names", 2, strings
);
178 selftest(rc
== 0 && !strcmp(strings
[0], "third"), "of_property_read_string_index() failure; rc=%i\n", rc
);
180 rc
= of_property_read_string_index(np
, "phandle-list-names", 3, strings
);
181 selftest(rc
== -ENODATA
&& strings
[0] == NULL
, "of_property_read_string_index() failure; rc=%i\n", rc
);
183 rc
= of_property_read_string_index(np
, "unterminated-string", 0, strings
);
184 selftest(rc
== -EILSEQ
&& strings
[0] == NULL
, "of_property_read_string_index() failure; rc=%i\n", rc
);
185 rc
= of_property_read_string_index(np
, "unterminated-string-list", 0, strings
);
186 selftest(rc
== 0 && !strcmp(strings
[0], "first"), "of_property_read_string_index() failure; rc=%i\n", rc
);
188 rc
= of_property_read_string_index(np
, "unterminated-string-list", 2, strings
); /* should fail */
189 selftest(rc
== -EILSEQ
&& strings
[0] == NULL
, "of_property_read_string_index() failure; rc=%i\n", rc
);
192 /* of_property_read_string_array() tests */
193 rc
= of_property_read_string_array(np
, "string-property", strings
, 4);
194 selftest(rc
== 1, "Incorrect string count; rc=%i\n", rc
);
195 rc
= of_property_read_string_array(np
, "phandle-list-names", strings
, 4);
196 selftest(rc
== 3, "Incorrect string count; rc=%i\n", rc
);
197 rc
= of_property_read_string_array(np
, "unterminated-string", strings
, 4);
198 selftest(rc
== -EILSEQ
, "unterminated string; rc=%i\n", rc
);
199 /* -- An incorrectly formed string should cause a failure */
200 rc
= of_property_read_string_array(np
, "unterminated-string-list", strings
, 4);
201 selftest(rc
== -EILSEQ
, "unterminated string array; rc=%i\n", rc
);
202 /* -- parsing the correctly formed strings should still work: */
204 rc
= of_property_read_string_array(np
, "unterminated-string-list", strings
, 2);
205 selftest(rc
== 2 && strings
[2] == NULL
, "of_property_read_string_array() failure; rc=%i\n", rc
);
207 rc
= of_property_read_string_array(np
, "phandle-list-names", strings
, 1);
208 selftest(rc
== 1 && strings
[1] == NULL
, "Overwrote end of string array; rc=%i, str='%s'\n", rc
, strings
[1]);
211 static int __init
of_selftest(void)
213 struct device_node
*np
;
215 np
= of_find_node_by_path("/testcase-data/phandle-tests/consumer-a");
217 pr_info("No testcase data in device tree; not running tests\n");
222 pr_info("start of selftest - you will see error messages\n");
223 of_selftest_parse_phandle_with_args();
224 of_selftest_property_string();
225 pr_info("end of selftest - %s\n", selftest_passed
? "PASS" : "FAIL");
228 late_initcall(of_selftest
);