ut.h 6.9 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233
  1. /* SPDX-License-Identifier: GPL-2.0+ */
  2. /*
  3. * Simple unit test library
  4. *
  5. * Copyright (c) 2013 Google, Inc
  6. */
  7. #ifndef __TEST_UT_H
  8. #define __TEST_UT_H
  9. #include <linux/err.h>
  10. struct unit_test_state;
  11. /**
  12. * ut_fail() - Record failure of a unit test
  13. *
  14. * @uts: Test state
  15. * @fname: Filename where the error occurred
  16. * @line: Line number where the error occurred
  17. * @func: Function name where the error occurred
  18. * @cond: The condition that failed
  19. */
  20. void ut_fail(struct unit_test_state *uts, const char *fname, int line,
  21. const char *func, const char *cond);
  22. /**
  23. * ut_failf() - Record failure of a unit test
  24. *
  25. * @uts: Test state
  26. * @fname: Filename where the error occurred
  27. * @line: Line number where the error occurred
  28. * @func: Function name where the error occurred
  29. * @cond: The condition that failed
  30. * @fmt: printf() format string for the error, followed by args
  31. */
  32. void ut_failf(struct unit_test_state *uts, const char *fname, int line,
  33. const char *func, const char *cond, const char *fmt, ...)
  34. __attribute__ ((format (__printf__, 6, 7)));
  35. /**
  36. * ut_check_console_line() - Check the next console line against expectations
  37. *
  38. * This creates a string and then checks it against the next line of console
  39. * output obtained with console_record_readline().
  40. *
  41. * After the function returns, uts->expect_str holds the expected string and
  42. * uts->actual_str holds the actual string read from the console.
  43. *
  44. * @uts: Test state
  45. * @fmt: printf() format string for the error, followed by args
  46. * @return 0 if OK, other value on error
  47. */
  48. int ut_check_console_line(struct unit_test_state *uts, const char *fmt, ...)
  49. __attribute__ ((format (__printf__, 2, 3)));
  50. /**
  51. * ut_check_console_end() - Check there is no more console output
  52. *
  53. * After the function returns, uts->actual_str holds the actual string read
  54. * from the console
  55. *
  56. * @uts: Test state
  57. * @return 0 if OK (console has no output), other value on error
  58. */
  59. int ut_check_console_end(struct unit_test_state *uts);
  60. /**
  61. * ut_check_console_dump() - Check that next lines have a print_buffer() dump
  62. *
  63. * This only supports a byte dump.
  64. *
  65. * @total_bytes: Size of the expected dump in bytes`
  66. * @return 0 if OK (looks like a dump and the length matches), other value on
  67. * error
  68. */
  69. int ut_check_console_dump(struct unit_test_state *uts, int total_bytes);
  70. /* Assert that a condition is non-zero */
  71. #define ut_assert(cond) \
  72. if (!(cond)) { \
  73. ut_fail(uts, __FILE__, __LINE__, __func__, #cond); \
  74. return CMD_RET_FAILURE; \
  75. }
  76. /* Assert that a condition is non-zero, with printf() string */
  77. #define ut_assertf(cond, fmt, args...) \
  78. if (!(cond)) { \
  79. ut_failf(uts, __FILE__, __LINE__, __func__, #cond, \
  80. fmt, ##args); \
  81. return CMD_RET_FAILURE; \
  82. }
  83. /* Assert that two int expressions are equal */
  84. #define ut_asserteq(expr1, expr2) { \
  85. unsigned int _val1 = (expr1), _val2 = (expr2); \
  86. \
  87. if (_val1 != _val2) { \
  88. ut_failf(uts, __FILE__, __LINE__, __func__, \
  89. #expr1 " == " #expr2, \
  90. "Expected %#x (%d), got %#x (%d)", \
  91. _val1, _val1, _val2, _val2); \
  92. return CMD_RET_FAILURE; \
  93. } \
  94. }
  95. /* Assert that two string expressions are equal */
  96. #define ut_asserteq_str(expr1, expr2) { \
  97. const char *_val1 = (expr1), *_val2 = (expr2); \
  98. \
  99. if (strcmp(_val1, _val2)) { \
  100. ut_failf(uts, __FILE__, __LINE__, __func__, \
  101. #expr1 " = " #expr2, \
  102. "Expected \"%s\", got \"%s\"", _val1, _val2); \
  103. return CMD_RET_FAILURE; \
  104. } \
  105. }
  106. /* Assert that two memory areas are equal */
  107. #define ut_asserteq_mem(expr1, expr2, len) { \
  108. const u8 *_val1 = (u8 *)(expr1), *_val2 = (u8 *)(expr2); \
  109. const uint __len = len; \
  110. \
  111. if (memcmp(_val1, _val2, __len)) { \
  112. char __buf1[64 + 1] = "\0"; \
  113. char __buf2[64 + 1] = "\0"; \
  114. bin2hex(__buf1, _val1, min(__len, (uint)32)); \
  115. bin2hex(__buf2, _val2, min(__len, (uint)32)); \
  116. ut_failf(uts, __FILE__, __LINE__, __func__, \
  117. #expr1 " = " #expr2, \
  118. "Expected \"%s\", got \"%s\"", \
  119. __buf1, __buf2); \
  120. return CMD_RET_FAILURE; \
  121. } \
  122. }
  123. /* Assert that two pointers are equal */
  124. #define ut_asserteq_ptr(expr1, expr2) { \
  125. const void *_val1 = (expr1), *_val2 = (expr2); \
  126. \
  127. if (_val1 != _val2) { \
  128. ut_failf(uts, __FILE__, __LINE__, __func__, \
  129. #expr1 " = " #expr2, \
  130. "Expected %p, got %p", _val1, _val2); \
  131. return CMD_RET_FAILURE; \
  132. } \
  133. }
  134. /* Assert that a pointer is NULL */
  135. #define ut_assertnull(expr) { \
  136. const void *_val = (expr); \
  137. \
  138. if (_val) { \
  139. ut_failf(uts, __FILE__, __LINE__, __func__, \
  140. #expr " != NULL", \
  141. "Expected NULL, got %p", _val); \
  142. return CMD_RET_FAILURE; \
  143. } \
  144. }
  145. /* Assert that a pointer is not NULL */
  146. #define ut_assertnonnull(expr) { \
  147. const void *_val = (expr); \
  148. \
  149. if (!_val) { \
  150. ut_failf(uts, __FILE__, __LINE__, __func__, \
  151. #expr " = NULL", \
  152. "Expected non-null, got NULL"); \
  153. return CMD_RET_FAILURE; \
  154. } \
  155. }
  156. /* Assert that a pointer is not an error pointer */
  157. #define ut_assertok_ptr(expr) { \
  158. const void *_val = (expr); \
  159. \
  160. if (IS_ERR(_val)) { \
  161. ut_failf(uts, __FILE__, __LINE__, __func__, \
  162. #expr " = NULL", \
  163. "Expected pointer, got error %ld", \
  164. PTR_ERR(_val)); \
  165. return CMD_RET_FAILURE; \
  166. } \
  167. }
  168. /* Assert that an operation succeeds (returns 0) */
  169. #define ut_assertok(cond) ut_asserteq(0, cond)
  170. /* Assert that the next console output line matches */
  171. #define ut_assert_nextline(fmt, args...) \
  172. if (ut_check_console_line(uts, fmt, ##args)) { \
  173. ut_failf(uts, __FILE__, __LINE__, __func__, \
  174. "console", "\nExpected '%s',\n got '%s'", \
  175. uts->expect_str, uts->actual_str); \
  176. return CMD_RET_FAILURE; \
  177. } \
  178. /* Assert that there is no more console output */
  179. #define ut_assert_console_end() \
  180. if (ut_check_console_end(uts)) { \
  181. ut_failf(uts, __FILE__, __LINE__, __func__, \
  182. "console", "Expected no more output, got '%s'",\
  183. uts->actual_str); \
  184. return CMD_RET_FAILURE; \
  185. } \
  186. /* Assert that the next lines are print_buffer() dump at an address */
  187. #define ut_assert_nextlines_are_dump(total_bytes) \
  188. if (ut_check_console_dump(uts, total_bytes)) { \
  189. ut_failf(uts, __FILE__, __LINE__, __func__, \
  190. "console", \
  191. "Expected dump of length %x bytes, got '%s'", \
  192. total_bytes, uts->actual_str); \
  193. return CMD_RET_FAILURE; \
  194. } \
  195. /**
  196. * ut_check_free() - Return the number of bytes free in the malloc() pool
  197. *
  198. * @return bytes free
  199. */
  200. ulong ut_check_free(void);
  201. /**
  202. * ut_check_delta() - Return the number of bytes allocated/freed
  203. *
  204. * @last: Last value from ut_check_free
  205. * @return free memory delta from @last; positive means more memory has been
  206. * allocated, negative means less has been allocated (i.e. some is freed)
  207. */
  208. long ut_check_delta(ulong last);
  209. #endif