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 111 /* Create a dummy device. */ 112 std::filesystem::create_directories("abc/device"); 113 const std::string dummyTypeFileName("abc/device/type"); 114 std::ofstream dummyTypeFile(dummyTypeFileName, 115 std::ios::out | std::ios::trunc); 116 dummyTypeFile << "SSD"; 117 dummyTypeFile.close(); 118 119 /* Another device. */ 120 std::filesystem::create_directories("def/device"); 121 122 /* Create a dummy eMMC device. */ 123 std::filesystem::create_directories("mmcblk0/device"); 124 const std::string typeFileName("mmcblk0/device/type"); 125 std::ofstream typeFile(typeFileName, std::ios::out | std::ios::trunc); 126 typeFile << "MMC"; 127 typeFile.close(); 128 129 /* Look for the device file. */ 130 std::filesystem::path deviceFile, sysfsDir; 131 std::string luksName; 132 EXPECT_TRUE(estoraged::util::findDevice(data, std::filesystem::path("./"), 133 deviceFile, sysfsDir, luksName)); 134 135 /* Validate the results. */ 136 EXPECT_EQ("/dev/mmcblk0", deviceFile.string()); 137 EXPECT_EQ("./mmcblk0/device", sysfsDir.string()); 138 EXPECT_EQ("luks-mmcblk0", luksName); 139 140 /* Delete the dummy files. */ 141 EXPECT_EQ(3U, std::filesystem::remove_all("mmcblk0")); 142 EXPECT_EQ(3U, std::filesystem::remove_all("abc")); 143 EXPECT_EQ(2U, std::filesystem::remove_all("def")); 144 } 145 146 /* Test case where the "Type" property doesn't exist. */ 147 TEST(utilTest, findDeviceNoTypeFail) 148 { 149 estoraged::StorageData data; 150 151 /* Set up the map of properties (with the "Type" property missing). */ 152 data.emplace(std::string("Name"), estoraged::BasicVariantType("emmc")); 153 154 /* Create a dummy device. */ 155 std::filesystem::create_directories("abc/device"); 156 const std::string dummyTypeFileName("abc/device/type"); 157 std::ofstream dummyTypeFile(dummyTypeFileName, 158 std::ios::out | std::ios::trunc); 159 dummyTypeFile << "SSD"; 160 dummyTypeFile.close(); 161 162 /* Another device. */ 163 std::filesystem::create_directories("def/device"); 164 165 /* Create a dummy eMMC device. */ 166 std::filesystem::create_directories("mmcblk0/device"); 167 const std::string typeFileName("mmcblk0/device/type"); 168 std::ofstream typeFile(typeFileName, std::ios::out | std::ios::trunc); 169 typeFile << "MMC"; 170 typeFile.close(); 171 172 /* Look for the device file. */ 173 std::filesystem::path deviceFile, sysfsDir; 174 std::string luksName; 175 EXPECT_FALSE(estoraged::util::findDevice(data, std::filesystem::path("./"), 176 deviceFile, sysfsDir, luksName)); 177 178 /* Delete the dummy files. */ 179 EXPECT_EQ(3U, std::filesystem::remove_all("mmcblk0")); 180 EXPECT_EQ(3U, std::filesystem::remove_all("abc")); 181 EXPECT_EQ(2U, std::filesystem::remove_all("def")); 182 } 183 184 /* Test case where the device type is not supported. */ 185 TEST(utilTest, findDeviceUnsupportedTypeFail) 186 { 187 estoraged::StorageData data; 188 189 /* Set up the map of properties (with an unsupported "Type"). */ 190 data.emplace(std::string("Type"), estoraged::BasicVariantType("NvmeDrive")); 191 data.emplace(std::string("Name"), 192 estoraged::BasicVariantType("some_drive")); 193 194 /* Create a dummy device. */ 195 std::filesystem::create_directories("abc/device"); 196 const std::string dummyTypeFileName("abc/device/type"); 197 std::ofstream dummyTypeFile(dummyTypeFileName, 198 std::ios::out | std::ios::trunc); 199 dummyTypeFile << "SSD"; 200 dummyTypeFile.close(); 201 202 /* Another device. */ 203 std::filesystem::create_directories("def/device"); 204 205 /* Create a dummy eMMC device. */ 206 std::filesystem::create_directories("mmcblk0/device"); 207 const std::string typeFileName("mmcblk0/device/type"); 208 std::ofstream typeFile(typeFileName, std::ios::out | std::ios::trunc); 209 typeFile << "MMC"; 210 typeFile.close(); 211 212 /* Look for the device file. */ 213 std::filesystem::path deviceFile, sysfsDir; 214 std::string luksName; 215 EXPECT_FALSE(estoraged::util::findDevice(data, std::filesystem::path("./"), 216 deviceFile, sysfsDir, luksName)); 217 218 /* Delete the dummy files. */ 219 EXPECT_EQ(3U, std::filesystem::remove_all("mmcblk0")); 220 EXPECT_EQ(3U, std::filesystem::remove_all("abc")); 221 EXPECT_EQ(2U, std::filesystem::remove_all("def")); 222 } 223 224 /* Test case where we can't find the device file. */ 225 TEST(utilTest, findDeviceNotFoundFail) 226 { 227 estoraged::StorageData data; 228 229 /* Set up the map of properties. */ 230 data.emplace(std::string("Type"), 231 estoraged::BasicVariantType("EmmcDevice")); 232 data.emplace(std::string("Name"), estoraged::BasicVariantType("emmc")); 233 234 /* Create a dummy device. */ 235 std::filesystem::create_directories("abc/device"); 236 const std::string dummyTypeFileName("abc/device/type"); 237 std::ofstream dummyTypeFile(dummyTypeFileName, 238 std::ios::out | std::ios::trunc); 239 dummyTypeFile << "SSD"; 240 dummyTypeFile.close(); 241 242 /* Another device. */ 243 std::filesystem::create_directories("def/device"); 244 245 /* Look for the device file. */ 246 std::filesystem::path deviceFile, sysfsDir; 247 std::string luksName; 248 EXPECT_FALSE(estoraged::util::findDevice(data, std::filesystem::path("./"), 249 deviceFile, sysfsDir, luksName)); 250 251 /* Delete the dummy files. */ 252 EXPECT_EQ(3U, std::filesystem::remove_all("abc")); 253 EXPECT_EQ(2U, std::filesystem::remove_all("def")); 254 } 255 256 } // namespace estoraged_test 257