OpenVPN 3 Core Library
Loading...
Searching...
No Matches
test_path.cpp
Go to the documentation of this file.
1#include "test_common.hpp"
2
3#include <iostream>
4
6
7using namespace openvpn;
8
9// Ugly hack
10static std::stringstream out;
11
12void test_basename(const std::string &path)
13{
14 const std::string res = path::basename(path);
15 out << "basename('" << path << "') = '" << res << "'\n";
16}
17
18void dirname(const std::string &path)
19{
20 const std::string res = path::dirname(path);
21 out << "dirname('" << path << "') = '" << res << "'\n";
22}
23
24void ext(const std::string &path)
25{
26 const std::string res = path::ext(path);
27 out << "ext('" << path << "') = '" << res << "'\n";
28}
29
30void is_flat(const std::string &path)
31{
32 const bool res = path::is_flat(path);
33 out << "is_flat('" << path << "') = " << res << '\n';
34}
35
36void join(const std::string &p1, const std::string &p2)
37{
38 const std::string res = path::join(p1, p2);
39 out << "join('" << p1 << "', '" << p2 << "') = '" << res << "'\n";
40}
41
42void join3(const std::string &p1, const std::string &p2, const std::string &p3)
43{
44 const std::string res = path::join(p1, p2, p3);
45 out << "join('" << p1 << "', '" << p2 << "', '" << p3 << "') = '" << res << "'\n";
46}
47
48void join4(const std::string &p1, const std::string &p2, const std::string &p3, const std::string &p4)
49{
50 const std::string res = path::join(p1, p2, p3, p4);
51 out << "join('" << p1 << "', '" << p2 << "', '" << p3 << "', '" << p4 << "') = '" << res << "'\n";
52}
53
54void splitjoin(const std::string &p1)
55{
56 const std::string d = path::dirname(p1);
57 const std::string b = path::basename(p1);
58 const std::string p2 = path::join(d, b);
59 out << "splitjoin p1='" << p1 << "' dir='" << d << "' bn='" << b << "' p2='" << p2 << "'\n";
60}
61
62TEST(Path, Test1)
63{
64 out.clear();
65 out.str("");
66 // basename
67 test_basename("");
68 test_basename("/");
69 test_basename("/foo");
70 test_basename("/foo/bar");
71 test_basename("foo/bar/boo");
72 test_basename("foo/bar/");
73 test_basename("foo\\bar\\boo");
74
75 // dirname
76 dirname("");
77 dirname("/");
78 dirname("/foo");
79 dirname("/foo/bar");
80 dirname("foo/bar/boo");
81 dirname("foo/bar/");
82 dirname("foo\\bar\\boo");
83
84 // is_flat
85 is_flat("");
86 is_flat("/");
87 is_flat("foo.bar");
88 is_flat("foo/bar");
89 is_flat("c:/foo");
90 is_flat("c:foo");
91 is_flat("z:\\foo");
92 is_flat(".");
93 is_flat("..");
94 is_flat("./foo");
95
96 // join
97 join("foo", "bar");
98 join("foo", "");
99 join("", "foo/bar");
100 join("", "bar");
101 join("foo", "/bar");
102 join("/", "bar");
103
104 // join (3 or more parms)
105 join3("", "", "three");
106 join3("one", "two", "three");
107 join3("one", "/two", "three");
108 join4("one", "two", "three", "four");
109 join4("one", "two", "", "four");
110
111 // ext
112 ext("");
113 ext("foo");
114 ext("foo.bar");
115 ext("foo.bar.moo");
116 ext("foo.");
117 ext(".foo");
118
119 // splitjoin
120 splitjoin("");
121 splitjoin("/");
122 splitjoin("/foo");
123 splitjoin("/foo/");
124 splitjoin("/foo/bar");
125 splitjoin("/foo/bar/");
126
127#ifdef WIN32
128 ASSERT_EQ(getExpectedOutput("test_path_win32.txt"), out.str());
129#else
130 ASSERT_EQ(getExpectedOutput("test_path.txt"), out.str());
131#endif
132}
133
134void test_contained(const std::string &path, const bool expected)
135{
136 const bool contained = path::is_contained(path);
137 ASSERT_EQ(contained, expected);
138}
139
140TEST(Path, Test2)
141{
142 test_contained("", false);
143 test_contained(".", true);
144 test_contained("..", false);
145 test_contained("..x", true);
146 test_contained("x..", true);
147 test_contained("...", true);
148 test_contained("../", false);
149 test_contained("/..", false);
150 test_contained("/foo", false);
151 test_contained("foo", true);
152 test_contained("foo/bar", true);
153 test_contained("foo//bar", true);
154 test_contained("foo/bar/", true);
155 test_contained("foo/bar//", true);
156 test_contained("..foo", true);
157 test_contained(".foo", true);
158 test_contained("./foo", true);
159 test_contained("../foo", false);
160 test_contained("..//foo", false);
161 test_contained(".../foo", true);
162 test_contained("foo/..", false);
163 test_contained("foo/.", true);
164 test_contained("foo//..", false);
165 test_contained("foo/...", true);
166 test_contained("foo/./bar", true);
167 test_contained("foo/../bar", false);
168 test_contained("foo/.../bar", true);
169}
std::string ext(const std::string &basename)
Definition path.hpp:152
bool is_contained(const std::string &path)
Definition path.hpp:102
std::string dirname(const std::string &path)
Definition path.hpp:89
std::string basename(const std::string &path)
Definition path.hpp:76
std::string join(const std::string &p1, const std::string &p2)
Definition path.hpp:173
bool is_flat(const std::string &path)
Definition path.hpp:67
const std::string expected
std::string getExpectedOutput(const std::string &filename)
void join4(const std::string &p1, const std::string &p2, const std::string &p3, const std::string &p4)
Definition test_path.cpp:48
void is_flat(const std::string &path)
Definition test_path.cpp:30
void dirname(const std::string &path)
Definition test_path.cpp:18
void test_contained(const std::string &path, const bool expected)
void ext(const std::string &path)
Definition test_path.cpp:24
TEST(Path, Test1)
Definition test_path.cpp:62
void join3(const std::string &p1, const std::string &p2, const std::string &p3)
Definition test_path.cpp:42
void join(const std::string &p1, const std::string &p2)
Definition test_path.cpp:36
static std::stringstream out
Definition test_path.cpp:10
void test_basename(const std::string &path)
Definition test_path.cpp:12
void splitjoin(const std::string &p1)
Definition test_path.cpp:54