blob: 3d31112d96d395c85d645c09cc5195477fb62e5e [file] [log] [blame]
John Edward Broadbent59dffa62022-01-13 17:41:32 -08001#include "estoraged_test.hpp"
John Wedigb810c922021-11-17 16:38:03 -08002
John Wedigb810c922021-11-17 16:38:03 -08003#include "estoraged.hpp"
Tom Tung043af592023-11-24 13:37:05 +08004#include "estoraged_conf.hpp"
John Wedigb810c922021-11-17 16:38:03 -08005
6#include <unistd.h>
7
John Wedig67a47442022-04-05 17:21:29 -07008#include <boost/asio/io_context.hpp>
9#include <sdbusplus/asio/connection.hpp>
10#include <sdbusplus/asio/object_server.hpp>
John Wedig972c3fa2021-12-29 17:30:41 -080011#include <xyz/openbmc_project/Common/error.hpp>
Patrick Williams0e2a46f2023-05-10 14:35:52 -050012#include <xyz/openbmc_project/Inventory/Item/Volume/server.hpp>
John Wedigb810c922021-11-17 16:38:03 -080013
14#include <exception>
15#include <filesystem>
16#include <fstream>
17#include <iterator>
John Wedig67a47442022-04-05 17:21:29 -070018#include <memory>
John Wedigb810c922021-11-17 16:38:03 -080019#include <string>
20#include <vector>
21
22#include <gmock/gmock.h>
23#include <gtest/gtest.h>
24
25namespace estoraged_test
26{
27
Patrick Williams0e2a46f2023-05-10 14:35:52 -050028using sdbusplus::server::xyz::openbmc_project::inventory::item::Volume;
John Wedig972c3fa2021-12-29 17:30:41 -080029using sdbusplus::xyz::openbmc_project::Common::Error::InternalFailure;
30using sdbusplus::xyz::openbmc_project::Common::Error::ResourceNotFound;
John Wedigb810c922021-11-17 16:38:03 -080031using std::filesystem::path;
32using ::testing::_;
John Wedigb810c922021-11-17 16:38:03 -080033using ::testing::Return;
34using ::testing::StrEq;
35
Ed Tanous82897c32022-02-21 14:11:59 -080036class EStoragedTest : public testing::Test
John Wedigb810c922021-11-17 16:38:03 -080037{
38 public:
Ed Tanous82897c32022-02-21 14:11:59 -080039 const char* testFileName = "testfile";
40 const char* testLuksDevName = "testfile_luksDev";
John Wedig2443a022023-03-17 13:42:32 -070041 const char* testCryptDir = "/tmp";
John Wedig6c0d8ce2022-04-22 14:00:43 -070042 const std::string testConfigPath =
43 "/xyz/openbmc_project/inventory/system/board/test_board/test_emmc";
John Wedig67a47442022-04-05 17:21:29 -070044 const uint64_t testSize = 24;
John Edward Broadbent5d799bb2022-03-22 16:14:24 -070045 const uint8_t testLifeTime = 25;
John Wedigb4838302022-07-22 13:51:16 -070046 const std::string testPartNumber = "12345678";
47 const std::string testSerialNumber = "ABCDEF1234";
Rahul Kapoor19825052023-05-27 01:52:23 +000048 const std::string testLocationCode = "U102020";
John Wedigd7be42b2024-01-19 16:07:19 -080049 const std::string testDriveType = "SSD";
John Wedigb810c922021-11-17 16:38:03 -080050 std::ofstream testFile;
John Wedigb810c922021-11-17 16:38:03 -080051 std::string passwordString;
52 std::vector<uint8_t> password;
Ed Tanous82897c32022-02-21 14:11:59 -080053 MockCryptsetupInterface* mockCryptIface{};
54 MockFilesystemInterface* mockFsIface{};
John Wedig67a47442022-04-05 17:21:29 -070055 boost::asio::io_context io;
56 std::shared_ptr<sdbusplus::asio::connection> conn;
57 std::unique_ptr<sdbusplus::asio::object_server> objectServer;
58 std::unique_ptr<estoraged::EStoraged> esObject;
John Wedigb810c922021-11-17 16:38:03 -080059
Ed Tanous82897c32022-02-21 14:11:59 -080060 EStoragedTest() :
John Wedig67a47442022-04-05 17:21:29 -070061 passwordString("password"),
John Wedigb810c922021-11-17 16:38:03 -080062 password(passwordString.begin(), passwordString.end())
63 {}
64
65 void SetUp() override
66 {
67 /* Create an empty file that we'll pretend is a 'storage device'. */
68 testFile.open(testFileName,
69 std::ios::out | std::ios::binary | std::ios::trunc);
70 testFile.close();
71 if (testFile.fail())
72 {
73 throw std::runtime_error("Failed to open test file");
74 }
75
John Wedigb810c922021-11-17 16:38:03 -080076 std::unique_ptr<MockCryptsetupInterface> cryptIface =
77 std::make_unique<MockCryptsetupInterface>();
78 mockCryptIface = cryptIface.get();
79 std::unique_ptr<MockFilesystemInterface> fsIface =
80 std::make_unique<MockFilesystemInterface>();
81 mockFsIface = fsIface.get();
82
John Wedig2443a022023-03-17 13:42:32 -070083 /* Set up location of dummy mapped crypt file. */
84 EXPECT_CALL(*cryptIface, cryptGetDir).WillOnce(Return(testCryptDir));
85
John Wedig67a47442022-04-05 17:21:29 -070086 conn = std::make_shared<sdbusplus::asio::connection>(io);
87 // request D-Bus server name.
88 conn->request_name("xyz.openbmc_project.eStoraged.test");
89 objectServer = std::make_unique<sdbusplus::asio::object_server>(conn);
90
Ed Tanous82897c32022-02-21 14:11:59 -080091 esObject = std::make_unique<estoraged::EStoraged>(
John Wedig6c0d8ce2022-04-22 14:00:43 -070092 *objectServer, testConfigPath, testFileName, testLuksDevName,
John Wedigb4838302022-07-22 13:51:16 -070093 testSize, testLifeTime, testPartNumber, testSerialNumber,
Tom Tung043af592023-11-24 13:37:05 +080094 testLocationCode, ERASE_MAX_GEOMETRY, ERASE_MIN_GEOMETRY,
John Wedigd7be42b2024-01-19 16:07:19 -080095 testDriveType, std::move(cryptIface), std::move(fsIface));
John Wedigb810c922021-11-17 16:38:03 -080096 }
97
98 void TearDown() override
99 {
100 EXPECT_EQ(0, unlink(testFileName));
101 }
102};
103
John Wedig2443a022023-03-17 13:42:32 -0700104const char* mappedDevicePath = "/tmp/testfile_luksDev";
105std::ofstream mappedDevice;
106
107int createMappedDev()
108{
109 mappedDevice.open(mappedDevicePath,
110 std::ios::out | std::ios::binary | std::ios::trunc);
111 mappedDevice.close();
112 if (mappedDevice.fail())
113 {
114 throw std::runtime_error("Failed to open test mapped device");
115 }
116
117 return 0;
118}
119
120int removeMappedDev()
121{
122 EXPECT_EQ(0, unlink(mappedDevicePath));
123
124 return 0;
125}
126
John Wedigb810c922021-11-17 16:38:03 -0800127/* Test case to format and then lock the LUKS device. */
Ed Tanous82897c32022-02-21 14:11:59 -0800128TEST_F(EStoragedTest, FormatPass)
John Wedigb810c922021-11-17 16:38:03 -0800129{
John Wedigb810c922021-11-17 16:38:03 -0800130 EXPECT_CALL(*mockCryptIface, cryptFormat(_, _, _, _, _, _, _, _)).Times(1);
131
132 EXPECT_CALL(*mockCryptIface, cryptKeyslotAddByVolumeKey(_, _, _, _, _, _))
133 .Times(1);
134
135 EXPECT_CALL(*mockCryptIface, cryptLoad(_, _, _)).Times(1);
136
137 EXPECT_CALL(*mockCryptIface, cryptActivateByPassphrase(_, _, _, _, _, _))
John Wedig2443a022023-03-17 13:42:32 -0700138 .WillOnce(&createMappedDev);
John Wedigb810c922021-11-17 16:38:03 -0800139
John Wedig2443a022023-03-17 13:42:32 -0700140 EXPECT_CALL(*mockFsIface, runMkfs(StrEq(esObject->getCryptDevicePath())))
141 .WillOnce(Return(0));
John Wedigb810c922021-11-17 16:38:03 -0800142
John Wedigb17f8252022-01-12 14:24:26 -0800143 EXPECT_CALL(*mockFsIface, directoryExists(path(esObject->getMountPoint())))
144 .WillOnce(Return(false));
145
John Wedigb810c922021-11-17 16:38:03 -0800146 EXPECT_CALL(*mockFsIface, createDirectory(path(esObject->getMountPoint())))
147 .WillOnce(Return(true));
148
149 EXPECT_CALL(*mockFsIface,
John Wedig2443a022023-03-17 13:42:32 -0700150 doMount(StrEq(esObject->getCryptDevicePath()),
John Wedigb810c922021-11-17 16:38:03 -0800151 StrEq(esObject->getMountPoint()), _, _, _))
152 .WillOnce(Return(0));
153
154 EXPECT_CALL(*mockFsIface, doUnmount(StrEq(esObject->getMountPoint())))
155 .WillOnce(Return(0));
156
157 EXPECT_CALL(*mockFsIface, removeDirectory(path(esObject->getMountPoint())))
158 .WillOnce(Return(true));
159
John Wedig2443a022023-03-17 13:42:32 -0700160 EXPECT_CALL(*mockCryptIface, cryptDeactivate(_, _))
161 .WillOnce(&removeMappedDev);
John Wedigb810c922021-11-17 16:38:03 -0800162
163 /* Format the encrypted device. */
John Wedig972c3fa2021-12-29 17:30:41 -0800164 esObject->formatLuks(password, Volume::FilesystemType::ext4);
John Wedigb810c922021-11-17 16:38:03 -0800165 EXPECT_FALSE(esObject->isLocked());
166
John Wedig972c3fa2021-12-29 17:30:41 -0800167 esObject->lock();
John Wedigb810c922021-11-17 16:38:03 -0800168 EXPECT_TRUE(esObject->isLocked());
169}
170
John Wedigb17f8252022-01-12 14:24:26 -0800171/*
172 * Test case where the mount point directory already exists, so it shouldn't
173 * try to create it.
174 */
Ed Tanous82897c32022-02-21 14:11:59 -0800175TEST_F(EStoragedTest, MountPointExistsPass)
John Wedigb17f8252022-01-12 14:24:26 -0800176{
John Wedigb17f8252022-01-12 14:24:26 -0800177 EXPECT_CALL(*mockCryptIface, cryptFormat(_, _, _, _, _, _, _, _)).Times(1);
178
179 EXPECT_CALL(*mockCryptIface, cryptKeyslotAddByVolumeKey(_, _, _, _, _, _))
180 .Times(1);
181
182 EXPECT_CALL(*mockCryptIface, cryptLoad(_, _, _)).Times(1);
183
184 EXPECT_CALL(*mockCryptIface, cryptActivateByPassphrase(_, _, _, _, _, _))
John Wedig2443a022023-03-17 13:42:32 -0700185 .WillOnce(&createMappedDev);
John Wedigb17f8252022-01-12 14:24:26 -0800186
John Wedig2443a022023-03-17 13:42:32 -0700187 EXPECT_CALL(*mockFsIface, runMkfs(StrEq(esObject->getCryptDevicePath())))
188 .WillOnce(Return(0));
John Wedigb17f8252022-01-12 14:24:26 -0800189
190 EXPECT_CALL(*mockFsIface, directoryExists(path(esObject->getMountPoint())))
191 .WillOnce(Return(true));
192
193 EXPECT_CALL(*mockFsIface, createDirectory(path(esObject->getMountPoint())))
194 .Times(0);
195
196 EXPECT_CALL(*mockFsIface,
John Wedig2443a022023-03-17 13:42:32 -0700197 doMount(StrEq(esObject->getCryptDevicePath()),
John Wedigb17f8252022-01-12 14:24:26 -0800198 StrEq(esObject->getMountPoint()), _, _, _))
199 .WillOnce(Return(0));
200
201 EXPECT_CALL(*mockFsIface, doUnmount(StrEq(esObject->getMountPoint())))
202 .WillOnce(Return(0));
203
204 EXPECT_CALL(*mockFsIface, removeDirectory(path(esObject->getMountPoint())))
205 .WillOnce(Return(true));
206
John Wedig2443a022023-03-17 13:42:32 -0700207 EXPECT_CALL(*mockCryptIface, cryptDeactivate(_, _))
208 .WillOnce(&removeMappedDev);
John Wedigb17f8252022-01-12 14:24:26 -0800209
210 /* Format the encrypted device. */
211 esObject->formatLuks(password, Volume::FilesystemType::ext4);
212 EXPECT_FALSE(esObject->isLocked());
213
214 esObject->lock();
215 EXPECT_TRUE(esObject->isLocked());
216}
217
John Wedigb810c922021-11-17 16:38:03 -0800218/* Test case where the device/file doesn't exist. */
Ed Tanous82897c32022-02-21 14:11:59 -0800219TEST_F(EStoragedTest, FormatNoDeviceFail)
John Wedigb810c922021-11-17 16:38:03 -0800220{
221 /* Delete the test file. */
222 EXPECT_EQ(0, unlink(testFileName));
223
John Wedig972c3fa2021-12-29 17:30:41 -0800224 EXPECT_THROW(esObject->formatLuks(password, Volume::FilesystemType::ext4),
225 ResourceNotFound);
John Wedig2443a022023-03-17 13:42:32 -0700226 EXPECT_TRUE(esObject->isLocked());
John Wedigb810c922021-11-17 16:38:03 -0800227
228 /* Create the test file again, so that the TearDown function works. */
229 testFile.open(testFileName,
230 std::ios::out | std::ios::binary | std::ios::trunc);
231 testFile.close();
232}
233
234/* Test case where we fail to format the LUKS device. */
Ed Tanous82897c32022-02-21 14:11:59 -0800235TEST_F(EStoragedTest, FormatFail)
John Wedigb810c922021-11-17 16:38:03 -0800236{
237 EXPECT_CALL(*mockCryptIface, cryptFormat(_, _, _, _, _, _, _, _))
238 .WillOnce(Return(-1));
239
John Wedig972c3fa2021-12-29 17:30:41 -0800240 EXPECT_THROW(esObject->formatLuks(password, Volume::FilesystemType::ext4),
241 InternalFailure);
John Wedig2443a022023-03-17 13:42:32 -0700242 EXPECT_TRUE(esObject->isLocked());
John Wedigb810c922021-11-17 16:38:03 -0800243}
244
245/* Test case where we fail to set the password for the LUKS device. */
Ed Tanous82897c32022-02-21 14:11:59 -0800246TEST_F(EStoragedTest, AddKeyslotFail)
John Wedigb810c922021-11-17 16:38:03 -0800247{
John Wedigb810c922021-11-17 16:38:03 -0800248 EXPECT_CALL(*mockCryptIface, cryptFormat(_, _, _, _, _, _, _, _)).Times(1);
249
250 EXPECT_CALL(*mockCryptIface, cryptKeyslotAddByVolumeKey(_, _, _, _, _, _))
251 .WillOnce(Return(-1));
252
John Wedig972c3fa2021-12-29 17:30:41 -0800253 EXPECT_THROW(esObject->formatLuks(password, Volume::FilesystemType::ext4),
254 InternalFailure);
John Wedigb810c922021-11-17 16:38:03 -0800255 EXPECT_TRUE(esObject->isLocked());
256}
257
258/* Test case where we fail to load the LUKS header. */
Ed Tanous82897c32022-02-21 14:11:59 -0800259TEST_F(EStoragedTest, LoadLuksHeaderFail)
John Wedigb810c922021-11-17 16:38:03 -0800260{
John Wedigb810c922021-11-17 16:38:03 -0800261 EXPECT_CALL(*mockCryptIface, cryptFormat(_, _, _, _, _, _, _, _)).Times(1);
262
263 EXPECT_CALL(*mockCryptIface, cryptKeyslotAddByVolumeKey(_, _, _, _, _, _))
264 .Times(1);
265
266 EXPECT_CALL(*mockCryptIface, cryptLoad(_, _, _)).WillOnce(Return(-1));
267
John Wedig972c3fa2021-12-29 17:30:41 -0800268 EXPECT_THROW(esObject->formatLuks(password, Volume::FilesystemType::ext4),
269 InternalFailure);
John Wedigb810c922021-11-17 16:38:03 -0800270 EXPECT_TRUE(esObject->isLocked());
271}
272
273/* Test case where we fail to activate the LUKS device. */
Ed Tanous82897c32022-02-21 14:11:59 -0800274TEST_F(EStoragedTest, ActivateFail)
John Wedigb810c922021-11-17 16:38:03 -0800275{
John Wedigb810c922021-11-17 16:38:03 -0800276 EXPECT_CALL(*mockCryptIface, cryptFormat(_, _, _, _, _, _, _, _)).Times(1);
277
278 EXPECT_CALL(*mockCryptIface, cryptKeyslotAddByVolumeKey(_, _, _, _, _, _))
279 .Times(1);
280
281 EXPECT_CALL(*mockCryptIface, cryptLoad(_, _, _)).Times(1);
282
283 EXPECT_CALL(*mockCryptIface, cryptActivateByPassphrase(_, _, _, _, _, _))
284 .WillOnce(Return(-1));
285
John Wedig972c3fa2021-12-29 17:30:41 -0800286 EXPECT_THROW(esObject->formatLuks(password, Volume::FilesystemType::ext4),
287 InternalFailure);
John Wedigb810c922021-11-17 16:38:03 -0800288 EXPECT_TRUE(esObject->isLocked());
289}
290
291/* Test case where we fail to create the filesystem. */
Ed Tanous82897c32022-02-21 14:11:59 -0800292TEST_F(EStoragedTest, CreateFilesystemFail)
John Wedigb810c922021-11-17 16:38:03 -0800293{
John Wedigb810c922021-11-17 16:38:03 -0800294 EXPECT_CALL(*mockCryptIface, cryptFormat(_, _, _, _, _, _, _, _)).Times(1);
295
296 EXPECT_CALL(*mockCryptIface, cryptKeyslotAddByVolumeKey(_, _, _, _, _, _))
297 .Times(1);
298
299 EXPECT_CALL(*mockCryptIface, cryptLoad(_, _, _)).Times(1);
300
301 EXPECT_CALL(*mockCryptIface, cryptActivateByPassphrase(_, _, _, _, _, _))
John Wedig2443a022023-03-17 13:42:32 -0700302 .WillOnce(&createMappedDev);
John Wedigb810c922021-11-17 16:38:03 -0800303
John Wedig2443a022023-03-17 13:42:32 -0700304 EXPECT_CALL(*mockFsIface, runMkfs(StrEq(esObject->getCryptDevicePath())))
305 .WillOnce(Return(-1));
John Wedigb810c922021-11-17 16:38:03 -0800306
John Wedig972c3fa2021-12-29 17:30:41 -0800307 EXPECT_THROW(esObject->formatLuks(password, Volume::FilesystemType::ext4),
308 InternalFailure);
John Wedigb810c922021-11-17 16:38:03 -0800309 EXPECT_FALSE(esObject->isLocked());
John Wedig2443a022023-03-17 13:42:32 -0700310
311 EXPECT_EQ(0, removeMappedDev());
John Wedigb810c922021-11-17 16:38:03 -0800312}
313
314/* Test case where we fail to create the mount point. */
Ed Tanous82897c32022-02-21 14:11:59 -0800315TEST_F(EStoragedTest, CreateMountPointFail)
John Wedigb810c922021-11-17 16:38:03 -0800316{
John Wedigb810c922021-11-17 16:38:03 -0800317 EXPECT_CALL(*mockCryptIface, cryptFormat(_, _, _, _, _, _, _, _)).Times(1);
318
319 EXPECT_CALL(*mockCryptIface, cryptKeyslotAddByVolumeKey(_, _, _, _, _, _))
320 .Times(1);
321
322 EXPECT_CALL(*mockCryptIface, cryptLoad(_, _, _)).Times(1);
323
324 EXPECT_CALL(*mockCryptIface, cryptActivateByPassphrase(_, _, _, _, _, _))
John Wedig2443a022023-03-17 13:42:32 -0700325 .WillOnce(&createMappedDev);
John Wedigb810c922021-11-17 16:38:03 -0800326
John Wedig2443a022023-03-17 13:42:32 -0700327 EXPECT_CALL(*mockFsIface, runMkfs(StrEq(esObject->getCryptDevicePath())))
328 .WillOnce(Return(0));
John Wedigb810c922021-11-17 16:38:03 -0800329
John Wedigb17f8252022-01-12 14:24:26 -0800330 EXPECT_CALL(*mockFsIface, directoryExists(path(esObject->getMountPoint())))
331 .WillOnce(Return(false));
332
John Wedigb810c922021-11-17 16:38:03 -0800333 EXPECT_CALL(*mockFsIface, createDirectory(path(esObject->getMountPoint())))
334 .WillOnce(Return(false));
335
John Wedig972c3fa2021-12-29 17:30:41 -0800336 EXPECT_THROW(esObject->formatLuks(password, Volume::FilesystemType::ext4),
337 InternalFailure);
John Wedigb810c922021-11-17 16:38:03 -0800338 EXPECT_FALSE(esObject->isLocked());
John Wedig2443a022023-03-17 13:42:32 -0700339
340 EXPECT_EQ(0, removeMappedDev());
John Wedigb810c922021-11-17 16:38:03 -0800341}
342
343/* Test case where we fail to mount the filesystem. */
Ed Tanous82897c32022-02-21 14:11:59 -0800344TEST_F(EStoragedTest, MountFail)
John Wedigb810c922021-11-17 16:38:03 -0800345{
John Wedigb810c922021-11-17 16:38:03 -0800346 EXPECT_CALL(*mockCryptIface, cryptFormat(_, _, _, _, _, _, _, _)).Times(1);
347
348 EXPECT_CALL(*mockCryptIface, cryptKeyslotAddByVolumeKey(_, _, _, _, _, _))
349 .Times(1);
350
351 EXPECT_CALL(*mockCryptIface, cryptLoad(_, _, _)).Times(1);
352
353 EXPECT_CALL(*mockCryptIface, cryptActivateByPassphrase(_, _, _, _, _, _))
John Wedig2443a022023-03-17 13:42:32 -0700354 .WillOnce(&createMappedDev);
John Wedigb810c922021-11-17 16:38:03 -0800355
John Wedig2443a022023-03-17 13:42:32 -0700356 EXPECT_CALL(*mockFsIface, runMkfs(StrEq(esObject->getCryptDevicePath())))
357 .WillOnce(Return(0));
John Wedigb810c922021-11-17 16:38:03 -0800358
John Wedigb17f8252022-01-12 14:24:26 -0800359 EXPECT_CALL(*mockFsIface, directoryExists(path(esObject->getMountPoint())))
360 .WillOnce(Return(false));
361
John Wedigb810c922021-11-17 16:38:03 -0800362 EXPECT_CALL(*mockFsIface, createDirectory(path(esObject->getMountPoint())))
363 .WillOnce(Return(true));
364
365 EXPECT_CALL(*mockFsIface,
John Wedig2443a022023-03-17 13:42:32 -0700366 doMount(StrEq(esObject->getCryptDevicePath()),
John Wedigb810c922021-11-17 16:38:03 -0800367 StrEq(esObject->getMountPoint()), _, _, _))
368 .WillOnce(Return(-1));
369
370 EXPECT_CALL(*mockFsIface, removeDirectory(path(esObject->getMountPoint())))
371 .WillOnce(Return(true));
372
John Wedig972c3fa2021-12-29 17:30:41 -0800373 EXPECT_THROW(esObject->formatLuks(password, Volume::FilesystemType::ext4),
374 InternalFailure);
John Wedigb810c922021-11-17 16:38:03 -0800375 EXPECT_FALSE(esObject->isLocked());
John Wedig2443a022023-03-17 13:42:32 -0700376
377 EXPECT_EQ(0, removeMappedDev());
John Wedigb810c922021-11-17 16:38:03 -0800378}
379
380/* Test case where we fail to unmount the filesystem. */
Ed Tanous82897c32022-02-21 14:11:59 -0800381TEST_F(EStoragedTest, UnmountFail)
John Wedigb810c922021-11-17 16:38:03 -0800382{
John Wedigb810c922021-11-17 16:38:03 -0800383 EXPECT_CALL(*mockCryptIface, cryptFormat(_, _, _, _, _, _, _, _)).Times(1);
384
385 EXPECT_CALL(*mockCryptIface, cryptKeyslotAddByVolumeKey(_, _, _, _, _, _))
386 .Times(1);
387
388 EXPECT_CALL(*mockCryptIface, cryptLoad(_, _, _)).Times(1);
389
390 EXPECT_CALL(*mockCryptIface, cryptActivateByPassphrase(_, _, _, _, _, _))
John Wedig2443a022023-03-17 13:42:32 -0700391 .WillOnce(&createMappedDev);
John Wedigb810c922021-11-17 16:38:03 -0800392
John Wedig2443a022023-03-17 13:42:32 -0700393 EXPECT_CALL(*mockFsIface, runMkfs(StrEq(esObject->getCryptDevicePath())))
394 .WillOnce(Return(0));
John Wedigb810c922021-11-17 16:38:03 -0800395
John Wedigb17f8252022-01-12 14:24:26 -0800396 EXPECT_CALL(*mockFsIface, directoryExists(path(esObject->getMountPoint())))
397 .WillOnce(Return(false));
398
John Wedigb810c922021-11-17 16:38:03 -0800399 EXPECT_CALL(*mockFsIface, createDirectory(path(esObject->getMountPoint())))
400 .WillOnce(Return(true));
401
402 EXPECT_CALL(*mockFsIface,
John Wedig2443a022023-03-17 13:42:32 -0700403 doMount(StrEq(esObject->getCryptDevicePath()),
John Wedigb810c922021-11-17 16:38:03 -0800404 StrEq(esObject->getMountPoint()), _, _, _))
405 .WillOnce(Return(0));
406
407 EXPECT_CALL(*mockFsIface, doUnmount(StrEq(esObject->getMountPoint())))
408 .WillOnce(Return(-1));
409
John Wedig972c3fa2021-12-29 17:30:41 -0800410 esObject->formatLuks(password, Volume::FilesystemType::ext4);
John Wedigb810c922021-11-17 16:38:03 -0800411 EXPECT_FALSE(esObject->isLocked());
412
John Wedig972c3fa2021-12-29 17:30:41 -0800413 EXPECT_THROW(esObject->lock(), InternalFailure);
John Wedigb810c922021-11-17 16:38:03 -0800414 EXPECT_FALSE(esObject->isLocked());
John Wedig2443a022023-03-17 13:42:32 -0700415
416 EXPECT_EQ(0, removeMappedDev());
John Wedigb810c922021-11-17 16:38:03 -0800417}
418
419/* Test case where we fail to remove the mount point. */
Ed Tanous82897c32022-02-21 14:11:59 -0800420TEST_F(EStoragedTest, RemoveMountPointFail)
John Wedigb810c922021-11-17 16:38:03 -0800421{
John Wedigb810c922021-11-17 16:38:03 -0800422 EXPECT_CALL(*mockCryptIface, cryptFormat(_, _, _, _, _, _, _, _)).Times(1);
423
424 EXPECT_CALL(*mockCryptIface, cryptKeyslotAddByVolumeKey(_, _, _, _, _, _))
425 .Times(1);
426
427 EXPECT_CALL(*mockCryptIface, cryptLoad(_, _, _)).Times(1);
428
429 EXPECT_CALL(*mockCryptIface, cryptActivateByPassphrase(_, _, _, _, _, _))
John Wedig2443a022023-03-17 13:42:32 -0700430 .WillOnce(&createMappedDev);
John Wedigb810c922021-11-17 16:38:03 -0800431
John Wedig2443a022023-03-17 13:42:32 -0700432 EXPECT_CALL(*mockFsIface, runMkfs(StrEq(esObject->getCryptDevicePath())))
433 .WillOnce(Return(0));
John Wedigb810c922021-11-17 16:38:03 -0800434
John Wedigb17f8252022-01-12 14:24:26 -0800435 EXPECT_CALL(*mockFsIface, directoryExists(path(esObject->getMountPoint())))
436 .WillOnce(Return(false));
437
John Wedigb810c922021-11-17 16:38:03 -0800438 EXPECT_CALL(*mockFsIface, createDirectory(path(esObject->getMountPoint())))
439 .WillOnce(Return(true));
440
441 EXPECT_CALL(*mockFsIface,
John Wedig2443a022023-03-17 13:42:32 -0700442 doMount(StrEq(esObject->getCryptDevicePath()),
John Wedigb810c922021-11-17 16:38:03 -0800443 StrEq(esObject->getMountPoint()), _, _, _))
444 .WillOnce(Return(0));
445
446 EXPECT_CALL(*mockFsIface, doUnmount(StrEq(esObject->getMountPoint())))
447 .WillOnce(Return(0));
448
449 EXPECT_CALL(*mockFsIface, removeDirectory(path(esObject->getMountPoint())))
450 .WillOnce(Return(false));
451
John Wedig972c3fa2021-12-29 17:30:41 -0800452 esObject->formatLuks(password, Volume::FilesystemType::ext4);
John Wedigb810c922021-11-17 16:38:03 -0800453 EXPECT_FALSE(esObject->isLocked());
454
455 /* This will fail to remove the mount point. */
John Wedig972c3fa2021-12-29 17:30:41 -0800456 EXPECT_THROW(esObject->lock(), InternalFailure);
John Wedigb810c922021-11-17 16:38:03 -0800457 EXPECT_FALSE(esObject->isLocked());
John Wedig2443a022023-03-17 13:42:32 -0700458
459 EXPECT_EQ(0, removeMappedDev());
John Wedigb810c922021-11-17 16:38:03 -0800460}
461
462/* Test case where we fail to deactivate the LUKS device. */
Ed Tanous82897c32022-02-21 14:11:59 -0800463TEST_F(EStoragedTest, DeactivateFail)
John Wedigb810c922021-11-17 16:38:03 -0800464{
John Wedigb810c922021-11-17 16:38:03 -0800465 EXPECT_CALL(*mockCryptIface, cryptFormat(_, _, _, _, _, _, _, _)).Times(1);
466
467 EXPECT_CALL(*mockCryptIface, cryptKeyslotAddByVolumeKey(_, _, _, _, _, _))
468 .Times(1);
469
470 EXPECT_CALL(*mockCryptIface, cryptLoad(_, _, _)).Times(1);
471
472 EXPECT_CALL(*mockCryptIface, cryptActivateByPassphrase(_, _, _, _, _, _))
John Wedig2443a022023-03-17 13:42:32 -0700473 .WillOnce(&createMappedDev);
John Wedigb810c922021-11-17 16:38:03 -0800474
John Wedig2443a022023-03-17 13:42:32 -0700475 EXPECT_CALL(*mockFsIface, runMkfs(StrEq(esObject->getCryptDevicePath())))
476 .WillOnce(Return(0));
John Wedigb810c922021-11-17 16:38:03 -0800477
John Wedigb17f8252022-01-12 14:24:26 -0800478 EXPECT_CALL(*mockFsIface, directoryExists(path(esObject->getMountPoint())))
479 .WillOnce(Return(false));
480
John Wedigb810c922021-11-17 16:38:03 -0800481 EXPECT_CALL(*mockFsIface, createDirectory(path(esObject->getMountPoint())))
482 .WillOnce(Return(true));
483
484 EXPECT_CALL(*mockFsIface,
John Wedig2443a022023-03-17 13:42:32 -0700485 doMount(StrEq(esObject->getCryptDevicePath()),
John Wedigb810c922021-11-17 16:38:03 -0800486 StrEq(esObject->getMountPoint()), _, _, _))
487 .WillOnce(Return(0));
488
489 EXPECT_CALL(*mockFsIface, doUnmount(StrEq(esObject->getMountPoint())))
490 .WillOnce(Return(0));
491
492 EXPECT_CALL(*mockFsIface, removeDirectory(path(esObject->getMountPoint())))
493 .WillOnce(Return(true));
494
495 EXPECT_CALL(*mockCryptIface, cryptDeactivate(_, _)).WillOnce(Return(-1));
496
497 /* Format the encrypted device. */
John Wedig972c3fa2021-12-29 17:30:41 -0800498 esObject->formatLuks(password, Volume::FilesystemType::ext4);
John Wedigb810c922021-11-17 16:38:03 -0800499 EXPECT_FALSE(esObject->isLocked());
500
John Wedig972c3fa2021-12-29 17:30:41 -0800501 EXPECT_THROW(esObject->lock(), InternalFailure);
John Wedigb810c922021-11-17 16:38:03 -0800502 EXPECT_FALSE(esObject->isLocked());
John Wedig2443a022023-03-17 13:42:32 -0700503
504 EXPECT_EQ(0, removeMappedDev());
John Wedigb810c922021-11-17 16:38:03 -0800505}
506
John Wedig8d5a3a02022-09-29 15:25:58 -0700507/* Test case where we successfully change the password. */
508TEST_F(EStoragedTest, ChangePasswordSuccess)
509{
510 std::string newPasswordString("newPassword");
511 std::vector<uint8_t> newPassword(newPasswordString.begin(),
512 newPasswordString.end());
513
514 EXPECT_CALL(*mockCryptIface, cryptLoad(_, _, _)).Times(1);
515
516 EXPECT_CALL(*mockCryptIface,
517 cryptKeyslotChangeByPassphrase(
518 _, _, _, reinterpret_cast<const char*>(password.data()),
519 password.size(),
520 reinterpret_cast<const char*>(newPassword.data()),
521 newPassword.size()))
522 .WillOnce(Return(0));
523
524 /* Change the password for the LUKS-encrypted device. */
525 esObject->changePassword(password, newPassword);
526}
527
528/* Test case where we fail to change the password. */
529TEST_F(EStoragedTest, ChangePasswordFail)
530{
531 std::string newPasswordString("newPassword");
532 std::vector<uint8_t> newPassword(newPasswordString.begin(),
533 newPasswordString.end());
534
535 EXPECT_CALL(*mockCryptIface, cryptLoad(_, _, _)).Times(1);
536
537 EXPECT_CALL(*mockCryptIface,
538 cryptKeyslotChangeByPassphrase(
539 _, _, _, reinterpret_cast<const char*>(password.data()),
540 password.size(),
541 reinterpret_cast<const char*>(newPassword.data()),
542 newPassword.size()))
543 .WillOnce(Return(-1));
544
545 EXPECT_THROW(esObject->changePassword(password, newPassword),
546 InternalFailure);
547}
548
John Wedigb810c922021-11-17 16:38:03 -0800549} // namespace estoraged_test