File: protocoltest.cpp

package info (click to toggle)
akonadi 4%3A18.08.3-7~deb10u1
  • links: PTS, VCS
  • area: main
  • in suites: buster
  • size: 13,536 kB
  • sloc: cpp: 98,501; xml: 2,560; sh: 40; makefile: 24
file content (707 lines) | stat: -rw-r--r-- 29,780 bytes parent folder | download
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
307
308
309
310
311
312
313
314
315
316
317
318
319
320
321
322
323
324
325
326
327
328
329
330
331
332
333
334
335
336
337
338
339
340
341
342
343
344
345
346
347
348
349
350
351
352
353
354
355
356
357
358
359
360
361
362
363
364
365
366
367
368
369
370
371
372
373
374
375
376
377
378
379
380
381
382
383
384
385
386
387
388
389
390
391
392
393
394
395
396
397
398
399
400
401
402
403
404
405
406
407
408
409
410
411
412
413
414
415
416
417
418
419
420
421
422
423
424
425
426
427
428
429
430
431
432
433
434
435
436
437
438
439
440
441
442
443
444
445
446
447
448
449
450
451
452
453
454
455
456
457
458
459
460
461
462
463
464
465
466
467
468
469
470
471
472
473
474
475
476
477
478
479
480
481
482
483
484
485
486
487
488
489
490
491
492
493
494
495
496
497
498
499
500
501
502
503
504
505
506
507
508
509
510
511
512
513
514
515
516
517
518
519
520
521
522
523
524
525
526
527
528
529
530
531
532
533
534
535
536
537
538
539
540
541
542
543
544
545
546
547
548
549
550
551
552
553
554
555
556
557
558
559
560
561
562
563
564
565
566
567
568
569
570
571
572
573
574
575
576
577
578
579
580
581
582
583
584
585
586
587
588
589
590
591
592
593
594
595
596
597
598
599
600
601
602
603
604
605
606
607
608
609
610
611
612
613
614
615
616
617
618
619
620
621
622
623
624
625
626
627
628
629
630
631
632
633
634
635
636
637
638
639
640
641
642
643
644
645
646
647
648
649
650
651
652
653
654
655
656
657
658
659
660
661
662
663
664
665
666
667
668
669
670
671
672
673
674
675
676
677
678
679
680
681
682
683
684
685
686
687
688
689
690
691
692
693
694
695
696
697
698
699
700
701
702
703
704
705
706
707
/*
 * Copyright (C) 2015  Daniel Vrátil <dvratil@redhat.com>
 *
 * This library is free software; you can redistribute it and/or
 * modify it under the terms of the GNU Lesser General Public
 * License as published by the Free Software Foundation; either
 * version 2.1 of the License, or (at your option) any later version.
 *
 * This library is distributed in the hope that it will be useful,
 * but WITHOUT ANY WARRANTY; without even the implied warranty of
 * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the GNU
 * Lesser General Public License for more details.
 *
 * You should have received a copy of the GNU Lesser General Public
 * License along with this library; if not, write to the Free Software
 * Foundation, Inc., 51 Franklin Street, Fifth Floor, Boston, MA  02110-1301  USA
 *
 */

#include "protocoltest.h"

#include "private/scope_p.h"

#include <QTest>

using namespace Akonadi;
using namespace Akonadi::Protocol;

void ProtocolTest::testProtocolVersion()
{
    // So, this test started failing because you bumped protocol version in
    // protocol.cpp. That means that you most probably changed something
    // in the protocol. Before you just bump the number here to make the test
    // pass, please please pretty please make sure to extend the respective
    // test somewhere below to cover the change you've made. Protocol is the
    // most critical part of Akonadi and we can't afford not having it tested
    // properly.
    //
    // If it wasn't you who broke it, please go find that person who was too
    // lazy to extend the test case and beat them with a stick. -- Dan

    QCOMPARE(Akonadi::Protocol::version(), 59);
}

