File indexing completed on 2024-07-14 14:27:54

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 "knetworkmountstestpaths.h"
0009 
0010 #include <KNetworkMounts>
0011 
0012 #include <QFile>
0013 #include <QStandardPaths>
0014 #include <QTest>
0015 
0016 QTEST_MAIN(KNetworkMountsTestPaths)
0017 
0018 void KNetworkMountsTestPaths::initTestCase()
0019 {
0020     QStandardPaths::setTestModeEnabled(true);
0021     m_configFileName = QStringLiteral("%1/network_mounts").arg(QStandardPaths::writableLocation(QStandardPaths::ConfigLocation));
0022 
0023     QFile::remove(m_configFileName);
0024     QVERIFY(!QFile::exists(m_configFileName));
0025 
0026     KNetworkMounts::self()->setEnabled(true);
0027     QVERIFY(KNetworkMounts::self()->isEnabled());
0028     KNetworkMounts::self()->sync();
0029     QVERIFY(QFile::exists(m_configFileName));
0030     QVERIFY(KNetworkMounts::self()->isEnabled());
0031 
0032     // nfs path
0033     const QString nfsPath = QStringLiteral("/mnt/nfs");
0034     const QString savedNfsPath = QStringLiteral("/mnt/nfs/");
0035     const QStringList savedNfsPaths = {savedNfsPath};
0036     KNetworkMounts::self()->addPath(nfsPath, KNetworkMounts::NfsPaths);
0037 
0038     QStringList allSavedPaths = savedNfsPaths;
0039 
0040     QCOMPARE(KNetworkMounts::self()->paths(), allSavedPaths);
0041     QCOMPARE(KNetworkMounts::self()->paths(KNetworkMounts::SmbPaths), QStringList());
0042     QCOMPARE(KNetworkMounts::self()->paths(KNetworkMounts::NfsPaths), savedNfsPaths);
0043     QCOMPARE(KNetworkMounts::self()->paths(KNetworkMounts::SymlinkDirectory), QStringList());
0044     QCOMPARE(KNetworkMounts::self()->paths(KNetworkMounts::SymlinkToNetworkMount), QStringList());
0045 
0046     // smb shares
0047     const QStringList paths = {QStringLiteral("/mnt/server1"), QStringLiteral("/mnt/server2")};
0048     const QStringList savedSmbPaths = {QStringLiteral("/mnt/server1/"), QStringLiteral("/mnt/server2/")};
0049     KNetworkMounts::self()->setPaths(paths, KNetworkMounts::SmbPaths);
0050 
0051     allSavedPaths << savedSmbPaths;
0052 
0053     QCOMPARE(KNetworkMounts::self()->paths(), allSavedPaths);
0054     QCOMPARE(KNetworkMounts::self()->paths(KNetworkMounts::SmbPaths), savedSmbPaths);
0055     QCOMPARE(KNetworkMounts::self()->paths(KNetworkMounts::NfsPaths), savedNfsPaths);
0056     QCOMPARE(KNetworkMounts::self()->paths(KNetworkMounts::SymlinkDirectory), QStringList());
0057     QCOMPARE(KNetworkMounts::self()->paths(KNetworkMounts::SymlinkToNetworkMount), QStringList());
0058 
0059     // symlink dir
0060     const QStringList symlinkDirs = {QStringLiteral("/home/user/netshares")};
0061     const QStringList savedSymlinkDirs = {QStringLiteral("/home/user/netshares/")};
0062     KNetworkMounts::self()->setPaths(symlinkDirs, KNetworkMounts::SymlinkDirectory);
0063 
0064     allSavedPaths << savedSymlinkDirs;
0065 
0066     QCOMPARE(KNetworkMounts::self()->paths(), allSavedPaths);
0067     QCOMPARE(KNetworkMounts::self()->paths(KNetworkMounts::SmbPaths), savedSmbPaths);
0068     QCOMPARE(KNetworkMounts::self()->paths(KNetworkMounts::SymlinkDirectory), savedSymlinkDirs);
0069     QCOMPARE(KNetworkMounts::self()->paths(KNetworkMounts::NfsPaths), savedNfsPaths);
0070     QCOMPARE(KNetworkMounts::self()->paths(KNetworkMounts::SymlinkToNetworkMount), QStringList());
0071 
0072     // symlink to nfs or smb
0073     const QString symlinkToNfs = QStringLiteral("/somedir/symlinkToNfs");
0074     const QString savedSymlinkToNfs = QStringLiteral("/somedir/symlinkToNfs/");
0075     const QStringList savedSymlinkToNfsPaths = {savedSymlinkToNfs};
0076     KNetworkMounts::self()->addPath(symlinkToNfs, KNetworkMounts::SymlinkToNetworkMount);
0077 
0078     allSavedPaths << savedSymlinkToNfsPaths;
0079 
0080     QCOMPARE(KNetworkMounts::self()->paths(), allSavedPaths);
0081     QCOMPARE(KNetworkMounts::self()->paths(KNetworkMounts::SmbPaths), savedSmbPaths);
0082     QCOMPARE(KNetworkMounts::self()->paths(KNetworkMounts::SymlinkDirectory), savedSymlinkDirs);
0083     QCOMPARE(KNetworkMounts::self()->paths(KNetworkMounts::NfsPaths), savedNfsPaths);
0084     QCOMPARE(KNetworkMounts::self()->paths(KNetworkMounts::SymlinkToNetworkMount), savedSymlinkToNfsPaths);
0085 }
0086 
0087 void KNetworkMountsTestPaths::cleanupTestCase()
0088 {
0089     KNetworkMounts::self()->sync();
0090     QFile::remove(m_configFileName);
0091 }
0092 
0093 void KNetworkMountsTestPaths::testPaths_data()
0094 {
0095     QTest::addColumn<QString>("path");
0096     QTest::addColumn<bool>("expected_path_option");
0097     QTest::addColumn<bool>("expected_path");
0098     QTest::addColumn<bool>("expected_symlink_dir");
0099     QTest::addColumn<bool>("expected_symlink_to_nfs_or_smb");
0100     QTest::addColumn<bool>("expected_nfs");
0101     QTest::addColumn<bool>("expected_smb");
0102 
0103     QTest::newRow("fast_path") << "/mnt" << false << false << false << false << false << false;
0104     QTest::newRow("fast_path_slash_end") << "/mnt/" << false << false << false << false << false << false;
0105     QTest::newRow("slow_path1") << "/mnt/server1" << true << true << false << false << false << true;
0106     QTest::newRow("slow_path2") << "/mnt/server2" << true << true << false << false << false << true;
0107     QTest::newRow("slow_path2_dir") << "/mnt/server2/dir" << true << true << false << false << false << true;
0108     QTest::newRow("slow_path2_dir_subdir") << "/mnt/server2/dir/subdir" << true << true << false << false << false << true;
0109     QTest::newRow("slow_path2_dir_subdir_slash_end") << "/mnt/server2/dir/subdir/" << true << true << false << false << false << true;
0110     QTest::newRow("slow_symlink_path") << "/home/user/netshares" << true << true << true << false << false << false;
0111     QTest::newRow("fast_path_root") << "/" << false << false << false << false << false << false;
0112     QTest::newRow("fast_path_home") << "/home" << false << false << false << false << false << false;
0113     QTest::newRow("fast_path_home_user") << "/home/user" << false << false << false << false << false << false;
0114     QTest::newRow("slow_symlink_path_subdir1") << "/home/user/netshares/subdir1" << true << true << true << false << false << false;
0115     QTest::newRow("slow_symlink_path_subdir1_subdir2") << "/home/user/netshares/subdir1/subdir2" << true << true << true << false << false << false;
0116     QTest::newRow("slow_symlink_path_subdir1_subdir2_slash_end") << "/home/user/netshares/subdir1/subdir2/" << true << true << true << false << false << false;
0117     QTest::newRow("slow_path_nfs") << "/mnt/nfs" << true << true << false << false << true << false;
0118     QTest::newRow("slow_path_nfs_dir") << "/mnt/nfs/dir" << true << true << false << false << true << false;
0119     QTest::newRow("slow_path_nfs_dir_subdir") << "/mnt/nfs/dir/subdir" << true << true << false << false << true << false;
0120     QTest::newRow("slow_path_nfs_dir_subdir_slash_end") << "/mnt/nfs/dir/subdir/" << true << true << false << false << true << false;
0121     QTest::newRow("slow_path_symlink_to_nfs") << "/somedir/symlinkToNfs" << true << true << false << true << false << false;
0122     QTest::newRow("slow_path_symlink_to_nfs_dir") << "/somedir/symlinkToNfs/dir" << true << true << false << true << false << false;
0123     QTest::newRow("slow_path_symlink_to_nfs_dir_subdir") << "/somedir/symlinkToNfs/dir/subdir" << true << true << false << true << false << false;
0124     QTest::newRow("slow_path_symlink_to_nfs_dir_subdir_slash_end") << "/somedir/symlinkToNfs/dir/subdir/" << true << true << false << true << false << false;
0125 }
0126 
0127 void KNetworkMountsTestPaths::testPaths()
0128 {
0129     QFETCH(QString, path);
0130     QFETCH(bool, expected_path_option);
0131     QFETCH(bool, expected_path);
0132     QFETCH(bool, expected_symlink_dir);
0133     QFETCH(bool, expected_symlink_to_nfs_or_smb);
0134     QFETCH(bool, expected_nfs);
0135     QFETCH(bool, expected_smb);
0136 
0137     QCOMPARE(KNetworkMounts::self()->isOptionEnabledForPath(path, KNetworkMounts::SymlinkPathsUseCache), expected_path_option);
0138     QCOMPARE(KNetworkMounts::self()->isOptionEnabledForPath(path, KNetworkMounts::KDirWatchUseINotify), expected_path_option);
0139     QCOMPARE(KNetworkMounts::self()->isOptionEnabledForPath(path, KNetworkMounts::KDirWatchDontAddWatches), expected_path_option);
0140     QCOMPARE(KNetworkMounts::self()->isOptionEnabledForPath(path, KNetworkMounts::LowSideEffectsOptimizations), expected_path_option);
0141     QCOMPARE(KNetworkMounts::self()->isOptionEnabledForPath(path, KNetworkMounts::MediumSideEffectsOptimizations), expected_path_option);
0142     QCOMPARE(KNetworkMounts::self()->isOptionEnabledForPath(path, KNetworkMounts::StrongSideEffectsOptimizations), expected_path_option);
0143 
0144     QCOMPARE(KNetworkMounts::self()->isSlowPath(path), expected_path);
0145 
0146     QCOMPARE(KNetworkMounts::self()->isSlowPath(path, KNetworkMounts::KNetworkMountsType::Any), expected_path);
0147 
0148     QCOMPARE(KNetworkMounts::self()->isSlowPath(path, KNetworkMounts::SymlinkDirectory), expected_symlink_dir);
0149 
0150     QCOMPARE(KNetworkMounts::self()->isSlowPath(path, KNetworkMounts::SymlinkToNetworkMount), expected_symlink_to_nfs_or_smb);
0151 
0152     QCOMPARE(KNetworkMounts::self()->isSlowPath(path, KNetworkMounts::NfsPaths), expected_nfs);
0153 
0154     QCOMPARE(KNetworkMounts::self()->isSlowPath(path, KNetworkMounts::SmbPaths), expected_smb);
0155 }
0156 
0157 #include "moc_knetworkmountstestpaths.cpp"