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