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

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 "aktest.h"
0011 #include "private/protocol_p.h"
0012 
0013 #include <typeinfo>
0014 
0015 #include "handler.h"
0016 #include "handler/collectioncopyhandler.h"
0017 #include "handler/collectioncreatehandler.h"
0018 #include "handler/collectiondeletehandler.h"
0019 #include "handler/collectionfetchhandler.h"
0020 #include "handler/collectionmodifyhandler.h"
0021 #include "handler/collectionmovehandler.h"
0022 #include "handler/collectionstatsfetchhandler.h"
0023 #include "handler/itemcopyhandler.h"
0024 #include "handler/itemcreatehandler.h"
0025 #include "handler/itemdeletehandler.h"
0026 #include "handler/itemfetchhandler.h"
0027 #include "handler/itemlinkhandler.h"
0028 #include "handler/itemmodifyhandler.h"
0029 #include "handler/itemmovehandler.h"
0030 #include "handler/loginhandler.h"
0031 #include "handler/logouthandler.h"
0032 #include "handler/resourceselecthandler.h"
0033 #include "handler/searchcreatehandler.h"
0034 #include "handler/searchhandler.h"
0035 #include "handler/transactionhandler.h"
0036 
0037 #include "fakeakonadiserver.h"
0038 
0039 using namespace Akonadi;
0040 using namespace Akonadi::Server;
0041 
0042 #define MAKE_CMD_ROW(command, class) QTest::newRow(#command) << (command) << QByteArray(typeid(Akonadi::Server::class).name());
0043 
0044 class HandlerTest : public QObject
0045 {
0046     Q_OBJECT
0047 
0048     FakeAkonadiServer mAkonadi;
0049 
0050 public:
0051     explicit HandlerTest()
0052     {
0053         mAkonadi.init();
0054     }
0055 
0056 private:
0057     void setupTestData()
0058     {
0059         QTest::addColumn<Protocol::Command::Type>("command");
0060         QTest::addColumn<QByteArray>("className");
0061     }
0062 
0063     void addAuthCommands()
0064     {
0065         MAKE_CMD_ROW(Protocol::Command::CreateCollection, CollectionCreateHandler)
0066         MAKE_CMD_ROW(Protocol::Command::FetchCollections, CollectionFetchHandler)
0067         MAKE_CMD_ROW(Protocol::Command::StoreSearch, SearchCreateHandler)
0068         MAKE_CMD_ROW(Protocol::Command::Search, SearchHandler)
0069         MAKE_CMD_ROW(Protocol::Command::FetchItems, ItemFetchHandler)
0070         MAKE_CMD_ROW(Protocol::Command::ModifyItems, ItemModifyHandler)
0071         MAKE_CMD_ROW(Protocol::Command::FetchCollectionStats, CollectionStatsFetchHandler)
0072         MAKE_CMD_ROW(Protocol::Command::DeleteCollection, CollectionDeleteHandler)
0073         MAKE_CMD_ROW(Protocol::Command::ModifyCollection, CollectionModifyHandler)
0074         MAKE_CMD_ROW(Protocol::Command::Transaction, TransactionHandler)
0075         MAKE_CMD_ROW(Protocol::Command::CreateItem, ItemCreateHandler)
0076         MAKE_CMD_ROW(Protocol::Command::CopyItems, ItemCopyHandler)
0077         MAKE_CMD_ROW(Protocol::Command::CopyCollection, CollectionCopyHandler)
0078         MAKE_CMD_ROW(Protocol::Command::LinkItems, ItemLinkHandler)
0079         MAKE_CMD_ROW(Protocol::Command::SelectResource, ResourceSelectHandler)
0080         MAKE_CMD_ROW(Protocol::Command::DeleteItems, ItemDeleteHandler)
0081         MAKE_CMD_ROW(Protocol::Command::MoveItems, ItemMoveHandler)
0082         MAKE_CMD_ROW(Protocol::Command::MoveCollection, CollectionMoveHandler)
0083     }
0084 
0085     void addNonAuthCommands()
0086     {
0087         MAKE_CMD_ROW(Protocol::Command::Login, LoginHandler)
0088     }
0089 
0090     void addAlwaysCommands()
0091     {
0092         MAKE_CMD_ROW(Protocol::Command::Logout, LogoutHandler)
0093     }
0094 
0095     void addInvalidCommands()
0096     {
0097         // MAKE_CMD_ROW(Protocol::Command::Invalid, UnknownCommandHandler)
0098     }
0099 
0100     template<typename T>
0101     QByteArray typeName(const T &t)
0102     {
0103         const auto &v = *t;
0104         return typeid(v).name();
0105     }
0106 
0107 private Q_SLOTS:
0108     void testFindAuthenticatedCommand_data()
0109     {
0110         setupTestData();
0111         addAuthCommands();
0112     }
0113 
0114     void testFindAuthenticatedCommand()
0115     {
0116         QFETCH(Protocol::Command::Type, command);
0117         QFETCH(QByteArray, className);
0118         const auto handler = Handler::findHandlerForCommandAuthenticated(command, mAkonadi);
0119         QVERIFY(handler);
0120         QCOMPARE(typeName(handler), className);
0121     }
0122 
0123     void testFindAuthenticatedCommandNegative_data()
0124     {
0125         setupTestData();
0126         addNonAuthCommands();
0127         addAlwaysCommands();
0128         addInvalidCommands();
0129     }
0130 
0131     void testFindAuthenticatedCommandNegative()
0132     {
0133         QFETCH(Protocol::Command::Type, command);
0134         QFETCH(QByteArray, className);
0135 
0136         const auto handler = Handler::findHandlerForCommandAuthenticated(command, mAkonadi);
0137         QVERIFY(!handler);
0138     }
0139 
0140     void testFindNonAutenticatedCommand_data()
0141     {
0142         setupTestData();
0143         addNonAuthCommands();
0144     }
0145 
0146     void testFindNonAutenticatedCommand()
0147     {
0148         QFETCH(Protocol::Command::Type, command);
0149         QFETCH(QByteArray, className);
0150 
0151         auto handler = Handler::findHandlerForCommandNonAuthenticated(command, mAkonadi);
0152         QVERIFY(handler);
0153         QCOMPARE(typeName(handler), className);
0154     }
0155 
0156     void testFindAlwaysCommand_data()
0157     {
0158         setupTestData();
0159         addAlwaysCommands();
0160     }
0161 
0162     void testFindAlwaysCommand()
0163     {
0164         QFETCH(Protocol::Command::Type, command);
0165         QFETCH(QByteArray, className);
0166 
0167         const auto handler = Handler::findHandlerForCommandAlwaysAllowed(command, mAkonadi);
0168         QVERIFY(handler);
0169         QCOMPARE(typeName(handler), className);
0170     }
0171 
0172     void testFindAlwaysCommandNegative_data()
0173     {
0174         setupTestData();
0175         addAuthCommands();
0176         addNonAuthCommands();
0177         addInvalidCommands();
0178     }
0179 
0180     void testFindAlwaysCommandNegative()
0181     {
0182         QFETCH(Protocol::Command::Type, command);
0183         QFETCH(QByteArray, className);
0184 
0185         const auto handler = Handler::findHandlerForCommandAlwaysAllowed(command, mAkonadi);
0186         QVERIFY(!handler);
0187     }
0188 };
0189 
0190 AKTEST_MAIN(HandlerTest)
0191 
0192 #include "handlertest.moc"