blob: e77d4c65a0d7296a06fc8cc5807b7083568aa4f9 [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 Edward Broadbent86dfb242022-03-14 11:04:36 -070051 const char* driveInterface = "xyz.openbmc_project.Inventory.Item.Drive";
John Wedigb810c922021-11-17 16:38:03 -080052 sdbusplus::bus::bus bus;
53 std::string passwordString;
54 std::vector<uint8_t> password;
Ed Tanous82897c32022-02-21 14:11:59 -080055 MockCryptsetupInterface* mockCryptIface{};
56 MockFilesystemInterface* mockFsIface{};
John Wedigb810c922021-11-17 16:38:03 -080057
Ed Tanous82897c32022-02-21 14:11:59 -080058 EStoragedTest() :
John Wedigb810c922021-11-17 16:38:03 -080059 bus(sdbusplus::get_mocked_new(&sdbusMock)), passwordString("password"),
60 password(passwordString.begin(), passwordString.end())
61 {}
62
63 void SetUp() override
64 {
65 /* Create an empty file that we'll pretend is a 'storage device'. */
66 testFile.open(testFileName,
67 std::ios::out | std::ios::binary | std::ios::trunc);
68 testFile.close();
69 if (testFile.fail())
70 {
71 throw std::runtime_error("Failed to open test file");
72 }
73
74 EXPECT_CALL(sdbusMock,
Ed Tanous82897c32022-02-21 14:11:59 -080075 sd_bus_add_object_vtable(IsNull(), _, StrEq(testPath),
76 StrEq(estoragedInterface), _, _))
John Wedigb810c922021-11-17 16:38:03 -080077 .WillRepeatedly(Return(0));
78
79 EXPECT_CALL(sdbusMock,
John Edward Broadbent86dfb242022-03-14 11:04:36 -070080 sd_bus_add_object_vtable(IsNull(), _, StrEq(testPath),
81 StrEq(driveInterface), _, _))
82 .WillRepeatedly(Return(0));
83
84 EXPECT_CALL(sdbusMock,
Ed Tanous82897c32022-02-21 14:11:59 -080085 sd_bus_emit_object_added(IsNull(), StrEq(testPath)))
John Wedigb810c922021-11-17 16:38:03 -080086 .WillRepeatedly(Return(0));
87
88 EXPECT_CALL(sdbusMock,
Ed Tanous82897c32022-02-21 14:11:59 -080089 sd_bus_emit_object_removed(IsNull(), StrEq(testPath)))
John Wedigb810c922021-11-17 16:38:03 -080090 .WillRepeatedly(Return(0));
91
92 std::unique_ptr<MockCryptsetupInterface> cryptIface =
93 std::make_unique<MockCryptsetupInterface>();
94 mockCryptIface = cryptIface.get();
95 std::unique_ptr<MockFilesystemInterface> fsIface =
96 std::make_unique<MockFilesystemInterface>();
97 mockFsIface = fsIface.get();
98
Ed Tanous82897c32022-02-21 14:11:59 -080099 esObject = std::make_unique<estoraged::EStoraged>(
100 bus, testPath, testFileName, testLuksDevName, std::move(cryptIface),
John Wedigb810c922021-11-17 16:38:03 -0800101 std::move(fsIface));
102 }
103
104 void TearDown() override
105 {
106 EXPECT_EQ(0, unlink(testFileName));
107 }
108};
109
110/* Test case to format and then lock the LUKS device. */
Ed Tanous82897c32022-02-21 14:11:59 -0800111TEST_F(EStoragedTest, FormatPass)
John Wedigb810c922021-11-17 16:38:03 -0800112{
113 EXPECT_CALL(sdbusMock,
John Edward Broadbent86dfb242022-03-14 11:04:36 -0700114 sd_bus_emit_properties_changed_strv(IsNull(), StrEq(testPath),
115 StrEq(driveInterface), _))
116 .WillRepeatedly(Return(0));
117
118 EXPECT_CALL(sdbusMock,
John Wedigb810c922021-11-17 16:38:03 -0800119 sd_bus_emit_properties_changed_strv(
Ed Tanous82897c32022-02-21 14:11:59 -0800120 IsNull(), StrEq(testPath), StrEq(estoragedInterface), _))
John Wedigb810c922021-11-17 16:38:03 -0800121 .WillRepeatedly(Return(0));
122
123 EXPECT_CALL(*mockCryptIface, cryptFormat(_, _, _, _, _, _, _, _)).Times(1);
124
125 EXPECT_CALL(*mockCryptIface, cryptKeyslotAddByVolumeKey(_, _, _, _, _, _))
126 .Times(1);
127
128 EXPECT_CALL(*mockCryptIface, cryptLoad(_, _, _)).Times(1);
129
130 EXPECT_CALL(*mockCryptIface, cryptActivateByPassphrase(_, _, _, _, _, _))
131 .Times(1);
132
133 EXPECT_CALL(*mockFsIface, runMkfs(testLuksDevName)).WillOnce(Return(0));
134
John Wedigb17f8252022-01-12 14:24:26 -0800135 EXPECT_CALL(*mockFsIface, directoryExists(path(esObject->getMountPoint())))
136 .WillOnce(Return(false));
137
John Wedigb810c922021-11-17 16:38:03 -0800138 EXPECT_CALL(*mockFsIface, createDirectory(path(esObject->getMountPoint())))
139 .WillOnce(Return(true));
140
141 EXPECT_CALL(*mockFsIface,
142 doMount(ContainsRegex("/dev/mapper/"),
143 StrEq(esObject->getMountPoint()), _, _, _))
144 .WillOnce(Return(0));
145
146 EXPECT_CALL(*mockFsIface, doUnmount(StrEq(esObject->getMountPoint())))
147 .WillOnce(Return(0));
148
149 EXPECT_CALL(*mockFsIface, removeDirectory(path(esObject->getMountPoint())))
150 .WillOnce(Return(true));
151
152 EXPECT_CALL(*mockCryptIface, cryptDeactivate(_, _)).Times(1);
153
154 /* Format the encrypted device. */
John Wedig972c3fa2021-12-29 17:30:41 -0800155 esObject->formatLuks(password, Volume::FilesystemType::ext4);
John Wedigb810c922021-11-17 16:38:03 -0800156 EXPECT_FALSE(esObject->isLocked());
157
John Wedig972c3fa2021-12-29 17:30:41 -0800158 esObject->lock();
John Wedigb810c922021-11-17 16:38:03 -0800159 EXPECT_TRUE(esObject->isLocked());
160}
161
John Wedigb17f8252022-01-12 14:24:26 -0800162/*
163 * Test case where the mount point directory already exists, so it shouldn't
164 * try to create it.
165 */
Ed Tanous82897c32022-02-21 14:11:59 -0800166TEST_F(EStoragedTest, MountPointExistsPass)
John Wedigb17f8252022-01-12 14:24:26 -0800167{
168 EXPECT_CALL(sdbusMock,
John Edward Broadbent86dfb242022-03-14 11:04:36 -0700169 sd_bus_emit_properties_changed_strv(IsNull(), StrEq(testPath),
170 StrEq(driveInterface), _))
171 .WillRepeatedly(Return(0));
172
173 EXPECT_CALL(sdbusMock,
John Wedigb17f8252022-01-12 14:24:26 -0800174 sd_bus_emit_properties_changed_strv(
Ed Tanous82897c32022-02-21 14:11:59 -0800175 IsNull(), StrEq(testPath), StrEq(estoragedInterface), _))
John Wedigb17f8252022-01-12 14:24:26 -0800176 .WillRepeatedly(Return(0));
177
178 EXPECT_CALL(*mockCryptIface, cryptFormat(_, _, _, _, _, _, _, _)).Times(1);
179
180 EXPECT_CALL(*mockCryptIface, cryptKeyslotAddByVolumeKey(_, _, _, _, _, _))
181 .Times(1);
182
183 EXPECT_CALL(*mockCryptIface, cryptLoad(_, _, _)).Times(1);
184
185 EXPECT_CALL(*mockCryptIface, cryptActivateByPassphrase(_, _, _, _, _, _))
186 .Times(1);
187
188 EXPECT_CALL(*mockFsIface, runMkfs(testLuksDevName)).WillOnce(Return(0));
189
190 EXPECT_CALL(*mockFsIface, directoryExists(path(esObject->getMountPoint())))
191 .WillOnce(Return(true));
192
193 EXPECT_CALL(*mockFsIface, createDirectory(path(esObject->getMountPoint())))
194 .Times(0);
195
196 EXPECT_CALL(*mockFsIface,
197 doMount(ContainsRegex("/dev/mapper/"),
198 StrEq(esObject->getMountPoint()), _, _, _))
199 .WillOnce(Return(0));
200
201 EXPECT_CALL(*mockFsIface, doUnmount(StrEq(esObject->getMountPoint())))
202 .WillOnce(Return(0));
203
204 EXPECT_CALL(*mockFsIface, removeDirectory(path(esObject->getMountPoint())))
205 .WillOnce(Return(true));
206
207 EXPECT_CALL(*mockCryptIface, cryptDeactivate(_, _)).Times(1);
208
209 /* Format the encrypted device. */
210 esObject->formatLuks(password, Volume::FilesystemType::ext4);
211 EXPECT_FALSE(esObject->isLocked());
212
213 esObject->lock();
214 EXPECT_TRUE(esObject->isLocked());
215}
216
John Wedigb810c922021-11-17 16:38:03 -0800217/* Test case where the device/file doesn't exist. */
Ed Tanous82897c32022-02-21 14:11:59 -0800218TEST_F(EStoragedTest, FormatNoDeviceFail)
John Wedigb810c922021-11-17 16:38:03 -0800219{
220 /* Delete the test file. */
221 EXPECT_EQ(0, unlink(testFileName));
222
John Wedig972c3fa2021-12-29 17:30:41 -0800223 EXPECT_THROW(esObject->formatLuks(password, Volume::FilesystemType::ext4),
224 ResourceNotFound);
John Wedigb810c922021-11-17 16:38:03 -0800225 EXPECT_FALSE(esObject->isLocked());
226
227 /* Create the test file again, so that the TearDown function works. */
228 testFile.open(testFileName,
229 std::ios::out | std::ios::binary | std::ios::trunc);
230 testFile.close();
231}
232
233/* Test case where we fail to format the LUKS device. */
Ed Tanous82897c32022-02-21 14:11:59 -0800234TEST_F(EStoragedTest, FormatFail)
John Wedigb810c922021-11-17 16:38:03 -0800235{
236 EXPECT_CALL(*mockCryptIface, cryptFormat(_, _, _, _, _, _, _, _))
237 .WillOnce(Return(-1));
238
John Wedig972c3fa2021-12-29 17:30:41 -0800239 EXPECT_THROW(esObject->formatLuks(password, Volume::FilesystemType::ext4),
240 InternalFailure);
John Wedigb810c922021-11-17 16:38:03 -0800241 EXPECT_FALSE(esObject->isLocked());
242}
243
244/* Test case where we fail to set the password for the LUKS device. */
Ed Tanous82897c32022-02-21 14:11:59 -0800245TEST_F(EStoragedTest, AddKeyslotFail)
John Wedigb810c922021-11-17 16:38:03 -0800246{
247 EXPECT_CALL(sdbusMock,
John Edward Broadbent86dfb242022-03-14 11:04:36 -0700248 sd_bus_emit_properties_changed_strv(IsNull(), StrEq(testPath),
249 StrEq(driveInterface), _))
250 .WillRepeatedly(Return(0));
251
252 EXPECT_CALL(sdbusMock,
John Wedigb810c922021-11-17 16:38:03 -0800253 sd_bus_emit_properties_changed_strv(
Ed Tanous82897c32022-02-21 14:11:59 -0800254 IsNull(), StrEq(testPath), StrEq(estoragedInterface), _))
John Wedigb810c922021-11-17 16:38:03 -0800255 .WillRepeatedly(Return(0));
256
257 EXPECT_CALL(*mockCryptIface, cryptFormat(_, _, _, _, _, _, _, _)).Times(1);
258
259 EXPECT_CALL(*mockCryptIface, cryptKeyslotAddByVolumeKey(_, _, _, _, _, _))
260 .WillOnce(Return(-1));
261
John Wedig972c3fa2021-12-29 17:30:41 -0800262 EXPECT_THROW(esObject->formatLuks(password, Volume::FilesystemType::ext4),
263 InternalFailure);
John Wedigb810c922021-11-17 16:38:03 -0800264 EXPECT_TRUE(esObject->isLocked());
265}
266
267/* Test case where we fail to load the LUKS header. */
Ed Tanous82897c32022-02-21 14:11:59 -0800268TEST_F(EStoragedTest, LoadLuksHeaderFail)
John Wedigb810c922021-11-17 16:38:03 -0800269{
270 EXPECT_CALL(sdbusMock,
John Edward Broadbent86dfb242022-03-14 11:04:36 -0700271 sd_bus_emit_properties_changed_strv(IsNull(), StrEq(testPath),
272 StrEq(driveInterface), _))
273 .WillRepeatedly(Return(0));
274
275 EXPECT_CALL(sdbusMock,
John Wedigb810c922021-11-17 16:38:03 -0800276 sd_bus_emit_properties_changed_strv(
Ed Tanous82897c32022-02-21 14:11:59 -0800277 IsNull(), StrEq(testPath), StrEq(estoragedInterface), _))
John Wedigb810c922021-11-17 16:38:03 -0800278 .WillRepeatedly(Return(0));
279
280 EXPECT_CALL(*mockCryptIface, cryptFormat(_, _, _, _, _, _, _, _)).Times(1);
281
282 EXPECT_CALL(*mockCryptIface, cryptKeyslotAddByVolumeKey(_, _, _, _, _, _))
283 .Times(1);
284
285 EXPECT_CALL(*mockCryptIface, cryptLoad(_, _, _)).WillOnce(Return(-1));
286
John Wedig972c3fa2021-12-29 17:30:41 -0800287 EXPECT_THROW(esObject->formatLuks(password, Volume::FilesystemType::ext4),
288 InternalFailure);
John Wedigb810c922021-11-17 16:38:03 -0800289 EXPECT_TRUE(esObject->isLocked());
290}
291
292/* Test case where we fail to activate the LUKS device. */
Ed Tanous82897c32022-02-21 14:11:59 -0800293TEST_F(EStoragedTest, ActivateFail)
John Wedigb810c922021-11-17 16:38:03 -0800294{
295 EXPECT_CALL(sdbusMock,
John Edward Broadbent86dfb242022-03-14 11:04:36 -0700296 sd_bus_emit_properties_changed_strv(IsNull(), StrEq(testPath),
297 StrEq(driveInterface), _))
298 .WillRepeatedly(Return(0));
299
300 EXPECT_CALL(sdbusMock,
John Wedigb810c922021-11-17 16:38:03 -0800301 sd_bus_emit_properties_changed_strv(
Ed Tanous82897c32022-02-21 14:11:59 -0800302 IsNull(), StrEq(testPath), StrEq(estoragedInterface), _))
John Wedigb810c922021-11-17 16:38:03 -0800303 .WillRepeatedly(Return(0));
304
305 EXPECT_CALL(*mockCryptIface, cryptFormat(_, _, _, _, _, _, _, _)).Times(1);
306
307 EXPECT_CALL(*mockCryptIface, cryptKeyslotAddByVolumeKey(_, _, _, _, _, _))
308 .Times(1);
309
310 EXPECT_CALL(*mockCryptIface, cryptLoad(_, _, _)).Times(1);
311
312 EXPECT_CALL(*mockCryptIface, cryptActivateByPassphrase(_, _, _, _, _, _))
313 .WillOnce(Return(-1));
314
John Wedig972c3fa2021-12-29 17:30:41 -0800315 EXPECT_THROW(esObject->formatLuks(password, Volume::FilesystemType::ext4),
316 InternalFailure);
John Wedigb810c922021-11-17 16:38:03 -0800317 EXPECT_TRUE(esObject->isLocked());
318}
319
320/* Test case where we fail to create the filesystem. */
Ed Tanous82897c32022-02-21 14:11:59 -0800321TEST_F(EStoragedTest, CreateFilesystemFail)
John Wedigb810c922021-11-17 16:38:03 -0800322{
323 EXPECT_CALL(sdbusMock,
John Edward Broadbent86dfb242022-03-14 11:04:36 -0700324 sd_bus_emit_properties_changed_strv(IsNull(), StrEq(testPath),
325 StrEq(driveInterface), _))
326 .WillRepeatedly(Return(0));
327
328 EXPECT_CALL(sdbusMock,
John Wedigb810c922021-11-17 16:38:03 -0800329 sd_bus_emit_properties_changed_strv(
Ed Tanous82897c32022-02-21 14:11:59 -0800330 IsNull(), StrEq(testPath), StrEq(estoragedInterface), _))
John Wedigb810c922021-11-17 16:38:03 -0800331 .WillRepeatedly(Return(0));
332
333 EXPECT_CALL(*mockCryptIface, cryptFormat(_, _, _, _, _, _, _, _)).Times(1);
334
335 EXPECT_CALL(*mockCryptIface, cryptKeyslotAddByVolumeKey(_, _, _, _, _, _))
336 .Times(1);
337
338 EXPECT_CALL(*mockCryptIface, cryptLoad(_, _, _)).Times(1);
339
340 EXPECT_CALL(*mockCryptIface, cryptActivateByPassphrase(_, _, _, _, _, _))
341 .Times(1);
342
343 EXPECT_CALL(*mockFsIface, runMkfs(testLuksDevName)).WillOnce(Return(-1));
344
John Wedig972c3fa2021-12-29 17:30:41 -0800345 EXPECT_THROW(esObject->formatLuks(password, Volume::FilesystemType::ext4),
346 InternalFailure);
John Wedigb810c922021-11-17 16:38:03 -0800347 EXPECT_FALSE(esObject->isLocked());
348}
349
350/* Test case where we fail to create the mount point. */
Ed Tanous82897c32022-02-21 14:11:59 -0800351TEST_F(EStoragedTest, CreateMountPointFail)
John Wedigb810c922021-11-17 16:38:03 -0800352{
353 EXPECT_CALL(sdbusMock,
John Edward Broadbent86dfb242022-03-14 11:04:36 -0700354 sd_bus_emit_properties_changed_strv(IsNull(), StrEq(testPath),
355 StrEq(driveInterface), _))
356 .WillRepeatedly(Return(0));
357
358 EXPECT_CALL(sdbusMock,
John Wedigb810c922021-11-17 16:38:03 -0800359 sd_bus_emit_properties_changed_strv(
Ed Tanous82897c32022-02-21 14:11:59 -0800360 IsNull(), StrEq(testPath), StrEq(estoragedInterface), _))
John Wedigb810c922021-11-17 16:38:03 -0800361 .WillRepeatedly(Return(0));
362
363 EXPECT_CALL(*mockCryptIface, cryptFormat(_, _, _, _, _, _, _, _)).Times(1);
364
365 EXPECT_CALL(*mockCryptIface, cryptKeyslotAddByVolumeKey(_, _, _, _, _, _))
366 .Times(1);
367
368 EXPECT_CALL(*mockCryptIface, cryptLoad(_, _, _)).Times(1);
369
370 EXPECT_CALL(*mockCryptIface, cryptActivateByPassphrase(_, _, _, _, _, _))
371 .Times(1);
372
373 EXPECT_CALL(*mockFsIface, runMkfs(testLuksDevName)).WillOnce(Return(0));
374
John Wedigb17f8252022-01-12 14:24:26 -0800375 EXPECT_CALL(*mockFsIface, directoryExists(path(esObject->getMountPoint())))
376 .WillOnce(Return(false));
377
John Wedigb810c922021-11-17 16:38:03 -0800378 EXPECT_CALL(*mockFsIface, createDirectory(path(esObject->getMountPoint())))
379 .WillOnce(Return(false));
380
John Wedig972c3fa2021-12-29 17:30:41 -0800381 EXPECT_THROW(esObject->formatLuks(password, Volume::FilesystemType::ext4),
382 InternalFailure);
John Wedigb810c922021-11-17 16:38:03 -0800383 EXPECT_FALSE(esObject->isLocked());
384}
385
386/* Test case where we fail to mount the filesystem. */
Ed Tanous82897c32022-02-21 14:11:59 -0800387TEST_F(EStoragedTest, MountFail)
John Wedigb810c922021-11-17 16:38:03 -0800388{
389 EXPECT_CALL(sdbusMock,
John Edward Broadbent86dfb242022-03-14 11:04:36 -0700390 sd_bus_emit_properties_changed_strv(IsNull(), StrEq(testPath),
391 StrEq(driveInterface), _))
392 .WillRepeatedly(Return(0));
393
394 EXPECT_CALL(sdbusMock,
John Wedigb810c922021-11-17 16:38:03 -0800395 sd_bus_emit_properties_changed_strv(
Ed Tanous82897c32022-02-21 14:11:59 -0800396 IsNull(), StrEq(testPath), StrEq(estoragedInterface), _))
John Wedigb810c922021-11-17 16:38:03 -0800397 .WillRepeatedly(Return(0));
398
399 EXPECT_CALL(*mockCryptIface, cryptFormat(_, _, _, _, _, _, _, _)).Times(1);
400
401 EXPECT_CALL(*mockCryptIface, cryptKeyslotAddByVolumeKey(_, _, _, _, _, _))
402 .Times(1);
403
404 EXPECT_CALL(*mockCryptIface, cryptLoad(_, _, _)).Times(1);
405
406 EXPECT_CALL(*mockCryptIface, cryptActivateByPassphrase(_, _, _, _, _, _))
407 .Times(1);
408
409 EXPECT_CALL(*mockFsIface, runMkfs(testLuksDevName)).WillOnce(Return(0));
410
John Wedigb17f8252022-01-12 14:24:26 -0800411 EXPECT_CALL(*mockFsIface, directoryExists(path(esObject->getMountPoint())))
412 .WillOnce(Return(false));
413
John Wedigb810c922021-11-17 16:38:03 -0800414 EXPECT_CALL(*mockFsIface, createDirectory(path(esObject->getMountPoint())))
415 .WillOnce(Return(true));
416
417 EXPECT_CALL(*mockFsIface,
418 doMount(ContainsRegex("/dev/mapper/"),
419 StrEq(esObject->getMountPoint()), _, _, _))
420 .WillOnce(Return(-1));
421
422 EXPECT_CALL(*mockFsIface, removeDirectory(path(esObject->getMountPoint())))
423 .WillOnce(Return(true));
424
John Wedig972c3fa2021-12-29 17:30:41 -0800425 EXPECT_THROW(esObject->formatLuks(password, Volume::FilesystemType::ext4),
426 InternalFailure);
John Wedigb810c922021-11-17 16:38:03 -0800427 EXPECT_FALSE(esObject->isLocked());
428}
429
430/* Test case where we fail to unmount the filesystem. */
Ed Tanous82897c32022-02-21 14:11:59 -0800431TEST_F(EStoragedTest, UnmountFail)
John Wedigb810c922021-11-17 16:38:03 -0800432{
433 EXPECT_CALL(sdbusMock,
John Edward Broadbent86dfb242022-03-14 11:04:36 -0700434 sd_bus_emit_properties_changed_strv(IsNull(), StrEq(testPath),
435 StrEq(driveInterface), _))
436 .WillRepeatedly(Return(0));
437
438 EXPECT_CALL(sdbusMock,
John Wedigb810c922021-11-17 16:38:03 -0800439 sd_bus_emit_properties_changed_strv(
Ed Tanous82897c32022-02-21 14:11:59 -0800440 IsNull(), StrEq(testPath), StrEq(estoragedInterface), _))
John Wedigb810c922021-11-17 16:38:03 -0800441 .WillRepeatedly(Return(0));
442
443 EXPECT_CALL(*mockCryptIface, cryptFormat(_, _, _, _, _, _, _, _)).Times(1);
444
445 EXPECT_CALL(*mockCryptIface, cryptKeyslotAddByVolumeKey(_, _, _, _, _, _))
446 .Times(1);
447
448 EXPECT_CALL(*mockCryptIface, cryptLoad(_, _, _)).Times(1);
449
450 EXPECT_CALL(*mockCryptIface, cryptActivateByPassphrase(_, _, _, _, _, _))
451 .Times(1);
452
453 EXPECT_CALL(*mockFsIface, runMkfs(testLuksDevName)).WillOnce(Return(0));
454
John Wedigb17f8252022-01-12 14:24:26 -0800455 EXPECT_CALL(*mockFsIface, directoryExists(path(esObject->getMountPoint())))
456 .WillOnce(Return(false));
457
John Wedigb810c922021-11-17 16:38:03 -0800458 EXPECT_CALL(*mockFsIface, createDirectory(path(esObject->getMountPoint())))
459 .WillOnce(Return(true));
460
461 EXPECT_CALL(*mockFsIface,
462 doMount(ContainsRegex("/dev/mapper/"),
463 StrEq(esObject->getMountPoint()), _, _, _))
464 .WillOnce(Return(0));
465
466 EXPECT_CALL(*mockFsIface, doUnmount(StrEq(esObject->getMountPoint())))
467 .WillOnce(Return(-1));
468
John Wedig972c3fa2021-12-29 17:30:41 -0800469 esObject->formatLuks(password, Volume::FilesystemType::ext4);
John Wedigb810c922021-11-17 16:38:03 -0800470 EXPECT_FALSE(esObject->isLocked());
471
John Wedig972c3fa2021-12-29 17:30:41 -0800472 EXPECT_THROW(esObject->lock(), InternalFailure);
John Wedigb810c922021-11-17 16:38:03 -0800473 EXPECT_FALSE(esObject->isLocked());
474}
475
476/* Test case where we fail to remove the mount point. */
Ed Tanous82897c32022-02-21 14:11:59 -0800477TEST_F(EStoragedTest, RemoveMountPointFail)
John Wedigb810c922021-11-17 16:38:03 -0800478{
479 EXPECT_CALL(sdbusMock,
John Edward Broadbent86dfb242022-03-14 11:04:36 -0700480 sd_bus_emit_properties_changed_strv(IsNull(), StrEq(testPath),
481 StrEq(driveInterface), _))
482 .WillRepeatedly(Return(0));
483
484 EXPECT_CALL(sdbusMock,
John Wedigb810c922021-11-17 16:38:03 -0800485 sd_bus_emit_properties_changed_strv(
Ed Tanous82897c32022-02-21 14:11:59 -0800486 IsNull(), StrEq(testPath), StrEq(estoragedInterface), _))
John Wedigb810c922021-11-17 16:38:03 -0800487 .WillRepeatedly(Return(0));
488
489 EXPECT_CALL(*mockCryptIface, cryptFormat(_, _, _, _, _, _, _, _)).Times(1);
490
491 EXPECT_CALL(*mockCryptIface, cryptKeyslotAddByVolumeKey(_, _, _, _, _, _))
492 .Times(1);
493
494 EXPECT_CALL(*mockCryptIface, cryptLoad(_, _, _)).Times(1);
495
496 EXPECT_CALL(*mockCryptIface, cryptActivateByPassphrase(_, _, _, _, _, _))
497 .Times(1);
498
499 EXPECT_CALL(*mockFsIface, runMkfs(testLuksDevName)).WillOnce(Return(0));
500
John Wedigb17f8252022-01-12 14:24:26 -0800501 EXPECT_CALL(*mockFsIface, directoryExists(path(esObject->getMountPoint())))
502 .WillOnce(Return(false));
503
John Wedigb810c922021-11-17 16:38:03 -0800504 EXPECT_CALL(*mockFsIface, createDirectory(path(esObject->getMountPoint())))
505 .WillOnce(Return(true));
506
507 EXPECT_CALL(*mockFsIface,
508 doMount(ContainsRegex("/dev/mapper/"),
509 StrEq(esObject->getMountPoint()), _, _, _))
510 .WillOnce(Return(0));
511
512 EXPECT_CALL(*mockFsIface, doUnmount(StrEq(esObject->getMountPoint())))
513 .WillOnce(Return(0));
514
515 EXPECT_CALL(*mockFsIface, removeDirectory(path(esObject->getMountPoint())))
516 .WillOnce(Return(false));
517
John Wedig972c3fa2021-12-29 17:30:41 -0800518 esObject->formatLuks(password, Volume::FilesystemType::ext4);
John Wedigb810c922021-11-17 16:38:03 -0800519 EXPECT_FALSE(esObject->isLocked());
520
521 /* This will fail to remove the mount point. */
John Wedig972c3fa2021-12-29 17:30:41 -0800522 EXPECT_THROW(esObject->lock(), InternalFailure);
John Wedigb810c922021-11-17 16:38:03 -0800523 EXPECT_FALSE(esObject->isLocked());
524}
525
526/* Test case where we fail to deactivate the LUKS device. */
Ed Tanous82897c32022-02-21 14:11:59 -0800527TEST_F(EStoragedTest, DeactivateFail)
John Wedigb810c922021-11-17 16:38:03 -0800528{
529 EXPECT_CALL(sdbusMock,
John Edward Broadbent86dfb242022-03-14 11:04:36 -0700530 sd_bus_emit_properties_changed_strv(IsNull(), StrEq(testPath),
531 StrEq(driveInterface), _))
532 .WillRepeatedly(Return(0));
533
534 EXPECT_CALL(sdbusMock,
John Wedigb810c922021-11-17 16:38:03 -0800535 sd_bus_emit_properties_changed_strv(
Ed Tanous82897c32022-02-21 14:11:59 -0800536 IsNull(), StrEq(testPath), StrEq(estoragedInterface), _))
John Wedigb810c922021-11-17 16:38:03 -0800537 .WillRepeatedly(Return(0));
538
539 EXPECT_CALL(*mockCryptIface, cryptFormat(_, _, _, _, _, _, _, _)).Times(1);
540
541 EXPECT_CALL(*mockCryptIface, cryptKeyslotAddByVolumeKey(_, _, _, _, _, _))
542 .Times(1);
543
544 EXPECT_CALL(*mockCryptIface, cryptLoad(_, _, _)).Times(1);
545
546 EXPECT_CALL(*mockCryptIface, cryptActivateByPassphrase(_, _, _, _, _, _))
547 .Times(1);
548
549 EXPECT_CALL(*mockFsIface, runMkfs(testLuksDevName)).WillOnce(Return(0));
550
John Wedigb17f8252022-01-12 14:24:26 -0800551 EXPECT_CALL(*mockFsIface, directoryExists(path(esObject->getMountPoint())))
552 .WillOnce(Return(false));
553
John Wedigb810c922021-11-17 16:38:03 -0800554 EXPECT_CALL(*mockFsIface, createDirectory(path(esObject->getMountPoint())))
555 .WillOnce(Return(true));
556
557 EXPECT_CALL(*mockFsIface,
558 doMount(ContainsRegex("/dev/mapper/"),
559 StrEq(esObject->getMountPoint()), _, _, _))
560 .WillOnce(Return(0));
561
562 EXPECT_CALL(*mockFsIface, doUnmount(StrEq(esObject->getMountPoint())))
563 .WillOnce(Return(0));
564
565 EXPECT_CALL(*mockFsIface, removeDirectory(path(esObject->getMountPoint())))
566 .WillOnce(Return(true));
567
568 EXPECT_CALL(*mockCryptIface, cryptDeactivate(_, _)).WillOnce(Return(-1));
569
570 /* Format the encrypted device. */
John Wedig972c3fa2021-12-29 17:30:41 -0800571 esObject->formatLuks(password, Volume::FilesystemType::ext4);
John Wedigb810c922021-11-17 16:38:03 -0800572 EXPECT_FALSE(esObject->isLocked());
573
John Wedig972c3fa2021-12-29 17:30:41 -0800574 EXPECT_THROW(esObject->lock(), InternalFailure);
John Wedigb810c922021-11-17 16:38:03 -0800575 EXPECT_FALSE(esObject->isLocked());
576}
577
578} // namespace estoraged_test