void ProtocolTest::testFactory_data()
{
    QTest::addColumn<Command::Type>("type");
    QTest::addColumn<bool>("response");
    QTest::addColumn<bool>("success");

    QTest::newRow("invalid cmd") << Command::Invalid << false << false;
    QTest::newRow("invalid resp") << Command::Invalid << true << false;
    QTest::newRow("hello cmd") << Command::Hello << false << false;
    QTest::newRow("hello resp") << Command::Hello << true << true;
    QTest::newRow("login cmd") << Command::Login << false << true;
    QTest::newRow("login resp") << Command::Login << true << true;
    QTest::newRow("logout cmd") << Command::Logout << false << true;
    QTest::newRow("logout resp") << Command::Logout << true << true;
    QTest::newRow("transaction cmd") << Command::Transaction << false << true;
    QTest::newRow("transaction resp") << Command::Transaction << true << true;
    QTest::newRow("createItem cmd") << Command::CreateItem << false << true;
    QTest::newRow("createItem resp") << Command::CreateItem << true << true;
    QTest::newRow("copyItems cmd") << Command::CopyItems << false << true;
    QTest::newRow("copyItems resp") << Command::CopyItems << true << true;
    QTest::newRow("deleteItems cmd") << Command::DeleteItems << false << true;
    QTest::newRow("deleteItems resp") << Command::DeleteItems << true << true;
    QTest::newRow("fetchItems cmd") << Command::FetchItems << false << true;
    QTest::newRow("fetchItems resp") << Command::FetchItems << true << true;
    QTest::newRow("linkItems cmd") << Command::LinkItems << false << true;
    QTest::newRow("linkItems resp") << Command::LinkItems << true << true;
    QTest::newRow("modifyItems cmd") << Command::ModifyItems << false << true;
    QTest::newRow("modifyItems resp") << Command::ModifyItems << true << true;
    QTest::newRow("moveItems cmd") << Command::MoveItems << false << true;
    QTest::newRow("moveItems resp") << Command::MoveItems << true << true;
    QTest::newRow("createCollection cmd") << Command::CreateCollection << false << true;
    QTest::newRow("createCollection resp") << Command::CreateCollection << true << true;
    QTest::newRow("copyCollection cmd") << Command::CopyCollection << false << true;
    QTest::newRow("copyCollection resp") << Command::CopyCollection << true << true;
    QTest::newRow("deleteCollection cmd") << Command::DeleteCollection << false << true;
    QTest::newRow("deleteCollection resp") << Command::DeleteCollection << true << true;
    QTest::newRow("fetchCollections cmd") << Command::FetchCollections << false << true;
    QTest::newRow("fetchCollections resp") << Command::FetchCollections << true << true;
    QTest::newRow("fetchCollectionStats cmd") << Command::FetchCollectionStats << false << true;
    QTest::newRow("fetchCollectionStats resp") << Command::FetchCollectionStats << false << true;
    QTest::newRow("modifyCollection cmd") << Command::ModifyCollection << false << true;
    QTest::newRow("modifyCollection resp") << Command::ModifyCollection << true << true;
    QTest::newRow("moveCollection cmd") << Command::MoveCollection << false << true;
    QTest::newRow("moveCollection resp") << Command::MoveCollection << true << true;
    QTest::newRow("search cmd") << Command::Search << false << true;
    QTest::newRow("search resp") << Command::Search << true << true;
    QTest::newRow("searchResult cmd") << Command::SearchResult << false << true;
    QTest::newRow("searchResult resp") << Command::SearchResult << true << true;
    QTest::newRow("storeSearch cmd") << Command::StoreSearch << false << true;
    QTest::newRow("storeSearch resp") << Command::StoreSearch << true << true;
    QTest::newRow("createTag cmd") << Command::CreateTag << false << true;
    QTest::newRow("createTag resp") << Command::CreateTag << true << true;
    QTest::newRow("deleteTag cmd") << Command::DeleteTag << false << true;
    QTest::newRow("deleteTag resp") << Command::DeleteTag << true << true;
    QTest::newRow("fetchTags cmd") << Command::FetchTags << false << true;
    QTest::newRow("fetchTags resp") << Command::FetchTags << true << true;
    QTest::newRow("modifyTag cmd") << Command::ModifyTag << false << true;
    QTest::newRow("modifyTag resp") << Command::ModifyTag << true << true;
    QTest::newRow("fetchRelations cmd") << Command::FetchRelations << false << true;
    QTest::newRow("fetchRelations resp") << Command::FetchRelations << true << true;
    QTest::newRow("modifyRelation cmd") << Command::ModifyRelation << false << true;
    QTest::newRow("modifyRelation resp") << Command::ModifyRelation << true << true;
    QTest::newRow("removeRelations cmd") << Command::RemoveRelations << false << true;
    QTest::newRow("removeRelations resp") << Command::RemoveRelations << true << true;
    QTest::newRow("selectResource cmd") << Command::SelectResource << false << true;
    QTest::newRow("selectResource resp") << Command::SelectResource << true << true;
    QTest::newRow("streamPayload cmd") << Command::StreamPayload << false << true;
    QTest::newRow("streamPayload resp") << Command::StreamPayload << true << true;
    QTest::newRow("itemChangeNotification cmd") << Command::ItemChangeNotification << false << true;
    QTest::newRow("itemChangeNotification resp") << Command::ItemChangeNotification << true << false;
    QTest::newRow("collectionChangeNotification cmd") << Command::CollectionChangeNotification << false << true;
    QTest::newRow("collectionChangeNotification resp") << Command::CollectionChangeNotification << true << false;
    QTest::newRow("tagChangeNotification cmd") << Command::TagChangeNotification << false << true;
    QTest::newRow("tagChangENotification resp") << Command::TagChangeNotification << true << false;
    QTest::newRow("relationChangeNotification cmd") << Command::RelationChangeNotification << false << true;
    QTest::newRow("relationChangeNotification resp") << Command::RelationChangeNotification << true << false;
    QTest::newRow("_responseBit cmd") << Command::_ResponseBit << false << false;
    QTest::newRow("_responseBit resp") << Command::_ResponseBit << true << false;
}

