blob: 5ad8f00e4679b358e174348eb5e73ed113f081ba [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 Wedigb17f8252022-01-12 14:24:26 -0800145 EXPECT_CALL(*mockFsIface, directoryExists(path(esObject->getMountPoint())))
146 .WillOnce(Return(false));
147
John Wedigb810c922021-11-17 16:38:03 -0800148 EXPECT_CALL(*mockFsIface, createDirectory(path(esObject->getMountPoint())))
149 .WillOnce(Return(true));
150
151 EXPECT_CALL(*mockFsIface,
John Wedig2443a022023-03-17 13:42:32 -0700152 doMount(StrEq(esObject->getCryptDevicePath()),
John Wedigb810c922021-11-17 16:38:03 -0800153 StrEq(esObject->getMountPoint()), _, _, _))
154 .WillOnce(Return(0));
155
156 EXPECT_CALL(*mockFsIface, doUnmount(StrEq(esObject->getMountPoint())))
157 .WillOnce(Return(0));
158
159 EXPECT_CALL(*mockFsIface, removeDirectory(path(esObject->getMountPoint())))
160 .WillOnce(Return(true));
161
John Wedig2443a022023-03-17 13:42:32 -0700162 EXPECT_CALL(*mockCryptIface, cryptDeactivate(_, _))
163 .WillOnce(&removeMappedDev);
John Wedigb810c922021-11-17 16:38:03 -0800164
165 /* Format the encrypted device. */
John Wedig972c3fa2021-12-29 17:30:41 -0800166 esObject->formatLuks(password, Volume::FilesystemType::ext4);
John Wedigb810c922021-11-17 16:38:03 -0800167 EXPECT_FALSE(esObject->isLocked());
168
John Wedig972c3fa2021-12-29 17:30:41 -0800169 esObject->lock();
John Wedigb810c922021-11-17 16:38:03 -0800170 EXPECT_TRUE(esObject->isLocked());
171}
172
John Wedigb17f8252022-01-12 14:24:26 -0800173/*
174 * Test case where the mount point directory already exists, so it shouldn't
175 * try to create it.
176 */
Ed Tanous82897c32022-02-21 14:11:59 -0800177TEST_F(EStoragedTest, MountPointExistsPass)
John Wedigb17f8252022-01-12 14:24:26 -0800178{
John Wedigb17f8252022-01-12 14:24:26 -0800179 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(_, _, _, _, _, _))
John Wedig2443a022023-03-17 13:42:32 -0700187 .WillOnce(&createMappedDev);
John Wedigb17f8252022-01-12 14:24:26 -0800188
John Wedig2443a022023-03-17 13:42:32 -0700189 EXPECT_CALL(*mockFsIface, runMkfs(StrEq(esObject->getCryptDevicePath())))
190 .WillOnce(Return(0));
John Wedigb17f8252022-01-12 14:24:26 -0800191
192 EXPECT_CALL(*mockFsIface, directoryExists(path(esObject->getMountPoint())))
193 .WillOnce(Return(true));
194
195 EXPECT_CALL(*mockFsIface, createDirectory(path(esObject->getMountPoint())))
196 .Times(0);
197
198 EXPECT_CALL(*mockFsIface,
John Wedig2443a022023-03-17 13:42:32 -0700199 doMount(StrEq(esObject->getCryptDevicePath()),
John Wedigb17f8252022-01-12 14:24:26 -0800200 StrEq(esObject->getMountPoint()), _, _, _))
201 .WillOnce(Return(0));
202
203 EXPECT_CALL(*mockFsIface, doUnmount(StrEq(esObject->getMountPoint())))
204 .WillOnce(Return(0));
205
206 EXPECT_CALL(*mockFsIface, removeDirectory(path(esObject->getMountPoint())))
207 .WillOnce(Return(true));
208
John Wedig2443a022023-03-17 13:42:32 -0700209 EXPECT_CALL(*mockCryptIface, cryptDeactivate(_, _))
210 .WillOnce(&removeMappedDev);
John Wedigb17f8252022-01-12 14:24:26 -0800211
212 /* Format the encrypted device. */
213 esObject->formatLuks(password, Volume::FilesystemType::ext4);
214 EXPECT_FALSE(esObject->isLocked());
215
216 esObject->lock();
217 EXPECT_TRUE(esObject->isLocked());
218}
219
John Wedigb810c922021-11-17 16:38:03 -0800220/* Test case where the device/file doesn't exist. */
Ed Tanous82897c32022-02-21 14:11:59 -0800221TEST_F(EStoragedTest, FormatNoDeviceFail)
John Wedigb810c922021-11-17 16:38:03 -0800222{
223 /* Delete the test file. */
224 EXPECT_EQ(0, unlink(testFileName));
225
John Wedig972c3fa2021-12-29 17:30:41 -0800226 EXPECT_THROW(esObject->formatLuks(password, Volume::FilesystemType::ext4),
227 ResourceNotFound);
John Wedig2443a022023-03-17 13:42:32 -0700228 EXPECT_TRUE(esObject->isLocked());
John Wedigb810c922021-11-17 16:38:03 -0800229
230 /* Create the test file again, so that the TearDown function works. */
231 testFile.open(testFileName,
232 std::ios::out | std::ios::binary | std::ios::trunc);
233 testFile.close();
234}
235
236/* Test case where we fail to format the LUKS device. */
Ed Tanous82897c32022-02-21 14:11:59 -0800237TEST_F(EStoragedTest, FormatFail)
John Wedigb810c922021-11-17 16:38:03 -0800238{
239 EXPECT_CALL(*mockCryptIface, cryptFormat(_, _, _, _, _, _, _, _))
240 .WillOnce(Return(-1));
241
John Wedig972c3fa2021-12-29 17:30:41 -0800242 EXPECT_THROW(esObject->formatLuks(password, Volume::FilesystemType::ext4),
243 InternalFailure);
John Wedig2443a022023-03-17 13:42:32 -0700244 EXPECT_TRUE(esObject->isLocked());
John Wedigb810c922021-11-17 16:38:03 -0800245}
246
247/* Test case where we fail to set the password for the LUKS device. */
Ed Tanous82897c32022-02-21 14:11:59 -0800248TEST_F(EStoragedTest, AddKeyslotFail)
John Wedigb810c922021-11-17 16:38:03 -0800249{
John Wedigb810c922021-11-17 16:38:03 -0800250 EXPECT_CALL(*mockCryptIface, cryptFormat(_, _, _, _, _, _, _, _)).Times(1);
251
252 EXPECT_CALL(*mockCryptIface, cryptKeyslotAddByVolumeKey(_, _, _, _, _, _))
253 .WillOnce(Return(-1));
254
John Wedig972c3fa2021-12-29 17:30:41 -0800255 EXPECT_THROW(esObject->formatLuks(password, Volume::FilesystemType::ext4),
256 InternalFailure);
John Wedigb810c922021-11-17 16:38:03 -0800257 EXPECT_TRUE(esObject->isLocked());
258}
259
260/* Test case where we fail to load the LUKS header. */
Ed Tanous82897c32022-02-21 14:11:59 -0800261TEST_F(EStoragedTest, LoadLuksHeaderFail)
John Wedigb810c922021-11-17 16:38:03 -0800262{
John Wedigb810c922021-11-17 16:38:03 -0800263 EXPECT_CALL(*mockCryptIface, cryptFormat(_, _, _, _, _, _, _, _)).Times(1);
264
265 EXPECT_CALL(*mockCryptIface, cryptKeyslotAddByVolumeKey(_, _, _, _, _, _))
266 .Times(1);
267
268 EXPECT_CALL(*mockCryptIface, cryptLoad(_, _, _)).WillOnce(Return(-1));
269
John Wedig972c3fa2021-12-29 17:30:41 -0800270 EXPECT_THROW(esObject->formatLuks(password, Volume::FilesystemType::ext4),
271 InternalFailure);
John Wedigb810c922021-11-17 16:38:03 -0800272 EXPECT_TRUE(esObject->isLocked());
273}
274
275/* Test case where we fail to activate the LUKS device. */
Ed Tanous82897c32022-02-21 14:11:59 -0800276TEST_F(EStoragedTest, ActivateFail)
John Wedigb810c922021-11-17 16:38:03 -0800277{
John Wedigb810c922021-11-17 16:38:03 -0800278 EXPECT_CALL(*mockCryptIface, cryptFormat(_, _, _, _, _, _, _, _)).Times(1);
279
280 EXPECT_CALL(*mockCryptIface, cryptKeyslotAddByVolumeKey(_, _, _, _, _, _))
281 .Times(1);
282
283 EXPECT_CALL(*mockCryptIface, cryptLoad(_, _, _)).Times(1);
284
285 EXPECT_CALL(*mockCryptIface, cryptActivateByPassphrase(_, _, _, _, _, _))
286 .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 create the filesystem. */
Ed Tanous82897c32022-02-21 14:11:59 -0800294TEST_F(EStoragedTest, CreateFilesystemFail)
John Wedigb810c922021-11-17 16:38:03 -0800295{
John Wedigb810c922021-11-17 16:38:03 -0800296 EXPECT_CALL(*mockCryptIface, cryptFormat(_, _, _, _, _, _, _, _)).Times(1);
297
298 EXPECT_CALL(*mockCryptIface, cryptKeyslotAddByVolumeKey(_, _, _, _, _, _))
299 .Times(1);
300
301 EXPECT_CALL(*mockCryptIface, cryptLoad(_, _, _)).Times(1);
302
303 EXPECT_CALL(*mockCryptIface, cryptActivateByPassphrase(_, _, _, _, _, _))
John Wedig2443a022023-03-17 13:42:32 -0700304 .WillOnce(&createMappedDev);
John Wedigb810c922021-11-17 16:38:03 -0800305
John Wedig2443a022023-03-17 13:42:32 -0700306 EXPECT_CALL(*mockFsIface, runMkfs(StrEq(esObject->getCryptDevicePath())))
307 .WillOnce(Return(-1));
John Wedigb810c922021-11-17 16:38:03 -0800308
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());
John Wedig2443a022023-03-17 13:42:32 -0700312
313 EXPECT_EQ(0, removeMappedDev());
John Wedigb810c922021-11-17 16:38:03 -0800314}
315
316/* Test case where we fail to create the mount point. */
Ed Tanous82897c32022-02-21 14:11:59 -0800317TEST_F(EStoragedTest, CreateMountPointFail)
John Wedigb810c922021-11-17 16:38:03 -0800318{
John Wedigb810c922021-11-17 16:38:03 -0800319 EXPECT_CALL(*mockCryptIface, cryptFormat(_, _, _, _, _, _, _, _)).Times(1);
320
321 EXPECT_CALL(*mockCryptIface, cryptKeyslotAddByVolumeKey(_, _, _, _, _, _))
322 .Times(1);
323
324 EXPECT_CALL(*mockCryptIface, cryptLoad(_, _, _)).Times(1);
325
326 EXPECT_CALL(*mockCryptIface, cryptActivateByPassphrase(_, _, _, _, _, _))
John Wedig2443a022023-03-17 13:42:32 -0700327 .WillOnce(&createMappedDev);
John Wedigb810c922021-11-17 16:38:03 -0800328
John Wedig2443a022023-03-17 13:42:32 -0700329 EXPECT_CALL(*mockFsIface, runMkfs(StrEq(esObject->getCryptDevicePath())))
330 .WillOnce(Return(0));
John Wedigb810c922021-11-17 16:38:03 -0800331
John Wedigb17f8252022-01-12 14:24:26 -0800332 EXPECT_CALL(*mockFsIface, directoryExists(path(esObject->getMountPoint())))
333 .WillOnce(Return(false));
334
John Wedigb810c922021-11-17 16:38:03 -0800335 EXPECT_CALL(*mockFsIface, createDirectory(path(esObject->getMountPoint())))
336 .WillOnce(Return(false));
337
John Wedig972c3fa2021-12-29 17:30:41 -0800338 EXPECT_THROW(esObject->formatLuks(password, Volume::FilesystemType::ext4),
339 InternalFailure);
John Wedigb810c922021-11-17 16:38:03 -0800340 EXPECT_FALSE(esObject->isLocked());
John Wedig2443a022023-03-17 13:42:32 -0700341
342 EXPECT_EQ(0, removeMappedDev());
John Wedigb810c922021-11-17 16:38:03 -0800343}
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{
John Wedigb810c922021-11-17 16:38:03 -0800348 EXPECT_CALL(*mockCryptIface, cryptFormat(_, _, _, _, _, _, _, _)).Times(1);
349
350 EXPECT_CALL(*mockCryptIface, cryptKeyslotAddByVolumeKey(_, _, _, _, _, _))
351 .Times(1);
352
353 EXPECT_CALL(*mockCryptIface, cryptLoad(_, _, _)).Times(1);
354
355 EXPECT_CALL(*mockCryptIface, cryptActivateByPassphrase(_, _, _, _, _, _))
John Wedig2443a022023-03-17 13:42:32 -0700356 .WillOnce(&createMappedDev);
John Wedigb810c922021-11-17 16:38:03 -0800357
John Wedig2443a022023-03-17 13:42:32 -0700358 EXPECT_CALL(*mockFsIface, runMkfs(StrEq(esObject->getCryptDevicePath())))
359 .WillOnce(Return(0));
John Wedigb810c922021-11-17 16:38:03 -0800360
John Wedigb17f8252022-01-12 14:24:26 -0800361 EXPECT_CALL(*mockFsIface, directoryExists(path(esObject->getMountPoint())))
362 .WillOnce(Return(false));
363
John Wedigb810c922021-11-17 16:38:03 -0800364 EXPECT_CALL(*mockFsIface, createDirectory(path(esObject->getMountPoint())))
365 .WillOnce(Return(true));
366
367 EXPECT_CALL(*mockFsIface,
John Wedig2443a022023-03-17 13:42:32 -0700368 doMount(StrEq(esObject->getCryptDevicePath()),
John Wedigb810c922021-11-17 16:38:03 -0800369 StrEq(esObject->getMountPoint()), _, _, _))
370 .WillOnce(Return(-1));
371
372 EXPECT_CALL(*mockFsIface, removeDirectory(path(esObject->getMountPoint())))
373 .WillOnce(Return(true));
374
John Wedig972c3fa2021-12-29 17:30:41 -0800375 EXPECT_THROW(esObject->formatLuks(password, Volume::FilesystemType::ext4),
376 InternalFailure);
John Wedigb810c922021-11-17 16:38:03 -0800377 EXPECT_FALSE(esObject->isLocked());
John Wedig2443a022023-03-17 13:42:32 -0700378
379 EXPECT_EQ(0, removeMappedDev());
John Wedigb810c922021-11-17 16:38:03 -0800380}
381
382/* Test case where we fail to unmount the filesystem. */
Ed Tanous82897c32022-02-21 14:11:59 -0800383TEST_F(EStoragedTest, UnmountFail)
John Wedigb810c922021-11-17 16:38:03 -0800384{
John Wedigb810c922021-11-17 16:38:03 -0800385 EXPECT_CALL(*mockCryptIface, cryptFormat(_, _, _, _, _, _, _, _)).Times(1);
386
387 EXPECT_CALL(*mockCryptIface, cryptKeyslotAddByVolumeKey(_, _, _, _, _, _))
388 .Times(1);
389
390 EXPECT_CALL(*mockCryptIface, cryptLoad(_, _, _)).Times(1);
391
392 EXPECT_CALL(*mockCryptIface, cryptActivateByPassphrase(_, _, _, _, _, _))
John Wedig2443a022023-03-17 13:42:32 -0700393 .WillOnce(&createMappedDev);
John Wedigb810c922021-11-17 16:38:03 -0800394
John Wedig2443a022023-03-17 13:42:32 -0700395 EXPECT_CALL(*mockFsIface, runMkfs(StrEq(esObject->getCryptDevicePath())))
396 .WillOnce(Return(0));
John Wedigb810c922021-11-17 16:38:03 -0800397
John Wedigb17f8252022-01-12 14:24:26 -0800398 EXPECT_CALL(*mockFsIface, directoryExists(path(esObject->getMountPoint())))
399 .WillOnce(Return(false));
400
John Wedigb810c922021-11-17 16:38:03 -0800401 EXPECT_CALL(*mockFsIface, createDirectory(path(esObject->getMountPoint())))
402 .WillOnce(Return(true));
403
404 EXPECT_CALL(*mockFsIface,
John Wedig2443a022023-03-17 13:42:32 -0700405 doMount(StrEq(esObject->getCryptDevicePath()),
John Wedigb810c922021-11-17 16:38:03 -0800406 StrEq(esObject->getMountPoint()), _, _, _))
407 .WillOnce(Return(0));
408
409 EXPECT_CALL(*mockFsIface, doUnmount(StrEq(esObject->getMountPoint())))
410 .WillOnce(Return(-1));
411
John Wedig972c3fa2021-12-29 17:30:41 -0800412 esObject->formatLuks(password, Volume::FilesystemType::ext4);
John Wedigb810c922021-11-17 16:38:03 -0800413 EXPECT_FALSE(esObject->isLocked());
414
John Wedig972c3fa2021-12-29 17:30:41 -0800415 EXPECT_THROW(esObject->lock(), InternalFailure);
John Wedigb810c922021-11-17 16:38:03 -0800416 EXPECT_FALSE(esObject->isLocked());
John Wedig2443a022023-03-17 13:42:32 -0700417
418 EXPECT_EQ(0, removeMappedDev());
John Wedigb810c922021-11-17 16:38:03 -0800419}
420
421/* Test case where we fail to remove the mount point. */
Ed Tanous82897c32022-02-21 14:11:59 -0800422TEST_F(EStoragedTest, RemoveMountPointFail)
John Wedigb810c922021-11-17 16:38:03 -0800423{
John Wedigb810c922021-11-17 16:38:03 -0800424 EXPECT_CALL(*mockCryptIface, cryptFormat(_, _, _, _, _, _, _, _)).Times(1);
425
426 EXPECT_CALL(*mockCryptIface, cryptKeyslotAddByVolumeKey(_, _, _, _, _, _))
427 .Times(1);
428
429 EXPECT_CALL(*mockCryptIface, cryptLoad(_, _, _)).Times(1);
430
431 EXPECT_CALL(*mockCryptIface, cryptActivateByPassphrase(_, _, _, _, _, _))
John Wedig2443a022023-03-17 13:42:32 -0700432 .WillOnce(&createMappedDev);
John Wedigb810c922021-11-17 16:38:03 -0800433
John Wedig2443a022023-03-17 13:42:32 -0700434 EXPECT_CALL(*mockFsIface, runMkfs(StrEq(esObject->getCryptDevicePath())))
435 .WillOnce(Return(0));
John Wedigb810c922021-11-17 16:38:03 -0800436
John Wedigb17f8252022-01-12 14:24:26 -0800437 EXPECT_CALL(*mockFsIface, directoryExists(path(esObject->getMountPoint())))
438 .WillOnce(Return(false));
439
John Wedigb810c922021-11-17 16:38:03 -0800440 EXPECT_CALL(*mockFsIface, createDirectory(path(esObject->getMountPoint())))
441 .WillOnce(Return(true));
442
443 EXPECT_CALL(*mockFsIface,
John Wedig2443a022023-03-17 13:42:32 -0700444 doMount(StrEq(esObject->getCryptDevicePath()),
John Wedigb810c922021-11-17 16:38:03 -0800445 StrEq(esObject->getMountPoint()), _, _, _))
446 .WillOnce(Return(0));
447
448 EXPECT_CALL(*mockFsIface, doUnmount(StrEq(esObject->getMountPoint())))
449 .WillOnce(Return(0));
450
451 EXPECT_CALL(*mockFsIface, removeDirectory(path(esObject->getMountPoint())))
452 .WillOnce(Return(false));
453
John Wedig972c3fa2021-12-29 17:30:41 -0800454 esObject->formatLuks(password, Volume::FilesystemType::ext4);
John Wedigb810c922021-11-17 16:38:03 -0800455 EXPECT_FALSE(esObject->isLocked());
456
457 /* This will fail to remove the mount point. */
John Wedig972c3fa2021-12-29 17:30:41 -0800458 EXPECT_THROW(esObject->lock(), InternalFailure);
John Wedigb810c922021-11-17 16:38:03 -0800459 EXPECT_FALSE(esObject->isLocked());
John Wedig2443a022023-03-17 13:42:32 -0700460
461 EXPECT_EQ(0, removeMappedDev());
John Wedigb810c922021-11-17 16:38:03 -0800462}
463
464/* Test case where we fail to deactivate the LUKS device. */
Ed Tanous82897c32022-02-21 14:11:59 -0800465TEST_F(EStoragedTest, DeactivateFail)
John Wedigb810c922021-11-17 16:38:03 -0800466{
John Wedigb810c922021-11-17 16:38:03 -0800467 EXPECT_CALL(*mockCryptIface, cryptFormat(_, _, _, _, _, _, _, _)).Times(1);
468
469 EXPECT_CALL(*mockCryptIface, cryptKeyslotAddByVolumeKey(_, _, _, _, _, _))
470 .Times(1);
471
472 EXPECT_CALL(*mockCryptIface, cryptLoad(_, _, _)).Times(1);
473
474 EXPECT_CALL(*mockCryptIface, cryptActivateByPassphrase(_, _, _, _, _, _))
John Wedig2443a022023-03-17 13:42:32 -0700475 .WillOnce(&createMappedDev);
John Wedigb810c922021-11-17 16:38:03 -0800476
John Wedig2443a022023-03-17 13:42:32 -0700477 EXPECT_CALL(*mockFsIface, runMkfs(StrEq(esObject->getCryptDevicePath())))
478 .WillOnce(Return(0));
John Wedigb810c922021-11-17 16:38:03 -0800479
John Wedigb17f8252022-01-12 14:24:26 -0800480 EXPECT_CALL(*mockFsIface, directoryExists(path(esObject->getMountPoint())))
481 .WillOnce(Return(false));
482
John Wedigb810c922021-11-17 16:38:03 -0800483 EXPECT_CALL(*mockFsIface, createDirectory(path(esObject->getMountPoint())))
484 .WillOnce(Return(true));
485
486 EXPECT_CALL(*mockFsIface,
John Wedig2443a022023-03-17 13:42:32 -0700487 doMount(StrEq(esObject->getCryptDevicePath()),
John Wedigb810c922021-11-17 16:38:03 -0800488 StrEq(esObject->getMountPoint()), _, _, _))
489 .WillOnce(Return(0));
490
491 EXPECT_CALL(*mockFsIface, doUnmount(StrEq(esObject->getMountPoint())))
492 .WillOnce(Return(0));
493
494 EXPECT_CALL(*mockFsIface, removeDirectory(path(esObject->getMountPoint())))
495 .WillOnce(Return(true));
496
497 EXPECT_CALL(*mockCryptIface, cryptDeactivate(_, _)).WillOnce(Return(-1));
498
499 /* Format the encrypted device. */
John Wedig972c3fa2021-12-29 17:30:41 -0800500 esObject->formatLuks(password, Volume::FilesystemType::ext4);
John Wedigb810c922021-11-17 16:38:03 -0800501 EXPECT_FALSE(esObject->isLocked());
502
John Wedig972c3fa2021-12-29 17:30:41 -0800503 EXPECT_THROW(esObject->lock(), InternalFailure);
John Wedigb810c922021-11-17 16:38:03 -0800504 EXPECT_FALSE(esObject->isLocked());
John Wedig2443a022023-03-17 13:42:32 -0700505
506 EXPECT_EQ(0, removeMappedDev());
John Wedigb810c922021-11-17 16:38:03 -0800507}
508
John Wedig8d5a3a02022-09-29 15:25:58 -0700509/* Test case where we successfully change the password. */
510TEST_F(EStoragedTest, ChangePasswordSuccess)
511{
512 std::string newPasswordString("newPassword");
513 std::vector<uint8_t> newPassword(newPasswordString.begin(),
514 newPasswordString.end());
515
516 EXPECT_CALL(*mockCryptIface, cryptLoad(_, _, _)).Times(1);
517
518 EXPECT_CALL(*mockCryptIface,
519 cryptKeyslotChangeByPassphrase(
520 _, _, _, reinterpret_cast<const char*>(password.data()),
521 password.size(),
522 reinterpret_cast<const char*>(newPassword.data()),
523 newPassword.size()))
524 .WillOnce(Return(0));
525
526 /* Change the password for the LUKS-encrypted device. */
527 esObject->changePassword(password, newPassword);
528}
529
530/* Test case where we fail to change the password. */
531TEST_F(EStoragedTest, ChangePasswordFail)
532{
533 std::string newPasswordString("newPassword");
534 std::vector<uint8_t> newPassword(newPasswordString.begin(),
535 newPasswordString.end());
536
537 EXPECT_CALL(*mockCryptIface, cryptLoad(_, _, _)).Times(1);
538
539 EXPECT_CALL(*mockCryptIface,
540 cryptKeyslotChangeByPassphrase(
541 _, _, _, reinterpret_cast<const char*>(password.data()),
542 password.size(),
543 reinterpret_cast<const char*>(newPassword.data()),
544 newPassword.size()))
545 .WillOnce(Return(-1));
546
547 EXPECT_THROW(esObject->changePassword(password, newPassword),
548 InternalFailure);
549}
550
John Wedigb810c922021-11-17 16:38:03 -0800551} // namespace estoraged_test