ut.c 2.9 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142
  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. int ut_check_console_line(struct unit_test_state *uts, const char *fmt, ...)
  46. {
  47. va_list args;
  48. va_start(args, fmt);
  49. vsnprintf(uts->expect_str, sizeof(uts->expect_str), fmt, args);
  50. va_end(args);
  51. console_record_readline(uts->actual_str, sizeof(uts->actual_str));
  52. return strcmp(uts->expect_str, uts->actual_str);
  53. }
  54. int ut_check_console_linen(struct unit_test_state *uts, const char *fmt, ...)
  55. {
  56. va_list args;
  57. va_start(args, fmt);
  58. vsnprintf(uts->expect_str, sizeof(uts->expect_str), fmt, args);
  59. va_end(args);
  60. console_record_readline(uts->actual_str, sizeof(uts->actual_str));
  61. return strncmp(uts->expect_str, uts->actual_str,
  62. strlen(uts->expect_str));
  63. }
  64. int ut_check_skipline(struct unit_test_state *uts)
  65. {
  66. if (!console_record_avail())
  67. return -ENFILE;
  68. console_record_readline(uts->actual_str, sizeof(uts->actual_str));
  69. return 0;
  70. }
  71. int ut_check_console_end(struct unit_test_state *uts)
  72. {
  73. if (!console_record_avail())
  74. return 0;
  75. console_record_readline(uts->actual_str, sizeof(uts->actual_str));
  76. return 1;
  77. }
  78. int ut_check_console_dump(struct unit_test_state *uts, int total_bytes)
  79. {
  80. char *str = uts->actual_str;
  81. int upto;
  82. /* Handle empty dump */
  83. if (!total_bytes)
  84. return 0;
  85. for (upto = 0; upto < total_bytes;) {
  86. int len;
  87. int bytes;
  88. len = console_record_readline(str, sizeof(uts->actual_str));
  89. if (str[8] != ':' || str[9] != ' ')
  90. return 1;
  91. bytes = len - 8 - 2 - 3 * 16 - 4;
  92. upto += bytes;
  93. }
  94. return upto == total_bytes ? 0 : 1;
  95. }
  96. void ut_silence_console(struct unit_test_state *uts)
  97. {
  98. #ifdef CONFIG_SANDBOX
  99. struct sandbox_state *state = state_get_current();
  100. if (!state->show_test_output)
  101. gd->flags |= GD_FLG_SILENT;
  102. #endif
  103. }
  104. void ut_unsilence_console(struct unit_test_state *uts)
  105. {
  106. gd->flags &= ~(GD_FLG_SILENT | GD_FLG_RECORD);
  107. }
  108. void ut_set_skip_delays(struct unit_test_state *uts, bool skip_delays)
  109. {
  110. #ifdef CONFIG_SANDBOX
  111. state_set_skip_delays(skip_delays);
  112. #endif
  113. }