//
// TDS 4.1.6
//
EFI_STATUS
BBTestStrToFatFunctionAutoTest (
  IN EFI_BB_TEST_PROTOCOL       *This,
  IN VOID                       *ClientInterface,
  IN EFI_TEST_LEVEL             TestLevel,
  IN EFI_HANDLE                 SupportHandle
  )
{

  EFI_STANDARD_TEST_LIBRARY_PROTOCOL   *StandardLib;
  EFI_STATUS                           Status;
  EFI_UNICODE_COLLATION_PROTOCOL       *UnicodeCollation;

  UINTN                                Index;
  UINTN                                SubIndex;
  UINTN                                FatSize;

  EFI_TEST_ASSERTION                   AssertionType;

  //
  // Test Data
  //
  CHAR16                               *TestData[] ={
                                             L"longfilename.c",
                                             L"\x40\x50[]-\x20\x30",
                                             L"\x40\x50.\x20\x30",
                                             L"\x40\x50 \x20\x30",
                                             L"\x40\x50\x1234\x20\x30",
                                             L"\x40\x50\x1234\x20\x30",
                                             L"filename.c",
                                             L"longfilename.c"
                                        };


  CHAR8                                 Result[MAX_SIZE_OF_STRING + 1];
  CHAR16                                UnicodeBuf[MAX_SIZE_OF_STRING + 1];

  BOOLEAN                               IsLongFileName;

  //
  // Verify whether it is one of IHV interfaces
  //
  if (! IsIhvInterface (ClientInterface, SupportHandle)) {
    return EFI_UNSUPPORTED;
  }

  //
  // Get the Standard Library Interface
  //
  Status = gtBS->HandleProtocol (
                   SupportHandle,
                   &gEfiStandardTestLibraryGuid,
                   &StandardLib
                   );

  if (EFI_ERROR(Status)) {
    StandardLib->RecordAssertion (
                   StandardLib,
                   EFI_TEST_ASSERTION_FAILED,
                   gTestGenericFailureGuid,
                   L"BS.HandleProtocol - Handle standard test library",
                   L"%a:%d:Status - %r",
                   __FILE__,
                   __LINE__,
                   Status
                   );
    return Status;
  }

  UnicodeCollation = (EFI_UNICODE_COLLATION_PROTOCOL *)ClientInterface;

  for (Index = 0; Index < sizeof (TestData) / sizeof (CHAR16*); Index++) {
    //
    // For each test data, test the StrToFat functionality.
    //
    for (SubIndex = 0; SubIndex < MAX_SIZE_OF_STRING; SubIndex++) {
      Result[SubIndex] = 'x';
    }
    Result[MAX_SIZE_OF_STRING] = 0;

    //
    // FatSize is equal to the length of TestData[Index]
    //
    FatSize = GetStrLen16 (TestData[Index]);
    IsLongFileName = UnicodeCollation->StrToFat (
                                         UnicodeCollation,
                                         TestData[Index],
                                         FatSize,
                                         Result
                                         );

    if (CheckStrToFat (TestData[Index], FatSize, Result, IsLongFileName) == TRUE &&
          IsLongFileName == CheckLongFileName (TestData[Index], FatSize)) {
      AssertionType = EFI_TEST_ASSERTION_PASSED;
    } else {
      AssertionType = EFI_TEST_ASSERTION_FAILED;
    }

    //
    // Change Char8 to Char16 for record log
    //
    UnicodeCollation->FatToStr (
                        UnicodeCollation,
                        FatSize,
                        Result,
                        UnicodeBuf
                        );


    StandardLib->RecordAssertion (
                   StandardLib,
                   AssertionType,
                   gUnicodeCollationFunctionTestAssertionGuid010,
                   L"EFI_UNICODE_COLLATION_PROTOCOL.StrToFat - Verification of StrToFat interface",
                   L"%a:%d: Str='%s', StrSize=%d, Parameter FatSize=%d, Fat='%s', Return Fat Length= %d, LongFileNmae=%d, Expected=%d",
                   __FILE__,
                   __LINE__,
                   TestData[Index],
                   GetStrLen16 (TestData[Index]),
                   FatSize,
                   UnicodeBuf,
                   GetStrLen8 (Result),
                   IsLongFileName,
                   CheckLongFileName (TestData[Index], FatSize)
                   );

    //
    // FatSize is larger than the length of TestData[Index]
    //
    FatSize = GetStrLen16 (TestData[Index]) + 2;
    IsLongFileName = UnicodeCollation->StrToFat (
                                         UnicodeCollation,
                                         TestData[Index],
                                         FatSize,
                                         Result
                                         );

    if (CheckStrToFat (TestData[Index], FatSize, Result, IsLongFileName) == TRUE &&
          IsLongFileName == CheckLongFileName (TestData[Index], FatSize)) {
      AssertionType = EFI_TEST_ASSERTION_PASSED;
    } else {
      AssertionType = EFI_TEST_ASSERTION_FAILED;
    }

    //
    // Change Char8 to Char16 for record log
    //
    UnicodeCollation->FatToStr (
                        UnicodeCollation,
                        FatSize,
                        Result,
                        UnicodeBuf
                        );

    StandardLib->RecordAssertion (
                   StandardLib,
                   AssertionType,
                   gUnicodeCollationFunctionTestAssertionGuid011,
                   L"EFI_UNICODE_COLLATION_PROTOCOL.StrToFat - Verification of StrToFat interface",
                   L"%a:%d: Str='%s', StrSize=%d, Parameter FatSize=%d, Fat='%s', Return Fat Length= %d, LongFileNmae=%d, Expected=%d",
                   __FILE__,
                   __LINE__,
                   TestData[Index],
                   GetStrLen16 (TestData[Index]),
                   FatSize,
                   UnicodeBuf,
                   GetStrLen8 (Result),
                   IsLongFileName,
                   CheckLongFileName (TestData[Index], FatSize)
                   );

    //
    // FatSize is smaller than the length of TestData[Index]
    //
    FatSize = GetStrLen16 (TestData[Index]) - 2;

    IsLongFileName = UnicodeCollation->StrToFat (
                                         UnicodeCollation,
                                         TestData[Index],
                                         FatSize,
                                         Result
                                         );

    if (CheckStrToFat (TestData[Index], FatSize, Result, IsLongFileName) == TRUE &&
          IsLongFileName == CheckLongFileName (TestData[Index], FatSize)) {
      AssertionType = EFI_TEST_ASSERTION_PASSED;
    } else {
      AssertionType = EFI_TEST_ASSERTION_FAILED;
    }

    //
    // Change Char8 to Char16 for record log
    //
    UnicodeCollation->FatToStr (
                        UnicodeCollation,
                        FatSize,
                        Result,
                        UnicodeBuf
                        );
    StandardLib->RecordAssertion (
                   StandardLib,
                   AssertionType,
                   gUnicodeCollationFunctionTestAssertionGuid012,
                   L"EFI_UNICODE_COLLATION_PROTOCOL.StrToFat - Verification of StrToFat interface",
                   L"%a:%d: Str='%s', StrSize=%d, Parameter FatSize=%d, Fat='%s', Return Fat Length= %d, ConversionFail=%d, Expected=%d",
                   __FILE__,
                   __LINE__,
                   TestData[Index],
                   GetStrLen16 (TestData[Index]),
                   FatSize,
                   UnicodeBuf,
                   GetStrLen8 (Result),
                   IsLongFileName,
                   CheckLongFileName (TestData[Index], FatSize)
                   );
  };

  return EFI_SUCCESS;
}
//
// TDS 4.1.5
//
EFI_STATUS
BBTestFatToStrFunctionAutoTest (
  IN EFI_BB_TEST_PROTOCOL       *This,
  IN VOID                       *ClientInterface,
  IN EFI_TEST_LEVEL             TestLevel,
  IN EFI_HANDLE                 SupportHandle
  )
{

  EFI_STANDARD_TEST_LIBRARY_PROTOCOL   *StandardLib;
  EFI_STATUS                           Status;
  EFI_UNICODE_COLLATION_PROTOCOL       *UnicodeCollation;

  UINTN                                Index;
  UINTN                                FatSize;

  EFI_TEST_ASSERTION                   AssertionType;

  //
  // Test Data
  //
  CHAR8                                *TestData[] ={
                                             "filename.c",
                                             "longfilename.c",
                                             "\x10\x11\x20\x22\x50\x51\x61\x62",
                                        };


  CHAR16                               Result[MAX_SIZE_OF_STRING + 1];


  //
  // Verify whether it is one of IHV interfaces
  //
  if (! IsIhvInterface (ClientInterface, SupportHandle)) {
    return EFI_UNSUPPORTED;
  }

  //
  // Get the Standard Library Interface
  //
  Status = gtBS->HandleProtocol (
                   SupportHandle,
                   &gEfiStandardTestLibraryGuid,
                   &StandardLib
                   );

  if (EFI_ERROR(Status)) {
    StandardLib->RecordAssertion (
                   StandardLib,
                   EFI_TEST_ASSERTION_FAILED,
                   gTestGenericFailureGuid,
                   L"BS.HandleProtocol - Handle standard test library",
                   L"%a:%d:Status - %r",
                   __FILE__,
                   __LINE__,
                   Status
                   );
    return Status;
  }

  UnicodeCollation = (EFI_UNICODE_COLLATION_PROTOCOL *)ClientInterface;

  for (Index = 0; Index < sizeof (TestData) / sizeof (CHAR8*); Index++) {
    //
    // For each test data, test the FatToStr functionality.
    //

    //
    // FatSize reflect length of TestData[Index] correctly
    //
    FatSize = GetStrLen8 (TestData[Index]);
    UnicodeCollation->FatToStr (
                        UnicodeCollation,
                        FatSize,
                        TestData[Index],
                        Result
                        );

    if (CheckFatToStr (TestData[Index], FatSize, Result) == TRUE) {
      AssertionType = EFI_TEST_ASSERTION_PASSED;
    } else {
      AssertionType = EFI_TEST_ASSERTION_FAILED;
    }

    StandardLib->RecordAssertion (
                   StandardLib,
                   AssertionType,
                   gUnicodeCollationFunctionTestAssertionGuid007,
                   L"EFI_UNICODE_COLLATION_PROTOCOL.FatToStr - Verification of FatToStr interface",
                   L"%a:%d: Fat='%s', FatToStr='%s', FatSize=%d, StrSize=%d",
                   __FILE__,
                   __LINE__,
                   TestData[Index],
                   Result,
                   FatSize,
                   GetStrLen16 (Result)
                   );

    //
    // FatSize is larger then length of TestData[Index]
    //
    FatSize = GetStrLen8 (TestData[Index]) + 2;
    if (FatSize >= 0 || FatSize <= MAX_SIZE_OF_STRING) {
      UnicodeCollation->FatToStr (
                          UnicodeCollation,
                          FatSize,
                          TestData[Index],
                          Result
                          );

      if (CheckFatToStr (TestData[Index], FatSize, Result) == TRUE) {
        AssertionType = EFI_TEST_ASSERTION_PASSED;
      } else {
        AssertionType = EFI_TEST_ASSERTION_FAILED;
      }

      StandardLib->RecordAssertion (
                     StandardLib,
                     AssertionType,
                     gUnicodeCollationFunctionTestAssertionGuid008,
                     L"EFI_UNICODE_COLLATION_PROTOCOL.FatToStr - Verification of FatToStr interface",
                     L"%a:%d: Fat='%s', FatToStr='%s', FatSize=%d, StrSize=%d",
                     __FILE__,
                     __LINE__,
                     TestData[Index],
                     Result,
                     FatSize,
                     GetStrLen16 (Result)
                     );
    }

    //
    // FatSize is less then length of TestData[Index]
    //
    FatSize = GetStrLen8 (TestData[Index]) - 2;
    if (FatSize >= 0 || FatSize <= MAX_SIZE_OF_STRING) {
      UnicodeCollation->FatToStr (
                          UnicodeCollation,
                          FatSize,
                          TestData[Index],
                          Result
                          );

      if (CheckFatToStr (TestData[Index], FatSize, Result) == TRUE) {
        AssertionType = EFI_TEST_ASSERTION_PASSED;
      } else {
        AssertionType = EFI_TEST_ASSERTION_FAILED;
      }

      StandardLib->RecordAssertion (
                     StandardLib,
                     AssertionType,
                     gUnicodeCollationFunctionTestAssertionGuid009,
                     L"EFI_UNICODE_COLLATION_PROTOCOL.FatToStr - Verification of FatToStr interface",
                     L"%a:%d: Fat='%s', FatToStr='%s', FatSize=%d, StrSize=%d",
                     __FILE__,
                     __LINE__,
                     TestData[Index],
                     Result,
                     FatSize,
                     GetStrLen16 (Result)
                     );
    }
  };

  return EFI_SUCCESS;
}