File indexing completed on 2024-11-10 04:40:17
0001 /* 0002 * SPDX-FileCopyrightText: 2015 Daniel Vrátil <dvratil@redhat.com> 0003 * 0004 * SPDX-License-Identifier: LGPL-2.1-or-later 0005 * 0006 */ 0007 0008 #include "protocoltest.h" 0009 0010 #include "private/scope_p.h" 0011 0012 #include <QTest> 0013 0014 using namespace Akonadi; 0015 using namespace Akonadi::Protocol; 0016 0017 void ProtocolTest::testFactory_data() 0018 { 0019 QTest::addColumn<Command::Type>("type"); 0020 QTest::addColumn<bool>("response"); 0021 QTest::addColumn<bool>("success"); 0022 0023 QTest::newRow("invalid cmd") << Command::Invalid << false << false; 0024 QTest::newRow("invalid resp") << Command::Invalid << true << false; 0025 QTest::newRow("hello cmd") << Command::Hello << false << false; 0026 QTest::newRow("hello resp") << Command::Hello << true << true; 0027 QTest::newRow("login cmd") << Command::Login << false << true; 0028 QTest::newRow("login resp") << Command::Login << true << true; 0029 QTest::newRow("logout cmd") << Command::Logout << false << true; 0030 QTest::newRow("logout resp") << Command::Logout << true << true; 0031 QTest::newRow("transaction cmd") << Command::Transaction << false << true; 0032 QTest::newRow("transaction resp") << Command::Transaction << true << true; 0033 QTest::newRow("createItem cmd") << Command::CreateItem << false << true; 0034 QTest::newRow("createItem resp") << Command::CreateItem << true << true; 0035 QTest::newRow("copyItems cmd") << Command::CopyItems << false << true; 0036 QTest::newRow("copyItems resp") << Command::CopyItems << true << true; 0037 QTest::newRow("deleteItems cmd") << Command::DeleteItems << false << true; 0038 QTest::newRow("deleteItems resp") << Command::DeleteItems << true << true; 0039 QTest::newRow("fetchItems cmd") << Command::FetchItems << false << true; 0040 QTest::newRow("fetchItems resp") << Command::FetchItems << true << true; 0041 QTest::newRow("linkItems cmd") << Command::LinkItems << false << true; 0042 QTest::newRow("linkItems resp") << Command::LinkItems << true << true; 0043 QTest::newRow("modifyItems cmd") << Command::ModifyItems << false << true; 0044 QTest::newRow("modifyItems resp") << Command::ModifyItems << true << true; 0045 QTest::newRow("moveItems cmd") << Command::MoveItems << false << true; 0046 QTest::newRow("moveItems resp") << Command::MoveItems << true << true; 0047 QTest::newRow("createCollection cmd") << Command::CreateCollection << false << true; 0048 QTest::newRow("createCollection resp") << Command::CreateCollection << true << true; 0049 QTest::newRow("copyCollection cmd") << Command::CopyCollection << false << true; 0050 QTest::newRow("copyCollection resp") << Command::CopyCollection << true << true; 0051 QTest::newRow("deleteCollection cmd") << Command::DeleteCollection << false << true; 0052 QTest::newRow("deleteCollection resp") << Command::DeleteCollection << true << true; 0053 QTest::newRow("fetchCollections cmd") << Command::FetchCollections << false << true; 0054 QTest::newRow("fetchCollections resp") << Command::FetchCollections << true << true; 0055 QTest::newRow("fetchCollectionStats cmd") << Command::FetchCollectionStats << false << true; 0056 QTest::newRow("fetchCollectionStats resp") << Command::FetchCollectionStats << false << true; 0057 QTest::newRow("modifyCollection cmd") << Command::ModifyCollection << false << true; 0058 QTest::newRow("modifyCollection resp") << Command::ModifyCollection << true << true; 0059 QTest::newRow("moveCollection cmd") << Command::MoveCollection << false << true; 0060 QTest::newRow("moveCollection resp") << Command::MoveCollection << true << true; 0061 QTest::newRow("search cmd") << Command::Search << false << true; 0062 QTest::newRow("search resp") << Command::Search << true << true; 0063 QTest::newRow("searchResult cmd") << Command::SearchResult << false << true; 0064 QTest::newRow("searchResult resp") << Command::SearchResult << true << true; 0065 QTest::newRow("storeSearch cmd") << Command::StoreSearch << false << true; 0066 QTest::newRow("storeSearch resp") << Command::StoreSearch << true << true; 0067 QTest::newRow("createTag cmd") << Command::CreateTag << false << true; 0068 QTest::newRow("createTag resp") << Command::CreateTag << true << true; 0069 QTest::newRow("deleteTag cmd") << Command::DeleteTag << false << true; 0070 QTest::newRow("deleteTag resp") << Command::DeleteTag << true << true; 0071 QTest::newRow("fetchTags cmd") << Command::FetchTags << false << true; 0072 QTest::newRow("fetchTags resp") << Command::FetchTags << true << true; 0073 QTest::newRow("modifyTag cmd") << Command::ModifyTag << false << true; 0074 QTest::newRow("modifyTag resp") << Command::ModifyTag << true << true; 0075 QTest::newRow("fetchRelations cmd") << Command::FetchRelations << false << true; 0076 QTest::newRow("fetchRelations resp") << Command::FetchRelations << true << true; 0077 QTest::newRow("modifyRelation cmd") << Command::ModifyRelation << false << true; 0078 QTest::newRow("modifyRelation resp") << Command::ModifyRelation << true << true; 0079 QTest::newRow("removeRelations cmd") << Command::RemoveRelations << false << true; 0080 QTest::newRow("removeRelations resp") << Command::RemoveRelations << true << true; 0081 QTest::newRow("selectResource cmd") << Command::SelectResource << false << true; 0082 QTest::newRow("selectResource resp") << Command::SelectResource << true << true; 0083 QTest::newRow("streamPayload cmd") << Command::StreamPayload << false << true; 0084 QTest::newRow("streamPayload resp") << Command::StreamPayload << true << true; 0085 QTest::newRow("itemChangeNotification cmd") << Command::ItemChangeNotification << false << true; 0086 QTest::newRow("itemChangeNotification resp") << Command::ItemChangeNotification << true << false; 0087 QTest::newRow("collectionChangeNotification cmd") << Command::CollectionChangeNotification << false << true; 0088 QTest::newRow("collectionChangeNotification resp") << Command::CollectionChangeNotification << true << false; 0089 QTest::newRow("tagChangeNotification cmd") << Command::TagChangeNotification << false << true; 0090 QTest::newRow("tagChangENotification resp") << Command::TagChangeNotification << true << false; 0091 QTest::newRow("relationChangeNotification cmd") << Command::RelationChangeNotification << false << true; 0092 QTest::newRow("relationChangeNotification resp") << Command::RelationChangeNotification << true << false; 0093 QTest::newRow("_responseBit cmd") << Command::_ResponseBit << false << false; 0094 QTest::newRow("_responseBit resp") << Command::_ResponseBit << true << false; 0095 } 0096 0097 void ProtocolTest::testFactory() 0098 { 0099 QFETCH(Command::Type, type); 0100 QFETCH(bool, response); 0101 QFETCH(bool, success); 0102 0103 CommandPtr result; 0104 if (response) { 0105 result = Factory::response(type); 0106 } else { 0107 result = Factory::command(type); 0108 } 0109 0110 QCOMPARE(result->isValid(), success); 0111 QCOMPARE(result->isResponse(), response); 0112 if (success) { 0113 QCOMPARE(result->type(), type); 0114 } 0115 } 0116 0117 void ProtocolTest::testCommand() 0118 { 0119 // There is no way to construct a valid Command directly 0120 auto cmd = CommandPtr::create(); 0121 QCOMPARE(cmd->type(), Command::Invalid); 0122 QVERIFY(!cmd->isValid()); 0123 QVERIFY(!cmd->isResponse()); 0124 0125 CommandPtr cmdTest = serializeAndDeserialize(cmd); 0126 QCOMPARE(cmdTest->type(), Command::Invalid); 0127 QVERIFY(!cmd->isValid()); 0128 QVERIFY(!cmd->isResponse()); 0129 } 0130 0131 void ProtocolTest::testResponse_data() 0132 { 0133 QTest::addColumn<bool>("isError"); 0134 QTest::addColumn<int>("errorCode"); 0135 QTest::addColumn<QString>("errorString"); 0136 0137 QTest::newRow("no error") << false << 0 << QString(); 0138 QTest::newRow("error") << true << 10 << QStringLiteral("Oh noes, there was an error!"); 0139 } 0140 0141 void ProtocolTest::testResponse() 0142 { 0143 QFETCH(bool, isError); 0144 QFETCH(int, errorCode); 0145 QFETCH(QString, errorString); 0146 0147 Response response; 0148 if (isError) { 0149 response.setError(errorCode, errorString); 0150 } 0151 0152 const auto res = serializeAndDeserialize(ResponsePtr::create(response)); 0153 QCOMPARE(res->type(), Command::Invalid); 0154 QVERIFY(!res->isValid()); 0155 QVERIFY(res->isResponse()); 0156 QCOMPARE(res->isError(), isError); 0157 QCOMPARE(res->errorCode(), errorCode); 0158 QCOMPARE(res->errorMessage(), errorString); 0159 QVERIFY(*res == response); 0160 const bool notEquals = (*res != response); 0161 QVERIFY(!notEquals); 0162 } 0163 0164 void ProtocolTest::testAncestor() 0165 { 0166 Ancestor in; 0167 in.setId(42); 0168 in.setRemoteId(QStringLiteral("remoteId")); 0169 in.setName(QStringLiteral("Col 42")); 0170 in.setAttributes({{"Attr1", "Val 1"}, {"Attr2", "Röndom útéef řetězec"}}); 0171 0172 const Ancestor out = serializeAndDeserialize(in); 0173 QCOMPARE(out.id(), 42); 0174 QCOMPARE(out.remoteId(), QStringLiteral("remoteId")); 0175 QCOMPARE(out.name(), QStringLiteral("Col 42")); 0176 QCOMPARE(out.attributes(), Attributes({{"Attr1", "Val 1"}, {"Attr2", "Röndom útéef řetězec"}})); 0177 QVERIFY(out == in); 0178 const bool notEquals = (out != in); 0179 QVERIFY(!notEquals); 0180 } 0181 0182 void ProtocolTest::testFetchScope_data() 0183 { 0184 QTest::addColumn<bool>("fullPayload"); 0185 QTest::addColumn<QList<QByteArray>>("requestedParts"); 0186 QTest::addColumn<QList<QByteArray>>("expectedParts"); 0187 QTest::addColumn<QList<QByteArray>>("expectedPayloads"); 0188 QTest::newRow("full payload (via flag") << true << QList<QByteArray>{"PLD:HEAD", "ATR:MYATR"} << QList<QByteArray>{"PLD:HEAD", "ATR:MYATR", "PLD:RFC822"} 0189 << QList<QByteArray>{"PLD:HEAD", "PLD:RFC822"}; 0190 QTest::newRow("full payload (via part name") << false << QList<QByteArray>{"PLD:HEAD", "ATR:MYATR", "PLD:RFC822"} 0191 << QList<QByteArray>{"PLD:HEAD", "ATR:MYATR", "PLD:RFC822"} << QList<QByteArray>{"PLD:HEAD", "PLD:RFC822"}; 0192 QTest::newRow("full payload (via both") << true << QList<QByteArray>{"PLD:HEAD", "ATR:MYATR", "PLD:RFC822"} 0193 << QList<QByteArray>{"PLD:HEAD", "ATR:MYATR", "PLD:RFC822"} << QList<QByteArray>{"PLD:HEAD", "PLD:RFC822"}; 0194 QTest::newRow("without full payload") << false << QList<QByteArray>{"PLD:HEAD", "ATR:MYATR"} << QList<QByteArray>{"PLD:HEAD", "ATR:MYATR"} 0195 << QList<QByteArray>{"PLD:HEAD"}; 0196 } 0197 0198 void ProtocolTest::testFetchScope() 0199 { 0200 QFETCH(bool, fullPayload); 0201 QFETCH(QList<QByteArray>, requestedParts); 0202 QFETCH(QList<QByteArray>, expectedParts); 0203 QFETCH(QList<QByteArray>, expectedPayloads); 0204 0205 ItemFetchScope in; 0206 for (unsigned i = ItemFetchScope::CacheOnly; i <= ItemFetchScope::VirtReferences; i = i << 1) { 0207 QVERIFY(!in.fetch(static_cast<ItemFetchScope::FetchFlag>(i))); 0208 } 0209 QVERIFY(in.fetch(ItemFetchScope::None)); 0210 0211 in.setRequestedParts(requestedParts); 0212 in.setChangedSince(QDateTime(QDate(2015, 8, 10), QTime(23, 52, 20), QTimeZone::UTC)); 0213 in.setAncestorDepth(ItemFetchScope::AllAncestors); 0214 in.setFetch(ItemFetchScope::CacheOnly); 0215 in.setFetch(ItemFetchScope::CheckCachedPayloadPartsOnly); 0216 in.setFetch(ItemFetchScope::FullPayload, fullPayload); 0217 in.setFetch(ItemFetchScope::AllAttributes); 0218 in.setFetch(ItemFetchScope::Size); 0219 in.setFetch(ItemFetchScope::MTime); 0220 in.setFetch(ItemFetchScope::RemoteRevision); 0221 in.setFetch(ItemFetchScope::IgnoreErrors); 0222 in.setFetch(ItemFetchScope::Flags); 0223 in.setFetch(ItemFetchScope::RemoteID); 0224 in.setFetch(ItemFetchScope::GID); 0225 in.setFetch(ItemFetchScope::Tags); 0226 in.setFetch(ItemFetchScope::Relations); 0227 in.setFetch(ItemFetchScope::VirtReferences); 0228 0229 const ItemFetchScope out = serializeAndDeserialize(in); 0230 QCOMPARE(out.requestedParts(), expectedParts); 0231 QCOMPARE(out.requestedPayloads(), expectedPayloads); 0232 QCOMPARE(out.changedSince(), QDateTime(QDate(2015, 8, 10), QTime(23, 52, 20), QTimeZone::UTC)); 0233 QCOMPARE(out.ancestorDepth(), ItemFetchScope::AllAncestors); 0234 QCOMPARE(out.fetch(ItemFetchScope::None), false); 0235 QCOMPARE(out.cacheOnly(), true); 0236 QCOMPARE(out.checkCachedPayloadPartsOnly(), true); 0237 QCOMPARE(out.fullPayload(), fullPayload); 0238 QCOMPARE(out.allAttributes(), true); 0239 QCOMPARE(out.fetchSize(), true); 0240 QCOMPARE(out.fetchMTime(), true); 0241 QCOMPARE(out.fetchRemoteRevision(), true); 0242 QCOMPARE(out.ignoreErrors(), true); 0243 QCOMPARE(out.fetchFlags(), true); 0244 QCOMPARE(out.fetchRemoteId(), true); 0245 QCOMPARE(out.fetchGID(), true); 0246 QCOMPARE(out.fetchRelations(), true); 0247 QCOMPARE(out.fetchVirtualReferences(), true); 0248 } 0249 0250 void ProtocolTest::testScopeContext_data() 0251 { 0252 QTest::addColumn<qint64>("colId"); 0253 QTest::addColumn<QString>("colRid"); 0254 QTest::addColumn<qint64>("tagId"); 0255 QTest::addColumn<QString>("tagRid"); 0256 0257 QTest::newRow("collection - id") << 42LL << QString() << 0LL << QString(); 0258 QTest::newRow("collection - rid") << 0LL << QStringLiteral("rid") << 0LL << QString(); 0259 QTest::newRow("collection - both") << 42LL << QStringLiteral("rid") << 0LL << QString(); 0260 0261 QTest::newRow("tag - id") << 0LL << QString() << 42LL << QString(); 0262 QTest::newRow("tag - rid") << 0LL << QString() << 0LL << QStringLiteral("rid"); 0263 QTest::newRow("tag - both") << 0LL << QString() << 42LL << QStringLiteral("rid"); 0264 0265 QTest::newRow("both - id") << 42LL << QString() << 10LL << QString(); 0266 QTest::newRow("both - rid") << 0LL << QStringLiteral("colRid") << 0LL << QStringLiteral("tagRid"); 0267 QTest::newRow("col - id, tag - rid") << 42LL << QString() << 0LL << QStringLiteral("tagRid"); 0268 QTest::newRow("col - rid, tag - id") << 0LL << QStringLiteral("colRid") << 42LL << QString(); 0269 QTest::newRow("both - both") << 42LL << QStringLiteral("colRid") << 10LL << QStringLiteral("tagRid"); 0270 } 0271 0272 void ProtocolTest::testScopeContext() 0273 { 0274 QFETCH(qint64, colId); 0275 QFETCH(QString, colRid); 0276 QFETCH(qint64, tagId); 0277 QFETCH(QString, tagRid); 0278 0279 const bool hasColId = colId > 0; 0280 const bool hasColRid = !colRid.isEmpty(); 0281 const bool hasTagId = tagId > 0; 0282 const bool hasTagRid = !tagRid.isEmpty(); 0283 0284 ScopeContext in; 0285 QVERIFY(in.isEmpty()); 0286 if (hasColId) { 0287 in.setContext(ScopeContext::Collection, colId); 0288 } 0289 if (hasColRid) { 0290 in.setContext(ScopeContext::Collection, colRid); 0291 } 0292 if (hasTagId) { 0293 in.setContext(ScopeContext::Tag, tagId); 0294 } 0295 if (hasTagRid) { 0296 in.setContext(ScopeContext::Tag, tagRid); 0297 } 0298 0299 QCOMPARE(in.hasContextId(ScopeContext::Any), false); 0300 QCOMPARE(in.hasContextRID(ScopeContext::Any), false); 0301 QEXPECT_FAIL("collection - both", "Cannot set both ID and RID context", Continue); 0302 QEXPECT_FAIL("both - both", "Cannot set both ID and RID context", Continue); 0303 QCOMPARE(in.hasContextId(ScopeContext::Collection), hasColId); 0304 QCOMPARE(in.hasContextRID(ScopeContext::Collection), hasColRid); 0305 QEXPECT_FAIL("both - both", "Cannot set both ID and RID context", Continue); 0306 QEXPECT_FAIL("tag - both", "Cannot set both ID and RID context", Continue); 0307 QCOMPARE(in.hasContextId(ScopeContext::Tag), hasTagId); 0308 QCOMPARE(in.hasContextRID(ScopeContext::Tag), hasTagRid); 0309 QVERIFY(!in.isEmpty()); 0310 0311 ScopeContext out = serializeAndDeserialize(in); 0312 QCOMPARE(out.isEmpty(), false); 0313 QEXPECT_FAIL("collection - both", "Cannot set both ID and RID context", Continue); 0314 QEXPECT_FAIL("both - both", "Cannot set both ID and RID context", Continue); 0315 QCOMPARE(out.hasContextId(ScopeContext::Collection), hasColId); 0316 QEXPECT_FAIL("collection - both", "Cannot set both ID and RID context", Continue); 0317 QEXPECT_FAIL("both - both", "Cannot set both ID and RID context", Continue); 0318 QCOMPARE(out.contextId(ScopeContext::Collection), colId); 0319 QCOMPARE(out.hasContextRID(ScopeContext::Collection), hasColRid); 0320 QCOMPARE(out.contextRID(ScopeContext::Collection), colRid); 0321 QEXPECT_FAIL("tag - both", "Cannot set both ID and RID context", Continue); 0322 QEXPECT_FAIL("both - both", "Cannot set both ID and RID context", Continue); 0323 QCOMPARE(out.hasContextId(ScopeContext::Tag), hasTagId); 0324 QEXPECT_FAIL("tag - both", "Cannot set both ID and RID context", Continue); 0325 QEXPECT_FAIL("both - both", "Cannot set both ID and RID context", Continue); 0326 QCOMPARE(out.contextId(ScopeContext::Tag), tagId); 0327 QCOMPARE(out.hasContextRID(ScopeContext::Tag), hasTagRid); 0328 QCOMPARE(out.contextRID(ScopeContext::Tag), tagRid); 0329 QCOMPARE(out, in); 0330 const bool notEquals = (out != in); 0331 QVERIFY(!notEquals); 0332 0333 // Clearing "any" should not do anything 0334 out.clearContext(ScopeContext::Any); 0335 QEXPECT_FAIL("collection - both", "Cannot set both ID and RID context", Continue); 0336 QEXPECT_FAIL("both - both", "Cannot set both ID and RID context", Continue); 0337 QCOMPARE(out.hasContextId(ScopeContext::Collection), hasColId); 0338 QEXPECT_FAIL("collection - both", "Cannot set both ID and RID context", Continue); 0339 QEXPECT_FAIL("both - both", "Cannot set both ID and RID context", Continue); 0340 QCOMPARE(out.contextId(ScopeContext::Collection), colId); 0341 QCOMPARE(out.hasContextRID(ScopeContext::Collection), hasColRid); 0342 QCOMPARE(out.contextRID(ScopeContext::Collection), colRid); 0343 QEXPECT_FAIL("tag - both", "Cannot set both ID and RID context", Continue); 0344 QEXPECT_FAIL("both - both", "Cannot set both ID and RID context", Continue); 0345 QCOMPARE(out.hasContextId(ScopeContext::Tag), hasTagId); 0346 QEXPECT_FAIL("tag - both", "Cannot set both ID and RID context", Continue); 0347 QEXPECT_FAIL("both - both", "Cannot set both ID and RID context", Continue); 0348 QCOMPARE(out.contextId(ScopeContext::Tag), tagId); 0349 QCOMPARE(out.hasContextRID(ScopeContext::Tag), hasTagRid); 0350 QCOMPARE(out.contextRID(ScopeContext::Tag), tagRid); 0351 0352 if (hasColId || hasColRid) { 0353 ScopeContext clear = out; 0354 clear.clearContext(ScopeContext::Collection); 0355 QCOMPARE(clear.hasContextId(ScopeContext::Collection), false); 0356 QCOMPARE(clear.hasContextRID(ScopeContext::Collection), false); 0357 QEXPECT_FAIL("both - both", "Cannot set both ID and RID context", Continue); 0358 QCOMPARE(clear.hasContextId(ScopeContext::Tag), hasTagId); 0359 QCOMPARE(clear.hasContextRID(ScopeContext::Tag), hasTagRid); 0360 } 0361 if (hasTagId || hasTagRid) { 0362 ScopeContext clear = out; 0363 clear.clearContext(ScopeContext::Tag); 0364 QEXPECT_FAIL("both - both", "Cannot set both ID and RID context", Continue); 0365 QCOMPARE(clear.hasContextId(ScopeContext::Collection), hasColId); 0366 QCOMPARE(clear.hasContextRID(ScopeContext::Collection), hasColRid); 0367 QCOMPARE(clear.hasContextId(ScopeContext::Tag), false); 0368 QCOMPARE(clear.hasContextRID(ScopeContext::Tag), false); 0369 } 0370 0371 out.clearContext(ScopeContext::Collection); 0372 out.clearContext(ScopeContext::Tag); 0373 QVERIFY(out.isEmpty()); 0374 } 0375 0376 void ProtocolTest::testPartMetaData() 0377 { 0378 PartMetaData in; 0379 in.setName("PLD:HEAD"); 0380 in.setSize(42); 0381 in.setVersion(1); 0382 in.setStorageType(PartMetaData::External); 0383 0384 const PartMetaData out = serializeAndDeserialize(in); 0385 QCOMPARE(out.name(), QByteArray("PLD:HEAD")); 0386 QCOMPARE(out.size(), 42); 0387 QCOMPARE(out.version(), 1); 0388 QCOMPARE(out.storageType(), PartMetaData::External); 0389 QCOMPARE(out, in); 0390 const bool notEquals = (in != out); 0391 QVERIFY(!notEquals); 0392 } 0393 0394 void ProtocolTest::testCachePolicy() 0395 { 0396 CachePolicy in; 0397 in.setInherit(true); 0398 in.setCheckInterval(42); 0399 in.setCacheTimeout(10); 0400 in.setSyncOnDemand(true); 0401 in.setLocalParts({QStringLiteral("PLD:HEAD"), QStringLiteral("PLD:ENVELOPE")}); 0402 0403 const CachePolicy out = serializeAndDeserialize(in); 0404 QCOMPARE(out.inherit(), true); 0405 QCOMPARE(out.checkInterval(), 42); 0406 QCOMPARE(out.cacheTimeout(), 10); 0407 QCOMPARE(out.syncOnDemand(), true); 0408 QCOMPARE(out.localParts(), QStringList() << QStringLiteral("PLD:HEAD") << QStringLiteral("PLD:ENVELOPE")); 0409 QCOMPARE(out, in); 0410 const bool notEquals = (out != in); 0411 QVERIFY(!notEquals); 0412 } 0413 0414 void ProtocolTest::testHelloResponse() 0415 { 0416 HelloResponse in; 0417 QVERIFY(in.isResponse()); 0418 QVERIFY(in.isValid()); 0419 QVERIFY(!in.isError()); 0420 in.setServerName(QStringLiteral("AkonadiTest")); 0421 in.setMessage(QStringLiteral("Oh, hello there!")); 0422 in.setProtocolVersion(42); 0423 in.setError(10, QStringLiteral("Ooops")); 0424 0425 const auto out = serializeAndDeserialize(HelloResponsePtr::create(in)); 0426 QVERIFY(out->isValid()); 0427 QVERIFY(out->isResponse()); 0428 QVERIFY(out->isError()); 0429 QCOMPARE(out->errorCode(), 10); 0430 QCOMPARE(out->errorMessage(), QStringLiteral("Ooops")); 0431 QCOMPARE(out->serverName(), QStringLiteral("AkonadiTest")); 0432 QCOMPARE(out->message(), QStringLiteral("Oh, hello there!")); 0433 QCOMPARE(out->protocolVersion(), 42); 0434 QCOMPARE(*out, in); 0435 const bool notEquals = (*out != in); 0436 QVERIFY(!notEquals); 0437 } 0438 0439 void ProtocolTest::testLoginCommand() 0440 { 0441 LoginCommand in; 0442 QVERIFY(!in.isResponse()); 0443 QVERIFY(in.isValid()); 0444 in.setSessionId("MySession-123-notifications"); 0445 0446 const auto out = serializeAndDeserialize(LoginCommandPtr::create(in)); 0447 QVERIFY(out->isValid()); 0448 QVERIFY(!out->isResponse()); 0449 QCOMPARE(out->sessionId(), QByteArray("MySession-123-notifications")); 0450 QCOMPARE(*out, in); 0451 const bool notEquals = (*out != in); 0452 QVERIFY(!notEquals); 0453 } 0454 0455 void ProtocolTest::testLoginResponse() 0456 { 0457 LoginResponse in; 0458 QVERIFY(in.isResponse()); 0459 QVERIFY(in.isValid()); 0460 QVERIFY(!in.isError()); 0461 in.setError(42, QStringLiteral("Ooops")); 0462 0463 const auto out = serializeAndDeserialize(LoginResponsePtr::create(in)); 0464 QVERIFY(out->isValid()); 0465 QVERIFY(out->isResponse()); 0466 QVERIFY(out->isError()); 0467 QCOMPARE(out->errorCode(), 42); 0468 QCOMPARE(out->errorMessage(), QStringLiteral("Ooops")); 0469 QCOMPARE(*out, in); 0470 const bool notEquals = (*out != in); 0471 QVERIFY(!notEquals); 0472 } 0473 0474 void ProtocolTest::testLogoutCommand() 0475 { 0476 LogoutCommand in; 0477 QVERIFY(!in.isResponse()); 0478 QVERIFY(in.isValid()); 0479 0480 const auto out = serializeAndDeserialize(LogoutCommandPtr::create(in)); 0481 QVERIFY(!out->isResponse()); 0482 QVERIFY(out->isValid()); 0483 QCOMPARE(*out, in); 0484 const bool notEquals = (*out != in); 0485 QVERIFY(!notEquals); 0486 } 0487 0488 void ProtocolTest::testLogoutResponse() 0489 { 0490 LogoutResponse in; 0491 QVERIFY(in.isResponse()); 0492 QVERIFY(in.isValid()); 0493 QVERIFY(!in.isError()); 0494 in.setError(42, QStringLiteral("Ooops")); 0495 0496 const auto out = serializeAndDeserialize(LogoutResponsePtr::create(in)); 0497 QVERIFY(out->isValid()); 0498 QVERIFY(out->isResponse()); 0499 QVERIFY(out->isError()); 0500 QCOMPARE(out->errorCode(), 42); 0501 QCOMPARE(out->errorMessage(), QStringLiteral("Ooops")); 0502 QCOMPARE(*out, in); 0503 const bool notEquals = (*out != in); 0504 QVERIFY(!notEquals); 0505 } 0506 0507 void ProtocolTest::testTransactionCommand() 0508 { 0509 TransactionCommand in; 0510 QVERIFY(!in.isResponse()); 0511 QVERIFY(in.isValid()); 0512 in.setMode(TransactionCommand::Begin); 0513 0514 const auto out = serializeAndDeserialize(TransactionCommandPtr::create(in)); 0515 QVERIFY(out->isValid()); 0516 QVERIFY(!out->isResponse()); 0517 QCOMPARE(out->mode(), TransactionCommand::Begin); 0518 QCOMPARE(*out, in); 0519 const bool notEquals = (*out != in); 0520 QVERIFY(!notEquals); 0521 } 0522 0523 void ProtocolTest::testTransactionResponse() 0524 { 0525 TransactionResponse in; 0526 QVERIFY(in.isResponse()); 0527 QVERIFY(in.isValid()); 0528 QVERIFY(!in.isError()); 0529 in.setError(42, QStringLiteral("Ooops")); 0530 0531 const auto out = serializeAndDeserialize(TransactionResponsePtr::create(in)); 0532 QVERIFY(out->isValid()); 0533 QVERIFY(out->isResponse()); 0534 QVERIFY(out->isError()); 0535 QCOMPARE(out->errorCode(), 42); 0536 QCOMPARE(out->errorMessage(), QStringLiteral("Ooops")); 0537 QCOMPARE(*out, in); 0538 const bool notEquals = (*out != in); 0539 QVERIFY(!notEquals); 0540 } 0541 0542 void ProtocolTest::testCreateItemCommand() 0543 { 0544 Scope addedTags(QList<qint64>{3, 4}); 0545 Scope removedTags(QList<qint64>{5, 6}); 0546 Attributes attrs{{"ATTR1", "MyAttr"}, {"ATTR2", "Můj chlupaťoučký kůň"}}; 0547 QSet<QByteArray> parts{"PLD:HEAD", "PLD:ENVELOPE"}; 0548 0549 CreateItemCommand in; 0550 QVERIFY(!in.isResponse()); 0551 QVERIFY(in.isValid()); 0552 QCOMPARE(in.mergeModes(), CreateItemCommand::None); 0553 in.setMergeModes(CreateItemCommand::MergeModes(CreateItemCommand::GID | CreateItemCommand::RemoteID)); 0554 in.setCollection(Scope(1)); 0555 in.setItemSize(100); 0556 in.setMimeType(QStringLiteral("text/directory")); 0557 in.setGid(QStringLiteral("GID")); 0558 in.setRemoteId(QStringLiteral("RID")); 0559 in.setRemoteRevision(QStringLiteral("RREV")); 0560 in.setDateTime(QDateTime(QDate(2015, 8, 11), QTime(14, 32, 21), QTimeZone::UTC)); 0561 in.setFlags({"\\SEEN", "FLAG"}); 0562 in.setFlagsOverwritten(true); 0563 in.setAddedFlags({"FLAG2"}); 0564 in.setRemovedFlags({"FLAG3"}); 0565 in.setTags(Scope(2)); 0566 in.setAddedTags(addedTags); 0567 in.setRemovedTags(removedTags); 0568 in.setAttributes(attrs); 0569 in.setParts(parts); 0570 0571 const auto out = serializeAndDeserialize(CreateItemCommandPtr::create(in)); 0572 QVERIFY(out->isValid()); 0573 QVERIFY(!out->isResponse()); 0574 QCOMPARE(out->mergeModes(), CreateItemCommand::GID | CreateItemCommand::RemoteID); 0575 QCOMPARE(out->collection(), Scope(1)); 0576 QCOMPARE(out->itemSize(), 100); 0577 QCOMPARE(out->mimeType(), QStringLiteral("text/directory")); 0578 QCOMPARE(out->gid(), QStringLiteral("GID")); 0579 QCOMPARE(out->remoteId(), QStringLiteral("RID")); 0580 QCOMPARE(out->remoteRevision(), QStringLiteral("RREV")); 0581 QCOMPARE(out->dateTime(), QDateTime(QDate(2015, 8, 11), QTime(14, 32, 21), QTimeZone::UTC)); 0582 QCOMPARE(out->flags(), 0583 QSet<QByteArray>() << "\\SEEN" 0584 << "FLAG"); 0585 QCOMPARE(out->flagsOverwritten(), true); 0586 QCOMPARE(out->addedFlags(), QSet<QByteArray>{"FLAG2"}); 0587 QCOMPARE(out->removedFlags(), QSet<QByteArray>{"FLAG3"}); 0588 QCOMPARE(out->tags(), Scope(2)); 0589 QCOMPARE(out->addedTags(), addedTags); 0590 QCOMPARE(out->removedTags(), removedTags); 0591 QCOMPARE(out->attributes(), attrs); 0592 QCOMPARE(out->parts(), parts); 0593 QCOMPARE(*out, in); 0594 const bool notEquals = (*out != in); 0595 QVERIFY(!notEquals); 0596 } 0597 0598 void ProtocolTest::testCreateItemResponse() 0599 { 0600 CreateItemResponse in; 0601 QVERIFY(in.isResponse()); 0602 QVERIFY(in.isValid()); 0603 QVERIFY(!in.isError()); 0604 in.setError(42, QStringLiteral("Ooops")); 0605 0606 const auto out = serializeAndDeserialize(CreateItemResponsePtr::create(in)); 0607 QVERIFY(out->isValid()); 0608 QVERIFY(out->isResponse()); 0609 QVERIFY(out->isError()); 0610 QCOMPARE(out->errorCode(), 42); 0611 QCOMPARE(out->errorMessage(), QStringLiteral("Ooops")); 0612 QCOMPARE(*out, in); 0613 const bool notEquals = (*out != in); 0614 QVERIFY(!notEquals); 0615 } 0616 0617 void ProtocolTest::testCopyItemsCommand() 0618 { 0619 const Scope items(QList<qint64>{1, 2, 3, 10}); 0620 0621 CopyItemsCommand in; 0622 QVERIFY(in.isValid()); 0623 QVERIFY(!in.isResponse()); 0624 in.setItems(items); 0625 in.setDestination(Scope(42)); 0626 0627 const auto out = serializeAndDeserialize(CopyItemsCommandPtr::create(in)); 0628 QVERIFY(out->isValid()); 0629 QVERIFY(!out->isResponse()); 0630 QCOMPARE(out->items(), items); 0631 QCOMPARE(out->destination(), Scope(42)); 0632 QCOMPARE(*out, in); 0633 const bool notEquals = (*out != in); 0634 QVERIFY(!notEquals); 0635 } 0636 0637 void ProtocolTest::testCopyItemsResponse() 0638 { 0639 CopyItemsResponse in; 0640 QVERIFY(in.isResponse()); 0641 QVERIFY(in.isValid()); 0642 QVERIFY(!in.isError()); 0643 in.setError(42, QStringLiteral("Ooops")); 0644 0645 const auto out = serializeAndDeserialize(CopyItemsResponsePtr::create(in)); 0646 QVERIFY(out->isValid()); 0647 QVERIFY(out->isResponse()); 0648 QVERIFY(out->isError()); 0649 QCOMPARE(out->errorCode(), 42); 0650 QCOMPARE(out->errorMessage(), QStringLiteral("Ooops")); 0651 QCOMPARE(*out, in); 0652 const bool notEquals = (*out != in); 0653 QVERIFY(!notEquals); 0654 } 0655 0656 QTEST_MAIN(ProtocolTest) 0657 0658 #include "moc_protocoltest.cpp"