2 This is an instance of the Unit Test Persistence Lib that will utilize
3 the filesystem that a test application is running from to save a serialized
4 version of the internal test state in case the test needs to quit and restore.
6 Copyright (c) Microsoft Corporation.<BR>
7 SPDX-License-Identifier: BSD-2-Clause-Patent
11 #include <Library/UnitTestPersistenceLib.h>
12 #include <Library/BaseLib.h>
13 #include <Library/DebugLib.h>
14 #include <Library/MemoryAllocationLib.h>
15 #include <Library/UefiBootServicesTableLib.h>
16 #include <Library/DevicePathLib.h>
17 #include <Library/ShellLib.h>
18 #include <Protocol/LoadedImage.h>
20 #define CACHE_FILE_SUFFIX L"_Cache.dat"
23 Generate the device path to the cache file.
25 @param[in] FrameworkHandle A pointer to the framework that is being persisted.
27 @retval !NULL A pointer to the EFI_FILE protocol instance for the filesystem.
28 @retval NULL Filesystem could not be found or an error occurred.
32 EFI_DEVICE_PATH_PROTOCOL
*
33 GetCacheFileDevicePath (
34 IN UNIT_TEST_FRAMEWORK_HANDLE FrameworkHandle
38 UNIT_TEST_FRAMEWORK
*Framework
;
39 EFI_LOADED_IMAGE_PROTOCOL
*LoadedImage
;
41 CHAR16
*CacheFilePath
;
43 UINTN DirectorySlashOffset
;
44 UINTN CacheFilePathLength
;
45 EFI_DEVICE_PATH_PROTOCOL
*CacheFileDevicePath
;
47 Framework
= (UNIT_TEST_FRAMEWORK
*)FrameworkHandle
;
51 CacheFileDevicePath
= NULL
;
54 // First, we need to get some information from the loaded image.
56 Status
= gBS
->HandleProtocol (
58 &gEfiLoadedImageProtocolGuid
,
61 if (EFI_ERROR (Status
)) {
62 DEBUG ((DEBUG_WARN
, "%a - Failed to locate DevicePath for loaded image. %r\n", __FUNCTION__
, Status
));
67 // Before we can start, change test name from ASCII to Unicode.
69 CacheFilePathLength
= AsciiStrLen (Framework
->ShortTitle
) + 1;
70 TestName
= AllocatePool (CacheFilePathLength
* sizeof (CHAR16
));
75 AsciiStrToUnicodeStrS (Framework
->ShortTitle
, TestName
, CacheFilePathLength
);
78 // Now we should have the device path of the root device and a file path for the rest.
79 // In order to target the directory for the test application, we must process
80 // the file path a little.
82 // NOTE: This may not be necessary... Path processing functions exist...
83 // PathCleanUpDirectories (FileNameCopy);
84 // if (PathRemoveLastItem (FileNameCopy)) {
86 AppPath
= ConvertDevicePathToText (LoadedImage
->FilePath
, TRUE
, TRUE
); // NOTE: This must be freed.
87 DirectorySlashOffset
= StrLen (AppPath
);
89 // Make sure we didn't get any weird data.
91 if (DirectorySlashOffset
== 0) {
92 DEBUG ((DEBUG_ERROR
, "%a - Weird 0-length string when processing app path.\n", __FUNCTION__
));
97 // Now that we know we have a decent string, let's take a deeper look.
100 if (AppPath
[DirectorySlashOffset
] == L
'\\') {
104 DirectorySlashOffset
--;
105 } while (DirectorySlashOffset
> 0);
108 // After that little maneuver, DirectorySlashOffset should be pointing at the last '\' in AppString.
109 // That would be the path to the parent directory that the test app is executing from.
110 // Let's check and make sure that's right.
112 if (AppPath
[DirectorySlashOffset
] != L
'\\') {
113 DEBUG ((DEBUG_ERROR
, "%a - Could not find a single directory separator in app path.\n", __FUNCTION__
));
118 // Now we know some things, we're ready to produce our output string, I think.
120 CacheFilePathLength
= DirectorySlashOffset
+ 1;
121 CacheFilePathLength
+= StrLen (TestName
);
122 CacheFilePathLength
+= StrLen (CACHE_FILE_SUFFIX
);
123 CacheFilePathLength
+= 1; // Don't forget the NULL terminator.
124 CacheFilePath
= AllocateZeroPool (CacheFilePathLength
* sizeof (CHAR16
));
125 if (!CacheFilePath
) {
130 // Let's produce our final path string, shall we?
132 StrnCpyS (CacheFilePath
, CacheFilePathLength
, AppPath
, DirectorySlashOffset
+ 1); // Copy the path for the parent directory.
133 StrCatS (CacheFilePath
, CacheFilePathLength
, TestName
); // Copy the base name for the test cache.
134 StrCatS (CacheFilePath
, CacheFilePathLength
, CACHE_FILE_SUFFIX
); // Copy the file suffix.
137 // Finally, try to create the device path for the thing thing.
139 CacheFileDevicePath
= FileDevicePath (LoadedImage
->DeviceHandle
, CacheFilePath
);
143 // Free allocated buffers.
145 if (AppPath
!= NULL
) {
149 if (CacheFilePath
!= NULL
) {
150 FreePool (CacheFilePath
);
153 if (TestName
!= NULL
) {
157 return CacheFileDevicePath
;
161 Determines whether a persistence cache already exists for
164 @param[in] FrameworkHandle A pointer to the framework that is being persisted.
167 @retval FALSE Cache doesn't exist or an error occurred.
173 IN UNIT_TEST_FRAMEWORK_HANDLE FrameworkHandle
176 EFI_DEVICE_PATH_PROTOCOL
*FileDevicePath
;
178 SHELL_FILE_HANDLE FileHandle
;
181 // NOTE: This devpath is allocated and must be freed.
183 FileDevicePath
= GetCacheFileDevicePath (FrameworkHandle
);
186 // Check to see whether the file exists. If the file can be opened for
187 // reading, it exists. Otherwise, probably not.
189 Status
= ShellOpenFileByDevicePath (
195 if (!EFI_ERROR (Status
)) {
196 ShellCloseFile (&FileHandle
);
199 if (FileDevicePath
!= NULL
) {
200 FreePool (FileDevicePath
);
203 DEBUG ((DEBUG_VERBOSE
, "%a - Returning %d\n", __FUNCTION__
, !EFI_ERROR (Status
)));
205 return (!EFI_ERROR (Status
));
209 Will save the data associated with an internal Unit Test Framework
210 state in a manner that can persist a Unit Test Application quit or
211 even a system reboot.
213 @param[in] FrameworkHandle A pointer to the framework that is being persisted.
214 @param[in] SaveData A pointer to the buffer containing the serialized
215 framework internal state.
217 @retval EFI_SUCCESS Data is persisted and the test can be safely quit.
218 @retval Others Data is not persisted and test cannot be resumed upon exit.
224 IN UNIT_TEST_FRAMEWORK_HANDLE FrameworkHandle
,
225 IN UNIT_TEST_SAVE_HEADER
*SaveData
228 EFI_DEVICE_PATH_PROTOCOL
*FileDevicePath
;
230 SHELL_FILE_HANDLE FileHandle
;
234 // Check the inputs for sanity.
236 if ((FrameworkHandle
== NULL
) || (SaveData
== NULL
)) {
237 return EFI_INVALID_PARAMETER
;
241 // Determine the path for the cache file.
242 // NOTE: This devpath is allocated and must be freed.
244 FileDevicePath
= GetCacheFileDevicePath (FrameworkHandle
);
247 // First lets open the file if it exists so we can delete it...This is the work around for truncation
249 Status
= ShellOpenFileByDevicePath (
252 (EFI_FILE_MODE_READ
| EFI_FILE_MODE_WRITE
),
256 if (!EFI_ERROR (Status
)) {
258 // If file handle above was opened it will be closed by the delete.
260 Status
= ShellDeleteFile (&FileHandle
);
261 if (EFI_ERROR (Status
)) {
262 DEBUG ((DEBUG_ERROR
, "%a failed to delete file %r\n", __FUNCTION__
, Status
));
267 // Now that we know the path to the file... let's open it for writing.
269 Status
= ShellOpenFileByDevicePath (
272 (EFI_FILE_MODE_READ
| EFI_FILE_MODE_WRITE
| EFI_FILE_MODE_CREATE
),
275 if (EFI_ERROR (Status
)) {
276 DEBUG ((DEBUG_ERROR
, "%a - Opening file for writing failed! %r\n", __FUNCTION__
, Status
));
281 // Write the data to the file.
283 WriteCount
= SaveData
->SaveStateSize
;
284 DEBUG ((DEBUG_INFO
, "%a - Writing %d bytes to file...\n", __FUNCTION__
, WriteCount
));
285 Status
= ShellWriteFile (
291 if (EFI_ERROR (Status
) || (WriteCount
!= SaveData
->SaveStateSize
)) {
292 DEBUG ((DEBUG_ERROR
, "%a - Writing to file failed! %r\n", __FUNCTION__
, Status
));
294 DEBUG ((DEBUG_INFO
, "%a - SUCCESS!\n", __FUNCTION__
));
298 // No matter what, we should probably close the file.
300 ShellCloseFile (&FileHandle
);
303 if (FileDevicePath
!= NULL
) {
304 FreePool (FileDevicePath
);
311 Will retrieve any cached state associated with the given framework.
312 Will allocate a buffer to hold the loaded data.
314 @param[in] FrameworkHandle A pointer to the framework that is being persisted.
315 @param[in] SaveData A pointer pointer that will be updated with the address
316 of the loaded data buffer.
318 @retval EFI_SUCCESS Data has been loaded successfully and SaveData is updated
319 with a pointer to the buffer.
320 @retval Others An error has occurred and no data has been loaded. SaveData
327 IN UNIT_TEST_FRAMEWORK_HANDLE FrameworkHandle
,
328 OUT UNIT_TEST_SAVE_HEADER
**SaveData
332 EFI_DEVICE_PATH_PROTOCOL
*FileDevicePath
;
333 SHELL_FILE_HANDLE FileHandle
;
334 BOOLEAN IsFileOpened
;
335 UINT64 LargeFileSize
;
337 UNIT_TEST_SAVE_HEADER
*Buffer
;
339 IsFileOpened
= FALSE
;
343 // Check the inputs for sanity.
345 if ((FrameworkHandle
== NULL
) || (SaveData
== NULL
)) {
346 return EFI_INVALID_PARAMETER
;
350 // Determine the path for the cache file.
351 // NOTE: This devpath is allocated and must be freed.
353 FileDevicePath
= GetCacheFileDevicePath (FrameworkHandle
);
356 // Now that we know the path to the file... let's open it for writing.
358 Status
= ShellOpenFileByDevicePath (
364 if (EFI_ERROR (Status
)) {
365 DEBUG ((DEBUG_ERROR
, "%a - Opening file for writing failed! %r\n", __FUNCTION__
, Status
));
372 // Now that the file is opened, we need to determine how large a buffer we need.
374 Status
= ShellGetFileSize (FileHandle
, &LargeFileSize
);
375 if (EFI_ERROR (Status
)) {
376 DEBUG ((DEBUG_ERROR
, "%a - Failed to determine file size! %r\n", __FUNCTION__
, Status
));
381 // Now that we know the size, let's allocated a buffer to hold the contents.
383 FileSize
= (UINTN
)LargeFileSize
; // You know what... if it's too large, this lib don't care.
384 Buffer
= AllocatePool (FileSize
);
385 if (Buffer
== NULL
) {
386 DEBUG ((DEBUG_ERROR
, "%a - Failed to allocate a pool to hold the file contents! %r\n", __FUNCTION__
, Status
));
387 Status
= EFI_OUT_OF_RESOURCES
;
392 // Finally, let's read the data.
394 Status
= ShellReadFile (FileHandle
, &FileSize
, Buffer
);
395 if (EFI_ERROR (Status
)) {
396 DEBUG ((DEBUG_ERROR
, "%a - Failed to read the file contents! %r\n", __FUNCTION__
, Status
));
401 // Free allocated buffers
403 if (FileDevicePath
!= NULL
) {
404 FreePool (FileDevicePath
);
408 ShellCloseFile (&FileHandle
);
412 // If we're returning an error, make sure
413 // the state is sane.
414 if (EFI_ERROR (Status
) && (Buffer
!= NULL
)) {