blob: 909e54d922773356882cb59e80b0412daa90e1d9 [file] [log] [blame]
John Edward Broadbent59dffa62022-01-13 17:41:32 -08001
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
19namespace estoraged_test
20{
21
22using estoraged::CryptErase;
John Edward Broadbent59dffa62022-01-13 17:41:32 -080023using sdbusplus::xyz::openbmc_project::Common::Error::InternalFailure;
24using sdbusplus::xyz::openbmc_project::Common::Error::ResourceNotFound;
John Edward Broadbent59dffa62022-01-13 17:41:32 -080025using ::testing::_;
26using ::testing::Return;
27using ::testing::StrEq;
28
Ed Tanous82897c32022-02-21 14:11:59 -080029const std::string testFileName = "testFile";
30
31class CryptoEraseTest : public testing::Test
John Edward Broadbent59dffa62022-01-13 17:41:32 -080032{
33 public:
John Edward Broadbent59dffa62022-01-13 17:41:32 -080034 std::ofstream testFile;
35
36 void SetUp() override
37 {
38 /* Create an empty file that we'll pretend is a 'storage device'. */
Patrick Williamsba000b92023-05-10 14:48:57 -050039 testFile.open(testFileName,
John Edward Broadbent59dffa62022-01-13 17:41:32 -080040 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 Tanous82897c32022-02-21 14:11:59 -080050TEST_F(CryptoEraseTest, EraseCryptPass)
John Edward Broadbent59dffa62022-01-13 17:41:32 -080051{
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 Williams15b63e12024-08-16 15:22:01 -040066 CryptErase myCryptErase =
67 CryptErase(testFileName, std::move(mockCryptIface));
John Edward Broadbent59dffa62022-01-13 17:41:32 -080068 EXPECT_NO_THROW(myCryptErase.doErase());
69}
70
Ed Tanous82897c32022-02-21 14:11:59 -080071TEST_F(CryptoEraseTest, EraseCrypMaxSlotFails)
John Edward Broadbent59dffa62022-01-13 17:41:32 -080072{
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 Williams15b63e12024-08-16 15:22:01 -040082 CryptErase myCryptErase =
83 CryptErase(testFileName, std::move(mockCryptIface));
John Edward Broadbent59dffa62022-01-13 17:41:32 -080084 EXPECT_THROW(myCryptErase.doErase(), ResourceNotFound);
85}
86
Ed Tanous82897c32022-02-21 14:11:59 -080087TEST_F(CryptoEraseTest, EraseCrypMaxSlotZero)
John Edward Broadbent59dffa62022-01-13 17:41:32 -080088{
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 Williams15b63e12024-08-16 15:22:01 -040098 CryptErase myCryptErase =
99 CryptErase(testFileName, std::move(mockCryptIface));
John Edward Broadbent59dffa62022-01-13 17:41:32 -0800100 EXPECT_THROW(myCryptErase.doErase(), ResourceNotFound);
101}
102
Ed Tanous82897c32022-02-21 14:11:59 -0800103TEST_F(CryptoEraseTest, EraseCrypOnlyInvalid)
John Edward Broadbent59dffa62022-01-13 17:41:32 -0800104{
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 Williams15b63e12024-08-16 15:22:01 -0400117 CryptErase myCryptErase =
118 CryptErase(testFileName, std::move(mockCryptIface));
John Edward Broadbent59dffa62022-01-13 17:41:32 -0800119 EXPECT_NO_THROW(myCryptErase.doErase());
120}
121
Ed Tanous82897c32022-02-21 14:11:59 -0800122TEST_F(CryptoEraseTest, EraseCrypDestoryFails)
John Edward Broadbent59dffa62022-01-13 17:41:32 -0800123{
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 Williams15b63e12024-08-16 15:22:01 -0400139 CryptErase myCryptErase =
140 CryptErase(testFileName, std::move(mockCryptIface));
John Edward Broadbent59dffa62022-01-13 17:41:32 -0800141 EXPECT_THROW(myCryptErase.doErase(), InternalFailure);
142}
143
144} // namespace estoraged_test