From mboxrd@z Thu Jan 1 00:00:00 1970 Received: from szxga08-in.huawei.com (szxga08-in.huawei.com [45.249.212.255]) by mx.groups.io with SMTP id smtpd.web09.4757.1668427688339765151 for ; Mon, 14 Nov 2022 04:08:09 -0800 Authentication-Results: mx.groups.io; dkim=missing; spf=pass (domain: huawei.com, ip: 45.249.212.255, mailfrom: xiewenyi2@huawei.com) Received: from dggemv711-chm.china.huawei.com (unknown [172.30.72.56]) by szxga08-in.huawei.com (SkyGuard) with ESMTP id 4N9p3N4mTcz15MdX; Mon, 14 Nov 2022 20:07:44 +0800 (CST) Received: from kwepemm600004.china.huawei.com (7.193.23.242) by dggemv711-chm.china.huawei.com (10.1.198.66) with Microsoft SMTP Server (version=TLS1_2, cipher=TLS_ECDHE_RSA_WITH_AES_128_GCM_SHA256) id 15.1.2375.31; Mon, 14 Nov 2022 20:08:03 +0800 Received: from kwephisprg16640.huawei.com (10.247.83.252) by kwepemm600004.china.huawei.com (7.193.23.242) with Microsoft SMTP Server (version=TLS1_2, cipher=TLS_ECDHE_RSA_WITH_AES_128_GCM_SHA256) id 15.1.2375.31; Mon, 14 Nov 2022 20:08:02 +0800 From: "wenyi,xie" To: , , , , CC: , Subject: [PATCH EDK2 v1 1/1] MdeModulePkg/BaseBmpSupportLib:Add unit test Date: Mon, 14 Nov 2022 20:07:43 +0800 Message-ID: <20221114120743.3615088-2-xiewenyi2@huawei.com> X-Mailer: git-send-email 2.18.0.huawei.25 In-Reply-To: <20221114120743.3615088-1-xiewenyi2@huawei.com> References: <20221114120743.3615088-1-xiewenyi2@huawei.com> MIME-Version: 1.0 X-Originating-IP: [10.247.83.252] X-ClientProxiedBy: dggems706-chm.china.huawei.com (10.3.19.183) To kwepemm600004.china.huawei.com (7.193.23.242) X-CFilter-Loop: Reflected Content-Type: text/plain Add unit test for function TranslateBmpToGopBlt in BaseBmpSupportLib. As the translation will fail when color map size is zero and this problem is not solved yet, temporarily mask the fail case check. Cc: Jian J Wang Cc: Liming Gao Cc: Zhichao Gao Cc: Ray Ni Signed-off-by: Wenyi Xie --- MdeModulePkg/Test/MdeModulePkgHostTest.dsc | 5 + MdeModulePkg/Library/BaseBmpSupportLib/UnitTest/BaseBmpSupportLibUnitTest.inf | 33 ++ MdeModulePkg/Library/BaseBmpSupportLib/UnitTest/BaseBmpSupportLibUnitTest.c | 345 ++++++++++++++++++++ MdeModulePkg/MdeModulePkg.ci.yaml | 1 + 4 files changed, 384 insertions(+) diff --git a/MdeModulePkg/Test/MdeModulePkgHostTest.dsc b/MdeModulePkg/Test/MdeModulePkgHostTest.dsc index c9ec835df65d..4ca54dcca5c7 100644 --- a/MdeModulePkg/Test/MdeModulePkgHostTest.dsc +++ b/MdeModulePkg/Test/MdeModulePkgHostTest.dsc @@ -47,3 +47,8 @@ [Components] UefiSortLib|MdeModulePkg/Library/UefiSortLib/UefiSortLib.inf DevicePathLib|MdePkg/Library/UefiDevicePathLib/UefiDevicePathLib.inf } + + MdeModulePkg/Library/BaseBmpSupportLib/UnitTest/BaseBmpSupportLibUnitTest.inf { + + BaseBmpSupportLib|MdeModulePkg/Library/BaseBmpSupportLib/BaseBmpSupportLib.inf + } diff --git a/MdeModulePkg/Library/BaseBmpSupportLib/UnitTest/BaseBmpSupportLibUnitTest.inf b/MdeModulePkg/Library/BaseBmpSupportLib/UnitTest/BaseBmpSupportLibUnitTest.inf new file mode 100644 index 000000000000..d4eb6732d2fb --- /dev/null +++ b/MdeModulePkg/Library/BaseBmpSupportLib/UnitTest/BaseBmpSupportLibUnitTest.inf @@ -0,0 +1,33 @@ +## @file +# This is a unit test for the BaseBmpSupportLib. +# +# Copyright (C) Huawei Technologies Co., Ltd. All rights reserved +# SPDX-License-Identifier: BSD-2-Clause-Patent +## + +[Defines] + INF_VERSION = 0x00010017 + BASE_NAME = BaseBmpSupportLibUnitTest + FILE_GUID = EA3E37BF-CA48-6816-406C-B10CFB1DDAD4 + VERSION_STRING = 1.0 + MODULE_TYPE = HOST_APPLICATION + +# +# The following information is for reference only and not required by the build tools. +# +# VALID_ARCHITECTURES = IA32 X64 +# + +[Sources] + BaseBmpSupportLibUnitTest.c + +[Packages] + MdePkg/MdePkg.dec + MdeModulePkg/MdeModulePkg.dec + UnitTestFrameworkPkg/UnitTestFrameworkPkg.dec + +[LibraryClasses] + UnitTestLib + DebugLib + BaseMemoryLib + BaseBmpSupportLib diff --git a/MdeModulePkg/Library/BaseBmpSupportLib/UnitTest/BaseBmpSupportLibUnitTest.c b/MdeModulePkg/Library/BaseBmpSupportLib/UnitTest/BaseBmpSupportLibUnitTest.c new file mode 100644 index 000000000000..dd3520cb97a9 --- /dev/null +++ b/MdeModulePkg/Library/BaseBmpSupportLib/UnitTest/BaseBmpSupportLibUnitTest.c @@ -0,0 +1,345 @@ +/** @file + Unit tests of the BaseBmpSupportLib + + Copyright (C) Huawei Technologies Co., Ltd. All rights reserved + SPDX-License-Identifier: BSD-2-Clause-Patent + +**/ + +#include +#include +#include +#include +#include +#include + +#include +#include +#include +#include +#include +#include + +#include +#include + +#define UNIT_TEST_APP_NAME "BaseBmpSupportLib Unit Tests" +#define UNIT_TEST_APP_VERSION "1.0" + +// Bmp file header template +static BMP_IMAGE_HEADER mBmpHeader = { + 'B', // CharB + 'M', // CharM + 0, // Image Size + { 0, 0 }, // Reserved + sizeof (BMP_IMAGE_HEADER), // ImageOffset + sizeof (BMP_IMAGE_HEADER) - OFFSET_OF (BMP_IMAGE_HEADER, HeaderSize), // HeaderSize + 4, // PixelWidth + 4, // PixelHeight + 1, // Planes + 8, // BitPerPixel + 0, // CompressionType + 0, // DataSize + 0, // XPixelsPerMeter + 0, // YPixelsPerMeter + 0, // NumberOfColors + 0 // ImportantColors +}; + +/** + Create the bmp file. + + @param BmpImage Bmp file image. + @param PixelHeight Bmp file Height. + @param PixelWidth Bmp file Width. + @param BitPerPixel Bit per pixel. + @param ColorMap Color map. + @param ColorMapSize Color map size. + @param Data Bmp file data. + @param DataSize Bmp file data size. + + @retval UNIT_TEST_PASSED Test passed. + @retval others Test failed. +**/ +RETURN_STATUS +CreateBmpFile ( + IN OUT VOID **BmpImage, + IN UINT32 PixelHeight, + IN UINT32 PixelWidth, + IN UINT16 BitPerPixel, + IN UINT8 *ColorMap, + IN UINT32 ColorMapSize, + IN UINT8 *Data, + IN UINT32 DataSize + ) +{ + UINT32 length; + BMP_IMAGE_HEADER Header; + + length = 0; + CopyMem (&Header, &mBmpHeader, sizeof (BMP_IMAGE_HEADER)); + Header.Size = sizeof (BMP_IMAGE_HEADER) + ColorMapSize + DataSize; + Header.PixelWidth = PixelHeight; + Header.PixelHeight = PixelWidth; + Header.BitPerPixel = BitPerPixel; + Header.ImageOffset = Header.Size - sizeof (BMP_IMAGE_HEADER) - ColorMapSize; + Header.ImageSize = DataSize; + + *BmpImage = AllocateZeroPool (Header.Size); + if (*BmpImage == NULL) { + return EFI_OUT_OF_RESOURCES; + } + + CopyMem ((UINT8 *)*BmpImage, &Header, sizeof (BMP_IMAGE_HEADER)); + + length = sizeof (BMP_IMAGE_HEADER); + if ((ColorMap != NULL) && (ColorMapSize != 0)) { + CopyMem ((UINT8 *)*BmpImage + length, ColorMap, ColorMapSize); + } + + length += ColorMapSize; + if ((Data != NULL) && (DataSize != 0)) { + CopyMem ((UINT8 *)*BmpImage + length, Data, DataSize); + } + + return EFI_SUCCESS; +} + +/** + Unit test for TranslateBmpToGopBlt () API of the BaseBmpSupportLib. + + @param[in] Context [Optional] An optional parameter that enables: + 1) test-case reuse with varied parameters and + 2) test-case re-entry for Target tests that need a + reboot. This parameter is a VOID* and it is the + responsibility of the test author to ensure that the + contents are well understood by all test cases that may + consume it. + + @retval UNIT_TEST_PASSED The Unit test has completed and the test + case was successful. + @retval UNIT_TEST_ERROR_TEST_FAILED A test case assertion has failed. +**/ +UNIT_TEST_STATUS +EFIAPI +TranslateBmpShouldSuccess ( + IN UNIT_TEST_CONTEXT Context + ) +{ + VOID *BmpImage; + EFI_GRAPHICS_OUTPUT_BLT_PIXEL *GopBlt; + UINTN GopBltSize; + UINTN PixelHeight; + UINTN PixelWidth; + UINT32 ImageHeight; + UINT32 ImageWidth; + UINT16 BitPerPixel; + UINT8 *ColorMap; + UINT32 ColorMapSize; + UINT8 *Data; + UINT32 DataSize; + UINT32 Size; + RETURN_STATUS Status; + + ColorMap = NULL; + Data = NULL; + ImageHeight = 4; + ImageWidth = 4; + BitPerPixel = 8; + ColorMapSize = 256 * 4; + DataSize = 0x10; + Size = sizeof (BMP_IMAGE_HEADER) + ColorMapSize + DataSize; + + // + // Set color map to NULL so that Translate will fail. + // + ColorMap = AllocateZeroPool (ColorMapSize); + if (ColorMap == NULL) { + return UNIT_TEST_ERROR_TEST_FAILED; + } + + // + // Initial the data section in image file. + // + Data = AllocateZeroPool (DataSize); + if (Data == NULL) { + return UNIT_TEST_ERROR_TEST_FAILED; + } + + Status = CreateBmpFile (&BmpImage, ImageHeight, ImageWidth, BitPerPixel, ColorMap, ColorMapSize, Data, DataSize); + + Status = TranslateBmpToGopBlt (BmpImage, Size, &GopBlt, &GopBltSize, &PixelHeight, &PixelWidth); + UT_ASSERT_TRUE (EFI_ERROR (Status)); + + if (BmpImage != NULL) { + FreePool (BmpImage); + } + + if (ColorMap != NULL) { + FreePool (ColorMap); + } + + if (Data != NULL) { + FreePool (Data); + } + + return UNIT_TEST_PASSED; +} + +/** + Unit test for TranslateBmpToGopBlt () API of the BaseBmpSupportLib. + + @param[in] Context [Optional] An optional parameter that enables: + 1) test-case reuse with varied parameters and + 2) test-case re-entry for Target tests that need a + reboot. This parameter is a VOID* and it is the + responsibility of the test author to ensure that the + contents are well understood by all test cases that may + consume it. + + @retval UNIT_TEST_PASSED The Unit test has completed and the test + case was successful. + @retval UNIT_TEST_ERROR_TEST_FAILED A test case assertion has failed. +**/ +UNIT_TEST_STATUS +EFIAPI +TranslateBmpShouldFailed ( + IN UNIT_TEST_CONTEXT Context + ) +{ + VOID *BmpImage; + EFI_GRAPHICS_OUTPUT_BLT_PIXEL *GopBlt; + UINTN GopBltSize; + UINTN PixelHeight; + UINTN PixelWidth; + UINT32 ImageHeight; + UINT32 ImageWidth; + UINT16 BitPerPixel; + UINT8 *ColorMap; + UINT32 ColorMapSize; + UINT8 *Data; + UINT32 DataSize; + UINT32 Size; + + ImageHeight = 4; + ImageWidth = 4; + BitPerPixel = 8; + DataSize = 0x10; + // + // Set color map to NULL so that Translate will fail. + // + ColorMap = NULL; + ColorMapSize = 0; + Size = sizeof (BMP_IMAGE_HEADER) + ColorMapSize + DataSize; + + // + // Initial the data section in image file. + // + Data = AllocateZeroPool (DataSize); + if (Data == NULL) { + return UNIT_TEST_ERROR_TEST_FAILED; + } + + CreateBmpFile (&BmpImage, ImageHeight, ImageWidth, BitPerPixel, ColorMap, ColorMapSize, Data, DataSize); + + TranslateBmpToGopBlt (BmpImage, Size, &GopBlt, &GopBltSize, &PixelHeight, &PixelWidth); + + if (BmpImage != NULL) { + FreePool (BmpImage); + } + + if (ColorMap != NULL) { + FreePool (ColorMap); + } + + if (Data != NULL) { + FreePool (Data); + } + + return UNIT_TEST_PASSED; +} + +/** + Initialze the unit test framework, suite, and unit tests for the + BaseBmpSupportLib and run the BaseBmpSupportLib unit test. + + @retval EFI_SUCCESS All test cases were dispatched. + @retval EFI_OUT_OF_RESOURCES There are not enough resources available to + initialize the unit tests. +**/ +STATIC +EFI_STATUS +EFIAPI +UnitTestingEntry ( + VOID + ) +{ + EFI_STATUS Status; + UNIT_TEST_FRAMEWORK_HANDLE Framework; + UNIT_TEST_SUITE_HANDLE SortTests; + + Framework = NULL; + + DEBUG ((DEBUG_INFO, "%a v%a\n", UNIT_TEST_APP_NAME, UNIT_TEST_APP_VERSION)); + + // + // Start setting up the test framework for running the tests. + // + Status = InitUnitTestFramework (&Framework, UNIT_TEST_APP_NAME, gEfiCallerBaseName, UNIT_TEST_APP_VERSION); + if (EFI_ERROR (Status)) { + DEBUG ((DEBUG_ERROR, "Failed in InitUnitTestFramework. Status = %r\n", Status)); + goto EXIT; + } + + // + // Populate the BaseBmpSupportLib Unit Test Suite. + // + Status = CreateUnitTestSuite (&SortTests, Framework, "BaseBmpSupportLib Translate Tests", "BaseBmpSupportLib", NULL, NULL); + if (EFI_ERROR (Status)) { + DEBUG ((DEBUG_ERROR, "Failed in CreateUnitTestSuite for BaseBmpSupportLib API Tests\n")); + Status = EFI_OUT_OF_RESOURCES; + goto EXIT; + } + + // + // --------------Suite--------Description------------Name--------------Function-------------Pre---Post---Context--- + // + AddTestCase (SortTests, "Translate Bmp1 To GopBlt", "Translate1", TranslateBmpShouldSuccess, NULL, NULL, NULL); + AddTestCase (SortTests, "Translate Bmp2 To GopBlt", "Translate2", TranslateBmpShouldFailed, NULL, NULL, NULL); + + // + // Execute the tests. + // + Status = RunAllTestSuites (Framework); + +EXIT: + if (Framework) { + FreeUnitTestFramework (Framework); + } + + return Status; +} + +/// +/// Avoid ECC error for function name that starts with lower case letter +/// +#define BaseBmpSupportLibUnitTestMain main + +/** + Standard POSIX C entry point for host based unit test execution. + + @param[in] Argc Number of arguments + @param[in] Argv Array of pointers to arguments + + @retval 0 Success + @retval other Error +**/ +INT32 +BaseBmpSupportLibUnitTestMain ( + IN INT32 Argc, + IN CHAR8 *Argv[] + ) +{ + UnitTestingEntry (); + return 0; +} diff --git a/MdeModulePkg/MdeModulePkg.ci.yaml b/MdeModulePkg/MdeModulePkg.ci.yaml index f69989087b4c..5d64a7e47116 100644 --- a/MdeModulePkg/MdeModulePkg.ci.yaml +++ b/MdeModulePkg/MdeModulePkg.ci.yaml @@ -20,6 +20,7 @@ "8005", "UNIVERSAL_PAYLOAD_PCI_ROOT_BRIDGE.UID", "8005", "UNIVERSAL_PAYLOAD_PCI_ROOT_BRIDGE.HID", "8001", "UefiSortLibUnitTestMain", + "8001", "BaseBmpSupportLibUnitTestMain", ], ## Both file path and directory path are accepted. "IgnoreFiles": [ -- 2.20.1.windows.1