| Tom Tung | 043af59 | 2023-11-24 13:37:05 +0800 | [diff] [blame] | 1 | #include "estoraged_conf.hpp" | 
| John Wedig | d32b966 | 2022-04-13 18:12:25 -0700 | [diff] [blame] | 2 | #include "getConfig.hpp" | 
|  | 3 |  | 
| John Wedig | d32b966 | 2022-04-13 18:12:25 -0700 | [diff] [blame] | 4 | #include <boost/container/flat_map.hpp> | 
| John Edward Broadbent | 5d799bb | 2022-03-22 16:14:24 -0700 | [diff] [blame] | 5 | #include <util.hpp> | 
|  | 6 |  | 
| John Wedig | d32b966 | 2022-04-13 18:12:25 -0700 | [diff] [blame] | 7 | #include <filesystem> | 
| John Edward Broadbent | 5d799bb | 2022-03-22 16:14:24 -0700 | [diff] [blame] | 8 | #include <fstream> | 
|  | 9 |  | 
|  | 10 | #include <gmock/gmock-matchers.h> | 
|  | 11 | #include <gmock/gmock.h> | 
|  | 12 | #include <gtest/gtest.h> | 
|  | 13 |  | 
|  | 14 | namespace estoraged_test | 
|  | 15 | { | 
|  | 16 | using estoraged::util::findPredictedMediaLifeLeftPercent; | 
| John Wedig | b483830 | 2022-07-22 13:51:16 -0700 | [diff] [blame] | 17 | using estoraged::util::getPartNumber; | 
|  | 18 | using estoraged::util::getSerialNumber; | 
| John Edward Broadbent | 5d799bb | 2022-03-22 16:14:24 -0700 | [diff] [blame] | 19 |  | 
|  | 20 | TEST(utilTest, passFindPredictedMediaLife) | 
|  | 21 | { | 
|  | 22 | std::string prefixName = "."; | 
|  | 23 | std::string testFileName = prefixName + "/life_time"; | 
|  | 24 | std::ofstream testFile; | 
|  | 25 | testFile.open(testFileName, | 
|  | 26 | std::ios::out | std::ios::binary | std::ios::trunc); | 
|  | 27 | testFile << "0x07 0x04"; | 
|  | 28 | testFile.close(); | 
|  | 29 | EXPECT_EQ(findPredictedMediaLifeLeftPercent(prefixName), 40); | 
| John Wedig | b483830 | 2022-07-22 13:51:16 -0700 | [diff] [blame] | 30 | EXPECT_TRUE(std::filesystem::remove(testFileName)); | 
| John Edward Broadbent | 5d799bb | 2022-03-22 16:14:24 -0700 | [diff] [blame] | 31 | } | 
|  | 32 |  | 
|  | 33 | TEST(utilTest, estimatesSame) | 
|  | 34 | { | 
| John Edward Broadbent | 5d799bb | 2022-03-22 16:14:24 -0700 | [diff] [blame] | 35 | std::string prefixName = "."; | 
|  | 36 | std::string testFileName = prefixName + "/life_time"; | 
|  | 37 | std::ofstream testFile; | 
|  | 38 | testFile.open(testFileName, | 
|  | 39 | std::ios::out | std::ios::binary | std::ios::trunc); | 
|  | 40 | testFile << "0x04 0x04"; | 
|  | 41 | testFile.close(); | 
|  | 42 |  | 
|  | 43 | EXPECT_EQ(findPredictedMediaLifeLeftPercent(prefixName), 70); | 
| John Wedig | b483830 | 2022-07-22 13:51:16 -0700 | [diff] [blame] | 44 | EXPECT_TRUE(std::filesystem::remove(testFileName)); | 
| John Edward Broadbent | 5d799bb | 2022-03-22 16:14:24 -0700 | [diff] [blame] | 45 | } | 
|  | 46 |  | 
|  | 47 | TEST(utilTest, estimatesNotAvailable) | 
|  | 48 | { | 
| John Edward Broadbent | 5d799bb | 2022-03-22 16:14:24 -0700 | [diff] [blame] | 49 | std::string prefixName = "."; | 
|  | 50 | std::string testFileName = prefixName + "/life_time"; | 
|  | 51 | std::ofstream testFile; | 
|  | 52 | testFile.open(testFileName, | 
|  | 53 | std::ios::out | std::ios::binary | std::ios::trunc); | 
|  | 54 | testFile.close(); | 
|  | 55 |  | 
|  | 56 | EXPECT_EQ(findPredictedMediaLifeLeftPercent(prefixName), 255); | 
| John Wedig | b483830 | 2022-07-22 13:51:16 -0700 | [diff] [blame] | 57 | EXPECT_TRUE(std::filesystem::remove(testFileName)); | 
|  | 58 | } | 
|  | 59 |  | 
|  | 60 | TEST(utilTest, getPartNumberFail) | 
|  | 61 | { | 
|  | 62 | std::string prefixName = "."; | 
|  | 63 | std::string testFileName = prefixName + "/name"; | 
|  | 64 | /* The part name file won't exist for this test. */ | 
|  | 65 | EXPECT_EQ(getPartNumber(prefixName), "unknown"); | 
|  | 66 | } | 
|  | 67 |  | 
|  | 68 | TEST(utilTest, getPartNumberPass) | 
|  | 69 | { | 
|  | 70 | std::string prefixName = "."; | 
|  | 71 | std::string testFileName = prefixName + "/name"; | 
|  | 72 | std::ofstream testFile; | 
|  | 73 | testFile.open(testFileName, | 
|  | 74 | std::ios::out | std::ios::binary | std::ios::trunc); | 
|  | 75 | testFile << "ABCD1234"; | 
|  | 76 | testFile.close(); | 
|  | 77 | EXPECT_EQ(getPartNumber(prefixName), "ABCD1234"); | 
|  | 78 | EXPECT_TRUE(std::filesystem::remove(testFileName)); | 
|  | 79 | } | 
|  | 80 |  | 
|  | 81 | TEST(utilTest, getSerialNumberFail) | 
|  | 82 | { | 
|  | 83 | std::string prefixName = "."; | 
|  | 84 | std::string testFileName = prefixName + "/serial"; | 
|  | 85 | /* The serial number file won't exist for this test. */ | 
|  | 86 | EXPECT_EQ(getSerialNumber(prefixName), "unknown"); | 
|  | 87 | } | 
|  | 88 |  | 
|  | 89 | TEST(utilTest, getSerialNumberPass) | 
|  | 90 | { | 
|  | 91 | std::string prefixName = "."; | 
|  | 92 | std::string testFileName = prefixName + "/serial"; | 
|  | 93 | std::ofstream testFile; | 
|  | 94 | testFile.open(testFileName, | 
|  | 95 | std::ios::out | std::ios::binary | std::ios::trunc); | 
|  | 96 | testFile << "0x12345678"; | 
|  | 97 | testFile.close(); | 
|  | 98 | EXPECT_EQ(getSerialNumber(prefixName), "0x12345678"); | 
|  | 99 | EXPECT_TRUE(std::filesystem::remove(testFileName)); | 
| John Edward Broadbent | 5d799bb | 2022-03-22 16:14:24 -0700 | [diff] [blame] | 100 | } | 
|  | 101 |  | 
| John Wedig | d32b966 | 2022-04-13 18:12:25 -0700 | [diff] [blame] | 102 | /* Test case where we successfully find the device file. */ | 
|  | 103 | TEST(utilTest, findDevicePass) | 
|  | 104 | { | 
|  | 105 | estoraged::StorageData data; | 
|  | 106 |  | 
|  | 107 | /* Set up the map of properties. */ | 
|  | 108 | data.emplace(std::string("Type"), | 
|  | 109 | estoraged::BasicVariantType("EmmcDevice")); | 
|  | 110 | data.emplace(std::string("Name"), estoraged::BasicVariantType("emmc")); | 
| Rahul Kapoor | 1982505 | 2023-05-27 01:52:23 +0000 | [diff] [blame] | 111 | data.emplace(std::string("LocationCode"), | 
|  | 112 | estoraged::BasicVariantType("U102020")); | 
| John Wedig | d32b966 | 2022-04-13 18:12:25 -0700 | [diff] [blame] | 113 |  | 
| John Wedig | f78215f | 2022-06-07 13:39:22 -0700 | [diff] [blame] | 114 | /* Create a dummy device. */ | 
|  | 115 | std::filesystem::create_directories("abc/device"); | 
|  | 116 | const std::string dummyTypeFileName("abc/device/type"); | 
|  | 117 | std::ofstream dummyTypeFile(dummyTypeFileName, | 
|  | 118 | std::ios::out | std::ios::trunc); | 
|  | 119 | dummyTypeFile << "SSD"; | 
|  | 120 | dummyTypeFile.close(); | 
| John Wedig | d32b966 | 2022-04-13 18:12:25 -0700 | [diff] [blame] | 121 |  | 
| John Wedig | f78215f | 2022-06-07 13:39:22 -0700 | [diff] [blame] | 122 | /* Another device. */ | 
|  | 123 | std::filesystem::create_directories("def/device"); | 
|  | 124 |  | 
|  | 125 | /* Create a dummy eMMC device. */ | 
|  | 126 | std::filesystem::create_directories("mmcblk0/device"); | 
|  | 127 | const std::string typeFileName("mmcblk0/device/type"); | 
|  | 128 | std::ofstream typeFile(typeFileName, std::ios::out | std::ios::trunc); | 
|  | 129 | typeFile << "MMC"; | 
|  | 130 | typeFile.close(); | 
| John Wedig | d32b966 | 2022-04-13 18:12:25 -0700 | [diff] [blame] | 131 |  | 
|  | 132 | /* Look for the device file. */ | 
|  | 133 | std::filesystem::path deviceFile, sysfsDir; | 
| Rahul Kapoor | 1982505 | 2023-05-27 01:52:23 +0000 | [diff] [blame] | 134 | std::string luksName, locationCode; | 
| Tom Tung | 043af59 | 2023-11-24 13:37:05 +0800 | [diff] [blame] | 135 | auto result = estoraged::util::findDevice(data, | 
|  | 136 | std::filesystem::path("./")); | 
|  | 137 | EXPECT_TRUE(result.has_value()); | 
| John Wedig | d32b966 | 2022-04-13 18:12:25 -0700 | [diff] [blame] | 138 |  | 
|  | 139 | /* Validate the results. */ | 
| Tom Tung | 043af59 | 2023-11-24 13:37:05 +0800 | [diff] [blame] | 140 | EXPECT_EQ("/dev/mmcblk0", result->deviceFile.string()); | 
|  | 141 | EXPECT_EQ("./mmcblk0/device", result->sysfsDir.string()); | 
|  | 142 | EXPECT_EQ("luks-mmcblk0", result->luksName); | 
|  | 143 | EXPECT_EQ("U102020", result->locationCode); | 
|  | 144 | EXPECT_EQ(ERASE_MAX_GEOMETRY, result->eraseMaxGeometry); | 
|  | 145 | EXPECT_EQ(ERASE_MIN_GEOMETRY, result->eraseMinGeometry); | 
| John Wedig | d7be42b | 2024-01-19 16:07:19 -0800 | [diff] [blame^] | 146 | EXPECT_EQ("SSD", result->driveType); | 
| Tom Tung | 043af59 | 2023-11-24 13:37:05 +0800 | [diff] [blame] | 147 |  | 
|  | 148 | /* Delete the dummy files. */ | 
|  | 149 | EXPECT_EQ(3U, std::filesystem::remove_all("mmcblk0")); | 
|  | 150 | EXPECT_EQ(3U, std::filesystem::remove_all("abc")); | 
|  | 151 | EXPECT_EQ(2U, std::filesystem::remove_all("def")); | 
|  | 152 | } | 
|  | 153 |  | 
|  | 154 | /* Test case where we successfully find the device file with updating the | 
|  | 155 | * min/max geometry. */ | 
|  | 156 | TEST(utilTest, findDeviceWithMaxAndMinGeometryPass) | 
|  | 157 | { | 
|  | 158 | estoraged::StorageData data; | 
|  | 159 |  | 
|  | 160 | /* Set up the map of properties. */ | 
|  | 161 | data.emplace(std::string("Type"), | 
|  | 162 | estoraged::BasicVariantType("EmmcDevice")); | 
|  | 163 | data.emplace(std::string("Name"), estoraged::BasicVariantType("emmc")); | 
|  | 164 | data.emplace(std::string("LocationCode"), | 
|  | 165 | estoraged::BasicVariantType("U102020")); | 
|  | 166 | data.emplace(std::string("EraseMaxGeometry"), | 
|  | 167 | estoraged::BasicVariantType((uint64_t)5566)); | 
|  | 168 | data.emplace(std::string("EraseMinGeometry"), | 
|  | 169 | estoraged::BasicVariantType((uint64_t)1234)); | 
|  | 170 |  | 
|  | 171 | /* Create a dummy device. */ | 
|  | 172 | std::filesystem::create_directories("abc/device"); | 
|  | 173 | const std::string dummyTypeFileName("abc/device/type"); | 
|  | 174 | std::ofstream dummyTypeFile(dummyTypeFileName, | 
|  | 175 | std::ios::out | std::ios::trunc); | 
|  | 176 | dummyTypeFile << "SSD"; | 
|  | 177 | dummyTypeFile.close(); | 
|  | 178 |  | 
|  | 179 | /* Another device. */ | 
|  | 180 | std::filesystem::create_directories("def/device"); | 
|  | 181 |  | 
|  | 182 | /* Create a dummy eMMC device. */ | 
|  | 183 | std::filesystem::create_directories("mmcblk0/device"); | 
|  | 184 | const std::string typeFileName("mmcblk0/device/type"); | 
|  | 185 | std::ofstream typeFile(typeFileName, std::ios::out | std::ios::trunc); | 
|  | 186 | typeFile << "MMC"; | 
|  | 187 | typeFile.close(); | 
|  | 188 |  | 
|  | 189 | /* Look for the device file. */ | 
|  | 190 | std::filesystem::path deviceFile, sysfsDir; | 
|  | 191 | std::string luksName, locationCode; | 
|  | 192 | auto result = estoraged::util::findDevice(data, | 
|  | 193 | std::filesystem::path("./")); | 
|  | 194 | EXPECT_TRUE(result.has_value()); | 
|  | 195 |  | 
|  | 196 | /* Validate the results. */ | 
|  | 197 | EXPECT_EQ("/dev/mmcblk0", result->deviceFile.string()); | 
|  | 198 | EXPECT_EQ("./mmcblk0/device", result->sysfsDir.string()); | 
|  | 199 | EXPECT_EQ("luks-mmcblk0", result->luksName); | 
|  | 200 | EXPECT_EQ("U102020", result->locationCode); | 
|  | 201 | EXPECT_EQ(5566, result->eraseMaxGeometry); | 
|  | 202 | EXPECT_EQ(1234, result->eraseMinGeometry); | 
| John Wedig | d7be42b | 2024-01-19 16:07:19 -0800 | [diff] [blame^] | 203 | EXPECT_EQ("SSD", result->driveType); | 
| John Wedig | d32b966 | 2022-04-13 18:12:25 -0700 | [diff] [blame] | 204 |  | 
|  | 205 | /* Delete the dummy files. */ | 
| John Edward Broadbent | 9e63982 | 2022-06-10 10:16:05 -0700 | [diff] [blame] | 206 | EXPECT_EQ(3U, std::filesystem::remove_all("mmcblk0")); | 
|  | 207 | EXPECT_EQ(3U, std::filesystem::remove_all("abc")); | 
|  | 208 | EXPECT_EQ(2U, std::filesystem::remove_all("def")); | 
| John Wedig | d32b966 | 2022-04-13 18:12:25 -0700 | [diff] [blame] | 209 | } | 
|  | 210 |  | 
|  | 211 | /* Test case where the "Type" property doesn't exist. */ | 
|  | 212 | TEST(utilTest, findDeviceNoTypeFail) | 
|  | 213 | { | 
|  | 214 | estoraged::StorageData data; | 
|  | 215 |  | 
|  | 216 | /* Set up the map of properties (with the "Type" property missing). */ | 
|  | 217 | data.emplace(std::string("Name"), estoraged::BasicVariantType("emmc")); | 
|  | 218 |  | 
| John Wedig | f78215f | 2022-06-07 13:39:22 -0700 | [diff] [blame] | 219 | /* Create a dummy device. */ | 
|  | 220 | std::filesystem::create_directories("abc/device"); | 
|  | 221 | const std::string dummyTypeFileName("abc/device/type"); | 
|  | 222 | std::ofstream dummyTypeFile(dummyTypeFileName, | 
|  | 223 | std::ios::out | std::ios::trunc); | 
|  | 224 | dummyTypeFile << "SSD"; | 
|  | 225 | dummyTypeFile.close(); | 
| John Wedig | d32b966 | 2022-04-13 18:12:25 -0700 | [diff] [blame] | 226 |  | 
| John Wedig | f78215f | 2022-06-07 13:39:22 -0700 | [diff] [blame] | 227 | /* Another device. */ | 
|  | 228 | std::filesystem::create_directories("def/device"); | 
|  | 229 |  | 
|  | 230 | /* Create a dummy eMMC device. */ | 
|  | 231 | std::filesystem::create_directories("mmcblk0/device"); | 
|  | 232 | const std::string typeFileName("mmcblk0/device/type"); | 
|  | 233 | std::ofstream typeFile(typeFileName, std::ios::out | std::ios::trunc); | 
|  | 234 | typeFile << "MMC"; | 
|  | 235 | typeFile.close(); | 
| John Wedig | d32b966 | 2022-04-13 18:12:25 -0700 | [diff] [blame] | 236 |  | 
|  | 237 | /* Look for the device file. */ | 
| Tom Tung | 043af59 | 2023-11-24 13:37:05 +0800 | [diff] [blame] | 238 | auto result = estoraged::util::findDevice(data, | 
|  | 239 | std::filesystem::path("./")); | 
|  | 240 | EXPECT_FALSE(result.has_value()); | 
| John Wedig | d32b966 | 2022-04-13 18:12:25 -0700 | [diff] [blame] | 241 |  | 
|  | 242 | /* Delete the dummy files. */ | 
| John Edward Broadbent | 9e63982 | 2022-06-10 10:16:05 -0700 | [diff] [blame] | 243 | EXPECT_EQ(3U, std::filesystem::remove_all("mmcblk0")); | 
|  | 244 | EXPECT_EQ(3U, std::filesystem::remove_all("abc")); | 
|  | 245 | EXPECT_EQ(2U, std::filesystem::remove_all("def")); | 
| John Wedig | d32b966 | 2022-04-13 18:12:25 -0700 | [diff] [blame] | 246 | } | 
|  | 247 |  | 
|  | 248 | /* Test case where the device type is not supported. */ | 
|  | 249 | TEST(utilTest, findDeviceUnsupportedTypeFail) | 
|  | 250 | { | 
|  | 251 | estoraged::StorageData data; | 
|  | 252 |  | 
|  | 253 | /* Set up the map of properties (with an unsupported "Type"). */ | 
|  | 254 | data.emplace(std::string("Type"), estoraged::BasicVariantType("NvmeDrive")); | 
|  | 255 | data.emplace(std::string("Name"), | 
|  | 256 | estoraged::BasicVariantType("some_drive")); | 
|  | 257 |  | 
| John Wedig | f78215f | 2022-06-07 13:39:22 -0700 | [diff] [blame] | 258 | /* Create a dummy device. */ | 
|  | 259 | std::filesystem::create_directories("abc/device"); | 
|  | 260 | const std::string dummyTypeFileName("abc/device/type"); | 
|  | 261 | std::ofstream dummyTypeFile(dummyTypeFileName, | 
|  | 262 | std::ios::out | std::ios::trunc); | 
|  | 263 | dummyTypeFile << "SSD"; | 
|  | 264 | dummyTypeFile.close(); | 
| John Wedig | d32b966 | 2022-04-13 18:12:25 -0700 | [diff] [blame] | 265 |  | 
| John Wedig | f78215f | 2022-06-07 13:39:22 -0700 | [diff] [blame] | 266 | /* Another device. */ | 
|  | 267 | std::filesystem::create_directories("def/device"); | 
|  | 268 |  | 
|  | 269 | /* Create a dummy eMMC device. */ | 
|  | 270 | std::filesystem::create_directories("mmcblk0/device"); | 
|  | 271 | const std::string typeFileName("mmcblk0/device/type"); | 
|  | 272 | std::ofstream typeFile(typeFileName, std::ios::out | std::ios::trunc); | 
|  | 273 | typeFile << "MMC"; | 
|  | 274 | typeFile.close(); | 
| John Wedig | d32b966 | 2022-04-13 18:12:25 -0700 | [diff] [blame] | 275 |  | 
|  | 276 | /* Look for the device file. */ | 
| Tom Tung | 043af59 | 2023-11-24 13:37:05 +0800 | [diff] [blame] | 277 | auto result = estoraged::util::findDevice(data, | 
|  | 278 | std::filesystem::path("./")); | 
|  | 279 | EXPECT_FALSE(result.has_value()); | 
| John Wedig | d32b966 | 2022-04-13 18:12:25 -0700 | [diff] [blame] | 280 |  | 
|  | 281 | /* Delete the dummy files. */ | 
| John Edward Broadbent | 9e63982 | 2022-06-10 10:16:05 -0700 | [diff] [blame] | 282 | EXPECT_EQ(3U, std::filesystem::remove_all("mmcblk0")); | 
|  | 283 | EXPECT_EQ(3U, std::filesystem::remove_all("abc")); | 
|  | 284 | EXPECT_EQ(2U, std::filesystem::remove_all("def")); | 
| John Wedig | d32b966 | 2022-04-13 18:12:25 -0700 | [diff] [blame] | 285 | } | 
|  | 286 |  | 
|  | 287 | /* Test case where we can't find the device file. */ | 
|  | 288 | TEST(utilTest, findDeviceNotFoundFail) | 
|  | 289 | { | 
|  | 290 | estoraged::StorageData data; | 
|  | 291 |  | 
|  | 292 | /* Set up the map of properties. */ | 
|  | 293 | data.emplace(std::string("Type"), | 
|  | 294 | estoraged::BasicVariantType("EmmcDevice")); | 
|  | 295 | data.emplace(std::string("Name"), estoraged::BasicVariantType("emmc")); | 
|  | 296 |  | 
| John Wedig | f78215f | 2022-06-07 13:39:22 -0700 | [diff] [blame] | 297 | /* Create a dummy device. */ | 
|  | 298 | std::filesystem::create_directories("abc/device"); | 
|  | 299 | const std::string dummyTypeFileName("abc/device/type"); | 
|  | 300 | std::ofstream dummyTypeFile(dummyTypeFileName, | 
|  | 301 | std::ios::out | std::ios::trunc); | 
|  | 302 | dummyTypeFile << "SSD"; | 
|  | 303 | dummyTypeFile.close(); | 
|  | 304 |  | 
|  | 305 | /* Another device. */ | 
|  | 306 | std::filesystem::create_directories("def/device"); | 
| John Wedig | d32b966 | 2022-04-13 18:12:25 -0700 | [diff] [blame] | 307 |  | 
|  | 308 | /* Look for the device file. */ | 
| Tom Tung | 043af59 | 2023-11-24 13:37:05 +0800 | [diff] [blame] | 309 | auto result = estoraged::util::findDevice(data, | 
|  | 310 | std::filesystem::path("./")); | 
|  | 311 | EXPECT_FALSE(result.has_value()); | 
| John Wedig | d32b966 | 2022-04-13 18:12:25 -0700 | [diff] [blame] | 312 |  | 
| John Wedig | f78215f | 2022-06-07 13:39:22 -0700 | [diff] [blame] | 313 | /* Delete the dummy files. */ | 
| John Edward Broadbent | 9e63982 | 2022-06-10 10:16:05 -0700 | [diff] [blame] | 314 | EXPECT_EQ(3U, std::filesystem::remove_all("abc")); | 
|  | 315 | EXPECT_EQ(2U, std::filesystem::remove_all("def")); | 
| John Wedig | d32b966 | 2022-04-13 18:12:25 -0700 | [diff] [blame] | 316 | } | 
|  | 317 |  | 
| John Edward Broadbent | 5d799bb | 2022-03-22 16:14:24 -0700 | [diff] [blame] | 318 | } // namespace estoraged_test |