1
2
3
4
5
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
16struct unit_test_state;
17
18
19
20
21
22
23
24
25
26
27void ut_fail(struct unit_test_state *uts, const char *fname, int line,
28 const char *func, const char *cond);
29
30
31
32
33
34
35
36
37
38
39
40void 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
46
47
48
49
50
51
52
53
54
55
56
57int ut_check_console_line(struct unit_test_state *uts, const char *fmt, ...)
58 __attribute__ ((format (__printf__, 2, 3)));
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74int ut_check_console_linen(struct unit_test_state *uts, const char *fmt, ...)
75 __attribute__ ((format (__printf__, 2, 3)));
76
77
78
79
80
81
82
83int ut_check_skipline(struct unit_test_state *uts);
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98int ut_check_skip_to_line(struct unit_test_state *uts, const char *fmt, ...);
99
100
101
102
103
104
105
106
107
108
109int ut_check_console_end(struct unit_test_state *uts);
110
111
112
113
114
115
116
117
118
119
120int ut_check_console_dump(struct unit_test_state *uts, int total_bytes);
121
122
123#define ut_assert(cond) \
124 if (!(cond)) { \
125 ut_fail(uts, __FILE__, __LINE__, __func__, #cond); \
126 return CMD_RET_FAILURE; \
127 }
128
129
130#define ut_assertf(cond, fmt, args...) \
131 if (!(cond)) { \
132 ut_failf(uts, __FILE__, __LINE__, __func__, #cond, \
133 fmt, ##args); \
134 return CMD_RET_FAILURE; \
135 }
136
137
138#define ut_asserteq(expr1, expr2) { \
139 unsigned int _val1 = (expr1), _val2 = (expr2); \
140 \
141 if (_val1 != _val2) { \
142 ut_failf(uts, __FILE__, __LINE__, __func__, \
143 #expr1 " == " #expr2, \
144 "Expected %#x (%d), got %#x (%d)", \
145 _val1, _val1, _val2, _val2); \
146 return CMD_RET_FAILURE; \
147 } \
148}
149
150
151#define ut_asserteq_64(expr1, expr2) { \
152 u64 _val1 = (expr1), _val2 = (expr2); \
153 \
154 if (_val1 != _val2) { \
155 ut_failf(uts, __FILE__, __LINE__, __func__, \
156 #expr1 " == " #expr2, \
157 "Expected %#llx (%lld), got %#llx (%lld)", \
158 (unsigned long long)_val1, \
159 (unsigned long long)_val1, \
160 (unsigned long long)_val2, \
161 (unsigned long long)_val2); \
162 return CMD_RET_FAILURE; \
163 } \
164}
165
166
167#define ut_asserteq_str(expr1, expr2) { \
168 const char *_val1 = (expr1), *_val2 = (expr2); \
169 \
170 if (strcmp(_val1, _val2)) { \
171 ut_failf(uts, __FILE__, __LINE__, __func__, \
172 #expr1 " = " #expr2, \
173 "Expected \"%s\", got \"%s\"", _val1, _val2); \
174 return CMD_RET_FAILURE; \
175 } \
176}
177
178
179
180
181
182#define ut_asserteq_strn(expr1, expr2) { \
183 const char *_val1 = (expr1), *_val2 = (expr2); \
184 int _len = strlen(_val1); \
185 \
186 if (memcmp(_val1, _val2, _len)) { \
187 ut_failf(uts, __FILE__, __LINE__, __func__, \
188 #expr1 " = " #expr2, \
189 "Expected \"%.*s\", got \"%.*s\"", \
190 _len, _val1, _len, _val2); \
191 return CMD_RET_FAILURE; \
192 } \
193}
194
195
196#define ut_asserteq_mem(expr1, expr2, len) { \
197 const u8 *_val1 = (u8 *)(expr1), *_val2 = (u8 *)(expr2); \
198 const uint __len = len; \
199 \
200 if (memcmp(_val1, _val2, __len)) { \
201 char __buf1[64 + 1] = "\0"; \
202 char __buf2[64 + 1] = "\0"; \
203 bin2hex(__buf1, _val1, min(__len, (uint)32)); \
204 bin2hex(__buf2, _val2, min(__len, (uint)32)); \
205 ut_failf(uts, __FILE__, __LINE__, __func__, \
206 #expr1 " = " #expr2, \
207 "Expected \"%s\", got \"%s\"", \
208 __buf1, __buf2); \
209 return CMD_RET_FAILURE; \
210 } \
211}
212
213
214#define ut_asserteq_ptr(expr1, expr2) { \
215 const void *_val1 = (expr1), *_val2 = (expr2); \
216 \
217 if (_val1 != _val2) { \
218 ut_failf(uts, __FILE__, __LINE__, __func__, \
219 #expr1 " = " #expr2, \
220 "Expected %p, got %p", _val1, _val2); \
221 return CMD_RET_FAILURE; \
222 } \
223}
224
225
226#define ut_asserteq_addr(expr1, expr2) { \
227 ulong _val1 = map_to_sysmem(expr1); \
228 ulong _val2 = map_to_sysmem(expr2); \
229 \
230 if (_val1 != _val2) { \
231 ut_failf(uts, __FILE__, __LINE__, __func__, \
232 #expr1 " = " #expr2, \
233 "Expected %lx, got %lx", _val1, _val2); \
234 return CMD_RET_FAILURE; \
235 } \
236}
237
238
239#define ut_assertnull(expr) { \
240 const void *_val = (expr); \
241 \
242 if (_val) { \
243 ut_failf(uts, __FILE__, __LINE__, __func__, \
244 #expr " != NULL", \
245 "Expected NULL, got %p", _val); \
246 return CMD_RET_FAILURE; \
247 } \
248}
249
250
251#define ut_assertnonnull(expr) { \
252 const void *_val = (expr); \
253 \
254 if (!_val) { \
255 ut_failf(uts, __FILE__, __LINE__, __func__, \
256 #expr " = NULL", \
257 "Expected non-null, got NULL"); \
258 return CMD_RET_FAILURE; \
259 } \
260}
261
262
263#define ut_assertok_ptr(expr) { \
264 const void *_val = (expr); \
265 \
266 if (IS_ERR(_val)) { \
267 ut_failf(uts, __FILE__, __LINE__, __func__, \
268 #expr " = NULL", \
269 "Expected pointer, got error %ld", \
270 PTR_ERR(_val)); \
271 return CMD_RET_FAILURE; \
272 } \
273}
274
275
276#define ut_assertok(cond) ut_asserteq(0, cond)
277
278
279#define ut_assert_nextline(fmt, args...) \
280 if (ut_check_console_line(uts, fmt, ##args)) { \
281 ut_failf(uts, __FILE__, __LINE__, __func__, \
282 "console", "\nExpected '%s',\n got '%s'", \
283 uts->expect_str, uts->actual_str); \
284 return CMD_RET_FAILURE; \
285 } \
286
287
288#define ut_assert_nextlinen(fmt, args...) \
289 if (ut_check_console_linen(uts, fmt, ##args)) { \
290 ut_failf(uts, __FILE__, __LINE__, __func__, \
291 "console", "\nExpected '%s',\n got '%s'", \
292 uts->expect_str, uts->actual_str); \
293 return CMD_RET_FAILURE; \
294 } \
295
296
297#define ut_assert_skipline() \
298 if (ut_check_skipline(uts)) { \
299 ut_failf(uts, __FILE__, __LINE__, __func__, \
300 "console", "\nExpected a line, got end"); \
301 return CMD_RET_FAILURE; \
302 } \
303
304
305#define ut_assert_skip_to_line(fmt, args...) \
306 if (ut_check_skip_to_line(uts, fmt, ##args)) { \
307 ut_failf(uts, __FILE__, __LINE__, __func__, \
308 "console", "\nExpected '%s',\n got to '%s'", \
309 uts->expect_str, uts->actual_str); \
310 return CMD_RET_FAILURE; \
311 } \
312
313
314#define ut_assert_console_end() \
315 if (ut_check_console_end(uts)) { \
316 ut_failf(uts, __FILE__, __LINE__, __func__, \
317 "console", "Expected no more output, got '%s'",\
318 uts->actual_str); \
319 return CMD_RET_FAILURE; \
320 } \
321
322
323#define ut_assert_nextlines_are_dump(total_bytes) \
324 if (ut_check_console_dump(uts, total_bytes)) { \
325 ut_failf(uts, __FILE__, __LINE__, __func__, \
326 "console", \
327 "Expected dump of length %x bytes, got '%s'", \
328 total_bytes, uts->actual_str); \
329 return CMD_RET_FAILURE; \
330 } \
331
332
333
334
335
336
337ulong ut_check_free(void);
338
339
340
341
342
343
344
345
346long ut_check_delta(ulong last);
347
348
349
350
351
352
353
354
355
356void ut_silence_console(struct unit_test_state *uts);
357
358
359
360
361
362
363
364void ut_unsilence_console(struct unit_test_state *uts);
365
366
367
368
369
370
371
372
373
374
375void ut_set_skip_delays(struct unit_test_state *uts, bool skip_delays);
376
377
378
379
380
381
382struct unit_test_state *test_get_state(void);
383
384
385
386
387
388
389void test_set_state(struct unit_test_state *uts);
390
391
392
393
394
395
396
397
398
399
400
401
402
403
404
405
406
407
408int ut_run_list(const char *name, const char *prefix, struct unit_test *tests,
409 int count, const char *select_name);
410
411#endif
412