]> xenbits.xensource.com Git - ovmf.git/commitdiff
SecurityPkg: Add gmock example
authorChris Johnson <chris.n.johnson@intel.com>
Fri, 24 Mar 2023 23:12:17 +0000 (16:12 -0700)
committermergify[bot] <37929162+mergify[bot]@users.noreply.github.com>
Mon, 10 Apr 2023 05:59:02 +0000 (05:59 +0000)
REF: https://bugzilla.tianocore.org/show_bug.cgi?id=4389

Cc: Jiewen Yao <jiewen.yao@intel.com>
Cc: Jian J Wang <jian.j.wang@intel.com>
Signed-off-by: Chris Johnson <chris.n.johnson@intel.com>
Acked-by: Jiewen Yao <jiewen.yao@intel.com>
Reviewed-by: Oliver Smith-Denny <osde@linux.microsoft.com>
Reviewed-by: Michael D Kinney <michael.d.kinney@intel.com>
SecurityPkg/Library/SecureBootVariableLib/GoogleTest/SecureBootVariableLibGoogleTest.cpp [new file with mode: 0644]
SecurityPkg/Library/SecureBootVariableLib/GoogleTest/SecureBootVariableLibGoogleTest.inf [new file with mode: 0644]
SecurityPkg/Library/SecureBootVariableLib/UnitTest/MockPlatformPKProtectionLib.inf
SecurityPkg/Library/SecureBootVariableLib/UnitTest/MockUefiLib.inf
SecurityPkg/Library/SecureBootVariableLib/UnitTest/MockUefiRuntimeServicesTableLib.inf
SecurityPkg/Library/SecureBootVariableLib/UnitTest/SecureBootVariableLibUnitTest.c
SecurityPkg/SecurityPkg.dec
SecurityPkg/Test/Mock/Include/GoogleTest/Library/MockPlatformPKProtectionLib.h [new file with mode: 0644]
SecurityPkg/Test/Mock/Library/GoogleTest/MockPlatformPKProtectionLib/MockPlatformPKProtectionLib.cpp [new file with mode: 0644]
SecurityPkg/Test/Mock/Library/GoogleTest/MockPlatformPKProtectionLib/MockPlatformPKProtectionLib.inf [new file with mode: 0644]
SecurityPkg/Test/SecurityPkgHostTest.dsc