void ProtocolTest::testFactory()
{
    QFETCH(Command::Type, type);
    QFETCH(bool, response);
    QFETCH(bool, success);

    CommandPtr result;
    if (response) {
        result = Factory::response(type);
    } else {
        result = Factory::command(type);
    }

    QCOMPARE(result->isValid(), success);
    QCOMPARE(result->isResponse(), response);
    if (success) {
        QCOMPARE(result->type(), type);
    }
}



void ProtocolTest::testCommand()
{
    // There is no way to construct a valid Command directly
    auto cmd = CommandPtr::create();
    QCOMPARE(cmd->type(), Command::Invalid);
    QVERIFY(!cmd->isValid());
    QVERIFY(!cmd->isResponse());

    CommandPtr cmdTest = serializeAndDeserialize(cmd);
    QCOMPARE(cmdTest->type(), Command::Invalid);
    QVERIFY(!cmd->isValid());
    QVERIFY(!cmd->isResponse());
}

void ProtocolTest::testResponse_data()
{
    QTest::addColumn<bool>("isError");
    QTest::addColumn<int>("errorCode");
    QTest::addColumn<QString>("errorString");

    QTest::newRow("no error") << false << 0 << QString();
    QTest::newRow("error") << true << 10 << QStringLiteral("Oh noes, there was an error!");
}

void ProtocolTest::testResponse()
{
    QFETCH(bool, isError);
    QFETCH(int, errorCode);
    QFETCH(QString, errorString);

    Response response;
    if (isError) {
        response.setError(errorCode, errorString);
    }

    const auto res = serializeAndDeserialize(ResponsePtr::create(response));
    QCOMPARE(res->type(), Command::Invalid);
    QVERIFY(!res->isValid());
    QVERIFY(res->isResponse());
    QCOMPARE(res->isError(), isError);
    QCOMPARE(res->errorCode(), errorCode);
    QCOMPARE(res->errorMessage(), errorString);
    QVERIFY(*res == response);
    const bool notEquals = (*res != response);
    QVERIFY(!notEquals);
}

void ProtocolTest::testAncestor()
{
    Ancestor in;
    in.setId(42);
    in.setRemoteId(QStringLiteral("remoteId"));
    in.setName(QStringLiteral("Col 42"));
    in.setAttributes({{ "Attr1", "Val 1" }, { "Attr2", "Röndom útéef řetězec" }});

    const Ancestor out = serializeAndDeserialize(in);
    QCOMPARE(out.id(), 42);
    QCOMPARE(out.remoteId(), QStringLiteral("remoteId"));
    QCOMPARE(out.name(), QStringLiteral("Col 42"));
    QCOMPARE(out.attributes(), Attributes({{ "Attr1", "Val 1" }, { "Attr2", "Röndom útéef řetězec" }}));
    QVERIFY(out == in);
    const bool notEquals = (out != in);
    QVERIFY(!notEquals);
}

void ProtocolTest::testFetchScope_data()
{
    QTest::addColumn<bool>("fullPayload");
    QTest::addColumn<QVector<QByteArray>>("requestedParts");
    QTest::addColumn<QVector<QByteArray>>("expectedParts");
    QTest::addColumn<QVector<QByteArray>>("expectedPayloads");
    QTest::newRow("full payload (via flag") << true
                                << QVector<QByteArray>{ "PLD:HEAD", "ATR:MYATR" }
                                << QVector<QByteArray>{ "PLD:HEAD", "ATR:MYATR", "PLD:RFC822" }
                                << QVector<QByteArray>{ "PLD:HEAD", "PLD:RFC822" };
    QTest::newRow("full payload (via part name") << false
                                << QVector<QByteArray>{ "PLD:HEAD", "ATR:MYATR", "PLD:RFC822" }
                                << QVector<QByteArray>{ "PLD:HEAD", "ATR:MYATR", "PLD:RFC822" }
                                << QVector<QByteArray>{ "PLD:HEAD", "PLD:RFC822" };
    QTest::newRow("full payload (via both") << true
                                << QVector<QByteArray>{ "PLD:HEAD", "ATR:MYATR", "PLD:RFC822" }
                                << QVector<QByteArray>{ "PLD:HEAD", "ATR:MYATR", "PLD:RFC822" }
                                << QVector<QByteArray>{ "PLD:HEAD", "PLD:RFC822" };
    QTest::newRow("without full payload") << false
                                << QVector<QByteArray>{ "PLD:HEAD", "ATR:MYATR" }
                                << QVector<QByteArray>{ "PLD:HEAD", "ATR:MYATR" }
                                << QVector<QByteArray>{ "PLD:HEAD" };
}


