blob: 05f872fc2e2edf847de78c8c9ce201c3c1a093f6 [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"
John Wedigb810c922021-11-17 16:38:03 -08004
5#include <unistd.h>
6
John Wedig67a47442022-04-05 17:21:29 -07007#include <boost/asio/io_context.hpp>
8#include <sdbusplus/asio/connection.hpp>
9#include <sdbusplus/asio/object_server.hpp>
John Wedig972c3fa2021-12-29 17:30:41 -080010#include <xyz/openbmc_project/Common/error.hpp>
John Edward Broadbent59dffa62022-01-13 17:41:32 -080011#include <xyz/openbmc_project/Inventory/Item/Volume/client.hpp>
John Wedigb810c922021-11-17 16:38:03 -080012
13#include <exception>
14#include <filesystem>
15#include <fstream>
16#include <iterator>
John Wedig67a47442022-04-05 17:21:29 -070017#include <memory>
John Wedigb810c922021-11-17 16:38:03 -080018#include <string>
19#include <vector>
20
21#include <gmock/gmock.h>
22#include <gtest/gtest.h>
23
24namespace estoraged_test
25{
26
John Wedig972c3fa2021-12-29 17:30:41 -080027using sdbusplus::xyz::openbmc_project::Common::Error::InternalFailure;
28using sdbusplus::xyz::openbmc_project::Common::Error::ResourceNotFound;
29using sdbusplus::xyz::openbmc_project::Inventory::Item::server::Volume;
John Wedigb810c922021-11-17 16:38:03 -080030using std::filesystem::path;
31using ::testing::_;
32using ::testing::ContainsRegex;
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 Wedig67a47442022-04-05 17:21:29 -070041 const uint64_t testSize = 24;
John Wedigb810c922021-11-17 16:38:03 -080042 std::ofstream testFile;
Ed Tanous82897c32022-02-21 14:11:59 -080043 const char* testPath = "/test/openbmc_project/storage/test_dev";
44 const char* estoragedInterface =
John Wedig972c3fa2021-12-29 17:30:41 -080045 "xyz.openbmc_project.Inventory.Item.Volume";
John Edward Broadbent86dfb242022-03-14 11:04:36 -070046 const char* driveInterface = "xyz.openbmc_project.Inventory.Item.Drive";
John Wedigb810c922021-11-17 16:38:03 -080047 std::string passwordString;
48 std::vector<uint8_t> password;
Ed Tanous82897c32022-02-21 14:11:59 -080049 MockCryptsetupInterface* mockCryptIface{};
50 MockFilesystemInterface* mockFsIface{};
John Wedig67a47442022-04-05 17:21:29 -070051 boost::asio::io_context io;
52 std::shared_ptr<sdbusplus::asio::connection> conn;
53 std::unique_ptr<sdbusplus::asio::object_server> objectServer;
54 std::unique_ptr<estoraged::EStoraged> esObject;
John Wedigb810c922021-11-17 16:38:03 -080055
Ed Tanous82897c32022-02-21 14:11:59 -080056 EStoragedTest() :
John Wedig67a47442022-04-05 17:21:29 -070057 passwordString("password"),
John Wedigb810c922021-11-17 16:38:03 -080058 password(passwordString.begin(), passwordString.end())
59 {}
60
61 void SetUp() override
62 {
63 /* Create an empty file that we'll pretend is a 'storage device'. */
64 testFile.open(testFileName,
65 std::ios::out | std::ios::binary | std::ios::trunc);
66 testFile.close();
67 if (testFile.fail())
68 {
69 throw std::runtime_error("Failed to open test file");
70 }
71
John Wedigb810c922021-11-17 16:38:03 -080072 std::unique_ptr<MockCryptsetupInterface> cryptIface =
73 std::make_unique<MockCryptsetupInterface>();
74 mockCryptIface = cryptIface.get();
75 std::unique_ptr<MockFilesystemInterface> fsIface =
76 std::make_unique<MockFilesystemInterface>();
77 mockFsIface = fsIface.get();
78
John Wedig67a47442022-04-05 17:21:29 -070079 conn = std::make_shared<sdbusplus::asio::connection>(io);
80 // request D-Bus server name.
81 conn->request_name("xyz.openbmc_project.eStoraged.test");
82 objectServer = std::make_unique<sdbusplus::asio::object_server>(conn);
83
Ed Tanous82897c32022-02-21 14:11:59 -080084 esObject = std::make_unique<estoraged::EStoraged>(
John Wedig67a47442022-04-05 17:21:29 -070085 *objectServer, testFileName, testLuksDevName, testSize,
John Edward Broadbente35e7362022-03-22 16:14:24 -070086 std::move(cryptIface), std::move(fsIface));
John Wedigb810c922021-11-17 16:38:03 -080087 }
88
89 void TearDown() override
90 {
91 EXPECT_EQ(0, unlink(testFileName));
92 }
93};
94
95/* Test case to format and then lock the LUKS device. */
Ed Tanous82897c32022-02-21 14:11:59 -080096TEST_F(EStoragedTest, FormatPass)
John Wedigb810c922021-11-17 16:38:03 -080097{
John Wedigb810c922021-11-17 16:38:03 -080098 EXPECT_CALL(*mockCryptIface, cryptFormat(_, _, _, _, _, _, _, _)).Times(1);
99
100 EXPECT_CALL(*mockCryptIface, cryptKeyslotAddByVolumeKey(_, _, _, _, _, _))
101 .Times(1);
102
103 EXPECT_CALL(*mockCryptIface, cryptLoad(_, _, _)).Times(1);
104
105 EXPECT_CALL(*mockCryptIface, cryptActivateByPassphrase(_, _, _, _, _, _))
106 .Times(1);
107
108 EXPECT_CALL(*mockFsIface, runMkfs(testLuksDevName)).WillOnce(Return(0));
109
John Wedigb17f8252022-01-12 14:24:26 -0800110 EXPECT_CALL(*mockFsIface, directoryExists(path(esObject->getMountPoint())))
111 .WillOnce(Return(false));
112
John Wedigb810c922021-11-17 16:38:03 -0800113 EXPECT_CALL(*mockFsIface, createDirectory(path(esObject->getMountPoint())))
114 .WillOnce(Return(true));
115
116 EXPECT_CALL(*mockFsIface,
117 doMount(ContainsRegex("/dev/mapper/"),
118 StrEq(esObject->getMountPoint()), _, _, _))
119 .WillOnce(Return(0));
120
121 EXPECT_CALL(*mockFsIface, doUnmount(StrEq(esObject->getMountPoint())))
122 .WillOnce(Return(0));
123
124 EXPECT_CALL(*mockFsIface, removeDirectory(path(esObject->getMountPoint())))
125 .WillOnce(Return(true));
126
127 EXPECT_CALL(*mockCryptIface, cryptDeactivate(_, _)).Times(1);
128
129 /* Format the encrypted device. */
John Wedig972c3fa2021-12-29 17:30:41 -0800130 esObject->formatLuks(password, Volume::FilesystemType::ext4);
John Wedigb810c922021-11-17 16:38:03 -0800131 EXPECT_FALSE(esObject->isLocked());
132
John Wedig972c3fa2021-12-29 17:30:41 -0800133 esObject->lock();
John Wedigb810c922021-11-17 16:38:03 -0800134 EXPECT_TRUE(esObject->isLocked());
135}
136
John Wedigb17f8252022-01-12 14:24:26 -0800137/*
138 * Test case where the mount point directory already exists, so it shouldn't
139 * try to create it.
140 */
Ed Tanous82897c32022-02-21 14:11:59 -0800141TEST_F(EStoragedTest, MountPointExistsPass)
John Wedigb17f8252022-01-12 14:24:26 -0800142{
John Wedigb17f8252022-01-12 14:24:26 -0800143 EXPECT_CALL(*mockCryptIface, cryptFormat(_, _, _, _, _, _, _, _)).Times(1);
144
145 EXPECT_CALL(*mockCryptIface, cryptKeyslotAddByVolumeKey(_, _, _, _, _, _))
146 .Times(1);
147
148 EXPECT_CALL(*mockCryptIface, cryptLoad(_, _, _)).Times(1);
149
150 EXPECT_CALL(*mockCryptIface, cryptActivateByPassphrase(_, _, _, _, _, _))
151 .Times(1);
152
153 EXPECT_CALL(*mockFsIface, runMkfs(testLuksDevName)).WillOnce(Return(0));
154
155 EXPECT_CALL(*mockFsIface, directoryExists(path(esObject->getMountPoint())))
156 .WillOnce(Return(true));
157
158 EXPECT_CALL(*mockFsIface, createDirectory(path(esObject->getMountPoint())))
159 .Times(0);
160
161 EXPECT_CALL(*mockFsIface,
162 doMount(ContainsRegex("/dev/mapper/"),
163 StrEq(esObject->getMountPoint()), _, _, _))
164 .WillOnce(Return(0));
165
166 EXPECT_CALL(*mockFsIface, doUnmount(StrEq(esObject->getMountPoint())))
167 .WillOnce(Return(0));
168
169 EXPECT_CALL(*mockFsIface, removeDirectory(path(esObject->getMountPoint())))
170 .WillOnce(Return(true));
171
172 EXPECT_CALL(*mockCryptIface, cryptDeactivate(_, _)).Times(1);
173
174 /* Format the encrypted device. */
175 esObject->formatLuks(password, Volume::FilesystemType::ext4);
176 EXPECT_FALSE(esObject->isLocked());
177
178 esObject->lock();
179 EXPECT_TRUE(esObject->isLocked());
180}
181
John Wedigb810c922021-11-17 16:38:03 -0800182/* Test case where the device/file doesn't exist. */
Ed Tanous82897c32022-02-21 14:11:59 -0800183TEST_F(EStoragedTest, FormatNoDeviceFail)
John Wedigb810c922021-11-17 16:38:03 -0800184{
185 /* Delete the test file. */
186 EXPECT_EQ(0, unlink(testFileName));
187
John Wedig972c3fa2021-12-29 17:30:41 -0800188 EXPECT_THROW(esObject->formatLuks(password, Volume::FilesystemType::ext4),
189 ResourceNotFound);
John Wedigb810c922021-11-17 16:38:03 -0800190 EXPECT_FALSE(esObject->isLocked());
191
192 /* Create the test file again, so that the TearDown function works. */
193 testFile.open(testFileName,
194 std::ios::out | std::ios::binary | std::ios::trunc);
195 testFile.close();
196}
197
198/* Test case where we fail to format the LUKS device. */
Ed Tanous82897c32022-02-21 14:11:59 -0800199TEST_F(EStoragedTest, FormatFail)
John Wedigb810c922021-11-17 16:38:03 -0800200{
201 EXPECT_CALL(*mockCryptIface, cryptFormat(_, _, _, _, _, _, _, _))
202 .WillOnce(Return(-1));
203
John Wedig972c3fa2021-12-29 17:30:41 -0800204 EXPECT_THROW(esObject->formatLuks(password, Volume::FilesystemType::ext4),
205 InternalFailure);
John Wedigb810c922021-11-17 16:38:03 -0800206 EXPECT_FALSE(esObject->isLocked());
207}
208
209/* Test case where we fail to set the password for the LUKS device. */
Ed Tanous82897c32022-02-21 14:11:59 -0800210TEST_F(EStoragedTest, AddKeyslotFail)
John Wedigb810c922021-11-17 16:38:03 -0800211{
John Wedigb810c922021-11-17 16:38:03 -0800212 EXPECT_CALL(*mockCryptIface, cryptFormat(_, _, _, _, _, _, _, _)).Times(1);
213
214 EXPECT_CALL(*mockCryptIface, cryptKeyslotAddByVolumeKey(_, _, _, _, _, _))
215 .WillOnce(Return(-1));
216
John Wedig972c3fa2021-12-29 17:30:41 -0800217 EXPECT_THROW(esObject->formatLuks(password, Volume::FilesystemType::ext4),
218 InternalFailure);
John Wedigb810c922021-11-17 16:38:03 -0800219 EXPECT_TRUE(esObject->isLocked());
220}
221
222/* Test case where we fail to load the LUKS header. */
Ed Tanous82897c32022-02-21 14:11:59 -0800223TEST_F(EStoragedTest, LoadLuksHeaderFail)
John Wedigb810c922021-11-17 16:38:03 -0800224{
John Wedigb810c922021-11-17 16:38:03 -0800225 EXPECT_CALL(*mockCryptIface, cryptFormat(_, _, _, _, _, _, _, _)).Times(1);
226
227 EXPECT_CALL(*mockCryptIface, cryptKeyslotAddByVolumeKey(_, _, _, _, _, _))
228 .Times(1);
229
230 EXPECT_CALL(*mockCryptIface, cryptLoad(_, _, _)).WillOnce(Return(-1));
231
John Wedig972c3fa2021-12-29 17:30:41 -0800232 EXPECT_THROW(esObject->formatLuks(password, Volume::FilesystemType::ext4),
233 InternalFailure);
John Wedigb810c922021-11-17 16:38:03 -0800234 EXPECT_TRUE(esObject->isLocked());
235}
236
237/* Test case where we fail to activate the LUKS device. */
Ed Tanous82897c32022-02-21 14:11:59 -0800238TEST_F(EStoragedTest, ActivateFail)
John Wedigb810c922021-11-17 16:38:03 -0800239{
John Wedigb810c922021-11-17 16:38:03 -0800240 EXPECT_CALL(*mockCryptIface, cryptFormat(_, _, _, _, _, _, _, _)).Times(1);
241
242 EXPECT_CALL(*mockCryptIface, cryptKeyslotAddByVolumeKey(_, _, _, _, _, _))
243 .Times(1);
244
245 EXPECT_CALL(*mockCryptIface, cryptLoad(_, _, _)).Times(1);
246
247 EXPECT_CALL(*mockCryptIface, cryptActivateByPassphrase(_, _, _, _, _, _))
248 .WillOnce(Return(-1));
249
John Wedig972c3fa2021-12-29 17:30:41 -0800250 EXPECT_THROW(esObject->formatLuks(password, Volume::FilesystemType::ext4),
251 InternalFailure);
John Wedigb810c922021-11-17 16:38:03 -0800252 EXPECT_TRUE(esObject->isLocked());
253}
254
255/* Test case where we fail to create the filesystem. */
Ed Tanous82897c32022-02-21 14:11:59 -0800256TEST_F(EStoragedTest, CreateFilesystemFail)
John Wedigb810c922021-11-17 16:38:03 -0800257{
John Wedigb810c922021-11-17 16:38:03 -0800258 EXPECT_CALL(*mockCryptIface, cryptFormat(_, _, _, _, _, _, _, _)).Times(1);
259
260 EXPECT_CALL(*mockCryptIface, cryptKeyslotAddByVolumeKey(_, _, _, _, _, _))
261 .Times(1);
262
263 EXPECT_CALL(*mockCryptIface, cryptLoad(_, _, _)).Times(1);
264
265 EXPECT_CALL(*mockCryptIface, cryptActivateByPassphrase(_, _, _, _, _, _))
266 .Times(1);
267
268 EXPECT_CALL(*mockFsIface, runMkfs(testLuksDevName)).WillOnce(Return(-1));
269
John Wedig972c3fa2021-12-29 17:30:41 -0800270 EXPECT_THROW(esObject->formatLuks(password, Volume::FilesystemType::ext4),
271 InternalFailure);
John Wedigb810c922021-11-17 16:38:03 -0800272 EXPECT_FALSE(esObject->isLocked());
273}
274
275/* Test case where we fail to create the mount point. */
Ed Tanous82897c32022-02-21 14:11:59 -0800276TEST_F(EStoragedTest, CreateMountPointFail)
John Wedigb810c922021-11-17 16:38:03 -0800277{
John Wedigb810c922021-11-17 16:38:03 -0800278 EXPECT_CALL(*mockCryptIface, cryptFormat(_, _, _, _, _, _, _, _)).Times(1);
279
280 EXPECT_CALL(*mockCryptIface, cryptKeyslotAddByVolumeKey(_, _, _, _, _, _))
281 .Times(1);
282
283 EXPECT_CALL(*mockCryptIface, cryptLoad(_, _, _)).Times(1);
284
285 EXPECT_CALL(*mockCryptIface, cryptActivateByPassphrase(_, _, _, _, _, _))
286 .Times(1);
287
288 EXPECT_CALL(*mockFsIface, runMkfs(testLuksDevName)).WillOnce(Return(0));
289
John Wedigb17f8252022-01-12 14:24:26 -0800290 EXPECT_CALL(*mockFsIface, directoryExists(path(esObject->getMountPoint())))
291 .WillOnce(Return(false));
292
John Wedigb810c922021-11-17 16:38:03 -0800293 EXPECT_CALL(*mockFsIface, createDirectory(path(esObject->getMountPoint())))
294 .WillOnce(Return(false));
295
John Wedig972c3fa2021-12-29 17:30:41 -0800296 EXPECT_THROW(esObject->formatLuks(password, Volume::FilesystemType::ext4),
297 InternalFailure);
John Wedigb810c922021-11-17 16:38:03 -0800298 EXPECT_FALSE(esObject->isLocked());
299}
300
301/* Test case where we fail to mount the filesystem. */
Ed Tanous82897c32022-02-21 14:11:59 -0800302TEST_F(EStoragedTest, MountFail)
John Wedigb810c922021-11-17 16:38:03 -0800303{
John Wedigb810c922021-11-17 16:38:03 -0800304 EXPECT_CALL(*mockCryptIface, cryptFormat(_, _, _, _, _, _, _, _)).Times(1);
305
306 EXPECT_CALL(*mockCryptIface, cryptKeyslotAddByVolumeKey(_, _, _, _, _, _))
307 .Times(1);
308
309 EXPECT_CALL(*mockCryptIface, cryptLoad(_, _, _)).Times(1);
310
311 EXPECT_CALL(*mockCryptIface, cryptActivateByPassphrase(_, _, _, _, _, _))
312 .Times(1);
313
314 EXPECT_CALL(*mockFsIface, runMkfs(testLuksDevName)).WillOnce(Return(0));
315
John Wedigb17f8252022-01-12 14:24:26 -0800316 EXPECT_CALL(*mockFsIface, directoryExists(path(esObject->getMountPoint())))
317 .WillOnce(Return(false));
318
John Wedigb810c922021-11-17 16:38:03 -0800319 EXPECT_CALL(*mockFsIface, createDirectory(path(esObject->getMountPoint())))
320 .WillOnce(Return(true));
321
322 EXPECT_CALL(*mockFsIface,
323 doMount(ContainsRegex("/dev/mapper/"),
324 StrEq(esObject->getMountPoint()), _, _, _))
325 .WillOnce(Return(-1));
326
327 EXPECT_CALL(*mockFsIface, removeDirectory(path(esObject->getMountPoint())))
328 .WillOnce(Return(true));
329
John Wedig972c3fa2021-12-29 17:30:41 -0800330 EXPECT_THROW(esObject->formatLuks(password, Volume::FilesystemType::ext4),
331 InternalFailure);
John Wedigb810c922021-11-17 16:38:03 -0800332 EXPECT_FALSE(esObject->isLocked());
333}
334
335/* Test case where we fail to unmount the filesystem. */
Ed Tanous82897c32022-02-21 14:11:59 -0800336TEST_F(EStoragedTest, UnmountFail)
John Wedigb810c922021-11-17 16:38:03 -0800337{
John Wedigb810c922021-11-17 16:38:03 -0800338 EXPECT_CALL(*mockCryptIface, cryptFormat(_, _, _, _, _, _, _, _)).Times(1);
339
340 EXPECT_CALL(*mockCryptIface, cryptKeyslotAddByVolumeKey(_, _, _, _, _, _))
341 .Times(1);
342
343 EXPECT_CALL(*mockCryptIface, cryptLoad(_, _, _)).Times(1);
344
345 EXPECT_CALL(*mockCryptIface, cryptActivateByPassphrase(_, _, _, _, _, _))
346 .Times(1);
347
348 EXPECT_CALL(*mockFsIface, runMkfs(testLuksDevName)).WillOnce(Return(0));
349
John Wedigb17f8252022-01-12 14:24:26 -0800350 EXPECT_CALL(*mockFsIface, directoryExists(path(esObject->getMountPoint())))
351 .WillOnce(Return(false));
352
John Wedigb810c922021-11-17 16:38:03 -0800353 EXPECT_CALL(*mockFsIface, createDirectory(path(esObject->getMountPoint())))
354 .WillOnce(Return(true));
355
356 EXPECT_CALL(*mockFsIface,
357 doMount(ContainsRegex("/dev/mapper/"),
358 StrEq(esObject->getMountPoint()), _, _, _))
359 .WillOnce(Return(0));
360
361 EXPECT_CALL(*mockFsIface, doUnmount(StrEq(esObject->getMountPoint())))
362 .WillOnce(Return(-1));
363
John Wedig972c3fa2021-12-29 17:30:41 -0800364 esObject->formatLuks(password, Volume::FilesystemType::ext4);
John Wedigb810c922021-11-17 16:38:03 -0800365 EXPECT_FALSE(esObject->isLocked());
366
John Wedig972c3fa2021-12-29 17:30:41 -0800367 EXPECT_THROW(esObject->lock(), InternalFailure);
John Wedigb810c922021-11-17 16:38:03 -0800368 EXPECT_FALSE(esObject->isLocked());
369}
370
371/* Test case where we fail to remove the mount point. */
Ed Tanous82897c32022-02-21 14:11:59 -0800372TEST_F(EStoragedTest, RemoveMountPointFail)
John Wedigb810c922021-11-17 16:38:03 -0800373{
John Wedigb810c922021-11-17 16:38:03 -0800374 EXPECT_CALL(*mockCryptIface, cryptFormat(_, _, _, _, _, _, _, _)).Times(1);
375
376 EXPECT_CALL(*mockCryptIface, cryptKeyslotAddByVolumeKey(_, _, _, _, _, _))
377 .Times(1);
378
379 EXPECT_CALL(*mockCryptIface, cryptLoad(_, _, _)).Times(1);
380
381 EXPECT_CALL(*mockCryptIface, cryptActivateByPassphrase(_, _, _, _, _, _))
382 .Times(1);
383
384 EXPECT_CALL(*mockFsIface, runMkfs(testLuksDevName)).WillOnce(Return(0));
385
John Wedigb17f8252022-01-12 14:24:26 -0800386 EXPECT_CALL(*mockFsIface, directoryExists(path(esObject->getMountPoint())))
387 .WillOnce(Return(false));
388
John Wedigb810c922021-11-17 16:38:03 -0800389 EXPECT_CALL(*mockFsIface, createDirectory(path(esObject->getMountPoint())))
390 .WillOnce(Return(true));
391
392 EXPECT_CALL(*mockFsIface,
393 doMount(ContainsRegex("/dev/mapper/"),
394 StrEq(esObject->getMountPoint()), _, _, _))
395 .WillOnce(Return(0));
396
397 EXPECT_CALL(*mockFsIface, doUnmount(StrEq(esObject->getMountPoint())))
398 .WillOnce(Return(0));
399
400 EXPECT_CALL(*mockFsIface, removeDirectory(path(esObject->getMountPoint())))
401 .WillOnce(Return(false));
402
John Wedig972c3fa2021-12-29 17:30:41 -0800403 esObject->formatLuks(password, Volume::FilesystemType::ext4);
John Wedigb810c922021-11-17 16:38:03 -0800404 EXPECT_FALSE(esObject->isLocked());
405
406 /* This will fail to remove the mount point. */
John Wedig972c3fa2021-12-29 17:30:41 -0800407 EXPECT_THROW(esObject->lock(), InternalFailure);
John Wedigb810c922021-11-17 16:38:03 -0800408 EXPECT_FALSE(esObject->isLocked());
409}
410
411/* Test case where we fail to deactivate the LUKS device. */
Ed Tanous82897c32022-02-21 14:11:59 -0800412TEST_F(EStoragedTest, DeactivateFail)
John Wedigb810c922021-11-17 16:38:03 -0800413{
John Wedigb810c922021-11-17 16:38:03 -0800414 EXPECT_CALL(*mockCryptIface, cryptFormat(_, _, _, _, _, _, _, _)).Times(1);
415
416 EXPECT_CALL(*mockCryptIface, cryptKeyslotAddByVolumeKey(_, _, _, _, _, _))
417 .Times(1);
418
419 EXPECT_CALL(*mockCryptIface, cryptLoad(_, _, _)).Times(1);
420
421 EXPECT_CALL(*mockCryptIface, cryptActivateByPassphrase(_, _, _, _, _, _))
422 .Times(1);
423
424 EXPECT_CALL(*mockFsIface, runMkfs(testLuksDevName)).WillOnce(Return(0));
425
John Wedigb17f8252022-01-12 14:24:26 -0800426 EXPECT_CALL(*mockFsIface, directoryExists(path(esObject->getMountPoint())))
427 .WillOnce(Return(false));
428
John Wedigb810c922021-11-17 16:38:03 -0800429 EXPECT_CALL(*mockFsIface, createDirectory(path(esObject->getMountPoint())))
430 .WillOnce(Return(true));
431
432 EXPECT_CALL(*mockFsIface,
433 doMount(ContainsRegex("/dev/mapper/"),
434 StrEq(esObject->getMountPoint()), _, _, _))
435 .WillOnce(Return(0));
436
437 EXPECT_CALL(*mockFsIface, doUnmount(StrEq(esObject->getMountPoint())))
438 .WillOnce(Return(0));
439
440 EXPECT_CALL(*mockFsIface, removeDirectory(path(esObject->getMountPoint())))
441 .WillOnce(Return(true));
442
443 EXPECT_CALL(*mockCryptIface, cryptDeactivate(_, _)).WillOnce(Return(-1));
444
445 /* Format the encrypted device. */
John Wedig972c3fa2021-12-29 17:30:41 -0800446 esObject->formatLuks(password, Volume::FilesystemType::ext4);
John Wedigb810c922021-11-17 16:38:03 -0800447 EXPECT_FALSE(esObject->isLocked());
448
John Wedig972c3fa2021-12-29 17:30:41 -0800449 EXPECT_THROW(esObject->lock(), InternalFailure);
John Wedigb810c922021-11-17 16:38:03 -0800450 EXPECT_FALSE(esObject->isLocked());
451}
452
453} // namespace estoraged_test