blob: b98144e5e9e5414a1dc9d53b64e0da6b555762ba [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 Edward Broadbent5d799bb2022-03-22 16:14:24 -070042 const uint8_t testLifeTime = 25;
John Wedigb810c922021-11-17 16:38:03 -080043 std::ofstream testFile;
Ed Tanous82897c32022-02-21 14:11:59 -080044 const char* testPath = "/test/openbmc_project/storage/test_dev";
45 const char* estoragedInterface =
John Wedig972c3fa2021-12-29 17:30:41 -080046 "xyz.openbmc_project.Inventory.Item.Volume";
John Edward Broadbent86dfb242022-03-14 11:04:36 -070047 const char* driveInterface = "xyz.openbmc_project.Inventory.Item.Drive";
John Wedigb810c922021-11-17 16:38:03 -080048 std::string passwordString;
49 std::vector<uint8_t> password;
Ed Tanous82897c32022-02-21 14:11:59 -080050 MockCryptsetupInterface* mockCryptIface{};
51 MockFilesystemInterface* mockFsIface{};
John Wedig67a47442022-04-05 17:21:29 -070052 boost::asio::io_context io;
53 std::shared_ptr<sdbusplus::asio::connection> conn;
54 std::unique_ptr<sdbusplus::asio::object_server> objectServer;
55 std::unique_ptr<estoraged::EStoraged> esObject;
John Wedigb810c922021-11-17 16:38:03 -080056
Ed Tanous82897c32022-02-21 14:11:59 -080057 EStoragedTest() :
John Wedig67a47442022-04-05 17:21:29 -070058 passwordString("password"),
John Wedigb810c922021-11-17 16:38:03 -080059 password(passwordString.begin(), passwordString.end())
60 {}
61
62 void SetUp() override
63 {
64 /* Create an empty file that we'll pretend is a 'storage device'. */
65 testFile.open(testFileName,
66 std::ios::out | std::ios::binary | std::ios::trunc);
67 testFile.close();
68 if (testFile.fail())
69 {
70 throw std::runtime_error("Failed to open test file");
71 }
72
John Wedigb810c922021-11-17 16:38:03 -080073 std::unique_ptr<MockCryptsetupInterface> cryptIface =
74 std::make_unique<MockCryptsetupInterface>();
75 mockCryptIface = cryptIface.get();
76 std::unique_ptr<MockFilesystemInterface> fsIface =
77 std::make_unique<MockFilesystemInterface>();
78 mockFsIface = fsIface.get();
79
John Wedig67a47442022-04-05 17:21:29 -070080 conn = std::make_shared<sdbusplus::asio::connection>(io);
81 // request D-Bus server name.
82 conn->request_name("xyz.openbmc_project.eStoraged.test");
83 objectServer = std::make_unique<sdbusplus::asio::object_server>(conn);
84
Ed Tanous82897c32022-02-21 14:11:59 -080085 esObject = std::make_unique<estoraged::EStoraged>(
John Wedig67a47442022-04-05 17:21:29 -070086 *objectServer, testFileName, testLuksDevName, testSize,
John Edward Broadbent5d799bb2022-03-22 16:14:24 -070087 testLifeTime, std::move(cryptIface), std::move(fsIface));
John Wedigb810c922021-11-17 16:38:03 -080088 }
89
90 void TearDown() override
91 {
92 EXPECT_EQ(0, unlink(testFileName));
93 }
94};
95
96/* Test case to format and then lock the LUKS device. */
Ed Tanous82897c32022-02-21 14:11:59 -080097TEST_F(EStoragedTest, FormatPass)
John Wedigb810c922021-11-17 16:38:03 -080098{
John Wedigb810c922021-11-17 16:38:03 -080099 EXPECT_CALL(*mockCryptIface, cryptFormat(_, _, _, _, _, _, _, _)).Times(1);
100
101 EXPECT_CALL(*mockCryptIface, cryptKeyslotAddByVolumeKey(_, _, _, _, _, _))
102 .Times(1);
103
104 EXPECT_CALL(*mockCryptIface, cryptLoad(_, _, _)).Times(1);
105
106 EXPECT_CALL(*mockCryptIface, cryptActivateByPassphrase(_, _, _, _, _, _))
107 .Times(1);
108
109 EXPECT_CALL(*mockFsIface, runMkfs(testLuksDevName)).WillOnce(Return(0));
110
John Wedigb17f8252022-01-12 14:24:26 -0800111 EXPECT_CALL(*mockFsIface, directoryExists(path(esObject->getMountPoint())))
112 .WillOnce(Return(false));
113
John Wedigb810c922021-11-17 16:38:03 -0800114 EXPECT_CALL(*mockFsIface, createDirectory(path(esObject->getMountPoint())))
115 .WillOnce(Return(true));
116
117 EXPECT_CALL(*mockFsIface,
118 doMount(ContainsRegex("/dev/mapper/"),
119 StrEq(esObject->getMountPoint()), _, _, _))
120 .WillOnce(Return(0));
121
122 EXPECT_CALL(*mockFsIface, doUnmount(StrEq(esObject->getMountPoint())))
123 .WillOnce(Return(0));
124
125 EXPECT_CALL(*mockFsIface, removeDirectory(path(esObject->getMountPoint())))
126 .WillOnce(Return(true));
127
128 EXPECT_CALL(*mockCryptIface, cryptDeactivate(_, _)).Times(1);
129
130 /* Format the encrypted device. */
John Wedig972c3fa2021-12-29 17:30:41 -0800131 esObject->formatLuks(password, Volume::FilesystemType::ext4);
John Wedigb810c922021-11-17 16:38:03 -0800132 EXPECT_FALSE(esObject->isLocked());
133
John Wedig972c3fa2021-12-29 17:30:41 -0800134 esObject->lock();
John Wedigb810c922021-11-17 16:38:03 -0800135 EXPECT_TRUE(esObject->isLocked());
136}
137
John Wedigb17f8252022-01-12 14:24:26 -0800138/*
139 * Test case where the mount point directory already exists, so it shouldn't
140 * try to create it.
141 */
Ed Tanous82897c32022-02-21 14:11:59 -0800142TEST_F(EStoragedTest, MountPointExistsPass)
John Wedigb17f8252022-01-12 14:24:26 -0800143{
John Wedigb17f8252022-01-12 14:24:26 -0800144 EXPECT_CALL(*mockCryptIface, cryptFormat(_, _, _, _, _, _, _, _)).Times(1);
145
146 EXPECT_CALL(*mockCryptIface, cryptKeyslotAddByVolumeKey(_, _, _, _, _, _))
147 .Times(1);
148
149 EXPECT_CALL(*mockCryptIface, cryptLoad(_, _, _)).Times(1);
150
151 EXPECT_CALL(*mockCryptIface, cryptActivateByPassphrase(_, _, _, _, _, _))
152 .Times(1);
153
154 EXPECT_CALL(*mockFsIface, runMkfs(testLuksDevName)).WillOnce(Return(0));
155
156 EXPECT_CALL(*mockFsIface, directoryExists(path(esObject->getMountPoint())))
157 .WillOnce(Return(true));
158
159 EXPECT_CALL(*mockFsIface, createDirectory(path(esObject->getMountPoint())))
160 .Times(0);
161
162 EXPECT_CALL(*mockFsIface,
163 doMount(ContainsRegex("/dev/mapper/"),
164 StrEq(esObject->getMountPoint()), _, _, _))
165 .WillOnce(Return(0));
166
167 EXPECT_CALL(*mockFsIface, doUnmount(StrEq(esObject->getMountPoint())))
168 .WillOnce(Return(0));
169
170 EXPECT_CALL(*mockFsIface, removeDirectory(path(esObject->getMountPoint())))
171 .WillOnce(Return(true));
172
173 EXPECT_CALL(*mockCryptIface, cryptDeactivate(_, _)).Times(1);
174
175 /* Format the encrypted device. */
176 esObject->formatLuks(password, Volume::FilesystemType::ext4);
177 EXPECT_FALSE(esObject->isLocked());
178
179 esObject->lock();
180 EXPECT_TRUE(esObject->isLocked());
181}
182
John Wedigb810c922021-11-17 16:38:03 -0800183/* Test case where the device/file doesn't exist. */
Ed Tanous82897c32022-02-21 14:11:59 -0800184TEST_F(EStoragedTest, FormatNoDeviceFail)
John Wedigb810c922021-11-17 16:38:03 -0800185{
186 /* Delete the test file. */
187 EXPECT_EQ(0, unlink(testFileName));
188
John Wedig972c3fa2021-12-29 17:30:41 -0800189 EXPECT_THROW(esObject->formatLuks(password, Volume::FilesystemType::ext4),
190 ResourceNotFound);
John Wedigb810c922021-11-17 16:38:03 -0800191 EXPECT_FALSE(esObject->isLocked());
192
193 /* Create the test file again, so that the TearDown function works. */
194 testFile.open(testFileName,
195 std::ios::out | std::ios::binary | std::ios::trunc);
196 testFile.close();
197}
198
199/* Test case where we fail to format the LUKS device. */
Ed Tanous82897c32022-02-21 14:11:59 -0800200TEST_F(EStoragedTest, FormatFail)
John Wedigb810c922021-11-17 16:38:03 -0800201{
202 EXPECT_CALL(*mockCryptIface, cryptFormat(_, _, _, _, _, _, _, _))
203 .WillOnce(Return(-1));
204
John Wedig972c3fa2021-12-29 17:30:41 -0800205 EXPECT_THROW(esObject->formatLuks(password, Volume::FilesystemType::ext4),
206 InternalFailure);
John Wedigb810c922021-11-17 16:38:03 -0800207 EXPECT_FALSE(esObject->isLocked());
208}
209
210/* Test case where we fail to set the password for the LUKS device. */
Ed Tanous82897c32022-02-21 14:11:59 -0800211TEST_F(EStoragedTest, AddKeyslotFail)
John Wedigb810c922021-11-17 16:38:03 -0800212{
John Wedigb810c922021-11-17 16:38:03 -0800213 EXPECT_CALL(*mockCryptIface, cryptFormat(_, _, _, _, _, _, _, _)).Times(1);
214
215 EXPECT_CALL(*mockCryptIface, cryptKeyslotAddByVolumeKey(_, _, _, _, _, _))
216 .WillOnce(Return(-1));
217
John Wedig972c3fa2021-12-29 17:30:41 -0800218 EXPECT_THROW(esObject->formatLuks(password, Volume::FilesystemType::ext4),
219 InternalFailure);
John Wedigb810c922021-11-17 16:38:03 -0800220 EXPECT_TRUE(esObject->isLocked());
221}
222
223/* Test case where we fail to load the LUKS header. */
Ed Tanous82897c32022-02-21 14:11:59 -0800224TEST_F(EStoragedTest, LoadLuksHeaderFail)
John Wedigb810c922021-11-17 16:38:03 -0800225{
John Wedigb810c922021-11-17 16:38:03 -0800226 EXPECT_CALL(*mockCryptIface, cryptFormat(_, _, _, _, _, _, _, _)).Times(1);
227
228 EXPECT_CALL(*mockCryptIface, cryptKeyslotAddByVolumeKey(_, _, _, _, _, _))
229 .Times(1);
230
231 EXPECT_CALL(*mockCryptIface, cryptLoad(_, _, _)).WillOnce(Return(-1));
232
John Wedig972c3fa2021-12-29 17:30:41 -0800233 EXPECT_THROW(esObject->formatLuks(password, Volume::FilesystemType::ext4),
234 InternalFailure);
John Wedigb810c922021-11-17 16:38:03 -0800235 EXPECT_TRUE(esObject->isLocked());
236}
237
238/* Test case where we fail to activate the LUKS device. */
Ed Tanous82897c32022-02-21 14:11:59 -0800239TEST_F(EStoragedTest, ActivateFail)
John Wedigb810c922021-11-17 16:38:03 -0800240{
John Wedigb810c922021-11-17 16:38:03 -0800241 EXPECT_CALL(*mockCryptIface, cryptFormat(_, _, _, _, _, _, _, _)).Times(1);
242
243 EXPECT_CALL(*mockCryptIface, cryptKeyslotAddByVolumeKey(_, _, _, _, _, _))
244 .Times(1);
245
246 EXPECT_CALL(*mockCryptIface, cryptLoad(_, _, _)).Times(1);
247
248 EXPECT_CALL(*mockCryptIface, cryptActivateByPassphrase(_, _, _, _, _, _))
249 .WillOnce(Return(-1));
250
John Wedig972c3fa2021-12-29 17:30:41 -0800251 EXPECT_THROW(esObject->formatLuks(password, Volume::FilesystemType::ext4),
252 InternalFailure);
John Wedigb810c922021-11-17 16:38:03 -0800253 EXPECT_TRUE(esObject->isLocked());
254}
255
256/* Test case where we fail to create the filesystem. */
Ed Tanous82897c32022-02-21 14:11:59 -0800257TEST_F(EStoragedTest, CreateFilesystemFail)
John Wedigb810c922021-11-17 16:38:03 -0800258{
John Wedigb810c922021-11-17 16:38:03 -0800259 EXPECT_CALL(*mockCryptIface, cryptFormat(_, _, _, _, _, _, _, _)).Times(1);
260
261 EXPECT_CALL(*mockCryptIface, cryptKeyslotAddByVolumeKey(_, _, _, _, _, _))
262 .Times(1);
263
264 EXPECT_CALL(*mockCryptIface, cryptLoad(_, _, _)).Times(1);
265
266 EXPECT_CALL(*mockCryptIface, cryptActivateByPassphrase(_, _, _, _, _, _))
267 .Times(1);
268
269 EXPECT_CALL(*mockFsIface, runMkfs(testLuksDevName)).WillOnce(Return(-1));
270
John Wedig972c3fa2021-12-29 17:30:41 -0800271 EXPECT_THROW(esObject->formatLuks(password, Volume::FilesystemType::ext4),
272 InternalFailure);
John Wedigb810c922021-11-17 16:38:03 -0800273 EXPECT_FALSE(esObject->isLocked());
274}
275
276/* Test case where we fail to create the mount point. */
Ed Tanous82897c32022-02-21 14:11:59 -0800277TEST_F(EStoragedTest, CreateMountPointFail)
John Wedigb810c922021-11-17 16:38:03 -0800278{
John Wedigb810c922021-11-17 16:38:03 -0800279 EXPECT_CALL(*mockCryptIface, cryptFormat(_, _, _, _, _, _, _, _)).Times(1);
280
281 EXPECT_CALL(*mockCryptIface, cryptKeyslotAddByVolumeKey(_, _, _, _, _, _))
282 .Times(1);
283
284 EXPECT_CALL(*mockCryptIface, cryptLoad(_, _, _)).Times(1);
285
286 EXPECT_CALL(*mockCryptIface, cryptActivateByPassphrase(_, _, _, _, _, _))
287 .Times(1);
288
289 EXPECT_CALL(*mockFsIface, runMkfs(testLuksDevName)).WillOnce(Return(0));
290
John Wedigb17f8252022-01-12 14:24:26 -0800291 EXPECT_CALL(*mockFsIface, directoryExists(path(esObject->getMountPoint())))
292 .WillOnce(Return(false));
293
John Wedigb810c922021-11-17 16:38:03 -0800294 EXPECT_CALL(*mockFsIface, createDirectory(path(esObject->getMountPoint())))
295 .WillOnce(Return(false));
296
John Wedig972c3fa2021-12-29 17:30:41 -0800297 EXPECT_THROW(esObject->formatLuks(password, Volume::FilesystemType::ext4),
298 InternalFailure);
John Wedigb810c922021-11-17 16:38:03 -0800299 EXPECT_FALSE(esObject->isLocked());
300}
301
302/* Test case where we fail to mount the filesystem. */
Ed Tanous82897c32022-02-21 14:11:59 -0800303TEST_F(EStoragedTest, MountFail)
John Wedigb810c922021-11-17 16:38:03 -0800304{
John Wedigb810c922021-11-17 16:38:03 -0800305 EXPECT_CALL(*mockCryptIface, cryptFormat(_, _, _, _, _, _, _, _)).Times(1);
306
307 EXPECT_CALL(*mockCryptIface, cryptKeyslotAddByVolumeKey(_, _, _, _, _, _))
308 .Times(1);
309
310 EXPECT_CALL(*mockCryptIface, cryptLoad(_, _, _)).Times(1);
311
312 EXPECT_CALL(*mockCryptIface, cryptActivateByPassphrase(_, _, _, _, _, _))
313 .Times(1);
314
315 EXPECT_CALL(*mockFsIface, runMkfs(testLuksDevName)).WillOnce(Return(0));
316
John Wedigb17f8252022-01-12 14:24:26 -0800317 EXPECT_CALL(*mockFsIface, directoryExists(path(esObject->getMountPoint())))
318 .WillOnce(Return(false));
319
John Wedigb810c922021-11-17 16:38:03 -0800320 EXPECT_CALL(*mockFsIface, createDirectory(path(esObject->getMountPoint())))
321 .WillOnce(Return(true));
322
323 EXPECT_CALL(*mockFsIface,
324 doMount(ContainsRegex("/dev/mapper/"),
325 StrEq(esObject->getMountPoint()), _, _, _))
326 .WillOnce(Return(-1));
327
328 EXPECT_CALL(*mockFsIface, removeDirectory(path(esObject->getMountPoint())))
329 .WillOnce(Return(true));
330
John Wedig972c3fa2021-12-29 17:30:41 -0800331 EXPECT_THROW(esObject->formatLuks(password, Volume::FilesystemType::ext4),
332 InternalFailure);
John Wedigb810c922021-11-17 16:38:03 -0800333 EXPECT_FALSE(esObject->isLocked());
334}
335
336/* Test case where we fail to unmount the filesystem. */
Ed Tanous82897c32022-02-21 14:11:59 -0800337TEST_F(EStoragedTest, UnmountFail)
John Wedigb810c922021-11-17 16:38:03 -0800338{
John Wedigb810c922021-11-17 16:38:03 -0800339 EXPECT_CALL(*mockCryptIface, cryptFormat(_, _, _, _, _, _, _, _)).Times(1);
340
341 EXPECT_CALL(*mockCryptIface, cryptKeyslotAddByVolumeKey(_, _, _, _, _, _))
342 .Times(1);
343
344 EXPECT_CALL(*mockCryptIface, cryptLoad(_, _, _)).Times(1);
345
346 EXPECT_CALL(*mockCryptIface, cryptActivateByPassphrase(_, _, _, _, _, _))
347 .Times(1);
348
349 EXPECT_CALL(*mockFsIface, runMkfs(testLuksDevName)).WillOnce(Return(0));
350
John Wedigb17f8252022-01-12 14:24:26 -0800351 EXPECT_CALL(*mockFsIface, directoryExists(path(esObject->getMountPoint())))
352 .WillOnce(Return(false));
353
John Wedigb810c922021-11-17 16:38:03 -0800354 EXPECT_CALL(*mockFsIface, createDirectory(path(esObject->getMountPoint())))
355 .WillOnce(Return(true));
356
357 EXPECT_CALL(*mockFsIface,
358 doMount(ContainsRegex("/dev/mapper/"),
359 StrEq(esObject->getMountPoint()), _, _, _))
360 .WillOnce(Return(0));
361
362 EXPECT_CALL(*mockFsIface, doUnmount(StrEq(esObject->getMountPoint())))
363 .WillOnce(Return(-1));
364
John Wedig972c3fa2021-12-29 17:30:41 -0800365 esObject->formatLuks(password, Volume::FilesystemType::ext4);
John Wedigb810c922021-11-17 16:38:03 -0800366 EXPECT_FALSE(esObject->isLocked());
367
John Wedig972c3fa2021-12-29 17:30:41 -0800368 EXPECT_THROW(esObject->lock(), InternalFailure);
John Wedigb810c922021-11-17 16:38:03 -0800369 EXPECT_FALSE(esObject->isLocked());
370}
371
372/* Test case where we fail to remove the mount point. */
Ed Tanous82897c32022-02-21 14:11:59 -0800373TEST_F(EStoragedTest, RemoveMountPointFail)
John Wedigb810c922021-11-17 16:38:03 -0800374{
John Wedigb810c922021-11-17 16:38:03 -0800375 EXPECT_CALL(*mockCryptIface, cryptFormat(_, _, _, _, _, _, _, _)).Times(1);
376
377 EXPECT_CALL(*mockCryptIface, cryptKeyslotAddByVolumeKey(_, _, _, _, _, _))
378 .Times(1);
379
380 EXPECT_CALL(*mockCryptIface, cryptLoad(_, _, _)).Times(1);
381
382 EXPECT_CALL(*mockCryptIface, cryptActivateByPassphrase(_, _, _, _, _, _))
383 .Times(1);
384
385 EXPECT_CALL(*mockFsIface, runMkfs(testLuksDevName)).WillOnce(Return(0));
386
John Wedigb17f8252022-01-12 14:24:26 -0800387 EXPECT_CALL(*mockFsIface, directoryExists(path(esObject->getMountPoint())))
388 .WillOnce(Return(false));
389
John Wedigb810c922021-11-17 16:38:03 -0800390 EXPECT_CALL(*mockFsIface, createDirectory(path(esObject->getMountPoint())))
391 .WillOnce(Return(true));
392
393 EXPECT_CALL(*mockFsIface,
394 doMount(ContainsRegex("/dev/mapper/"),
395 StrEq(esObject->getMountPoint()), _, _, _))
396 .WillOnce(Return(0));
397
398 EXPECT_CALL(*mockFsIface, doUnmount(StrEq(esObject->getMountPoint())))
399 .WillOnce(Return(0));
400
401 EXPECT_CALL(*mockFsIface, removeDirectory(path(esObject->getMountPoint())))
402 .WillOnce(Return(false));
403
John Wedig972c3fa2021-12-29 17:30:41 -0800404 esObject->formatLuks(password, Volume::FilesystemType::ext4);
John Wedigb810c922021-11-17 16:38:03 -0800405 EXPECT_FALSE(esObject->isLocked());
406
407 /* This will fail to remove the mount point. */
John Wedig972c3fa2021-12-29 17:30:41 -0800408 EXPECT_THROW(esObject->lock(), InternalFailure);
John Wedigb810c922021-11-17 16:38:03 -0800409 EXPECT_FALSE(esObject->isLocked());
410}
411
412/* Test case where we fail to deactivate the LUKS device. */
Ed Tanous82897c32022-02-21 14:11:59 -0800413TEST_F(EStoragedTest, DeactivateFail)
John Wedigb810c922021-11-17 16:38:03 -0800414{
John Wedigb810c922021-11-17 16:38:03 -0800415 EXPECT_CALL(*mockCryptIface, cryptFormat(_, _, _, _, _, _, _, _)).Times(1);
416
417 EXPECT_CALL(*mockCryptIface, cryptKeyslotAddByVolumeKey(_, _, _, _, _, _))
418 .Times(1);
419
420 EXPECT_CALL(*mockCryptIface, cryptLoad(_, _, _)).Times(1);
421
422 EXPECT_CALL(*mockCryptIface, cryptActivateByPassphrase(_, _, _, _, _, _))
423 .Times(1);
424
425 EXPECT_CALL(*mockFsIface, runMkfs(testLuksDevName)).WillOnce(Return(0));
426
John Wedigb17f8252022-01-12 14:24:26 -0800427 EXPECT_CALL(*mockFsIface, directoryExists(path(esObject->getMountPoint())))
428 .WillOnce(Return(false));
429
John Wedigb810c922021-11-17 16:38:03 -0800430 EXPECT_CALL(*mockFsIface, createDirectory(path(esObject->getMountPoint())))
431 .WillOnce(Return(true));
432
433 EXPECT_CALL(*mockFsIface,
434 doMount(ContainsRegex("/dev/mapper/"),
435 StrEq(esObject->getMountPoint()), _, _, _))
436 .WillOnce(Return(0));
437
438 EXPECT_CALL(*mockFsIface, doUnmount(StrEq(esObject->getMountPoint())))
439 .WillOnce(Return(0));
440
441 EXPECT_CALL(*mockFsIface, removeDirectory(path(esObject->getMountPoint())))
442 .WillOnce(Return(true));
443
444 EXPECT_CALL(*mockCryptIface, cryptDeactivate(_, _)).WillOnce(Return(-1));
445
446 /* Format the encrypted device. */
John Wedig972c3fa2021-12-29 17:30:41 -0800447 esObject->formatLuks(password, Volume::FilesystemType::ext4);
John Wedigb810c922021-11-17 16:38:03 -0800448 EXPECT_FALSE(esObject->isLocked());
449
John Wedig972c3fa2021-12-29 17:30:41 -0800450 EXPECT_THROW(esObject->lock(), InternalFailure);
John Wedigb810c922021-11-17 16:38:03 -0800451 EXPECT_FALSE(esObject->isLocked());
452}
453
454} // namespace estoraged_test