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" |
John Wedig | b810c92 | 2021-11-17 16:38:03 -0800 | [diff] [blame] | 4 | |
| 5 | #include <unistd.h> |
| 6 | |
John Wedig | 67a4744 | 2022-04-05 17:21:29 -0700 | [diff] [blame] | 7 | #include <boost/asio/io_context.hpp> |
| 8 | #include <sdbusplus/asio/connection.hpp> |
| 9 | #include <sdbusplus/asio/object_server.hpp> |
John Wedig | 972c3fa | 2021-12-29 17:30:41 -0800 | [diff] [blame] | 10 | #include <xyz/openbmc_project/Common/error.hpp> |
John Edward Broadbent | 59dffa6 | 2022-01-13 17:41:32 -0800 | [diff] [blame] | 11 | #include <xyz/openbmc_project/Inventory/Item/Volume/client.hpp> |
John Wedig | b810c92 | 2021-11-17 16:38:03 -0800 | [diff] [blame] | 12 | |
| 13 | #include <exception> |
| 14 | #include <filesystem> |
| 15 | #include <fstream> |
| 16 | #include <iterator> |
John Wedig | 67a4744 | 2022-04-05 17:21:29 -0700 | [diff] [blame] | 17 | #include <memory> |
John Wedig | b810c92 | 2021-11-17 16:38:03 -0800 | [diff] [blame] | 18 | #include <string> |
| 19 | #include <vector> |
| 20 | |
| 21 | #include <gmock/gmock.h> |
| 22 | #include <gtest/gtest.h> |
| 23 | |
| 24 | namespace estoraged_test |
| 25 | { |
| 26 | |
John Wedig | 972c3fa | 2021-12-29 17:30:41 -0800 | [diff] [blame] | 27 | using sdbusplus::xyz::openbmc_project::Common::Error::InternalFailure; |
| 28 | using sdbusplus::xyz::openbmc_project::Common::Error::ResourceNotFound; |
| 29 | using sdbusplus::xyz::openbmc_project::Inventory::Item::server::Volume; |
John Wedig | b810c92 | 2021-11-17 16:38:03 -0800 | [diff] [blame] | 30 | using std::filesystem::path; |
| 31 | using ::testing::_; |
John Wedig | b810c92 | 2021-11-17 16:38:03 -0800 | [diff] [blame] | 32 | using ::testing::Return; |
| 33 | using ::testing::StrEq; |
| 34 | |
Ed Tanous | 82897c3 | 2022-02-21 14:11:59 -0800 | [diff] [blame] | 35 | class EStoragedTest : public testing::Test |
John Wedig | b810c92 | 2021-11-17 16:38:03 -0800 | [diff] [blame] | 36 | { |
| 37 | public: |
Ed Tanous | 82897c3 | 2022-02-21 14:11:59 -0800 | [diff] [blame] | 38 | const char* testFileName = "testfile"; |
| 39 | const char* testLuksDevName = "testfile_luksDev"; |
John Wedig | 2443a02 | 2023-03-17 13:42:32 -0700 | [diff] [blame] | 40 | const char* testCryptDir = "/tmp"; |
John Wedig | 6c0d8ce | 2022-04-22 14:00:43 -0700 | [diff] [blame] | 41 | const std::string testConfigPath = |
| 42 | "/xyz/openbmc_project/inventory/system/board/test_board/test_emmc"; |
John Wedig | 67a4744 | 2022-04-05 17:21:29 -0700 | [diff] [blame] | 43 | const uint64_t testSize = 24; |
John Edward Broadbent | 5d799bb | 2022-03-22 16:14:24 -0700 | [diff] [blame] | 44 | const uint8_t testLifeTime = 25; |
John Wedig | b483830 | 2022-07-22 13:51:16 -0700 | [diff] [blame] | 45 | const std::string testPartNumber = "12345678"; |
| 46 | const std::string testSerialNumber = "ABCDEF1234"; |
John Wedig | b810c92 | 2021-11-17 16:38:03 -0800 | [diff] [blame] | 47 | std::ofstream testFile; |
John Wedig | b810c92 | 2021-11-17 16:38:03 -0800 | [diff] [blame] | 48 | std::string passwordString; |
| 49 | std::vector<uint8_t> password; |
Ed Tanous | 82897c3 | 2022-02-21 14:11:59 -0800 | [diff] [blame] | 50 | MockCryptsetupInterface* mockCryptIface{}; |
| 51 | MockFilesystemInterface* mockFsIface{}; |
John Wedig | 67a4744 | 2022-04-05 17:21:29 -0700 | [diff] [blame] | 52 | 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 Wedig | b810c92 | 2021-11-17 16:38:03 -0800 | [diff] [blame] | 56 | |
Ed Tanous | 82897c3 | 2022-02-21 14:11:59 -0800 | [diff] [blame] | 57 | EStoragedTest() : |
John Wedig | 67a4744 | 2022-04-05 17:21:29 -0700 | [diff] [blame] | 58 | passwordString("password"), |
John Wedig | b810c92 | 2021-11-17 16:38:03 -0800 | [diff] [blame] | 59 | 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 Wedig | b810c92 | 2021-11-17 16:38:03 -0800 | [diff] [blame] | 73 | 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 Wedig | 2443a02 | 2023-03-17 13:42:32 -0700 | [diff] [blame] | 80 | /* Set up location of dummy mapped crypt file. */ |
| 81 | EXPECT_CALL(*cryptIface, cryptGetDir).WillOnce(Return(testCryptDir)); |
| 82 | |
John Wedig | 67a4744 | 2022-04-05 17:21:29 -0700 | [diff] [blame] | 83 | 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 Tanous | 82897c3 | 2022-02-21 14:11:59 -0800 | [diff] [blame] | 88 | esObject = std::make_unique<estoraged::EStoraged>( |
John Wedig | 6c0d8ce | 2022-04-22 14:00:43 -0700 | [diff] [blame] | 89 | *objectServer, testConfigPath, testFileName, testLuksDevName, |
John Wedig | b483830 | 2022-07-22 13:51:16 -0700 | [diff] [blame] | 90 | testSize, testLifeTime, testPartNumber, testSerialNumber, |
| 91 | std::move(cryptIface), std::move(fsIface)); |
John Wedig | b810c92 | 2021-11-17 16:38:03 -0800 | [diff] [blame] | 92 | } |
| 93 | |
| 94 | void TearDown() override |
| 95 | { |
| 96 | EXPECT_EQ(0, unlink(testFileName)); |
| 97 | } |
| 98 | }; |
| 99 | |
John Wedig | 2443a02 | 2023-03-17 13:42:32 -0700 | [diff] [blame] | 100 | const char* mappedDevicePath = "/tmp/testfile_luksDev"; |
| 101 | std::ofstream mappedDevice; |
| 102 | |
| 103 | int 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 | |
| 116 | int removeMappedDev() |
| 117 | { |
| 118 | EXPECT_EQ(0, unlink(mappedDevicePath)); |
| 119 | |
| 120 | return 0; |
| 121 | } |
| 122 | |
John Wedig | b810c92 | 2021-11-17 16:38:03 -0800 | [diff] [blame] | 123 | /* Test case to format and then lock the LUKS device. */ |
Ed Tanous | 82897c3 | 2022-02-21 14:11:59 -0800 | [diff] [blame] | 124 | TEST_F(EStoragedTest, FormatPass) |
John Wedig | b810c92 | 2021-11-17 16:38:03 -0800 | [diff] [blame] | 125 | { |
John Wedig | b810c92 | 2021-11-17 16:38:03 -0800 | [diff] [blame] | 126 | 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 Wedig | 2443a02 | 2023-03-17 13:42:32 -0700 | [diff] [blame] | 134 | .WillOnce(&createMappedDev); |
John Wedig | b810c92 | 2021-11-17 16:38:03 -0800 | [diff] [blame] | 135 | |
John Wedig | 2443a02 | 2023-03-17 13:42:32 -0700 | [diff] [blame] | 136 | EXPECT_CALL(*mockFsIface, runMkfs(StrEq(esObject->getCryptDevicePath()))) |
| 137 | .WillOnce(Return(0)); |
John Wedig | b810c92 | 2021-11-17 16:38:03 -0800 | [diff] [blame] | 138 | |
John Wedig | b17f825 | 2022-01-12 14:24:26 -0800 | [diff] [blame] | 139 | EXPECT_CALL(*mockFsIface, directoryExists(path(esObject->getMountPoint()))) |
| 140 | .WillOnce(Return(false)); |
| 141 | |
John Wedig | b810c92 | 2021-11-17 16:38:03 -0800 | [diff] [blame] | 142 | EXPECT_CALL(*mockFsIface, createDirectory(path(esObject->getMountPoint()))) |
| 143 | .WillOnce(Return(true)); |
| 144 | |
| 145 | EXPECT_CALL(*mockFsIface, |
John Wedig | 2443a02 | 2023-03-17 13:42:32 -0700 | [diff] [blame] | 146 | doMount(StrEq(esObject->getCryptDevicePath()), |
John Wedig | b810c92 | 2021-11-17 16:38:03 -0800 | [diff] [blame] | 147 | 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 Wedig | 2443a02 | 2023-03-17 13:42:32 -0700 | [diff] [blame] | 156 | EXPECT_CALL(*mockCryptIface, cryptDeactivate(_, _)) |
| 157 | .WillOnce(&removeMappedDev); |
John Wedig | b810c92 | 2021-11-17 16:38:03 -0800 | [diff] [blame] | 158 | |
| 159 | /* Format the encrypted device. */ |
John Wedig | 972c3fa | 2021-12-29 17:30:41 -0800 | [diff] [blame] | 160 | esObject->formatLuks(password, Volume::FilesystemType::ext4); |
John Wedig | b810c92 | 2021-11-17 16:38:03 -0800 | [diff] [blame] | 161 | EXPECT_FALSE(esObject->isLocked()); |
| 162 | |
John Wedig | 972c3fa | 2021-12-29 17:30:41 -0800 | [diff] [blame] | 163 | esObject->lock(); |
John Wedig | b810c92 | 2021-11-17 16:38:03 -0800 | [diff] [blame] | 164 | EXPECT_TRUE(esObject->isLocked()); |
| 165 | } |
| 166 | |
John Wedig | b17f825 | 2022-01-12 14:24:26 -0800 | [diff] [blame] | 167 | /* |
| 168 | * Test case where the mount point directory already exists, so it shouldn't |
| 169 | * try to create it. |
| 170 | */ |
Ed Tanous | 82897c3 | 2022-02-21 14:11:59 -0800 | [diff] [blame] | 171 | TEST_F(EStoragedTest, MountPointExistsPass) |
John Wedig | b17f825 | 2022-01-12 14:24:26 -0800 | [diff] [blame] | 172 | { |
John Wedig | b17f825 | 2022-01-12 14:24:26 -0800 | [diff] [blame] | 173 | 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 Wedig | 2443a02 | 2023-03-17 13:42:32 -0700 | [diff] [blame] | 181 | .WillOnce(&createMappedDev); |
John Wedig | b17f825 | 2022-01-12 14:24:26 -0800 | [diff] [blame] | 182 | |
John Wedig | 2443a02 | 2023-03-17 13:42:32 -0700 | [diff] [blame] | 183 | EXPECT_CALL(*mockFsIface, runMkfs(StrEq(esObject->getCryptDevicePath()))) |
| 184 | .WillOnce(Return(0)); |
John Wedig | b17f825 | 2022-01-12 14:24:26 -0800 | [diff] [blame] | 185 | |
| 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 Wedig | 2443a02 | 2023-03-17 13:42:32 -0700 | [diff] [blame] | 193 | doMount(StrEq(esObject->getCryptDevicePath()), |
John Wedig | b17f825 | 2022-01-12 14:24:26 -0800 | [diff] [blame] | 194 | 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 Wedig | 2443a02 | 2023-03-17 13:42:32 -0700 | [diff] [blame] | 203 | EXPECT_CALL(*mockCryptIface, cryptDeactivate(_, _)) |
| 204 | .WillOnce(&removeMappedDev); |
John Wedig | b17f825 | 2022-01-12 14:24:26 -0800 | [diff] [blame] | 205 | |
| 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 Wedig | b810c92 | 2021-11-17 16:38:03 -0800 | [diff] [blame] | 214 | /* Test case where the device/file doesn't exist. */ |
Ed Tanous | 82897c3 | 2022-02-21 14:11:59 -0800 | [diff] [blame] | 215 | TEST_F(EStoragedTest, FormatNoDeviceFail) |
John Wedig | b810c92 | 2021-11-17 16:38:03 -0800 | [diff] [blame] | 216 | { |
| 217 | /* Delete the test file. */ |
| 218 | EXPECT_EQ(0, unlink(testFileName)); |
| 219 | |
John Wedig | 972c3fa | 2021-12-29 17:30:41 -0800 | [diff] [blame] | 220 | EXPECT_THROW(esObject->formatLuks(password, Volume::FilesystemType::ext4), |
| 221 | ResourceNotFound); |
John Wedig | 2443a02 | 2023-03-17 13:42:32 -0700 | [diff] [blame] | 222 | EXPECT_TRUE(esObject->isLocked()); |
John Wedig | b810c92 | 2021-11-17 16:38:03 -0800 | [diff] [blame] | 223 | |
| 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 Tanous | 82897c3 | 2022-02-21 14:11:59 -0800 | [diff] [blame] | 231 | TEST_F(EStoragedTest, FormatFail) |
John Wedig | b810c92 | 2021-11-17 16:38:03 -0800 | [diff] [blame] | 232 | { |
| 233 | EXPECT_CALL(*mockCryptIface, cryptFormat(_, _, _, _, _, _, _, _)) |
| 234 | .WillOnce(Return(-1)); |
| 235 | |
John Wedig | 972c3fa | 2021-12-29 17:30:41 -0800 | [diff] [blame] | 236 | EXPECT_THROW(esObject->formatLuks(password, Volume::FilesystemType::ext4), |
| 237 | InternalFailure); |
John Wedig | 2443a02 | 2023-03-17 13:42:32 -0700 | [diff] [blame] | 238 | EXPECT_TRUE(esObject->isLocked()); |
John Wedig | b810c92 | 2021-11-17 16:38:03 -0800 | [diff] [blame] | 239 | } |
| 240 | |
| 241 | /* 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] | 242 | TEST_F(EStoragedTest, AddKeyslotFail) |
John Wedig | b810c92 | 2021-11-17 16:38:03 -0800 | [diff] [blame] | 243 | { |
John Wedig | b810c92 | 2021-11-17 16:38:03 -0800 | [diff] [blame] | 244 | EXPECT_CALL(*mockCryptIface, cryptFormat(_, _, _, _, _, _, _, _)).Times(1); |
| 245 | |
| 246 | EXPECT_CALL(*mockCryptIface, cryptKeyslotAddByVolumeKey(_, _, _, _, _, _)) |
| 247 | .WillOnce(Return(-1)); |
| 248 | |
John Wedig | 972c3fa | 2021-12-29 17:30:41 -0800 | [diff] [blame] | 249 | EXPECT_THROW(esObject->formatLuks(password, Volume::FilesystemType::ext4), |
| 250 | InternalFailure); |
John Wedig | b810c92 | 2021-11-17 16:38:03 -0800 | [diff] [blame] | 251 | EXPECT_TRUE(esObject->isLocked()); |
| 252 | } |
| 253 | |
| 254 | /* Test case where we fail to load the LUKS header. */ |
Ed Tanous | 82897c3 | 2022-02-21 14:11:59 -0800 | [diff] [blame] | 255 | TEST_F(EStoragedTest, LoadLuksHeaderFail) |
John Wedig | b810c92 | 2021-11-17 16:38:03 -0800 | [diff] [blame] | 256 | { |
John Wedig | b810c92 | 2021-11-17 16:38:03 -0800 | [diff] [blame] | 257 | 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 Wedig | 972c3fa | 2021-12-29 17:30:41 -0800 | [diff] [blame] | 264 | EXPECT_THROW(esObject->formatLuks(password, Volume::FilesystemType::ext4), |
| 265 | InternalFailure); |
John Wedig | b810c92 | 2021-11-17 16:38:03 -0800 | [diff] [blame] | 266 | EXPECT_TRUE(esObject->isLocked()); |
| 267 | } |
| 268 | |
| 269 | /* Test case where we fail to activate the LUKS device. */ |
Ed Tanous | 82897c3 | 2022-02-21 14:11:59 -0800 | [diff] [blame] | 270 | TEST_F(EStoragedTest, ActivateFail) |
John Wedig | b810c92 | 2021-11-17 16:38:03 -0800 | [diff] [blame] | 271 | { |
John Wedig | b810c92 | 2021-11-17 16:38:03 -0800 | [diff] [blame] | 272 | 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 Wedig | 972c3fa | 2021-12-29 17:30:41 -0800 | [diff] [blame] | 282 | EXPECT_THROW(esObject->formatLuks(password, Volume::FilesystemType::ext4), |
| 283 | InternalFailure); |
John Wedig | b810c92 | 2021-11-17 16:38:03 -0800 | [diff] [blame] | 284 | EXPECT_TRUE(esObject->isLocked()); |
| 285 | } |
| 286 | |
| 287 | /* Test case where we fail to create the filesystem. */ |
Ed Tanous | 82897c3 | 2022-02-21 14:11:59 -0800 | [diff] [blame] | 288 | TEST_F(EStoragedTest, CreateFilesystemFail) |
John Wedig | b810c92 | 2021-11-17 16:38:03 -0800 | [diff] [blame] | 289 | { |
John Wedig | b810c92 | 2021-11-17 16:38:03 -0800 | [diff] [blame] | 290 | 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 Wedig | 2443a02 | 2023-03-17 13:42:32 -0700 | [diff] [blame] | 298 | .WillOnce(&createMappedDev); |
John Wedig | b810c92 | 2021-11-17 16:38:03 -0800 | [diff] [blame] | 299 | |
John Wedig | 2443a02 | 2023-03-17 13:42:32 -0700 | [diff] [blame] | 300 | EXPECT_CALL(*mockFsIface, runMkfs(StrEq(esObject->getCryptDevicePath()))) |
| 301 | .WillOnce(Return(-1)); |
John Wedig | b810c92 | 2021-11-17 16:38:03 -0800 | [diff] [blame] | 302 | |
John Wedig | 972c3fa | 2021-12-29 17:30:41 -0800 | [diff] [blame] | 303 | EXPECT_THROW(esObject->formatLuks(password, Volume::FilesystemType::ext4), |
| 304 | InternalFailure); |
John Wedig | b810c92 | 2021-11-17 16:38:03 -0800 | [diff] [blame] | 305 | EXPECT_FALSE(esObject->isLocked()); |
John Wedig | 2443a02 | 2023-03-17 13:42:32 -0700 | [diff] [blame] | 306 | |
| 307 | EXPECT_EQ(0, removeMappedDev()); |
John Wedig | b810c92 | 2021-11-17 16:38:03 -0800 | [diff] [blame] | 308 | } |
| 309 | |
| 310 | /* Test case where we fail to create the mount point. */ |
Ed Tanous | 82897c3 | 2022-02-21 14:11:59 -0800 | [diff] [blame] | 311 | TEST_F(EStoragedTest, CreateMountPointFail) |
John Wedig | b810c92 | 2021-11-17 16:38:03 -0800 | [diff] [blame] | 312 | { |
John Wedig | b810c92 | 2021-11-17 16:38:03 -0800 | [diff] [blame] | 313 | 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 Wedig | 2443a02 | 2023-03-17 13:42:32 -0700 | [diff] [blame] | 321 | .WillOnce(&createMappedDev); |
John Wedig | b810c92 | 2021-11-17 16:38:03 -0800 | [diff] [blame] | 322 | |
John Wedig | 2443a02 | 2023-03-17 13:42:32 -0700 | [diff] [blame] | 323 | EXPECT_CALL(*mockFsIface, runMkfs(StrEq(esObject->getCryptDevicePath()))) |
| 324 | .WillOnce(Return(0)); |
John Wedig | b810c92 | 2021-11-17 16:38:03 -0800 | [diff] [blame] | 325 | |
John Wedig | b17f825 | 2022-01-12 14:24:26 -0800 | [diff] [blame] | 326 | EXPECT_CALL(*mockFsIface, directoryExists(path(esObject->getMountPoint()))) |
| 327 | .WillOnce(Return(false)); |
| 328 | |
John Wedig | b810c92 | 2021-11-17 16:38:03 -0800 | [diff] [blame] | 329 | EXPECT_CALL(*mockFsIface, createDirectory(path(esObject->getMountPoint()))) |
| 330 | .WillOnce(Return(false)); |
| 331 | |
John Wedig | 972c3fa | 2021-12-29 17:30:41 -0800 | [diff] [blame] | 332 | EXPECT_THROW(esObject->formatLuks(password, Volume::FilesystemType::ext4), |
| 333 | InternalFailure); |
John Wedig | b810c92 | 2021-11-17 16:38:03 -0800 | [diff] [blame] | 334 | EXPECT_FALSE(esObject->isLocked()); |
John Wedig | 2443a02 | 2023-03-17 13:42:32 -0700 | [diff] [blame] | 335 | |
| 336 | EXPECT_EQ(0, removeMappedDev()); |
John Wedig | b810c92 | 2021-11-17 16:38:03 -0800 | [diff] [blame] | 337 | } |
| 338 | |
| 339 | /* Test case where we fail to mount the filesystem. */ |
Ed Tanous | 82897c3 | 2022-02-21 14:11:59 -0800 | [diff] [blame] | 340 | TEST_F(EStoragedTest, MountFail) |
John Wedig | b810c92 | 2021-11-17 16:38:03 -0800 | [diff] [blame] | 341 | { |
John Wedig | b810c92 | 2021-11-17 16:38:03 -0800 | [diff] [blame] | 342 | 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 Wedig | 2443a02 | 2023-03-17 13:42:32 -0700 | [diff] [blame] | 350 | .WillOnce(&createMappedDev); |
John Wedig | b810c92 | 2021-11-17 16:38:03 -0800 | [diff] [blame] | 351 | |
John Wedig | 2443a02 | 2023-03-17 13:42:32 -0700 | [diff] [blame] | 352 | EXPECT_CALL(*mockFsIface, runMkfs(StrEq(esObject->getCryptDevicePath()))) |
| 353 | .WillOnce(Return(0)); |
John Wedig | b810c92 | 2021-11-17 16:38:03 -0800 | [diff] [blame] | 354 | |
John Wedig | b17f825 | 2022-01-12 14:24:26 -0800 | [diff] [blame] | 355 | EXPECT_CALL(*mockFsIface, directoryExists(path(esObject->getMountPoint()))) |
| 356 | .WillOnce(Return(false)); |
| 357 | |
John Wedig | b810c92 | 2021-11-17 16:38:03 -0800 | [diff] [blame] | 358 | EXPECT_CALL(*mockFsIface, createDirectory(path(esObject->getMountPoint()))) |
| 359 | .WillOnce(Return(true)); |
| 360 | |
| 361 | EXPECT_CALL(*mockFsIface, |
John Wedig | 2443a02 | 2023-03-17 13:42:32 -0700 | [diff] [blame] | 362 | doMount(StrEq(esObject->getCryptDevicePath()), |
John Wedig | b810c92 | 2021-11-17 16:38:03 -0800 | [diff] [blame] | 363 | StrEq(esObject->getMountPoint()), _, _, _)) |
| 364 | .WillOnce(Return(-1)); |
| 365 | |
| 366 | EXPECT_CALL(*mockFsIface, removeDirectory(path(esObject->getMountPoint()))) |
| 367 | .WillOnce(Return(true)); |
| 368 | |
John Wedig | 972c3fa | 2021-12-29 17:30:41 -0800 | [diff] [blame] | 369 | EXPECT_THROW(esObject->formatLuks(password, Volume::FilesystemType::ext4), |
| 370 | InternalFailure); |
John Wedig | b810c92 | 2021-11-17 16:38:03 -0800 | [diff] [blame] | 371 | EXPECT_FALSE(esObject->isLocked()); |
John Wedig | 2443a02 | 2023-03-17 13:42:32 -0700 | [diff] [blame] | 372 | |
| 373 | EXPECT_EQ(0, removeMappedDev()); |
John Wedig | b810c92 | 2021-11-17 16:38:03 -0800 | [diff] [blame] | 374 | } |
| 375 | |
| 376 | /* Test case where we fail to unmount the filesystem. */ |
Ed Tanous | 82897c3 | 2022-02-21 14:11:59 -0800 | [diff] [blame] | 377 | TEST_F(EStoragedTest, UnmountFail) |
John Wedig | b810c92 | 2021-11-17 16:38:03 -0800 | [diff] [blame] | 378 | { |
John Wedig | b810c92 | 2021-11-17 16:38:03 -0800 | [diff] [blame] | 379 | 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 Wedig | 2443a02 | 2023-03-17 13:42:32 -0700 | [diff] [blame] | 387 | .WillOnce(&createMappedDev); |
John Wedig | b810c92 | 2021-11-17 16:38:03 -0800 | [diff] [blame] | 388 | |
John Wedig | 2443a02 | 2023-03-17 13:42:32 -0700 | [diff] [blame] | 389 | EXPECT_CALL(*mockFsIface, runMkfs(StrEq(esObject->getCryptDevicePath()))) |
| 390 | .WillOnce(Return(0)); |
John Wedig | b810c92 | 2021-11-17 16:38:03 -0800 | [diff] [blame] | 391 | |
John Wedig | b17f825 | 2022-01-12 14:24:26 -0800 | [diff] [blame] | 392 | EXPECT_CALL(*mockFsIface, directoryExists(path(esObject->getMountPoint()))) |
| 393 | .WillOnce(Return(false)); |
| 394 | |
John Wedig | b810c92 | 2021-11-17 16:38:03 -0800 | [diff] [blame] | 395 | EXPECT_CALL(*mockFsIface, createDirectory(path(esObject->getMountPoint()))) |
| 396 | .WillOnce(Return(true)); |
| 397 | |
| 398 | EXPECT_CALL(*mockFsIface, |
John Wedig | 2443a02 | 2023-03-17 13:42:32 -0700 | [diff] [blame] | 399 | doMount(StrEq(esObject->getCryptDevicePath()), |
John Wedig | b810c92 | 2021-11-17 16:38:03 -0800 | [diff] [blame] | 400 | StrEq(esObject->getMountPoint()), _, _, _)) |
| 401 | .WillOnce(Return(0)); |
| 402 | |
| 403 | EXPECT_CALL(*mockFsIface, doUnmount(StrEq(esObject->getMountPoint()))) |
| 404 | .WillOnce(Return(-1)); |
| 405 | |
John Wedig | 972c3fa | 2021-12-29 17:30:41 -0800 | [diff] [blame] | 406 | esObject->formatLuks(password, Volume::FilesystemType::ext4); |
John Wedig | b810c92 | 2021-11-17 16:38:03 -0800 | [diff] [blame] | 407 | EXPECT_FALSE(esObject->isLocked()); |
| 408 | |
John Wedig | 972c3fa | 2021-12-29 17:30:41 -0800 | [diff] [blame] | 409 | EXPECT_THROW(esObject->lock(), InternalFailure); |
John Wedig | b810c92 | 2021-11-17 16:38:03 -0800 | [diff] [blame] | 410 | EXPECT_FALSE(esObject->isLocked()); |
John Wedig | 2443a02 | 2023-03-17 13:42:32 -0700 | [diff] [blame] | 411 | |
| 412 | EXPECT_EQ(0, removeMappedDev()); |
John Wedig | b810c92 | 2021-11-17 16:38:03 -0800 | [diff] [blame] | 413 | } |
| 414 | |
| 415 | /* Test case where we fail to remove the mount point. */ |
Ed Tanous | 82897c3 | 2022-02-21 14:11:59 -0800 | [diff] [blame] | 416 | TEST_F(EStoragedTest, RemoveMountPointFail) |
John Wedig | b810c92 | 2021-11-17 16:38:03 -0800 | [diff] [blame] | 417 | { |
John Wedig | b810c92 | 2021-11-17 16:38:03 -0800 | [diff] [blame] | 418 | 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 Wedig | 2443a02 | 2023-03-17 13:42:32 -0700 | [diff] [blame] | 426 | .WillOnce(&createMappedDev); |
John Wedig | b810c92 | 2021-11-17 16:38:03 -0800 | [diff] [blame] | 427 | |
John Wedig | 2443a02 | 2023-03-17 13:42:32 -0700 | [diff] [blame] | 428 | EXPECT_CALL(*mockFsIface, runMkfs(StrEq(esObject->getCryptDevicePath()))) |
| 429 | .WillOnce(Return(0)); |
John Wedig | b810c92 | 2021-11-17 16:38:03 -0800 | [diff] [blame] | 430 | |
John Wedig | b17f825 | 2022-01-12 14:24:26 -0800 | [diff] [blame] | 431 | EXPECT_CALL(*mockFsIface, directoryExists(path(esObject->getMountPoint()))) |
| 432 | .WillOnce(Return(false)); |
| 433 | |
John Wedig | b810c92 | 2021-11-17 16:38:03 -0800 | [diff] [blame] | 434 | EXPECT_CALL(*mockFsIface, createDirectory(path(esObject->getMountPoint()))) |
| 435 | .WillOnce(Return(true)); |
| 436 | |
| 437 | EXPECT_CALL(*mockFsIface, |
John Wedig | 2443a02 | 2023-03-17 13:42:32 -0700 | [diff] [blame] | 438 | doMount(StrEq(esObject->getCryptDevicePath()), |
John Wedig | b810c92 | 2021-11-17 16:38:03 -0800 | [diff] [blame] | 439 | 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 Wedig | 972c3fa | 2021-12-29 17:30:41 -0800 | [diff] [blame] | 448 | esObject->formatLuks(password, Volume::FilesystemType::ext4); |
John Wedig | b810c92 | 2021-11-17 16:38:03 -0800 | [diff] [blame] | 449 | EXPECT_FALSE(esObject->isLocked()); |
| 450 | |
| 451 | /* This will fail to remove the mount point. */ |
John Wedig | 972c3fa | 2021-12-29 17:30:41 -0800 | [diff] [blame] | 452 | EXPECT_THROW(esObject->lock(), InternalFailure); |
John Wedig | b810c92 | 2021-11-17 16:38:03 -0800 | [diff] [blame] | 453 | EXPECT_FALSE(esObject->isLocked()); |
John Wedig | 2443a02 | 2023-03-17 13:42:32 -0700 | [diff] [blame] | 454 | |
| 455 | EXPECT_EQ(0, removeMappedDev()); |
John Wedig | b810c92 | 2021-11-17 16:38:03 -0800 | [diff] [blame] | 456 | } |
| 457 | |
| 458 | /* Test case where we fail to deactivate the LUKS device. */ |
Ed Tanous | 82897c3 | 2022-02-21 14:11:59 -0800 | [diff] [blame] | 459 | TEST_F(EStoragedTest, DeactivateFail) |
John Wedig | b810c92 | 2021-11-17 16:38:03 -0800 | [diff] [blame] | 460 | { |
John Wedig | b810c92 | 2021-11-17 16:38:03 -0800 | [diff] [blame] | 461 | 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 Wedig | 2443a02 | 2023-03-17 13:42:32 -0700 | [diff] [blame] | 469 | .WillOnce(&createMappedDev); |
John Wedig | b810c92 | 2021-11-17 16:38:03 -0800 | [diff] [blame] | 470 | |
John Wedig | 2443a02 | 2023-03-17 13:42:32 -0700 | [diff] [blame] | 471 | EXPECT_CALL(*mockFsIface, runMkfs(StrEq(esObject->getCryptDevicePath()))) |
| 472 | .WillOnce(Return(0)); |
John Wedig | b810c92 | 2021-11-17 16:38:03 -0800 | [diff] [blame] | 473 | |
John Wedig | b17f825 | 2022-01-12 14:24:26 -0800 | [diff] [blame] | 474 | EXPECT_CALL(*mockFsIface, directoryExists(path(esObject->getMountPoint()))) |
| 475 | .WillOnce(Return(false)); |
| 476 | |
John Wedig | b810c92 | 2021-11-17 16:38:03 -0800 | [diff] [blame] | 477 | EXPECT_CALL(*mockFsIface, createDirectory(path(esObject->getMountPoint()))) |
| 478 | .WillOnce(Return(true)); |
| 479 | |
| 480 | EXPECT_CALL(*mockFsIface, |
John Wedig | 2443a02 | 2023-03-17 13:42:32 -0700 | [diff] [blame] | 481 | doMount(StrEq(esObject->getCryptDevicePath()), |
John Wedig | b810c92 | 2021-11-17 16:38:03 -0800 | [diff] [blame] | 482 | 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 Wedig | 972c3fa | 2021-12-29 17:30:41 -0800 | [diff] [blame] | 494 | esObject->formatLuks(password, Volume::FilesystemType::ext4); |
John Wedig | b810c92 | 2021-11-17 16:38:03 -0800 | [diff] [blame] | 495 | EXPECT_FALSE(esObject->isLocked()); |
| 496 | |
John Wedig | 972c3fa | 2021-12-29 17:30:41 -0800 | [diff] [blame] | 497 | EXPECT_THROW(esObject->lock(), InternalFailure); |
John Wedig | b810c92 | 2021-11-17 16:38:03 -0800 | [diff] [blame] | 498 | EXPECT_FALSE(esObject->isLocked()); |
John Wedig | 2443a02 | 2023-03-17 13:42:32 -0700 | [diff] [blame] | 499 | |
| 500 | EXPECT_EQ(0, removeMappedDev()); |
John Wedig | b810c92 | 2021-11-17 16:38:03 -0800 | [diff] [blame] | 501 | } |
| 502 | |
John Wedig | 8d5a3a0 | 2022-09-29 15:25:58 -0700 | [diff] [blame] | 503 | /* Test case where we successfully change the password. */ |
| 504 | TEST_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. */ |
| 525 | TEST_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 Wedig | b810c92 | 2021-11-17 16:38:03 -0800 | [diff] [blame] | 545 | } // namespace estoraged_test |