void ProtocolTest::testFetchScope()
{
    QFETCH(bool, fullPayload);
    QFETCH(QVector<QByteArray>, requestedParts);
    QFETCH(QVector<QByteArray>, expectedParts);
    QFETCH(QVector<QByteArray>, expectedPayloads);

    ItemFetchScope in;
    for (int i = ItemFetchScope::CacheOnly; i <= ItemFetchScope::VirtReferences; i = i << 1) {
        QVERIFY(!in.fetch(static_cast<ItemFetchScope::FetchFlag>(i)));
    }
    QVERIFY(in.fetch(ItemFetchScope::None));

    in.setRequestedParts(requestedParts);
    in.setChangedSince(QDateTime(QDate(2015, 8, 10), QTime(23, 52, 20), Qt::UTC));
    in.setTagFetchScope({ "TAGID" });
    in.setAncestorDepth(ItemFetchScope::AllAncestors);
    in.setFetch(ItemFetchScope::CacheOnly);
    in.setFetch(ItemFetchScope::CheckCachedPayloadPartsOnly);
    in.setFetch(ItemFetchScope::FullPayload, fullPayload);
    in.setFetch(ItemFetchScope::AllAttributes);
    in.setFetch(ItemFetchScope::Size);
    in.setFetch(ItemFetchScope::MTime);
    in.setFetch(ItemFetchScope::RemoteRevision);
    in.setFetch(ItemFetchScope::IgnoreErrors);
    in.setFetch(ItemFetchScope::Flags);
    in.setFetch(ItemFetchScope::RemoteID);
    in.setFetch(ItemFetchScope::GID);
    in.setFetch(ItemFetchScope::Tags);
    in.setFetch(ItemFetchScope::Relations);
    in.setFetch(ItemFetchScope::VirtReferences);

    const ItemFetchScope out = serializeAndDeserialize(in);
    QCOMPARE(out.requestedParts(), expectedParts);
    QCOMPARE(out.requestedPayloads(), expectedPayloads);
    QCOMPARE(out.changedSince(), QDateTime(QDate(2015, 8, 10), QTime(23, 52, 20), Qt::UTC));
    QCOMPARE(out.tagFetchScope(), QSet<QByteArray>{ "TAGID" });
    QCOMPARE(out.ancestorDepth(), ItemFetchScope::AllAncestors);
    QCOMPARE(out.fetch(ItemFetchScope::None), false);
    QCOMPARE(out.cacheOnly(), true);
    QCOMPARE(out.checkCachedPayloadPartsOnly(), true);
    QCOMPARE(out.fullPayload(), fullPayload);
    QCOMPARE(out.allAttributes(), true);
    QCOMPARE(out.fetchSize(), true);
    QCOMPARE(out.fetchMTime(), true);
    QCOMPARE(out.fetchRemoteRevision(), true);
    QCOMPARE(out.ignoreErrors(), true);
    QCOMPARE(out.fetchFlags(), true);
    QCOMPARE(out.fetchRemoteId(), true);
    QCOMPARE(out.fetchGID(), true);
    QCOMPARE(out.fetchRelations(), true);
    QCOMPARE(out.fetchVirtualReferences(), true);
}

