Falkon Develop
Cross-platform Qt-based web browser
qztoolstest.cpp
Go to the documentation of this file.
1/* ============================================================
2* Falkon - Qt web browser
3* Copyright (C) 2013-2014 David Rosca <nowrep@gmail.com>
4*
5* This program is free software: you can redistribute it and/or modify
6* it under the terms of the GNU General Public License as published by
7* the Free Software Foundation, either version 3 of the License, or
8* (at your option) any later version.
9*
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.
14*
15* You should have received a copy of the GNU General Public License
16* along with this program. If not, see <http://www.gnu.org/licenses/>.
17* ============================================================ */
18#include "qztoolstest.h"
19#include "qztools.h"
20
21#include <QDir>
22#include <QtTest/QtTest>
23
24void QzToolsTest::initTestCase()
25{
26 m_tmpPath = QDir::tempPath() + QL1S("/falkon-test/qztoolstest");
27 QDir().mkpath(m_tmpPath);
28
29 QVERIFY(QDir(m_tmpPath).exists());
30}
31
32void QzToolsTest::cleanupTestCase()
33{
34 QDir().rmpath(m_tmpPath);
35
36 QVERIFY(!QDir(m_tmpPath).exists());
37}
38
39void QzToolsTest::samePartOfStrings_data()
40{
41 QTest::addColumn<QString>("string1");
42 QTest::addColumn<QString>("string2");
43 QTest::addColumn<QString>("result");
44
45 // Lorem ipsum dolor sit amet, consectetur adipiscing elit.
46 QTest::newRow("General") << "Lorem ipsum dolor" << "Lorem ipsum dolor Test_1" << "Lorem ipsum dolor";
47 QTest::newRow("OneChar") << "L" << "LTest_1" << "L";
48 QTest::newRow("EmptyReturn") << "Lorem ipsum dolor" << "orem ipsum dolor Test_1" << "";
49 QTest::newRow("EmptyString1") << "" << "orem ipsum dolor Test_1" << "";
50 QTest::newRow("EmptyString2") << "Lorem ipsum dolor" << "" << "";
51 QTest::newRow("EmptyBoth") << "" << "" << "";
52}
53
54void QzToolsTest::samePartOfStrings()
55{
56 QFETCH(QString, string1);
57 QFETCH(QString, string2);
58 QFETCH(QString, result);
59
60 QCOMPARE(QzTools::samePartOfStrings(string1, string2), result);
61}
62
63void QzToolsTest::getFileNameFromUrl_data()
64{
65 QTest::addColumn<QUrl>("url");
66 QTest::addColumn<QString>("result");
67
68 QTest::newRow("Basic") << QUrl(QSL("http://www.google.com/filename.html")) << QSL("filename.html");
69 QTest::newRow("OnlyHost") << QUrl(QSL("http://www.google.com/")) << QSL("www.google.com");
70 QTest::newRow("OnlyHostWithoutSlash") << QUrl(QSL("http://www.google.com")) << QSL("www.google.com");
71 QTest::newRow("EndingDirectory") << QUrl(QSL("http://www.google.com/filename/")) << QSL("filename");
72 QTest::newRow("EmptyUrl") << QUrl(QSL("")) << QSL("");
73 QTest::newRow("OnlyScheme") << QUrl(QSL("http:")) << QSL("");
74 QTest::newRow("FileSchemeUrl") << QUrl(QSL("file:///usr/share/test/file.tx")) << QSL("file.tx");
75 QTest::newRow("FileSchemeUrlDirectory") << QUrl(QSL("file:///usr/share/test/")) << QSL("test");
76 QTest::newRow("FileSchemeUrlRoot") << QUrl(QSL("file:///")) << QSL("");
77}
78
79void QzToolsTest::getFileNameFromUrl()
80{
81 QFETCH(QUrl, url);
82 QFETCH(QString, result);
83
84 QCOMPARE(QzTools::getFileNameFromUrl(url), result);
85}
86
87void QzToolsTest::splitCommandArguments_data()
88{
89 QTest::addColumn<QString>("command");
90 QTest::addColumn<QStringList>("result");
91
92 QTest::newRow("Basic") << "/usr/bin/foo -o foo.out"
93 << (QStringList() << QSL("/usr/bin/foo") << QSL("-o") << QSL("foo.out"));
94 QTest::newRow("Empty") << QString()
95 << QStringList();
96 QTest::newRow("OnlySpaces") << QSL(" ")
97 << QStringList();
98 QTest::newRow("OnlyQuotes") << QSL(R"("" "")")
99 << QStringList();
100 QTest::newRow("EmptyQuotesAndSpace") << QSL(R"("" "" " ")")
101 << QStringList(QSL(" "));
102 QTest::newRow("MultipleSpaces") << " /usr/foo -o foo.out "
103 << (QStringList() << QSL("/usr/foo") << QSL("-o") << QSL("foo.out"));
104 QTest::newRow("Quotes") << R"("/usr/foo" "-o" "foo.out")"
105 << (QStringList() << QSL("/usr/foo") << QSL("-o") << QSL("foo.out"));
106 QTest::newRow("SingleQuotes") << "'/usr/foo' '-o' 'foo.out'"
107 << (QStringList() << QSL("/usr/foo") << QSL("-o") << QSL("foo.out"));
108 QTest::newRow("SingleAndDoubleQuotes") << " '/usr/foo' \"-o\" 'foo.out' "
109 << (QStringList() << QSL("/usr/foo") << QSL("-o") << QSL("foo.out"));
110 QTest::newRow("SingleInDoubleQuotes") << "/usr/foo \"-o 'ds' \" 'foo.out' "
111 << (QStringList() << QSL("/usr/foo") << QSL("-o 'ds' ") << QSL("foo.out"));
112 QTest::newRow("DoubleInSingleQuotes") << "/usr/foo -o 'foo\" d \".out' "
113 << (QStringList() << QSL("/usr/foo") << QSL("-o") << QSL("foo\" d \".out"));
114 QTest::newRow("SpacesWithQuotes") << QSL(R"( " " " " )")
115 << (QStringList() << QSL(" ") << QSL(" "));
116 QTest::newRow("QuotesAndSpaces") << "/usr/foo -o \"foo - out\""
117 << (QStringList() << QSL("/usr/foo") << QSL("-o") << QSL("foo - out"));
118 QTest::newRow("EqualAndQuotes") << "/usr/foo -o=\"foo - out\""
119 << (QStringList() << QSL("/usr/foo") << QSL("-o=foo - out"));
120 QTest::newRow("EqualWithSpaces") << "/usr/foo -o = \"foo - out\""
121 << (QStringList() << QSL("/usr/foo") << QSL("-o") << QSL("=") << QSL("foo - out"));
122 QTest::newRow("MultipleSpacesAndQuotes") << " /usr/foo -o=\" foo.out \" "
123 << (QStringList() << QSL("/usr/foo") << QSL("-o= foo.out "));
124 // Unmatched quotes should be treated as an error
125 QTest::newRow("UnmatchedQuote") << "/usr/bin/foo -o \"bar"
126 << QStringList();
127}
128
129void QzToolsTest::splitCommandArguments()
130{
131 QFETCH(QString, command);
132 QFETCH(QStringList, result);
133
134 QCOMPARE(QzTools::splitCommandArguments(command), result);
135}
136
137void QzToolsTest::escapeSqlGlobString_data()
138{
139 QTest::addColumn<QString>("input");
140 QTest::addColumn<QString>("result");
141
142 QTest::newRow("NothingToEscape") << "http://test" << "http://test";
143 QTest::newRow("Escape *") << "http://test*/heh" << "http://test[*]/heh";
144 QTest::newRow("Escape **") << "http://test**/he*h" << "http://test[*][*]/he[*]h";
145 QTest::newRow("Escape ?") << "http://test?/heh" << "http://test[?]/heh";
146 QTest::newRow("Escape ??") << "http://t??est?/heh" << "http://t[?][?]est[?]/heh";
147 QTest::newRow("Escape [") << "http://[test/heh" << "http://[[]test/heh";
148 QTest::newRow("Escape [[") << "http://[[te[st/heh" << "http://[[][[]te[[]st/heh";
149 QTest::newRow("Escape ]") << "http://]test/heh" << "http://[]]test/heh";
150 QTest::newRow("Escape ]]") << "http://]]te]st/heh" << "http://[]][]]te[]]st/heh";
151 QTest::newRow("Escape []") << "http://[]test/heh" << "http://[[][]]test/heh";
152 QTest::newRow("Escape [][[]][]") << "http://t[][[]][]est/heh" << "http://t[[][]][[][[][]][]][[][]]est/heh";
153 QTest::newRow("Escape [?]][[*]") << "http://t[?]][[*]est/heh" << "http://t[[][?][]][]][[][[][*][]]est/heh";
154}
155
156void QzToolsTest::escapeSqlGlobString()
157{
158 QFETCH(QString, input);
159 QFETCH(QString, result);
160
161 QCOMPARE(QzTools::escapeSqlGlobString(input), result);
162}
163
165{
166 QString name;
167
168public:
169 explicit TempFile(const QString &name)
170 : name(name)
171 {
172 QFile file(name);
173 file.open(QFile::WriteOnly);
174 file.write(QByteArrayLiteral("falkon-test"));
175 file.close();
176 }
177
179 {
180 QFile::remove(name);
181 }
182};
183
184void QzToolsTest::ensureUniqueFilename()
185{
186 QCOMPARE(QzTools::ensureUniqueFilename(createPath("test.out")), createPath("test.out"));
187 QCOMPARE(QzTools::ensureUniqueFilename(createPath("test")), createPath("test"));
188
189 // default appendFormat = (%1)
190 {
191 TempFile f1(createPath("test.out"));
192 QCOMPARE(QzTools::ensureUniqueFilename(createPath("test.out")), createPath("test(1).out"));
193 TempFile f2(createPath("test(1).out"));
194 QCOMPARE(QzTools::ensureUniqueFilename(createPath("test.out")), createPath("test(2).out"));
195 TempFile f3(createPath("test(2).out"));
196 QCOMPARE(QzTools::ensureUniqueFilename(createPath("test.out")), createPath("test(3).out"));
197 }
198 {
199 TempFile f1(createPath("test"));
200 QCOMPARE(QzTools::ensureUniqueFilename(createPath("test")), createPath("test(1)"));
201 TempFile f2(createPath("test(1)"));
202 QCOMPARE(QzTools::ensureUniqueFilename(createPath("test")), createPath("test(2)"));
203 TempFile f3(createPath("test(2)"));
204 QCOMPARE(QzTools::ensureUniqueFilename(createPath("test")), createPath("test(3)"));
205 }
206 {
207 TempFile f1(createPath("test(1)"));
208 QCOMPARE(QzTools::ensureUniqueFilename(createPath("test")), createPath("test"));
209 TempFile f2(createPath("test"));
210 QCOMPARE(QzTools::ensureUniqueFilename(createPath("test")), createPath("test(2)"));
211 }
212
213 // appendFormat = %1
214 {
215 QString appendFormat = QSL("%1");
216
217 TempFile f1(createPath("test.out"));
218 QCOMPARE(QzTools::ensureUniqueFilename(createPath("test.out"), appendFormat), createPath("test1.out"));
219 TempFile f2(createPath("test1.out"));
220 QCOMPARE(QzTools::ensureUniqueFilename(createPath("test.out"), appendFormat), createPath("test2.out"));
221 TempFile f3(createPath("test2.out"));
222 QCOMPARE(QzTools::ensureUniqueFilename(createPath("test.out"), appendFormat), createPath("test3.out"));
223 }
224 {
225 QString appendFormat = QSL("%1");
226
227 TempFile f1(createPath("test"));
228 QCOMPARE(QzTools::ensureUniqueFilename(createPath("test"), appendFormat), createPath("test1"));
229 TempFile f2(createPath("test1"));
230 QCOMPARE(QzTools::ensureUniqueFilename(createPath("test"), appendFormat), createPath("test2"));
231 TempFile f3(createPath("test2"));
232 QCOMPARE(QzTools::ensureUniqueFilename(createPath("test"), appendFormat), createPath("test3"));
233 }
234 {
235 QString appendFormat = QSL("%1");
236
237 TempFile f1(createPath("test1"));
238 QCOMPARE(QzTools::ensureUniqueFilename(createPath("test"), appendFormat), createPath("test"));
239 TempFile f2(createPath("test"));
240 QCOMPARE(QzTools::ensureUniqueFilename(createPath("test"), appendFormat), createPath("test2"));
241 }
242
243 // appendFormat = .%1
244 {
245 QString appendFormat = QSL(".%1");
246
247 TempFile f1(createPath("test.out"));
248 QCOMPARE(QzTools::ensureUniqueFilename(createPath("test.out"), appendFormat), createPath("test.1.out"));
249 TempFile f2(createPath("test.1.out"));
250 QCOMPARE(QzTools::ensureUniqueFilename(createPath("test.out"), appendFormat), createPath("test.2.out"));
251 TempFile f3(createPath("test.2.out"));
252 QCOMPARE(QzTools::ensureUniqueFilename(createPath("test.out"), appendFormat), createPath("test.3.out"));
253 }
254 {
255 QString appendFormat = QSL(".%1");
256
257 TempFile f1(createPath("test"));
258 QCOMPARE(QzTools::ensureUniqueFilename(createPath("test"), appendFormat), createPath("test.1"));
259 TempFile f2(createPath("test.1"));
260 QCOMPARE(QzTools::ensureUniqueFilename(createPath("test"), appendFormat), createPath("test.2"));
261 TempFile f3(createPath("test.2"));
262 QCOMPARE(QzTools::ensureUniqueFilename(createPath("test"), appendFormat), createPath("test.3"));
263 }
264 {
265 QString appendFormat = QSL(".%1");
266
267 TempFile f1(createPath("test.1"));
268 QCOMPARE(QzTools::ensureUniqueFilename(createPath("test"), appendFormat), createPath("test"));
269 TempFile f2(createPath("test"));
270 QCOMPARE(QzTools::ensureUniqueFilename(createPath("test"), appendFormat), createPath("test.2"));
271 }
272}
273
274static void createTestDirectoryStructure(const QString &path)
275{
276 QDir().mkdir(path);
277 QDir dir(path);
278 dir.mkdir(QSL("dir1"));
279 dir.mkdir(QSL("dir2"));
280 dir.mkdir(QSL("dir3"));
281 dir.cd(QSL("dir1"));
282 dir.mkdir(QSL("dir1_1"));
283 dir.mkdir(QSL("dir1_2"));
284 dir.mkdir(QSL("dir1_3"));
285 dir.cdUp();
286 dir.cd(QSL("dir3"));
287 dir.mkdir(QSL("dir3_1"));
288 QFile file(path + QSL("/dir1/dir1_2/file1.txt"));
289 file.open(QFile::WriteOnly);
290 file.write("test");
291 file.close();
292}
293
294void QzToolsTest::copyRecursivelyTest()
295{
296 const QString testDir = createPath("copyRecursivelyTest");
297 createTestDirectoryStructure(testDir);
298
299 QVERIFY(!QFileInfo::exists(testDir + QSL("-copy")));
300
301 // Copy to non-existent target
302 QCOMPARE(QzTools::copyRecursively(testDir, testDir + QSL("-copy")), true);
303
304 QCOMPARE(QFileInfo(testDir + QSL("-copy")).isDir(), true);
305 QCOMPARE(QFileInfo(testDir + QSL("-copy/dir1")).isDir(), true);
306 QCOMPARE(QFileInfo(testDir + QSL("-copy/dir2")).isDir(), true);
307 QCOMPARE(QFileInfo(testDir + QSL("-copy/dir3")).isDir(), true);
308 QCOMPARE(QFileInfo(testDir + QSL("-copy/dir1/dir1_1")).isDir(), true);
309 QCOMPARE(QFileInfo(testDir + QSL("-copy/dir1/dir1_2")).isDir(), true);
310 QCOMPARE(QFileInfo(testDir + QSL("-copy/dir1/dir1_3")).isDir(), true);
311 QCOMPARE(QFileInfo(testDir + QSL("-copy/dir3/dir3_1")).isDir(), true);
312 QCOMPARE(QFileInfo(testDir + QSL("-copy/dir1/dir1_2/file1.txt")).isFile(), true);
313
314 QFile file(testDir + QSL("-copy/dir1/dir1_2/file1.txt"));
315 file.open(QFile::ReadOnly);
316 QCOMPARE(file.readAll(), QByteArray("test"));
317 file.close();
318
319 // Copy to target that already exists
320 QCOMPARE(QzTools::copyRecursively(testDir, testDir + QSL("-copy")), false);
321
322 // Cleanup
323 QCOMPARE(QzTools::removeRecursively(testDir), true);
324 QCOMPARE(QzTools::removeRecursively(testDir + QSL("-copy")), true);
325}
326
327void QzToolsTest::removeRecursivelyTest()
328{
329 const QString testDir = createPath("removeRecursivelyTest");
330 createTestDirectoryStructure(testDir);
331
332 QCOMPARE(QzTools::copyRecursively(testDir, testDir + QSL("-copy")), true);
333 QCOMPARE(QzTools::removeRecursively(testDir + QSL("-copy")), true);
334 QCOMPARE(QFileInfo::exists(testDir + QSL("-copy")), false);
335
336 // Remove non-existent path returns success
337 QCOMPARE(QzTools::removeRecursively(testDir + QSL("-copy")), true);
338
339 QCOMPARE(QzTools::copyRecursively(testDir, testDir + QSL("-copy2")), true);
340
341 QFile dir(testDir + QSL("-copy2"));
342 dir.setPermissions(dir.permissions() & ~(QFile::WriteOwner | QFile::WriteUser | QFile::WriteGroup | QFile::WriteOther));
343
344 QCOMPARE(QzTools::removeRecursively(testDir + QSL("-copy2")), false);
345
346 dir.setPermissions(dir.permissions() | QFile::WriteOwner);
347
348 QCOMPARE(QzTools::removeRecursively(testDir + QSL("-copy2")), true);
349
350 // Cleanup
351 QCOMPARE(QzTools::removeRecursively(testDir), true);
352}
353
354void QzToolsTest::dontFollowSymlinksTest()
355{
356 const QString testDir = createPath("removeRecursivelyTest");
357 createTestDirectoryStructure(testDir);
358
359 QDir().mkpath(testDir + QSL("/subdir"));
360 QFile::link(testDir, testDir + QSL("/subdir/link"));
361
362 QVERIFY(QzTools::removeRecursively(testDir + QSL("/subdir")));
363
364 QVERIFY(!QFile::exists(testDir + QSL("/subdir")));
365 QVERIFY(QFile::exists(testDir));
366
367 QDir().mkpath(testDir + QSL("/subdir/normalfolder"));
368 QFile::link(QSL(".."), testDir + QSL("/subdir/link"));
369
370 QVERIFY(QzTools::copyRecursively(testDir + QSL("/subdir"), testDir + QSL("/subdir2")));
371
372 QCOMPARE(QFile::exists(testDir + QSL("/subdir2/link")), true);
373 QCOMPARE(QFile::exists(testDir + QSL("/subdir2/normalfolder")), true);
374
375 // Cleanup
376 QCOMPARE(QzTools::removeRecursively(testDir), true);
377}
378
379QString QzToolsTest::createPath(const char *file) const
380{
381 return m_tmpPath + QL1S("/") + QString::fromUtf8(file);
382}
383
384QTEST_GUILESS_MAIN(QzToolsTest)
static QString ensureUniqueFilename(const QString &name, const QString &appendFormat=QSL("(%1)"))
Definition: qztools.cpp:257
static bool copyRecursively(const QString &sourcePath, const QString &targetPath)
Definition: qztools.cpp:167
static QString getFileNameFromUrl(const QUrl &url)
Definition: qztools.cpp:286
static QStringList splitCommandArguments(const QString &command)
Definition: qztools.cpp:768
static QString escapeSqlGlobString(QString urlString)
Definition: qztools.cpp:247
static QString samePartOfStrings(const QString &one, const QString &other)
Definition: qztools.cpp:203
static bool removeRecursively(const QString &filePath)
Definition: qztools.cpp:139
TempFile(const QString &name)
#define QL1S(x)
Definition: qzcommon.h:44
#define QSL(x)
Definition: qzcommon.h:40