ut.h 14 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385386387388389390391392393394395396397398399400401402403404405406407408409410411412413414415416417418419420421422423424425426427428429430431432433434435436437438439440441442443444445446
  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 <command.h>
  10. #include <hexdump.h>
  11. #include <linux/err.h>
  12. #include <test/test.h>
  13. struct unit_test_state;
  14. /**
  15. * ut_fail() - Record failure of a unit test
  16. *
  17. * @uts: Test state
  18. * @fname: Filename where the error occurred
  19. * @line: Line number where the error occurred
  20. * @func: Function name where the error occurred
  21. * @cond: The condition that failed
  22. */
  23. void ut_fail(struct unit_test_state *uts, const char *fname, int line,
  24. const char *func, const char *cond);
  25. /**
  26. * ut_failf() - Record failure of a unit test
  27. *
  28. * @uts: Test state
  29. * @fname: Filename where the error occurred
  30. * @line: Line number where the error occurred
  31. * @func: Function name where the error occurred
  32. * @cond: The condition that failed
  33. * @fmt: printf() format string for the error, followed by args
  34. */
  35. void ut_failf(struct unit_test_state *uts, const char *fname, int line,
  36. const char *func, const char *cond, const char *fmt, ...)
  37. __attribute__ ((format (__printf__, 6, 7)));
  38. /**
  39. * ut_check_console_line() - Check the next console line against expectations
  40. *
  41. * This creates a string and then checks it against the next line of console
  42. * output obtained with console_record_readline().
  43. *
  44. * After the function returns, uts->expect_str holds the expected string and
  45. * uts->actual_str holds the actual string read from the console.
  46. *
  47. * @uts: Test state
  48. * @fmt: printf() format string for the error, followed by args
  49. * @return 0 if OK, other value on error
  50. */
  51. int ut_check_console_line(struct unit_test_state *uts, const char *fmt, ...)
  52. __attribute__ ((format (__printf__, 2, 3)));
  53. /**
  54. * ut_check_console_linen() - Check part of the next console line
  55. *
  56. * This creates a string and then checks it against the next line of console
  57. * output obtained with console_record_readline(). Only the length of the
  58. * string is checked
  59. *
  60. * After the function returns, uts->expect_str holds the expected string and
  61. * uts->actual_str holds the actual string read from the console.
  62. *
  63. * @uts: Test state
  64. * @fmt: printf() format string for the error, followed by args
  65. * @return 0 if OK, other value on error
  66. */
  67. int ut_check_console_linen(struct unit_test_state *uts, const char *fmt, ...)
  68. __attribute__ ((format (__printf__, 2, 3)));
  69. /**
  70. * ut_check_skipline() - Check that the next console line exists and skip it
  71. *
  72. * @uts: Test state
  73. * @return 0 if OK, other value on error
  74. */
  75. int ut_check_skipline(struct unit_test_state *uts);
  76. /**
  77. * ut_check_console_end() - Check there is no more console output
  78. *
  79. * After the function returns, uts->actual_str holds the actual string read
  80. * from the console
  81. *
  82. * @uts: Test state
  83. * @return 0 if OK (console has no output), other value on error
  84. */
  85. int ut_check_console_end(struct unit_test_state *uts);
  86. /**
  87. * ut_check_console_dump() - Check that next lines have a print_buffer() dump
  88. *
  89. * This only supports a byte dump.
  90. *
  91. * @total_bytes: Size of the expected dump in bytes`
  92. * @return 0 if OK (looks like a dump and the length matches), other value on
  93. * error
  94. */
  95. int ut_check_console_dump(struct unit_test_state *uts, int total_bytes);
  96. /* Assert that a condition is non-zero */
  97. #define ut_assert(cond) \
  98. if (!(cond)) { \
  99. ut_fail(uts, __FILE__, __LINE__, __func__, #cond); \
  100. return CMD_RET_FAILURE; \
  101. }
  102. /* Assert that a condition is non-zero, with printf() string */
  103. #define ut_assertf(cond, fmt, args...) \
  104. if (!(cond)) { \
  105. ut_failf(uts, __FILE__, __LINE__, __func__, #cond, \
  106. fmt, ##args); \
  107. return CMD_RET_FAILURE; \
  108. }
  109. /* Assert that two int expressions are equal */
  110. #define ut_asserteq(expr1, expr2) { \
  111. unsigned int _val1 = (expr1), _val2 = (expr2); \
  112. \
  113. if (_val1 != _val2) { \
  114. ut_failf(uts, __FILE__, __LINE__, __func__, \
  115. #expr1 " == " #expr2, \
  116. "Expected %#x (%d), got %#x (%d)", \
  117. _val1, _val1, _val2, _val2); \
  118. return CMD_RET_FAILURE; \
  119. } \
  120. }
  121. /* Assert that two 64 int expressions are equal */
  122. #define ut_asserteq_64(expr1, expr2) { \
  123. u64 _val1 = (expr1), _val2 = (expr2); \
  124. \
  125. if (_val1 != _val2) { \
  126. ut_failf(uts, __FILE__, __LINE__, __func__, \
  127. #expr1 " == " #expr2, \
  128. "Expected %#llx (%lld), got %#llx (%lld)", \
  129. (unsigned long long)_val1, \
  130. (unsigned long long)_val1, \
  131. (unsigned long long)_val2, \
  132. (unsigned long long)_val2); \
  133. return CMD_RET_FAILURE; \
  134. } \
  135. }
  136. /* Assert that two string expressions are equal */
  137. #define ut_asserteq_str(expr1, expr2) { \
  138. const char *_val1 = (expr1), *_val2 = (expr2); \
  139. \
  140. if (strcmp(_val1, _val2)) { \
  141. ut_failf(uts, __FILE__, __LINE__, __func__, \
  142. #expr1 " = " #expr2, \
  143. "Expected \"%s\", got \"%s\"", _val1, _val2); \
  144. return CMD_RET_FAILURE; \
  145. } \
  146. }
  147. /*
  148. * Assert that two string expressions are equal, up to length of the
  149. * first
  150. */
  151. #define ut_asserteq_strn(expr1, expr2) { \
  152. const char *_val1 = (expr1), *_val2 = (expr2); \
  153. int _len = strlen(_val1); \
  154. \
  155. if (memcmp(_val1, _val2, _len)) { \
  156. ut_failf(uts, __FILE__, __LINE__, __func__, \
  157. #expr1 " = " #expr2, \
  158. "Expected \"%.*s\", got \"%.*s\"", \
  159. _len, _val1, _len, _val2); \
  160. return CMD_RET_FAILURE; \
  161. } \
  162. }
  163. /*
  164. * Assert that two string expressions are equal, up to length of the
  165. * first
  166. */
  167. #define ut_asserteq_strn(expr1, expr2) { \
  168. const char *_val1 = (expr1), *_val2 = (expr2); \
  169. int _len = strlen(_val1); \
  170. \
  171. if (memcmp(_val1, _val2, _len)) { \
  172. ut_failf(uts, __FILE__, __LINE__, __func__, \
  173. #expr1 " = " #expr2, \
  174. "Expected \"%.*s\", got \"%.*s\"", \
  175. _len, _val1, _len, _val2); \
  176. return CMD_RET_FAILURE; \
  177. } \
  178. }
  179. /* Assert that two memory areas are equal */
  180. #define ut_asserteq_mem(expr1, expr2, len) { \
  181. const u8 *_val1 = (u8 *)(expr1), *_val2 = (u8 *)(expr2); \
  182. const uint __len = len; \
  183. \
  184. if (memcmp(_val1, _val2, __len)) { \
  185. char __buf1[64 + 1] = "\0"; \
  186. char __buf2[64 + 1] = "\0"; \
  187. bin2hex(__buf1, _val1, min(__len, (uint)32)); \
  188. bin2hex(__buf2, _val2, min(__len, (uint)32)); \
  189. ut_failf(uts, __FILE__, __LINE__, __func__, \
  190. #expr1 " = " #expr2, \
  191. "Expected \"%s\", got \"%s\"", \
  192. __buf1, __buf2); \
  193. return CMD_RET_FAILURE; \
  194. } \
  195. }
  196. /* Assert that two pointers are equal */
  197. #define ut_asserteq_ptr(expr1, expr2) { \
  198. const void *_val1 = (expr1), *_val2 = (expr2); \
  199. \
  200. if (_val1 != _val2) { \
  201. ut_failf(uts, __FILE__, __LINE__, __func__, \
  202. #expr1 " = " #expr2, \
  203. "Expected %p, got %p", _val1, _val2); \
  204. return CMD_RET_FAILURE; \
  205. } \
  206. }
  207. /* Assert that two addresses (converted from pointers) are equal */
  208. #define ut_asserteq_addr(expr1, expr2) { \
  209. ulong _val1 = map_to_sysmem(expr1); \
  210. ulong _val2 = map_to_sysmem(expr2); \
  211. \
  212. if (_val1 != _val2) { \
  213. ut_failf(uts, __FILE__, __LINE__, __func__, \
  214. #expr1 " = " #expr2, \
  215. "Expected %lx, got %lx", _val1, _val2); \
  216. return CMD_RET_FAILURE; \
  217. } \
  218. }
  219. /* Assert that a pointer is NULL */
  220. #define ut_assertnull(expr) { \
  221. const void *_val = (expr); \
  222. \
  223. if (_val) { \
  224. ut_failf(uts, __FILE__, __LINE__, __func__, \
  225. #expr " != NULL", \
  226. "Expected NULL, got %p", _val); \
  227. return CMD_RET_FAILURE; \
  228. } \
  229. }
  230. /* Assert that a pointer is not NULL */
  231. #define ut_assertnonnull(expr) { \
  232. const void *_val = (expr); \
  233. \
  234. if (!_val) { \
  235. ut_failf(uts, __FILE__, __LINE__, __func__, \
  236. #expr " = NULL", \
  237. "Expected non-null, got NULL"); \
  238. return CMD_RET_FAILURE; \
  239. } \
  240. }
  241. /* Assert that a pointer is not an error pointer */
  242. #define ut_assertok_ptr(expr) { \
  243. const void *_val = (expr); \
  244. \
  245. if (IS_ERR(_val)) { \
  246. ut_failf(uts, __FILE__, __LINE__, __func__, \
  247. #expr " = NULL", \
  248. "Expected pointer, got error %ld", \
  249. PTR_ERR(_val)); \
  250. return CMD_RET_FAILURE; \
  251. } \
  252. }
  253. /* Assert that an operation succeeds (returns 0) */
  254. #define ut_assertok(cond) ut_asserteq(0, cond)
  255. /* Assert that the next console output line matches */
  256. #define ut_assert_nextline(fmt, args...) \
  257. if (ut_check_console_line(uts, fmt, ##args)) { \
  258. ut_failf(uts, __FILE__, __LINE__, __func__, \
  259. "console", "\nExpected '%s',\n got '%s'", \
  260. uts->expect_str, uts->actual_str); \
  261. return CMD_RET_FAILURE; \
  262. } \
  263. /* Assert that the next console output line matches up to the length */
  264. #define ut_assert_nextlinen(fmt, args...) \
  265. if (ut_check_console_linen(uts, fmt, ##args)) { \
  266. ut_failf(uts, __FILE__, __LINE__, __func__, \
  267. "console", "\nExpected '%s',\n got '%s'", \
  268. uts->expect_str, uts->actual_str); \
  269. return CMD_RET_FAILURE; \
  270. } \
  271. /* Assert that there is a 'next' console output line, and skip it */
  272. #define ut_assert_skipline() \
  273. if (ut_check_skipline(uts)) { \
  274. ut_failf(uts, __FILE__, __LINE__, __func__, \
  275. "console", "\nExpected a line, got end"); \
  276. return CMD_RET_FAILURE; \
  277. } \
  278. /* Assert that there is no more console output */
  279. #define ut_assert_console_end() \
  280. if (ut_check_console_end(uts)) { \
  281. ut_failf(uts, __FILE__, __LINE__, __func__, \
  282. "console", "Expected no more output, got '%s'",\
  283. uts->actual_str); \
  284. return CMD_RET_FAILURE; \
  285. } \
  286. /* Assert that the next lines are print_buffer() dump at an address */
  287. #define ut_assert_nextlines_are_dump(total_bytes) \
  288. if (ut_check_console_dump(uts, total_bytes)) { \
  289. ut_failf(uts, __FILE__, __LINE__, __func__, \
  290. "console", \
  291. "Expected dump of length %x bytes, got '%s'", \
  292. total_bytes, uts->actual_str); \
  293. return CMD_RET_FAILURE; \
  294. } \
  295. /**
  296. * ut_check_free() - Return the number of bytes free in the malloc() pool
  297. *
  298. * @return bytes free
  299. */
  300. ulong ut_check_free(void);
  301. /**
  302. * ut_check_delta() - Return the number of bytes allocated/freed
  303. *
  304. * @last: Last value from ut_check_free
  305. * @return free memory delta from @last; positive means more memory has been
  306. * allocated, negative means less has been allocated (i.e. some is freed)
  307. */
  308. long ut_check_delta(ulong last);
  309. /**
  310. * ut_silence_console() - Silence the console if requested by the user
  311. *
  312. * This stops test output from appear on the console. It is the default on
  313. * sandbox, unless the -v flag is given. For other boards, this does nothing.
  314. *
  315. * @uts: Test state (in case in future we want to keep state here)
  316. */
  317. void ut_silence_console(struct unit_test_state *uts);
  318. /**
  319. * ut_unsilence_console() - Unsilence the console after a test
  320. *
  321. * This restarts console output again and turns off console recording. This
  322. * happens on all boards, including sandbox.
  323. */
  324. void ut_unsilence_console(struct unit_test_state *uts);
  325. /**
  326. * ut_set_skip_delays() - Sets whether delays should be skipped
  327. *
  328. * Normally functions like mdelay() cause U-Boot to wait for a while. This
  329. * allows all such delays to be skipped on sandbox, to speed up tests
  330. *
  331. * @uts: Test state (in case in future we want to keep state here)
  332. * @skip_delays: true to skip delays, false to process them normally
  333. */
  334. void ut_set_skip_delays(struct unit_test_state *uts, bool skip_delays);
  335. /**
  336. * test_get_state() - Get the active test state
  337. *
  338. * @return the currently active test state, or NULL if none
  339. */
  340. struct unit_test_state *test_get_state(void);
  341. /**
  342. * test_set_state() - Set the active test state
  343. *
  344. * @uts: Test state to use as currently active test state, or NULL if none
  345. */
  346. void test_set_state(struct unit_test_state *uts);
  347. /**
  348. * ut_run_test_live_flat() - Run a test with both live and flat tree
  349. *
  350. * This calls ut_run_test() with livetree enabled, which is the standard setup
  351. * for runnig tests. Then, for driver model test, it calls it again with
  352. * livetree disabled. This allows checking of flattree being used when OF_LIVE
  353. * is enabled, as is the case in U-Boot proper before relocation, as well as in
  354. * SPL.
  355. *
  356. * @uts: Test state to update. The caller should ensure that this is zeroed for
  357. * the first call to this function. On exit, @uts->fail_count is
  358. * incremented by the number of failures (0, one hopes)
  359. * @test: Test to run
  360. * @name: Name of test, possibly skipping a prefix that should not be displayed
  361. * @return 0 if all tests passed, -EAGAIN if the test should be skipped, -1 if
  362. * any failed
  363. */
  364. int ut_run_test_live_flat(struct unit_test_state *uts, struct unit_test *test,
  365. const char *name);
  366. /**
  367. * ut_run_tests() - Run a set of tests
  368. *
  369. * This runs the tests, handling any preparation and clean-up needed. It prints
  370. * the name of each test before running it.
  371. *
  372. * @uts: Test state to update. The caller should ensure that this is zeroed for
  373. * the first call to this function. On exit, @uts->fail_count is
  374. * incremented by the number of failures (0, one hopes)
  375. * @prefix: String prefix for the tests. Any tests that have this prefix will be
  376. * printed without the prefix, so that it is easier to see the unique part
  377. * of the test name. If NULL, no prefix processing is done
  378. * @tests: List of tests to run
  379. * @count: Number of tests to run
  380. * @select_name: Name of a single test to run (from the list provided). If NULL
  381. * then all tests are run
  382. * @return 0 if all tests passed, -ENOENT if test @select_name was not found,
  383. * -EBADF if any failed
  384. */
  385. int ut_run_tests(struct unit_test_state *uts, const char *prefix,
  386. struct unit_test *tests, int count, const char *select_name);
  387. /**
  388. * ut_run_tests() - Run a set of tests
  389. *
  390. * This runs the test, handling any preparation and clean-up needed. It prints
  391. * the name of each test before running it.
  392. *
  393. * @category: Category of these tests. This is a string printed at the start to
  394. * announce the the number of tests
  395. * @prefix: String prefix for the tests. Any tests that have this prefix will be
  396. * printed without the prefix, so that it is easier to see the unique part
  397. * of the test name. If NULL, no prefix processing is done
  398. * @tests: List of tests to run
  399. * @count: Number of tests to run
  400. * @select_name: Name of a single test to run (from the list provided). If NULL
  401. * then all tests are run
  402. * @return 0 if all tests passed, -1 if any failed
  403. */
  404. int ut_run_list(const char *name, const char *prefix, struct unit_test *tests,
  405. int count, const char *select_name);
  406. #endif