diff --git a/SecurityPkg/Library/SecureBootVariableLib/GoogleTest/SecureBootVariableLibGoogleTest.cpp b/SecurityPkg/Library/SecureBootVariableLib/GoogleTest/SecureBootVariableLibGoogleTest.cpp
new file mode 100644 (file)
index 0000000..c9190c8
--- /dev/null
@@ -0,0 +1,174 @@
+/** @file\r
+  Unit tests for the implementation of SecureBootVariableLib.\r
+\r
+  Copyright (c) 2022, Intel Corporation. All rights reserved.\r
+  SPDX-License-Identifier: BSD-2-Clause-Patent\r
+**/\r
+#include <Library/GoogleTestLib.h>\r
+#include <GoogleTest/Library/MockUefiLib.h>\r
+#include <GoogleTest/Library/MockUefiRuntimeServicesTableLib.h>\r
+\r
+extern "C" {\r
+  #include <Uefi.h>\r
+  #include <UefiSecureBoot.h>\r
+  #include <Guid/AuthenticatedVariableFormat.h>\r
+  #include <Guid/ImageAuthentication.h>\r
+  #include <Library/SecureBootVariableLib.h>\r
+  #include <Library/MemoryAllocationLib.h>\r
+}\r
+\r
+using namespace testing;\r
+\r
+//////////////////////////////////////////////////////////////////////////////\r
+class SetSecureBootModeTest : public Test {\r
+  protected:\r
+    MockUefiRuntimeServicesTableLib RtServicesMock;\r
+    UINT8       SecureBootMode;\r
+    EFI_STATUS  Status;\r
+\r
+    void SetUp() override {\r
+      // Any random magic number can be used for these tests\r
+      SecureBootMode = 0xAB;\r
+    }\r
+};\r
+\r
+// Test SetSecureBootMode() API from SecureBootVariableLib to verify the\r
+// expected error is returned when the call to gRT->SetVariable() fails.\r
+TEST_F(SetSecureBootModeTest, SetVarError) {\r
+  EXPECT_CALL(RtServicesMock, gRT_SetVariable)\r
+    .WillOnce(Return(EFI_INVALID_PARAMETER));\r
+\r
+  Status = SetSecureBootMode(SecureBootMode);\r
+  EXPECT_EQ(Status, EFI_INVALID_PARAMETER);\r
+}\r
+\r
+// Test SetSecureBootMode() API from SecureBootVariableLib to verify the\r
+// expected secure boot mode is written to the correct variable in the call\r
+// to gRT->SetVariable().\r
+TEST_F(SetSecureBootModeTest, PropogateModeToSetVar) {\r
+  EXPECT_CALL(RtServicesMock,\r
+    gRT_SetVariable(\r
+      Char16StrEq(EFI_CUSTOM_MODE_NAME),\r
+      BufferEq(&gEfiCustomModeEnableGuid, sizeof(EFI_GUID)),\r
+      EFI_VARIABLE_NON_VOLATILE | EFI_VARIABLE_BOOTSERVICE_ACCESS,\r
+      sizeof(SecureBootMode),\r
+      BufferEq(&SecureBootMode, sizeof(SecureBootMode))))\r
+    .WillOnce(Return(EFI_SUCCESS));\r
+\r
+  Status = SetSecureBootMode(SecureBootMode);\r
+  EXPECT_EQ(Status, EFI_SUCCESS);\r
+}\r
+\r
+//////////////////////////////////////////////////////////////////////////////\r
+class GetSetupModeTest : public Test {\r
+  protected:\r
+    MockUefiRuntimeServicesTableLib RtServicesMock;\r
+    UINT8       SetupMode;\r
+    EFI_STATUS  Status;\r
+    UINT8       ExpSetupMode;\r
+\r
+    void SetUp() override {\r
+      // Any random magic number can be used for these tests\r
+      ExpSetupMode = 0xAB;\r
+    }\r
+};\r
+\r
+// Test GetSetupMode() API from SecureBootVariableLib to verify the expected\r
+// error is returned when the call to gRT->GetVariable() fails.\r
+TEST_F(GetSetupModeTest, GetVarError) {\r
+  EXPECT_CALL(RtServicesMock, gRT_GetVariable)\r
+    .WillOnce(Return(EFI_INVALID_PARAMETER));\r
+\r
+  Status = GetSetupMode (&SetupMode);\r
+  EXPECT_EQ(Status, EFI_INVALID_PARAMETER);\r
+}\r
+\r
+// Test GetSetupMode() API from SecureBootVariableLib to verify the expected\r
+// setup mode is returned (and with a success return code) when the mode is\r
+// successfully read from the call to gRT->GetVariable().\r
+TEST_F(GetSetupModeTest, FetchModeFromGetVar) {\r
+  EXPECT_CALL(RtServicesMock,\r
+    gRT_GetVariable(\r
+      Char16StrEq(EFI_SETUP_MODE_NAME),\r
+      BufferEq(&gEfiGlobalVariableGuid, sizeof(EFI_GUID)),\r
+      _,\r
+      Pointee(Eq(sizeof(SetupMode))),\r
+      NotNull()))\r
+    .WillOnce(DoAll(\r
+      SetArgPointee<3>(sizeof(ExpSetupMode)),\r
+      SetArgBuffer<4>(&ExpSetupMode, sizeof(ExpSetupMode)),\r
+      Return(EFI_SUCCESS)));\r
+\r
+  Status = GetSetupMode (&SetupMode);\r
+  ASSERT_EQ(Status, EFI_SUCCESS);\r
+  EXPECT_EQ(SetupMode, ExpSetupMode);\r
+}\r
+\r
+//////////////////////////////////////////////////////////////////////////////\r
+class IsSecureBootEnabledTest : public Test {\r
+  protected:\r
+    MockUefiLib UefiLibMock;\r
+    BOOLEAN     Enabled;\r
+};\r
+\r
+// Test IsSecureBootEnabled() API from SecureBootVariableLib to verify FALSE\r
+// is returned when the call to GetEfiGlobalVariable2() fails.\r
+TEST_F(IsSecureBootEnabledTest, GetVarError) {\r
+  EXPECT_CALL(UefiLibMock, GetEfiGlobalVariable2)\r
+    .WillOnce(Return(EFI_ABORTED));\r
+\r
+  Enabled = IsSecureBootEnabled ();\r
+  EXPECT_EQ(Enabled, FALSE);\r
+}\r
+\r
+//////////////////////////////////////////////////////////////////////////////\r
+class IsSecureBootEnabledAllocTest : public IsSecureBootEnabledTest {\r
+  protected:\r
+    UINT8 *BootEnabledBuffer;\r
+\r
+    void SetUp() override {\r
+      BootEnabledBuffer = (UINT8*) AllocatePool(1);\r
+      ASSERT_NE(BootEnabledBuffer, nullptr);\r
+    }\r
+};\r
+\r
+// Test IsSecureBootEnabled() API from SecureBootVariableLib to verify TRUE\r
+// is returned when the call to GetEfiGlobalVariable2() is successful and\r
+// returns SECURE_BOOT_MODE_ENABLE.\r
+TEST_F(IsSecureBootEnabledAllocTest, IsEnabled) {\r
+  *BootEnabledBuffer = SECURE_BOOT_MODE_ENABLE;\r
+  EXPECT_CALL(UefiLibMock,\r
+    GetEfiGlobalVariable2(\r
+      Char16StrEq(EFI_SECURE_BOOT_MODE_NAME),\r
+      NotNull(),\r
+      _))\r
+    .WillOnce(DoAll(\r
+      SetArgBuffer<1>(&BootEnabledBuffer, sizeof(VOID*)),\r
+      Return(EFI_SUCCESS)));\r
+\r
+  Enabled = IsSecureBootEnabled ();\r
+  EXPECT_EQ(Enabled, TRUE);\r
+}\r
+\r
+// Test IsSecureBootEnabled() API from SecureBootVariableLib to verify FALSE\r
+// is returned when the call to GetEfiGlobalVariable2() is successful and\r
+// returns SECURE_BOOT_MODE_DISABLE.\r
+TEST_F(IsSecureBootEnabledAllocTest, IsDisabled) {\r
+  *BootEnabledBuffer = SECURE_BOOT_MODE_DISABLE;\r
+  EXPECT_CALL(UefiLibMock,\r
+    GetEfiGlobalVariable2(\r
+      Char16StrEq(EFI_SECURE_BOOT_MODE_NAME),\r
+      NotNull(),\r
+      _))\r
+    .WillOnce(DoAll(\r
+      SetArgBuffer<1>(&BootEnabledBuffer, sizeof(VOID*)),\r
+      Return(EFI_SUCCESS)));\r
+\r
+  Enabled = IsSecureBootEnabled ();\r
+  EXPECT_EQ(Enabled, FALSE);\r
+}\r
+\r
+int main(int argc, char* argv[]) {\r
+  testing::InitGoogleTest(&argc, argv);\r
+  return RUN_ALL_TESTS();\r
+}\r
diff --git a/SecurityPkg/Library/SecureBootVariableLib/GoogleTest/SecureBootVariableLibGoogleTest.inf b/SecurityPkg/Library/SecureBootVariableLib/GoogleTest/SecureBootVariableLibGoogleTest.inf
new file mode 100644 (file)
index 0000000..5503dcf
--- /dev/null
@@ -0,0 +1,32 @@
+## @file\r
+# Unit test suite for the SecureBootVariableLib using Google Test\r
+#\r
+# Copyright (c) 2022, Intel Corporation. All rights reserved.\r
+# SPDX-License-Identifier: BSD-2-Clause-Patent\r
+##\r
+\r
+[Defines]\r
+  INF_VERSION         = 0x00010017\r
+  BASE_NAME           = SecureBootVariableLibGoogleTest\r
+  FILE_GUID           = C88372AB-726B-4344-A250-6C7F826C874E\r
+  VERSION_STRING      = 1.0\r
+  MODULE_TYPE         = HOST_APPLICATION\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
+  SecureBootVariableLibGoogleTest.cpp\r
+\r
+[Packages]\r
+  MdePkg/MdePkg.dec\r
+  MdeModulePkg/MdeModulePkg.dec\r
+  SecurityPkg/SecurityPkg.dec\r
+  UnitTestFrameworkPkg/UnitTestFrameworkPkg.dec\r
+\r
+[LibraryClasses]\r
+  GoogleTestLib\r
+  SecureBootVariableLib\r
index 1e19033c5a91a67c0731a1e2fcdeb90e7987c36c..c927ef709958fc86d9a84a764cccbc8269b8f758 100644 (file)
@@ -10,9 +10,9 @@
   INF_VERSION                    = 0x00010005\r
   BASE_NAME                      = MockPlatformPKProtectionLib\r
   FILE_GUID                      = 5FCD74D3-3965-4D56-AB83-000B9B4806A0\r
