blob: 5f8dca5f0472b746572b74a9d92e59feee9a8a44 [file] [log] [blame]
John Wedigd32b9662022-04-13 18:12:25 -07001#include "getConfig.hpp"
2
John Wedigd32b9662022-04-13 18:12:25 -07003#include <boost/container/flat_map.hpp>
John Edward Broadbent5d799bb2022-03-22 16:14:24 -07004#include <util.hpp>
5
John Wedigd32b9662022-04-13 18:12:25 -07006#include <filesystem>
John Edward Broadbent5d799bb2022-03-22 16:14:24 -07007#include <fstream>
8
9#include <gmock/gmock-matchers.h>
10#include <gmock/gmock.h>
11#include <gtest/gtest.h>
12
13namespace estoraged_test
14{
15using estoraged::util::findPredictedMediaLifeLeftPercent;
John Wedigb4838302022-07-22 13:51:16 -070016using estoraged::util::getPartNumber;
17using estoraged::util::getSerialNumber;
John Edward Broadbent5d799bb2022-03-22 16:14:24 -070018
19TEST(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 Wedigb4838302022-07-22 13:51:16 -070029 EXPECT_TRUE(std::filesystem::remove(testFileName));
John Edward Broadbent5d799bb2022-03-22 16:14:24 -070030}
31
32TEST(utilTest, estimatesSame)
33{
John Edward Broadbent5d799bb2022-03-22 16:14:24 -070034 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 Wedigb4838302022-07-22 13:51:16 -070043 EXPECT_TRUE(std::filesystem::remove(testFileName));
John Edward Broadbent5d799bb2022-03-22 16:14:24 -070044}
45
46TEST(utilTest, estimatesNotAvailable)
47{
John Edward Broadbent5d799bb2022-03-22 16:14:24 -070048 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 Wedigb4838302022-07-22 13:51:16 -070056 EXPECT_TRUE(std::filesystem::remove(testFileName));
57}
58
59TEST(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
67TEST(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
80TEST(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
88TEST(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 Broadbent5d799bb2022-03-22 16:14:24 -070099}
100
John Wedigd32b9662022-04-13 18:12:25 -0700101/* Test case where we successfully find the device file. */
102TEST(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 Kapoor19825052023-05-27 01:52:23 +0000110 data.emplace(std::string("LocationCode"),
111 estoraged::BasicVariantType("U102020"));
John Wedigd32b9662022-04-13 18:12:25 -0700112
John Wedigf78215f2022-06-07 13:39:22 -0700113 /* 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 Wedigd32b9662022-04-13 18:12:25 -0700120
John Wedigf78215f2022-06-07 13:39:22 -0700121 /* 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 Wedigd32b9662022-04-13 18:12:25 -0700130
131 /* Look for the device file. */
132 std::filesystem::path deviceFile, sysfsDir;
Rahul Kapoor19825052023-05-27 01:52:23 +0000133 std::string luksName, locationCode;
John Wedigd32b9662022-04-13 18:12:25 -0700134 EXPECT_TRUE(estoraged::util::findDevice(data, std::filesystem::path("./"),
Rahul Kapoor19825052023-05-27 01:52:23 +0000135 deviceFile, sysfsDir, luksName,
136 locationCode));
John Wedigd32b9662022-04-13 18:12:25 -0700137
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 Kapoor19825052023-05-27 01:52:23 +0000142 EXPECT_EQ("U102020", locationCode);
John Wedigd32b9662022-04-13 18:12:25 -0700143
144 /* Delete the dummy files. */
John Edward Broadbent9e639822022-06-10 10:16:05 -0700145 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 Wedigd32b9662022-04-13 18:12:25 -0700148}
149
150/* Test case where the "Type" property doesn't exist. */
151TEST(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 Wedigf78215f2022-06-07 13:39:22 -0700158 /* 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 Wedigd32b9662022-04-13 18:12:25 -0700165
John Wedigf78215f2022-06-07 13:39:22 -0700166 /* 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 Wedigd32b9662022-04-13 18:12:25 -0700175
176 /* Look for the device file. */
177 std::filesystem::path deviceFile, sysfsDir;
Rahul Kapoor19825052023-05-27 01:52:23 +0000178 std::string luksName, locationCode;
John Wedigd32b9662022-04-13 18:12:25 -0700179 EXPECT_FALSE(estoraged::util::findDevice(data, std::filesystem::path("./"),
Rahul Kapoor19825052023-05-27 01:52:23 +0000180 deviceFile, sysfsDir, luksName,
181 locationCode));
John Wedigd32b9662022-04-13 18:12:25 -0700182
183 /* Delete the dummy files. */
John Edward Broadbent9e639822022-06-10 10:16:05 -0700184 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 Wedigd32b9662022-04-13 18:12:25 -0700187}
188
189/* Test case where the device type is not supported. */
190TEST(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 Wedigf78215f2022-06-07 13:39:22 -0700199 /* 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 Wedigd32b9662022-04-13 18:12:25 -0700206
John Wedigf78215f2022-06-07 13:39:22 -0700207 /* 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 Wedigd32b9662022-04-13 18:12:25 -0700216
217 /* Look for the device file. */
218 std::filesystem::path deviceFile, sysfsDir;
Rahul Kapoor19825052023-05-27 01:52:23 +0000219 std::string luksName, locationCode;
John Wedigd32b9662022-04-13 18:12:25 -0700220 EXPECT_FALSE(estoraged::util::findDevice(data, std::filesystem::path("./"),
Rahul Kapoor19825052023-05-27 01:52:23 +0000221 deviceFile, sysfsDir, luksName,
222 locationCode));
John Wedigd32b9662022-04-13 18:12:25 -0700223
224 /* Delete the dummy files. */
John Edward Broadbent9e639822022-06-10 10:16:05 -0700225 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 Wedigd32b9662022-04-13 18:12:25 -0700228}
229
230/* Test case where we can't find the device file. */
231TEST(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 Wedigf78215f2022-06-07 13:39:22 -0700240 /* 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 Wedigd32b9662022-04-13 18:12:25 -0700250
251 /* Look for the device file. */
252 std::filesystem::path deviceFile, sysfsDir;
Rahul Kapoor19825052023-05-27 01:52:23 +0000253 std::string luksName, locationCode;
John Wedigd32b9662022-04-13 18:12:25 -0700254 EXPECT_FALSE(estoraged::util::findDevice(data, std::filesystem::path("./"),
Rahul Kapoor19825052023-05-27 01:52:23 +0000255 deviceFile, sysfsDir, luksName,
256 locationCode));
John Wedigd32b9662022-04-13 18:12:25 -0700257
John Wedigf78215f2022-06-07 13:39:22 -0700258 /* Delete the dummy files. */
John Edward Broadbent9e639822022-06-10 10:16:05 -0700259 EXPECT_EQ(3U, std::filesystem::remove_all("abc"));
260 EXPECT_EQ(2U, std::filesystem::remove_all("def"));
John Wedigd32b9662022-04-13 18:12:25 -0700261}
262
John Edward Broadbent5d799bb2022-03-22 16:14:24 -0700263} // namespace estoraged_test