0
0
mirror of https://github.com/OpenVPN/openvpn3.git synced 2024-09-20 12:12:15 +02:00
openvpn3/test/misc/path.cpp
James Yonan dccfd5bc09 [test/misc] testing: added jytest unit test wrapper
See test/jytest/README.txt for documentation.

Signed-off-by: James Yonan <james@openvpn.net>
2022-07-01 15:57:30 +02:00

195 lines
4.7 KiB
C++

// TEST : {"cmd": "./go path", "expected_output": "path.txt"}
#include <iostream>
//#define OPENVPN_PATH_SIMULATE_WINDOWS
#include <openvpn/log/logsimple.hpp>
#include <openvpn/common/size.hpp>
#include <openvpn/common/exception.hpp>
#include <openvpn/common/path.hpp>
using namespace openvpn;
void basename(const std::string& path)
{
const std::string res = path::basename(path);
std::cout << "basename('" << path << "') = '" << res << "'" << std::endl;
}
void dirname(const std::string& path)
{
const std::string res = path::dirname(path);
std::cout << "dirname('" << path << "') = '" << res << "'" << std::endl;
}
void ext(const std::string& path)
{
const std::string res = path::ext(path);
std::cout << "ext('" << path << "') = '" << res << "'" << std::endl;
}
void is_flat(const std::string& path)
{
const bool res = path::is_flat(path);
std::cout << "is_flat('" << path << "') = " << res << std::endl;
}
void join(const std::string& p1, const std::string& p2)
{
const std::string res = path::join(p1, p2);
std::cout << "join('" << p1 << "', '" << p2 << "') = '" << res << "'" << std::endl;
}
void join3(const std::string& p1, const std::string& p2, const std::string& p3)
{
const std::string res = path::join(p1, p2, p3);
std::cout << "join('" << p1 << "', '" << p2 << "', '" << p3 << "') = '" << res << "'" << std::endl;
}
void join4(const std::string& p1, const std::string& p2, const std::string& p3, const std::string& p4)
{
const std::string res = path::join(p1, p2, p3, p4);
std::cout << "join('" << p1 << "', '" << p2 << "', '" << p3 << "', '" << p4 << "') = '" << res << "'" << std::endl;
}
void splitjoin(const std::string& p1)
{
const std::string d = path::dirname(p1);
const std::string b = path::basename(p1);
const std::string p2 = path::join(d, b);
std::cout << "splitjoin p1='" << p1 << "' dir='" << d << "' bn='" << b << "' p2='" << p2 << "'" << std::endl;
}
void test1()
{
OPENVPN_LOG("======= TEST1 =======");
// basename
basename("");
basename("/");
basename("/foo");
basename("/foo/bar");
basename("foo/bar/boo");
basename("foo/bar/");
basename("foo\\bar\\boo");
// dirname
dirname("");
dirname("/");
dirname("/foo");
dirname("/foo/bar");
dirname("foo/bar/boo");
dirname("foo/bar/");
dirname("foo\\bar\\boo");
// is_flat
is_flat("");
is_flat("/");
is_flat("foo.bar");
is_flat("foo/bar");
is_flat("c:/foo");
is_flat("c:foo");
is_flat("z:\\foo");
is_flat(".");
is_flat("..");
is_flat("./foo");
// join
join("foo", "bar");
join("foo", "");
join("", "foo/bar");
join("", "bar");
join("foo", "/bar");
join("/", "bar");
// join (3 or more parms)
join3("", "", "three");
join3("one", "two", "three");
join3("one", "/two", "three");
join4("one", "two", "three", "four");
join4("one", "two", "", "four");
// ext
ext("");
ext("foo");
ext("foo.bar");
ext("foo.bar.moo");
ext("foo.");
ext(".foo");
// splitjoin
splitjoin("");
splitjoin("/");
splitjoin("/foo");
splitjoin("/foo/");
splitjoin("/foo/bar");
splitjoin("/foo/bar/");
}
void test_contained(const std::string& path, const bool expected)
{
const bool contained = path::is_contained(path);
OPENVPN_LOG("is_contained('" << path << "') = " << contained);
if (contained != expected)
OPENVPN_THROW_EXCEPTION("contained=" << contained << " expected=" << expected);
}
void test2()
{
OPENVPN_LOG("======= TEST2 =======");
test_contained("", false);
test_contained(".", true);
test_contained("..", false);
test_contained("..x", true);
test_contained("x..", true);
test_contained("...", true);
test_contained("../", false);
test_contained("/..", false);
test_contained("/foo", false);
test_contained("foo", true);
test_contained("foo/bar", true);
test_contained("foo//bar", true);
test_contained("foo/bar/", true);
test_contained("foo/bar//", true);
test_contained("..foo", true);
test_contained(".foo", true);
test_contained("./foo", true);
test_contained("../foo", false);
test_contained("..//foo", false);
test_contained(".../foo", true);
test_contained("foo/..", false);
test_contained("foo/.", true);
test_contained("foo//..", false);
test_contained("foo/...", true);
test_contained("foo/./bar", true);
test_contained("foo/../bar", false);
test_contained("foo/.../bar", true);
}
void test_join_speed()
{
size_t count = 0;
for (int i = 0; i < 10000000; ++i)
{
const std::string s = path::join("one", "two", "three", "four");
count += s.length();
}
std::cout << count << std::endl;
}
int main()
{
try {
test1();
test2();
//test_join_speed();
}
catch (const std::exception& e)
{
std::cerr << "Exception: " << e.what() << std::endl;
return 1;
}
return 0;
}