-  MODULE_TYPE                    = DXE_DRIVER\r
+  MODULE_TYPE                    = HOST_APPLICATION\r
   VERSION_STRING                 = 1.0\r
-  LIBRARY_CLASS                  = PlatformPKProtectionLib|HOST_APPLICATION\r
+  LIBRARY_CLASS                  = PlatformPKProtectionLib\r
 \r
 #\r
 # The following information is for reference only and not required by the build tools.\r
index a84242ac7205f30f9c2a9d8bd7ced1d6a9f591c6..fecf46841131215ea569f0fc5439ac02ac66fd58 100644 (file)
@@ -18,9 +18,9 @@
   INF_VERSION                    = 0x00010005\r
   BASE_NAME                      = MockUefiLib\r
   FILE_GUID                      = E3B7AEF9-4E55-49AF-B035-ED776C928EC6\r
-  MODULE_TYPE                    = UEFI_DRIVER\r
+  MODULE_TYPE                    = HOST_APPLICATION\r
   VERSION_STRING                 = 1.0\r
-  LIBRARY_CLASS                  = UefiLib|HOST_APPLICATION\r
+  LIBRARY_CLASS                  = UefiLib\r
 \r
 #\r
 #  VALID_ARCHITECTURES           = IA32 X64 EBC\r
