test-main.c 3.2 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141
  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 <dm.h>
  9. #include <errno.h>
  10. #include <malloc.h>
  11. #include <dm/test.h>
  12. #include <dm/root.h>
  13. #include <dm/uclass-internal.h>
  14. #include <test/ut.h>
  15. DECLARE_GLOBAL_DATA_PTR;
  16. struct unit_test_state global_dm_test_state;
  17. static struct dm_test_state _global_priv_dm_test_state;
  18. /* Get ready for testing */
  19. static int dm_test_init(struct unit_test_state *uts)
  20. {
  21. struct dm_test_state *dms = uts->priv;
  22. memset(dms, '\0', sizeof(*dms));
  23. gd->dm_root = NULL;
  24. memset(dm_testdrv_op_count, '\0', sizeof(dm_testdrv_op_count));
  25. ut_assertok(dm_init());
  26. dms->root = dm_root();
  27. return 0;
  28. }
  29. /* Ensure all the test devices are probed */
  30. static int do_autoprobe(struct unit_test_state *uts)
  31. {
  32. struct udevice *dev;
  33. int ret;
  34. /* Scanning the uclass is enough to probe all the devices */
  35. for (ret = uclass_first_device(UCLASS_TEST, &dev);
  36. dev;
  37. ret = uclass_next_device(&dev))
  38. ;
  39. return ret;
  40. }
  41. static int dm_test_destroy(struct unit_test_state *uts)
  42. {
  43. int id;
  44. for (id = 0; id < UCLASS_COUNT; id++) {
  45. struct uclass *uc;
  46. /*
  47. * If the uclass doesn't exist we don't want to create it. So
  48. * check that here before we call uclass_find_device()/
  49. */
  50. uc = uclass_find(id);
  51. if (!uc)
  52. continue;
  53. ut_assertok(uclass_destroy(uc));
  54. }
  55. return 0;
  56. }
  57. static int dm_test_main(const char *test_name)
  58. {
  59. struct unit_test *tests = ll_entry_start(struct unit_test, dm_test);
  60. const int n_ents = ll_entry_count(struct unit_test, dm_test);
  61. struct unit_test_state *uts = &global_dm_test_state;
  62. uts->priv = &_global_priv_dm_test_state;
  63. struct unit_test *test;
  64. int run_count;
  65. /*
  66. * If we have no device tree, or it only has a root node, then these
  67. * tests clearly aren't going to work...
  68. */
  69. if (!gd->fdt_blob || fdt_next_node(gd->fdt_blob, 0, NULL) < 0) {
  70. puts("Please run with test device tree:\n"
  71. " ./u-boot -d arch/sandbox/dts/test.dtb\n");
  72. ut_assert(gd->fdt_blob);
  73. }
  74. if (!test_name)
  75. printf("Running %d driver model tests\n", n_ents);
  76. run_count = 0;
  77. for (test = tests; test < tests + n_ents; test++) {
  78. const char *name = test->name;
  79. /* All tests have this prefix */
  80. if (!strncmp(name, "dm_test_", 8))
  81. name += 8;
  82. if (test_name && strcmp(test_name, name))
  83. continue;
  84. printf("Test: %s\n", test->name);
  85. run_count++;
  86. ut_assertok(dm_test_init(uts));
  87. uts->start = mallinfo();
  88. if (test->flags & DM_TESTF_SCAN_PDATA)
  89. ut_assertok(dm_scan_platdata(false));
  90. if (test->flags & DM_TESTF_PROBE_TEST)
  91. ut_assertok(do_autoprobe(uts));
  92. if (test->flags & DM_TESTF_SCAN_FDT)
  93. ut_assertok(dm_scan_fdt(gd->fdt_blob, false));
  94. test->func(uts);
  95. ut_assertok(dm_test_destroy(uts));
  96. }
  97. if (test_name && !run_count)
  98. printf("Test '%s' not found\n", test_name);
  99. else
  100. printf("Failures: %d\n", uts->fail_count);
  101. gd->dm_root = NULL;
  102. ut_assertok(dm_init());
  103. dm_scan_platdata(false);
  104. dm_scan_fdt(gd->fdt_blob, false);
  105. return uts->fail_count ? CMD_RET_FAILURE : 0;
  106. }
  107. int do_ut_dm(cmd_tbl_t *cmdtp, int flag, int argc, char * const argv[])
  108. {
  109. const char *test_name = NULL;
  110. if (argc > 1)
  111. test_name = argv[1];
  112. return dm_test_main(test_name);
  113. }