selftest.c 7.8 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214
  1. /*
  2. * Self tests for device tree subsystem
  3. */
  4. #define pr_fmt(fmt) "### dt-test ### " fmt
  5. #include <linux/clk.h>
  6. #include <linux/err.h>
  7. #include <linux/errno.h>
  8. #include <linux/module.h>
  9. #include <linux/of.h>
  10. #include <linux/list.h>
  11. #include <linux/mutex.h>
  12. #include <linux/slab.h>
  13. #include <linux/device.h>
  14. static bool selftest_passed = true;
  15. #define selftest(result, fmt, ...) { \
  16. if (!(result)) { \
  17. pr_err("FAIL %s:%i " fmt, __FILE__, __LINE__, ##__VA_ARGS__); \
  18. selftest_passed = false; \
  19. } else { \
  20. pr_info("pass %s:%i\n", __FILE__, __LINE__); \
  21. } \
  22. }
  23. static void __init of_selftest_parse_phandle_with_args(void)
  24. {
  25. struct device_node *np;
  26. struct of_phandle_args args;
  27. int i, rc;
  28. np = of_find_node_by_path("/testcase-data/phandle-tests/consumer-a");
  29. if (!np) {
  30. pr_err("missing testcase data\n");
  31. return;
  32. }
  33. for (i = 0; i < 8; i++) {
  34. bool passed = true;
  35. rc = of_parse_phandle_with_args(np, "phandle-list",
  36. "#phandle-cells", i, &args);
  37. /* Test the values from tests-phandle.dtsi */
  38. switch (i) {
  39. case 0:
  40. passed &= !rc;
  41. passed &= (args.args_count == 1);
  42. passed &= (args.args[0] == (i + 1));
  43. break;
  44. case 1:
  45. passed &= !rc;
  46. passed &= (args.args_count == 2);
  47. passed &= (args.args[0] == (i + 1));
  48. passed &= (args.args[1] == 0);
  49. break;
  50. case 2:
  51. passed &= (rc == -ENOENT);
  52. break;
  53. case 3:
  54. passed &= !rc;
  55. passed &= (args.args_count == 3);
  56. passed &= (args.args[0] == (i + 1));
  57. passed &= (args.args[1] == 4);
  58. passed &= (args.args[2] == 3);
  59. break;
  60. case 4:
  61. passed &= !rc;
  62. passed &= (args.args_count == 2);
  63. passed &= (args.args[0] == (i + 1));
  64. passed &= (args.args[1] == 100);
  65. break;
  66. case 5:
  67. passed &= !rc;
  68. passed &= (args.args_count == 0);
  69. break;
  70. case 6:
  71. passed &= !rc;
  72. passed &= (args.args_count == 1);
  73. passed &= (args.args[0] == (i + 1));
  74. break;
  75. case 7:
  76. passed &= (rc == -ENOENT);
  77. break;
  78. default:
  79. passed = false;
  80. }
  81. selftest(passed, "index %i - data error on node %s rc=%i\n",
  82. i, args.np->full_name, rc);
  83. }
  84. /* Check for missing list property */
  85. rc = of_parse_phandle_with_args(np, "phandle-list-missing",
  86. "#phandle-cells", 0, &args);
  87. selftest(rc == -ENOENT, "expected:%i got:%i\n", -ENOENT, rc);
  88. /* Check for missing cells property */
  89. rc = of_parse_phandle_with_args(np, "phandle-list",
  90. "#phandle-cells-missing", 0, &args);
  91. selftest(rc == -EINVAL, "expected:%i got:%i\n", -EINVAL, rc);
  92. /* Check for bad phandle in list */
  93. rc = of_parse_phandle_with_args(np, "phandle-list-bad-phandle",
  94. "#phandle-cells", 0, &args);
  95. selftest(rc == -EINVAL, "expected:%i got:%i\n", -EINVAL, rc);
  96. /* Check for incorrectly formed argument list */
  97. rc = of_parse_phandle_with_args(np, "phandle-list-bad-args",
  98. "#phandle-cells", 1, &args);
  99. selftest(rc == -EINVAL, "expected:%i got:%i\n", -EINVAL, rc);
  100. }
  101. static void __init of_selftest_property_string(void)
  102. {
  103. const char *strings[4];
  104. struct device_node *np;
  105. int rc;
  106. pr_info("start\n");
  107. np = of_find_node_by_path("/testcase-data/phandle-tests/consumer-a");
  108. if (!np) {
  109. pr_err("No testcase data in device tree\n");
  110. return;
  111. }
  112. rc = of_property_match_string(np, "phandle-list-names", "first");
  113. selftest(rc == 0, "first expected:0 got:%i\n", rc);
  114. rc = of_property_match_string(np, "phandle-list-names", "second");
  115. selftest(rc == 1, "second expected:0 got:%i\n", rc);
  116. rc = of_property_match_string(np, "phandle-list-names", "third");
  117. selftest(rc == 2, "third expected:0 got:%i\n", rc);
  118. rc = of_property_match_string(np, "phandle-list-names", "fourth");
  119. selftest(rc == -ENODATA, "unmatched string; rc=%i\n", rc);
  120. rc = of_property_match_string(np, "missing-property", "blah");
  121. selftest(rc == -EINVAL, "missing property; rc=%i\n", rc);
  122. rc = of_property_match_string(np, "empty-property", "blah");
  123. selftest(rc == -ENODATA, "empty property; rc=%i\n", rc);
  124. rc = of_property_match_string(np, "unterminated-string", "blah");
  125. selftest(rc == -EILSEQ, "unterminated string; rc=%i\n", rc);
  126. /* of_property_count_strings() tests */
  127. rc = of_property_count_strings(np, "string-property");
  128. selftest(rc == 1, "Incorrect string count; rc=%i\n", rc);
  129. rc = of_property_count_strings(np, "phandle-list-names");
  130. selftest(rc == 3, "Incorrect string count; rc=%i\n", rc);
  131. rc = of_property_count_strings(np, "unterminated-string");
  132. selftest(rc == -EILSEQ, "unterminated string; rc=%i\n", rc);
  133. rc = of_property_count_strings(np, "unterminated-string-list");
  134. selftest(rc == -EILSEQ, "unterminated string array; rc=%i\n", rc);
  135. /* of_property_read_string_index() tests */
  136. rc = of_property_read_string_index(np, "string-property", 0, strings);
  137. selftest(rc == 0 && !strcmp(strings[0], "foobar"), "of_property_read_string_index() failure; rc=%i\n", rc);
  138. strings[0] = NULL;
  139. rc = of_property_read_string_index(np, "string-property", 1, strings);
  140. selftest(rc == -ENODATA && strings[0] == NULL, "of_property_read_string_index() failure; rc=%i\n", rc);
  141. rc = of_property_read_string_index(np, "phandle-list-names", 0, strings);
  142. selftest(rc == 0 && !strcmp(strings[0], "first"), "of_property_read_string_index() failure; rc=%i\n", rc);
  143. rc = of_property_read_string_index(np, "phandle-list-names", 1, strings);
  144. selftest(rc == 0 && !strcmp(strings[0], "second"), "of_property_read_string_index() failure; rc=%i\n", rc);
  145. rc = of_property_read_string_index(np, "phandle-list-names", 2, strings);
  146. selftest(rc == 0 && !strcmp(strings[0], "third"), "of_property_read_string_index() failure; rc=%i\n", rc);
  147. strings[0] = NULL;
  148. rc = of_property_read_string_index(np, "phandle-list-names", 3, strings);
  149. selftest(rc == -ENODATA && strings[0] == NULL, "of_property_read_string_index() failure; rc=%i\n", rc);
  150. strings[0] = NULL;
  151. rc = of_property_read_string_index(np, "unterminated-string", 0, strings);
  152. selftest(rc == -EILSEQ && strings[0] == NULL, "of_property_read_string_index() failure; rc=%i\n", rc);
  153. rc = of_property_read_string_index(np, "unterminated-string-list", 0, strings);
  154. selftest(rc == 0 && !strcmp(strings[0], "first"), "of_property_read_string_index() failure; rc=%i\n", rc);
  155. strings[0] = NULL;
  156. rc = of_property_read_string_index(np, "unterminated-string-list", 2, strings); /* should fail */
  157. selftest(rc == -EILSEQ && strings[0] == NULL, "of_property_read_string_index() failure; rc=%i\n", rc);
  158. strings[1] = NULL;
  159. /* of_property_read_string_array() tests */
  160. rc = of_property_read_string_array(np, "string-property", strings, 4);
  161. selftest(rc == 1, "Incorrect string count; rc=%i\n", rc);
  162. rc = of_property_read_string_array(np, "phandle-list-names", strings, 4);
  163. selftest(rc == 3, "Incorrect string count; rc=%i\n", rc);
  164. rc = of_property_read_string_array(np, "unterminated-string", strings, 4);
  165. selftest(rc == -EILSEQ, "unterminated string; rc=%i\n", rc);
  166. /* -- An incorrectly formed string should cause a failure */
  167. rc = of_property_read_string_array(np, "unterminated-string-list", strings, 4);
  168. selftest(rc == -EILSEQ, "unterminated string array; rc=%i\n", rc);
  169. /* -- parsing the correctly formed strings should still work: */
  170. strings[2] = NULL;
  171. rc = of_property_read_string_array(np, "unterminated-string-list", strings, 2);
  172. selftest(rc == 2 && strings[2] == NULL, "of_property_read_string_array() failure; rc=%i\n", rc);
  173. strings[1] = NULL;
  174. rc = of_property_read_string_array(np, "phandle-list-names", strings, 1);
  175. selftest(rc == 1 && strings[1] == NULL, "Overwrote end of string array; rc=%i, str='%s'\n", rc, strings[1]);
  176. }
  177. static int __init of_selftest(void)
  178. {
  179. struct device_node *np;
  180. np = of_find_node_by_path("/testcase-data/phandle-tests/consumer-a");
  181. if (!np) {
  182. pr_info("No testcase data in device tree; not running tests\n");
  183. return 0;
  184. }
  185. of_node_put(np);
  186. pr_info("start of selftest - you will see error messages\n");
  187. of_selftest_parse_phandle_with_args();
  188. of_selftest_property_string();
  189. pr_info("end of selftest - %s\n", selftest_passed ? "PASS" : "FAIL");
  190. return 0;
  191. }
  192. late_initcall(of_selftest);