| 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 | |
| Willy Tu | da5aa61 | 2025-12-18 07:32:34 +0000 | [diff] [blame] | 6 | #include <linux/mmc/core.h> |
| 7 | #include <linux/mmc/ioctl.h> |
| 8 | #include <linux/mmc/mmc.h> |
| John Wedig | b810c92 | 2021-11-17 16:38:03 -0800 | [diff] [blame] | 9 | #include <unistd.h> |
| 10 | |
| John Wedig | 67a4744 | 2022-04-05 17:21:29 -0700 | [diff] [blame] | 11 | #include <boost/asio/io_context.hpp> |
| 12 | #include <sdbusplus/asio/connection.hpp> |
| 13 | #include <sdbusplus/asio/object_server.hpp> |
| Willy Tu | 6c39e6a | 2026-01-06 19:05:37 +0000 | [diff] [blame] | 14 | #include <stdplus/fd/gmock.hpp> |
| John Wedig | 972c3fa | 2021-12-29 17:30:41 -0800 | [diff] [blame] | 15 | #include <xyz/openbmc_project/Common/error.hpp> |
| Patrick Williams | 0e2a46f | 2023-05-10 14:35:52 -0500 | [diff] [blame] | 16 | #include <xyz/openbmc_project/Inventory/Item/Volume/server.hpp> |
| John Wedig | b810c92 | 2021-11-17 16:38:03 -0800 | [diff] [blame] | 17 | |
| Rom Lemarchand | 530ce1e | 2025-12-16 21:45:08 +0000 | [diff] [blame] | 18 | #include <array> |
| John Wedig | b810c92 | 2021-11-17 16:38:03 -0800 | [diff] [blame] | 19 | #include <exception> |
| 20 | #include <filesystem> |
| 21 | #include <fstream> |
| 22 | #include <iterator> |
| John Wedig | 67a4744 | 2022-04-05 17:21:29 -0700 | [diff] [blame] | 23 | #include <memory> |
| Willy Tu | da5aa61 | 2025-12-18 07:32:34 +0000 | [diff] [blame] | 24 | #include <span> |
| John Wedig | b810c92 | 2021-11-17 16:38:03 -0800 | [diff] [blame] | 25 | #include <string> |
| 26 | #include <vector> |
| 27 | |
| 28 | #include <gmock/gmock.h> |
| 29 | #include <gtest/gtest.h> |
| 30 | |
| 31 | namespace estoraged_test |
| 32 | { |
| 33 | |
| Patrick Williams | 0e2a46f | 2023-05-10 14:35:52 -0500 | [diff] [blame] | 34 | using sdbusplus::server::xyz::openbmc_project::inventory::item::Volume; |
| John Wedig | 972c3fa | 2021-12-29 17:30:41 -0800 | [diff] [blame] | 35 | using sdbusplus::xyz::openbmc_project::Common::Error::InternalFailure; |
| 36 | using sdbusplus::xyz::openbmc_project::Common::Error::ResourceNotFound; |
| John Wedig | b810c92 | 2021-11-17 16:38:03 -0800 | [diff] [blame] | 37 | using std::filesystem::path; |
| Willy Tu | 6c39e6a | 2026-01-06 19:05:37 +0000 | [diff] [blame] | 38 | using stdplus::fd::FdMock; |
| John Wedig | b810c92 | 2021-11-17 16:38:03 -0800 | [diff] [blame] | 39 | using ::testing::_; |
| Rom Lemarchand | 530ce1e | 2025-12-16 21:45:08 +0000 | [diff] [blame] | 40 | using ::testing::ElementsAreArray; |
| John Wedig | b810c92 | 2021-11-17 16:38:03 -0800 | [diff] [blame] | 41 | using ::testing::Return; |
| 42 | using ::testing::StrEq; |
| 43 | |
| Ed Tanous | 82897c3 | 2022-02-21 14:11:59 -0800 | [diff] [blame] | 44 | class EStoragedTest : public testing::Test |
| John Wedig | b810c92 | 2021-11-17 16:38:03 -0800 | [diff] [blame] | 45 | { |
| 46 | public: |
| Ed Tanous | 82897c3 | 2022-02-21 14:11:59 -0800 | [diff] [blame] | 47 | const char* testFileName = "testfile"; |
| 48 | const char* testLuksDevName = "testfile_luksDev"; |
| John Wedig | 2443a02 | 2023-03-17 13:42:32 -0700 | [diff] [blame] | 49 | const char* testCryptDir = "/tmp"; |
| John Wedig | 6c0d8ce | 2022-04-22 14:00:43 -0700 | [diff] [blame] | 50 | const std::string testConfigPath = |
| 51 | "/xyz/openbmc_project/inventory/system/board/test_board/test_emmc"; |
| John Wedig | 67a4744 | 2022-04-05 17:21:29 -0700 | [diff] [blame] | 52 | const uint64_t testSize = 24; |
| John Edward Broadbent | 5d799bb | 2022-03-22 16:14:24 -0700 | [diff] [blame] | 53 | const uint8_t testLifeTime = 25; |
| John Wedig | b483830 | 2022-07-22 13:51:16 -0700 | [diff] [blame] | 54 | const std::string testPartNumber = "12345678"; |
| 55 | const std::string testSerialNumber = "ABCDEF1234"; |
| Rahul Kapoor | 1982505 | 2023-05-27 01:52:23 +0000 | [diff] [blame] | 56 | const std::string testLocationCode = "U102020"; |
| John Wedig | d7be42b | 2024-01-19 16:07:19 -0800 | [diff] [blame] | 57 | const std::string testDriveType = "SSD"; |
| John Wedig | c0d66eb | 2024-02-26 15:54:47 -0800 | [diff] [blame] | 58 | const std::string testDriveProtocol = "eMMC"; |
| John Wedig | b810c92 | 2021-11-17 16:38:03 -0800 | [diff] [blame] | 59 | std::ofstream testFile; |
| John Wedig | b810c92 | 2021-11-17 16:38:03 -0800 | [diff] [blame] | 60 | std::string passwordString; |
| 61 | std::vector<uint8_t> password; |
| Ed Tanous | 82897c3 | 2022-02-21 14:11:59 -0800 | [diff] [blame] | 62 | MockCryptsetupInterface* mockCryptIface{}; |
| Rom Lemarchand | 530ce1e | 2025-12-16 21:45:08 +0000 | [diff] [blame] | 63 | static const constexpr std::array<const char*, 2> options = { |
| 64 | "-E", "discard"}; |
| Ed Tanous | 82897c3 | 2022-02-21 14:11:59 -0800 | [diff] [blame] | 65 | MockFilesystemInterface* mockFsIface{}; |
| John Wedig | 67a4744 | 2022-04-05 17:21:29 -0700 | [diff] [blame] | 66 | boost::asio::io_context io; |
| 67 | std::shared_ptr<sdbusplus::asio::connection> conn; |
| 68 | std::unique_ptr<sdbusplus::asio::object_server> objectServer; |
| 69 | std::unique_ptr<estoraged::EStoraged> esObject; |
| John Wedig | b810c92 | 2021-11-17 16:38:03 -0800 | [diff] [blame] | 70 | |
| Ed Tanous | 82897c3 | 2022-02-21 14:11:59 -0800 | [diff] [blame] | 71 | EStoragedTest() : |
| John Wedig | 67a4744 | 2022-04-05 17:21:29 -0700 | [diff] [blame] | 72 | passwordString("password"), |
| John Wedig | b810c92 | 2021-11-17 16:38:03 -0800 | [diff] [blame] | 73 | password(passwordString.begin(), passwordString.end()) |
| 74 | {} |
| 75 | |
| 76 | void SetUp() override |
| 77 | { |
| 78 | /* Create an empty file that we'll pretend is a 'storage device'. */ |
| 79 | testFile.open(testFileName, |
| 80 | std::ios::out | std::ios::binary | std::ios::trunc); |
| 81 | testFile.close(); |
| 82 | if (testFile.fail()) |
| 83 | { |
| 84 | throw std::runtime_error("Failed to open test file"); |
| 85 | } |
| 86 | |
| John Wedig | b810c92 | 2021-11-17 16:38:03 -0800 | [diff] [blame] | 87 | std::unique_ptr<MockCryptsetupInterface> cryptIface = |
| 88 | std::make_unique<MockCryptsetupInterface>(); |
| 89 | mockCryptIface = cryptIface.get(); |
| 90 | std::unique_ptr<MockFilesystemInterface> fsIface = |
| 91 | std::make_unique<MockFilesystemInterface>(); |
| 92 | mockFsIface = fsIface.get(); |
| 93 | |
| John Wedig | 2443a02 | 2023-03-17 13:42:32 -0700 | [diff] [blame] | 94 | /* Set up location of dummy mapped crypt file. */ |
| 95 | EXPECT_CALL(*cryptIface, cryptGetDir).WillOnce(Return(testCryptDir)); |
| 96 | |
| John Wedig | 67a4744 | 2022-04-05 17:21:29 -0700 | [diff] [blame] | 97 | conn = std::make_shared<sdbusplus::asio::connection>(io); |
| 98 | // request D-Bus server name. |
| 99 | conn->request_name("xyz.openbmc_project.eStoraged.test"); |
| 100 | objectServer = std::make_unique<sdbusplus::asio::object_server>(conn); |
| 101 | |
| Willy Tu | 6c39e6a | 2026-01-06 19:05:37 +0000 | [diff] [blame] | 102 | std::unique_ptr<FdMock> mockFd = std::make_unique<FdMock>(); |
| Ed Tanous | 82897c3 | 2022-02-21 14:11:59 -0800 | [diff] [blame] | 103 | esObject = std::make_unique<estoraged::EStoraged>( |
| Willy Tu | da5aa61 | 2025-12-18 07:32:34 +0000 | [diff] [blame] | 104 | std::move(mockFd), *objectServer, testConfigPath, testFileName, |
| 105 | testLuksDevName, testSize, testLifeTime, testPartNumber, |
| 106 | testSerialNumber, testLocationCode, ERASE_MAX_GEOMETRY, |
| 107 | ERASE_MIN_GEOMETRY, testDriveType, testDriveProtocol, |
| 108 | std::move(cryptIface), std::move(fsIface)); |
| John Wedig | b810c92 | 2021-11-17 16:38:03 -0800 | [diff] [blame] | 109 | } |
| 110 | |
| 111 | void TearDown() override |
| 112 | { |
| 113 | EXPECT_EQ(0, unlink(testFileName)); |
| 114 | } |
| 115 | }; |
| 116 | |
| John Wedig | 2443a02 | 2023-03-17 13:42:32 -0700 | [diff] [blame] | 117 | const char* mappedDevicePath = "/tmp/testfile_luksDev"; |
| 118 | std::ofstream mappedDevice; |
| 119 | |
| 120 | int createMappedDev() |
| 121 | { |
| 122 | mappedDevice.open(mappedDevicePath, |
| 123 | std::ios::out | std::ios::binary | std::ios::trunc); |
| 124 | mappedDevice.close(); |
| 125 | if (mappedDevice.fail()) |
| 126 | { |
| 127 | throw std::runtime_error("Failed to open test mapped device"); |
| 128 | } |
| 129 | |
| 130 | return 0; |
| 131 | } |
| 132 | |
| 133 | int removeMappedDev() |
| 134 | { |
| 135 | EXPECT_EQ(0, unlink(mappedDevicePath)); |
| 136 | |
| 137 | return 0; |
| 138 | } |
| 139 | |
| John Wedig | b810c92 | 2021-11-17 16:38:03 -0800 | [diff] [blame] | 140 | /* Test case to format and then lock the LUKS device. */ |
| Ed Tanous | 82897c3 | 2022-02-21 14:11:59 -0800 | [diff] [blame] | 141 | TEST_F(EStoragedTest, FormatPass) |
| John Wedig | b810c92 | 2021-11-17 16:38:03 -0800 | [diff] [blame] | 142 | { |
| John Wedig | b810c92 | 2021-11-17 16:38:03 -0800 | [diff] [blame] | 143 | EXPECT_CALL(*mockCryptIface, cryptFormat(_, _, _, _, _, _, _, _)).Times(1); |
| 144 | |
| 145 | EXPECT_CALL(*mockCryptIface, cryptKeyslotAddByVolumeKey(_, _, _, _, _, _)) |
| 146 | .Times(1); |
| 147 | |
| 148 | EXPECT_CALL(*mockCryptIface, cryptLoad(_, _, _)).Times(1); |
| 149 | |
| 150 | EXPECT_CALL(*mockCryptIface, cryptActivateByPassphrase(_, _, _, _, _, _)) |
| John Wedig | 2443a02 | 2023-03-17 13:42:32 -0700 | [diff] [blame] | 151 | .WillOnce(&createMappedDev); |
| John Wedig | b810c92 | 2021-11-17 16:38:03 -0800 | [diff] [blame] | 152 | |
| Rom Lemarchand | 530ce1e | 2025-12-16 21:45:08 +0000 | [diff] [blame] | 153 | EXPECT_CALL(*mockFsIface, runMkfs(StrEq(esObject->getCryptDevicePath()), |
| 154 | ElementsAreArray(EStoragedTest::options))) |
| John Wedig | 2443a02 | 2023-03-17 13:42:32 -0700 | [diff] [blame] | 155 | .WillOnce(Return(0)); |
| John Wedig | b810c92 | 2021-11-17 16:38:03 -0800 | [diff] [blame] | 156 | |
| John Wedig | 1d6665f | 2025-12-04 19:08:06 +0000 | [diff] [blame] | 157 | EXPECT_CALL(*mockFsIface, runFsck(StrEq(esObject->getCryptDevicePath()), |
| 158 | StrEq("-t ext4 -p"))) |
| 159 | .WillOnce(Return(0)); |
| 160 | |
| John Wedig | b17f825 | 2022-01-12 14:24:26 -0800 | [diff] [blame] | 161 | EXPECT_CALL(*mockFsIface, directoryExists(path(esObject->getMountPoint()))) |
| 162 | .WillOnce(Return(false)); |
| 163 | |
| John Wedig | b810c92 | 2021-11-17 16:38:03 -0800 | [diff] [blame] | 164 | EXPECT_CALL(*mockFsIface, createDirectory(path(esObject->getMountPoint()))) |
| 165 | .WillOnce(Return(true)); |
| 166 | |
| 167 | EXPECT_CALL(*mockFsIface, |
| John Wedig | 2443a02 | 2023-03-17 13:42:32 -0700 | [diff] [blame] | 168 | doMount(StrEq(esObject->getCryptDevicePath()), |
| John Wedig | b810c92 | 2021-11-17 16:38:03 -0800 | [diff] [blame] | 169 | StrEq(esObject->getMountPoint()), _, _, _)) |
| 170 | .WillOnce(Return(0)); |
| 171 | |
| 172 | EXPECT_CALL(*mockFsIface, doUnmount(StrEq(esObject->getMountPoint()))) |
| 173 | .WillOnce(Return(0)); |
| 174 | |
| 175 | EXPECT_CALL(*mockFsIface, removeDirectory(path(esObject->getMountPoint()))) |
| 176 | .WillOnce(Return(true)); |
| 177 | |
| John Wedig | 2443a02 | 2023-03-17 13:42:32 -0700 | [diff] [blame] | 178 | EXPECT_CALL(*mockCryptIface, cryptDeactivate(_, _)) |
| 179 | .WillOnce(&removeMappedDev); |
| John Wedig | b810c92 | 2021-11-17 16:38:03 -0800 | [diff] [blame] | 180 | |
| 181 | /* Format the encrypted device. */ |
| John Wedig | 972c3fa | 2021-12-29 17:30:41 -0800 | [diff] [blame] | 182 | esObject->formatLuks(password, Volume::FilesystemType::ext4); |
| John Wedig | b810c92 | 2021-11-17 16:38:03 -0800 | [diff] [blame] | 183 | EXPECT_FALSE(esObject->isLocked()); |
| 184 | |
| John Wedig | 972c3fa | 2021-12-29 17:30:41 -0800 | [diff] [blame] | 185 | esObject->lock(); |
| John Wedig | b810c92 | 2021-11-17 16:38:03 -0800 | [diff] [blame] | 186 | EXPECT_TRUE(esObject->isLocked()); |
| 187 | } |
| 188 | |
| John Wedig | b17f825 | 2022-01-12 14:24:26 -0800 | [diff] [blame] | 189 | /* |
| 190 | * Test case where the mount point directory already exists, so it shouldn't |
| 191 | * try to create it. |
| 192 | */ |
| Ed Tanous | 82897c3 | 2022-02-21 14:11:59 -0800 | [diff] [blame] | 193 | TEST_F(EStoragedTest, MountPointExistsPass) |
| John Wedig | b17f825 | 2022-01-12 14:24:26 -0800 | [diff] [blame] | 194 | { |
| John Wedig | b17f825 | 2022-01-12 14:24:26 -0800 | [diff] [blame] | 195 | EXPECT_CALL(*mockCryptIface, cryptFormat(_, _, _, _, _, _, _, _)).Times(1); |
| 196 | |
| 197 | EXPECT_CALL(*mockCryptIface, cryptKeyslotAddByVolumeKey(_, _, _, _, _, _)) |
| 198 | .Times(1); |
| 199 | |
| 200 | EXPECT_CALL(*mockCryptIface, cryptLoad(_, _, _)).Times(1); |
| 201 | |
| 202 | EXPECT_CALL(*mockCryptIface, cryptActivateByPassphrase(_, _, _, _, _, _)) |
| John Wedig | 2443a02 | 2023-03-17 13:42:32 -0700 | [diff] [blame] | 203 | .WillOnce(&createMappedDev); |
| John Wedig | b17f825 | 2022-01-12 14:24:26 -0800 | [diff] [blame] | 204 | |
| Rom Lemarchand | 530ce1e | 2025-12-16 21:45:08 +0000 | [diff] [blame] | 205 | EXPECT_CALL(*mockFsIface, runMkfs(StrEq(esObject->getCryptDevicePath()), |
| 206 | ElementsAreArray(EStoragedTest::options))) |
| John Wedig | 2443a02 | 2023-03-17 13:42:32 -0700 | [diff] [blame] | 207 | .WillOnce(Return(0)); |
| John Wedig | b17f825 | 2022-01-12 14:24:26 -0800 | [diff] [blame] | 208 | |
| John Wedig | 1d6665f | 2025-12-04 19:08:06 +0000 | [diff] [blame] | 209 | EXPECT_CALL(*mockFsIface, runFsck(StrEq(esObject->getCryptDevicePath()), |
| 210 | StrEq("-t ext4 -p"))) |
| 211 | .WillOnce(Return(0)); |
| 212 | |
| John Wedig | b17f825 | 2022-01-12 14:24:26 -0800 | [diff] [blame] | 213 | EXPECT_CALL(*mockFsIface, directoryExists(path(esObject->getMountPoint()))) |
| 214 | .WillOnce(Return(true)); |
| 215 | |
| 216 | EXPECT_CALL(*mockFsIface, createDirectory(path(esObject->getMountPoint()))) |
| 217 | .Times(0); |
| 218 | |
| 219 | EXPECT_CALL(*mockFsIface, |
| John Wedig | 2443a02 | 2023-03-17 13:42:32 -0700 | [diff] [blame] | 220 | doMount(StrEq(esObject->getCryptDevicePath()), |
| John Wedig | b17f825 | 2022-01-12 14:24:26 -0800 | [diff] [blame] | 221 | StrEq(esObject->getMountPoint()), _, _, _)) |
| 222 | .WillOnce(Return(0)); |
| 223 | |
| 224 | EXPECT_CALL(*mockFsIface, doUnmount(StrEq(esObject->getMountPoint()))) |
| 225 | .WillOnce(Return(0)); |
| 226 | |
| 227 | EXPECT_CALL(*mockFsIface, removeDirectory(path(esObject->getMountPoint()))) |
| 228 | .WillOnce(Return(true)); |
| 229 | |
| John Wedig | 2443a02 | 2023-03-17 13:42:32 -0700 | [diff] [blame] | 230 | EXPECT_CALL(*mockCryptIface, cryptDeactivate(_, _)) |
| 231 | .WillOnce(&removeMappedDev); |
| John Wedig | b17f825 | 2022-01-12 14:24:26 -0800 | [diff] [blame] | 232 | |
| 233 | /* Format the encrypted device. */ |
| 234 | esObject->formatLuks(password, Volume::FilesystemType::ext4); |
| 235 | EXPECT_FALSE(esObject->isLocked()); |
| 236 | |
| 237 | esObject->lock(); |
| 238 | EXPECT_TRUE(esObject->isLocked()); |
| 239 | } |
| 240 | |
| John Wedig | b810c92 | 2021-11-17 16:38:03 -0800 | [diff] [blame] | 241 | /* Test case where the device/file doesn't exist. */ |
| Ed Tanous | 82897c3 | 2022-02-21 14:11:59 -0800 | [diff] [blame] | 242 | TEST_F(EStoragedTest, FormatNoDeviceFail) |
| John Wedig | b810c92 | 2021-11-17 16:38:03 -0800 | [diff] [blame] | 243 | { |
| 244 | /* Delete the test file. */ |
| 245 | EXPECT_EQ(0, unlink(testFileName)); |
| 246 | |
| John Wedig | 972c3fa | 2021-12-29 17:30:41 -0800 | [diff] [blame] | 247 | EXPECT_THROW(esObject->formatLuks(password, Volume::FilesystemType::ext4), |
| 248 | ResourceNotFound); |
| John Wedig | 2443a02 | 2023-03-17 13:42:32 -0700 | [diff] [blame] | 249 | EXPECT_TRUE(esObject->isLocked()); |
| John Wedig | b810c92 | 2021-11-17 16:38:03 -0800 | [diff] [blame] | 250 | |
| 251 | /* Create the test file again, so that the TearDown function works. */ |
| 252 | testFile.open(testFileName, |
| 253 | std::ios::out | std::ios::binary | std::ios::trunc); |
| 254 | testFile.close(); |
| 255 | } |
| 256 | |
| 257 | /* Test case where we fail to format the LUKS device. */ |
| Ed Tanous | 82897c3 | 2022-02-21 14:11:59 -0800 | [diff] [blame] | 258 | TEST_F(EStoragedTest, FormatFail) |
| John Wedig | b810c92 | 2021-11-17 16:38:03 -0800 | [diff] [blame] | 259 | { |
| 260 | EXPECT_CALL(*mockCryptIface, cryptFormat(_, _, _, _, _, _, _, _)) |
| 261 | .WillOnce(Return(-1)); |
| 262 | |
| John Wedig | 972c3fa | 2021-12-29 17:30:41 -0800 | [diff] [blame] | 263 | EXPECT_THROW(esObject->formatLuks(password, Volume::FilesystemType::ext4), |
| 264 | InternalFailure); |
| John Wedig | 2443a02 | 2023-03-17 13:42:32 -0700 | [diff] [blame] | 265 | EXPECT_TRUE(esObject->isLocked()); |
| John Wedig | b810c92 | 2021-11-17 16:38:03 -0800 | [diff] [blame] | 266 | } |
| 267 | |
| 268 | /* 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] | 269 | TEST_F(EStoragedTest, AddKeyslotFail) |
| John Wedig | b810c92 | 2021-11-17 16:38:03 -0800 | [diff] [blame] | 270 | { |
| John Wedig | b810c92 | 2021-11-17 16:38:03 -0800 | [diff] [blame] | 271 | EXPECT_CALL(*mockCryptIface, cryptFormat(_, _, _, _, _, _, _, _)).Times(1); |
| 272 | |
| 273 | EXPECT_CALL(*mockCryptIface, cryptKeyslotAddByVolumeKey(_, _, _, _, _, _)) |
| 274 | .WillOnce(Return(-1)); |
| 275 | |
| John Wedig | 972c3fa | 2021-12-29 17:30:41 -0800 | [diff] [blame] | 276 | EXPECT_THROW(esObject->formatLuks(password, Volume::FilesystemType::ext4), |
| 277 | InternalFailure); |
| John Wedig | b810c92 | 2021-11-17 16:38:03 -0800 | [diff] [blame] | 278 | EXPECT_TRUE(esObject->isLocked()); |
| 279 | } |
| 280 | |
| 281 | /* Test case where we fail to load the LUKS header. */ |
| Ed Tanous | 82897c3 | 2022-02-21 14:11:59 -0800 | [diff] [blame] | 282 | TEST_F(EStoragedTest, LoadLuksHeaderFail) |
| John Wedig | b810c92 | 2021-11-17 16:38:03 -0800 | [diff] [blame] | 283 | { |
| John Wedig | b810c92 | 2021-11-17 16:38:03 -0800 | [diff] [blame] | 284 | EXPECT_CALL(*mockCryptIface, cryptFormat(_, _, _, _, _, _, _, _)).Times(1); |
| 285 | |
| 286 | EXPECT_CALL(*mockCryptIface, cryptKeyslotAddByVolumeKey(_, _, _, _, _, _)) |
| 287 | .Times(1); |
| 288 | |
| 289 | EXPECT_CALL(*mockCryptIface, cryptLoad(_, _, _)).WillOnce(Return(-1)); |
| 290 | |
| John Wedig | 972c3fa | 2021-12-29 17:30:41 -0800 | [diff] [blame] | 291 | EXPECT_THROW(esObject->formatLuks(password, Volume::FilesystemType::ext4), |
| 292 | InternalFailure); |
| John Wedig | b810c92 | 2021-11-17 16:38:03 -0800 | [diff] [blame] | 293 | EXPECT_TRUE(esObject->isLocked()); |
| 294 | } |
| 295 | |
| 296 | /* Test case where we fail to activate the LUKS device. */ |
| Ed Tanous | 82897c3 | 2022-02-21 14:11:59 -0800 | [diff] [blame] | 297 | TEST_F(EStoragedTest, ActivateFail) |
| John Wedig | b810c92 | 2021-11-17 16:38:03 -0800 | [diff] [blame] | 298 | { |
| John Wedig | b810c92 | 2021-11-17 16:38:03 -0800 | [diff] [blame] | 299 | EXPECT_CALL(*mockCryptIface, cryptFormat(_, _, _, _, _, _, _, _)).Times(1); |
| 300 | |
| 301 | EXPECT_CALL(*mockCryptIface, cryptKeyslotAddByVolumeKey(_, _, _, _, _, _)) |
| 302 | .Times(1); |
| 303 | |
| 304 | EXPECT_CALL(*mockCryptIface, cryptLoad(_, _, _)).Times(1); |
| 305 | |
| 306 | EXPECT_CALL(*mockCryptIface, cryptActivateByPassphrase(_, _, _, _, _, _)) |
| 307 | .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_TRUE(esObject->isLocked()); |
| 312 | } |
| 313 | |
| 314 | /* Test case where we fail to create the filesystem. */ |
| Ed Tanous | 82897c3 | 2022-02-21 14:11:59 -0800 | [diff] [blame] | 315 | TEST_F(EStoragedTest, CreateFilesystemFail) |
| John Wedig | b810c92 | 2021-11-17 16:38:03 -0800 | [diff] [blame] | 316 | { |
| John Wedig | b810c92 | 2021-11-17 16:38:03 -0800 | [diff] [blame] | 317 | EXPECT_CALL(*mockCryptIface, cryptFormat(_, _, _, _, _, _, _, _)).Times(1); |
| 318 | |
| 319 | EXPECT_CALL(*mockCryptIface, cryptKeyslotAddByVolumeKey(_, _, _, _, _, _)) |
| 320 | .Times(1); |
| 321 | |
| 322 | EXPECT_CALL(*mockCryptIface, cryptLoad(_, _, _)).Times(1); |
| 323 | |
| 324 | EXPECT_CALL(*mockCryptIface, cryptActivateByPassphrase(_, _, _, _, _, _)) |
| John Wedig | 2443a02 | 2023-03-17 13:42:32 -0700 | [diff] [blame] | 325 | .WillOnce(&createMappedDev); |
| John Wedig | b810c92 | 2021-11-17 16:38:03 -0800 | [diff] [blame] | 326 | |
| Rom Lemarchand | 530ce1e | 2025-12-16 21:45:08 +0000 | [diff] [blame] | 327 | EXPECT_CALL(*mockFsIface, runMkfs(StrEq(esObject->getCryptDevicePath()), |
| 328 | ElementsAreArray(EStoragedTest::options))) |
| John Wedig | 2443a02 | 2023-03-17 13:42:32 -0700 | [diff] [blame] | 329 | .WillOnce(Return(-1)); |
| John Wedig | b810c92 | 2021-11-17 16:38:03 -0800 | [diff] [blame] | 330 | |
| John Wedig | 972c3fa | 2021-12-29 17:30:41 -0800 | [diff] [blame] | 331 | EXPECT_THROW(esObject->formatLuks(password, Volume::FilesystemType::ext4), |
| 332 | InternalFailure); |
| John Wedig | b810c92 | 2021-11-17 16:38:03 -0800 | [diff] [blame] | 333 | EXPECT_FALSE(esObject->isLocked()); |
| John Wedig | 2443a02 | 2023-03-17 13:42:32 -0700 | [diff] [blame] | 334 | |
| 335 | EXPECT_EQ(0, removeMappedDev()); |
| John Wedig | b810c92 | 2021-11-17 16:38:03 -0800 | [diff] [blame] | 336 | } |
| 337 | |
| 338 | /* Test case where we fail to create the mount point. */ |
| Ed Tanous | 82897c3 | 2022-02-21 14:11:59 -0800 | [diff] [blame] | 339 | TEST_F(EStoragedTest, CreateMountPointFail) |
| John Wedig | b810c92 | 2021-11-17 16:38:03 -0800 | [diff] [blame] | 340 | { |
| John Wedig | b810c92 | 2021-11-17 16:38:03 -0800 | [diff] [blame] | 341 | EXPECT_CALL(*mockCryptIface, cryptFormat(_, _, _, _, _, _, _, _)).Times(1); |
| 342 | |
| 343 | EXPECT_CALL(*mockCryptIface, cryptKeyslotAddByVolumeKey(_, _, _, _, _, _)) |
| 344 | .Times(1); |
| 345 | |
| 346 | EXPECT_CALL(*mockCryptIface, cryptLoad(_, _, _)).Times(1); |
| 347 | |
| 348 | EXPECT_CALL(*mockCryptIface, cryptActivateByPassphrase(_, _, _, _, _, _)) |
| John Wedig | 2443a02 | 2023-03-17 13:42:32 -0700 | [diff] [blame] | 349 | .WillOnce(&createMappedDev); |
| John Wedig | b810c92 | 2021-11-17 16:38:03 -0800 | [diff] [blame] | 350 | |
| Rom Lemarchand | 530ce1e | 2025-12-16 21:45:08 +0000 | [diff] [blame] | 351 | EXPECT_CALL(*mockFsIface, runMkfs(StrEq(esObject->getCryptDevicePath()), |
| 352 | ElementsAreArray(EStoragedTest::options))) |
| John Wedig | 2443a02 | 2023-03-17 13:42:32 -0700 | [diff] [blame] | 353 | .WillOnce(Return(0)); |
| John Wedig | b810c92 | 2021-11-17 16:38:03 -0800 | [diff] [blame] | 354 | |
| John Wedig | 1d6665f | 2025-12-04 19:08:06 +0000 | [diff] [blame] | 355 | EXPECT_CALL(*mockFsIface, runFsck(StrEq(esObject->getCryptDevicePath()), |
| 356 | StrEq("-t ext4 -p"))) |
| 357 | .WillOnce(Return(0)); |
| 358 | |
| John Wedig | b17f825 | 2022-01-12 14:24:26 -0800 | [diff] [blame] | 359 | EXPECT_CALL(*mockFsIface, directoryExists(path(esObject->getMountPoint()))) |
| 360 | .WillOnce(Return(false)); |
| 361 | |
| John Wedig | b810c92 | 2021-11-17 16:38:03 -0800 | [diff] [blame] | 362 | EXPECT_CALL(*mockFsIface, createDirectory(path(esObject->getMountPoint()))) |
| 363 | .WillOnce(Return(false)); |
| 364 | |
| John Wedig | 972c3fa | 2021-12-29 17:30:41 -0800 | [diff] [blame] | 365 | EXPECT_THROW(esObject->formatLuks(password, Volume::FilesystemType::ext4), |
| 366 | InternalFailure); |
| John Wedig | b810c92 | 2021-11-17 16:38:03 -0800 | [diff] [blame] | 367 | EXPECT_FALSE(esObject->isLocked()); |
| John Wedig | 2443a02 | 2023-03-17 13:42:32 -0700 | [diff] [blame] | 368 | |
| 369 | EXPECT_EQ(0, removeMappedDev()); |
| John Wedig | b810c92 | 2021-11-17 16:38:03 -0800 | [diff] [blame] | 370 | } |
| 371 | |
| 372 | /* Test case where we fail to mount the filesystem. */ |
| Ed Tanous | 82897c3 | 2022-02-21 14:11:59 -0800 | [diff] [blame] | 373 | TEST_F(EStoragedTest, MountFail) |
| John Wedig | b810c92 | 2021-11-17 16:38:03 -0800 | [diff] [blame] | 374 | { |
| John Wedig | b810c92 | 2021-11-17 16:38:03 -0800 | [diff] [blame] | 375 | EXPECT_CALL(*mockCryptIface, cryptFormat(_, _, _, _, _, _, _, _)).Times(1); |
| 376 | |
| 377 | EXPECT_CALL(*mockCryptIface, cryptKeyslotAddByVolumeKey(_, _, _, _, _, _)) |
| 378 | .Times(1); |
| 379 | |
| 380 | EXPECT_CALL(*mockCryptIface, cryptLoad(_, _, _)).Times(1); |
| 381 | |
| 382 | EXPECT_CALL(*mockCryptIface, cryptActivateByPassphrase(_, _, _, _, _, _)) |
| John Wedig | 2443a02 | 2023-03-17 13:42:32 -0700 | [diff] [blame] | 383 | .WillOnce(&createMappedDev); |
| John Wedig | b810c92 | 2021-11-17 16:38:03 -0800 | [diff] [blame] | 384 | |
| Rom Lemarchand | 530ce1e | 2025-12-16 21:45:08 +0000 | [diff] [blame] | 385 | EXPECT_CALL(*mockFsIface, runMkfs(StrEq(esObject->getCryptDevicePath()), |
| 386 | ElementsAreArray(EStoragedTest::options))) |
| John Wedig | 2443a02 | 2023-03-17 13:42:32 -0700 | [diff] [blame] | 387 | .WillOnce(Return(0)); |
| John Wedig | b810c92 | 2021-11-17 16:38:03 -0800 | [diff] [blame] | 388 | |
| John Wedig | 1d6665f | 2025-12-04 19:08:06 +0000 | [diff] [blame] | 389 | EXPECT_CALL(*mockFsIface, runFsck(StrEq(esObject->getCryptDevicePath()), |
| 390 | StrEq("-t ext4 -p"))) |
| 391 | .WillOnce(Return(0)); |
| 392 | |
| John Wedig | b17f825 | 2022-01-12 14:24:26 -0800 | [diff] [blame] | 393 | EXPECT_CALL(*mockFsIface, directoryExists(path(esObject->getMountPoint()))) |
| 394 | .WillOnce(Return(false)); |
| 395 | |
| John Wedig | b810c92 | 2021-11-17 16:38:03 -0800 | [diff] [blame] | 396 | EXPECT_CALL(*mockFsIface, createDirectory(path(esObject->getMountPoint()))) |
| 397 | .WillOnce(Return(true)); |
| 398 | |
| 399 | EXPECT_CALL(*mockFsIface, |
| John Wedig | 2443a02 | 2023-03-17 13:42:32 -0700 | [diff] [blame] | 400 | doMount(StrEq(esObject->getCryptDevicePath()), |
| John Wedig | b810c92 | 2021-11-17 16:38:03 -0800 | [diff] [blame] | 401 | StrEq(esObject->getMountPoint()), _, _, _)) |
| 402 | .WillOnce(Return(-1)); |
| 403 | |
| 404 | EXPECT_CALL(*mockFsIface, removeDirectory(path(esObject->getMountPoint()))) |
| 405 | .WillOnce(Return(true)); |
| 406 | |
| John Wedig | 972c3fa | 2021-12-29 17:30:41 -0800 | [diff] [blame] | 407 | EXPECT_THROW(esObject->formatLuks(password, Volume::FilesystemType::ext4), |
| 408 | InternalFailure); |
| John Wedig | b810c92 | 2021-11-17 16:38:03 -0800 | [diff] [blame] | 409 | EXPECT_FALSE(esObject->isLocked()); |
| John Wedig | 2443a02 | 2023-03-17 13:42:32 -0700 | [diff] [blame] | 410 | |
| 411 | EXPECT_EQ(0, removeMappedDev()); |
| John Wedig | b810c92 | 2021-11-17 16:38:03 -0800 | [diff] [blame] | 412 | } |
| 413 | |
| 414 | /* Test case where we fail to unmount the filesystem. */ |
| Ed Tanous | 82897c3 | 2022-02-21 14:11:59 -0800 | [diff] [blame] | 415 | TEST_F(EStoragedTest, UnmountFail) |
| John Wedig | b810c92 | 2021-11-17 16:38:03 -0800 | [diff] [blame] | 416 | { |
| John Wedig | b810c92 | 2021-11-17 16:38:03 -0800 | [diff] [blame] | 417 | EXPECT_CALL(*mockCryptIface, cryptFormat(_, _, _, _, _, _, _, _)).Times(1); |
| 418 | |
| 419 | EXPECT_CALL(*mockCryptIface, cryptKeyslotAddByVolumeKey(_, _, _, _, _, _)) |
| 420 | .Times(1); |
| 421 | |
| 422 | EXPECT_CALL(*mockCryptIface, cryptLoad(_, _, _)).Times(1); |
| 423 | |
| 424 | EXPECT_CALL(*mockCryptIface, cryptActivateByPassphrase(_, _, _, _, _, _)) |
| John Wedig | 2443a02 | 2023-03-17 13:42:32 -0700 | [diff] [blame] | 425 | .WillOnce(&createMappedDev); |
| John Wedig | b810c92 | 2021-11-17 16:38:03 -0800 | [diff] [blame] | 426 | |
| Rom Lemarchand | 530ce1e | 2025-12-16 21:45:08 +0000 | [diff] [blame] | 427 | EXPECT_CALL(*mockFsIface, runMkfs(StrEq(esObject->getCryptDevicePath()), |
| 428 | ElementsAreArray(EStoragedTest::options))) |
| John Wedig | 2443a02 | 2023-03-17 13:42:32 -0700 | [diff] [blame] | 429 | .WillOnce(Return(0)); |
| John Wedig | b810c92 | 2021-11-17 16:38:03 -0800 | [diff] [blame] | 430 | |
| John Wedig | 1d6665f | 2025-12-04 19:08:06 +0000 | [diff] [blame] | 431 | EXPECT_CALL(*mockFsIface, runFsck(StrEq(esObject->getCryptDevicePath()), |
| 432 | StrEq("-t ext4 -p"))) |
| 433 | .WillOnce(Return(0)); |
| 434 | |
| John Wedig | b17f825 | 2022-01-12 14:24:26 -0800 | [diff] [blame] | 435 | EXPECT_CALL(*mockFsIface, directoryExists(path(esObject->getMountPoint()))) |
| 436 | .WillOnce(Return(false)); |
| 437 | |
| John Wedig | b810c92 | 2021-11-17 16:38:03 -0800 | [diff] [blame] | 438 | EXPECT_CALL(*mockFsIface, createDirectory(path(esObject->getMountPoint()))) |
| 439 | .WillOnce(Return(true)); |
| 440 | |
| 441 | EXPECT_CALL(*mockFsIface, |
| John Wedig | 2443a02 | 2023-03-17 13:42:32 -0700 | [diff] [blame] | 442 | doMount(StrEq(esObject->getCryptDevicePath()), |
| John Wedig | b810c92 | 2021-11-17 16:38:03 -0800 | [diff] [blame] | 443 | StrEq(esObject->getMountPoint()), _, _, _)) |
| 444 | .WillOnce(Return(0)); |
| 445 | |
| 446 | EXPECT_CALL(*mockFsIface, doUnmount(StrEq(esObject->getMountPoint()))) |
| 447 | .WillOnce(Return(-1)); |
| 448 | |
| John Wedig | 972c3fa | 2021-12-29 17:30:41 -0800 | [diff] [blame] | 449 | esObject->formatLuks(password, Volume::FilesystemType::ext4); |
| John Wedig | b810c92 | 2021-11-17 16:38:03 -0800 | [diff] [blame] | 450 | EXPECT_FALSE(esObject->isLocked()); |
| 451 | |
| John Wedig | 972c3fa | 2021-12-29 17:30:41 -0800 | [diff] [blame] | 452 | EXPECT_THROW(esObject->lock(), InternalFailure); |
| John Wedig | b810c92 | 2021-11-17 16:38:03 -0800 | [diff] [blame] | 453 | EXPECT_FALSE(esObject->isLocked()); |
| John Wedig | 2443a02 | 2023-03-17 13:42:32 -0700 | [diff] [blame] | 454 | |
| 455 | EXPECT_EQ(0, removeMappedDev()); |
| John Wedig | b810c92 | 2021-11-17 16:38:03 -0800 | [diff] [blame] | 456 | } |
| 457 | |
| 458 | /* Test case where we fail to remove the mount point. */ |
| Ed Tanous | 82897c3 | 2022-02-21 14:11:59 -0800 | [diff] [blame] | 459 | TEST_F(EStoragedTest, RemoveMountPointFail) |
| John Wedig | b810c92 | 2021-11-17 16:38:03 -0800 | [diff] [blame] | 460 | { |
| John Wedig | b810c92 | 2021-11-17 16:38:03 -0800 | [diff] [blame] | 461 | EXPECT_CALL(*mockCryptIface, cryptFormat(_, _, _, _, _, _, _, _)).Times(1); |
| 462 | |
| 463 | EXPECT_CALL(*mockCryptIface, cryptKeyslotAddByVolumeKey(_, _, _, _, _, _)) |
| 464 | .Times(1); |
| 465 | |
| 466 | EXPECT_CALL(*mockCryptIface, cryptLoad(_, _, _)).Times(1); |
| 467 | |
| 468 | EXPECT_CALL(*mockCryptIface, cryptActivateByPassphrase(_, _, _, _, _, _)) |
| John Wedig | 2443a02 | 2023-03-17 13:42:32 -0700 | [diff] [blame] | 469 | .WillOnce(&createMappedDev); |
| John Wedig | b810c92 | 2021-11-17 16:38:03 -0800 | [diff] [blame] | 470 | |
| Rom Lemarchand | 530ce1e | 2025-12-16 21:45:08 +0000 | [diff] [blame] | 471 | EXPECT_CALL(*mockFsIface, runMkfs(StrEq(esObject->getCryptDevicePath()), |
| 472 | ElementsAreArray(EStoragedTest::options))) |
| John Wedig | 2443a02 | 2023-03-17 13:42:32 -0700 | [diff] [blame] | 473 | .WillOnce(Return(0)); |
| John Wedig | b810c92 | 2021-11-17 16:38:03 -0800 | [diff] [blame] | 474 | |
| John Wedig | 1d6665f | 2025-12-04 19:08:06 +0000 | [diff] [blame] | 475 | EXPECT_CALL(*mockFsIface, runFsck(StrEq(esObject->getCryptDevicePath()), |
| 476 | StrEq("-t ext4 -p"))) |
| 477 | .WillOnce(Return(0)); |
| 478 | |
| John Wedig | b17f825 | 2022-01-12 14:24:26 -0800 | [diff] [blame] | 479 | EXPECT_CALL(*mockFsIface, directoryExists(path(esObject->getMountPoint()))) |
| 480 | .WillOnce(Return(false)); |
| 481 | |
| John Wedig | b810c92 | 2021-11-17 16:38:03 -0800 | [diff] [blame] | 482 | EXPECT_CALL(*mockFsIface, createDirectory(path(esObject->getMountPoint()))) |
| 483 | .WillOnce(Return(true)); |
| 484 | |
| 485 | EXPECT_CALL(*mockFsIface, |
| John Wedig | 2443a02 | 2023-03-17 13:42:32 -0700 | [diff] [blame] | 486 | doMount(StrEq(esObject->getCryptDevicePath()), |
| John Wedig | b810c92 | 2021-11-17 16:38:03 -0800 | [diff] [blame] | 487 | StrEq(esObject->getMountPoint()), _, _, _)) |
| 488 | .WillOnce(Return(0)); |
| 489 | |
| 490 | EXPECT_CALL(*mockFsIface, doUnmount(StrEq(esObject->getMountPoint()))) |
| 491 | .WillOnce(Return(0)); |
| 492 | |
| 493 | EXPECT_CALL(*mockFsIface, removeDirectory(path(esObject->getMountPoint()))) |
| 494 | .WillOnce(Return(false)); |
| 495 | |
| John Wedig | 972c3fa | 2021-12-29 17:30:41 -0800 | [diff] [blame] | 496 | esObject->formatLuks(password, Volume::FilesystemType::ext4); |
| John Wedig | b810c92 | 2021-11-17 16:38:03 -0800 | [diff] [blame] | 497 | EXPECT_FALSE(esObject->isLocked()); |
| 498 | |
| 499 | /* This will fail to remove the mount point. */ |
| 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 | |
| 506 | /* Test case where we fail to deactivate the LUKS device. */ |
| Ed Tanous | 82897c3 | 2022-02-21 14:11:59 -0800 | [diff] [blame] | 507 | TEST_F(EStoragedTest, DeactivateFail) |
| John Wedig | b810c92 | 2021-11-17 16:38:03 -0800 | [diff] [blame] | 508 | { |
| John Wedig | b810c92 | 2021-11-17 16:38:03 -0800 | [diff] [blame] | 509 | EXPECT_CALL(*mockCryptIface, cryptFormat(_, _, _, _, _, _, _, _)).Times(1); |
| 510 | |
| 511 | EXPECT_CALL(*mockCryptIface, cryptKeyslotAddByVolumeKey(_, _, _, _, _, _)) |
| 512 | .Times(1); |
| 513 | |
| 514 | EXPECT_CALL(*mockCryptIface, cryptLoad(_, _, _)).Times(1); |
| 515 | |
| 516 | EXPECT_CALL(*mockCryptIface, cryptActivateByPassphrase(_, _, _, _, _, _)) |
| John Wedig | 2443a02 | 2023-03-17 13:42:32 -0700 | [diff] [blame] | 517 | .WillOnce(&createMappedDev); |
| John Wedig | b810c92 | 2021-11-17 16:38:03 -0800 | [diff] [blame] | 518 | |
| Rom Lemarchand | 530ce1e | 2025-12-16 21:45:08 +0000 | [diff] [blame] | 519 | EXPECT_CALL(*mockFsIface, runMkfs(StrEq(esObject->getCryptDevicePath()), |
| 520 | ElementsAreArray(EStoragedTest::options))) |
| John Wedig | 2443a02 | 2023-03-17 13:42:32 -0700 | [diff] [blame] | 521 | .WillOnce(Return(0)); |
| John Wedig | b810c92 | 2021-11-17 16:38:03 -0800 | [diff] [blame] | 522 | |
| John Wedig | 1d6665f | 2025-12-04 19:08:06 +0000 | [diff] [blame] | 523 | EXPECT_CALL(*mockFsIface, runFsck(StrEq(esObject->getCryptDevicePath()), |
| 524 | StrEq("-t ext4 -p"))) |
| 525 | .WillOnce(Return(0)); |
| 526 | |
| John Wedig | b17f825 | 2022-01-12 14:24:26 -0800 | [diff] [blame] | 527 | EXPECT_CALL(*mockFsIface, directoryExists(path(esObject->getMountPoint()))) |
| 528 | .WillOnce(Return(false)); |
| 529 | |
| John Wedig | b810c92 | 2021-11-17 16:38:03 -0800 | [diff] [blame] | 530 | EXPECT_CALL(*mockFsIface, createDirectory(path(esObject->getMountPoint()))) |
| 531 | .WillOnce(Return(true)); |
| 532 | |
| 533 | EXPECT_CALL(*mockFsIface, |
| John Wedig | 2443a02 | 2023-03-17 13:42:32 -0700 | [diff] [blame] | 534 | doMount(StrEq(esObject->getCryptDevicePath()), |
| John Wedig | b810c92 | 2021-11-17 16:38:03 -0800 | [diff] [blame] | 535 | StrEq(esObject->getMountPoint()), _, _, _)) |
| 536 | .WillOnce(Return(0)); |
| 537 | |
| 538 | EXPECT_CALL(*mockFsIface, doUnmount(StrEq(esObject->getMountPoint()))) |
| 539 | .WillOnce(Return(0)); |
| 540 | |
| 541 | EXPECT_CALL(*mockFsIface, removeDirectory(path(esObject->getMountPoint()))) |
| 542 | .WillOnce(Return(true)); |
| 543 | |
| 544 | EXPECT_CALL(*mockCryptIface, cryptDeactivate(_, _)).WillOnce(Return(-1)); |
| 545 | |
| 546 | /* Format the encrypted device. */ |
| John Wedig | 972c3fa | 2021-12-29 17:30:41 -0800 | [diff] [blame] | 547 | esObject->formatLuks(password, Volume::FilesystemType::ext4); |
| John Wedig | b810c92 | 2021-11-17 16:38:03 -0800 | [diff] [blame] | 548 | EXPECT_FALSE(esObject->isLocked()); |
| 549 | |
| John Wedig | 972c3fa | 2021-12-29 17:30:41 -0800 | [diff] [blame] | 550 | EXPECT_THROW(esObject->lock(), InternalFailure); |
| John Wedig | b810c92 | 2021-11-17 16:38:03 -0800 | [diff] [blame] | 551 | EXPECT_FALSE(esObject->isLocked()); |
| John Wedig | 2443a02 | 2023-03-17 13:42:32 -0700 | [diff] [blame] | 552 | |
| 553 | EXPECT_EQ(0, removeMappedDev()); |
| John Wedig | b810c92 | 2021-11-17 16:38:03 -0800 | [diff] [blame] | 554 | } |
| 555 | |
| John Wedig | 8d5a3a0 | 2022-09-29 15:25:58 -0700 | [diff] [blame] | 556 | /* Test case where we successfully change the password. */ |
| 557 | TEST_F(EStoragedTest, ChangePasswordSuccess) |
| 558 | { |
| 559 | std::string newPasswordString("newPassword"); |
| 560 | std::vector<uint8_t> newPassword(newPasswordString.begin(), |
| 561 | newPasswordString.end()); |
| 562 | |
| 563 | EXPECT_CALL(*mockCryptIface, cryptLoad(_, _, _)).Times(1); |
| 564 | |
| 565 | EXPECT_CALL(*mockCryptIface, |
| 566 | cryptKeyslotChangeByPassphrase( |
| 567 | _, _, _, reinterpret_cast<const char*>(password.data()), |
| 568 | password.size(), |
| 569 | reinterpret_cast<const char*>(newPassword.data()), |
| 570 | newPassword.size())) |
| 571 | .WillOnce(Return(0)); |
| 572 | |
| 573 | /* Change the password for the LUKS-encrypted device. */ |
| 574 | esObject->changePassword(password, newPassword); |
| 575 | } |
| 576 | |
| 577 | /* Test case where we fail to change the password. */ |
| 578 | TEST_F(EStoragedTest, ChangePasswordFail) |
| 579 | { |
| 580 | std::string newPasswordString("newPassword"); |
| 581 | std::vector<uint8_t> newPassword(newPasswordString.begin(), |
| 582 | newPasswordString.end()); |
| 583 | |
| 584 | EXPECT_CALL(*mockCryptIface, cryptLoad(_, _, _)).Times(1); |
| 585 | |
| 586 | EXPECT_CALL(*mockCryptIface, |
| 587 | cryptKeyslotChangeByPassphrase( |
| 588 | _, _, _, reinterpret_cast<const char*>(password.data()), |
| 589 | password.size(), |
| 590 | reinterpret_cast<const char*>(newPassword.data()), |
| 591 | newPassword.size())) |
| 592 | .WillOnce(Return(-1)); |
| 593 | |
| 594 | EXPECT_THROW(esObject->changePassword(password, newPassword), |
| 595 | InternalFailure); |
| 596 | } |
| 597 | |
| Willy Tu | da5aa61 | 2025-12-18 07:32:34 +0000 | [diff] [blame] | 598 | TEST(EMMCBackgroundOperation, IoCtlFailure) |
| 599 | { |
| Willy Tu | 6c39e6a | 2026-01-06 19:05:37 +0000 | [diff] [blame] | 600 | std::unique_ptr<FdMock> mockFd = std::make_unique<FdMock>(); |
| Willy Tu | da5aa61 | 2025-12-18 07:32:34 +0000 | [diff] [blame] | 601 | |
| 602 | EXPECT_CALL(*mockFd, ioctl(MMC_IOC_CMD, testing::_)).WillOnce(Return(1)); |
| 603 | EXPECT_THROW(estoraged::EStoraged::enableBackgroundOperation( |
| 604 | std::move(mockFd), "/dev/test"), |
| 605 | estoraged::BkopsIoctlFailure); |
| 606 | } |
| 607 | |
| 608 | TEST(EMMCBackgroundOperation, BkOpsNotSupported) |
| 609 | { |
| Willy Tu | 6c39e6a | 2026-01-06 19:05:37 +0000 | [diff] [blame] | 610 | std::unique_ptr<FdMock> mockFd = std::make_unique<FdMock>(); |
| Willy Tu | da5aa61 | 2025-12-18 07:32:34 +0000 | [diff] [blame] | 611 | |
| 612 | EXPECT_CALL(*mockFd, ioctl(MMC_IOC_CMD, testing::_)).WillOnce(Return(0)); |
| 613 | EXPECT_FALSE(estoraged::EStoraged::enableBackgroundOperation( |
| 614 | std::move(mockFd), "/dev/test")); |
| 615 | } |
| 616 | |
| 617 | TEST(EMMCBackgroundOperation, EnableFailure) |
| 618 | { |
| Willy Tu | 6c39e6a | 2026-01-06 19:05:37 +0000 | [diff] [blame] | 619 | std::unique_ptr<FdMock> mockFd = std::make_unique<FdMock>(); |
| Willy Tu | da5aa61 | 2025-12-18 07:32:34 +0000 | [diff] [blame] | 620 | |
| 621 | EXPECT_CALL(*mockFd, ioctl(MMC_IOC_CMD, testing::_)) |
| 622 | .WillOnce(testing::Invoke([](unsigned long, void* data) { |
| 623 | struct mmc_ioc_cmd* idata = static_cast<struct mmc_ioc_cmd*>(data); |
| 624 | EXPECT_EQ(idata->opcode, MMC_SEND_EXT_CSD); |
| 625 | EXPECT_EQ(idata->blksz, 512); |
| 626 | EXPECT_EQ(idata->flags, MMC_RSP_SPI_R1 | MMC_RSP_R1 | MMC_CMD_ADTC); |
| 627 | |
| 628 | // Set specific bytes to simulate a hardware state |
| 629 | // NOLINTNEXTLINE(performance-no-int-to-ptr) |
| 630 | *reinterpret_cast<uint8_t*>(idata->data_ptr + 502) = |
| 631 | 0x01; // BKOPS_SUPPORT = Supported |
| 632 | // NOLINTNEXTLINE(performance-no-int-to-ptr) |
| 633 | *reinterpret_cast<uint8_t*>(idata->data_ptr + 163) = |
| 634 | 0x00; // BKOPS_EN = Disabled |
| 635 | return 0; // Success |
| 636 | })) |
| 637 | .WillOnce(Return(1)); |
| 638 | EXPECT_THROW(estoraged::EStoraged::enableBackgroundOperation( |
| 639 | std::move(mockFd), "/dev/test"), |
| 640 | estoraged::BkopsEnableFailure); |
| 641 | } |
| 642 | |
| 643 | TEST(EMMCBackgroundOperation, AlreadyEnabled) |
| 644 | { |
| Willy Tu | 6c39e6a | 2026-01-06 19:05:37 +0000 | [diff] [blame] | 645 | std::unique_ptr<FdMock> mockFd = std::make_unique<FdMock>(); |
| Willy Tu | da5aa61 | 2025-12-18 07:32:34 +0000 | [diff] [blame] | 646 | |
| 647 | EXPECT_CALL(*mockFd, ioctl(MMC_IOC_CMD, testing::_)) |
| 648 | .WillOnce(testing::Invoke([](unsigned long, void* data) { |
| 649 | struct mmc_ioc_cmd* idata = static_cast<struct mmc_ioc_cmd*>(data); |
| 650 | EXPECT_EQ(idata->opcode, MMC_SEND_EXT_CSD); |
| 651 | EXPECT_EQ(idata->blksz, 512); |
| 652 | EXPECT_EQ(idata->flags, MMC_RSP_SPI_R1 | MMC_RSP_R1 | MMC_CMD_ADTC); |
| 653 | |
| 654 | // Set specific bytes to simulate a hardware state |
| 655 | // NOLINTNEXTLINE(performance-no-int-to-ptr) |
| 656 | *reinterpret_cast<uint8_t*>(idata->data_ptr + 502) = |
| 657 | 0x01; // BKOPS_SUPPORT = Supported |
| 658 | // NOLINTNEXTLINE(performance-no-int-to-ptr) |
| 659 | *reinterpret_cast<uint8_t*>(idata->data_ptr + 163) = |
| 660 | EXT_CSD_MANUAL_BKOPS_MASK; // BKOPS_EN = Manual Mode |
| 661 | return 0; // Success |
| 662 | })); |
| 663 | EXPECT_FALSE(estoraged::EStoraged::enableBackgroundOperation( |
| 664 | std::move(mockFd), "/dev/test")); |
| 665 | } |
| 666 | |
| 667 | TEST(EMMCBackgroundOperation, EnableSuccess) |
| 668 | { |
| Willy Tu | 6c39e6a | 2026-01-06 19:05:37 +0000 | [diff] [blame] | 669 | std::unique_ptr<FdMock> mockFd = std::make_unique<FdMock>(); |
| Willy Tu | da5aa61 | 2025-12-18 07:32:34 +0000 | [diff] [blame] | 670 | |
| 671 | EXPECT_CALL(*mockFd, ioctl(MMC_IOC_CMD, testing::_)) |
| 672 | .WillOnce(testing::Invoke([](unsigned long, void* data) { |
| 673 | struct mmc_ioc_cmd* idata = static_cast<struct mmc_ioc_cmd*>(data); |
| 674 | EXPECT_EQ(idata->opcode, MMC_SEND_EXT_CSD); |
| 675 | EXPECT_EQ(idata->blksz, 512); |
| 676 | EXPECT_EQ(idata->flags, MMC_RSP_SPI_R1 | MMC_RSP_R1 | MMC_CMD_ADTC); |
| 677 | |
| 678 | // Set specific bytes to simulate a hardware state |
| 679 | // NOLINTNEXTLINE(performance-no-int-to-ptr) |
| 680 | *reinterpret_cast<uint8_t*>(idata->data_ptr + 502) = |
| 681 | 0x01; // BKOPS_SUPPORT = Supported |
| 682 | // NOLINTNEXTLINE(performance-no-int-to-ptr) |
| 683 | *reinterpret_cast<uint8_t*>(idata->data_ptr + 163) = |
| 684 | 0x00; // BKOPS_EN = Disabled |
| 685 | return 0; // Success |
| 686 | })) |
| 687 | .WillOnce(testing::Invoke([](unsigned long, void* data) { |
| 688 | struct mmc_ioc_cmd* idata = static_cast<struct mmc_ioc_cmd*>(data); |
| 689 | EXPECT_EQ(idata->opcode, MMC_SWITCH); |
| 690 | EXPECT_EQ(idata->arg, (MMC_SWITCH_MODE_WRITE_BYTE << 24) | |
| 691 | (EXT_CSD_BKOPS_EN << 16) | |
| 692 | (EXT_CSD_MANUAL_BKOPS_MASK << 8) | |
| 693 | EXT_CSD_CMD_SET_NORMAL); |
| 694 | EXPECT_EQ(idata->flags, MMC_RSP_SPI_R1B | MMC_RSP_R1B | MMC_CMD_AC); |
| 695 | return 0; // Success |
| 696 | })); |
| 697 | EXPECT_TRUE(estoraged::EStoraged::enableBackgroundOperation( |
| 698 | std::move(mockFd), "/dev/test")); |
| 699 | } |
| 700 | |
| Willy Tu | 30bfe8e | 2025-12-30 19:47:52 +0000 | [diff] [blame^] | 701 | TEST(HSMode, HsModeError) |
| 702 | { |
| 703 | std::unique_ptr<FdMock> mockFd = std::make_unique<FdMock>(); |
| 704 | EXPECT_CALL(*mockFd, ioctl(MMC_IOC_CMD, testing::_)).WillOnce(Return(1)); |
| 705 | EXPECT_THROW(estoraged::EStoraged::changeHsTimingIfNeeded( |
| 706 | mockFd.get(), "/dev/test", "TestPart"), |
| 707 | estoraged::HsModeError); |
| 708 | } |
| 709 | |
| 710 | TEST(HSMode, EnableSuccess) |
| 711 | { |
| 712 | std::unique_ptr<FdMock> mockFd = std::make_unique<FdMock>(); |
| 713 | |
| 714 | EXPECT_CALL(*mockFd, ioctl(MMC_IOC_CMD, testing::_)) |
| 715 | .WillOnce(testing::Invoke([](unsigned long, void* data) { |
| 716 | struct mmc_ioc_cmd* idata = static_cast<struct mmc_ioc_cmd*>(data); |
| 717 | EXPECT_EQ(idata->opcode, MMC_SWITCH); |
| 718 | EXPECT_EQ(idata->arg, (MMC_SWITCH_MODE_WRITE_BYTE << 24) | |
| 719 | (EXT_CSD_HS_TIMING << 16) | |
| 720 | (EXT_CSD_TIMING_HS400 << 8)); |
| 721 | EXPECT_EQ(idata->flags, MMC_RSP_R1B | MMC_CMD_AC); |
| 722 | return 0; // Success |
| 723 | })); |
| 724 | EXPECT_TRUE(estoraged::EStoraged::changeHsTimingIfNeeded( |
| 725 | mockFd.get(), "/dev/test", "TestPart")); |
| 726 | } |
| 727 | |
| 728 | TEST(HSMode, InvalidPart) |
| 729 | { |
| 730 | std::unique_ptr<FdMock> mockFd = std::make_unique<FdMock>(); |
| 731 | EXPECT_FALSE(estoraged::EStoraged::changeHsTimingIfNeeded( |
| 732 | mockFd.get(), "/dev/test", "InvalidPart")); |
| 733 | } |
| 734 | |
| 735 | TEST(HSMode, Null) |
| 736 | { |
| 737 | EXPECT_FALSE(estoraged::EStoraged::changeHsTimingIfNeeded( |
| 738 | nullptr, "/dev/test", "TestPart")); |
| 739 | } |
| 740 | |
| John Wedig | b810c92 | 2021-11-17 16:38:03 -0800 | [diff] [blame] | 741 | } // namespace estoraged_test |