cmd_ut.c 3.7 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153
  1. // SPDX-License-Identifier: GPL-2.0
  2. /*
  3. * (C) Copyright 2015
  4. * Joe Hershberger, National Instruments, joe.hershberger@ni.com
  5. */
  6. #include <common.h>
  7. #include <command.h>
  8. #include <test/suites.h>
  9. #include <test/test.h>
  10. static int do_ut_all(cmd_tbl_t *cmdtp, int flag, int argc, char * const argv[]);
  11. int cmd_ut_category(const char *name, const char *prefix,
  12. struct unit_test *tests, int n_ents,
  13. int argc, char * const argv[])
  14. {
  15. struct unit_test_state uts = { .fail_count = 0 };
  16. struct unit_test *test;
  17. int prefix_len = prefix ? strlen(prefix) : 0;
  18. if (argc == 1)
  19. printf("Running %d %s tests\n", n_ents, name);
  20. for (test = tests; test < tests + n_ents; test++) {
  21. const char *test_name = test->name;
  22. /* Remove the prefix */
  23. if (prefix && !strncmp(test_name, prefix, prefix_len))
  24. test_name += prefix_len;
  25. if (argc > 1 && strcmp(argv[1], test_name))
  26. continue;
  27. printf("Test: %s\n", test->name);
  28. uts.start = mallinfo();
  29. test->func(&uts);
  30. }
  31. printf("Failures: %d\n", uts.fail_count);
  32. return uts.fail_count ? CMD_RET_FAILURE : 0;
  33. }
  34. static cmd_tbl_t cmd_ut_sub[] = {
  35. U_BOOT_CMD_MKENT(all, CONFIG_SYS_MAXARGS, 1, do_ut_all, "", ""),
  36. #if defined(CONFIG_UT_DM)
  37. U_BOOT_CMD_MKENT(dm, CONFIG_SYS_MAXARGS, 1, do_ut_dm, "", ""),
  38. #endif
  39. #if defined(CONFIG_UT_ENV)
  40. U_BOOT_CMD_MKENT(env, CONFIG_SYS_MAXARGS, 1, do_ut_env, "", ""),
  41. #endif
  42. #ifdef CONFIG_UT_OPTEE
  43. U_BOOT_CMD_MKENT(optee, CONFIG_SYS_MAXARGS, 1, do_ut_optee, "", ""),
  44. #endif
  45. #ifdef CONFIG_UT_OVERLAY
  46. U_BOOT_CMD_MKENT(overlay, CONFIG_SYS_MAXARGS, 1, do_ut_overlay, "", ""),
  47. #endif
  48. #ifdef CONFIG_UT_LIB
  49. U_BOOT_CMD_MKENT(lib, CONFIG_SYS_MAXARGS, 1, do_ut_lib, "", ""),
  50. #endif
  51. #ifdef CONFIG_UT_LOG
  52. U_BOOT_CMD_MKENT(log, CONFIG_SYS_MAXARGS, 1, do_ut_log, "", ""),
  53. #endif
  54. #ifdef CONFIG_UT_TIME
  55. U_BOOT_CMD_MKENT(time, CONFIG_SYS_MAXARGS, 1, do_ut_time, "", ""),
  56. #endif
  57. #if CONFIG_IS_ENABLED(UT_UNICODE) && !defined(API_BUILD)
  58. U_BOOT_CMD_MKENT(unicode, CONFIG_SYS_MAXARGS, 1, do_ut_unicode, "", ""),
  59. #endif
  60. #ifdef CONFIG_SANDBOX
  61. U_BOOT_CMD_MKENT(compression, CONFIG_SYS_MAXARGS, 1, do_ut_compression,
  62. "", ""),
  63. U_BOOT_CMD_MKENT(bloblist, CONFIG_SYS_MAXARGS, 1, do_ut_bloblist,
  64. "", ""),
  65. #endif
  66. };
  67. static int do_ut_all(cmd_tbl_t *cmdtp, int flag, int argc, char * const argv[])
  68. {
  69. int i;
  70. int retval;
  71. int any_fail = 0;
  72. for (i = 1; i < ARRAY_SIZE(cmd_ut_sub); i++) {
  73. printf("----Running %s tests----\n", cmd_ut_sub[i].name);
  74. retval = cmd_ut_sub[i].cmd(cmdtp, flag, 1, &cmd_ut_sub[i].name);
  75. if (!any_fail)
  76. any_fail = retval;
  77. }
  78. return any_fail;
  79. }
  80. static int do_ut(cmd_tbl_t *cmdtp, int flag, int argc, char * const argv[])
  81. {
  82. cmd_tbl_t *cp;
  83. if (argc < 2)
  84. return CMD_RET_USAGE;
  85. /* drop initial "ut" arg */
  86. argc--;
  87. argv++;
  88. cp = find_cmd_tbl(argv[0], cmd_ut_sub, ARRAY_SIZE(cmd_ut_sub));
  89. if (cp)
  90. return cp->cmd(cmdtp, flag, argc, argv);
  91. return CMD_RET_USAGE;
  92. }
  93. #ifdef CONFIG_SYS_LONGHELP
  94. static char ut_help_text[] =
  95. "all - execute all enabled tests\n"
  96. #ifdef CONFIG_SANDBOX
  97. "ut bloblist - Test bloblist implementation\n"
  98. "ut compression - Test compressors and bootm decompression\n"
  99. #endif
  100. #ifdef CONFIG_UT_DM
  101. "ut dm [test-name]\n"
  102. #endif
  103. #ifdef CONFIG_UT_ENV
  104. "ut env [test-name]\n"
  105. #endif
  106. #ifdef CONFIG_UT_LIB
  107. "ut lib [test-name] - test library functions\n"
  108. #endif
  109. #ifdef CONFIG_UT_LOG
  110. "ut log [test-name] - test logging functions\n"
  111. #endif
  112. #ifdef CONFIG_UT_OPTEE
  113. "ut optee [test-name]\n"
  114. #endif
  115. #ifdef CONFIG_UT_OVERLAY
  116. "ut overlay [test-name]\n"
  117. #endif
  118. #ifdef CONFIG_UT_TIME
  119. "ut time - Very basic test of time functions\n"
  120. #endif
  121. #if defined(CONFIG_UT_UNICODE) && \
  122. !defined(CONFIG_SPL_BUILD) && !defined(API_BUILD)
  123. "ut unicode [test-name] - test Unicode functions\n"
  124. #endif
  125. ;
  126. #endif /* CONFIG_SYS_LONGHELP */
  127. U_BOOT_CMD(
  128. ut, CONFIG_SYS_MAXARGS, 1, do_ut,
  129. "unit tests", ut_help_text
  130. );