test: Add a way to check part of a console line or skip it
[platform/kernel/u-boot.git] / include / test / ut.h
1 /* SPDX-License-Identifier: GPL-2.0+ */
2 /*
3  * Simple unit test library
4  *
5  * Copyright (c) 2013 Google, Inc
6  */
7
8 #ifndef __TEST_UT_H
9 #define __TEST_UT_H
10
11 #include <command.h>
12 #include <hexdump.h>
13 #include <linux/err.h>
14 #include <test/test.h>
15
16 struct unit_test_state;
17
18 /**
19  * ut_fail() - Record failure of a unit test
20  *
21  * @uts: Test state
22  * @fname: Filename where the error occurred
23  * @line: Line number where the error occurred
24  * @func: Function name where the error occurred
25  * @cond: The condition that failed
26  */
27 void ut_fail(struct unit_test_state *uts, const char *fname, int line,
28              const char *func, const char *cond);
29
30 /**
31  * ut_failf() - Record failure of a unit test
32  *
33  * @uts: Test state
34  * @fname: Filename where the error occurred
35  * @line: Line number where the error occurred
36  * @func: Function name where the error occurred
37  * @cond: The condition that failed
38  * @fmt: printf() format string for the error, followed by args
39  */
40 void ut_failf(struct unit_test_state *uts, const char *fname, int line,
41               const char *func, const char *cond, const char *fmt, ...)
42                         __attribute__ ((format (__printf__, 6, 7)));
43
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
55  * @return 0 if OK, other value on error
56  */
57 int ut_check_console_line(struct unit_test_state *uts, const char *fmt, ...)
58                         __attribute__ ((format (__printf__, 2, 3)));
59
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
72  * @return 0 if OK, other value on error
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
81  * @return 0 if OK, other value on error
82  */
83 int ut_check_skipline(struct unit_test_state *uts);
84
85 /**
86  * 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  */
94 int 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  */
105 int ut_check_console_dump(struct unit_test_state *uts, int total_bytes);
106
107 /* Assert that a condition is non-zero */
108 #define ut_assert(cond)                                                 \
109         if (!(cond)) {                                                  \
110                 ut_fail(uts, __FILE__, __LINE__, __func__, #cond);      \
111                 return CMD_RET_FAILURE;                                 \
112         }
113
114 /* Assert that a condition is non-zero, with printf() string */
115 #define ut_assertf(cond, fmt, args...)                                  \
116         if (!(cond)) {                                                  \
117                 ut_failf(uts, __FILE__, __LINE__, __func__, #cond,      \
118                          fmt, ##args);                                  \
119                 return CMD_RET_FAILURE;                                 \
120         }
121
122 /* Assert that two int expressions are equal */
123 #define ut_asserteq(expr1, expr2) {                                     \
124         unsigned int _val1 = (expr1), _val2 = (expr2);                  \
125                                                                         \
126         if (_val1 != _val2) {                                           \
127                 ut_failf(uts, __FILE__, __LINE__, __func__,             \
128                          #expr1 " == " #expr2,                          \
129                          "Expected %#x (%d), got %#x (%d)",             \
130                          _val1, _val1, _val2, _val2);                   \
131                 return CMD_RET_FAILURE;                                 \
132         }                                                               \
133 }
134
135 /* 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
151 /* Assert that two string expressions are equal */
152 #define ut_asserteq_str(expr1, expr2) {                                 \
153         const char *_val1 = (expr1), *_val2 = (expr2);                  \
154                                                                         \
155         if (strcmp(_val1, _val2)) {                                     \
156                 ut_failf(uts, __FILE__, __LINE__, __func__,             \
157                          #expr1 " = " #expr2,                           \
158                          "Expected \"%s\", got \"%s\"", _val1, _val2);  \
159                 return CMD_RET_FAILURE;                                 \
160         }                                                               \
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
180 /*
181  * Assert that two string expressions are equal, up to length of the
182  * first
183  */
184 #define ut_asserteq_strn(expr1, expr2) {                                \
185         const char *_val1 = (expr1), *_val2 = (expr2);                  \
186         int _len = strlen(_val1);                                       \
187                                                                         \
188         if (memcmp(_val1, _val2, _len)) {                               \
189                 ut_failf(uts, __FILE__, __LINE__, __func__,             \
190                          #expr1 " = " #expr2,                           \
191                          "Expected \"%.*s\", got \"%.*s\"",             \
192                          _len, _val1, _len, _val2);                     \
193                 return CMD_RET_FAILURE;                                 \
194         }                                                               \
195 }
196
197 /* Assert that two memory areas are equal */
198 #define ut_asserteq_mem(expr1, expr2, len) {                            \
199         const u8 *_val1 = (u8 *)(expr1), *_val2 = (u8 *)(expr2);        \
200         const uint __len = len;                                         \
201                                                                         \
202         if (memcmp(_val1, _val2, __len)) {                              \
203                 char __buf1[64 + 1] = "\0";                             \
204                 char __buf2[64 + 1] = "\0";                             \
205                 bin2hex(__buf1, _val1, min(__len, (uint)32));           \
206                 bin2hex(__buf2, _val2, min(__len, (uint)32));           \
207                 ut_failf(uts, __FILE__, __LINE__, __func__,             \
208                          #expr1 " = " #expr2,                           \
209                          "Expected \"%s\", got \"%s\"",                 \
210                          __buf1, __buf2);                               \
211                 return CMD_RET_FAILURE;                                 \
212         }                                                               \
213 }
214
215 /* Assert that two pointers are equal */
216 #define ut_asserteq_ptr(expr1, expr2) {                                 \
217         const void *_val1 = (expr1), *_val2 = (expr2);                  \
218                                                                         \
219         if (_val1 != _val2) {                                           \
220                 ut_failf(uts, __FILE__, __LINE__, __func__,             \
221                          #expr1 " = " #expr2,                           \
222                          "Expected %p, got %p", _val1, _val2);          \
223                 return CMD_RET_FAILURE;                                 \
224         }                                                               \
225 }
226
227 /* Assert that a pointer is NULL */
228 #define ut_assertnull(expr) {                                   \
229         const void *_val = (expr);                                      \
230                                                                         \
231         if (_val) {                                             \
232                 ut_failf(uts, __FILE__, __LINE__, __func__,             \
233                          #expr " != NULL",                              \
234                          "Expected NULL, got %p", _val);                \
235                 return CMD_RET_FAILURE;                                 \
236         }                                                               \
237 }
238
239 /* Assert that a pointer is not NULL */
240 #define ut_assertnonnull(expr) {                                        \
241         const void *_val = (expr);                                      \
242                                                                         \
243         if (!_val) {                                            \
244                 ut_failf(uts, __FILE__, __LINE__, __func__,             \
245                          #expr " = NULL",                               \
246                          "Expected non-null, got NULL");                \
247                 return CMD_RET_FAILURE;                                 \
248         }                                                               \
249 }
250
251 /* Assert that a pointer is not an error pointer */
252 #define ut_assertok_ptr(expr) {                                         \
253         const void *_val = (expr);                                      \
254                                                                         \
255         if (IS_ERR(_val)) {                                             \
256                 ut_failf(uts, __FILE__, __LINE__, __func__,             \
257                          #expr " = NULL",                               \
258                          "Expected pointer, got error %ld",             \
259                          PTR_ERR(_val));                                \
260                 return CMD_RET_FAILURE;                                 \
261         }                                                               \
262 }
263
264 /* Assert that an operation succeeds (returns 0) */
265 #define ut_assertok(cond)       ut_asserteq(0, cond)
266
267 /* Assert that the next console output line matches */
268 #define ut_assert_nextline(fmt, args...)                                \
269         if (ut_check_console_line(uts, fmt, ##args)) {                  \
270                 ut_failf(uts, __FILE__, __LINE__, __func__,             \
271                          "console", "\nExpected '%s',\n     got '%s'",  \
272                          uts->expect_str, uts->actual_str);             \
273                 return CMD_RET_FAILURE;                                 \
274         }                                                               \
275
276 /* Assert that the next console output line matches up to the length */
277 #define ut_assert_nextlinen(fmt, args...)                               \
278         if (ut_check_console_linen(uts, fmt, ##args)) {                 \
279                 ut_failf(uts, __FILE__, __LINE__, __func__,             \
280                          "console", "\nExpected '%s',\n     got '%s'",  \
281                          uts->expect_str, uts->actual_str);             \
282                 return CMD_RET_FAILURE;                                 \
283         }                                                               \
284
285 /* Assert that there is a 'next' console output line, and skip it */
286 #define ut_assert_skipline()                                            \
287         if (ut_check_skipline(uts)) {                                   \
288                 ut_failf(uts, __FILE__, __LINE__, __func__,             \
289                          "console", "\nExpected a line, got end");      \
290                 return CMD_RET_FAILURE;                                 \
291         }                                                               \
292
293 /* Assert that there is no more console output */
294 #define ut_assert_console_end()                                         \
295         if (ut_check_console_end(uts)) {                                \
296                 ut_failf(uts, __FILE__, __LINE__, __func__,             \
297                          "console", "Expected no more output, got '%s'",\
298                          uts->actual_str);                              \
299                 return CMD_RET_FAILURE;                                 \
300         }                                                               \
301
302 /* Assert that the next lines are print_buffer() dump at an address */
303 #define ut_assert_nextlines_are_dump(total_bytes)                       \
304         if (ut_check_console_dump(uts, total_bytes)) {                  \
305                 ut_failf(uts, __FILE__, __LINE__, __func__,             \
306                          "console",                                     \
307                         "Expected dump of length %x bytes, got '%s'",   \
308                          total_bytes, uts->actual_str);                 \
309                 return CMD_RET_FAILURE;                                 \
310         }                                                               \
311
312 /**
313  * ut_check_free() - Return the number of bytes free in the malloc() pool
314  *
315  * @return bytes free
316  */
317 ulong ut_check_free(void);
318
319 /**
320  * ut_check_delta() - Return the number of bytes allocated/freed
321  *
322  * @last: Last value from ut_check_free
323  * @return free memory delta from @last; positive means more memory has been
324  *      allocated, negative means less has been allocated (i.e. some is freed)
325  */
326 long ut_check_delta(ulong last);
327
328 #endif