blob: 89581abf484857830b3f0531566c2b45c69364ff [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 Wedig6c0d8ce2022-04-22 14:00:43 -070041 const std::string testConfigPath =
42 "/xyz/openbmc_project/inventory/system/board/test_board/test_emmc";
John Wedig67a47442022-04-05 17:21:29 -070043 const uint64_t testSize = 24;
John Edward Broadbent5d799bb2022-03-22 16:14:24 -070044 const uint8_t testLifeTime = 25;
John Wedigb810c922021-11-17 16:38:03 -080045 std::ofstream testFile;
John Wedigb810c922021-11-17 16:38:03 -080046 std::string passwordString;
47 std::vector<uint8_t> password;
Ed Tanous82897c32022-02-21 14:11:59 -080048 MockCryptsetupInterface* mockCryptIface{};
49 MockFilesystemInterface* mockFsIface{};
John Wedig67a47442022-04-05 17:21:29 -070050 boost::asio::io_context io;
51 std::shared_ptr<sdbusplus::asio::connection> conn;
52 std::unique_ptr<sdbusplus::asio::object_server> objectServer;
53 std::unique_ptr<estoraged::EStoraged> esObject;
John Wedigb810c922021-11-17 16:38:03 -080054
Ed Tanous82897c32022-02-21 14:11:59 -080055 EStoragedTest() :
John Wedig67a47442022-04-05 17:21:29 -070056 passwordString("password"),
John Wedigb810c922021-11-17 16:38:03 -080057 password(passwordString.begin(), passwordString.end())
58 {}
59
60 void SetUp() override
61 {
62 /* Create an empty file that we'll pretend is a 'storage device'. */
63 testFile.open(testFileName,
64 std::ios::out | std::ios::binary | std::ios::trunc);
65 testFile.close();
66 if (testFile.fail())
67 {
68 throw std::runtime_error("Failed to open test file");
69 }
70
John Wedigb810c922021-11-17 16:38:03 -080071 std::unique_ptr<MockCryptsetupInterface> cryptIface =
72 std::make_unique<MockCryptsetupInterface>();
73 mockCryptIface = cryptIface.get();
74 std::unique_ptr<MockFilesystemInterface> fsIface =
75 std::make_unique<MockFilesystemInterface>();
76 mockFsIface = fsIface.get();
77
John Wedig67a47442022-04-05 17:21:29 -070078 conn = std::make_shared<sdbusplus::asio::connection>(io);
79 // request D-Bus server name.
80 conn->request_name("xyz.openbmc_project.eStoraged.test");
81 objectServer = std::make_unique<sdbusplus::asio::object_server>(conn);
82
Ed Tanous82897c32022-02-21 14:11:59 -080083 esObject = std::make_unique<estoraged::EStoraged>(
John Wedig6c0d8ce2022-04-22 14:00:43 -070084 *objectServer, testConfigPath, testFileName, testLuksDevName,
85 testSize, testLifeTime, std::move(cryptIface), std::move(fsIface));
John Wedigb810c922021-11-17 16:38:03 -080086 }
87
88 void TearDown() override
89 {
90 EXPECT_EQ(0, unlink(testFileName));
91 }
92};
93
94/* Test case to format and then lock the LUKS device. */
Ed Tanous82897c32022-02-21 14:11:59 -080095TEST_F(EStoragedTest, FormatPass)
John Wedigb810c922021-11-17 16:38:03 -080096{
John Wedigb810c922021-11-17 16:38:03 -080097 EXPECT_CALL(*mockCryptIface, cryptFormat(_, _, _, _, _, _, _, _)).Times(1);
98
99 EXPECT_CALL(*mockCryptIface, cryptKeyslotAddByVolumeKey(_, _, _, _, _, _))
100 .Times(1);
101
102 EXPECT_CALL(*mockCryptIface, cryptLoad(_, _, _)).Times(1);
103
104 EXPECT_CALL(*mockCryptIface, cryptActivateByPassphrase(_, _, _, _, _, _))
105 .Times(1);
106
107 EXPECT_CALL(*mockFsIface, runMkfs(testLuksDevName)).WillOnce(Return(0));
108
John Wedigb17f8252022-01-12 14:24:26 -0800109 EXPECT_CALL(*mockFsIface, directoryExists(path(esObject->getMountPoint())))
110 .WillOnce(Return(false));
111
John Wedigb810c922021-11-17 16:38:03 -0800112 EXPECT_CALL(*mockFsIface, createDirectory(path(esObject->getMountPoint())))
113 .WillOnce(Return(true));
114
115 EXPECT_CALL(*mockFsIface,
116 doMount(ContainsRegex("/dev/mapper/"),
117 StrEq(esObject->getMountPoint()), _, _, _))
118 .WillOnce(Return(0));
119
120 EXPECT_CALL(*mockFsIface, doUnmount(StrEq(esObject->getMountPoint())))
121 .WillOnce(Return(0));
122
123 EXPECT_CALL(*mockFsIface, removeDirectory(path(esObject->getMountPoint())))
124 .WillOnce(Return(true));
125
126 EXPECT_CALL(*mockCryptIface, cryptDeactivate(_, _)).Times(1);
127
128 /* Format the encrypted device. */
John Wedig972c3fa2021-12-29 17:30:41 -0800129 esObject->formatLuks(password, Volume::FilesystemType::ext4);
John Wedigb810c922021-11-17 16:38:03 -0800130 EXPECT_FALSE(esObject->isLocked());
131
John Wedig972c3fa2021-12-29 17:30:41 -0800132 esObject->lock();
John Wedigb810c922021-11-17 16:38:03 -0800133 EXPECT_TRUE(esObject->isLocked());
134}
135
John Wedigb17f8252022-01-12 14:24:26 -0800136/*
137 * Test case where the mount point directory already exists, so it shouldn't
138 * try to create it.
139 */
Ed Tanous82897c32022-02-21 14:11:59 -0800140TEST_F(EStoragedTest, MountPointExistsPass)
John Wedigb17f8252022-01-12 14:24:26 -0800141{
John Wedigb17f8252022-01-12 14:24:26 -0800142 EXPECT_CALL(*mockCryptIface, cryptFormat(_, _, _, _, _, _, _, _)).Times(1);
143
144 EXPECT_CALL(*mockCryptIface, cryptKeyslotAddByVolumeKey(_, _, _, _, _, _))
145 .Times(1);
146
147 EXPECT_CALL(*mockCryptIface, cryptLoad(_, _, _)).Times(1);
148
149 EXPECT_CALL(*mockCryptIface, cryptActivateByPassphrase(_, _, _, _, _, _))
150 .Times(1);
151
152 EXPECT_CALL(*mockFsIface, runMkfs(testLuksDevName)).WillOnce(Return(0));
153
154 EXPECT_CALL(*mockFsIface, directoryExists(path(esObject->getMountPoint())))
155 .WillOnce(Return(true));
156
157 EXPECT_CALL(*mockFsIface, createDirectory(path(esObject->getMountPoint())))
158 .Times(0);
159
160 EXPECT_CALL(*mockFsIface,
161 doMount(ContainsRegex("/dev/mapper/"),
162 StrEq(esObject->getMountPoint()), _, _, _))
163 .WillOnce(Return(0));
164
165 EXPECT_CALL(*mockFsIface, doUnmount(StrEq(esObject->getMountPoint())))
166 .WillOnce(Return(0));
167
168 EXPECT_CALL(*mockFsIface, removeDirectory(path(esObject->getMountPoint())))
169 .WillOnce(Return(true));
170
171 EXPECT_CALL(*mockCryptIface, cryptDeactivate(_, _)).Times(1);
172
173 /* Format the encrypted device. */
174 esObject->formatLuks(password, Volume::FilesystemType::ext4);
175 EXPECT_FALSE(esObject->isLocked());
176
177 esObject->lock();
178 EXPECT_TRUE(esObject->isLocked());
179}
180
John Wedigb810c922021-11-17 16:38:03 -0800181/* Test case where the device/file doesn't exist. */
Ed Tanous82897c32022-02-21 14:11:59 -0800182TEST_F(EStoragedTest, FormatNoDeviceFail)
John Wedigb810c922021-11-17 16:38:03 -0800183{
184 /* Delete the test file. */
185 EXPECT_EQ(0, unlink(testFileName));
186
John Wedig972c3fa2021-12-29 17:30:41 -0800187 EXPECT_THROW(esObject->formatLuks(password, Volume::FilesystemType::ext4),
188 ResourceNotFound);
John Wedigb810c922021-11-17 16:38:03 -0800189 EXPECT_FALSE(esObject->isLocked());
190
191 /* Create the test file again, so that the TearDown function works. */
192 testFile.open(testFileName,
193 std::ios::out | std::ios::binary | std::ios::trunc);
194 testFile.close();
195}
196
197/* Test case where we fail to format the LUKS device. */
Ed Tanous82897c32022-02-21 14:11:59 -0800198TEST_F(EStoragedTest, FormatFail)
John Wedigb810c922021-11-17 16:38:03 -0800199{
200 EXPECT_CALL(*mockCryptIface, cryptFormat(_, _, _, _, _, _, _, _))
201 .WillOnce(Return(-1));
202
John Wedig972c3fa2021-12-29 17:30:41 -0800203 EXPECT_THROW(esObject->formatLuks(password, Volume::FilesystemType::ext4),
204 InternalFailure);
John Wedigb810c922021-11-17 16:38:03 -0800205 EXPECT_FALSE(esObject->isLocked());
206}
207
208/* Test case where we fail to set the password for the LUKS device. */
Ed Tanous82897c32022-02-21 14:11:59 -0800209TEST_F(EStoragedTest, AddKeyslotFail)
John Wedigb810c922021-11-17 16:38:03 -0800210{
John Wedigb810c922021-11-17 16:38:03 -0800211 EXPECT_CALL(*mockCryptIface, cryptFormat(_, _, _, _, _, _, _, _)).Times(1);
212
213 EXPECT_CALL(*mockCryptIface, cryptKeyslotAddByVolumeKey(_, _, _, _, _, _))
214 .WillOnce(Return(-1));
215
John Wedig972c3fa2021-12-29 17:30:41 -0800216 EXPECT_THROW(esObject->formatLuks(password, Volume::FilesystemType::ext4),
217 InternalFailure);
John Wedigb810c922021-11-17 16:38:03 -0800218 EXPECT_TRUE(esObject->isLocked());
219}
220
221/* Test case where we fail to load the LUKS header. */
Ed Tanous82897c32022-02-21 14:11:59 -0800222TEST_F(EStoragedTest, LoadLuksHeaderFail)
John Wedigb810c922021-11-17 16:38:03 -0800223{
John Wedigb810c922021-11-17 16:38:03 -0800224 EXPECT_CALL(*mockCryptIface, cryptFormat(_, _, _, _, _, _, _, _)).Times(1);
225
226 EXPECT_CALL(*mockCryptIface, cryptKeyslotAddByVolumeKey(_, _, _, _, _, _))
227 .Times(1);
228
229 EXPECT_CALL(*mockCryptIface, cryptLoad(_, _, _)).WillOnce(Return(-1));
230
John Wedig972c3fa2021-12-29 17:30:41 -0800231 EXPECT_THROW(esObject->formatLuks(password, Volume::FilesystemType::ext4),
232 InternalFailure);
John Wedigb810c922021-11-17 16:38:03 -0800233 EXPECT_TRUE(esObject->isLocked());
234}
235
236/* Test case where we fail to activate the LUKS device. */
Ed Tanous82897c32022-02-21 14:11:59 -0800237TEST_F(EStoragedTest, ActivateFail)
John Wedigb810c922021-11-17 16:38:03 -0800238{
John Wedigb810c922021-11-17 16:38:03 -0800239 EXPECT_CALL(*mockCryptIface, cryptFormat(_, _, _, _, _, _, _, _)).Times(1);
240
241 EXPECT_CALL(*mockCryptIface, cryptKeyslotAddByVolumeKey(_, _, _, _, _, _))
242 .Times(1);
243
244 EXPECT_CALL(*mockCryptIface, cryptLoad(_, _, _)).Times(1);
245
246 EXPECT_CALL(*mockCryptIface, cryptActivateByPassphrase(_, _, _, _, _, _))
247 .WillOnce(Return(-1));
248
John Wedig972c3fa2021-12-29 17:30:41 -0800249 EXPECT_THROW(esObject->formatLuks(password, Volume::FilesystemType::ext4),
250 InternalFailure);
John Wedigb810c922021-11-17 16:38:03 -0800251 EXPECT_TRUE(esObject->isLocked());
252}
253
254/* Test case where we fail to create the filesystem. */
Ed Tanous82897c32022-02-21 14:11:59 -0800255TEST_F(EStoragedTest, CreateFilesystemFail)
John Wedigb810c922021-11-17 16:38:03 -0800256{
John Wedigb810c922021-11-17 16:38:03 -0800257 EXPECT_CALL(*mockCryptIface, cryptFormat(_, _, _, _, _, _, _, _)).Times(1);
258
259 EXPECT_CALL(*mockCryptIface, cryptKeyslotAddByVolumeKey(_, _, _, _, _, _))
260 .Times(1);
261
262 EXPECT_CALL(*mockCryptIface, cryptLoad(_, _, _)).Times(1);
263
264 EXPECT_CALL(*mockCryptIface, cryptActivateByPassphrase(_, _, _, _, _, _))
265 .Times(1);
266
267 EXPECT_CALL(*mockFsIface, runMkfs(testLuksDevName)).WillOnce(Return(-1));
268
John Wedig972c3fa2021-12-29 17:30:41 -0800269 EXPECT_THROW(esObject->formatLuks(password, Volume::FilesystemType::ext4),
270 InternalFailure);
John Wedigb810c922021-11-17 16:38:03 -0800271 EXPECT_FALSE(esObject->isLocked());
272}
273
274/* Test case where we fail to create the mount point. */
Ed Tanous82897c32022-02-21 14:11:59 -0800275TEST_F(EStoragedTest, CreateMountPointFail)
John Wedigb810c922021-11-17 16:38:03 -0800276{
John Wedigb810c922021-11-17 16:38:03 -0800277 EXPECT_CALL(*mockCryptIface, cryptFormat(_, _, _, _, _, _, _, _)).Times(1);
278
279 EXPECT_CALL(*mockCryptIface, cryptKeyslotAddByVolumeKey(_, _, _, _, _, _))
280 .Times(1);
281
282 EXPECT_CALL(*mockCryptIface, cryptLoad(_, _, _)).Times(1);
283
284 EXPECT_CALL(*mockCryptIface, cryptActivateByPassphrase(_, _, _, _, _, _))
285 .Times(1);
286
287 EXPECT_CALL(*mockFsIface, runMkfs(testLuksDevName)).WillOnce(Return(0));
288
John Wedigb17f8252022-01-12 14:24:26 -0800289 EXPECT_CALL(*mockFsIface, directoryExists(path(esObject->getMountPoint())))
290 .WillOnce(Return(false));
291
John Wedigb810c922021-11-17 16:38:03 -0800292 EXPECT_CALL(*mockFsIface, createDirectory(path(esObject->getMountPoint())))
293 .WillOnce(Return(false));
294
John Wedig972c3fa2021-12-29 17:30:41 -0800295 EXPECT_THROW(esObject->formatLuks(password, Volume::FilesystemType::ext4),
296 InternalFailure);
John Wedigb810c922021-11-17 16:38:03 -0800297 EXPECT_FALSE(esObject->isLocked());
298}
299
300/* Test case where we fail to mount the filesystem. */
Ed Tanous82897c32022-02-21 14:11:59 -0800301TEST_F(EStoragedTest, MountFail)
John Wedigb810c922021-11-17 16:38:03 -0800302{
John Wedigb810c922021-11-17 16:38:03 -0800303 EXPECT_CALL(*mockCryptIface, cryptFormat(_, _, _, _, _, _, _, _)).Times(1);
304
305 EXPECT_CALL(*mockCryptIface, cryptKeyslotAddByVolumeKey(_, _, _, _, _, _))
306 .Times(1);
307
308 EXPECT_CALL(*mockCryptIface, cryptLoad(_, _, _)).Times(1);
309
310 EXPECT_CALL(*mockCryptIface, cryptActivateByPassphrase(_, _, _, _, _, _))
311 .Times(1);
312
313 EXPECT_CALL(*mockFsIface, runMkfs(testLuksDevName)).WillOnce(Return(0));
314
John Wedigb17f8252022-01-12 14:24:26 -0800315 EXPECT_CALL(*mockFsIface, directoryExists(path(esObject->getMountPoint())))
316 .WillOnce(Return(false));
317
John Wedigb810c922021-11-17 16:38:03 -0800318 EXPECT_CALL(*mockFsIface, createDirectory(path(esObject->getMountPoint())))
319 .WillOnce(Return(true));
320
321 EXPECT_CALL(*mockFsIface,
322 doMount(ContainsRegex("/dev/mapper/"),
323 StrEq(esObject->getMountPoint()), _, _, _))
324 .WillOnce(Return(-1));
325
326 EXPECT_CALL(*mockFsIface, removeDirectory(path(esObject->getMountPoint())))
327 .WillOnce(Return(true));
328
John Wedig972c3fa2021-12-29 17:30:41 -0800329 EXPECT_THROW(esObject->formatLuks(password, Volume::FilesystemType::ext4),
330 InternalFailure);
John Wedigb810c922021-11-17 16:38:03 -0800331 EXPECT_FALSE(esObject->isLocked());
332}
333
334/* Test case where we fail to unmount the filesystem. */
Ed Tanous82897c32022-02-21 14:11:59 -0800335TEST_F(EStoragedTest, UnmountFail)
John Wedigb810c922021-11-17 16:38:03 -0800336{
John Wedigb810c922021-11-17 16:38:03 -0800337 EXPECT_CALL(*mockCryptIface, cryptFormat(_, _, _, _, _, _, _, _)).Times(1);
338
339 EXPECT_CALL(*mockCryptIface, cryptKeyslotAddByVolumeKey(_, _, _, _, _, _))
340 .Times(1);
341
342 EXPECT_CALL(*mockCryptIface, cryptLoad(_, _, _)).Times(1);
343
344 EXPECT_CALL(*mockCryptIface, cryptActivateByPassphrase(_, _, _, _, _, _))
345 .Times(1);
346
347 EXPECT_CALL(*mockFsIface, runMkfs(testLuksDevName)).WillOnce(Return(0));
348
John Wedigb17f8252022-01-12 14:24:26 -0800349 EXPECT_CALL(*mockFsIface, directoryExists(path(esObject->getMountPoint())))
350 .WillOnce(Return(false));
351
John Wedigb810c922021-11-17 16:38:03 -0800352 EXPECT_CALL(*mockFsIface, createDirectory(path(esObject->getMountPoint())))
353 .WillOnce(Return(true));
354
355 EXPECT_CALL(*mockFsIface,
356 doMount(ContainsRegex("/dev/mapper/"),
357 StrEq(esObject->getMountPoint()), _, _, _))
358 .WillOnce(Return(0));
359
360 EXPECT_CALL(*mockFsIface, doUnmount(StrEq(esObject->getMountPoint())))
361 .WillOnce(Return(-1));
362
John Wedig972c3fa2021-12-29 17:30:41 -0800363 esObject->formatLuks(password, Volume::FilesystemType::ext4);
John Wedigb810c922021-11-17 16:38:03 -0800364 EXPECT_FALSE(esObject->isLocked());
365
John Wedig972c3fa2021-12-29 17:30:41 -0800366 EXPECT_THROW(esObject->lock(), InternalFailure);
John Wedigb810c922021-11-17 16:38:03 -0800367 EXPECT_FALSE(esObject->isLocked());
368}
369
370/* Test case where we fail to remove the mount point. */
Ed Tanous82897c32022-02-21 14:11:59 -0800371TEST_F(EStoragedTest, RemoveMountPointFail)
John Wedigb810c922021-11-17 16:38:03 -0800372{
John Wedigb810c922021-11-17 16:38:03 -0800373 EXPECT_CALL(*mockCryptIface, cryptFormat(_, _, _, _, _, _, _, _)).Times(1);
374
375 EXPECT_CALL(*mockCryptIface, cryptKeyslotAddByVolumeKey(_, _, _, _, _, _))
376 .Times(1);
377
378 EXPECT_CALL(*mockCryptIface, cryptLoad(_, _, _)).Times(1);
379
380 EXPECT_CALL(*mockCryptIface, cryptActivateByPassphrase(_, _, _, _, _, _))
381 .Times(1);
382
383 EXPECT_CALL(*mockFsIface, runMkfs(testLuksDevName)).WillOnce(Return(0));
384
John Wedigb17f8252022-01-12 14:24:26 -0800385 EXPECT_CALL(*mockFsIface, directoryExists(path(esObject->getMountPoint())))
386 .WillOnce(Return(false));
387
John Wedigb810c922021-11-17 16:38:03 -0800388 EXPECT_CALL(*mockFsIface, createDirectory(path(esObject->getMountPoint())))
389 .WillOnce(Return(true));
390
391 EXPECT_CALL(*mockFsIface,
392 doMount(ContainsRegex("/dev/mapper/"),
393 StrEq(esObject->getMountPoint()), _, _, _))
394 .WillOnce(Return(0));
395
396 EXPECT_CALL(*mockFsIface, doUnmount(StrEq(esObject->getMountPoint())))
397 .WillOnce(Return(0));
398
399 EXPECT_CALL(*mockFsIface, removeDirectory(path(esObject->getMountPoint())))
400 .WillOnce(Return(false));
401
John Wedig972c3fa2021-12-29 17:30:41 -0800402 esObject->formatLuks(password, Volume::FilesystemType::ext4);
John Wedigb810c922021-11-17 16:38:03 -0800403 EXPECT_FALSE(esObject->isLocked());
404
405 /* This will fail to remove the mount point. */
John Wedig972c3fa2021-12-29 17:30:41 -0800406 EXPECT_THROW(esObject->lock(), InternalFailure);
John Wedigb810c922021-11-17 16:38:03 -0800407 EXPECT_FALSE(esObject->isLocked());
408}
409
410/* Test case where we fail to deactivate the LUKS device. */
Ed Tanous82897c32022-02-21 14:11:59 -0800411TEST_F(EStoragedTest, DeactivateFail)
John Wedigb810c922021-11-17 16:38:03 -0800412{
John Wedigb810c922021-11-17 16:38:03 -0800413 EXPECT_CALL(*mockCryptIface, cryptFormat(_, _, _, _, _, _, _, _)).Times(1);
414
415 EXPECT_CALL(*mockCryptIface, cryptKeyslotAddByVolumeKey(_, _, _, _, _, _))
416 .Times(1);
417
418 EXPECT_CALL(*mockCryptIface, cryptLoad(_, _, _)).Times(1);
419
420 EXPECT_CALL(*mockCryptIface, cryptActivateByPassphrase(_, _, _, _, _, _))
421 .Times(1);
422
423 EXPECT_CALL(*mockFsIface, runMkfs(testLuksDevName)).WillOnce(Return(0));
424
John Wedigb17f8252022-01-12 14:24:26 -0800425 EXPECT_CALL(*mockFsIface, directoryExists(path(esObject->getMountPoint())))
426 .WillOnce(Return(false));
427
John Wedigb810c922021-11-17 16:38:03 -0800428 EXPECT_CALL(*mockFsIface, createDirectory(path(esObject->getMountPoint())))
429 .WillOnce(Return(true));
430
431 EXPECT_CALL(*mockFsIface,
432 doMount(ContainsRegex("/dev/mapper/"),
433 StrEq(esObject->getMountPoint()), _, _, _))
434 .WillOnce(Return(0));
435
436 EXPECT_CALL(*mockFsIface, doUnmount(StrEq(esObject->getMountPoint())))
437 .WillOnce(Return(0));
438
439 EXPECT_CALL(*mockFsIface, removeDirectory(path(esObject->getMountPoint())))
440 .WillOnce(Return(true));
441
442 EXPECT_CALL(*mockCryptIface, cryptDeactivate(_, _)).WillOnce(Return(-1));
443
444 /* Format the encrypted device. */
John Wedig972c3fa2021-12-29 17:30:41 -0800445 esObject->formatLuks(password, Volume::FilesystemType::ext4);
John Wedigb810c922021-11-17 16:38:03 -0800446 EXPECT_FALSE(esObject->isLocked());
447
John Wedig972c3fa2021-12-29 17:30:41 -0800448 EXPECT_THROW(esObject->lock(), InternalFailure);
John Wedigb810c922021-11-17 16:38:03 -0800449 EXPECT_FALSE(esObject->isLocked());
450}
451
452} // namespace estoraged_test