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" |
Tom Tung | 043af59 | 2023-11-24 13:37:05 +0800 | [diff] [blame^] | 4 | #include "estoraged_conf.hpp" |
John Wedig | b810c92 | 2021-11-17 16:38:03 -0800 | [diff] [blame] | 5 | |
| 6 | #include <unistd.h> |
| 7 | |
John Wedig | 67a4744 | 2022-04-05 17:21:29 -0700 | [diff] [blame] | 8 | #include <boost/asio/io_context.hpp> |
| 9 | #include <sdbusplus/asio/connection.hpp> |
| 10 | #include <sdbusplus/asio/object_server.hpp> |
John Wedig | 972c3fa | 2021-12-29 17:30:41 -0800 | [diff] [blame] | 11 | #include <xyz/openbmc_project/Common/error.hpp> |
Patrick Williams | 0e2a46f | 2023-05-10 14:35:52 -0500 | [diff] [blame] | 12 | #include <xyz/openbmc_project/Inventory/Item/Volume/server.hpp> |
John Wedig | b810c92 | 2021-11-17 16:38:03 -0800 | [diff] [blame] | 13 | |
| 14 | #include <exception> |
| 15 | #include <filesystem> |
| 16 | #include <fstream> |
| 17 | #include <iterator> |
John Wedig | 67a4744 | 2022-04-05 17:21:29 -0700 | [diff] [blame] | 18 | #include <memory> |
John Wedig | b810c92 | 2021-11-17 16:38:03 -0800 | [diff] [blame] | 19 | #include <string> |
| 20 | #include <vector> |
| 21 | |
| 22 | #include <gmock/gmock.h> |
| 23 | #include <gtest/gtest.h> |
| 24 | |
| 25 | namespace estoraged_test |
| 26 | { |
| 27 | |
Patrick Williams | 0e2a46f | 2023-05-10 14:35:52 -0500 | [diff] [blame] | 28 | using sdbusplus::server::xyz::openbmc_project::inventory::item::Volume; |
John Wedig | 972c3fa | 2021-12-29 17:30:41 -0800 | [diff] [blame] | 29 | using sdbusplus::xyz::openbmc_project::Common::Error::InternalFailure; |
| 30 | using sdbusplus::xyz::openbmc_project::Common::Error::ResourceNotFound; |
John Wedig | b810c92 | 2021-11-17 16:38:03 -0800 | [diff] [blame] | 31 | using std::filesystem::path; |
| 32 | using ::testing::_; |
John Wedig | b810c92 | 2021-11-17 16:38:03 -0800 | [diff] [blame] | 33 | using ::testing::Return; |
| 34 | using ::testing::StrEq; |
| 35 | |
Ed Tanous | 82897c3 | 2022-02-21 14:11:59 -0800 | [diff] [blame] | 36 | class EStoragedTest : public testing::Test |
John Wedig | b810c92 | 2021-11-17 16:38:03 -0800 | [diff] [blame] | 37 | { |
| 38 | public: |
Ed Tanous | 82897c3 | 2022-02-21 14:11:59 -0800 | [diff] [blame] | 39 | const char* testFileName = "testfile"; |
| 40 | const char* testLuksDevName = "testfile_luksDev"; |
John Wedig | 2443a02 | 2023-03-17 13:42:32 -0700 | [diff] [blame] | 41 | const char* testCryptDir = "/tmp"; |
John Wedig | 6c0d8ce | 2022-04-22 14:00:43 -0700 | [diff] [blame] | 42 | const std::string testConfigPath = |
| 43 | "/xyz/openbmc_project/inventory/system/board/test_board/test_emmc"; |
John Wedig | 67a4744 | 2022-04-05 17:21:29 -0700 | [diff] [blame] | 44 | const uint64_t testSize = 24; |
John Edward Broadbent | 5d799bb | 2022-03-22 16:14:24 -0700 | [diff] [blame] | 45 | const uint8_t testLifeTime = 25; |
John Wedig | b483830 | 2022-07-22 13:51:16 -0700 | [diff] [blame] | 46 | const std::string testPartNumber = "12345678"; |
| 47 | const std::string testSerialNumber = "ABCDEF1234"; |
Rahul Kapoor | 1982505 | 2023-05-27 01:52:23 +0000 | [diff] [blame] | 48 | const std::string testLocationCode = "U102020"; |
John Wedig | b810c92 | 2021-11-17 16:38:03 -0800 | [diff] [blame] | 49 | std::ofstream testFile; |
John Wedig | b810c92 | 2021-11-17 16:38:03 -0800 | [diff] [blame] | 50 | std::string passwordString; |
| 51 | std::vector<uint8_t> password; |
Ed Tanous | 82897c3 | 2022-02-21 14:11:59 -0800 | [diff] [blame] | 52 | MockCryptsetupInterface* mockCryptIface{}; |
| 53 | MockFilesystemInterface* mockFsIface{}; |
John Wedig | 67a4744 | 2022-04-05 17:21:29 -0700 | [diff] [blame] | 54 | boost::asio::io_context io; |
| 55 | std::shared_ptr<sdbusplus::asio::connection> conn; |
| 56 | std::unique_ptr<sdbusplus::asio::object_server> objectServer; |
| 57 | std::unique_ptr<estoraged::EStoraged> esObject; |
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 | 67a4744 | 2022-04-05 17:21:29 -0700 | [diff] [blame] | 60 | passwordString("password"), |
John Wedig | b810c92 | 2021-11-17 16:38:03 -0800 | [diff] [blame] | 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 | |
John Wedig | b810c92 | 2021-11-17 16:38:03 -0800 | [diff] [blame] | 75 | std::unique_ptr<MockCryptsetupInterface> cryptIface = |
| 76 | std::make_unique<MockCryptsetupInterface>(); |
| 77 | mockCryptIface = cryptIface.get(); |
| 78 | std::unique_ptr<MockFilesystemInterface> fsIface = |
| 79 | std::make_unique<MockFilesystemInterface>(); |
| 80 | mockFsIface = fsIface.get(); |
| 81 | |
John Wedig | 2443a02 | 2023-03-17 13:42:32 -0700 | [diff] [blame] | 82 | /* Set up location of dummy mapped crypt file. */ |
| 83 | EXPECT_CALL(*cryptIface, cryptGetDir).WillOnce(Return(testCryptDir)); |
| 84 | |
John Wedig | 67a4744 | 2022-04-05 17:21:29 -0700 | [diff] [blame] | 85 | conn = std::make_shared<sdbusplus::asio::connection>(io); |
| 86 | // request D-Bus server name. |
| 87 | conn->request_name("xyz.openbmc_project.eStoraged.test"); |
| 88 | objectServer = std::make_unique<sdbusplus::asio::object_server>(conn); |
| 89 | |
Ed Tanous | 82897c3 | 2022-02-21 14:11:59 -0800 | [diff] [blame] | 90 | esObject = std::make_unique<estoraged::EStoraged>( |
John Wedig | 6c0d8ce | 2022-04-22 14:00:43 -0700 | [diff] [blame] | 91 | *objectServer, testConfigPath, testFileName, testLuksDevName, |
John Wedig | b483830 | 2022-07-22 13:51:16 -0700 | [diff] [blame] | 92 | testSize, testLifeTime, testPartNumber, testSerialNumber, |
Tom Tung | 043af59 | 2023-11-24 13:37:05 +0800 | [diff] [blame^] | 93 | testLocationCode, ERASE_MAX_GEOMETRY, ERASE_MIN_GEOMETRY, |
| 94 | std::move(cryptIface), std::move(fsIface)); |
John Wedig | b810c92 | 2021-11-17 16:38:03 -0800 | [diff] [blame] | 95 | } |
| 96 | |
| 97 | void TearDown() override |
| 98 | { |
| 99 | EXPECT_EQ(0, unlink(testFileName)); |
| 100 | } |
| 101 | }; |
| 102 | |
John Wedig | 2443a02 | 2023-03-17 13:42:32 -0700 | [diff] [blame] | 103 | const char* mappedDevicePath = "/tmp/testfile_luksDev"; |
| 104 | std::ofstream mappedDevice; |
| 105 | |
| 106 | int createMappedDev() |
| 107 | { |
| 108 | mappedDevice.open(mappedDevicePath, |
| 109 | std::ios::out | std::ios::binary | std::ios::trunc); |
| 110 | mappedDevice.close(); |
| 111 | if (mappedDevice.fail()) |
| 112 | { |
| 113 | throw std::runtime_error("Failed to open test mapped device"); |
| 114 | } |
| 115 | |
| 116 | return 0; |
| 117 | } |
| 118 | |
| 119 | int removeMappedDev() |
| 120 | { |
| 121 | EXPECT_EQ(0, unlink(mappedDevicePath)); |
| 122 | |
| 123 | return 0; |
| 124 | } |
| 125 | |
John Wedig | b810c92 | 2021-11-17 16:38:03 -0800 | [diff] [blame] | 126 | /* Test case to format and then lock the LUKS device. */ |
Ed Tanous | 82897c3 | 2022-02-21 14:11:59 -0800 | [diff] [blame] | 127 | TEST_F(EStoragedTest, FormatPass) |
John Wedig | b810c92 | 2021-11-17 16:38:03 -0800 | [diff] [blame] | 128 | { |
John Wedig | b810c92 | 2021-11-17 16:38:03 -0800 | [diff] [blame] | 129 | EXPECT_CALL(*mockCryptIface, cryptFormat(_, _, _, _, _, _, _, _)).Times(1); |
| 130 | |
| 131 | EXPECT_CALL(*mockCryptIface, cryptKeyslotAddByVolumeKey(_, _, _, _, _, _)) |
| 132 | .Times(1); |
| 133 | |
| 134 | EXPECT_CALL(*mockCryptIface, cryptLoad(_, _, _)).Times(1); |
| 135 | |
| 136 | EXPECT_CALL(*mockCryptIface, cryptActivateByPassphrase(_, _, _, _, _, _)) |
John Wedig | 2443a02 | 2023-03-17 13:42:32 -0700 | [diff] [blame] | 137 | .WillOnce(&createMappedDev); |
John Wedig | b810c92 | 2021-11-17 16:38:03 -0800 | [diff] [blame] | 138 | |
John Wedig | 2443a02 | 2023-03-17 13:42:32 -0700 | [diff] [blame] | 139 | EXPECT_CALL(*mockFsIface, runMkfs(StrEq(esObject->getCryptDevicePath()))) |
| 140 | .WillOnce(Return(0)); |
John Wedig | b810c92 | 2021-11-17 16:38:03 -0800 | [diff] [blame] | 141 | |
John Wedig | b17f825 | 2022-01-12 14:24:26 -0800 | [diff] [blame] | 142 | EXPECT_CALL(*mockFsIface, directoryExists(path(esObject->getMountPoint()))) |
| 143 | .WillOnce(Return(false)); |
| 144 | |
John Wedig | b810c92 | 2021-11-17 16:38:03 -0800 | [diff] [blame] | 145 | EXPECT_CALL(*mockFsIface, createDirectory(path(esObject->getMountPoint()))) |
| 146 | .WillOnce(Return(true)); |
| 147 | |
| 148 | EXPECT_CALL(*mockFsIface, |
John Wedig | 2443a02 | 2023-03-17 13:42:32 -0700 | [diff] [blame] | 149 | doMount(StrEq(esObject->getCryptDevicePath()), |
John Wedig | b810c92 | 2021-11-17 16:38:03 -0800 | [diff] [blame] | 150 | StrEq(esObject->getMountPoint()), _, _, _)) |
| 151 | .WillOnce(Return(0)); |
| 152 | |
| 153 | EXPECT_CALL(*mockFsIface, doUnmount(StrEq(esObject->getMountPoint()))) |
| 154 | .WillOnce(Return(0)); |
| 155 | |
| 156 | EXPECT_CALL(*mockFsIface, removeDirectory(path(esObject->getMountPoint()))) |
| 157 | .WillOnce(Return(true)); |
| 158 | |
John Wedig | 2443a02 | 2023-03-17 13:42:32 -0700 | [diff] [blame] | 159 | EXPECT_CALL(*mockCryptIface, cryptDeactivate(_, _)) |
| 160 | .WillOnce(&removeMappedDev); |
John Wedig | b810c92 | 2021-11-17 16:38:03 -0800 | [diff] [blame] | 161 | |
| 162 | /* Format the encrypted device. */ |
John Wedig | 972c3fa | 2021-12-29 17:30:41 -0800 | [diff] [blame] | 163 | esObject->formatLuks(password, Volume::FilesystemType::ext4); |
John Wedig | b810c92 | 2021-11-17 16:38:03 -0800 | [diff] [blame] | 164 | EXPECT_FALSE(esObject->isLocked()); |
| 165 | |
John Wedig | 972c3fa | 2021-12-29 17:30:41 -0800 | [diff] [blame] | 166 | esObject->lock(); |
John Wedig | b810c92 | 2021-11-17 16:38:03 -0800 | [diff] [blame] | 167 | EXPECT_TRUE(esObject->isLocked()); |
| 168 | } |
| 169 | |
John Wedig | b17f825 | 2022-01-12 14:24:26 -0800 | [diff] [blame] | 170 | /* |
| 171 | * Test case where the mount point directory already exists, so it shouldn't |
| 172 | * try to create it. |
| 173 | */ |
Ed Tanous | 82897c3 | 2022-02-21 14:11:59 -0800 | [diff] [blame] | 174 | TEST_F(EStoragedTest, MountPointExistsPass) |
John Wedig | b17f825 | 2022-01-12 14:24:26 -0800 | [diff] [blame] | 175 | { |
John Wedig | b17f825 | 2022-01-12 14:24:26 -0800 | [diff] [blame] | 176 | EXPECT_CALL(*mockCryptIface, cryptFormat(_, _, _, _, _, _, _, _)).Times(1); |
| 177 | |
| 178 | EXPECT_CALL(*mockCryptIface, cryptKeyslotAddByVolumeKey(_, _, _, _, _, _)) |
| 179 | .Times(1); |
| 180 | |
| 181 | EXPECT_CALL(*mockCryptIface, cryptLoad(_, _, _)).Times(1); |
| 182 | |
| 183 | EXPECT_CALL(*mockCryptIface, cryptActivateByPassphrase(_, _, _, _, _, _)) |
John Wedig | 2443a02 | 2023-03-17 13:42:32 -0700 | [diff] [blame] | 184 | .WillOnce(&createMappedDev); |
John Wedig | b17f825 | 2022-01-12 14:24:26 -0800 | [diff] [blame] | 185 | |
John Wedig | 2443a02 | 2023-03-17 13:42:32 -0700 | [diff] [blame] | 186 | EXPECT_CALL(*mockFsIface, runMkfs(StrEq(esObject->getCryptDevicePath()))) |
| 187 | .WillOnce(Return(0)); |
John Wedig | b17f825 | 2022-01-12 14:24:26 -0800 | [diff] [blame] | 188 | |
| 189 | EXPECT_CALL(*mockFsIface, directoryExists(path(esObject->getMountPoint()))) |
| 190 | .WillOnce(Return(true)); |
| 191 | |
| 192 | EXPECT_CALL(*mockFsIface, createDirectory(path(esObject->getMountPoint()))) |
| 193 | .Times(0); |
| 194 | |
| 195 | EXPECT_CALL(*mockFsIface, |
John Wedig | 2443a02 | 2023-03-17 13:42:32 -0700 | [diff] [blame] | 196 | doMount(StrEq(esObject->getCryptDevicePath()), |
John Wedig | b17f825 | 2022-01-12 14:24:26 -0800 | [diff] [blame] | 197 | StrEq(esObject->getMountPoint()), _, _, _)) |
| 198 | .WillOnce(Return(0)); |
| 199 | |
| 200 | EXPECT_CALL(*mockFsIface, doUnmount(StrEq(esObject->getMountPoint()))) |
| 201 | .WillOnce(Return(0)); |
| 202 | |
| 203 | EXPECT_CALL(*mockFsIface, removeDirectory(path(esObject->getMountPoint()))) |
| 204 | .WillOnce(Return(true)); |
| 205 | |
John Wedig | 2443a02 | 2023-03-17 13:42:32 -0700 | [diff] [blame] | 206 | EXPECT_CALL(*mockCryptIface, cryptDeactivate(_, _)) |
| 207 | .WillOnce(&removeMappedDev); |
John Wedig | b17f825 | 2022-01-12 14:24:26 -0800 | [diff] [blame] | 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 | 2443a02 | 2023-03-17 13:42:32 -0700 | [diff] [blame] | 225 | EXPECT_TRUE(esObject->isLocked()); |
John Wedig | b810c92 | 2021-11-17 16:38:03 -0800 | [diff] [blame] | 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 | 2443a02 | 2023-03-17 13:42:32 -0700 | [diff] [blame] | 241 | EXPECT_TRUE(esObject->isLocked()); |
John Wedig | b810c92 | 2021-11-17 16:38:03 -0800 | [diff] [blame] | 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 | { |
John Wedig | b810c92 | 2021-11-17 16:38:03 -0800 | [diff] [blame] | 247 | EXPECT_CALL(*mockCryptIface, cryptFormat(_, _, _, _, _, _, _, _)).Times(1); |
| 248 | |
| 249 | EXPECT_CALL(*mockCryptIface, cryptKeyslotAddByVolumeKey(_, _, _, _, _, _)) |
| 250 | .WillOnce(Return(-1)); |
| 251 | |
John Wedig | 972c3fa | 2021-12-29 17:30:41 -0800 | [diff] [blame] | 252 | EXPECT_THROW(esObject->formatLuks(password, Volume::FilesystemType::ext4), |
| 253 | InternalFailure); |
John Wedig | b810c92 | 2021-11-17 16:38:03 -0800 | [diff] [blame] | 254 | EXPECT_TRUE(esObject->isLocked()); |
| 255 | } |
| 256 | |
| 257 | /* Test case where we fail to load the LUKS header. */ |
Ed Tanous | 82897c3 | 2022-02-21 14:11:59 -0800 | [diff] [blame] | 258 | TEST_F(EStoragedTest, LoadLuksHeaderFail) |
John Wedig | b810c92 | 2021-11-17 16:38:03 -0800 | [diff] [blame] | 259 | { |
John Wedig | b810c92 | 2021-11-17 16:38:03 -0800 | [diff] [blame] | 260 | EXPECT_CALL(*mockCryptIface, cryptFormat(_, _, _, _, _, _, _, _)).Times(1); |
| 261 | |
| 262 | EXPECT_CALL(*mockCryptIface, cryptKeyslotAddByVolumeKey(_, _, _, _, _, _)) |
| 263 | .Times(1); |
| 264 | |
| 265 | EXPECT_CALL(*mockCryptIface, cryptLoad(_, _, _)).WillOnce(Return(-1)); |
| 266 | |
John Wedig | 972c3fa | 2021-12-29 17:30:41 -0800 | [diff] [blame] | 267 | EXPECT_THROW(esObject->formatLuks(password, Volume::FilesystemType::ext4), |
| 268 | InternalFailure); |
John Wedig | b810c92 | 2021-11-17 16:38:03 -0800 | [diff] [blame] | 269 | EXPECT_TRUE(esObject->isLocked()); |
| 270 | } |
| 271 | |
| 272 | /* Test case where we fail to activate the LUKS device. */ |
Ed Tanous | 82897c3 | 2022-02-21 14:11:59 -0800 | [diff] [blame] | 273 | TEST_F(EStoragedTest, ActivateFail) |
John Wedig | b810c92 | 2021-11-17 16:38:03 -0800 | [diff] [blame] | 274 | { |
John Wedig | b810c92 | 2021-11-17 16:38:03 -0800 | [diff] [blame] | 275 | EXPECT_CALL(*mockCryptIface, cryptFormat(_, _, _, _, _, _, _, _)).Times(1); |
| 276 | |
| 277 | EXPECT_CALL(*mockCryptIface, cryptKeyslotAddByVolumeKey(_, _, _, _, _, _)) |
| 278 | .Times(1); |
| 279 | |
| 280 | EXPECT_CALL(*mockCryptIface, cryptLoad(_, _, _)).Times(1); |
| 281 | |
| 282 | EXPECT_CALL(*mockCryptIface, cryptActivateByPassphrase(_, _, _, _, _, _)) |
| 283 | .WillOnce(Return(-1)); |
| 284 | |
John Wedig | 972c3fa | 2021-12-29 17:30:41 -0800 | [diff] [blame] | 285 | EXPECT_THROW(esObject->formatLuks(password, Volume::FilesystemType::ext4), |
| 286 | InternalFailure); |
John Wedig | b810c92 | 2021-11-17 16:38:03 -0800 | [diff] [blame] | 287 | EXPECT_TRUE(esObject->isLocked()); |
| 288 | } |
| 289 | |
| 290 | /* Test case where we fail to create the filesystem. */ |
Ed Tanous | 82897c3 | 2022-02-21 14:11:59 -0800 | [diff] [blame] | 291 | TEST_F(EStoragedTest, CreateFilesystemFail) |
John Wedig | b810c92 | 2021-11-17 16:38:03 -0800 | [diff] [blame] | 292 | { |
John Wedig | b810c92 | 2021-11-17 16:38:03 -0800 | [diff] [blame] | 293 | EXPECT_CALL(*mockCryptIface, cryptFormat(_, _, _, _, _, _, _, _)).Times(1); |
| 294 | |
| 295 | EXPECT_CALL(*mockCryptIface, cryptKeyslotAddByVolumeKey(_, _, _, _, _, _)) |
| 296 | .Times(1); |
| 297 | |
| 298 | EXPECT_CALL(*mockCryptIface, cryptLoad(_, _, _)).Times(1); |
| 299 | |
| 300 | EXPECT_CALL(*mockCryptIface, cryptActivateByPassphrase(_, _, _, _, _, _)) |
John Wedig | 2443a02 | 2023-03-17 13:42:32 -0700 | [diff] [blame] | 301 | .WillOnce(&createMappedDev); |
John Wedig | b810c92 | 2021-11-17 16:38:03 -0800 | [diff] [blame] | 302 | |
John Wedig | 2443a02 | 2023-03-17 13:42:32 -0700 | [diff] [blame] | 303 | EXPECT_CALL(*mockFsIface, runMkfs(StrEq(esObject->getCryptDevicePath()))) |
| 304 | .WillOnce(Return(-1)); |
John Wedig | b810c92 | 2021-11-17 16:38:03 -0800 | [diff] [blame] | 305 | |
John Wedig | 972c3fa | 2021-12-29 17:30:41 -0800 | [diff] [blame] | 306 | EXPECT_THROW(esObject->formatLuks(password, Volume::FilesystemType::ext4), |
| 307 | InternalFailure); |
John Wedig | b810c92 | 2021-11-17 16:38:03 -0800 | [diff] [blame] | 308 | EXPECT_FALSE(esObject->isLocked()); |
John Wedig | 2443a02 | 2023-03-17 13:42:32 -0700 | [diff] [blame] | 309 | |
| 310 | EXPECT_EQ(0, removeMappedDev()); |
John Wedig | b810c92 | 2021-11-17 16:38:03 -0800 | [diff] [blame] | 311 | } |
| 312 | |
| 313 | /* Test case where we fail to create the mount point. */ |
Ed Tanous | 82897c3 | 2022-02-21 14:11:59 -0800 | [diff] [blame] | 314 | TEST_F(EStoragedTest, CreateMountPointFail) |
John Wedig | b810c92 | 2021-11-17 16:38:03 -0800 | [diff] [blame] | 315 | { |
John Wedig | b810c92 | 2021-11-17 16:38:03 -0800 | [diff] [blame] | 316 | EXPECT_CALL(*mockCryptIface, cryptFormat(_, _, _, _, _, _, _, _)).Times(1); |
| 317 | |
| 318 | EXPECT_CALL(*mockCryptIface, cryptKeyslotAddByVolumeKey(_, _, _, _, _, _)) |
| 319 | .Times(1); |
| 320 | |
| 321 | EXPECT_CALL(*mockCryptIface, cryptLoad(_, _, _)).Times(1); |
| 322 | |
| 323 | EXPECT_CALL(*mockCryptIface, cryptActivateByPassphrase(_, _, _, _, _, _)) |
John Wedig | 2443a02 | 2023-03-17 13:42:32 -0700 | [diff] [blame] | 324 | .WillOnce(&createMappedDev); |
John Wedig | b810c92 | 2021-11-17 16:38:03 -0800 | [diff] [blame] | 325 | |
John Wedig | 2443a02 | 2023-03-17 13:42:32 -0700 | [diff] [blame] | 326 | EXPECT_CALL(*mockFsIface, runMkfs(StrEq(esObject->getCryptDevicePath()))) |
| 327 | .WillOnce(Return(0)); |
John Wedig | b810c92 | 2021-11-17 16:38:03 -0800 | [diff] [blame] | 328 | |
John Wedig | b17f825 | 2022-01-12 14:24:26 -0800 | [diff] [blame] | 329 | EXPECT_CALL(*mockFsIface, directoryExists(path(esObject->getMountPoint()))) |
| 330 | .WillOnce(Return(false)); |
| 331 | |
John Wedig | b810c92 | 2021-11-17 16:38:03 -0800 | [diff] [blame] | 332 | EXPECT_CALL(*mockFsIface, createDirectory(path(esObject->getMountPoint()))) |
| 333 | .WillOnce(Return(false)); |
| 334 | |
John Wedig | 972c3fa | 2021-12-29 17:30:41 -0800 | [diff] [blame] | 335 | EXPECT_THROW(esObject->formatLuks(password, Volume::FilesystemType::ext4), |
| 336 | InternalFailure); |
John Wedig | b810c92 | 2021-11-17 16:38:03 -0800 | [diff] [blame] | 337 | EXPECT_FALSE(esObject->isLocked()); |
John Wedig | 2443a02 | 2023-03-17 13:42:32 -0700 | [diff] [blame] | 338 | |
| 339 | EXPECT_EQ(0, removeMappedDev()); |
John Wedig | b810c92 | 2021-11-17 16:38:03 -0800 | [diff] [blame] | 340 | } |
| 341 | |
| 342 | /* Test case where we fail to mount the filesystem. */ |
Ed Tanous | 82897c3 | 2022-02-21 14:11:59 -0800 | [diff] [blame] | 343 | TEST_F(EStoragedTest, MountFail) |
John Wedig | b810c92 | 2021-11-17 16:38:03 -0800 | [diff] [blame] | 344 | { |
John Wedig | b810c92 | 2021-11-17 16:38:03 -0800 | [diff] [blame] | 345 | EXPECT_CALL(*mockCryptIface, cryptFormat(_, _, _, _, _, _, _, _)).Times(1); |
| 346 | |
| 347 | EXPECT_CALL(*mockCryptIface, cryptKeyslotAddByVolumeKey(_, _, _, _, _, _)) |
| 348 | .Times(1); |
| 349 | |
| 350 | EXPECT_CALL(*mockCryptIface, cryptLoad(_, _, _)).Times(1); |
| 351 | |
| 352 | EXPECT_CALL(*mockCryptIface, cryptActivateByPassphrase(_, _, _, _, _, _)) |
John Wedig | 2443a02 | 2023-03-17 13:42:32 -0700 | [diff] [blame] | 353 | .WillOnce(&createMappedDev); |
John Wedig | b810c92 | 2021-11-17 16:38:03 -0800 | [diff] [blame] | 354 | |
John Wedig | 2443a02 | 2023-03-17 13:42:32 -0700 | [diff] [blame] | 355 | EXPECT_CALL(*mockFsIface, runMkfs(StrEq(esObject->getCryptDevicePath()))) |
| 356 | .WillOnce(Return(0)); |
John Wedig | b810c92 | 2021-11-17 16:38:03 -0800 | [diff] [blame] | 357 | |
John Wedig | b17f825 | 2022-01-12 14:24:26 -0800 | [diff] [blame] | 358 | EXPECT_CALL(*mockFsIface, directoryExists(path(esObject->getMountPoint()))) |
| 359 | .WillOnce(Return(false)); |
| 360 | |
John Wedig | b810c92 | 2021-11-17 16:38:03 -0800 | [diff] [blame] | 361 | EXPECT_CALL(*mockFsIface, createDirectory(path(esObject->getMountPoint()))) |
| 362 | .WillOnce(Return(true)); |
| 363 | |
| 364 | EXPECT_CALL(*mockFsIface, |
John Wedig | 2443a02 | 2023-03-17 13:42:32 -0700 | [diff] [blame] | 365 | doMount(StrEq(esObject->getCryptDevicePath()), |
John Wedig | b810c92 | 2021-11-17 16:38:03 -0800 | [diff] [blame] | 366 | StrEq(esObject->getMountPoint()), _, _, _)) |
| 367 | .WillOnce(Return(-1)); |
| 368 | |
| 369 | EXPECT_CALL(*mockFsIface, removeDirectory(path(esObject->getMountPoint()))) |
| 370 | .WillOnce(Return(true)); |
| 371 | |
John Wedig | 972c3fa | 2021-12-29 17:30:41 -0800 | [diff] [blame] | 372 | EXPECT_THROW(esObject->formatLuks(password, Volume::FilesystemType::ext4), |
| 373 | InternalFailure); |
John Wedig | b810c92 | 2021-11-17 16:38:03 -0800 | [diff] [blame] | 374 | EXPECT_FALSE(esObject->isLocked()); |
John Wedig | 2443a02 | 2023-03-17 13:42:32 -0700 | [diff] [blame] | 375 | |
| 376 | EXPECT_EQ(0, removeMappedDev()); |
John Wedig | b810c92 | 2021-11-17 16:38:03 -0800 | [diff] [blame] | 377 | } |
| 378 | |
| 379 | /* Test case where we fail to unmount the filesystem. */ |
Ed Tanous | 82897c3 | 2022-02-21 14:11:59 -0800 | [diff] [blame] | 380 | TEST_F(EStoragedTest, UnmountFail) |
John Wedig | b810c92 | 2021-11-17 16:38:03 -0800 | [diff] [blame] | 381 | { |
John Wedig | b810c92 | 2021-11-17 16:38:03 -0800 | [diff] [blame] | 382 | EXPECT_CALL(*mockCryptIface, cryptFormat(_, _, _, _, _, _, _, _)).Times(1); |
| 383 | |
| 384 | EXPECT_CALL(*mockCryptIface, cryptKeyslotAddByVolumeKey(_, _, _, _, _, _)) |
| 385 | .Times(1); |
| 386 | |
| 387 | EXPECT_CALL(*mockCryptIface, cryptLoad(_, _, _)).Times(1); |
| 388 | |
| 389 | EXPECT_CALL(*mockCryptIface, cryptActivateByPassphrase(_, _, _, _, _, _)) |
John Wedig | 2443a02 | 2023-03-17 13:42:32 -0700 | [diff] [blame] | 390 | .WillOnce(&createMappedDev); |
John Wedig | b810c92 | 2021-11-17 16:38:03 -0800 | [diff] [blame] | 391 | |
John Wedig | 2443a02 | 2023-03-17 13:42:32 -0700 | [diff] [blame] | 392 | EXPECT_CALL(*mockFsIface, runMkfs(StrEq(esObject->getCryptDevicePath()))) |
| 393 | .WillOnce(Return(0)); |
John Wedig | b810c92 | 2021-11-17 16:38:03 -0800 | [diff] [blame] | 394 | |
John Wedig | b17f825 | 2022-01-12 14:24:26 -0800 | [diff] [blame] | 395 | EXPECT_CALL(*mockFsIface, directoryExists(path(esObject->getMountPoint()))) |
| 396 | .WillOnce(Return(false)); |
| 397 | |
John Wedig | b810c92 | 2021-11-17 16:38:03 -0800 | [diff] [blame] | 398 | EXPECT_CALL(*mockFsIface, createDirectory(path(esObject->getMountPoint()))) |
| 399 | .WillOnce(Return(true)); |
| 400 | |
| 401 | EXPECT_CALL(*mockFsIface, |
John Wedig | 2443a02 | 2023-03-17 13:42:32 -0700 | [diff] [blame] | 402 | doMount(StrEq(esObject->getCryptDevicePath()), |
John Wedig | b810c92 | 2021-11-17 16:38:03 -0800 | [diff] [blame] | 403 | StrEq(esObject->getMountPoint()), _, _, _)) |
| 404 | .WillOnce(Return(0)); |
| 405 | |
| 406 | EXPECT_CALL(*mockFsIface, doUnmount(StrEq(esObject->getMountPoint()))) |
| 407 | .WillOnce(Return(-1)); |
| 408 | |
John Wedig | 972c3fa | 2021-12-29 17:30:41 -0800 | [diff] [blame] | 409 | esObject->formatLuks(password, Volume::FilesystemType::ext4); |
John Wedig | b810c92 | 2021-11-17 16:38:03 -0800 | [diff] [blame] | 410 | EXPECT_FALSE(esObject->isLocked()); |
| 411 | |
John Wedig | 972c3fa | 2021-12-29 17:30:41 -0800 | [diff] [blame] | 412 | EXPECT_THROW(esObject->lock(), InternalFailure); |
John Wedig | b810c92 | 2021-11-17 16:38:03 -0800 | [diff] [blame] | 413 | EXPECT_FALSE(esObject->isLocked()); |
John Wedig | 2443a02 | 2023-03-17 13:42:32 -0700 | [diff] [blame] | 414 | |
| 415 | EXPECT_EQ(0, removeMappedDev()); |
John Wedig | b810c92 | 2021-11-17 16:38:03 -0800 | [diff] [blame] | 416 | } |
| 417 | |
| 418 | /* Test case where we fail to remove the mount point. */ |
Ed Tanous | 82897c3 | 2022-02-21 14:11:59 -0800 | [diff] [blame] | 419 | TEST_F(EStoragedTest, RemoveMountPointFail) |
John Wedig | b810c92 | 2021-11-17 16:38:03 -0800 | [diff] [blame] | 420 | { |
John Wedig | b810c92 | 2021-11-17 16:38:03 -0800 | [diff] [blame] | 421 | EXPECT_CALL(*mockCryptIface, cryptFormat(_, _, _, _, _, _, _, _)).Times(1); |
| 422 | |
| 423 | EXPECT_CALL(*mockCryptIface, cryptKeyslotAddByVolumeKey(_, _, _, _, _, _)) |
| 424 | .Times(1); |
| 425 | |
| 426 | EXPECT_CALL(*mockCryptIface, cryptLoad(_, _, _)).Times(1); |
| 427 | |
| 428 | EXPECT_CALL(*mockCryptIface, cryptActivateByPassphrase(_, _, _, _, _, _)) |
John Wedig | 2443a02 | 2023-03-17 13:42:32 -0700 | [diff] [blame] | 429 | .WillOnce(&createMappedDev); |
John Wedig | b810c92 | 2021-11-17 16:38:03 -0800 | [diff] [blame] | 430 | |
John Wedig | 2443a02 | 2023-03-17 13:42:32 -0700 | [diff] [blame] | 431 | EXPECT_CALL(*mockFsIface, runMkfs(StrEq(esObject->getCryptDevicePath()))) |
| 432 | .WillOnce(Return(0)); |
John Wedig | b810c92 | 2021-11-17 16:38:03 -0800 | [diff] [blame] | 433 | |
John Wedig | b17f825 | 2022-01-12 14:24:26 -0800 | [diff] [blame] | 434 | EXPECT_CALL(*mockFsIface, directoryExists(path(esObject->getMountPoint()))) |
| 435 | .WillOnce(Return(false)); |
| 436 | |
John Wedig | b810c92 | 2021-11-17 16:38:03 -0800 | [diff] [blame] | 437 | EXPECT_CALL(*mockFsIface, createDirectory(path(esObject->getMountPoint()))) |
| 438 | .WillOnce(Return(true)); |
| 439 | |
| 440 | EXPECT_CALL(*mockFsIface, |
John Wedig | 2443a02 | 2023-03-17 13:42:32 -0700 | [diff] [blame] | 441 | doMount(StrEq(esObject->getCryptDevicePath()), |
John Wedig | b810c92 | 2021-11-17 16:38:03 -0800 | [diff] [blame] | 442 | StrEq(esObject->getMountPoint()), _, _, _)) |
| 443 | .WillOnce(Return(0)); |
| 444 | |
| 445 | EXPECT_CALL(*mockFsIface, doUnmount(StrEq(esObject->getMountPoint()))) |
| 446 | .WillOnce(Return(0)); |
| 447 | |
| 448 | EXPECT_CALL(*mockFsIface, removeDirectory(path(esObject->getMountPoint()))) |
| 449 | .WillOnce(Return(false)); |
| 450 | |
John Wedig | 972c3fa | 2021-12-29 17:30:41 -0800 | [diff] [blame] | 451 | esObject->formatLuks(password, Volume::FilesystemType::ext4); |
John Wedig | b810c92 | 2021-11-17 16:38:03 -0800 | [diff] [blame] | 452 | EXPECT_FALSE(esObject->isLocked()); |
| 453 | |
| 454 | /* This will fail to remove the mount point. */ |
John Wedig | 972c3fa | 2021-12-29 17:30:41 -0800 | [diff] [blame] | 455 | EXPECT_THROW(esObject->lock(), InternalFailure); |
John Wedig | b810c92 | 2021-11-17 16:38:03 -0800 | [diff] [blame] | 456 | EXPECT_FALSE(esObject->isLocked()); |
John Wedig | 2443a02 | 2023-03-17 13:42:32 -0700 | [diff] [blame] | 457 | |
| 458 | EXPECT_EQ(0, removeMappedDev()); |
John Wedig | b810c92 | 2021-11-17 16:38:03 -0800 | [diff] [blame] | 459 | } |
| 460 | |
| 461 | /* Test case where we fail to deactivate the LUKS device. */ |
Ed Tanous | 82897c3 | 2022-02-21 14:11:59 -0800 | [diff] [blame] | 462 | TEST_F(EStoragedTest, DeactivateFail) |
John Wedig | b810c92 | 2021-11-17 16:38:03 -0800 | [diff] [blame] | 463 | { |
John Wedig | b810c92 | 2021-11-17 16:38:03 -0800 | [diff] [blame] | 464 | EXPECT_CALL(*mockCryptIface, cryptFormat(_, _, _, _, _, _, _, _)).Times(1); |
| 465 | |
| 466 | EXPECT_CALL(*mockCryptIface, cryptKeyslotAddByVolumeKey(_, _, _, _, _, _)) |
| 467 | .Times(1); |
| 468 | |
| 469 | EXPECT_CALL(*mockCryptIface, cryptLoad(_, _, _)).Times(1); |
| 470 | |
| 471 | EXPECT_CALL(*mockCryptIface, cryptActivateByPassphrase(_, _, _, _, _, _)) |
John Wedig | 2443a02 | 2023-03-17 13:42:32 -0700 | [diff] [blame] | 472 | .WillOnce(&createMappedDev); |
John Wedig | b810c92 | 2021-11-17 16:38:03 -0800 | [diff] [blame] | 473 | |
John Wedig | 2443a02 | 2023-03-17 13:42:32 -0700 | [diff] [blame] | 474 | EXPECT_CALL(*mockFsIface, runMkfs(StrEq(esObject->getCryptDevicePath()))) |
| 475 | .WillOnce(Return(0)); |
John Wedig | b810c92 | 2021-11-17 16:38:03 -0800 | [diff] [blame] | 476 | |
John Wedig | b17f825 | 2022-01-12 14:24:26 -0800 | [diff] [blame] | 477 | EXPECT_CALL(*mockFsIface, directoryExists(path(esObject->getMountPoint()))) |
| 478 | .WillOnce(Return(false)); |
| 479 | |
John Wedig | b810c92 | 2021-11-17 16:38:03 -0800 | [diff] [blame] | 480 | EXPECT_CALL(*mockFsIface, createDirectory(path(esObject->getMountPoint()))) |
| 481 | .WillOnce(Return(true)); |
| 482 | |
| 483 | EXPECT_CALL(*mockFsIface, |
John Wedig | 2443a02 | 2023-03-17 13:42:32 -0700 | [diff] [blame] | 484 | doMount(StrEq(esObject->getCryptDevicePath()), |
John Wedig | b810c92 | 2021-11-17 16:38:03 -0800 | [diff] [blame] | 485 | StrEq(esObject->getMountPoint()), _, _, _)) |
| 486 | .WillOnce(Return(0)); |
| 487 | |
| 488 | EXPECT_CALL(*mockFsIface, doUnmount(StrEq(esObject->getMountPoint()))) |
| 489 | .WillOnce(Return(0)); |
| 490 | |
| 491 | EXPECT_CALL(*mockFsIface, removeDirectory(path(esObject->getMountPoint()))) |
| 492 | .WillOnce(Return(true)); |
| 493 | |
| 494 | EXPECT_CALL(*mockCryptIface, cryptDeactivate(_, _)).WillOnce(Return(-1)); |
| 495 | |
| 496 | /* Format the encrypted device. */ |
John Wedig | 972c3fa | 2021-12-29 17:30:41 -0800 | [diff] [blame] | 497 | esObject->formatLuks(password, Volume::FilesystemType::ext4); |
John Wedig | b810c92 | 2021-11-17 16:38:03 -0800 | [diff] [blame] | 498 | EXPECT_FALSE(esObject->isLocked()); |
| 499 | |
John Wedig | 972c3fa | 2021-12-29 17:30:41 -0800 | [diff] [blame] | 500 | EXPECT_THROW(esObject->lock(), InternalFailure); |
John Wedig | b810c92 | 2021-11-17 16:38:03 -0800 | [diff] [blame] | 501 | EXPECT_FALSE(esObject->isLocked()); |
John Wedig | 2443a02 | 2023-03-17 13:42:32 -0700 | [diff] [blame] | 502 | |
| 503 | EXPECT_EQ(0, removeMappedDev()); |
John Wedig | b810c92 | 2021-11-17 16:38:03 -0800 | [diff] [blame] | 504 | } |
| 505 | |
John Wedig | 8d5a3a0 | 2022-09-29 15:25:58 -0700 | [diff] [blame] | 506 | /* Test case where we successfully change the password. */ |
| 507 | TEST_F(EStoragedTest, ChangePasswordSuccess) |
| 508 | { |
| 509 | std::string newPasswordString("newPassword"); |
| 510 | std::vector<uint8_t> newPassword(newPasswordString.begin(), |
| 511 | newPasswordString.end()); |
| 512 | |
| 513 | EXPECT_CALL(*mockCryptIface, cryptLoad(_, _, _)).Times(1); |
| 514 | |
| 515 | EXPECT_CALL(*mockCryptIface, |
| 516 | cryptKeyslotChangeByPassphrase( |
| 517 | _, _, _, reinterpret_cast<const char*>(password.data()), |
| 518 | password.size(), |
| 519 | reinterpret_cast<const char*>(newPassword.data()), |
| 520 | newPassword.size())) |
| 521 | .WillOnce(Return(0)); |
| 522 | |
| 523 | /* Change the password for the LUKS-encrypted device. */ |
| 524 | esObject->changePassword(password, newPassword); |
| 525 | } |
| 526 | |
| 527 | /* Test case where we fail to change the password. */ |
| 528 | TEST_F(EStoragedTest, ChangePasswordFail) |
| 529 | { |
| 530 | std::string newPasswordString("newPassword"); |
| 531 | std::vector<uint8_t> newPassword(newPasswordString.begin(), |
| 532 | newPasswordString.end()); |
| 533 | |
| 534 | EXPECT_CALL(*mockCryptIface, cryptLoad(_, _, _)).Times(1); |
| 535 | |
| 536 | EXPECT_CALL(*mockCryptIface, |
| 537 | cryptKeyslotChangeByPassphrase( |
| 538 | _, _, _, reinterpret_cast<const char*>(password.data()), |
| 539 | password.size(), |
| 540 | reinterpret_cast<const char*>(newPassword.data()), |
| 541 | newPassword.size())) |
| 542 | .WillOnce(Return(-1)); |
| 543 | |
| 544 | EXPECT_THROW(esObject->changePassword(password, newPassword), |
| 545 | InternalFailure); |
| 546 | } |
| 547 | |
John Wedig | b810c92 | 2021-11-17 16:38:03 -0800 | [diff] [blame] | 548 | } // namespace estoraged_test |