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; |
| 16 | |
| 17 | TEST(utilTest, passFindPredictedMediaLife) |
| 18 | { |
| 19 | std::string prefixName = "."; |
| 20 | std::string testFileName = prefixName + "/life_time"; |
| 21 | std::ofstream testFile; |
| 22 | testFile.open(testFileName, |
| 23 | std::ios::out | std::ios::binary | std::ios::trunc); |
| 24 | testFile << "0x07 0x04"; |
| 25 | testFile.close(); |
| 26 | EXPECT_EQ(findPredictedMediaLifeLeftPercent(prefixName), 40); |
| 27 | } |
| 28 | |
| 29 | TEST(utilTest, estimatesSame) |
| 30 | { |
| 31 | |
| 32 | std::string prefixName = "."; |
| 33 | std::string testFileName = prefixName + "/life_time"; |
| 34 | std::ofstream testFile; |
| 35 | testFile.open(testFileName, |
| 36 | std::ios::out | std::ios::binary | std::ios::trunc); |
| 37 | testFile << "0x04 0x04"; |
| 38 | testFile.close(); |
| 39 | |
| 40 | EXPECT_EQ(findPredictedMediaLifeLeftPercent(prefixName), 70); |
| 41 | } |
| 42 | |
| 43 | TEST(utilTest, estimatesNotAvailable) |
| 44 | { |
| 45 | |
| 46 | std::string prefixName = "."; |
| 47 | std::string testFileName = prefixName + "/life_time"; |
| 48 | std::ofstream testFile; |
| 49 | testFile.open(testFileName, |
| 50 | std::ios::out | std::ios::binary | std::ios::trunc); |
| 51 | testFile.close(); |
| 52 | |
| 53 | EXPECT_EQ(findPredictedMediaLifeLeftPercent(prefixName), 255); |
| 54 | } |
| 55 | |
John Wedig | d32b966 | 2022-04-13 18:12:25 -0700 | [diff] [blame] | 56 | /* Test case where we successfully find the device file. */ |
| 57 | TEST(utilTest, findDevicePass) |
| 58 | { |
| 59 | estoraged::StorageData data; |
| 60 | |
| 61 | /* Set up the map of properties. */ |
| 62 | data.emplace(std::string("Type"), |
| 63 | estoraged::BasicVariantType("EmmcDevice")); |
| 64 | data.emplace(std::string("Name"), estoraged::BasicVariantType("emmc")); |
| 65 | |
John Wedig | f78215f | 2022-06-07 13:39:22 -0700 | [diff] [blame] | 66 | /* Create a dummy device. */ |
| 67 | std::filesystem::create_directories("abc/device"); |
| 68 | const std::string dummyTypeFileName("abc/device/type"); |
| 69 | std::ofstream dummyTypeFile(dummyTypeFileName, |
| 70 | std::ios::out | std::ios::trunc); |
| 71 | dummyTypeFile << "SSD"; |
| 72 | dummyTypeFile.close(); |
John Wedig | d32b966 | 2022-04-13 18:12:25 -0700 | [diff] [blame] | 73 | |
John Wedig | f78215f | 2022-06-07 13:39:22 -0700 | [diff] [blame] | 74 | /* Another device. */ |
| 75 | std::filesystem::create_directories("def/device"); |
| 76 | |
| 77 | /* Create a dummy eMMC device. */ |
| 78 | std::filesystem::create_directories("mmcblk0/device"); |
| 79 | const std::string typeFileName("mmcblk0/device/type"); |
| 80 | std::ofstream typeFile(typeFileName, std::ios::out | std::ios::trunc); |
| 81 | typeFile << "MMC"; |
| 82 | typeFile.close(); |
John Wedig | d32b966 | 2022-04-13 18:12:25 -0700 | [diff] [blame] | 83 | |
| 84 | /* Look for the device file. */ |
| 85 | std::filesystem::path deviceFile, sysfsDir; |
| 86 | std::string luksName; |
| 87 | EXPECT_TRUE(estoraged::util::findDevice(data, std::filesystem::path("./"), |
| 88 | deviceFile, sysfsDir, luksName)); |
| 89 | |
| 90 | /* Validate the results. */ |
| 91 | EXPECT_EQ("/dev/mmcblk0", deviceFile.string()); |
| 92 | EXPECT_EQ("./mmcblk0/device", sysfsDir.string()); |
| 93 | EXPECT_EQ("luks-mmcblk0", luksName); |
| 94 | |
| 95 | /* Delete the dummy files. */ |
John Wedig | f78215f | 2022-06-07 13:39:22 -0700 | [diff] [blame] | 96 | EXPECT_EQ(3, std::filesystem::remove_all("mmcblk0")); |
| 97 | EXPECT_EQ(3, std::filesystem::remove_all("abc")); |
| 98 | EXPECT_EQ(2, std::filesystem::remove_all("def")); |
John Wedig | d32b966 | 2022-04-13 18:12:25 -0700 | [diff] [blame] | 99 | } |
| 100 | |
| 101 | /* Test case where the "Type" property doesn't exist. */ |
| 102 | TEST(utilTest, findDeviceNoTypeFail) |
| 103 | { |
| 104 | estoraged::StorageData data; |
| 105 | |
| 106 | /* Set up the map of properties (with the "Type" property missing). */ |
| 107 | data.emplace(std::string("Name"), estoraged::BasicVariantType("emmc")); |
| 108 | |
John Wedig | f78215f | 2022-06-07 13:39:22 -0700 | [diff] [blame] | 109 | /* Create a dummy device. */ |
| 110 | std::filesystem::create_directories("abc/device"); |
| 111 | const std::string dummyTypeFileName("abc/device/type"); |
| 112 | std::ofstream dummyTypeFile(dummyTypeFileName, |
| 113 | std::ios::out | std::ios::trunc); |
| 114 | dummyTypeFile << "SSD"; |
| 115 | dummyTypeFile.close(); |
John Wedig | d32b966 | 2022-04-13 18:12:25 -0700 | [diff] [blame] | 116 | |
John Wedig | f78215f | 2022-06-07 13:39:22 -0700 | [diff] [blame] | 117 | /* Another device. */ |
| 118 | std::filesystem::create_directories("def/device"); |
| 119 | |
| 120 | /* Create a dummy eMMC device. */ |
| 121 | std::filesystem::create_directories("mmcblk0/device"); |
| 122 | const std::string typeFileName("mmcblk0/device/type"); |
| 123 | std::ofstream typeFile(typeFileName, std::ios::out | std::ios::trunc); |
| 124 | typeFile << "MMC"; |
| 125 | typeFile.close(); |
John Wedig | d32b966 | 2022-04-13 18:12:25 -0700 | [diff] [blame] | 126 | |
| 127 | /* Look for the device file. */ |
| 128 | std::filesystem::path deviceFile, sysfsDir; |
| 129 | std::string luksName; |
| 130 | EXPECT_FALSE(estoraged::util::findDevice(data, std::filesystem::path("./"), |
| 131 | deviceFile, sysfsDir, luksName)); |
| 132 | |
| 133 | /* Delete the dummy files. */ |
John Wedig | f78215f | 2022-06-07 13:39:22 -0700 | [diff] [blame] | 134 | EXPECT_EQ(3, std::filesystem::remove_all("mmcblk0")); |
| 135 | EXPECT_EQ(3, std::filesystem::remove_all("abc")); |
| 136 | EXPECT_EQ(2, std::filesystem::remove_all("def")); |
John Wedig | d32b966 | 2022-04-13 18:12:25 -0700 | [diff] [blame] | 137 | } |
| 138 | |
| 139 | /* Test case where the device type is not supported. */ |
| 140 | TEST(utilTest, findDeviceUnsupportedTypeFail) |
| 141 | { |
| 142 | estoraged::StorageData data; |
| 143 | |
| 144 | /* Set up the map of properties (with an unsupported "Type"). */ |
| 145 | data.emplace(std::string("Type"), estoraged::BasicVariantType("NvmeDrive")); |
| 146 | data.emplace(std::string("Name"), |
| 147 | estoraged::BasicVariantType("some_drive")); |
| 148 | |
John Wedig | f78215f | 2022-06-07 13:39:22 -0700 | [diff] [blame] | 149 | /* Create a dummy device. */ |
| 150 | std::filesystem::create_directories("abc/device"); |
| 151 | const std::string dummyTypeFileName("abc/device/type"); |
| 152 | std::ofstream dummyTypeFile(dummyTypeFileName, |
| 153 | std::ios::out | std::ios::trunc); |
| 154 | dummyTypeFile << "SSD"; |
| 155 | dummyTypeFile.close(); |
John Wedig | d32b966 | 2022-04-13 18:12:25 -0700 | [diff] [blame] | 156 | |
John Wedig | f78215f | 2022-06-07 13:39:22 -0700 | [diff] [blame] | 157 | /* Another device. */ |
| 158 | std::filesystem::create_directories("def/device"); |
| 159 | |
| 160 | /* Create a dummy eMMC device. */ |
| 161 | std::filesystem::create_directories("mmcblk0/device"); |
| 162 | const std::string typeFileName("mmcblk0/device/type"); |
| 163 | std::ofstream typeFile(typeFileName, std::ios::out | std::ios::trunc); |
| 164 | typeFile << "MMC"; |
| 165 | typeFile.close(); |
John Wedig | d32b966 | 2022-04-13 18:12:25 -0700 | [diff] [blame] | 166 | |
| 167 | /* Look for the device file. */ |
| 168 | std::filesystem::path deviceFile, sysfsDir; |
| 169 | std::string luksName; |
| 170 | EXPECT_FALSE(estoraged::util::findDevice(data, std::filesystem::path("./"), |
| 171 | deviceFile, sysfsDir, luksName)); |
| 172 | |
| 173 | /* Delete the dummy files. */ |
John Wedig | f78215f | 2022-06-07 13:39:22 -0700 | [diff] [blame] | 174 | EXPECT_EQ(3, std::filesystem::remove_all("mmcblk0")); |
| 175 | EXPECT_EQ(3, std::filesystem::remove_all("abc")); |
| 176 | EXPECT_EQ(2, std::filesystem::remove_all("def")); |
John Wedig | d32b966 | 2022-04-13 18:12:25 -0700 | [diff] [blame] | 177 | } |
| 178 | |
| 179 | /* Test case where we can't find the device file. */ |
| 180 | TEST(utilTest, findDeviceNotFoundFail) |
| 181 | { |
| 182 | estoraged::StorageData data; |
| 183 | |
| 184 | /* Set up the map of properties. */ |
| 185 | data.emplace(std::string("Type"), |
| 186 | estoraged::BasicVariantType("EmmcDevice")); |
| 187 | data.emplace(std::string("Name"), estoraged::BasicVariantType("emmc")); |
| 188 | |
John Wedig | f78215f | 2022-06-07 13:39:22 -0700 | [diff] [blame] | 189 | /* Create a dummy device. */ |
| 190 | std::filesystem::create_directories("abc/device"); |
| 191 | const std::string dummyTypeFileName("abc/device/type"); |
| 192 | std::ofstream dummyTypeFile(dummyTypeFileName, |
| 193 | std::ios::out | std::ios::trunc); |
| 194 | dummyTypeFile << "SSD"; |
| 195 | dummyTypeFile.close(); |
| 196 | |
| 197 | /* Another device. */ |
| 198 | std::filesystem::create_directories("def/device"); |
John Wedig | d32b966 | 2022-04-13 18:12:25 -0700 | [diff] [blame] | 199 | |
| 200 | /* Look for the device file. */ |
| 201 | std::filesystem::path deviceFile, sysfsDir; |
| 202 | std::string luksName; |
| 203 | EXPECT_FALSE(estoraged::util::findDevice(data, std::filesystem::path("./"), |
| 204 | deviceFile, sysfsDir, luksName)); |
| 205 | |
John Wedig | f78215f | 2022-06-07 13:39:22 -0700 | [diff] [blame] | 206 | /* Delete the dummy files. */ |
| 207 | EXPECT_EQ(3, std::filesystem::remove_all("abc")); |
| 208 | EXPECT_EQ(2, std::filesystem::remove_all("def")); |
John Wedig | d32b966 | 2022-04-13 18:12:25 -0700 | [diff] [blame] | 209 | } |
| 210 | |
John Edward Broadbent | 5d799bb | 2022-03-22 16:14:24 -0700 | [diff] [blame] | 211 | } // namespace estoraged_test |