blob: 656e25fe57406dd97e811863679a9217f28abfe5 [file] [log] [blame]
Tom Rini83d290c2018-05-06 17:58:06 -04001/* SPDX-License-Identifier: GPL-2.0+ */
Simon Glass2e7d35d2014-02-26 15:59:21 -07002/*
Joe Hershbergere721b882015-05-20 14:27:27 -05003 * Simple unit test library
Simon Glass2e7d35d2014-02-26 15:59:21 -07004 *
5 * Copyright (c) 2013 Google, Inc
Simon Glass2e7d35d2014-02-26 15:59:21 -07006 */
7
Joe Hershbergere721b882015-05-20 14:27:27 -05008#ifndef __TEST_UT_H
9#define __TEST_UT_H
Simon Glass2e7d35d2014-02-26 15:59:21 -070010
Simon Glass09140112020-05-10 11:40:03 -060011#include <command.h>
Simon Glass1f4431e2020-04-08 16:57:40 -060012#include <hexdump.h>
Simon Glass85aeda42015-07-06 12:54:37 -060013#include <linux/err.h>
Simon Glass0e1fad42020-07-19 10:15:37 -060014#include <test/test.h>
Simon Glass85aeda42015-07-06 12:54:37 -060015
Joe Hershbergere721b882015-05-20 14:27:27 -050016struct unit_test_state;
Simon Glass2e7d35d2014-02-26 15:59:21 -070017
18/**
19 * ut_fail() - Record failure of a unit test
20 *
Joe Hershbergere721b882015-05-20 14:27:27 -050021 * @uts: Test state
Vagrant Cascadianeae4b2b2016-04-30 19:18:00 -070022 * @fname: Filename where the error occurred
23 * @line: Line number where the error occurred
24 * @func: Function name where the error occurred
Simon Glass2e7d35d2014-02-26 15:59:21 -070025 * @cond: The condition that failed
26 */
Joe Hershbergere721b882015-05-20 14:27:27 -050027void ut_fail(struct unit_test_state *uts, const char *fname, int line,
Simon Glass2e7d35d2014-02-26 15:59:21 -070028 const char *func, const char *cond);
29
30/**
31 * ut_failf() - Record failure of a unit test
32 *
Joe Hershbergere721b882015-05-20 14:27:27 -050033 * @uts: Test state
Vagrant Cascadianeae4b2b2016-04-30 19:18:00 -070034 * @fname: Filename where the error occurred
35 * @line: Line number where the error occurred
36 * @func: Function name where the error occurred
Simon Glass2e7d35d2014-02-26 15:59:21 -070037 * @cond: The condition that failed
38 * @fmt: printf() format string for the error, followed by args
39 */
Joe Hershbergere721b882015-05-20 14:27:27 -050040void ut_failf(struct unit_test_state *uts, const char *fname, int line,
Simon Glass2e7d35d2014-02-26 15:59:21 -070041 const char *func, const char *cond, const char *fmt, ...)
42 __attribute__ ((format (__printf__, 6, 7)));
43
Simon Glass400175b2020-01-27 08:49:56 -070044/**
45 * ut_check_console_line() - Check the next console line against expectations
46 *
47 * This creates a string and then checks it against the next line of console
48 * output obtained with console_record_readline().
49 *
50 * After the function returns, uts->expect_str holds the expected string and
51 * uts->actual_str holds the actual string read from the console.
52 *
53 * @uts: Test state
54 * @fmt: printf() format string for the error, followed by args
55 * @return 0 if OK, other value on error
56 */
57int ut_check_console_line(struct unit_test_state *uts, const char *fmt, ...)
58 __attribute__ ((format (__printf__, 2, 3)));
59
60/**
Simon Glass33d7edf2020-07-28 19:41:10 -060061 * ut_check_console_linen() - Check part of the next console line
62 *
63 * This creates a string and then checks it against the next line of console
64 * output obtained with console_record_readline(). Only the length of the
65 * string is checked
66 *
67 * After the function returns, uts->expect_str holds the expected string and
68 * uts->actual_str holds the actual string read from the console.
69 *
70 * @uts: Test state
71 * @fmt: printf() format string for the error, followed by args
72 * @return 0 if OK, other value on error
73 */
74int ut_check_console_linen(struct unit_test_state *uts, const char *fmt, ...)
75 __attribute__ ((format (__printf__, 2, 3)));
76
77/**
78 * ut_check_skipline() - Check that the next console line exists and skip it
79 *
80 * @uts: Test state
81 * @return 0 if OK, other value on error
82 */
83int ut_check_skipline(struct unit_test_state *uts);
84
85/**
Simon Glass400175b2020-01-27 08:49:56 -070086 * ut_check_console_end() - Check there is no more console output
87 *
88 * After the function returns, uts->actual_str holds the actual string read
89 * from the console
90 *
91 * @uts: Test state
92 * @return 0 if OK (console has no output), other value on error
93 */
94int ut_check_console_end(struct unit_test_state *uts);
95
96/**
97 * ut_check_console_dump() - Check that next lines have a print_buffer() dump
98 *
99 * This only supports a byte dump.
100 *
101 * @total_bytes: Size of the expected dump in bytes`
102 * @return 0 if OK (looks like a dump and the length matches), other value on
103 * error
104 */
105int ut_check_console_dump(struct unit_test_state *uts, int total_bytes);
Simon Glass2e7d35d2014-02-26 15:59:21 -0700106
107/* Assert that a condition is non-zero */
108#define ut_assert(cond) \
109 if (!(cond)) { \
Joe Hershbergere721b882015-05-20 14:27:27 -0500110 ut_fail(uts, __FILE__, __LINE__, __func__, #cond); \
Joe Hershbergerfe3f6a62015-05-20 14:27:34 -0500111 return CMD_RET_FAILURE; \
Simon Glass2e7d35d2014-02-26 15:59:21 -0700112 }
113
114/* Assert that a condition is non-zero, with printf() string */
115#define ut_assertf(cond, fmt, args...) \
116 if (!(cond)) { \
Joe Hershbergere721b882015-05-20 14:27:27 -0500117 ut_failf(uts, __FILE__, __LINE__, __func__, #cond, \
Simon Glass2e7d35d2014-02-26 15:59:21 -0700118 fmt, ##args); \
Joe Hershbergerfe3f6a62015-05-20 14:27:34 -0500119 return CMD_RET_FAILURE; \
Simon Glass2e7d35d2014-02-26 15:59:21 -0700120 }
121
122/* Assert that two int expressions are equal */
123#define ut_asserteq(expr1, expr2) { \
Simon Glassba8444a2020-01-27 08:49:41 -0700124 unsigned int _val1 = (expr1), _val2 = (expr2); \
Simon Glass2e7d35d2014-02-26 15:59:21 -0700125 \
Simon Glassba8444a2020-01-27 08:49:41 -0700126 if (_val1 != _val2) { \
Joe Hershbergere721b882015-05-20 14:27:27 -0500127 ut_failf(uts, __FILE__, __LINE__, __func__, \
Simon Glass2e7d35d2014-02-26 15:59:21 -0700128 #expr1 " == " #expr2, \
Simon Glassba8444a2020-01-27 08:49:41 -0700129 "Expected %#x (%d), got %#x (%d)", \
130 _val1, _val1, _val2, _val2); \
Joe Hershbergerfe3f6a62015-05-20 14:27:34 -0500131 return CMD_RET_FAILURE; \
Simon Glass2e7d35d2014-02-26 15:59:21 -0700132 } \
133}
134
Dario Binacchi70573c62020-03-29 18:04:40 +0200135/* Assert that two 64 int expressions are equal */
136#define ut_asserteq_64(expr1, expr2) { \
137 u64 _val1 = (expr1), _val2 = (expr2); \
138 \
139 if (_val1 != _val2) { \
140 ut_failf(uts, __FILE__, __LINE__, __func__, \
141 #expr1 " == " #expr2, \
142 "Expected %#llx (%lld), got %#llx (%lld)", \
143 (unsigned long long)_val1, \
144 (unsigned long long)_val1, \
145 (unsigned long long)_val2, \
146 (unsigned long long)_val2); \
147 return CMD_RET_FAILURE; \
148 } \
149}
150
Simon Glass2e7d35d2014-02-26 15:59:21 -0700151/* Assert that two string expressions are equal */
152#define ut_asserteq_str(expr1, expr2) { \
Simon Glassba8444a2020-01-27 08:49:41 -0700153 const char *_val1 = (expr1), *_val2 = (expr2); \
Simon Glass2e7d35d2014-02-26 15:59:21 -0700154 \
Simon Glassba8444a2020-01-27 08:49:41 -0700155 if (strcmp(_val1, _val2)) { \
Joe Hershbergere721b882015-05-20 14:27:27 -0500156 ut_failf(uts, __FILE__, __LINE__, __func__, \
Simon Glass2e7d35d2014-02-26 15:59:21 -0700157 #expr1 " = " #expr2, \
Simon Glassba8444a2020-01-27 08:49:41 -0700158 "Expected \"%s\", got \"%s\"", _val1, _val2); \
Joe Hershbergerfe3f6a62015-05-20 14:27:34 -0500159 return CMD_RET_FAILURE; \
Simon Glass2e7d35d2014-02-26 15:59:21 -0700160 } \
161}
162
Simon Glass7aed90d2020-07-07 13:11:54 -0600163/*
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
Mario Six41f67e32018-09-27 09:19:32 +0200180/* Assert that two memory areas are equal */
181#define ut_asserteq_mem(expr1, expr2, len) { \
Simon Glassba8444a2020-01-27 08:49:41 -0700182 const u8 *_val1 = (u8 *)(expr1), *_val2 = (u8 *)(expr2); \
Mario Six41f67e32018-09-27 09:19:32 +0200183 const uint __len = len; \
184 \
Simon Glassba8444a2020-01-27 08:49:41 -0700185 if (memcmp(_val1, _val2, __len)) { \
Mario Six41f67e32018-09-27 09:19:32 +0200186 char __buf1[64 + 1] = "\0"; \
187 char __buf2[64 + 1] = "\0"; \
Simon Glassba8444a2020-01-27 08:49:41 -0700188 bin2hex(__buf1, _val1, min(__len, (uint)32)); \
189 bin2hex(__buf2, _val2, min(__len, (uint)32)); \
Mario Six41f67e32018-09-27 09:19:32 +0200190 ut_failf(uts, __FILE__, __LINE__, __func__, \
191 #expr1 " = " #expr2, \
192 "Expected \"%s\", got \"%s\"", \
193 __buf1, __buf2); \
194 return CMD_RET_FAILURE; \
195 } \
196}
197
Simon Glass2e7d35d2014-02-26 15:59:21 -0700198/* Assert that two pointers are equal */
199#define ut_asserteq_ptr(expr1, expr2) { \
Simon Glassba8444a2020-01-27 08:49:41 -0700200 const void *_val1 = (expr1), *_val2 = (expr2); \
Simon Glass2e7d35d2014-02-26 15:59:21 -0700201 \
Simon Glassba8444a2020-01-27 08:49:41 -0700202 if (_val1 != _val2) { \
Joe Hershbergere721b882015-05-20 14:27:27 -0500203 ut_failf(uts, __FILE__, __LINE__, __func__, \
Simon Glass2e7d35d2014-02-26 15:59:21 -0700204 #expr1 " = " #expr2, \
Simon Glassba8444a2020-01-27 08:49:41 -0700205 "Expected %p, got %p", _val1, _val2); \
Joe Hershbergerfe3f6a62015-05-20 14:27:34 -0500206 return CMD_RET_FAILURE; \
Simon Glass2e7d35d2014-02-26 15:59:21 -0700207 } \
208}
209
Simon Glasscdd4e302020-09-19 18:49:27 -0600210/* Assert that two addresses (converted from pointers) are equal */
211#define ut_asserteq_addr(expr1, expr2) { \
212 ulong _val1 = map_to_sysmem(expr1); \
213 ulong _val2 = map_to_sysmem(expr2); \
214 \
215 if (_val1 != _val2) { \
216 ut_failf(uts, __FILE__, __LINE__, __func__, \
217 #expr1 " = " #expr2, \
218 "Expected %lx, got %lx", _val1, _val2); \
219 return CMD_RET_FAILURE; \
220 } \
221}
222
Ramon Fried8d545792018-06-21 17:47:16 +0300223/* Assert that a pointer is NULL */
224#define ut_assertnull(expr) { \
Simon Glassba8444a2020-01-27 08:49:41 -0700225 const void *_val = (expr); \
Ramon Fried8d545792018-06-21 17:47:16 +0300226 \
Simon Glassba8444a2020-01-27 08:49:41 -0700227 if (_val) { \
Ramon Fried8d545792018-06-21 17:47:16 +0300228 ut_failf(uts, __FILE__, __LINE__, __func__, \
229 #expr " != NULL", \
Simon Glassba8444a2020-01-27 08:49:41 -0700230 "Expected NULL, got %p", _val); \
Ramon Fried8d545792018-06-21 17:47:16 +0300231 return CMD_RET_FAILURE; \
232 } \
233}
234
Simon Glassecc2ed52014-12-10 08:55:55 -0700235/* Assert that a pointer is not NULL */
236#define ut_assertnonnull(expr) { \
Simon Glassba8444a2020-01-27 08:49:41 -0700237 const void *_val = (expr); \
Simon Glassecc2ed52014-12-10 08:55:55 -0700238 \
Simon Glassba8444a2020-01-27 08:49:41 -0700239 if (!_val) { \
Joe Hershbergere721b882015-05-20 14:27:27 -0500240 ut_failf(uts, __FILE__, __LINE__, __func__, \
Simon Glassecc2ed52014-12-10 08:55:55 -0700241 #expr " = NULL", \
242 "Expected non-null, got NULL"); \
Joe Hershbergerfe3f6a62015-05-20 14:27:34 -0500243 return CMD_RET_FAILURE; \
Simon Glassecc2ed52014-12-10 08:55:55 -0700244 } \
245}
246
Simon Glass85aeda42015-07-06 12:54:37 -0600247/* Assert that a pointer is not an error pointer */
Simon Glassfe3c0b52017-05-18 20:10:00 -0600248#define ut_assertok_ptr(expr) { \
Simon Glassba8444a2020-01-27 08:49:41 -0700249 const void *_val = (expr); \
Simon Glass85aeda42015-07-06 12:54:37 -0600250 \
Simon Glassba8444a2020-01-27 08:49:41 -0700251 if (IS_ERR(_val)) { \
Simon Glass85aeda42015-07-06 12:54:37 -0600252 ut_failf(uts, __FILE__, __LINE__, __func__, \
253 #expr " = NULL", \
254 "Expected pointer, got error %ld", \
Simon Glassba8444a2020-01-27 08:49:41 -0700255 PTR_ERR(_val)); \
Simon Glass85aeda42015-07-06 12:54:37 -0600256 return CMD_RET_FAILURE; \
257 } \
258}
259
Simon Glass2e7d35d2014-02-26 15:59:21 -0700260/* Assert that an operation succeeds (returns 0) */
261#define ut_assertok(cond) ut_asserteq(0, cond)
262
Simon Glass400175b2020-01-27 08:49:56 -0700263/* Assert that the next console output line matches */
264#define ut_assert_nextline(fmt, args...) \
265 if (ut_check_console_line(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
Simon Glass33d7edf2020-07-28 19:41:10 -0600272/* Assert that the next console output line matches up to the length */
273#define ut_assert_nextlinen(fmt, args...) \
274 if (ut_check_console_linen(uts, fmt, ##args)) { \
275 ut_failf(uts, __FILE__, __LINE__, __func__, \
276 "console", "\nExpected '%s',\n got '%s'", \
277 uts->expect_str, uts->actual_str); \
278 return CMD_RET_FAILURE; \
279 } \
280
281/* Assert that there is a 'next' console output line, and skip it */
282#define ut_assert_skipline() \
283 if (ut_check_skipline(uts)) { \
284 ut_failf(uts, __FILE__, __LINE__, __func__, \
285 "console", "\nExpected a line, got end"); \
286 return CMD_RET_FAILURE; \
287 } \
288
Simon Glass400175b2020-01-27 08:49:56 -0700289/* Assert that there is no more console output */
290#define ut_assert_console_end() \
291 if (ut_check_console_end(uts)) { \
292 ut_failf(uts, __FILE__, __LINE__, __func__, \
293 "console", "Expected no more output, got '%s'",\
294 uts->actual_str); \
295 return CMD_RET_FAILURE; \
296 } \
297
298/* Assert that the next lines are print_buffer() dump at an address */
299#define ut_assert_nextlines_are_dump(total_bytes) \
300 if (ut_check_console_dump(uts, total_bytes)) { \
301 ut_failf(uts, __FILE__, __LINE__, __func__, \
302 "console", \
303 "Expected dump of length %x bytes, got '%s'", \
304 total_bytes, uts->actual_str); \
305 return CMD_RET_FAILURE; \
306 } \
307
Simon Glass81098632019-12-29 21:19:23 -0700308/**
309 * ut_check_free() - Return the number of bytes free in the malloc() pool
310 *
311 * @return bytes free
312 */
313ulong ut_check_free(void);
314
315/**
316 * ut_check_delta() - Return the number of bytes allocated/freed
317 *
318 * @last: Last value from ut_check_free
319 * @return free memory delta from @last; positive means more memory has been
320 * allocated, negative means less has been allocated (i.e. some is freed)
321 */
322long ut_check_delta(ulong last);
323
Simon Glassef7e2642020-11-08 21:08:43 -0700324/**
325 * ut_silence_console() - Silence the console if requested by the user
326 *
327 * This stops test output from appear on the console. It is the default on
328 * sandbox, unless the -v flag is given. For other boards, this does nothing.
329 *
330 * @uts: Test state (in case in future we want to keep state here)
331 */
332void ut_silence_console(struct unit_test_state *uts);
333
334/**
335 * ut_unsilence_console() - Unsilence the console after a test
336 *
337 * This restarts console output again and turns off console recording. This
338 * happens on all boards, including sandbox.
339 */
340void ut_unsilence_console(struct unit_test_state *uts);
341
Simon Glass1c721752021-03-07 17:34:47 -0700342/**
Simon Glass47ec3ed2021-03-07 17:34:55 -0700343 * ut_set_skip_delays() - Sets whether delays should be skipped
344 *
345 * Normally functions like mdelay() cause U-Boot to wait for a while. This
346 * allows all such delays to be skipped on sandbox, to speed up tests
347 *
348 * @uts: Test state (in case in future we want to keep state here)
349 * @skip_delays: true to skip delays, false to process them normally
350 */
351void ut_set_skip_delays(struct unit_test_state *uts, bool skip_delays);
352
353/**
Simon Glassfe806862021-03-07 17:35:04 -0700354 * test_get_state() - Get the active test state
355 *
356 * @return the currently active test state, or NULL if none
357 */
358struct unit_test_state *test_get_state(void);
359
360/**
361 * test_set_state() - Set the active test state
362 *
363 * @uts: Test state to use as currently active test state, or NULL if none
364 */
365void test_set_state(struct unit_test_state *uts);
366
367/**
Simon Glass1c721752021-03-07 17:34:47 -0700368 * ut_run_tests() - Run a set of tests
369 *
370 * This runs the test, handling any preparation and clean-up needed. It prints
371 * the name of each test before running it.
372 *
373 * @category: Category of these tests. This is a string printed at the start to
374 * announce the the number of tests
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, -1 if any failed
383 */
384int ut_run_list(const char *name, const char *prefix, struct unit_test *tests,
385 int count, const char *select_name);
386
Simon Glass2e7d35d2014-02-26 15:59:21 -0700387#endif