selftest.c 7.8 KB

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