--- /dev/null
+/** @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