File indexing completed on 2024-04-21 14:52:54

0001 /*
0002     Copyright 2015 Harald Sitter <sitter@kde.org>
0003 
0004     This library is free software; you can redistribute it and/or
0005     modify it under the terms of the GNU Lesser General Public
0006     License as published by the Free Software Foundation; either
0007     version 2.1 of the License, or (at your option) version 3, or any
0008     later version accepted by the membership of KDE e.V. (or its
0009     successor approved by the membership of KDE e.V.), which shall
0010     act as a proxy defined in Section 6 of version 3 of the license.
0011 
0012     This library is distributed in the hope that it will be useful,
0013     but WITHOUT ANY WARRANTY; without even the implied warranty of
0014     MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the GNU
0015     Lesser General Public License for more details.
0016 
0017     You should have received a copy of the GNU Lesser General Public
0018     License along with this library. If not, see <http://www.gnu.org/licenses/>.
0019 */
0020 
0021 #include <QDirIterator>
0022 #include <QObject>
0023 #include <QTest>
0024 
0025 #include "testhelpers.h"
0026 
0027 class SymlinkTest : public QObject
0028 {
0029     Q_OBJECT
0030 
0031 private Q_SLOTS:
0032     void initTestCase()
0033     {
0034         // Go up one level from the bin dir
0035         m_buildDir = QDir(QCoreApplication::applicationDirPath() + "/..").canonicalPath();
0036     }
0037     // Invalid symlinks shouldn't happen.
0038     void test_broken()
0039     {
0040         // By default broken symlinks are not listed unless the System filter
0041         // is used. System may however also include pipes and the like, so we
0042         // still manually need to filter for symlinks afterwards.
0043         QDirIterator it(PROJECT_SOURCE_DIR, QDir::System, QDirIterator::Subdirectories);
0044         QList<QFileInfo> brokenSymLinks;
0045         while (it.hasNext()) {
0046             it.next();
0047             auto info = it.fileInfo();
0048             if (!info.isSymLink() || info.exists()) {
0049                 continue;
0050             }
0051             if (QFileInfo(info.absolutePath()).canonicalPath().startsWith(m_buildDir)) {
0052                 // ignore any symlink in the builddir, they might point to a relative path that doesn't exist in the builddir
0053                 // and everything will still be fine after make install, when reunited with the source dir
0054                 continue;
0055             }
0056             brokenSymLinks << info;
0057         }
0058         failSymlinkList(brokenSymLinks, QStringLiteral("Found broken symlinks:\n"));
0059     }
0060 
0061     // Symlinks should never point to something outside the tree, even if valid!
0062     void test_outOfTree()
0063     {
0064         QDirIterator it(PROJECT_SOURCE_DIR, QDir::AllEntries, QDirIterator::Subdirectories);
0065         QList<QFileInfo> OOTSymLinks;
0066         while (it.hasNext()) {
0067             it.next();
0068             auto info = it.fileInfo();
0069             if (!info.isSymLink() || info.symLinkTarget().startsWith(PROJECT_SOURCE_DIR)) {
0070                 continue;
0071             }
0072             OOTSymLinks << info;
0073         }
0074         failSymlinkList(OOTSymLinks, QStringLiteral("Found out-of-tree symlinks:\n"));
0075     }
0076 
0077 private:
0078     QString m_buildDir;
0079 };
0080 
0081 QTEST_GUILESS_MAIN(SymlinkTest)
0082 
0083 #include "symlinktest.moc"