[edk2] [staging/edk2-test Patch V2] MdePkgUnitTest: Add UefiLib unit tests

Kinney, Michael D posted 1 patch 6 years, 2 months ago
Failed in applying to current master (apply log)
MdePkgUnitTest/MdePkgUnitTest.dsc           |  17 +-
MdePkgUnitTest/UefiLib/UefiLibUnitTests.c   | 266 ++++++++++++++++++++++++++++
MdePkgUnitTest/UefiLib/UefiLibUnitTests.inf |  46 +++++
3 files changed, 328 insertions(+), 1 deletion(-)
create mode 100644 MdePkgUnitTest/UefiLib/UefiLibUnitTests.c
create mode 100644 MdePkgUnitTest/UefiLib/UefiLibUnitTests.inf
[edk2] [staging/edk2-test Patch V2] MdePkgUnitTest: Add UefiLib unit tests
Posted by Kinney, Michael D 6 years, 2 months ago
Add UefiLib unit tests for the new API
EfiLocateProtocolBuffer().

Cc: Sean Brogan <sean.brogan@microsoft.com>
Cc: Liming Gao <liming.gao@intel.com>
Contributed-under: TianoCore Contribution Agreement 1.1
Signed-off-by: Michael D Kinney <michael.d.kinney@intel.com>
---
 MdePkgUnitTest/MdePkgUnitTest.dsc           |  17 +-
 MdePkgUnitTest/UefiLib/UefiLibUnitTests.c   | 266 ++++++++++++++++++++++++++++
 MdePkgUnitTest/UefiLib/UefiLibUnitTests.inf |  46 +++++
 3 files changed, 328 insertions(+), 1 deletion(-)
 create mode 100644 MdePkgUnitTest/UefiLib/UefiLibUnitTests.c
 create mode 100644 MdePkgUnitTest/UefiLib/UefiLibUnitTests.inf

diff --git a/MdePkgUnitTest/MdePkgUnitTest.dsc b/MdePkgUnitTest/MdePkgUnitTest.dsc
index d98483020b..522464b545 100644
--- a/MdePkgUnitTest/MdePkgUnitTest.dsc
+++ b/MdePkgUnitTest/MdePkgUnitTest.dsc
@@ -1,7 +1,7 @@
 ## @file
 # This Package provides unit tests for the MdePkg
 #
-# Copyright (c) 2017, Intel Corporation. All rights reserved.<BR>
+# Copyright (c) 2017 - 2018, Intel Corporation. All rights reserved.<BR>
 #
 # This program and the accompanying materials are licensed and made available under
 # the terms and conditions of the BSD License which accompanies this distribution.
@@ -33,6 +33,7 @@
   BaseLib|MdePkg/Library/BaseLib/BaseLib.inf
   BaseMemoryLib|MdePkg/Library/BaseMemoryLib/BaseMemoryLib.inf
   PrintLib|MdePkg/Library/BasePrintLib/BasePrintLib.inf
+
   DebugLib|MdePkg/Library/UefiDebugLibStdErr/UefiDebugLibStdErr.inf
   DebugPrintErrorLevelLib|MdePkg/Library/BaseDebugPrintErrorLevelLib/BaseDebugPrintErrorLevelLib.inf
   PcdLib|MdePkg/Library/BasePcdLibNull/BasePcdLibNull.inf
@@ -50,8 +51,22 @@
   UefiHiiServicesLib|MdeModulePkg/Library/UefiHiiServicesLib/UefiHiiServicesLib.inf
   SortLib|MdeModulePkg/Library/UefiSortLib/UefiSortLib.inf
 
+[PcdsFixedAtBuild]
+  gEfiMdePkgTokenSpaceGuid.PcdDebugPrintErrorLevel|0x80000047
+  gEfiMdePkgTokenSpaceGuid.PcdDebugPropertyMask|0x1f
+
 [Components]
   MdePkgUnitTest/SafeIntLib/SafeIntLibUnitTests.inf {
     <LibraryClasses>
       SafeIntLib|MdePkg/Library/BaseSafeIntLib/BaseSafeIntLib.inf
   }