void ProtocolTest::testScopeContext_data()
{
    QTest::addColumn<qint64>("colId");
    QTest::addColumn<QString>("colRid");
    QTest::addColumn<qint64>("tagId");
    QTest::addColumn<QString>("tagRid");

    QTest::newRow("collection - id") << 42ll << QString()
                                     << 0ll << QString();
    QTest::newRow("collection - rid") << 0ll << QStringLiteral("rid")
                                      << 0ll << QString();
    QTest::newRow("collection - both") << 42ll << QStringLiteral("rid")
                                       << 0ll << QString();

    QTest::newRow("tag - id") << 0ll << QString()
                              << 42ll << QString();
    QTest::newRow("tag - rid") << 0ll << QString()
                               << 0ll << QStringLiteral("rid");
    QTest::newRow("tag - both") << 0ll << QString()
                                << 42ll << QStringLiteral("rid");

    QTest::newRow("both - id") << 42ll << QString()
                               << 10ll << QString();
    QTest::newRow("both - rid") << 0ll << QStringLiteral("colRid")
                                << 0ll << QStringLiteral("tagRid");
    QTest::newRow("col - id, tag - rid") << 42ll << QString()
                                         << 0ll << QStringLiteral("tagRid");
    QTest::newRow("col - rid, tag - id") << 0ll << QStringLiteral("colRid")
                                         << 42ll << QString();
    QTest::newRow("both - both") << 42ll << QStringLiteral("colRid")
                                 << 10ll << QStringLiteral("tagRid");
}

void ProtocolTest::testScopeContext()
{
    QFETCH(qint64, colId);
    QFETCH(QString, colRid);
    QFETCH(qint64, tagId);
    QFETCH(QString, tagRid);

    const bool hasColId = colId > 0;
    const bool hasColRid = !colRid.isEmpty();
    const bool hasTagId = tagId > 0;
    const bool hasTagRid = !tagRid.isEmpty();

    ScopeContext in;
    QVERIFY(in.isEmpty());
    if (hasColId) {
        in.setContext(ScopeContext::Collection, colId);
    }
    if (hasColRid) {
        in.setContext(ScopeContext::Collection, colRid);
    }
    if (hasTagId) {
        in.setContext(ScopeContext::Tag, tagId);
    }
    if (hasTagRid) {
        in.setContext(ScopeContext::Tag, tagRid);
    }

    QCOMPARE(in.hasContextId(ScopeContext::Any), false);
    QCOMPARE(in.hasContextRID(ScopeContext::Any), false);
    QEXPECT_FAIL("collection - both", "Cannot set both ID and RID context", Continue);
    QEXPECT_FAIL("both - both", "Cannot set both ID and RID context", Continue);
    QCOMPARE(in.hasContextId(ScopeContext::Collection), hasColId);
    QCOMPARE(in.hasContextRID(ScopeContext::Collection), hasColRid);
    QEXPECT_FAIL("both - both", "Cannot set both ID and RID context", Continue);
    QEXPECT_FAIL("tag - both", "Cannot set both ID and RID context", Continue);
    QCOMPARE(in.hasContextId(ScopeContext::Tag), hasTagId);
    QCOMPARE(in.hasContextRID(ScopeContext::Tag), hasTagRid);
    QVERIFY(!in.isEmpty());

    ScopeContext out = serializeAndDeserialize(in);
    QCOMPARE(out.isEmpty(), false);
    QEXPECT_FAIL("collection - both", "Cannot set both ID and RID context", Continue);
    QEXPECT_FAIL("both - both", "Cannot set both ID and RID context", Continue);
    QCOMPARE(out.hasContextId(ScopeContext::Collection), hasColId);
    QEXPECT_FAIL("collection - both", "Cannot set both ID and RID context", Continue);
    QEXPECT_FAIL("both - both", "Cannot set both ID and RID context", Continue);
    QCOMPARE(out.contextId(ScopeContext::Collection), colId);
    QCOMPARE(out.hasContextRID(ScopeContext::Collection), hasColRid);
    QCOMPARE(out.contextRID(ScopeContext::Collection), colRid);
    QEXPECT_FAIL("tag - both", "Cannot set both ID and RID context", Continue);
    QEXPECT_FAIL("both - both", "Cannot set both ID and RID context", Continue);
    QCOMPARE(out.hasContextId(ScopeContext::Tag), hasTagId);
    QEXPECT_FAIL("tag - both", "Cannot set both ID and RID context", Continue);
    QEXPECT_FAIL("both - both", "Cannot set both ID and RID context", Continue);
    QCOMPARE(out.contextId(ScopeContext::Tag), tagId);
    QCOMPARE(out.hasContextRID(ScopeContext::Tag), hasTagRid);
    QCOMPARE(out.contextRID(ScopeContext::Tag), tagRid);
    QCOMPARE(out, in);
    const bool notEquals = (out != in);
    QVERIFY(!notEquals);

    // Clearing "any" should not do anything
    out.clearContext(ScopeContext::Any);
    QEXPECT_FAIL("collection - both", "Cannot set both ID and RID context", Continue);
    QEXPECT_FAIL("both - both", "Cannot set both ID and RID context", Continue);
    QCOMPARE(out.hasContextId(ScopeContext::Collection), hasColId);
    QEXPECT_FAIL("collection - both", "Cannot set both ID and RID context", Continue);
    QEXPECT_FAIL("both - both", "Cannot set both ID and RID context", Continue);
    QCOMPARE(out.contextId(ScopeContext::Collection), colId);
    QCOMPARE(out.hasContextRID(ScopeContext::Collection), hasColRid);
    QCOMPARE(out.contextRID(ScopeContext::Collection), colRid);
    QEXPECT_FAIL("tag - both", "Cannot set both ID and RID context", Continue);
    QEXPECT_FAIL("both - both", "Cannot set both ID and RID context", Continue);
    QCOMPARE(out.hasContextId(ScopeContext::Tag), hasTagId);
    QEXPECT_FAIL("tag - both", "Cannot set both ID and RID context", Continue);
    QEXPECT_FAIL("both - both", "Cannot set both ID and RID context", Continue);
    QCOMPARE(out.contextId(ScopeContext::Tag), tagId);
    QCOMPARE(out.hasContextRID(ScopeContext::Tag), hasTagRid);
    QCOMPARE(out.contextRID(ScopeContext::Tag), tagRid);

    if (hasColId || hasColRid) {
        ScopeContext clear = out;
        clear.clearContext(ScopeContext::Collection);
        QCOMPARE(clear.hasContextId(ScopeContext::Collection), false);
        QCOMPARE(clear.hasContextRID(ScopeContext::Collection), false);
        QEXPECT_FAIL("both - both", "Cannot set both ID and RID context", Continue);
        QCOMPARE(clear.hasContextId(ScopeContext::Tag), hasTagId);
        QCOMPARE(clear.hasContextRID(ScopeContext::Tag), hasTagRid);
    }
    if (hasTagId || hasTagRid) {
        ScopeContext clear = out;
        clear.clearContext(ScopeContext::Tag);
        QEXPECT_FAIL("both - both", "Cannot set both ID and RID context", Continue);
        QCOMPARE(clear.hasContextId(ScopeContext::Collection), hasColId);
        QCOMPARE(clear.hasContextRID(ScopeContext::Collection), hasColRid);
        QCOMPARE(clear.hasContextId(ScopeContext::Tag), false);
        QCOMPARE(clear.hasContextRID(ScopeContext::Tag), false);
    }

    out.clearContext(ScopeContext::Collection);
    out.clearContext(ScopeContext::Tag);
    QVERIFY(out.isEmpty());
}

