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::_; |
| 32 | using ::testing::ContainsRegex; |
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 | 67a4744 | 2022-04-05 17:21:29 -0700 | [diff] [blame] | 41 | const uint64_t testSize = 24; |
John Edward Broadbent | 5d799bb | 2022-03-22 16:14:24 -0700 | [diff] [blame] | 42 | const uint8_t testLifeTime = 25; |
John Wedig | b810c92 | 2021-11-17 16:38:03 -0800 | [diff] [blame] | 43 | std::ofstream testFile; |
Ed Tanous | 82897c3 | 2022-02-21 14:11:59 -0800 | [diff] [blame] | 44 | const char* testPath = "/test/openbmc_project/storage/test_dev"; |
| 45 | const char* estoragedInterface = |
John Wedig | 972c3fa | 2021-12-29 17:30:41 -0800 | [diff] [blame] | 46 | "xyz.openbmc_project.Inventory.Item.Volume"; |
John Edward Broadbent | 86dfb24 | 2022-03-14 11:04:36 -0700 | [diff] [blame] | 47 | const char* driveInterface = "xyz.openbmc_project.Inventory.Item.Drive"; |
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 | 67a4744 | 2022-04-05 17:21:29 -0700 | [diff] [blame] | 80 | conn = std::make_shared<sdbusplus::asio::connection>(io); |
| 81 | // request D-Bus server name. |
| 82 | conn->request_name("xyz.openbmc_project.eStoraged.test"); |
| 83 | objectServer = std::make_unique<sdbusplus::asio::object_server>(conn); |
| 84 | |
Ed Tanous | 82897c3 | 2022-02-21 14:11:59 -0800 | [diff] [blame] | 85 | esObject = std::make_unique<estoraged::EStoraged>( |
John Wedig | 67a4744 | 2022-04-05 17:21:29 -0700 | [diff] [blame] | 86 | *objectServer, testFileName, testLuksDevName, testSize, |
John Edward Broadbent | 5d799bb | 2022-03-22 16:14:24 -0700 | [diff] [blame] | 87 | testLifeTime, std::move(cryptIface), std::move(fsIface)); |
John Wedig | b810c92 | 2021-11-17 16:38:03 -0800 | [diff] [blame] | 88 | } |
| 89 | |
| 90 | void TearDown() override |
| 91 | { |
| 92 | EXPECT_EQ(0, unlink(testFileName)); |
| 93 | } |
| 94 | }; |
| 95 | |
| 96 | /* Test case to format and then lock the LUKS device. */ |
Ed Tanous | 82897c3 | 2022-02-21 14:11:59 -0800 | [diff] [blame] | 97 | TEST_F(EStoragedTest, FormatPass) |
John Wedig | b810c92 | 2021-11-17 16:38:03 -0800 | [diff] [blame] | 98 | { |
John Wedig | b810c92 | 2021-11-17 16:38:03 -0800 | [diff] [blame] | 99 | EXPECT_CALL(*mockCryptIface, cryptFormat(_, _, _, _, _, _, _, _)).Times(1); |
| 100 | |
| 101 | EXPECT_CALL(*mockCryptIface, cryptKeyslotAddByVolumeKey(_, _, _, _, _, _)) |
| 102 | .Times(1); |
| 103 | |
| 104 | EXPECT_CALL(*mockCryptIface, cryptLoad(_, _, _)).Times(1); |
| 105 | |
| 106 | EXPECT_CALL(*mockCryptIface, cryptActivateByPassphrase(_, _, _, _, _, _)) |
| 107 | .Times(1); |
| 108 | |
| 109 | EXPECT_CALL(*mockFsIface, runMkfs(testLuksDevName)).WillOnce(Return(0)); |
| 110 | |
John Wedig | b17f825 | 2022-01-12 14:24:26 -0800 | [diff] [blame] | 111 | EXPECT_CALL(*mockFsIface, directoryExists(path(esObject->getMountPoint()))) |
| 112 | .WillOnce(Return(false)); |
| 113 | |
John Wedig | b810c92 | 2021-11-17 16:38:03 -0800 | [diff] [blame] | 114 | EXPECT_CALL(*mockFsIface, createDirectory(path(esObject->getMountPoint()))) |
| 115 | .WillOnce(Return(true)); |
| 116 | |
| 117 | EXPECT_CALL(*mockFsIface, |
| 118 | doMount(ContainsRegex("/dev/mapper/"), |
| 119 | StrEq(esObject->getMountPoint()), _, _, _)) |
| 120 | .WillOnce(Return(0)); |
| 121 | |
| 122 | EXPECT_CALL(*mockFsIface, doUnmount(StrEq(esObject->getMountPoint()))) |
| 123 | .WillOnce(Return(0)); |
| 124 | |
| 125 | EXPECT_CALL(*mockFsIface, removeDirectory(path(esObject->getMountPoint()))) |
| 126 | .WillOnce(Return(true)); |
| 127 | |
| 128 | EXPECT_CALL(*mockCryptIface, cryptDeactivate(_, _)).Times(1); |
| 129 | |
| 130 | /* Format the encrypted device. */ |
John Wedig | 972c3fa | 2021-12-29 17:30:41 -0800 | [diff] [blame] | 131 | esObject->formatLuks(password, Volume::FilesystemType::ext4); |
John Wedig | b810c92 | 2021-11-17 16:38:03 -0800 | [diff] [blame] | 132 | EXPECT_FALSE(esObject->isLocked()); |
| 133 | |
John Wedig | 972c3fa | 2021-12-29 17:30:41 -0800 | [diff] [blame] | 134 | esObject->lock(); |
John Wedig | b810c92 | 2021-11-17 16:38:03 -0800 | [diff] [blame] | 135 | EXPECT_TRUE(esObject->isLocked()); |
| 136 | } |
| 137 | |
John Wedig | b17f825 | 2022-01-12 14:24:26 -0800 | [diff] [blame] | 138 | /* |
| 139 | * Test case where the mount point directory already exists, so it shouldn't |
| 140 | * try to create it. |
| 141 | */ |
Ed Tanous | 82897c3 | 2022-02-21 14:11:59 -0800 | [diff] [blame] | 142 | TEST_F(EStoragedTest, MountPointExistsPass) |
John Wedig | b17f825 | 2022-01-12 14:24:26 -0800 | [diff] [blame] | 143 | { |
John Wedig | b17f825 | 2022-01-12 14:24:26 -0800 | [diff] [blame] | 144 | EXPECT_CALL(*mockCryptIface, cryptFormat(_, _, _, _, _, _, _, _)).Times(1); |
| 145 | |
| 146 | EXPECT_CALL(*mockCryptIface, cryptKeyslotAddByVolumeKey(_, _, _, _, _, _)) |
| 147 | .Times(1); |
| 148 | |
| 149 | EXPECT_CALL(*mockCryptIface, cryptLoad(_, _, _)).Times(1); |
| 150 | |
| 151 | EXPECT_CALL(*mockCryptIface, cryptActivateByPassphrase(_, _, _, _, _, _)) |
| 152 | .Times(1); |
| 153 | |
| 154 | EXPECT_CALL(*mockFsIface, runMkfs(testLuksDevName)).WillOnce(Return(0)); |
| 155 | |
| 156 | EXPECT_CALL(*mockFsIface, directoryExists(path(esObject->getMountPoint()))) |
| 157 | .WillOnce(Return(true)); |
| 158 | |
| 159 | EXPECT_CALL(*mockFsIface, createDirectory(path(esObject->getMountPoint()))) |
| 160 | .Times(0); |
| 161 | |
| 162 | EXPECT_CALL(*mockFsIface, |
| 163 | doMount(ContainsRegex("/dev/mapper/"), |
| 164 | StrEq(esObject->getMountPoint()), _, _, _)) |
| 165 | .WillOnce(Return(0)); |
| 166 | |
| 167 | EXPECT_CALL(*mockFsIface, doUnmount(StrEq(esObject->getMountPoint()))) |
| 168 | .WillOnce(Return(0)); |
| 169 | |
| 170 | EXPECT_CALL(*mockFsIface, removeDirectory(path(esObject->getMountPoint()))) |
| 171 | .WillOnce(Return(true)); |
| 172 | |
| 173 | EXPECT_CALL(*mockCryptIface, cryptDeactivate(_, _)).Times(1); |
| 174 | |
| 175 | /* Format the encrypted device. */ |
| 176 | esObject->formatLuks(password, Volume::FilesystemType::ext4); |
| 177 | EXPECT_FALSE(esObject->isLocked()); |
| 178 | |
| 179 | esObject->lock(); |
| 180 | EXPECT_TRUE(esObject->isLocked()); |
| 181 | } |
| 182 | |
John Wedig | b810c92 | 2021-11-17 16:38:03 -0800 | [diff] [blame] | 183 | /* Test case where the device/file doesn't exist. */ |
Ed Tanous | 82897c3 | 2022-02-21 14:11:59 -0800 | [diff] [blame] | 184 | TEST_F(EStoragedTest, FormatNoDeviceFail) |
John Wedig | b810c92 | 2021-11-17 16:38:03 -0800 | [diff] [blame] | 185 | { |
| 186 | /* Delete the test file. */ |
| 187 | EXPECT_EQ(0, unlink(testFileName)); |
| 188 | |
John Wedig | 972c3fa | 2021-12-29 17:30:41 -0800 | [diff] [blame] | 189 | EXPECT_THROW(esObject->formatLuks(password, Volume::FilesystemType::ext4), |
| 190 | ResourceNotFound); |
John Wedig | b810c92 | 2021-11-17 16:38:03 -0800 | [diff] [blame] | 191 | EXPECT_FALSE(esObject->isLocked()); |
| 192 | |
| 193 | /* Create the test file again, so that the TearDown function works. */ |
| 194 | testFile.open(testFileName, |
| 195 | std::ios::out | std::ios::binary | std::ios::trunc); |
| 196 | testFile.close(); |
| 197 | } |
| 198 | |
| 199 | /* Test case where we fail to format the LUKS device. */ |
Ed Tanous | 82897c3 | 2022-02-21 14:11:59 -0800 | [diff] [blame] | 200 | TEST_F(EStoragedTest, FormatFail) |
John Wedig | b810c92 | 2021-11-17 16:38:03 -0800 | [diff] [blame] | 201 | { |
| 202 | EXPECT_CALL(*mockCryptIface, cryptFormat(_, _, _, _, _, _, _, _)) |
| 203 | .WillOnce(Return(-1)); |
| 204 | |
John Wedig | 972c3fa | 2021-12-29 17:30:41 -0800 | [diff] [blame] | 205 | EXPECT_THROW(esObject->formatLuks(password, Volume::FilesystemType::ext4), |
| 206 | InternalFailure); |
John Wedig | b810c92 | 2021-11-17 16:38:03 -0800 | [diff] [blame] | 207 | EXPECT_FALSE(esObject->isLocked()); |
| 208 | } |
| 209 | |
| 210 | /* 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] | 211 | TEST_F(EStoragedTest, AddKeyslotFail) |
John Wedig | b810c92 | 2021-11-17 16:38:03 -0800 | [diff] [blame] | 212 | { |
John Wedig | b810c92 | 2021-11-17 16:38:03 -0800 | [diff] [blame] | 213 | EXPECT_CALL(*mockCryptIface, cryptFormat(_, _, _, _, _, _, _, _)).Times(1); |
| 214 | |
| 215 | EXPECT_CALL(*mockCryptIface, cryptKeyslotAddByVolumeKey(_, _, _, _, _, _)) |
| 216 | .WillOnce(Return(-1)); |
| 217 | |
John Wedig | 972c3fa | 2021-12-29 17:30:41 -0800 | [diff] [blame] | 218 | EXPECT_THROW(esObject->formatLuks(password, Volume::FilesystemType::ext4), |
| 219 | InternalFailure); |
John Wedig | b810c92 | 2021-11-17 16:38:03 -0800 | [diff] [blame] | 220 | EXPECT_TRUE(esObject->isLocked()); |
| 221 | } |
| 222 | |
| 223 | /* Test case where we fail to load the LUKS header. */ |
Ed Tanous | 82897c3 | 2022-02-21 14:11:59 -0800 | [diff] [blame] | 224 | TEST_F(EStoragedTest, LoadLuksHeaderFail) |
John Wedig | b810c92 | 2021-11-17 16:38:03 -0800 | [diff] [blame] | 225 | { |
John Wedig | b810c92 | 2021-11-17 16:38:03 -0800 | [diff] [blame] | 226 | EXPECT_CALL(*mockCryptIface, cryptFormat(_, _, _, _, _, _, _, _)).Times(1); |
| 227 | |
| 228 | EXPECT_CALL(*mockCryptIface, cryptKeyslotAddByVolumeKey(_, _, _, _, _, _)) |
| 229 | .Times(1); |
| 230 | |
| 231 | EXPECT_CALL(*mockCryptIface, cryptLoad(_, _, _)).WillOnce(Return(-1)); |
| 232 | |
John Wedig | 972c3fa | 2021-12-29 17:30:41 -0800 | [diff] [blame] | 233 | EXPECT_THROW(esObject->formatLuks(password, Volume::FilesystemType::ext4), |
| 234 | InternalFailure); |
John Wedig | b810c92 | 2021-11-17 16:38:03 -0800 | [diff] [blame] | 235 | EXPECT_TRUE(esObject->isLocked()); |
| 236 | } |
| 237 | |
| 238 | /* Test case where we fail to activate the LUKS device. */ |
Ed Tanous | 82897c3 | 2022-02-21 14:11:59 -0800 | [diff] [blame] | 239 | TEST_F(EStoragedTest, ActivateFail) |
John Wedig | b810c92 | 2021-11-17 16:38:03 -0800 | [diff] [blame] | 240 | { |
John Wedig | b810c92 | 2021-11-17 16:38:03 -0800 | [diff] [blame] | 241 | EXPECT_CALL(*mockCryptIface, cryptFormat(_, _, _, _, _, _, _, _)).Times(1); |
| 242 | |
| 243 | EXPECT_CALL(*mockCryptIface, cryptKeyslotAddByVolumeKey(_, _, _, _, _, _)) |
| 244 | .Times(1); |
| 245 | |
| 246 | EXPECT_CALL(*mockCryptIface, cryptLoad(_, _, _)).Times(1); |
| 247 | |
| 248 | EXPECT_CALL(*mockCryptIface, cryptActivateByPassphrase(_, _, _, _, _, _)) |
| 249 | .WillOnce(Return(-1)); |
| 250 | |
John Wedig | 972c3fa | 2021-12-29 17:30:41 -0800 | [diff] [blame] | 251 | EXPECT_THROW(esObject->formatLuks(password, Volume::FilesystemType::ext4), |
| 252 | InternalFailure); |
John Wedig | b810c92 | 2021-11-17 16:38:03 -0800 | [diff] [blame] | 253 | EXPECT_TRUE(esObject->isLocked()); |
| 254 | } |
| 255 | |
| 256 | /* Test case where we fail to create the filesystem. */ |
Ed Tanous | 82897c3 | 2022-02-21 14:11:59 -0800 | [diff] [blame] | 257 | TEST_F(EStoragedTest, CreateFilesystemFail) |
John Wedig | b810c92 | 2021-11-17 16:38:03 -0800 | [diff] [blame] | 258 | { |
John Wedig | b810c92 | 2021-11-17 16:38:03 -0800 | [diff] [blame] | 259 | EXPECT_CALL(*mockCryptIface, cryptFormat(_, _, _, _, _, _, _, _)).Times(1); |
| 260 | |
| 261 | EXPECT_CALL(*mockCryptIface, cryptKeyslotAddByVolumeKey(_, _, _, _, _, _)) |
| 262 | .Times(1); |
| 263 | |
| 264 | EXPECT_CALL(*mockCryptIface, cryptLoad(_, _, _)).Times(1); |
| 265 | |
| 266 | EXPECT_CALL(*mockCryptIface, cryptActivateByPassphrase(_, _, _, _, _, _)) |
| 267 | .Times(1); |
| 268 | |
| 269 | EXPECT_CALL(*mockFsIface, runMkfs(testLuksDevName)).WillOnce(Return(-1)); |
| 270 | |
John Wedig | 972c3fa | 2021-12-29 17:30:41 -0800 | [diff] [blame] | 271 | EXPECT_THROW(esObject->formatLuks(password, Volume::FilesystemType::ext4), |
| 272 | InternalFailure); |
John Wedig | b810c92 | 2021-11-17 16:38:03 -0800 | [diff] [blame] | 273 | EXPECT_FALSE(esObject->isLocked()); |
| 274 | } |
| 275 | |
| 276 | /* Test case where we fail to create the mount point. */ |
Ed Tanous | 82897c3 | 2022-02-21 14:11:59 -0800 | [diff] [blame] | 277 | TEST_F(EStoragedTest, CreateMountPointFail) |
John Wedig | b810c92 | 2021-11-17 16:38:03 -0800 | [diff] [blame] | 278 | { |
John Wedig | b810c92 | 2021-11-17 16:38:03 -0800 | [diff] [blame] | 279 | EXPECT_CALL(*mockCryptIface, cryptFormat(_, _, _, _, _, _, _, _)).Times(1); |
| 280 | |
| 281 | EXPECT_CALL(*mockCryptIface, cryptKeyslotAddByVolumeKey(_, _, _, _, _, _)) |
| 282 | .Times(1); |
| 283 | |
| 284 | EXPECT_CALL(*mockCryptIface, cryptLoad(_, _, _)).Times(1); |
| 285 | |
| 286 | EXPECT_CALL(*mockCryptIface, cryptActivateByPassphrase(_, _, _, _, _, _)) |
| 287 | .Times(1); |
| 288 | |
| 289 | EXPECT_CALL(*mockFsIface, runMkfs(testLuksDevName)).WillOnce(Return(0)); |
| 290 | |
John Wedig | b17f825 | 2022-01-12 14:24:26 -0800 | [diff] [blame] | 291 | EXPECT_CALL(*mockFsIface, directoryExists(path(esObject->getMountPoint()))) |
| 292 | .WillOnce(Return(false)); |
| 293 | |
John Wedig | b810c92 | 2021-11-17 16:38:03 -0800 | [diff] [blame] | 294 | EXPECT_CALL(*mockFsIface, createDirectory(path(esObject->getMountPoint()))) |
| 295 | .WillOnce(Return(false)); |
| 296 | |
John Wedig | 972c3fa | 2021-12-29 17:30:41 -0800 | [diff] [blame] | 297 | EXPECT_THROW(esObject->formatLuks(password, Volume::FilesystemType::ext4), |
| 298 | InternalFailure); |
John Wedig | b810c92 | 2021-11-17 16:38:03 -0800 | [diff] [blame] | 299 | EXPECT_FALSE(esObject->isLocked()); |
| 300 | } |
| 301 | |
| 302 | /* Test case where we fail to mount the filesystem. */ |
Ed Tanous | 82897c3 | 2022-02-21 14:11:59 -0800 | [diff] [blame] | 303 | TEST_F(EStoragedTest, MountFail) |
John Wedig | b810c92 | 2021-11-17 16:38:03 -0800 | [diff] [blame] | 304 | { |
John Wedig | b810c92 | 2021-11-17 16:38:03 -0800 | [diff] [blame] | 305 | EXPECT_CALL(*mockCryptIface, cryptFormat(_, _, _, _, _, _, _, _)).Times(1); |
| 306 | |
| 307 | EXPECT_CALL(*mockCryptIface, cryptKeyslotAddByVolumeKey(_, _, _, _, _, _)) |
| 308 | .Times(1); |
| 309 | |
| 310 | EXPECT_CALL(*mockCryptIface, cryptLoad(_, _, _)).Times(1); |
| 311 | |
| 312 | EXPECT_CALL(*mockCryptIface, cryptActivateByPassphrase(_, _, _, _, _, _)) |
| 313 | .Times(1); |
| 314 | |
| 315 | EXPECT_CALL(*mockFsIface, runMkfs(testLuksDevName)).WillOnce(Return(0)); |
| 316 | |
John Wedig | b17f825 | 2022-01-12 14:24:26 -0800 | [diff] [blame] | 317 | EXPECT_CALL(*mockFsIface, directoryExists(path(esObject->getMountPoint()))) |
| 318 | .WillOnce(Return(false)); |
| 319 | |
John Wedig | b810c92 | 2021-11-17 16:38:03 -0800 | [diff] [blame] | 320 | EXPECT_CALL(*mockFsIface, createDirectory(path(esObject->getMountPoint()))) |
| 321 | .WillOnce(Return(true)); |
| 322 | |
| 323 | EXPECT_CALL(*mockFsIface, |
| 324 | doMount(ContainsRegex("/dev/mapper/"), |
| 325 | StrEq(esObject->getMountPoint()), _, _, _)) |
| 326 | .WillOnce(Return(-1)); |
| 327 | |
| 328 | EXPECT_CALL(*mockFsIface, removeDirectory(path(esObject->getMountPoint()))) |
| 329 | .WillOnce(Return(true)); |
| 330 | |
John Wedig | 972c3fa | 2021-12-29 17:30:41 -0800 | [diff] [blame] | 331 | EXPECT_THROW(esObject->formatLuks(password, Volume::FilesystemType::ext4), |
| 332 | InternalFailure); |
John Wedig | b810c92 | 2021-11-17 16:38:03 -0800 | [diff] [blame] | 333 | EXPECT_FALSE(esObject->isLocked()); |
| 334 | } |
| 335 | |
| 336 | /* Test case where we fail to unmount the filesystem. */ |
Ed Tanous | 82897c3 | 2022-02-21 14:11:59 -0800 | [diff] [blame] | 337 | TEST_F(EStoragedTest, UnmountFail) |
John Wedig | b810c92 | 2021-11-17 16:38:03 -0800 | [diff] [blame] | 338 | { |
John Wedig | b810c92 | 2021-11-17 16:38:03 -0800 | [diff] [blame] | 339 | EXPECT_CALL(*mockCryptIface, cryptFormat(_, _, _, _, _, _, _, _)).Times(1); |
| 340 | |
| 341 | EXPECT_CALL(*mockCryptIface, cryptKeyslotAddByVolumeKey(_, _, _, _, _, _)) |
| 342 | .Times(1); |
| 343 | |
| 344 | EXPECT_CALL(*mockCryptIface, cryptLoad(_, _, _)).Times(1); |
| 345 | |
| 346 | EXPECT_CALL(*mockCryptIface, cryptActivateByPassphrase(_, _, _, _, _, _)) |
| 347 | .Times(1); |
| 348 | |
| 349 | EXPECT_CALL(*mockFsIface, runMkfs(testLuksDevName)).WillOnce(Return(0)); |
| 350 | |
John Wedig | b17f825 | 2022-01-12 14:24:26 -0800 | [diff] [blame] | 351 | EXPECT_CALL(*mockFsIface, directoryExists(path(esObject->getMountPoint()))) |
| 352 | .WillOnce(Return(false)); |
| 353 | |
John Wedig | b810c92 | 2021-11-17 16:38:03 -0800 | [diff] [blame] | 354 | EXPECT_CALL(*mockFsIface, createDirectory(path(esObject->getMountPoint()))) |
| 355 | .WillOnce(Return(true)); |
| 356 | |
| 357 | EXPECT_CALL(*mockFsIface, |
| 358 | doMount(ContainsRegex("/dev/mapper/"), |
| 359 | StrEq(esObject->getMountPoint()), _, _, _)) |
| 360 | .WillOnce(Return(0)); |
| 361 | |
| 362 | EXPECT_CALL(*mockFsIface, doUnmount(StrEq(esObject->getMountPoint()))) |
| 363 | .WillOnce(Return(-1)); |
| 364 | |
John Wedig | 972c3fa | 2021-12-29 17:30:41 -0800 | [diff] [blame] | 365 | esObject->formatLuks(password, Volume::FilesystemType::ext4); |
John Wedig | b810c92 | 2021-11-17 16:38:03 -0800 | [diff] [blame] | 366 | EXPECT_FALSE(esObject->isLocked()); |
| 367 | |
John Wedig | 972c3fa | 2021-12-29 17:30:41 -0800 | [diff] [blame] | 368 | EXPECT_THROW(esObject->lock(), InternalFailure); |
John Wedig | b810c92 | 2021-11-17 16:38:03 -0800 | [diff] [blame] | 369 | EXPECT_FALSE(esObject->isLocked()); |
| 370 | } |
| 371 | |
| 372 | /* Test case where we fail to remove the mount point. */ |
Ed Tanous | 82897c3 | 2022-02-21 14:11:59 -0800 | [diff] [blame] | 373 | TEST_F(EStoragedTest, RemoveMountPointFail) |
John Wedig | b810c92 | 2021-11-17 16:38:03 -0800 | [diff] [blame] | 374 | { |
John Wedig | b810c92 | 2021-11-17 16:38:03 -0800 | [diff] [blame] | 375 | EXPECT_CALL(*mockCryptIface, cryptFormat(_, _, _, _, _, _, _, _)).Times(1); |
| 376 | |
| 377 | EXPECT_CALL(*mockCryptIface, cryptKeyslotAddByVolumeKey(_, _, _, _, _, _)) |
| 378 | .Times(1); |
| 379 | |
| 380 | EXPECT_CALL(*mockCryptIface, cryptLoad(_, _, _)).Times(1); |
| 381 | |
| 382 | EXPECT_CALL(*mockCryptIface, cryptActivateByPassphrase(_, _, _, _, _, _)) |
| 383 | .Times(1); |
| 384 | |
| 385 | EXPECT_CALL(*mockFsIface, runMkfs(testLuksDevName)).WillOnce(Return(0)); |
| 386 | |
John Wedig | b17f825 | 2022-01-12 14:24:26 -0800 | [diff] [blame] | 387 | EXPECT_CALL(*mockFsIface, directoryExists(path(esObject->getMountPoint()))) |
| 388 | .WillOnce(Return(false)); |
| 389 | |
John Wedig | b810c92 | 2021-11-17 16:38:03 -0800 | [diff] [blame] | 390 | EXPECT_CALL(*mockFsIface, createDirectory(path(esObject->getMountPoint()))) |
| 391 | .WillOnce(Return(true)); |
| 392 | |
| 393 | EXPECT_CALL(*mockFsIface, |
| 394 | doMount(ContainsRegex("/dev/mapper/"), |
| 395 | StrEq(esObject->getMountPoint()), _, _, _)) |
| 396 | .WillOnce(Return(0)); |
| 397 | |
| 398 | EXPECT_CALL(*mockFsIface, doUnmount(StrEq(esObject->getMountPoint()))) |
| 399 | .WillOnce(Return(0)); |
| 400 | |
| 401 | EXPECT_CALL(*mockFsIface, removeDirectory(path(esObject->getMountPoint()))) |
| 402 | .WillOnce(Return(false)); |
| 403 | |
John Wedig | 972c3fa | 2021-12-29 17:30:41 -0800 | [diff] [blame] | 404 | esObject->formatLuks(password, Volume::FilesystemType::ext4); |
John Wedig | b810c92 | 2021-11-17 16:38:03 -0800 | [diff] [blame] | 405 | EXPECT_FALSE(esObject->isLocked()); |
| 406 | |
| 407 | /* This will fail to remove the mount point. */ |
John Wedig | 972c3fa | 2021-12-29 17:30:41 -0800 | [diff] [blame] | 408 | EXPECT_THROW(esObject->lock(), InternalFailure); |
John Wedig | b810c92 | 2021-11-17 16:38:03 -0800 | [diff] [blame] | 409 | EXPECT_FALSE(esObject->isLocked()); |
| 410 | } |
| 411 | |
| 412 | /* Test case where we fail to deactivate the LUKS device. */ |
Ed Tanous | 82897c3 | 2022-02-21 14:11:59 -0800 | [diff] [blame] | 413 | TEST_F(EStoragedTest, DeactivateFail) |
John Wedig | b810c92 | 2021-11-17 16:38:03 -0800 | [diff] [blame] | 414 | { |
John Wedig | b810c92 | 2021-11-17 16:38:03 -0800 | [diff] [blame] | 415 | EXPECT_CALL(*mockCryptIface, cryptFormat(_, _, _, _, _, _, _, _)).Times(1); |
| 416 | |
| 417 | EXPECT_CALL(*mockCryptIface, cryptKeyslotAddByVolumeKey(_, _, _, _, _, _)) |
| 418 | .Times(1); |
| 419 | |
| 420 | EXPECT_CALL(*mockCryptIface, cryptLoad(_, _, _)).Times(1); |
| 421 | |
| 422 | EXPECT_CALL(*mockCryptIface, cryptActivateByPassphrase(_, _, _, _, _, _)) |
| 423 | .Times(1); |
| 424 | |
| 425 | EXPECT_CALL(*mockFsIface, runMkfs(testLuksDevName)).WillOnce(Return(0)); |
| 426 | |
John Wedig | b17f825 | 2022-01-12 14:24:26 -0800 | [diff] [blame] | 427 | EXPECT_CALL(*mockFsIface, directoryExists(path(esObject->getMountPoint()))) |
| 428 | .WillOnce(Return(false)); |
| 429 | |
John Wedig | b810c92 | 2021-11-17 16:38:03 -0800 | [diff] [blame] | 430 | EXPECT_CALL(*mockFsIface, createDirectory(path(esObject->getMountPoint()))) |
| 431 | .WillOnce(Return(true)); |
| 432 | |
| 433 | EXPECT_CALL(*mockFsIface, |
| 434 | doMount(ContainsRegex("/dev/mapper/"), |
| 435 | StrEq(esObject->getMountPoint()), _, _, _)) |
| 436 | .WillOnce(Return(0)); |
| 437 | |
| 438 | EXPECT_CALL(*mockFsIface, doUnmount(StrEq(esObject->getMountPoint()))) |
| 439 | .WillOnce(Return(0)); |
| 440 | |
| 441 | EXPECT_CALL(*mockFsIface, removeDirectory(path(esObject->getMountPoint()))) |
| 442 | .WillOnce(Return(true)); |
| 443 | |
| 444 | EXPECT_CALL(*mockCryptIface, cryptDeactivate(_, _)).WillOnce(Return(-1)); |
| 445 | |
| 446 | /* Format the encrypted device. */ |
John Wedig | 972c3fa | 2021-12-29 17:30:41 -0800 | [diff] [blame] | 447 | esObject->formatLuks(password, Volume::FilesystemType::ext4); |
John Wedig | b810c92 | 2021-11-17 16:38:03 -0800 | [diff] [blame] | 448 | EXPECT_FALSE(esObject->isLocked()); |
| 449 | |
John Wedig | 972c3fa | 2021-12-29 17:30:41 -0800 | [diff] [blame] | 450 | EXPECT_THROW(esObject->lock(), InternalFailure); |
John Wedig | b810c92 | 2021-11-17 16:38:03 -0800 | [diff] [blame] | 451 | EXPECT_FALSE(esObject->isLocked()); |
| 452 | } |
| 453 | |
| 454 | } // namespace estoraged_test |