File indexing completed on 2024-04-21 05:42:27

0001 // clang-format off
0002 /**
0003  * KDiff3 - Text Diff And Merge Tool
0004  *
0005  * SPDX-FileCopyrightText: 2020 Michael Reeves <reeves.87@gmail.com>
0006  * SPDX-License-Identifier: GPL-2.0-or-later
0007  *
0008  */
0009 // clang-format on
0010 
0011 #include <QTest>
0012 #include <QtGlobal>
0013 
0014 #include "../fileaccess.h"
0015 #include "FileAccessJobHandlerMoc.h"
0016 
0017 class FileAccessMoc: public FileAccess
0018 {
0019   public:
0020     void setEngine(FileAccessJobHandler* handler)
0021     {
0022         if(handler != mJobHandler.get()) mJobHandler.reset(handler);
0023     }
0024 
0025     void setParent(FileAccessMoc* parent)
0026     {
0027         m_pParent = (FileAccess*)parent;
0028         m_baseDir = parent->m_baseDir;
0029     }
0030     void setFileName(const QString& name) { m_name = name; }
0031     void setUrl(const QUrl& url) { m_url = url; }
0032 
0033     void loadData() override {}
0034 };
0035 
0036 class FileAccessTest: public QObject
0037 {
0038     Q_OBJECT;
0039 
0040   private Q_SLOTS:
0041 
0042     void testFileRelPath()
0043     {
0044         FileAccessMoc mocFile, mocRoot, mocFile2;
0045 
0046         mocRoot.setEngine(new FileAccessJobHandlerMoc(&mocRoot));
0047         mocFile.setEngine(new FileAccessJobHandlerMoc(&mocFile));
0048         mocFile2.setEngine(new FileAccessJobHandlerMoc(&mocFile2));
0049 
0050         //Check remote url.
0051         mocRoot.setFile(u8"fish://i@0.0.0.0/root");
0052         QCOMPARE(mocRoot.fileRelPath(), u8"");
0053 
0054         mocFile.setFile(u8"fish://i@0.0.0.0/root/x");
0055         mocFile.setParent(&mocRoot);
0056         QCOMPARE(mocFile.fileRelPath(), QStringLiteral("x"));
0057 
0058         mocFile2.setFile("fish://i@0.0.0.0/root/x/y");
0059         mocFile2.setParent(&mocFile);
0060         QCOMPARE(mocFile2.fileRelPath(), QStringLiteral("x/y"));
0061     }
0062 
0063     void testUrl()
0064     {
0065 #ifndef Q_OS_WIN
0066         const QString expected = "/dds/root";
0067         const QString expected2 = "file:///dds/root";
0068 #else
0069         const QString expected = "C:/dds/root";
0070         const QString expected2 = "file:///C:/dds/root";
0071 #endif // !Q_OS_WIN
0072         FileAccessMoc mocFile;
0073 
0074         mocFile.setEngine(new FileAccessJobHandlerMoc(&mocFile));
0075 
0076         //Sanity check FileAccess::url by directly setting the url.
0077         mocFile.setFileName(u8"root");
0078         mocFile.setUrl(QUrl("fish://i@0.0.0.0/root"));
0079         QCOMPARE(mocFile.url(), QUrl("fish://i@0.0.0.0/root"));
0080 
0081         //Now check that setFile does what its supposed to
0082         mocFile.setFile(QUrl("fish://i@0.0.0.0/root"));
0083         QCOMPARE(mocFile.url(), QUrl("fish://i@0.0.0.0/root"));
0084 
0085         mocFile.setFile(QUrl(expected));
0086         QCOMPARE(mocFile.url(), QUrl(expected));
0087         //Try the QString version
0088         mocFile.setFile("fish://i@0.0.0.0/root");
0089         QCOMPARE(mocFile.url(), QUrl("fish://i@0.0.0.0/root"));
0090 
0091         mocFile.setFile(expected);
0092         QCOMPARE(mocFile.url(), QUrl(expected2));
0093     }
0094 
0095     void testIsLocal()
0096     {
0097         FileAccessMoc mocFile;
0098 
0099         mocFile.setEngine(new FileAccessJobHandlerMoc(&mocFile));
0100 
0101         QVERIFY(!FileAccess::isLocal(QUrl("fish://i@0.0.0.0/root")));
0102         QVERIFY(FileAccess::isLocal(QUrl("file:///dds/root")));
0103         QVERIFY(FileAccess::isLocal(QUrl("/dds/root")));
0104         //Check remote url.
0105         mocFile.setFile("fish://i@0.0.0.0/root");
0106         QVERIFY(!mocFile.isLocal());
0107 
0108         //Check local file.
0109         mocFile.setFile(QStringLiteral("file:///dds/root"));
0110         QVERIFY(mocFile.isLocal());
0111         //Path only
0112         mocFile.setFile(QStringLiteral("/dds/root"));
0113         QVERIFY(mocFile.isLocal());
0114     }
0115 
0116     void testAbsolutePath()
0117     {
0118 #ifndef Q_OS_WIN
0119         const QString expected = "/dds/root";
0120 #else
0121         const QString expected = "c:/dds/root";
0122 #endif // !Q_OS_WIN
0123         FileAccessMoc mocFile;
0124         mocFile.setEngine(new FileAccessJobHandlerMoc(&mocFile));
0125 
0126         const QUrl url = QUrl("fish://i@0.0.0.0/root");
0127 
0128         QCOMPARE(FileAccess::prettyAbsPath(url), url.toDisplayString());
0129         QCOMPARE(FileAccess::prettyAbsPath(QUrl("file:///dds/root")).toLower(), expected);
0130         QCOMPARE(FileAccess::prettyAbsPath(QUrl("/dds/root")).toLower(), expected);
0131         QCOMPARE(FileAccess::prettyAbsPath(QUrl("c:/dds/root")).toLower(), "c:/dds/root");
0132         //work around for bad path in windows drop event urls. (Qt 5.15.2 affected)
0133 #ifndef Q_OS_WIN
0134         QCOMPARE(FileAccess::prettyAbsPath(QUrl("file:///c:/dds/root")).toLower(), "/c:/dds/root");
0135 #else
0136         QCOMPARE(FileAccess::prettyAbsPath(QUrl("file:///c:/dds/root")).toLower(), "c:/dds/root");
0137 #endif // !Q_OS_WIN
0138 
0139         mocFile.setFile(url);
0140         QCOMPARE(mocFile.prettyAbsPath(), url.toDisplayString());
0141 
0142         mocFile.setFile(QUrl("file:///dds/root"));
0143         QCOMPARE(mocFile.prettyAbsPath().toLower(), expected);
0144 
0145         mocFile.setFile(QUrl("/dds/root"));
0146         QCOMPARE(mocFile.prettyAbsPath().toLower(), expected);
0147 
0148         mocFile.setFile(QStringLiteral("file:///dds/root"));
0149         QCOMPARE(mocFile.prettyAbsPath().toLower(), expected);
0150 
0151         mocFile.setFile(QStringLiteral("/dds/root"));
0152         QCOMPARE(mocFile.prettyAbsPath().toLower(), expected);
0153     }
0154 
0155     void liveTest()
0156     {
0157         QTemporaryFile testFile;
0158         QVERIFY(testFile.open());
0159         FileAccess fileData(testFile.fileName());
0160 
0161         QVERIFY(fileData.isValid());
0162         QVERIFY(fileData.isLocal());
0163         QVERIFY(fileData.isNormal());
0164         QVERIFY(fileData.isReadable());
0165         QVERIFY(fileData.isWritable());
0166         //sanity check
0167         QVERIFY(!fileData.isSymLink());
0168         QVERIFY(!fileData.isDir());
0169         QVERIFY(fileData.isFile());
0170     }
0171 };
0172 
0173 QTEST_APPLESS_MAIN(FileAccessTest);
0174 
0175 #include "FileAccessTest.moc"