blob: b85fca0b3079916a9225778d24f128fc8f2b3c71 [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"));
110
John Wedigf78215f2022-06-07 13:39:22 -0700111 /* 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();
John Wedigd32b9662022-04-13 18:12:25 -0700118
John Wedigf78215f2022-06-07 13:39:22 -0700119 /* 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();
John Wedigd32b9662022-04-13 18:12:25 -0700128
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. */
John Edward Broadbent9e639822022-06-10 10:16:05 -0700141 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"));
John Wedigd32b9662022-04-13 18:12:25 -0700144}
145
146/* Test case where the "Type" property doesn't exist. */
147TEST(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
John Wedigf78215f2022-06-07 13:39:22 -0700154 /* 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();
John Wedigd32b9662022-04-13 18:12:25 -0700161
John Wedigf78215f2022-06-07 13:39:22 -0700162 /* 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();
John Wedigd32b9662022-04-13 18:12:25 -0700171
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. */
John Edward Broadbent9e639822022-06-10 10:16:05 -0700179 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"));
John Wedigd32b9662022-04-13 18:12:25 -0700182}
183
184/* Test case where the device type is not supported. */
185TEST(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
John Wedigf78215f2022-06-07 13:39:22 -0700194 /* 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();
John Wedigd32b9662022-04-13 18:12:25 -0700201
John Wedigf78215f2022-06-07 13:39:22 -0700202 /* 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();
John Wedigd32b9662022-04-13 18:12:25 -0700211
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. */
John Edward Broadbent9e639822022-06-10 10:16:05 -0700219 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"));
John Wedigd32b9662022-04-13 18:12:25 -0700222}
223
224/* Test case where we can't find the device file. */
225TEST(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
John Wedigf78215f2022-06-07 13:39:22 -0700234 /* 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");
John Wedigd32b9662022-04-13 18:12:25 -0700244
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
John Wedigf78215f2022-06-07 13:39:22 -0700251 /* Delete the dummy files. */
John Edward Broadbent9e639822022-06-10 10:16:05 -0700252 EXPECT_EQ(3U, std::filesystem::remove_all("abc"));
253 EXPECT_EQ(2U, std::filesystem::remove_all("def"));
John Wedigd32b9662022-04-13 18:12:25 -0700254}
255
John Edward Broadbent5d799bb2022-03-22 16:14:24 -0700256} // namespace estoraged_test