blob: 0bbb32b6bd5d84f46d067b5630ee2d207480c3f5 [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 Wedigb4838302022-07-22 13:51:16 -070045 const std::string testPartNumber = "12345678";
46 const std::string testSerialNumber = "ABCDEF1234";
John Wedigb810c922021-11-17 16:38:03 -080047 std::ofstream testFile;
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 Wedig6c0d8ce2022-04-22 14:00:43 -070086 *objectServer, testConfigPath, testFileName, testLuksDevName,
John Wedigb4838302022-07-22 13:51:16 -070087 testSize, testLifeTime, testPartNumber, testSerialNumber,
88 std::move(cryptIface), std::move(fsIface));
John Wedigb810c922021-11-17 16:38:03 -080089 }
90
91 void TearDown() override
92 {
93 EXPECT_EQ(0, unlink(testFileName));
94 }
95};
96
97/* Test case to format and then lock the LUKS device. */
Ed Tanous82897c32022-02-21 14:11:59 -080098TEST_F(EStoragedTest, FormatPass)
John Wedigb810c922021-11-17 16:38:03 -080099{
John Wedigb810c922021-11-17 16:38:03 -0800100 EXPECT_CALL(*mockCryptIface, cryptFormat(_, _, _, _, _, _, _, _)).Times(1);
101
102 EXPECT_CALL(*mockCryptIface, cryptKeyslotAddByVolumeKey(_, _, _, _, _, _))
103 .Times(1);
104
105 EXPECT_CALL(*mockCryptIface, cryptLoad(_, _, _)).Times(1);
106
107 EXPECT_CALL(*mockCryptIface, cryptActivateByPassphrase(_, _, _, _, _, _))
108 .Times(1);
109
110 EXPECT_CALL(*mockFsIface, runMkfs(testLuksDevName)).WillOnce(Return(0));
111
John Wedigb17f8252022-01-12 14:24:26 -0800112 EXPECT_CALL(*mockFsIface, directoryExists(path(esObject->getMountPoint())))
113 .WillOnce(Return(false));
114
John Wedigb810c922021-11-17 16:38:03 -0800115 EXPECT_CALL(*mockFsIface, createDirectory(path(esObject->getMountPoint())))
116 .WillOnce(Return(true));
117
118 EXPECT_CALL(*mockFsIface,
119 doMount(ContainsRegex("/dev/mapper/"),
120 StrEq(esObject->getMountPoint()), _, _, _))
121 .WillOnce(Return(0));
122
123 EXPECT_CALL(*mockFsIface, doUnmount(StrEq(esObject->getMountPoint())))
124 .WillOnce(Return(0));
125
126 EXPECT_CALL(*mockFsIface, removeDirectory(path(esObject->getMountPoint())))
127 .WillOnce(Return(true));
128
129 EXPECT_CALL(*mockCryptIface, cryptDeactivate(_, _)).Times(1);
130
131 /* Format the encrypted device. */
John Wedig972c3fa2021-12-29 17:30:41 -0800132 esObject->formatLuks(password, Volume::FilesystemType::ext4);
John Wedigb810c922021-11-17 16:38:03 -0800133 EXPECT_FALSE(esObject->isLocked());
134
John Wedig972c3fa2021-12-29 17:30:41 -0800135 esObject->lock();
John Wedigb810c922021-11-17 16:38:03 -0800136 EXPECT_TRUE(esObject->isLocked());
137}
138
John Wedigb17f8252022-01-12 14:24:26 -0800139/*
140 * Test case where the mount point directory already exists, so it shouldn't
141 * try to create it.
142 */
Ed Tanous82897c32022-02-21 14:11:59 -0800143TEST_F(EStoragedTest, MountPointExistsPass)
John Wedigb17f8252022-01-12 14:24:26 -0800144{
John Wedigb17f8252022-01-12 14:24:26 -0800145 EXPECT_CALL(*mockCryptIface, cryptFormat(_, _, _, _, _, _, _, _)).Times(1);
146
147 EXPECT_CALL(*mockCryptIface, cryptKeyslotAddByVolumeKey(_, _, _, _, _, _))
148 .Times(1);
149
150 EXPECT_CALL(*mockCryptIface, cryptLoad(_, _, _)).Times(1);
151
152 EXPECT_CALL(*mockCryptIface, cryptActivateByPassphrase(_, _, _, _, _, _))
153 .Times(1);
154
155 EXPECT_CALL(*mockFsIface, runMkfs(testLuksDevName)).WillOnce(Return(0));
156
157 EXPECT_CALL(*mockFsIface, directoryExists(path(esObject->getMountPoint())))
158 .WillOnce(Return(true));
159
160 EXPECT_CALL(*mockFsIface, createDirectory(path(esObject->getMountPoint())))
161 .Times(0);
162
163 EXPECT_CALL(*mockFsIface,
164 doMount(ContainsRegex("/dev/mapper/"),
165 StrEq(esObject->getMountPoint()), _, _, _))
166 .WillOnce(Return(0));
167
168 EXPECT_CALL(*mockFsIface, doUnmount(StrEq(esObject->getMountPoint())))
169 .WillOnce(Return(0));
170
171 EXPECT_CALL(*mockFsIface, removeDirectory(path(esObject->getMountPoint())))
172 .WillOnce(Return(true));
173
174 EXPECT_CALL(*mockCryptIface, cryptDeactivate(_, _)).Times(1);
175
176 /* Format the encrypted device. */
177 esObject->formatLuks(password, Volume::FilesystemType::ext4);
178 EXPECT_FALSE(esObject->isLocked());
179
180 esObject->lock();
181 EXPECT_TRUE(esObject->isLocked());
182}
183
John Wedigb810c922021-11-17 16:38:03 -0800184/* Test case where the device/file doesn't exist. */
Ed Tanous82897c32022-02-21 14:11:59 -0800185TEST_F(EStoragedTest, FormatNoDeviceFail)
John Wedigb810c922021-11-17 16:38:03 -0800186{
187 /* Delete the test file. */
188 EXPECT_EQ(0, unlink(testFileName));
189
John Wedig972c3fa2021-12-29 17:30:41 -0800190 EXPECT_THROW(esObject->formatLuks(password, Volume::FilesystemType::ext4),
191 ResourceNotFound);
John Wedigb810c922021-11-17 16:38:03 -0800192 EXPECT_FALSE(esObject->isLocked());
193
194 /* Create the test file again, so that the TearDown function works. */
195 testFile.open(testFileName,
196 std::ios::out | std::ios::binary | std::ios::trunc);
197 testFile.close();
198}
199
200/* Test case where we fail to format the LUKS device. */
Ed Tanous82897c32022-02-21 14:11:59 -0800201TEST_F(EStoragedTest, FormatFail)
John Wedigb810c922021-11-17 16:38:03 -0800202{
203 EXPECT_CALL(*mockCryptIface, cryptFormat(_, _, _, _, _, _, _, _))
204 .WillOnce(Return(-1));
205
John Wedig972c3fa2021-12-29 17:30:41 -0800206 EXPECT_THROW(esObject->formatLuks(password, Volume::FilesystemType::ext4),
207 InternalFailure);
John Wedigb810c922021-11-17 16:38:03 -0800208 EXPECT_FALSE(esObject->isLocked());
209}
210
211/* Test case where we fail to set the password for the LUKS device. */
Ed Tanous82897c32022-02-21 14:11:59 -0800212TEST_F(EStoragedTest, AddKeyslotFail)
John Wedigb810c922021-11-17 16:38:03 -0800213{
John Wedigb810c922021-11-17 16:38:03 -0800214 EXPECT_CALL(*mockCryptIface, cryptFormat(_, _, _, _, _, _, _, _)).Times(1);
215
216 EXPECT_CALL(*mockCryptIface, cryptKeyslotAddByVolumeKey(_, _, _, _, _, _))
217 .WillOnce(Return(-1));
218
John Wedig972c3fa2021-12-29 17:30:41 -0800219 EXPECT_THROW(esObject->formatLuks(password, Volume::FilesystemType::ext4),
220 InternalFailure);
John Wedigb810c922021-11-17 16:38:03 -0800221 EXPECT_TRUE(esObject->isLocked());
222}
223
224/* Test case where we fail to load the LUKS header. */
Ed Tanous82897c32022-02-21 14:11:59 -0800225TEST_F(EStoragedTest, LoadLuksHeaderFail)
John Wedigb810c922021-11-17 16:38:03 -0800226{
John Wedigb810c922021-11-17 16:38:03 -0800227 EXPECT_CALL(*mockCryptIface, cryptFormat(_, _, _, _, _, _, _, _)).Times(1);
228
229 EXPECT_CALL(*mockCryptIface, cryptKeyslotAddByVolumeKey(_, _, _, _, _, _))
230 .Times(1);
231
232 EXPECT_CALL(*mockCryptIface, cryptLoad(_, _, _)).WillOnce(Return(-1));
233
John Wedig972c3fa2021-12-29 17:30:41 -0800234 EXPECT_THROW(esObject->formatLuks(password, Volume::FilesystemType::ext4),
235 InternalFailure);
John Wedigb810c922021-11-17 16:38:03 -0800236 EXPECT_TRUE(esObject->isLocked());
237}
238
239/* Test case where we fail to activate the LUKS device. */
Ed Tanous82897c32022-02-21 14:11:59 -0800240TEST_F(EStoragedTest, ActivateFail)
John Wedigb810c922021-11-17 16:38:03 -0800241{
John Wedigb810c922021-11-17 16:38:03 -0800242 EXPECT_CALL(*mockCryptIface, cryptFormat(_, _, _, _, _, _, _, _)).Times(1);
243
244 EXPECT_CALL(*mockCryptIface, cryptKeyslotAddByVolumeKey(_, _, _, _, _, _))
245 .Times(1);
246
247 EXPECT_CALL(*mockCryptIface, cryptLoad(_, _, _)).Times(1);
248
249 EXPECT_CALL(*mockCryptIface, cryptActivateByPassphrase(_, _, _, _, _, _))
250 .WillOnce(Return(-1));
251
John Wedig972c3fa2021-12-29 17:30:41 -0800252 EXPECT_THROW(esObject->formatLuks(password, Volume::FilesystemType::ext4),
253 InternalFailure);
John Wedigb810c922021-11-17 16:38:03 -0800254 EXPECT_TRUE(esObject->isLocked());
255}
256
257/* Test case where we fail to create the filesystem. */
Ed Tanous82897c32022-02-21 14:11:59 -0800258TEST_F(EStoragedTest, CreateFilesystemFail)
John Wedigb810c922021-11-17 16:38:03 -0800259{
John Wedigb810c922021-11-17 16:38:03 -0800260 EXPECT_CALL(*mockCryptIface, cryptFormat(_, _, _, _, _, _, _, _)).Times(1);
261
262 EXPECT_CALL(*mockCryptIface, cryptKeyslotAddByVolumeKey(_, _, _, _, _, _))
263 .Times(1);
264
265 EXPECT_CALL(*mockCryptIface, cryptLoad(_, _, _)).Times(1);
266
267 EXPECT_CALL(*mockCryptIface, cryptActivateByPassphrase(_, _, _, _, _, _))
268 .Times(1);
269
270 EXPECT_CALL(*mockFsIface, runMkfs(testLuksDevName)).WillOnce(Return(-1));
271
John Wedig972c3fa2021-12-29 17:30:41 -0800272 EXPECT_THROW(esObject->formatLuks(password, Volume::FilesystemType::ext4),
273 InternalFailure);
John Wedigb810c922021-11-17 16:38:03 -0800274 EXPECT_FALSE(esObject->isLocked());
275}
276
277/* Test case where we fail to create the mount point. */
Ed Tanous82897c32022-02-21 14:11:59 -0800278TEST_F(EStoragedTest, CreateMountPointFail)
John Wedigb810c922021-11-17 16:38:03 -0800279{
John Wedigb810c922021-11-17 16:38:03 -0800280 EXPECT_CALL(*mockCryptIface, cryptFormat(_, _, _, _, _, _, _, _)).Times(1);
281
282 EXPECT_CALL(*mockCryptIface, cryptKeyslotAddByVolumeKey(_, _, _, _, _, _))
283 .Times(1);
284
285 EXPECT_CALL(*mockCryptIface, cryptLoad(_, _, _)).Times(1);
286
287 EXPECT_CALL(*mockCryptIface, cryptActivateByPassphrase(_, _, _, _, _, _))
288 .Times(1);
289
290 EXPECT_CALL(*mockFsIface, runMkfs(testLuksDevName)).WillOnce(Return(0));
291
John Wedigb17f8252022-01-12 14:24:26 -0800292 EXPECT_CALL(*mockFsIface, directoryExists(path(esObject->getMountPoint())))
293 .WillOnce(Return(false));
294
John Wedigb810c922021-11-17 16:38:03 -0800295 EXPECT_CALL(*mockFsIface, createDirectory(path(esObject->getMountPoint())))
296 .WillOnce(Return(false));
297
John Wedig972c3fa2021-12-29 17:30:41 -0800298 EXPECT_THROW(esObject->formatLuks(password, Volume::FilesystemType::ext4),
299 InternalFailure);
John Wedigb810c922021-11-17 16:38:03 -0800300 EXPECT_FALSE(esObject->isLocked());
301}
302
303/* Test case where we fail to mount the filesystem. */
Ed Tanous82897c32022-02-21 14:11:59 -0800304TEST_F(EStoragedTest, MountFail)
John Wedigb810c922021-11-17 16:38:03 -0800305{
John Wedigb810c922021-11-17 16:38:03 -0800306 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 .Times(1);
315
316 EXPECT_CALL(*mockFsIface, runMkfs(testLuksDevName)).WillOnce(Return(0));
317
John Wedigb17f8252022-01-12 14:24:26 -0800318 EXPECT_CALL(*mockFsIface, directoryExists(path(esObject->getMountPoint())))
319 .WillOnce(Return(false));
320
John Wedigb810c922021-11-17 16:38:03 -0800321 EXPECT_CALL(*mockFsIface, createDirectory(path(esObject->getMountPoint())))
322 .WillOnce(Return(true));
323
324 EXPECT_CALL(*mockFsIface,
325 doMount(ContainsRegex("/dev/mapper/"),
326 StrEq(esObject->getMountPoint()), _, _, _))
327 .WillOnce(Return(-1));
328
329 EXPECT_CALL(*mockFsIface, removeDirectory(path(esObject->getMountPoint())))
330 .WillOnce(Return(true));
331
John Wedig972c3fa2021-12-29 17:30:41 -0800332 EXPECT_THROW(esObject->formatLuks(password, Volume::FilesystemType::ext4),
333 InternalFailure);
John Wedigb810c922021-11-17 16:38:03 -0800334 EXPECT_FALSE(esObject->isLocked());
335}
336
337/* Test case where we fail to unmount the filesystem. */
Ed Tanous82897c32022-02-21 14:11:59 -0800338TEST_F(EStoragedTest, UnmountFail)
John Wedigb810c922021-11-17 16:38:03 -0800339{
John Wedigb810c922021-11-17 16:38:03 -0800340 EXPECT_CALL(*mockCryptIface, cryptFormat(_, _, _, _, _, _, _, _)).Times(1);
341
342 EXPECT_CALL(*mockCryptIface, cryptKeyslotAddByVolumeKey(_, _, _, _, _, _))
343 .Times(1);
344
345 EXPECT_CALL(*mockCryptIface, cryptLoad(_, _, _)).Times(1);
346
347 EXPECT_CALL(*mockCryptIface, cryptActivateByPassphrase(_, _, _, _, _, _))
348 .Times(1);
349
350 EXPECT_CALL(*mockFsIface, runMkfs(testLuksDevName)).WillOnce(Return(0));
351
John Wedigb17f8252022-01-12 14:24:26 -0800352 EXPECT_CALL(*mockFsIface, directoryExists(path(esObject->getMountPoint())))
353 .WillOnce(Return(false));
354
John Wedigb810c922021-11-17 16:38:03 -0800355 EXPECT_CALL(*mockFsIface, createDirectory(path(esObject->getMountPoint())))
356 .WillOnce(Return(true));
357
358 EXPECT_CALL(*mockFsIface,
359 doMount(ContainsRegex("/dev/mapper/"),
360 StrEq(esObject->getMountPoint()), _, _, _))
361 .WillOnce(Return(0));
362
363 EXPECT_CALL(*mockFsIface, doUnmount(StrEq(esObject->getMountPoint())))
364 .WillOnce(Return(-1));
365
John Wedig972c3fa2021-12-29 17:30:41 -0800366 esObject->formatLuks(password, Volume::FilesystemType::ext4);
John Wedigb810c922021-11-17 16:38:03 -0800367 EXPECT_FALSE(esObject->isLocked());
368
John Wedig972c3fa2021-12-29 17:30:41 -0800369 EXPECT_THROW(esObject->lock(), InternalFailure);
John Wedigb810c922021-11-17 16:38:03 -0800370 EXPECT_FALSE(esObject->isLocked());
371}
372
373/* Test case where we fail to remove the mount point. */
Ed Tanous82897c32022-02-21 14:11:59 -0800374TEST_F(EStoragedTest, RemoveMountPointFail)
John Wedigb810c922021-11-17 16:38:03 -0800375{
John Wedigb810c922021-11-17 16:38:03 -0800376 EXPECT_CALL(*mockCryptIface, cryptFormat(_, _, _, _, _, _, _, _)).Times(1);
377
378 EXPECT_CALL(*mockCryptIface, cryptKeyslotAddByVolumeKey(_, _, _, _, _, _))
379 .Times(1);
380
381 EXPECT_CALL(*mockCryptIface, cryptLoad(_, _, _)).Times(1);
382
383 EXPECT_CALL(*mockCryptIface, cryptActivateByPassphrase(_, _, _, _, _, _))
384 .Times(1);
385
386 EXPECT_CALL(*mockFsIface, runMkfs(testLuksDevName)).WillOnce(Return(0));
387
John Wedigb17f8252022-01-12 14:24:26 -0800388 EXPECT_CALL(*mockFsIface, directoryExists(path(esObject->getMountPoint())))
389 .WillOnce(Return(false));
390
John Wedigb810c922021-11-17 16:38:03 -0800391 EXPECT_CALL(*mockFsIface, createDirectory(path(esObject->getMountPoint())))
392 .WillOnce(Return(true));
393
394 EXPECT_CALL(*mockFsIface,
395 doMount(ContainsRegex("/dev/mapper/"),
396 StrEq(esObject->getMountPoint()), _, _, _))
397 .WillOnce(Return(0));
398
399 EXPECT_CALL(*mockFsIface, doUnmount(StrEq(esObject->getMountPoint())))
400 .WillOnce(Return(0));
401
402 EXPECT_CALL(*mockFsIface, removeDirectory(path(esObject->getMountPoint())))
403 .WillOnce(Return(false));
404
John Wedig972c3fa2021-12-29 17:30:41 -0800405 esObject->formatLuks(password, Volume::FilesystemType::ext4);
John Wedigb810c922021-11-17 16:38:03 -0800406 EXPECT_FALSE(esObject->isLocked());
407
408 /* This will fail to remove the mount point. */
John Wedig972c3fa2021-12-29 17:30:41 -0800409 EXPECT_THROW(esObject->lock(), InternalFailure);
John Wedigb810c922021-11-17 16:38:03 -0800410 EXPECT_FALSE(esObject->isLocked());
411}
412
413/* Test case where we fail to deactivate the LUKS device. */
Ed Tanous82897c32022-02-21 14:11:59 -0800414TEST_F(EStoragedTest, DeactivateFail)
John Wedigb810c922021-11-17 16:38:03 -0800415{
John Wedigb810c922021-11-17 16:38:03 -0800416 EXPECT_CALL(*mockCryptIface, cryptFormat(_, _, _, _, _, _, _, _)).Times(1);
417
418 EXPECT_CALL(*mockCryptIface, cryptKeyslotAddByVolumeKey(_, _, _, _, _, _))
419 .Times(1);
420
421 EXPECT_CALL(*mockCryptIface, cryptLoad(_, _, _)).Times(1);
422
423 EXPECT_CALL(*mockCryptIface, cryptActivateByPassphrase(_, _, _, _, _, _))
424 .Times(1);
425
426 EXPECT_CALL(*mockFsIface, runMkfs(testLuksDevName)).WillOnce(Return(0));
427
John Wedigb17f8252022-01-12 14:24:26 -0800428 EXPECT_CALL(*mockFsIface, directoryExists(path(esObject->getMountPoint())))
429 .WillOnce(Return(false));
430
John Wedigb810c922021-11-17 16:38:03 -0800431 EXPECT_CALL(*mockFsIface, createDirectory(path(esObject->getMountPoint())))
432 .WillOnce(Return(true));
433
434 EXPECT_CALL(*mockFsIface,
435 doMount(ContainsRegex("/dev/mapper/"),
436 StrEq(esObject->getMountPoint()), _, _, _))
437 .WillOnce(Return(0));
438
439 EXPECT_CALL(*mockFsIface, doUnmount(StrEq(esObject->getMountPoint())))
440 .WillOnce(Return(0));
441
442 EXPECT_CALL(*mockFsIface, removeDirectory(path(esObject->getMountPoint())))
443 .WillOnce(Return(true));
444
445 EXPECT_CALL(*mockCryptIface, cryptDeactivate(_, _)).WillOnce(Return(-1));
446
447 /* Format the encrypted device. */
John Wedig972c3fa2021-12-29 17:30:41 -0800448 esObject->formatLuks(password, Volume::FilesystemType::ext4);
John Wedigb810c922021-11-17 16:38:03 -0800449 EXPECT_FALSE(esObject->isLocked());
450
John Wedig972c3fa2021-12-29 17:30:41 -0800451 EXPECT_THROW(esObject->lock(), InternalFailure);
John Wedigb810c922021-11-17 16:38:03 -0800452 EXPECT_FALSE(esObject->isLocked());
453}
454
455} // namespace estoraged_test