22#include <QtTest/QtTest>
24void QzToolsTest::initTestCase()
26 m_tmpPath = QDir::tempPath() +
QL1S(
"/falkon-test/qztoolstest");
27 QDir().mkpath(m_tmpPath);
29 QVERIFY(QDir(m_tmpPath).exists());
32void QzToolsTest::cleanupTestCase()
34 QDir().rmpath(m_tmpPath);
36 QVERIFY(!QDir(m_tmpPath).exists());
39void QzToolsTest::samePartOfStrings_data()
41 QTest::addColumn<QString>(
"string1");
42 QTest::addColumn<QString>(
"string2");
43 QTest::addColumn<QString>(
"result");
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") <<
"" <<
"" <<
"";
54void QzToolsTest::samePartOfStrings()
56 QFETCH(QString, string1);
57 QFETCH(QString, string2);
58 QFETCH(QString, result);
63void QzToolsTest::getFileNameFromUrl_data()
65 QTest::addColumn<QUrl>(
"url");
66 QTest::addColumn<QString>(
"result");
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(
"");
79void QzToolsTest::getFileNameFromUrl()
82 QFETCH(QString, result);
87void QzToolsTest::splitCommandArguments_data()
89 QTest::addColumn<QString>(
"command");
90 QTest::addColumn<QStringList>(
"result");
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()
96 QTest::newRow(
"OnlySpaces") <<
QSL(
" ")
98 QTest::newRow(
"OnlyQuotes") <<
QSL(R
"("" "")")
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 "));
125 QTest::newRow(
"UnmatchedQuote") <<
"/usr/bin/foo -o \"bar"
129void QzToolsTest::splitCommandArguments()
131 QFETCH(QString, command);
132 QFETCH(QStringList, result);
137void QzToolsTest::escapeSqlGlobString_data()
139 QTest::addColumn<QString>(
"input");
140 QTest::addColumn<QString>(
"result");
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";
156void QzToolsTest::escapeSqlGlobString()
158 QFETCH(QString, input);
159 QFETCH(QString, result);
173 file.open(QFile::WriteOnly);
174 file.write(QByteArrayLiteral(
"falkon-test"));
184void QzToolsTest::ensureUniqueFilename()
191 TempFile f1(createPath(
"test.out"));
193 TempFile f2(createPath(
"test(1).out"));
195 TempFile f3(createPath(
"test(2).out"));
215 QString appendFormat =
QSL(
"%1");
217 TempFile f1(createPath(
"test.out"));
219 TempFile f2(createPath(
"test1.out"));
221 TempFile f3(createPath(
"test2.out"));
225 QString appendFormat =
QSL(
"%1");
235 QString appendFormat =
QSL(
"%1");
245 QString appendFormat =
QSL(
".%1");
247 TempFile f1(createPath(
"test.out"));
249 TempFile f2(createPath(
"test.1.out"));
251 TempFile f3(createPath(
"test.2.out"));
255 QString appendFormat =
QSL(
".%1");
265 QString appendFormat =
QSL(
".%1");
274static void createTestDirectoryStructure(
const QString &path)
278 dir.mkdir(
QSL(
"dir1"));
279 dir.mkdir(
QSL(
"dir2"));
280 dir.mkdir(
QSL(
"dir3"));
282 dir.mkdir(
QSL(
"dir1_1"));
283 dir.mkdir(
QSL(
"dir1_2"));
284 dir.mkdir(
QSL(
"dir1_3"));
287 dir.mkdir(
QSL(
"dir3_1"));
288 QFile file(path +
QSL(
"/dir1/dir1_2/file1.txt"));
289 file.open(QFile::WriteOnly);
294void QzToolsTest::copyRecursivelyTest()
296 const QString testDir = createPath(
"copyRecursivelyTest");
297 createTestDirectoryStructure(testDir);
299 QVERIFY(!QFileInfo::exists(testDir +
QSL(
"-copy")));
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);
314 QFile file(testDir +
QSL(
"-copy/dir1/dir1_2/file1.txt"));
315 file.open(QFile::ReadOnly);
316 QCOMPARE(file.readAll(), QByteArray(
"test"));
327void QzToolsTest::removeRecursivelyTest()
329 const QString testDir = createPath(
"removeRecursivelyTest");
330 createTestDirectoryStructure(testDir);
334 QCOMPARE(QFileInfo::exists(testDir +
QSL(
"-copy")),
false);
341 QFile dir(testDir +
QSL(
"-copy2"));
342 dir.setPermissions(dir.permissions() & ~(QFile::WriteOwner | QFile::WriteUser | QFile::WriteGroup | QFile::WriteOther));
346 dir.setPermissions(dir.permissions() | QFile::WriteOwner);
354void QzToolsTest::dontFollowSymlinksTest()
356 const QString testDir = createPath(
"removeRecursivelyTest");
357 createTestDirectoryStructure(testDir);
359 QDir().mkpath(testDir +
QSL(
"/subdir"));
360 QFile::link(testDir, testDir +
QSL(
"/subdir/link"));
364 QVERIFY(!QFile::exists(testDir +
QSL(
"/subdir")));
365 QVERIFY(QFile::exists(testDir));
367 QDir().mkpath(testDir +
QSL(
"/subdir/normalfolder"));
368 QFile::link(
QSL(
".."), testDir +
QSL(
"/subdir/link"));
372 QCOMPARE(QFile::exists(testDir +
QSL(
"/subdir2/link")),
true);
373 QCOMPARE(QFile::exists(testDir +
QSL(
"/subdir2/normalfolder")),
true);
379QString QzToolsTest::createPath(
const char *file)
const
381 return m_tmpPath +
QL1S(
"/") + QString::fromUtf8(file);
TempFile(const QString &name)