1 #include "getConfig.hpp" 2 3 #include <boost/container/flat_map.hpp> 4 #include <util.hpp> 5 6 #include <filesystem> 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; 16 using estoraged::util::getPartNumber; 17 using estoraged::util::getSerialNumber; 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); 29 EXPECT_TRUE(std::filesystem::remove(testFileName)); 30 } 31 32 TEST(utilTest, estimatesSame) 33 { 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); 43 EXPECT_TRUE(std::filesystem::remove(testFileName)); 44 } 45 46 TEST(utilTest, estimatesNotAvailable) 47 { 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); 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)); 99 } 100 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")); 110 data.emplace(std::string("LocationCode"), 111 estoraged::BasicVariantType("U102020")); 112 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(); 120 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(); 130 131 /* Look for the device file. */ 132 std::filesystem::path deviceFile, sysfsDir; 133 std::string luksName, locationCode; 134 EXPECT_TRUE(estoraged::util::findDevice(data, std::filesystem::path("./"), 135 deviceFile, sysfsDir, luksName, 136 locationCode)); 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); 142 EXPECT_EQ("U102020", locationCode); 143 144 /* Delete the dummy files. */ 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")); 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 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(); 165 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(); 175 176 /* Look for the device file. */ 177 std::filesystem::path deviceFile, sysfsDir; 178 std::string luksName, locationCode; 179 EXPECT_FALSE(estoraged::util::findDevice(data, std::filesystem::path("./"), 180 deviceFile, sysfsDir, luksName, 181 locationCode)); 182 183 /* Delete the dummy files. */ 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")); 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 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(); 206 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(); 216 217 /* Look for the device file. */ 218 std::filesystem::path deviceFile, sysfsDir; 219 std::string luksName, locationCode; 220 EXPECT_FALSE(estoraged::util::findDevice(data, std::filesystem::path("./"), 221 deviceFile, sysfsDir, luksName, 222 locationCode)); 223 224 /* Delete the dummy files. */ 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")); 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 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"); 250 251 /* Look for the device file. */ 252 std::filesystem::path deviceFile, sysfsDir; 253 std::string luksName, locationCode; 254 EXPECT_FALSE(estoraged::util::findDevice(data, std::filesystem::path("./"), 255 deviceFile, sysfsDir, luksName, 256 locationCode)); 257 258 /* Delete the dummy files. */ 259 EXPECT_EQ(3U, std::filesystem::remove_all("abc")); 260 EXPECT_EQ(2U, std::filesystem::remove_all("def")); 261 } 262 263 } // namespace estoraged_test 264