5 #include <gtest/gtest.h>
13 #include <boost/algorithm/hex.hpp>
14 #include <boost/archive/iterators/dataflow_exception.hpp>
17 #include "utilities/utils.h"
20 if ((
'a' <= c) && (c <=
'z')) {
22 }
else if ((
'0' <= c) && (c <=
'9')) {
24 }
else if (c ==
'-') {
31 bool PrettyNameOk(
const std::string &name) {
32 if (name.size() < 5) {
35 for (std::string::const_iterator c = name.begin(); c != name.end(); ++c) {
44 EXPECT_TRUE(PrettyNameOk(
"foo-bar-123"));
45 EXPECT_FALSE(PrettyNameOk(
"NoCapitals"));
46 EXPECT_FALSE(PrettyNameOk(
""));
47 EXPECT_FALSE(PrettyNameOk(
"foo-bar-123&"));
56 const std::string sample =
" { \"b\": 0, \"a\": [1, 2, {}], \"0\": \"x\"}";
59 parsed = Utils::parseJSON(sample);
60 EXPECT_EQ(Utils::jsonToCanonicalStr(parsed),
"{\"0\":\"x\",\"a\":[1,2,{}],\"b\":0}");
62 const std::string sample2 =
"0";
63 parsed = Utils::parseJSON(sample2);
64 EXPECT_EQ(Utils::jsonToCanonicalStr(parsed),
"0");
69 Json::Value hwinfo = Utils::getHardwareInfo();
70 EXPECT_NE(hwinfo, Json::Value());
71 EXPECT_FALSE(hwinfo.isArray());
76 Json::Value netinfo = Utils::getNetworkInfo();
77 EXPECT_NE(netinfo[
"local_ipv4"].asString(),
"");
78 EXPECT_NE(netinfo[
"mac"].asString(),
"");
79 EXPECT_NE(netinfo[
"hostname"].asString(),
"");
83 TEST(
Utils, getHostname) { EXPECT_NE(Utils::getHostname(),
""); }
93 RecordProperty(
"zephyr_key",
"OTA-986,TST-144");
94 std::set<std::string> names;
95 for (
int i = 0; i < 100; i++) {
96 const std::string name = Utils::genPrettyName();
98 const auto count = names.count(name);
100 FAIL() <<
"Something wrong with randomness: " << name;
101 }
else if (count == 2) {
102 std::cerr <<
"Lucky draw: " << name;
109 std::set<std::string> uuids;
110 for (
int i = 0; i < 1000; i++) {
111 std::string uuid = Utils::randomUuid();
112 EXPECT_EQ(0, uuids.count(uuid));
114 EXPECT_EQ(1, uuids.count(uuid));
120 EXPECT_EQ(
"aGVsbG8=", Utils::toBase64(
"hello"));
121 EXPECT_EQ(
"", Utils::toBase64(
""));
122 EXPECT_EQ(
"CQ==", Utils::toBase64(
"\t"));
123 EXPECT_EQ(
"YWI=", Utils::toBase64(
"ab"));
124 EXPECT_EQ(
"YWJj", Utils::toBase64(
"abc"));
128 EXPECT_EQ(Utils::fromBase64(
"aGVsbG8="),
"hello");
129 EXPECT_EQ(Utils::fromBase64(
""),
"");
130 EXPECT_EQ(Utils::fromBase64(
"YWI="),
"ab");
131 EXPECT_EQ(Utils::fromBase64(
"YWJj"),
"abc");
135 EXPECT_THROW(Utils::fromBase64(
"Привіт"), boost::archive::iterators::dataflow_exception);
136 EXPECT_THROW(Utils::fromBase64(
"aGVsbG8=="), boost::archive::iterators::dataflow_exception);
137 EXPECT_THROW(Utils::fromBase64(
"CQ==="), boost::archive::iterators::dataflow_exception);
142 std::uniform_int_distribution<char> chars(std::numeric_limits<char>::min(), std::numeric_limits<char>::max());
144 std::uniform_int_distribution<int> length(0, 20);
146 for (
int test = 0; test < 100; test++) {
147 int len = length(gen);
148 std::string original;
149 for (
int i = 0; i < len; i++) {
150 original += chars(gen);
152 std::string b64 = Utils::toBase64(original);
153 std::string output = Utils::fromBase64(b64);
154 EXPECT_EQ(original, output);
160 const std::string archive_path =
"tests/test_data/credentials.zip";
163 std::ifstream as(archive_path, std::ios::binary | std::ios::in);
164 EXPECT_FALSE(as.fail());
165 EXPECT_THROW(Utils::readFileFromArchive(as,
"bogus_filename"), std::runtime_error);
169 std::ifstream as(archive_path, std::ios::binary | std::ios::in);
170 EXPECT_FALSE(as.fail());
171 std::string url = Utils::readFileFromArchive(as,
"autoprov.url");
172 EXPECT_EQ(url.rfind(
"https://", 0), 0);
177 std::string archive_bytes;
179 std::map<std::string, std::string> fm{{
"test",
"A"}};
180 std::stringstream as;
181 Utils::writeArchive(fm, as);
182 archive_bytes = as.str();
186 std::stringstream as(archive_bytes);
187 EXPECT_EQ(Utils::readFileFromArchive(as,
"test"),
"A");
193 const boost::filesystem::path old_path =
"tests/test_data/credentials.zip";
195 const boost::filesystem::path new_path = temp_dir.Path() /
"credentials.zip";
196 boost::filesystem::copy_file(old_path, new_path);
198 Utils::removeFileFromArchive(new_path,
"autoprov_credentials.p12");
199 EXPECT_THROW(Utils::removeFileFromArchive(new_path,
"bogus_filename"), std::runtime_error);
202 std::ifstream as(new_path.c_str(), std::ios::binary | std::ios::in);
203 EXPECT_FALSE(as.fail());
204 EXPECT_THROW(Utils::readFileFromArchive(as,
"autoprov_credentials.p12"), std::runtime_error);
210 std::ifstream as_old(old_path.c_str(), std::ios::binary | std::ios::in);
211 EXPECT_FALSE(as_old.fail());
212 std::ifstream as_new(new_path.c_str(), std::ios::binary | std::ios::in);
213 EXPECT_FALSE(as_new.fail());
214 EXPECT_EQ(Utils::readFileFromArchive(as_old,
"autoprov.url"), Utils::readFileFromArchive(as_new,
"autoprov.url"));
220 boost::filesystem::path p;
224 EXPECT_TRUE(boost::filesystem::exists(p));
225 EXPECT_NE(p.string().find(
"ahint"), std::string::npos);
227 struct stat statbuf {};
228 EXPECT_GE(stat(p.parent_path().c_str(), &statbuf), 0);
229 EXPECT_EQ(statbuf.st_mode & (S_IRWXU | S_IRWXG | S_IRWXO), S_IRWXU);
231 EXPECT_FALSE(boost::filesystem::exists(p));
236 boost::filesystem::path p;
240 EXPECT_FALSE(boost::filesystem::exists(p));
241 std::ofstream file(p.c_str());
245 EXPECT_TRUE(boost::filesystem::exists(p));
246 EXPECT_NE(p.string().find(
"ahint"), std::string::npos);
248 struct stat statbuf {};
249 EXPECT_GE(stat(p.parent_path().c_str(), &statbuf), 0);
250 EXPECT_EQ(statbuf.st_mode & (S_IRWXU | S_IRWXG | S_IRWXO), S_IRWXU);
252 EXPECT_FALSE(boost::filesystem::exists(p));
258 f.PutContents(
"thecontents");
259 EXPECT_TRUE(boost::filesystem::exists(f.Path()));
260 std::ifstream a(f.Path().c_str());
263 EXPECT_EQ(b,
"thecontents");
269 Utils::writeFile(temp_dir.Path() /
"from/1/foo", std::string(
"foo"));
270 Utils::writeFile(temp_dir.Path() /
"from/1/2/bar", std::string(
"bar"));
271 Utils::writeFile(temp_dir.Path() /
"from/1/2/baz", std::string(
"baz"));
273 Utils::copyDir(temp_dir.Path() /
"from", temp_dir.Path() /
"to");
274 EXPECT_TRUE(boost::filesystem::exists(temp_dir.Path() /
"to"));
275 EXPECT_TRUE(boost::filesystem::exists(temp_dir.Path() /
"to/1"));
276 EXPECT_TRUE(boost::filesystem::exists(temp_dir.Path() /
"to/1/foo"));
277 EXPECT_TRUE(boost::filesystem::exists(temp_dir.Path() /
"to/1/2"));
278 EXPECT_TRUE(boost::filesystem::exists(temp_dir.Path() /
"to/1/2/bar"));
279 EXPECT_TRUE(boost::filesystem::exists(temp_dir.Path() /
"to/1/2/baz"));
280 EXPECT_EQ(Utils::readFile(temp_dir.Path() /
"to/1/foo"),
"foo");
281 EXPECT_EQ(Utils::readFile(temp_dir.Path() /
"to/1/2/bar"),
"bar");
282 EXPECT_EQ(Utils::readFile(temp_dir.Path() /
"to/1/2/baz"),
"baz");
285 TEST(
Utils, writeFileWithoutDirAutoCreation) {
288 boost::filesystem::create_directories(temp_dir.Path() /
"1/2");
289 Utils::writeFile(temp_dir.Path() /
"1/foo", std::string(
"foo"),
false);
290 Utils::writeFile(temp_dir.Path() /
"1/2/bar", std::string(
"bar"),
false);
292 EXPECT_EQ(Utils::readFile(temp_dir.Path() /
"1/foo"),
"foo");
293 EXPECT_EQ(Utils::readFile(temp_dir.Path() /
"1/2/bar"),
"bar");
296 TEST(
Utils, writeFileWithDirAutoCreation) {
299 Utils::writeFile(temp_dir.Path() /
"1/foo", std::string(
"foo"),
true);
300 Utils::writeFile(temp_dir.Path() /
"1/2/bar", std::string(
"bar"),
true);
302 EXPECT_EQ(Utils::readFile(temp_dir.Path() /
"1/foo"),
"foo");
303 EXPECT_EQ(Utils::readFile(temp_dir.Path() /
"1/2/bar"),
"bar");
306 TEST(
Utils, writeFileWithDirAutoCreationDefault) {
309 Utils::writeFile(temp_dir.Path() /
"1/foo", std::string(
"foo"));
310 Utils::writeFile(temp_dir.Path() /
"1/2/bar", std::string(
"bar"));
312 EXPECT_EQ(Utils::readFile(temp_dir.Path() /
"1/foo"),
"foo");
313 EXPECT_EQ(Utils::readFile(temp_dir.Path() /
"1/2/bar"),
"bar");
316 TEST(
Utils, writeFileWithoutDirAutoCreationException) {
320 Utils::writeFile(temp_dir.Path() /
"1/foo", std::string(
"foo"),
false);
332 Utils::writeFile(temp_dir.Path() /
"1/foo", val);
333 Json::Value result_json = Utils::parseJSONFile(temp_dir.Path() /
"1/foo");
334 EXPECT_EQ(result_json[
"key"].asString(), val[
"key"].asString());
339 int statuscode = Utils::shell(
"ls /", &out);
340 EXPECT_EQ(statuscode, 0);
342 statuscode = Utils::shell(
"ls /nonexistentdir123", &out);
343 EXPECT_NE(statuscode, 0);
349 EXPECT_TRUE(Utils::createSecureDirectory(temp_dir /
"sec"));
350 EXPECT_TRUE(boost::filesystem::is_directory(temp_dir /
"sec"));
352 EXPECT_TRUE(Utils::createSecureDirectory(temp_dir /
"sec"));
355 boost::filesystem::create_directory(temp_dir /
"unsec");
356 EXPECT_FALSE(Utils::createSecureDirectory(temp_dir /
"unsec"));
359 TEST(
Utils, urlencode) { EXPECT_EQ(Utils::urlEncode(
"test! test@ test#"),
"test%21%20test%40%20test%23"); }
364 Utils::writeFile(temp_dir /
"02-test.toml", std::string(
""));
365 Utils::writeFile(temp_dir /
"0x-test.noml", std::string(
""));
366 Utils::writeFile(temp_dir /
"03-test.toml", std::string(
""));
367 Utils::writeFile(temp_dir /
"01-test.toml", std::string(
""));
369 auto files = Utils::getDirEntriesByExt(temp_dir.Path(),
".toml");
370 std::vector<boost::filesystem::path> expected{temp_dir /
"01-test.toml", temp_dir /
"02-test.toml",
371 temp_dir /
"03-test.toml"};
372 EXPECT_EQ(files, expected);
379 EXPECT_EQ(bp.get(
"/"),
"/a/test.xml");
380 EXPECT_EQ(bp.get(
"/x"),
"/x/a/test.xml");
384 EXPECT_EQ(abp.get(
""),
"/a/test.xml");
385 EXPECT_EQ(abp.get(
"/root/var"),
"/a/test.xml");
390 std::string input =
"test with newline";
391 Utils::writeFile(f.Path(), input +
"\n");
392 std::string output = Utils::readFile(f.Path(),
false);
393 EXPECT_EQ(output, input +
"\n");
394 output = Utils::readFile(f.Path(),
true);
395 EXPECT_EQ(output, input);
399 int main(
int argc,
char **argv) {
400 ::testing::InitGoogleTest(&argc, argv);
401 return RUN_ALL_TESTS();