Ensure the profile path is pointing to a directory
[qBittorrent.git] / test / testpath.cpp
blob50ee84f66f2e38b2fc9b1fb0f557e30f28b8cf22
1 /*
2 * Bittorrent Client using Qt and libtorrent.
3 * Copyright (C) 2022 Mike Tzou (Chocobo1)
5 * This program is free software; you can redistribute it and/or
6 * modify it under the terms of the GNU General Public License
7 * as published by the Free Software Foundation; either version 2
8 * of the License, or (at your option) any later version.
10 * This program is distributed in the hope that it will be useful,
11 * but WITHOUT ANY WARRANTY; without even the implied warranty of
12 * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
13 * GNU General Public License for more details.
15 * You should have received a copy of the GNU General Public License
16 * along with this program; if not, write to the Free Software
17 * Foundation, Inc., 51 Franklin Street, Fifth Floor, Boston, MA 02110-1301, USA.
19 * In addition, as a special exception, the copyright holders give permission to
20 * link this program with the OpenSSL project's "OpenSSL" library (or with
21 * modified versions of it that use the same license as the "OpenSSL" library),
22 * and distribute the linked executables. You must obey the GNU General Public
23 * License in all respects for all of the code used other than "OpenSSL". If you
24 * modify file(s), you may extend this exception to your version of the file(s),
25 * but you are not obligated to do so. If you do not wish to do so, delete this
26 * exception statement from your version.
29 #include <QtGlobal>
30 #include <QTest>
32 #include "base/global.h"
33 #include "base/path.h"
35 class TestPath final : public QObject
37 Q_OBJECT
38 Q_DISABLE_COPY_MOVE(TestPath)
40 public:
41 TestPath() = default;
43 private slots:
44 void testConstructors() const
46 QVERIFY(Path(u""_s) == Path(std::string("")));
47 QVERIFY(Path(u"abc"_s) == Path(std::string("abc")));
48 QVERIFY(Path(u"/abc"_s) == Path(std::string("/abc")));
49 QVERIFY(Path(uR"(\abc)"_s) == Path(std::string(R"(\abc)")));
51 #ifdef Q_OS_WIN
52 QVERIFY(Path(uR"(c:)"_s) == Path(std::string(R"(c:)")));
53 QVERIFY(Path(uR"(c:/)"_s) == Path(std::string(R"(c:/)")));
54 QVERIFY(Path(uR"(c:/)"_s) == Path(std::string(R"(c:\)")));
55 QVERIFY(Path(uR"(c:\)"_s) == Path(std::string(R"(c:/)")));
56 QVERIFY(Path(uR"(c:\)"_s) == Path(std::string(R"(c:\)")));
58 QVERIFY(Path(uR"(\\?\C:)"_s) == Path(std::string(R"(\\?\C:)")));
59 QVERIFY(Path(uR"(\\?\C:/)"_s) == Path(std::string(R"(\\?\C:/)")));
60 QVERIFY(Path(uR"(\\?\C:/)"_s) == Path(std::string(R"(\\?\C:\)")));
61 QVERIFY(Path(uR"(\\?\C:\)"_s) == Path(std::string(R"(\\?\C:/)")));
62 QVERIFY(Path(uR"(\\?\C:\)"_s) == Path(std::string(R"(\\?\C:\)")));
64 QVERIFY(Path(uR"(\\?\C:\abc)"_s) == Path(std::string(R"(\\?\C:\abc)")));
65 #endif
68 void testIsValid() const
70 QCOMPARE(Path().isValid(), false);
71 QCOMPARE(Path(u""_s).isValid(), false);
73 QCOMPARE(Path(u"/"_s).isValid(), true);
74 QCOMPARE(Path(uR"(\)"_s).isValid(), true);
76 QCOMPARE(Path(u"a"_s).isValid(), true);
77 QCOMPARE(Path(u"/a"_s).isValid(), true);
78 QCOMPARE(Path(uR"(\a)"_s).isValid(), true);
80 QCOMPARE(Path(u"a/b"_s).isValid(), true);
81 QCOMPARE(Path(uR"(a\b)"_s).isValid(), true);
82 QCOMPARE(Path(u"/a/b"_s).isValid(), true);
83 QCOMPARE(Path(uR"(/a\b)"_s).isValid(), true);
84 QCOMPARE(Path(uR"(\a/b)"_s).isValid(), true);
85 QCOMPARE(Path(uR"(\a\b)"_s).isValid(), true);
87 QCOMPARE(Path(u"//"_s).isValid(), true);
88 QCOMPARE(Path(uR"(\\)"_s).isValid(), true);
89 QCOMPARE(Path(u"//a"_s).isValid(), true);
90 QCOMPARE(Path(uR"(\\a)"_s).isValid(), true);
92 #if defined Q_OS_MACOS
93 QCOMPARE(Path(u"\0"_s).isValid(), false);
94 QCOMPARE(Path(u":"_s).isValid(), false);
95 #elif defined Q_OS_WIN
96 QCOMPARE(Path(u"c:"_s).isValid(), false);
97 QCOMPARE(Path(u"c:/"_s).isValid(), true);
98 QCOMPARE(Path(uR"(c:\)"_s).isValid(), true);
99 QCOMPARE(Path(uR"(c:\a)"_s).isValid(), true);
100 QCOMPARE(Path(uR"(c:\a\b)"_s).isValid(), true);
102 for (int i = 0; i <= 31; ++i)
103 QCOMPARE(Path(QChar(i)).isValid(), false);
104 QCOMPARE(Path(u":"_s).isValid(), false);
105 QCOMPARE(Path(u"?"_s).isValid(), false);
106 QCOMPARE(Path(u"\""_s).isValid(), false);
107 QCOMPARE(Path(u"*"_s).isValid(), false);
108 QCOMPARE(Path(u"<"_s).isValid(), false);
109 QCOMPARE(Path(u">"_s).isValid(), false);
110 QCOMPARE(Path(u"|"_s).isValid(), false);
111 #else
112 QCOMPARE(Path(u"\0"_s).isValid(), false);
113 #endif
116 void testIsEmpty() const
118 QCOMPARE(Path().isEmpty(), true);
119 QCOMPARE(Path(u""_s).isEmpty(), true);
121 QCOMPARE(Path(u"\0"_s).isEmpty(), false);
123 QCOMPARE(Path(u"/"_s).isEmpty(), false);
124 QCOMPARE(Path(uR"(\)"_s).isEmpty(), false);
126 QCOMPARE(Path(u"a"_s).isEmpty(), false);
127 QCOMPARE(Path(u"/a"_s).isEmpty(), false);
128 QCOMPARE(Path(uR"(\a)"_s).isEmpty(), false);
130 QCOMPARE(Path(uR"(c:)"_s).isEmpty(), false);
131 QCOMPARE(Path(uR"(c:/)"_s).isEmpty(), false);
132 QCOMPARE(Path(uR"(c:\)"_s).isEmpty(), false);
135 void testIsAbsolute() const
137 QCOMPARE(Path().isAbsolute(), false);
138 QCOMPARE(Path(u""_s).isAbsolute(), false);
140 #ifdef Q_OS_WIN
141 QCOMPARE(Path(u"/"_s).isAbsolute(), true);
142 QCOMPARE(Path(uR"(\)"_s).isAbsolute(), true);
144 QCOMPARE(Path(u"a"_s).isAbsolute(), false);
145 QCOMPARE(Path(u"/a"_s).isAbsolute(), true);
146 QCOMPARE(Path(uR"(\a)"_s).isAbsolute(), true);
148 QCOMPARE(Path(u"//"_s).isAbsolute(), true);
149 QCOMPARE(Path(uR"(\\)"_s).isAbsolute(), true);
150 QCOMPARE(Path(u"//a"_s).isAbsolute(), true);
151 QCOMPARE(Path(uR"(\\a)"_s).isAbsolute(), true);
153 QCOMPARE(Path(uR"(c:)"_s).isAbsolute(), true);
154 QCOMPARE(Path(uR"(c:/)"_s).isAbsolute(), true);
155 QCOMPARE(Path(uR"(c:\)"_s).isAbsolute(), true);
156 QCOMPARE(Path(uR"(c:\a)"_s).isAbsolute(), true);
157 QCOMPARE(Path(uR"(c:\a\b)"_s).isAbsolute(), true);
159 QCOMPARE(Path(uR"(\\?\C:)"_s).isAbsolute(), true);
160 QCOMPARE(Path(uR"(\\?\C:/)"_s).isAbsolute(), true);
161 QCOMPARE(Path(uR"(\\?\C:\)"_s).isAbsolute(), true);
162 QCOMPARE(Path(uR"(\\?\C:\a)"_s).isAbsolute(), true);
163 QCOMPARE(Path(uR"(\\?\C:\a\b)"_s).isAbsolute(), true);
164 #else
165 QCOMPARE(Path(u"/"_s).isAbsolute(), true);
166 QCOMPARE(Path(uR"(\)"_s).isAbsolute(), false);
168 QCOMPARE(Path(u"a"_s).isAbsolute(), false);
169 QCOMPARE(Path(u"/a"_s).isAbsolute(), true);
170 QCOMPARE(Path(uR"(\a)"_s).isAbsolute(), false);
172 QCOMPARE(Path(u"//"_s).isAbsolute(), true);
173 QCOMPARE(Path(uR"(\\)"_s).isAbsolute(), false);
174 QCOMPARE(Path(u"//a"_s).isAbsolute(), true);
175 QCOMPARE(Path(uR"(\\a)"_s).isAbsolute(), false);
176 #endif
179 void testIsRelative() const
181 QCOMPARE(Path().isRelative(), true);
182 QCOMPARE(Path(u""_s).isRelative(), true);
184 #if defined Q_OS_WIN
185 QCOMPARE(Path(u"/"_s).isRelative(), false);
186 QCOMPARE(Path(uR"(\)"_s).isRelative(), false);
188 QCOMPARE(Path(u"a"_s).isRelative(), true);
189 QCOMPARE(Path(u"/a"_s).isRelative(), false);
190 QCOMPARE(Path(uR"(\a)"_s).isRelative(), false);
192 QCOMPARE(Path(u"//"_s).isRelative(), false);
193 QCOMPARE(Path(uR"(\\)"_s).isRelative(), false);
194 QCOMPARE(Path(u"//a"_s).isRelative(), false);
195 QCOMPARE(Path(uR"(\\a)"_s).isRelative(), false);
197 QCOMPARE(Path(uR"(c:)"_s).isRelative(), false);
198 QCOMPARE(Path(uR"(c:/)"_s).isRelative(), false);
199 QCOMPARE(Path(uR"(c:\)"_s).isRelative(), false);
200 QCOMPARE(Path(uR"(c:\a)"_s).isRelative(), false);
201 QCOMPARE(Path(uR"(c:\a\b)"_s).isRelative(), false);
203 QCOMPARE(Path(uR"(\\?\C:)"_s).isRelative(), false);
204 QCOMPARE(Path(uR"(\\?\C:/)"_s).isRelative(), false);
205 QCOMPARE(Path(uR"(\\?\C:\)"_s).isRelative(), false);
206 QCOMPARE(Path(uR"(\\?\C:\a)"_s).isRelative(), false);
207 QCOMPARE(Path(uR"(\\?\C:\a\b)"_s).isRelative(), false);
208 #else
209 QCOMPARE(Path(u"/"_s).isRelative(), false);
210 QCOMPARE(Path(uR"(\)"_s).isRelative(), true);
212 QCOMPARE(Path(u"a"_s).isRelative(), true);
213 QCOMPARE(Path(u"/a"_s).isRelative(), false);
214 QCOMPARE(Path(uR"(\a)"_s).isRelative(), true);
216 QCOMPARE(Path(u"//"_s).isRelative(), false);
217 QCOMPARE(Path(uR"(\\)"_s).isRelative(), true);
218 QCOMPARE(Path(u"//a"_s).isRelative(), false);
219 QCOMPARE(Path(uR"(\\a)"_s).isRelative(), true);
220 #endif
223 void testRootItem() const
225 QCOMPARE(Path().rootItem(), Path());
226 QCOMPARE(Path(u""_s).rootItem(), Path());
228 QCOMPARE(Path(u"/"_s).rootItem(), Path(u"/"_s));
229 QCOMPARE(Path(uR"(\)"_s).rootItem(), Path(uR"(\)"_s));
231 QCOMPARE(Path(u"a"_s).rootItem(), Path(u"a"_s));
232 QCOMPARE(Path(u"/a"_s).rootItem(), Path(u"/"_s));
233 QCOMPARE(Path(u"/a/b"_s).rootItem(), Path(u"/"_s));
235 QCOMPARE(Path(u"//"_s).rootItem(), Path(u"/"_s));
236 QCOMPARE(Path(uR"(\\)"_s).rootItem(), Path(uR"(\\)"_s));
237 QCOMPARE(Path(u"//a"_s).rootItem(), Path(u"/"_s));
239 #ifdef Q_OS_WIN
240 QCOMPARE(Path(uR"(\a)"_s).rootItem(), Path(uR"(\)"_s));
241 QCOMPARE(Path(uR"(\\a)"_s).rootItem(), Path(uR"(\)"_s));
243 QCOMPARE(Path(uR"(c:)"_s).rootItem(), Path(uR"(c:)"_s));
244 QCOMPARE(Path(uR"(c:/)"_s).rootItem(), Path(uR"(c:/)"_s));
245 QCOMPARE(Path(uR"(c:\)"_s).rootItem(), Path(uR"(c:\)"_s));
246 QCOMPARE(Path(uR"(c:\)"_s).rootItem(), Path(uR"(c:/)"_s));
247 QCOMPARE(Path(uR"(c:\a)"_s).rootItem(), Path(uR"(c:\)"_s));
248 QCOMPARE(Path(uR"(c:\a\b)"_s).rootItem(), Path(uR"(c:\)"_s));
249 #else
250 QCOMPARE(Path(uR"(\a)"_s).rootItem(), Path(uR"(\a)"_s));
251 QCOMPARE(Path(uR"(\\a)"_s).rootItem(), Path(uR"(\\a)"_s));
252 #endif
255 void testParentPath() const
257 QCOMPARE(Path().parentPath(), Path());
258 QCOMPARE(Path(u""_s).parentPath(), Path());
260 QCOMPARE(Path(u"/"_s).parentPath(), Path());
261 QCOMPARE(Path(uR"(\)"_s).parentPath(), Path());
263 QCOMPARE(Path(u"a"_s).parentPath(), Path());
264 QCOMPARE(Path(u"/a"_s).parentPath(), Path(u"/"_s));
266 QCOMPARE(Path(u"//"_s).parentPath(), Path());
267 QCOMPARE(Path(uR"(\\)"_s).parentPath(), Path());
268 QCOMPARE(Path(u"//a"_s).parentPath(), Path(u"/"_s));
270 QCOMPARE(Path(u"a/b"_s).parentPath(), Path(u"a"_s));
272 #ifdef Q_OS_WIN
273 QCOMPARE(Path(uR"(\a)"_s).parentPath(), Path(uR"(\)"_s));
274 QCOMPARE(Path(uR"(\\a)"_s).parentPath(), Path(uR"(\)"_s));
275 QCOMPARE(Path(uR"(a\b)"_s).parentPath(), Path(u"a"_s));
277 QCOMPARE(Path(uR"(c:)"_s).parentPath(), Path());
278 QCOMPARE(Path(uR"(c:/)"_s).parentPath(), Path());
279 QCOMPARE(Path(uR"(c:\)"_s).parentPath(), Path());
280 QCOMPARE(Path(uR"(c:\a)"_s).parentPath(), Path(uR"(c:\)"_s));
281 QCOMPARE(Path(uR"(c:\a\b)"_s).parentPath(), Path(uR"(c:\a)"_s));
282 #else
283 QCOMPARE(Path(uR"(\a)"_s).parentPath(), Path());
284 QCOMPARE(Path(uR"(\\a)"_s).parentPath(), Path());
285 QCOMPARE(Path(uR"(a\b)"_s).parentPath(), Path());
286 #endif
289 // TODO: add tests for remaining methods
292 QTEST_APPLESS_MAIN(TestPath)
293 #include "testpath.moc"