+  MdePkgUnitTest/UefiLib/UefiLibUnitTests.inf {
+    <LibraryClasses>
+      UefiLib|MdePkg/Library/UefiLib/UefiLib.inf
+  }
+  MdePkgUnitTest/UefiLib/UefiLibUnitTests.inf {
+    <Defines>
+      FILE_GUID = 98286492-B21F-42CD-87BF-4DA8BF617CEB
+    <LibraryClasses>
+      UefiLib|IntelFrameworkPkg/Library/FrameworkUefiLib/FrameworkUefiLib.inf
+  }
diff --git a/MdePkgUnitTest/UefiLib/UefiLibUnitTests.c b/MdePkgUnitTest/UefiLib/UefiLibUnitTests.c
new file mode 100644
index 0000000000..244715c2ca
--- /dev/null
+++ b/MdePkgUnitTest/UefiLib/UefiLibUnitTests.c
@@ -0,0 +1,266 @@
+/** @file
+  Uefi Shell based Application that Unit Tests the UefiLib
+
+  Copyright (c) 2018, Intel Corporation. All rights reserved.<BR>
+  This program and the accompanying materials
+  are licensed and made available under the terms and conditions of the BSD License
+  which accompanies this distribution.  The full text of the license may be found at
+  http://opensource.org/licenses/bsd-license.php
+
+  THE PROGRAM IS DISTRIBUTED UNDER THE BSD LICENSE ON AN "AS IS" BASIS,
+  WITHOUT WARRANTIES OR REPRESENTATIONS OF ANY KIND, EITHER EXPRESS OR IMPLIED.
+
+**/
+
+#include <Uefi.h>
+#include <Library/UefiLib.h>
+#include <Library/PrintLib.h>
+#include <Library/DebugLib.h>
+#include <Library/MemoryAllocationLib.h>
+#include <UnitTestTypes.h>
+#include <Library/UnitTestLib.h>
+#include <Library/UnitTestAssertLib.h>
+#include <Library/UefiLib.h>
+#include <Library/UefiBootServicesTableLib.h>
+
+#include <Protocol/LoadedImage.h>
+
+
+#define UNIT_TEST_APP_NAME        L"Uefi Lib Unit Test Application"
+#define UNIT_TEST_APP_VERSION     L"0.1"
+
+EFI_GUID  mUnitTestProtocolGuid = { 0xe5f282af, 0x895c, 0x4ece, { 0xae, 0xf0, 0x19, 0xbf, 0x6f, 0xdd, 0x41, 0x2 } };
+
+//
+// Conversion function tests:
+//
+UNIT_TEST_STATUS
+EFIAPI
+TestEfiLocateProtocolBuffer (
+  IN UNIT_TEST_FRAMEWORK_HANDLE  Framework,
+  IN UNIT_TEST_CONTEXT           Context
+  )
+{
+  EFI_STATUS  Status;
+  UINTN       NoProtocols;
+  VOID        **Buffer;
+  EFI_HANDLE  Handle1;
+  EFI_HANDLE  Handle2;
+  EFI_HANDLE  Handle3;
+  UINT32      Instance2;
+  UINT32      Instance3;
+
+  //
+  // NULL Protocol should result in EFI_INVALID_PARAMETER
+  //
+  Status = EfiLocateProtocolBuffer (NULL, &NoProtocols, &Buffer);
+  UT_ASSERT_EQUAL (EFI_INVALID_PARAMETER, Status);
+
+  //
+  // NULL NoProtocols should result in EFI_INVALID_PARAMETER
+  //
+  Status = EfiLocateProtocolBuffer (&gEfiCallerIdGuid, NULL, &Buffer);
+  UT_ASSERT_EQUAL (EFI_INVALID_PARAMETER, Status);
+
+  //
+  // NULL Buffer should result in EFI_INVALID_PARAMETER
+  //
+  Status = EfiLocateProtocolBuffer (&gEfiCallerIdGuid, &NoProtocols, NULL);
+  UT_ASSERT_EQUAL (EFI_INVALID_PARAMETER, Status);
+
+  //
+  // All NULL should result in EFI_INVALID_PARAMETER
+  //
+  Status = EfiLocateProtocolBuffer (NULL, NULL, NULL);
+  UT_ASSERT_EQUAL (EFI_INVALID_PARAMETER, Status);
+
+  //
+  // Request for unknown protocol should result in EFI_NOT_FOUND
+  //
+  NoProtocols = 0;
+  Buffer = NULL;
+  Status = EfiLocateProtocolBuffer (&mUnitTestProtocolGuid, &NoProtocols, &Buffer);
+  UT_ASSERT_EQUAL (EFI_NOT_FOUND, Status);
+
+  //
+  // Request for Loaded Image Protocol should result in EFI_SUCCESS
+  //
+  NoProtocols = 0;
+  Buffer = NULL;
+  Status = EfiLocateProtocolBuffer (&gEfiLoadedImageProtocolGuid, &NoProtocols, &Buffer);
+  UT_ASSERT_EQUAL (EFI_SUCCESS, Status);
+  UT_ASSERT_TRUE (NoProtocols > 0);
+  UT_ASSERT_NOT_NULL (Buffer);
+
+  if (Buffer != NULL) {
+    FreePool (Buffer);
+  }
+
+  //
+  // Install 1 instance of a protocol
+  //
+  Handle1 = NULL;
+  Status = gBS->InstallProtocolInterface (
+                  &Handle1,
+                  &mUnitTestProtocolGuid,
+                  EFI_NATIVE_INTERFACE,
+                  NULL
+                  );
+
+  //
+  // Verify 1 instance found
+  //
+  NoProtocols = 0;
+  Buffer = NULL;
+  Status = EfiLocateProtocolBuffer (&mUnitTestProtocolGuid, &NoProtocols, &Buffer);
+  UT_ASSERT_EQUAL (EFI_SUCCESS, Status);
+  UT_ASSERT_EQUAL (NoProtocols, 1);
+  UT_ASSERT_EQUAL ((UINTN)Buffer[0], (UINTN)NULL);
+
+  if (Buffer != NULL) {
+    FreePool (Buffer);
+  }
+
+  //
+  // Install a 2nd instance of a protocol
+  //
+  Handle2 = NULL;
+  Status = gBS->InstallProtocolInterface (
+                  &Handle2,
+                  &mUnitTestProtocolGuid,
+                  EFI_NATIVE_INTERFACE,
+                  &Instance2
+                  );
+
+  //
+  // Verify 2 instances found
+  //
+  NoProtocols = 0;
+  Buffer = NULL;
+  Status = EfiLocateProtocolBuffer (&mUnitTestProtocolGuid, &NoProtocols, &Buffer);
+  UT_ASSERT_EQUAL (EFI_SUCCESS, Status);
+  UT_ASSERT_EQUAL (NoProtocols, 2);
+  UT_ASSERT_EQUAL ((UINTN)Buffer[0], (UINTN)NULL);
+  UT_ASSERT_EQUAL ((UINTN)Buffer[1], (UINTN)&Instance2);
+
+  if (Buffer != NULL) {
+    FreePool (Buffer);
+  }
+
+  //
+  // Install a 3rd instance of a protocol
+  //
+  Handle3 = NULL;
+  Status = gBS->InstallProtocolInterface (
+                  &Handle3,
+                  &mUnitTestProtocolGuid,
+                  EFI_NATIVE_INTERFACE,
+                  &Instance3
+                  );
+
+  //
+  // Verify 3 instances found
+  //
+  NoProtocols = 0;
+  Buffer = NULL;
+  Status = EfiLocateProtocolBuffer (&mUnitTestProtocolGuid, &NoProtocols, &Buffer);
+  UT_ASSERT_EQUAL (EFI_SUCCESS, Status);
+  UT_ASSERT_EQUAL (NoProtocols, 3);
+  UT_ASSERT_EQUAL ((UINTN)Buffer[0], (UINTN)NULL);
+  UT_ASSERT_EQUAL ((UINTN)Buffer[1], (UINTN)&Instance2);
+  UT_ASSERT_EQUAL ((UINTN)Buffer[2], (UINTN)&Instance3);
+
+  if (Buffer != NULL) {
+    FreePool (Buffer);
+  }
+
+  //
+  // Uninstall test protocols
+  //
+  Status = gBS->UninstallProtocolInterface (
+                  Handle3,
+                  &mUnitTestProtocolGuid,
+                  &Instance3
+                  );
+
+  Status = gBS->UninstallProtocolInterface (
+                  Handle2,
+                  &mUnitTestProtocolGuid,
+                  &Instance2
+                  );
+
+  Status = gBS->UninstallProtocolInterface (
+                  Handle1,
+                  &mUnitTestProtocolGuid,
+                  NULL
+                  );
+
+  return UNIT_TEST_PASSED;
+}
+
+/**
+
+  Main fuction sets up the unit test environment
+
+**/
+EFI_STATUS
+EFIAPI
+UefiMain (
+  IN EFI_HANDLE        ImageHandle,
+  IN EFI_SYSTEM_TABLE  *SystemTable
+  )
+{
+  EFI_STATUS  Status;
+  UNIT_TEST_FRAMEWORK  *Fw;
+  UNIT_TEST_SUITE      *ProtocolTestSuite;
+  CHAR16               ShortName[100];
+
+
+  DEBUG ((DEBUG_INFO, "%s v%s\n", UNIT_TEST_APP_NAME, UNIT_TEST_APP_VERSION));
+
+  ShortName[0] = L'\0';
+  UnicodeSPrint (&ShortName[0], sizeof (ShortName), L"%a", gEfiCallerBaseName);
+
+  //
+  // Start setting up the test framework for running the tests.
+  //
+  Fw = NULL;
+  Status = InitUnitTestFramework (&Fw, UNIT_TEST_APP_NAME, ShortName, UNIT_TEST_APP_VERSION);
+  if (EFI_ERROR (Status)) {
+    DEBUG ((DEBUG_ERROR, "Failed in InitUnitTestFramework. Status = %r\n", Status));
+    goto EXIT;
+  }
+
+  //
+  // Test the protocol functions
+  //
+  Status = CreateUnitTestSuite (
+             &ProtocolTestSuite,
+             Fw,
+             L"Uefi Lib Test Suite",
+             L"Common.UefiLib.Protocol",
+             NULL, NULL
+             );
+  if (EFI_ERROR (Status)) {
+    DEBUG ((DEBUG_ERROR, "Failed in CreateUnitTestSuite for Protocol Test Suite\n"));
+    Status = EFI_OUT_OF_RESOURCES;
+    goto EXIT;
+  }
+
+  //
+  // Add test protocol test cases
+  //
+  AddTestCase (ProtocolTestSuite, L"Test EfiLocateProtocolBuffer",    L"Common.UefiLib.Protocol.EfiLocateProtocolBuffer",    TestEfiLocateProtocolBuffer,    NULL, NULL, NULL);
+
+  //
+  // Execute the tests.
+  //
+  Status = RunAllTestSuites (Fw);
+
+EXIT:
+  if (Fw) {
+    FreeUnitTestFramework (Fw);
+  }
+
+  return Status;
+}
diff --git a/MdePkgUnitTest/UefiLib/UefiLibUnitTests.inf b/MdePkgUnitTest/UefiLib/UefiLibUnitTests.inf
new file mode 100644
index 0000000000..b7913c9105
--- /dev/null
+++ b/MdePkgUnitTest/UefiLib/UefiLibUnitTests.inf
@@ -0,0 +1,46 @@
+## @file
+#  Uefi Shell based Application that Unit Tests the UefiLib
+#
+#  Copyright (c) 2018, Intel Corporation. All rights reserved.<BR>
+#  This program and the accompanying materials
+#  are licensed and made available under the terms and conditions of the BSD License
+#  which accompanies this distribution.  The full text of the license may be found at
+#  http://opensource.org/licenses/bsd-license.php
+#
+#  THE PROGRAM IS DISTRIBUTED UNDER THE BSD LICENSE ON AN "AS IS" BASIS,
+#  WITHOUT WARRANTIES OR REPRESENTATIONS OF ANY KIND, EITHER EXPRESS OR IMPLIED.
+#
+##
+
+[Defines]
+  INF_VERSION                    = 0x00010005
+  BASE_NAME                      = UefiLibUnitTestApp
+  FILE_GUID                      = BD9FC1C5-A381-4201-BD8A-4EC78A6F3A17
+  MODULE_TYPE                    = UEFI_APPLICATION
+  VERSION_STRING                 = 1.0
+  ENTRY_POINT                    = UefiMain
+
+#
+# The following information is for reference only and not required by the build tools.
+#
+#  VALID_ARCHITECTURES           = IA32 X64
+#
+
+[Sources]
+  UefiLibUnitTests.c
+
+[Packages]
+  MdePkg/MdePkg.dec
+  MsUnitTestPkg/MsUnitTestPkg.dec
+  MdeModulePkg/MdeModulePkg.dec
+
+[LibraryClasses]
+  UefiApplicationEntryPoint
+  BaseLib
+  UefiLib
+  UnitTestLib
+  UnitTestAssertLib
+  PrintLib
+
+[Protocols]
+  gEfiLoadedImageProtocolGuid
-- 
2.14.2.windows.3

