]> git.proxmox.com Git - mirror_edk2.git/commitdiff
MdeModulePkg: Add DxeResetSystemLib unit test
authorMichael D Kinney <michael.d.kinney@intel.com>
Wed, 22 Jan 2020 18:16:14 +0000 (10:16 -0800)
committermergify[bot] <37929162+mergify[bot]@users.noreply.github.com>
Fri, 7 Feb 2020 19:18:53 +0000 (19:18 +0000)
https://bugzilla.tianocore.org/show_bug.cgi?id=2505

* Add unit test of DxeResetSystemLib library
  instance that uses cmocka interfaces to mock the
  UEFI Runtime Services Table and its ResetSystem()
  service.  When a unit test uses the cmocka
  interfaces, the unit test does not support being
  run from target environments.

  cmocka APIs: https://api.cmocka.org/index.html

  This example puts the unit test in a UnitTest
  directory below the library INF file and this location
  means the unit test is only designed to work this
  this one library instance.

* Add Test/MdeModulePkgHostTest.dsc to build host
  based unit tests

Cc: Sean Brogan <sean.brogan@microsoft.com>
Cc: Bret Barkelew <Bret.Barkelew@microsoft.com>
Cc: Liming Gao <liming.gao@intel.com>
Cc: Hao A Wu <hao.a.wu@intel.com>
Signed-off-by: Michael D Kinney <michael.d.kinney@intel.com>
Acked-by: Hao A Wu <hao.a.wu@intel.com>
Reviewed-by: Bret Barkelew <Bret.Barkelew@microsoft.com>
MdeModulePkg/Library/DxeResetSystemLib/UnitTest/DxeResetSystemLibUnitTest.c [new file with mode: 0644]
MdeModulePkg/Library/DxeResetSystemLib/UnitTest/DxeResetSystemLibUnitTestHost.inf [new file with mode: 0644]
MdeModulePkg/Library/DxeResetSystemLib/UnitTest/MockUefiRuntimeServicesTableLib.c [new file with mode: 0644]
MdeModulePkg/Library/DxeResetSystemLib/UnitTest/MockUefiRuntimeServicesTableLib.inf [new file with mode: 0644]
MdeModulePkg/MdeModulePkg.ci.yaml
MdeModulePkg/Test/MdeModulePkgHostTest.dsc [new file with mode: 0644]

