ut.h 7.4 KB

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