23#define CACHE_FILE_SUFFIX L"_Cache.dat"
25CHAR16 *mCachePath =
NULL;
39 IN UNIT_TEST_FRAMEWORK_HANDLE FrameworkHandle
46 CHAR16 *CacheFilePath;
48 UINTN DirectorySlashOffset;
49 UINTN CacheFilePathLength;
59 Status =
gBS->HandleProtocol (
61 &gEfiLoadedImageProtocolGuid,
64 if (EFI_ERROR (Status)) {
65 DEBUG ((DEBUG_WARN,
"%a - Failed to locate DevicePath for loaded image. %r\n", __func__, Status));
72 CacheFilePathLength =
AsciiStrLen (Framework->ShortTitle) + 1;
73 TestName =
AllocatePool (CacheFilePathLength *
sizeof (CHAR16));
89 if (mCachePath ==
NULL) {
91 if (AppPath ==
NULL) {
95 DirectorySlashOffset =
StrLen (AppPath);
99 if (DirectorySlashOffset == 0) {
100 DEBUG ((DEBUG_ERROR,
"%a - Weird 0-length string when processing app path.\n", __func__));
108 if (AppPath[DirectorySlashOffset] == L
'\\') {
112 DirectorySlashOffset--;
113 }
while (DirectorySlashOffset > 0);
120 if (AppPath[DirectorySlashOffset] != L
'\\') {
121 DEBUG ((DEBUG_ERROR,
"%a - Could not find a single directory separator in app path.\n", __func__));
126 if (AppPath ==
NULL) {
130 DirectorySlashOffset =
StrLen (AppPath);
132 if (AppPath[DirectorySlashOffset - 1] != L
'\\') {
135 (DirectorySlashOffset + 1) *
sizeof (CHAR16),
136 (DirectorySlashOffset + 2) *
sizeof (CHAR16),
139 AppPath[DirectorySlashOffset] = L
'\\';
140 AppPath[DirectorySlashOffset + 1] = L
'\0';
143 DirectorySlashOffset--;
150 CacheFilePathLength = DirectorySlashOffset + 1;
151 CacheFilePathLength +=
StrLen (TestName);
152 CacheFilePathLength +=
StrLen (CACHE_FILE_SUFFIX);
153 CacheFilePathLength += 1;
155 if (!CacheFilePath) {
162 StrnCpyS (CacheFilePath, CacheFilePathLength, AppPath, DirectorySlashOffset + 1);
163 StrCatS (CacheFilePath, CacheFilePathLength, TestName);
164 StrCatS (CacheFilePath, CacheFilePathLength, CACHE_FILE_SUFFIX);
170 if (AppPath !=
NULL) {
174 if (TestName !=
NULL) {
178 return CacheFilePath;
194 IN UNIT_TEST_FRAMEWORK_HANDLE FrameworkHandle
199 SHELL_FILE_HANDLE FileHandle;
205 if (FileName ==
NULL) {
219 if (!EFI_ERROR (Status)) {
223 if (FileName !=
NULL) {
227 DEBUG ((DEBUG_VERBOSE,
"%a - Returning %d\n", __func__, !EFI_ERROR (Status)));
229 return (!EFI_ERROR (Status));
249 IN UNIT_TEST_FRAMEWORK_HANDLE FrameworkHandle,
256 SHELL_FILE_HANDLE FileHandle;
262 if ((FrameworkHandle ==
NULL) || (SaveData ==
NULL)) {
263 return EFI_INVALID_PARAMETER;
271 if (FileName ==
NULL) {
272 return EFI_INVALID_PARAMETER;
281 (EFI_FILE_MODE_READ | EFI_FILE_MODE_WRITE),
285 if (!EFI_ERROR (Status)) {
290 if (EFI_ERROR (Status)) {
291 DEBUG ((DEBUG_ERROR,
"%a failed to delete file %r\n", __func__, Status));
301 (EFI_FILE_MODE_READ | EFI_FILE_MODE_WRITE | EFI_FILE_MODE_CREATE),
304 if (EFI_ERROR (Status)) {
305 DEBUG ((DEBUG_ERROR,
"%a - Opening file for writing failed! %r\n", __func__, Status));
312 WriteCount = SaveStateSize;
313 DEBUG ((DEBUG_INFO,
"%a - Writing %d bytes to file...\n", __func__, WriteCount));
320 if (EFI_ERROR (Status) || (WriteCount != SaveStateSize)) {
321 DEBUG ((DEBUG_ERROR,
"%a - Writing to file failed! %r\n", __func__, Status));
323 DEBUG ((DEBUG_INFO,
"%a - SUCCESS!\n", __func__));
332 if (FileName !=
NULL) {
357 IN UNIT_TEST_FRAMEWORK_HANDLE FrameworkHandle,
364 SHELL_FILE_HANDLE FileHandle;
365 BOOLEAN IsFileOpened;
366 UINT64 LargeFileSize;
370 IsFileOpened =
FALSE;
376 if ((FrameworkHandle ==
NULL) || (SaveData ==
NULL)) {
377 return EFI_INVALID_PARAMETER;
385 if (FileName ==
NULL) {
386 return EFI_INVALID_PARAMETER;
398 if (EFI_ERROR (Status)) {
399 DEBUG ((DEBUG_ERROR,
"%a - Opening file for writing failed! %r\n", __func__, Status));
409 if (EFI_ERROR (Status)) {
410 DEBUG ((DEBUG_ERROR,
"%a - Failed to determine file size! %r\n", __func__, Status));
417 FileSize = (
UINTN)LargeFileSize;
418 *SaveStateSize = FileSize;
420 if (Buffer ==
NULL) {
421 DEBUG ((DEBUG_ERROR,
"%a - Failed to allocate a pool to hold the file contents! %r\n", __func__, Status));
422 Status = EFI_OUT_OF_RESOURCES;
430 if (EFI_ERROR (Status)) {
431 DEBUG ((DEBUG_ERROR,
"%a - Failed to read the file contents! %r\n", __func__, Status));
438 if (FileName !=
NULL) {
449 if (EFI_ERROR (Status) && (Buffer !=
NULL)) {
480 Status =
gBS->HandleProtocol (
482 &gEfiShellParametersProtocolGuid,
483 (VOID **)&ShellParameters
485 if (EFI_ERROR (Status)) {
490 Argc = ShellParameters->
Argc;
491 Argv = ShellParameters->
Argv;
494 if ((Argc > 1) && (Argv !=
NULL)) {
496 for (Index = 1; Index < Argc; Index++) {
497 if (
StrCmp (Argv[Index], L
"--CachePath") == 0) {
499 if (Index < Argc - 1) {
500 mCachePath = Argv[Index + 1];
502 Print (L
" --CachePath <Path of where to save unit test cache files, i.e. FS0:TestFolder>\n");
INTN EFIAPI StrCmp(IN CONST CHAR16 *FirstString, IN CONST CHAR16 *SecondString)
UINTN EFIAPI AsciiStrLen(IN CONST CHAR8 *String)
RETURN_STATUS EFIAPI StrCatS(IN OUT CHAR16 *Destination, IN UINTN DestMax, IN CONST CHAR16 *Source)
RETURN_STATUS EFIAPI AsciiStrToUnicodeStrS(IN CONST CHAR8 *Source, OUT CHAR16 *Destination, IN UINTN DestMax)
RETURN_STATUS EFIAPI StrnCpyS(OUT CHAR16 *Destination, IN UINTN DestMax, IN CONST CHAR16 *Source, IN UINTN Length)
UINTN EFIAPI StrLen(IN CONST CHAR16 *String)
CHAR16 *EFIAPI ConvertDevicePathToText(IN CONST EFI_DEVICE_PATH_PROTOCOL *DevicePath, IN BOOLEAN DisplayOnly, IN BOOLEAN AllowShortcuts)
VOID *EFIAPI ReallocatePool(IN UINTN OldSize, IN UINTN NewSize, IN VOID *OldBuffer OPTIONAL)
VOID *EFIAPI AllocateZeroPool(IN UINTN AllocationSize)
VOID EFIAPI FreePool(IN VOID *Buffer)
#define ASSERT_EFI_ERROR(StatusParameter)
#define DEBUG(Expression)
VOID *EFIAPI AllocatePool(IN UINTN AllocationSize)
EFI_STATUS EFIAPI ShellGetFileSize(IN SHELL_FILE_HANDLE FileHandle, OUT UINT64 *Size)
CHAR16 *EFIAPI FullyQualifyPath(IN CONST CHAR16 *Path)
EFI_STATUS EFIAPI ShellDeleteFile(IN SHELL_FILE_HANDLE *FileHandle)
EFI_STATUS EFIAPI ShellOpenFileByName(IN CONST CHAR16 *FileName, OUT SHELL_FILE_HANDLE *FileHandle, IN UINT64 OpenMode, IN UINT64 Attributes)
EFI_STATUS EFIAPI ShellWriteFile(IN SHELL_FILE_HANDLE FileHandle, IN OUT UINTN *BufferSize, IN VOID *Buffer)
EFI_STATUS EFIAPI ShellCloseFile(IN SHELL_FILE_HANDLE *FileHandle)
EFI_STATUS EFIAPI ShellReadFile(IN SHELL_FILE_HANDLE FileHandle, IN OUT UINTN *ReadSize, OUT VOID *Buffer)
VOID EFIAPI Exit(IN EFI_STATUS Status)
UINTN EFIAPI Print(IN CONST CHAR16 *Format,...)
STATIC CHAR16 * GetCacheFileName(IN UNIT_TEST_FRAMEWORK_HANDLE FrameworkHandle)
EFI_STATUS EFIAPI UnitTestPersistenceLibConstructor(IN EFI_HANDLE ImageHandle, IN EFI_SYSTEM_TABLE *SystemTable)
EFI_STATUS EFIAPI SaveUnitTestCache(IN UNIT_TEST_FRAMEWORK_HANDLE FrameworkHandle, IN VOID *SaveData, IN UINTN SaveStateSize)
EFI_STATUS EFIAPI LoadUnitTestCache(IN UNIT_TEST_FRAMEWORK_HANDLE FrameworkHandle, OUT VOID **SaveData, OUT UINTN *SaveStateSize)
BOOLEAN EFIAPI DoesCacheExist(IN UNIT_TEST_FRAMEWORK_HANDLE FrameworkHandle)
EFI_DEVICE_PATH_PROTOCOL * FilePath