diff --git a/MdeModulePkg/Library/DxeResetSystemLib/UnitTest/DxeResetSystemLibUnitTest.c b/MdeModulePkg/Library/DxeResetSystemLib/UnitTest/DxeResetSystemLibUnitTest.c
new file mode 100644 (file)
index 0000000..3bba38b
--- /dev/null
@@ -0,0 +1,312 @@
+/** @file\r
+  Unit tests of the DxeResetSystemLib instance of the ResetSystemLib class\r
+\r
+  Copyright (C) Microsoft Corporation.\r
+  SPDX-License-Identifier: BSD-2-Clause-Patent\r
+\r
+**/\r
+\r
+#include <stdio.h>\r
+#include <string.h>\r
+#include <stdarg.h>\r
+#include <stddef.h>\r
+#include <setjmp.h>\r
+#include <cmocka.h>\r
+\r
+#include <Uefi.h>\r
+#include <Library/BaseLib.h>\r
+#include <Library/BaseMemoryLib.h>\r
+#include <Library/DebugLib.h>\r
+#include <Library/MemoryAllocationLib.h>\r
+\r
+#include <Library/UnitTestLib.h>\r
+#include <Library/ResetSystemLib.h>\r
+\r
+#define UNIT_TEST_APP_NAME        "DxeResetSystemLib Unit Tests"\r
+#define UNIT_TEST_APP_VERSION     "1.0"\r
+\r
+/**\r
+  Resets the entire platform.\r
+\r
+  @param[in]  ResetType         The type of reset to perform.\r
+  @param[in]  ResetStatus       The status code for the reset.\r
+  @param[in]  DataSize          The size, in bytes, of ResetData.\r
+  @param[in]  ResetData         For a ResetType of EfiResetCold, EfiResetWarm, or\r
+                                EfiResetShutdown the data buffer starts with a Null-terminated\r
+                                string, optionally followed by additional binary data.\r
+                                The string is a description that the caller may use to further\r
+                                indicate the reason for the system reset.\r
+                                For a ResetType of EfiResetPlatformSpecific the data buffer\r
+                                also starts with a Null-terminated string that is followed\r
+                                by an EFI_GUID that describes the specific type of reset to perform.\r
+**/\r
+STATIC\r
+VOID\r
+EFIAPI\r
+MockResetSystem (\r
+  IN EFI_RESET_TYPE           ResetType,\r
+  IN EFI_STATUS               ResetStatus,\r
+  IN UINTN                    DataSize,\r
+  IN VOID                     *ResetData OPTIONAL\r
+  )\r
+{\r
+  check_expected_ptr (ResetType);\r
+  check_expected_ptr (ResetStatus);\r
+\r
+  //\r
+  // NOTE: Mocked functions can also return values, but that\r
+  //       is for another demo.\r
+}\r
+\r
+///\r
+/// Mock version of the UEFI Runtime Services Table\r
+///\r
+EFI_RUNTIME_SERVICES  MockRuntime = {\r
+  {\r
+    EFI_RUNTIME_SERVICES_SIGNATURE,     // Signature\r
+    EFI_RUNTIME_SERVICES_REVISION,      // Revision\r
+    sizeof (EFI_RUNTIME_SERVICES),      // HeaderSize\r
+    0,                                  // CRC32\r
+    0                                   // Reserved\r
+  },\r
+  NULL,               // GetTime\r
+  NULL,               // SetTime\r
+  NULL,               // GetWakeupTime\r
+  NULL,               // SetWakeupTime\r
+  NULL,               // SetVirtualAddressMap\r
+  NULL,               // ConvertPointer\r
+  NULL,               // GetVariable\r
+  NULL,               // GetNextVariableName\r
+  NULL,               // SetVariable\r
+  NULL,               // GetNextHighMonotonicCount\r
+  MockResetSystem,    // ResetSystem\r
+  NULL,               // UpdateCapsule\r
+  NULL,               // QueryCapsuleCapabilities\r
+  NULL                // QueryVariableInfo\r
+};\r
+\r
+/**\r
+  Unit test for ColdReset () API of the ResetSystemLib.\r
+\r
+  @param[in]  Context    [Optional] An optional parameter that enables:\r
+                         1) test-case reuse with varied parameters and\r
+                         2) test-case re-entry for Target tests that need a\r
+                         reboot.  This parameter is a VOID* and it is the\r
+                         responsibility of the test author to ensure that the\r
+                         contents are well understood by all test cases that may\r
+                         consume it.\r
+\r
+  @retval  UNIT_TEST_PASSED             The Unit test has completed and the test\r
+                                        case was successful.\r
+  @retval  UNIT_TEST_ERROR_TEST_FAILED  A test case assertion has failed.\r
+**/\r
+UNIT_TEST_STATUS\r
+EFIAPI\r
+ResetColdShouldIssueAColdReset (\r
+  IN UNIT_TEST_CONTEXT  Context\r
+  )\r
+{\r
+  expect_value (MockResetSystem, ResetType, EfiResetCold);\r
+  expect_value (MockResetSystem, ResetStatus, EFI_SUCCESS);\r
+\r
+  ResetCold ();\r
+\r
+  return UNIT_TEST_PASSED;\r
+}\r
+\r
+/**\r
+  Unit test for WarmReset () API of the ResetSystemLib.\r
+\r
+  @param[in]  Context    [Optional] An optional parameter that enables:\r
+                         1) test-case reuse with varied parameters and\r
+                         2) test-case re-entry for Target tests that need a\r
+                         reboot.  This parameter is a VOID* and it is the\r
+                         responsibility of the test author to ensure that the\r
+                         contents are well understood by all test cases that may\r
+                         consume it.\r
+\r
+  @retval  UNIT_TEST_PASSED             The Unit test has completed and the test\r
+                                        case was successful.\r
+  @retval  UNIT_TEST_ERROR_TEST_FAILED  A test case assertion has failed.\r
+**/\r
+UNIT_TEST_STATUS\r
+EFIAPI\r
+ResetWarmShouldIssueAWarmReset (\r
+  IN UNIT_TEST_CONTEXT  Context\r
+  )\r
+{\r
+  expect_value (MockResetSystem, ResetType, EfiResetWarm);\r
+  expect_value (MockResetSystem, ResetStatus, EFI_SUCCESS);\r
+\r
+  ResetWarm ();\r
+\r
+  return UNIT_TEST_PASSED;\r
+}\r
+\r
+/**\r
+  Unit test for ResetShutdown () API of the ResetSystemLib.\r
+\r
+  @param[in]  Context    [Optional] An optional parameter that enables:\r
+                         1) test-case reuse with varied parameters and\r
+                         2) test-case re-entry for Target tests that need a\r
+                         reboot.  This parameter is a VOID* and it is the\r
+                         responsibility of the test author to ensure that the\r
+                         contents are well understood by all test cases that may\r
+                         consume it.\r
+\r
+  @retval  UNIT_TEST_PASSED             The Unit test has completed and the test\r
+                                        case was successful.\r
+  @retval  UNIT_TEST_ERROR_TEST_FAILED  A test case assertion has failed.\r
+**/\r
+UNIT_TEST_STATUS\r
+EFIAPI\r
+ResetShutdownShouldIssueAShutdown (\r
+  IN UNIT_TEST_CONTEXT  Context\r
+  )\r
+{\r
+  expect_value (MockResetSystem, ResetType, EfiResetShutdown);\r
+  expect_value (MockResetSystem, ResetStatus, EFI_SUCCESS);\r
+\r
+  ResetShutdown ();\r
+\r
+  return UNIT_TEST_PASSED;\r
+}\r
+\r
+/**\r
+  Unit test for ResetPlatformSpecific () API of the ResetSystemLib.\r
+\r
+  @param[in]  Context    [Optional] An optional parameter that enables:\r
+                         1) test-case reuse with varied parameters and\r
+                         2) test-case re-entry for Target tests that need a\r
+                         reboot.  This parameter is a VOID* and it is the\r
+                         responsibility of the test author to ensure that the\r
+                         contents are well understood by all test cases that may\r
+                         consume it.\r
+\r
+  @retval  UNIT_TEST_PASSED             The Unit test has completed and the test\r
+                                        case was successful.\r
+  @retval  UNIT_TEST_ERROR_TEST_FAILED  A test case assertion has failed.\r
+**/\r
+UNIT_TEST_STATUS\r
+EFIAPI\r
+ResetPlatformSpecificShouldIssueAPlatformSpecificReset (\r
+  IN UNIT_TEST_CONTEXT  Context\r
+  )\r
+{\r
+  expect_value (MockResetSystem, ResetType, EfiResetPlatformSpecific);\r
+  expect_value (MockResetSystem, ResetStatus, EFI_SUCCESS);\r
+\r
+  ResetPlatformSpecific (0, NULL);\r
+\r
+  return UNIT_TEST_PASSED;\r
+}\r
+\r
+/**\r
+  Unit test for ResetSystem () API of the ResetSystemLib.\r
+\r
+  @param[in]  Context    [Optional] An optional parameter that enables:\r
+                         1) test-case reuse with varied parameters and\r
+                         2) test-case re-entry for Target tests that need a\r
+                         reboot.  This parameter is a VOID* and it is the\r
+                         responsibility of the test author to ensure that the\r
+                         contents are well understood by all test cases that may\r
+                         consume it.\r
+\r
+  @retval  UNIT_TEST_PASSED             The Unit test has completed and the test\r
+                                        case was successful.\r
+  @retval  UNIT_TEST_ERROR_TEST_FAILED  A test case assertion has failed.\r
+**/\r
+UNIT_TEST_STATUS\r
+EFIAPI\r
+ResetSystemShouldPassTheParametersThrough (\r
+  IN UNIT_TEST_CONTEXT  Context\r
+  )\r
+{\r
+  expect_value (MockResetSystem, ResetType, EfiResetCold);\r
+  expect_value (MockResetSystem, ResetStatus, EFI_SUCCESS);\r
+\r
+  ResetSystem (EfiResetCold, EFI_SUCCESS, 0, NULL);\r
+\r
+  expect_value (MockResetSystem, ResetType, EfiResetShutdown);\r
+  expect_value (MockResetSystem, ResetStatus, EFI_SUCCESS);\r
+\r
+  ResetSystem (EfiResetShutdown, EFI_SUCCESS, 0, NULL);\r
+\r
+  return UNIT_TEST_PASSED;\r
+}\r
+\r
+/**\r
+  Initialze the unit test framework, suite, and unit tests for the\r
+  ResetSystemLib and run the ResetSystemLib unit test.\r
+\r
+  @retval  EFI_SUCCESS           All test cases were dispatched.\r
+  @retval  EFI_OUT_OF_RESOURCES  There are not enough resources available to\r
+                                 initialize the unit tests.\r
+**/\r
+STATIC\r
+EFI_STATUS\r
+EFIAPI\r
+UnitTestingEntry (\r
+  VOID\r
+  )\r
+{\r
+  EFI_STATUS                  Status;\r
+  UNIT_TEST_FRAMEWORK_HANDLE  Framework;\r
+  UNIT_TEST_SUITE_HANDLE      ResetTests;\r
+\r
+  Framework = NULL;\r
+\r
+  DEBUG(( DEBUG_INFO, "%a v%a\n", UNIT_TEST_APP_NAME, UNIT_TEST_APP_VERSION ));\r
+\r
+  //\r
+  // Start setting up the test framework for running the tests.\r
+  //\r
+  Status = InitUnitTestFramework (&Framework, UNIT_TEST_APP_NAME, gEfiCallerBaseName, UNIT_TEST_APP_VERSION);\r
+  if (EFI_ERROR (Status)) {\r
+      DEBUG ((DEBUG_ERROR, "Failed in InitUnitTestFramework. Status = %r\n", Status));\r
+      goto EXIT;\r
+  }\r
+\r
+  //\r
+  // Populate the ResetSytemLib Unit Test Suite.\r
+  //\r
+  Status = CreateUnitTestSuite (&ResetTests, Framework, "DxeResetSystemLib Reset Tests", "ResetSystemLib.Reset", NULL, NULL);\r
+  if (EFI_ERROR (Status)) {\r
+    DEBUG ((DEBUG_ERROR, "Failed in CreateUnitTestSuite for ResetTests\n"));\r
+    Status = EFI_OUT_OF_RESOURCES;\r
+    goto EXIT;\r
+  }\r
+\r
+  //\r
+  // --------------Suite-----------Description--------------Name----------Function--------Pre---Post-------------------Context-----------\r
+  //\r
+  AddTestCase (ResetTests, "ResetCold should issue a cold reset", "Cold", ResetColdShouldIssueAColdReset, NULL, NULL, NULL);\r
+  AddTestCase (ResetTests, "ResetWarm should issue a warm reset", "Warm", ResetWarmShouldIssueAWarmReset, NULL, NULL, NULL);\r
+  AddTestCase (ResetTests, "ResetShutdown should issue a shutdown", "Shutdown", ResetShutdownShouldIssueAShutdown, NULL, NULL, NULL);\r
+  AddTestCase (ResetTests, "ResetPlatformSpecific should issue a platform-specific reset", "Platform", ResetPlatformSpecificShouldIssueAPlatformSpecificReset, NULL, NULL, NULL);\r
+  AddTestCase (ResetTests, "ResetSystem should pass all parameters through", "Parameters", ResetSystemShouldPassTheParametersThrough, NULL, NULL, NULL);\r
+\r
+  //\r
+  // Execute the tests.\r
+  //\r
+  Status = RunAllTestSuites (Framework);\r
+\r
+EXIT:\r
+  if (Framework) {\r
+    FreeUnitTestFramework (Framework);\r
+  }\r
+\r
+  return Status;\r
+}\r
+\r
+/**\r
+  Standard POSIX C entry point for host based unit test execution.\r
+**/\r
+int\r
+main (\r
+  int argc,\r
+  char *argv[]\r
+  )\r
+{\r
+  return UnitTestingEntry ();\r
+}\r
diff --git a/MdeModulePkg/Library/DxeResetSystemLib/UnitTest/DxeResetSystemLibUnitTestHost.inf b/MdeModulePkg/Library/DxeResetSystemLib/UnitTest/DxeResetSystemLibUnitTestHost.inf
new file mode 100644 (file)
index 0000000..54f968e
--- /dev/null
@@ -0,0 +1,34 @@
+## @file\r
+# Unit tests of the DxeResetSystemLib instance of the ResetSystemLib class\r
+#\r
+# Copyright (C) Microsoft Corporation.\r
+# SPDX-License-Identifier: BSD-2-Clause-Patent\r
+##\r
+\r
+[Defines]\r
+  INF_VERSION                    = 0x00010006\r
+  BASE_NAME                      = DxeResetSystemLibUnitTestHost\r
+  FILE_GUID                      = 83E35653-B943-4C5F-BA08-9B2996AE9273\r
+  MODULE_TYPE                    = HOST_APPLICATION\r
+  VERSION_STRING                 = 1.0\r
+\r
+#\r
+# The following information is for reference only and not required by the build tools.\r
+#\r
+#  VALID_ARCHITECTURES           = IA32 X64\r
+#\r
+\r
+[Sources]\r
+  DxeResetSystemLibUnitTest.c\r
+\r
+[Packages]\r
+  MdePkg/MdePkg.dec\r
+  MdeModulePkg/MdeModulePkg.dec\r
+  UnitTestFrameworkPkg/UnitTestFrameworkPkg.dec\r
+\r
+[LibraryClasses]\r
+  ResetSystemLib\r
+  BaseLib\r
+  BaseMemoryLib\r
+  DebugLib\r
+  UnitTestLib\r
diff --git a/MdeModulePkg/Library/DxeResetSystemLib/UnitTest/MockUefiRuntimeServicesTableLib.c b/MdeModulePkg/Library/DxeResetSystemLib/UnitTest/MockUefiRuntimeServicesTableLib.c
new file mode 100644 (file)
index 0000000..3540e1c
--- /dev/null
@@ -0,0 +1,13 @@
+/** @file\r
+  Mock implementation of the UEFI Runtime Services Table Library.\r
+\r
+  Copyright (C) Microsoft Corporation.\r
+  SPDX-License-Identifier: BSD-2-Clause-Patent\r
+\r
+**/\r
+\r
+#include <Uefi.h>\r
+\r
+extern EFI_RUNTIME_SERVICES  MockRuntime;\r
+\r
+EFI_RUNTIME_SERVICES  *gRT = &MockRuntime;\r
diff --git a/MdeModulePkg/Library/DxeResetSystemLib/UnitTest/MockUefiRuntimeServicesTableLib.inf b/MdeModulePkg/Library/DxeResetSystemLib/UnitTest/MockUefiRuntimeServicesTableLib.inf
new file mode 100644 (file)
index 0000000..e716b85
--- /dev/null
@@ -0,0 +1,25 @@
+## @file\r
+#  Mock implementation of the UEFI Runtime Services Table Library.\r
+#\r
+#  Copyright (c) 2020, Intel Corporation. All rights reserved.<BR>\r
+#  SPDX-License-Identifier: BSD-2-Clause-Patent\r
+#\r
+##\r
+\r
+[Defines]\r
+  INF_VERSION                    = 0x00010005\r
+  BASE_NAME                      = MockUefiRuntimeServicesTableLib\r
+  FILE_GUID                      = 4EA215EE-85C1-4A0A-847F-D2A8DE20805F\r
+  MODULE_TYPE                    = UEFI_DRIVER\r
+  VERSION_STRING                 = 1.0\r
+  LIBRARY_CLASS                  = UefiRuntimeServicesTableLib|HOST_APPLICATION\r
+\r
+#\r
+#  VALID_ARCHITECTURES           = IA32 X64 EBC\r
+#\r
+\r
+[Sources]\r
+  MockUefiRuntimeServicesTableLib.c\r
+\r
+[Packages]\r
+  MdePkg/MdePkg.dec\r
index 0bf149f205e6dd81c6f3e407c8a5ca4785b862db..3b6e747075719bdbf50d7f5e81921dfaa480815b 100644 (file)
@@ -9,6 +9,10 @@
     "CompilerPlugin": {\r
         "DscPath": "MdeModulePkg.dsc"\r
     },\r
