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