File indexing completed on 2024-07-14 03:52:36

0001 /*
0002     This software is a contribution of the LiMux project of the city of Munich.
0003     SPDX-FileCopyrightText: 2021 Robert Hoffmann <robert@roberthoffmann.de>
0004 
0005     SPDX-License-Identifier: LGPL-2.0-or-later
0006 */
0007 
0008 #include "knetworkmountstestcanonical.h"
0009 
0010 #include <KNetworkMounts>
0011 
0012 #include <QFile>
0013 #include <QProcess>
0014 #include <QStandardPaths>
0015 #include <QTest>
0016 
0017 QTEST_MAIN(KNetworkMountsTestCanonical)
0018 
0019 void KNetworkMountsTestCanonical::initTestCase()
0020 {
0021     QStandardPaths::setTestModeEnabled(true);
0022     m_configFileName = QStringLiteral("%1/network_mounts").arg(QStandardPaths::writableLocation(QStandardPaths::ConfigLocation));
0023 
0024     QFile::remove(m_configFileName);
0025     QVERIFY(!QFile::exists(m_configFileName));
0026 
0027     // create directory structure
0028     QVERIFY(m_tmpDir.isValid());
0029 
0030     const QString relLinkToPath = QStringLiteral("dir");
0031     const QString relSymlinkDirectory = QStringLiteral("symlinkDirectory");
0032     const QStringList relPaths = {relLinkToPath,
0033                                   QStringLiteral("dir/subdir1"),
0034                                   QStringLiteral("dir/subdir1/subdir1"),
0035                                   QStringLiteral("dir/subdir1/subdir2"),
0036                                   QStringLiteral("dir/subdir1/subdir3"),
0037                                   QStringLiteral("dir/subdir2"),
0038                                   QStringLiteral("dir/subdir2/subdir1"),
0039                                   QStringLiteral("dir/subdir2/subdir2"),
0040                                   QStringLiteral("dir/subdir2/subdir3"),
0041                                   relSymlinkDirectory};
0042 
0043     const QString relSymlinkToSmbPath = QStringLiteral("symlinkToSmbPath");
0044 
0045     QDir dir(m_tmpDir.path());
0046     for (const QString &relPath : relPaths) {
0047         QVERIFY(dir.mkpath(relPath));
0048         QVERIFY(QFile::exists(m_tmpDir.path() + QLatin1Char('/') + relPath));
0049 
0050         const QString fileName = m_tmpDir.path() + QLatin1Char('/') + relPath + QLatin1String("/file.txt");
0051         QFile file(fileName);
0052         QVERIFY(file.open(QIODevice::WriteOnly | QIODevice::Text));
0053         file.close();
0054         QVERIFY(QFile::exists(fileName));
0055     }
0056 
0057     const QString linkToPath = m_tmpDir.path() + QLatin1Char('/') + relLinkToPath;
0058 
0059     // SymlinkToNetworkMount
0060     const QString symlinkToSmbPath = dir.path() + QLatin1Char('/') + relSymlinkToSmbPath;
0061 
0062     QVERIFY(QFile::link(linkToPath, symlinkToSmbPath));
0063     qDebug() << "linkToPath=" << linkToPath << ", symlinkToSmbPath=" << symlinkToSmbPath;
0064 
0065     // SymlinkDirectory
0066     QVERIFY(dir.cd(relSymlinkDirectory));
0067     const QString symlinkDirectory = dir.path();
0068     const QString linkStr = symlinkDirectory + QLatin1Char('/') + relLinkToPath;
0069 
0070     QVERIFY(QFile::link(linkToPath, linkStr));
0071     qDebug() << "linkToPath=" << linkToPath << ", symlinkDirectory=" << symlinkDirectory << ", linkStr=" << linkStr;
0072 
0073     // setup config
0074     KNetworkMounts::self()->setEnabled(true);
0075 
0076     const QStringList paths = {linkToPath};
0077     KNetworkMounts::self()->setPaths(paths, KNetworkMounts::SmbPaths);
0078 
0079     const QStringList savedPaths = {linkToPath + QLatin1Char('/')};
0080     QCOMPARE(KNetworkMounts::self()->paths(), savedPaths);
0081 
0082     // SymlinkDirectory
0083     const QStringList symlinkDirectories = {symlinkDirectory};
0084     KNetworkMounts::self()->setPaths(symlinkDirectories, KNetworkMounts::SymlinkDirectory);
0085 
0086     const QStringList savedSymlinkDirectories = {symlinkDirectory + QLatin1Char('/')};
0087     QCOMPARE(KNetworkMounts::self()->paths(KNetworkMounts::SmbPaths), savedPaths);
0088     QCOMPARE(KNetworkMounts::self()->paths(KNetworkMounts::SymlinkDirectory), savedSymlinkDirectories);
0089 
0090     // SymlinkToNetworkMount
0091     // addPath
0092     KNetworkMounts::self()->addPath(symlinkToSmbPath, KNetworkMounts::SymlinkToNetworkMount);
0093 
0094     const QString savedSymlinkToSmbPath = symlinkToSmbPath + QLatin1Char('/');
0095     const QStringList savedSymlinkToSmbPaths = {savedSymlinkToSmbPath};
0096     QCOMPARE(KNetworkMounts::self()->paths(KNetworkMounts::SymlinkToNetworkMount), savedSymlinkToSmbPaths);
0097 
0098     // setPaths
0099     const QStringList symlinkToSmbPaths = {symlinkToSmbPath};
0100     KNetworkMounts::self()->setPaths(symlinkToSmbPaths, KNetworkMounts::SymlinkToNetworkMount);
0101     QCOMPARE(KNetworkMounts::self()->paths(KNetworkMounts::SymlinkToNetworkMount), savedSymlinkToSmbPaths);
0102 
0103     QCOMPARE(KNetworkMounts::self()->paths(KNetworkMounts::SmbPaths), savedPaths);
0104     QCOMPARE(KNetworkMounts::self()->paths(KNetworkMounts::SymlinkDirectory), savedSymlinkDirectories);
0105 }
0106 
0107 void KNetworkMountsTestCanonical::cleanupTestCase()
0108 {
0109     KNetworkMounts::self()->sync();
0110     QFile::remove(m_configFileName);
0111 }
0112 
0113 void KNetworkMountsTestCanonical::testCanonicalSymlinkPath_data()
0114 {
0115     QTest::addColumn<QString>("relPath");
0116     QTest::addColumn<QString>("symlinkedRelPath");
0117 
0118     // SymlinkDirectory
0119     QTest::newRow("symlinkDirectory/dir") << "dir"
0120                                           << "symlinkDirectory/dir";
0121     QTest::newRow("symlinkDirectory/dir/file.txt") << "dir/file.txt"
0122                                                    << "symlinkDirectory/dir/file.txt";
0123     QTest::newRow("symlinkDirectory/dir/subdir1") << "dir/subdir1"
0124                                                   << "symlinkDirectory/dir/subdir1";
0125     QTest::newRow("symlinkDirectory/dir/subdir1/file.txt") << "dir/subdir1/file.txt"
0126                                                            << "symlinkDirectory/dir/subdir1/file.txt";
0127     QTest::newRow("symlinkDirectory/dir/subdir1/subdir1") << "dir/subdir1/subdir1"
0128                                                           << "symlinkDirectory/dir/subdir1/subdir1";
0129     QTest::newRow("symlinkDirectory/dir/subdir1/subdir1/file.txt") << "dir/subdir1/subdir1/file.txt"
0130                                                                    << "symlinkDirectory/dir/subdir1/subdir1/file.txt";
0131     QTest::newRow("symlinkDirectory/dir/subdir1/subdir2") << "dir/subdir1/subdir2"
0132                                                           << "symlinkDirectory/dir/subdir1/subdir2";
0133     QTest::newRow("symlinkDirectory/dir/subdir1/subdir2/file.txt") << "dir/subdir1/subdir2/file.txt"
0134                                                                    << "symlinkDirectory/dir/subdir1/subdir2/file.txt";
0135     QTest::newRow("symlinkDirectory/dir/subdir1/subdir3") << "dir/subdir1/subdir3"
0136                                                           << "symlinkDirectory/dir/subdir1/subdir3";
0137     QTest::newRow("symlinkDirectory/dir/subdir1/subdir3/file.txt") << "dir/subdir1/subdir3/file.txt"
0138                                                                    << "symlinkDirectory/dir/subdir1/subdir3/file.txt";
0139     QTest::newRow("symlinkDirectory/dir/subdir2") << "dir/subdir2"
0140                                                   << "symlinkDirectory/dir/subdir2";
0141     QTest::newRow("symlinkDirectory/dir/subdir2/file.txt") << "dir/subdir2/file.txt"
0142                                                            << "symlinkDirectory/dir/subdir2/file.txt";
0143     QTest::newRow("symlinkDirectory/dir/subdir2/subdir1") << "dir/subdir2/subdir1"
0144                                                           << "symlinkDirectory/dir/subdir2/subdir1";
0145     QTest::newRow("symlinkDirectory/dir/subdir2/subdir1/file.txt") << "dir/subdir2/subdir1/file.txt"
0146                                                                    << "symlinkDirectory/dir/subdir2/subdir1/file.txt";
0147     QTest::newRow("symlinkDirectory/dir/subdir2/subdir2") << "dir/subdir2/subdir2"
0148                                                           << "symlinkDirectory/dir/subdir2/subdir2";
0149     QTest::newRow("symlinkDirectory/dir/subdir2/subdir2/file.txt") << "dir/subdir2/subdir2/file.txt"
0150                                                                    << "symlinkDirectory/dir/subdir2/subdir2/file.txt";
0151     QTest::newRow("symlinkDirectory/dir/subdir2/subdir3") << "dir/subdir2/subdir3"
0152                                                           << "symlinkDirectory/dir/subdir2/subdir3";
0153     QTest::newRow("symlinkDirectory/dir/subdir2/subdir3/file.txt") << "dir/subdir2/subdir3/file.txt"
0154                                                                    << "symlinkDirectory/dir/subdir2/subdir3/file.txt";
0155     QTest::newRow("symlinkDirectory") << "symlinkDirectory"
0156                                       << "symlinkDirectory";
0157     QTest::newRow("symlinkDirectory/file.txt") << "symlinkDirectory/file.txt"
0158                                                << "symlinkDirectory/file.txt";
0159 
0160     // SymlinkToNetworkMount
0161     QTest::newRow("symlinkToSmbPath") << "dir"
0162                                       << "symlinkToSmbPath";
0163     QTest::newRow("symlinkToSmbPath/file.txt") << "dir/file.txt"
0164                                                << "symlinkToSmbPath/file.txt";
0165     QTest::newRow("symlinkToSmbPath/subdir1") << "dir/subdir1"
0166                                               << "symlinkToSmbPath/subdir1";
0167     QTest::newRow("symlinkToSmbPath/subdir1/file.txt") << "dir/subdir1/file.txt"
0168                                                        << "symlinkToSmbPath/subdir1/file.txt";
0169     QTest::newRow("symlinkToSmbPath/subdir1/subdir1") << "dir/subdir1/subdir1"
0170                                                       << "symlinkToSmbPath/subdir1/subdir1";
0171     QTest::newRow("symlinkToSmbPath/subdir1/subdir1/file.txt") << "dir/subdir1/subdir1/file.txt"
0172                                                                << "symlinkToSmbPath/subdir1/subdir1/file.txt";
0173     QTest::newRow("symlinkToSmbPath/subdir1/subdir2") << "dir/subdir1/subdir2"
0174                                                       << "symlinkToSmbPath/subdir1/subdir2";
0175     QTest::newRow("symlinkToSmbPath/subdir1/subdir2/file.txt") << "dir/subdir1/subdir2/file.txt"
0176                                                                << "symlinkToSmbPath/subdir1/subdir2/file.txt";
0177     QTest::newRow("symlinkToSmbPath/subdir1/subdir3") << "dir/subdir1/subdir3"
0178                                                       << "symlinkToSmbPath/subdir1/subdir3";
0179     QTest::newRow("symlinkToSmbPath/subdir1/subdir3/file.txt") << "dir/subdir1/subdir3/file.txt"
0180                                                                << "symlinkToSmbPath/subdir1/subdir3/file.txt";
0181     QTest::newRow("symlinkToSmbPath/subdir2") << "dir/subdir2"
0182                                               << "symlinkToSmbPath/subdir2";
0183     QTest::newRow("symlinkToSmbPath/subdir2/file.txt") << "dir/subdir2/file.txt"
0184                                                        << "symlinkToSmbPath/subdir2/file.txt";
0185     QTest::newRow("symlinkToSmbPath/subdir2/subdir1") << "dir/subdir2/subdir1"
0186                                                       << "symlinkToSmbPath/subdir2/subdir1";
0187     QTest::newRow("symlinkToSmbPath/subdir2/subdir1/file.txt") << "dir/subdir2/subdir1/file.txt"
0188                                                                << "symlinkToSmbPath/subdir2/subdir1/file.txt";
0189     QTest::newRow("symlinkToSmbPath/subdir2/subdir2") << "dir/subdir2/subdir2"
0190                                                       << "symlinkToSmbPath/subdir2/subdir2";
0191     QTest::newRow("symlinkToSmbPath/subdir2/subdir2/file.txt") << "dir/subdir2/subdir2/file.txt"
0192                                                                << "symlinkToSmbPath/subdir2/subdir2/file.txt";
0193     QTest::newRow("symlinkToSmbPath/subdir2/subdir3") << "dir/subdir2/subdir3"
0194                                                       << "symlinkToSmbPath/subdir2/subdir3";
0195     QTest::newRow("symlinkToSmbPath/subdir2/subdir3/file.txt") << "dir/subdir2/subdir3/file.txt"
0196                                                                << "symlinkToSmbPath/subdir2/subdir3/file.txt";
0197 }
0198 
0199 void KNetworkMountsTestCanonical::testCanonicalSymlinkPath()
0200 {
0201     QFETCH(QString, relPath);
0202     QFETCH(QString, symlinkedRelPath);
0203 
0204 #ifdef Q_OS_WIN
0205     QSKIP("QFile::link creates a shortcut on Windows, not a symlink, so no effect on canonical paths, skipped");
0206 #endif
0207 
0208     const QString path = m_tmpDir.path() + QLatin1Char('/') + relPath;
0209     const QString symlinkedPath = m_tmpDir.path() + QLatin1Char('/') + symlinkedRelPath;
0210     const QString canonicalPath = QFileInfo(symlinkedPath).canonicalFilePath();
0211 
0212     // default with cache
0213     QCOMPARE(KNetworkMounts::self()->canonicalSymlinkPath(symlinkedPath), canonicalPath);
0214     QCOMPARE(path, canonicalPath);
0215     qDebug() << "path=" << path << ", canonicalPath=" << canonicalPath << ", symlinkedPath=" << symlinkedPath;
0216 
0217     // from cache
0218     QCOMPARE(KNetworkMounts::self()->canonicalSymlinkPath(symlinkedPath), canonicalPath);
0219 
0220     // no cache
0221     KNetworkMounts::self()->clearCache();
0222     QCOMPARE(KNetworkMounts::self()->canonicalSymlinkPath(symlinkedPath), canonicalPath);
0223 
0224     KNetworkMounts::self()->clearCache();
0225     KNetworkMounts::self()->setOption(KNetworkMounts::SymlinkPathsUseCache, false);
0226     QCOMPARE(KNetworkMounts::self()->canonicalSymlinkPath(symlinkedPath), canonicalPath);
0227 
0228     // with cache
0229     KNetworkMounts::self()->setOption(KNetworkMounts::SymlinkPathsUseCache, true);
0230     QCOMPARE(KNetworkMounts::self()->canonicalSymlinkPath(symlinkedPath), canonicalPath);
0231 
0232     QCOMPARE(KNetworkMounts::self()->canonicalSymlinkPath(symlinkedPath), canonicalPath);
0233 }
0234 
0235 #include "moc_knetworkmountstestcanonical.cpp"