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