void ProtocolTest::testPartMetaData()
{
    PartMetaData in;
    in.setName("PLD:HEAD");
    in.setSize(42);
    in.setVersion(1);
    in.setStorageType(PartMetaData::External);

    const PartMetaData out = serializeAndDeserialize(in);
    QCOMPARE(out.name(), QByteArray("PLD:HEAD"));
    QCOMPARE(out.size(), 42);
    QCOMPARE(out.version(), 1);
    QCOMPARE(out.storageType(), PartMetaData::External);
    QCOMPARE(out, in);
    const bool notEquals = (in != out);
    QVERIFY(!notEquals);
}

void ProtocolTest::testCachePolicy()
{
    CachePolicy in;
    in.setInherit(true);
    in.setCheckInterval(42);
    in.setCacheTimeout(10);
    in.setSyncOnDemand(true);
    in.setLocalParts({ QStringLiteral("PLD:HEAD"), QStringLiteral("PLD:ENVELOPE") });

    const CachePolicy out = serializeAndDeserialize(in);
    QCOMPARE(out.inherit(), true);
    QCOMPARE(out.checkInterval(), 42);
    QCOMPARE(out.cacheTimeout(), 10);
    QCOMPARE(out.syncOnDemand(), true);
    QCOMPARE(out.localParts(), QStringList() << QStringLiteral("PLD:HEAD") << QStringLiteral("PLD:ENVELOPE"));
    QCOMPARE(out, in);
    const bool notEquals = (out != in);
    QVERIFY(!notEquals);
}

void ProtocolTest::testHelloResponse()
{
    HelloResponse in;
    QVERIFY(in.isResponse());
    QVERIFY(in.isValid());
    QVERIFY(!in.isError());
    in.setServerName(QStringLiteral("AkonadiTest"));
    in.setMessage(QStringLiteral("Oh, hello there!"));
    in.setProtocolVersion(42);
    in.setError(10, QStringLiteral("Ooops"));

    const auto out = serializeAndDeserialize(HelloResponsePtr::create(in));
    QVERIFY(out->isValid());
    QVERIFY(out->isResponse());
    QVERIFY(out->isError());
    QCOMPARE(out->errorCode(), 10);
    QCOMPARE(out->errorMessage(), QStringLiteral("Ooops"));
    QCOMPARE(out->serverName(), QStringLiteral("AkonadiTest"));
    QCOMPARE(out->message(), QStringLiteral("Oh, hello there!"));
    QCOMPARE(out->protocolVersion(), 42);
    QCOMPARE(*out, in);
    const bool notEquals = (*out != in);
    QVERIFY(!notEquals);
}

