5 #include <gtest/gtest.h> 13 #include <boost/algorithm/hex.hpp> 14 #include <boost/archive/iterators/dataflow_exception.hpp> 16 #include "utilities/utils.h" 19 if ((
'a' <= c) && (c <=
'z')) {
21 }
else if ((
'0' <= c) && (c <=
'9')) {
23 }
else if (c ==
'-') {
30 bool PrettyNameOk(
const std::string &name) {
31 if (name.size() < 5) {
34 for (std::string::const_iterator c = name.begin(); c != name.end(); ++c) {
43 EXPECT_TRUE(PrettyNameOk(
"foo-bar-123"));
44 EXPECT_FALSE(PrettyNameOk(
"NoCapitals"));
45 EXPECT_FALSE(PrettyNameOk(
""));
46 EXPECT_FALSE(PrettyNameOk(
"foo-bar-123&"));
55 const std::string sample =
" { \"b\": 0, \"a\": [1, 2, {}], \"0\": \"x\"}";
58 parsed = Utils::parseJSON(sample);
59 EXPECT_EQ(Utils::jsonToCanonicalStr(parsed),
"{\"0\":\"x\",\"a\":[1,2,{}],\"b\":0}");
61 const std::string sample2 =
"0";
62 parsed = Utils::parseJSON(sample2);
63 EXPECT_EQ(Utils::jsonToCanonicalStr(parsed),
"0");
68 Json::Value hwinfo = Utils::getHardwareInfo();
69 EXPECT_NE(hwinfo, Json::Value());
70 EXPECT_FALSE(hwinfo.isArray());
75 Json::Value netinfo = Utils::getNetworkInfo();
76 EXPECT_NE(netinfo[
"local_ipv4"].asString(),
"");
77 EXPECT_NE(netinfo[
"mac"].asString(),
"");
78 EXPECT_NE(netinfo[
"hostname"].asString(),
"");
82 TEST(
Utils, getHostname) { EXPECT_NE(Utils::getHostname(),
""); }
92 RecordProperty(
"zephyr_key",
"OTA-986,TST-144");
93 std::set<std::string> names;
94 for (
int i = 0; i < 100; i++) {
95 const std::string name = Utils::genPrettyName();
97 const auto count = names.count(name);
99 FAIL() <<
"Something wrong with randomness: " << name;
100 }
else if (count == 2) {
101 std::cerr <<
"Lucky draw: " << name;
108 std::set<std::string> uuids;
109 for (
int i = 0; i < 1000; i++) {
110 std::string uuid = Utils::randomUuid();
111 EXPECT_EQ(0, uuids.count(uuid));
113 EXPECT_EQ(1, uuids.count(uuid));
119 EXPECT_EQ(
"aGVsbG8=", Utils::toBase64(
"hello"));
120 EXPECT_EQ(
"", Utils::toBase64(
""));
121 EXPECT_EQ(
"CQ==", Utils::toBase64(
"\t"));
122 EXPECT_EQ(
"YWI=", Utils::toBase64(
"ab"));
123 EXPECT_EQ(
"YWJj", Utils::toBase64(
"abc"));
127 EXPECT_EQ(Utils::fromBase64(
"aGVsbG8="),
"hello");
128 EXPECT_EQ(Utils::fromBase64(
""),
"");
129 EXPECT_EQ(Utils::fromBase64(
"YWI="),
"ab");
130 EXPECT_EQ(Utils::fromBase64(
"YWJj"),
"abc");
134 EXPECT_THROW(Utils::fromBase64(
"Привіт"), boost::archive::iterators::dataflow_exception);
135 EXPECT_THROW(Utils::fromBase64(
"aGVsbG8=="), boost::archive::iterators::dataflow_exception);
136 EXPECT_THROW(Utils::fromBase64(
"CQ==="), boost::archive::iterators::dataflow_exception);
141 std::uniform_int_distribution<char> chars(std::numeric_limits<char>::min(), std::numeric_limits<char>::max());
143 std::uniform_int_distribution<int> length(0, 20);
145 for (
int test = 0; test < 100; test++) {
146 int len = length(gen);
147 std::string original;
148 for (
int i = 0; i < len; i++) {
149 original += chars(gen);
151 std::string b64 = Utils::toBase64(original);
152 std::string output = Utils::fromBase64(b64);
153 EXPECT_EQ(original, output);
161 const std::string archive_path =
"tests/test_data/credentials.zip";
164 std::ifstream as(archive_path, std::ios::binary | std::ios::in);
165 EXPECT_FALSE(as.fail());
166 EXPECT_THROW(Utils::readFileFromArchive(as,
"bogus_filename"), std::runtime_error);
170 std::ifstream as(archive_path, std::ios::binary | std::ios::in);
171 EXPECT_FALSE(as.fail());
173 std::string url = Utils::readFileFromArchive(as,
"autoprov.url");
174 EXPECT_EQ(url.rfind(
"https://", 0), 0);
179 std::string archive_bytes;
181 std::map<std::string, std::string> fm{{
"test",
"A"}};
182 std::stringstream as;
183 Utils::writeArchive(fm, as);
184 archive_bytes = as.str();
188 std::stringstream as(archive_bytes);
189 EXPECT_EQ(Utils::readFileFromArchive(as,
"test"),
"A");
195 boost::filesystem::path p;
199 EXPECT_TRUE(boost::filesystem::exists(p));
200 EXPECT_NE(p.string().find(
"ahint"), std::string::npos);
202 struct stat statbuf {};
203 EXPECT_GE(stat(p.parent_path().c_str(), &statbuf), 0);
204 EXPECT_EQ(statbuf.st_mode & (S_IRWXU | S_IRWXG | S_IRWXO), S_IRWXU);
206 EXPECT_FALSE(boost::filesystem::exists(p));
211 boost::filesystem::path p;
215 EXPECT_FALSE(boost::filesystem::exists(p));
216 std::ofstream file(p.c_str());
220 EXPECT_TRUE(boost::filesystem::exists(p));
221 EXPECT_NE(p.string().find(
"ahint"), std::string::npos);
223 struct stat statbuf {};
224 EXPECT_GE(stat(p.parent_path().c_str(), &statbuf), 0);
225 EXPECT_EQ(statbuf.st_mode & (S_IRWXU | S_IRWXG | S_IRWXO), S_IRWXU);
227 EXPECT_FALSE(boost::filesystem::exists(p));
233 f.PutContents(
"thecontents");
234 EXPECT_TRUE(boost::filesystem::exists(f.Path()));
235 std::ifstream a(f.Path().c_str());
238 EXPECT_EQ(b,
"thecontents");
244 Utils::writeFile(temp_dir.Path() /
"from/1/foo", std::string(
"foo"));
245 Utils::writeFile(temp_dir.Path() /
"from/1/2/bar", std::string(
"bar"));
246 Utils::writeFile(temp_dir.Path() /
"from/1/2/baz", std::string(
"baz"));
248 Utils::copyDir(temp_dir.Path() /
"from", temp_dir.Path() /
"to");
249 EXPECT_TRUE(boost::filesystem::exists(temp_dir.Path() /
"to"));
250 EXPECT_TRUE(boost::filesystem::exists(temp_dir.Path() /
"to/1"));
251 EXPECT_TRUE(boost::filesystem::exists(temp_dir.Path() /
"to/1/foo"));
252 EXPECT_TRUE(boost::filesystem::exists(temp_dir.Path() /
"to/1/2"));
253 EXPECT_TRUE(boost::filesystem::exists(temp_dir.Path() /
"to/1/2/bar"));
254 EXPECT_TRUE(boost::filesystem::exists(temp_dir.Path() /
"to/1/2/baz"));
255 EXPECT_EQ(Utils::readFile(temp_dir.Path() /
"to/1/foo"),
"foo");
256 EXPECT_EQ(Utils::readFile(temp_dir.Path() /
"to/1/2/bar"),
"bar");
257 EXPECT_EQ(Utils::readFile(temp_dir.Path() /
"to/1/2/baz"),
"baz");
260 TEST(
Utils, writeFileWithoutDirAutoCreation) {
263 boost::filesystem::create_directories(temp_dir.Path() /
"1/2");
264 Utils::writeFile(temp_dir.Path() /
"1/foo", std::string(
"foo"),
false);
265 Utils::writeFile(temp_dir.Path() /
"1/2/bar", std::string(
"bar"),
false);
267 EXPECT_EQ(Utils::readFile(temp_dir.Path() /
"1/foo"),
"foo");
268 EXPECT_EQ(Utils::readFile(temp_dir.Path() /
"1/2/bar"),
"bar");
271 TEST(
Utils, writeFileWithDirAutoCreation) {
274 Utils::writeFile(temp_dir.Path() /
"1/foo", std::string(
"foo"),
true);
275 Utils::writeFile(temp_dir.Path() /
"1/2/bar", std::string(
"bar"),
true);
277 EXPECT_EQ(Utils::readFile(temp_dir.Path() /
"1/foo"),
"foo");
278 EXPECT_EQ(Utils::readFile(temp_dir.Path() /
"1/2/bar"),
"bar");
281 TEST(
Utils, writeFileWithDirAutoCreationDefault) {
284 Utils::writeFile(temp_dir.Path() /
"1/foo", std::string(
"foo"));
285 Utils::writeFile(temp_dir.Path() /
"1/2/bar", std::string(
"bar"));
287 EXPECT_EQ(Utils::readFile(temp_dir.Path() /
"1/foo"),
"foo");
288 EXPECT_EQ(Utils::readFile(temp_dir.Path() /
"1/2/bar"),
"bar");
291 TEST(
Utils, writeFileWithoutDirAutoCreationException) {
295 Utils::writeFile(temp_dir.Path() /
"1/foo", std::string(
"foo"),
false);
307 Utils::writeFile(temp_dir.Path() /
"1/foo", val);
308 Json::Value result_json = Utils::parseJSONFile(temp_dir.Path() /
"1/foo");
309 EXPECT_EQ(result_json[
"key"].asString(), val[
"key"].asString());
314 int statuscode = Utils::shell(
"ls /", &out);
315 EXPECT_EQ(statuscode, 0);
317 statuscode = Utils::shell(
"ls /nonexistentdir123", &out);
318 EXPECT_NE(statuscode, 0);
324 EXPECT_TRUE(Utils::createSecureDirectory(temp_dir /
"sec"));
325 EXPECT_TRUE(boost::filesystem::is_directory(temp_dir /
"sec"));
327 EXPECT_TRUE(Utils::createSecureDirectory(temp_dir /
"sec"));
330 boost::filesystem::create_directory(temp_dir /
"unsec");
331 EXPECT_FALSE(Utils::createSecureDirectory(temp_dir /
"unsec"));
334 TEST(
Utils, urlencode) { EXPECT_EQ(Utils::urlEncode(
"test! test@ test#"),
"test%21%20test%40%20test%23"); }
339 Utils::writeFile(temp_dir /
"02-test.toml", std::string(
""));
340 Utils::writeFile(temp_dir /
"0x-test.noml", std::string(
""));
341 Utils::writeFile(temp_dir /
"03-test.toml", std::string(
""));
342 Utils::writeFile(temp_dir /
"01-test.toml", std::string(
""));
344 auto files = Utils::getDirEntriesByExt(temp_dir.Path(),
".toml");
345 std::vector<boost::filesystem::path> expected{temp_dir /
"01-test.toml", temp_dir /
"02-test.toml",
346 temp_dir /
"03-test.toml"};
347 EXPECT_EQ(files, expected);
354 EXPECT_EQ(bp.get(
"/"),
"/a/test.xml");
355 EXPECT_EQ(bp.get(
"/x"),
"/x/a/test.xml");
359 EXPECT_EQ(abp.get(
""),
"/a/test.xml");
360 EXPECT_EQ(abp.get(
"/root/var"),
"/a/test.xml");
365 std::string input =
"test with newline";
366 Utils::writeFile(f.Path(), input +
"\n");
367 std::string output = Utils::readFile(f.Path(),
false);
368 EXPECT_EQ(output, input +
"\n");
369 output = Utils::readFile(f.Path(),
true);
370 EXPECT_EQ(output, input);
374 int main(
int argc,
char **argv) {
375 ::testing::InitGoogleTest(&argc, argv);
376 return RUN_ALL_TESTS();
TEST(Utils, GenPrettyNameSane)
Check that aktualizr can generate a pet name.
RAII Temporary file creation.