From mboxrd@z Thu Jan 1 00:00:00 1970 Received: from linux.microsoft.com (linux.microsoft.com [13.77.154.182]) by mx.groups.io with SMTP id smtpd.web08.12178.1647966160811068995 for ; Tue, 22 Mar 2022 09:22:40 -0700 Authentication-Results: mx.groups.io; dkim=fail reason="body hash did not verify" header.i=@linux.microsoft.com header.s=default header.b=b5xl6hon; spf=pass (domain: linux.microsoft.com, ip: 13.77.154.182, mailfrom: mikuback@linux.microsoft.com) Received: from localhost.localdomain (unknown [47.202.59.224]) by linux.microsoft.com (Postfix) with ESMTPSA id 83C0520B4783; Tue, 22 Mar 2022 09:22:39 -0700 (PDT) DKIM-Filter: OpenDKIM Filter v2.11.0 linux.microsoft.com 83C0520B4783 DKIM-Signature: v=1; a=rsa-sha256; c=relaxed/relaxed; d=linux.microsoft.com; s=default; t=1647966160; bh=UVwXZzp6DlibtSfhfl3w/Dr4i1mZa+wDrLQUxjfR1kE=; h=From:To:Cc:Subject:Date:In-Reply-To:References:From; b=b5xl6honvsFTYaFlwMgZQrLbu8KmPOyT7Uoy36tPfyhPeSb/391Wn1RYsMYaXTfWm q3K+KPFFT9D9QCwE+y55Q0wruGSTMjd5SnUX+flIbeRsLPD+q0pU+62VKvRnBRhZPF 7Yf99myuaqlmpLQ7SyGZ5SAczGt8IkdjHUPT44nI= From: "Michael Kubacki" To: devel@edk2.groups.io Cc: Andrew Fish , Kang Gao , Michael D Kinney , Michael Kubacki , Leif Lindholm , Benjamin You , Liu Yun , Ankit Sinha , Nate DeSimone Subject: [PATCH v1 30/41] PrmPkg/Library/DxePrmContextBufferLib: Add host-based unit tests Date: Tue, 22 Mar 2022 12:19:36 -0400 Message-Id: <20220322161947.9319-31-mikuback@linux.microsoft.com> X-Mailer: git-send-email 2.28.0.windows.1 In-Reply-To: <20220322161947.9319-1-mikuback@linux.microsoft.com> References: <20220322161947.9319-1-mikuback@linux.microsoft.com> MIME-Version: 1.0 Content-Transfer-Encoding: quoted-printable From: Michael Kubacki Adds host-based unit tests for DxePrmContextBufferLib. This tests each function in DxePrmContextBufferLib. Any changes to DxePrmContextBufferLib moving forward must pass the unit tests. The unit tests should be updated as needed if the library API changes. Cc: Andrew Fish Cc: Kang Gao Cc: Michael D Kinney Cc: Michael Kubacki Cc: Leif Lindholm Cc: Benjamin You Cc: Liu Yun Cc: Ankit Sinha Cc: Nate DeSimone Signed-off-by: Michael Kubacki --- PrmPkg/Library/DxePrmContextBufferLib/UnitTest/DxePrmContextBufferLibUni= tTest.c | 649 ++++++++++++++++++++ PrmPkg/Library/DxePrmContextBufferLib/UnitTest/DxePrmContextBufferLibUni= tTestHost.inf | 46 ++ PrmPkg/Test/PrmPkgHostTest.dsc = | 6 + 3 files changed, 701 insertions(+) diff --git a/PrmPkg/Library/DxePrmContextBufferLib/UnitTest/DxePrmContext= BufferLibUnitTest.c b/PrmPkg/Library/DxePrmContextBufferLib/UnitTest/DxeP= rmContextBufferLibUnitTest.c new file mode 100644 index 000000000000..07e247305880 --- /dev/null +++ b/PrmPkg/Library/DxePrmContextBufferLib/UnitTest/DxePrmContextBufferL= ibUnitTest.c @@ -0,0 +1,649 @@ +/** @file + + Unit tests for the PRM Context Buffer Library. + + Copyright (c) Microsoft Corporation + SPDX-License-Identifier: BSD-2-Clause-Patent + +**/ + +#include +#include +#include +#include +#include +#include + +#include + +#include +#include +#include +#include +#include +#include +#include +#include +#include +#include + +#define UNIT_TEST_NAME "PRM Context Buffer Library Unit Test" +#define UNIT_TEST_VERSION "0.1" + +///=3D=3D=3D TEST DATA =3D=3D=3D=3D=3D=3D=3D=3D=3D=3D=3D=3D=3D=3D=3D=3D=3D= =3D=3D=3D=3D=3D=3D=3D=3D=3D=3D=3D=3D=3D=3D=3D=3D=3D=3D=3D=3D=3D=3D=3D=3D=3D= =3D=3D=3D=3D=3D=3D=3D=3D=3D=3D=3D=3D=3D=3D=3D=3D=3D=3D=3D=3D=3D=3D=3D=3D=3D= =3D=3D=3D=3D=3D=3D=3D=3D=3D=3D=3D=3D=3D=3D=3D + +EFI_HANDLE mTestProtocolHandle; + +//*---------------------------------------------------------------------= -------------* +//* Context Structures = * +//*---------------------------------------------------------------------= -------------* +typedef struct { + PRM_MODULE_CONTEXT_BUFFERS *InstallationStructure; + EFI_HANDLE Handle; + PRM_GUID_SEARCH_TYPE GuidSearchType; + EFI_GUID *Guid; + EFI_GUID *ExpectedModuleGuid; + EFI_STATUS ExpectedStatus; +} PRM_CONTEXT_BUFFERS_TEST_CONTEXT; + +typedef struct { + EFI_GUID *HandlerGuid; + PRM_MODULE_CONTEXT_BUFFERS *ContextBuffers; + PRM_CONTEXT_BUFFER *ExpectedContextBuffer; + EFI_STATUS ExpectedStatus; +} PRM_CONTEXT_BUFFER_TEST_CONTEXT; + +//*---------------------------------------------------------------------= -------------* +//* Test GUIDs = * +//*---------------------------------------------------------------------= -------------* + +// {52960b90-2f3a-4917-b91a-ed5f599a8809} +#define HANDLER_TEST_GUID_1 {0x52960b90, 0x2f3a, 0x4917, { 0xb9, 0x1a, 0= xed, 0x5f, 0x59, 0x9a, 0x88, 0x09 }} +EFI_GUID mHandlerTestGuid1 =3D HANDLER_TEST_GUID_1; + +// {9316a80d-06dc-417b-b21d-6b3c2ae4ed6f} +#define HANDLER_TEST_GUID_2 {0x9316a80d, 0x06dc, 0x417b, { 0xb2, 0x1d, 0= x6b, 0x3c, 0x2a, 0xe4, 0xed, 0x6f }} +EFI_GUID mHandlerTestGuid2 =3D HANDLER_TEST_GUID_2; + +// {d32ac8ba-6cc6-456f-9ed9-9233fa310434} +#define HANDLER_TEST_GUID_3 {0xd32ac8ba, 0x6cc6, 0x456f, { 0x9e, 0xd9, 0= x92, 0x33, 0xfa, 0x31, 0x04, 0x34 }} +EFI_GUID mHandlerTestGuid3 =3D HANDLER_TEST_GUID_3; + +// {faadaa95-070b-4a34-a919-18305dc07370} +#define MODULE_TEST_GUID_1 {0xfaadaa95, 0x070b, 0x4a34, { 0xa9, 0x19, 0x= 18, 0x30, 0x5d, 0xc0, 0x73, 0x70 }} +EFI_GUID mModuleTestGuid1 =3D MODULE_TEST_GUID_1; + +// {0ea24584-731c-4863-9100-75780af509a7} +#define MODULE_TEST_GUID_2 {0x0ea24584, 0x731c, 0x4863, { 0x91, 0x00, 0x= 75, 0x78, 0x0a, 0xf5, 0x09, 0xa7 }} +EFI_GUID mModuleTestGuid2 =3D MODULE_TEST_GUID_2; + +// {f456b7a1-82a6-4427-8486-87e3a602df43} +#define MODULE_TEST_GUID_3 {0xf456b7a1, 0x82a6, 0x4427, { 0x84, 0x86, 0x= 87, 0xe3, 0xa6, 0x02, 0xdf, 0x43 }} +EFI_GUID mModuleTestGuid3 =3D MODULE_TEST_GUID_3; + +// {4a941a9c-9dcf-471b-94b5-d9e2d8c64a1b} +#define NEGATIVE_TEST_GUID {0x4a941a9c, 0x9dcf, 0x471b, {0x94, 0xb5, 0x= d9, 0xe2, 0xd8, 0xc6, 0x4a, 0x1b }} +EFI_GUID mNegativeTestGuid =3D NEGATIVE_TEST_GUID; + +//*---------------------------------------------------------------------= -------------* +//* PRM Static Test Structures = * +//*---------------------------------------------------------------------= -------------* + +PRM_DATA_BUFFER mTestStaticDataBuffer1 =3D { + { + PRM_DATA_BUFFER_HEADER_SIGNATURE, + sizeof (PRM_DATA_BUFFER) + } + // No data in the buffer (only a header) +}; + +PRM_CONTEXT_BUFFER mTestPrmContextBuffer1 =3D { + PRM_CONTEXT_BUFFER_SIGNATURE, // Signature + PRM_CONTEXT_BUFFER_INTERFACE_VERSION, // Version + 0, // Reserved + HANDLER_TEST_GUID_1, // HandlerGuid + &mTestStaticDataBuffer1 // StaticDataBuffer +}; + +PRM_CONTEXT_BUFFER mTestPrmContextBuffer2[2] =3D { + // Context buffer #1 + { + PRM_CONTEXT_BUFFER_SIGNATURE, // Signature + PRM_CONTEXT_BUFFER_INTERFACE_VERSION, // Version + 0, // Reserved + HANDLER_TEST_GUID_2, // HandlerGuid + NULL // StaticDataBuffer + }, + // Context buffer #2 + { + PRM_CONTEXT_BUFFER_SIGNATURE, // Signature + PRM_CONTEXT_BUFFER_INTERFACE_VERSION, // Version + 0, // Reserved + HANDLER_TEST_GUID_3, // HandlerGuid + &mTestStaticDataBuffer1 // StaticDataBuffer (reuse b= uffer StaticDataBuffer1) + } +}; + +PRM_MODULE_CONTEXT_BUFFERS mTestPrmModuleContextBuffers1 =3D { + MODULE_TEST_GUID_1, + 1, + &mTestPrmContextBuffer1, + NULL +}; + +PRM_MODULE_CONTEXT_BUFFERS mTestPrmModuleContextBuffers2 =3D { + MODULE_TEST_GUID_2, + 1, + &mTestPrmContextBuffer1, + NULL +}; + +PRM_MODULE_CONTEXT_BUFFERS mTestPrmModuleContextBuffers3 =3D { + MODULE_TEST_GUID_3, + 2, + &mTestPrmContextBuffer2[0], + NULL +}; + + +//*---------------------------------------------------------------------= -------------* +//* Test Contexts = * +//*---------------------------------------------------------------------= -------------* + +//* Searches by module GUID * +// +------------------= --------------+--------+----------------+--------------------+-----------= ---------+--------------------+ +// + InstallationStruc= ture | Handle | GuidSearchType | Guid | ExpectedMo= duleGuid | ExpectedStatus | +// +------------------= --------------+--------+----------------+--------------------+-----------= ---------+--------------------+ +PRM_CONTEXT_BUFFERS_TEST_CONTEXT mContextBuffers00 =3D { &mTestPrmModule= ContextBuffers1, NULL, ByModuleGuid, &mModuleTestGuid1, &mModule= TestGuid1, EFI_SUCCESS }; +PRM_CONTEXT_BUFFERS_TEST_CONTEXT mContextBuffers01 =3D { &mTestPrmModule= ContextBuffers2, NULL, ByModuleGuid, &mModuleTestGuid2, &mModule= TestGuid2, EFI_SUCCESS }; +PRM_CONTEXT_BUFFERS_TEST_CONTEXT mContextBuffers02 =3D { &mTestPrmModule= ContextBuffers3, NULL, ByModuleGuid, &mModuleTestGuid3, &mModule= TestGuid3, EFI_SUCCESS }; +PRM_CONTEXT_BUFFERS_TEST_CONTEXT mContextBuffers03 =3D { &mTestPrmModule= ContextBuffers3, NULL, ByModuleGuid, &mNegativeTestGuid, &gZeroGu= id, EFI_NOT_FOUND }; +PRM_CONTEXT_BUFFERS_TEST_CONTEXT mContextBuffers04 =3D { &mTestPrmModule= ContextBuffers1, NULL, ByModuleGuid, &gZeroGuid, &gZeroGu= id, EFI_NOT_FOUND }; + +//* Searches by handler GUID * +// +------------------= --------------+--------+----------------+--------------------+-----------= ---------+--------------------+ +// + InstallationStruc= ture | Handle | GuidSearchType | Guid | ExpectedMo= duleGuid | ExpectedStatus | +// +------------------= --------------+--------+----------------+--------------------+-----------= ---------+--------------------+ +PRM_CONTEXT_BUFFERS_TEST_CONTEXT mContextBuffers05 =3D { &mTestPrmModule= ContextBuffers1, NULL, ByHandlerGuid, &mHandlerTestGuid1, &mModule= TestGuid1, EFI_SUCCESS }; +PRM_CONTEXT_BUFFERS_TEST_CONTEXT mContextBuffers06 =3D { &mTestPrmModule= ContextBuffers1, NULL, ByHandlerGuid, &gZeroGuid, &gZeroGu= id, EFI_NOT_FOUND }; +PRM_CONTEXT_BUFFERS_TEST_CONTEXT mContextBuffers07 =3D { &mTestPrmModule= ContextBuffers2, NULL, ByHandlerGuid, &mHandlerTestGuid1, &mModule= TestGuid2, EFI_SUCCESS }; +PRM_CONTEXT_BUFFERS_TEST_CONTEXT mContextBuffers08 =3D { &mTestPrmModule= ContextBuffers2, NULL, ByHandlerGuid, &mNegativeTestGuid, &gZeroGu= id, EFI_NOT_FOUND }; +PRM_CONTEXT_BUFFERS_TEST_CONTEXT mContextBuffers09 =3D { &mTestPrmModule= ContextBuffers3, NULL, ByHandlerGuid, &mHandlerTestGuid1, &gZeroGu= id, EFI_NOT_FOUND }; +PRM_CONTEXT_BUFFERS_TEST_CONTEXT mContextBuffers10 =3D { &mTestPrmModule= ContextBuffers3, NULL, ByHandlerGuid, &mHandlerTestGuid2, &mModule= TestGuid3, EFI_SUCCESS }; +PRM_CONTEXT_BUFFERS_TEST_CONTEXT mContextBuffers11 =3D { &mTestPrmModule= ContextBuffers3, NULL, ByHandlerGuid, &mHandlerTestGuid3, &mModule= TestGuid3, EFI_SUCCESS }; +PRM_CONTEXT_BUFFERS_TEST_CONTEXT mContextBuffers12 =3D { &mTestPrmModule= ContextBuffers3, NULL, ByHandlerGuid, &gZeroGuid, &gZeroGu= id, EFI_NOT_FOUND }; + +PRM_CONTEXT_BUFFERS_TEST_CONTEXT *mContextBuffersArray[] =3D { + &mContextBuffers00, + &mContextBuffers01, + &mContextBuffers02, + &mContextBuffers03, + &mContextBuffers04, + &mContextBuffers05, + &mContextBuffers06, + &mContextBuffers07, + &mContextBuffers08, + &mContextBuffers09, + &mContextBuffers10, + &mContextBuffers11, + &mContextBuffers12 +}; + +// +--------------------= --+----------------------------------+-----------------------------------= -------+--------------------+ +// + HandlerGuid = | ContextBuffers | ExpectedContextBuffer = | ExpectedStatus | +// +--------------------= --+----------------------------------+-----------------------------------= -------+--------------------+ +PRM_CONTEXT_BUFFER_TEST_CONTEXT mContextBuffer00 =3D { &mHandlerTestGuid= 1, &mTestPrmModuleContextBuffers1, &mTestPrmContextBuffer1, = EFI_SUCCESS }; +PRM_CONTEXT_BUFFER_TEST_CONTEXT mContextBuffer01 =3D { &mHandlerTestGuid= 1, &mTestPrmModuleContextBuffers2, &mTestPrmContextBuffer1, = EFI_SUCCESS }; +PRM_CONTEXT_BUFFER_TEST_CONTEXT mContextBuffer02 =3D { &mHandlerTestGuid= 2, &mTestPrmModuleContextBuffers3, &mTestPrmContextBuffer2[0], = EFI_SUCCESS }; +PRM_CONTEXT_BUFFER_TEST_CONTEXT mContextBuffer03 =3D { &mHandlerTestGuid= 3, &mTestPrmModuleContextBuffers3, &mTestPrmContextBuffer2[1], = EFI_SUCCESS }; +PRM_CONTEXT_BUFFER_TEST_CONTEXT mContextBuffer04 =3D { &mNegativeTestGui= d, &mTestPrmModuleContextBuffers1, NULL, = EFI_NOT_FOUND }; +PRM_CONTEXT_BUFFER_TEST_CONTEXT mContextBuffer05 =3D { &gZeroGuid, = &mTestPrmModuleContextBuffers3, NULL, = EFI_NOT_FOUND }; + +PRM_CONTEXT_BUFFER_TEST_CONTEXT *mContextBufferArray[] =3D { + &mContextBuffer00, + &mContextBuffer01, + &mContextBuffer02, + &mContextBuffer03, + &mContextBuffer04, + &mContextBuffer05 +}; + +///=3D=3D=3D HELPER FUNCTIONS =3D=3D=3D=3D=3D=3D=3D=3D=3D=3D=3D=3D=3D=3D= =3D=3D=3D=3D=3D=3D=3D=3D=3D=3D=3D=3D=3D=3D=3D=3D=3D=3D=3D=3D=3D=3D=3D=3D=3D= =3D=3D=3D=3D=3D=3D=3D=3D=3D=3D=3D=3D=3D=3D=3D=3D=3D=3D=3D=3D=3D=3D=3D=3D=3D= =3D=3D=3D=3D=3D=3D=3D=3D=3D=3D=3D + +// None + +///=3D=3D=3D TEST CASES =3D=3D=3D=3D=3D=3D=3D=3D=3D=3D=3D=3D=3D=3D=3D=3D= =3D=3D=3D=3D=3D=3D=3D=3D=3D=3D=3D=3D=3D=3D=3D=3D=3D=3D=3D=3D=3D=3D=3D=3D=3D= =3D=3D=3D=3D=3D=3D=3D=3D=3D=3D=3D=3D=3D=3D=3D=3D=3D=3D=3D=3D=3D=3D=3D=3D=3D= =3D=3D=3D=3D=3D=3D=3D=3D=3D=3D=3D=3D=3D=3D=3D + +///=3D=3D=3D=3D=3D BASIC SUITE =3D=3D=3D=3D=3D=3D=3D=3D=3D=3D=3D=3D=3D=3D= =3D=3D=3D=3D=3D=3D=3D=3D=3D=3D=3D=3D=3D=3D=3D=3D=3D=3D=3D=3D=3D=3D=3D=3D=3D= =3D=3D=3D=3D=3D=3D=3D=3D=3D=3D=3D + +/** + Verifies that passing NULL arguments to all library functions fails wi= th EFI_INVALID_PARAMETER. + + @param[in] Context [Optional] An optional context paramet= er. + Not used in this unit test. + + @retval UNIT_TEST_PASSED Unit test case prerequi= sites are met. + @retval UNIT_TEST_ERROR_PREREQUISITE_NOT_MET Test case should be ski= pped.. + +**/ +UNIT_TEST_STATUS +EFIAPI +NullPointerArgumentsShouldFailGracefully ( + IN UNIT_TEST_CONTEXT Context + ) +{ + EFI_GUID Guid; + PRM_CONTEXT_BUFFER *ContextBufferPtr; + PRM_MODULE_CONTEXT_BUFFERS ModuleContextBuffers; + PRM_MODULE_CONTEXT_BUFFERS *ModuleContextBuffersPtr; + + UT_ASSERT_EQUAL (FindContextBufferInModuleBuffers (NULL, NULL, NULL), = EFI_INVALID_PARAMETER); + UT_ASSERT_EQUAL (FindContextBufferInModuleBuffers (NULL, &ModuleContex= tBuffers, &ContextBufferPtr), EFI_INVALID_PARAMETER); + UT_ASSERT_EQUAL (FindContextBufferInModuleBuffers (&Guid, NULL, &Conte= xtBufferPtr), EFI_INVALID_PARAMETER); + UT_ASSERT_EQUAL (FindContextBufferInModuleBuffers (&Guid, &ModuleConte= xtBuffers, NULL), EFI_INVALID_PARAMETER); + + UT_ASSERT_EQUAL (GetModuleContextBuffers (ByModuleGuid, NULL, NULL), E= FI_INVALID_PARAMETER); + UT_ASSERT_EQUAL (GetModuleContextBuffers (ByModuleGuid, NULL, &ModuleC= ontextBuffersPtr), EFI_INVALID_PARAMETER); + UT_ASSERT_EQUAL (GetModuleContextBuffers (ByModuleGuid, &Guid, NULL), = EFI_INVALID_PARAMETER); + + UT_ASSERT_EQUAL (GetContextBuffer (NULL, NULL, NULL), EFI_INVALID_PARA= METER); + UT_ASSERT_EQUAL (GetContextBuffer (NULL, &ModuleContextBuffers, &Conte= xtBufferPtr), EFI_INVALID_PARAMETER); + UT_ASSERT_EQUAL (GetContextBuffer (&Guid, NULL, &ContextBufferPtr), EF= I_NOT_FOUND); + UT_ASSERT_EQUAL (GetContextBuffer (&Guid, &ModuleContextBuffers, NULL)= , EFI_INVALID_PARAMETER); + + return UNIT_TEST_PASSED; +} + +///=3D=3D=3D=3D=3D FUNCTIONAL CORRECTNESS SUITE =3D=3D=3D=3D=3D=3D=3D=3D= =3D=3D=3D=3D=3D=3D=3D=3D=3D=3D=3D=3D=3D=3D=3D=3D=3D=3D=3D=3D=3D=3D=3D=3D=3D= =3D=3D=3D=3D=3D=3D=3D=3D=3D=3D=3D=3D=3D=3D=3D=3D=3D + +/** + Functional Correctness pre-requisite function. + + Installs a gPrmConfigProtocolGuid protocol instance as specified by th= e provided + context in preparation for unit test execution + + @param[in] Context [Optional] An optional parameter that enab= les: + A pointer to a PRM_CONTEXT_BUFFERS_TEST_CO= NTEXT structure with + context information for this unit test. + + @retval UNIT_TEST_PASSED Unit test case prerequi= sites + are met. + @retval UNIT_TEST_ERROR_PREREQUISITE_NOT_MET Test case should be ski= pped. + +**/ +STATIC +UNIT_TEST_STATUS +EFIAPI +InitializeFunctionalCorrectness ( + IN UNIT_TEST_CONTEXT Context + ) +{ + EFI_STATUS Status; + PRM_CONFIG_PROTOCOL *PrmConfigProtocol; + PRM_MODULE_CONTEXT_BUFFERS *ModuleContextBuffers; + PRM_CONTEXT_BUFFERS_TEST_CONTEXT *TestContext; + + UT_ASSERT_NOT_NULL (Context); + TestContext =3D (PRM_CONTEXT_BUFFERS_TEST_CONTEXT *) Context; + ModuleContextBuffers =3D TestContext->InstallationStructure; + + PrmConfigProtocol =3D AllocateZeroPool (sizeof (*PrmConfigProtocol)); + if (PrmConfigProtocol =3D=3D NULL) { + return UNIT_TEST_ERROR_PREREQUISITE_NOT_MET; + } + + CopyGuid (&PrmConfigProtocol->ModuleContextBuffers.ModuleGuid, &Module= ContextBuffers->ModuleGuid); + PrmConfigProtocol->ModuleContextBuffers.BufferCount =3D ModuleContextB= uffers->BufferCount; + PrmConfigProtocol->ModuleContextBuffers.Buffer =3D ModuleContextBuffer= s->Buffer; + + Status =3D gBS->InstallProtocolInterface ( + &TestContext->Handle, + &gPrmConfigProtocolGuid, + EFI_NATIVE_INTERFACE, + (VOID *) PrmConfigProtocol + ); + UT_ASSERT_NOT_EFI_ERROR (Status); + + return UNIT_TEST_PASSED; +} + +/** + Functional Correctness cleanup function. + + Uninstalls the gPrmConfigProtocolGuid protocol instance as specified b= y the + provided context. This is used to clean up the mocked protocol databas= e after + unit test execution. + + @param[in] Context [Optional] An optional parameter that enable= s: + A pointer to a PRM_CONTEXT_BUFFERS_TEST_CONT= EXT structure with + context information for this unit test. + + @retval UNIT_TEST_PASSED Test case cleanup succeeded. + @retval UNIT_TEST_ERROR_CLEANUP_FAILED Test case cleanup failed. + +**/ +STATIC +UNIT_TEST_STATUS +EFIAPI +DeInitializeFunctionalCorrectness ( + IN UNIT_TEST_CONTEXT Context + ) +{ + EFI_STATUS Status; + PRM_CONFIG_PROTOCOL *PrmConfigProtocol; + PRM_CONTEXT_BUFFERS_TEST_CONTEXT *TestContext; + + UT_ASSERT_NOT_NULL (Context); + TestContext =3D (PRM_CONTEXT_BUFFERS_TEST_CONTEXT *) Context; + + Status =3D gBS->HandleProtocol ( + TestContext->Handle, + &gPrmConfigProtocolGuid, + (VOID **) &PrmConfigProtocol + ); + UT_ASSERT_NOT_EFI_ERROR (Status); + + if (!EFI_ERROR (Status)) { + Status =3D gBS->UninstallProtocolInterface ( + TestContext->Handle, + &gPrmConfigProtocolGuid, + PrmConfigProtocol + ); + UT_ASSERT_NOT_EFI_ERROR (Status); + if (!EFI_ERROR (Status)) { + FreePool (PrmConfigProtocol); + } + } + + return UNIT_TEST_PASSED; +} + +/** + Verifies that the correct PRM_MODULE_CONTEXT_BUFFERS structure instanc= e is found + for a given PRM module or PRM handler GUID. + + @param[in] Context [Optional] An optional context parameter. + A pointer to a PRM_CONTEXT_BUFFERS_TEST_CONT= EXT structure with + context information for this unit test. + + @retval UNIT_TEST_PASSED Unit test case prerequi= sites are met. + @retval UNIT_TEST_ERROR_PREREQUISITE_NOT_MET Test case should be ski= pped.. + +**/ +UNIT_TEST_STATUS +EFIAPI +VerifyGetModuleContextBuffers ( + IN UNIT_TEST_CONTEXT Context + ) +{ + EFI_STATUS Status; + PRM_MODULE_CONTEXT_BUFFERS *ContextBuffers; + PRM_CONTEXT_BUFFERS_TEST_CONTEXT *TestContext; + + ContextBuffers =3D NULL; + TestContext =3D (PRM_CONTEXT_BUFFERS_TEST_CONTEXT *) Context; + + Status =3D GetModuleContextBuffers (TestContext->GuidSearchType, TestC= ontext->Guid, &ContextBuffers); + UT_ASSERT_STATUS_EQUAL (Status, TestContext->ExpectedStatus); + + if (!EFI_ERROR (TestContext->ExpectedStatus)) { + UT_ASSERT_TRUE (CompareGuid (TestContext->ExpectedModuleGuid, &Conte= xtBuffers->ModuleGuid)); + UT_LOG_INFO ( + "%a: Searching by %a GUID ({%g}) returned ContextBuffers at 0x%x\n= ", + __FUNCTION__, + ((TestContext->GuidSearchType =3D=3D ByModuleGuid) ? "module" : "h= andler"), + TestContext->Guid, + (UINTN) ContextBuffers + ); + } + + return UNIT_TEST_PASSED; +} + +/** + Verifies that the expected PRM_CONTEXT_BUFFER instance is found for th= e given HandlerGuid + in the provided PRM_MODULE_CONTEXT_BUFFERS structure. + + @param[in] Context [Optional] An optional context parameter. + A pointer to a PRM_CONTEXT_BUFFERS_TEST_CONT= EXT structure with + context information for this unit test. + + @retval UNIT_TEST_PASSED Unit test case prerequi= sites are met. + @retval UNIT_TEST_ERROR_PREREQUISITE_NOT_MET Test case should be ski= pped.. + +**/ +UNIT_TEST_STATUS +EFIAPI +VerifyFindContextBufferInModuleBuffers ( + IN UNIT_TEST_CONTEXT Context + ) +{ + EFI_STATUS Status; + PRM_CONTEXT_BUFFER *FoundContextBuffer; + PRM_MODULE_CONTEXT_BUFFERS *ContextBuffers; + PRM_CONTEXT_BUFFER_TEST_CONTEXT *TestContext; + + ContextBuffers =3D NULL; + FoundContextBuffer =3D NULL; + TestContext =3D (PRM_CONTEXT_BUFFER_TEST_CONTEXT *) Context; + + Status =3D FindContextBufferInModuleBuffers (TestContext->HandlerGuid,= TestContext->ContextBuffers, &FoundContextBuffer); + UT_ASSERT_STATUS_EQUAL (Status, TestContext->ExpectedStatus); + + if (!EFI_ERROR (TestContext->ExpectedStatus)) { + UT_ASSERT_NOT_NULL (FoundContextBuffer); + UT_ASSERT_TRUE (FoundContextBuffer =3D=3D TestContext->ExpectedConte= xtBuffer); + } + + return UNIT_TEST_PASSED; +} + +/** + Verifies that the expected PRM_CONTEXT_BUFFER instance is found for th= e given HandlerGuid. + + This function checks both the case when a PRM_MODULE_CONTEXT_BUFFERS s= tructure pointer is provided and + not provided. + + NOTES: + - In the future, this function should mock the internal calls to other= library functions but the direct + calls are left in place for now. + - The PrmModuleContextBuffers being NULL is not actually tested at the= moment. In the future, that case + should also be added. + + @param[in] Context [Optional] An optional context parameter. + A pointer to a PRM_CONTEXT_BUFFERS_TEST_CONT= EXT structure with + context information for this unit test. + + @retval UNIT_TEST_PASSED Unit test case prerequi= sites are met. + @retval UNIT_TEST_ERROR_PREREQUISITE_NOT_MET Test case should be ski= pped.. + +**/ +UNIT_TEST_STATUS +EFIAPI +VerifyGetContextBuffer ( + IN UNIT_TEST_CONTEXT Context + ) +{ + EFI_STATUS Status; + PRM_CONTEXT_BUFFER *FoundContextBuffer; + PRM_MODULE_CONTEXT_BUFFERS *ContextBuffers; + PRM_CONTEXT_BUFFER_TEST_CONTEXT *TestContext; + + ContextBuffers =3D NULL; + FoundContextBuffer =3D NULL; + TestContext =3D (PRM_CONTEXT_BUFFER_TEST_CONTEXT *) Context; + + Status =3D GetContextBuffer (TestContext->HandlerGuid, TestContext->Co= ntextBuffers, &FoundContextBuffer); + UT_ASSERT_STATUS_EQUAL (Status, TestContext->ExpectedStatus); + + if (!EFI_ERROR (TestContext->ExpectedStatus)) { + UT_ASSERT_NOT_NULL (FoundContextBuffer); + UT_ASSERT_TRUE (FoundContextBuffer =3D=3D TestContext->ExpectedConte= xtBuffer); + } + + return UNIT_TEST_PASSED; +} + +///=3D=3D=3D TEST ENGINE =3D=3D=3D=3D=3D=3D=3D=3D=3D=3D=3D=3D=3D=3D=3D=3D= =3D=3D=3D=3D=3D=3D=3D=3D=3D=3D=3D=3D=3D=3D=3D=3D=3D=3D=3D=3D=3D=3D=3D=3D=3D= =3D=3D=3D=3D=3D=3D=3D=3D=3D=3D=3D=3D=3D=3D=3D=3D=3D=3D=3D=3D=3D=3D=3D=3D=3D= =3D=3D=3D=3D=3D=3D=3D=3D=3D=3D=3D=3D=3D=3D + +/** + Entry point for the PRM Context Buffer Library unit tests. + + @param[in] ImageHandle The firmware allocated handle for the EFI imag= e. + @param[in] SystemTable A pointer to the EFI System Table. + + @retval EFI_SUCCESS The entry point executed successfully. + @retval other Some error occurred when executing this entry = point. + +**/ +int main () +{ + EFI_STATUS Status; + UINTN Index; + UNIT_TEST_FRAMEWORK_HANDLE Framework; + UNIT_TEST_SUITE_HANDLE BasicTests; + UNIT_TEST_SUITE_HANDLE FunctionalCorrectnessTests; + CHAR8 TestCaseClassNameString[256]; + CHAR8 TestCaseDescriptionString[256]; + + Framework =3D NULL; + + DEBUG ((DEBUG_INFO, "%a v%a\n", UNIT_TEST_NAME, UNIT_TEST_VERSION)); + + // + // Start setting up the test framework for running the tests. + // + Status =3D InitUnitTestFramework (&Framework, UNIT_TEST_NAME, gEfiCall= erBaseName, UNIT_TEST_VERSION); + if (EFI_ERROR (Status)) { + DEBUG ((DEBUG_ERROR, "Failed in InitUnitTestFramework. Status =3D %r= \n", Status)); + goto EXIT; + } + + // + // Add all test suites and tests. + // + Status =3D CreateUnitTestSuite (&BasicTests, Framework, "Basic Context= Buffer Tests", "PrmContextBufferLib.Basic", NULL, NULL); + if (EFI_ERROR (Status)) { + DEBUG ((DEBUG_ERROR, "Failed in CreateUnitTestSuite for PrmContextBu= fferLib.Basic\n")); + Status =3D EFI_OUT_OF_RESOURCES; + goto EXIT; + } + + AddTestCase ( + BasicTests, + "", + "PrmContextBufferLib.Basic.NullPointerGracefulFailure", + NullPointerArgumentsShouldFailGracefully, + NULL, + NULL, + NULL + ); + + Status =3D CreateUnitTestSuite (&FunctionalCorrectnessTests, Framework= , "Functional Correctness Tests", "PrmContextBufferLib.Functional", NULL,= NULL); + if (EFI_ERROR (Status)) { + DEBUG ((DEBUG_ERROR, "Failed in CreateUnitTestSuite for PrmContextBu= fferLib.Functional\n")); + Status =3D EFI_OUT_OF_RESOURCES; + goto EXIT; + } + + // + // Add Functional Correctness unit tests + // + for (Index =3D 0; Index < ARRAY_SIZE (mContextBuffersArray); Index++) = { + ZeroMem (&TestCaseClassNameString[0], ARRAY_SIZE (TestCaseClassNameS= tring)); + ZeroMem (&TestCaseDescriptionString[0], ARRAY_SIZE (TestCaseDescript= ionString)); + + AsciiSPrint ( + &TestCaseClassNameString[0], + ARRAY_SIZE (TestCaseClassNameString), + "PrmContextBufferLib.Functional.VerifyGetModuleContextBuffers%d", + Index + 1 + ); + AsciiSPrint ( + &TestCaseDescriptionString[0], + ARRAY_SIZE (TestCaseDescriptionString), + "Verify Get PRM Module Context Buffers Structure by %a GUID %d\n", + ((mContextBuffersArray[Index]->GuidSearchType =3D=3D ByModuleGuid)= ? "module" : "handler"), + Index + 1 + ); + + AddTestCase ( + FunctionalCorrectnessTests, + &TestCaseDescriptionString[0], + &TestCaseClassNameString[0], + VerifyGetModuleContextBuffers, + InitializeFunctionalCorrectness, + DeInitializeFunctionalCorrectness, + mContextBuffersArray[Index] + ); + } + + for (Index =3D 0; Index < ARRAY_SIZE (mContextBufferArray); Index++) { + ZeroMem (&TestCaseClassNameString[0], ARRAY_SIZE (TestCaseClassNameS= tring)); + ZeroMem (&TestCaseDescriptionString[0], ARRAY_SIZE (TestCaseDescript= ionString)); + + AsciiSPrint ( + &TestCaseClassNameString[0], + ARRAY_SIZE (TestCaseClassNameString), + "PrmContextBufferLib.Functional.VerifyFindContextBufferInModuleBuf= fers%d", + Index + 1 + ); + AsciiSPrint ( + &TestCaseDescriptionString[0], + ARRAY_SIZE (TestCaseDescriptionString), + "Verify Find PRM Context Buffer by Handler GUID %d\n", + Index + 1 + ); + + AddTestCase ( + FunctionalCorrectnessTests, + &TestCaseDescriptionString[0], + &TestCaseClassNameString[0], + VerifyFindContextBufferInModuleBuffers, + NULL, + NULL, + mContextBufferArray[Index] + ); + } + + for (Index =3D 0; Index < ARRAY_SIZE (mContextBufferArray); Index++) { + ZeroMem (&TestCaseClassNameString[0], ARRAY_SIZE (TestCaseClassNameS= tring)); + ZeroMem (&TestCaseDescriptionString[0], ARRAY_SIZE (TestCaseDescript= ionString)); + + AsciiSPrint ( + &TestCaseClassNameString[0], + ARRAY_SIZE (TestCaseClassNameString), + "PrmContextBufferLib.Functional.VerifyGetContextBuffer%d", + Index + 1 + ); + AsciiSPrint ( + &TestCaseDescriptionString[0], + ARRAY_SIZE (TestCaseDescriptionString), + "Verify Get PRM Context Buffer by Handler GUID %d\n", + Index + 1 + ); + + AddTestCase ( + FunctionalCorrectnessTests, + &TestCaseDescriptionString[0], + &TestCaseClassNameString[0], + VerifyGetContextBuffer, + NULL, + NULL, + mContextBufferArray[Index] + ); + } + + // + // Execute the tests. + // + Status =3D RunAllTestSuites (Framework); + +EXIT: + if (Framework) + { + FreeUnitTestFramework (Framework); + } + + return Status; +} diff --git a/PrmPkg/Library/DxePrmContextBufferLib/UnitTest/DxePrmContext= BufferLibUnitTestHost.inf b/PrmPkg/Library/DxePrmContextBufferLib/UnitTes= t/DxePrmContextBufferLibUnitTestHost.inf new file mode 100644 index 000000000000..7cf6a16867d4 --- /dev/null +++ b/PrmPkg/Library/DxePrmContextBufferLib/UnitTest/DxePrmContextBufferL= ibUnitTestHost.inf @@ -0,0 +1,46 @@ +## @file +# PRM Context Buffer Library Host-Based Unit Tests +# +# Copyright (c) Microsoft Corporation +# +# SPDX-License-Identifier: BSD-2-Clause-Patent +# +## + +[Defines] + INF_VERSION =3D 0x00010006 + BASE_NAME =3D PrmContextBufferLibUnitTestHost + FILE_GUID =3D F1FB5F32-BDB5-4391-BD6D-979E90EE2DC= 3 + MODULE_TYPE =3D HOST_APPLICATION + VERSION_STRING =3D 1.0 + +# +# The following information is for reference only and not required by th= e build tools. +# +# VALID_ARCHITECTURES =3D IA32 X64 ARM AARCH64 +# + +[Sources] + DxePrmContextBufferLibUnitTest.c + +[Packages] + MdePkg/MdePkg.dec + MdeModulePkg/MdeModulePkg.dec + UnitTestFrameworkPkg/UnitTestFrameworkPkg.dec + PrmPkg/PrmPkg.dec + +[Guids] + gZeroGuid + +[Protocols] + gPrmConfigProtocolGuid + +[LibraryClasses] + BaseLib + BaseMemoryLib + DebugLib + MemoryAllocationLib + PrintLib + PrmContextBufferLib + UefiBootServicesTableLib + UnitTestLib diff --git a/PrmPkg/Test/PrmPkgHostTest.dsc b/PrmPkg/Test/PrmPkgHostTest.= dsc index 2d718f8888b8..1f44037d0102 100644 --- a/PrmPkg/Test/PrmPkgHostTest.dsc +++ b/PrmPkg/Test/PrmPkgHostTest.dsc @@ -19,6 +19,7 @@ [Defines] !include UnitTestFrameworkPkg/UnitTestFrameworkPkgHost.dsc.inc =20 [LibraryClasses] + PrmContextBufferLib|PrmPkg/Library/DxePrmContextBufferLib/DxePrmContex= tBufferLib.inf UefiBootServicesTableLib|PrmPkg/Test/UnitTest/Library/UefiBootServices= TableLibUnitTest/UefiBootServicesTableLibUnitTest.inf =20 [Components] @@ -26,3 +27,8 @@ [Components] # Unit test helper libraries # PrmPkg/Test/UnitTest/Library/UefiBootServicesTableLibUnitTest/UefiBoot= ServicesTableLibUnitTest.inf + + # + # Unit test host applications + # + PrmPkg/Library/DxePrmContextBufferLib/UnitTest/DxePrmContextBufferLibU= nitTestHost.inf --=20 2.28.0.windows.1