blob: a4b796c2923e9a67c3ca5080317aa5ff40da5556 [file] [log] [blame]
John Wedigd32b9662022-04-13 18:12:25 -07001#include "getConfig.hpp"
2
3#include <unistd.h>
4
5#include <boost/container/flat_map.hpp>
John Edward Broadbent5d799bb2022-03-22 16:14:24 -07006#include <util.hpp>
7
John Wedigd32b9662022-04-13 18:12:25 -07008#include <filesystem>
John Edward Broadbent5d799bb2022-03-22 16:14:24 -07009#include <fstream>
10
11#include <gmock/gmock-matchers.h>
12#include <gmock/gmock.h>
13#include <gtest/gtest.h>
14
15namespace estoraged_test
16{
17using estoraged::util::findPredictedMediaLifeLeftPercent;
18
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);
29}
30
31TEST(utilTest, estimatesSame)
32{
33
34 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);
43}
44
45TEST(utilTest, estimatesNotAvailable)
46{
47
48 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);
56}
57
John Wedigd32b9662022-04-13 18:12:25 -070058/* Test case where we successfully find the device file. */
59TEST(utilTest, findDevicePass)
60{
61 estoraged::StorageData data;
62
63 /* Set up the map of properties. */
64 data.emplace(std::string("Type"),
65 estoraged::BasicVariantType("EmmcDevice"));
66 data.emplace(std::string("Name"), estoraged::BasicVariantType("emmc"));
67
68 /* Create a dummy device file. */
69 const std::string testDevName("mmcblk0");
70 std::ofstream testFile;
71 testFile.open(testDevName,
72 std::ios::out | std::ios::binary | std::ios::trunc);
73 testFile.close();
74
75 /* Create another dummy file. */
76 const std::string testDummyFilename("abc");
77 testFile.open(testDummyFilename,
78 std::ios::out | std::ios::binary | std::ios::trunc);
79 testFile.close();
80
81 /* Look for the device file. */
82 std::filesystem::path deviceFile, sysfsDir;
83 std::string luksName;
84 EXPECT_TRUE(estoraged::util::findDevice(data, std::filesystem::path("./"),
85 deviceFile, sysfsDir, luksName));
86
87 /* Validate the results. */
88 EXPECT_EQ("/dev/mmcblk0", deviceFile.string());
89 EXPECT_EQ("./mmcblk0/device", sysfsDir.string());
90 EXPECT_EQ("luks-mmcblk0", luksName);
91
92 /* Delete the dummy files. */
93 EXPECT_EQ(0, unlink(testDevName.c_str()));
94 EXPECT_EQ(0, unlink(testDummyFilename.c_str()));
95}
96
97/* Test case where the "Type" property doesn't exist. */
98TEST(utilTest, findDeviceNoTypeFail)
99{
100 estoraged::StorageData data;
101
102 /* Set up the map of properties (with the "Type" property missing). */
103 data.emplace(std::string("Name"), estoraged::BasicVariantType("emmc"));
104
105 /* Create a dummy device file. */
106 const std::string testDevName("mmcblk0");
107 std::ofstream testFile;
108 testFile.open(testDevName,
109 std::ios::out | std::ios::binary | std::ios::trunc);
110 testFile.close();
111
112 /* Create another dummy file. */
113 const std::string testDummyFilename("abc");
114 testFile.open(testDummyFilename,
115 std::ios::out | std::ios::binary | std::ios::trunc);
116 testFile.close();
117
118 /* Look for the device file. */
119 std::filesystem::path deviceFile, sysfsDir;
120 std::string luksName;
121 EXPECT_FALSE(estoraged::util::findDevice(data, std::filesystem::path("./"),
122 deviceFile, sysfsDir, luksName));
123
124 /* Delete the dummy files. */
125 EXPECT_EQ(0, unlink(testDevName.c_str()));
126 EXPECT_EQ(0, unlink(testDummyFilename.c_str()));
127}
128
129/* Test case where the device type is not supported. */
130TEST(utilTest, findDeviceUnsupportedTypeFail)
131{
132 estoraged::StorageData data;
133
134 /* Set up the map of properties (with an unsupported "Type"). */
135 data.emplace(std::string("Type"), estoraged::BasicVariantType("NvmeDrive"));
136 data.emplace(std::string("Name"),
137 estoraged::BasicVariantType("some_drive"));
138
139 /* Create a dummy device file. */
140 const std::string testDevName("mmcblk0");
141 std::ofstream testFile;
142 testFile.open(testDevName,
143 std::ios::out | std::ios::binary | std::ios::trunc);
144 testFile.close();
145
146 /* Create another dummy file. */
147 const std::string testDummyFilename("abc");
148 testFile.open(testDummyFilename,
149 std::ios::out | std::ios::binary | std::ios::trunc);
150 testFile.close();
151
152 /* Look for the device file. */
153 std::filesystem::path deviceFile, sysfsDir;
154 std::string luksName;
155 EXPECT_FALSE(estoraged::util::findDevice(data, std::filesystem::path("./"),
156 deviceFile, sysfsDir, luksName));
157
158 /* Delete the dummy files. */
159 EXPECT_EQ(0, unlink(testDevName.c_str()));
160 EXPECT_EQ(0, unlink(testDummyFilename.c_str()));
161}
162
163/* Test case where we can't find the device file. */
164TEST(utilTest, findDeviceNotFoundFail)
165{
166 estoraged::StorageData data;
167
168 /* Set up the map of properties. */
169 data.emplace(std::string("Type"),
170 estoraged::BasicVariantType("EmmcDevice"));
171 data.emplace(std::string("Name"), estoraged::BasicVariantType("emmc"));
172
173 /* Create a dummy file. */
174 const std::string testDummyFilename("abc");
175 std::ofstream testFile;
176 testFile.open(testDummyFilename,
177 std::ios::out | std::ios::binary | std::ios::trunc);
178 testFile.close();
179
180 /* Look for the device file. */
181 std::filesystem::path deviceFile, sysfsDir;
182 std::string luksName;
183 EXPECT_FALSE(estoraged::util::findDevice(data, std::filesystem::path("./"),
184 deviceFile, sysfsDir, luksName));
185
186 /* Delete the dummy file. */
187 EXPECT_EQ(0, unlink(testDummyFilename.c_str()));
188}
189
John Edward Broadbent5d799bb2022-03-22 16:14:24 -0700190} // namespace estoraged_test