5 #include <gtest/gtest.h> 13 #include <boost/algorithm/hex.hpp> 14 #include <boost/archive/iterators/dataflow_exception.hpp> 15 #include <boost/random/uniform_smallint.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&"));
51 Json::Value netinfo = Utils::getNetworkInfo();
53 EXPECT_NE(netinfo[
"local_ipv4"].asString(),
"");
54 EXPECT_NE(netinfo[
"mac"].asString(),
"");
55 EXPECT_NE(netinfo[
"hostname"].asString(),
"");
58 TEST(
Utils, getHostname) { EXPECT_NE(Utils::getHostname(),
""); }
64 std::set<std::string> names;
65 for (
int i = 0; i < 100; i++) {
66 std::string name = Utils::genPrettyName();
68 auto count = names.count(name);
70 std::cerr <<
"Something wrong with randomness: " << name;
72 }
else if (count == 2) {
73 std::cerr <<
"Lucky draw: " << name;
79 std::set<std::string> uuids;
80 for (
int i = 0; i < 1000; i++) {
81 std::string uuid = Utils::randomUuid();
82 EXPECT_EQ(0, uuids.count(uuid));
84 EXPECT_EQ(1, uuids.count(uuid));
90 EXPECT_EQ(
"aGVsbG8=", Utils::toBase64(
"hello"));
91 EXPECT_EQ(
"", Utils::toBase64(
""));
92 EXPECT_EQ(
"CQ==", Utils::toBase64(
"\t"));
93 EXPECT_EQ(
"YWI=", Utils::toBase64(
"ab"));
94 EXPECT_EQ(
"YWJj", Utils::toBase64(
"abc"));
98 EXPECT_EQ(Utils::fromBase64(
"aGVsbG8="),
"hello");
99 EXPECT_EQ(Utils::fromBase64(
""),
"");
100 EXPECT_EQ(Utils::fromBase64(
"YWI="),
"ab");
101 EXPECT_EQ(Utils::fromBase64(
"YWJj"),
"abc");
105 EXPECT_THROW(Utils::fromBase64(
"Привіт"), boost::archive::iterators::dataflow_exception);
106 EXPECT_THROW(Utils::fromBase64(
"aGVsbG8=="), boost::archive::iterators::dataflow_exception);
107 EXPECT_THROW(Utils::fromBase64(
"CQ==="), boost::archive::iterators::dataflow_exception);
112 boost::random::uniform_smallint<char> chars(std::numeric_limits<char>::min(), std::numeric_limits<char>::max());
114 boost::random::uniform_smallint<int> length(0, 20);
116 for (
int test = 0; test < 100; test++) {
117 int len = length(gen);
118 std::string original;
119 for (
int i = 0; i < len; i++) {
120 original += chars(gen);
122 std::string b64 = Utils::toBase64(original);
123 std::string output = Utils::fromBase64(b64);
124 EXPECT_EQ(original, output);
129 const std::string archive_path =
"tests/test_data/credentials.zip";
132 std::ifstream as(archive_path, std::ios::binary | std::ios::in);
133 EXPECT_FALSE(as.fail());
134 EXPECT_THROW(Utils::readFileFromArchive(as,
"bogus_filename"), std::runtime_error);
138 std::ifstream as(archive_path, std::ios::binary | std::ios::in);
139 EXPECT_FALSE(as.fail());
141 std::string url = Utils::readFileFromArchive(as,
"autoprov.url");
142 EXPECT_EQ(url.rfind(
"https://", 0), 0);
147 std::string archive_bytes;
149 std::map<std::string, std::string> fm{{
"test",
"A"}};
150 std::stringstream as;
151 Utils::writeArchive(fm, as);
152 archive_bytes = as.str();
156 std::stringstream as(archive_bytes);
157 EXPECT_EQ(Utils::readFileFromArchive(as,
"test"),
"A");
162 boost::filesystem::path p;
166 EXPECT_TRUE(boost::filesystem::exists(p));
167 EXPECT_NE(p.string().find(
"ahint"), std::string::npos);
170 stat(p.parent_path().c_str(), &statbuf);
171 EXPECT_EQ(statbuf.st_mode & (S_IRWXU | S_IRWXG | S_IRWXO), S_IRWXU);
173 EXPECT_FALSE(boost::filesystem::exists(p));
177 boost::filesystem::path p;
181 EXPECT_FALSE(boost::filesystem::exists(p));
182 std::ofstream file(p.c_str());
186 EXPECT_TRUE(boost::filesystem::exists(p));
187 EXPECT_NE(p.string().find(
"ahint"), std::string::npos);
190 stat(p.parent_path().c_str(), &statbuf);
191 EXPECT_EQ(statbuf.st_mode & (S_IRWXU | S_IRWXG | S_IRWXO), S_IRWXU);
193 EXPECT_FALSE(boost::filesystem::exists(p));
198 f.PutContents(
"thecontents");
199 EXPECT_TRUE(boost::filesystem::exists(f.Path()));
200 std::ifstream a(f.Path().c_str());
203 EXPECT_EQ(b,
"thecontents");
209 Utils::writeFile(temp_dir.Path() /
"from/1/foo", std::string(
"foo"));
210 Utils::writeFile(temp_dir.Path() /
"from/1/2/bar", std::string(
"bar"));
211 Utils::writeFile(temp_dir.Path() /
"from/1/2/baz", std::string(
"baz"));
213 Utils::copyDir(temp_dir.Path() /
"from", temp_dir.Path() /
"to");
214 EXPECT_TRUE(boost::filesystem::exists(temp_dir.Path() /
"to"));
215 EXPECT_TRUE(boost::filesystem::exists(temp_dir.Path() /
"to/1"));
216 EXPECT_TRUE(boost::filesystem::exists(temp_dir.Path() /
"to/1/foo"));
217 EXPECT_TRUE(boost::filesystem::exists(temp_dir.Path() /
"to/1/2"));
218 EXPECT_TRUE(boost::filesystem::exists(temp_dir.Path() /
"to/1/2/bar"));
219 EXPECT_TRUE(boost::filesystem::exists(temp_dir.Path() /
"to/1/2/baz"));
220 EXPECT_EQ(Utils::readFile(temp_dir.Path() /
"to/1/foo"),
"foo");
221 EXPECT_EQ(Utils::readFile(temp_dir.Path() /
"to/1/2/bar"),
"bar");
222 EXPECT_EQ(Utils::readFile(temp_dir.Path() /
"to/1/2/baz"),
"baz");
225 TEST(
Utils, writeFileWithoutDirAutoCreation) {
228 boost::filesystem::create_directories(temp_dir.Path() /
"1/2");
229 Utils::writeFile(temp_dir.Path() /
"1/foo", std::string(
"foo"),
false);
230 Utils::writeFile(temp_dir.Path() /
"1/2/bar", std::string(
"bar"),
false);
232 EXPECT_EQ(Utils::readFile(temp_dir.Path() /
"1/foo"),
"foo");
233 EXPECT_EQ(Utils::readFile(temp_dir.Path() /
"1/2/bar"),
"bar");
236 TEST(
Utils, writeFileWithDirAutoCreation) {
239 Utils::writeFile(temp_dir.Path() /
"1/foo", std::string(
"foo"),
true);
240 Utils::writeFile(temp_dir.Path() /
"1/2/bar", std::string(
"bar"),
true);
242 EXPECT_EQ(Utils::readFile(temp_dir.Path() /
"1/foo"),
"foo");
243 EXPECT_EQ(Utils::readFile(temp_dir.Path() /
"1/2/bar"),
"bar");
246 TEST(
Utils, writeFileWithDirAutoCreationDefault) {
249 Utils::writeFile(temp_dir.Path() /
"1/foo", std::string(
"foo"));
250 Utils::writeFile(temp_dir.Path() /
"1/2/bar", std::string(
"bar"));
252 EXPECT_EQ(Utils::readFile(temp_dir.Path() /
"1/foo"),
"foo");
253 EXPECT_EQ(Utils::readFile(temp_dir.Path() /
"1/2/bar"),
"bar");
256 TEST(
Utils, writeFileWithoutDirAutoCreationException) {
260 Utils::writeFile(temp_dir.Path() /
"1/foo", std::string(
"foo"),
false);
272 Utils::writeFile(temp_dir.Path() /
"1/foo", val);
273 Json::Value result_json = Utils::parseJSONFile(temp_dir.Path() /
"1/foo");
274 EXPECT_EQ(result_json[
"key"].asString(), val[
"key"].asString());
278 int fd = socket(AF_INET6, SOCK_STREAM, 0);
281 SocketHandle hdl(
new int(fd));
285 memset(&sa, 0,
sizeof(sa));
286 sa.sin6_family = AF_INET6;
287 sa.sin6_port = htons(0);
288 sa.sin6_addr = IN6ADDR_ANY_INIT;
291 if (setsockopt(*hdl, SOL_SOCKET, SO_REUSEADDR, &reuseaddr,
sizeof(reuseaddr)) < 0) {
292 throw std::runtime_error(
"setsockopt(SO_REUSEADDR) failed");
295 EXPECT_NE(bind(*hdl, reinterpret_cast<const sockaddr *>(&sa),
sizeof(sa)), -1);
298 EXPECT_NO_THROW(ss = Utils::ipGetSockaddr(*hdl));
300 EXPECT_NE(Utils::ipDisplayName(ss),
"unknown");
301 EXPECT_NE(Utils::ipPort(ss), -1);
306 int statuscode = Utils::shell(
"ls /", &out);
307 EXPECT_EQ(statuscode, 0);
309 statuscode = Utils::shell(
"ls /nonexistentdir123", &out);
310 EXPECT_NE(statuscode, 0);
317 EXPECT_EQ(bp.get(
"/"),
"/a/test.xml");
318 EXPECT_EQ(bp.get(
"/x"),
"/x/a/test.xml");
322 EXPECT_EQ(abp.get(
""),
"/a/test.xml");
323 EXPECT_EQ(abp.get(
"/root/var"),
"/a/test.xml");
327 int main(
int argc,
char **argv) {
328 ::testing::InitGoogleTest(&argc, argv);
329 return RUN_ALL_TESTS();
TEST(Utils, GenPrettyNameSane)
RAII Temporary file creation.