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