blob: 708454dc1aca064c6d7b62492b8885ce3e11d01b [file] [log] [blame]
Tom Tung043af592023-11-24 13:37:05 +08001#include "estoraged_conf.hpp"
John Wedigd32b9662022-04-13 18:12:25 -07002#include "getConfig.hpp"
3
John Wedigd32b9662022-04-13 18:12:25 -07004#include <boost/container/flat_map.hpp>
John Edward Broadbent5d799bb2022-03-22 16:14:24 -07005#include <util.hpp>
6
John Wedigd32b9662022-04-13 18:12:25 -07007#include <filesystem>
John Edward Broadbent5d799bb2022-03-22 16:14:24 -07008#include <fstream>
9
10#include <gmock/gmock-matchers.h>
11#include <gmock/gmock.h>
12#include <gtest/gtest.h>
13
14namespace estoraged_test
15{
16using estoraged::util::findPredictedMediaLifeLeftPercent;
John Wedigb4838302022-07-22 13:51:16 -070017using estoraged::util::getPartNumber;
18using estoraged::util::getSerialNumber;
John Edward Broadbent5d799bb2022-03-22 16:14:24 -070019
20TEST(utilTest, passFindPredictedMediaLife)
21{
22 std::string prefixName = ".";
23 std::string testFileName = prefixName + "/life_time";
24 std::ofstream testFile;
25 testFile.open(testFileName,
26 std::ios::out | std::ios::binary | std::ios::trunc);
27 testFile << "0x07 0x04";
28 testFile.close();
29 EXPECT_EQ(findPredictedMediaLifeLeftPercent(prefixName), 40);
John Wedigb4838302022-07-22 13:51:16 -070030 EXPECT_TRUE(std::filesystem::remove(testFileName));
John Edward Broadbent5d799bb2022-03-22 16:14:24 -070031}
32
33TEST(utilTest, estimatesSame)
34{
John Edward Broadbent5d799bb2022-03-22 16:14:24 -070035 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 Wedigb4838302022-07-22 13:51:16 -070044 EXPECT_TRUE(std::filesystem::remove(testFileName));
John Edward Broadbent5d799bb2022-03-22 16:14:24 -070045}
46
47TEST(utilTest, estimatesNotAvailable)
48{
John Edward Broadbent5d799bb2022-03-22 16:14:24 -070049 std::string prefixName = ".";
50 std::string testFileName = prefixName + "/life_time";
51 std::ofstream testFile;
52 testFile.open(testFileName,
53 std::ios::out | std::ios::binary | std::ios::trunc);
54 testFile.close();
55
56 EXPECT_EQ(findPredictedMediaLifeLeftPercent(prefixName), 255);
John Wedigb4838302022-07-22 13:51:16 -070057 EXPECT_TRUE(std::filesystem::remove(testFileName));
58}
59
60TEST(utilTest, getPartNumberFail)
61{
62 std::string prefixName = ".";
63 std::string testFileName = prefixName + "/name";
64 /* The part name file won't exist for this test. */
65 EXPECT_EQ(getPartNumber(prefixName), "unknown");
66}
67
68TEST(utilTest, getPartNumberPass)
69{
70 std::string prefixName = ".";
71 std::string testFileName = prefixName + "/name";
72 std::ofstream testFile;
73 testFile.open(testFileName,
74 std::ios::out | std::ios::binary | std::ios::trunc);
75 testFile << "ABCD1234";
76 testFile.close();
77 EXPECT_EQ(getPartNumber(prefixName), "ABCD1234");
78 EXPECT_TRUE(std::filesystem::remove(testFileName));
79}
80
81TEST(utilTest, getSerialNumberFail)
82{
83 std::string prefixName = ".";
84 std::string testFileName = prefixName + "/serial";
85 /* The serial number file won't exist for this test. */
86 EXPECT_EQ(getSerialNumber(prefixName), "unknown");
87}
88
89TEST(utilTest, getSerialNumberPass)
90{
91 std::string prefixName = ".";
92 std::string testFileName = prefixName + "/serial";
93 std::ofstream testFile;
94 testFile.open(testFileName,
95 std::ios::out | std::ios::binary | std::ios::trunc);
96 testFile << "0x12345678";
97 testFile.close();
98 EXPECT_EQ(getSerialNumber(prefixName), "0x12345678");
99 EXPECT_TRUE(std::filesystem::remove(testFileName));
John Edward Broadbent5d799bb2022-03-22 16:14:24 -0700100}
101
John Wedigd32b9662022-04-13 18:12:25 -0700102/* Test case where we successfully find the device file. */
103TEST(utilTest, findDevicePass)
104{
105 estoraged::StorageData data;
106
107 /* Set up the map of properties. */
108 data.emplace(std::string("Type"),
109 estoraged::BasicVariantType("EmmcDevice"));
110 data.emplace(std::string("Name"), estoraged::BasicVariantType("emmc"));
Rahul Kapoor19825052023-05-27 01:52:23 +0000111 data.emplace(std::string("LocationCode"),
112 estoraged::BasicVariantType("U102020"));
John Wedigd32b9662022-04-13 18:12:25 -0700113
John Wedigf78215f2022-06-07 13:39:22 -0700114 /* Create a dummy device. */
115 std::filesystem::create_directories("abc/device");
116 const std::string dummyTypeFileName("abc/device/type");
117 std::ofstream dummyTypeFile(dummyTypeFileName,
118 std::ios::out | std::ios::trunc);
119 dummyTypeFile << "SSD";
120 dummyTypeFile.close();
John Wedigd32b9662022-04-13 18:12:25 -0700121
John Wedigf78215f2022-06-07 13:39:22 -0700122 /* Another device. */
123 std::filesystem::create_directories("def/device");
124
125 /* Create a dummy eMMC device. */
126 std::filesystem::create_directories("mmcblk0/device");
127 const std::string typeFileName("mmcblk0/device/type");
128 std::ofstream typeFile(typeFileName, std::ios::out | std::ios::trunc);
129 typeFile << "MMC";
130 typeFile.close();
John Wedigd32b9662022-04-13 18:12:25 -0700131
132 /* Look for the device file. */
133 std::filesystem::path deviceFile, sysfsDir;
Rahul Kapoor19825052023-05-27 01:52:23 +0000134 std::string luksName, locationCode;
Tom Tung043af592023-11-24 13:37:05 +0800135 auto result = estoraged::util::findDevice(data,
136 std::filesystem::path("./"));
137 EXPECT_TRUE(result.has_value());
John Wedigd32b9662022-04-13 18:12:25 -0700138
139 /* Validate the results. */
Tom Tung043af592023-11-24 13:37:05 +0800140 EXPECT_EQ("/dev/mmcblk0", result->deviceFile.string());
141 EXPECT_EQ("./mmcblk0/device", result->sysfsDir.string());
142 EXPECT_EQ("luks-mmcblk0", result->luksName);
143 EXPECT_EQ("U102020", result->locationCode);
144 EXPECT_EQ(ERASE_MAX_GEOMETRY, result->eraseMaxGeometry);
145 EXPECT_EQ(ERASE_MIN_GEOMETRY, result->eraseMinGeometry);
146
147 /* Delete the dummy files. */
148 EXPECT_EQ(3U, std::filesystem::remove_all("mmcblk0"));
149 EXPECT_EQ(3U, std::filesystem::remove_all("abc"));
150 EXPECT_EQ(2U, std::filesystem::remove_all("def"));
151}
152
153/* Test case where we successfully find the device file with updating the
154 * min/max geometry. */
155TEST(utilTest, findDeviceWithMaxAndMinGeometryPass)
156{
157 estoraged::StorageData data;
158
159 /* Set up the map of properties. */
160 data.emplace(std::string("Type"),
161 estoraged::BasicVariantType("EmmcDevice"));
162 data.emplace(std::string("Name"), estoraged::BasicVariantType("emmc"));
163 data.emplace(std::string("LocationCode"),
164 estoraged::BasicVariantType("U102020"));
165 data.emplace(std::string("EraseMaxGeometry"),
166 estoraged::BasicVariantType((uint64_t)5566));
167 data.emplace(std::string("EraseMinGeometry"),
168 estoraged::BasicVariantType((uint64_t)1234));
169
170 /* Create a dummy device. */
171 std::filesystem::create_directories("abc/device");
172 const std::string dummyTypeFileName("abc/device/type");
173 std::ofstream dummyTypeFile(dummyTypeFileName,
174 std::ios::out | std::ios::trunc);
175 dummyTypeFile << "SSD";
176 dummyTypeFile.close();
177
178 /* Another device. */
179 std::filesystem::create_directories("def/device");
180
181 /* Create a dummy eMMC device. */
182 std::filesystem::create_directories("mmcblk0/device");
183 const std::string typeFileName("mmcblk0/device/type");
184 std::ofstream typeFile(typeFileName, std::ios::out | std::ios::trunc);
185 typeFile << "MMC";
186 typeFile.close();
187
188 /* Look for the device file. */
189 std::filesystem::path deviceFile, sysfsDir;
190 std::string luksName, locationCode;
191 auto result = estoraged::util::findDevice(data,
192 std::filesystem::path("./"));
193 EXPECT_TRUE(result.has_value());
194
195 /* Validate the results. */
196 EXPECT_EQ("/dev/mmcblk0", result->deviceFile.string());
197 EXPECT_EQ("./mmcblk0/device", result->sysfsDir.string());
198 EXPECT_EQ("luks-mmcblk0", result->luksName);
199 EXPECT_EQ("U102020", result->locationCode);
200 EXPECT_EQ(5566, result->eraseMaxGeometry);
201 EXPECT_EQ(1234, result->eraseMinGeometry);
John Wedigd32b9662022-04-13 18:12:25 -0700202
203 /* Delete the dummy files. */
John Edward Broadbent9e639822022-06-10 10:16:05 -0700204 EXPECT_EQ(3U, std::filesystem::remove_all("mmcblk0"));
205 EXPECT_EQ(3U, std::filesystem::remove_all("abc"));
206 EXPECT_EQ(2U, std::filesystem::remove_all("def"));
John Wedigd32b9662022-04-13 18:12:25 -0700207}
208
209/* Test case where the "Type" property doesn't exist. */
210TEST(utilTest, findDeviceNoTypeFail)
211{
212 estoraged::StorageData data;
213
214 /* Set up the map of properties (with the "Type" property missing). */
215 data.emplace(std::string("Name"), estoraged::BasicVariantType("emmc"));
216
John Wedigf78215f2022-06-07 13:39:22 -0700217 /* Create a dummy device. */
218 std::filesystem::create_directories("abc/device");
219 const std::string dummyTypeFileName("abc/device/type");
220 std::ofstream dummyTypeFile(dummyTypeFileName,
221 std::ios::out | std::ios::trunc);
222 dummyTypeFile << "SSD";
223 dummyTypeFile.close();
John Wedigd32b9662022-04-13 18:12:25 -0700224
John Wedigf78215f2022-06-07 13:39:22 -0700225 /* Another device. */
226 std::filesystem::create_directories("def/device");
227
228 /* Create a dummy eMMC device. */
229 std::filesystem::create_directories("mmcblk0/device");
230 const std::string typeFileName("mmcblk0/device/type");
231 std::ofstream typeFile(typeFileName, std::ios::out | std::ios::trunc);
232 typeFile << "MMC";
233 typeFile.close();
John Wedigd32b9662022-04-13 18:12:25 -0700234
235 /* Look for the device file. */
Tom Tung043af592023-11-24 13:37:05 +0800236 auto result = estoraged::util::findDevice(data,
237 std::filesystem::path("./"));
238 EXPECT_FALSE(result.has_value());
John Wedigd32b9662022-04-13 18:12:25 -0700239
240 /* Delete the dummy files. */
John Edward Broadbent9e639822022-06-10 10:16:05 -0700241 EXPECT_EQ(3U, std::filesystem::remove_all("mmcblk0"));
242 EXPECT_EQ(3U, std::filesystem::remove_all("abc"));
243 EXPECT_EQ(2U, std::filesystem::remove_all("def"));
John Wedigd32b9662022-04-13 18:12:25 -0700244}
245
246/* Test case where the device type is not supported. */
247TEST(utilTest, findDeviceUnsupportedTypeFail)
248{
249 estoraged::StorageData data;
250
251 /* Set up the map of properties (with an unsupported "Type"). */
252 data.emplace(std::string("Type"), estoraged::BasicVariantType("NvmeDrive"));
253 data.emplace(std::string("Name"),
254 estoraged::BasicVariantType("some_drive"));
255
John Wedigf78215f2022-06-07 13:39:22 -0700256 /* Create a dummy device. */
257 std::filesystem::create_directories("abc/device");
258 const std::string dummyTypeFileName("abc/device/type");
259 std::ofstream dummyTypeFile(dummyTypeFileName,
260 std::ios::out | std::ios::trunc);
261 dummyTypeFile << "SSD";
262 dummyTypeFile.close();
John Wedigd32b9662022-04-13 18:12:25 -0700263
John Wedigf78215f2022-06-07 13:39:22 -0700264 /* Another device. */
265 std::filesystem::create_directories("def/device");
266
267 /* Create a dummy eMMC device. */
268 std::filesystem::create_directories("mmcblk0/device");
269 const std::string typeFileName("mmcblk0/device/type");
270 std::ofstream typeFile(typeFileName, std::ios::out | std::ios::trunc);
271 typeFile << "MMC";
272 typeFile.close();
John Wedigd32b9662022-04-13 18:12:25 -0700273
274 /* Look for the device file. */
Tom Tung043af592023-11-24 13:37:05 +0800275 auto result = estoraged::util::findDevice(data,
276 std::filesystem::path("./"));
277 EXPECT_FALSE(result.has_value());
John Wedigd32b9662022-04-13 18:12:25 -0700278
279 /* Delete the dummy files. */
John Edward Broadbent9e639822022-06-10 10:16:05 -0700280 EXPECT_EQ(3U, std::filesystem::remove_all("mmcblk0"));
281 EXPECT_EQ(3U, std::filesystem::remove_all("abc"));
282 EXPECT_EQ(2U, std::filesystem::remove_all("def"));
John Wedigd32b9662022-04-13 18:12:25 -0700283}
284
285/* Test case where we can't find the device file. */
286TEST(utilTest, findDeviceNotFoundFail)
287{
288 estoraged::StorageData data;
289
290 /* Set up the map of properties. */
291 data.emplace(std::string("Type"),
292 estoraged::BasicVariantType("EmmcDevice"));
293 data.emplace(std::string("Name"), estoraged::BasicVariantType("emmc"));
294
John Wedigf78215f2022-06-07 13:39:22 -0700295 /* Create a dummy device. */
296 std::filesystem::create_directories("abc/device");
297 const std::string dummyTypeFileName("abc/device/type");
298 std::ofstream dummyTypeFile(dummyTypeFileName,
299 std::ios::out | std::ios::trunc);
300 dummyTypeFile << "SSD";
301 dummyTypeFile.close();
302
303 /* Another device. */
304 std::filesystem::create_directories("def/device");
John Wedigd32b9662022-04-13 18:12:25 -0700305
306 /* Look for the device file. */
Tom Tung043af592023-11-24 13:37:05 +0800307 auto result = estoraged::util::findDevice(data,
308 std::filesystem::path("./"));
309 EXPECT_FALSE(result.has_value());
John Wedigd32b9662022-04-13 18:12:25 -0700310
John Wedigf78215f2022-06-07 13:39:22 -0700311 /* Delete the dummy files. */
John Edward Broadbent9e639822022-06-10 10:16:05 -0700312 EXPECT_EQ(3U, std::filesystem::remove_all("abc"));
313 EXPECT_EQ(2U, std::filesystem::remove_all("def"));
John Wedigd32b9662022-04-13 18:12:25 -0700314}
315
John Edward Broadbent5d799bb2022-03-22 16:14:24 -0700316} // namespace estoraged_test