blob: bd6db0de3171599ebc33ee0585e22e839d9fd179 [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 Wedig67a47442022-04-05 17:21:29 -07007#include <boost/asio/io_context.hpp>
8#include <sdbusplus/asio/connection.hpp>
9#include <sdbusplus/asio/object_server.hpp>
John Wedig972c3fa2021-12-29 17:30:41 -080010#include <xyz/openbmc_project/Common/error.hpp>
Patrick Williams0e2a46f2023-05-10 14:35:52 -050011#include <xyz/openbmc_project/Inventory/Item/Volume/server.hpp>
John Wedigb810c922021-11-17 16:38:03 -080012
13#include <exception>
14#include <filesystem>
15#include <fstream>
16#include <iterator>
John Wedig67a47442022-04-05 17:21:29 -070017#include <memory>
John Wedigb810c922021-11-17 16:38:03 -080018#include <string>
19#include <vector>
20
21#include <gmock/gmock.h>
22#include <gtest/gtest.h>
23
24namespace estoraged_test
25{
26
Patrick Williams0e2a46f2023-05-10 14:35:52 -050027using sdbusplus::server::xyz::openbmc_project::inventory::item::Volume;
John Wedig972c3fa2021-12-29 17:30:41 -080028using sdbusplus::xyz::openbmc_project::Common::Error::InternalFailure;
29using sdbusplus::xyz::openbmc_project::Common::Error::ResourceNotFound;
John Wedigb810c922021-11-17 16:38:03 -080030using std::filesystem::path;
31using ::testing::_;
John Wedigb810c922021-11-17 16:38:03 -080032using ::testing::Return;
33using ::testing::StrEq;
34
Ed Tanous82897c32022-02-21 14:11:59 -080035class EStoragedTest : public testing::Test
John Wedigb810c922021-11-17 16:38:03 -080036{
37 public:
Ed Tanous82897c32022-02-21 14:11:59 -080038 const char* testFileName = "testfile";
39 const char* testLuksDevName = "testfile_luksDev";
John Wedig2443a022023-03-17 13:42:32 -070040 const char* testCryptDir = "/tmp";
John Wedig6c0d8ce2022-04-22 14:00:43 -070041 const std::string testConfigPath =
42 "/xyz/openbmc_project/inventory/system/board/test_board/test_emmc";
John Wedig67a47442022-04-05 17:21:29 -070043 const uint64_t testSize = 24;
John Edward Broadbent5d799bb2022-03-22 16:14:24 -070044 const uint8_t testLifeTime = 25;
John Wedigb4838302022-07-22 13:51:16 -070045 const std::string testPartNumber = "12345678";
46 const std::string testSerialNumber = "ABCDEF1234";
Rahul Kapoor19825052023-05-27 01:52:23 +000047 const std::string testLocationCode = "U102020";
John Wedigb810c922021-11-17 16:38:03 -080048 std::ofstream testFile;
John Wedigb810c922021-11-17 16:38:03 -080049 std::string passwordString;
50 std::vector<uint8_t> password;
Ed Tanous82897c32022-02-21 14:11:59 -080051 MockCryptsetupInterface* mockCryptIface{};
52 MockFilesystemInterface* mockFsIface{};
John Wedig67a47442022-04-05 17:21:29 -070053 boost::asio::io_context io;
54 std::shared_ptr<sdbusplus::asio::connection> conn;
55 std::unique_ptr<sdbusplus::asio::object_server> objectServer;
56 std::unique_ptr<estoraged::EStoraged> esObject;
John Wedigb810c922021-11-17 16:38:03 -080057
Ed Tanous82897c32022-02-21 14:11:59 -080058 EStoragedTest() :
John Wedig67a47442022-04-05 17:21:29 -070059 passwordString("password"),
John Wedigb810c922021-11-17 16:38:03 -080060 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
John Wedigb810c922021-11-17 16:38:03 -080074 std::unique_ptr<MockCryptsetupInterface> cryptIface =
75 std::make_unique<MockCryptsetupInterface>();
76 mockCryptIface = cryptIface.get();
77 std::unique_ptr<MockFilesystemInterface> fsIface =
78 std::make_unique<MockFilesystemInterface>();
79 mockFsIface = fsIface.get();
80
John Wedig2443a022023-03-17 13:42:32 -070081 /* Set up location of dummy mapped crypt file. */
82 EXPECT_CALL(*cryptIface, cryptGetDir).WillOnce(Return(testCryptDir));
83
John Wedig67a47442022-04-05 17:21:29 -070084 conn = std::make_shared<sdbusplus::asio::connection>(io);
85 // request D-Bus server name.
86 conn->request_name("xyz.openbmc_project.eStoraged.test");
87 objectServer = std::make_unique<sdbusplus::asio::object_server>(conn);
88
Ed Tanous82897c32022-02-21 14:11:59 -080089 esObject = std::make_unique<estoraged::EStoraged>(
John Wedig6c0d8ce2022-04-22 14:00:43 -070090 *objectServer, testConfigPath, testFileName, testLuksDevName,
John Wedigb4838302022-07-22 13:51:16 -070091 testSize, testLifeTime, testPartNumber, testSerialNumber,
Rahul Kapoor19825052023-05-27 01:52:23 +000092 testLocationCode, std::move(cryptIface), std::move(fsIface));
John Wedigb810c922021-11-17 16:38:03 -080093 }
94
95 void TearDown() override
96 {
97 EXPECT_EQ(0, unlink(testFileName));
98 }
99};
100
John Wedig2443a022023-03-17 13:42:32 -0700101const char* mappedDevicePath = "/tmp/testfile_luksDev";
102std::ofstream mappedDevice;
103
104int createMappedDev()
105{
106 mappedDevice.open(mappedDevicePath,
107 std::ios::out | std::ios::binary | std::ios::trunc);
108 mappedDevice.close();
109 if (mappedDevice.fail())
110 {
111 throw std::runtime_error("Failed to open test mapped device");
112 }
113
114 return 0;
115}
116
117int removeMappedDev()
118{
119 EXPECT_EQ(0, unlink(mappedDevicePath));
120
121 return 0;
122}
123
John Wedigb810c922021-11-17 16:38:03 -0800124/* Test case to format and then lock the LUKS device. */
Ed Tanous82897c32022-02-21 14:11:59 -0800125TEST_F(EStoragedTest, FormatPass)
John Wedigb810c922021-11-17 16:38:03 -0800126{
John Wedigb810c922021-11-17 16:38:03 -0800127 EXPECT_CALL(*mockCryptIface, cryptFormat(_, _, _, _, _, _, _, _)).Times(1);
128
129 EXPECT_CALL(*mockCryptIface, cryptKeyslotAddByVolumeKey(_, _, _, _, _, _))
130 .Times(1);
131
132 EXPECT_CALL(*mockCryptIface, cryptLoad(_, _, _)).Times(1);
133
134 EXPECT_CALL(*mockCryptIface, cryptActivateByPassphrase(_, _, _, _, _, _))
John Wedig2443a022023-03-17 13:42:32 -0700135 .WillOnce(&createMappedDev);
John Wedigb810c922021-11-17 16:38:03 -0800136
John Wedig2443a022023-03-17 13:42:32 -0700137 EXPECT_CALL(*mockFsIface, runMkfs(StrEq(esObject->getCryptDevicePath())))
138 .WillOnce(Return(0));
John Wedigb810c922021-11-17 16:38:03 -0800139
John Wedigb17f8252022-01-12 14:24:26 -0800140 EXPECT_CALL(*mockFsIface, directoryExists(path(esObject->getMountPoint())))
141 .WillOnce(Return(false));
142
John Wedigb810c922021-11-17 16:38:03 -0800143 EXPECT_CALL(*mockFsIface, createDirectory(path(esObject->getMountPoint())))
144 .WillOnce(Return(true));
145
146 EXPECT_CALL(*mockFsIface,
John Wedig2443a022023-03-17 13:42:32 -0700147 doMount(StrEq(esObject->getCryptDevicePath()),
John Wedigb810c922021-11-17 16:38:03 -0800148 StrEq(esObject->getMountPoint()), _, _, _))
149 .WillOnce(Return(0));
150
151 EXPECT_CALL(*mockFsIface, doUnmount(StrEq(esObject->getMountPoint())))
152 .WillOnce(Return(0));
153
154 EXPECT_CALL(*mockFsIface, removeDirectory(path(esObject->getMountPoint())))
155 .WillOnce(Return(true));
156
John Wedig2443a022023-03-17 13:42:32 -0700157 EXPECT_CALL(*mockCryptIface, cryptDeactivate(_, _))
158 .WillOnce(&removeMappedDev);
John Wedigb810c922021-11-17 16:38:03 -0800159
160 /* Format the encrypted device. */
John Wedig972c3fa2021-12-29 17:30:41 -0800161 esObject->formatLuks(password, Volume::FilesystemType::ext4);
John Wedigb810c922021-11-17 16:38:03 -0800162 EXPECT_FALSE(esObject->isLocked());
163
John Wedig972c3fa2021-12-29 17:30:41 -0800164 esObject->lock();
John Wedigb810c922021-11-17 16:38:03 -0800165 EXPECT_TRUE(esObject->isLocked());
166}
167
John Wedigb17f8252022-01-12 14:24:26 -0800168/*
169 * Test case where the mount point directory already exists, so it shouldn't
170 * try to create it.
171 */
Ed Tanous82897c32022-02-21 14:11:59 -0800172TEST_F(EStoragedTest, MountPointExistsPass)
John Wedigb17f8252022-01-12 14:24:26 -0800173{
John Wedigb17f8252022-01-12 14:24:26 -0800174 EXPECT_CALL(*mockCryptIface, cryptFormat(_, _, _, _, _, _, _, _)).Times(1);
175
176 EXPECT_CALL(*mockCryptIface, cryptKeyslotAddByVolumeKey(_, _, _, _, _, _))
177 .Times(1);
178
179 EXPECT_CALL(*mockCryptIface, cryptLoad(_, _, _)).Times(1);
180
181 EXPECT_CALL(*mockCryptIface, cryptActivateByPassphrase(_, _, _, _, _, _))
John Wedig2443a022023-03-17 13:42:32 -0700182 .WillOnce(&createMappedDev);
John Wedigb17f8252022-01-12 14:24:26 -0800183
John Wedig2443a022023-03-17 13:42:32 -0700184 EXPECT_CALL(*mockFsIface, runMkfs(StrEq(esObject->getCryptDevicePath())))
185 .WillOnce(Return(0));
John Wedigb17f8252022-01-12 14:24:26 -0800186
187 EXPECT_CALL(*mockFsIface, directoryExists(path(esObject->getMountPoint())))
188 .WillOnce(Return(true));
189
190 EXPECT_CALL(*mockFsIface, createDirectory(path(esObject->getMountPoint())))
191 .Times(0);
192
193 EXPECT_CALL(*mockFsIface,
John Wedig2443a022023-03-17 13:42:32 -0700194 doMount(StrEq(esObject->getCryptDevicePath()),
John Wedigb17f8252022-01-12 14:24:26 -0800195 StrEq(esObject->getMountPoint()), _, _, _))
196 .WillOnce(Return(0));
197
198 EXPECT_CALL(*mockFsIface, doUnmount(StrEq(esObject->getMountPoint())))
199 .WillOnce(Return(0));
200
201 EXPECT_CALL(*mockFsIface, removeDirectory(path(esObject->getMountPoint())))
202 .WillOnce(Return(true));
203
John Wedig2443a022023-03-17 13:42:32 -0700204 EXPECT_CALL(*mockCryptIface, cryptDeactivate(_, _))
205 .WillOnce(&removeMappedDev);
John Wedigb17f8252022-01-12 14:24:26 -0800206
207 /* Format the encrypted device. */
208 esObject->formatLuks(password, Volume::FilesystemType::ext4);
209 EXPECT_FALSE(esObject->isLocked());
210
211 esObject->lock();
212 EXPECT_TRUE(esObject->isLocked());
213}
214
John Wedigb810c922021-11-17 16:38:03 -0800215/* Test case where the device/file doesn't exist. */
Ed Tanous82897c32022-02-21 14:11:59 -0800216TEST_F(EStoragedTest, FormatNoDeviceFail)
John Wedigb810c922021-11-17 16:38:03 -0800217{
218 /* Delete the test file. */
219 EXPECT_EQ(0, unlink(testFileName));
220
John Wedig972c3fa2021-12-29 17:30:41 -0800221 EXPECT_THROW(esObject->formatLuks(password, Volume::FilesystemType::ext4),
222 ResourceNotFound);
John Wedig2443a022023-03-17 13:42:32 -0700223 EXPECT_TRUE(esObject->isLocked());
John Wedigb810c922021-11-17 16:38:03 -0800224
225 /* Create the test file again, so that the TearDown function works. */
226 testFile.open(testFileName,
227 std::ios::out | std::ios::binary | std::ios::trunc);
228 testFile.close();
229}
230
231/* Test case where we fail to format the LUKS device. */
Ed Tanous82897c32022-02-21 14:11:59 -0800232TEST_F(EStoragedTest, FormatFail)
John Wedigb810c922021-11-17 16:38:03 -0800233{
234 EXPECT_CALL(*mockCryptIface, cryptFormat(_, _, _, _, _, _, _, _))
235 .WillOnce(Return(-1));
236
John Wedig972c3fa2021-12-29 17:30:41 -0800237 EXPECT_THROW(esObject->formatLuks(password, Volume::FilesystemType::ext4),
238 InternalFailure);
John Wedig2443a022023-03-17 13:42:32 -0700239 EXPECT_TRUE(esObject->isLocked());
John Wedigb810c922021-11-17 16:38:03 -0800240}
241
242/* Test case where we fail to set the password for the LUKS device. */
Ed Tanous82897c32022-02-21 14:11:59 -0800243TEST_F(EStoragedTest, AddKeyslotFail)
John Wedigb810c922021-11-17 16:38:03 -0800244{
John Wedigb810c922021-11-17 16:38:03 -0800245 EXPECT_CALL(*mockCryptIface, cryptFormat(_, _, _, _, _, _, _, _)).Times(1);
246
247 EXPECT_CALL(*mockCryptIface, cryptKeyslotAddByVolumeKey(_, _, _, _, _, _))
248 .WillOnce(Return(-1));
249
John Wedig972c3fa2021-12-29 17:30:41 -0800250 EXPECT_THROW(esObject->formatLuks(password, Volume::FilesystemType::ext4),
251 InternalFailure);
John Wedigb810c922021-11-17 16:38:03 -0800252 EXPECT_TRUE(esObject->isLocked());
253}
254
255/* Test case where we fail to load the LUKS header. */
Ed Tanous82897c32022-02-21 14:11:59 -0800256TEST_F(EStoragedTest, LoadLuksHeaderFail)
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 .Times(1);
262
263 EXPECT_CALL(*mockCryptIface, cryptLoad(_, _, _)).WillOnce(Return(-1));
264
John Wedig972c3fa2021-12-29 17:30:41 -0800265 EXPECT_THROW(esObject->formatLuks(password, Volume::FilesystemType::ext4),
266 InternalFailure);
John Wedigb810c922021-11-17 16:38:03 -0800267 EXPECT_TRUE(esObject->isLocked());
268}
269
270/* Test case where we fail to activate the LUKS device. */
Ed Tanous82897c32022-02-21 14:11:59 -0800271TEST_F(EStoragedTest, ActivateFail)
John Wedigb810c922021-11-17 16:38:03 -0800272{
John Wedigb810c922021-11-17 16:38:03 -0800273 EXPECT_CALL(*mockCryptIface, cryptFormat(_, _, _, _, _, _, _, _)).Times(1);
274
275 EXPECT_CALL(*mockCryptIface, cryptKeyslotAddByVolumeKey(_, _, _, _, _, _))
276 .Times(1);
277
278 EXPECT_CALL(*mockCryptIface, cryptLoad(_, _, _)).Times(1);
279
280 EXPECT_CALL(*mockCryptIface, cryptActivateByPassphrase(_, _, _, _, _, _))
281 .WillOnce(Return(-1));
282
John Wedig972c3fa2021-12-29 17:30:41 -0800283 EXPECT_THROW(esObject->formatLuks(password, Volume::FilesystemType::ext4),
284 InternalFailure);
John Wedigb810c922021-11-17 16:38:03 -0800285 EXPECT_TRUE(esObject->isLocked());
286}
287
288/* Test case where we fail to create the filesystem. */
Ed Tanous82897c32022-02-21 14:11:59 -0800289TEST_F(EStoragedTest, CreateFilesystemFail)
John Wedigb810c922021-11-17 16:38:03 -0800290{
John Wedigb810c922021-11-17 16:38:03 -0800291 EXPECT_CALL(*mockCryptIface, cryptFormat(_, _, _, _, _, _, _, _)).Times(1);
292
293 EXPECT_CALL(*mockCryptIface, cryptKeyslotAddByVolumeKey(_, _, _, _, _, _))
294 .Times(1);
295
296 EXPECT_CALL(*mockCryptIface, cryptLoad(_, _, _)).Times(1);
297
298 EXPECT_CALL(*mockCryptIface, cryptActivateByPassphrase(_, _, _, _, _, _))
John Wedig2443a022023-03-17 13:42:32 -0700299 .WillOnce(&createMappedDev);
John Wedigb810c922021-11-17 16:38:03 -0800300
John Wedig2443a022023-03-17 13:42:32 -0700301 EXPECT_CALL(*mockFsIface, runMkfs(StrEq(esObject->getCryptDevicePath())))
302 .WillOnce(Return(-1));
John Wedigb810c922021-11-17 16:38:03 -0800303
John Wedig972c3fa2021-12-29 17:30:41 -0800304 EXPECT_THROW(esObject->formatLuks(password, Volume::FilesystemType::ext4),
305 InternalFailure);
John Wedigb810c922021-11-17 16:38:03 -0800306 EXPECT_FALSE(esObject->isLocked());
John Wedig2443a022023-03-17 13:42:32 -0700307
308 EXPECT_EQ(0, removeMappedDev());
John Wedigb810c922021-11-17 16:38:03 -0800309}
310
311/* Test case where we fail to create the mount point. */
Ed Tanous82897c32022-02-21 14:11:59 -0800312TEST_F(EStoragedTest, CreateMountPointFail)
John Wedigb810c922021-11-17 16:38:03 -0800313{
John Wedigb810c922021-11-17 16:38:03 -0800314 EXPECT_CALL(*mockCryptIface, cryptFormat(_, _, _, _, _, _, _, _)).Times(1);
315
316 EXPECT_CALL(*mockCryptIface, cryptKeyslotAddByVolumeKey(_, _, _, _, _, _))
317 .Times(1);
318
319 EXPECT_CALL(*mockCryptIface, cryptLoad(_, _, _)).Times(1);
320
321 EXPECT_CALL(*mockCryptIface, cryptActivateByPassphrase(_, _, _, _, _, _))
John Wedig2443a022023-03-17 13:42:32 -0700322 .WillOnce(&createMappedDev);
John Wedigb810c922021-11-17 16:38:03 -0800323
John Wedig2443a022023-03-17 13:42:32 -0700324 EXPECT_CALL(*mockFsIface, runMkfs(StrEq(esObject->getCryptDevicePath())))
325 .WillOnce(Return(0));
John Wedigb810c922021-11-17 16:38:03 -0800326
John Wedigb17f8252022-01-12 14:24:26 -0800327 EXPECT_CALL(*mockFsIface, directoryExists(path(esObject->getMountPoint())))
328 .WillOnce(Return(false));
329
John Wedigb810c922021-11-17 16:38:03 -0800330 EXPECT_CALL(*mockFsIface, createDirectory(path(esObject->getMountPoint())))
331 .WillOnce(Return(false));
332
John Wedig972c3fa2021-12-29 17:30:41 -0800333 EXPECT_THROW(esObject->formatLuks(password, Volume::FilesystemType::ext4),
334 InternalFailure);
John Wedigb810c922021-11-17 16:38:03 -0800335 EXPECT_FALSE(esObject->isLocked());
John Wedig2443a022023-03-17 13:42:32 -0700336
337 EXPECT_EQ(0, removeMappedDev());
John Wedigb810c922021-11-17 16:38:03 -0800338}
339
340/* Test case where we fail to mount the filesystem. */
Ed Tanous82897c32022-02-21 14:11:59 -0800341TEST_F(EStoragedTest, MountFail)
John Wedigb810c922021-11-17 16:38:03 -0800342{
John Wedigb810c922021-11-17 16:38:03 -0800343 EXPECT_CALL(*mockCryptIface, cryptFormat(_, _, _, _, _, _, _, _)).Times(1);
344
345 EXPECT_CALL(*mockCryptIface, cryptKeyslotAddByVolumeKey(_, _, _, _, _, _))
346 .Times(1);
347
348 EXPECT_CALL(*mockCryptIface, cryptLoad(_, _, _)).Times(1);
349
350 EXPECT_CALL(*mockCryptIface, cryptActivateByPassphrase(_, _, _, _, _, _))
John Wedig2443a022023-03-17 13:42:32 -0700351 .WillOnce(&createMappedDev);
John Wedigb810c922021-11-17 16:38:03 -0800352
John Wedig2443a022023-03-17 13:42:32 -0700353 EXPECT_CALL(*mockFsIface, runMkfs(StrEq(esObject->getCryptDevicePath())))
354 .WillOnce(Return(0));
John Wedigb810c922021-11-17 16:38:03 -0800355
John Wedigb17f8252022-01-12 14:24:26 -0800356 EXPECT_CALL(*mockFsIface, directoryExists(path(esObject->getMountPoint())))
357 .WillOnce(Return(false));
358
John Wedigb810c922021-11-17 16:38:03 -0800359 EXPECT_CALL(*mockFsIface, createDirectory(path(esObject->getMountPoint())))
360 .WillOnce(Return(true));
361
362 EXPECT_CALL(*mockFsIface,
John Wedig2443a022023-03-17 13:42:32 -0700363 doMount(StrEq(esObject->getCryptDevicePath()),
John Wedigb810c922021-11-17 16:38:03 -0800364 StrEq(esObject->getMountPoint()), _, _, _))
365 .WillOnce(Return(-1));
366
367 EXPECT_CALL(*mockFsIface, removeDirectory(path(esObject->getMountPoint())))
368 .WillOnce(Return(true));
369
John Wedig972c3fa2021-12-29 17:30:41 -0800370 EXPECT_THROW(esObject->formatLuks(password, Volume::FilesystemType::ext4),
371 InternalFailure);
John Wedigb810c922021-11-17 16:38:03 -0800372 EXPECT_FALSE(esObject->isLocked());
John Wedig2443a022023-03-17 13:42:32 -0700373
374 EXPECT_EQ(0, removeMappedDev());
John Wedigb810c922021-11-17 16:38:03 -0800375}
376
377/* Test case where we fail to unmount the filesystem. */
Ed Tanous82897c32022-02-21 14:11:59 -0800378TEST_F(EStoragedTest, UnmountFail)
John Wedigb810c922021-11-17 16:38:03 -0800379{
John Wedigb810c922021-11-17 16:38:03 -0800380 EXPECT_CALL(*mockCryptIface, cryptFormat(_, _, _, _, _, _, _, _)).Times(1);
381
382 EXPECT_CALL(*mockCryptIface, cryptKeyslotAddByVolumeKey(_, _, _, _, _, _))
383 .Times(1);
384
385 EXPECT_CALL(*mockCryptIface, cryptLoad(_, _, _)).Times(1);
386
387 EXPECT_CALL(*mockCryptIface, cryptActivateByPassphrase(_, _, _, _, _, _))
John Wedig2443a022023-03-17 13:42:32 -0700388 .WillOnce(&createMappedDev);
John Wedigb810c922021-11-17 16:38:03 -0800389
John Wedig2443a022023-03-17 13:42:32 -0700390 EXPECT_CALL(*mockFsIface, runMkfs(StrEq(esObject->getCryptDevicePath())))
391 .WillOnce(Return(0));
John Wedigb810c922021-11-17 16:38:03 -0800392
John Wedigb17f8252022-01-12 14:24:26 -0800393 EXPECT_CALL(*mockFsIface, directoryExists(path(esObject->getMountPoint())))
394 .WillOnce(Return(false));
395
John Wedigb810c922021-11-17 16:38:03 -0800396 EXPECT_CALL(*mockFsIface, createDirectory(path(esObject->getMountPoint())))
397 .WillOnce(Return(true));
398
399 EXPECT_CALL(*mockFsIface,
John Wedig2443a022023-03-17 13:42:32 -0700400 doMount(StrEq(esObject->getCryptDevicePath()),
John Wedigb810c922021-11-17 16:38:03 -0800401 StrEq(esObject->getMountPoint()), _, _, _))
402 .WillOnce(Return(0));
403
404 EXPECT_CALL(*mockFsIface, doUnmount(StrEq(esObject->getMountPoint())))
405 .WillOnce(Return(-1));
406
John Wedig972c3fa2021-12-29 17:30:41 -0800407 esObject->formatLuks(password, Volume::FilesystemType::ext4);
John Wedigb810c922021-11-17 16:38:03 -0800408 EXPECT_FALSE(esObject->isLocked());
409
John Wedig972c3fa2021-12-29 17:30:41 -0800410 EXPECT_THROW(esObject->lock(), InternalFailure);
John Wedigb810c922021-11-17 16:38:03 -0800411 EXPECT_FALSE(esObject->isLocked());
John Wedig2443a022023-03-17 13:42:32 -0700412
413 EXPECT_EQ(0, removeMappedDev());
John Wedigb810c922021-11-17 16:38:03 -0800414}
415
416/* Test case where we fail to remove the mount point. */
Ed Tanous82897c32022-02-21 14:11:59 -0800417TEST_F(EStoragedTest, RemoveMountPointFail)
John Wedigb810c922021-11-17 16:38:03 -0800418{
John Wedigb810c922021-11-17 16:38:03 -0800419 EXPECT_CALL(*mockCryptIface, cryptFormat(_, _, _, _, _, _, _, _)).Times(1);
420
421 EXPECT_CALL(*mockCryptIface, cryptKeyslotAddByVolumeKey(_, _, _, _, _, _))
422 .Times(1);
423
424 EXPECT_CALL(*mockCryptIface, cryptLoad(_, _, _)).Times(1);
425
426 EXPECT_CALL(*mockCryptIface, cryptActivateByPassphrase(_, _, _, _, _, _))
John Wedig2443a022023-03-17 13:42:32 -0700427 .WillOnce(&createMappedDev);
John Wedigb810c922021-11-17 16:38:03 -0800428
John Wedig2443a022023-03-17 13:42:32 -0700429 EXPECT_CALL(*mockFsIface, runMkfs(StrEq(esObject->getCryptDevicePath())))
430 .WillOnce(Return(0));
John Wedigb810c922021-11-17 16:38:03 -0800431
John Wedigb17f8252022-01-12 14:24:26 -0800432 EXPECT_CALL(*mockFsIface, directoryExists(path(esObject->getMountPoint())))
433 .WillOnce(Return(false));
434
John Wedigb810c922021-11-17 16:38:03 -0800435 EXPECT_CALL(*mockFsIface, createDirectory(path(esObject->getMountPoint())))
436 .WillOnce(Return(true));
437
438 EXPECT_CALL(*mockFsIface,
John Wedig2443a022023-03-17 13:42:32 -0700439 doMount(StrEq(esObject->getCryptDevicePath()),
John Wedigb810c922021-11-17 16:38:03 -0800440 StrEq(esObject->getMountPoint()), _, _, _))
441 .WillOnce(Return(0));
442
443 EXPECT_CALL(*mockFsIface, doUnmount(StrEq(esObject->getMountPoint())))
444 .WillOnce(Return(0));
445
446 EXPECT_CALL(*mockFsIface, removeDirectory(path(esObject->getMountPoint())))
447 .WillOnce(Return(false));
448
John Wedig972c3fa2021-12-29 17:30:41 -0800449 esObject->formatLuks(password, Volume::FilesystemType::ext4);
John Wedigb810c922021-11-17 16:38:03 -0800450 EXPECT_FALSE(esObject->isLocked());
451
452 /* This will fail to remove the mount point. */
John Wedig972c3fa2021-12-29 17:30:41 -0800453 EXPECT_THROW(esObject->lock(), InternalFailure);
John Wedigb810c922021-11-17 16:38:03 -0800454 EXPECT_FALSE(esObject->isLocked());
John Wedig2443a022023-03-17 13:42:32 -0700455
456 EXPECT_EQ(0, removeMappedDev());
John Wedigb810c922021-11-17 16:38:03 -0800457}
458
459/* Test case where we fail to deactivate the LUKS device. */
Ed Tanous82897c32022-02-21 14:11:59 -0800460TEST_F(EStoragedTest, DeactivateFail)
John Wedigb810c922021-11-17 16:38:03 -0800461{
John Wedigb810c922021-11-17 16:38:03 -0800462 EXPECT_CALL(*mockCryptIface, cryptFormat(_, _, _, _, _, _, _, _)).Times(1);
463
464 EXPECT_CALL(*mockCryptIface, cryptKeyslotAddByVolumeKey(_, _, _, _, _, _))
465 .Times(1);
466
467 EXPECT_CALL(*mockCryptIface, cryptLoad(_, _, _)).Times(1);
468
469 EXPECT_CALL(*mockCryptIface, cryptActivateByPassphrase(_, _, _, _, _, _))
John Wedig2443a022023-03-17 13:42:32 -0700470 .WillOnce(&createMappedDev);
John Wedigb810c922021-11-17 16:38:03 -0800471
John Wedig2443a022023-03-17 13:42:32 -0700472 EXPECT_CALL(*mockFsIface, runMkfs(StrEq(esObject->getCryptDevicePath())))
473 .WillOnce(Return(0));
John Wedigb810c922021-11-17 16:38:03 -0800474
John Wedigb17f8252022-01-12 14:24:26 -0800475 EXPECT_CALL(*mockFsIface, directoryExists(path(esObject->getMountPoint())))
476 .WillOnce(Return(false));
477
John Wedigb810c922021-11-17 16:38:03 -0800478 EXPECT_CALL(*mockFsIface, createDirectory(path(esObject->getMountPoint())))
479 .WillOnce(Return(true));
480
481 EXPECT_CALL(*mockFsIface,
John Wedig2443a022023-03-17 13:42:32 -0700482 doMount(StrEq(esObject->getCryptDevicePath()),
John Wedigb810c922021-11-17 16:38:03 -0800483 StrEq(esObject->getMountPoint()), _, _, _))
484 .WillOnce(Return(0));
485
486 EXPECT_CALL(*mockFsIface, doUnmount(StrEq(esObject->getMountPoint())))
487 .WillOnce(Return(0));
488
489 EXPECT_CALL(*mockFsIface, removeDirectory(path(esObject->getMountPoint())))
490 .WillOnce(Return(true));
491
492 EXPECT_CALL(*mockCryptIface, cryptDeactivate(_, _)).WillOnce(Return(-1));
493
494 /* Format the encrypted device. */
John Wedig972c3fa2021-12-29 17:30:41 -0800495 esObject->formatLuks(password, Volume::FilesystemType::ext4);
John Wedigb810c922021-11-17 16:38:03 -0800496 EXPECT_FALSE(esObject->isLocked());
497
John Wedig972c3fa2021-12-29 17:30:41 -0800498 EXPECT_THROW(esObject->lock(), InternalFailure);
John Wedigb810c922021-11-17 16:38:03 -0800499 EXPECT_FALSE(esObject->isLocked());
John Wedig2443a022023-03-17 13:42:32 -0700500
501 EXPECT_EQ(0, removeMappedDev());
John Wedigb810c922021-11-17 16:38:03 -0800502}
503
John Wedig8d5a3a02022-09-29 15:25:58 -0700504/* Test case where we successfully change the password. */
505TEST_F(EStoragedTest, ChangePasswordSuccess)
506{
507 std::string newPasswordString("newPassword");
508 std::vector<uint8_t> newPassword(newPasswordString.begin(),
509 newPasswordString.end());
510
511 EXPECT_CALL(*mockCryptIface, cryptLoad(_, _, _)).Times(1);
512
513 EXPECT_CALL(*mockCryptIface,
514 cryptKeyslotChangeByPassphrase(
515 _, _, _, reinterpret_cast<const char*>(password.data()),
516 password.size(),
517 reinterpret_cast<const char*>(newPassword.data()),
518 newPassword.size()))
519 .WillOnce(Return(0));
520
521 /* Change the password for the LUKS-encrypted device. */
522 esObject->changePassword(password, newPassword);
523}
524
525/* Test case where we fail to change the password. */
526TEST_F(EStoragedTest, ChangePasswordFail)
527{
528 std::string newPasswordString("newPassword");
529 std::vector<uint8_t> newPassword(newPasswordString.begin(),
530 newPasswordString.end());
531
532 EXPECT_CALL(*mockCryptIface, cryptLoad(_, _, _)).Times(1);
533
534 EXPECT_CALL(*mockCryptIface,
535 cryptKeyslotChangeByPassphrase(
536 _, _, _, reinterpret_cast<const char*>(password.data()),
537 password.size(),
538 reinterpret_cast<const char*>(newPassword.data()),
539 newPassword.size()))
540 .WillOnce(Return(-1));
541
542 EXPECT_THROW(esObject->changePassword(password, newPassword),
543 InternalFailure);
544}
545
John Wedigb810c922021-11-17 16:38:03 -0800546} // namespace estoraged_test