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