void ProtocolTest::testLoginCommand()
{
    LoginCommand in;
    QVERIFY(!in.isResponse());
    QVERIFY(in.isValid());
    in.setSessionId("MySession-123-notifications");

    const auto out = serializeAndDeserialize(LoginCommandPtr::create(in));
    QVERIFY(out->isValid());
    QVERIFY(!out->isResponse());
    QCOMPARE(out->sessionId(), QByteArray("MySession-123-notifications"));
    QCOMPARE(*out, in);
    const bool notEquals = (*out != in);
    QVERIFY(!notEquals);
}

void ProtocolTest::testLoginResponse()
{
    LoginResponse in;
    QVERIFY(in.isResponse());
    QVERIFY(in.isValid());
    QVERIFY(!in.isError());
    in.setError(42, QStringLiteral("Ooops"));

    const auto out = serializeAndDeserialize(LoginResponsePtr::create(in));
    QVERIFY(out->isValid());
    QVERIFY(out->isResponse());
    QVERIFY(out->isError());
    QCOMPARE(out->errorCode(), 42);
    QCOMPARE(out->errorMessage(), QStringLiteral("Ooops"));
    QCOMPARE(*out, in);
    const bool notEquals = (*out != in);
    QVERIFY(!notEquals);
}

void ProtocolTest::testLogoutCommand()
{
    LogoutCommand in;
    QVERIFY(!in.isResponse());
    QVERIFY(in.isValid());

    const auto out = serializeAndDeserialize(LogoutCommandPtr::create(in));
    QVERIFY(!out->isResponse());
    QVERIFY(out->isValid());
    QCOMPARE(*out, in);
    const bool notEquals = (*out != in);
    QVERIFY(!notEquals);
}

void ProtocolTest::testLogoutResponse()
{
    LogoutResponse in;
    QVERIFY(in.isResponse());
    QVERIFY(in.isValid());
    QVERIFY(!in.isError());
    in.setError(42, QStringLiteral("Ooops"));

    const auto out = serializeAndDeserialize(LogoutResponsePtr::create(in));
    QVERIFY(out->isValid());
    QVERIFY(out->isResponse());
    QVERIFY(out->isError());
    QCOMPARE(out->errorCode(), 42);
    QCOMPARE(out->errorMessage(), QStringLiteral("Ooops"));
    QCOMPARE(*out, in);
    const bool notEquals = (*out != in);
    QVERIFY(!notEquals);
}


void ProtocolTest::testTransactionCommand()
{
    TransactionCommand in;
    QVERIFY(!in.isResponse());
    QVERIFY(in.isValid());
    in.setMode(TransactionCommand::Begin);

    const auto out = serializeAndDeserialize(TransactionCommandPtr::create(in));
    QVERIFY(out->isValid());
    QVERIFY(!out->isResponse());
    QCOMPARE(out->mode(), TransactionCommand::Begin);
    QCOMPARE(*out, in);
    const bool notEquals = (*out != in);
    QVERIFY(!notEquals);
}

void ProtocolTest::testTransactionResponse()
{
    TransactionResponse in;
    QVERIFY(in.isResponse());
    QVERIFY(in.isValid());
    QVERIFY(!in.isError());
    in.setError(42, QStringLiteral("Ooops"));

    const auto out = serializeAndDeserialize(TransactionResponsePtr::create(in));
    QVERIFY(out->isValid());
    QVERIFY(out->isResponse());
    QVERIFY(out->isError());
    QCOMPARE(out->errorCode(), 42);
    QCOMPARE(out->errorMessage(), QStringLiteral("Ooops"));
    QCOMPARE(*out, in);
    const bool notEquals = (*out != in);
    QVERIFY(!notEquals);
}

