blob: 27367779948d540fbb289b0ee7c17b6083cb37c0 [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;
Patrick Williams15b63e12024-08-16 15:22:01 -0400135 auto result =
136 estoraged::util::findDevice(data, std::filesystem::path("./"));
Tom Tung043af592023-11-24 13:37:05 +0800137 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);
John Wedigd7be42b2024-01-19 16:07:19 -0800146 EXPECT_EQ("SSD", result->driveType);
John Wedigc0d66eb2024-02-26 15:54:47 -0800147 EXPECT_EQ("eMMC", result->driveProtocol);
Tom Tung043af592023-11-24 13:37:05 +0800148
149 /* Delete the dummy files. */
150 EXPECT_EQ(3U, std::filesystem::remove_all("mmcblk0"));
151 EXPECT_EQ(3U, std::filesystem::remove_all("abc"));
152 EXPECT_EQ(2U, std::filesystem::remove_all("def"));
153}
154
155/* Test case where we successfully find the device file with updating the
156 * min/max geometry. */
157TEST(utilTest, findDeviceWithMaxAndMinGeometryPass)
158{
159 estoraged::StorageData data;
160
161 /* Set up the map of properties. */
162 data.emplace(std::string("Type"),
163 estoraged::BasicVariantType("EmmcDevice"));
164 data.emplace(std::string("Name"), estoraged::BasicVariantType("emmc"));
165 data.emplace(std::string("LocationCode"),
166 estoraged::BasicVariantType("U102020"));
167 data.emplace(std::string("EraseMaxGeometry"),
168 estoraged::BasicVariantType((uint64_t)5566));
169 data.emplace(std::string("EraseMinGeometry"),
170 estoraged::BasicVariantType((uint64_t)1234));
171
172 /* Create a dummy device. */
173 std::filesystem::create_directories("abc/device");
174 const std::string dummyTypeFileName("abc/device/type");
175 std::ofstream dummyTypeFile(dummyTypeFileName,
176 std::ios::out | std::ios::trunc);
177 dummyTypeFile << "SSD";
178 dummyTypeFile.close();
179
180 /* Another device. */
181 std::filesystem::create_directories("def/device");
182
183 /* Create a dummy eMMC device. */
184 std::filesystem::create_directories("mmcblk0/device");
185 const std::string typeFileName("mmcblk0/device/type");
186 std::ofstream typeFile(typeFileName, std::ios::out | std::ios::trunc);
187 typeFile << "MMC";
188 typeFile.close();
189
190 /* Look for the device file. */
191 std::filesystem::path deviceFile, sysfsDir;
192 std::string luksName, locationCode;
Patrick Williams15b63e12024-08-16 15:22:01 -0400193 auto result =
194 estoraged::util::findDevice(data, std::filesystem::path("./"));
Tom Tung043af592023-11-24 13:37:05 +0800195 EXPECT_TRUE(result.has_value());
196
197 /* Validate the results. */
198 EXPECT_EQ("/dev/mmcblk0", result->deviceFile.string());
199 EXPECT_EQ("./mmcblk0/device", result->sysfsDir.string());
200 EXPECT_EQ("luks-mmcblk0", result->luksName);
201 EXPECT_EQ("U102020", result->locationCode);
202 EXPECT_EQ(5566, result->eraseMaxGeometry);
203 EXPECT_EQ(1234, result->eraseMinGeometry);
John Wedigd7be42b2024-01-19 16:07:19 -0800204 EXPECT_EQ("SSD", result->driveType);
John Wedigc0d66eb2024-02-26 15:54:47 -0800205 EXPECT_EQ("eMMC", result->driveProtocol);
John Wedigd32b9662022-04-13 18:12:25 -0700206
207 /* Delete the dummy files. */
John Edward Broadbent9e639822022-06-10 10:16:05 -0700208 EXPECT_EQ(3U, std::filesystem::remove_all("mmcblk0"));
209 EXPECT_EQ(3U, std::filesystem::remove_all("abc"));
210 EXPECT_EQ(2U, std::filesystem::remove_all("def"));
John Wedigd32b9662022-04-13 18:12:25 -0700211}
212
213/* Test case where the "Type" property doesn't exist. */
214TEST(utilTest, findDeviceNoTypeFail)
215{
216 estoraged::StorageData data;
217
218 /* Set up the map of properties (with the "Type" property missing). */
219 data.emplace(std::string("Name"), estoraged::BasicVariantType("emmc"));
220
John Wedigf78215f2022-06-07 13:39:22 -0700221 /* Create a dummy device. */
222 std::filesystem::create_directories("abc/device");
223 const std::string dummyTypeFileName("abc/device/type");
224 std::ofstream dummyTypeFile(dummyTypeFileName,
225 std::ios::out | std::ios::trunc);
226 dummyTypeFile << "SSD";
227 dummyTypeFile.close();
John Wedigd32b9662022-04-13 18:12:25 -0700228
John Wedigf78215f2022-06-07 13:39:22 -0700229 /* Another device. */
230 std::filesystem::create_directories("def/device");
231
232 /* Create a dummy eMMC device. */
233 std::filesystem::create_directories("mmcblk0/device");
234 const std::string typeFileName("mmcblk0/device/type");
235 std::ofstream typeFile(typeFileName, std::ios::out | std::ios::trunc);
236 typeFile << "MMC";
237 typeFile.close();
John Wedigd32b9662022-04-13 18:12:25 -0700238
239 /* Look for the device file. */
Patrick Williams15b63e12024-08-16 15:22:01 -0400240 auto result =
241 estoraged::util::findDevice(data, std::filesystem::path("./"));
Tom Tung043af592023-11-24 13:37:05 +0800242 EXPECT_FALSE(result.has_value());
John Wedigd32b9662022-04-13 18:12:25 -0700243
244 /* Delete the dummy files. */
John Edward Broadbent9e639822022-06-10 10:16:05 -0700245 EXPECT_EQ(3U, std::filesystem::remove_all("mmcblk0"));
246 EXPECT_EQ(3U, std::filesystem::remove_all("abc"));
247 EXPECT_EQ(2U, std::filesystem::remove_all("def"));
John Wedigd32b9662022-04-13 18:12:25 -0700248}
249
250/* Test case where the device type is not supported. */
251TEST(utilTest, findDeviceUnsupportedTypeFail)
252{
253 estoraged::StorageData data;
254
255 /* Set up the map of properties (with an unsupported "Type"). */
256 data.emplace(std::string("Type"), estoraged::BasicVariantType("NvmeDrive"));
257 data.emplace(std::string("Name"),
258 estoraged::BasicVariantType("some_drive"));
259
John Wedigf78215f2022-06-07 13:39:22 -0700260 /* Create a dummy device. */
261 std::filesystem::create_directories("abc/device");
262 const std::string dummyTypeFileName("abc/device/type");
263 std::ofstream dummyTypeFile(dummyTypeFileName,
264 std::ios::out | std::ios::trunc);
265 dummyTypeFile << "SSD";
266 dummyTypeFile.close();
John Wedigd32b9662022-04-13 18:12:25 -0700267
John Wedigf78215f2022-06-07 13:39:22 -0700268 /* Another device. */
269 std::filesystem::create_directories("def/device");
270
271 /* Create a dummy eMMC device. */
272 std::filesystem::create_directories("mmcblk0/device");
273 const std::string typeFileName("mmcblk0/device/type");
274 std::ofstream typeFile(typeFileName, std::ios::out | std::ios::trunc);
275 typeFile << "MMC";
276 typeFile.close();
John Wedigd32b9662022-04-13 18:12:25 -0700277
278 /* Look for the device file. */
Patrick Williams15b63e12024-08-16 15:22:01 -0400279 auto result =
280 estoraged::util::findDevice(data, std::filesystem::path("./"));
Tom Tung043af592023-11-24 13:37:05 +0800281 EXPECT_FALSE(result.has_value());
John Wedigd32b9662022-04-13 18:12:25 -0700282
283 /* Delete the dummy files. */
John Edward Broadbent9e639822022-06-10 10:16:05 -0700284 EXPECT_EQ(3U, std::filesystem::remove_all("mmcblk0"));
285 EXPECT_EQ(3U, std::filesystem::remove_all("abc"));
286 EXPECT_EQ(2U, std::filesystem::remove_all("def"));
John Wedigd32b9662022-04-13 18:12:25 -0700287}
288
289/* Test case where we can't find the device file. */
290TEST(utilTest, findDeviceNotFoundFail)
291{
292 estoraged::StorageData data;
293
294 /* Set up the map of properties. */
295 data.emplace(std::string("Type"),
296 estoraged::BasicVariantType("EmmcDevice"));
297 data.emplace(std::string("Name"), estoraged::BasicVariantType("emmc"));
298
John Wedigf78215f2022-06-07 13:39:22 -0700299 /* Create a dummy device. */
300 std::filesystem::create_directories("abc/device");
301 const std::string dummyTypeFileName("abc/device/type");
302 std::ofstream dummyTypeFile(dummyTypeFileName,
303 std::ios::out | std::ios::trunc);
304 dummyTypeFile << "SSD";
305 dummyTypeFile.close();
306
307 /* Another device. */
308 std::filesystem::create_directories("def/device");
John Wedigd32b9662022-04-13 18:12:25 -0700309
310 /* Look for the device file. */
Patrick Williams15b63e12024-08-16 15:22:01 -0400311 auto result =
312 estoraged::util::findDevice(data, std::filesystem::path("./"));
Tom Tung043af592023-11-24 13:37:05 +0800313 EXPECT_FALSE(result.has_value());
John Wedigd32b9662022-04-13 18:12:25 -0700314
John Wedigf78215f2022-06-07 13:39:22 -0700315 /* Delete the dummy files. */
John Edward Broadbent9e639822022-06-10 10:16:05 -0700316 EXPECT_EQ(3U, std::filesystem::remove_all("abc"));
317 EXPECT_EQ(2U, std::filesystem::remove_all("def"));
John Wedigd32b9662022-04-13 18:12:25 -0700318}
319
John Edward Broadbent5d799bb2022-03-22 16:14:24 -0700320} // namespace estoraged_test