blob: a0c56fe2f0fbc5e9c84c79b0f3d413bfca784e21 [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>
John Edward Broadbent59dffa62022-01-13 17:41:32 -080011#include <xyz/openbmc_project/Inventory/Item/Volume/client.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
John Wedig972c3fa2021-12-29 17:30:41 -080027using sdbusplus::xyz::openbmc_project::Common::Error::InternalFailure;
28using sdbusplus::xyz::openbmc_project::Common::Error::ResourceNotFound;
29using sdbusplus::xyz::openbmc_project::Inventory::Item::server::Volume;
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";
John Wedigb810c922021-11-17 16:38:03 -080047 std::ofstream testFile;
John Wedigb810c922021-11-17 16:38:03 -080048 std::string passwordString;
49 std::vector<uint8_t> password;
Ed Tanous82897c32022-02-21 14:11:59 -080050 MockCryptsetupInterface* mockCryptIface{};
51 MockFilesystemInterface* mockFsIface{};
John Wedig67a47442022-04-05 17:21:29 -070052 boost::asio::io_context io;
53 std::shared_ptr<sdbusplus::asio::connection> conn;
54 std::unique_ptr<sdbusplus::asio::object_server> objectServer;
55 std::unique_ptr<estoraged::EStoraged> esObject;
John Wedigb810c922021-11-17 16:38:03 -080056
Ed Tanous82897c32022-02-21 14:11:59 -080057 EStoragedTest() :
John Wedig67a47442022-04-05 17:21:29 -070058 passwordString("password"),
John Wedigb810c922021-11-17 16:38:03 -080059 password(passwordString.begin(), passwordString.end())
60 {}
61
62 void SetUp() override
63 {
64 /* Create an empty file that we'll pretend is a 'storage device'. */
65 testFile.open(testFileName,
66 std::ios::out | std::ios::binary | std::ios::trunc);
67 testFile.close();
68 if (testFile.fail())
69 {
70 throw std::runtime_error("Failed to open test file");
71 }
72
John Wedigb810c922021-11-17 16:38:03 -080073 std::unique_ptr<MockCryptsetupInterface> cryptIface =
74 std::make_unique<MockCryptsetupInterface>();
75 mockCryptIface = cryptIface.get();
76 std::unique_ptr<MockFilesystemInterface> fsIface =
77 std::make_unique<MockFilesystemInterface>();
78 mockFsIface = fsIface.get();
79
John Wedig2443a022023-03-17 13:42:32 -070080 /* Set up location of dummy mapped crypt file. */
81 EXPECT_CALL(*cryptIface, cryptGetDir).WillOnce(Return(testCryptDir));
82
John Wedig67a47442022-04-05 17:21:29 -070083 conn = std::make_shared<sdbusplus::asio::connection>(io);
84 // request D-Bus server name.
85 conn->request_name("xyz.openbmc_project.eStoraged.test");
86 objectServer = std::make_unique<sdbusplus::asio::object_server>(conn);
87
Ed Tanous82897c32022-02-21 14:11:59 -080088 esObject = std::make_unique<estoraged::EStoraged>(
John Wedig6c0d8ce2022-04-22 14:00:43 -070089 *objectServer, testConfigPath, testFileName, testLuksDevName,
John Wedigb4838302022-07-22 13:51:16 -070090 testSize, testLifeTime, testPartNumber, testSerialNumber,
91 std::move(cryptIface), std::move(fsIface));
John Wedigb810c922021-11-17 16:38:03 -080092 }
93
94 void TearDown() override
95 {
96 EXPECT_EQ(0, unlink(testFileName));
97 }
98};
99
John Wedig2443a022023-03-17 13:42:32 -0700100const char* mappedDevicePath = "/tmp/testfile_luksDev";
101std::ofstream mappedDevice;
102
103int createMappedDev()
104{
105 mappedDevice.open(mappedDevicePath,
106 std::ios::out | std::ios::binary | std::ios::trunc);
107 mappedDevice.close();
108 if (mappedDevice.fail())
109 {
110 throw std::runtime_error("Failed to open test mapped device");
111 }
112
113 return 0;
114}
115
116int removeMappedDev()
117{
118 EXPECT_EQ(0, unlink(mappedDevicePath));
119
120 return 0;
121}
122
John Wedigb810c922021-11-17 16:38:03 -0800123/* Test case to format and then lock the LUKS device. */
Ed Tanous82897c32022-02-21 14:11:59 -0800124TEST_F(EStoragedTest, FormatPass)
John Wedigb810c922021-11-17 16:38:03 -0800125{
John Wedigb810c922021-11-17 16:38:03 -0800126 EXPECT_CALL(*mockCryptIface, cryptFormat(_, _, _, _, _, _, _, _)).Times(1);
127
128 EXPECT_CALL(*mockCryptIface, cryptKeyslotAddByVolumeKey(_, _, _, _, _, _))
129 .Times(1);
130
131 EXPECT_CALL(*mockCryptIface, cryptLoad(_, _, _)).Times(1);
132
133 EXPECT_CALL(*mockCryptIface, cryptActivateByPassphrase(_, _, _, _, _, _))
John Wedig2443a022023-03-17 13:42:32 -0700134 .WillOnce(&createMappedDev);
John Wedigb810c922021-11-17 16:38:03 -0800135
John Wedig2443a022023-03-17 13:42:32 -0700136 EXPECT_CALL(*mockFsIface, runMkfs(StrEq(esObject->getCryptDevicePath())))
137 .WillOnce(Return(0));
John Wedigb810c922021-11-17 16:38:03 -0800138
John Wedigb17f8252022-01-12 14:24:26 -0800139 EXPECT_CALL(*mockFsIface, directoryExists(path(esObject->getMountPoint())))
140 .WillOnce(Return(false));
141
John Wedigb810c922021-11-17 16:38:03 -0800142 EXPECT_CALL(*mockFsIface, createDirectory(path(esObject->getMountPoint())))
143 .WillOnce(Return(true));
144
145 EXPECT_CALL(*mockFsIface,
John Wedig2443a022023-03-17 13:42:32 -0700146 doMount(StrEq(esObject->getCryptDevicePath()),
John Wedigb810c922021-11-17 16:38:03 -0800147 StrEq(esObject->getMountPoint()), _, _, _))
148 .WillOnce(Return(0));
149
150 EXPECT_CALL(*mockFsIface, doUnmount(StrEq(esObject->getMountPoint())))
151 .WillOnce(Return(0));
152
153 EXPECT_CALL(*mockFsIface, removeDirectory(path(esObject->getMountPoint())))
154 .WillOnce(Return(true));
155
John Wedig2443a022023-03-17 13:42:32 -0700156 EXPECT_CALL(*mockCryptIface, cryptDeactivate(_, _))
157 .WillOnce(&removeMappedDev);
John Wedigb810c922021-11-17 16:38:03 -0800158
159 /* Format the encrypted device. */
John Wedig972c3fa2021-12-29 17:30:41 -0800160 esObject->formatLuks(password, Volume::FilesystemType::ext4);
John Wedigb810c922021-11-17 16:38:03 -0800161 EXPECT_FALSE(esObject->isLocked());
162
John Wedig972c3fa2021-12-29 17:30:41 -0800163 esObject->lock();
John Wedigb810c922021-11-17 16:38:03 -0800164 EXPECT_TRUE(esObject->isLocked());
165}
166
John Wedigb17f8252022-01-12 14:24:26 -0800167/*
168 * Test case where the mount point directory already exists, so it shouldn't
169 * try to create it.
170 */
Ed Tanous82897c32022-02-21 14:11:59 -0800171TEST_F(EStoragedTest, MountPointExistsPass)
John Wedigb17f8252022-01-12 14:24:26 -0800172{
John Wedigb17f8252022-01-12 14:24:26 -0800173 EXPECT_CALL(*mockCryptIface, cryptFormat(_, _, _, _, _, _, _, _)).Times(1);
174
175 EXPECT_CALL(*mockCryptIface, cryptKeyslotAddByVolumeKey(_, _, _, _, _, _))
176 .Times(1);
177
178 EXPECT_CALL(*mockCryptIface, cryptLoad(_, _, _)).Times(1);
179
180 EXPECT_CALL(*mockCryptIface, cryptActivateByPassphrase(_, _, _, _, _, _))
John Wedig2443a022023-03-17 13:42:32 -0700181 .WillOnce(&createMappedDev);
John Wedigb17f8252022-01-12 14:24:26 -0800182
John Wedig2443a022023-03-17 13:42:32 -0700183 EXPECT_CALL(*mockFsIface, runMkfs(StrEq(esObject->getCryptDevicePath())))
184 .WillOnce(Return(0));
John Wedigb17f8252022-01-12 14:24:26 -0800185
186 EXPECT_CALL(*mockFsIface, directoryExists(path(esObject->getMountPoint())))
187 .WillOnce(Return(true));
188
189 EXPECT_CALL(*mockFsIface, createDirectory(path(esObject->getMountPoint())))
190 .Times(0);
191
192 EXPECT_CALL(*mockFsIface,
John Wedig2443a022023-03-17 13:42:32 -0700193 doMount(StrEq(esObject->getCryptDevicePath()),
John Wedigb17f8252022-01-12 14:24:26 -0800194 StrEq(esObject->getMountPoint()), _, _, _))
195 .WillOnce(Return(0));
196
197 EXPECT_CALL(*mockFsIface, doUnmount(StrEq(esObject->getMountPoint())))
198 .WillOnce(Return(0));
199
200 EXPECT_CALL(*mockFsIface, removeDirectory(path(esObject->getMountPoint())))
201 .WillOnce(Return(true));
202
John Wedig2443a022023-03-17 13:42:32 -0700203 EXPECT_CALL(*mockCryptIface, cryptDeactivate(_, _))
204 .WillOnce(&removeMappedDev);
John Wedigb17f8252022-01-12 14:24:26 -0800205
206 /* Format the encrypted device. */
207 esObject->formatLuks(password, Volume::FilesystemType::ext4);
208 EXPECT_FALSE(esObject->isLocked());
209
210 esObject->lock();
211 EXPECT_TRUE(esObject->isLocked());
212}
213
John Wedigb810c922021-11-17 16:38:03 -0800214/* Test case where the device/file doesn't exist. */
Ed Tanous82897c32022-02-21 14:11:59 -0800215TEST_F(EStoragedTest, FormatNoDeviceFail)
John Wedigb810c922021-11-17 16:38:03 -0800216{
217 /* Delete the test file. */
218 EXPECT_EQ(0, unlink(testFileName));
219
John Wedig972c3fa2021-12-29 17:30:41 -0800220 EXPECT_THROW(esObject->formatLuks(password, Volume::FilesystemType::ext4),
221 ResourceNotFound);
John Wedig2443a022023-03-17 13:42:32 -0700222 EXPECT_TRUE(esObject->isLocked());
John Wedigb810c922021-11-17 16:38:03 -0800223
224 /* Create the test file again, so that the TearDown function works. */
225 testFile.open(testFileName,
226 std::ios::out | std::ios::binary | std::ios::trunc);
227 testFile.close();
228}
229
230/* Test case where we fail to format the LUKS device. */
Ed Tanous82897c32022-02-21 14:11:59 -0800231TEST_F(EStoragedTest, FormatFail)
John Wedigb810c922021-11-17 16:38:03 -0800232{
233 EXPECT_CALL(*mockCryptIface, cryptFormat(_, _, _, _, _, _, _, _))
234 .WillOnce(Return(-1));
235
John Wedig972c3fa2021-12-29 17:30:41 -0800236 EXPECT_THROW(esObject->formatLuks(password, Volume::FilesystemType::ext4),
237 InternalFailure);
John Wedig2443a022023-03-17 13:42:32 -0700238 EXPECT_TRUE(esObject->isLocked());
John Wedigb810c922021-11-17 16:38:03 -0800239}
240
241/* Test case where we fail to set the password for the LUKS device. */
Ed Tanous82897c32022-02-21 14:11:59 -0800242TEST_F(EStoragedTest, AddKeyslotFail)
John Wedigb810c922021-11-17 16:38:03 -0800243{
John Wedigb810c922021-11-17 16:38:03 -0800244 EXPECT_CALL(*mockCryptIface, cryptFormat(_, _, _, _, _, _, _, _)).Times(1);
245
246 EXPECT_CALL(*mockCryptIface, cryptKeyslotAddByVolumeKey(_, _, _, _, _, _))
247 .WillOnce(Return(-1));
248
John Wedig972c3fa2021-12-29 17:30:41 -0800249 EXPECT_THROW(esObject->formatLuks(password, Volume::FilesystemType::ext4),
250 InternalFailure);
John Wedigb810c922021-11-17 16:38:03 -0800251 EXPECT_TRUE(esObject->isLocked());
252}
253
254/* Test case where we fail to load the LUKS header. */
Ed Tanous82897c32022-02-21 14:11:59 -0800255TEST_F(EStoragedTest, LoadLuksHeaderFail)
John Wedigb810c922021-11-17 16:38:03 -0800256{
John Wedigb810c922021-11-17 16:38:03 -0800257 EXPECT_CALL(*mockCryptIface, cryptFormat(_, _, _, _, _, _, _, _)).Times(1);
258
259 EXPECT_CALL(*mockCryptIface, cryptKeyslotAddByVolumeKey(_, _, _, _, _, _))
260 .Times(1);
261
262 EXPECT_CALL(*mockCryptIface, cryptLoad(_, _, _)).WillOnce(Return(-1));
263
John Wedig972c3fa2021-12-29 17:30:41 -0800264 EXPECT_THROW(esObject->formatLuks(password, Volume::FilesystemType::ext4),
265 InternalFailure);
John Wedigb810c922021-11-17 16:38:03 -0800266 EXPECT_TRUE(esObject->isLocked());
267}
268
269/* Test case where we fail to activate the LUKS device. */
Ed Tanous82897c32022-02-21 14:11:59 -0800270TEST_F(EStoragedTest, ActivateFail)
John Wedigb810c922021-11-17 16:38:03 -0800271{
John Wedigb810c922021-11-17 16:38:03 -0800272 EXPECT_CALL(*mockCryptIface, cryptFormat(_, _, _, _, _, _, _, _)).Times(1);
273
274 EXPECT_CALL(*mockCryptIface, cryptKeyslotAddByVolumeKey(_, _, _, _, _, _))
275 .Times(1);
276
277 EXPECT_CALL(*mockCryptIface, cryptLoad(_, _, _)).Times(1);
278
279 EXPECT_CALL(*mockCryptIface, cryptActivateByPassphrase(_, _, _, _, _, _))
280 .WillOnce(Return(-1));
281
John Wedig972c3fa2021-12-29 17:30:41 -0800282 EXPECT_THROW(esObject->formatLuks(password, Volume::FilesystemType::ext4),
283 InternalFailure);
John Wedigb810c922021-11-17 16:38:03 -0800284 EXPECT_TRUE(esObject->isLocked());
285}
286
287/* Test case where we fail to create the filesystem. */
Ed Tanous82897c32022-02-21 14:11:59 -0800288TEST_F(EStoragedTest, CreateFilesystemFail)
John Wedigb810c922021-11-17 16:38:03 -0800289{
John Wedigb810c922021-11-17 16:38:03 -0800290 EXPECT_CALL(*mockCryptIface, cryptFormat(_, _, _, _, _, _, _, _)).Times(1);
291
292 EXPECT_CALL(*mockCryptIface, cryptKeyslotAddByVolumeKey(_, _, _, _, _, _))
293 .Times(1);
294
295 EXPECT_CALL(*mockCryptIface, cryptLoad(_, _, _)).Times(1);
296
297 EXPECT_CALL(*mockCryptIface, cryptActivateByPassphrase(_, _, _, _, _, _))
John Wedig2443a022023-03-17 13:42:32 -0700298 .WillOnce(&createMappedDev);
John Wedigb810c922021-11-17 16:38:03 -0800299
John Wedig2443a022023-03-17 13:42:32 -0700300 EXPECT_CALL(*mockFsIface, runMkfs(StrEq(esObject->getCryptDevicePath())))
301 .WillOnce(Return(-1));
John Wedigb810c922021-11-17 16:38:03 -0800302
John Wedig972c3fa2021-12-29 17:30:41 -0800303 EXPECT_THROW(esObject->formatLuks(password, Volume::FilesystemType::ext4),
304 InternalFailure);
John Wedigb810c922021-11-17 16:38:03 -0800305 EXPECT_FALSE(esObject->isLocked());
John Wedig2443a022023-03-17 13:42:32 -0700306
307 EXPECT_EQ(0, removeMappedDev());
John Wedigb810c922021-11-17 16:38:03 -0800308}
309
310/* Test case where we fail to create the mount point. */
Ed Tanous82897c32022-02-21 14:11:59 -0800311TEST_F(EStoragedTest, CreateMountPointFail)
John Wedigb810c922021-11-17 16:38:03 -0800312{
John Wedigb810c922021-11-17 16:38:03 -0800313 EXPECT_CALL(*mockCryptIface, cryptFormat(_, _, _, _, _, _, _, _)).Times(1);
314
315 EXPECT_CALL(*mockCryptIface, cryptKeyslotAddByVolumeKey(_, _, _, _, _, _))
316 .Times(1);
317
318 EXPECT_CALL(*mockCryptIface, cryptLoad(_, _, _)).Times(1);
319
320 EXPECT_CALL(*mockCryptIface, cryptActivateByPassphrase(_, _, _, _, _, _))
John Wedig2443a022023-03-17 13:42:32 -0700321 .WillOnce(&createMappedDev);
John Wedigb810c922021-11-17 16:38:03 -0800322
John Wedig2443a022023-03-17 13:42:32 -0700323 EXPECT_CALL(*mockFsIface, runMkfs(StrEq(esObject->getCryptDevicePath())))
324 .WillOnce(Return(0));
John Wedigb810c922021-11-17 16:38:03 -0800325
John Wedigb17f8252022-01-12 14:24:26 -0800326 EXPECT_CALL(*mockFsIface, directoryExists(path(esObject->getMountPoint())))
327 .WillOnce(Return(false));
328
John Wedigb810c922021-11-17 16:38:03 -0800329 EXPECT_CALL(*mockFsIface, createDirectory(path(esObject->getMountPoint())))
330 .WillOnce(Return(false));
331
John Wedig972c3fa2021-12-29 17:30:41 -0800332 EXPECT_THROW(esObject->formatLuks(password, Volume::FilesystemType::ext4),
333 InternalFailure);
John Wedigb810c922021-11-17 16:38:03 -0800334 EXPECT_FALSE(esObject->isLocked());
John Wedig2443a022023-03-17 13:42:32 -0700335
336 EXPECT_EQ(0, removeMappedDev());
John Wedigb810c922021-11-17 16:38:03 -0800337}
338
339/* Test case where we fail to mount the filesystem. */
Ed Tanous82897c32022-02-21 14:11:59 -0800340TEST_F(EStoragedTest, MountFail)
John Wedigb810c922021-11-17 16:38:03 -0800341{
John Wedigb810c922021-11-17 16:38:03 -0800342 EXPECT_CALL(*mockCryptIface, cryptFormat(_, _, _, _, _, _, _, _)).Times(1);
343
344 EXPECT_CALL(*mockCryptIface, cryptKeyslotAddByVolumeKey(_, _, _, _, _, _))
345 .Times(1);
346
347 EXPECT_CALL(*mockCryptIface, cryptLoad(_, _, _)).Times(1);
348
349 EXPECT_CALL(*mockCryptIface, cryptActivateByPassphrase(_, _, _, _, _, _))
John Wedig2443a022023-03-17 13:42:32 -0700350 .WillOnce(&createMappedDev);
John Wedigb810c922021-11-17 16:38:03 -0800351
John Wedig2443a022023-03-17 13:42:32 -0700352 EXPECT_CALL(*mockFsIface, runMkfs(StrEq(esObject->getCryptDevicePath())))
353 .WillOnce(Return(0));
John Wedigb810c922021-11-17 16:38:03 -0800354
John Wedigb17f8252022-01-12 14:24:26 -0800355 EXPECT_CALL(*mockFsIface, directoryExists(path(esObject->getMountPoint())))
356 .WillOnce(Return(false));
357
John Wedigb810c922021-11-17 16:38:03 -0800358 EXPECT_CALL(*mockFsIface, createDirectory(path(esObject->getMountPoint())))
359 .WillOnce(Return(true));
360
361 EXPECT_CALL(*mockFsIface,
John Wedig2443a022023-03-17 13:42:32 -0700362 doMount(StrEq(esObject->getCryptDevicePath()),
John Wedigb810c922021-11-17 16:38:03 -0800363 StrEq(esObject->getMountPoint()), _, _, _))
364 .WillOnce(Return(-1));
365
366 EXPECT_CALL(*mockFsIface, removeDirectory(path(esObject->getMountPoint())))
367 .WillOnce(Return(true));
368
John Wedig972c3fa2021-12-29 17:30:41 -0800369 EXPECT_THROW(esObject->formatLuks(password, Volume::FilesystemType::ext4),
370 InternalFailure);
John Wedigb810c922021-11-17 16:38:03 -0800371 EXPECT_FALSE(esObject->isLocked());
John Wedig2443a022023-03-17 13:42:32 -0700372
373 EXPECT_EQ(0, removeMappedDev());
John Wedigb810c922021-11-17 16:38:03 -0800374}
375
376/* Test case where we fail to unmount the filesystem. */
Ed Tanous82897c32022-02-21 14:11:59 -0800377TEST_F(EStoragedTest, UnmountFail)
John Wedigb810c922021-11-17 16:38:03 -0800378{
John Wedigb810c922021-11-17 16:38:03 -0800379 EXPECT_CALL(*mockCryptIface, cryptFormat(_, _, _, _, _, _, _, _)).Times(1);
380
381 EXPECT_CALL(*mockCryptIface, cryptKeyslotAddByVolumeKey(_, _, _, _, _, _))
382 .Times(1);
383
384 EXPECT_CALL(*mockCryptIface, cryptLoad(_, _, _)).Times(1);
385
386 EXPECT_CALL(*mockCryptIface, cryptActivateByPassphrase(_, _, _, _, _, _))
John Wedig2443a022023-03-17 13:42:32 -0700387 .WillOnce(&createMappedDev);
John Wedigb810c922021-11-17 16:38:03 -0800388
John Wedig2443a022023-03-17 13:42:32 -0700389 EXPECT_CALL(*mockFsIface, runMkfs(StrEq(esObject->getCryptDevicePath())))
390 .WillOnce(Return(0));
John Wedigb810c922021-11-17 16:38:03 -0800391
John Wedigb17f8252022-01-12 14:24:26 -0800392 EXPECT_CALL(*mockFsIface, directoryExists(path(esObject->getMountPoint())))
393 .WillOnce(Return(false));
394
John Wedigb810c922021-11-17 16:38:03 -0800395 EXPECT_CALL(*mockFsIface, createDirectory(path(esObject->getMountPoint())))
396 .WillOnce(Return(true));
397
398 EXPECT_CALL(*mockFsIface,
John Wedig2443a022023-03-17 13:42:32 -0700399 doMount(StrEq(esObject->getCryptDevicePath()),
John Wedigb810c922021-11-17 16:38:03 -0800400 StrEq(esObject->getMountPoint()), _, _, _))
401 .WillOnce(Return(0));
402
403 EXPECT_CALL(*mockFsIface, doUnmount(StrEq(esObject->getMountPoint())))
404 .WillOnce(Return(-1));
405
John Wedig972c3fa2021-12-29 17:30:41 -0800406 esObject->formatLuks(password, Volume::FilesystemType::ext4);
John Wedigb810c922021-11-17 16:38:03 -0800407 EXPECT_FALSE(esObject->isLocked());
408
John Wedig972c3fa2021-12-29 17:30:41 -0800409 EXPECT_THROW(esObject->lock(), InternalFailure);
John Wedigb810c922021-11-17 16:38:03 -0800410 EXPECT_FALSE(esObject->isLocked());
John Wedig2443a022023-03-17 13:42:32 -0700411
412 EXPECT_EQ(0, removeMappedDev());
John Wedigb810c922021-11-17 16:38:03 -0800413}
414
415/* Test case where we fail to remove the mount point. */
Ed Tanous82897c32022-02-21 14:11:59 -0800416TEST_F(EStoragedTest, RemoveMountPointFail)
John Wedigb810c922021-11-17 16:38:03 -0800417{
John Wedigb810c922021-11-17 16:38:03 -0800418 EXPECT_CALL(*mockCryptIface, cryptFormat(_, _, _, _, _, _, _, _)).Times(1);
419
420 EXPECT_CALL(*mockCryptIface, cryptKeyslotAddByVolumeKey(_, _, _, _, _, _))
421 .Times(1);
422
423 EXPECT_CALL(*mockCryptIface, cryptLoad(_, _, _)).Times(1);
424
425 EXPECT_CALL(*mockCryptIface, cryptActivateByPassphrase(_, _, _, _, _, _))
John Wedig2443a022023-03-17 13:42:32 -0700426 .WillOnce(&createMappedDev);
John Wedigb810c922021-11-17 16:38:03 -0800427
John Wedig2443a022023-03-17 13:42:32 -0700428 EXPECT_CALL(*mockFsIface, runMkfs(StrEq(esObject->getCryptDevicePath())))
429 .WillOnce(Return(0));
John Wedigb810c922021-11-17 16:38:03 -0800430
John Wedigb17f8252022-01-12 14:24:26 -0800431 EXPECT_CALL(*mockFsIface, directoryExists(path(esObject->getMountPoint())))
432 .WillOnce(Return(false));
433
John Wedigb810c922021-11-17 16:38:03 -0800434 EXPECT_CALL(*mockFsIface, createDirectory(path(esObject->getMountPoint())))
435 .WillOnce(Return(true));
436
437 EXPECT_CALL(*mockFsIface,
John Wedig2443a022023-03-17 13:42:32 -0700438 doMount(StrEq(esObject->getCryptDevicePath()),
John Wedigb810c922021-11-17 16:38:03 -0800439 StrEq(esObject->getMountPoint()), _, _, _))
440 .WillOnce(Return(0));
441
442 EXPECT_CALL(*mockFsIface, doUnmount(StrEq(esObject->getMountPoint())))
443 .WillOnce(Return(0));
444
445 EXPECT_CALL(*mockFsIface, removeDirectory(path(esObject->getMountPoint())))
446 .WillOnce(Return(false));
447
John Wedig972c3fa2021-12-29 17:30:41 -0800448 esObject->formatLuks(password, Volume::FilesystemType::ext4);
John Wedigb810c922021-11-17 16:38:03 -0800449 EXPECT_FALSE(esObject->isLocked());
450
451 /* This will fail to remove the mount point. */
John Wedig972c3fa2021-12-29 17:30:41 -0800452 EXPECT_THROW(esObject->lock(), InternalFailure);
John Wedigb810c922021-11-17 16:38:03 -0800453 EXPECT_FALSE(esObject->isLocked());
John Wedig2443a022023-03-17 13:42:32 -0700454
455 EXPECT_EQ(0, removeMappedDev());
John Wedigb810c922021-11-17 16:38:03 -0800456}
457
458/* Test case where we fail to deactivate the LUKS device. */
Ed Tanous82897c32022-02-21 14:11:59 -0800459TEST_F(EStoragedTest, DeactivateFail)
John Wedigb810c922021-11-17 16:38:03 -0800460{
John Wedigb810c922021-11-17 16:38:03 -0800461 EXPECT_CALL(*mockCryptIface, cryptFormat(_, _, _, _, _, _, _, _)).Times(1);
462
463 EXPECT_CALL(*mockCryptIface, cryptKeyslotAddByVolumeKey(_, _, _, _, _, _))
464 .Times(1);
465
466 EXPECT_CALL(*mockCryptIface, cryptLoad(_, _, _)).Times(1);
467
468 EXPECT_CALL(*mockCryptIface, cryptActivateByPassphrase(_, _, _, _, _, _))
John Wedig2443a022023-03-17 13:42:32 -0700469 .WillOnce(&createMappedDev);
John Wedigb810c922021-11-17 16:38:03 -0800470
John Wedig2443a022023-03-17 13:42:32 -0700471 EXPECT_CALL(*mockFsIface, runMkfs(StrEq(esObject->getCryptDevicePath())))
472 .WillOnce(Return(0));
John Wedigb810c922021-11-17 16:38:03 -0800473
John Wedigb17f8252022-01-12 14:24:26 -0800474 EXPECT_CALL(*mockFsIface, directoryExists(path(esObject->getMountPoint())))
475 .WillOnce(Return(false));
476
John Wedigb810c922021-11-17 16:38:03 -0800477 EXPECT_CALL(*mockFsIface, createDirectory(path(esObject->getMountPoint())))
478 .WillOnce(Return(true));
479
480 EXPECT_CALL(*mockFsIface,
John Wedig2443a022023-03-17 13:42:32 -0700481 doMount(StrEq(esObject->getCryptDevicePath()),
John Wedigb810c922021-11-17 16:38:03 -0800482 StrEq(esObject->getMountPoint()), _, _, _))
483 .WillOnce(Return(0));
484
485 EXPECT_CALL(*mockFsIface, doUnmount(StrEq(esObject->getMountPoint())))
486 .WillOnce(Return(0));
487
488 EXPECT_CALL(*mockFsIface, removeDirectory(path(esObject->getMountPoint())))
489 .WillOnce(Return(true));
490
491 EXPECT_CALL(*mockCryptIface, cryptDeactivate(_, _)).WillOnce(Return(-1));
492
493 /* Format the encrypted device. */
John Wedig972c3fa2021-12-29 17:30:41 -0800494 esObject->formatLuks(password, Volume::FilesystemType::ext4);
John Wedigb810c922021-11-17 16:38:03 -0800495 EXPECT_FALSE(esObject->isLocked());
496
John Wedig972c3fa2021-12-29 17:30:41 -0800497 EXPECT_THROW(esObject->lock(), InternalFailure);
John Wedigb810c922021-11-17 16:38:03 -0800498 EXPECT_FALSE(esObject->isLocked());
John Wedig2443a022023-03-17 13:42:32 -0700499
500 EXPECT_EQ(0, removeMappedDev());
John Wedigb810c922021-11-17 16:38:03 -0800501}
502
John Wedig8d5a3a02022-09-29 15:25:58 -0700503/* Test case where we successfully change the password. */
504TEST_F(EStoragedTest, ChangePasswordSuccess)
505{
506 std::string newPasswordString("newPassword");
507 std::vector<uint8_t> newPassword(newPasswordString.begin(),
508 newPasswordString.end());
509
510 EXPECT_CALL(*mockCryptIface, cryptLoad(_, _, _)).Times(1);
511
512 EXPECT_CALL(*mockCryptIface,
513 cryptKeyslotChangeByPassphrase(
514 _, _, _, reinterpret_cast<const char*>(password.data()),
515 password.size(),
516 reinterpret_cast<const char*>(newPassword.data()),
517 newPassword.size()))
518 .WillOnce(Return(0));
519
520 /* Change the password for the LUKS-encrypted device. */
521 esObject->changePassword(password, newPassword);
522}
523
524/* Test case where we fail to change the password. */
525TEST_F(EStoragedTest, ChangePasswordFail)
526{
527 std::string newPasswordString("newPassword");
528 std::vector<uint8_t> newPassword(newPasswordString.begin(),
529 newPasswordString.end());
530
531 EXPECT_CALL(*mockCryptIface, cryptLoad(_, _, _)).Times(1);
532
533 EXPECT_CALL(*mockCryptIface,
534 cryptKeyslotChangeByPassphrase(
535 _, _, _, reinterpret_cast<const char*>(password.data()),
536 password.size(),
537 reinterpret_cast<const char*>(newPassword.data()),
538 newPassword.size()))
539 .WillOnce(Return(-1));
540
541 EXPECT_THROW(esObject->changePassword(password, newPassword),
542 InternalFailure);
543}
544
John Wedigb810c922021-11-17 16:38:03 -0800545} // namespace estoraged_test