_______________________________________________
edk2-devel mailing list
edk2-devel@lists.01.org
https://lists.01.org/mailman/listinfo/edk2-devel
Re: [edk2] [staging/edk2-test Patch V2] MdePkgUnitTest: Add UefiLib unit tests
Posted by Bret Barkelew 6 years, 2 months ago
Approved.
Reviewed-By: Bret Barkelew Bret.Barkelew@microsoft.com

- Bret

From: Kinney, Michael D<mailto:michael.d.kinney@intel.com>
Sent: Friday, February 2, 2018 5:09 PM
To: edk2-devel@lists.01.org<mailto:edk2-devel@lists.01.org>
Cc: Michael D Kinney<mailto:michael.d.kinney@intel.com>; Liming Gao<mailto:liming.gao@intel.com>
Subject: [edk2] [staging/edk2-test Patch V2] MdePkgUnitTest: Add UefiLib unit tests

Add UefiLib unit tests for the new API
EfiLocateProtocolBuffer().

Cc: Sean Brogan <sean.brogan@microsoft.com>
Cc: Liming Gao <liming.gao@intel.com>
Contributed-under: TianoCore Contribution Agreement 1.1
Signed-off-by: Michael D Kinney <michael.d.kinney@intel.com>
---
 MdePkgUnitTest/MdePkgUnitTest.dsc           |  17 +-
 MdePkgUnitTest/UefiLib/UefiLibUnitTests.c   | 266 ++++++++++++++++++++++++++++
 MdePkgUnitTest/UefiLib/UefiLibUnitTests.inf |  46 +++++
 3 files changed, 328 insertions(+), 1 deletion(-)
 create mode 100644 MdePkgUnitTest/UefiLib/UefiLibUnitTests.c
 create mode 100644 MdePkgUnitTest/UefiLib/UefiLibUnitTests.inf