void ProtocolTest::testCreateItemCommand()
{
    Scope addedTags(QVector<qint64>{ 3, 4 });
    Scope removedTags(QVector<qint64>{ 5, 6 });
    Attributes attrs{ { "ATTR1", "MyAttr" }, { "ATTR2", "Můj chlupaťoučký kůň" } };
    QSet<QByteArray> parts{ "PLD:HEAD", "PLD:ENVELOPE" };

    CreateItemCommand in;
    QVERIFY(!in.isResponse());
    QVERIFY(in.isValid());
    QCOMPARE(in.mergeModes(), CreateItemCommand::None);
    in.setMergeModes(CreateItemCommand::MergeModes(CreateItemCommand::GID | CreateItemCommand::RemoteID));
    in.setCollection(Scope(1));
    in.setItemSize(100);
    in.setMimeType(QStringLiteral("text/directory"));
    in.setGid(QStringLiteral("GID"));
    in.setRemoteId(QStringLiteral("RID"));
    in.setRemoteRevision(QStringLiteral("RREV"));
    in.setDateTime(QDateTime(QDate(2015, 8, 11), QTime(14, 32, 21), Qt::UTC));
    in.setFlags({ "\\SEEN", "FLAG" });
    in.setFlagsOverwritten(true);
    in.setAddedFlags({ "FLAG2" });
    in.setRemovedFlags({ "FLAG3" });
    in.setTags(Scope(2));
    in.setAddedTags(addedTags);
    in.setRemovedTags(removedTags);
    in.setAttributes(attrs);
    in.setParts(parts);

    const auto out = serializeAndDeserialize(CreateItemCommandPtr::create(in));
    QVERIFY(out->isValid());
    QVERIFY(!out->isResponse());
    QCOMPARE(out->mergeModes(), CreateItemCommand::GID | CreateItemCommand::RemoteID);
    QCOMPARE(out->collection(), Scope(1));
    QCOMPARE(out->itemSize(), 100);
    QCOMPARE(out->mimeType(), QStringLiteral("text/directory"));
    QCOMPARE(out->gid(), QStringLiteral("GID"));
    QCOMPARE(out->remoteId(), QStringLiteral("RID"));
    QCOMPARE(out->remoteRevision(), QStringLiteral("RREV"));
    QCOMPARE(out->dateTime(), QDateTime(QDate(2015, 8, 11), QTime(14, 32, 21), Qt::UTC));
    QCOMPARE(out->flags(), QSet<QByteArray>() << "\\SEEN" << "FLAG");
    QCOMPARE(out->flagsOverwritten(), true);
    QCOMPARE(out->addedFlags(), QSet<QByteArray>{ "FLAG2" });
    QCOMPARE(out->removedFlags(), QSet<QByteArray>{ "FLAG3" });
    QCOMPARE(out->tags(), Scope(2));
    QCOMPARE(out->addedTags(), addedTags);
    QCOMPARE(out->removedTags(), removedTags);
    QCOMPARE(out->attributes(), attrs);
    QCOMPARE(out->parts(), parts);
    QCOMPARE(*out, in);
    const bool notEquals = (*out != in);
    QVERIFY(!notEquals);
}

void ProtocolTest::testCreateItemResponse()
{
    CreateItemResponse in;
    QVERIFY(in.isResponse());
    QVERIFY(in.isValid());
    QVERIFY(!in.isError());
    in.setError(42, QStringLiteral("Ooops"));

    const auto out = serializeAndDeserialize(CreateItemResponsePtr::create(in));
    QVERIFY(out->isValid());
    QVERIFY(out->isResponse());
    QVERIFY(out->isError());
    QCOMPARE(out->errorCode(), 42);
    QCOMPARE(out->errorMessage(), QStringLiteral("Ooops"));
    QCOMPARE(*out, in);
    const bool notEquals = (*out != in);
    QVERIFY(!notEquals);
}

void ProtocolTest::testCopyItemsCommand()
{
    const Scope items(QVector<qint64>{ 1, 2, 3, 10 });

    CopyItemsCommand in;
    QVERIFY(in.isValid());
    QVERIFY(!in.isResponse());
    in.setItems(items);
    in.setDestination(Scope(42));

    const auto out = serializeAndDeserialize(CopyItemsCommandPtr::create(in));
    QVERIFY(out->isValid());
    QVERIFY(!out->isResponse());
    QCOMPARE(out->items(), items);
    QCOMPARE(out->destination(), Scope(42));
    QCOMPARE(*out, in);
    const bool notEquals = (*out != in);
    QVERIFY(!notEquals);
}

void ProtocolTest::testCopyItemsResponse()
{
    CopyItemsResponse in;
    QVERIFY(in.isResponse());
    QVERIFY(in.isValid());
    QVERIFY(!in.isError());
    in.setError(42, QStringLiteral("Ooops"));

    const auto out = serializeAndDeserialize(CopyItemsResponsePtr::create(in));
    QVERIFY(out->isValid());
    QVERIFY(out->isResponse());
    QVERIFY(out->isError());
    QCOMPARE(out->errorCode(), 42);
    QCOMPARE(out->errorMessage(), QStringLiteral("Ooops"));
    QCOMPARE(*out, in);
    const bool notEquals = (*out != in);
    QVERIFY(!notEquals);
}

QTEST_MAIN(ProtocolTest)