blob: 520c7ecf01ae22f266ab0a3d5d5d6e5d4291e731 [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 Wedigb810c922021-11-17 16:38:03 -080049 std::ofstream testFile;
John Wedigb810c922021-11-17 16:38:03 -080050 std::string passwordString;
51 std::vector<uint8_t> password;
Ed Tanous82897c32022-02-21 14:11:59 -080052 MockCryptsetupInterface* mockCryptIface{};
53 MockFilesystemInterface* mockFsIface{};
John Wedig67a47442022-04-05 17:21:29 -070054 boost::asio::io_context io;
55 std::shared_ptr<sdbusplus::asio::connection> conn;
56 std::unique_ptr<sdbusplus::asio::object_server> objectServer;
57 std::unique_ptr<estoraged::EStoraged> esObject;
John Wedigb810c922021-11-17 16:38:03 -080058
Ed Tanous82897c32022-02-21 14:11:59 -080059 EStoragedTest() :
John Wedig67a47442022-04-05 17:21:29 -070060 passwordString("password"),
John Wedigb810c922021-11-17 16:38:03 -080061 password(passwordString.begin(), passwordString.end())
62 {}
63
64 void SetUp() override
65 {
66 /* Create an empty file that we'll pretend is a 'storage device'. */
67 testFile.open(testFileName,
68 std::ios::out | std::ios::binary | std::ios::trunc);
69 testFile.close();
70 if (testFile.fail())
71 {
72 throw std::runtime_error("Failed to open test file");
73 }
74
John Wedigb810c922021-11-17 16:38:03 -080075 std::unique_ptr<MockCryptsetupInterface> cryptIface =
76 std::make_unique<MockCryptsetupInterface>();
77 mockCryptIface = cryptIface.get();
78 std::unique_ptr<MockFilesystemInterface> fsIface =
79 std::make_unique<MockFilesystemInterface>();
80 mockFsIface = fsIface.get();
81
John Wedig2443a022023-03-17 13:42:32 -070082 /* Set up location of dummy mapped crypt file. */
83 EXPECT_CALL(*cryptIface, cryptGetDir).WillOnce(Return(testCryptDir));
84
John Wedig67a47442022-04-05 17:21:29 -070085 conn = std::make_shared<sdbusplus::asio::connection>(io);
86 // request D-Bus server name.
87 conn->request_name("xyz.openbmc_project.eStoraged.test");
88 objectServer = std::make_unique<sdbusplus::asio::object_server>(conn);
89
Ed Tanous82897c32022-02-21 14:11:59 -080090 esObject = std::make_unique<estoraged::EStoraged>(
John Wedig6c0d8ce2022-04-22 14:00:43 -070091 *objectServer, testConfigPath, testFileName, testLuksDevName,
John Wedigb4838302022-07-22 13:51:16 -070092 testSize, testLifeTime, testPartNumber, testSerialNumber,
Tom Tung043af592023-11-24 13:37:05 +080093 testLocationCode, ERASE_MAX_GEOMETRY, ERASE_MIN_GEOMETRY,
94 std::move(cryptIface), std::move(fsIface));
John Wedigb810c922021-11-17 16:38:03 -080095 }
96
97 void TearDown() override
98 {
99 EXPECT_EQ(0, unlink(testFileName));
100 }
101};
102
John Wedig2443a022023-03-17 13:42:32 -0700103const char* mappedDevicePath = "/tmp/testfile_luksDev";
104std::ofstream mappedDevice;
105
106int createMappedDev()
107{
108 mappedDevice.open(mappedDevicePath,
109 std::ios::out | std::ios::binary | std::ios::trunc);
110 mappedDevice.close();
111 if (mappedDevice.fail())
112 {
113 throw std::runtime_error("Failed to open test mapped device");
114 }
115
116 return 0;
117}
118
119int removeMappedDev()
120{
121 EXPECT_EQ(0, unlink(mappedDevicePath));
122
123 return 0;
124}
125
John Wedigb810c922021-11-17 16:38:03 -0800126/* Test case to format and then lock the LUKS device. */
Ed Tanous82897c32022-02-21 14:11:59 -0800127TEST_F(EStoragedTest, FormatPass)
John Wedigb810c922021-11-17 16:38:03 -0800128{
John Wedigb810c922021-11-17 16:38:03 -0800129 EXPECT_CALL(*mockCryptIface, cryptFormat(_, _, _, _, _, _, _, _)).Times(1);
130
131 EXPECT_CALL(*mockCryptIface, cryptKeyslotAddByVolumeKey(_, _, _, _, _, _))
132 .Times(1);
133
134 EXPECT_CALL(*mockCryptIface, cryptLoad(_, _, _)).Times(1);
135
136 EXPECT_CALL(*mockCryptIface, cryptActivateByPassphrase(_, _, _, _, _, _))
John Wedig2443a022023-03-17 13:42:32 -0700137 .WillOnce(&createMappedDev);
John Wedigb810c922021-11-17 16:38:03 -0800138
John Wedig2443a022023-03-17 13:42:32 -0700139 EXPECT_CALL(*mockFsIface, runMkfs(StrEq(esObject->getCryptDevicePath())))
140 .WillOnce(Return(0));
John Wedigb810c922021-11-17 16:38:03 -0800141
John Wedigb17f8252022-01-12 14:24:26 -0800142 EXPECT_CALL(*mockFsIface, directoryExists(path(esObject->getMountPoint())))
143 .WillOnce(Return(false));
144
John Wedigb810c922021-11-17 16:38:03 -0800145 EXPECT_CALL(*mockFsIface, createDirectory(path(esObject->getMountPoint())))
146 .WillOnce(Return(true));
147
148 EXPECT_CALL(*mockFsIface,
John Wedig2443a022023-03-17 13:42:32 -0700149 doMount(StrEq(esObject->getCryptDevicePath()),
John Wedigb810c922021-11-17 16:38:03 -0800150 StrEq(esObject->getMountPoint()), _, _, _))
151 .WillOnce(Return(0));
152
153 EXPECT_CALL(*mockFsIface, doUnmount(StrEq(esObject->getMountPoint())))
154 .WillOnce(Return(0));
155
156 EXPECT_CALL(*mockFsIface, removeDirectory(path(esObject->getMountPoint())))
157 .WillOnce(Return(true));
158
John Wedig2443a022023-03-17 13:42:32 -0700159 EXPECT_CALL(*mockCryptIface, cryptDeactivate(_, _))
160 .WillOnce(&removeMappedDev);
John Wedigb810c922021-11-17 16:38:03 -0800161
162 /* Format the encrypted device. */
John Wedig972c3fa2021-12-29 17:30:41 -0800163 esObject->formatLuks(password, Volume::FilesystemType::ext4);
John Wedigb810c922021-11-17 16:38:03 -0800164 EXPECT_FALSE(esObject->isLocked());
165
John Wedig972c3fa2021-12-29 17:30:41 -0800166 esObject->lock();
John Wedigb810c922021-11-17 16:38:03 -0800167 EXPECT_TRUE(esObject->isLocked());
168}
169
John Wedigb17f8252022-01-12 14:24:26 -0800170/*
171 * Test case where the mount point directory already exists, so it shouldn't
172 * try to create it.
173 */
Ed Tanous82897c32022-02-21 14:11:59 -0800174TEST_F(EStoragedTest, MountPointExistsPass)
John Wedigb17f8252022-01-12 14:24:26 -0800175{
John Wedigb17f8252022-01-12 14:24:26 -0800176 EXPECT_CALL(*mockCryptIface, cryptFormat(_, _, _, _, _, _, _, _)).Times(1);
177
178 EXPECT_CALL(*mockCryptIface, cryptKeyslotAddByVolumeKey(_, _, _, _, _, _))
179 .Times(1);
180
181 EXPECT_CALL(*mockCryptIface, cryptLoad(_, _, _)).Times(1);
182
183 EXPECT_CALL(*mockCryptIface, cryptActivateByPassphrase(_, _, _, _, _, _))
John Wedig2443a022023-03-17 13:42:32 -0700184 .WillOnce(&createMappedDev);
John Wedigb17f8252022-01-12 14:24:26 -0800185
John Wedig2443a022023-03-17 13:42:32 -0700186 EXPECT_CALL(*mockFsIface, runMkfs(StrEq(esObject->getCryptDevicePath())))
187 .WillOnce(Return(0));
John Wedigb17f8252022-01-12 14:24:26 -0800188
189 EXPECT_CALL(*mockFsIface, directoryExists(path(esObject->getMountPoint())))
190 .WillOnce(Return(true));
191
192 EXPECT_CALL(*mockFsIface, createDirectory(path(esObject->getMountPoint())))
193 .Times(0);
194
195 EXPECT_CALL(*mockFsIface,
John Wedig2443a022023-03-17 13:42:32 -0700196 doMount(StrEq(esObject->getCryptDevicePath()),
John Wedigb17f8252022-01-12 14:24:26 -0800197 StrEq(esObject->getMountPoint()), _, _, _))
198 .WillOnce(Return(0));
199
200 EXPECT_CALL(*mockFsIface, doUnmount(StrEq(esObject->getMountPoint())))
201 .WillOnce(Return(0));
202
203 EXPECT_CALL(*mockFsIface, removeDirectory(path(esObject->getMountPoint())))
204 .WillOnce(Return(true));
205
John Wedig2443a022023-03-17 13:42:32 -0700206 EXPECT_CALL(*mockCryptIface, cryptDeactivate(_, _))
207 .WillOnce(&removeMappedDev);
John Wedigb17f8252022-01-12 14:24:26 -0800208
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 Wedig2443a022023-03-17 13:42:32 -0700225 EXPECT_TRUE(esObject->isLocked());
John Wedigb810c922021-11-17 16:38:03 -0800226
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 Wedig2443a022023-03-17 13:42:32 -0700241 EXPECT_TRUE(esObject->isLocked());
John Wedigb810c922021-11-17 16:38:03 -0800242}
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{
John Wedigb810c922021-11-17 16:38:03 -0800247 EXPECT_CALL(*mockCryptIface, cryptFormat(_, _, _, _, _, _, _, _)).Times(1);
248
249 EXPECT_CALL(*mockCryptIface, cryptKeyslotAddByVolumeKey(_, _, _, _, _, _))
250 .WillOnce(Return(-1));
251
John Wedig972c3fa2021-12-29 17:30:41 -0800252 EXPECT_THROW(esObject->formatLuks(password, Volume::FilesystemType::ext4),
253 InternalFailure);
John Wedigb810c922021-11-17 16:38:03 -0800254 EXPECT_TRUE(esObject->isLocked());
255}
256
257/* Test case where we fail to load the LUKS header. */
Ed Tanous82897c32022-02-21 14:11:59 -0800258TEST_F(EStoragedTest, LoadLuksHeaderFail)
John Wedigb810c922021-11-17 16:38:03 -0800259{
John Wedigb810c922021-11-17 16:38:03 -0800260 EXPECT_CALL(*mockCryptIface, cryptFormat(_, _, _, _, _, _, _, _)).Times(1);
261
262 EXPECT_CALL(*mockCryptIface, cryptKeyslotAddByVolumeKey(_, _, _, _, _, _))
263 .Times(1);
264
265 EXPECT_CALL(*mockCryptIface, cryptLoad(_, _, _)).WillOnce(Return(-1));
266
John Wedig972c3fa2021-12-29 17:30:41 -0800267 EXPECT_THROW(esObject->formatLuks(password, Volume::FilesystemType::ext4),
268 InternalFailure);
John Wedigb810c922021-11-17 16:38:03 -0800269 EXPECT_TRUE(esObject->isLocked());
270}
271
272/* Test case where we fail to activate the LUKS device. */
Ed Tanous82897c32022-02-21 14:11:59 -0800273TEST_F(EStoragedTest, ActivateFail)
John Wedigb810c922021-11-17 16:38:03 -0800274{
John Wedigb810c922021-11-17 16:38:03 -0800275 EXPECT_CALL(*mockCryptIface, cryptFormat(_, _, _, _, _, _, _, _)).Times(1);
276
277 EXPECT_CALL(*mockCryptIface, cryptKeyslotAddByVolumeKey(_, _, _, _, _, _))
278 .Times(1);
279
280 EXPECT_CALL(*mockCryptIface, cryptLoad(_, _, _)).Times(1);
281
282 EXPECT_CALL(*mockCryptIface, cryptActivateByPassphrase(_, _, _, _, _, _))
283 .WillOnce(Return(-1));
284
John Wedig972c3fa2021-12-29 17:30:41 -0800285 EXPECT_THROW(esObject->formatLuks(password, Volume::FilesystemType::ext4),
286 InternalFailure);
John Wedigb810c922021-11-17 16:38:03 -0800287 EXPECT_TRUE(esObject->isLocked());
288}
289
290/* Test case where we fail to create the filesystem. */
Ed Tanous82897c32022-02-21 14:11:59 -0800291TEST_F(EStoragedTest, CreateFilesystemFail)
John Wedigb810c922021-11-17 16:38:03 -0800292{
John Wedigb810c922021-11-17 16:38:03 -0800293 EXPECT_CALL(*mockCryptIface, cryptFormat(_, _, _, _, _, _, _, _)).Times(1);
294
295 EXPECT_CALL(*mockCryptIface, cryptKeyslotAddByVolumeKey(_, _, _, _, _, _))
296 .Times(1);
297
298 EXPECT_CALL(*mockCryptIface, cryptLoad(_, _, _)).Times(1);
299
300 EXPECT_CALL(*mockCryptIface, cryptActivateByPassphrase(_, _, _, _, _, _))
John Wedig2443a022023-03-17 13:42:32 -0700301 .WillOnce(&createMappedDev);
John Wedigb810c922021-11-17 16:38:03 -0800302
John Wedig2443a022023-03-17 13:42:32 -0700303 EXPECT_CALL(*mockFsIface, runMkfs(StrEq(esObject->getCryptDevicePath())))
304 .WillOnce(Return(-1));
John Wedigb810c922021-11-17 16:38:03 -0800305
John Wedig972c3fa2021-12-29 17:30:41 -0800306 EXPECT_THROW(esObject->formatLuks(password, Volume::FilesystemType::ext4),
307 InternalFailure);
John Wedigb810c922021-11-17 16:38:03 -0800308 EXPECT_FALSE(esObject->isLocked());
John Wedig2443a022023-03-17 13:42:32 -0700309
310 EXPECT_EQ(0, removeMappedDev());
John Wedigb810c922021-11-17 16:38:03 -0800311}
312
313/* Test case where we fail to create the mount point. */
Ed Tanous82897c32022-02-21 14:11:59 -0800314TEST_F(EStoragedTest, CreateMountPointFail)
John Wedigb810c922021-11-17 16:38:03 -0800315{
John Wedigb810c922021-11-17 16:38:03 -0800316 EXPECT_CALL(*mockCryptIface, cryptFormat(_, _, _, _, _, _, _, _)).Times(1);
317
318 EXPECT_CALL(*mockCryptIface, cryptKeyslotAddByVolumeKey(_, _, _, _, _, _))
319 .Times(1);
320
321 EXPECT_CALL(*mockCryptIface, cryptLoad(_, _, _)).Times(1);
322
323 EXPECT_CALL(*mockCryptIface, cryptActivateByPassphrase(_, _, _, _, _, _))
John Wedig2443a022023-03-17 13:42:32 -0700324 .WillOnce(&createMappedDev);
John Wedigb810c922021-11-17 16:38:03 -0800325
John Wedig2443a022023-03-17 13:42:32 -0700326 EXPECT_CALL(*mockFsIface, runMkfs(StrEq(esObject->getCryptDevicePath())))
327 .WillOnce(Return(0));
John Wedigb810c922021-11-17 16:38:03 -0800328
John Wedigb17f8252022-01-12 14:24:26 -0800329 EXPECT_CALL(*mockFsIface, directoryExists(path(esObject->getMountPoint())))
330 .WillOnce(Return(false));
331
John Wedigb810c922021-11-17 16:38:03 -0800332 EXPECT_CALL(*mockFsIface, createDirectory(path(esObject->getMountPoint())))
333 .WillOnce(Return(false));
334
John Wedig972c3fa2021-12-29 17:30:41 -0800335 EXPECT_THROW(esObject->formatLuks(password, Volume::FilesystemType::ext4),
336 InternalFailure);
John Wedigb810c922021-11-17 16:38:03 -0800337 EXPECT_FALSE(esObject->isLocked());
John Wedig2443a022023-03-17 13:42:32 -0700338
339 EXPECT_EQ(0, removeMappedDev());
John Wedigb810c922021-11-17 16:38:03 -0800340}
341
342/* Test case where we fail to mount the filesystem. */
Ed Tanous82897c32022-02-21 14:11:59 -0800343TEST_F(EStoragedTest, MountFail)
John Wedigb810c922021-11-17 16:38:03 -0800344{
John Wedigb810c922021-11-17 16:38:03 -0800345 EXPECT_CALL(*mockCryptIface, cryptFormat(_, _, _, _, _, _, _, _)).Times(1);
346
347 EXPECT_CALL(*mockCryptIface, cryptKeyslotAddByVolumeKey(_, _, _, _, _, _))
348 .Times(1);
349
350 EXPECT_CALL(*mockCryptIface, cryptLoad(_, _, _)).Times(1);
351
352 EXPECT_CALL(*mockCryptIface, cryptActivateByPassphrase(_, _, _, _, _, _))
John Wedig2443a022023-03-17 13:42:32 -0700353 .WillOnce(&createMappedDev);
John Wedigb810c922021-11-17 16:38:03 -0800354
John Wedig2443a022023-03-17 13:42:32 -0700355 EXPECT_CALL(*mockFsIface, runMkfs(StrEq(esObject->getCryptDevicePath())))
356 .WillOnce(Return(0));
John Wedigb810c922021-11-17 16:38:03 -0800357
John Wedigb17f8252022-01-12 14:24:26 -0800358 EXPECT_CALL(*mockFsIface, directoryExists(path(esObject->getMountPoint())))
359 .WillOnce(Return(false));
360
John Wedigb810c922021-11-17 16:38:03 -0800361 EXPECT_CALL(*mockFsIface, createDirectory(path(esObject->getMountPoint())))
362 .WillOnce(Return(true));
363
364 EXPECT_CALL(*mockFsIface,
John Wedig2443a022023-03-17 13:42:32 -0700365 doMount(StrEq(esObject->getCryptDevicePath()),
John Wedigb810c922021-11-17 16:38:03 -0800366 StrEq(esObject->getMountPoint()), _, _, _))
367 .WillOnce(Return(-1));
368
369 EXPECT_CALL(*mockFsIface, removeDirectory(path(esObject->getMountPoint())))
370 .WillOnce(Return(true));
371
John Wedig972c3fa2021-12-29 17:30:41 -0800372 EXPECT_THROW(esObject->formatLuks(password, Volume::FilesystemType::ext4),
373 InternalFailure);
John Wedigb810c922021-11-17 16:38:03 -0800374 EXPECT_FALSE(esObject->isLocked());
John Wedig2443a022023-03-17 13:42:32 -0700375
376 EXPECT_EQ(0, removeMappedDev());
John Wedigb810c922021-11-17 16:38:03 -0800377}
378
379/* Test case where we fail to unmount the filesystem. */
Ed Tanous82897c32022-02-21 14:11:59 -0800380TEST_F(EStoragedTest, UnmountFail)
John Wedigb810c922021-11-17 16:38:03 -0800381{
John Wedigb810c922021-11-17 16:38:03 -0800382 EXPECT_CALL(*mockCryptIface, cryptFormat(_, _, _, _, _, _, _, _)).Times(1);
383
384 EXPECT_CALL(*mockCryptIface, cryptKeyslotAddByVolumeKey(_, _, _, _, _, _))
385 .Times(1);
386
387 EXPECT_CALL(*mockCryptIface, cryptLoad(_, _, _)).Times(1);
388
389 EXPECT_CALL(*mockCryptIface, cryptActivateByPassphrase(_, _, _, _, _, _))
John Wedig2443a022023-03-17 13:42:32 -0700390 .WillOnce(&createMappedDev);
John Wedigb810c922021-11-17 16:38:03 -0800391
John Wedig2443a022023-03-17 13:42:32 -0700392 EXPECT_CALL(*mockFsIface, runMkfs(StrEq(esObject->getCryptDevicePath())))
393 .WillOnce(Return(0));
John Wedigb810c922021-11-17 16:38:03 -0800394
John Wedigb17f8252022-01-12 14:24:26 -0800395 EXPECT_CALL(*mockFsIface, directoryExists(path(esObject->getMountPoint())))
396 .WillOnce(Return(false));
397
John Wedigb810c922021-11-17 16:38:03 -0800398 EXPECT_CALL(*mockFsIface, createDirectory(path(esObject->getMountPoint())))
399 .WillOnce(Return(true));
400
401 EXPECT_CALL(*mockFsIface,
John Wedig2443a022023-03-17 13:42:32 -0700402 doMount(StrEq(esObject->getCryptDevicePath()),
John Wedigb810c922021-11-17 16:38:03 -0800403 StrEq(esObject->getMountPoint()), _, _, _))
404 .WillOnce(Return(0));
405
406 EXPECT_CALL(*mockFsIface, doUnmount(StrEq(esObject->getMountPoint())))
407 .WillOnce(Return(-1));
408
John Wedig972c3fa2021-12-29 17:30:41 -0800409 esObject->formatLuks(password, Volume::FilesystemType::ext4);
John Wedigb810c922021-11-17 16:38:03 -0800410 EXPECT_FALSE(esObject->isLocked());
411
John Wedig972c3fa2021-12-29 17:30:41 -0800412 EXPECT_THROW(esObject->lock(), InternalFailure);
John Wedigb810c922021-11-17 16:38:03 -0800413 EXPECT_FALSE(esObject->isLocked());
John Wedig2443a022023-03-17 13:42:32 -0700414
415 EXPECT_EQ(0, removeMappedDev());
John Wedigb810c922021-11-17 16:38:03 -0800416}
417
418/* Test case where we fail to remove the mount point. */
Ed Tanous82897c32022-02-21 14:11:59 -0800419TEST_F(EStoragedTest, RemoveMountPointFail)
John Wedigb810c922021-11-17 16:38:03 -0800420{
John Wedigb810c922021-11-17 16:38:03 -0800421 EXPECT_CALL(*mockCryptIface, cryptFormat(_, _, _, _, _, _, _, _)).Times(1);
422
423 EXPECT_CALL(*mockCryptIface, cryptKeyslotAddByVolumeKey(_, _, _, _, _, _))
424 .Times(1);
425
426 EXPECT_CALL(*mockCryptIface, cryptLoad(_, _, _)).Times(1);
427
428 EXPECT_CALL(*mockCryptIface, cryptActivateByPassphrase(_, _, _, _, _, _))
John Wedig2443a022023-03-17 13:42:32 -0700429 .WillOnce(&createMappedDev);
John Wedigb810c922021-11-17 16:38:03 -0800430
John Wedig2443a022023-03-17 13:42:32 -0700431 EXPECT_CALL(*mockFsIface, runMkfs(StrEq(esObject->getCryptDevicePath())))
432 .WillOnce(Return(0));
John Wedigb810c922021-11-17 16:38:03 -0800433
John Wedigb17f8252022-01-12 14:24:26 -0800434 EXPECT_CALL(*mockFsIface, directoryExists(path(esObject->getMountPoint())))
435 .WillOnce(Return(false));
436
John Wedigb810c922021-11-17 16:38:03 -0800437 EXPECT_CALL(*mockFsIface, createDirectory(path(esObject->getMountPoint())))
438 .WillOnce(Return(true));
439
440 EXPECT_CALL(*mockFsIface,
John Wedig2443a022023-03-17 13:42:32 -0700441 doMount(StrEq(esObject->getCryptDevicePath()),
John Wedigb810c922021-11-17 16:38:03 -0800442 StrEq(esObject->getMountPoint()), _, _, _))
443 .WillOnce(Return(0));
444
445 EXPECT_CALL(*mockFsIface, doUnmount(StrEq(esObject->getMountPoint())))
446 .WillOnce(Return(0));
447
448 EXPECT_CALL(*mockFsIface, removeDirectory(path(esObject->getMountPoint())))
449 .WillOnce(Return(false));
450
John Wedig972c3fa2021-12-29 17:30:41 -0800451 esObject->formatLuks(password, Volume::FilesystemType::ext4);
John Wedigb810c922021-11-17 16:38:03 -0800452 EXPECT_FALSE(esObject->isLocked());
453
454 /* This will fail to remove the mount point. */
John Wedig972c3fa2021-12-29 17:30:41 -0800455 EXPECT_THROW(esObject->lock(), InternalFailure);
John Wedigb810c922021-11-17 16:38:03 -0800456 EXPECT_FALSE(esObject->isLocked());
John Wedig2443a022023-03-17 13:42:32 -0700457
458 EXPECT_EQ(0, removeMappedDev());
John Wedigb810c922021-11-17 16:38:03 -0800459}
460
461/* Test case where we fail to deactivate the LUKS device. */
Ed Tanous82897c32022-02-21 14:11:59 -0800462TEST_F(EStoragedTest, DeactivateFail)
John Wedigb810c922021-11-17 16:38:03 -0800463{
John Wedigb810c922021-11-17 16:38:03 -0800464 EXPECT_CALL(*mockCryptIface, cryptFormat(_, _, _, _, _, _, _, _)).Times(1);
465
466 EXPECT_CALL(*mockCryptIface, cryptKeyslotAddByVolumeKey(_, _, _, _, _, _))
467 .Times(1);
468
469 EXPECT_CALL(*mockCryptIface, cryptLoad(_, _, _)).Times(1);
470
471 EXPECT_CALL(*mockCryptIface, cryptActivateByPassphrase(_, _, _, _, _, _))
John Wedig2443a022023-03-17 13:42:32 -0700472 .WillOnce(&createMappedDev);
John Wedigb810c922021-11-17 16:38:03 -0800473
John Wedig2443a022023-03-17 13:42:32 -0700474 EXPECT_CALL(*mockFsIface, runMkfs(StrEq(esObject->getCryptDevicePath())))
475 .WillOnce(Return(0));
John Wedigb810c922021-11-17 16:38:03 -0800476
John Wedigb17f8252022-01-12 14:24:26 -0800477 EXPECT_CALL(*mockFsIface, directoryExists(path(esObject->getMountPoint())))
478 .WillOnce(Return(false));
479
John Wedigb810c922021-11-17 16:38:03 -0800480 EXPECT_CALL(*mockFsIface, createDirectory(path(esObject->getMountPoint())))
481 .WillOnce(Return(true));
482
483 EXPECT_CALL(*mockFsIface,
John Wedig2443a022023-03-17 13:42:32 -0700484 doMount(StrEq(esObject->getCryptDevicePath()),
John Wedigb810c922021-11-17 16:38:03 -0800485 StrEq(esObject->getMountPoint()), _, _, _))
486 .WillOnce(Return(0));
487
488 EXPECT_CALL(*mockFsIface, doUnmount(StrEq(esObject->getMountPoint())))
489 .WillOnce(Return(0));
490
491 EXPECT_CALL(*mockFsIface, removeDirectory(path(esObject->getMountPoint())))
492 .WillOnce(Return(true));
493
494 EXPECT_CALL(*mockCryptIface, cryptDeactivate(_, _)).WillOnce(Return(-1));
495
496 /* Format the encrypted device. */
John Wedig972c3fa2021-12-29 17:30:41 -0800497 esObject->formatLuks(password, Volume::FilesystemType::ext4);
John Wedigb810c922021-11-17 16:38:03 -0800498 EXPECT_FALSE(esObject->isLocked());
499
John Wedig972c3fa2021-12-29 17:30:41 -0800500 EXPECT_THROW(esObject->lock(), InternalFailure);
John Wedigb810c922021-11-17 16:38:03 -0800501 EXPECT_FALSE(esObject->isLocked());
John Wedig2443a022023-03-17 13:42:32 -0700502
503 EXPECT_EQ(0, removeMappedDev());
John Wedigb810c922021-11-17 16:38:03 -0800504}
505
John Wedig8d5a3a02022-09-29 15:25:58 -0700506/* Test case where we successfully change the password. */
507TEST_F(EStoragedTest, ChangePasswordSuccess)
508{
509 std::string newPasswordString("newPassword");
510 std::vector<uint8_t> newPassword(newPasswordString.begin(),
511 newPasswordString.end());
512
513 EXPECT_CALL(*mockCryptIface, cryptLoad(_, _, _)).Times(1);
514
515 EXPECT_CALL(*mockCryptIface,
516 cryptKeyslotChangeByPassphrase(
517 _, _, _, reinterpret_cast<const char*>(password.data()),
518 password.size(),
519 reinterpret_cast<const char*>(newPassword.data()),
520 newPassword.size()))
521 .WillOnce(Return(0));
522
523 /* Change the password for the LUKS-encrypted device. */
524 esObject->changePassword(password, newPassword);
525}
526
527/* Test case where we fail to change the password. */
528TEST_F(EStoragedTest, ChangePasswordFail)
529{
530 std::string newPasswordString("newPassword");
531 std::vector<uint8_t> newPassword(newPasswordString.begin(),
532 newPasswordString.end());
533
534 EXPECT_CALL(*mockCryptIface, cryptLoad(_, _, _)).Times(1);
535
536 EXPECT_CALL(*mockCryptIface,
537 cryptKeyslotChangeByPassphrase(
538 _, _, _, reinterpret_cast<const char*>(password.data()),
539 password.size(),
540 reinterpret_cast<const char*>(newPassword.data()),
541 newPassword.size()))
542 .WillOnce(Return(-1));
543
544 EXPECT_THROW(esObject->changePassword(password, newPassword),
545 InternalFailure);
546}
547
John Wedigb810c922021-11-17 16:38:03 -0800548} // namespace estoraged_test