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
|
#include "connection.h"
#include "room.h"
#include "user.h"
#include "csapi/room_send.h"
#include "csapi/joining.h"
#include "csapi/leaving.h"
#include "events/simplestateevents.h"
#include <QtCore/QCoreApplication>
#include <QtCore/QStringBuilder>
#include <QtCore/QTimer>
#include <QtCore/QTemporaryFile>
#include <QtCore/QFileInfo>
#include <iostream>
#include <functional>
using namespace QMatrixClient;
using std::cout;
using std::endl;
using namespace std::placeholders;
class QMCTest : public QObject
{
public:
QMCTest(Connection* conn, QString testRoomName, QString source);
private slots:
void setupAndRun();
void onNewRoom(Room* r);
void run();
void doTests();
void loadMembers();
void sendMessage();
void sendFile();
void checkFileSendingOutcome(const QString& txnId,
const QString& fileName);
void setTopic();
void addAndRemoveTag();
void sendAndRedact();
bool checkRedactionOutcome(const QString& evtIdToRedact);
void markDirectChat();
void checkDirectChatOutcome(
const Connection::DirectChatsMap& added);
void conclude();
void finalize();
private:
QScopedPointer<Connection, QScopedPointerDeleteLater> c;
QStringList running;
QStringList succeeded;
QStringList failed;
QString origin;
QString targetRoomName;
Room* targetRoom = nullptr;
bool validatePendingEvent(const QString& txnId);
};
#define QMC_CHECK(description, condition) \
{ \
Q_ASSERT(running.removeOne(description)); \
if (!!(condition)) \
{ \
succeeded.push_back(description); \
cout << (description) << " successful" << endl; \
if (targetRoom) \
targetRoom->postMessage( \
origin % ": " % (description) % " successful", \
MessageEventType::Notice); \
} else { \
failed.push_back(description); \
cout << (description) << " FAILED" << endl; \
if (targetRoom) \
targetRoom->postPlainText( \
origin % ": " % (description) % " FAILED"); \
} \
}
bool QMCTest::validatePendingEvent(const QString& txnId)
{
auto it = targetRoom->findPendingEvent(txnId);
return it != targetRoom->pendingEvents().end() &&
it->deliveryStatus() == EventStatus::Submitted &&
(*it)->transactionId() == txnId;
}
QMCTest::QMCTest(Connection* conn, QString testRoomName, QString source)
: c(conn), origin(std::move(source)), targetRoomName(std::move(testRoomName))
{
if (!origin.isEmpty())
cout << "Origin for the test message: " << origin.toStdString() << endl;
if (!targetRoomName.isEmpty())
cout << "Test room name: " << targetRoomName.toStdString() << endl;
connect(c.data(), &Connection::connected, this, &QMCTest::setupAndRun);
connect(c.data(), &Connection::loadedRoomState, this, &QMCTest::onNewRoom);
// Big countdown watchdog
QTimer::singleShot(180000, this, &QMCTest::conclude);
}
void QMCTest::setupAndRun()
{
Q_ASSERT(!c->homeserver().isEmpty() && c->homeserver().isValid());
Q_ASSERT(c->domain() == c->userId().section(':', 1));
cout << "Connected, server: "
<< c->homeserver().toDisplayString().toStdString() << endl;
cout << "Access token: " << c->accessToken().toStdString() << endl;
if (!targetRoomName.isEmpty())
{
cout << "Joining " << targetRoomName.toStdString() << endl;
running.push_back("Join room");
auto joinJob = c->joinRoom(targetRoomName);
connect(joinJob, &BaseJob::failure, this,
[this] { QMC_CHECK("Join room", false); conclude(); });
// Connection::joinRoom() creates a Room object upon JoinRoomJob::success
// but this object is empty until the first sync is done.
connect(joinJob, &BaseJob::success, this, [this,joinJob] {
targetRoom = c->room(joinJob->roomId(), JoinState::Join);
QMC_CHECK("Join room", targetRoom != nullptr);
run();
});
} else
run();
}
void QMCTest::onNewRoom(Room* r)
{
cout << "New room: " << r->id().toStdString() << endl
<< " Name: " << r->name().toStdString() << endl
<< " Canonical alias: " << r->canonicalAlias().toStdString() << endl
<< endl;
connect(r, &Room::aboutToAddNewMessages, r, [r] (RoomEventsRange timeline) {
cout << timeline.size() << " new event(s) in room "
<< r->canonicalAlias().toStdString() << endl;
// for (const auto& item: timeline)
// {
// cout << "From: "
// << r->roomMembername(item->senderId()).toStdString()
// << endl << "Timestamp:"
// << item->timestamp().toString().toStdString() << endl
// << "JSON:" << endl << item->originalJson().toStdString() << endl;
// }
});
}
void QMCTest::run()
{
c->setLazyLoading(true);
c->syncLoop();
connectSingleShot(c.data(), &Connection::syncDone, this, &QMCTest::doTests);
connect(c.data(), &Connection::syncDone, c.data(), [this] {
cout << "Sync complete, " << running.size() << " test(s) in the air: "
<< running.join(", ").toStdString() << endl;
if (running.isEmpty())
conclude();
});
}
void QMCTest::doTests()
{
cout << "Starting tests" << endl;
loadMembers();
// Add here tests not requiring the test room
if (targetRoomName.isEmpty())
return;
sendMessage();
sendFile();
setTopic();
addAndRemoveTag();
sendAndRedact();
markDirectChat();
// Add here tests with the test room
}
void QMCTest::loadMembers()
{
running.push_back("Loading members");
// The dedicated qmc-test room is too small to test
// lazy-loading-then-full-loading; use #qmatrixclient:matrix.org instead.
// TODO: #264
auto* r = c->room(QStringLiteral("!PCzUtxtOjUySxSelof:matrix.org"));
if (!r)
{
cout << "#test:matrix.org is not found in the test user's rooms" << endl;
QMC_CHECK("Loading members", false);
return;
}
// It's not exactly correct because an arbitrary server might not support
// lazy loading; but in the absence of capabilities framework we assume
// it does.
if (r->memberNames().size() >= r->joinedCount())
{
cout << "Lazy loading doesn't seem to be enabled" << endl;
QMC_CHECK("Loading members", false);
return;
}
r->setDisplayed();
connect(r, &Room::allMembersLoaded, [this,r] {
QMC_CHECK("Loading members",
r->memberNames().size() >= r->joinedCount());
});
}
void QMCTest::sendMessage()
{
running.push_back("Message sending");
cout << "Sending a message" << endl;
auto txnId = targetRoom->postPlainText("Hello, " % origin % " is here");
if (!validatePendingEvent(txnId))
{
cout << "Invalid pending event right after submitting" << endl;
QMC_CHECK("Message sending", false);
return;
}
connectUntil(targetRoom, &Room::pendingEventAboutToMerge, this,
[this,txnId] (const RoomEvent* evt, int pendingIdx) {
const auto& pendingEvents = targetRoom->pendingEvents();
Q_ASSERT(pendingIdx >= 0 && pendingIdx < int(pendingEvents.size()));
if (evt->transactionId() != txnId)
return false;
QMC_CHECK("Message sending",
is<RoomMessageEvent>(*evt) && !evt->id().isEmpty() &&
pendingEvents[size_t(pendingIdx)]->transactionId()
== evt->transactionId());
return true;
});
}
void QMCTest::sendFile()
{
running.push_back("File sending");
cout << "Sending a file" << endl;
auto* tf = new QTemporaryFile;
if (!tf->open())
{
cout << "Failed to create a temporary file" << endl;
QMC_CHECK("File sending", false);
return;
}
tf->write("Test");
tf->close();
// QFileInfo::fileName brings only the file name; QFile::fileName brings
// the full path
const auto tfName = QFileInfo(*tf).fileName();
cout << "Sending file" << tfName.toStdString() << endl;
const auto txnId = targetRoom->postFile("Test file",
QUrl::fromLocalFile(tf->fileName()));
if (!validatePendingEvent(txnId))
{
cout << "Invalid pending event right after submitting" << endl;
QMC_CHECK("File sending", false);
delete tf;
return;
}
// FIXME: Clean away connections (connectUntil doesn't help here).
connect(targetRoom, &Room::fileTransferCompleted, this,
[this,txnId,tf,tfName] (const QString& id) {
auto fti = targetRoom->fileTransferInfo(id);
Q_ASSERT(fti.status == FileTransferInfo::Completed);
if (id != txnId)
return;
delete tf;
checkFileSendingOutcome(txnId, tfName);
});
connect(targetRoom, &Room::fileTransferFailed, this,
[this,txnId,tf]
(const QString& id, const QString& error) {
if (id != txnId)
return;
targetRoom->postPlainText(origin % ": File upload failed: " % error);
delete tf;
QMC_CHECK("File sending", false);
});
}
void QMCTest::checkFileSendingOutcome(const QString& txnId,
const QString& fileName)
{
auto it = targetRoom->findPendingEvent(txnId);
if (it == targetRoom->pendingEvents().end())
{
cout << "Pending file event dropped before upload completion"
<< endl;
QMC_CHECK("File sending", false);
return;
}
if (it->deliveryStatus() != EventStatus::FileUploaded)
{
cout << "Pending file event status upon upload completion is "
<< it->deliveryStatus() << " != FileUploaded("
<< EventStatus::FileUploaded << ')' << endl;
QMC_CHECK("File sending", false);
return;
}
connectUntil(targetRoom, &Room::pendingEventAboutToMerge, this,
[this,txnId,fileName] (const RoomEvent* evt, int pendingIdx) {
const auto& pendingEvents = targetRoom->pendingEvents();
Q_ASSERT(pendingIdx >= 0 && pendingIdx < int(pendingEvents.size()));
if (evt->transactionId() != txnId)
return false;
cout << "File event " << txnId.toStdString()
<< " arrived in the timeline" << endl;
visit(*evt,
[&] (const RoomMessageEvent& e) {
QMC_CHECK("File sending",
!e.id().isEmpty() &&
pendingEvents[size_t(pendingIdx)]
->transactionId() == txnId &&
e.hasFileContent() &&
e.content()->fileInfo()->originalName == fileName);
},
[this] (const RoomEvent&) {
QMC_CHECK("File sending", false);
});
return true;
});
}
void QMCTest::setTopic()
{
static const char* const stateTestName = "State setting test";
static const char* const fakeStateTestName = "Fake state event immunity test";
running.push_back(stateTestName);
running.push_back(fakeStateTestName);
auto initialTopic = targetRoom->topic();
const auto newTopic = c->generateTxnId();
targetRoom->setTopic(newTopic); // Sets the state by proper means
const auto fakeTopic = c->generateTxnId();
targetRoom->postJson(RoomTopicEvent::matrixTypeId(), // Fake state event
RoomTopicEvent(fakeTopic).contentJson());
connectUntil(targetRoom, &Room::topicChanged, this,
[this,newTopic,fakeTopic,initialTopic] {
if (targetRoom->topic() == newTopic)
{
QMC_CHECK(stateTestName, true);
// Don't reset the topic yet if the negative test still runs
if (!running.contains(fakeStateTestName))
targetRoom->setTopic(initialTopic);
return true;
}
return false;
});
connectUntil(targetRoom, &Room::pendingEventAboutToMerge, this,
[this,fakeTopic,initialTopic] (const RoomEvent* e, int) {
if (e->contentJson().value("topic").toString() != fakeTopic)
return false; // Wait on for the right event
QMC_CHECK(fakeStateTestName, !e->isStateEvent());
if (!running.contains(fakeStateTestName))
targetRoom->setTopic(initialTopic);
return true;
});
}
void QMCTest::addAndRemoveTag()
{
running.push_back("Tagging test");
static const auto TestTag = QStringLiteral("org.qmatrixclient.test");
// Pre-requisite
if (targetRoom->tags().contains(TestTag))
targetRoom->removeTag(TestTag);
// Connect first because the signal is emitted synchronously.
connect(targetRoom, &Room::tagsChanged, targetRoom, [=] {
cout << "Room " << targetRoom->id().toStdString()
<< ", tag(s) changed:" << endl
<< " " << targetRoom->tagNames().join(", ").toStdString() << endl;
if (targetRoom->tags().contains(TestTag))
{
cout << "Test tag set, removing it now" << endl;
targetRoom->removeTag(TestTag);
QMC_CHECK("Tagging test", !targetRoom->tags().contains(TestTag));
disconnect(targetRoom, &Room::tagsChanged, nullptr, nullptr);
}
});
cout << "Adding a tag" << endl;
targetRoom->addTag(TestTag);
}
void QMCTest::sendAndRedact()
{
running.push_back("Redaction");
cout << "Sending a message to redact" << endl;
auto txnId = targetRoom->postPlainText(origin % ": message to redact");
if (txnId.isEmpty())
{
QMC_CHECK("Redaction", false);
return;
}
connect(targetRoom, &Room::messageSent, this,
[this,txnId] (const QString& tId, const QString& evtId) {
if (tId != txnId)
return;
cout << "Redacting the message" << endl;
targetRoom->redactEvent(evtId, origin);
connectUntil(targetRoom, &Room::addedMessages, this,
[this,evtId] { return checkRedactionOutcome(evtId); });
});
}
bool QMCTest::checkRedactionOutcome(const QString& evtIdToRedact)
{
// There are two possible (correct) outcomes: either the event comes already
// redacted at the next sync, or the nearest sync completes with
// the unredacted event but the next one brings redaction.
auto it = targetRoom->findInTimeline(evtIdToRedact);
if (it == targetRoom->timelineEdge())
return false; // Waiting for the next sync
if ((*it)->isRedacted())
{
cout << "The sync brought already redacted message" << endl;
QMC_CHECK("Redaction", true);
} else {
cout << "Message came non-redacted with the sync, waiting for redaction"
<< endl;
connectUntil(targetRoom, &Room::replacedEvent, this,
[this,evtIdToRedact]
(const RoomEvent* newEvent, const RoomEvent* oldEvent) {
if (oldEvent->id() != evtIdToRedact)
return false;
QMC_CHECK("Redaction", newEvent->isRedacted() &&
newEvent->redactionReason() == origin);
return true;
});
}
return true;
}
void QMCTest::markDirectChat()
{
if (targetRoom->directChatUsers().contains(c->user()))
{
cout << "Warning: the room is already a direct chat,"
" only unmarking will be tested" << endl;
checkDirectChatOutcome({{ c->user(), targetRoom->id() }});
return;
}
// Connect first because the signal is emitted synchronously.
connect(c.data(), &Connection::directChatsListChanged,
this, &QMCTest::checkDirectChatOutcome);
cout << "Marking the room as a direct chat" << endl;
c->addToDirectChats(targetRoom, c->user());
}
void QMCTest::checkDirectChatOutcome(const Connection::DirectChatsMap& added)
{
running.push_back("Direct chat test");
disconnect(c.data(), &Connection::directChatsListChanged, nullptr, nullptr);
if (!targetRoom->isDirectChat())
{
cout << "The room has not been marked as a direct chat" << endl;
QMC_CHECK("Direct chat test", false);
return;
}
if (!added.contains(c->user(), targetRoom->id()))
{
cout << "The room has not been listed in new direct chats" << endl;
QMC_CHECK("Direct chat test", false);
return;
}
cout << "Unmarking the direct chat" << endl;
c->removeFromDirectChats(targetRoom->id(), c->user());
QMC_CHECK("Direct chat test", !c->isDirectChat(targetRoom->id()));
}
void QMCTest::conclude()
{
c->stopSync();
auto succeededRec = QString::number(succeeded.size()) + " tests succeeded";
if (!failed.isEmpty() || !running.isEmpty())
succeededRec += " of " %
QString::number(succeeded.size() + failed.size() + running.size()) %
" total";
QString plainReport = origin % ": Testing complete, " % succeededRec;
QString color = failed.isEmpty() && running.isEmpty() ? "00AA00" : "AA0000";
QString htmlReport = origin % ": <strong><font data-mx-color='#" % color %
"' color='#" % color % "'>Testing complete</font></strong>, " %
succeededRec;
if (!failed.isEmpty())
{
plainReport += "\nFAILED: " % failed.join(", ");
htmlReport += "<br><strong>Failed:</strong> " % failed.join(", ");
}
if (!running.isEmpty())
{
plainReport += "\nDID NOT FINISH: " % running.join(", ");
htmlReport +=
"<br><strong>Did not finish:</strong> " % running.join(", ");
}
cout << plainReport.toStdString() << endl;
if (targetRoom)
{
// TODO: Waiting for proper futures to come so that it could be:
// targetRoom->postHtmlText(...)
// .then(this, &QMCTest::finalize); // Qt-style or
// .then([this] { finalize(); }); // STL-style
auto txnId = targetRoom->postHtmlText(plainReport, htmlReport);
connect(targetRoom, &Room::messageSent, this,
[this,txnId] (QString serverTxnId) {
if (txnId != serverTxnId)
return;
cout << "Leaving the room" << endl;
connect(targetRoom->leaveRoom(), &BaseJob::finished,
this, &QMCTest::finalize);
});
}
else
finalize();
}
void QMCTest::finalize()
{
cout << "Logging out" << endl;
c->logout();
connect(c.data(), &Connection::loggedOut, qApp,
[this] {
QCoreApplication::processEvents();
QCoreApplication::exit(failed.size() + running.size());
});
}
int main(int argc, char* argv[])
{
QCoreApplication app(argc, argv);
if (argc < 4)
{
cout << "Usage: qmc-example <user> <passwd> <device_name> [<room_alias> [origin]]" << endl;
return -1;
}
cout << "Connecting to the server as " << argv[1] << endl;
auto conn = new Connection;
conn->connectToServer(argv[1], argv[2], argv[3]);
QMCTest test { conn, argc >= 5 ? argv[4] : nullptr,
argc >= 6 ? argv[5] : nullptr };
return app.exec();
}
|