File indexing completed on 2024-11-10 04:40:16

0001 /*
0002     SPDX-FileCopyrightText: 2011 Volker Krause <vkrause@kde.org>
0003 
0004     SPDX-License-Identifier: LGPL-2.0-or-later
0005 */
0006 
0007 #include <QObject>
0008 #include <QTest>
0009 
0010 #include "private/dbus_p.h"
0011 
0012 #include "shared/aktest.h"
0013 
0014 using namespace Akonadi;
0015 
0016 Q_DECLARE_METATYPE(DBus::AgentType)
0017 
0018 class DBusTest : public QObject
0019 {
0020     Q_OBJECT
0021 private Q_SLOTS:
0022     void testServiceName()
0023     {
0024         akTestSetInstanceIdentifier(QString());
0025         QCOMPARE(DBus::serviceName(DBus::Server), QLatin1StringView("org.freedesktop.Akonadi"));
0026         akTestSetInstanceIdentifier(QStringLiteral("foo"));
0027         QCOMPARE(DBus::serviceName(DBus::Server), QLatin1StringView("org.freedesktop.Akonadi.foo"));
0028     }
0029 
0030     void testParseAgentServiceName_data()
0031     {
0032         QTest::addColumn<QString>("instanceId");
0033         QTest::addColumn<QString>("serviceName");
0034         QTest::addColumn<QString>("agentId");
0035         QTest::addColumn<DBus::AgentType>("agentType");
0036         QTest::addColumn<bool>("valid");
0037 
0038         // generic invalid
0039         QTest::newRow("empty") << QString() << QString() << QString() << DBus::Unknown << false;
0040         QTest::newRow("wrong base") << QString() << "org.freedesktop.Agent.foo" << QString() << DBus::Unknown << false;
0041         QTest::newRow("wrong type") << QString() << "org.freedesktop.Akonadi.Randomizer.akonadi_maildir_resource_0" << QString() << DBus::Unknown << false;
0042         QTest::newRow("too long") << QString() << "org.freedesktop.Akonadi.Agent.akonadi_maildir_resource_0.foo.bar" << QString() << DBus::Unknown << false;
0043 
0044         // single instance cases
0045         QTest::newRow("agent, no multi-instance") << QString() << "org.freedesktop.Akonadi.Agent.akonadi_maildir_resource_0"
0046                                                   << "akonadi_maildir_resource_0" << DBus::Agent << true;
0047         QTest::newRow("resource, no multi-instance") << QString() << "org.freedesktop.Akonadi.Resource.akonadi_maildir_resource_0"
0048                                                      << "akonadi_maildir_resource_0" << DBus::Resource << true;
0049         QTest::newRow("preproc, no multi-instance") << QString() << "org.freedesktop.Akonadi.Preprocessor.akonadi_maildir_resource_0"
0050                                                     << "akonadi_maildir_resource_0" << DBus::Preprocessor << true;
0051         QTest::newRow("multi-instance name in single-instance setup")
0052             << QString() << "org.freedesktop.Akonadi.Agent.akonadi_maildir_resource_0.foo" << QString() << DBus::Unknown << false;
0053 
0054         // multi-instance cases
0055         QTest::newRow("agent, multi-instance") << "foo"
0056                                                << "org.freedesktop.Akonadi.Agent.akonadi_maildir_resource_0.foo"
0057                                                << "akonadi_maildir_resource_0" << DBus::Agent << true;
0058         QTest::newRow("resource, multi-instance") << "foo"
0059                                                   << "org.freedesktop.Akonadi.Resource.akonadi_maildir_resource_0.foo"
0060                                                   << "akonadi_maildir_resource_0" << DBus::Resource << true;
0061         QTest::newRow("preproc, multi-instance") << "foo"
0062                                                  << "org.freedesktop.Akonadi.Preprocessor.akonadi_maildir_resource_0.foo"
0063                                                  << "akonadi_maildir_resource_0" << DBus::Preprocessor << true;
0064         QTest::newRow("single-instance name in multi-instance setup")
0065             << "foo"
0066             << "org.freedesktop.Akonadi.Agent.akonadi_maildir_resource_0" << QString() << DBus::Unknown << false;
0067     }
0068 
0069     void testParseAgentServiceName()
0070     {
0071         QFETCH(QString, instanceId);
0072         QFETCH(QString, serviceName);
0073         QFETCH(QString, agentId);
0074         QFETCH(DBus::AgentType, agentType);
0075         QFETCH(bool, valid);
0076 
0077         akTestSetInstanceIdentifier(instanceId);
0078 
0079         const auto service = DBus::parseAgentServiceName(serviceName);
0080         QCOMPARE(service.has_value(), valid);
0081         if (service.has_value()) {
0082             QCOMPARE(service->identifier, agentId);
0083             QCOMPARE(service->agentType, agentType);
0084         }
0085     }
0086 
0087     void testAgentServiceName()
0088     {
0089         akTestSetInstanceIdentifier(QString());
0090         QCOMPARE(DBus::agentServiceName(QStringLiteral("akonadi_maildir_resource_0"), DBus::Agent),
0091                  QStringLiteral("org.freedesktop.Akonadi.Agent.akonadi_maildir_resource_0"));
0092 
0093         akTestSetInstanceIdentifier(QStringLiteral("foo"));
0094         QCOMPARE(DBus::agentServiceName(QStringLiteral("akonadi_maildir_resource_0"), DBus::Agent),
0095                  QStringLiteral("org.freedesktop.Akonadi.Agent.akonadi_maildir_resource_0.foo"));
0096     }
0097 
0098     void testParseInstanceIdentifier_data()
0099     {
0100         QTest::addColumn<QString>("serviceName");
0101         QTest::addColumn<bool>("hasInstance");
0102 
0103         QTest::newRow("server") << QStringLiteral("org.freedesktop.Akonadi") << false;
0104         QTest::newRow("server instance") << QStringLiteral("org.freedesktop.Akonadi.instance") << true;
0105         QTest::newRow("control") << QStringLiteral("org.freedesktop.Akonadi.Control") << false;
0106         QTest::newRow("control instance") << QStringLiteral("org.freedesktop.Akonadi.Control.instance") << true;
0107         QTest::newRow("control lock") << QStringLiteral("org.freedesktop.Akonadi.Control.lock") << false;
0108         QTest::newRow("control lock instance") << QStringLiteral("org.freedesktop.Akonadi.Control.lock.instance") << true;
0109         QTest::newRow("janitor") << QStringLiteral("org.freedesktop.Akonadi.Janitor") << false;
0110         QTest::newRow("janitor instance") << QStringLiteral("org.freedesktop.Akonadi.Janitor.instance") << true;
0111         QTest::newRow("agentserver") << QStringLiteral("org.freedesktop.Akonadi.AgentServer") << false;
0112         QTest::newRow("agentserver instance") << QStringLiteral("org.freedesktop.Akonadi.AgentServer.instance") << true;
0113         QTest::newRow("upgrading") << QStringLiteral("org.freedesktop.Akonadi.upgrading") << false;
0114         QTest::newRow("upgrading instance") << QStringLiteral("org.freedesktop.Akonadi.upgrading.instance") << true;
0115         QTest::newRow("agent") << QStringLiteral("org.freedesktop.Akonadi.Agent.akonadi_agent_identifier") << false;
0116         QTest::newRow("agent instance") << QStringLiteral("org.freedesktop.Akonadi.Agent.akonadi_agent_identifier.instance") << true;
0117         QTest::newRow("resource") << QStringLiteral("org.freedesktop.Akonadi.Resource.akonadi_resource_identifier") << false;
0118         QTest::newRow("resource instance") << QStringLiteral("org.freedesktop.Akonadi.Resource.akonadi_resource_identifier.instance") << true;
0119         QTest::newRow("preprocessor") << QStringLiteral("org.freedesktop.Akonadi.Preprocessor.akonadi_preprocessor_identifier") << false;
0120         QTest::newRow("preprocessor instance") << QStringLiteral("org.freedesktop.Akonadi.Preprocessor.akonadi_preprocessor_identifier.instance") << true;
0121     }
0122 
0123     void testParseInstanceIdentifier()
0124     {
0125         QFETCH(QString, serviceName);
0126         QFETCH(bool, hasInstance);
0127 
0128         const auto identifier = DBus::parseInstanceIdentifier(serviceName);
0129         QCOMPARE(identifier.has_value(), hasInstance);
0130         if (hasInstance) {
0131             QCOMPARE(*identifier, QStringLiteral("instance"));
0132         }
0133     }
0134 };
0135 
0136 AKTEST_MAIN(DBusTest)
0137 
0138 #include "akdbustest.moc"