test-main.c 4.9 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208
  1. /*
  2. * Copyright (c) 2013 Google, Inc
  3. *
  4. * SPDX-License-Identifier: GPL-2.0+
  5. */
  6. #include <common.h>
  7. #include <command.h>
  8. #include <console.h>
  9. #include <dm.h>
  10. #include <errno.h>
  11. #include <malloc.h>
  12. #include <asm/state.h>
  13. #include <dm/test.h>
  14. #include <dm/root.h>
  15. #include <dm/uclass-internal.h>
  16. #include <test/ut.h>
  17. DECLARE_GLOBAL_DATA_PTR;
  18. struct unit_test_state global_dm_test_state;
  19. static struct dm_test_state _global_priv_dm_test_state;
  20. /* Get ready for testing */
  21. static int dm_test_init(struct unit_test_state *uts, bool of_live)
  22. {
  23. struct dm_test_state *dms = uts->priv;
  24. memset(dms, '\0', sizeof(*dms));
  25. gd->dm_root = NULL;
  26. memset(dm_testdrv_op_count, '\0', sizeof(dm_testdrv_op_count));
  27. state_reset_for_test(state_get_current());
  28. #ifdef CONFIG_OF_LIVE
  29. /* Determine whether to make the live tree available */
  30. gd->of_root = of_live ? uts->of_root : NULL;
  31. #endif
  32. ut_assertok(dm_init(of_live));
  33. dms->root = dm_root();
  34. return 0;
  35. }
  36. /* Ensure all the test devices are probed */
  37. static int do_autoprobe(struct unit_test_state *uts)
  38. {
  39. struct udevice *dev;
  40. int ret;
  41. /* Scanning the uclass is enough to probe all the devices */
  42. for (ret = uclass_first_device(UCLASS_TEST, &dev);
  43. dev;
  44. ret = uclass_next_device(&dev))
  45. ;
  46. return ret;
  47. }
  48. static int dm_test_destroy(struct unit_test_state *uts)
  49. {
  50. int id;
  51. for (id = 0; id < UCLASS_COUNT; id++) {
  52. struct uclass *uc;
  53. /*
  54. * If the uclass doesn't exist we don't want to create it. So
  55. * check that here before we call uclass_find_device()/
  56. */
  57. uc = uclass_find(id);
  58. if (!uc)
  59. continue;
  60. ut_assertok(uclass_destroy(uc));
  61. }
  62. return 0;
  63. }
  64. static int dm_do_test(struct unit_test_state *uts, struct unit_test *test,
  65. bool of_live)
  66. {
  67. struct sandbox_state *state = state_get_current();
  68. const char *fname = strrchr(test->file, '/') + 1;
  69. printf("Test: %s: %s%s\n", test->name, fname,
  70. !of_live ? " (flat tree)" : "");
  71. ut_assertok(dm_test_init(uts, of_live));
  72. uts->start = mallinfo();
  73. if (test->flags & DM_TESTF_SCAN_PDATA)
  74. ut_assertok(dm_scan_platdata(false));
  75. if (test->flags & DM_TESTF_PROBE_TEST)
  76. ut_assertok(do_autoprobe(uts));
  77. if (test->flags & DM_TESTF_SCAN_FDT)
  78. ut_assertok(dm_extended_scan_fdt(gd->fdt_blob, false));
  79. /*
  80. * Silence the console and rely on console reocrding to get
  81. * our output.
  82. */
  83. console_record_reset();
  84. if (!state->show_test_output)
  85. gd->flags |= GD_FLG_SILENT;
  86. test->func(uts);
  87. gd->flags &= ~GD_FLG_SILENT;
  88. state_set_skip_delays(false);
  89. ut_assertok(dm_test_destroy(uts));
  90. return 0;
  91. }
  92. /**
  93. * dm_test_run_on_flattree() - Check if we should run a test with flat DT
  94. *
  95. * This skips long/slow tests where there is not much value in running a flat
  96. * DT test in addition to a live DT test.
  97. *
  98. * @return true to run the given test on the flat device tree
  99. */
  100. static bool dm_test_run_on_flattree(struct unit_test *test)
  101. {
  102. const char *fname = strrchr(test->file, '/') + 1;
  103. return !strstr(fname, "video") || strstr(test->name, "video_base");
  104. }
  105. static int dm_test_main(const char *test_name)
  106. {
  107. struct unit_test *tests = ll_entry_start(struct unit_test, dm_test);
  108. const int n_ents = ll_entry_count(struct unit_test, dm_test);
  109. struct unit_test_state *uts = &global_dm_test_state;
  110. struct unit_test *test;
  111. int run_count;
  112. uts->priv = &_global_priv_dm_test_state;
  113. uts->fail_count = 0;
  114. /*
  115. * If we have no device tree, or it only has a root node, then these
  116. * tests clearly aren't going to work...
  117. */
  118. if (!gd->fdt_blob || fdt_next_node(gd->fdt_blob, 0, NULL) < 0) {
  119. puts("Please run with test device tree:\n"
  120. " ./u-boot -d arch/sandbox/dts/test.dtb\n");
  121. ut_assert(gd->fdt_blob);
  122. }
  123. if (!test_name)
  124. printf("Running %d driver model tests\n", n_ents);
  125. run_count = 0;
  126. #ifdef CONFIG_OF_LIVE
  127. uts->of_root = gd->of_root;
  128. #endif
  129. for (test = tests; test < tests + n_ents; test++) {
  130. const char *name = test->name;
  131. int runs;
  132. /* All tests have this prefix */
  133. if (!strncmp(name, "dm_test_", 8))
  134. name += 8;
  135. if (test_name && strcmp(test_name, name))
  136. continue;
  137. /* Run with the live tree if possible */
  138. runs = 0;
  139. if (IS_ENABLED(CONFIG_OF_LIVE)) {
  140. if (!(test->flags & DM_TESTF_FLAT_TREE)) {
  141. ut_assertok(dm_do_test(uts, test, true));
  142. runs++;
  143. }
  144. }
  145. /*
  146. * Run with the flat tree if we couldn't run it with live tree,
  147. * or it is a core test.
  148. */
  149. if (!(test->flags & DM_TESTF_LIVE_TREE) &&
  150. (!runs || dm_test_run_on_flattree(test))) {
  151. ut_assertok(dm_do_test(uts, test, false));
  152. runs++;
  153. }
  154. run_count += runs;
  155. }
  156. if (test_name && !run_count)
  157. printf("Test '%s' not found\n", test_name);
  158. else
  159. printf("Failures: %d\n", uts->fail_count);
  160. gd->dm_root = NULL;
  161. ut_assertok(dm_init(false));
  162. dm_scan_platdata(false);
  163. dm_scan_fdt(gd->fdt_blob, false);
  164. return uts->fail_count ? CMD_RET_FAILURE : 0;
  165. }
  166. int do_ut_dm(cmd_tbl_t *cmdtp, int flag, int argc, char * const argv[])
  167. {
  168. const char *test_name = NULL;
  169. if (argc > 1)
  170. test_name = argv[1];
  171. return dm_test_main(test_name);
  172. }