index f832a93e2254b721a8a24f90f1561c323f418e63..6fe04189606ebdc46c1a2bf66cb43788136daaf9 100644 (file)
@@ -10,9 +10,9 @@
   INF_VERSION                    = 0x00010005\r
   BASE_NAME                      = MockUefiRuntimeServicesTableLib\r
   FILE_GUID                      = 84CE0021-ABEE-403C-9A1B-763CCF2D40F1\r
-  MODULE_TYPE                    = UEFI_DRIVER\r
+  MODULE_TYPE                    = HOST_APPLICATION\r
   VERSION_STRING                 = 1.0\r
-  LIBRARY_CLASS                  = UefiRuntimeServicesTableLib|HOST_APPLICATION\r
+  LIBRARY_CLASS                  = UefiRuntimeServicesTableLib\r
 \r
 #\r
 #  VALID_ARCHITECTURES           = IA32 X64 EBC\r
index a23135dfb016d0741df2e35ccdf8f25b6295aae7..3a92d5d834572973fd419c5aaa3661a50cfaf1a6 100644 (file)
@@ -163,7 +163,7 @@ MockGetVariable (
     return EFI_BUFFER_TOO_SMALL;\r
   } else {\r
     assert_non_null (Data);\r
-    CopyMem (Data, (VOID *)mock (), TargetSize);\r
+    CopyMem (Data, (VOID *)(UINTN)mock (), TargetSize);\r
   }\r
 \r
   return EFI_SUCCESS;\r
index 0382090f4e75cc13a937e55a5319eaca7cce4a8e..0a8042d63fe17249a9fa9b8ee6c9ebc74fa46196 100644 (file)
@@ -21,6 +21,7 @@
 \r
 [Includes]\r
   Include\r
+  Test/Mock/Include\r
 \r
 [LibraryClasses]\r
   ##  @libraryclass  Provides hash interfaces from different implementations.\r
