test-main.c 4.9 KB

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