blob: 5f160689a87d4c8044ee865c8feee540c994f0be [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{
34
35 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{
49
50 std::string prefixName = ".";
51 std::string testFileName = prefixName + "/life_time";
52 std::ofstream testFile;
53 testFile.open(testFileName,
54 std::ios::out | std::ios::binary | std::ios::trunc);
55 testFile.close();
56
57 EXPECT_EQ(findPredictedMediaLifeLeftPercent(prefixName), 255);
John Wedigb4838302022-07-22 13:51:16 -070058 EXPECT_TRUE(std::filesystem::remove(testFileName));
59}
60
61TEST(utilTest, getPartNumberFail)
62{
63 std::string prefixName = ".";
64 std::string testFileName = prefixName + "/name";
65 /* The part name file won't exist for this test. */
66 EXPECT_EQ(getPartNumber(prefixName), "unknown");
67}
68
69TEST(utilTest, getPartNumberPass)
70{
71 std::string prefixName = ".";
72 std::string testFileName = prefixName + "/name";
73 std::ofstream testFile;
74 testFile.open(testFileName,
75 std::ios::out | std::ios::binary | std::ios::trunc);
76 testFile << "ABCD1234";
77 testFile.close();
78 EXPECT_EQ(getPartNumber(prefixName), "ABCD1234");
79 EXPECT_TRUE(std::filesystem::remove(testFileName));
80}
81
82TEST(utilTest, getSerialNumberFail)
83{
84 std::string prefixName = ".";
85 std::string testFileName = prefixName + "/serial";
86 /* The serial number file won't exist for this test. */
87 EXPECT_EQ(getSerialNumber(prefixName), "unknown");
88}
89
90TEST(utilTest, getSerialNumberPass)
91{
92 std::string prefixName = ".";
93 std::string testFileName = prefixName + "/serial";
94 std::ofstream testFile;
95 testFile.open(testFileName,
96 std::ios::out | std::ios::binary | std::ios::trunc);
97 testFile << "0x12345678";
98 testFile.close();
99 EXPECT_EQ(getSerialNumber(prefixName), "0x12345678");
100 EXPECT_TRUE(std::filesystem::remove(testFileName));
John Edward Broadbent5d799bb2022-03-22 16:14:24 -0700101}
102
John Wedigd32b9662022-04-13 18:12:25 -0700103/* Test case where we successfully find the device file. */
104TEST(utilTest, findDevicePass)
105{
106 estoraged::StorageData data;
107
108 /* Set up the map of properties. */
109 data.emplace(std::string("Type"),
110 estoraged::BasicVariantType("EmmcDevice"));
111 data.emplace(std::string("Name"), estoraged::BasicVariantType("emmc"));
112
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;
133 std::string luksName;
134 EXPECT_TRUE(estoraged::util::findDevice(data, std::filesystem::path("./"),
135 deviceFile, sysfsDir, luksName));
136
137 /* Validate the results. */
138 EXPECT_EQ("/dev/mmcblk0", deviceFile.string());
139 EXPECT_EQ("./mmcblk0/device", sysfsDir.string());
140 EXPECT_EQ("luks-mmcblk0", luksName);
141
142 /* Delete the dummy files. */
John Edward Broadbent9e639822022-06-10 10:16:05 -0700143 EXPECT_EQ(3U, std::filesystem::remove_all("mmcblk0"));
144 EXPECT_EQ(3U, std::filesystem::remove_all("abc"));
145 EXPECT_EQ(2U, std::filesystem::remove_all("def"));
John Wedigd32b9662022-04-13 18:12:25 -0700146}
147
148/* Test case where the "Type" property doesn't exist. */
149TEST(utilTest, findDeviceNoTypeFail)
150{
151 estoraged::StorageData data;
152
153 /* Set up the map of properties (with the "Type" property missing). */
154 data.emplace(std::string("Name"), estoraged::BasicVariantType("emmc"));
155
John Wedigf78215f2022-06-07 13:39:22 -0700156 /* Create a dummy device. */
157 std::filesystem::create_directories("abc/device");
158 const std::string dummyTypeFileName("abc/device/type");
159 std::ofstream dummyTypeFile(dummyTypeFileName,
160 std::ios::out | std::ios::trunc);
161 dummyTypeFile << "SSD";
162 dummyTypeFile.close();
John Wedigd32b9662022-04-13 18:12:25 -0700163
John Wedigf78215f2022-06-07 13:39:22 -0700164 /* Another device. */
165 std::filesystem::create_directories("def/device");
166
167 /* Create a dummy eMMC device. */
168 std::filesystem::create_directories("mmcblk0/device");
169 const std::string typeFileName("mmcblk0/device/type");
170 std::ofstream typeFile(typeFileName, std::ios::out | std::ios::trunc);
171 typeFile << "MMC";
172 typeFile.close();
John Wedigd32b9662022-04-13 18:12:25 -0700173
174 /* Look for the device file. */
175 std::filesystem::path deviceFile, sysfsDir;
176 std::string luksName;
177 EXPECT_FALSE(estoraged::util::findDevice(data, std::filesystem::path("./"),
178 deviceFile, sysfsDir, luksName));
179
180 /* Delete the dummy files. */
John Edward Broadbent9e639822022-06-10 10:16:05 -0700181 EXPECT_EQ(3U, std::filesystem::remove_all("mmcblk0"));
182 EXPECT_EQ(3U, std::filesystem::remove_all("abc"));
183 EXPECT_EQ(2U, std::filesystem::remove_all("def"));
John Wedigd32b9662022-04-13 18:12:25 -0700184}
185
186/* Test case where the device type is not supported. */
187TEST(utilTest, findDeviceUnsupportedTypeFail)
188{
189 estoraged::StorageData data;
190
191 /* Set up the map of properties (with an unsupported "Type"). */
192 data.emplace(std::string("Type"), estoraged::BasicVariantType("NvmeDrive"));
193 data.emplace(std::string("Name"),
194 estoraged::BasicVariantType("some_drive"));
195
John Wedigf78215f2022-06-07 13:39:22 -0700196 /* Create a dummy device. */
197 std::filesystem::create_directories("abc/device");
198 const std::string dummyTypeFileName("abc/device/type");
199 std::ofstream dummyTypeFile(dummyTypeFileName,
200 std::ios::out | std::ios::trunc);
201 dummyTypeFile << "SSD";
202 dummyTypeFile.close();
John Wedigd32b9662022-04-13 18:12:25 -0700203
John Wedigf78215f2022-06-07 13:39:22 -0700204 /* Another device. */
205 std::filesystem::create_directories("def/device");
206
207 /* Create a dummy eMMC device. */
208 std::filesystem::create_directories("mmcblk0/device");
209 const std::string typeFileName("mmcblk0/device/type");
210 std::ofstream typeFile(typeFileName, std::ios::out | std::ios::trunc);
211 typeFile << "MMC";
212 typeFile.close();
John Wedigd32b9662022-04-13 18:12:25 -0700213
214 /* Look for the device file. */
215 std::filesystem::path deviceFile, sysfsDir;
216 std::string luksName;
217 EXPECT_FALSE(estoraged::util::findDevice(data, std::filesystem::path("./"),
218 deviceFile, sysfsDir, luksName));
219
220 /* Delete the dummy files. */
John Edward Broadbent9e639822022-06-10 10:16:05 -0700221 EXPECT_EQ(3U, std::filesystem::remove_all("mmcblk0"));
222 EXPECT_EQ(3U, std::filesystem::remove_all("abc"));
223 EXPECT_EQ(2U, std::filesystem::remove_all("def"));
John Wedigd32b9662022-04-13 18:12:25 -0700224}
225
226/* Test case where we can't find the device file. */
227TEST(utilTest, findDeviceNotFoundFail)
228{
229 estoraged::StorageData data;
230
231 /* Set up the map of properties. */
232 data.emplace(std::string("Type"),
233 estoraged::BasicVariantType("EmmcDevice"));
234 data.emplace(std::string("Name"), estoraged::BasicVariantType("emmc"));
235
John Wedigf78215f2022-06-07 13:39:22 -0700236 /* Create a dummy device. */
237 std::filesystem::create_directories("abc/device");
238 const std::string dummyTypeFileName("abc/device/type");
239 std::ofstream dummyTypeFile(dummyTypeFileName,
240 std::ios::out | std::ios::trunc);
241 dummyTypeFile << "SSD";
242 dummyTypeFile.close();
243
244 /* Another device. */
245 std::filesystem::create_directories("def/device");
John Wedigd32b9662022-04-13 18:12:25 -0700246
247 /* Look for the device file. */
248 std::filesystem::path deviceFile, sysfsDir;
249 std::string luksName;
250 EXPECT_FALSE(estoraged::util::findDevice(data, std::filesystem::path("./"),
251 deviceFile, sysfsDir, luksName));
252
John Wedigf78215f2022-06-07 13:39:22 -0700253 /* Delete the dummy files. */
John Edward Broadbent9e639822022-06-10 10:16:05 -0700254 EXPECT_EQ(3U, std::filesystem::remove_all("abc"));
255 EXPECT_EQ(2U, std::filesystem::remove_all("def"));
John Wedigd32b9662022-04-13 18:12:25 -0700256}
257
John Edward Broadbent5d799bb2022-03-22 16:14:24 -0700258} // namespace estoraged_test