]>
Commit | Line | Data |
---|---|---|
0eb52298 MK |
1 | /**\r |
2 | Implement UnitTestLib assert services\r | |
3 | \r | |
4 | Copyright (c) Microsoft Corporation.<BR>\r | |
5 | SPDX-License-Identifier: BSD-2-Clause-Patent\r | |
6 | **/\r | |
7 | \r | |
8 | #include <Uefi.h>\r | |
9 | #include <UnitTestFrameworkTypes.h>\r | |
10 | #include <Library/UnitTestLib.h>\r | |
11 | #include <Library/BaseLib.h>\r | |
12 | #include <Library/BaseMemoryLib.h>\r | |
13 | #include <Library/DebugLib.h>\r | |
14 | #include <Library/PrintLib.h>\r | |
15 | \r | |
16 | STATIC\r | |
17 | EFI_STATUS\r | |
18 | AddUnitTestFailure (\r | |
19 | IN OUT UNIT_TEST *UnitTest,\r | |
20 | IN CONST CHAR8 *FailureMessage,\r | |
21 | IN FAILURE_TYPE FailureType\r | |
22 | )\r | |
23 | {\r | |
24 | //\r | |
25 | // Make sure that you're cooking with gas.\r | |
26 | //\r | |
27 | if (UnitTest == NULL || FailureMessage == NULL) {\r | |
28 | return EFI_INVALID_PARAMETER;\r | |
29 | }\r | |
30 | \r | |
31 | UnitTest->FailureType = FailureType;\r | |
32 | AsciiStrCpyS (\r | |
33 | &UnitTest->FailureMessage[0],\r | |
34 | UNIT_TEST_TESTFAILUREMSG_LENGTH,\r | |
35 | FailureMessage\r | |
36 | );\r | |
37 | \r | |
38 | return EFI_SUCCESS;\r | |
39 | }\r | |
40 | \r | |
41 | STATIC\r | |
42 | VOID\r | |
43 | UnitTestLogFailure (\r | |
44 | IN FAILURE_TYPE FailureType,\r | |
45 | IN CONST CHAR8 *Format,\r | |
46 | ...\r | |
47 | )\r | |
48 | {\r | |
49 | UNIT_TEST_FRAMEWORK_HANDLE FrameworkHandle;\r | |
50 | CHAR8 LogString[UNIT_TEST_TESTFAILUREMSG_LENGTH];\r | |
51 | VA_LIST Marker;\r | |
52 | \r | |
53 | //\r | |
54 | // Get active Framework handle\r | |
55 | //\r | |
56 | FrameworkHandle = GetActiveFrameworkHandle ();\r | |
57 | \r | |
58 | //\r | |
59 | // Convert the message to an ASCII String\r | |
60 | //\r | |
61 | VA_START (Marker, Format);\r | |
62 | AsciiVSPrint (LogString, sizeof (LogString), Format, Marker);\r | |
63 | VA_END (Marker);\r | |
64 | \r | |
65 | //\r | |
66 | // Finally, add the string to the log.\r | |
67 | //\r | |
68 | AddUnitTestFailure (\r | |
69 | ((UNIT_TEST_FRAMEWORK *)FrameworkHandle)->CurrentTest,\r | |
70 | LogString,\r | |
71 | FailureType\r | |
72 | );\r | |
73 | \r | |
74 | return;\r | |
75 | }\r | |
76 | \r | |
77 | /**\r | |
78 | If Expression is TRUE, then TRUE is returned.\r | |
79 | If Expression is FALSE, then an assert is triggered and the location of the\r | |
80 | assert provided by FunctionName, LineNumber, FileName, and Description are\r | |
81 | recorded and FALSE is returned.\r | |
82 | \r | |
83 | @param[in] Expression The BOOLEAN result of the expression evaluation.\r | |
84 | @param[in] FunctionName Null-terminated ASCII string of the function\r | |
85 | executing the assert macro.\r | |
86 | @param[in] LineNumber The source file line number of the assert macro.\r | |
87 | @param[in] FileName Null-terminated ASCII string of the filename\r | |
88 | executing the assert macro.\r | |
89 | @param[in] Description Null-terminated ASCII string of the expression being\r | |
90 | evaluated.\r | |
91 | \r | |
92 | @retval TRUE Expression is TRUE.\r | |
93 | @retval FALSE Expression is FALSE.\r | |
94 | **/\r | |
95 | BOOLEAN\r | |
96 | EFIAPI\r | |
97 | UnitTestAssertTrue (\r | |
98 | IN BOOLEAN Expression,\r | |
99 | IN CONST CHAR8 *FunctionName,\r | |
100 | IN UINTN LineNumber,\r | |
101 | IN CONST CHAR8 *FileName,\r | |
102 | IN CONST CHAR8 *Description\r | |
103 | )\r | |
104 | {\r | |
105 | if (!Expression) {\r | |
106 | UnitTestLogFailure (\r | |
107 | FAILURETYPE_ASSERTTRUE,\r | |
108 | "%a::%d Expression (%a) is not TRUE!\n",\r | |
109 | FunctionName,\r | |
110 | LineNumber,\r | |
111 | Description\r | |
112 | );\r | |
113 | UT_LOG_ERROR (\r | |
114 | "[ASSERT FAIL] %a::%d Expression (%a) is not TRUE!\n",\r | |
115 | FunctionName,\r | |
116 | LineNumber,\r | |
117 | Description\r | |
118 | );\r | |
119 | }\r | |
120 | return Expression;\r | |
121 | }\r | |
122 | \r | |
123 | /**\r | |
124 | If Expression is FALSE, then TRUE is returned.\r | |
125 | If Expression is TRUE, then an assert is triggered and the location of the\r | |
126 | assert provided by FunctionName, LineNumber, FileName, and Description are\r | |
127 | recorded and FALSE is returned.\r | |
128 | \r | |
129 | @param[in] Expression The BOOLEAN result of the expression evaluation.\r | |
130 | @param[in] FunctionName Null-terminated ASCII string of the function\r | |
131 | executing the assert macro.\r | |
132 | @param[in] LineNumber The source file line number of the assert macro.\r | |
133 | @param[in] FileName Null-terminated ASCII string of the filename\r | |
134 | executing the assert macro.\r | |
135 | @param[in] Description Null-terminated ASCII string of the expression being\r | |
136 | evaluated.\r | |
137 | \r | |
138 | @retval TRUE Expression is FALSE.\r | |
139 | @retval FALSE Expression is TRUE.\r | |
140 | **/\r | |
141 | BOOLEAN\r | |
142 | EFIAPI\r | |
143 | UnitTestAssertFalse (\r | |
144 | IN BOOLEAN Expression,\r | |
145 | IN CONST CHAR8 *FunctionName,\r | |
146 | IN UINTN LineNumber,\r | |
147 | IN CONST CHAR8 *FileName,\r | |
148 | IN CONST CHAR8 *Description\r | |
149 | )\r | |
150 | {\r | |
151 | if (Expression) {\r | |
152 | UnitTestLogFailure (\r | |
153 | FAILURETYPE_ASSERTFALSE,\r | |
154 | "%a::%d Expression(%a) is not FALSE!\n",\r | |
155 | FunctionName,\r | |
156 | LineNumber,\r | |
157 | Description\r | |
158 | );\r | |
159 | UT_LOG_ERROR (\r | |
160 | "[ASSERT FAIL] %a::%d Expression (%a) is not FALSE!\n",\r | |
161 | FunctionName,\r | |
162 | LineNumber,\r | |
163 | Description\r | |
164 | );\r | |
165 | }\r | |
166 | return !Expression;\r | |
167 | }\r | |
168 | \r | |
169 | /**\r | |
170 | If Status is not an EFI_ERROR(), then TRUE is returned.\r | |
171 | If Status is an EFI_ERROR(), then an assert is triggered and the location of\r | |
172 | the assert provided by FunctionName, LineNumber, FileName, and Description are\r | |
173 | recorded and FALSE is returned.\r | |
174 | \r | |
175 | @param[in] Status The EFI_STATUS value to evaluate.\r | |
176 | @param[in] FunctionName Null-terminated ASCII string of the function\r | |
177 | executing the assert macro.\r | |
178 | @param[in] LineNumber The source file line number of the assert macro.\r | |
179 | @param[in] FileName Null-terminated ASCII string of the filename\r | |
180 | executing the assert macro.\r | |
181 | @param[in] Description Null-terminated ASCII string of the status\r | |
182 | expression being evaluated.\r | |
183 | \r | |
184 | @retval TRUE Status is not an EFI_ERROR().\r | |
185 | @retval FALSE Status is an EFI_ERROR().\r | |
186 | **/\r | |
187 | BOOLEAN\r | |
188 | EFIAPI\r | |
189 | UnitTestAssertNotEfiError (\r | |
190 | IN EFI_STATUS Status,\r | |
191 | IN CONST CHAR8 *FunctionName,\r | |
192 | IN UINTN LineNumber,\r | |
193 | IN CONST CHAR8 *FileName,\r | |
194 | IN CONST CHAR8 *Description\r | |
195 | )\r | |
196 | {\r | |
197 | if (EFI_ERROR (Status)) {\r | |
198 | UnitTestLogFailure (\r | |
199 | FAILURETYPE_ASSERTNOTEFIERROR,\r | |
200 | "%a::%d Status '%a' is EFI_ERROR (%r)!\n",\r | |
201 | FunctionName,\r | |
202 | LineNumber,\r | |
203 | Description,\r | |
204 | Status\r | |
205 | );\r | |
206 | UT_LOG_ERROR (\r | |
207 | "[ASSERT FAIL] %a::%d Status '%a' is EFI_ERROR (%r)!\n",\r | |
208 | FunctionName,\r | |
209 | LineNumber,\r | |
210 | Description,\r | |
211 | Status\r | |
212 | );\r | |
213 | }\r | |
214 | return !EFI_ERROR( Status );\r | |
215 | }\r | |
216 | \r | |
217 | /**\r | |
218 | If ValueA is equal ValueB, then TRUE is returned.\r | |
219 | If ValueA is not equal to ValueB, then an assert is triggered and the location\r | |
220 | of the assert provided by FunctionName, LineNumber, FileName, DescriptionA,\r | |
221 | and DescriptionB are recorded and FALSE is returned.\r | |
222 | \r | |
223 | @param[in] ValueA 64-bit value.\r | |
224 | @param[in] ValueB 64-bit value.\r | |
225 | @param[in] FunctionName Null-terminated ASCII string of the function\r | |
226 | executing the assert macro.\r | |
227 | @param[in] LineNumber The source file line number of the assert macro.\r | |
228 | @param[in] FileName Null-terminated ASCII string of the filename\r | |
229 | executing the assert macro.\r | |
230 | @param[in] DescriptionA Null-terminated ASCII string that is a description\r | |
231 | of ValueA.\r | |
232 | @param[in] DescriptionB Null-terminated ASCII string that is a description\r | |
233 | of ValueB.\r | |
234 | \r | |
235 | @retval TRUE ValueA is equal to ValueB.\r | |
236 | @retval FALSE ValueA is not equal to ValueB.\r | |
237 | **/\r | |
238 | BOOLEAN\r | |
239 | EFIAPI\r | |
240 | UnitTestAssertEqual (\r | |
241 | IN UINT64 ValueA,\r | |
242 | IN UINT64 ValueB,\r | |
243 | IN CONST CHAR8 *FunctionName,\r | |
244 | IN UINTN LineNumber,\r | |
245 | IN CONST CHAR8 *FileName,\r | |
246 | IN CONST CHAR8 *DescriptionA,\r | |
247 | IN CONST CHAR8 *DescriptionB\r | |
248 | )\r | |
249 | {\r | |
f76d5016 | 250 | if (ValueA != ValueB) {\r |
0eb52298 MK |
251 | UnitTestLogFailure (\r |
252 | FAILURETYPE_ASSERTEQUAL,\r | |
253 | "%a::%d Value %a != %a (%d != %d)!\n",\r | |
254 | FunctionName,\r | |
255 | LineNumber,\r | |
256 | DescriptionA,\r | |
257 | DescriptionB,\r | |
258 | ValueA,\r | |
259 | ValueB\r | |
260 | );\r | |
261 | UT_LOG_ERROR (\r | |
262 | "[ASSERT FAIL] %a::%d Value %a != %a (%d != %d)!\n",\r | |
263 | FunctionName,\r | |
264 | LineNumber,\r | |
265 | DescriptionA,\r | |
266 | DescriptionB,\r | |
267 | ValueA,\r | |
268 | ValueB\r | |
269 | );\r | |
270 | }\r | |
271 | return (ValueA == ValueB);\r | |
272 | }\r | |
273 | \r | |
274 | /**\r | |
275 | If the contents of BufferA are identical to the contents of BufferB, then TRUE\r | |
276 | is returned. If the contents of BufferA are not identical to the contents of\r | |
277 | BufferB, then an assert is triggered and the location of the assert provided\r | |
278 | by FunctionName, LineNumber, FileName, DescriptionA, and DescriptionB are\r | |
279 | recorded and FALSE is returned.\r | |
280 | \r | |
281 | @param[in] BufferA Pointer to a buffer for comparison.\r | |
282 | @param[in] BufferB Pointer to a buffer for comparison.\r | |
283 | @param[in] Length Number of bytes to compare in BufferA and BufferB.\r | |
284 | @param[in] FunctionName Null-terminated ASCII string of the function\r | |
285 | executing the assert macro.\r | |
286 | @param[in] LineNumber The source file line number of the assert macro.\r | |
287 | @param[in] FileName Null-terminated ASCII string of the filename\r | |
288 | executing the assert macro.\r | |
289 | @param[in] DescriptionA Null-terminated ASCII string that is a description\r | |
290 | of BufferA.\r | |
291 | @param[in] DescriptionB Null-terminated ASCII string that is a description\r | |
292 | of BufferB.\r | |
293 | \r | |
294 | @retval TRUE The contents of BufferA are identical to the contents of\r | |
295 | BufferB.\r | |
296 | @retval FALSE The contents of BufferA are not identical to the contents of\r | |
297 | BufferB.\r | |
298 | **/\r | |
299 | BOOLEAN\r | |
300 | EFIAPI\r | |
301 | UnitTestAssertMemEqual (\r | |
302 | IN VOID *BufferA,\r | |
303 | IN VOID *BufferB,\r | |
304 | IN UINTN Length,\r | |
305 | IN CONST CHAR8 *FunctionName,\r | |
306 | IN UINTN LineNumber,\r | |
307 | IN CONST CHAR8 *FileName,\r | |
308 | IN CONST CHAR8 *DescriptionA,\r | |
309 | IN CONST CHAR8 *DescriptionB\r | |
310 | )\r | |
311 | {\r | |
312 | if (CompareMem(BufferA, BufferB, Length) != 0) {\r | |
313 | UnitTestLogFailure (\r | |
314 | FAILURETYPE_ASSERTEQUAL,\r | |
315 | "%a::%d Memory at %a != %a for length %d bytes!\n",\r | |
316 | FunctionName,\r | |
317 | LineNumber,\r | |
318 | DescriptionA,\r | |
319 | DescriptionB,\r | |
320 | Length\r | |
321 | );\r | |
322 | UT_LOG_ERROR (\r | |
323 | "[ASSERT FAIL] %a::%d Value %a != %a for length %d bytes!\n",\r | |
324 | FunctionName,\r | |
325 | LineNumber,\r | |
326 | DescriptionA,\r | |
327 | DescriptionB,\r | |
328 | Length\r | |
329 | );\r | |
330 | return FALSE;\r | |
331 | }\r | |
332 | return TRUE;\r | |
333 | }\r | |
334 | \r | |
335 | /**\r | |
336 | If ValueA is not equal ValueB, then TRUE is returned.\r | |
337 | If ValueA is equal to ValueB, then an assert is triggered and the location\r | |
338 | of the assert provided by FunctionName, LineNumber, FileName, DescriptionA\r | |
339 | and DescriptionB are recorded and FALSE is returned.\r | |
340 | \r | |
341 | @param[in] ValueA 64-bit value.\r | |
342 | @param[in] ValueB 64-bit value.\r | |
343 | @param[in] FunctionName Null-terminated ASCII string of the function\r | |
344 | executing the assert macro.\r | |
345 | @param[in] LineNumber The source file line number of the assert macro.\r | |
346 | @param[in] FileName Null-terminated ASCII string of the filename\r | |
347 | executing the assert macro.\r | |
348 | @param[in] DescriptionA Null-terminated ASCII string that is a description\r | |
349 | of ValueA.\r | |
350 | @param[in] DescriptionB Null-terminated ASCII string that is a description\r | |
351 | of ValueB.\r | |
352 | \r | |
353 | @retval TRUE ValueA is not equal to ValueB.\r | |
354 | @retval FALSE ValueA is equal to ValueB.\r | |
355 | **/\r | |
356 | BOOLEAN\r | |
357 | EFIAPI\r | |
358 | UnitTestAssertNotEqual (\r | |
359 | IN UINT64 ValueA,\r | |
360 | IN UINT64 ValueB,\r | |
361 | IN CONST CHAR8 *FunctionName,\r | |
362 | IN UINTN LineNumber,\r | |
363 | IN CONST CHAR8 *FileName,\r | |
364 | IN CONST CHAR8 *DescriptionA,\r | |
365 | IN CONST CHAR8 *DescriptionB\r | |
366 | )\r | |
367 | {\r | |
f76d5016 | 368 | if (ValueA == ValueB) {\r |
0eb52298 MK |
369 | UnitTestLogFailure (\r |
370 | FAILURETYPE_ASSERTNOTEQUAL,\r | |
371 | "%a::%d Value %a == %a (%d == %d)!\n",\r | |
372 | FunctionName,\r | |
373 | LineNumber,\r | |
374 | DescriptionA,\r | |
375 | DescriptionB,\r | |
376 | ValueA,\r | |
377 | ValueB\r | |
378 | );\r | |
379 | UT_LOG_ERROR (\r | |
380 | "[ASSERT FAIL] %a::%d Value %a == %a (%d == %d)!\n",\r | |
381 | FunctionName,\r | |
382 | LineNumber,\r | |
383 | DescriptionA,\r | |
384 | DescriptionB,\r | |
385 | ValueA,\r | |
386 | ValueB\r | |
387 | );\r | |
388 | }\r | |
389 | return (ValueA != ValueB);\r | |
390 | }\r | |
391 | \r | |
392 | /**\r | |
393 | If Status is equal to Expected, then TRUE is returned.\r | |
394 | If Status is not equal to Expected, then an assert is triggered and the\r | |
395 | location of the assert provided by FunctionName, LineNumber, FileName, and\r | |
396 | Description are recorded and FALSE is returned.\r | |
397 | \r | |
398 | @param[in] Status EFI_STATUS value returned from an API under test.\r | |
399 | @param[in] Expected The expected EFI_STATUS return value from an API\r | |
400 | under test.\r | |
401 | @param[in] FunctionName Null-terminated ASCII string of the function\r | |
402 | executing the assert macro.\r | |
403 | @param[in] LineNumber The source file line number of the assert macro.\r | |
404 | @param[in] FileName Null-terminated ASCII string of the filename\r | |
405 | executing the assert macro.\r | |
406 | @param[in] Description Null-terminated ASCII string that is a description\r | |
407 | of Status.\r | |
408 | \r | |
409 | @retval TRUE Status is equal to Expected.\r | |
410 | @retval FALSE Status is not equal to Expected.\r | |
411 | **/\r | |
412 | BOOLEAN\r | |
413 | EFIAPI\r | |
414 | UnitTestAssertStatusEqual (\r | |
415 | IN EFI_STATUS Status,\r | |
416 | IN EFI_STATUS Expected,\r | |
417 | IN CONST CHAR8 *FunctionName,\r | |
418 | IN UINTN LineNumber,\r | |
419 | IN CONST CHAR8 *FileName,\r | |
420 | IN CONST CHAR8 *Description\r | |
421 | )\r | |
422 | {\r | |
f76d5016 | 423 | if (Status != Expected) {\r |
0eb52298 MK |
424 | UnitTestLogFailure (\r |
425 | FAILURETYPE_ASSERTSTATUSEQUAL,\r | |
426 | "%a::%d Status '%a' is %r, should be %r!\n",\r | |
427 | FunctionName,\r | |
428 | LineNumber,\r | |
429 | Description,\r | |
430 | Status,\r | |
431 | Expected\r | |
432 | );\r | |
433 | UT_LOG_ERROR (\r | |
434 | "[ASSERT FAIL] %a::%d Status '%a' is %r, should be %r!\n",\r | |
435 | FunctionName,\r | |
436 | LineNumber,\r | |
437 | Description,\r | |
438 | Status,\r | |
439 | Expected\r | |
440 | );\r | |
441 | }\r | |
442 | return (Status == Expected);\r | |
443 | }\r | |
444 | \r | |
445 | /**\r | |
446 | If Pointer is not equal to NULL, then TRUE is returned.\r | |
447 | If Pointer is equal to NULL, then an assert is triggered and the location of\r | |
448 | the assert provided by FunctionName, LineNumber, FileName, and PointerName\r | |
449 | are recorded and FALSE is returned.\r | |
450 | \r | |
451 | @param[in] Pointer Pointer value to be checked against NULL.\r | |
452 | @param[in] Expected The expected EFI_STATUS return value from a function\r | |
453 | under test.\r | |
454 | @param[in] FunctionName Null-terminated ASCII string of the function\r | |
455 | executing the assert macro.\r | |
456 | @param[in] LineNumber The source file line number of the assert macro.\r | |
457 | @param[in] FileName Null-terminated ASCII string of the filename\r | |
458 | executing the assert macro.\r | |
459 | @param[in] PointerName Null-terminated ASCII string that is a description\r | |
460 | of Pointer.\r | |
461 | \r | |
462 | @retval TRUE Pointer is not equal to NULL.\r | |
463 | @retval FALSE Pointer is equal to NULL.\r | |
464 | **/\r | |
465 | BOOLEAN\r | |
466 | EFIAPI\r | |
467 | UnitTestAssertNotNull (\r | |
468 | IN VOID *Pointer,\r | |
469 | IN CONST CHAR8 *FunctionName,\r | |
470 | IN UINTN LineNumber,\r | |
471 | IN CONST CHAR8 *FileName,\r | |
472 | IN CONST CHAR8 *PointerName\r | |
473 | )\r | |
474 | {\r | |
475 | if (Pointer == NULL) {\r | |
476 | UnitTestLogFailure (\r | |
477 | FAILURETYPE_ASSERTNOTNULL,\r | |
478 | "%a::%d Pointer (%a) is NULL!\n",\r | |
479 | FunctionName,\r | |
480 | LineNumber,\r | |
481 | PointerName\r | |
482 | );\r | |
483 | UT_LOG_ERROR (\r | |
484 | "[ASSERT FAIL] %a::%d Pointer (%a) is NULL!\n",\r | |
485 | FunctionName,\r | |
486 | LineNumber,\r | |
487 | PointerName\r | |
488 | );\r | |
489 | }\r | |
490 | return (Pointer != NULL);\r | |
491 | }\r |