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