ut.c 3.6 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181
  1. // SPDX-License-Identifier: GPL-2.0+
  2. /*
  3. * Simple unit test library
  4. *
  5. * Copyright (c) 2013 Google, Inc
  6. */
  7. #include <common.h>
  8. #include <console.h>
  9. #include <malloc.h>
  10. #ifdef CONFIG_SANDBOX
  11. #include <asm/state.h>
  12. #endif
  13. #include <asm/global_data.h>
  14. #include <test/test.h>
  15. #include <test/ut.h>
  16. DECLARE_GLOBAL_DATA_PTR;
  17. void ut_fail(struct unit_test_state *uts, const char *fname, int line,
  18. const char *func, const char *cond)
  19. {
  20. gd->flags &= ~(GD_FLG_SILENT | GD_FLG_RECORD);
  21. printf("%s:%d, %s(): %s\n", fname, line, func, cond);
  22. uts->fail_count++;
  23. }
  24. void ut_failf(struct unit_test_state *uts, const char *fname, int line,
  25. const char *func, const char *cond, const char *fmt, ...)
  26. {
  27. va_list args;
  28. gd->flags &= ~(GD_FLG_SILENT | GD_FLG_RECORD);
  29. printf("%s:%d, %s(): %s: ", fname, line, func, cond);
  30. va_start(args, fmt);
  31. vprintf(fmt, args);
  32. va_end(args);
  33. putc('\n');
  34. uts->fail_count++;
  35. }
  36. ulong ut_check_free(void)
  37. {
  38. struct mallinfo info = mallinfo();
  39. return info.uordblks;
  40. }
  41. long ut_check_delta(ulong last)
  42. {
  43. return ut_check_free() - last;
  44. }
  45. static int readline_check(struct unit_test_state *uts)
  46. {
  47. int ret;
  48. ret = console_record_readline(uts->actual_str, sizeof(uts->actual_str));
  49. if (ret == -ENOSPC) {
  50. ut_fail(uts, __FILE__, __LINE__, __func__,
  51. "Console record buffer too small - increase CONFIG_CONSOLE_RECORD_OUT_SIZE");
  52. return ret;
  53. }
  54. return 0;
  55. }
  56. int ut_check_console_line(struct unit_test_state *uts, const char *fmt, ...)
  57. {
  58. va_list args;
  59. int len;
  60. int ret;
  61. va_start(args, fmt);
  62. len = vsnprintf(uts->expect_str, sizeof(uts->expect_str), fmt, args);
  63. va_end(args);
  64. if (len >= sizeof(uts->expect_str)) {
  65. ut_fail(uts, __FILE__, __LINE__, __func__,
  66. "unit_test_state->expect_str too small");
  67. return -EOVERFLOW;
  68. }
  69. ret = readline_check(uts);
  70. if (ret < 0)
  71. return ret;
  72. return strcmp(uts->expect_str, uts->actual_str);
  73. }
  74. int ut_check_console_linen(struct unit_test_state *uts, const char *fmt, ...)
  75. {
  76. va_list args;
  77. int len;
  78. int ret;
  79. va_start(args, fmt);
  80. len = vsnprintf(uts->expect_str, sizeof(uts->expect_str), fmt, args);
  81. va_end(args);
  82. if (len >= sizeof(uts->expect_str)) {
  83. ut_fail(uts, __FILE__, __LINE__, __func__,
  84. "unit_test_state->expect_str too small");
  85. return -EOVERFLOW;
  86. }
  87. ret = readline_check(uts);
  88. if (ret < 0)
  89. return ret;
  90. return strncmp(uts->expect_str, uts->actual_str,
  91. strlen(uts->expect_str));
  92. }
  93. int ut_check_skipline(struct unit_test_state *uts)
  94. {
  95. int ret;
  96. if (!console_record_avail())
  97. return -ENFILE;
  98. ret = readline_check(uts);
  99. if (ret < 0)
  100. return ret;
  101. return 0;
  102. }
  103. int ut_check_console_end(struct unit_test_state *uts)
  104. {
  105. int ret;
  106. if (!console_record_avail())
  107. return 0;
  108. ret = readline_check(uts);
  109. if (ret < 0)
  110. return ret;
  111. return 1;
  112. }
  113. int ut_check_console_dump(struct unit_test_state *uts, int total_bytes)
  114. {
  115. char *str = uts->actual_str;
  116. int upto;
  117. /* Handle empty dump */
  118. if (!total_bytes)
  119. return 0;
  120. for (upto = 0; upto < total_bytes;) {
  121. int len;
  122. int bytes;
  123. len = console_record_readline(str, sizeof(uts->actual_str));
  124. if (str[8] != ':' || str[9] != ' ')
  125. return 1;
  126. bytes = len - 8 - 2 - 3 * 16 - 2;
  127. upto += bytes;
  128. }
  129. return upto == total_bytes ? 0 : 1;
  130. }
  131. void ut_silence_console(struct unit_test_state *uts)
  132. {
  133. #ifdef CONFIG_SANDBOX
  134. struct sandbox_state *state = state_get_current();
  135. if (!state->show_test_output)
  136. gd->flags |= GD_FLG_SILENT;
  137. #endif
  138. }
  139. void ut_unsilence_console(struct unit_test_state *uts)
  140. {
  141. gd->flags &= ~(GD_FLG_SILENT | GD_FLG_RECORD);
  142. }
  143. void ut_set_skip_delays(struct unit_test_state *uts, bool skip_delays)
  144. {
  145. #ifdef CONFIG_SANDBOX
  146. state_set_skip_delays(skip_delays);
  147. #endif
  148. }