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 | b810c92 | 2021-11-17 16:38:03 -0800 | [diff] [blame] | 7 | #include <sdbusplus/test/sdbus_mock.hpp> |
John Wedig | 972c3fa | 2021-12-29 17:30:41 -0800 | [diff] [blame] | 8 | #include <xyz/openbmc_project/Common/error.hpp> |
John Edward Broadbent | 59dffa6 | 2022-01-13 17:41:32 -0800 | [diff] [blame] | 9 | #include <xyz/openbmc_project/Inventory/Item/Volume/client.hpp> |
John Wedig | b810c92 | 2021-11-17 16:38:03 -0800 | [diff] [blame] | 10 | |
| 11 | #include <exception> |
| 12 | #include <filesystem> |
| 13 | #include <fstream> |
| 14 | #include <iterator> |
| 15 | #include <string> |
| 16 | #include <vector> |
| 17 | |
| 18 | #include <gmock/gmock.h> |
| 19 | #include <gtest/gtest.h> |
| 20 | |
| 21 | namespace estoraged_test |
| 22 | { |
| 23 | |
John Wedig | 972c3fa | 2021-12-29 17:30:41 -0800 | [diff] [blame] | 24 | using sdbusplus::xyz::openbmc_project::Common::Error::InternalFailure; |
| 25 | using sdbusplus::xyz::openbmc_project::Common::Error::ResourceNotFound; |
| 26 | using sdbusplus::xyz::openbmc_project::Inventory::Item::server::Volume; |
John Wedig | b810c92 | 2021-11-17 16:38:03 -0800 | [diff] [blame] | 27 | using std::filesystem::path; |
| 28 | using ::testing::_; |
| 29 | using ::testing::ContainsRegex; |
| 30 | using ::testing::IsNull; |
| 31 | using ::testing::Return; |
| 32 | using ::testing::StrEq; |
| 33 | |
| 34 | /* |
| 35 | * This sdbus mock object gets used in the destructor of one of the parent |
| 36 | * classes for the MockeStoraged object, so this can't be part of the |
| 37 | * eStoragedTest class. |
| 38 | */ |
| 39 | sdbusplus::SdBusMock sdbusMock; |
| 40 | |
Ed Tanous | 82897c3 | 2022-02-21 14:11:59 -0800 | [diff] [blame] | 41 | class EStoragedTest : public testing::Test |
John Wedig | b810c92 | 2021-11-17 16:38:03 -0800 | [diff] [blame] | 42 | { |
| 43 | public: |
Ed Tanous | 82897c3 | 2022-02-21 14:11:59 -0800 | [diff] [blame] | 44 | const char* testFileName = "testfile"; |
| 45 | const char* testLuksDevName = "testfile_luksDev"; |
John Edward Broadbent | e35e736 | 2022-03-22 16:14:24 -0700 | [diff] [blame] | 46 | uint64_t testSize = 24; |
John Wedig | b810c92 | 2021-11-17 16:38:03 -0800 | [diff] [blame] | 47 | std::ofstream testFile; |
Ed Tanous | 82897c3 | 2022-02-21 14:11:59 -0800 | [diff] [blame] | 48 | std::unique_ptr<estoraged::EStoraged> esObject; |
| 49 | const char* testPath = "/test/openbmc_project/storage/test_dev"; |
| 50 | const char* estoragedInterface = |
John Wedig | 972c3fa | 2021-12-29 17:30:41 -0800 | [diff] [blame] | 51 | "xyz.openbmc_project.Inventory.Item.Volume"; |
John Edward Broadbent | 86dfb24 | 2022-03-14 11:04:36 -0700 | [diff] [blame] | 52 | const char* driveInterface = "xyz.openbmc_project.Inventory.Item.Drive"; |
John Wedig | b810c92 | 2021-11-17 16:38:03 -0800 | [diff] [blame] | 53 | sdbusplus::bus::bus bus; |
| 54 | std::string passwordString; |
| 55 | std::vector<uint8_t> password; |
Ed Tanous | 82897c3 | 2022-02-21 14:11:59 -0800 | [diff] [blame] | 56 | MockCryptsetupInterface* mockCryptIface{}; |
| 57 | MockFilesystemInterface* mockFsIface{}; |
John Wedig | b810c92 | 2021-11-17 16:38:03 -0800 | [diff] [blame] | 58 | |
Ed Tanous | 82897c3 | 2022-02-21 14:11:59 -0800 | [diff] [blame] | 59 | EStoragedTest() : |
John Wedig | b810c92 | 2021-11-17 16:38:03 -0800 | [diff] [blame] | 60 | bus(sdbusplus::get_mocked_new(&sdbusMock)), passwordString("password"), |
| 61 | password(passwordString.begin(), passwordString.end()) |
| 62 | {} |
| 63 | |
| 64 | void SetUp() override |
| 65 | { |
| 66 | /* Create an empty file that we'll pretend is a 'storage device'. */ |
| 67 | testFile.open(testFileName, |
| 68 | std::ios::out | std::ios::binary | std::ios::trunc); |
| 69 | testFile.close(); |
| 70 | if (testFile.fail()) |
| 71 | { |
| 72 | throw std::runtime_error("Failed to open test file"); |
| 73 | } |
| 74 | |
| 75 | EXPECT_CALL(sdbusMock, |
Ed Tanous | 82897c3 | 2022-02-21 14:11:59 -0800 | [diff] [blame] | 76 | sd_bus_add_object_vtable(IsNull(), _, StrEq(testPath), |
| 77 | StrEq(estoragedInterface), _, _)) |
John Wedig | b810c92 | 2021-11-17 16:38:03 -0800 | [diff] [blame] | 78 | .WillRepeatedly(Return(0)); |
| 79 | |
| 80 | EXPECT_CALL(sdbusMock, |
John Edward Broadbent | 86dfb24 | 2022-03-14 11:04:36 -0700 | [diff] [blame] | 81 | sd_bus_add_object_vtable(IsNull(), _, StrEq(testPath), |
| 82 | StrEq(driveInterface), _, _)) |
| 83 | .WillRepeatedly(Return(0)); |
| 84 | |
| 85 | EXPECT_CALL(sdbusMock, |
Ed Tanous | 82897c3 | 2022-02-21 14:11:59 -0800 | [diff] [blame] | 86 | sd_bus_emit_object_added(IsNull(), StrEq(testPath))) |
John Wedig | b810c92 | 2021-11-17 16:38:03 -0800 | [diff] [blame] | 87 | .WillRepeatedly(Return(0)); |
| 88 | |
| 89 | EXPECT_CALL(sdbusMock, |
Ed Tanous | 82897c3 | 2022-02-21 14:11:59 -0800 | [diff] [blame] | 90 | sd_bus_emit_object_removed(IsNull(), StrEq(testPath))) |
John Wedig | b810c92 | 2021-11-17 16:38:03 -0800 | [diff] [blame] | 91 | .WillRepeatedly(Return(0)); |
| 92 | |
| 93 | std::unique_ptr<MockCryptsetupInterface> cryptIface = |
| 94 | std::make_unique<MockCryptsetupInterface>(); |
| 95 | mockCryptIface = cryptIface.get(); |
| 96 | std::unique_ptr<MockFilesystemInterface> fsIface = |
| 97 | std::make_unique<MockFilesystemInterface>(); |
| 98 | mockFsIface = fsIface.get(); |
| 99 | |
Ed Tanous | 82897c3 | 2022-02-21 14:11:59 -0800 | [diff] [blame] | 100 | esObject = std::make_unique<estoraged::EStoraged>( |
John Edward Broadbent | e35e736 | 2022-03-22 16:14:24 -0700 | [diff] [blame] | 101 | bus, testPath, testFileName, testLuksDevName, testSize, |
| 102 | std::move(cryptIface), std::move(fsIface)); |
John Wedig | b810c92 | 2021-11-17 16:38:03 -0800 | [diff] [blame] | 103 | } |
| 104 | |
| 105 | void TearDown() override |
| 106 | { |
| 107 | EXPECT_EQ(0, unlink(testFileName)); |
| 108 | } |
| 109 | }; |
| 110 | |
| 111 | /* Test case to format and then lock the LUKS device. */ |
Ed Tanous | 82897c3 | 2022-02-21 14:11:59 -0800 | [diff] [blame] | 112 | TEST_F(EStoragedTest, FormatPass) |
John Wedig | b810c92 | 2021-11-17 16:38:03 -0800 | [diff] [blame] | 113 | { |
| 114 | EXPECT_CALL(sdbusMock, |
John Edward Broadbent | 86dfb24 | 2022-03-14 11:04:36 -0700 | [diff] [blame] | 115 | sd_bus_emit_properties_changed_strv(IsNull(), StrEq(testPath), |
| 116 | StrEq(driveInterface), _)) |
| 117 | .WillRepeatedly(Return(0)); |
| 118 | |
| 119 | EXPECT_CALL(sdbusMock, |
John Wedig | b810c92 | 2021-11-17 16:38:03 -0800 | [diff] [blame] | 120 | sd_bus_emit_properties_changed_strv( |
Ed Tanous | 82897c3 | 2022-02-21 14:11:59 -0800 | [diff] [blame] | 121 | IsNull(), StrEq(testPath), StrEq(estoragedInterface), _)) |
John Wedig | b810c92 | 2021-11-17 16:38:03 -0800 | [diff] [blame] | 122 | .WillRepeatedly(Return(0)); |
| 123 | |
| 124 | EXPECT_CALL(*mockCryptIface, cryptFormat(_, _, _, _, _, _, _, _)).Times(1); |
| 125 | |
| 126 | EXPECT_CALL(*mockCryptIface, cryptKeyslotAddByVolumeKey(_, _, _, _, _, _)) |
| 127 | .Times(1); |
| 128 | |
| 129 | EXPECT_CALL(*mockCryptIface, cryptLoad(_, _, _)).Times(1); |
| 130 | |
| 131 | EXPECT_CALL(*mockCryptIface, cryptActivateByPassphrase(_, _, _, _, _, _)) |
| 132 | .Times(1); |
| 133 | |
| 134 | EXPECT_CALL(*mockFsIface, runMkfs(testLuksDevName)).WillOnce(Return(0)); |
| 135 | |
John Wedig | b17f825 | 2022-01-12 14:24:26 -0800 | [diff] [blame] | 136 | EXPECT_CALL(*mockFsIface, directoryExists(path(esObject->getMountPoint()))) |
| 137 | .WillOnce(Return(false)); |
| 138 | |
John Wedig | b810c92 | 2021-11-17 16:38:03 -0800 | [diff] [blame] | 139 | EXPECT_CALL(*mockFsIface, createDirectory(path(esObject->getMountPoint()))) |
| 140 | .WillOnce(Return(true)); |
| 141 | |
| 142 | EXPECT_CALL(*mockFsIface, |
| 143 | doMount(ContainsRegex("/dev/mapper/"), |
| 144 | StrEq(esObject->getMountPoint()), _, _, _)) |
| 145 | .WillOnce(Return(0)); |
| 146 | |
| 147 | EXPECT_CALL(*mockFsIface, doUnmount(StrEq(esObject->getMountPoint()))) |
| 148 | .WillOnce(Return(0)); |
| 149 | |
| 150 | EXPECT_CALL(*mockFsIface, removeDirectory(path(esObject->getMountPoint()))) |
| 151 | .WillOnce(Return(true)); |
| 152 | |
| 153 | EXPECT_CALL(*mockCryptIface, cryptDeactivate(_, _)).Times(1); |
| 154 | |
| 155 | /* Format the encrypted device. */ |
John Wedig | 972c3fa | 2021-12-29 17:30:41 -0800 | [diff] [blame] | 156 | esObject->formatLuks(password, Volume::FilesystemType::ext4); |
John Wedig | b810c92 | 2021-11-17 16:38:03 -0800 | [diff] [blame] | 157 | EXPECT_FALSE(esObject->isLocked()); |
| 158 | |
John Wedig | 972c3fa | 2021-12-29 17:30:41 -0800 | [diff] [blame] | 159 | esObject->lock(); |
John Wedig | b810c92 | 2021-11-17 16:38:03 -0800 | [diff] [blame] | 160 | EXPECT_TRUE(esObject->isLocked()); |
| 161 | } |
| 162 | |
John Wedig | b17f825 | 2022-01-12 14:24:26 -0800 | [diff] [blame] | 163 | /* |
| 164 | * Test case where the mount point directory already exists, so it shouldn't |
| 165 | * try to create it. |
| 166 | */ |
Ed Tanous | 82897c3 | 2022-02-21 14:11:59 -0800 | [diff] [blame] | 167 | TEST_F(EStoragedTest, MountPointExistsPass) |
John Wedig | b17f825 | 2022-01-12 14:24:26 -0800 | [diff] [blame] | 168 | { |
| 169 | EXPECT_CALL(sdbusMock, |
John Edward Broadbent | 86dfb24 | 2022-03-14 11:04:36 -0700 | [diff] [blame] | 170 | sd_bus_emit_properties_changed_strv(IsNull(), StrEq(testPath), |
| 171 | StrEq(driveInterface), _)) |
| 172 | .WillRepeatedly(Return(0)); |
| 173 | |
| 174 | EXPECT_CALL(sdbusMock, |
John Wedig | b17f825 | 2022-01-12 14:24:26 -0800 | [diff] [blame] | 175 | sd_bus_emit_properties_changed_strv( |
Ed Tanous | 82897c3 | 2022-02-21 14:11:59 -0800 | [diff] [blame] | 176 | IsNull(), StrEq(testPath), StrEq(estoragedInterface), _)) |
John Wedig | b17f825 | 2022-01-12 14:24:26 -0800 | [diff] [blame] | 177 | .WillRepeatedly(Return(0)); |
| 178 | |
| 179 | EXPECT_CALL(*mockCryptIface, cryptFormat(_, _, _, _, _, _, _, _)).Times(1); |
| 180 | |
| 181 | EXPECT_CALL(*mockCryptIface, cryptKeyslotAddByVolumeKey(_, _, _, _, _, _)) |
| 182 | .Times(1); |
| 183 | |
| 184 | EXPECT_CALL(*mockCryptIface, cryptLoad(_, _, _)).Times(1); |
| 185 | |
| 186 | EXPECT_CALL(*mockCryptIface, cryptActivateByPassphrase(_, _, _, _, _, _)) |
| 187 | .Times(1); |
| 188 | |
| 189 | EXPECT_CALL(*mockFsIface, runMkfs(testLuksDevName)).WillOnce(Return(0)); |
| 190 | |
| 191 | EXPECT_CALL(*mockFsIface, directoryExists(path(esObject->getMountPoint()))) |
| 192 | .WillOnce(Return(true)); |
| 193 | |
| 194 | EXPECT_CALL(*mockFsIface, createDirectory(path(esObject->getMountPoint()))) |
| 195 | .Times(0); |
| 196 | |
| 197 | EXPECT_CALL(*mockFsIface, |
| 198 | doMount(ContainsRegex("/dev/mapper/"), |
| 199 | StrEq(esObject->getMountPoint()), _, _, _)) |
| 200 | .WillOnce(Return(0)); |
| 201 | |
| 202 | EXPECT_CALL(*mockFsIface, doUnmount(StrEq(esObject->getMountPoint()))) |
| 203 | .WillOnce(Return(0)); |
| 204 | |
| 205 | EXPECT_CALL(*mockFsIface, removeDirectory(path(esObject->getMountPoint()))) |
| 206 | .WillOnce(Return(true)); |
| 207 | |
| 208 | EXPECT_CALL(*mockCryptIface, cryptDeactivate(_, _)).Times(1); |
| 209 | |
| 210 | /* Format the encrypted device. */ |
| 211 | esObject->formatLuks(password, Volume::FilesystemType::ext4); |
| 212 | EXPECT_FALSE(esObject->isLocked()); |
| 213 | |
| 214 | esObject->lock(); |
| 215 | EXPECT_TRUE(esObject->isLocked()); |
| 216 | } |
| 217 | |
John Wedig | b810c92 | 2021-11-17 16:38:03 -0800 | [diff] [blame] | 218 | /* Test case where the device/file doesn't exist. */ |
Ed Tanous | 82897c3 | 2022-02-21 14:11:59 -0800 | [diff] [blame] | 219 | TEST_F(EStoragedTest, FormatNoDeviceFail) |
John Wedig | b810c92 | 2021-11-17 16:38:03 -0800 | [diff] [blame] | 220 | { |
| 221 | /* Delete the test file. */ |
| 222 | EXPECT_EQ(0, unlink(testFileName)); |
| 223 | |
John Wedig | 972c3fa | 2021-12-29 17:30:41 -0800 | [diff] [blame] | 224 | EXPECT_THROW(esObject->formatLuks(password, Volume::FilesystemType::ext4), |
| 225 | ResourceNotFound); |
John Wedig | b810c92 | 2021-11-17 16:38:03 -0800 | [diff] [blame] | 226 | EXPECT_FALSE(esObject->isLocked()); |
| 227 | |
| 228 | /* Create the test file again, so that the TearDown function works. */ |
| 229 | testFile.open(testFileName, |
| 230 | std::ios::out | std::ios::binary | std::ios::trunc); |
| 231 | testFile.close(); |
| 232 | } |
| 233 | |
| 234 | /* Test case where we fail to format the LUKS device. */ |
Ed Tanous | 82897c3 | 2022-02-21 14:11:59 -0800 | [diff] [blame] | 235 | TEST_F(EStoragedTest, FormatFail) |
John Wedig | b810c92 | 2021-11-17 16:38:03 -0800 | [diff] [blame] | 236 | { |
| 237 | EXPECT_CALL(*mockCryptIface, cryptFormat(_, _, _, _, _, _, _, _)) |
| 238 | .WillOnce(Return(-1)); |
| 239 | |
John Wedig | 972c3fa | 2021-12-29 17:30:41 -0800 | [diff] [blame] | 240 | EXPECT_THROW(esObject->formatLuks(password, Volume::FilesystemType::ext4), |
| 241 | InternalFailure); |
John Wedig | b810c92 | 2021-11-17 16:38:03 -0800 | [diff] [blame] | 242 | EXPECT_FALSE(esObject->isLocked()); |
| 243 | } |
| 244 | |
| 245 | /* 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] | 246 | TEST_F(EStoragedTest, AddKeyslotFail) |
John Wedig | b810c92 | 2021-11-17 16:38:03 -0800 | [diff] [blame] | 247 | { |
| 248 | EXPECT_CALL(sdbusMock, |
John Edward Broadbent | 86dfb24 | 2022-03-14 11:04:36 -0700 | [diff] [blame] | 249 | sd_bus_emit_properties_changed_strv(IsNull(), StrEq(testPath), |
| 250 | StrEq(driveInterface), _)) |
| 251 | .WillRepeatedly(Return(0)); |
| 252 | |
| 253 | EXPECT_CALL(sdbusMock, |
John Wedig | b810c92 | 2021-11-17 16:38:03 -0800 | [diff] [blame] | 254 | sd_bus_emit_properties_changed_strv( |
Ed Tanous | 82897c3 | 2022-02-21 14:11:59 -0800 | [diff] [blame] | 255 | IsNull(), StrEq(testPath), StrEq(estoragedInterface), _)) |
John Wedig | b810c92 | 2021-11-17 16:38:03 -0800 | [diff] [blame] | 256 | .WillRepeatedly(Return(0)); |
| 257 | |
| 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 | { |
| 271 | EXPECT_CALL(sdbusMock, |
John Edward Broadbent | 86dfb24 | 2022-03-14 11:04:36 -0700 | [diff] [blame] | 272 | sd_bus_emit_properties_changed_strv(IsNull(), StrEq(testPath), |
| 273 | StrEq(driveInterface), _)) |
| 274 | .WillRepeatedly(Return(0)); |
| 275 | |
| 276 | EXPECT_CALL(sdbusMock, |
John Wedig | b810c92 | 2021-11-17 16:38:03 -0800 | [diff] [blame] | 277 | sd_bus_emit_properties_changed_strv( |
Ed Tanous | 82897c3 | 2022-02-21 14:11:59 -0800 | [diff] [blame] | 278 | IsNull(), StrEq(testPath), StrEq(estoragedInterface), _)) |
John Wedig | b810c92 | 2021-11-17 16:38:03 -0800 | [diff] [blame] | 279 | .WillRepeatedly(Return(0)); |
| 280 | |
| 281 | EXPECT_CALL(*mockCryptIface, cryptFormat(_, _, _, _, _, _, _, _)).Times(1); |
| 282 | |
| 283 | EXPECT_CALL(*mockCryptIface, cryptKeyslotAddByVolumeKey(_, _, _, _, _, _)) |
| 284 | .Times(1); |
| 285 | |
| 286 | EXPECT_CALL(*mockCryptIface, cryptLoad(_, _, _)).WillOnce(Return(-1)); |
| 287 | |
John Wedig | 972c3fa | 2021-12-29 17:30:41 -0800 | [diff] [blame] | 288 | EXPECT_THROW(esObject->formatLuks(password, Volume::FilesystemType::ext4), |
| 289 | InternalFailure); |
John Wedig | b810c92 | 2021-11-17 16:38:03 -0800 | [diff] [blame] | 290 | EXPECT_TRUE(esObject->isLocked()); |
| 291 | } |
| 292 | |
| 293 | /* Test case where we fail to activate the LUKS device. */ |
Ed Tanous | 82897c3 | 2022-02-21 14:11:59 -0800 | [diff] [blame] | 294 | TEST_F(EStoragedTest, ActivateFail) |
John Wedig | b810c92 | 2021-11-17 16:38:03 -0800 | [diff] [blame] | 295 | { |
| 296 | EXPECT_CALL(sdbusMock, |
John Edward Broadbent | 86dfb24 | 2022-03-14 11:04:36 -0700 | [diff] [blame] | 297 | sd_bus_emit_properties_changed_strv(IsNull(), StrEq(testPath), |
| 298 | StrEq(driveInterface), _)) |
| 299 | .WillRepeatedly(Return(0)); |
| 300 | |
| 301 | EXPECT_CALL(sdbusMock, |
John Wedig | b810c92 | 2021-11-17 16:38:03 -0800 | [diff] [blame] | 302 | sd_bus_emit_properties_changed_strv( |
Ed Tanous | 82897c3 | 2022-02-21 14:11:59 -0800 | [diff] [blame] | 303 | IsNull(), StrEq(testPath), StrEq(estoragedInterface), _)) |
John Wedig | b810c92 | 2021-11-17 16:38:03 -0800 | [diff] [blame] | 304 | .WillRepeatedly(Return(0)); |
| 305 | |
| 306 | EXPECT_CALL(*mockCryptIface, cryptFormat(_, _, _, _, _, _, _, _)).Times(1); |
| 307 | |
| 308 | EXPECT_CALL(*mockCryptIface, cryptKeyslotAddByVolumeKey(_, _, _, _, _, _)) |
| 309 | .Times(1); |
| 310 | |
| 311 | EXPECT_CALL(*mockCryptIface, cryptLoad(_, _, _)).Times(1); |
| 312 | |
| 313 | EXPECT_CALL(*mockCryptIface, cryptActivateByPassphrase(_, _, _, _, _, _)) |
| 314 | .WillOnce(Return(-1)); |
| 315 | |
John Wedig | 972c3fa | 2021-12-29 17:30:41 -0800 | [diff] [blame] | 316 | EXPECT_THROW(esObject->formatLuks(password, Volume::FilesystemType::ext4), |
| 317 | InternalFailure); |
John Wedig | b810c92 | 2021-11-17 16:38:03 -0800 | [diff] [blame] | 318 | EXPECT_TRUE(esObject->isLocked()); |
| 319 | } |
| 320 | |
| 321 | /* Test case where we fail to create the filesystem. */ |
Ed Tanous | 82897c3 | 2022-02-21 14:11:59 -0800 | [diff] [blame] | 322 | TEST_F(EStoragedTest, CreateFilesystemFail) |
John Wedig | b810c92 | 2021-11-17 16:38:03 -0800 | [diff] [blame] | 323 | { |
| 324 | EXPECT_CALL(sdbusMock, |
John Edward Broadbent | 86dfb24 | 2022-03-14 11:04:36 -0700 | [diff] [blame] | 325 | sd_bus_emit_properties_changed_strv(IsNull(), StrEq(testPath), |
| 326 | StrEq(driveInterface), _)) |
| 327 | .WillRepeatedly(Return(0)); |
| 328 | |
| 329 | EXPECT_CALL(sdbusMock, |
John Wedig | b810c92 | 2021-11-17 16:38:03 -0800 | [diff] [blame] | 330 | sd_bus_emit_properties_changed_strv( |
Ed Tanous | 82897c3 | 2022-02-21 14:11:59 -0800 | [diff] [blame] | 331 | IsNull(), StrEq(testPath), StrEq(estoragedInterface), _)) |
John Wedig | b810c92 | 2021-11-17 16:38:03 -0800 | [diff] [blame] | 332 | .WillRepeatedly(Return(0)); |
| 333 | |
| 334 | EXPECT_CALL(*mockCryptIface, cryptFormat(_, _, _, _, _, _, _, _)).Times(1); |
| 335 | |
| 336 | EXPECT_CALL(*mockCryptIface, cryptKeyslotAddByVolumeKey(_, _, _, _, _, _)) |
| 337 | .Times(1); |
| 338 | |
| 339 | EXPECT_CALL(*mockCryptIface, cryptLoad(_, _, _)).Times(1); |
| 340 | |
| 341 | EXPECT_CALL(*mockCryptIface, cryptActivateByPassphrase(_, _, _, _, _, _)) |
| 342 | .Times(1); |
| 343 | |
| 344 | EXPECT_CALL(*mockFsIface, runMkfs(testLuksDevName)).WillOnce(Return(-1)); |
| 345 | |
John Wedig | 972c3fa | 2021-12-29 17:30:41 -0800 | [diff] [blame] | 346 | EXPECT_THROW(esObject->formatLuks(password, Volume::FilesystemType::ext4), |
| 347 | InternalFailure); |
John Wedig | b810c92 | 2021-11-17 16:38:03 -0800 | [diff] [blame] | 348 | EXPECT_FALSE(esObject->isLocked()); |
| 349 | } |
| 350 | |
| 351 | /* Test case where we fail to create the mount point. */ |
Ed Tanous | 82897c3 | 2022-02-21 14:11:59 -0800 | [diff] [blame] | 352 | TEST_F(EStoragedTest, CreateMountPointFail) |
John Wedig | b810c92 | 2021-11-17 16:38:03 -0800 | [diff] [blame] | 353 | { |
| 354 | EXPECT_CALL(sdbusMock, |
John Edward Broadbent | 86dfb24 | 2022-03-14 11:04:36 -0700 | [diff] [blame] | 355 | sd_bus_emit_properties_changed_strv(IsNull(), StrEq(testPath), |
| 356 | StrEq(driveInterface), _)) |
| 357 | .WillRepeatedly(Return(0)); |
| 358 | |
| 359 | EXPECT_CALL(sdbusMock, |
John Wedig | b810c92 | 2021-11-17 16:38:03 -0800 | [diff] [blame] | 360 | sd_bus_emit_properties_changed_strv( |
Ed Tanous | 82897c3 | 2022-02-21 14:11:59 -0800 | [diff] [blame] | 361 | IsNull(), StrEq(testPath), StrEq(estoragedInterface), _)) |
John Wedig | b810c92 | 2021-11-17 16:38:03 -0800 | [diff] [blame] | 362 | .WillRepeatedly(Return(0)); |
| 363 | |
| 364 | EXPECT_CALL(*mockCryptIface, cryptFormat(_, _, _, _, _, _, _, _)).Times(1); |
| 365 | |
| 366 | EXPECT_CALL(*mockCryptIface, cryptKeyslotAddByVolumeKey(_, _, _, _, _, _)) |
| 367 | .Times(1); |
| 368 | |
| 369 | EXPECT_CALL(*mockCryptIface, cryptLoad(_, _, _)).Times(1); |
| 370 | |
| 371 | EXPECT_CALL(*mockCryptIface, cryptActivateByPassphrase(_, _, _, _, _, _)) |
| 372 | .Times(1); |
| 373 | |
| 374 | EXPECT_CALL(*mockFsIface, runMkfs(testLuksDevName)).WillOnce(Return(0)); |
| 375 | |
John Wedig | b17f825 | 2022-01-12 14:24:26 -0800 | [diff] [blame] | 376 | EXPECT_CALL(*mockFsIface, directoryExists(path(esObject->getMountPoint()))) |
| 377 | .WillOnce(Return(false)); |
| 378 | |
John Wedig | b810c92 | 2021-11-17 16:38:03 -0800 | [diff] [blame] | 379 | EXPECT_CALL(*mockFsIface, createDirectory(path(esObject->getMountPoint()))) |
| 380 | .WillOnce(Return(false)); |
| 381 | |
John Wedig | 972c3fa | 2021-12-29 17:30:41 -0800 | [diff] [blame] | 382 | EXPECT_THROW(esObject->formatLuks(password, Volume::FilesystemType::ext4), |
| 383 | InternalFailure); |
John Wedig | b810c92 | 2021-11-17 16:38:03 -0800 | [diff] [blame] | 384 | EXPECT_FALSE(esObject->isLocked()); |
| 385 | } |
| 386 | |
| 387 | /* Test case where we fail to mount the filesystem. */ |
Ed Tanous | 82897c3 | 2022-02-21 14:11:59 -0800 | [diff] [blame] | 388 | TEST_F(EStoragedTest, MountFail) |
John Wedig | b810c92 | 2021-11-17 16:38:03 -0800 | [diff] [blame] | 389 | { |
| 390 | EXPECT_CALL(sdbusMock, |
John Edward Broadbent | 86dfb24 | 2022-03-14 11:04:36 -0700 | [diff] [blame] | 391 | sd_bus_emit_properties_changed_strv(IsNull(), StrEq(testPath), |
| 392 | StrEq(driveInterface), _)) |
| 393 | .WillRepeatedly(Return(0)); |
| 394 | |
| 395 | EXPECT_CALL(sdbusMock, |
John Wedig | b810c92 | 2021-11-17 16:38:03 -0800 | [diff] [blame] | 396 | sd_bus_emit_properties_changed_strv( |
Ed Tanous | 82897c3 | 2022-02-21 14:11:59 -0800 | [diff] [blame] | 397 | IsNull(), StrEq(testPath), StrEq(estoragedInterface), _)) |
John Wedig | b810c92 | 2021-11-17 16:38:03 -0800 | [diff] [blame] | 398 | .WillRepeatedly(Return(0)); |
| 399 | |
| 400 | EXPECT_CALL(*mockCryptIface, cryptFormat(_, _, _, _, _, _, _, _)).Times(1); |
| 401 | |
| 402 | EXPECT_CALL(*mockCryptIface, cryptKeyslotAddByVolumeKey(_, _, _, _, _, _)) |
| 403 | .Times(1); |
| 404 | |
| 405 | EXPECT_CALL(*mockCryptIface, cryptLoad(_, _, _)).Times(1); |
| 406 | |
| 407 | EXPECT_CALL(*mockCryptIface, cryptActivateByPassphrase(_, _, _, _, _, _)) |
| 408 | .Times(1); |
| 409 | |
| 410 | EXPECT_CALL(*mockFsIface, runMkfs(testLuksDevName)).WillOnce(Return(0)); |
| 411 | |
John Wedig | b17f825 | 2022-01-12 14:24:26 -0800 | [diff] [blame] | 412 | EXPECT_CALL(*mockFsIface, directoryExists(path(esObject->getMountPoint()))) |
| 413 | .WillOnce(Return(false)); |
| 414 | |
John Wedig | b810c92 | 2021-11-17 16:38:03 -0800 | [diff] [blame] | 415 | EXPECT_CALL(*mockFsIface, createDirectory(path(esObject->getMountPoint()))) |
| 416 | .WillOnce(Return(true)); |
| 417 | |
| 418 | EXPECT_CALL(*mockFsIface, |
| 419 | doMount(ContainsRegex("/dev/mapper/"), |
| 420 | StrEq(esObject->getMountPoint()), _, _, _)) |
| 421 | .WillOnce(Return(-1)); |
| 422 | |
| 423 | EXPECT_CALL(*mockFsIface, removeDirectory(path(esObject->getMountPoint()))) |
| 424 | .WillOnce(Return(true)); |
| 425 | |
John Wedig | 972c3fa | 2021-12-29 17:30:41 -0800 | [diff] [blame] | 426 | EXPECT_THROW(esObject->formatLuks(password, Volume::FilesystemType::ext4), |
| 427 | InternalFailure); |
John Wedig | b810c92 | 2021-11-17 16:38:03 -0800 | [diff] [blame] | 428 | EXPECT_FALSE(esObject->isLocked()); |
| 429 | } |
| 430 | |
| 431 | /* Test case where we fail to unmount the filesystem. */ |
Ed Tanous | 82897c3 | 2022-02-21 14:11:59 -0800 | [diff] [blame] | 432 | TEST_F(EStoragedTest, UnmountFail) |
John Wedig | b810c92 | 2021-11-17 16:38:03 -0800 | [diff] [blame] | 433 | { |
| 434 | EXPECT_CALL(sdbusMock, |
John Edward Broadbent | 86dfb24 | 2022-03-14 11:04:36 -0700 | [diff] [blame] | 435 | sd_bus_emit_properties_changed_strv(IsNull(), StrEq(testPath), |
| 436 | StrEq(driveInterface), _)) |
| 437 | .WillRepeatedly(Return(0)); |
| 438 | |
| 439 | EXPECT_CALL(sdbusMock, |
John Wedig | b810c92 | 2021-11-17 16:38:03 -0800 | [diff] [blame] | 440 | sd_bus_emit_properties_changed_strv( |
Ed Tanous | 82897c3 | 2022-02-21 14:11:59 -0800 | [diff] [blame] | 441 | IsNull(), StrEq(testPath), StrEq(estoragedInterface), _)) |
John Wedig | b810c92 | 2021-11-17 16:38:03 -0800 | [diff] [blame] | 442 | .WillRepeatedly(Return(0)); |
| 443 | |
| 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(_, _, _, _, _, _)) |
| 452 | .Times(1); |
| 453 | |
| 454 | EXPECT_CALL(*mockFsIface, runMkfs(testLuksDevName)).WillOnce(Return(0)); |
| 455 | |
John Wedig | b17f825 | 2022-01-12 14:24:26 -0800 | [diff] [blame] | 456 | EXPECT_CALL(*mockFsIface, directoryExists(path(esObject->getMountPoint()))) |
| 457 | .WillOnce(Return(false)); |
| 458 | |
John Wedig | b810c92 | 2021-11-17 16:38:03 -0800 | [diff] [blame] | 459 | EXPECT_CALL(*mockFsIface, createDirectory(path(esObject->getMountPoint()))) |
| 460 | .WillOnce(Return(true)); |
| 461 | |
| 462 | EXPECT_CALL(*mockFsIface, |
| 463 | doMount(ContainsRegex("/dev/mapper/"), |
| 464 | StrEq(esObject->getMountPoint()), _, _, _)) |
| 465 | .WillOnce(Return(0)); |
| 466 | |
| 467 | EXPECT_CALL(*mockFsIface, doUnmount(StrEq(esObject->getMountPoint()))) |
| 468 | .WillOnce(Return(-1)); |
| 469 | |
John Wedig | 972c3fa | 2021-12-29 17:30:41 -0800 | [diff] [blame] | 470 | esObject->formatLuks(password, Volume::FilesystemType::ext4); |
John Wedig | b810c92 | 2021-11-17 16:38:03 -0800 | [diff] [blame] | 471 | EXPECT_FALSE(esObject->isLocked()); |
| 472 | |
John Wedig | 972c3fa | 2021-12-29 17:30:41 -0800 | [diff] [blame] | 473 | EXPECT_THROW(esObject->lock(), InternalFailure); |
John Wedig | b810c92 | 2021-11-17 16:38:03 -0800 | [diff] [blame] | 474 | EXPECT_FALSE(esObject->isLocked()); |
| 475 | } |
| 476 | |
| 477 | /* Test case where we fail to remove the mount point. */ |
Ed Tanous | 82897c3 | 2022-02-21 14:11:59 -0800 | [diff] [blame] | 478 | TEST_F(EStoragedTest, RemoveMountPointFail) |
John Wedig | b810c92 | 2021-11-17 16:38:03 -0800 | [diff] [blame] | 479 | { |
| 480 | EXPECT_CALL(sdbusMock, |
John Edward Broadbent | 86dfb24 | 2022-03-14 11:04:36 -0700 | [diff] [blame] | 481 | sd_bus_emit_properties_changed_strv(IsNull(), StrEq(testPath), |
| 482 | StrEq(driveInterface), _)) |
| 483 | .WillRepeatedly(Return(0)); |
| 484 | |
| 485 | EXPECT_CALL(sdbusMock, |
John Wedig | b810c92 | 2021-11-17 16:38:03 -0800 | [diff] [blame] | 486 | sd_bus_emit_properties_changed_strv( |
Ed Tanous | 82897c3 | 2022-02-21 14:11:59 -0800 | [diff] [blame] | 487 | IsNull(), StrEq(testPath), StrEq(estoragedInterface), _)) |
John Wedig | b810c92 | 2021-11-17 16:38:03 -0800 | [diff] [blame] | 488 | .WillRepeatedly(Return(0)); |
| 489 | |
| 490 | EXPECT_CALL(*mockCryptIface, cryptFormat(_, _, _, _, _, _, _, _)).Times(1); |
| 491 | |
| 492 | EXPECT_CALL(*mockCryptIface, cryptKeyslotAddByVolumeKey(_, _, _, _, _, _)) |
| 493 | .Times(1); |
| 494 | |
| 495 | EXPECT_CALL(*mockCryptIface, cryptLoad(_, _, _)).Times(1); |
| 496 | |
| 497 | EXPECT_CALL(*mockCryptIface, cryptActivateByPassphrase(_, _, _, _, _, _)) |
| 498 | .Times(1); |
| 499 | |
| 500 | EXPECT_CALL(*mockFsIface, runMkfs(testLuksDevName)).WillOnce(Return(0)); |
| 501 | |
John Wedig | b17f825 | 2022-01-12 14:24:26 -0800 | [diff] [blame] | 502 | EXPECT_CALL(*mockFsIface, directoryExists(path(esObject->getMountPoint()))) |
| 503 | .WillOnce(Return(false)); |
| 504 | |
John Wedig | b810c92 | 2021-11-17 16:38:03 -0800 | [diff] [blame] | 505 | EXPECT_CALL(*mockFsIface, createDirectory(path(esObject->getMountPoint()))) |
| 506 | .WillOnce(Return(true)); |
| 507 | |
| 508 | EXPECT_CALL(*mockFsIface, |
| 509 | doMount(ContainsRegex("/dev/mapper/"), |
| 510 | StrEq(esObject->getMountPoint()), _, _, _)) |
| 511 | .WillOnce(Return(0)); |
| 512 | |
| 513 | EXPECT_CALL(*mockFsIface, doUnmount(StrEq(esObject->getMountPoint()))) |
| 514 | .WillOnce(Return(0)); |
| 515 | |
| 516 | EXPECT_CALL(*mockFsIface, removeDirectory(path(esObject->getMountPoint()))) |
| 517 | .WillOnce(Return(false)); |
| 518 | |
John Wedig | 972c3fa | 2021-12-29 17:30:41 -0800 | [diff] [blame] | 519 | esObject->formatLuks(password, Volume::FilesystemType::ext4); |
John Wedig | b810c92 | 2021-11-17 16:38:03 -0800 | [diff] [blame] | 520 | EXPECT_FALSE(esObject->isLocked()); |
| 521 | |
| 522 | /* This will fail to remove the mount point. */ |
John Wedig | 972c3fa | 2021-12-29 17:30:41 -0800 | [diff] [blame] | 523 | EXPECT_THROW(esObject->lock(), InternalFailure); |
John Wedig | b810c92 | 2021-11-17 16:38:03 -0800 | [diff] [blame] | 524 | EXPECT_FALSE(esObject->isLocked()); |
| 525 | } |
| 526 | |
| 527 | /* Test case where we fail to deactivate the LUKS device. */ |
Ed Tanous | 82897c3 | 2022-02-21 14:11:59 -0800 | [diff] [blame] | 528 | TEST_F(EStoragedTest, DeactivateFail) |
John Wedig | b810c92 | 2021-11-17 16:38:03 -0800 | [diff] [blame] | 529 | { |
| 530 | EXPECT_CALL(sdbusMock, |
John Edward Broadbent | 86dfb24 | 2022-03-14 11:04:36 -0700 | [diff] [blame] | 531 | sd_bus_emit_properties_changed_strv(IsNull(), StrEq(testPath), |
| 532 | StrEq(driveInterface), _)) |
| 533 | .WillRepeatedly(Return(0)); |
| 534 | |
| 535 | EXPECT_CALL(sdbusMock, |
John Wedig | b810c92 | 2021-11-17 16:38:03 -0800 | [diff] [blame] | 536 | sd_bus_emit_properties_changed_strv( |
Ed Tanous | 82897c3 | 2022-02-21 14:11:59 -0800 | [diff] [blame] | 537 | IsNull(), StrEq(testPath), StrEq(estoragedInterface), _)) |
John Wedig | b810c92 | 2021-11-17 16:38:03 -0800 | [diff] [blame] | 538 | .WillRepeatedly(Return(0)); |
| 539 | |
| 540 | EXPECT_CALL(*mockCryptIface, cryptFormat(_, _, _, _, _, _, _, _)).Times(1); |
| 541 | |
| 542 | EXPECT_CALL(*mockCryptIface, cryptKeyslotAddByVolumeKey(_, _, _, _, _, _)) |
| 543 | .Times(1); |
| 544 | |
| 545 | EXPECT_CALL(*mockCryptIface, cryptLoad(_, _, _)).Times(1); |
| 546 | |
| 547 | EXPECT_CALL(*mockCryptIface, cryptActivateByPassphrase(_, _, _, _, _, _)) |
| 548 | .Times(1); |
| 549 | |
| 550 | EXPECT_CALL(*mockFsIface, runMkfs(testLuksDevName)).WillOnce(Return(0)); |
| 551 | |
John Wedig | b17f825 | 2022-01-12 14:24:26 -0800 | [diff] [blame] | 552 | EXPECT_CALL(*mockFsIface, directoryExists(path(esObject->getMountPoint()))) |
| 553 | .WillOnce(Return(false)); |
| 554 | |
John Wedig | b810c92 | 2021-11-17 16:38:03 -0800 | [diff] [blame] | 555 | EXPECT_CALL(*mockFsIface, createDirectory(path(esObject->getMountPoint()))) |
| 556 | .WillOnce(Return(true)); |
| 557 | |
| 558 | EXPECT_CALL(*mockFsIface, |
| 559 | doMount(ContainsRegex("/dev/mapper/"), |
| 560 | StrEq(esObject->getMountPoint()), _, _, _)) |
| 561 | .WillOnce(Return(0)); |
| 562 | |
| 563 | EXPECT_CALL(*mockFsIface, doUnmount(StrEq(esObject->getMountPoint()))) |
| 564 | .WillOnce(Return(0)); |
| 565 | |
| 566 | EXPECT_CALL(*mockFsIface, removeDirectory(path(esObject->getMountPoint()))) |
| 567 | .WillOnce(Return(true)); |
| 568 | |
| 569 | EXPECT_CALL(*mockCryptIface, cryptDeactivate(_, _)).WillOnce(Return(-1)); |
| 570 | |
| 571 | /* Format the encrypted device. */ |
John Wedig | 972c3fa | 2021-12-29 17:30:41 -0800 | [diff] [blame] | 572 | esObject->formatLuks(password, Volume::FilesystemType::ext4); |
John Wedig | b810c92 | 2021-11-17 16:38:03 -0800 | [diff] [blame] | 573 | EXPECT_FALSE(esObject->isLocked()); |
| 574 | |
John Wedig | 972c3fa | 2021-12-29 17:30:41 -0800 | [diff] [blame] | 575 | EXPECT_THROW(esObject->lock(), InternalFailure); |
John Wedig | b810c92 | 2021-11-17 16:38:03 -0800 | [diff] [blame] | 576 | EXPECT_FALSE(esObject->isLocked()); |
| 577 | } |
| 578 | |
| 579 | } // namespace estoraged_test |