xref: /openbmc/estoraged/src/test/util_test.cpp (revision 8d5a3a07)
1 #include "getConfig.hpp"
2 
3 #include <boost/container/flat_map.hpp>
4 #include <util.hpp>
5 
6 #include <filesystem>
7 #include <fstream>
8 
9 #include <gmock/gmock-matchers.h>
10 #include <gmock/gmock.h>
11 #include <gtest/gtest.h>
12 
13 namespace estoraged_test
14 {
15 using estoraged::util::findPredictedMediaLifeLeftPercent;
16 using estoraged::util::getPartNumber;
17 using estoraged::util::getSerialNumber;
18 
19 TEST(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);
29     EXPECT_TRUE(std::filesystem::remove(testFileName));
30 }
31 
32 TEST(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);
44     EXPECT_TRUE(std::filesystem::remove(testFileName));
45 }
46 
47 TEST(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);
58     EXPECT_TRUE(std::filesystem::remove(testFileName));
59 }
60 
61 TEST(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 
69 TEST(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 
82 TEST(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 
90 TEST(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));
101 }
102 
103 /* Test case where we successfully find the device file. */
104 TEST(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 
113     /* 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();
120 
121     /* 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();
130 
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. */
143     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"));
146 }
147 
148 /* Test case where the "Type" property doesn't exist. */
149 TEST(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 
156     /* 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();
163 
164     /* 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();
173 
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. */
181     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"));
184 }
185 
186 /* Test case where the device type is not supported. */
187 TEST(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 
196     /* 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();
203 
204     /* 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();
213 
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. */
221     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"));
224 }
225 
226 /* Test case where we can't find the device file. */
227 TEST(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 
236     /* 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");
246 
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 
253     /* Delete the dummy files. */
254     EXPECT_EQ(3U, std::filesystem::remove_all("abc"));
255     EXPECT_EQ(2U, std::filesystem::remove_all("def"));
256 }
257 
258 } // namespace estoraged_test
259