blob: 7a3286c750f1564aa7e162bac76ca060bdbc76ee [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 Wedigb810c922021-11-17 16:38:03 -08007#include <sdbusplus/test/sdbus_mock.hpp>
John Wedig972c3fa2021-12-29 17:30:41 -08008#include <xyz/openbmc_project/Common/error.hpp>
John Edward Broadbent59dffa62022-01-13 17:41:32 -08009#include <xyz/openbmc_project/Inventory/Item/Volume/client.hpp>
John Wedigb810c922021-11-17 16:38:03 -080010
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
21namespace estoraged_test
22{
23
John Wedig972c3fa2021-12-29 17:30:41 -080024using sdbusplus::xyz::openbmc_project::Common::Error::InternalFailure;
25using sdbusplus::xyz::openbmc_project::Common::Error::ResourceNotFound;
26using sdbusplus::xyz::openbmc_project::Inventory::Item::server::Volume;
John Wedigb810c922021-11-17 16:38:03 -080027using std::filesystem::path;
28using ::testing::_;
29using ::testing::ContainsRegex;
30using ::testing::IsNull;
31using ::testing::Return;
32using ::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 */
39sdbusplus::SdBusMock sdbusMock;
40
Ed Tanous82897c32022-02-21 14:11:59 -080041class EStoragedTest : public testing::Test
John Wedigb810c922021-11-17 16:38:03 -080042{
43 public:
Ed Tanous82897c32022-02-21 14:11:59 -080044 const char* testFileName = "testfile";
45 const char* testLuksDevName = "testfile_luksDev";
John Wedigb810c922021-11-17 16:38:03 -080046 std::ofstream testFile;
Ed Tanous82897c32022-02-21 14:11:59 -080047 std::unique_ptr<estoraged::EStoraged> esObject;
48 const char* testPath = "/test/openbmc_project/storage/test_dev";
49 const char* estoragedInterface =
John Wedig972c3fa2021-12-29 17:30:41 -080050 "xyz.openbmc_project.Inventory.Item.Volume";
John Wedigb810c922021-11-17 16:38:03 -080051 sdbusplus::bus::bus bus;
52 std::string passwordString;
53 std::vector<uint8_t> password;
Ed Tanous82897c32022-02-21 14:11:59 -080054 MockCryptsetupInterface* mockCryptIface{};
55 MockFilesystemInterface* mockFsIface{};
John Wedigb810c922021-11-17 16:38:03 -080056
Ed Tanous82897c32022-02-21 14:11:59 -080057 EStoragedTest() :
John Wedigb810c922021-11-17 16:38:03 -080058 bus(sdbusplus::get_mocked_new(&sdbusMock)), passwordString("password"),
59 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
73 EXPECT_CALL(sdbusMock,
Ed Tanous82897c32022-02-21 14:11:59 -080074 sd_bus_add_object_vtable(IsNull(), _, StrEq(testPath),
75 StrEq(estoragedInterface), _, _))
John Wedigb810c922021-11-17 16:38:03 -080076 .WillRepeatedly(Return(0));
77
78 EXPECT_CALL(sdbusMock,
Ed Tanous82897c32022-02-21 14:11:59 -080079 sd_bus_emit_object_added(IsNull(), StrEq(testPath)))
John Wedigb810c922021-11-17 16:38:03 -080080 .WillRepeatedly(Return(0));
81
82 EXPECT_CALL(sdbusMock,
Ed Tanous82897c32022-02-21 14:11:59 -080083 sd_bus_emit_object_removed(IsNull(), StrEq(testPath)))
John Wedigb810c922021-11-17 16:38:03 -080084 .WillRepeatedly(Return(0));
85
86 std::unique_ptr<MockCryptsetupInterface> cryptIface =
87 std::make_unique<MockCryptsetupInterface>();
88 mockCryptIface = cryptIface.get();
89 std::unique_ptr<MockFilesystemInterface> fsIface =
90 std::make_unique<MockFilesystemInterface>();
91 mockFsIface = fsIface.get();
92
Ed Tanous82897c32022-02-21 14:11:59 -080093 esObject = std::make_unique<estoraged::EStoraged>(
94 bus, testPath, testFileName, testLuksDevName, std::move(cryptIface),
John Wedigb810c922021-11-17 16:38:03 -080095 std::move(fsIface));
96 }
97
98 void TearDown() override
99 {
100 EXPECT_EQ(0, unlink(testFileName));
101 }
102};
103
104/* Test case to format and then lock the LUKS device. */
Ed Tanous82897c32022-02-21 14:11:59 -0800105TEST_F(EStoragedTest, FormatPass)
John Wedigb810c922021-11-17 16:38:03 -0800106{
107 EXPECT_CALL(sdbusMock,
108 sd_bus_emit_properties_changed_strv(
Ed Tanous82897c32022-02-21 14:11:59 -0800109 IsNull(), StrEq(testPath), StrEq(estoragedInterface), _))
John Wedigb810c922021-11-17 16:38:03 -0800110 .WillRepeatedly(Return(0));
111
112 EXPECT_CALL(*mockCryptIface, cryptFormat(_, _, _, _, _, _, _, _)).Times(1);
113
114 EXPECT_CALL(*mockCryptIface, cryptKeyslotAddByVolumeKey(_, _, _, _, _, _))
115 .Times(1);
116
117 EXPECT_CALL(*mockCryptIface, cryptLoad(_, _, _)).Times(1);
118
119 EXPECT_CALL(*mockCryptIface, cryptActivateByPassphrase(_, _, _, _, _, _))
120 .Times(1);
121
122 EXPECT_CALL(*mockFsIface, runMkfs(testLuksDevName)).WillOnce(Return(0));
123
John Wedigb17f8252022-01-12 14:24:26 -0800124 EXPECT_CALL(*mockFsIface, directoryExists(path(esObject->getMountPoint())))
125 .WillOnce(Return(false));
126
John Wedigb810c922021-11-17 16:38:03 -0800127 EXPECT_CALL(*mockFsIface, createDirectory(path(esObject->getMountPoint())))
128 .WillOnce(Return(true));
129
130 EXPECT_CALL(*mockFsIface,
131 doMount(ContainsRegex("/dev/mapper/"),
132 StrEq(esObject->getMountPoint()), _, _, _))
133 .WillOnce(Return(0));
134
135 EXPECT_CALL(*mockFsIface, doUnmount(StrEq(esObject->getMountPoint())))
136 .WillOnce(Return(0));
137
138 EXPECT_CALL(*mockFsIface, removeDirectory(path(esObject->getMountPoint())))
139 .WillOnce(Return(true));
140
141 EXPECT_CALL(*mockCryptIface, cryptDeactivate(_, _)).Times(1);
142
143 /* Format the encrypted device. */
John Wedig972c3fa2021-12-29 17:30:41 -0800144 esObject->formatLuks(password, Volume::FilesystemType::ext4);
John Wedigb810c922021-11-17 16:38:03 -0800145 EXPECT_FALSE(esObject->isLocked());
146
John Wedig972c3fa2021-12-29 17:30:41 -0800147 esObject->lock();
John Wedigb810c922021-11-17 16:38:03 -0800148 EXPECT_TRUE(esObject->isLocked());
149}
150
John Wedigb17f8252022-01-12 14:24:26 -0800151/*
152 * Test case where the mount point directory already exists, so it shouldn't
153 * try to create it.
154 */
Ed Tanous82897c32022-02-21 14:11:59 -0800155TEST_F(EStoragedTest, MountPointExistsPass)
John Wedigb17f8252022-01-12 14:24:26 -0800156{
157 EXPECT_CALL(sdbusMock,
158 sd_bus_emit_properties_changed_strv(
Ed Tanous82897c32022-02-21 14:11:59 -0800159 IsNull(), StrEq(testPath), StrEq(estoragedInterface), _))
John Wedigb17f8252022-01-12 14:24:26 -0800160 .WillRepeatedly(Return(0));
161
162 EXPECT_CALL(*mockCryptIface, cryptFormat(_, _, _, _, _, _, _, _)).Times(1);
163
164 EXPECT_CALL(*mockCryptIface, cryptKeyslotAddByVolumeKey(_, _, _, _, _, _))
165 .Times(1);
166
167 EXPECT_CALL(*mockCryptIface, cryptLoad(_, _, _)).Times(1);
168
169 EXPECT_CALL(*mockCryptIface, cryptActivateByPassphrase(_, _, _, _, _, _))
170 .Times(1);
171
172 EXPECT_CALL(*mockFsIface, runMkfs(testLuksDevName)).WillOnce(Return(0));
173
174 EXPECT_CALL(*mockFsIface, directoryExists(path(esObject->getMountPoint())))
175 .WillOnce(Return(true));
176
177 EXPECT_CALL(*mockFsIface, createDirectory(path(esObject->getMountPoint())))
178 .Times(0);
179
180 EXPECT_CALL(*mockFsIface,
181 doMount(ContainsRegex("/dev/mapper/"),
182 StrEq(esObject->getMountPoint()), _, _, _))
183 .WillOnce(Return(0));
184
185 EXPECT_CALL(*mockFsIface, doUnmount(StrEq(esObject->getMountPoint())))
186 .WillOnce(Return(0));
187
188 EXPECT_CALL(*mockFsIface, removeDirectory(path(esObject->getMountPoint())))
189 .WillOnce(Return(true));
190
191 EXPECT_CALL(*mockCryptIface, cryptDeactivate(_, _)).Times(1);
192
193 /* Format the encrypted device. */
194 esObject->formatLuks(password, Volume::FilesystemType::ext4);
195 EXPECT_FALSE(esObject->isLocked());
196
197 esObject->lock();
198 EXPECT_TRUE(esObject->isLocked());
199}
200
John Wedigb810c922021-11-17 16:38:03 -0800201/* Test case where the device/file doesn't exist. */
Ed Tanous82897c32022-02-21 14:11:59 -0800202TEST_F(EStoragedTest, FormatNoDeviceFail)
John Wedigb810c922021-11-17 16:38:03 -0800203{
204 /* Delete the test file. */
205 EXPECT_EQ(0, unlink(testFileName));
206
John Wedig972c3fa2021-12-29 17:30:41 -0800207 EXPECT_THROW(esObject->formatLuks(password, Volume::FilesystemType::ext4),
208 ResourceNotFound);
John Wedigb810c922021-11-17 16:38:03 -0800209 EXPECT_FALSE(esObject->isLocked());
210
211 /* Create the test file again, so that the TearDown function works. */
212 testFile.open(testFileName,
213 std::ios::out | std::ios::binary | std::ios::trunc);
214 testFile.close();
215}
216
217/* Test case where we fail to format the LUKS device. */
Ed Tanous82897c32022-02-21 14:11:59 -0800218TEST_F(EStoragedTest, FormatFail)
John Wedigb810c922021-11-17 16:38:03 -0800219{
220 EXPECT_CALL(*mockCryptIface, cryptFormat(_, _, _, _, _, _, _, _))
221 .WillOnce(Return(-1));
222
John Wedig972c3fa2021-12-29 17:30:41 -0800223 EXPECT_THROW(esObject->formatLuks(password, Volume::FilesystemType::ext4),
224 InternalFailure);
John Wedigb810c922021-11-17 16:38:03 -0800225 EXPECT_FALSE(esObject->isLocked());
226}
227
228/* Test case where we fail to set the password for the LUKS device. */
Ed Tanous82897c32022-02-21 14:11:59 -0800229TEST_F(EStoragedTest, AddKeyslotFail)
John Wedigb810c922021-11-17 16:38:03 -0800230{
231 EXPECT_CALL(sdbusMock,
232 sd_bus_emit_properties_changed_strv(
Ed Tanous82897c32022-02-21 14:11:59 -0800233 IsNull(), StrEq(testPath), StrEq(estoragedInterface), _))
John Wedigb810c922021-11-17 16:38:03 -0800234 .WillRepeatedly(Return(0));
235
236 EXPECT_CALL(*mockCryptIface, cryptFormat(_, _, _, _, _, _, _, _)).Times(1);
237
238 EXPECT_CALL(*mockCryptIface, cryptKeyslotAddByVolumeKey(_, _, _, _, _, _))
239 .WillOnce(Return(-1));
240
John Wedig972c3fa2021-12-29 17:30:41 -0800241 EXPECT_THROW(esObject->formatLuks(password, Volume::FilesystemType::ext4),
242 InternalFailure);
John Wedigb810c922021-11-17 16:38:03 -0800243 EXPECT_TRUE(esObject->isLocked());
244}
245
246/* Test case where we fail to load the LUKS header. */
Ed Tanous82897c32022-02-21 14:11:59 -0800247TEST_F(EStoragedTest, LoadLuksHeaderFail)
John Wedigb810c922021-11-17 16:38:03 -0800248{
249 EXPECT_CALL(sdbusMock,
250 sd_bus_emit_properties_changed_strv(
Ed Tanous82897c32022-02-21 14:11:59 -0800251 IsNull(), StrEq(testPath), StrEq(estoragedInterface), _))
John Wedigb810c922021-11-17 16:38:03 -0800252 .WillRepeatedly(Return(0));
253
254 EXPECT_CALL(*mockCryptIface, cryptFormat(_, _, _, _, _, _, _, _)).Times(1);
255
256 EXPECT_CALL(*mockCryptIface, cryptKeyslotAddByVolumeKey(_, _, _, _, _, _))
257 .Times(1);
258
259 EXPECT_CALL(*mockCryptIface, cryptLoad(_, _, _)).WillOnce(Return(-1));
260
John Wedig972c3fa2021-12-29 17:30:41 -0800261 EXPECT_THROW(esObject->formatLuks(password, Volume::FilesystemType::ext4),
262 InternalFailure);
John Wedigb810c922021-11-17 16:38:03 -0800263 EXPECT_TRUE(esObject->isLocked());
264}
265
266/* Test case where we fail to activate the LUKS device. */
Ed Tanous82897c32022-02-21 14:11:59 -0800267TEST_F(EStoragedTest, ActivateFail)
John Wedigb810c922021-11-17 16:38:03 -0800268{
269 EXPECT_CALL(sdbusMock,
270 sd_bus_emit_properties_changed_strv(
Ed Tanous82897c32022-02-21 14:11:59 -0800271 IsNull(), StrEq(testPath), StrEq(estoragedInterface), _))
John Wedigb810c922021-11-17 16:38:03 -0800272 .WillRepeatedly(Return(0));
273
274 EXPECT_CALL(*mockCryptIface, cryptFormat(_, _, _, _, _, _, _, _)).Times(1);
275
276 EXPECT_CALL(*mockCryptIface, cryptKeyslotAddByVolumeKey(_, _, _, _, _, _))
277 .Times(1);
278
279 EXPECT_CALL(*mockCryptIface, cryptLoad(_, _, _)).Times(1);
280
281 EXPECT_CALL(*mockCryptIface, cryptActivateByPassphrase(_, _, _, _, _, _))
282 .WillOnce(Return(-1));
283
John Wedig972c3fa2021-12-29 17:30:41 -0800284 EXPECT_THROW(esObject->formatLuks(password, Volume::FilesystemType::ext4),
285 InternalFailure);
John Wedigb810c922021-11-17 16:38:03 -0800286 EXPECT_TRUE(esObject->isLocked());
287}
288
289/* Test case where we fail to create the filesystem. */
Ed Tanous82897c32022-02-21 14:11:59 -0800290TEST_F(EStoragedTest, CreateFilesystemFail)
John Wedigb810c922021-11-17 16:38:03 -0800291{
292 EXPECT_CALL(sdbusMock,
293 sd_bus_emit_properties_changed_strv(
Ed Tanous82897c32022-02-21 14:11:59 -0800294 IsNull(), StrEq(testPath), StrEq(estoragedInterface), _))
John Wedigb810c922021-11-17 16:38:03 -0800295 .WillRepeatedly(Return(0));
296
297 EXPECT_CALL(*mockCryptIface, cryptFormat(_, _, _, _, _, _, _, _)).Times(1);
298
299 EXPECT_CALL(*mockCryptIface, cryptKeyslotAddByVolumeKey(_, _, _, _, _, _))
300 .Times(1);
301
302 EXPECT_CALL(*mockCryptIface, cryptLoad(_, _, _)).Times(1);
303
304 EXPECT_CALL(*mockCryptIface, cryptActivateByPassphrase(_, _, _, _, _, _))
305 .Times(1);
306
307 EXPECT_CALL(*mockFsIface, runMkfs(testLuksDevName)).WillOnce(Return(-1));
308
John Wedig972c3fa2021-12-29 17:30:41 -0800309 EXPECT_THROW(esObject->formatLuks(password, Volume::FilesystemType::ext4),
310 InternalFailure);
John Wedigb810c922021-11-17 16:38:03 -0800311 EXPECT_FALSE(esObject->isLocked());
312}
313
314/* Test case where we fail to create the mount point. */
Ed Tanous82897c32022-02-21 14:11:59 -0800315TEST_F(EStoragedTest, CreateMountPointFail)
John Wedigb810c922021-11-17 16:38:03 -0800316{
317 EXPECT_CALL(sdbusMock,
318 sd_bus_emit_properties_changed_strv(
Ed Tanous82897c32022-02-21 14:11:59 -0800319 IsNull(), StrEq(testPath), StrEq(estoragedInterface), _))
John Wedigb810c922021-11-17 16:38:03 -0800320 .WillRepeatedly(Return(0));
321
322 EXPECT_CALL(*mockCryptIface, cryptFormat(_, _, _, _, _, _, _, _)).Times(1);
323
324 EXPECT_CALL(*mockCryptIface, cryptKeyslotAddByVolumeKey(_, _, _, _, _, _))
325 .Times(1);
326
327 EXPECT_CALL(*mockCryptIface, cryptLoad(_, _, _)).Times(1);
328
329 EXPECT_CALL(*mockCryptIface, cryptActivateByPassphrase(_, _, _, _, _, _))
330 .Times(1);
331
332 EXPECT_CALL(*mockFsIface, runMkfs(testLuksDevName)).WillOnce(Return(0));
333
John Wedigb17f8252022-01-12 14:24:26 -0800334 EXPECT_CALL(*mockFsIface, directoryExists(path(esObject->getMountPoint())))
335 .WillOnce(Return(false));
336
John Wedigb810c922021-11-17 16:38:03 -0800337 EXPECT_CALL(*mockFsIface, createDirectory(path(esObject->getMountPoint())))
338 .WillOnce(Return(false));
339
John Wedig972c3fa2021-12-29 17:30:41 -0800340 EXPECT_THROW(esObject->formatLuks(password, Volume::FilesystemType::ext4),
341 InternalFailure);
John Wedigb810c922021-11-17 16:38:03 -0800342 EXPECT_FALSE(esObject->isLocked());
343}
344
345/* Test case where we fail to mount the filesystem. */
Ed Tanous82897c32022-02-21 14:11:59 -0800346TEST_F(EStoragedTest, MountFail)
John Wedigb810c922021-11-17 16:38:03 -0800347{
348 EXPECT_CALL(sdbusMock,
349 sd_bus_emit_properties_changed_strv(
Ed Tanous82897c32022-02-21 14:11:59 -0800350 IsNull(), StrEq(testPath), StrEq(estoragedInterface), _))
John Wedigb810c922021-11-17 16:38:03 -0800351 .WillRepeatedly(Return(0));
352
353 EXPECT_CALL(*mockCryptIface, cryptFormat(_, _, _, _, _, _, _, _)).Times(1);
354
355 EXPECT_CALL(*mockCryptIface, cryptKeyslotAddByVolumeKey(_, _, _, _, _, _))
356 .Times(1);
357
358 EXPECT_CALL(*mockCryptIface, cryptLoad(_, _, _)).Times(1);
359
360 EXPECT_CALL(*mockCryptIface, cryptActivateByPassphrase(_, _, _, _, _, _))
361 .Times(1);
362
363 EXPECT_CALL(*mockFsIface, runMkfs(testLuksDevName)).WillOnce(Return(0));
364
John Wedigb17f8252022-01-12 14:24:26 -0800365 EXPECT_CALL(*mockFsIface, directoryExists(path(esObject->getMountPoint())))
366 .WillOnce(Return(false));
367
John Wedigb810c922021-11-17 16:38:03 -0800368 EXPECT_CALL(*mockFsIface, createDirectory(path(esObject->getMountPoint())))
369 .WillOnce(Return(true));
370
371 EXPECT_CALL(*mockFsIface,
372 doMount(ContainsRegex("/dev/mapper/"),
373 StrEq(esObject->getMountPoint()), _, _, _))
374 .WillOnce(Return(-1));
375
376 EXPECT_CALL(*mockFsIface, removeDirectory(path(esObject->getMountPoint())))
377 .WillOnce(Return(true));
378
John Wedig972c3fa2021-12-29 17:30:41 -0800379 EXPECT_THROW(esObject->formatLuks(password, Volume::FilesystemType::ext4),
380 InternalFailure);
John Wedigb810c922021-11-17 16:38:03 -0800381 EXPECT_FALSE(esObject->isLocked());
382}
383
384/* Test case where we fail to unmount the filesystem. */
Ed Tanous82897c32022-02-21 14:11:59 -0800385TEST_F(EStoragedTest, UnmountFail)
John Wedigb810c922021-11-17 16:38:03 -0800386{
387 EXPECT_CALL(sdbusMock,
388 sd_bus_emit_properties_changed_strv(
Ed Tanous82897c32022-02-21 14:11:59 -0800389 IsNull(), StrEq(testPath), StrEq(estoragedInterface), _))
John Wedigb810c922021-11-17 16:38:03 -0800390 .WillRepeatedly(Return(0));
391
392 EXPECT_CALL(*mockCryptIface, cryptFormat(_, _, _, _, _, _, _, _)).Times(1);
393
394 EXPECT_CALL(*mockCryptIface, cryptKeyslotAddByVolumeKey(_, _, _, _, _, _))
395 .Times(1);
396
397 EXPECT_CALL(*mockCryptIface, cryptLoad(_, _, _)).Times(1);
398
399 EXPECT_CALL(*mockCryptIface, cryptActivateByPassphrase(_, _, _, _, _, _))
400 .Times(1);
401
402 EXPECT_CALL(*mockFsIface, runMkfs(testLuksDevName)).WillOnce(Return(0));
403
John Wedigb17f8252022-01-12 14:24:26 -0800404 EXPECT_CALL(*mockFsIface, directoryExists(path(esObject->getMountPoint())))
405 .WillOnce(Return(false));
406
John Wedigb810c922021-11-17 16:38:03 -0800407 EXPECT_CALL(*mockFsIface, createDirectory(path(esObject->getMountPoint())))
408 .WillOnce(Return(true));
409
410 EXPECT_CALL(*mockFsIface,
411 doMount(ContainsRegex("/dev/mapper/"),
412 StrEq(esObject->getMountPoint()), _, _, _))
413 .WillOnce(Return(0));
414
415 EXPECT_CALL(*mockFsIface, doUnmount(StrEq(esObject->getMountPoint())))
416 .WillOnce(Return(-1));
417
John Wedig972c3fa2021-12-29 17:30:41 -0800418 esObject->formatLuks(password, Volume::FilesystemType::ext4);
John Wedigb810c922021-11-17 16:38:03 -0800419 EXPECT_FALSE(esObject->isLocked());
420
John Wedig972c3fa2021-12-29 17:30:41 -0800421 EXPECT_THROW(esObject->lock(), InternalFailure);
John Wedigb810c922021-11-17 16:38:03 -0800422 EXPECT_FALSE(esObject->isLocked());
423}
424
425/* Test case where we fail to remove the mount point. */
Ed Tanous82897c32022-02-21 14:11:59 -0800426TEST_F(EStoragedTest, RemoveMountPointFail)
John Wedigb810c922021-11-17 16:38:03 -0800427{
428 EXPECT_CALL(sdbusMock,
429 sd_bus_emit_properties_changed_strv(
Ed Tanous82897c32022-02-21 14:11:59 -0800430 IsNull(), StrEq(testPath), StrEq(estoragedInterface), _))
John Wedigb810c922021-11-17 16:38:03 -0800431 .WillRepeatedly(Return(0));
432
433 EXPECT_CALL(*mockCryptIface, cryptFormat(_, _, _, _, _, _, _, _)).Times(1);
434
435 EXPECT_CALL(*mockCryptIface, cryptKeyslotAddByVolumeKey(_, _, _, _, _, _))
436 .Times(1);
437
438 EXPECT_CALL(*mockCryptIface, cryptLoad(_, _, _)).Times(1);
439
440 EXPECT_CALL(*mockCryptIface, cryptActivateByPassphrase(_, _, _, _, _, _))
441 .Times(1);
442
443 EXPECT_CALL(*mockFsIface, runMkfs(testLuksDevName)).WillOnce(Return(0));
444
John Wedigb17f8252022-01-12 14:24:26 -0800445 EXPECT_CALL(*mockFsIface, directoryExists(path(esObject->getMountPoint())))
446 .WillOnce(Return(false));
447
John Wedigb810c922021-11-17 16:38:03 -0800448 EXPECT_CALL(*mockFsIface, createDirectory(path(esObject->getMountPoint())))
449 .WillOnce(Return(true));
450
451 EXPECT_CALL(*mockFsIface,
452 doMount(ContainsRegex("/dev/mapper/"),
453 StrEq(esObject->getMountPoint()), _, _, _))
454 .WillOnce(Return(0));
455
456 EXPECT_CALL(*mockFsIface, doUnmount(StrEq(esObject->getMountPoint())))
457 .WillOnce(Return(0));
458
459 EXPECT_CALL(*mockFsIface, removeDirectory(path(esObject->getMountPoint())))
460 .WillOnce(Return(false));
461
John Wedig972c3fa2021-12-29 17:30:41 -0800462 esObject->formatLuks(password, Volume::FilesystemType::ext4);
John Wedigb810c922021-11-17 16:38:03 -0800463 EXPECT_FALSE(esObject->isLocked());
464
465 /* This will fail to remove the mount point. */
John Wedig972c3fa2021-12-29 17:30:41 -0800466 EXPECT_THROW(esObject->lock(), InternalFailure);
John Wedigb810c922021-11-17 16:38:03 -0800467 EXPECT_FALSE(esObject->isLocked());
468}
469
470/* Test case where we fail to deactivate the LUKS device. */
Ed Tanous82897c32022-02-21 14:11:59 -0800471TEST_F(EStoragedTest, DeactivateFail)
John Wedigb810c922021-11-17 16:38:03 -0800472{
473 EXPECT_CALL(sdbusMock,
474 sd_bus_emit_properties_changed_strv(
Ed Tanous82897c32022-02-21 14:11:59 -0800475 IsNull(), StrEq(testPath), StrEq(estoragedInterface), _))
John Wedigb810c922021-11-17 16:38:03 -0800476 .WillRepeatedly(Return(0));
477
478 EXPECT_CALL(*mockCryptIface, cryptFormat(_, _, _, _, _, _, _, _)).Times(1);
479
480 EXPECT_CALL(*mockCryptIface, cryptKeyslotAddByVolumeKey(_, _, _, _, _, _))
481 .Times(1);
482
483 EXPECT_CALL(*mockCryptIface, cryptLoad(_, _, _)).Times(1);
484
485 EXPECT_CALL(*mockCryptIface, cryptActivateByPassphrase(_, _, _, _, _, _))
486 .Times(1);
487
488 EXPECT_CALL(*mockFsIface, runMkfs(testLuksDevName)).WillOnce(Return(0));
489
John Wedigb17f8252022-01-12 14:24:26 -0800490 EXPECT_CALL(*mockFsIface, directoryExists(path(esObject->getMountPoint())))
491 .WillOnce(Return(false));
492
John Wedigb810c922021-11-17 16:38:03 -0800493 EXPECT_CALL(*mockFsIface, createDirectory(path(esObject->getMountPoint())))
494 .WillOnce(Return(true));
495
496 EXPECT_CALL(*mockFsIface,
497 doMount(ContainsRegex("/dev/mapper/"),
498 StrEq(esObject->getMountPoint()), _, _, _))
499 .WillOnce(Return(0));
500
501 EXPECT_CALL(*mockFsIface, doUnmount(StrEq(esObject->getMountPoint())))
502 .WillOnce(Return(0));
503
504 EXPECT_CALL(*mockFsIface, removeDirectory(path(esObject->getMountPoint())))
505 .WillOnce(Return(true));
506
507 EXPECT_CALL(*mockCryptIface, cryptDeactivate(_, _)).WillOnce(Return(-1));
508
509 /* Format the encrypted device. */
John Wedig972c3fa2021-12-29 17:30:41 -0800510 esObject->formatLuks(password, Volume::FilesystemType::ext4);
John Wedigb810c922021-11-17 16:38:03 -0800511 EXPECT_FALSE(esObject->isLocked());
512
John Wedig972c3fa2021-12-29 17:30:41 -0800513 EXPECT_THROW(esObject->lock(), InternalFailure);
John Wedigb810c922021-11-17 16:38:03 -0800514 EXPECT_FALSE(esObject->isLocked());
515}
516
517} // namespace estoraged_test