blob: e7ad5621a3483e21aa8d957d371a39e08b079f36 [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 Edward Broadbente35e7362022-03-22 16:14:24 -070046 uint64_t testSize = 24;
John Wedigb810c922021-11-17 16:38:03 -080047 std::ofstream testFile;
Ed Tanous82897c32022-02-21 14:11:59 -080048 std::unique_ptr<estoraged::EStoraged> esObject;
49 const char* testPath = "/test/openbmc_project/storage/test_dev";
50 const char* estoragedInterface =
John Wedig972c3fa2021-12-29 17:30:41 -080051 "xyz.openbmc_project.Inventory.Item.Volume";
John Edward Broadbent86dfb242022-03-14 11:04:36 -070052 const char* driveInterface = "xyz.openbmc_project.Inventory.Item.Drive";
John Wedigb810c922021-11-17 16:38:03 -080053 sdbusplus::bus::bus bus;
54 std::string passwordString;
55 std::vector<uint8_t> password;
Ed Tanous82897c32022-02-21 14:11:59 -080056 MockCryptsetupInterface* mockCryptIface{};
57 MockFilesystemInterface* mockFsIface{};
John Wedigb810c922021-11-17 16:38:03 -080058
Ed Tanous82897c32022-02-21 14:11:59 -080059 EStoragedTest() :
John Wedigb810c922021-11-17 16:38:03 -080060 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,
Ed Tanous82897c32022-02-21 14:11:59 -080076 sd_bus_add_object_vtable(IsNull(), _, StrEq(testPath),
77 StrEq(estoragedInterface), _, _))
John Wedigb810c922021-11-17 16:38:03 -080078 .WillRepeatedly(Return(0));
79
80 EXPECT_CALL(sdbusMock,
John Edward Broadbent86dfb242022-03-14 11:04:36 -070081 sd_bus_add_object_vtable(IsNull(), _, StrEq(testPath),
82 StrEq(driveInterface), _, _))
83 .WillRepeatedly(Return(0));
84
85 EXPECT_CALL(sdbusMock,
Ed Tanous82897c32022-02-21 14:11:59 -080086 sd_bus_emit_object_added(IsNull(), StrEq(testPath)))
John Wedigb810c922021-11-17 16:38:03 -080087 .WillRepeatedly(Return(0));
88
89 EXPECT_CALL(sdbusMock,
Ed Tanous82897c32022-02-21 14:11:59 -080090 sd_bus_emit_object_removed(IsNull(), StrEq(testPath)))
John Wedigb810c922021-11-17 16:38:03 -080091 .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
Ed Tanous82897c32022-02-21 14:11:59 -0800100 esObject = std::make_unique<estoraged::EStoraged>(
John Edward Broadbente35e7362022-03-22 16:14:24 -0700101 bus, testPath, testFileName, testLuksDevName, testSize,
102 std::move(cryptIface), std::move(fsIface));
John Wedigb810c922021-11-17 16:38:03 -0800103 }
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. */
Ed Tanous82897c32022-02-21 14:11:59 -0800112TEST_F(EStoragedTest, FormatPass)
John Wedigb810c922021-11-17 16:38:03 -0800113{
114 EXPECT_CALL(sdbusMock,
John Edward Broadbent86dfb242022-03-14 11:04:36 -0700115 sd_bus_emit_properties_changed_strv(IsNull(), StrEq(testPath),
116 StrEq(driveInterface), _))
117 .WillRepeatedly(Return(0));
118
119 EXPECT_CALL(sdbusMock,
John Wedigb810c922021-11-17 16:38:03 -0800120 sd_bus_emit_properties_changed_strv(
Ed Tanous82897c32022-02-21 14:11:59 -0800121 IsNull(), StrEq(testPath), StrEq(estoragedInterface), _))
John Wedigb810c922021-11-17 16:38:03 -0800122 .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
John Wedigb17f8252022-01-12 14:24:26 -0800136 EXPECT_CALL(*mockFsIface, directoryExists(path(esObject->getMountPoint())))
137 .WillOnce(Return(false));
138
John Wedigb810c922021-11-17 16:38:03 -0800139 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. */
John Wedig972c3fa2021-12-29 17:30:41 -0800156 esObject->formatLuks(password, Volume::FilesystemType::ext4);
John Wedigb810c922021-11-17 16:38:03 -0800157 EXPECT_FALSE(esObject->isLocked());
158
John Wedig972c3fa2021-12-29 17:30:41 -0800159 esObject->lock();
John Wedigb810c922021-11-17 16:38:03 -0800160 EXPECT_TRUE(esObject->isLocked());
161}
162
John Wedigb17f8252022-01-12 14:24:26 -0800163/*
164 * Test case where the mount point directory already exists, so it shouldn't
165 * try to create it.
166 */
Ed Tanous82897c32022-02-21 14:11:59 -0800167TEST_F(EStoragedTest, MountPointExistsPass)
John Wedigb17f8252022-01-12 14:24:26 -0800168{
169 EXPECT_CALL(sdbusMock,
John Edward Broadbent86dfb242022-03-14 11:04:36 -0700170 sd_bus_emit_properties_changed_strv(IsNull(), StrEq(testPath),
171 StrEq(driveInterface), _))
172 .WillRepeatedly(Return(0));
173
174 EXPECT_CALL(sdbusMock,
John Wedigb17f8252022-01-12 14:24:26 -0800175 sd_bus_emit_properties_changed_strv(
Ed Tanous82897c32022-02-21 14:11:59 -0800176 IsNull(), StrEq(testPath), StrEq(estoragedInterface), _))
John Wedigb17f8252022-01-12 14:24:26 -0800177 .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
John Wedigb810c922021-11-17 16:38:03 -0800218/* Test case where the device/file doesn't exist. */
Ed Tanous82897c32022-02-21 14:11:59 -0800219TEST_F(EStoragedTest, FormatNoDeviceFail)
John Wedigb810c922021-11-17 16:38:03 -0800220{
221 /* Delete the test file. */
222 EXPECT_EQ(0, unlink(testFileName));
223
John Wedig972c3fa2021-12-29 17:30:41 -0800224 EXPECT_THROW(esObject->formatLuks(password, Volume::FilesystemType::ext4),
225 ResourceNotFound);
John Wedigb810c922021-11-17 16:38:03 -0800226 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. */
Ed Tanous82897c32022-02-21 14:11:59 -0800235TEST_F(EStoragedTest, FormatFail)
John Wedigb810c922021-11-17 16:38:03 -0800236{
237 EXPECT_CALL(*mockCryptIface, cryptFormat(_, _, _, _, _, _, _, _))
238 .WillOnce(Return(-1));
239
John Wedig972c3fa2021-12-29 17:30:41 -0800240 EXPECT_THROW(esObject->formatLuks(password, Volume::FilesystemType::ext4),
241 InternalFailure);
John Wedigb810c922021-11-17 16:38:03 -0800242 EXPECT_FALSE(esObject->isLocked());
243}
244
245/* Test case where we fail to set the password for the LUKS device. */
Ed Tanous82897c32022-02-21 14:11:59 -0800246TEST_F(EStoragedTest, AddKeyslotFail)
John Wedigb810c922021-11-17 16:38:03 -0800247{
248 EXPECT_CALL(sdbusMock,
John Edward Broadbent86dfb242022-03-14 11:04:36 -0700249 sd_bus_emit_properties_changed_strv(IsNull(), StrEq(testPath),
250 StrEq(driveInterface), _))
251 .WillRepeatedly(Return(0));
252
253 EXPECT_CALL(sdbusMock,
John Wedigb810c922021-11-17 16:38:03 -0800254 sd_bus_emit_properties_changed_strv(
Ed Tanous82897c32022-02-21 14:11:59 -0800255 IsNull(), StrEq(testPath), StrEq(estoragedInterface), _))
John Wedigb810c922021-11-17 16:38:03 -0800256 .WillRepeatedly(Return(0));
257
258 EXPECT_CALL(*mockCryptIface, cryptFormat(_, _, _, _, _, _, _, _)).Times(1);
259
260 EXPECT_CALL(*mockCryptIface, cryptKeyslotAddByVolumeKey(_, _, _, _, _, _))
261 .WillOnce(Return(-1));
262
John Wedig972c3fa2021-12-29 17:30:41 -0800263 EXPECT_THROW(esObject->formatLuks(password, Volume::FilesystemType::ext4),
264 InternalFailure);
John Wedigb810c922021-11-17 16:38:03 -0800265 EXPECT_TRUE(esObject->isLocked());
266}
267
268/* Test case where we fail to load the LUKS header. */
Ed Tanous82897c32022-02-21 14:11:59 -0800269TEST_F(EStoragedTest, LoadLuksHeaderFail)
John Wedigb810c922021-11-17 16:38:03 -0800270{
271 EXPECT_CALL(sdbusMock,
John Edward Broadbent86dfb242022-03-14 11:04:36 -0700272 sd_bus_emit_properties_changed_strv(IsNull(), StrEq(testPath),
273 StrEq(driveInterface), _))
274 .WillRepeatedly(Return(0));
275
276 EXPECT_CALL(sdbusMock,
John Wedigb810c922021-11-17 16:38:03 -0800277 sd_bus_emit_properties_changed_strv(
Ed Tanous82897c32022-02-21 14:11:59 -0800278 IsNull(), StrEq(testPath), StrEq(estoragedInterface), _))
John Wedigb810c922021-11-17 16:38:03 -0800279 .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
John Wedig972c3fa2021-12-29 17:30:41 -0800288 EXPECT_THROW(esObject->formatLuks(password, Volume::FilesystemType::ext4),
289 InternalFailure);
John Wedigb810c922021-11-17 16:38:03 -0800290 EXPECT_TRUE(esObject->isLocked());
291}
292
293/* Test case where we fail to activate the LUKS device. */
Ed Tanous82897c32022-02-21 14:11:59 -0800294TEST_F(EStoragedTest, ActivateFail)
John Wedigb810c922021-11-17 16:38:03 -0800295{
296 EXPECT_CALL(sdbusMock,
John Edward Broadbent86dfb242022-03-14 11:04:36 -0700297 sd_bus_emit_properties_changed_strv(IsNull(), StrEq(testPath),
298 StrEq(driveInterface), _))
299 .WillRepeatedly(Return(0));
300
301 EXPECT_CALL(sdbusMock,
John Wedigb810c922021-11-17 16:38:03 -0800302 sd_bus_emit_properties_changed_strv(
Ed Tanous82897c32022-02-21 14:11:59 -0800303 IsNull(), StrEq(testPath), StrEq(estoragedInterface), _))
John Wedigb810c922021-11-17 16:38:03 -0800304 .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
John Wedig972c3fa2021-12-29 17:30:41 -0800316 EXPECT_THROW(esObject->formatLuks(password, Volume::FilesystemType::ext4),
317 InternalFailure);
John Wedigb810c922021-11-17 16:38:03 -0800318 EXPECT_TRUE(esObject->isLocked());
319}
320
321/* Test case where we fail to create the filesystem. */
Ed Tanous82897c32022-02-21 14:11:59 -0800322TEST_F(EStoragedTest, CreateFilesystemFail)
John Wedigb810c922021-11-17 16:38:03 -0800323{
324 EXPECT_CALL(sdbusMock,
John Edward Broadbent86dfb242022-03-14 11:04:36 -0700325 sd_bus_emit_properties_changed_strv(IsNull(), StrEq(testPath),
326 StrEq(driveInterface), _))
327 .WillRepeatedly(Return(0));
328
329 EXPECT_CALL(sdbusMock,
John Wedigb810c922021-11-17 16:38:03 -0800330 sd_bus_emit_properties_changed_strv(
Ed Tanous82897c32022-02-21 14:11:59 -0800331 IsNull(), StrEq(testPath), StrEq(estoragedInterface), _))
John Wedigb810c922021-11-17 16:38:03 -0800332 .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
John Wedig972c3fa2021-12-29 17:30:41 -0800346 EXPECT_THROW(esObject->formatLuks(password, Volume::FilesystemType::ext4),
347 InternalFailure);
John Wedigb810c922021-11-17 16:38:03 -0800348 EXPECT_FALSE(esObject->isLocked());
349}
350
351/* Test case where we fail to create the mount point. */
Ed Tanous82897c32022-02-21 14:11:59 -0800352TEST_F(EStoragedTest, CreateMountPointFail)
John Wedigb810c922021-11-17 16:38:03 -0800353{
354 EXPECT_CALL(sdbusMock,
John Edward Broadbent86dfb242022-03-14 11:04:36 -0700355 sd_bus_emit_properties_changed_strv(IsNull(), StrEq(testPath),
356 StrEq(driveInterface), _))
357 .WillRepeatedly(Return(0));
358
359 EXPECT_CALL(sdbusMock,
John Wedigb810c922021-11-17 16:38:03 -0800360 sd_bus_emit_properties_changed_strv(
Ed Tanous82897c32022-02-21 14:11:59 -0800361 IsNull(), StrEq(testPath), StrEq(estoragedInterface), _))
John Wedigb810c922021-11-17 16:38:03 -0800362 .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
John Wedigb17f8252022-01-12 14:24:26 -0800376 EXPECT_CALL(*mockFsIface, directoryExists(path(esObject->getMountPoint())))
377 .WillOnce(Return(false));
378
John Wedigb810c922021-11-17 16:38:03 -0800379 EXPECT_CALL(*mockFsIface, createDirectory(path(esObject->getMountPoint())))
380 .WillOnce(Return(false));
381
John Wedig972c3fa2021-12-29 17:30:41 -0800382 EXPECT_THROW(esObject->formatLuks(password, Volume::FilesystemType::ext4),
383 InternalFailure);
John Wedigb810c922021-11-17 16:38:03 -0800384 EXPECT_FALSE(esObject->isLocked());
385}
386
387/* Test case where we fail to mount the filesystem. */
Ed Tanous82897c32022-02-21 14:11:59 -0800388TEST_F(EStoragedTest, MountFail)
John Wedigb810c922021-11-17 16:38:03 -0800389{
390 EXPECT_CALL(sdbusMock,
John Edward Broadbent86dfb242022-03-14 11:04:36 -0700391 sd_bus_emit_properties_changed_strv(IsNull(), StrEq(testPath),
392 StrEq(driveInterface), _))
393 .WillRepeatedly(Return(0));
394
395 EXPECT_CALL(sdbusMock,
John Wedigb810c922021-11-17 16:38:03 -0800396 sd_bus_emit_properties_changed_strv(
Ed Tanous82897c32022-02-21 14:11:59 -0800397 IsNull(), StrEq(testPath), StrEq(estoragedInterface), _))
John Wedigb810c922021-11-17 16:38:03 -0800398 .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
John Wedigb17f8252022-01-12 14:24:26 -0800412 EXPECT_CALL(*mockFsIface, directoryExists(path(esObject->getMountPoint())))
413 .WillOnce(Return(false));
414
John Wedigb810c922021-11-17 16:38:03 -0800415 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
John Wedig972c3fa2021-12-29 17:30:41 -0800426 EXPECT_THROW(esObject->formatLuks(password, Volume::FilesystemType::ext4),
427 InternalFailure);
John Wedigb810c922021-11-17 16:38:03 -0800428 EXPECT_FALSE(esObject->isLocked());
429}
430
431/* Test case where we fail to unmount the filesystem. */
Ed Tanous82897c32022-02-21 14:11:59 -0800432TEST_F(EStoragedTest, UnmountFail)
John Wedigb810c922021-11-17 16:38:03 -0800433{
434 EXPECT_CALL(sdbusMock,
John Edward Broadbent86dfb242022-03-14 11:04:36 -0700435 sd_bus_emit_properties_changed_strv(IsNull(), StrEq(testPath),
436 StrEq(driveInterface), _))
437 .WillRepeatedly(Return(0));
438
439 EXPECT_CALL(sdbusMock,
John Wedigb810c922021-11-17 16:38:03 -0800440 sd_bus_emit_properties_changed_strv(
Ed Tanous82897c32022-02-21 14:11:59 -0800441 IsNull(), StrEq(testPath), StrEq(estoragedInterface), _))
John Wedigb810c922021-11-17 16:38:03 -0800442 .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
John Wedigb17f8252022-01-12 14:24:26 -0800456 EXPECT_CALL(*mockFsIface, directoryExists(path(esObject->getMountPoint())))
457 .WillOnce(Return(false));
458
John Wedigb810c922021-11-17 16:38:03 -0800459 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
John Wedig972c3fa2021-12-29 17:30:41 -0800470 esObject->formatLuks(password, Volume::FilesystemType::ext4);
John Wedigb810c922021-11-17 16:38:03 -0800471 EXPECT_FALSE(esObject->isLocked());
472
John Wedig972c3fa2021-12-29 17:30:41 -0800473 EXPECT_THROW(esObject->lock(), InternalFailure);
John Wedigb810c922021-11-17 16:38:03 -0800474 EXPECT_FALSE(esObject->isLocked());
475}
476
477/* Test case where we fail to remove the mount point. */
Ed Tanous82897c32022-02-21 14:11:59 -0800478TEST_F(EStoragedTest, RemoveMountPointFail)
John Wedigb810c922021-11-17 16:38:03 -0800479{
480 EXPECT_CALL(sdbusMock,
John Edward Broadbent86dfb242022-03-14 11:04:36 -0700481 sd_bus_emit_properties_changed_strv(IsNull(), StrEq(testPath),
482 StrEq(driveInterface), _))
483 .WillRepeatedly(Return(0));
484
485 EXPECT_CALL(sdbusMock,
John Wedigb810c922021-11-17 16:38:03 -0800486 sd_bus_emit_properties_changed_strv(
Ed Tanous82897c32022-02-21 14:11:59 -0800487 IsNull(), StrEq(testPath), StrEq(estoragedInterface), _))
John Wedigb810c922021-11-17 16:38:03 -0800488 .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
John Wedigb17f8252022-01-12 14:24:26 -0800502 EXPECT_CALL(*mockFsIface, directoryExists(path(esObject->getMountPoint())))
503 .WillOnce(Return(false));
504
John Wedigb810c922021-11-17 16:38:03 -0800505 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
John Wedig972c3fa2021-12-29 17:30:41 -0800519 esObject->formatLuks(password, Volume::FilesystemType::ext4);
John Wedigb810c922021-11-17 16:38:03 -0800520 EXPECT_FALSE(esObject->isLocked());
521
522 /* This will fail to remove the mount point. */
John Wedig972c3fa2021-12-29 17:30:41 -0800523 EXPECT_THROW(esObject->lock(), InternalFailure);
John Wedigb810c922021-11-17 16:38:03 -0800524 EXPECT_FALSE(esObject->isLocked());
525}
526
527/* Test case where we fail to deactivate the LUKS device. */
Ed Tanous82897c32022-02-21 14:11:59 -0800528TEST_F(EStoragedTest, DeactivateFail)
John Wedigb810c922021-11-17 16:38:03 -0800529{
530 EXPECT_CALL(sdbusMock,
John Edward Broadbent86dfb242022-03-14 11:04:36 -0700531 sd_bus_emit_properties_changed_strv(IsNull(), StrEq(testPath),
532 StrEq(driveInterface), _))
533 .WillRepeatedly(Return(0));
534
535 EXPECT_CALL(sdbusMock,
John Wedigb810c922021-11-17 16:38:03 -0800536 sd_bus_emit_properties_changed_strv(
Ed Tanous82897c32022-02-21 14:11:59 -0800537 IsNull(), StrEq(testPath), StrEq(estoragedInterface), _))
John Wedigb810c922021-11-17 16:38:03 -0800538 .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
John Wedigb17f8252022-01-12 14:24:26 -0800552 EXPECT_CALL(*mockFsIface, directoryExists(path(esObject->getMountPoint())))
553 .WillOnce(Return(false));
554
John Wedigb810c922021-11-17 16:38:03 -0800555 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. */
John Wedig972c3fa2021-12-29 17:30:41 -0800572 esObject->formatLuks(password, Volume::FilesystemType::ext4);
John Wedigb810c922021-11-17 16:38:03 -0800573 EXPECT_FALSE(esObject->isLocked());
574
John Wedig972c3fa2021-12-29 17:30:41 -0800575 EXPECT_THROW(esObject->lock(), InternalFailure);
John Wedigb810c922021-11-17 16:38:03 -0800576 EXPECT_FALSE(esObject->isLocked());
577}
578
579} // namespace estoraged_test