test-main.c 3.5 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155
  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)
  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. ut_assertok(dm_init());
  28. dms->root = dm_root();
  29. return 0;
  30. }
  31. /* Ensure all the test devices are probed */
  32. static int do_autoprobe(struct unit_test_state *uts)
  33. {
  34. struct udevice *dev;
  35. int ret;
  36. /* Scanning the uclass is enough to probe all the devices */
  37. for (ret = uclass_first_device(UCLASS_TEST, &dev);
  38. dev;
  39. ret = uclass_next_device(&dev))
  40. ;
  41. return ret;
  42. }
  43. static int dm_test_destroy(struct unit_test_state *uts)
  44. {
  45. int id;
  46. for (id = 0; id < UCLASS_COUNT; id++) {
  47. struct uclass *uc;
  48. /*
  49. * If the uclass doesn't exist we don't want to create it. So
  50. * check that here before we call uclass_find_device()/
  51. */
  52. uc = uclass_find(id);
  53. if (!uc)
  54. continue;
  55. ut_assertok(uclass_destroy(uc));
  56. }
  57. return 0;
  58. }
  59. static int dm_test_main(const char *test_name)
  60. {
  61. struct unit_test *tests = ll_entry_start(struct unit_test, dm_test);
  62. const int n_ents = ll_entry_count(struct unit_test, dm_test);
  63. struct unit_test_state *uts = &global_dm_test_state;
  64. struct sandbox_state *state = state_get_current();
  65. uts->priv = &_global_priv_dm_test_state;
  66. struct unit_test *test;
  67. int run_count;
  68. uts->fail_count = 0;
  69. /*
  70. * If we have no device tree, or it only has a root node, then these
  71. * tests clearly aren't going to work...
  72. */
  73. if (!gd->fdt_blob || fdt_next_node(gd->fdt_blob, 0, NULL) < 0) {
  74. puts("Please run with test device tree:\n"
  75. " ./u-boot -d arch/sandbox/dts/test.dtb\n");
  76. ut_assert(gd->fdt_blob);
  77. }
  78. if (!test_name)
  79. printf("Running %d driver model tests\n", n_ents);
  80. run_count = 0;
  81. for (test = tests; test < tests + n_ents; test++) {
  82. const char *name = test->name;
  83. /* All tests have this prefix */
  84. if (!strncmp(name, "dm_test_", 8))
  85. name += 8;
  86. if (test_name && strcmp(test_name, name))
  87. continue;
  88. printf("Test: %s\n", test->name);
  89. run_count++;
  90. ut_assertok(dm_test_init(uts));
  91. uts->start = mallinfo();
  92. if (test->flags & DM_TESTF_SCAN_PDATA)
  93. ut_assertok(dm_scan_platdata(false));
  94. if (test->flags & DM_TESTF_PROBE_TEST)
  95. ut_assertok(do_autoprobe(uts));
  96. if (test->flags & DM_TESTF_SCAN_FDT)
  97. ut_assertok(dm_scan_fdt(gd->fdt_blob, false));
  98. /*
  99. * Silence the console and rely on console reocrding to get
  100. * our output.
  101. */
  102. console_record_reset();
  103. if (!state->show_test_output)
  104. gd->flags |= GD_FLG_SILENT;
  105. test->func(uts);
  106. gd->flags &= ~GD_FLG_SILENT;
  107. state_set_skip_delays(false);
  108. ut_assertok(dm_test_destroy(uts));
  109. }
  110. if (test_name && !run_count)
  111. printf("Test '%s' not found\n", test_name);
  112. else
  113. printf("Failures: %d\n", uts->fail_count);
  114. gd->dm_root = NULL;
  115. ut_assertok(dm_init());
  116. dm_scan_platdata(false);
  117. dm_scan_fdt(gd->fdt_blob, false);
  118. return uts->fail_count ? CMD_RET_FAILURE : 0;
  119. }
  120. int do_ut_dm(cmd_tbl_t *cmdtp, int flag, int argc, char * const argv[])
  121. {
  122. const char *test_name = NULL;
  123. if (argc > 1)
  124. test_name = argv[1];
  125. return dm_test_main(test_name);
  126. }