diff --git a/MdePkgUnitTest/MdePkgUnitTest.dsc b/MdePkgUnitTest/MdePkgUnitTest.dsc
index d98483020b..522464b545 100644
--- a/MdePkgUnitTest/MdePkgUnitTest.dsc
+++ b/MdePkgUnitTest/MdePkgUnitTest.dsc
@@ -1,7 +1,7 @@
 ## @file
 # This Package provides unit tests for the MdePkg
 #
-# Copyright (c) 2017, Intel Corporation. All rights reserved.<BR>
+# Copyright (c) 2017 - 2018, Intel Corporation. All rights reserved.<BR>
 #
 # This program and the accompanying materials are licensed and made available under
 # the terms and conditions of the BSD License which accompanies this distribution.
@@ -33,6 +33,7 @@
   BaseLib|MdePkg/Library/BaseLib/BaseLib.inf
   BaseMemoryLib|MdePkg/Library/BaseMemoryLib/BaseMemoryLib.inf
   PrintLib|MdePkg/Library/BasePrintLib/BasePrintLib.inf
+
   DebugLib|MdePkg/Library/UefiDebugLibStdErr/UefiDebugLibStdErr.inf
   DebugPrintErrorLevelLib|MdePkg/Library/BaseDebugPrintErrorLevelLib/BaseDebugPrintErrorLevelLib.inf
   PcdLib|MdePkg/Library/BasePcdLibNull/BasePcdLibNull.inf
