]>
Commit | Line | Data |
---|---|---|
0eb52298 MK |
1 | /** @file\r |
2 | Implement UnitTestLib assert services using cmocka services\r | |
3 | \r | |
4 | Copyright (c) 2019 - 2020, Intel Corporation. All rights reserved.<BR>\r | |
5 | SPDX-License-Identifier: BSD-2-Clause-Patent\r | |
6 | **/\r | |
7 | \r | |
8 | #include <stdio.h>\r | |
9 | #include <string.h>\r | |
10 | #include <stdarg.h>\r | |
11 | #include <stddef.h>\r | |
12 | #include <setjmp.h>\r | |
13 | #include <cmocka.h>\r | |
14 | \r | |
15 | #include <Uefi.h>\r | |
16 | #include <Library/BaseMemoryLib.h>\r | |
17 | #include <Library/UnitTestLib.h>\r | |
18 | \r | |
19 | #define MAX_STRING_SIZE 1025\r | |
20 | \r | |
21 | /**\r | |
22 | If Expression is TRUE, then TRUE is returned.\r | |
23 | If Expression is FALSE, then an assert is triggered and the location of the\r | |
24 | assert provided by FunctionName, LineNumber, FileName, and Description are\r | |
25 | recorded and FALSE is returned.\r | |
26 | \r | |
27 | @param[in] Expression The BOOLEAN result of the expression evaluation.\r | |
28 | @param[in] FunctionName Null-terminated ASCII string of the function\r | |
29 | executing the assert macro.\r | |
30 | @param[in] LineNumber The source file line number of the assert macro.\r | |
31 | @param[in] FileName Null-terminated ASCII string of the filename\r | |
32 | executing the assert macro.\r | |
33 | @param[in] Description Null-terminated ASCII string of the expression being\r | |
34 | evaluated.\r | |
35 | \r | |
36 | @retval TRUE Expression is TRUE.\r | |
37 | @retval FALSE Expression is FALSE.\r | |
38 | **/\r | |
39 | BOOLEAN\r | |
40 | EFIAPI\r | |
41 | UnitTestAssertTrue (\r | |
42 | IN BOOLEAN Expression,\r | |
43 | IN CONST CHAR8 *FunctionName,\r | |
44 | IN UINTN LineNumber,\r | |
45 | IN CONST CHAR8 *FileName,\r | |
46 | IN CONST CHAR8 *Description\r | |
47 | )\r | |
48 | {\r | |
49 | CHAR8 TempStr[MAX_STRING_SIZE];\r | |
50 | \r | |
7c0ad2c3 | 51 | snprintf (TempStr, sizeof (TempStr), "UT_ASSERT_TRUE(%s:%x)", Description, Expression);\r |
0eb52298 MK |
52 | _assert_true (Expression, TempStr, FileName, (INT32)LineNumber);\r |
53 | \r | |
54 | return Expression;\r | |
55 | }\r | |
56 | \r | |
57 | /**\r | |
58 | If Expression is FALSE, then TRUE is returned.\r | |
59 | If Expression is TRUE, then an assert is triggered and the location of the\r | |
60 | assert provided by FunctionName, LineNumber, FileName, and Description are\r | |
61 | recorded and FALSE is returned.\r | |
62 | \r | |
63 | @param[in] Expression The BOOLEAN result of the expression evaluation.\r | |
64 | @param[in] FunctionName Null-terminated ASCII string of the function\r | |
65 | executing the assert macro.\r | |
66 | @param[in] LineNumber The source file line number of the assert macro.\r | |
67 | @param[in] FileName Null-terminated ASCII string of the filename\r | |
68 | executing the assert macro.\r | |
69 | @param[in] Description Null-terminated ASCII string of the expression being\r | |
70 | evaluated.\r | |
71 | \r | |
72 | @retval TRUE Expression is FALSE.\r | |
73 | @retval FALSE Expression is TRUE.\r | |
74 | **/\r | |
75 | BOOLEAN\r | |
76 | EFIAPI\r | |
77 | UnitTestAssertFalse (\r | |
78 | IN BOOLEAN Expression,\r | |
79 | IN CONST CHAR8 *FunctionName,\r | |
80 | IN UINTN LineNumber,\r | |
81 | IN CONST CHAR8 *FileName,\r | |
82 | IN CONST CHAR8 *Description\r | |
83 | )\r | |
84 | {\r | |
85 | CHAR8 TempStr[MAX_STRING_SIZE];\r | |
86 | \r | |
7c0ad2c3 | 87 | snprintf (TempStr, sizeof (TempStr), "UT_ASSERT_FALSE(%s:%x)", Description, Expression);\r |
0eb52298 MK |
88 | _assert_true (!Expression, TempStr, FileName, (INT32)LineNumber);\r |
89 | \r | |
90 | return !Expression;\r | |
91 | }\r | |
92 | \r | |
93 | /**\r | |
94 | If Status is not an EFI_ERROR(), then TRUE is returned.\r | |
95 | If Status is an EFI_ERROR(), then an assert is triggered and the location of\r | |
96 | the assert provided by FunctionName, LineNumber, FileName, and Description are\r | |
97 | recorded and FALSE is returned.\r | |
98 | \r | |
99 | @param[in] Status The EFI_STATUS value to evaluate.\r | |
100 | @param[in] FunctionName Null-terminated ASCII string of the function\r | |
101 | executing the assert macro.\r | |
102 | @param[in] LineNumber The source file line number of the assert macro.\r | |
103 | @param[in] FileName Null-terminated ASCII string of the filename\r | |
104 | executing the assert macro.\r | |
105 | @param[in] Description Null-terminated ASCII string of the status\r | |
106 | expression being evaluated.\r | |
107 | \r | |
108 | @retval TRUE Status is not an EFI_ERROR().\r | |
109 | @retval FALSE Status is an EFI_ERROR().\r | |
110 | **/\r | |
111 | BOOLEAN\r | |
112 | EFIAPI\r | |
113 | UnitTestAssertNotEfiError (\r | |
114 | IN EFI_STATUS Status,\r | |
115 | IN CONST CHAR8 *FunctionName,\r | |
116 | IN UINTN LineNumber,\r | |
117 | IN CONST CHAR8 *FileName,\r | |
118 | IN CONST CHAR8 *Description\r | |
119 | )\r | |
120 | {\r | |
121 | CHAR8 TempStr[MAX_STRING_SIZE];\r | |
122 | \r | |
7c0ad2c3 | 123 | snprintf (TempStr, sizeof (TempStr), "UT_ASSERT_NOT_EFI_ERROR(%s:%p)", Description, (void *)Status);\r |
0eb52298 MK |
124 | _assert_true (!EFI_ERROR (Status), TempStr, FileName, (INT32)LineNumber);\r |
125 | \r | |
126 | return !EFI_ERROR (Status);\r | |
127 | }\r | |
128 | \r | |
129 | /**\r | |
130 | If ValueA is equal ValueB, then TRUE is returned.\r | |
131 | If ValueA is not equal to ValueB, then an assert is triggered and the location\r | |
132 | of the assert provided by FunctionName, LineNumber, FileName, DescriptionA,\r | |
133 | and DescriptionB are recorded and FALSE is returned.\r | |
134 | \r | |
135 | @param[in] ValueA 64-bit value.\r | |
136 | @param[in] ValueB 64-bit value.\r | |
137 | @param[in] FunctionName Null-terminated ASCII string of the function\r | |
138 | executing the assert macro.\r | |
139 | @param[in] LineNumber The source file line number of the assert macro.\r | |
140 | @param[in] FileName Null-terminated ASCII string of the filename\r | |
141 | executing the assert macro.\r | |
142 | @param[in] DescriptionA Null-terminated ASCII string that is a description\r | |
143 | of ValueA.\r | |
144 | @param[in] DescriptionB Null-terminated ASCII string that is a description\r | |
145 | of ValueB.\r | |
146 | \r | |
147 | @retval TRUE ValueA is equal to ValueB.\r | |
148 | @retval FALSE ValueA is not equal to ValueB.\r | |
149 | **/\r | |
150 | BOOLEAN\r | |
151 | EFIAPI\r | |
152 | UnitTestAssertEqual (\r | |
153 | IN UINT64 ValueA,\r | |
154 | IN UINT64 ValueB,\r | |
155 | IN CONST CHAR8 *FunctionName,\r | |
156 | IN UINTN LineNumber,\r | |
157 | IN CONST CHAR8 *FileName,\r | |
158 | IN CONST CHAR8 *DescriptionA,\r | |
159 | IN CONST CHAR8 *DescriptionB\r | |
160 | )\r | |
161 | {\r | |
162 | CHAR8 TempStr[MAX_STRING_SIZE];\r | |
163 | \r | |
7c0ad2c3 | 164 | snprintf (TempStr, sizeof (TempStr), "UT_ASSERT_EQUAL(%s:%llx, %s:%llx)", DescriptionA, ValueA, DescriptionB, ValueB);\r |
0eb52298 MK |
165 | _assert_true ((ValueA == ValueB), TempStr, FileName, (INT32)LineNumber);\r |
166 | \r | |
167 | return (ValueA == ValueB);\r | |
168 | }\r | |
169 | \r | |
170 | /**\r | |
171 | If the contents of BufferA are identical to the contents of BufferB, then TRUE\r | |
172 | is returned. If the contents of BufferA are not identical to the contents of\r | |
173 | BufferB, then an assert is triggered and the location of the assert provided\r | |
174 | by FunctionName, LineNumber, FileName, DescriptionA, and DescriptionB are\r | |
175 | recorded and FALSE is returned.\r | |
176 | \r | |
177 | @param[in] BufferA Pointer to a buffer for comparison.\r | |
178 | @param[in] BufferB Pointer to a buffer for comparison.\r | |
179 | @param[in] Length Number of bytes to compare in BufferA and BufferB.\r | |
180 | @param[in] FunctionName Null-terminated ASCII string of the function\r | |
181 | executing the assert macro.\r | |
182 | @param[in] LineNumber The source file line number of the assert macro.\r | |
183 | @param[in] FileName Null-terminated ASCII string of the filename\r | |
184 | executing the assert macro.\r | |
185 | @param[in] DescriptionA Null-terminated ASCII string that is a description\r | |
186 | of BufferA.\r | |
187 | @param[in] DescriptionB Null-terminated ASCII string that is a description\r | |
188 | of BufferB.\r | |
189 | \r | |
190 | @retval TRUE The contents of BufferA are identical to the contents of\r | |
191 | BufferB.\r | |
192 | @retval FALSE The contents of BufferA are not identical to the contents of\r | |
193 | BufferB.\r | |
194 | **/\r | |
195 | BOOLEAN\r | |
196 | EFIAPI\r | |
197 | UnitTestAssertMemEqual (\r | |
198 | IN VOID *BufferA,\r | |
199 | IN VOID *BufferB,\r | |
200 | IN UINTN Length,\r | |
201 | IN CONST CHAR8 *FunctionName,\r | |
202 | IN UINTN LineNumber,\r | |
203 | IN CONST CHAR8 *FileName,\r | |
204 | IN CONST CHAR8 *DescriptionA,\r | |
205 | IN CONST CHAR8 *DescriptionB\r | |
206 | )\r | |
207 | {\r | |
208 | CHAR8 TempStr[MAX_STRING_SIZE];\r | |
209 | BOOLEAN Result;\r | |
210 | \r | |
7c0ad2c3 | 211 | Result = (CompareMem (BufferA, BufferB, Length) == 0);\r |
0eb52298 | 212 | \r |
7c0ad2c3 | 213 | snprintf (TempStr, sizeof (TempStr), "UT_ASSERT_MEM_EQUAL(%s:%p, %s:%p)", DescriptionA, BufferA, DescriptionB, BufferB);\r |
0eb52298 MK |
214 | _assert_true (Result, TempStr, FileName, (INT32)LineNumber);\r |
215 | \r | |
216 | return Result;\r | |
217 | }\r | |
218 | \r | |
219 | /**\r | |
220 | If ValueA is not equal ValueB, then TRUE is returned.\r | |
221 | If ValueA is equal to ValueB, then an assert is triggered and the location\r | |
222 | of the assert provided by FunctionName, LineNumber, FileName, DescriptionA\r | |
223 | and DescriptionB are recorded and FALSE is returned.\r | |
224 | \r | |
225 | @param[in] ValueA 64-bit value.\r | |
226 | @param[in] ValueB 64-bit value.\r | |
227 | @param[in] FunctionName Null-terminated ASCII string of the function\r | |
228 | executing the assert macro.\r | |
229 | @param[in] LineNumber The source file line number of the assert macro.\r | |
230 | @param[in] FileName Null-terminated ASCII string of the filename\r | |
231 | executing the assert macro.\r | |
232 | @param[in] DescriptionA Null-terminated ASCII string that is a description\r | |
233 | of ValueA.\r | |
234 | @param[in] DescriptionB Null-terminated ASCII string that is a description\r | |
235 | of ValueB.\r | |
236 | \r | |
237 | @retval TRUE ValueA is not equal to ValueB.\r | |
238 | @retval FALSE ValueA is equal to ValueB.\r | |
239 | **/\r | |
240 | BOOLEAN\r | |
241 | EFIAPI\r | |
242 | UnitTestAssertNotEqual (\r | |
243 | IN UINT64 ValueA,\r | |
244 | IN UINT64 ValueB,\r | |
245 | IN CONST CHAR8 *FunctionName,\r | |
246 | IN UINTN LineNumber,\r | |
247 | IN CONST CHAR8 *FileName,\r | |
248 | IN CONST CHAR8 *DescriptionA,\r | |
249 | IN CONST CHAR8 *DescriptionB\r | |
250 | )\r | |
251 | {\r | |
252 | CHAR8 TempStr[MAX_STRING_SIZE];\r | |
253 | \r | |
7c0ad2c3 | 254 | snprintf (TempStr, sizeof (TempStr), "UT_ASSERT_NOT_EQUAL(%s:%llx, %s:%llx)", DescriptionA, ValueA, DescriptionB, ValueB);\r |
0eb52298 MK |
255 | _assert_true ((ValueA != ValueB), TempStr, FileName, (INT32)LineNumber);\r |
256 | \r | |
257 | return (ValueA != ValueB);\r | |
258 | }\r | |
259 | \r | |
260 | /**\r | |
261 | If Status is equal to Expected, then TRUE is returned.\r | |
262 | If Status is not equal to Expected, then an assert is triggered and the\r | |
263 | location of the assert provided by FunctionName, LineNumber, FileName, and\r | |
264 | Description are recorded and FALSE is returned.\r | |
265 | \r | |
266 | @param[in] Status EFI_STATUS value returned from an API under test.\r | |
267 | @param[in] Expected The expected EFI_STATUS return value from an API\r | |
268 | under test.\r | |
269 | @param[in] FunctionName Null-terminated ASCII string of the function\r | |
270 | executing the assert macro.\r | |
271 | @param[in] LineNumber The source file line number of the assert macro.\r | |
272 | @param[in] FileName Null-terminated ASCII string of the filename\r | |
273 | executing the assert macro.\r | |
274 | @param[in] Description Null-terminated ASCII string that is a description\r | |
275 | of Status.\r | |
276 | \r | |
277 | @retval TRUE Status is equal to Expected.\r | |
278 | @retval FALSE Status is not equal to Expected.\r | |
279 | **/\r | |
280 | BOOLEAN\r | |
281 | EFIAPI\r | |
282 | UnitTestAssertStatusEqual (\r | |
283 | IN EFI_STATUS Status,\r | |
284 | IN EFI_STATUS Expected,\r | |
285 | IN CONST CHAR8 *FunctionName,\r | |
286 | IN UINTN LineNumber,\r | |
287 | IN CONST CHAR8 *FileName,\r | |
288 | IN CONST CHAR8 *Description\r | |
289 | )\r | |
290 | {\r | |
291 | CHAR8 TempStr[MAX_STRING_SIZE];\r | |
292 | \r | |
44fc90eb | 293 | snprintf (TempStr, sizeof (TempStr), "UT_ASSERT_STATUS_EQUAL(%s:%p expected:%p)", Description, (VOID *)Status, (VOID *)Expected);\r |
0eb52298 MK |
294 | _assert_true ((Status == Expected), TempStr, FileName, (INT32)LineNumber);\r |
295 | \r | |
296 | return (Status == Expected);\r | |
297 | }\r | |
298 | \r | |
299 | /**\r | |
300 | If Pointer is not equal to NULL, then TRUE is returned.\r | |
301 | If Pointer is equal to NULL, then an assert is triggered and the location of\r | |
302 | the assert provided by FunctionName, LineNumber, FileName, and PointerName\r | |
303 | are recorded and FALSE is returned.\r | |
304 | \r | |
305 | @param[in] Pointer Pointer value to be checked against NULL.\r | |
306 | @param[in] Expected The expected EFI_STATUS return value from a function\r | |
307 | under test.\r | |
308 | @param[in] FunctionName Null-terminated ASCII string of the function\r | |
309 | executing the assert macro.\r | |
310 | @param[in] LineNumber The source file line number of the assert macro.\r | |
311 | @param[in] FileName Null-terminated ASCII string of the filename\r | |
312 | executing the assert macro.\r | |
313 | @param[in] PointerName Null-terminated ASCII string that is a description\r | |
314 | of Pointer.\r | |
315 | \r | |
316 | @retval TRUE Pointer is not equal to NULL.\r | |
317 | @retval FALSE Pointer is equal to NULL.\r | |
318 | **/\r | |
319 | BOOLEAN\r | |
320 | EFIAPI\r | |
321 | UnitTestAssertNotNull (\r | |
322 | IN VOID *Pointer,\r | |
323 | IN CONST CHAR8 *FunctionName,\r | |
324 | IN UINTN LineNumber,\r | |
325 | IN CONST CHAR8 *FileName,\r | |
326 | IN CONST CHAR8 *PointerName\r | |
327 | )\r | |
328 | {\r | |
329 | CHAR8 TempStr[MAX_STRING_SIZE];\r | |
330 | \r | |
7c0ad2c3 | 331 | snprintf (TempStr, sizeof (TempStr), "UT_ASSERT_NOT_NULL(%s:%p)", PointerName, Pointer);\r |
0eb52298 MK |
332 | _assert_true ((Pointer != NULL), TempStr, FileName, (INT32)LineNumber);\r |
333 | \r | |
334 | return (Pointer != NULL);\r | |
335 | }\r | |
26824851 MK |
336 | \r |
337 | /**\r | |
338 | If UnitTestStatus is UNIT_TEST_PASSED, then log an info message and return\r | |
339 | TRUE because an ASSERT() was expected when FunctionCall was executed and an\r | |
340 | ASSERT() was triggered. If UnitTestStatus is UNIT_TEST_SKIPPED, then log a\r | |
341 | warning message and return TRUE because ASSERT() macros are disabled. If\r | |
342 | UnitTestStatus is UNIT_TEST_ERROR_TEST_FAILED, then log an error message and\r | |
343 | return FALSE because an ASSERT() was expected when FunctionCall was executed,\r | |
344 | but no ASSERT() conditions were triggered. The log messages contain\r | |
345 | FunctionName, LineNumber, and FileName strings to provide the location of the\r | |
346 | UT_EXPECT_ASSERT_FAILURE() macro.\r | |
347 | \r | |
348 | @param[in] UnitTestStatus The status from UT_EXPECT_ASSERT_FAILURE() that\r | |
349 | is either pass, skipped, or failed.\r | |
350 | @param[in] FunctionName Null-terminated ASCII string of the function\r | |
351 | executing the UT_EXPECT_ASSERT_FAILURE() macro.\r | |
352 | @param[in] LineNumber The source file line number of the the function\r | |
353 | executing the UT_EXPECT_ASSERT_FAILURE() macro.\r | |
354 | @param[in] FileName Null-terminated ASCII string of the filename\r | |
355 | executing the UT_EXPECT_ASSERT_FAILURE() macro.\r | |
356 | @param[in] FunctionCall Null-terminated ASCII string of the function call\r | |
357 | executed by the UT_EXPECT_ASSERT_FAILURE() macro.\r | |
358 | @param[out] ResultStatus Used to return the UnitTestStatus value to the\r | |
359 | caller of UT_EXPECT_ASSERT_FAILURE(). This is\r | |
360 | optional parameter that may be NULL.\r | |
361 | \r | |
362 | @retval TRUE UnitTestStatus is UNIT_TEST_PASSED.\r | |
363 | @retval TRUE UnitTestStatus is UNIT_TEST_SKIPPED.\r | |
364 | @retval FALSE UnitTestStatus is UNIT_TEST_ERROR_TEST_FAILED.\r | |
365 | **/\r | |
366 | BOOLEAN\r | |
367 | EFIAPI\r | |
368 | UnitTestExpectAssertFailure (\r | |
369 | IN UNIT_TEST_STATUS UnitTestStatus,\r | |
370 | IN CONST CHAR8 *FunctionName,\r | |
371 | IN UINTN LineNumber,\r | |
372 | IN CONST CHAR8 *FileName,\r | |
373 | IN CONST CHAR8 *FunctionCall,\r | |
374 | OUT UNIT_TEST_STATUS *ResultStatus OPTIONAL\r | |
375 | )\r | |
376 | {\r | |
377 | CHAR8 TempStr[MAX_STRING_SIZE];\r | |
378 | \r | |
379 | if (ResultStatus != NULL) {\r | |
380 | *ResultStatus = UnitTestStatus;\r | |
381 | }\r | |
7c0ad2c3 | 382 | \r |
26824851 MK |
383 | if (UnitTestStatus == UNIT_TEST_PASSED) {\r |
384 | UT_LOG_INFO (\r | |
385 | "[ASSERT PASS] %a:%d: UT_EXPECT_ASSERT_FAILURE(%a) detected expected assert\n",\r | |
386 | FileName,\r | |
387 | LineNumber,\r | |
388 | FunctionCall\r | |
389 | );\r | |
390 | }\r | |
7c0ad2c3 | 391 | \r |
26824851 MK |
392 | if (UnitTestStatus == UNIT_TEST_SKIPPED) {\r |
393 | UT_LOG_WARNING (\r | |
394 | "[ASSERT WARN] %a:%d: UT_EXPECT_ASSERT_FAILURE(%a) disabled\n",\r | |
395 | FileName,\r | |
396 | LineNumber,\r | |
397 | FunctionCall\r | |
398 | );\r | |
399 | }\r | |
7c0ad2c3 | 400 | \r |
26824851 | 401 | if (UnitTestStatus == UNIT_TEST_ERROR_TEST_FAILED) {\r |
7c0ad2c3 | 402 | snprintf (TempStr, sizeof (TempStr), "UT_EXPECT_ASSERT_FAILURE(%s) did not trigger ASSERT()", FunctionCall);\r |
26824851 MK |
403 | _assert_true (FALSE, TempStr, FileName, (INT32)LineNumber);\r |
404 | }\r | |
7c0ad2c3 | 405 | \r |
26824851 MK |
406 | return (UnitTestStatus != UNIT_TEST_ERROR_TEST_FAILED);\r |
407 | }\r |