1 #include "estoraged_test.hpp"
2 
3 #include "estoraged.hpp"
4 
5 #include <unistd.h>
6 
7 #include <sdbusplus/test/sdbus_mock.hpp>
8 #include <xyz/openbmc_project/Common/error.hpp>
9 #include <xyz/openbmc_project/Inventory/Item/Volume/client.hpp>
10 
11 #include <exception>
12 #include <filesystem>
13 #include <fstream>
14 #include <iterator>
15 #include <string>
16 #include <vector>
17 
18 #include <gmock/gmock.h>
19 #include <gtest/gtest.h>
20 
21 namespace estoraged_test
22 {
23 
24 using sdbusplus::xyz::openbmc_project::Common::Error::InternalFailure;
25 using sdbusplus::xyz::openbmc_project::Common::Error::ResourceNotFound;
26 using sdbusplus::xyz::openbmc_project::Inventory::Item::server::Volume;
27 using std::filesystem::path;
28 using ::testing::_;
29 using ::testing::ContainsRegex;
30 using ::testing::IsNull;
31 using ::testing::Return;
32 using ::testing::StrEq;
33 
34 /*
35  * This sdbus mock object gets used in the destructor of one of the parent
36  * classes for the MockeStoraged object, so this can't be part of the
37  * eStoragedTest class.
38  */
39 sdbusplus::SdBusMock sdbusMock;
40 
41 class EStoragedTest : public testing::Test
42 {
43   public:
44     const char* testFileName = "testfile";
45     const char* testLuksDevName = "testfile_luksDev";
46     uint64_t testSize = 24;
47     std::ofstream testFile;
48     std::unique_ptr<estoraged::EStoraged> esObject;
49     const char* testPath = "/test/openbmc_project/storage/test_dev";
50     const char* estoragedInterface =
51         "xyz.openbmc_project.Inventory.Item.Volume";
52     const char* driveInterface = "xyz.openbmc_project.Inventory.Item.Drive";
53     sdbusplus::bus::bus bus;
54     std::string passwordString;
55     std::vector<uint8_t> password;
56     MockCryptsetupInterface* mockCryptIface{};
57     MockFilesystemInterface* mockFsIface{};
58 
59     EStoragedTest() :
60         bus(sdbusplus::get_mocked_new(&sdbusMock)), passwordString("password"),
61         password(passwordString.begin(), passwordString.end())
62     {}
63 
64     void SetUp() override
65     {
66         /* Create an empty file that we'll pretend is a 'storage device'. */
67         testFile.open(testFileName,
68                       std::ios::out | std::ios::binary | std::ios::trunc);
69         testFile.close();
70         if (testFile.fail())
71         {
72             throw std::runtime_error("Failed to open test file");
73         }
74 
75         EXPECT_CALL(sdbusMock,
76                     sd_bus_add_object_vtable(IsNull(), _, StrEq(testPath),
77                                              StrEq(estoragedInterface), _, _))
78             .WillRepeatedly(Return(0));
79 
80         EXPECT_CALL(sdbusMock,
81                     sd_bus_add_object_vtable(IsNull(), _, StrEq(testPath),
82                                              StrEq(driveInterface), _, _))
83             .WillRepeatedly(Return(0));
84 
85         EXPECT_CALL(sdbusMock,
86                     sd_bus_emit_object_added(IsNull(), StrEq(testPath)))
87             .WillRepeatedly(Return(0));
88 
89         EXPECT_CALL(sdbusMock,
90                     sd_bus_emit_object_removed(IsNull(), StrEq(testPath)))
91             .WillRepeatedly(Return(0));
92 
93         std::unique_ptr<MockCryptsetupInterface> cryptIface =
94             std::make_unique<MockCryptsetupInterface>();
95         mockCryptIface = cryptIface.get();
96         std::unique_ptr<MockFilesystemInterface> fsIface =
97             std::make_unique<MockFilesystemInterface>();
98         mockFsIface = fsIface.get();
99 
100         esObject = std::make_unique<estoraged::EStoraged>(
101             bus, testPath, testFileName, testLuksDevName, testSize,
102             std::move(cryptIface), std::move(fsIface));
103     }
104 
105     void TearDown() override
106     {
107         EXPECT_EQ(0, unlink(testFileName));
108     }
109 };
110 
111 /* Test case to format and then lock the LUKS device. */
112 TEST_F(EStoragedTest, FormatPass)
113 {
114     EXPECT_CALL(sdbusMock,
115                 sd_bus_emit_properties_changed_strv(IsNull(), StrEq(testPath),
116                                                     StrEq(driveInterface), _))
117         .WillRepeatedly(Return(0));
118 
119     EXPECT_CALL(sdbusMock,
120                 sd_bus_emit_properties_changed_strv(
121                     IsNull(), StrEq(testPath), StrEq(estoragedInterface), _))
122         .WillRepeatedly(Return(0));
123 
124     EXPECT_CALL(*mockCryptIface, cryptFormat(_, _, _, _, _, _, _, _)).Times(1);
125 
126     EXPECT_CALL(*mockCryptIface, cryptKeyslotAddByVolumeKey(_, _, _, _, _, _))
127         .Times(1);
128 
129     EXPECT_CALL(*mockCryptIface, cryptLoad(_, _, _)).Times(1);
130 
131     EXPECT_CALL(*mockCryptIface, cryptActivateByPassphrase(_, _, _, _, _, _))
132         .Times(1);
133 
134     EXPECT_CALL(*mockFsIface, runMkfs(testLuksDevName)).WillOnce(Return(0));
135 
136     EXPECT_CALL(*mockFsIface, directoryExists(path(esObject->getMountPoint())))
137         .WillOnce(Return(false));
138 
139     EXPECT_CALL(*mockFsIface, createDirectory(path(esObject->getMountPoint())))
140         .WillOnce(Return(true));
141 
142     EXPECT_CALL(*mockFsIface,
143                 doMount(ContainsRegex("/dev/mapper/"),
144                         StrEq(esObject->getMountPoint()), _, _, _))
145         .WillOnce(Return(0));
146 
147     EXPECT_CALL(*mockFsIface, doUnmount(StrEq(esObject->getMountPoint())))
148         .WillOnce(Return(0));
149 
150     EXPECT_CALL(*mockFsIface, removeDirectory(path(esObject->getMountPoint())))
151         .WillOnce(Return(true));
152 
153     EXPECT_CALL(*mockCryptIface, cryptDeactivate(_, _)).Times(1);
154 
155     /* Format the encrypted device. */
156     esObject->formatLuks(password, Volume::FilesystemType::ext4);
157     EXPECT_FALSE(esObject->isLocked());
158 
159     esObject->lock();
160     EXPECT_TRUE(esObject->isLocked());
161 }
162 
163 /*
164  * Test case where the mount point directory already exists, so it shouldn't
165  * try to create it.
166  */
167 TEST_F(EStoragedTest, MountPointExistsPass)
168 {
169     EXPECT_CALL(sdbusMock,
170                 sd_bus_emit_properties_changed_strv(IsNull(), StrEq(testPath),
171                                                     StrEq(driveInterface), _))
172         .WillRepeatedly(Return(0));
173 
174     EXPECT_CALL(sdbusMock,
175                 sd_bus_emit_properties_changed_strv(
176                     IsNull(), StrEq(testPath), StrEq(estoragedInterface), _))
177         .WillRepeatedly(Return(0));
178 
179     EXPECT_CALL(*mockCryptIface, cryptFormat(_, _, _, _, _, _, _, _)).Times(1);
180 
181     EXPECT_CALL(*mockCryptIface, cryptKeyslotAddByVolumeKey(_, _, _, _, _, _))
182         .Times(1);
183 
184     EXPECT_CALL(*mockCryptIface, cryptLoad(_, _, _)).Times(1);
185 
186     EXPECT_CALL(*mockCryptIface, cryptActivateByPassphrase(_, _, _, _, _, _))
187         .Times(1);
188 
189     EXPECT_CALL(*mockFsIface, runMkfs(testLuksDevName)).WillOnce(Return(0));
190 
191     EXPECT_CALL(*mockFsIface, directoryExists(path(esObject->getMountPoint())))
192         .WillOnce(Return(true));
193 
194     EXPECT_CALL(*mockFsIface, createDirectory(path(esObject->getMountPoint())))
195         .Times(0);
196 
197     EXPECT_CALL(*mockFsIface,
198                 doMount(ContainsRegex("/dev/mapper/"),
199                         StrEq(esObject->getMountPoint()), _, _, _))
200         .WillOnce(Return(0));
201 
202     EXPECT_CALL(*mockFsIface, doUnmount(StrEq(esObject->getMountPoint())))
203         .WillOnce(Return(0));
204 
205     EXPECT_CALL(*mockFsIface, removeDirectory(path(esObject->getMountPoint())))
206         .WillOnce(Return(true));
207 
208     EXPECT_CALL(*mockCryptIface, cryptDeactivate(_, _)).Times(1);
209 
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 
218 /* Test case where the device/file doesn't exist. */
219 TEST_F(EStoragedTest, FormatNoDeviceFail)
220 {
221     /* Delete the test file. */
222     EXPECT_EQ(0, unlink(testFileName));
223 
224     EXPECT_THROW(esObject->formatLuks(password, Volume::FilesystemType::ext4),
225                  ResourceNotFound);
226     EXPECT_FALSE(esObject->isLocked());
227 
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. */
235 TEST_F(EStoragedTest, FormatFail)
236 {
237     EXPECT_CALL(*mockCryptIface, cryptFormat(_, _, _, _, _, _, _, _))
238         .WillOnce(Return(-1));
239 
240     EXPECT_THROW(esObject->formatLuks(password, Volume::FilesystemType::ext4),
241                  InternalFailure);
242     EXPECT_FALSE(esObject->isLocked());
243 }
244 
245 /* Test case where we fail to set the password for the LUKS device. */
246 TEST_F(EStoragedTest, AddKeyslotFail)
247 {
248     EXPECT_CALL(sdbusMock,
249                 sd_bus_emit_properties_changed_strv(IsNull(), StrEq(testPath),
250                                                     StrEq(driveInterface), _))
251         .WillRepeatedly(Return(0));
252 
253     EXPECT_CALL(sdbusMock,
254                 sd_bus_emit_properties_changed_strv(
255                     IsNull(), StrEq(testPath), StrEq(estoragedInterface), _))
256         .WillRepeatedly(Return(0));
257 
258     EXPECT_CALL(*mockCryptIface, cryptFormat(_, _, _, _, _, _, _, _)).Times(1);
259 
260     EXPECT_CALL(*mockCryptIface, cryptKeyslotAddByVolumeKey(_, _, _, _, _, _))
261         .WillOnce(Return(-1));
262 
263     EXPECT_THROW(esObject->formatLuks(password, Volume::FilesystemType::ext4),
264                  InternalFailure);
265     EXPECT_TRUE(esObject->isLocked());
266 }
267 
268 /* Test case where we fail to load the LUKS header. */
269 TEST_F(EStoragedTest, LoadLuksHeaderFail)
270 {
271     EXPECT_CALL(sdbusMock,
272                 sd_bus_emit_properties_changed_strv(IsNull(), StrEq(testPath),
273                                                     StrEq(driveInterface), _))
274         .WillRepeatedly(Return(0));
275 
276     EXPECT_CALL(sdbusMock,
277                 sd_bus_emit_properties_changed_strv(
278                     IsNull(), StrEq(testPath), StrEq(estoragedInterface), _))
279         .WillRepeatedly(Return(0));
280 
281     EXPECT_CALL(*mockCryptIface, cryptFormat(_, _, _, _, _, _, _, _)).Times(1);
282 
283     EXPECT_CALL(*mockCryptIface, cryptKeyslotAddByVolumeKey(_, _, _, _, _, _))
284         .Times(1);
285 
286     EXPECT_CALL(*mockCryptIface, cryptLoad(_, _, _)).WillOnce(Return(-1));
287 
288     EXPECT_THROW(esObject->formatLuks(password, Volume::FilesystemType::ext4),
289                  InternalFailure);
290     EXPECT_TRUE(esObject->isLocked());
291 }
292 
293 /* Test case where we fail to activate the LUKS device. */
294 TEST_F(EStoragedTest, ActivateFail)
295 {
296     EXPECT_CALL(sdbusMock,
297                 sd_bus_emit_properties_changed_strv(IsNull(), StrEq(testPath),
298                                                     StrEq(driveInterface), _))
299         .WillRepeatedly(Return(0));
300 
301     EXPECT_CALL(sdbusMock,
302                 sd_bus_emit_properties_changed_strv(
303                     IsNull(), StrEq(testPath), StrEq(estoragedInterface), _))
304         .WillRepeatedly(Return(0));
305 
306     EXPECT_CALL(*mockCryptIface, cryptFormat(_, _, _, _, _, _, _, _)).Times(1);
307 
308     EXPECT_CALL(*mockCryptIface, cryptKeyslotAddByVolumeKey(_, _, _, _, _, _))
309         .Times(1);
310 
311     EXPECT_CALL(*mockCryptIface, cryptLoad(_, _, _)).Times(1);
312 
313     EXPECT_CALL(*mockCryptIface, cryptActivateByPassphrase(_, _, _, _, _, _))
314         .WillOnce(Return(-1));
315 
316     EXPECT_THROW(esObject->formatLuks(password, Volume::FilesystemType::ext4),
317                  InternalFailure);
318     EXPECT_TRUE(esObject->isLocked());
319 }
320 
321 /* Test case where we fail to create the filesystem. */
322 TEST_F(EStoragedTest, CreateFilesystemFail)
323 {
324     EXPECT_CALL(sdbusMock,
325                 sd_bus_emit_properties_changed_strv(IsNull(), StrEq(testPath),
326                                                     StrEq(driveInterface), _))
327         .WillRepeatedly(Return(0));
328 
329     EXPECT_CALL(sdbusMock,
330                 sd_bus_emit_properties_changed_strv(
331                     IsNull(), StrEq(testPath), StrEq(estoragedInterface), _))
332         .WillRepeatedly(Return(0));
333 
334     EXPECT_CALL(*mockCryptIface, cryptFormat(_, _, _, _, _, _, _, _)).Times(1);
335 
336     EXPECT_CALL(*mockCryptIface, cryptKeyslotAddByVolumeKey(_, _, _, _, _, _))
337         .Times(1);
338 
339     EXPECT_CALL(*mockCryptIface, cryptLoad(_, _, _)).Times(1);
340 
341     EXPECT_CALL(*mockCryptIface, cryptActivateByPassphrase(_, _, _, _, _, _))
342         .Times(1);
343 
344     EXPECT_CALL(*mockFsIface, runMkfs(testLuksDevName)).WillOnce(Return(-1));
345 
346     EXPECT_THROW(esObject->formatLuks(password, Volume::FilesystemType::ext4),
347                  InternalFailure);
348     EXPECT_FALSE(esObject->isLocked());
349 }
350 
351 /* Test case where we fail to create the mount point. */
352 TEST_F(EStoragedTest, CreateMountPointFail)
353 {
354     EXPECT_CALL(sdbusMock,
355                 sd_bus_emit_properties_changed_strv(IsNull(), StrEq(testPath),
356                                                     StrEq(driveInterface), _))
357         .WillRepeatedly(Return(0));
358 
359     EXPECT_CALL(sdbusMock,
360                 sd_bus_emit_properties_changed_strv(
361                     IsNull(), StrEq(testPath), StrEq(estoragedInterface), _))
362         .WillRepeatedly(Return(0));
363 
364     EXPECT_CALL(*mockCryptIface, cryptFormat(_, _, _, _, _, _, _, _)).Times(1);
365 
366     EXPECT_CALL(*mockCryptIface, cryptKeyslotAddByVolumeKey(_, _, _, _, _, _))
367         .Times(1);
368 
369     EXPECT_CALL(*mockCryptIface, cryptLoad(_, _, _)).Times(1);
370 
371     EXPECT_CALL(*mockCryptIface, cryptActivateByPassphrase(_, _, _, _, _, _))
372         .Times(1);
373 
374     EXPECT_CALL(*mockFsIface, runMkfs(testLuksDevName)).WillOnce(Return(0));
375 
376     EXPECT_CALL(*mockFsIface, directoryExists(path(esObject->getMountPoint())))
377         .WillOnce(Return(false));
378 
379     EXPECT_CALL(*mockFsIface, createDirectory(path(esObject->getMountPoint())))
380         .WillOnce(Return(false));
381 
382     EXPECT_THROW(esObject->formatLuks(password, Volume::FilesystemType::ext4),
383                  InternalFailure);
384     EXPECT_FALSE(esObject->isLocked());
385 }
386 
387 /* Test case where we fail to mount the filesystem. */
388 TEST_F(EStoragedTest, MountFail)
389 {
390     EXPECT_CALL(sdbusMock,
391                 sd_bus_emit_properties_changed_strv(IsNull(), StrEq(testPath),
392                                                     StrEq(driveInterface), _))
393         .WillRepeatedly(Return(0));
394 
395     EXPECT_CALL(sdbusMock,
396                 sd_bus_emit_properties_changed_strv(
397                     IsNull(), StrEq(testPath), StrEq(estoragedInterface), _))
398         .WillRepeatedly(Return(0));
399 
400     EXPECT_CALL(*mockCryptIface, cryptFormat(_, _, _, _, _, _, _, _)).Times(1);
401 
402     EXPECT_CALL(*mockCryptIface, cryptKeyslotAddByVolumeKey(_, _, _, _, _, _))
403         .Times(1);
404 
405     EXPECT_CALL(*mockCryptIface, cryptLoad(_, _, _)).Times(1);
406 
407     EXPECT_CALL(*mockCryptIface, cryptActivateByPassphrase(_, _, _, _, _, _))
408         .Times(1);
409 
410     EXPECT_CALL(*mockFsIface, runMkfs(testLuksDevName)).WillOnce(Return(0));
411 
412     EXPECT_CALL(*mockFsIface, directoryExists(path(esObject->getMountPoint())))
413         .WillOnce(Return(false));
414 
415     EXPECT_CALL(*mockFsIface, createDirectory(path(esObject->getMountPoint())))
416         .WillOnce(Return(true));
417 
418     EXPECT_CALL(*mockFsIface,
419                 doMount(ContainsRegex("/dev/mapper/"),
420                         StrEq(esObject->getMountPoint()), _, _, _))
421         .WillOnce(Return(-1));
422 
423     EXPECT_CALL(*mockFsIface, removeDirectory(path(esObject->getMountPoint())))
424         .WillOnce(Return(true));
425 
426     EXPECT_THROW(esObject->formatLuks(password, Volume::FilesystemType::ext4),
427                  InternalFailure);
428     EXPECT_FALSE(esObject->isLocked());
429 }
430 
431 /* Test case where we fail to unmount the filesystem. */
432 TEST_F(EStoragedTest, UnmountFail)
433 {
434     EXPECT_CALL(sdbusMock,
435                 sd_bus_emit_properties_changed_strv(IsNull(), StrEq(testPath),
436                                                     StrEq(driveInterface), _))
437         .WillRepeatedly(Return(0));
438 
439     EXPECT_CALL(sdbusMock,
440                 sd_bus_emit_properties_changed_strv(
441                     IsNull(), StrEq(testPath), StrEq(estoragedInterface), _))
442         .WillRepeatedly(Return(0));
443 
444     EXPECT_CALL(*mockCryptIface, cryptFormat(_, _, _, _, _, _, _, _)).Times(1);
445 
446     EXPECT_CALL(*mockCryptIface, cryptKeyslotAddByVolumeKey(_, _, _, _, _, _))
447         .Times(1);
448 
449     EXPECT_CALL(*mockCryptIface, cryptLoad(_, _, _)).Times(1);
450 
451     EXPECT_CALL(*mockCryptIface, cryptActivateByPassphrase(_, _, _, _, _, _))
452         .Times(1);
453 
454     EXPECT_CALL(*mockFsIface, runMkfs(testLuksDevName)).WillOnce(Return(0));
455 
456     EXPECT_CALL(*mockFsIface, directoryExists(path(esObject->getMountPoint())))
457         .WillOnce(Return(false));
458 
459     EXPECT_CALL(*mockFsIface, createDirectory(path(esObject->getMountPoint())))
460         .WillOnce(Return(true));
461 
462     EXPECT_CALL(*mockFsIface,
463                 doMount(ContainsRegex("/dev/mapper/"),
464                         StrEq(esObject->getMountPoint()), _, _, _))
465         .WillOnce(Return(0));
466 
467     EXPECT_CALL(*mockFsIface, doUnmount(StrEq(esObject->getMountPoint())))
468         .WillOnce(Return(-1));
469 
470     esObject->formatLuks(password, Volume::FilesystemType::ext4);
471     EXPECT_FALSE(esObject->isLocked());
472 
473     EXPECT_THROW(esObject->lock(), InternalFailure);
474     EXPECT_FALSE(esObject->isLocked());
475 }
476 
477 /* Test case where we fail to remove the mount point. */
478 TEST_F(EStoragedTest, RemoveMountPointFail)
479 {
480     EXPECT_CALL(sdbusMock,
481                 sd_bus_emit_properties_changed_strv(IsNull(), StrEq(testPath),
482                                                     StrEq(driveInterface), _))
483         .WillRepeatedly(Return(0));
484 
485     EXPECT_CALL(sdbusMock,
486                 sd_bus_emit_properties_changed_strv(
487                     IsNull(), StrEq(testPath), StrEq(estoragedInterface), _))
488         .WillRepeatedly(Return(0));
489 
490     EXPECT_CALL(*mockCryptIface, cryptFormat(_, _, _, _, _, _, _, _)).Times(1);
491 
492     EXPECT_CALL(*mockCryptIface, cryptKeyslotAddByVolumeKey(_, _, _, _, _, _))
493         .Times(1);
494 
495     EXPECT_CALL(*mockCryptIface, cryptLoad(_, _, _)).Times(1);
496 
497     EXPECT_CALL(*mockCryptIface, cryptActivateByPassphrase(_, _, _, _, _, _))
498         .Times(1);
499 
500     EXPECT_CALL(*mockFsIface, runMkfs(testLuksDevName)).WillOnce(Return(0));
501 
502     EXPECT_CALL(*mockFsIface, directoryExists(path(esObject->getMountPoint())))
503         .WillOnce(Return(false));
504 
505     EXPECT_CALL(*mockFsIface, createDirectory(path(esObject->getMountPoint())))
506         .WillOnce(Return(true));
507 
508     EXPECT_CALL(*mockFsIface,
509                 doMount(ContainsRegex("/dev/mapper/"),
510                         StrEq(esObject->getMountPoint()), _, _, _))
511         .WillOnce(Return(0));
512 
513     EXPECT_CALL(*mockFsIface, doUnmount(StrEq(esObject->getMountPoint())))
514         .WillOnce(Return(0));
515 
516     EXPECT_CALL(*mockFsIface, removeDirectory(path(esObject->getMountPoint())))
517         .WillOnce(Return(false));
518 
519     esObject->formatLuks(password, Volume::FilesystemType::ext4);
520     EXPECT_FALSE(esObject->isLocked());
521 
522     /* This will fail to remove the mount point. */
523     EXPECT_THROW(esObject->lock(), InternalFailure);
524     EXPECT_FALSE(esObject->isLocked());
525 }
526 
527 /* Test case where we fail to deactivate the LUKS device. */
528 TEST_F(EStoragedTest, DeactivateFail)
529 {
530     EXPECT_CALL(sdbusMock,
531                 sd_bus_emit_properties_changed_strv(IsNull(), StrEq(testPath),
532                                                     StrEq(driveInterface), _))
533         .WillRepeatedly(Return(0));
534 
535     EXPECT_CALL(sdbusMock,
536                 sd_bus_emit_properties_changed_strv(
537                     IsNull(), StrEq(testPath), StrEq(estoragedInterface), _))
538         .WillRepeatedly(Return(0));
539 
540     EXPECT_CALL(*mockCryptIface, cryptFormat(_, _, _, _, _, _, _, _)).Times(1);
541 
542     EXPECT_CALL(*mockCryptIface, cryptKeyslotAddByVolumeKey(_, _, _, _, _, _))
543         .Times(1);
544 
545     EXPECT_CALL(*mockCryptIface, cryptLoad(_, _, _)).Times(1);
546 
547     EXPECT_CALL(*mockCryptIface, cryptActivateByPassphrase(_, _, _, _, _, _))
548         .Times(1);
549 
550     EXPECT_CALL(*mockFsIface, runMkfs(testLuksDevName)).WillOnce(Return(0));
551 
552     EXPECT_CALL(*mockFsIface, directoryExists(path(esObject->getMountPoint())))
553         .WillOnce(Return(false));
554 
555     EXPECT_CALL(*mockFsIface, createDirectory(path(esObject->getMountPoint())))
556         .WillOnce(Return(true));
557 
558     EXPECT_CALL(*mockFsIface,
559                 doMount(ContainsRegex("/dev/mapper/"),
560                         StrEq(esObject->getMountPoint()), _, _, _))
561         .WillOnce(Return(0));
562 
563     EXPECT_CALL(*mockFsIface, doUnmount(StrEq(esObject->getMountPoint())))
564         .WillOnce(Return(0));
565 
566     EXPECT_CALL(*mockFsIface, removeDirectory(path(esObject->getMountPoint())))
567         .WillOnce(Return(true));
568 
569     EXPECT_CALL(*mockCryptIface, cryptDeactivate(_, _)).WillOnce(Return(-1));
570 
571     /* Format the encrypted device. */
572     esObject->formatLuks(password, Volume::FilesystemType::ext4);
573     EXPECT_FALSE(esObject->isLocked());
574 
575     EXPECT_THROW(esObject->lock(), InternalFailure);
576     EXPECT_FALSE(esObject->isLocked());
577 }
578 
579 } // namespace estoraged_test
580