| John Edward Broadbent | 59dffa6 | 2022-01-13 17:41:32 -0800 | [diff] [blame] | 1 | #include "estoraged_test.hpp" |
| John Wedig | b810c92 | 2021-11-17 16:38:03 -0800 | [diff] [blame] | 2 | |
| John Wedig | b810c92 | 2021-11-17 16:38:03 -0800 | [diff] [blame] | 3 | #include "estoraged.hpp" |
| Tom Tung | 043af59 | 2023-11-24 13:37:05 +0800 | [diff] [blame] | 4 | #include "estoraged_conf.hpp" |
| John Wedig | b810c92 | 2021-11-17 16:38:03 -0800 | [diff] [blame] | 5 | |
| 6 | #include <unistd.h> |
| 7 | |
| John Wedig | 67a4744 | 2022-04-05 17:21:29 -0700 | [diff] [blame] | 8 | #include <boost/asio/io_context.hpp> |
| 9 | #include <sdbusplus/asio/connection.hpp> |
| 10 | #include <sdbusplus/asio/object_server.hpp> |
| John Wedig | 972c3fa | 2021-12-29 17:30:41 -0800 | [diff] [blame] | 11 | #include <xyz/openbmc_project/Common/error.hpp> |
| Patrick Williams | 0e2a46f | 2023-05-10 14:35:52 -0500 | [diff] [blame] | 12 | #include <xyz/openbmc_project/Inventory/Item/Volume/server.hpp> |
| John Wedig | b810c92 | 2021-11-17 16:38:03 -0800 | [diff] [blame] | 13 | |
| 14 | #include <exception> |
| 15 | #include <filesystem> |
| 16 | #include <fstream> |
| 17 | #include <iterator> |
| John Wedig | 67a4744 | 2022-04-05 17:21:29 -0700 | [diff] [blame] | 18 | #include <memory> |
| John Wedig | b810c92 | 2021-11-17 16:38:03 -0800 | [diff] [blame] | 19 | #include <string> |
| 20 | #include <vector> |
| 21 | |
| 22 | #include <gmock/gmock.h> |
| 23 | #include <gtest/gtest.h> |
| 24 | |
| 25 | namespace estoraged_test |
| 26 | { |
| 27 | |
| Patrick Williams | 0e2a46f | 2023-05-10 14:35:52 -0500 | [diff] [blame] | 28 | using sdbusplus::server::xyz::openbmc_project::inventory::item::Volume; |
| John Wedig | 972c3fa | 2021-12-29 17:30:41 -0800 | [diff] [blame] | 29 | using sdbusplus::xyz::openbmc_project::Common::Error::InternalFailure; |
| 30 | using sdbusplus::xyz::openbmc_project::Common::Error::ResourceNotFound; |
| John Wedig | b810c92 | 2021-11-17 16:38:03 -0800 | [diff] [blame] | 31 | using std::filesystem::path; |
| 32 | using ::testing::_; |
| John Wedig | b810c92 | 2021-11-17 16:38:03 -0800 | [diff] [blame] | 33 | using ::testing::Return; |
| 34 | using ::testing::StrEq; |
| 35 | |
| Ed Tanous | 82897c3 | 2022-02-21 14:11:59 -0800 | [diff] [blame] | 36 | class EStoragedTest : public testing::Test |
| John Wedig | b810c92 | 2021-11-17 16:38:03 -0800 | [diff] [blame] | 37 | { |
| 38 | public: |
| Ed Tanous | 82897c3 | 2022-02-21 14:11:59 -0800 | [diff] [blame] | 39 | const char* testFileName = "testfile"; |
| 40 | const char* testLuksDevName = "testfile_luksDev"; |
| John Wedig | 2443a02 | 2023-03-17 13:42:32 -0700 | [diff] [blame] | 41 | const char* testCryptDir = "/tmp"; |
| John Wedig | 6c0d8ce | 2022-04-22 14:00:43 -0700 | [diff] [blame] | 42 | const std::string testConfigPath = |
| 43 | "/xyz/openbmc_project/inventory/system/board/test_board/test_emmc"; |
| John Wedig | 67a4744 | 2022-04-05 17:21:29 -0700 | [diff] [blame] | 44 | const uint64_t testSize = 24; |
| John Edward Broadbent | 5d799bb | 2022-03-22 16:14:24 -0700 | [diff] [blame] | 45 | const uint8_t testLifeTime = 25; |
| John Wedig | b483830 | 2022-07-22 13:51:16 -0700 | [diff] [blame] | 46 | const std::string testPartNumber = "12345678"; |
| 47 | const std::string testSerialNumber = "ABCDEF1234"; |
| Rahul Kapoor | 1982505 | 2023-05-27 01:52:23 +0000 | [diff] [blame] | 48 | const std::string testLocationCode = "U102020"; |
| John Wedig | d7be42b | 2024-01-19 16:07:19 -0800 | [diff] [blame] | 49 | const std::string testDriveType = "SSD"; |
| John Wedig | c0d66eb | 2024-02-26 15:54:47 -0800 | [diff] [blame] | 50 | const std::string testDriveProtocol = "eMMC"; |
| John Wedig | b810c92 | 2021-11-17 16:38:03 -0800 | [diff] [blame] | 51 | std::ofstream testFile; |
| John Wedig | b810c92 | 2021-11-17 16:38:03 -0800 | [diff] [blame] | 52 | std::string passwordString; |
| 53 | std::vector<uint8_t> password; |
| Ed Tanous | 82897c3 | 2022-02-21 14:11:59 -0800 | [diff] [blame] | 54 | MockCryptsetupInterface* mockCryptIface{}; |
| 55 | MockFilesystemInterface* mockFsIface{}; |
| John Wedig | 67a4744 | 2022-04-05 17:21:29 -0700 | [diff] [blame] | 56 | 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 Wedig | b810c92 | 2021-11-17 16:38:03 -0800 | [diff] [blame] | 60 | |
| Ed Tanous | 82897c3 | 2022-02-21 14:11:59 -0800 | [diff] [blame] | 61 | EStoragedTest() : |
| John Wedig | 67a4744 | 2022-04-05 17:21:29 -0700 | [diff] [blame] | 62 | passwordString("password"), |
| John Wedig | b810c92 | 2021-11-17 16:38:03 -0800 | [diff] [blame] | 63 | 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 Wedig | b810c92 | 2021-11-17 16:38:03 -0800 | [diff] [blame] | 77 | 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 Wedig | 2443a02 | 2023-03-17 13:42:32 -0700 | [diff] [blame] | 84 | /* Set up location of dummy mapped crypt file. */ |
| 85 | EXPECT_CALL(*cryptIface, cryptGetDir).WillOnce(Return(testCryptDir)); |
| 86 | |
| John Wedig | 67a4744 | 2022-04-05 17:21:29 -0700 | [diff] [blame] | 87 | 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 Tanous | 82897c3 | 2022-02-21 14:11:59 -0800 | [diff] [blame] | 92 | esObject = std::make_unique<estoraged::EStoraged>( |
| John Wedig | 6c0d8ce | 2022-04-22 14:00:43 -0700 | [diff] [blame] | 93 | *objectServer, testConfigPath, testFileName, testLuksDevName, |
| John Wedig | b483830 | 2022-07-22 13:51:16 -0700 | [diff] [blame] | 94 | testSize, testLifeTime, testPartNumber, testSerialNumber, |
| Tom Tung | 043af59 | 2023-11-24 13:37:05 +0800 | [diff] [blame] | 95 | testLocationCode, ERASE_MAX_GEOMETRY, ERASE_MIN_GEOMETRY, |
| John Wedig | c0d66eb | 2024-02-26 15:54:47 -0800 | [diff] [blame] | 96 | testDriveType, testDriveProtocol, std::move(cryptIface), |
| 97 | std::move(fsIface)); |
| John Wedig | b810c92 | 2021-11-17 16:38:03 -0800 | [diff] [blame] | 98 | } |
| 99 | |
| 100 | void TearDown() override |
| 101 | { |
| 102 | EXPECT_EQ(0, unlink(testFileName)); |
| 103 | } |
| 104 | }; |
| 105 | |
| John Wedig | 2443a02 | 2023-03-17 13:42:32 -0700 | [diff] [blame] | 106 | const char* mappedDevicePath = "/tmp/testfile_luksDev"; |
| 107 | std::ofstream mappedDevice; |
| 108 | |
| 109 | int 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 | |
| 122 | int removeMappedDev() |
| 123 | { |
| 124 | EXPECT_EQ(0, unlink(mappedDevicePath)); |
| 125 | |
| 126 | return 0; |
| 127 | } |
| 128 | |
| John Wedig | b810c92 | 2021-11-17 16:38:03 -0800 | [diff] [blame] | 129 | /* Test case to format and then lock the LUKS device. */ |
| Ed Tanous | 82897c3 | 2022-02-21 14:11:59 -0800 | [diff] [blame] | 130 | TEST_F(EStoragedTest, FormatPass) |
| John Wedig | b810c92 | 2021-11-17 16:38:03 -0800 | [diff] [blame] | 131 | { |
| John Wedig | b810c92 | 2021-11-17 16:38:03 -0800 | [diff] [blame] | 132 | 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 Wedig | 2443a02 | 2023-03-17 13:42:32 -0700 | [diff] [blame] | 140 | .WillOnce(&createMappedDev); |
| John Wedig | b810c92 | 2021-11-17 16:38:03 -0800 | [diff] [blame] | 141 | |
| John Wedig | 2443a02 | 2023-03-17 13:42:32 -0700 | [diff] [blame] | 142 | EXPECT_CALL(*mockFsIface, runMkfs(StrEq(esObject->getCryptDevicePath()))) |
| 143 | .WillOnce(Return(0)); |
| John Wedig | b810c92 | 2021-11-17 16:38:03 -0800 | [diff] [blame] | 144 | |
| John Wedig | 1d6665f | 2025-12-04 19:08:06 +0000 | [diff] [blame^] | 145 | EXPECT_CALL(*mockFsIface, runFsck(StrEq(esObject->getCryptDevicePath()), |
| 146 | StrEq("-t ext4 -p"))) |
| 147 | .WillOnce(Return(0)); |
| 148 | |
| John Wedig | b17f825 | 2022-01-12 14:24:26 -0800 | [diff] [blame] | 149 | EXPECT_CALL(*mockFsIface, directoryExists(path(esObject->getMountPoint()))) |
| 150 | .WillOnce(Return(false)); |
| 151 | |
| John Wedig | b810c92 | 2021-11-17 16:38:03 -0800 | [diff] [blame] | 152 | EXPECT_CALL(*mockFsIface, createDirectory(path(esObject->getMountPoint()))) |
| 153 | .WillOnce(Return(true)); |
| 154 | |
| 155 | EXPECT_CALL(*mockFsIface, |
| John Wedig | 2443a02 | 2023-03-17 13:42:32 -0700 | [diff] [blame] | 156 | doMount(StrEq(esObject->getCryptDevicePath()), |
| John Wedig | b810c92 | 2021-11-17 16:38:03 -0800 | [diff] [blame] | 157 | StrEq(esObject->getMountPoint()), _, _, _)) |
| 158 | .WillOnce(Return(0)); |
| 159 | |
| 160 | EXPECT_CALL(*mockFsIface, doUnmount(StrEq(esObject->getMountPoint()))) |
| 161 | .WillOnce(Return(0)); |
| 162 | |
| 163 | EXPECT_CALL(*mockFsIface, removeDirectory(path(esObject->getMountPoint()))) |
| 164 | .WillOnce(Return(true)); |
| 165 | |
| John Wedig | 2443a02 | 2023-03-17 13:42:32 -0700 | [diff] [blame] | 166 | EXPECT_CALL(*mockCryptIface, cryptDeactivate(_, _)) |
| 167 | .WillOnce(&removeMappedDev); |
| John Wedig | b810c92 | 2021-11-17 16:38:03 -0800 | [diff] [blame] | 168 | |
| 169 | /* Format the encrypted device. */ |
| John Wedig | 972c3fa | 2021-12-29 17:30:41 -0800 | [diff] [blame] | 170 | esObject->formatLuks(password, Volume::FilesystemType::ext4); |
| John Wedig | b810c92 | 2021-11-17 16:38:03 -0800 | [diff] [blame] | 171 | EXPECT_FALSE(esObject->isLocked()); |
| 172 | |
| John Wedig | 972c3fa | 2021-12-29 17:30:41 -0800 | [diff] [blame] | 173 | esObject->lock(); |
| John Wedig | b810c92 | 2021-11-17 16:38:03 -0800 | [diff] [blame] | 174 | EXPECT_TRUE(esObject->isLocked()); |
| 175 | } |
| 176 | |
| John Wedig | b17f825 | 2022-01-12 14:24:26 -0800 | [diff] [blame] | 177 | /* |
| 178 | * Test case where the mount point directory already exists, so it shouldn't |
| 179 | * try to create it. |
| 180 | */ |
| Ed Tanous | 82897c3 | 2022-02-21 14:11:59 -0800 | [diff] [blame] | 181 | TEST_F(EStoragedTest, MountPointExistsPass) |
| John Wedig | b17f825 | 2022-01-12 14:24:26 -0800 | [diff] [blame] | 182 | { |
| John Wedig | b17f825 | 2022-01-12 14:24:26 -0800 | [diff] [blame] | 183 | EXPECT_CALL(*mockCryptIface, cryptFormat(_, _, _, _, _, _, _, _)).Times(1); |
| 184 | |
| 185 | EXPECT_CALL(*mockCryptIface, cryptKeyslotAddByVolumeKey(_, _, _, _, _, _)) |
| 186 | .Times(1); |
| 187 | |
| 188 | EXPECT_CALL(*mockCryptIface, cryptLoad(_, _, _)).Times(1); |
| 189 | |
| 190 | EXPECT_CALL(*mockCryptIface, cryptActivateByPassphrase(_, _, _, _, _, _)) |
| John Wedig | 2443a02 | 2023-03-17 13:42:32 -0700 | [diff] [blame] | 191 | .WillOnce(&createMappedDev); |
| John Wedig | b17f825 | 2022-01-12 14:24:26 -0800 | [diff] [blame] | 192 | |
| John Wedig | 2443a02 | 2023-03-17 13:42:32 -0700 | [diff] [blame] | 193 | EXPECT_CALL(*mockFsIface, runMkfs(StrEq(esObject->getCryptDevicePath()))) |
| 194 | .WillOnce(Return(0)); |
| John Wedig | b17f825 | 2022-01-12 14:24:26 -0800 | [diff] [blame] | 195 | |
| John Wedig | 1d6665f | 2025-12-04 19:08:06 +0000 | [diff] [blame^] | 196 | EXPECT_CALL(*mockFsIface, runFsck(StrEq(esObject->getCryptDevicePath()), |
| 197 | StrEq("-t ext4 -p"))) |
| 198 | .WillOnce(Return(0)); |
| 199 | |
| John Wedig | b17f825 | 2022-01-12 14:24:26 -0800 | [diff] [blame] | 200 | EXPECT_CALL(*mockFsIface, directoryExists(path(esObject->getMountPoint()))) |
| 201 | .WillOnce(Return(true)); |
| 202 | |
| 203 | EXPECT_CALL(*mockFsIface, createDirectory(path(esObject->getMountPoint()))) |
| 204 | .Times(0); |
| 205 | |
| 206 | EXPECT_CALL(*mockFsIface, |
| John Wedig | 2443a02 | 2023-03-17 13:42:32 -0700 | [diff] [blame] | 207 | doMount(StrEq(esObject->getCryptDevicePath()), |
| John Wedig | b17f825 | 2022-01-12 14:24:26 -0800 | [diff] [blame] | 208 | StrEq(esObject->getMountPoint()), _, _, _)) |
| 209 | .WillOnce(Return(0)); |
| 210 | |
| 211 | EXPECT_CALL(*mockFsIface, doUnmount(StrEq(esObject->getMountPoint()))) |
| 212 | .WillOnce(Return(0)); |
| 213 | |
| 214 | EXPECT_CALL(*mockFsIface, removeDirectory(path(esObject->getMountPoint()))) |
| 215 | .WillOnce(Return(true)); |
| 216 | |
| John Wedig | 2443a02 | 2023-03-17 13:42:32 -0700 | [diff] [blame] | 217 | EXPECT_CALL(*mockCryptIface, cryptDeactivate(_, _)) |
| 218 | .WillOnce(&removeMappedDev); |
| John Wedig | b17f825 | 2022-01-12 14:24:26 -0800 | [diff] [blame] | 219 | |
| 220 | /* Format the encrypted device. */ |
| 221 | esObject->formatLuks(password, Volume::FilesystemType::ext4); |
| 222 | EXPECT_FALSE(esObject->isLocked()); |
| 223 | |
| 224 | esObject->lock(); |
| 225 | EXPECT_TRUE(esObject->isLocked()); |
| 226 | } |
| 227 | |
| John Wedig | b810c92 | 2021-11-17 16:38:03 -0800 | [diff] [blame] | 228 | /* Test case where the device/file doesn't exist. */ |
| Ed Tanous | 82897c3 | 2022-02-21 14:11:59 -0800 | [diff] [blame] | 229 | TEST_F(EStoragedTest, FormatNoDeviceFail) |
| John Wedig | b810c92 | 2021-11-17 16:38:03 -0800 | [diff] [blame] | 230 | { |
| 231 | /* Delete the test file. */ |
| 232 | EXPECT_EQ(0, unlink(testFileName)); |
| 233 | |
| John Wedig | 972c3fa | 2021-12-29 17:30:41 -0800 | [diff] [blame] | 234 | EXPECT_THROW(esObject->formatLuks(password, Volume::FilesystemType::ext4), |
| 235 | ResourceNotFound); |
| John Wedig | 2443a02 | 2023-03-17 13:42:32 -0700 | [diff] [blame] | 236 | EXPECT_TRUE(esObject->isLocked()); |
| John Wedig | b810c92 | 2021-11-17 16:38:03 -0800 | [diff] [blame] | 237 | |
| 238 | /* Create the test file again, so that the TearDown function works. */ |
| 239 | testFile.open(testFileName, |
| 240 | std::ios::out | std::ios::binary | std::ios::trunc); |
| 241 | testFile.close(); |
| 242 | } |
| 243 | |
| 244 | /* Test case where we fail to format the LUKS device. */ |
| Ed Tanous | 82897c3 | 2022-02-21 14:11:59 -0800 | [diff] [blame] | 245 | TEST_F(EStoragedTest, FormatFail) |
| John Wedig | b810c92 | 2021-11-17 16:38:03 -0800 | [diff] [blame] | 246 | { |
| 247 | EXPECT_CALL(*mockCryptIface, cryptFormat(_, _, _, _, _, _, _, _)) |
| 248 | .WillOnce(Return(-1)); |
| 249 | |
| John Wedig | 972c3fa | 2021-12-29 17:30:41 -0800 | [diff] [blame] | 250 | EXPECT_THROW(esObject->formatLuks(password, Volume::FilesystemType::ext4), |
| 251 | InternalFailure); |
| John Wedig | 2443a02 | 2023-03-17 13:42:32 -0700 | [diff] [blame] | 252 | EXPECT_TRUE(esObject->isLocked()); |
| John Wedig | b810c92 | 2021-11-17 16:38:03 -0800 | [diff] [blame] | 253 | } |
| 254 | |
| 255 | /* Test case where we fail to set the password for the LUKS device. */ |
| Ed Tanous | 82897c3 | 2022-02-21 14:11:59 -0800 | [diff] [blame] | 256 | TEST_F(EStoragedTest, AddKeyslotFail) |
| John Wedig | b810c92 | 2021-11-17 16:38:03 -0800 | [diff] [blame] | 257 | { |
| John Wedig | b810c92 | 2021-11-17 16:38:03 -0800 | [diff] [blame] | 258 | EXPECT_CALL(*mockCryptIface, cryptFormat(_, _, _, _, _, _, _, _)).Times(1); |
| 259 | |
| 260 | EXPECT_CALL(*mockCryptIface, cryptKeyslotAddByVolumeKey(_, _, _, _, _, _)) |
| 261 | .WillOnce(Return(-1)); |
| 262 | |
| John Wedig | 972c3fa | 2021-12-29 17:30:41 -0800 | [diff] [blame] | 263 | EXPECT_THROW(esObject->formatLuks(password, Volume::FilesystemType::ext4), |
| 264 | InternalFailure); |
| John Wedig | b810c92 | 2021-11-17 16:38:03 -0800 | [diff] [blame] | 265 | EXPECT_TRUE(esObject->isLocked()); |
| 266 | } |
| 267 | |
| 268 | /* Test case where we fail to load the LUKS header. */ |
| Ed Tanous | 82897c3 | 2022-02-21 14:11:59 -0800 | [diff] [blame] | 269 | TEST_F(EStoragedTest, LoadLuksHeaderFail) |
| John Wedig | b810c92 | 2021-11-17 16:38:03 -0800 | [diff] [blame] | 270 | { |
| John Wedig | b810c92 | 2021-11-17 16:38:03 -0800 | [diff] [blame] | 271 | EXPECT_CALL(*mockCryptIface, cryptFormat(_, _, _, _, _, _, _, _)).Times(1); |
| 272 | |
| 273 | EXPECT_CALL(*mockCryptIface, cryptKeyslotAddByVolumeKey(_, _, _, _, _, _)) |
| 274 | .Times(1); |
| 275 | |
| 276 | EXPECT_CALL(*mockCryptIface, cryptLoad(_, _, _)).WillOnce(Return(-1)); |
| 277 | |
| John Wedig | 972c3fa | 2021-12-29 17:30:41 -0800 | [diff] [blame] | 278 | EXPECT_THROW(esObject->formatLuks(password, Volume::FilesystemType::ext4), |
| 279 | InternalFailure); |
| John Wedig | b810c92 | 2021-11-17 16:38:03 -0800 | [diff] [blame] | 280 | EXPECT_TRUE(esObject->isLocked()); |
| 281 | } |
| 282 | |
| 283 | /* Test case where we fail to activate the LUKS device. */ |
| Ed Tanous | 82897c3 | 2022-02-21 14:11:59 -0800 | [diff] [blame] | 284 | TEST_F(EStoragedTest, ActivateFail) |
| John Wedig | b810c92 | 2021-11-17 16:38:03 -0800 | [diff] [blame] | 285 | { |
| John Wedig | b810c92 | 2021-11-17 16:38:03 -0800 | [diff] [blame] | 286 | EXPECT_CALL(*mockCryptIface, cryptFormat(_, _, _, _, _, _, _, _)).Times(1); |
| 287 | |
| 288 | EXPECT_CALL(*mockCryptIface, cryptKeyslotAddByVolumeKey(_, _, _, _, _, _)) |
| 289 | .Times(1); |
| 290 | |
| 291 | EXPECT_CALL(*mockCryptIface, cryptLoad(_, _, _)).Times(1); |
| 292 | |
| 293 | EXPECT_CALL(*mockCryptIface, cryptActivateByPassphrase(_, _, _, _, _, _)) |
| 294 | .WillOnce(Return(-1)); |
| 295 | |
| John Wedig | 972c3fa | 2021-12-29 17:30:41 -0800 | [diff] [blame] | 296 | EXPECT_THROW(esObject->formatLuks(password, Volume::FilesystemType::ext4), |
| 297 | InternalFailure); |
| John Wedig | b810c92 | 2021-11-17 16:38:03 -0800 | [diff] [blame] | 298 | EXPECT_TRUE(esObject->isLocked()); |
| 299 | } |
| 300 | |
| 301 | /* Test case where we fail to create the filesystem. */ |
| Ed Tanous | 82897c3 | 2022-02-21 14:11:59 -0800 | [diff] [blame] | 302 | TEST_F(EStoragedTest, CreateFilesystemFail) |
| John Wedig | b810c92 | 2021-11-17 16:38:03 -0800 | [diff] [blame] | 303 | { |
| John Wedig | b810c92 | 2021-11-17 16:38:03 -0800 | [diff] [blame] | 304 | EXPECT_CALL(*mockCryptIface, cryptFormat(_, _, _, _, _, _, _, _)).Times(1); |
| 305 | |
| 306 | EXPECT_CALL(*mockCryptIface, cryptKeyslotAddByVolumeKey(_, _, _, _, _, _)) |
| 307 | .Times(1); |
| 308 | |
| 309 | EXPECT_CALL(*mockCryptIface, cryptLoad(_, _, _)).Times(1); |
| 310 | |
| 311 | EXPECT_CALL(*mockCryptIface, cryptActivateByPassphrase(_, _, _, _, _, _)) |
| John Wedig | 2443a02 | 2023-03-17 13:42:32 -0700 | [diff] [blame] | 312 | .WillOnce(&createMappedDev); |
| John Wedig | b810c92 | 2021-11-17 16:38:03 -0800 | [diff] [blame] | 313 | |
| John Wedig | 2443a02 | 2023-03-17 13:42:32 -0700 | [diff] [blame] | 314 | EXPECT_CALL(*mockFsIface, runMkfs(StrEq(esObject->getCryptDevicePath()))) |
| 315 | .WillOnce(Return(-1)); |
| John Wedig | b810c92 | 2021-11-17 16:38:03 -0800 | [diff] [blame] | 316 | |
| John Wedig | 972c3fa | 2021-12-29 17:30:41 -0800 | [diff] [blame] | 317 | EXPECT_THROW(esObject->formatLuks(password, Volume::FilesystemType::ext4), |
| 318 | InternalFailure); |
| John Wedig | b810c92 | 2021-11-17 16:38:03 -0800 | [diff] [blame] | 319 | EXPECT_FALSE(esObject->isLocked()); |
| John Wedig | 2443a02 | 2023-03-17 13:42:32 -0700 | [diff] [blame] | 320 | |
| 321 | EXPECT_EQ(0, removeMappedDev()); |
| John Wedig | b810c92 | 2021-11-17 16:38:03 -0800 | [diff] [blame] | 322 | } |
| 323 | |
| 324 | /* Test case where we fail to create the mount point. */ |
| Ed Tanous | 82897c3 | 2022-02-21 14:11:59 -0800 | [diff] [blame] | 325 | TEST_F(EStoragedTest, CreateMountPointFail) |
| John Wedig | b810c92 | 2021-11-17 16:38:03 -0800 | [diff] [blame] | 326 | { |
| John Wedig | b810c92 | 2021-11-17 16:38:03 -0800 | [diff] [blame] | 327 | EXPECT_CALL(*mockCryptIface, cryptFormat(_, _, _, _, _, _, _, _)).Times(1); |
| 328 | |
| 329 | EXPECT_CALL(*mockCryptIface, cryptKeyslotAddByVolumeKey(_, _, _, _, _, _)) |
| 330 | .Times(1); |
| 331 | |
| 332 | EXPECT_CALL(*mockCryptIface, cryptLoad(_, _, _)).Times(1); |
| 333 | |
| 334 | EXPECT_CALL(*mockCryptIface, cryptActivateByPassphrase(_, _, _, _, _, _)) |
| John Wedig | 2443a02 | 2023-03-17 13:42:32 -0700 | [diff] [blame] | 335 | .WillOnce(&createMappedDev); |
| John Wedig | b810c92 | 2021-11-17 16:38:03 -0800 | [diff] [blame] | 336 | |
| John Wedig | 2443a02 | 2023-03-17 13:42:32 -0700 | [diff] [blame] | 337 | EXPECT_CALL(*mockFsIface, runMkfs(StrEq(esObject->getCryptDevicePath()))) |
| 338 | .WillOnce(Return(0)); |
| John Wedig | b810c92 | 2021-11-17 16:38:03 -0800 | [diff] [blame] | 339 | |
| John Wedig | 1d6665f | 2025-12-04 19:08:06 +0000 | [diff] [blame^] | 340 | EXPECT_CALL(*mockFsIface, runFsck(StrEq(esObject->getCryptDevicePath()), |
| 341 | StrEq("-t ext4 -p"))) |
| 342 | .WillOnce(Return(0)); |
| 343 | |
| John Wedig | b17f825 | 2022-01-12 14:24:26 -0800 | [diff] [blame] | 344 | EXPECT_CALL(*mockFsIface, directoryExists(path(esObject->getMountPoint()))) |
| 345 | .WillOnce(Return(false)); |
| 346 | |
| John Wedig | b810c92 | 2021-11-17 16:38:03 -0800 | [diff] [blame] | 347 | EXPECT_CALL(*mockFsIface, createDirectory(path(esObject->getMountPoint()))) |
| 348 | .WillOnce(Return(false)); |
| 349 | |
| John Wedig | 972c3fa | 2021-12-29 17:30:41 -0800 | [diff] [blame] | 350 | EXPECT_THROW(esObject->formatLuks(password, Volume::FilesystemType::ext4), |
| 351 | InternalFailure); |
| John Wedig | b810c92 | 2021-11-17 16:38:03 -0800 | [diff] [blame] | 352 | EXPECT_FALSE(esObject->isLocked()); |
| John Wedig | 2443a02 | 2023-03-17 13:42:32 -0700 | [diff] [blame] | 353 | |
| 354 | EXPECT_EQ(0, removeMappedDev()); |
| John Wedig | b810c92 | 2021-11-17 16:38:03 -0800 | [diff] [blame] | 355 | } |
| 356 | |
| 357 | /* Test case where we fail to mount the filesystem. */ |
| Ed Tanous | 82897c3 | 2022-02-21 14:11:59 -0800 | [diff] [blame] | 358 | TEST_F(EStoragedTest, MountFail) |
| John Wedig | b810c92 | 2021-11-17 16:38:03 -0800 | [diff] [blame] | 359 | { |
| John Wedig | b810c92 | 2021-11-17 16:38:03 -0800 | [diff] [blame] | 360 | EXPECT_CALL(*mockCryptIface, cryptFormat(_, _, _, _, _, _, _, _)).Times(1); |
| 361 | |
| 362 | EXPECT_CALL(*mockCryptIface, cryptKeyslotAddByVolumeKey(_, _, _, _, _, _)) |
| 363 | .Times(1); |
| 364 | |
| 365 | EXPECT_CALL(*mockCryptIface, cryptLoad(_, _, _)).Times(1); |
| 366 | |
| 367 | EXPECT_CALL(*mockCryptIface, cryptActivateByPassphrase(_, _, _, _, _, _)) |
| John Wedig | 2443a02 | 2023-03-17 13:42:32 -0700 | [diff] [blame] | 368 | .WillOnce(&createMappedDev); |
| John Wedig | b810c92 | 2021-11-17 16:38:03 -0800 | [diff] [blame] | 369 | |
| John Wedig | 2443a02 | 2023-03-17 13:42:32 -0700 | [diff] [blame] | 370 | EXPECT_CALL(*mockFsIface, runMkfs(StrEq(esObject->getCryptDevicePath()))) |
| 371 | .WillOnce(Return(0)); |
| John Wedig | b810c92 | 2021-11-17 16:38:03 -0800 | [diff] [blame] | 372 | |
| John Wedig | 1d6665f | 2025-12-04 19:08:06 +0000 | [diff] [blame^] | 373 | EXPECT_CALL(*mockFsIface, runFsck(StrEq(esObject->getCryptDevicePath()), |
| 374 | StrEq("-t ext4 -p"))) |
| 375 | .WillOnce(Return(0)); |
| 376 | |
| John Wedig | b17f825 | 2022-01-12 14:24:26 -0800 | [diff] [blame] | 377 | EXPECT_CALL(*mockFsIface, directoryExists(path(esObject->getMountPoint()))) |
| 378 | .WillOnce(Return(false)); |
| 379 | |
| John Wedig | b810c92 | 2021-11-17 16:38:03 -0800 | [diff] [blame] | 380 | EXPECT_CALL(*mockFsIface, createDirectory(path(esObject->getMountPoint()))) |
| 381 | .WillOnce(Return(true)); |
| 382 | |
| 383 | EXPECT_CALL(*mockFsIface, |
| John Wedig | 2443a02 | 2023-03-17 13:42:32 -0700 | [diff] [blame] | 384 | doMount(StrEq(esObject->getCryptDevicePath()), |
| John Wedig | b810c92 | 2021-11-17 16:38:03 -0800 | [diff] [blame] | 385 | StrEq(esObject->getMountPoint()), _, _, _)) |
| 386 | .WillOnce(Return(-1)); |
| 387 | |
| 388 | EXPECT_CALL(*mockFsIface, removeDirectory(path(esObject->getMountPoint()))) |
| 389 | .WillOnce(Return(true)); |
| 390 | |
| John Wedig | 972c3fa | 2021-12-29 17:30:41 -0800 | [diff] [blame] | 391 | EXPECT_THROW(esObject->formatLuks(password, Volume::FilesystemType::ext4), |
| 392 | InternalFailure); |
| John Wedig | b810c92 | 2021-11-17 16:38:03 -0800 | [diff] [blame] | 393 | EXPECT_FALSE(esObject->isLocked()); |
| John Wedig | 2443a02 | 2023-03-17 13:42:32 -0700 | [diff] [blame] | 394 | |
| 395 | EXPECT_EQ(0, removeMappedDev()); |
| John Wedig | b810c92 | 2021-11-17 16:38:03 -0800 | [diff] [blame] | 396 | } |
| 397 | |
| 398 | /* Test case where we fail to unmount the filesystem. */ |
| Ed Tanous | 82897c3 | 2022-02-21 14:11:59 -0800 | [diff] [blame] | 399 | TEST_F(EStoragedTest, UnmountFail) |
| John Wedig | b810c92 | 2021-11-17 16:38:03 -0800 | [diff] [blame] | 400 | { |
| John Wedig | b810c92 | 2021-11-17 16:38:03 -0800 | [diff] [blame] | 401 | EXPECT_CALL(*mockCryptIface, cryptFormat(_, _, _, _, _, _, _, _)).Times(1); |
| 402 | |
| 403 | EXPECT_CALL(*mockCryptIface, cryptKeyslotAddByVolumeKey(_, _, _, _, _, _)) |
| 404 | .Times(1); |
| 405 | |
| 406 | EXPECT_CALL(*mockCryptIface, cryptLoad(_, _, _)).Times(1); |
| 407 | |
| 408 | EXPECT_CALL(*mockCryptIface, cryptActivateByPassphrase(_, _, _, _, _, _)) |
| John Wedig | 2443a02 | 2023-03-17 13:42:32 -0700 | [diff] [blame] | 409 | .WillOnce(&createMappedDev); |
| John Wedig | b810c92 | 2021-11-17 16:38:03 -0800 | [diff] [blame] | 410 | |
| John Wedig | 2443a02 | 2023-03-17 13:42:32 -0700 | [diff] [blame] | 411 | EXPECT_CALL(*mockFsIface, runMkfs(StrEq(esObject->getCryptDevicePath()))) |
| 412 | .WillOnce(Return(0)); |
| John Wedig | b810c92 | 2021-11-17 16:38:03 -0800 | [diff] [blame] | 413 | |
| John Wedig | 1d6665f | 2025-12-04 19:08:06 +0000 | [diff] [blame^] | 414 | EXPECT_CALL(*mockFsIface, runFsck(StrEq(esObject->getCryptDevicePath()), |
| 415 | StrEq("-t ext4 -p"))) |
| 416 | .WillOnce(Return(0)); |
| 417 | |
| John Wedig | b17f825 | 2022-01-12 14:24:26 -0800 | [diff] [blame] | 418 | EXPECT_CALL(*mockFsIface, directoryExists(path(esObject->getMountPoint()))) |
| 419 | .WillOnce(Return(false)); |
| 420 | |
| John Wedig | b810c92 | 2021-11-17 16:38:03 -0800 | [diff] [blame] | 421 | EXPECT_CALL(*mockFsIface, createDirectory(path(esObject->getMountPoint()))) |
| 422 | .WillOnce(Return(true)); |
| 423 | |
| 424 | EXPECT_CALL(*mockFsIface, |
| John Wedig | 2443a02 | 2023-03-17 13:42:32 -0700 | [diff] [blame] | 425 | doMount(StrEq(esObject->getCryptDevicePath()), |
| John Wedig | b810c92 | 2021-11-17 16:38:03 -0800 | [diff] [blame] | 426 | StrEq(esObject->getMountPoint()), _, _, _)) |
| 427 | .WillOnce(Return(0)); |
| 428 | |
| 429 | EXPECT_CALL(*mockFsIface, doUnmount(StrEq(esObject->getMountPoint()))) |
| 430 | .WillOnce(Return(-1)); |
| 431 | |
| John Wedig | 972c3fa | 2021-12-29 17:30:41 -0800 | [diff] [blame] | 432 | esObject->formatLuks(password, Volume::FilesystemType::ext4); |
| John Wedig | b810c92 | 2021-11-17 16:38:03 -0800 | [diff] [blame] | 433 | EXPECT_FALSE(esObject->isLocked()); |
| 434 | |
| John Wedig | 972c3fa | 2021-12-29 17:30:41 -0800 | [diff] [blame] | 435 | EXPECT_THROW(esObject->lock(), InternalFailure); |
| John Wedig | b810c92 | 2021-11-17 16:38:03 -0800 | [diff] [blame] | 436 | EXPECT_FALSE(esObject->isLocked()); |
| John Wedig | 2443a02 | 2023-03-17 13:42:32 -0700 | [diff] [blame] | 437 | |
| 438 | EXPECT_EQ(0, removeMappedDev()); |
| John Wedig | b810c92 | 2021-11-17 16:38:03 -0800 | [diff] [blame] | 439 | } |
| 440 | |
| 441 | /* Test case where we fail to remove the mount point. */ |
| Ed Tanous | 82897c3 | 2022-02-21 14:11:59 -0800 | [diff] [blame] | 442 | TEST_F(EStoragedTest, RemoveMountPointFail) |
| John Wedig | b810c92 | 2021-11-17 16:38:03 -0800 | [diff] [blame] | 443 | { |
| John Wedig | b810c92 | 2021-11-17 16:38:03 -0800 | [diff] [blame] | 444 | EXPECT_CALL(*mockCryptIface, cryptFormat(_, _, _, _, _, _, _, _)).Times(1); |
| 445 | |
| 446 | EXPECT_CALL(*mockCryptIface, cryptKeyslotAddByVolumeKey(_, _, _, _, _, _)) |
| 447 | .Times(1); |
| 448 | |
| 449 | EXPECT_CALL(*mockCryptIface, cryptLoad(_, _, _)).Times(1); |
| 450 | |
| 451 | EXPECT_CALL(*mockCryptIface, cryptActivateByPassphrase(_, _, _, _, _, _)) |
| John Wedig | 2443a02 | 2023-03-17 13:42:32 -0700 | [diff] [blame] | 452 | .WillOnce(&createMappedDev); |
| John Wedig | b810c92 | 2021-11-17 16:38:03 -0800 | [diff] [blame] | 453 | |
| John Wedig | 2443a02 | 2023-03-17 13:42:32 -0700 | [diff] [blame] | 454 | EXPECT_CALL(*mockFsIface, runMkfs(StrEq(esObject->getCryptDevicePath()))) |
| 455 | .WillOnce(Return(0)); |
| John Wedig | b810c92 | 2021-11-17 16:38:03 -0800 | [diff] [blame] | 456 | |
| John Wedig | 1d6665f | 2025-12-04 19:08:06 +0000 | [diff] [blame^] | 457 | EXPECT_CALL(*mockFsIface, runFsck(StrEq(esObject->getCryptDevicePath()), |
| 458 | StrEq("-t ext4 -p"))) |
| 459 | .WillOnce(Return(0)); |
| 460 | |
| John Wedig | b17f825 | 2022-01-12 14:24:26 -0800 | [diff] [blame] | 461 | EXPECT_CALL(*mockFsIface, directoryExists(path(esObject->getMountPoint()))) |
| 462 | .WillOnce(Return(false)); |
| 463 | |
| John Wedig | b810c92 | 2021-11-17 16:38:03 -0800 | [diff] [blame] | 464 | EXPECT_CALL(*mockFsIface, createDirectory(path(esObject->getMountPoint()))) |
| 465 | .WillOnce(Return(true)); |
| 466 | |
| 467 | EXPECT_CALL(*mockFsIface, |
| John Wedig | 2443a02 | 2023-03-17 13:42:32 -0700 | [diff] [blame] | 468 | doMount(StrEq(esObject->getCryptDevicePath()), |
| John Wedig | b810c92 | 2021-11-17 16:38:03 -0800 | [diff] [blame] | 469 | StrEq(esObject->getMountPoint()), _, _, _)) |
| 470 | .WillOnce(Return(0)); |
| 471 | |
| 472 | EXPECT_CALL(*mockFsIface, doUnmount(StrEq(esObject->getMountPoint()))) |
| 473 | .WillOnce(Return(0)); |
| 474 | |
| 475 | EXPECT_CALL(*mockFsIface, removeDirectory(path(esObject->getMountPoint()))) |
| 476 | .WillOnce(Return(false)); |
| 477 | |
| John Wedig | 972c3fa | 2021-12-29 17:30:41 -0800 | [diff] [blame] | 478 | esObject->formatLuks(password, Volume::FilesystemType::ext4); |
| John Wedig | b810c92 | 2021-11-17 16:38:03 -0800 | [diff] [blame] | 479 | EXPECT_FALSE(esObject->isLocked()); |
| 480 | |
| 481 | /* This will fail to remove the mount point. */ |
| John Wedig | 972c3fa | 2021-12-29 17:30:41 -0800 | [diff] [blame] | 482 | EXPECT_THROW(esObject->lock(), InternalFailure); |
| John Wedig | b810c92 | 2021-11-17 16:38:03 -0800 | [diff] [blame] | 483 | EXPECT_FALSE(esObject->isLocked()); |
| John Wedig | 2443a02 | 2023-03-17 13:42:32 -0700 | [diff] [blame] | 484 | |
| 485 | EXPECT_EQ(0, removeMappedDev()); |
| John Wedig | b810c92 | 2021-11-17 16:38:03 -0800 | [diff] [blame] | 486 | } |
| 487 | |
| 488 | /* Test case where we fail to deactivate the LUKS device. */ |
| Ed Tanous | 82897c3 | 2022-02-21 14:11:59 -0800 | [diff] [blame] | 489 | TEST_F(EStoragedTest, DeactivateFail) |
| John Wedig | b810c92 | 2021-11-17 16:38:03 -0800 | [diff] [blame] | 490 | { |
| John Wedig | b810c92 | 2021-11-17 16:38:03 -0800 | [diff] [blame] | 491 | EXPECT_CALL(*mockCryptIface, cryptFormat(_, _, _, _, _, _, _, _)).Times(1); |
| 492 | |
| 493 | EXPECT_CALL(*mockCryptIface, cryptKeyslotAddByVolumeKey(_, _, _, _, _, _)) |
| 494 | .Times(1); |
| 495 | |
| 496 | EXPECT_CALL(*mockCryptIface, cryptLoad(_, _, _)).Times(1); |
| 497 | |
| 498 | EXPECT_CALL(*mockCryptIface, cryptActivateByPassphrase(_, _, _, _, _, _)) |
| John Wedig | 2443a02 | 2023-03-17 13:42:32 -0700 | [diff] [blame] | 499 | .WillOnce(&createMappedDev); |
| John Wedig | b810c92 | 2021-11-17 16:38:03 -0800 | [diff] [blame] | 500 | |
| John Wedig | 2443a02 | 2023-03-17 13:42:32 -0700 | [diff] [blame] | 501 | EXPECT_CALL(*mockFsIface, runMkfs(StrEq(esObject->getCryptDevicePath()))) |
| 502 | .WillOnce(Return(0)); |
| John Wedig | b810c92 | 2021-11-17 16:38:03 -0800 | [diff] [blame] | 503 | |
| John Wedig | 1d6665f | 2025-12-04 19:08:06 +0000 | [diff] [blame^] | 504 | EXPECT_CALL(*mockFsIface, runFsck(StrEq(esObject->getCryptDevicePath()), |
| 505 | StrEq("-t ext4 -p"))) |
| 506 | .WillOnce(Return(0)); |
| 507 | |
| John Wedig | b17f825 | 2022-01-12 14:24:26 -0800 | [diff] [blame] | 508 | EXPECT_CALL(*mockFsIface, directoryExists(path(esObject->getMountPoint()))) |
| 509 | .WillOnce(Return(false)); |
| 510 | |
| John Wedig | b810c92 | 2021-11-17 16:38:03 -0800 | [diff] [blame] | 511 | EXPECT_CALL(*mockFsIface, createDirectory(path(esObject->getMountPoint()))) |
| 512 | .WillOnce(Return(true)); |
| 513 | |
| 514 | EXPECT_CALL(*mockFsIface, |
| John Wedig | 2443a02 | 2023-03-17 13:42:32 -0700 | [diff] [blame] | 515 | doMount(StrEq(esObject->getCryptDevicePath()), |
| John Wedig | b810c92 | 2021-11-17 16:38:03 -0800 | [diff] [blame] | 516 | StrEq(esObject->getMountPoint()), _, _, _)) |
| 517 | .WillOnce(Return(0)); |
| 518 | |
| 519 | EXPECT_CALL(*mockFsIface, doUnmount(StrEq(esObject->getMountPoint()))) |
| 520 | .WillOnce(Return(0)); |
| 521 | |
| 522 | EXPECT_CALL(*mockFsIface, removeDirectory(path(esObject->getMountPoint()))) |
| 523 | .WillOnce(Return(true)); |
| 524 | |
| 525 | EXPECT_CALL(*mockCryptIface, cryptDeactivate(_, _)).WillOnce(Return(-1)); |
| 526 | |
| 527 | /* Format the encrypted device. */ |
| John Wedig | 972c3fa | 2021-12-29 17:30:41 -0800 | [diff] [blame] | 528 | esObject->formatLuks(password, Volume::FilesystemType::ext4); |
| John Wedig | b810c92 | 2021-11-17 16:38:03 -0800 | [diff] [blame] | 529 | EXPECT_FALSE(esObject->isLocked()); |
| 530 | |
| John Wedig | 972c3fa | 2021-12-29 17:30:41 -0800 | [diff] [blame] | 531 | EXPECT_THROW(esObject->lock(), InternalFailure); |
| John Wedig | b810c92 | 2021-11-17 16:38:03 -0800 | [diff] [blame] | 532 | EXPECT_FALSE(esObject->isLocked()); |
| John Wedig | 2443a02 | 2023-03-17 13:42:32 -0700 | [diff] [blame] | 533 | |
| 534 | EXPECT_EQ(0, removeMappedDev()); |
| John Wedig | b810c92 | 2021-11-17 16:38:03 -0800 | [diff] [blame] | 535 | } |
| 536 | |
| John Wedig | 8d5a3a0 | 2022-09-29 15:25:58 -0700 | [diff] [blame] | 537 | /* Test case where we successfully change the password. */ |
| 538 | TEST_F(EStoragedTest, ChangePasswordSuccess) |
| 539 | { |
| 540 | std::string newPasswordString("newPassword"); |
| 541 | std::vector<uint8_t> newPassword(newPasswordString.begin(), |
| 542 | newPasswordString.end()); |
| 543 | |
| 544 | EXPECT_CALL(*mockCryptIface, cryptLoad(_, _, _)).Times(1); |
| 545 | |
| 546 | EXPECT_CALL(*mockCryptIface, |
| 547 | cryptKeyslotChangeByPassphrase( |
| 548 | _, _, _, reinterpret_cast<const char*>(password.data()), |
| 549 | password.size(), |
| 550 | reinterpret_cast<const char*>(newPassword.data()), |
| 551 | newPassword.size())) |
| 552 | .WillOnce(Return(0)); |
| 553 | |
| 554 | /* Change the password for the LUKS-encrypted device. */ |
| 555 | esObject->changePassword(password, newPassword); |
| 556 | } |
| 557 | |
| 558 | /* Test case where we fail to change the password. */ |
| 559 | TEST_F(EStoragedTest, ChangePasswordFail) |
| 560 | { |
| 561 | std::string newPasswordString("newPassword"); |
| 562 | std::vector<uint8_t> newPassword(newPasswordString.begin(), |
| 563 | newPasswordString.end()); |
| 564 | |
| 565 | EXPECT_CALL(*mockCryptIface, cryptLoad(_, _, _)).Times(1); |
| 566 | |
| 567 | EXPECT_CALL(*mockCryptIface, |
| 568 | cryptKeyslotChangeByPassphrase( |
| 569 | _, _, _, reinterpret_cast<const char*>(password.data()), |
| 570 | password.size(), |
| 571 | reinterpret_cast<const char*>(newPassword.data()), |
| 572 | newPassword.size())) |
| 573 | .WillOnce(Return(-1)); |
| 574 | |
| 575 | EXPECT_THROW(esObject->changePassword(password, newPassword), |
| 576 | InternalFailure); |
| 577 | } |
| 578 | |
| John Wedig | b810c92 | 2021-11-17 16:38:03 -0800 | [diff] [blame] | 579 | } // namespace estoraged_test |