+    ## options defined ci/Plugin/HostUnitTestCompilerPlugin\r
+    "HostUnitTestCompilerPlugin": {\r
+        "DscPath": "Test/MdeModulePkgHostTest.dsc"\r
+    },\r
 \r
     ## options defined ci/Plugin/CharEncodingCheck\r
     "CharEncodingCheck": {\r
@@ -24,7 +28,9 @@
             "ArmPkg/ArmPkg.dec"  # this should be fixed by promoting an abstraction\r
         ],\r
         # For host based unit tests\r
-        "AcceptableDependencies-HOST_APPLICATION":[],\r
+        "AcceptableDependencies-HOST_APPLICATION":[\r
+            "UnitTestFrameworkPkg/UnitTestFrameworkPkg.dec"\r
+        ],\r
         # For UEFI shell based apps\r
         "AcceptableDependencies-UEFI_APPLICATION":[],\r
         "IgnoreInf": []\r
         "IgnoreInf": [],\r
         "DscPath": "MdeModulePkg.dsc"\r
     },\r
+    ## options defined ci/Plugin/HostUnitTestDscCompleteCheck\r
+    "HostUnitTestDscCompleteCheck": {\r
+        "IgnoreInf": [""],\r
+        "DscPath": "Test/MdeModulePkgHostTest.dsc"\r
+    },\r
 \r
     ## options defined ci/Plugin/GuidCheck\r
     "GuidCheck": {\r
diff --git a/MdeModulePkg/Test/MdeModulePkgHostTest.dsc b/MdeModulePkg/Test/MdeModulePkgHostTest.dsc
new file mode 100644 (file)
index 0000000..72a119d
--- /dev/null
@@ -0,0 +1,32 @@
+## @file\r
+# MdeModulePkg DSC file used to build host-based unit tests.\r
+#\r
+# Copyright (c) 2019 - 2020, Intel Corporation. All rights reserved.<BR>\r
+# Copyright (C) Microsoft Corporation.\r
+# SPDX-License-Identifier: BSD-2-Clause-Patent\r
+#\r
+##\r
+\r
+[Defines]\r
+  PLATFORM_NAME           = MdeModulePkgHostTest\r
+  PLATFORM_GUID           = F74AF7C6-698C-4EBA-BA49-FF6816916354\r
+  PLATFORM_VERSION        = 0.1\r
+  DSC_SPECIFICATION       = 0x00010005\r
+  OUTPUT_DIRECTORY        = Build/MdeModulePkg/HostTest\r
+  SUPPORTED_ARCHITECTURES = IA32|X64\r
+  BUILD_TARGETS           = NOOPT\r
+  SKUID_IDENTIFIER        = DEFAULT\r
+\r
+!include UnitTestFrameworkPkg/UnitTestFrameworkPkgHost.dsc.inc\r
+\r
+[Components]\r
+  MdeModulePkg/Library/DxeResetSystemLib/UnitTest/MockUefiRuntimeServicesTableLib.inf\r
+\r
+  #\r
+  # Build MdeModulePkg HOST_APPLICATION Tests\r
+  #\r
+  MdeModulePkg/Library/DxeResetSystemLib/UnitTest/DxeResetSystemLibUnitTestHost.inf {\r
+    <LibraryClasses>\r
+      ResetSystemLib|MdeModulePkg/Library/DxeResetSystemLib/DxeResetSystemLib.inf\r
+      UefiRuntimeServicesTableLib|MdeModulePkg/Library/DxeResetSystemLib/UnitTest/MockUefiRuntimeServicesTableLib.inf\r
+  }\r