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