]> git.proxmox.com Git - mirror_edk2.git/blob - UnitTestFrameworkPkg/Library/UnitTestLib/UnitTestLib.c
UnitTestFrameworkPkg: Modify APIs in UnitTestPersistenceLib
[mirror_edk2.git] / UnitTestFrameworkPkg / Library / UnitTestLib / UnitTestLib.c
1 /**
2 Implement UnitTestLib
3
4 Copyright (c) Microsoft Corporation.
5 Copyright (c) 2022, Intel Corporation. All rights reserved.<BR>
6 SPDX-License-Identifier: BSD-2-Clause-Patent
7 **/
8
9 #include <Uefi.h>
10 #include <Library/UnitTestLib.h>
11 #include <Library/BaseLib.h>
12 #include <Library/BaseMemoryLib.h>
13 #include <Library/MemoryAllocationLib.h>
14 #include <Library/DebugLib.h>
15 #include <Library/UnitTestPersistenceLib.h>
16 #include <Library/UnitTestResultReportLib.h>
17
18 ///
19 /// Forward declaration of prototype
20 ///
21 STATIC
22 VOID
23 UpdateTestFromSave (
24 IN OUT UNIT_TEST *Test,
25 IN UNIT_TEST_SAVE_HEADER *SavedState
26 );
27
28 /**
29 This function will determine whether the short name violates any rules that would
30 prevent it from being used as a reporting name or as a serialization name.
31
32 Example: If the name cannot be serialized to a filesystem file name.
33
34 @param[in] ShortTitleString A pointer to the short title string to be evaluated.
35
36 @retval TRUE The string is acceptable.
37 @retval FALSE The string should not be used.
38
39 **/
40 STATIC
41 BOOLEAN
42 IsFrameworkShortNameValid (
43 IN CHAR8 *ShortTitleString
44 )
45 {
46 // TODO: Finish this function.
47 return TRUE;
48 }
49
50 STATIC
51 CHAR8 *
52 AllocateAndCopyString (
53 IN CHAR8 *StringToCopy
54 )
55 {
56 CHAR8 *NewString;
57 UINTN NewStringLength;
58
59 NewString = NULL;
60 NewStringLength = AsciiStrnLenS (StringToCopy, UNIT_TEST_MAX_STRING_LENGTH) + 1;
61 NewString = AllocatePool (NewStringLength * sizeof (CHAR8));
62 if (NewString != NULL) {
63 AsciiStrCpyS (NewString, NewStringLength, StringToCopy);
64 }
65
66 return NewString;
67 }
68
69 STATIC
70 VOID
71 SetFrameworkFingerprint (
72 OUT UINT8 *Fingerprint,
73 IN UNIT_TEST_FRAMEWORK *Framework
74 )
75 {
76 UINT32 NewFingerprint;
77
78 // For this one we'll just use the title and version as the unique fingerprint.
79 NewFingerprint = CalculateCrc32 (Framework->Title, (AsciiStrLen (Framework->Title) * sizeof (CHAR8)));
80 NewFingerprint = (NewFingerprint >> 8) ^ CalculateCrc32 (Framework->VersionString, (AsciiStrLen (Framework->VersionString) * sizeof (CHAR8)));
81
82 CopyMem (Fingerprint, &NewFingerprint, UNIT_TEST_FINGERPRINT_SIZE);
83 return;
84 }
85
86 STATIC
87 VOID
88 SetSuiteFingerprint (
89 OUT UINT8 *Fingerprint,
90 IN UNIT_TEST_FRAMEWORK *Framework,
91 IN UNIT_TEST_SUITE *Suite
92 )
93 {
94 UINT32 NewFingerprint;
95
96 // For this one, we'll use the fingerprint from the framework, and the title of the suite.
97 NewFingerprint = CalculateCrc32 (&Framework->Fingerprint[0], UNIT_TEST_FINGERPRINT_SIZE);
98 NewFingerprint = (NewFingerprint >> 8) ^ CalculateCrc32 (Suite->Title, (AsciiStrLen (Suite->Title) * sizeof (CHAR8)));
99 NewFingerprint = (NewFingerprint >> 8) ^ CalculateCrc32 (Suite->Name, (AsciiStrLen (Suite->Name) * sizeof (CHAR8)));
100
101 CopyMem (Fingerprint, &NewFingerprint, UNIT_TEST_FINGERPRINT_SIZE);
102 return;
103 }
104
105 STATIC
106 VOID
107 SetTestFingerprint (
108 OUT UINT8 *Fingerprint,
109 IN UNIT_TEST_SUITE *Suite,
110 IN UNIT_TEST *Test
111 )
112 {
113 UINT32 NewFingerprint;
114
115 // For this one, we'll use the fingerprint from the suite, and the description and classname of the test.
116 NewFingerprint = CalculateCrc32 (&Suite->Fingerprint[0], UNIT_TEST_FINGERPRINT_SIZE);
117 NewFingerprint = (NewFingerprint >> 8) ^ CalculateCrc32 (Test->Description, (AsciiStrLen (Test->Description) * sizeof (CHAR8)));
118 NewFingerprint = (NewFingerprint >> 8) ^ CalculateCrc32 (Test->Name, (AsciiStrLen (Test->Name) * sizeof (CHAR8)));
119
120 CopyMem (Fingerprint, &NewFingerprint, UNIT_TEST_FINGERPRINT_SIZE);
121 return;
122 }
123
124 STATIC
125 BOOLEAN
126 CompareFingerprints (
127 IN UINT8 *FingerprintA,
128 IN UINT8 *FingerprintB
129 )
130 {
131 return (CompareMem (FingerprintA, FingerprintB, UNIT_TEST_FINGERPRINT_SIZE) == 0);
132 }
133
134 /**
135 Cleanup a test framework.
136
137 After tests are run, this will teardown the entire framework and free all
138 allocated data within.
139
140 @param[in] FrameworkHandle A handle to the current running framework that
141 dispatched the test. Necessary for recording
142 certain test events with the framework.
143
144 @retval EFI_SUCCESS All resources associated with framework were
145 freed.
146 @retval EFI_INVALID_PARAMETER FrameworkHandle is NULL.
147 **/
148 EFI_STATUS
149 EFIAPI
150 FreeUnitTestFramework (
151 IN UNIT_TEST_FRAMEWORK_HANDLE FrameworkHandle
152 )
153 {
154 // TODO: Finish this function.
155 return EFI_SUCCESS;
156 }
157
158 STATIC
159 EFI_STATUS
160 FreeUnitTestSuiteEntry (
161 IN UNIT_TEST_SUITE_LIST_ENTRY *SuiteEntry
162 )
163 {
164 // TODO: Finish this function.
165 return EFI_SUCCESS;
166 }
167
168 STATIC
169 EFI_STATUS
170 FreeUnitTestTestEntry (
171 IN UNIT_TEST_LIST_ENTRY *TestEntry
172 )
173 {
174 // TODO: Finish this function.
175 return EFI_SUCCESS;
176 }
177
178 /**
179 Method to Initialize the Unit Test framework. This function registers the
180 test name and also initializes the internal state of the test framework to
181 receive any new suites and tests.
182
183 @param[out] FrameworkHandle Unit test framework to be created.
184 @param[in] Title Null-terminated ASCII string that is the user
185 friendly name of the framework. String is
186 copied.
187 @param[in] ShortTitle Null-terminated ASCII short string that is the
188 short name of the framework with no spaces.
189 String is copied.
190 @param[in] VersionString Null-terminated ASCII version string for the
191 framework. String is copied.
192
193 @retval EFI_SUCCESS The unit test framework was initialized.
194 @retval EFI_INVALID_PARAMETER FrameworkHandle is NULL.
195 @retval EFI_INVALID_PARAMETER Title is NULL.
196 @retval EFI_INVALID_PARAMETER ShortTitle is NULL.
197 @retval EFI_INVALID_PARAMETER VersionString is NULL.
198 @retval EFI_INVALID_PARAMETER ShortTitle is invalid.
199 @retval EFI_OUT_OF_RESOURCES There are not enough resources available to
200 initialize the unit test framework.
201 **/
202 EFI_STATUS
203 EFIAPI
204 InitUnitTestFramework (
205 OUT UNIT_TEST_FRAMEWORK_HANDLE *FrameworkHandle,
206 IN CHAR8 *Title,
207 IN CHAR8 *ShortTitle,
208 IN CHAR8 *VersionString
209 )
210 {
211 EFI_STATUS Status;
212 UNIT_TEST_FRAMEWORK_HANDLE NewFrameworkHandle;
213 UNIT_TEST_FRAMEWORK *NewFramework;
214 UINTN SaveStateSize;
215
216 Status = EFI_SUCCESS;
217 NewFramework = NULL;
218
219 //
220 // First, check all pointers and make sure nothing's broked.
221 //
222 if ((FrameworkHandle == NULL) || (Title == NULL) ||
223 (ShortTitle == NULL) || (VersionString == NULL))
224 {
225 return EFI_INVALID_PARAMETER;
226 }
227
228 //
229 // Next, determine whether all of the strings are good to use.
230 //
231 if (!IsFrameworkShortNameValid (ShortTitle)) {
232 return EFI_INVALID_PARAMETER;
233 }
234
235 //
236 // Next, set aside some space to start messing with the framework.
237 //
238 NewFramework = AllocateZeroPool (sizeof (UNIT_TEST_FRAMEWORK));
239 if (NewFramework == NULL) {
240 return EFI_OUT_OF_RESOURCES;
241 }
242
243 //
244 // Next, set up all the test data.
245 //
246 NewFrameworkHandle = (UNIT_TEST_FRAMEWORK_HANDLE)NewFramework;
247 NewFramework->Title = AllocateAndCopyString (Title);
248 NewFramework->ShortTitle = AllocateAndCopyString (ShortTitle);
249 NewFramework->VersionString = AllocateAndCopyString (VersionString);
250 NewFramework->Log = NULL;
251 NewFramework->CurrentTest = NULL;
252 NewFramework->SavedState = NULL;
253 if ((NewFramework->Title == NULL) ||
254 (NewFramework->ShortTitle == NULL) ||
255 (NewFramework->VersionString == NULL))
256 {
257 Status = EFI_OUT_OF_RESOURCES;
258 goto Exit;
259 }
260
261 InitializeListHead (&(NewFramework->TestSuiteList));
262
263 //
264 // Create the framework fingerprint.
265 //
266 SetFrameworkFingerprint (&NewFramework->Fingerprint[0], NewFramework);
267
268 //
269 // If there is a persisted context, load it now.
270 //
271 if (DoesCacheExist (NewFrameworkHandle)) {
272 Status = LoadUnitTestCache (NewFrameworkHandle, (VOID **)(&NewFramework->SavedState), &SaveStateSize);
273 if (EFI_ERROR (Status)) {
274 //
275 // Don't actually report it as an error, but emit a warning.
276 //
277 DEBUG ((DEBUG_ERROR, "%a - Cache was detected, but failed to load.\n", __FUNCTION__));
278 Status = EFI_SUCCESS;
279 }
280 }
281
282 Exit:
283 //
284 // If we're good, then let's copy the framework.
285 //
286 if (!EFI_ERROR (Status)) {
287 *FrameworkHandle = NewFrameworkHandle;
288 } else {
289 //
290 // Otherwise, we need to undo this horrible thing that we've done.
291 //
292 FreeUnitTestFramework (NewFrameworkHandle);
293 }
294
295 return Status;
296 }
297
298 /**
299 Registers a Unit Test Suite in the Unit Test Framework.
300 At least one test suite must be registered, because all test cases must be
301 within a unit test suite.
302
303 @param[out] SuiteHandle Unit test suite to create
304 @param[in] FrameworkHandle Unit test framework to add unit test suite to
305 @param[in] Title Null-terminated ASCII string that is the user
306 friendly name of the test suite. String is
307 copied.
308 @param[in] Name Null-terminated ASCII string that is the short
309 name of the test suite with no spaces. String
310 is copied.
311 @param[in] Setup Setup function, runs before suite. This is an
312 optional parameter that may be NULL.
313 @param[in] Teardown Teardown function, runs after suite. This is an
314 optional parameter that may be NULL.
315
316 @retval EFI_SUCCESS The unit test suite was created.
317 @retval EFI_INVALID_PARAMETER SuiteHandle is NULL.
318 @retval EFI_INVALID_PARAMETER FrameworkHandle is NULL.
319 @retval EFI_INVALID_PARAMETER Title is NULL.
320 @retval EFI_INVALID_PARAMETER Name is NULL.
321 @retval EFI_OUT_OF_RESOURCES There are not enough resources available to
322 initialize the unit test suite.
323 **/
324 EFI_STATUS
325 EFIAPI
326 CreateUnitTestSuite (
327 OUT UNIT_TEST_SUITE_HANDLE *SuiteHandle,
328 IN UNIT_TEST_FRAMEWORK_HANDLE FrameworkHandle,
329 IN CHAR8 *Title,
330 IN CHAR8 *Name,
331 IN UNIT_TEST_SUITE_SETUP Setup OPTIONAL,
332 IN UNIT_TEST_SUITE_TEARDOWN Teardown OPTIONAL
333 )
334 {
335 EFI_STATUS Status;
336 UNIT_TEST_SUITE_LIST_ENTRY *NewSuiteEntry;
337 UNIT_TEST_FRAMEWORK *Framework;
338
339 Status = EFI_SUCCESS;
340 Framework = (UNIT_TEST_FRAMEWORK *)FrameworkHandle;
341
342 //
343 // First, let's check to make sure that our parameters look good.
344 //
345 if ((SuiteHandle == NULL) || (Framework == NULL) || (Title == NULL) || (Name == NULL)) {
346 return EFI_INVALID_PARAMETER;
347 }
348
349 //
350 // Create the new entry.
351 //
352 NewSuiteEntry = AllocateZeroPool (sizeof (UNIT_TEST_SUITE_LIST_ENTRY));
353 if (NewSuiteEntry == NULL) {
354 return EFI_OUT_OF_RESOURCES;
355 }
356
357 //
358 // Copy the fields we think we need.
359 //
360 NewSuiteEntry->UTS.NumTests = 0;
361 NewSuiteEntry->UTS.Title = AllocateAndCopyString (Title);
362 NewSuiteEntry->UTS.Name = AllocateAndCopyString (Name);
363 NewSuiteEntry->UTS.Setup = Setup;
364 NewSuiteEntry->UTS.Teardown = Teardown;
365 NewSuiteEntry->UTS.ParentFramework = FrameworkHandle;
366 InitializeListHead (&(NewSuiteEntry->Entry)); // List entry for sibling suites.
367 InitializeListHead (&(NewSuiteEntry->UTS.TestCaseList)); // List entry for child tests.
368 if (NewSuiteEntry->UTS.Title == NULL) {
369 Status = EFI_OUT_OF_RESOURCES;
370 goto Exit;
371 }
372
373 if (NewSuiteEntry->UTS.Name == NULL) {
374 Status = EFI_OUT_OF_RESOURCES;
375 goto Exit;
376 }
377
378 //
379 // Create the suite fingerprint.
380 //
381 SetSuiteFingerprint (&NewSuiteEntry->UTS.Fingerprint[0], Framework, &NewSuiteEntry->UTS);
382
383 Exit:
384 //
385 // If everything is going well, add the new suite to the tail list for the framework.
386 //
387 if (!EFI_ERROR (Status)) {
388 InsertTailList (&(Framework->TestSuiteList), (LIST_ENTRY *)NewSuiteEntry);
389 *SuiteHandle = (UNIT_TEST_SUITE_HANDLE)(&NewSuiteEntry->UTS);
390 } else {
391 //
392 // Otherwise, make with the destruction.
393 //
394 FreeUnitTestSuiteEntry (NewSuiteEntry);
395 }
396
397 return Status;
398 }
399
400 /**
401 Adds test case to Suite
402
403 @param[in] SuiteHandle Unit test suite to add test to.
404 @param[in] Description Null-terminated ASCII string that is the user
405 friendly description of a test. String is copied.
406 @param[in] Name Null-terminated ASCII string that is the short name
407 of the test with no spaces. String is copied.
408 @param[in] Function Unit test function.
409 @param[in] Prerequisite Prerequisite function, runs before test. This is
410 an optional parameter that may be NULL.
411 @param[in] CleanUp Clean up function, runs after test. This is an
412 optional parameter that may be NULL.
413 @param[in] Context Pointer to context. This is an optional parameter
414 that may be NULL.
415
416 @retval EFI_SUCCESS The unit test case was added to Suite.
417 @retval EFI_INVALID_PARAMETER SuiteHandle is NULL.
418 @retval EFI_INVALID_PARAMETER Description is NULL.
419 @retval EFI_INVALID_PARAMETER Name is NULL.
420 @retval EFI_INVALID_PARAMETER Function is NULL.
421 @retval EFI_OUT_OF_RESOURCES There are not enough resources available to
422 add the unit test case to Suite.
423 **/
424 EFI_STATUS
425 EFIAPI
426 AddTestCase (
427 IN UNIT_TEST_SUITE_HANDLE SuiteHandle,
428 IN CHAR8 *Description,
429 IN CHAR8 *Name,
430 IN UNIT_TEST_FUNCTION Function,
431 IN UNIT_TEST_PREREQUISITE Prerequisite OPTIONAL,
432 IN UNIT_TEST_CLEANUP CleanUp OPTIONAL,
433 IN UNIT_TEST_CONTEXT Context OPTIONAL
434 )
435 {
436 EFI_STATUS Status;
437 UNIT_TEST_LIST_ENTRY *NewTestEntry;
438 UNIT_TEST_FRAMEWORK *ParentFramework;
439 UNIT_TEST_SUITE *Suite;
440
441 Status = EFI_SUCCESS;
442 Suite = (UNIT_TEST_SUITE *)SuiteHandle;
443
444 //
445 // First, let's check to make sure that our parameters look good.
446 //
447 if ((Suite == NULL) || (Description == NULL) || (Name == NULL) || (Function == NULL)) {
448 return EFI_INVALID_PARAMETER;
449 }
450
451 ParentFramework = (UNIT_TEST_FRAMEWORK *)Suite->ParentFramework;
452 //
453 // Create the new entry.
454 NewTestEntry = AllocateZeroPool (sizeof (UNIT_TEST_LIST_ENTRY));
455 if (NewTestEntry == NULL) {
456 return EFI_OUT_OF_RESOURCES;
457 }
458
459 //
460 // Copy the fields we think we need.
461 NewTestEntry->UT.Description = AllocateAndCopyString (Description);
462 NewTestEntry->UT.Name = AllocateAndCopyString (Name);
463 NewTestEntry->UT.FailureType = FAILURETYPE_NOFAILURE;
464 NewTestEntry->UT.FailureMessage[0] = '\0';
465 NewTestEntry->UT.Log = NULL;
466 NewTestEntry->UT.Prerequisite = Prerequisite;
467 NewTestEntry->UT.CleanUp = CleanUp;
468 NewTestEntry->UT.RunTest = Function;
469 NewTestEntry->UT.Context = Context;
470 NewTestEntry->UT.Result = UNIT_TEST_PENDING;
471 NewTestEntry->UT.ParentSuite = SuiteHandle;
472 InitializeListHead (&(NewTestEntry->Entry)); // List entry for sibling tests.
473 if (NewTestEntry->UT.Description == NULL) {
474 Status = EFI_OUT_OF_RESOURCES;
475 goto Exit;
476 }
477
478 if (NewTestEntry->UT.Name == NULL) {
479 Status = EFI_OUT_OF_RESOURCES;
480 goto Exit;
481 }
482
483 //
484 // Create the test fingerprint.
485 //
486 SetTestFingerprint (&NewTestEntry->UT.Fingerprint[0], Suite, &NewTestEntry->UT);
487
488 // TODO: Make sure that duplicate fingerprints cannot be created.
489
490 //
491 // If there is saved test data, update this record.
492 //
493 if (ParentFramework->SavedState != NULL) {
494 UpdateTestFromSave (&NewTestEntry->UT, ParentFramework->SavedState);
495 }
496
497 Exit:
498 //
499 // If everything is going well, add the new suite to the tail list for the framework.
500 //
501 if (!EFI_ERROR (Status)) {
502 InsertTailList (&(Suite->TestCaseList), (LIST_ENTRY *)NewTestEntry);
503 Suite->NumTests++;
504 } else {
505 //
506 // Otherwise, make with the destruction.
507 //
508 FreeUnitTestTestEntry (NewTestEntry);
509 }
510
511 return Status;
512 }
513
514 STATIC
515 VOID
516 UpdateTestFromSave (
517 IN OUT UNIT_TEST *Test,
518 IN UNIT_TEST_SAVE_HEADER *SavedState
519 )
520 {
521 UNIT_TEST_SAVE_TEST *CurrentTest;
522 UNIT_TEST_SAVE_TEST *MatchingTest;
523 UINT8 *FloatingPointer;
524 UNIT_TEST_SAVE_CONTEXT *SavedContext;
525 UINTN Index;
526
527 //
528 // First, evaluate the inputs.
529 //
530 if ((Test == NULL) || (SavedState == NULL)) {
531 return;
532 }
533
534 if (SavedState->TestCount == 0) {
535 return;
536 }
537
538 //
539 // Next, determine whether a matching test can be found.
540 // Start at the beginning.
541 //
542 MatchingTest = NULL;
543 FloatingPointer = (UINT8 *)SavedState + sizeof (*SavedState);
544 for (Index = 0; Index < SavedState->TestCount; Index++) {
545 CurrentTest = (UNIT_TEST_SAVE_TEST *)FloatingPointer;
546 if (CompareFingerprints (&Test->Fingerprint[0], &CurrentTest->Fingerprint[0])) {
547 MatchingTest = CurrentTest;
548 //
549 // If there's a saved context, it's important that we iterate through the entire list.
550 //
551 if (!SavedState->HasSavedContext) {
552 break;
553 }
554 }
555
556 //
557 // If we didn't find it, we have to increment to the next test.
558 //
559 FloatingPointer = (UINT8 *)CurrentTest + CurrentTest->Size;
560 }
561
562 //
563 // If a matching test was found, copy the status.
564 //
565 if (MatchingTest) {
566 //
567 // Override the test status with the saved status.
568 //
569 Test->Result = MatchingTest->Result;
570
571 Test->FailureType = MatchingTest->FailureType;
572 AsciiStrnCpyS (
573 &Test->FailureMessage[0],
574 UNIT_TEST_TESTFAILUREMSG_LENGTH,
575 &MatchingTest->FailureMessage[0],
576 UNIT_TEST_TESTFAILUREMSG_LENGTH
577 );
578
579 //
580 // If there is a log string associated, grab that.
581 // We can tell that there's a log string because the "size" will be larger than
582 // the structure size.
583 // IMPORTANT NOTE: There are security implications here.
584 // This data is user-supplied and we're about to play kinda
585 // fast and loose with data buffers.
586 //
587 if (MatchingTest->Size > sizeof (UNIT_TEST_SAVE_TEST)) {
588 UnitTestLogInit (Test, (UINT8 *)MatchingTest->Log, MatchingTest->Size - sizeof (UNIT_TEST_SAVE_TEST));
589 }
590 }
591
592 //
593 // If the saved context exists and matches this test, grab it, too.
594 //
595 if (SavedState->HasSavedContext) {
596 //
597 // If there was a saved context, the "matching test" loop will have placed the FloatingPointer
598 // at the beginning of the context structure.
599 //
600 SavedContext = (UNIT_TEST_SAVE_CONTEXT *)FloatingPointer;
601 if (((SavedContext->Size - sizeof (UNIT_TEST_SAVE_CONTEXT)) > 0) &&
602 CompareFingerprints (&Test->Fingerprint[0], &SavedContext->Fingerprint[0]))
603 {
604 //
605 // Override the test context with the saved context.
606 //
607 Test->Context = (VOID *)SavedContext->Data;
608 }
609 }
610 }
611
612 STATIC
613 UNIT_TEST_SAVE_HEADER *
614 SerializeState (
615 IN UNIT_TEST_FRAMEWORK_HANDLE FrameworkHandle,
616 IN UNIT_TEST_CONTEXT ContextToSave OPTIONAL,
617 IN UINTN ContextToSaveSize
618 )
619 {
620 UNIT_TEST_FRAMEWORK *Framework;
621 UNIT_TEST_SAVE_HEADER *Header;
622 LIST_ENTRY *SuiteListHead;
623 LIST_ENTRY *Suite;
624 LIST_ENTRY *TestListHead;
625 LIST_ENTRY *Test;
626 UINT32 TestCount;
627 UINT32 TotalSize;
628 UINTN LogSize;
629 UNIT_TEST_SAVE_TEST *TestSaveData;
630 UNIT_TEST_SAVE_CONTEXT *TestSaveContext;
631 UNIT_TEST *UnitTest;
632 UINT8 *FloatingPointer;
633
634 Framework = (UNIT_TEST_FRAMEWORK *)FrameworkHandle;
635 Header = NULL;
636
637 //
638 // First, let's not make assumptions about the parameters.
639 //
640 if ((Framework == NULL) ||
641 ((ContextToSave != NULL) && (ContextToSaveSize == 0)) ||
642 (ContextToSaveSize > MAX_UINT32))
643 {
644 return NULL;
645 }
646
647 //
648 // Next, we've gotta figure out the resources that will be required to serialize the
649 // the framework state so that we can persist it.
650 // To start with, we're gonna need a header.
651 //
652 TotalSize = sizeof (UNIT_TEST_SAVE_HEADER);
653 //
654 // Now we need to figure out how many tests there are.
655 //
656 TestCount = 0;
657 //
658 // Iterate all suites.
659 //
660 SuiteListHead = &Framework->TestSuiteList;
661 for (Suite = GetFirstNode (SuiteListHead); Suite != SuiteListHead; Suite = GetNextNode (SuiteListHead, Suite)) {
662 //
663 // Iterate all tests within the suite.
664 //
665 TestListHead = &((UNIT_TEST_SUITE_LIST_ENTRY *)Suite)->UTS.TestCaseList;
666 for (Test = GetFirstNode (TestListHead); Test != TestListHead; Test = GetNextNode (TestListHead, Test)) {
667 UnitTest = &((UNIT_TEST_LIST_ENTRY *)Test)->UT;
668 //
669 // Account for the size of a test structure.
670 //
671 TotalSize += sizeof (UNIT_TEST_SAVE_TEST);
672 //
673 // If there's a log, make sure to account for the log size.
674 //
675 if (UnitTest->Log != NULL) {
676 //
677 // The +1 is for the NULL character. Can't forget the NULL character.
678 //
679 LogSize = (AsciiStrLen (UnitTest->Log) + 1) * sizeof (CHAR8);
680 ASSERT (LogSize < MAX_UINT32);
681 TotalSize += (UINT32)LogSize;
682 }
683
684 //
685 // Increment the test count.
686 //
687 TestCount++;
688 }
689 }
690
691 //
692 // If there are no tests, we're done here.
693 //
694 if (TestCount == 0) {
695 return NULL;
696 }
697
698 //
699 // Add room for the context, if there is one.
700 //
701 if (ContextToSave != NULL) {
702 TotalSize += sizeof (UNIT_TEST_SAVE_CONTEXT) + (UINT32)ContextToSaveSize;
703 }
704
705 //
706 // Now that we know the size, we need to allocate space for the serialized output.
707 //
708 Header = AllocateZeroPool (TotalSize);
709 if (Header == NULL) {
710 return NULL;
711 }
712
713 //
714 // Alright, let's start setting up some data.
715 //
716 Header->Version = UNIT_TEST_PERSISTENCE_LIB_VERSION;
717 Header->SaveStateSize = TotalSize;
718 CopyMem (&Header->Fingerprint[0], &Framework->Fingerprint[0], UNIT_TEST_FINGERPRINT_SIZE);
719 CopyMem (&Header->StartTime, &Framework->StartTime, sizeof (EFI_TIME));
720 Header->TestCount = TestCount;
721 Header->HasSavedContext = FALSE;
722
723 //
724 // Start adding all of the test cases.
725 // Set the floating pointer to the start of the current test save buffer.
726 //
727 FloatingPointer = (UINT8 *)Header + sizeof (UNIT_TEST_SAVE_HEADER);
728 //
729 // Iterate all suites.
730 //
731 SuiteListHead = &Framework->TestSuiteList;
732 for (Suite = GetFirstNode (SuiteListHead); Suite != SuiteListHead; Suite = GetNextNode (SuiteListHead, Suite)) {
733 //
734 // Iterate all tests within the suite.
735 //
736 TestListHead = &((UNIT_TEST_SUITE_LIST_ENTRY *)Suite)->UTS.TestCaseList;
737 for (Test = GetFirstNode (TestListHead); Test != TestListHead; Test = GetNextNode (TestListHead, Test)) {
738 TestSaveData = (UNIT_TEST_SAVE_TEST *)FloatingPointer;
739 UnitTest = &((UNIT_TEST_LIST_ENTRY *)Test)->UT;
740
741 //
742 // Save the fingerprint.
743 //
744 CopyMem (&TestSaveData->Fingerprint[0], &UnitTest->Fingerprint[0], UNIT_TEST_FINGERPRINT_SIZE);
745
746 //
747 // Save the result.
748 //
749 TestSaveData->Result = UnitTest->Result;
750 TestSaveData->FailureType = UnitTest->FailureType;
751 AsciiStrnCpyS (&TestSaveData->FailureMessage[0], UNIT_TEST_TESTFAILUREMSG_LENGTH, &UnitTest->FailureMessage[0], UNIT_TEST_TESTFAILUREMSG_LENGTH);
752
753 //
754 // If there is a log, save the log.
755 //
756 FloatingPointer += sizeof (UNIT_TEST_SAVE_TEST);
757 if (UnitTest->Log != NULL) {
758 //
759 // The +1 is for the NULL character. Can't forget the NULL character.
760 //
761 LogSize = (AsciiStrLen (UnitTest->Log) + 1) * sizeof (CHAR8);
762 CopyMem (FloatingPointer, UnitTest->Log, LogSize);
763 FloatingPointer += LogSize;
764 }
765
766 //
767 // Update the size once the structure is complete.
768 // NOTE: Should this be a straight cast without validation?
769 //
770 TestSaveData->Size = (UINT32)(FloatingPointer - (UINT8 *)TestSaveData);
771 }
772 }
773
774 //
775 // If there is a context to save, let's do that now.
776 //
777 if ((ContextToSave != NULL) && (Framework->CurrentTest != NULL)) {
778 TestSaveContext = (UNIT_TEST_SAVE_CONTEXT *)FloatingPointer;
779 TestSaveContext->Size = (UINT32)ContextToSaveSize + sizeof (UNIT_TEST_SAVE_CONTEXT);
780 CopyMem (&TestSaveContext->Fingerprint[0], &Framework->CurrentTest->Fingerprint[0], UNIT_TEST_FINGERPRINT_SIZE);
781 CopyMem (((UINT8 *)TestSaveContext + sizeof (UNIT_TEST_SAVE_CONTEXT)), ContextToSave, ContextToSaveSize);
782 Header->HasSavedContext = TRUE;
783 }
784
785 return Header;
786 }
787
788 /**
789 Leverages a framework-specific mechanism (see UnitTestPersistenceLib if you're
790 a framework author) to save the state of the executing framework along with
791 any allocated data so that the test may be resumed upon reentry. A test case
792 should pass any needed context (which, to prevent an infinite loop, should be
793 at least the current execution count) which will be saved by the framework and
794 passed to the test case upon resume.
795
796 This should be called while the current test framework is valid and active. It is
797 generally called from within a test case prior to quitting or rebooting.
798
799 @param[in] ContextToSave A buffer of test case-specific data to be saved
800 along with framework state. Will be passed as
801 "Context" to the test case upon resume. This
802 is an optional parameter that may be NULL.
803 @param[in] ContextToSaveSize Size of the ContextToSave buffer.
804
805 @retval EFI_SUCCESS The framework state and context were saved.
806 @retval EFI_NOT_FOUND An active framework handle was not found.
807 @retval EFI_INVALID_PARAMETER ContextToSave is not NULL and
808 ContextToSaveSize is 0.
809 @retval EFI_INVALID_PARAMETER ContextToSave is >= 4GB.
810 @retval EFI_OUT_OF_RESOURCES There are not enough resources available to
811 save the framework and context state.
812 @retval EFI_DEVICE_ERROR The framework and context state could not be
813 saved to a persistent storage device due to a
814 device error.
815 **/
816 EFI_STATUS
817 EFIAPI
818 SaveFrameworkState (
819 IN UNIT_TEST_CONTEXT ContextToSave OPTIONAL,
820 IN UINTN ContextToSaveSize
821 )
822 {
823 EFI_STATUS Status;
824 UNIT_TEST_FRAMEWORK_HANDLE FrameworkHandle;
825 UNIT_TEST_SAVE_HEADER *Header;
826
827 Header = NULL;
828 FrameworkHandle = GetActiveFrameworkHandle ();
829
830 //
831 // Return a unique error code if the framework is not set.
832 //
833 if (FrameworkHandle == NULL) {
834 return EFI_NOT_FOUND;
835 }
836
837 //
838 // First, let's not make assumptions about the parameters.
839 //
840 if (((ContextToSave != NULL) && (ContextToSaveSize == 0)) ||
841 (ContextToSaveSize > MAX_UINT32))
842 {
843 return EFI_INVALID_PARAMETER;
844 }
845
846 //
847 // Now, let's package up all the data for saving.
848 //
849 Header = SerializeState (FrameworkHandle, ContextToSave, ContextToSaveSize);
850 if (Header == NULL) {
851 return EFI_OUT_OF_RESOURCES;
852 }
853
854 //
855 // All that should be left to do is save it using the associated persistence lib.
856 //
857 Status = SaveUnitTestCache (FrameworkHandle, Header, Header->SaveStateSize);
858 if (EFI_ERROR (Status)) {
859 DEBUG ((DEBUG_ERROR, "%a - Could not save state! %r\n", __FUNCTION__, Status));
860 Status = EFI_DEVICE_ERROR;
861 }
862
863 //
864 // Free data that was used.
865 //
866 FreePool (Header);
867
868 return Status;
869 }