public inbox for devel@edk2.groups.io
 help / color / mirror / Atom feed
* [PATCH EDK2 v1 0/1] MdeModulePkg/BaseBmpSupportLib:Add unit test
@ 2022-11-14 12:07 wenyi,xie
  2022-11-14 12:07 ` [PATCH EDK2 v1 1/1] " wenyi,xie
  0 siblings, 1 reply; 2+ messages in thread
From: wenyi,xie @ 2022-11-14 12:07 UTC (permalink / raw)
  To: devel, jian.j.wang, gaoliming, zhichao.gao, ray.ni
  Cc: songdongkuang, xiewenyi2

Main Changes :
1.Adding two unit test case for TranslateBmpToGopBlt.

Wenyi Xie (1):
  MdeModulePkg/BaseBmpSupportLib:Add unit test

 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(+)
 create mode 100644 MdeModulePkg/Library/BaseBmpSupportLib/UnitTest/BaseBmpSupportLibUnitTest.inf
 create mode 100644 MdeModulePkg/Library/BaseBmpSupportLib/UnitTest/BaseBmpSupportLibUnitTest.c

-- 
2.20.1.windows.1


^ permalink raw reply	[flat|nested] 2+ messages in thread

* [PATCH EDK2 v1 1/1] MdeModulePkg/BaseBmpSupportLib:Add unit test
  2022-11-14 12:07 [PATCH EDK2 v1 0/1] MdeModulePkg/BaseBmpSupportLib:Add unit test wenyi,xie
@ 2022-11-14 12:07 ` wenyi,xie
  0 siblings, 0 replies; 2+ messages in thread
From: wenyi,xie @ 2022-11-14 12:07 UTC (permalink / raw)
  To: devel, jian.j.wang, gaoliming, zhichao.gao, ray.ni
  Cc: songdongkuang, xiewenyi2

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 <jian.j.wang@intel.com>
Cc: Liming Gao <gaoliming@byosoft.com.cn>
Cc: Zhichao Gao <zhichao.gao@intel.com>
Cc: Ray Ni <ray.ni@intel.com>
Signed-off-by: Wenyi Xie <xiewenyi2@huawei.com>
---
 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 {
+    <LibraryClasses>
+      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 <stdio.h>
+#include <string.h>
+#include <stdarg.h>
+#include <stddef.h>
+#include <setjmp.h>
+#include <cmocka.h>
+
+#include <Uefi.h>
+#include <Library/BaseLib.h>
+#include <Library/BaseMemoryLib.h>
+#include <Library/DebugLib.h>
+#include <Library/MemoryAllocationLib.h>
+#include <IndustryStandard/Bmp.h>
+
+#include <Library/UnitTestLib.h>
+#include <Library/BmpSupportLib.h>
+
+#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


^ permalink raw reply related	[flat|nested] 2+ messages in thread

end of thread, other threads:[~2022-11-14 12:08 UTC | newest]

Thread overview: 2+ messages (download: mbox.gz follow: Atom feed
-- links below jump to the message on this page --
2022-11-14 12:07 [PATCH EDK2 v1 0/1] MdeModulePkg/BaseBmpSupportLib:Add unit test wenyi,xie
2022-11-14 12:07 ` [PATCH EDK2 v1 1/1] " wenyi,xie

This is a public inbox, see mirroring instructions
for how to clone and mirror all data and code used for this inbox