File indexing completed on 2024-04-21 04:55:39

0001 /* ============================================================
0002 * Falkon - Qt web browser
0003 * Copyright (C) 2013-2014  David Rosca <nowrep@gmail.com>
0004 *
0005 * This program is free software: you can redistribute it and/or modify
0006 * it under the terms of the GNU General Public License as published by
0007 * the Free Software Foundation, either version 3 of the License, or
0008 * (at your option) any later version.
0009 *
0010 * This program is distributed in the hope that it will be useful,
0011 * but WITHOUT ANY WARRANTY; without even the implied warranty of
0012 * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the
0013 * GNU General Public License for more details.
0014 *
0015 * You should have received a copy of the GNU General Public License
0016 * along with this program.  If not, see <http://www.gnu.org/licenses/>.
0017 * ============================================================ */
0018 #include "qztoolstest.h"
0019 #include "qztools.h"
0020 
0021 #include <QDir>
0022 #include <QtTest/QtTest>
0023 
0024 void QzToolsTest::initTestCase()
0025 {
0026     m_tmpPath = QDir::tempPath() + QL1S("/falkon-test/qztoolstest");
0027     QDir().mkpath(m_tmpPath);
0028 
0029     QVERIFY(QDir(m_tmpPath).exists());
0030 }
0031 
0032 void QzToolsTest::cleanupTestCase()
0033 {
0034     QDir().rmpath(m_tmpPath);
0035 
0036     QVERIFY(!QDir(m_tmpPath).exists());
0037 }
0038 
0039 void QzToolsTest::samePartOfStrings_data()
0040 {
0041     QTest::addColumn<QString>("string1");
0042     QTest::addColumn<QString>("string2");
0043     QTest::addColumn<QString>("result");
0044 
0045     // Lorem ipsum dolor sit amet, consectetur adipiscing elit.
0046     QTest::newRow("General") << "Lorem ipsum dolor" << "Lorem ipsum dolor Test_1" << "Lorem ipsum dolor";
0047     QTest::newRow("OneChar") << "L" << "LTest_1" << "L";
0048     QTest::newRow("EmptyReturn") << "Lorem ipsum dolor" << "orem ipsum dolor Test_1" << "";
0049     QTest::newRow("EmptyString1") << "" << "orem ipsum dolor Test_1" << "";
0050     QTest::newRow("EmptyString2") << "Lorem ipsum dolor" << "" << "";
0051     QTest::newRow("EmptyBoth") << "" << "" << "";
0052 }
0053 
0054 void QzToolsTest::samePartOfStrings()
0055 {
0056     QFETCH(QString, string1);
0057     QFETCH(QString, string2);
0058     QFETCH(QString, result);
0059 
0060     QCOMPARE(QzTools::samePartOfStrings(string1, string2), result);
0061 }
0062 
0063 void QzToolsTest::getFileNameFromUrl_data()
0064 {
0065     QTest::addColumn<QUrl>("url");
0066     QTest::addColumn<QString>("result");
0067 
0068     QTest::newRow("Basic") << QUrl(QSL("http://www.google.com/filename.html")) << QSL("filename.html");
0069     QTest::newRow("OnlyHost") << QUrl(QSL("http://www.google.com/")) << QSL("www.google.com");
0070     QTest::newRow("OnlyHostWithoutSlash") << QUrl(QSL("http://www.google.com")) << QSL("www.google.com");
0071     QTest::newRow("EndingDirectory") << QUrl(QSL("http://www.google.com/filename/")) << QSL("filename");
0072     QTest::newRow("EmptyUrl") << QUrl(QSL("")) << QSL("");
0073     QTest::newRow("OnlyScheme") << QUrl(QSL("http:")) << QSL("");
0074     QTest::newRow("FileSchemeUrl") << QUrl(QSL("file:///usr/share/test/file.tx")) << QSL("file.tx");
0075     QTest::newRow("FileSchemeUrlDirectory") << QUrl(QSL("file:///usr/share/test/")) << QSL("test");
0076     QTest::newRow("FileSchemeUrlRoot") << QUrl(QSL("file:///")) << QSL("");
0077 }
0078 
0079 void QzToolsTest::getFileNameFromUrl()
0080 {
0081     QFETCH(QUrl, url);
0082     QFETCH(QString, result);
0083 
0084     QCOMPARE(QzTools::getFileNameFromUrl(url), result);
0085 }
0086 
0087 void QzToolsTest::splitCommandArguments_data()
0088 {
0089     QTest::addColumn<QString>("command");
0090     QTest::addColumn<QStringList>("result");
0091 
0092     QTest::newRow("Basic") << "/usr/bin/foo -o foo.out"
0093                            << (QStringList() << QSL("/usr/bin/foo") << QSL("-o") << QSL("foo.out"));
0094     QTest::newRow("Empty") << QString()
0095                            << QStringList();
0096     QTest::newRow("OnlySpaces") << QSL("                   ")
0097                            << QStringList();
0098     QTest::newRow("OnlyQuotes") << QSL(R"("" "")")
0099                            << QStringList();
0100     QTest::newRow("EmptyQuotesAndSpace") << QSL(R"("" "" " ")")
0101                            << QStringList(QSL(" "));
0102     QTest::newRow("MultipleSpaces") << "    /usr/foo   -o    foo.out    "
0103                            << (QStringList() << QSL("/usr/foo") << QSL("-o") << QSL("foo.out"));
0104     QTest::newRow("Quotes") << R"("/usr/foo" "-o" "foo.out")"
0105                            << (QStringList() << QSL("/usr/foo") << QSL("-o") << QSL("foo.out"));
0106     QTest::newRow("SingleQuotes") << "'/usr/foo' '-o' 'foo.out'"
0107                            << (QStringList() << QSL("/usr/foo") << QSL("-o") << QSL("foo.out"));
0108     QTest::newRow("SingleAndDoubleQuotes") << " '/usr/foo' \"-o\" 'foo.out' "
0109                            << (QStringList() << QSL("/usr/foo") << QSL("-o") << QSL("foo.out"));
0110     QTest::newRow("SingleInDoubleQuotes") << "/usr/foo \"-o 'ds' \" 'foo.out' "
0111                            << (QStringList() << QSL("/usr/foo") << QSL("-o 'ds' ") << QSL("foo.out"));
0112     QTest::newRow("DoubleInSingleQuotes") << "/usr/foo -o 'foo\" d \".out' "
0113                            << (QStringList() << QSL("/usr/foo") << QSL("-o") << QSL("foo\" d \".out"));
0114     QTest::newRow("SpacesWithQuotes") << QSL(R"(  "   "     "   "     )")
0115                            << (QStringList() << QSL("   ") << QSL("   "));
0116     QTest::newRow("QuotesAndSpaces") << "/usr/foo -o \"foo - out\""
0117                            << (QStringList() << QSL("/usr/foo") << QSL("-o") << QSL("foo - out"));
0118     QTest::newRow("EqualAndQuotes") << "/usr/foo -o=\"foo - out\""
0119                            << (QStringList() << QSL("/usr/foo") << QSL("-o=foo - out"));
0120     QTest::newRow("EqualWithSpaces") << "/usr/foo -o = \"foo - out\""
0121                            << (QStringList() << QSL("/usr/foo") << QSL("-o") << QSL("=") << QSL("foo - out"));
0122     QTest::newRow("MultipleSpacesAndQuotes") << "    /usr/foo   -o=\"    foo.out   \" "
0123                            << (QStringList() << QSL("/usr/foo") << QSL("-o=    foo.out   "));
0124     // Unmatched quotes should be treated as an error
0125     QTest::newRow("UnmatchedQuote") << "/usr/bin/foo -o \"bar"
0126                            << QStringList();
0127 }
0128 
0129 void QzToolsTest::splitCommandArguments()
0130 {
0131     QFETCH(QString, command);
0132     QFETCH(QStringList, result);
0133 
0134     QCOMPARE(QzTools::splitCommandArguments(command), result);
0135 }
0136 
0137 void QzToolsTest::escapeSqlGlobString_data()
0138 {
0139     QTest::addColumn<QString>("input");
0140     QTest::addColumn<QString>("result");
0141 
0142     QTest::newRow("NothingToEscape") << "http://test" << "http://test";
0143     QTest::newRow("Escape *") << "http://test*/heh" << "http://test[*]/heh";
0144     QTest::newRow("Escape **") << "http://test**/he*h" << "http://test[*][*]/he[*]h";
0145     QTest::newRow("Escape ?") << "http://test?/heh" << "http://test[?]/heh";
0146     QTest::newRow("Escape ??") << "http://t??est?/heh" << "http://t[?][?]est[?]/heh";
0147     QTest::newRow("Escape [") << "http://[test/heh" << "http://[[]test/heh";
0148     QTest::newRow("Escape [[") << "http://[[te[st/heh" << "http://[[][[]te[[]st/heh";
0149     QTest::newRow("Escape ]") << "http://]test/heh" << "http://[]]test/heh";
0150     QTest::newRow("Escape ]]") << "http://]]te]st/heh" << "http://[]][]]te[]]st/heh";
0151     QTest::newRow("Escape []") << "http://[]test/heh" << "http://[[][]]test/heh";
0152     QTest::newRow("Escape [][[]][]") << "http://t[][[]][]est/heh" << "http://t[[][]][[][[][]][]][[][]]est/heh";
0153     QTest::newRow("Escape [?]][[*]") << "http://t[?]][[*]est/heh" << "http://t[[][?][]][]][[][[][*][]]est/heh";
0154 }
0155 
0156 void QzToolsTest::escapeSqlGlobString()
0157 {
0158     QFETCH(QString, input);
0159     QFETCH(QString, result);
0160 
0161     QCOMPARE(QzTools::escapeSqlGlobString(input), result);
0162 }
0163 
0164 class TempFile
0165 {
0166     QString name;
0167 
0168 public:
0169     explicit TempFile(const QString &name)
0170         : name(name)
0171     {
0172         QFile file(name);
0173         file.open(QFile::WriteOnly);
0174         file.write(QByteArrayLiteral("falkon-test"));
0175         file.close();
0176     }
0177 
0178     ~TempFile()
0179     {
0180         QFile::remove(name);
0181     }
0182 };
0183 
0184 void QzToolsTest::ensureUniqueFilename()
0185 {
0186     QCOMPARE(QzTools::ensureUniqueFilename(createPath("test.out")), createPath("test.out"));
0187     QCOMPARE(QzTools::ensureUniqueFilename(createPath("test")), createPath("test"));
0188 
0189     // default appendFormat = (%1)
0190     {
0191         TempFile f1(createPath("test.out"));
0192         QCOMPARE(QzTools::ensureUniqueFilename(createPath("test.out")), createPath("test(1).out"));
0193         TempFile f2(createPath("test(1).out"));
0194         QCOMPARE(QzTools::ensureUniqueFilename(createPath("test.out")), createPath("test(2).out"));
0195         TempFile f3(createPath("test(2).out"));
0196         QCOMPARE(QzTools::ensureUniqueFilename(createPath("test.out")), createPath("test(3).out"));
0197     }
0198     {
0199         TempFile f1(createPath("test"));
0200         QCOMPARE(QzTools::ensureUniqueFilename(createPath("test")), createPath("test(1)"));
0201         TempFile f2(createPath("test(1)"));
0202         QCOMPARE(QzTools::ensureUniqueFilename(createPath("test")), createPath("test(2)"));
0203         TempFile f3(createPath("test(2)"));
0204         QCOMPARE(QzTools::ensureUniqueFilename(createPath("test")), createPath("test(3)"));
0205     }
0206     {
0207         TempFile f1(createPath("test(1)"));
0208         QCOMPARE(QzTools::ensureUniqueFilename(createPath("test")), createPath("test"));
0209         TempFile f2(createPath("test"));
0210         QCOMPARE(QzTools::ensureUniqueFilename(createPath("test")), createPath("test(2)"));
0211     }
0212 
0213     // appendFormat = %1
0214     {
0215         QString appendFormat = QSL("%1");
0216 
0217         TempFile f1(createPath("test.out"));
0218         QCOMPARE(QzTools::ensureUniqueFilename(createPath("test.out"), appendFormat), createPath("test1.out"));
0219         TempFile f2(createPath("test1.out"));
0220         QCOMPARE(QzTools::ensureUniqueFilename(createPath("test.out"), appendFormat), createPath("test2.out"));
0221         TempFile f3(createPath("test2.out"));
0222         QCOMPARE(QzTools::ensureUniqueFilename(createPath("test.out"), appendFormat), createPath("test3.out"));
0223     }
0224     {
0225         QString appendFormat = QSL("%1");
0226 
0227         TempFile f1(createPath("test"));
0228         QCOMPARE(QzTools::ensureUniqueFilename(createPath("test"), appendFormat), createPath("test1"));
0229         TempFile f2(createPath("test1"));
0230         QCOMPARE(QzTools::ensureUniqueFilename(createPath("test"), appendFormat), createPath("test2"));
0231         TempFile f3(createPath("test2"));
0232         QCOMPARE(QzTools::ensureUniqueFilename(createPath("test"), appendFormat), createPath("test3"));
0233     }
0234     {
0235         QString appendFormat = QSL("%1");
0236 
0237         TempFile f1(createPath("test1"));
0238         QCOMPARE(QzTools::ensureUniqueFilename(createPath("test"), appendFormat), createPath("test"));
0239         TempFile f2(createPath("test"));
0240         QCOMPARE(QzTools::ensureUniqueFilename(createPath("test"), appendFormat), createPath("test2"));
0241     }
0242 
0243     // appendFormat = .%1
0244     {
0245         QString appendFormat = QSL(".%1");
0246 
0247         TempFile f1(createPath("test.out"));
0248         QCOMPARE(QzTools::ensureUniqueFilename(createPath("test.out"), appendFormat), createPath("test.1.out"));
0249         TempFile f2(createPath("test.1.out"));
0250         QCOMPARE(QzTools::ensureUniqueFilename(createPath("test.out"), appendFormat), createPath("test.2.out"));
0251         TempFile f3(createPath("test.2.out"));
0252         QCOMPARE(QzTools::ensureUniqueFilename(createPath("test.out"), appendFormat), createPath("test.3.out"));
0253     }
0254     {
0255         QString appendFormat = QSL(".%1");
0256 
0257         TempFile f1(createPath("test"));
0258         QCOMPARE(QzTools::ensureUniqueFilename(createPath("test"), appendFormat), createPath("test.1"));
0259         TempFile f2(createPath("test.1"));
0260         QCOMPARE(QzTools::ensureUniqueFilename(createPath("test"), appendFormat), createPath("test.2"));
0261         TempFile f3(createPath("test.2"));
0262         QCOMPARE(QzTools::ensureUniqueFilename(createPath("test"), appendFormat), createPath("test.3"));
0263     }
0264     {
0265         QString appendFormat = QSL(".%1");
0266 
0267         TempFile f1(createPath("test.1"));
0268         QCOMPARE(QzTools::ensureUniqueFilename(createPath("test"), appendFormat), createPath("test"));
0269         TempFile f2(createPath("test"));
0270         QCOMPARE(QzTools::ensureUniqueFilename(createPath("test"), appendFormat), createPath("test.2"));
0271     }
0272 }
0273 
0274 static void createTestDirectoryStructure(const QString &path)
0275 {
0276     QDir().mkdir(path);
0277     QDir dir(path);
0278     dir.mkdir(QSL("dir1"));
0279     dir.mkdir(QSL("dir2"));
0280     dir.mkdir(QSL("dir3"));
0281     dir.cd(QSL("dir1"));
0282     dir.mkdir(QSL("dir1_1"));
0283     dir.mkdir(QSL("dir1_2"));
0284     dir.mkdir(QSL("dir1_3"));
0285     dir.cdUp();
0286     dir.cd(QSL("dir3"));
0287     dir.mkdir(QSL("dir3_1"));
0288     QFile file(path + QSL("/dir1/dir1_2/file1.txt"));
0289     file.open(QFile::WriteOnly);
0290     file.write("test");
0291     file.close();
0292 }
0293 
0294 void QzToolsTest::copyRecursivelyTest()
0295 {
0296     const QString testDir = createPath("copyRecursivelyTest");
0297     createTestDirectoryStructure(testDir);
0298 
0299     QVERIFY(!QFileInfo(testDir + QSL("-copy")).exists());
0300 
0301     // Copy to non-existent target
0302     QCOMPARE(QzTools::copyRecursively(testDir, testDir + QSL("-copy")), true);
0303 
0304     QCOMPARE(QFileInfo(testDir + QSL("-copy")).isDir(), true);
0305     QCOMPARE(QFileInfo(testDir + QSL("-copy/dir1")).isDir(), true);
0306     QCOMPARE(QFileInfo(testDir + QSL("-copy/dir2")).isDir(), true);
0307     QCOMPARE(QFileInfo(testDir + QSL("-copy/dir3")).isDir(), true);
0308     QCOMPARE(QFileInfo(testDir + QSL("-copy/dir1/dir1_1")).isDir(), true);
0309     QCOMPARE(QFileInfo(testDir + QSL("-copy/dir1/dir1_2")).isDir(), true);
0310     QCOMPARE(QFileInfo(testDir + QSL("-copy/dir1/dir1_3")).isDir(), true);
0311     QCOMPARE(QFileInfo(testDir + QSL("-copy/dir3/dir3_1")).isDir(), true);
0312     QCOMPARE(QFileInfo(testDir + QSL("-copy/dir1/dir1_2/file1.txt")).isFile(), true);
0313 
0314     QFile file(testDir + QSL("-copy/dir1/dir1_2/file1.txt"));
0315     file.open(QFile::ReadOnly);
0316     QCOMPARE(file.readAll(), QByteArray("test"));
0317     file.close();
0318 
0319     // Copy to target that already exists
0320     QCOMPARE(QzTools::copyRecursively(testDir, testDir + QSL("-copy")), false);
0321 
0322     // Cleanup
0323     QCOMPARE(QzTools::removeRecursively(testDir), true);
0324     QCOMPARE(QzTools::removeRecursively(testDir + QSL("-copy")), true);
0325 }
0326 
0327 void QzToolsTest::removeRecursivelyTest()
0328 {
0329     const QString testDir = createPath("removeRecursivelyTest");
0330     createTestDirectoryStructure(testDir);
0331 
0332     QCOMPARE(QzTools::copyRecursively(testDir, testDir + QSL("-copy")), true);
0333     QCOMPARE(QzTools::removeRecursively(testDir + QSL("-copy")), true);
0334     QCOMPARE(QFileInfo(testDir + QSL("-copy")).exists(), false);
0335 
0336     // Remove non-existent path returns success
0337     QCOMPARE(QzTools::removeRecursively(testDir + QSL("-copy")), true);
0338 
0339     QCOMPARE(QzTools::copyRecursively(testDir, testDir + QSL("-copy2")), true);
0340 
0341     QFile dir(testDir + QSL("-copy2"));
0342     dir.setPermissions(dir.permissions() & ~(QFile::WriteOwner | QFile::WriteUser | QFile::WriteGroup | QFile::WriteOther));
0343 
0344     QCOMPARE(QzTools::removeRecursively(testDir + QSL("-copy2")), false);
0345 
0346     dir.setPermissions(dir.permissions() | QFile::WriteOwner);
0347 
0348     QCOMPARE(QzTools::removeRecursively(testDir + QSL("-copy2")), true);
0349 
0350     // Cleanup
0351     QCOMPARE(QzTools::removeRecursively(testDir), true);
0352 }
0353 
0354 void QzToolsTest::dontFollowSymlinksTest()
0355 {
0356     const QString testDir = createPath("removeRecursivelyTest");
0357     createTestDirectoryStructure(testDir);
0358 
0359     QDir().mkpath(testDir + QSL("/subdir"));
0360     QFile::link(testDir, testDir + QSL("/subdir/link"));
0361 
0362     QVERIFY(QzTools::removeRecursively(testDir + QSL("/subdir")));
0363 
0364     QVERIFY(!QFile::exists(testDir + QSL("/subdir")));
0365     QVERIFY(QFile::exists(testDir));
0366 
0367     QDir().mkpath(testDir + QSL("/subdir/normalfolder"));
0368     QFile::link(QSL(".."), testDir + QSL("/subdir/link"));
0369 
0370     QVERIFY(QzTools::copyRecursively(testDir + QSL("/subdir"), testDir + QSL("/subdir2")));
0371 
0372     QCOMPARE(QFile::exists(testDir + QSL("/subdir2/link")), true);
0373     QCOMPARE(QFile::exists(testDir + QSL("/subdir2/normalfolder")), true);
0374 
0375     // Cleanup
0376     QCOMPARE(QzTools::removeRecursively(testDir), true);
0377 }
0378 
0379 QString QzToolsTest::createPath(const char *file) const
0380 {
0381     return m_tmpPath + QL1S("/") + QString::fromUtf8(file);
0382 }
0383 
0384 QTEST_GUILESS_MAIN(QzToolsTest)