John Edward Broadbent | 59dffa6 | 2022-01-13 17:41:32 -0800 | [diff] [blame] | 1 | |
| 2 | #include "cryptErase.hpp" |
| 3 | #include "cryptsetupInterface.hpp" |
| 4 | #include "estoraged.hpp" |
| 5 | #include "estoraged_test.hpp" |
| 6 | |
| 7 | #include <unistd.h> |
| 8 | |
| 9 | #include <xyz/openbmc_project/Common/error.hpp> |
| 10 | |
| 11 | #include <exception> |
| 12 | #include <filesystem> |
| 13 | #include <fstream> |
| 14 | #include <string> |
| 15 | |
| 16 | #include <gmock/gmock.h> |
| 17 | #include <gtest/gtest.h> |
| 18 | |
| 19 | namespace estoraged_test |
| 20 | { |
| 21 | |
| 22 | using estoraged::CryptErase; |
John Edward Broadbent | 59dffa6 | 2022-01-13 17:41:32 -0800 | [diff] [blame] | 23 | using sdbusplus::xyz::openbmc_project::Common::Error::InternalFailure; |
| 24 | using sdbusplus::xyz::openbmc_project::Common::Error::ResourceNotFound; |
John Edward Broadbent | 59dffa6 | 2022-01-13 17:41:32 -0800 | [diff] [blame] | 25 | using ::testing::_; |
| 26 | using ::testing::Return; |
| 27 | using ::testing::StrEq; |
| 28 | |
Ed Tanous | 82897c3 | 2022-02-21 14:11:59 -0800 | [diff] [blame] | 29 | const std::string testFileName = "testFile"; |
| 30 | |
| 31 | class CryptoEraseTest : public testing::Test |
John Edward Broadbent | 59dffa6 | 2022-01-13 17:41:32 -0800 | [diff] [blame] | 32 | { |
| 33 | public: |
John Edward Broadbent | 59dffa6 | 2022-01-13 17:41:32 -0800 | [diff] [blame] | 34 | std::ofstream testFile; |
| 35 | |
| 36 | void SetUp() override |
| 37 | { |
| 38 | /* Create an empty file that we'll pretend is a 'storage device'. */ |
Patrick Williams | ba000b9 | 2023-05-10 14:48:57 -0500 | [diff] [blame] | 39 | testFile.open(testFileName, |
John Edward Broadbent | 59dffa6 | 2022-01-13 17:41:32 -0800 | [diff] [blame] | 40 | std::ios::out | std::ios::binary | std::ios::trunc); |
| 41 | testFile.close(); |
| 42 | if (testFile.fail()) |
| 43 | { |
| 44 | throw std::runtime_error("Failed to open test file"); |
| 45 | } |
| 46 | testFile.close(); |
| 47 | } |
| 48 | }; |
| 49 | |
Ed Tanous | 82897c3 | 2022-02-21 14:11:59 -0800 | [diff] [blame] | 50 | TEST_F(CryptoEraseTest, EraseCryptPass) |
John Edward Broadbent | 59dffa6 | 2022-01-13 17:41:32 -0800 | [diff] [blame] | 51 | { |
| 52 | std::unique_ptr<MockCryptsetupInterface> mockCryptIface = |
| 53 | std::make_unique<MockCryptsetupInterface>(); |
| 54 | |
| 55 | EXPECT_CALL(*mockCryptIface, cryptLoad(_, StrEq(CRYPT_LUKS2), nullptr)) |
| 56 | .WillOnce(Return(0)); |
| 57 | |
| 58 | EXPECT_CALL(*mockCryptIface, cryptKeySlotMax(StrEq(CRYPT_LUKS2))) |
| 59 | .WillOnce(Return(1)); |
| 60 | |
| 61 | EXPECT_CALL(*mockCryptIface, cryptKeySlotStatus(_, 0)) |
| 62 | .WillOnce(Return(CRYPT_SLOT_ACTIVE_LAST)); |
| 63 | |
| 64 | EXPECT_CALL(*mockCryptIface, cryptKeyslotDestroy(_, 0)).Times(1); |
| 65 | |
Patrick Williams | 15b63e1 | 2024-08-16 15:22:01 -0400 | [diff] [blame^] | 66 | CryptErase myCryptErase = |
| 67 | CryptErase(testFileName, std::move(mockCryptIface)); |
John Edward Broadbent | 59dffa6 | 2022-01-13 17:41:32 -0800 | [diff] [blame] | 68 | EXPECT_NO_THROW(myCryptErase.doErase()); |
| 69 | } |
| 70 | |
Ed Tanous | 82897c3 | 2022-02-21 14:11:59 -0800 | [diff] [blame] | 71 | TEST_F(CryptoEraseTest, EraseCrypMaxSlotFails) |
John Edward Broadbent | 59dffa6 | 2022-01-13 17:41:32 -0800 | [diff] [blame] | 72 | { |
| 73 | std::unique_ptr<MockCryptsetupInterface> mockCryptIface = |
| 74 | std::make_unique<MockCryptsetupInterface>(); |
| 75 | |
| 76 | EXPECT_CALL(*mockCryptIface, cryptLoad(_, StrEq(CRYPT_LUKS2), nullptr)) |
| 77 | .WillOnce(Return(0)); |
| 78 | |
| 79 | EXPECT_CALL(*mockCryptIface, cryptKeySlotMax(StrEq(CRYPT_LUKS2))) |
| 80 | .WillOnce(Return(-1)); |
| 81 | |
Patrick Williams | 15b63e1 | 2024-08-16 15:22:01 -0400 | [diff] [blame^] | 82 | CryptErase myCryptErase = |
| 83 | CryptErase(testFileName, std::move(mockCryptIface)); |
John Edward Broadbent | 59dffa6 | 2022-01-13 17:41:32 -0800 | [diff] [blame] | 84 | EXPECT_THROW(myCryptErase.doErase(), ResourceNotFound); |
| 85 | } |
| 86 | |
Ed Tanous | 82897c3 | 2022-02-21 14:11:59 -0800 | [diff] [blame] | 87 | TEST_F(CryptoEraseTest, EraseCrypMaxSlotZero) |
John Edward Broadbent | 59dffa6 | 2022-01-13 17:41:32 -0800 | [diff] [blame] | 88 | { |
| 89 | std::unique_ptr<MockCryptsetupInterface> mockCryptIface = |
| 90 | std::make_unique<MockCryptsetupInterface>(); |
| 91 | |
| 92 | EXPECT_CALL(*mockCryptIface, cryptLoad(_, StrEq(CRYPT_LUKS2), nullptr)) |
| 93 | .WillOnce(Return(0)); |
| 94 | |
| 95 | EXPECT_CALL(*mockCryptIface, cryptKeySlotMax(StrEq(CRYPT_LUKS2))) |
| 96 | .WillOnce(Return(0)); |
| 97 | |
Patrick Williams | 15b63e1 | 2024-08-16 15:22:01 -0400 | [diff] [blame^] | 98 | CryptErase myCryptErase = |
| 99 | CryptErase(testFileName, std::move(mockCryptIface)); |
John Edward Broadbent | 59dffa6 | 2022-01-13 17:41:32 -0800 | [diff] [blame] | 100 | EXPECT_THROW(myCryptErase.doErase(), ResourceNotFound); |
| 101 | } |
| 102 | |
Ed Tanous | 82897c3 | 2022-02-21 14:11:59 -0800 | [diff] [blame] | 103 | TEST_F(CryptoEraseTest, EraseCrypOnlyInvalid) |
John Edward Broadbent | 59dffa6 | 2022-01-13 17:41:32 -0800 | [diff] [blame] | 104 | { |
| 105 | std::unique_ptr<MockCryptsetupInterface> mockCryptIface = |
| 106 | std::make_unique<MockCryptsetupInterface>(); |
| 107 | |
| 108 | EXPECT_CALL(*mockCryptIface, cryptLoad(_, StrEq(CRYPT_LUKS2), nullptr)) |
| 109 | .WillOnce(Return(0)); |
| 110 | |
| 111 | EXPECT_CALL(*mockCryptIface, cryptKeySlotMax(StrEq(CRYPT_LUKS2))) |
| 112 | .WillOnce(Return(32)); |
| 113 | |
| 114 | EXPECT_CALL(*mockCryptIface, cryptKeySlotStatus(_, _)) |
| 115 | .WillRepeatedly(Return(CRYPT_SLOT_INVALID)); |
| 116 | |
Patrick Williams | 15b63e1 | 2024-08-16 15:22:01 -0400 | [diff] [blame^] | 117 | CryptErase myCryptErase = |
| 118 | CryptErase(testFileName, std::move(mockCryptIface)); |
John Edward Broadbent | 59dffa6 | 2022-01-13 17:41:32 -0800 | [diff] [blame] | 119 | EXPECT_NO_THROW(myCryptErase.doErase()); |
| 120 | } |
| 121 | |
Ed Tanous | 82897c3 | 2022-02-21 14:11:59 -0800 | [diff] [blame] | 122 | TEST_F(CryptoEraseTest, EraseCrypDestoryFails) |
John Edward Broadbent | 59dffa6 | 2022-01-13 17:41:32 -0800 | [diff] [blame] | 123 | { |
| 124 | std::unique_ptr<MockCryptsetupInterface> mockCryptIface = |
| 125 | std::make_unique<MockCryptsetupInterface>(); |
| 126 | |
| 127 | EXPECT_CALL(*mockCryptIface, cryptLoad(_, StrEq(CRYPT_LUKS2), nullptr)) |
| 128 | .WillOnce(Return(0)); |
| 129 | |
| 130 | EXPECT_CALL(*mockCryptIface, cryptKeySlotMax(StrEq(CRYPT_LUKS2))) |
| 131 | .WillOnce(Return(1)); |
| 132 | |
| 133 | EXPECT_CALL(*mockCryptIface, cryptKeySlotStatus(_, 0)) |
| 134 | .WillOnce(Return(CRYPT_SLOT_ACTIVE)); |
| 135 | |
| 136 | EXPECT_CALL(*mockCryptIface, cryptKeyslotDestroy(_, 0)) |
| 137 | .WillOnce(Return(-1)); |
| 138 | |
Patrick Williams | 15b63e1 | 2024-08-16 15:22:01 -0400 | [diff] [blame^] | 139 | CryptErase myCryptErase = |
| 140 | CryptErase(testFileName, std::move(mockCryptIface)); |
John Edward Broadbent | 59dffa6 | 2022-01-13 17:41:32 -0800 | [diff] [blame] | 141 | EXPECT_THROW(myCryptErase.doErase(), InternalFailure); |
| 142 | } |
| 143 | |
| 144 | } // namespace estoraged_test |