blob: 807eb2062b35fece303ca176e1d9ea66e515cb73 [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;
16
17TEST(utilTest, passFindPredictedMediaLife)
18{
19 std::string prefixName = ".";
20 std::string testFileName = prefixName + "/life_time";
21 std::ofstream testFile;
22 testFile.open(testFileName,
23 std::ios::out | std::ios::binary | std::ios::trunc);
24 testFile << "0x07 0x04";
25 testFile.close();
26 EXPECT_EQ(findPredictedMediaLifeLeftPercent(prefixName), 40);
27}
28
29TEST(utilTest, estimatesSame)
30{
31
32 std::string prefixName = ".";
33 std::string testFileName = prefixName + "/life_time";
34 std::ofstream testFile;
35 testFile.open(testFileName,
36 std::ios::out | std::ios::binary | std::ios::trunc);
37 testFile << "0x04 0x04";
38 testFile.close();
39
40 EXPECT_EQ(findPredictedMediaLifeLeftPercent(prefixName), 70);
41}
42
43TEST(utilTest, estimatesNotAvailable)
44{
45
46 std::string prefixName = ".";
47 std::string testFileName = prefixName + "/life_time";
48 std::ofstream testFile;
49 testFile.open(testFileName,
50 std::ios::out | std::ios::binary | std::ios::trunc);
51 testFile.close();
52
53 EXPECT_EQ(findPredictedMediaLifeLeftPercent(prefixName), 255);
54}
55
John Wedigd32b9662022-04-13 18:12:25 -070056/* Test case where we successfully find the device file. */
57TEST(utilTest, findDevicePass)
58{
59 estoraged::StorageData data;
60
61 /* Set up the map of properties. */
62 data.emplace(std::string("Type"),
63 estoraged::BasicVariantType("EmmcDevice"));
64 data.emplace(std::string("Name"), estoraged::BasicVariantType("emmc"));
65
John Wedigf78215f2022-06-07 13:39:22 -070066 /* Create a dummy device. */
67 std::filesystem::create_directories("abc/device");
68 const std::string dummyTypeFileName("abc/device/type");
69 std::ofstream dummyTypeFile(dummyTypeFileName,
70 std::ios::out | std::ios::trunc);
71 dummyTypeFile << "SSD";
72 dummyTypeFile.close();
John Wedigd32b9662022-04-13 18:12:25 -070073
John Wedigf78215f2022-06-07 13:39:22 -070074 /* Another device. */
75 std::filesystem::create_directories("def/device");
76
77 /* Create a dummy eMMC device. */
78 std::filesystem::create_directories("mmcblk0/device");
79 const std::string typeFileName("mmcblk0/device/type");
80 std::ofstream typeFile(typeFileName, std::ios::out | std::ios::trunc);
81 typeFile << "MMC";
82 typeFile.close();
John Wedigd32b9662022-04-13 18:12:25 -070083
84 /* Look for the device file. */
85 std::filesystem::path deviceFile, sysfsDir;
86 std::string luksName;
87 EXPECT_TRUE(estoraged::util::findDevice(data, std::filesystem::path("./"),
88 deviceFile, sysfsDir, luksName));
89
90 /* Validate the results. */
91 EXPECT_EQ("/dev/mmcblk0", deviceFile.string());
92 EXPECT_EQ("./mmcblk0/device", sysfsDir.string());
93 EXPECT_EQ("luks-mmcblk0", luksName);
94
95 /* Delete the dummy files. */
John Wedigf78215f2022-06-07 13:39:22 -070096 EXPECT_EQ(3, std::filesystem::remove_all("mmcblk0"));
97 EXPECT_EQ(3, std::filesystem::remove_all("abc"));
98 EXPECT_EQ(2, std::filesystem::remove_all("def"));
John Wedigd32b9662022-04-13 18:12:25 -070099}
100
101/* Test case where the "Type" property doesn't exist. */
102TEST(utilTest, findDeviceNoTypeFail)
103{
104 estoraged::StorageData data;
105
106 /* Set up the map of properties (with the "Type" property missing). */
107 data.emplace(std::string("Name"), estoraged::BasicVariantType("emmc"));
108
John Wedigf78215f2022-06-07 13:39:22 -0700109 /* Create a dummy device. */
110 std::filesystem::create_directories("abc/device");
111 const std::string dummyTypeFileName("abc/device/type");
112 std::ofstream dummyTypeFile(dummyTypeFileName,
113 std::ios::out | std::ios::trunc);
114 dummyTypeFile << "SSD";
115 dummyTypeFile.close();
John Wedigd32b9662022-04-13 18:12:25 -0700116
John Wedigf78215f2022-06-07 13:39:22 -0700117 /* Another device. */
118 std::filesystem::create_directories("def/device");
119
120 /* Create a dummy eMMC device. */
121 std::filesystem::create_directories("mmcblk0/device");
122 const std::string typeFileName("mmcblk0/device/type");
123 std::ofstream typeFile(typeFileName, std::ios::out | std::ios::trunc);
124 typeFile << "MMC";
125 typeFile.close();
John Wedigd32b9662022-04-13 18:12:25 -0700126
127 /* Look for the device file. */
128 std::filesystem::path deviceFile, sysfsDir;
129 std::string luksName;
130 EXPECT_FALSE(estoraged::util::findDevice(data, std::filesystem::path("./"),
131 deviceFile, sysfsDir, luksName));
132
133 /* Delete the dummy files. */
John Wedigf78215f2022-06-07 13:39:22 -0700134 EXPECT_EQ(3, std::filesystem::remove_all("mmcblk0"));
135 EXPECT_EQ(3, std::filesystem::remove_all("abc"));
136 EXPECT_EQ(2, std::filesystem::remove_all("def"));
John Wedigd32b9662022-04-13 18:12:25 -0700137}
138
139/* Test case where the device type is not supported. */
140TEST(utilTest, findDeviceUnsupportedTypeFail)
141{
142 estoraged::StorageData data;
143
144 /* Set up the map of properties (with an unsupported "Type"). */
145 data.emplace(std::string("Type"), estoraged::BasicVariantType("NvmeDrive"));
146 data.emplace(std::string("Name"),
147 estoraged::BasicVariantType("some_drive"));
148
John Wedigf78215f2022-06-07 13:39:22 -0700149 /* Create a dummy device. */
150 std::filesystem::create_directories("abc/device");
151 const std::string dummyTypeFileName("abc/device/type");
152 std::ofstream dummyTypeFile(dummyTypeFileName,
153 std::ios::out | std::ios::trunc);
154 dummyTypeFile << "SSD";
155 dummyTypeFile.close();
John Wedigd32b9662022-04-13 18:12:25 -0700156
John Wedigf78215f2022-06-07 13:39:22 -0700157 /* Another device. */
158 std::filesystem::create_directories("def/device");
159
160 /* Create a dummy eMMC device. */
161 std::filesystem::create_directories("mmcblk0/device");
162 const std::string typeFileName("mmcblk0/device/type");
163 std::ofstream typeFile(typeFileName, std::ios::out | std::ios::trunc);
164 typeFile << "MMC";
165 typeFile.close();
John Wedigd32b9662022-04-13 18:12:25 -0700166
167 /* Look for the device file. */
168 std::filesystem::path deviceFile, sysfsDir;
169 std::string luksName;
170 EXPECT_FALSE(estoraged::util::findDevice(data, std::filesystem::path("./"),
171 deviceFile, sysfsDir, luksName));
172
173 /* Delete the dummy files. */
John Wedigf78215f2022-06-07 13:39:22 -0700174 EXPECT_EQ(3, std::filesystem::remove_all("mmcblk0"));
175 EXPECT_EQ(3, std::filesystem::remove_all("abc"));
176 EXPECT_EQ(2, std::filesystem::remove_all("def"));
John Wedigd32b9662022-04-13 18:12:25 -0700177}
178
179/* Test case where we can't find the device file. */
180TEST(utilTest, findDeviceNotFoundFail)
181{
182 estoraged::StorageData data;
183
184 /* Set up the map of properties. */
185 data.emplace(std::string("Type"),
186 estoraged::BasicVariantType("EmmcDevice"));
187 data.emplace(std::string("Name"), estoraged::BasicVariantType("emmc"));
188
John Wedigf78215f2022-06-07 13:39:22 -0700189 /* Create a dummy device. */
190 std::filesystem::create_directories("abc/device");
191 const std::string dummyTypeFileName("abc/device/type");
192 std::ofstream dummyTypeFile(dummyTypeFileName,
193 std::ios::out | std::ios::trunc);
194 dummyTypeFile << "SSD";
195 dummyTypeFile.close();
196
197 /* Another device. */
198 std::filesystem::create_directories("def/device");
John Wedigd32b9662022-04-13 18:12:25 -0700199
200 /* Look for the device file. */
201 std::filesystem::path deviceFile, sysfsDir;
202 std::string luksName;
203 EXPECT_FALSE(estoraged::util::findDevice(data, std::filesystem::path("./"),
204 deviceFile, sysfsDir, luksName));
205
John Wedigf78215f2022-06-07 13:39:22 -0700206 /* Delete the dummy files. */
207 EXPECT_EQ(3, std::filesystem::remove_all("abc"));
208 EXPECT_EQ(2, std::filesystem::remove_all("def"));
John Wedigd32b9662022-04-13 18:12:25 -0700209}
210
John Edward Broadbent5d799bb2022-03-22 16:14:24 -0700211} // namespace estoraged_test