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&"));
51 Json::Value hwinfo = Utils::getHardwareInfo();
52 EXPECT_NE(hwinfo, Json::Value());
53 EXPECT_FALSE(hwinfo.isArray());
58 Json::Value netinfo = Utils::getNetworkInfo();
59 EXPECT_NE(netinfo[
"local_ipv4"].asString(),
"");
60 EXPECT_NE(netinfo[
"mac"].asString(),
"");
61 EXPECT_NE(netinfo[
"hostname"].asString(),
"");
65 TEST(
Utils, getHostname) { EXPECT_NE(Utils::getHostname(),
""); }
75 RecordProperty(
"zephyr_key",
"OTA-986,TST-144");
76 std::set<std::string> names;
77 for (
int i = 0; i < 100; i++) {
78 const std::string name = Utils::genPrettyName();
80 const auto count = names.count(name);
82 FAIL() <<
"Something wrong with randomness: " << name;
83 }
else if (count == 2) {
84 std::cerr <<
"Lucky draw: " << name;
91 std::set<std::string> uuids;
92 for (
int i = 0; i < 1000; i++) {
93 std::string uuid = Utils::randomUuid();
94 EXPECT_EQ(0, uuids.count(uuid));
96 EXPECT_EQ(1, uuids.count(uuid));
102 EXPECT_EQ(
"aGVsbG8=", Utils::toBase64(
"hello"));
103 EXPECT_EQ(
"", Utils::toBase64(
""));
104 EXPECT_EQ(
"CQ==", Utils::toBase64(
"\t"));
105 EXPECT_EQ(
"YWI=", Utils::toBase64(
"ab"));
106 EXPECT_EQ(
"YWJj", Utils::toBase64(
"abc"));
110 EXPECT_EQ(Utils::fromBase64(
"aGVsbG8="),
"hello");
111 EXPECT_EQ(Utils::fromBase64(
""),
"");
112 EXPECT_EQ(Utils::fromBase64(
"YWI="),
"ab");
113 EXPECT_EQ(Utils::fromBase64(
"YWJj"),
"abc");
117 EXPECT_THROW(Utils::fromBase64(
"Привіт"), boost::archive::iterators::dataflow_exception);
118 EXPECT_THROW(Utils::fromBase64(
"aGVsbG8=="), boost::archive::iterators::dataflow_exception);
119 EXPECT_THROW(Utils::fromBase64(
"CQ==="), boost::archive::iterators::dataflow_exception);
124 std::uniform_int_distribution<char> chars(std::numeric_limits<char>::min(), std::numeric_limits<char>::max());
126 std::uniform_int_distribution<int> length(0, 20);
128 for (
int test = 0; test < 100; test++) {
129 int len = length(gen);
130 std::string original;
131 for (
int i = 0; i < len; i++) {
132 original += chars(gen);
134 std::string b64 = Utils::toBase64(original);
135 std::string output = Utils::fromBase64(b64);
136 EXPECT_EQ(original, output);
144 const std::string archive_path =
"tests/test_data/credentials.zip";
147 std::ifstream as(archive_path, std::ios::binary | std::ios::in);
148 EXPECT_FALSE(as.fail());
149 EXPECT_THROW(Utils::readFileFromArchive(as,
"bogus_filename"), std::runtime_error);
153 std::ifstream as(archive_path, std::ios::binary | std::ios::in);
154 EXPECT_FALSE(as.fail());
156 std::string url = Utils::readFileFromArchive(as,
"autoprov.url");
157 EXPECT_EQ(url.rfind(
"https://", 0), 0);
162 std::string archive_bytes;
164 std::map<std::string, std::string> fm{{
"test",
"A"}};
165 std::stringstream as;
166 Utils::writeArchive(fm, as);
167 archive_bytes = as.str();
171 std::stringstream as(archive_bytes);
172 EXPECT_EQ(Utils::readFileFromArchive(as,
"test"),
"A");
178 boost::filesystem::path p;
182 EXPECT_TRUE(boost::filesystem::exists(p));
183 EXPECT_NE(p.string().find(
"ahint"), std::string::npos);
186 EXPECT_GE(stat(p.parent_path().c_str(), &statbuf), 0);
187 EXPECT_EQ(statbuf.st_mode & (S_IRWXU | S_IRWXG | S_IRWXO), S_IRWXU);
189 EXPECT_FALSE(boost::filesystem::exists(p));
194 boost::filesystem::path p;
198 EXPECT_FALSE(boost::filesystem::exists(p));
199 std::ofstream file(p.c_str());
203 EXPECT_TRUE(boost::filesystem::exists(p));
204 EXPECT_NE(p.string().find(
"ahint"), std::string::npos);
207 EXPECT_GE(stat(p.parent_path().c_str(), &statbuf), 0);
208 EXPECT_EQ(statbuf.st_mode & (S_IRWXU | S_IRWXG | S_IRWXO), S_IRWXU);
210 EXPECT_FALSE(boost::filesystem::exists(p));
216 f.PutContents(
"thecontents");
217 EXPECT_TRUE(boost::filesystem::exists(f.Path()));
218 std::ifstream a(f.Path().c_str());
221 EXPECT_EQ(b,
"thecontents");
227 Utils::writeFile(temp_dir.Path() /
"from/1/foo", std::string(
"foo"));
228 Utils::writeFile(temp_dir.Path() /
"from/1/2/bar", std::string(
"bar"));
229 Utils::writeFile(temp_dir.Path() /
"from/1/2/baz", std::string(
"baz"));
231 Utils::copyDir(temp_dir.Path() /
"from", temp_dir.Path() /
"to");
232 EXPECT_TRUE(boost::filesystem::exists(temp_dir.Path() /
"to"));
233 EXPECT_TRUE(boost::filesystem::exists(temp_dir.Path() /
"to/1"));
234 EXPECT_TRUE(boost::filesystem::exists(temp_dir.Path() /
"to/1/foo"));
235 EXPECT_TRUE(boost::filesystem::exists(temp_dir.Path() /
"to/1/2"));
236 EXPECT_TRUE(boost::filesystem::exists(temp_dir.Path() /
"to/1/2/bar"));
237 EXPECT_TRUE(boost::filesystem::exists(temp_dir.Path() /
"to/1/2/baz"));
238 EXPECT_EQ(Utils::readFile(temp_dir.Path() /
"to/1/foo"),
"foo");
239 EXPECT_EQ(Utils::readFile(temp_dir.Path() /
"to/1/2/bar"),
"bar");
240 EXPECT_EQ(Utils::readFile(temp_dir.Path() /
"to/1/2/baz"),
"baz");
243 TEST(
Utils, writeFileWithoutDirAutoCreation) {
246 boost::filesystem::create_directories(temp_dir.Path() /
"1/2");
247 Utils::writeFile(temp_dir.Path() /
"1/foo", std::string(
"foo"),
false);
248 Utils::writeFile(temp_dir.Path() /
"1/2/bar", std::string(
"bar"),
false);
250 EXPECT_EQ(Utils::readFile(temp_dir.Path() /
"1/foo"),
"foo");
251 EXPECT_EQ(Utils::readFile(temp_dir.Path() /
"1/2/bar"),
"bar");
254 TEST(
Utils, writeFileWithDirAutoCreation) {
257 Utils::writeFile(temp_dir.Path() /
"1/foo", std::string(
"foo"),
true);
258 Utils::writeFile(temp_dir.Path() /
"1/2/bar", std::string(
"bar"),
true);
260 EXPECT_EQ(Utils::readFile(temp_dir.Path() /
"1/foo"),
"foo");
261 EXPECT_EQ(Utils::readFile(temp_dir.Path() /
"1/2/bar"),
"bar");
264 TEST(
Utils, writeFileWithDirAutoCreationDefault) {
267 Utils::writeFile(temp_dir.Path() /
"1/foo", std::string(
"foo"));
268 Utils::writeFile(temp_dir.Path() /
"1/2/bar", std::string(
"bar"));
270 EXPECT_EQ(Utils::readFile(temp_dir.Path() /
"1/foo"),
"foo");
271 EXPECT_EQ(Utils::readFile(temp_dir.Path() /
"1/2/bar"),
"bar");
274 TEST(
Utils, writeFileWithoutDirAutoCreationException) {
278 Utils::writeFile(temp_dir.Path() /
"1/foo", std::string(
"foo"),
false);
290 Utils::writeFile(temp_dir.Path() /
"1/foo", val);
291 Json::Value result_json = Utils::parseJSONFile(temp_dir.Path() /
"1/foo");
292 EXPECT_EQ(result_json[
"key"].asString(), val[
"key"].asString());
296 int fd = socket(AF_INET6, SOCK_STREAM, 0);
299 SocketHandle hdl(
new int(fd));
303 memset(&sa, 0,
sizeof(sa));
304 sa.sin6_family = AF_INET6;
305 sa.sin6_port = htons(0);
306 sa.sin6_addr = IN6ADDR_ANY_INIT;
309 if (setsockopt(*hdl, SOL_SOCKET, SO_REUSEADDR, &reuseaddr,
sizeof(reuseaddr)) < 0) {
310 throw std::runtime_error(
"setsockopt(SO_REUSEADDR) failed");
313 EXPECT_NE(bind(*hdl, reinterpret_cast<const sockaddr *>(&sa),
sizeof(sa)), -1);
316 EXPECT_NO_THROW(ss = Utils::ipGetSockaddr(*hdl));
318 EXPECT_NE(Utils::ipDisplayName(ss),
"unknown");
319 EXPECT_NE(Utils::ipPort(ss), -1);
324 int statuscode = Utils::shell(
"ls /", &out);
325 EXPECT_EQ(statuscode, 0);
327 statuscode = Utils::shell(
"ls /nonexistentdir123", &out);
328 EXPECT_NE(statuscode, 0);
331 TEST(
Utils, urlencode) { EXPECT_EQ(Utils::urlEncode(
"test! test@ test#"),
"test%21%20test%40%20test%23"); }
337 EXPECT_EQ(bp.get(
"/"),
"/a/test.xml");
338 EXPECT_EQ(bp.get(
"/x"),
"/x/a/test.xml");
342 EXPECT_EQ(abp.get(
""),
"/a/test.xml");
343 EXPECT_EQ(abp.get(
"/root/var"),
"/a/test.xml");
348 std::string input =
"test with newline";
349 Utils::writeFile(f.Path(), input +
"\n");
350 std::string output = Utils::readFile(f.Path(),
false);
351 EXPECT_EQ(output, input +
"\n");
352 output = Utils::readFile(f.Path(),
true);
353 EXPECT_EQ(output, input);
357 int main(
int argc,
char **argv) {
358 ::testing::InitGoogleTest(&argc, argv);
359 return RUN_ALL_TESTS();
TEST(Utils, GenPrettyNameSane)
Check that aktualizr can generate a pet name.
RAII Temporary file creation.