From mboxrd@z Thu Jan 1 00:00:00 1970 Return-Path: Received-SPF: Pass (sender SPF authorized) identity=mailfrom; client-ip=134.134.136.31; helo=mga06.intel.com; envelope-from=michael.d.kinney@intel.com; receiver=edk2-devel@lists.01.org Received: from mga06.intel.com (mga06.intel.com [134.134.136.31]) (using TLSv1.2 with cipher ECDHE-RSA-AES256-GCM-SHA384 (256/256 bits)) (No client certificate requested) by ml01.01.org (Postfix) with ESMTPS id 6DE9C2237A4CD for ; Fri, 2 Feb 2018 17:04:03 -0800 (PST) X-Amp-Result: SKIPPED(no attachment in message) X-Amp-File-Uploaded: False Received: from fmsmga006.fm.intel.com ([10.253.24.20]) by orsmga104.jf.intel.com with ESMTP/TLS/DHE-RSA-AES256-GCM-SHA384; 02 Feb 2018 17:09:42 -0800 X-ExtLoop1: 1 X-IronPort-AV: E=Sophos;i="5.46,452,1511856000"; d="scan'208";a="200986677" Received: from mdkinney-mobl2.amr.corp.intel.com ([10.254.102.40]) by fmsmga006.fm.intel.com with ESMTP; 02 Feb 2018 17:09:41 -0800 From: "Kinney, Michael D" To: edk2-devel@lists.01.org Cc: Sean Brogan , Liming Gao , Michael D Kinney Date: Fri, 2 Feb 2018 17:09:39 -0800 Message-Id: <20180203010939.8288-1-michael.d.kinney@intel.com> X-Mailer: git-send-email 2.14.2.windows.3 Subject: [staging/edk2-test Patch V2] MdePkgUnitTest: Add UefiLib unit tests X-BeenThere: edk2-devel@lists.01.org X-Mailman-Version: 2.1.23 Precedence: list List-Id: EDK II Development List-Unsubscribe: , List-Archive: List-Post: List-Help: List-Subscribe: , X-List-Received-Date: Sat, 03 Feb 2018 01:04:03 -0000 Add UefiLib unit tests for the new API EfiLocateProtocolBuffer(). Cc: Sean Brogan Cc: Liming Gao Contributed-under: TianoCore Contribution Agreement 1.1 Signed-off-by: Michael D Kinney --- 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.
+# Copyright (c) 2017 - 2018, Intel Corporation. All rights reserved.
# # 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 { SafeIntLib|MdePkg/Library/BaseSafeIntLib/BaseSafeIntLib.inf } + MdePkgUnitTest/UefiLib/UefiLibUnitTests.inf { + + UefiLib|MdePkg/Library/UefiLib/UefiLib.inf + } + MdePkgUnitTest/UefiLib/UefiLibUnitTests.inf { + + FILE_GUID = 98286492-B21F-42CD-87BF-4DA8BF617CEB + + 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.
+ 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 +#include +#include +#include +#include +#include +#include +#include +#include +#include + +#include + + +#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.
+# 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