blob: cef6e2163985ee62df84b1b45d240ea427aa555d [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"
Tom Tung043af592023-11-24 13:37:05 +08004#include "estoraged_conf.hpp"
John Wedigb810c922021-11-17 16:38:03 -08005
6#include <unistd.h>
7
John Wedig67a47442022-04-05 17:21:29 -07008#include <boost/asio/io_context.hpp>
9#include <sdbusplus/asio/connection.hpp>
10#include <sdbusplus/asio/object_server.hpp>
John Wedig972c3fa2021-12-29 17:30:41 -080011#include <xyz/openbmc_project/Common/error.hpp>
Patrick Williams0e2a46f2023-05-10 14:35:52 -050012#include <xyz/openbmc_project/Inventory/Item/Volume/server.hpp>
John Wedigb810c922021-11-17 16:38:03 -080013
14#include <exception>
15#include <filesystem>
16#include <fstream>
17#include <iterator>
John Wedig67a47442022-04-05 17:21:29 -070018#include <memory>
John Wedigb810c922021-11-17 16:38:03 -080019#include <string>
20#include <vector>
21
22#include <gmock/gmock.h>
23#include <gtest/gtest.h>
24
25namespace estoraged_test
26{
27
Patrick Williams0e2a46f2023-05-10 14:35:52 -050028using sdbusplus::server::xyz::openbmc_project::inventory::item::Volume;
John Wedig972c3fa2021-12-29 17:30:41 -080029using sdbusplus::xyz::openbmc_project::Common::Error::InternalFailure;
30using sdbusplus::xyz::openbmc_project::Common::Error::ResourceNotFound;
John Wedigb810c922021-11-17 16:38:03 -080031using std::filesystem::path;
32using ::testing::_;
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 Wedig2443a022023-03-17 13:42:32 -070041 const char* testCryptDir = "/tmp";
John Wedig6c0d8ce2022-04-22 14:00:43 -070042 const std::string testConfigPath =
43 "/xyz/openbmc_project/inventory/system/board/test_board/test_emmc";
John Wedig67a47442022-04-05 17:21:29 -070044 const uint64_t testSize = 24;
John Edward Broadbent5d799bb2022-03-22 16:14:24 -070045 const uint8_t testLifeTime = 25;
John Wedigb4838302022-07-22 13:51:16 -070046 const std::string testPartNumber = "12345678";
47 const std::string testSerialNumber = "ABCDEF1234";
Rahul Kapoor19825052023-05-27 01:52:23 +000048 const std::string testLocationCode = "U102020";
John Wedigd7be42b2024-01-19 16:07:19 -080049 const std::string testDriveType = "SSD";
John Wedigc0d66eb2024-02-26 15:54:47 -080050 const std::string testDriveProtocol = "eMMC";
John Wedigb810c922021-11-17 16:38:03 -080051 std::ofstream testFile;
John Wedigb810c922021-11-17 16:38:03 -080052 std::string passwordString;
53 std::vector<uint8_t> password;
Ed Tanous82897c32022-02-21 14:11:59 -080054 MockCryptsetupInterface* mockCryptIface{};
55 MockFilesystemInterface* mockFsIface{};
John Wedig67a47442022-04-05 17:21:29 -070056 boost::asio::io_context io;
57 std::shared_ptr<sdbusplus::asio::connection> conn;
58 std::unique_ptr<sdbusplus::asio::object_server> objectServer;
59 std::unique_ptr<estoraged::EStoraged> esObject;
John Wedigb810c922021-11-17 16:38:03 -080060
Ed Tanous82897c32022-02-21 14:11:59 -080061 EStoragedTest() :
John Wedig67a47442022-04-05 17:21:29 -070062 passwordString("password"),
John Wedigb810c922021-11-17 16:38:03 -080063 password(passwordString.begin(), passwordString.end())
64 {}
65
66 void SetUp() override
67 {
68 /* Create an empty file that we'll pretend is a 'storage device'. */
69 testFile.open(testFileName,
70 std::ios::out | std::ios::binary | std::ios::trunc);
71 testFile.close();
72 if (testFile.fail())
73 {
74 throw std::runtime_error("Failed to open test file");
75 }
76
John Wedigb810c922021-11-17 16:38:03 -080077 std::unique_ptr<MockCryptsetupInterface> cryptIface =
78 std::make_unique<MockCryptsetupInterface>();
79 mockCryptIface = cryptIface.get();
80 std::unique_ptr<MockFilesystemInterface> fsIface =
81 std::make_unique<MockFilesystemInterface>();
82 mockFsIface = fsIface.get();
83
John Wedig2443a022023-03-17 13:42:32 -070084 /* Set up location of dummy mapped crypt file. */
85 EXPECT_CALL(*cryptIface, cryptGetDir).WillOnce(Return(testCryptDir));
86
John Wedig67a47442022-04-05 17:21:29 -070087 conn = std::make_shared<sdbusplus::asio::connection>(io);
88 // request D-Bus server name.
89 conn->request_name("xyz.openbmc_project.eStoraged.test");
90 objectServer = std::make_unique<sdbusplus::asio::object_server>(conn);
91
Ed Tanous82897c32022-02-21 14:11:59 -080092 esObject = std::make_unique<estoraged::EStoraged>(
John Wedig6c0d8ce2022-04-22 14:00:43 -070093 *objectServer, testConfigPath, testFileName, testLuksDevName,
John Wedigb4838302022-07-22 13:51:16 -070094 testSize, testLifeTime, testPartNumber, testSerialNumber,
Tom Tung043af592023-11-24 13:37:05 +080095 testLocationCode, ERASE_MAX_GEOMETRY, ERASE_MIN_GEOMETRY,
John Wedigc0d66eb2024-02-26 15:54:47 -080096 testDriveType, testDriveProtocol, std::move(cryptIface),
97 std::move(fsIface));
John Wedigb810c922021-11-17 16:38:03 -080098 }
99
100 void TearDown() override
101 {
102 EXPECT_EQ(0, unlink(testFileName));
103 }
104};
105
John Wedig2443a022023-03-17 13:42:32 -0700106const char* mappedDevicePath = "/tmp/testfile_luksDev";
107std::ofstream mappedDevice;
108
109int createMappedDev()
110{
111 mappedDevice.open(mappedDevicePath,
112 std::ios::out | std::ios::binary | std::ios::trunc);
113 mappedDevice.close();
114 if (mappedDevice.fail())
115 {
116 throw std::runtime_error("Failed to open test mapped device");
117 }
118
119 return 0;
120}
121
122int removeMappedDev()
123{
124 EXPECT_EQ(0, unlink(mappedDevicePath));
125
126 return 0;
127}
128
John Wedigb810c922021-11-17 16:38:03 -0800129/* Test case to format and then lock the LUKS device. */
Ed Tanous82897c32022-02-21 14:11:59 -0800130TEST_F(EStoragedTest, FormatPass)
John Wedigb810c922021-11-17 16:38:03 -0800131{
John Wedigb810c922021-11-17 16:38:03 -0800132 EXPECT_CALL(*mockCryptIface, cryptFormat(_, _, _, _, _, _, _, _)).Times(1);
133
134 EXPECT_CALL(*mockCryptIface, cryptKeyslotAddByVolumeKey(_, _, _, _, _, _))
135 .Times(1);
136
137 EXPECT_CALL(*mockCryptIface, cryptLoad(_, _, _)).Times(1);
138
139 EXPECT_CALL(*mockCryptIface, cryptActivateByPassphrase(_, _, _, _, _, _))
John Wedig2443a022023-03-17 13:42:32 -0700140 .WillOnce(&createMappedDev);
John Wedigb810c922021-11-17 16:38:03 -0800141
John Wedig2443a022023-03-17 13:42:32 -0700142 EXPECT_CALL(*mockFsIface, runMkfs(StrEq(esObject->getCryptDevicePath())))
143 .WillOnce(Return(0));
John Wedigb810c922021-11-17 16:38:03 -0800144
John Wedig1d6665f2025-12-04 19:08:06 +0000145 EXPECT_CALL(*mockFsIface, runFsck(StrEq(esObject->getCryptDevicePath()),
146 StrEq("-t ext4 -p")))
147 .WillOnce(Return(0));
148
John Wedigb17f8252022-01-12 14:24:26 -0800149 EXPECT_CALL(*mockFsIface, directoryExists(path(esObject->getMountPoint())))
150 .WillOnce(Return(false));
151
John Wedigb810c922021-11-17 16:38:03 -0800152 EXPECT_CALL(*mockFsIface, createDirectory(path(esObject->getMountPoint())))
153 .WillOnce(Return(true));
154
155 EXPECT_CALL(*mockFsIface,
John Wedig2443a022023-03-17 13:42:32 -0700156 doMount(StrEq(esObject->getCryptDevicePath()),
John Wedigb810c922021-11-17 16:38:03 -0800157 StrEq(esObject->getMountPoint()), _, _, _))
158 .WillOnce(Return(0));
159
160 EXPECT_CALL(*mockFsIface, doUnmount(StrEq(esObject->getMountPoint())))
161 .WillOnce(Return(0));
162
163 EXPECT_CALL(*mockFsIface, removeDirectory(path(esObject->getMountPoint())))
164 .WillOnce(Return(true));
165
John Wedig2443a022023-03-17 13:42:32 -0700166 EXPECT_CALL(*mockCryptIface, cryptDeactivate(_, _))
167 .WillOnce(&removeMappedDev);
John Wedigb810c922021-11-17 16:38:03 -0800168
169 /* Format the encrypted device. */
John Wedig972c3fa2021-12-29 17:30:41 -0800170 esObject->formatLuks(password, Volume::FilesystemType::ext4);
John Wedigb810c922021-11-17 16:38:03 -0800171 EXPECT_FALSE(esObject->isLocked());
172
John Wedig972c3fa2021-12-29 17:30:41 -0800173 esObject->lock();
John Wedigb810c922021-11-17 16:38:03 -0800174 EXPECT_TRUE(esObject->isLocked());
175}
176
John Wedigb17f8252022-01-12 14:24:26 -0800177/*
178 * Test case where the mount point directory already exists, so it shouldn't
179 * try to create it.
180 */
Ed Tanous82897c32022-02-21 14:11:59 -0800181TEST_F(EStoragedTest, MountPointExistsPass)
John Wedigb17f8252022-01-12 14:24:26 -0800182{
John Wedigb17f8252022-01-12 14:24:26 -0800183 EXPECT_CALL(*mockCryptIface, cryptFormat(_, _, _, _, _, _, _, _)).Times(1);
184
185 EXPECT_CALL(*mockCryptIface, cryptKeyslotAddByVolumeKey(_, _, _, _, _, _))
186 .Times(1);
187
188 EXPECT_CALL(*mockCryptIface, cryptLoad(_, _, _)).Times(1);
189
190 EXPECT_CALL(*mockCryptIface, cryptActivateByPassphrase(_, _, _, _, _, _))
John Wedig2443a022023-03-17 13:42:32 -0700191 .WillOnce(&createMappedDev);
John Wedigb17f8252022-01-12 14:24:26 -0800192
John Wedig2443a022023-03-17 13:42:32 -0700193 EXPECT_CALL(*mockFsIface, runMkfs(StrEq(esObject->getCryptDevicePath())))
194 .WillOnce(Return(0));
John Wedigb17f8252022-01-12 14:24:26 -0800195
John Wedig1d6665f2025-12-04 19:08:06 +0000196 EXPECT_CALL(*mockFsIface, runFsck(StrEq(esObject->getCryptDevicePath()),
197 StrEq("-t ext4 -p")))
198 .WillOnce(Return(0));
199
John Wedigb17f8252022-01-12 14:24:26 -0800200 EXPECT_CALL(*mockFsIface, directoryExists(path(esObject->getMountPoint())))
201 .WillOnce(Return(true));
202
203 EXPECT_CALL(*mockFsIface, createDirectory(path(esObject->getMountPoint())))
204 .Times(0);
205
206 EXPECT_CALL(*mockFsIface,
John Wedig2443a022023-03-17 13:42:32 -0700207 doMount(StrEq(esObject->getCryptDevicePath()),
John Wedigb17f8252022-01-12 14:24:26 -0800208 StrEq(esObject->getMountPoint()), _, _, _))
209 .WillOnce(Return(0));
210
211 EXPECT_CALL(*mockFsIface, doUnmount(StrEq(esObject->getMountPoint())))
212 .WillOnce(Return(0));
213
214 EXPECT_CALL(*mockFsIface, removeDirectory(path(esObject->getMountPoint())))
215 .WillOnce(Return(true));
216
John Wedig2443a022023-03-17 13:42:32 -0700217 EXPECT_CALL(*mockCryptIface, cryptDeactivate(_, _))
218 .WillOnce(&removeMappedDev);
John Wedigb17f8252022-01-12 14:24:26 -0800219
220 /* Format the encrypted device. */
221 esObject->formatLuks(password, Volume::FilesystemType::ext4);
222 EXPECT_FALSE(esObject->isLocked());
223
224 esObject->lock();
225 EXPECT_TRUE(esObject->isLocked());
226}
227
John Wedigb810c922021-11-17 16:38:03 -0800228/* Test case where the device/file doesn't exist. */
Ed Tanous82897c32022-02-21 14:11:59 -0800229TEST_F(EStoragedTest, FormatNoDeviceFail)
John Wedigb810c922021-11-17 16:38:03 -0800230{
231 /* Delete the test file. */
232 EXPECT_EQ(0, unlink(testFileName));
233
John Wedig972c3fa2021-12-29 17:30:41 -0800234 EXPECT_THROW(esObject->formatLuks(password, Volume::FilesystemType::ext4),
235 ResourceNotFound);
John Wedig2443a022023-03-17 13:42:32 -0700236 EXPECT_TRUE(esObject->isLocked());
John Wedigb810c922021-11-17 16:38:03 -0800237
238 /* Create the test file again, so that the TearDown function works. */
239 testFile.open(testFileName,
240 std::ios::out | std::ios::binary | std::ios::trunc);
241 testFile.close();
242}
243
244/* Test case where we fail to format the LUKS device. */
Ed Tanous82897c32022-02-21 14:11:59 -0800245TEST_F(EStoragedTest, FormatFail)
John Wedigb810c922021-11-17 16:38:03 -0800246{
247 EXPECT_CALL(*mockCryptIface, cryptFormat(_, _, _, _, _, _, _, _))
248 .WillOnce(Return(-1));
249
John Wedig972c3fa2021-12-29 17:30:41 -0800250 EXPECT_THROW(esObject->formatLuks(password, Volume::FilesystemType::ext4),
251 InternalFailure);
John Wedig2443a022023-03-17 13:42:32 -0700252 EXPECT_TRUE(esObject->isLocked());
John Wedigb810c922021-11-17 16:38:03 -0800253}
254
255/* Test case where we fail to set the password for the LUKS device. */
Ed Tanous82897c32022-02-21 14:11:59 -0800256TEST_F(EStoragedTest, AddKeyslotFail)
John Wedigb810c922021-11-17 16:38:03 -0800257{
John Wedigb810c922021-11-17 16:38:03 -0800258 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{
John Wedigb810c922021-11-17 16:38:03 -0800271 EXPECT_CALL(*mockCryptIface, cryptFormat(_, _, _, _, _, _, _, _)).Times(1);
272
273 EXPECT_CALL(*mockCryptIface, cryptKeyslotAddByVolumeKey(_, _, _, _, _, _))
274 .Times(1);
275
276 EXPECT_CALL(*mockCryptIface, cryptLoad(_, _, _)).WillOnce(Return(-1));
277
John Wedig972c3fa2021-12-29 17:30:41 -0800278 EXPECT_THROW(esObject->formatLuks(password, Volume::FilesystemType::ext4),
279 InternalFailure);
John Wedigb810c922021-11-17 16:38:03 -0800280 EXPECT_TRUE(esObject->isLocked());
281}
282
283/* Test case where we fail to activate the LUKS device. */
Ed Tanous82897c32022-02-21 14:11:59 -0800284TEST_F(EStoragedTest, ActivateFail)
John Wedigb810c922021-11-17 16:38:03 -0800285{
John Wedigb810c922021-11-17 16:38:03 -0800286 EXPECT_CALL(*mockCryptIface, cryptFormat(_, _, _, _, _, _, _, _)).Times(1);
287
288 EXPECT_CALL(*mockCryptIface, cryptKeyslotAddByVolumeKey(_, _, _, _, _, _))
289 .Times(1);
290
291 EXPECT_CALL(*mockCryptIface, cryptLoad(_, _, _)).Times(1);
292
293 EXPECT_CALL(*mockCryptIface, cryptActivateByPassphrase(_, _, _, _, _, _))
294 .WillOnce(Return(-1));
295
John Wedig972c3fa2021-12-29 17:30:41 -0800296 EXPECT_THROW(esObject->formatLuks(password, Volume::FilesystemType::ext4),
297 InternalFailure);
John Wedigb810c922021-11-17 16:38:03 -0800298 EXPECT_TRUE(esObject->isLocked());
299}
300
301/* Test case where we fail to create the filesystem. */
Ed Tanous82897c32022-02-21 14:11:59 -0800302TEST_F(EStoragedTest, CreateFilesystemFail)
John Wedigb810c922021-11-17 16:38:03 -0800303{
John Wedigb810c922021-11-17 16:38:03 -0800304 EXPECT_CALL(*mockCryptIface, cryptFormat(_, _, _, _, _, _, _, _)).Times(1);
305
306 EXPECT_CALL(*mockCryptIface, cryptKeyslotAddByVolumeKey(_, _, _, _, _, _))
307 .Times(1);
308
309 EXPECT_CALL(*mockCryptIface, cryptLoad(_, _, _)).Times(1);
310
311 EXPECT_CALL(*mockCryptIface, cryptActivateByPassphrase(_, _, _, _, _, _))
John Wedig2443a022023-03-17 13:42:32 -0700312 .WillOnce(&createMappedDev);
John Wedigb810c922021-11-17 16:38:03 -0800313
John Wedig2443a022023-03-17 13:42:32 -0700314 EXPECT_CALL(*mockFsIface, runMkfs(StrEq(esObject->getCryptDevicePath())))
315 .WillOnce(Return(-1));
John Wedigb810c922021-11-17 16:38:03 -0800316
John Wedig972c3fa2021-12-29 17:30:41 -0800317 EXPECT_THROW(esObject->formatLuks(password, Volume::FilesystemType::ext4),
318 InternalFailure);
John Wedigb810c922021-11-17 16:38:03 -0800319 EXPECT_FALSE(esObject->isLocked());
John Wedig2443a022023-03-17 13:42:32 -0700320
321 EXPECT_EQ(0, removeMappedDev());
John Wedigb810c922021-11-17 16:38:03 -0800322}
323
324/* Test case where we fail to create the mount point. */
Ed Tanous82897c32022-02-21 14:11:59 -0800325TEST_F(EStoragedTest, CreateMountPointFail)
John Wedigb810c922021-11-17 16:38:03 -0800326{
John Wedigb810c922021-11-17 16:38:03 -0800327 EXPECT_CALL(*mockCryptIface, cryptFormat(_, _, _, _, _, _, _, _)).Times(1);
328
329 EXPECT_CALL(*mockCryptIface, cryptKeyslotAddByVolumeKey(_, _, _, _, _, _))
330 .Times(1);
331
332 EXPECT_CALL(*mockCryptIface, cryptLoad(_, _, _)).Times(1);
333
334 EXPECT_CALL(*mockCryptIface, cryptActivateByPassphrase(_, _, _, _, _, _))
John Wedig2443a022023-03-17 13:42:32 -0700335 .WillOnce(&createMappedDev);
John Wedigb810c922021-11-17 16:38:03 -0800336
John Wedig2443a022023-03-17 13:42:32 -0700337 EXPECT_CALL(*mockFsIface, runMkfs(StrEq(esObject->getCryptDevicePath())))
338 .WillOnce(Return(0));
John Wedigb810c922021-11-17 16:38:03 -0800339
John Wedig1d6665f2025-12-04 19:08:06 +0000340 EXPECT_CALL(*mockFsIface, runFsck(StrEq(esObject->getCryptDevicePath()),
341 StrEq("-t ext4 -p")))
342 .WillOnce(Return(0));
343
John Wedigb17f8252022-01-12 14:24:26 -0800344 EXPECT_CALL(*mockFsIface, directoryExists(path(esObject->getMountPoint())))
345 .WillOnce(Return(false));
346
John Wedigb810c922021-11-17 16:38:03 -0800347 EXPECT_CALL(*mockFsIface, createDirectory(path(esObject->getMountPoint())))
348 .WillOnce(Return(false));
349
John Wedig972c3fa2021-12-29 17:30:41 -0800350 EXPECT_THROW(esObject->formatLuks(password, Volume::FilesystemType::ext4),
351 InternalFailure);
John Wedigb810c922021-11-17 16:38:03 -0800352 EXPECT_FALSE(esObject->isLocked());
John Wedig2443a022023-03-17 13:42:32 -0700353
354 EXPECT_EQ(0, removeMappedDev());
John Wedigb810c922021-11-17 16:38:03 -0800355}
356
357/* Test case where we fail to mount the filesystem. */
Ed Tanous82897c32022-02-21 14:11:59 -0800358TEST_F(EStoragedTest, MountFail)
John Wedigb810c922021-11-17 16:38:03 -0800359{
John Wedigb810c922021-11-17 16:38:03 -0800360 EXPECT_CALL(*mockCryptIface, cryptFormat(_, _, _, _, _, _, _, _)).Times(1);
361
362 EXPECT_CALL(*mockCryptIface, cryptKeyslotAddByVolumeKey(_, _, _, _, _, _))
363 .Times(1);
364
365 EXPECT_CALL(*mockCryptIface, cryptLoad(_, _, _)).Times(1);
366
367 EXPECT_CALL(*mockCryptIface, cryptActivateByPassphrase(_, _, _, _, _, _))
John Wedig2443a022023-03-17 13:42:32 -0700368 .WillOnce(&createMappedDev);
John Wedigb810c922021-11-17 16:38:03 -0800369
John Wedig2443a022023-03-17 13:42:32 -0700370 EXPECT_CALL(*mockFsIface, runMkfs(StrEq(esObject->getCryptDevicePath())))
371 .WillOnce(Return(0));
John Wedigb810c922021-11-17 16:38:03 -0800372
John Wedig1d6665f2025-12-04 19:08:06 +0000373 EXPECT_CALL(*mockFsIface, runFsck(StrEq(esObject->getCryptDevicePath()),
374 StrEq("-t ext4 -p")))
375 .WillOnce(Return(0));
376
John Wedigb17f8252022-01-12 14:24:26 -0800377 EXPECT_CALL(*mockFsIface, directoryExists(path(esObject->getMountPoint())))
378 .WillOnce(Return(false));
379
John Wedigb810c922021-11-17 16:38:03 -0800380 EXPECT_CALL(*mockFsIface, createDirectory(path(esObject->getMountPoint())))
381 .WillOnce(Return(true));
382
383 EXPECT_CALL(*mockFsIface,
John Wedig2443a022023-03-17 13:42:32 -0700384 doMount(StrEq(esObject->getCryptDevicePath()),
John Wedigb810c922021-11-17 16:38:03 -0800385 StrEq(esObject->getMountPoint()), _, _, _))
386 .WillOnce(Return(-1));
387
388 EXPECT_CALL(*mockFsIface, removeDirectory(path(esObject->getMountPoint())))
389 .WillOnce(Return(true));
390
John Wedig972c3fa2021-12-29 17:30:41 -0800391 EXPECT_THROW(esObject->formatLuks(password, Volume::FilesystemType::ext4),
392 InternalFailure);
John Wedigb810c922021-11-17 16:38:03 -0800393 EXPECT_FALSE(esObject->isLocked());
John Wedig2443a022023-03-17 13:42:32 -0700394
395 EXPECT_EQ(0, removeMappedDev());
John Wedigb810c922021-11-17 16:38:03 -0800396}
397
398/* Test case where we fail to unmount the filesystem. */
Ed Tanous82897c32022-02-21 14:11:59 -0800399TEST_F(EStoragedTest, UnmountFail)
John Wedigb810c922021-11-17 16:38:03 -0800400{
John Wedigb810c922021-11-17 16:38:03 -0800401 EXPECT_CALL(*mockCryptIface, cryptFormat(_, _, _, _, _, _, _, _)).Times(1);
402
403 EXPECT_CALL(*mockCryptIface, cryptKeyslotAddByVolumeKey(_, _, _, _, _, _))
404 .Times(1);
405
406 EXPECT_CALL(*mockCryptIface, cryptLoad(_, _, _)).Times(1);
407
408 EXPECT_CALL(*mockCryptIface, cryptActivateByPassphrase(_, _, _, _, _, _))
John Wedig2443a022023-03-17 13:42:32 -0700409 .WillOnce(&createMappedDev);
John Wedigb810c922021-11-17 16:38:03 -0800410
John Wedig2443a022023-03-17 13:42:32 -0700411 EXPECT_CALL(*mockFsIface, runMkfs(StrEq(esObject->getCryptDevicePath())))
412 .WillOnce(Return(0));
John Wedigb810c922021-11-17 16:38:03 -0800413
John Wedig1d6665f2025-12-04 19:08:06 +0000414 EXPECT_CALL(*mockFsIface, runFsck(StrEq(esObject->getCryptDevicePath()),
415 StrEq("-t ext4 -p")))
416 .WillOnce(Return(0));
417
John Wedigb17f8252022-01-12 14:24:26 -0800418 EXPECT_CALL(*mockFsIface, directoryExists(path(esObject->getMountPoint())))
419 .WillOnce(Return(false));
420
John Wedigb810c922021-11-17 16:38:03 -0800421 EXPECT_CALL(*mockFsIface, createDirectory(path(esObject->getMountPoint())))
422 .WillOnce(Return(true));
423
424 EXPECT_CALL(*mockFsIface,
John Wedig2443a022023-03-17 13:42:32 -0700425 doMount(StrEq(esObject->getCryptDevicePath()),
John Wedigb810c922021-11-17 16:38:03 -0800426 StrEq(esObject->getMountPoint()), _, _, _))
427 .WillOnce(Return(0));
428
429 EXPECT_CALL(*mockFsIface, doUnmount(StrEq(esObject->getMountPoint())))
430 .WillOnce(Return(-1));
431
John Wedig972c3fa2021-12-29 17:30:41 -0800432 esObject->formatLuks(password, Volume::FilesystemType::ext4);
John Wedigb810c922021-11-17 16:38:03 -0800433 EXPECT_FALSE(esObject->isLocked());
434
John Wedig972c3fa2021-12-29 17:30:41 -0800435 EXPECT_THROW(esObject->lock(), InternalFailure);
John Wedigb810c922021-11-17 16:38:03 -0800436 EXPECT_FALSE(esObject->isLocked());
John Wedig2443a022023-03-17 13:42:32 -0700437
438 EXPECT_EQ(0, removeMappedDev());
John Wedigb810c922021-11-17 16:38:03 -0800439}
440
441/* Test case where we fail to remove the mount point. */
Ed Tanous82897c32022-02-21 14:11:59 -0800442TEST_F(EStoragedTest, RemoveMountPointFail)
John Wedigb810c922021-11-17 16:38:03 -0800443{
John Wedigb810c922021-11-17 16:38:03 -0800444 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(_, _, _, _, _, _))
John Wedig2443a022023-03-17 13:42:32 -0700452 .WillOnce(&createMappedDev);
John Wedigb810c922021-11-17 16:38:03 -0800453
John Wedig2443a022023-03-17 13:42:32 -0700454 EXPECT_CALL(*mockFsIface, runMkfs(StrEq(esObject->getCryptDevicePath())))
455 .WillOnce(Return(0));
John Wedigb810c922021-11-17 16:38:03 -0800456
John Wedig1d6665f2025-12-04 19:08:06 +0000457 EXPECT_CALL(*mockFsIface, runFsck(StrEq(esObject->getCryptDevicePath()),
458 StrEq("-t ext4 -p")))
459 .WillOnce(Return(0));
460
John Wedigb17f8252022-01-12 14:24:26 -0800461 EXPECT_CALL(*mockFsIface, directoryExists(path(esObject->getMountPoint())))
462 .WillOnce(Return(false));
463
John Wedigb810c922021-11-17 16:38:03 -0800464 EXPECT_CALL(*mockFsIface, createDirectory(path(esObject->getMountPoint())))
465 .WillOnce(Return(true));
466
467 EXPECT_CALL(*mockFsIface,
John Wedig2443a022023-03-17 13:42:32 -0700468 doMount(StrEq(esObject->getCryptDevicePath()),
John Wedigb810c922021-11-17 16:38:03 -0800469 StrEq(esObject->getMountPoint()), _, _, _))
470 .WillOnce(Return(0));
471
472 EXPECT_CALL(*mockFsIface, doUnmount(StrEq(esObject->getMountPoint())))
473 .WillOnce(Return(0));
474
475 EXPECT_CALL(*mockFsIface, removeDirectory(path(esObject->getMountPoint())))
476 .WillOnce(Return(false));
477
John Wedig972c3fa2021-12-29 17:30:41 -0800478 esObject->formatLuks(password, Volume::FilesystemType::ext4);
John Wedigb810c922021-11-17 16:38:03 -0800479 EXPECT_FALSE(esObject->isLocked());
480
481 /* This will fail to remove the mount point. */
John Wedig972c3fa2021-12-29 17:30:41 -0800482 EXPECT_THROW(esObject->lock(), InternalFailure);
John Wedigb810c922021-11-17 16:38:03 -0800483 EXPECT_FALSE(esObject->isLocked());
John Wedig2443a022023-03-17 13:42:32 -0700484
485 EXPECT_EQ(0, removeMappedDev());
John Wedigb810c922021-11-17 16:38:03 -0800486}
487
488/* Test case where we fail to deactivate the LUKS device. */
Ed Tanous82897c32022-02-21 14:11:59 -0800489TEST_F(EStoragedTest, DeactivateFail)
John Wedigb810c922021-11-17 16:38:03 -0800490{
John Wedigb810c922021-11-17 16:38:03 -0800491 EXPECT_CALL(*mockCryptIface, cryptFormat(_, _, _, _, _, _, _, _)).Times(1);
492
493 EXPECT_CALL(*mockCryptIface, cryptKeyslotAddByVolumeKey(_, _, _, _, _, _))
494 .Times(1);
495
496 EXPECT_CALL(*mockCryptIface, cryptLoad(_, _, _)).Times(1);
497
498 EXPECT_CALL(*mockCryptIface, cryptActivateByPassphrase(_, _, _, _, _, _))
John Wedig2443a022023-03-17 13:42:32 -0700499 .WillOnce(&createMappedDev);
John Wedigb810c922021-11-17 16:38:03 -0800500
John Wedig2443a022023-03-17 13:42:32 -0700501 EXPECT_CALL(*mockFsIface, runMkfs(StrEq(esObject->getCryptDevicePath())))
502 .WillOnce(Return(0));
John Wedigb810c922021-11-17 16:38:03 -0800503
John Wedig1d6665f2025-12-04 19:08:06 +0000504 EXPECT_CALL(*mockFsIface, runFsck(StrEq(esObject->getCryptDevicePath()),
505 StrEq("-t ext4 -p")))
506 .WillOnce(Return(0));
507
John Wedigb17f8252022-01-12 14:24:26 -0800508 EXPECT_CALL(*mockFsIface, directoryExists(path(esObject->getMountPoint())))
509 .WillOnce(Return(false));
510
John Wedigb810c922021-11-17 16:38:03 -0800511 EXPECT_CALL(*mockFsIface, createDirectory(path(esObject->getMountPoint())))
512 .WillOnce(Return(true));
513
514 EXPECT_CALL(*mockFsIface,
John Wedig2443a022023-03-17 13:42:32 -0700515 doMount(StrEq(esObject->getCryptDevicePath()),
John Wedigb810c922021-11-17 16:38:03 -0800516 StrEq(esObject->getMountPoint()), _, _, _))
517 .WillOnce(Return(0));
518
519 EXPECT_CALL(*mockFsIface, doUnmount(StrEq(esObject->getMountPoint())))
520 .WillOnce(Return(0));
521
522 EXPECT_CALL(*mockFsIface, removeDirectory(path(esObject->getMountPoint())))
523 .WillOnce(Return(true));
524
525 EXPECT_CALL(*mockCryptIface, cryptDeactivate(_, _)).WillOnce(Return(-1));
526
527 /* Format the encrypted device. */
John Wedig972c3fa2021-12-29 17:30:41 -0800528 esObject->formatLuks(password, Volume::FilesystemType::ext4);
John Wedigb810c922021-11-17 16:38:03 -0800529 EXPECT_FALSE(esObject->isLocked());
530
John Wedig972c3fa2021-12-29 17:30:41 -0800531 EXPECT_THROW(esObject->lock(), InternalFailure);
John Wedigb810c922021-11-17 16:38:03 -0800532 EXPECT_FALSE(esObject->isLocked());
John Wedig2443a022023-03-17 13:42:32 -0700533
534 EXPECT_EQ(0, removeMappedDev());
John Wedigb810c922021-11-17 16:38:03 -0800535}
536
John Wedig8d5a3a02022-09-29 15:25:58 -0700537/* Test case where we successfully change the password. */
538TEST_F(EStoragedTest, ChangePasswordSuccess)
539{
540 std::string newPasswordString("newPassword");
541 std::vector<uint8_t> newPassword(newPasswordString.begin(),
542 newPasswordString.end());
543
544 EXPECT_CALL(*mockCryptIface, cryptLoad(_, _, _)).Times(1);
545
546 EXPECT_CALL(*mockCryptIface,
547 cryptKeyslotChangeByPassphrase(
548 _, _, _, reinterpret_cast<const char*>(password.data()),
549 password.size(),
550 reinterpret_cast<const char*>(newPassword.data()),
551 newPassword.size()))
552 .WillOnce(Return(0));
553
554 /* Change the password for the LUKS-encrypted device. */
555 esObject->changePassword(password, newPassword);
556}
557
558/* Test case where we fail to change the password. */
559TEST_F(EStoragedTest, ChangePasswordFail)
560{
561 std::string newPasswordString("newPassword");
562 std::vector<uint8_t> newPassword(newPasswordString.begin(),
563 newPasswordString.end());
564
565 EXPECT_CALL(*mockCryptIface, cryptLoad(_, _, _)).Times(1);
566
567 EXPECT_CALL(*mockCryptIface,
568 cryptKeyslotChangeByPassphrase(
569 _, _, _, reinterpret_cast<const char*>(password.data()),
570 password.size(),
571 reinterpret_cast<const char*>(newPassword.data()),
572 newPassword.size()))
573 .WillOnce(Return(-1));
574
575 EXPECT_THROW(esObject->changePassword(password, newPassword),
576 InternalFailure);
577}
578
John Wedigb810c922021-11-17 16:38:03 -0800579} // namespace estoraged_test