@@ -50,8 +51,22 @@
   UefiHiiServicesLib|MdeModulePkg/Library/UefiHiiServicesLib/UefiHiiServicesLib.inf
   SortLib|MdeModulePkg/Library/UefiSortLib/UefiSortLib.inf

+[PcdsFixedAtBuild]
+  gEfiMdePkgTokenSpaceGuid.PcdDebugPrintErrorLevel|0x80000047
+  gEfiMdePkgTokenSpaceGuid.PcdDebugPropertyMask|0x1f
+
 [Components]
   MdePkgUnitTest/SafeIntLib/SafeIntLibUnitTests.inf {
     <LibraryClasses>
       SafeIntLib|MdePkg/Library/BaseSafeIntLib/BaseSafeIntLib.inf
   }
+  MdePkgUnitTest/UefiLib/UefiLibUnitTests.inf {
+    <LibraryClasses>
+      UefiLib|MdePkg/Library/UefiLib/UefiLib.inf
+  }
+  MdePkgUnitTest/UefiLib/UefiLibUnitTests.inf {
+    <Defines>
+      FILE_GUID = 98286492-B21F-42CD-87BF-4DA8BF617CEB
+    <LibraryClasses>
+      UefiLib|IntelFrameworkPkg/Library/FrameworkUefiLib/FrameworkUefiLib.inf
+  }
diff --git a/MdePkgUnitTest/UefiLib/UefiLibUnitTests.c b/MdePkgUnitTest/UefiLib/UefiLibUnitTests.c
new file mode 100644
index 0000000000..244715c2ca
--- /dev/null
+++ b/MdePkgUnitTest/UefiLib/UefiLibUnitTests.c
@@ -0,0 +1,266 @@
+/** @file
+  Uefi Shell based Application that Unit Tests the UefiLib
+
+  Copyright (c) 2018, Intel Corporation. All rights reserved.<BR>
+  This program and the accompanying materials
+  are licensed and made available under the terms and conditions of the BSD License
+  which accompanies this distribution.  The full text of the license may be found at
+  https://na01.safelinks.protection.outlook.com/?url=http%3A%2F%2Fopensource.org%2Flicenses%2Fbsd-license.php&data=02%7C01%7CBret.Barkelew%40microsoft.com%7Cc6d1ac93515942d16a0f08d56aa2d09b%7Cee3303d7fb734b0c8589bcd847f1c277%7C1%7C0%7C636532169906509120&sdata=a7LzLrRoxYE5HwgMsCwmq0MrRYQMYpSqaI4hMNOdVl4%3D&reserved=0
+
+  THE PROGRAM IS DISTRIBUTED UNDER THE BSD LICENSE ON AN "AS IS" BASIS,
+  WITHOUT WARRANTIES OR REPRESENTATIONS OF ANY KIND, EITHER EXPRESS OR IMPLIED.
+
+**/
+
+#include <Uefi.h>
+#include <Library/UefiLib.h>
+#include <Library/PrintLib.h>
+#include <Library/DebugLib.h>
+#include <Library/MemoryAllocationLib.h>
+#include <UnitTestTypes.h>
+#include <Library/UnitTestLib.h>
+#include <Library/UnitTestAssertLib.h>
+#include <Library/UefiLib.h>
+#include <Library/UefiBootServicesTableLib.h>
+
+#include <Protocol/LoadedImage.h>
+
+
+#define UNIT_TEST_APP_NAME        L"Uefi Lib Unit Test Application"
+#define UNIT_TEST_APP_VERSION     L"0.1"
+
+EFI_GUID  mUnitTestProtocolGuid = { 0xe5f282af, 0x895c, 0x4ece, { 0xae, 0xf0, 0x19, 0xbf, 0x6f, 0xdd, 0x41, 0x2 } };
+
+//
+// Conversion function tests:
+//
+UNIT_TEST_STATUS
+EFIAPI
+TestEfiLocateProtocolBuffer (
+  IN UNIT_TEST_FRAMEWORK_HANDLE  Framework,
+  IN UNIT_TEST_CONTEXT           Context
+  )
+{
+  EFI_STATUS  Status;
+  UINTN       NoProtocols;
+  VOID        **Buffer;
+  EFI_HANDLE  Handle1;
+  EFI_HANDLE  Handle2;
+  EFI_HANDLE  Handle3;
+  UINT32      Instance2;
+  UINT32      Instance3;
+
+  //
+  // NULL Protocol should result in EFI_INVALID_PARAMETER
+  //
+  Status = EfiLocateProtocolBuffer (NULL, &NoProtocols, &Buffer);
+  UT_ASSERT_EQUAL (EFI_INVALID_PARAMETER, Status);
+
+  //
+  // NULL NoProtocols should result in EFI_INVALID_PARAMETER
+  //
+  Status = EfiLocateProtocolBuffer (&gEfiCallerIdGuid, NULL, &Buffer);
+  UT_ASSERT_EQUAL (EFI_INVALID_PARAMETER, Status);
+
+  //
+  // NULL Buffer should result in EFI_INVALID_PARAMETER
+  //
+  Status = EfiLocateProtocolBuffer (&gEfiCallerIdGuid, &NoProtocols, NULL);
+  UT_ASSERT_EQUAL (EFI_INVALID_PARAMETER, Status);
+
+  //
+  // All NULL should result in EFI_INVALID_PARAMETER
+  //
+  Status = EfiLocateProtocolBuffer (NULL, NULL, NULL);
+  UT_ASSERT_EQUAL (EFI_INVALID_PARAMETER, Status);
+
+  //
+  // Request for unknown protocol should result in EFI_NOT_FOUND
+  //
+  NoProtocols = 0;
+  Buffer = NULL;
+  Status = EfiLocateProtocolBuffer (&mUnitTestProtocolGuid, &NoProtocols, &Buffer);
+  UT_ASSERT_EQUAL (EFI_NOT_FOUND, Status);
+
+  //
+  // Request for Loaded Image Protocol should result in EFI_SUCCESS
+  //
+  NoProtocols = 0;
+  Buffer = NULL;
+  Status = EfiLocateProtocolBuffer (&gEfiLoadedImageProtocolGuid, &NoProtocols, &Buffer);
+  UT_ASSERT_EQUAL (EFI_SUCCESS, Status);
+  UT_ASSERT_TRUE (NoProtocols > 0);
+  UT_ASSERT_NOT_NULL (Buffer);
+
+  if (Buffer != NULL) {
+    FreePool (Buffer);
+  }
+
+  //
+  // Install 1 instance of a protocol
+  //
+  Handle1 = NULL;
+  Status = gBS->InstallProtocolInterface (
+                  &Handle1,
+                  &mUnitTestProtocolGuid,
+                  EFI_NATIVE_INTERFACE,
+                  NULL
+                  );
+
+  //
+  // Verify 1 instance found
+  //
+  NoProtocols = 0;
+  Buffer = NULL;
+  Status = EfiLocateProtocolBuffer (&mUnitTestProtocolGuid, &NoProtocols, &Buffer);
+  UT_ASSERT_EQUAL (EFI_SUCCESS, Status);
+  UT_ASSERT_EQUAL (NoProtocols, 1);
+  UT_ASSERT_EQUAL ((UINTN)Buffer[0], (UINTN)NULL);
+
+  if (Buffer != NULL) {
+    FreePool (Buffer);
+  }
+
+  //
+  // Install a 2nd instance of a protocol
+  //
+  Handle2 = NULL;
+  Status = gBS->InstallProtocolInterface (
+                  &Handle2,
+                  &mUnitTestProtocolGuid,
+                  EFI_NATIVE_INTERFACE,
+                  &Instance2
+                  );
+
+  //
+  // Verify 2 instances found
+  //
+  NoProtocols = 0;
+  Buffer = NULL;
+  Status = EfiLocateProtocolBuffer (&mUnitTestProtocolGuid, &NoProtocols, &Buffer);
+  UT_ASSERT_EQUAL (EFI_SUCCESS, Status);
+  UT_ASSERT_EQUAL (NoProtocols, 2);
+  UT_ASSERT_EQUAL ((UINTN)Buffer[0], (UINTN)NULL);
+  UT_ASSERT_EQUAL ((UINTN)Buffer[1], (UINTN)&Instance2);
+
+  if (Buffer != NULL) {
+    FreePool (Buffer);
+  }
+
+  //
+  // Install a 3rd instance of a protocol
+  //
+  Handle3 = NULL;
+  Status = gBS->InstallProtocolInterface (
+                  &Handle3,
+                  &mUnitTestProtocolGuid,
+                  EFI_NATIVE_INTERFACE,
+                  &Instance3
+                  );
+
+  //
+  // Verify 3 instances found
+  //
+  NoProtocols = 0;
+  Buffer = NULL;
+  Status = EfiLocateProtocolBuffer (&mUnitTestProtocolGuid, &NoProtocols, &Buffer);
+  UT_ASSERT_EQUAL (EFI_SUCCESS, Status);
+  UT_ASSERT_EQUAL (NoProtocols, 3);
+  UT_ASSERT_EQUAL ((UINTN)Buffer[0], (UINTN)NULL);
+  UT_ASSERT_EQUAL ((UINTN)Buffer[1], (UINTN)&Instance2);
+  UT_ASSERT_EQUAL ((UINTN)Buffer[2], (UINTN)&Instance3);
+
+  if (Buffer != NULL) {
+    FreePool (Buffer);
+  }
+
+  //
+  // Uninstall test protocols
+  //
+  Status = gBS->UninstallProtocolInterface (
+                  Handle3,
+                  &mUnitTestProtocolGuid,
+                  &Instance3
+                  );
+
+  Status = gBS->UninstallProtocolInterface (
+                  Handle2,
+                  &mUnitTestProtocolGuid,
+                  &Instance2
+                  );
+
+  Status = gBS->UninstallProtocolInterface (
+                  Handle1,
+                  &mUnitTestProtocolGuid,
+                  NULL
+                  );
+
+  return UNIT_TEST_PASSED;
+}
+
+/**
+
+  Main fuction sets up the unit test environment
+
+**/
+EFI_STATUS
+EFIAPI
+UefiMain (
+  IN EFI_HANDLE        ImageHandle,
+  IN EFI_SYSTEM_TABLE  *SystemTable
+  )
+{
+  EFI_STATUS  Status;
+  UNIT_TEST_FRAMEWORK  *Fw;
+  UNIT_TEST_SUITE      *ProtocolTestSuite;
+  CHAR16               ShortName[100];
+
+
+  DEBUG ((DEBUG_INFO, "%s v%s\n", UNIT_TEST_APP_NAME, UNIT_TEST_APP_VERSION));
+
+  ShortName[0] = L'\0';
+  UnicodeSPrint (&ShortName[0], sizeof (ShortName), L"%a", gEfiCallerBaseName);
+
+  //
+  // Start setting up the test framework for running the tests.
+  //
+  Fw = NULL;
+  Status = InitUnitTestFramework (&Fw, UNIT_TEST_APP_NAME, ShortName, UNIT_TEST_APP_VERSION);
+  if (EFI_ERROR (Status)) {
+    DEBUG ((DEBUG_ERROR, "Failed in InitUnitTestFramework. Status = %r\n", Status));
+    goto EXIT;
+  }
+
+  //
+  // Test the protocol functions
+  //
+  Status = CreateUnitTestSuite (
+             &ProtocolTestSuite,
+             Fw,
+             L"Uefi Lib Test Suite",
+             L"Common.UefiLib.Protocol",
+             NULL, NULL
+             );
+  if (EFI_ERROR (Status)) {
+    DEBUG ((DEBUG_ERROR, "Failed in CreateUnitTestSuite for Protocol Test Suite\n"));
+    Status = EFI_OUT_OF_RESOURCES;
+    goto EXIT;
+  }
+
+  //
+  // Add test protocol test cases
+  //
+  AddTestCase (ProtocolTestSuite, L"Test EfiLocateProtocolBuffer",    L"Common.UefiLib.Protocol.EfiLocateProtocolBuffer",    TestEfiLocateProtocolBuffer,    NULL, NULL, NULL);
+
+  //
+  // Execute the tests.
+  //
+  Status = RunAllTestSuites (Fw);
+
+EXIT:
+  if (Fw) {
+    FreeUnitTestFramework (Fw);
+  }
+
+  return Status;
+}
diff --git a/MdePkgUnitTest/UefiLib/UefiLibUnitTests.inf b/MdePkgUnitTest/UefiLib/UefiLibUnitTests.inf
new file mode 100644
index 0000000000..b7913c9105
--- /dev/null
+++ b/MdePkgUnitTest/UefiLib/UefiLibUnitTests.inf
@@ -0,0 +1,46 @@
+## @file
+#  Uefi Shell based Application that Unit Tests the UefiLib
+#
+#  Copyright (c) 2018, Intel Corporation. All rights reserved.<BR>
+#  This program and the accompanying materials
+#  are licensed and made available under the terms and conditions of the BSD License
+#  which accompanies this distribution.  The full text of the license may be found at
+#  https://na01.safelinks.protection.outlook.com/?url=http%3A%2F%2Fopensource.org%2Flicenses%2Fbsd-license.php&data=02%7C01%7CBret.Barkelew%40microsoft.com%7Cc6d1ac93515942d16a0f08d56aa2d09b%7Cee3303d7fb734b0c8589bcd847f1c277%7C1%7C0%7C636532169906509120&sdata=a7LzLrRoxYE5HwgMsCwmq0MrRYQMYpSqaI4hMNOdVl4%3D&reserved=0
+#
+#  THE PROGRAM IS DISTRIBUTED UNDER THE BSD LICENSE ON AN "AS IS" BASIS,
+#  WITHOUT WARRANTIES OR REPRESENTATIONS OF ANY KIND, EITHER EXPRESS OR IMPLIED.
+#
+##
+
+[Defines]
+  INF_VERSION                    = 0x00010005
+  BASE_NAME                      = UefiLibUnitTestApp
+  FILE_GUID                      = BD9FC1C5-A381-4201-BD8A-4EC78A6F3A17
+  MODULE_TYPE                    = UEFI_APPLICATION
+  VERSION_STRING                 = 1.0
+  ENTRY_POINT                    = UefiMain
+
+#
+# The following information is for reference only and not required by the build tools.
+#
+#  VALID_ARCHITECTURES           = IA32 X64
+#
+
+[Sources]
+  UefiLibUnitTests.c
+
+[Packages]
+  MdePkg/MdePkg.dec
+  MsUnitTestPkg/MsUnitTestPkg.dec
+  MdeModulePkg/MdeModulePkg.dec
+
+[LibraryClasses]
+  UefiApplicationEntryPoint
+  BaseLib
+  UefiLib
+  UnitTestLib
+  UnitTestAssertLib
+  PrintLib
+
+[Protocols]
+  gEfiLoadedImageProtocolGuid
--
2.14.2.windows.3

_______________________________________________
edk2-devel mailing list
edk2-devel@lists.01.org
https://na01.safelinks.protection.outlook.com/?url=https%3A%2F%2Flists.01.org%2Fmailman%2Flistinfo%2Fedk2-devel&data=02%7C01%7CBret.Barkelew%40microsoft.com%7Cc6d1ac93515942d16a0f08d56aa2d09b%7Cee3303d7fb734b0c8589bcd847f1c277%7C1%7C0%7C636532169906509120&sdata=Jw1WiPq5q1IB44JtJPmwfCRrn%2Bm5WAY%2FP8T2ImVxbEI%3D&reserved=0

_______________________________________________
edk2-devel mailing list
edk2-devel@lists.01.org
https://lists.01.org/mailman/listinfo/edk2-devel