Warning, file /plasma/flatpak-kcm/autotests/flatpakfilesystemstest.cpp was not indexed or was modified since last indexation (in which case cross-reference links may be missing, inaccurate or erroneous).

0001 // SPDX-License-Identifier: GPL-2.0-only OR GPL-3.0-only OR LicenseRef-KDE-Accepted-GPL
0002 // SPDX-FileCopyrightText: 2023 ivan tkachenko <me@ratijas.tk>
0003 
0004 #include <QDebug>
0005 #include <QMetaEnum>
0006 #include <QTest>
0007 
0008 #include "flatpakpermission.h"
0009 
0010 class FlatpakFilesystemTest : public QObject
0011 {
0012     Q_OBJECT
0013 
0014     using FilesystemPrefix = FlatpakFilesystemsEntry::FilesystemPrefix;
0015     using AccessMode = FlatpakFilesystemsEntry::AccessMode;
0016 
0017 private Q_SLOTS:
0018 
0019     void testRoundtrip_data()
0020     {
0021         QTest::addColumn<QString>("rawInput");
0022         QTest::addColumn<QString>("expectedOutput");
0023 
0024         QTest::newRow("/path is same")   << QStringLiteral("/path")   << QStringLiteral("/path");
0025         QTest::newRow("!/path is same")  << QStringLiteral("!/path")  << QStringLiteral("!/path");
0026 
0027         QTest::newRow("home is home")     << QStringLiteral("home")   << QStringLiteral("home");
0028         QTest::newRow("~ is home too")    << QStringLiteral("~")      << QStringLiteral("home");
0029         QTest::newRow("not home")         << QStringLiteral("!home")  << QStringLiteral("!home");
0030         QTest::newRow("~/path is ~/path") << QStringLiteral("~/path") << QStringLiteral("~/path");
0031 
0032         QTest::newRow("host is same")     << QStringLiteral("host")     << QStringLiteral("host");
0033         QTest::newRow("host-os is same")  << QStringLiteral("host-os")  << QStringLiteral("host-os");
0034         QTest::newRow("host-etc is same") << QStringLiteral("host-etc") << QStringLiteral("host-etc");
0035 
0036         QTest::newRow("xdg-desktop is same")           << QStringLiteral("xdg-desktop")           << QStringLiteral("xdg-desktop");
0037         QTest::newRow("xdg-desktop/path is same")      << QStringLiteral("xdg-desktop/path")      << QStringLiteral("xdg-desktop/path");
0038         QTest::newRow("xdg-documents is same")         << QStringLiteral("xdg-documents")         << QStringLiteral("xdg-documents");
0039         QTest::newRow("xdg-documents/path is same")    << QStringLiteral("xdg-documents/path")    << QStringLiteral("xdg-documents/path");
0040         QTest::newRow("xdg-download is same")          << QStringLiteral("xdg-download")          << QStringLiteral("xdg-download");
0041         QTest::newRow("xdg-download/path is same")     << QStringLiteral("xdg-download/path")     << QStringLiteral("xdg-download/path");
0042         QTest::newRow("xdg-music is same")             << QStringLiteral("xdg-music")             << QStringLiteral("xdg-music");
0043         QTest::newRow("xdg-music/path is same")        << QStringLiteral("xdg-music/path")        << QStringLiteral("xdg-music/path");
0044         QTest::newRow("xdg-pictures is same")          << QStringLiteral("xdg-pictures")          << QStringLiteral("xdg-pictures");
0045         QTest::newRow("xdg-pictures/path is same")     << QStringLiteral("xdg-pictures/path")     << QStringLiteral("xdg-pictures/path");
0046         QTest::newRow("xdg-public-share is same")      << QStringLiteral("xdg-public-share")      << QStringLiteral("xdg-public-share");
0047         QTest::newRow("xdg-public-share/path is same") << QStringLiteral("xdg-public-share/path") << QStringLiteral("xdg-public-share/path");
0048         QTest::newRow("xdg-videos is same")            << QStringLiteral("xdg-videos")            << QStringLiteral("xdg-videos");
0049         QTest::newRow("xdg-videos/path is same")       << QStringLiteral("xdg-videos/path")       << QStringLiteral("xdg-videos/path");
0050         QTest::newRow("xdg-templates is same")         << QStringLiteral("xdg-templates")         << QStringLiteral("xdg-templates");
0051         QTest::newRow("xdg-templates/path is same")    << QStringLiteral("xdg-templates/path")    << QStringLiteral("xdg-templates/path");
0052 
0053         QTest::newRow("xdg-cache is same")             << QStringLiteral("xdg-cache")             << QStringLiteral("xdg-cache");
0054         QTest::newRow("xdg-cache/path is same")        << QStringLiteral("xdg-cache/path")        << QStringLiteral("xdg-cache/path");
0055         QTest::newRow("xdg-config is same")            << QStringLiteral("xdg-config")            << QStringLiteral("xdg-config");
0056         QTest::newRow("xdg-config/path is same")       << QStringLiteral("xdg-config/path")       << QStringLiteral("xdg-config/path");
0057         QTest::newRow("xdg-data is same")              << QStringLiteral("xdg-data")              << QStringLiteral("xdg-data");
0058         QTest::newRow("xdg-data/path is same")         << QStringLiteral("xdg-data/path")         << QStringLiteral("xdg-data/path");
0059 
0060         QTest::newRow("xdg-run/path is same")          << QStringLiteral("xdg-run/path")          << QStringLiteral("xdg-run/path");
0061 
0062         QTest::newRow(":ro is same")        << QStringLiteral("/path:ro")     << QStringLiteral("/path:ro");
0063         QTest::newRow(":rw is default")     << QStringLiteral("/path:rw")     << QStringLiteral("/path");
0064         QTest::newRow(":create is default") << QStringLiteral("/path:create") << QStringLiteral("/path:create");
0065     }
0066 
0067     void testRoundtrip()
0068     {
0069         QFETCH(QString, rawInput);
0070         QFETCH(QString, expectedOutput);
0071         const auto entry = FlatpakFilesystemsEntry::parse(rawInput);
0072         QVERIFY(entry.has_value());
0073         const auto actual = entry.value().format();
0074         QCOMPARE(actual, expectedOutput);
0075     }
0076 
0077     void testInvalid_data()
0078     {
0079         QTest::addColumn<QString>("rawInput");
0080 
0081         QTest::newRow("empty is invalid") << QStringLiteral("");
0082 
0083         QTest::newRow("/ is invalid")  << QStringLiteral("/");
0084         QTest::newRow("!/ is invalid") << QStringLiteral("!/");
0085 
0086         QTest::newRow("xdg-run/ can't omit path") << QStringLiteral("xdg-run/");
0087 
0088         QTest::newRow("standalone :ro")     << QStringLiteral(":ro");
0089         QTest::newRow("standalone :rw")     << QStringLiteral(":rw");
0090         QTest::newRow("standalone :create") << QStringLiteral(":create");
0091         QTest::newRow("standalone !")       << QStringLiteral("!");
0092 
0093         QTest::newRow("combine !:ro")                << QStringLiteral("!:ro");
0094         QTest::newRow("combine !:rw")                << QStringLiteral("!:rw");
0095         QTest::newRow("combine !:create")            << QStringLiteral("!:create");
0096         QTest::newRow("combine !:create with /path") << QStringLiteral("!/path:create");
0097     }
0098 
0099     void testInvalid()
0100     {
0101         QFETCH(QString, rawInput);
0102         const auto entry = FlatpakFilesystemsEntry::parse(rawInput);
0103         QVERIFY(!entry.has_value());
0104     }
0105 
0106     // Technically this is already covered by testRoundtrip, but we also want to test equality operator.
0107     void testParts_data()
0108     {
0109         QTest::addColumn<QString>("rawInput");
0110         QTest::addColumn<FlatpakFilesystemsEntry>("expected");
0111 
0112         QTest::newRow("/path")    << QStringLiteral("/path")        << FlatpakFilesystemsEntry(FilesystemPrefix::Absolute, AccessMode::ReadWrite, QLatin1String("path"));
0113         QTest::newRow("not home") << QStringLiteral("!home")        << FlatpakFilesystemsEntry(FilesystemPrefix::Home, AccessMode::Deny, QLatin1String());
0114         QTest::newRow(":ro")      << QStringLiteral("/path:ro")     << FlatpakFilesystemsEntry(FilesystemPrefix::Absolute, AccessMode::ReadOnly, QLatin1String("path"));
0115         QTest::newRow(":create")  << QStringLiteral("/path:create") << FlatpakFilesystemsEntry(FilesystemPrefix::Absolute, AccessMode::Create, QLatin1String("path"));
0116         // Instead of treating entries as invalid, parse them as Unknown and preserve content.
0117         QTest::newRow("host can't have path")     << QStringLiteral("host/abcd")     << FlatpakFilesystemsEntry(FilesystemPrefix::Unknown, AccessMode::ReadWrite, QStringLiteral("host/abcd"));
0118         QTest::newRow("host-os can't have path")  << QStringLiteral("host-os/abcd")  << FlatpakFilesystemsEntry(FilesystemPrefix::Unknown, AccessMode::ReadWrite, QStringLiteral("host-os/abcd"));
0119         QTest::newRow("host-etc can't have path") << QStringLiteral("host-etc/abcd") << FlatpakFilesystemsEntry(FilesystemPrefix::Unknown, AccessMode::ReadWrite, QStringLiteral("host-etc/abcd"));
0120 
0121         QTest::newRow("not host still can't have path") << QStringLiteral("!host/abcd") << FlatpakFilesystemsEntry(FilesystemPrefix::Unknown, AccessMode::Deny, QStringLiteral("host/abcd"));
0122 
0123         QTest::newRow("xdg-run can't omit path") << QStringLiteral("xdg-run")
0124                                                  << FlatpakFilesystemsEntry(FilesystemPrefix::Unknown, AccessMode::ReadWrite, QStringLiteral("xdg-run"));
0125         ;
0126     }
0127 
0128     void testParts()
0129     {
0130         QFETCH(QString, rawInput);
0131         QFETCH(FlatpakFilesystemsEntry, expected);
0132         const auto entry = FlatpakFilesystemsEntry::parse(rawInput);
0133         QVERIFY(entry.has_value());
0134         const auto actual = entry.value();
0135         QCOMPARE(actual, expected);
0136         // Test != operator as well
0137         QCOMPARE(!(actual == expected), (actual != expected));
0138     }
0139 };
0140 
0141 QTEST_MAIN(FlatpakFilesystemTest)
0142 
0143 #include "flatpakfilesystemstest.moc"