diff --git a/SecurityPkg/Test/Mock/Include/GoogleTest/Library/MockPlatformPKProtectionLib.h b/SecurityPkg/Test/Mock/Include/GoogleTest/Library/MockPlatformPKProtectionLib.h
new file mode 100644 (file)
index 0000000..8024f4b
--- /dev/null
@@ -0,0 +1,28 @@
+/** @file\r
+  Google Test mocks for PlatformPKProtectionLib\r
+\r
+  Copyright (c) 2022, Intel Corporation. All rights reserved.\r
+  SPDX-License-Identifier: BSD-2-Clause-Patent\r
+**/\r
+\r
+#ifndef MOCK_PLATFORM_PK_PROTECTION_LIB_H_\r
+#define MOCK_PLATFORM_PK_PROTECTION_LIB_H_\r
+\r
+#include <Library/GoogleTestLib.h>\r
+#include <Library/FunctionMockLib.h>\r
+extern "C" {\r
+#include <Uefi.h>\r
+#include <Library/PlatformPKProtectionLib.h>\r
+}\r
+\r
+struct MockPlatformPKProtectionLib {\r
+  MOCK_INTERFACE_DECLARATION (MockPlatformPKProtectionLib);\r
+\r
+  MOCK_FUNCTION_DECLARATION (\r
+    EFI_STATUS,\r
+    DisablePKProtection,\r
+    ()\r
+    );\r
+};\r
+\r
+#endif\r
diff --git a/SecurityPkg/Test/Mock/Library/GoogleTest/MockPlatformPKProtectionLib/MockPlatformPKProtectionLib.cpp b/SecurityPkg/Test/Mock/Library/GoogleTest/MockPlatformPKProtectionLib/MockPlatformPKProtectionLib.cpp
new file mode 100644 (file)
index 0000000..5ea030f
--- /dev/null
@@ -0,0 +1,11 @@
+/** @file\r
+  Google Test mocks for PlatformPKProtectionLib\r
+\r
+  Copyright (c) 2022, Intel Corporation. All rights reserved.\r
+  SPDX-License-Identifier: BSD-2-Clause-Patent\r
+**/\r
+#include <GoogleTest/Library/MockPlatformPKProtectionLib.h>\r
+\r
+MOCK_INTERFACE_DEFINITION(MockPlatformPKProtectionLib);\r
+\r
+MOCK_FUNCTION_DEFINITION(MockPlatformPKProtectionLib, DisablePKProtection, 0, EFIAPI);\r
diff --git a/SecurityPkg/Test/Mock/Library/GoogleTest/MockPlatformPKProtectionLib/MockPlatformPKProtectionLib.inf b/SecurityPkg/Test/Mock/Library/GoogleTest/MockPlatformPKProtectionLib/MockPlatformPKProtectionLib.inf
new file mode 100644 (file)
index 0000000..3ed638e
--- /dev/null
@@ -0,0 +1,34 @@
+## @file\r
+# Google Test mocks for PlatformPKProtectionLib\r
+#\r
+# Copyright (c) 2022, Intel Corporation. All rights reserved.\r
+# SPDX-License-Identifier: BSD-2-Clause-Patent\r
+##\r
+\r
+[Defines]\r
+  INF_VERSION                    = 0x00010005\r
+  BASE_NAME                      = MockPlatformPKProtectionLib\r
+  FILE_GUID                      = C1383D85-E0ED-44E0-A0A6-125F1D78B6E9\r
+  MODULE_TYPE                    = HOST_APPLICATION\r
+  VERSION_STRING                 = 1.0\r
+  LIBRARY_CLASS                  = PlatformPKProtectionLib\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
+  MockPlatformPKProtectionLib.cpp\r
+\r
+[Packages]\r
+  MdePkg/MdePkg.dec\r
+  SecurityPkg/SecurityPkg.dec\r
+  UnitTestFrameworkPkg/UnitTestFrameworkPkg.dec\r
+\r
+[LibraryClasses]\r
+  GoogleTestLib\r
+\r
+[BuildOptions]\r
+  MSFT:*_*_*_CC_FLAGS = /EHsc\r
index c4df01fe1b73665df2e76dab05136d4578f09c2f..ad5b4fc350eaa9ff16fac63f404a06c4a7932e70 100644 (file)
@@ -25,6 +25,7 @@
   SecurityPkg/Library/SecureBootVariableLib/UnitTest/MockUefiRuntimeServicesTableLib.inf\r
   SecurityPkg/Library/SecureBootVariableLib/UnitTest/MockPlatformPKProtectionLib.inf\r
   SecurityPkg/Library/SecureBootVariableLib/UnitTest/MockUefiLib.inf\r
+  SecurityPkg/Test/Mock/Library/GoogleTest/MockPlatformPKProtectionLib/MockPlatformPKProtectionLib.inf\r
 \r
   #\r
   # Build SecurityPkg HOST_APPLICATION Tests\r
       PlatformPKProtectionLib|SecurityPkg/Library/SecureBootVariableLib/UnitTest/MockPlatformPKProtectionLib.inf\r
       UefiLib|SecurityPkg/Library/SecureBootVariableLib/UnitTest/MockUefiLib.inf\r
   }\r
+  SecurityPkg/Library/SecureBootVariableLib/GoogleTest/SecureBootVariableLibGoogleTest.inf {\r
+    <LibraryClasses>\r
+      SecureBootVariableLib|SecurityPkg/Library/SecureBootVariableLib/SecureBootVariableLib.inf\r
+      UefiRuntimeServicesTableLib|MdePkg/Test/Mock/Library/GoogleTest/MockUefiRuntimeServicesTableLib/MockUefiRuntimeServicesTableLib.inf\r
+      PlatformPKProtectionLib|SecurityPkg/Test/Mock/Library/GoogleTest/MockPlatformPKProtectionLib/MockPlatformPKProtectionLib.inf\r
+      UefiLib|MdePkg/Test/Mock/Library/GoogleTest/MockUefiLib/MockUefiLib.inf\r
+  }\r