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
|
// SPDX-FileCopyrightText: 2012 Jeremy Lainé <jeremy.laine@m4x.org>
// SPDX-FileCopyrightText: 2022 Melvin Keskin <melvo@olomono.de>
//
// SPDX-License-Identifier: LGPL-2.1-or-later
#include "QXmppE2eeMetadata.h"
#include "QXmppStanza.h"
#include "util.h"
#include <QDateTime>
#include <QObject>
class QXmppStanzaStub : public QXmppStanza
{
public:
void toXml(QXmlStreamWriter *) const override {};
};
class tst_QXmppStanza : public QObject
{
Q_OBJECT
private:
Q_SLOT void testExtendedAddress_data();
Q_SLOT void testExtendedAddress();
Q_SLOT void testErrorCases_data();
Q_SLOT void testErrorCases();
Q_SLOT void testErrorFileTooLarge();
Q_SLOT void testErrorRetry();
Q_SLOT void testErrorEnums();
Q_SLOT void testEncryption();
Q_SLOT void testSenderKey();
Q_SLOT void testSceTimestamp();
};
void tst_QXmppStanza::testExtendedAddress_data()
{
QTest::addColumn<QByteArray>("xml");
QTest::addColumn<bool>("delivered");
QTest::addColumn<QString>("description");
QTest::addColumn<QString>("jid");
QTest::addColumn<QString>("type");
QTest::newRow("simple")
<< QByteArray(R"(<address jid="foo@example.com/QXmpp" type="bcc"/>)")
<< false
<< QString()
<< QString("foo@example.com/QXmpp")
<< QString("bcc");
QTest::newRow("full")
<< QByteArray(R"(<address delivered="true" desc="some description" jid="foo@example.com/QXmpp" type="bcc"/>)")
<< true
<< QString("some description")
<< QString("foo@example.com/QXmpp")
<< QString("bcc");
}
void tst_QXmppStanza::testExtendedAddress()
{
QFETCH(QByteArray, xml);
QFETCH(bool, delivered);
QFETCH(QString, description);
QFETCH(QString, jid);
QFETCH(QString, type);
QXmppExtendedAddress address;
parsePacket(address, xml);
QCOMPARE(address.isDelivered(), delivered);
QCOMPARE(address.description(), description);
QCOMPARE(address.jid(), jid);
QCOMPARE(address.type(), type);
serializePacket(address, xml);
}
void tst_QXmppStanza::testErrorCases_data()
{
QTest::addColumn<QByteArray>("xml");
QTest::addColumn<QXmppStanza::Error::Type>("type");
QTest::addColumn<QXmppStanza::Error::Condition>("condition");
QTest::addColumn<QString>("text");
QTest::addColumn<QString>("redirectionUri");
QTest::addColumn<QString>("by");
#define ROW(name, xml, type, condition, text, redirect, by) \
QTest::newRow(QT_STRINGIFY(name)) \
<< QByteArrayLiteral(xml) \
<< QXmppStanza::Error::type \
<< QXmppStanza::Error::condition \
<< text \
<< redirect \
<< by
#define BASIC(xml, type, condition) \
ROW(condition, xml, type, condition, QString(), QString(), QString())
ROW(
empty - text,
"<error type=\"modify\">"
"<bad-request xmlns=\"urn:ietf:params:xml:ns:xmpp-stanzas\"/>"
"</error>",
Modify,
BadRequest,
QString(),
QString(),
QString());
ROW(
redirection - uri - gone,
"<error by=\"example.net\" type=\"cancel\">"
"<gone xmlns=\"urn:ietf:params:xml:ns:xmpp-stanzas\">"
"xmpp:romeo@afterlife.example.net"
"</gone>"
"</error>",
Cancel,
Gone,
QString(),
"xmpp:romeo@afterlife.example.net",
"example.net");
ROW(
redirection - uri - redirect,
"<error type=\"cancel\">"
"<redirect xmlns=\"urn:ietf:params:xml:ns:xmpp-stanzas\">"
"xmpp:rms@afterlife.example.net"
"</redirect>"
"</error>",
Cancel,
Redirect,
QString(),
"xmpp:rms@afterlife.example.net",
QString());
ROW(
redirection - uri - empty,
"<error type=\"cancel\">"
"<redirect xmlns=\"urn:ietf:params:xml:ns:xmpp-stanzas\"/>"
"</error>",
Cancel,
Redirect,
QString(),
QString(),
QString());
ROW(
policy - violation - text,
"<error by=\"example.net\" type=\"modify\">"
"<policy-violation xmlns=\"urn:ietf:params:xml:ns:xmpp-stanzas\"/>"
"<text xml:lang=\"en\" xmlns=\"urn:ietf:params:xml:ns:xmpp-stanzas\">The used words are not allowed on this server.</text>"
"</error>",
Modify,
PolicyViolation,
"The used words are not allowed on this server.",
QString(),
"example.net");
ROW(
jid - malformed - with - by,
"<error by=\"muc.example.com\" type=\"modify\">"
"<jid-malformed xmlns=\"urn:ietf:params:xml:ns:xmpp-stanzas\"/>"
"</error>",
Modify,
JidMalformed,
QString(),
QString(),
"muc.example.com");
BASIC(
"<error type=\"modify\">"
"<bad-request xmlns=\"urn:ietf:params:xml:ns:xmpp-stanzas\"/>"
"</error>",
Modify,
BadRequest);
BASIC(
"<error type=\"cancel\">"
"<conflict xmlns=\"urn:ietf:params:xml:ns:xmpp-stanzas\"/>"
"</error>",
Cancel,
Conflict);
BASIC(
"<error type=\"cancel\">"
"<feature-not-implemented xmlns=\"urn:ietf:params:xml:ns:xmpp-stanzas\"/>"
"</error>",
Cancel,
FeatureNotImplemented);
BASIC(
"<error type=\"auth\">"
"<forbidden xmlns=\"urn:ietf:params:xml:ns:xmpp-stanzas\"/>"
"</error>",
Auth,
Forbidden);
BASIC(
"<error type=\"cancel\">"
"<gone xmlns=\"urn:ietf:params:xml:ns:xmpp-stanzas\"/>"
"</error>",
Cancel,
Gone);
BASIC(
"<error type=\"cancel\">"
"<internal-server-error xmlns=\"urn:ietf:params:xml:ns:xmpp-stanzas\"/>"
"</error>",
Cancel,
InternalServerError);
BASIC(
"<error type=\"cancel\">"
"<item-not-found xmlns=\"urn:ietf:params:xml:ns:xmpp-stanzas\"/>"
"</error>",
Cancel,
ItemNotFound);
BASIC(
"<error type=\"modify\">"
"<jid-malformed xmlns=\"urn:ietf:params:xml:ns:xmpp-stanzas\"/>"
"</error>",
Modify,
JidMalformed);
BASIC(
"<error type=\"modify\">"
"<not-acceptable xmlns=\"urn:ietf:params:xml:ns:xmpp-stanzas\"/>"
"</error>",
Modify,
NotAcceptable);
BASIC(
"<error type=\"cancel\">"
"<not-allowed xmlns=\"urn:ietf:params:xml:ns:xmpp-stanzas\"/>"
"</error>",
Cancel,
NotAllowed);
BASIC(
"<error type=\"auth\">"
"<not-authorized xmlns=\"urn:ietf:params:xml:ns:xmpp-stanzas\"/>"
"</error>",
Auth,
NotAuthorized);
BASIC(
"<error type=\"modify\">"
"<policy-violation xmlns=\"urn:ietf:params:xml:ns:xmpp-stanzas\"/>"
"</error>",
Modify,
PolicyViolation);
BASIC(
"<error type=\"wait\">"
"<recipient-unavailable xmlns=\"urn:ietf:params:xml:ns:xmpp-stanzas\"/>"
"</error>",
Wait,
RecipientUnavailable);
BASIC(
"<error type=\"modify\">"
"<redirect xmlns=\"urn:ietf:params:xml:ns:xmpp-stanzas\"/>"
"</error>",
Modify,
Redirect);
BASIC(
"<error type=\"auth\">"
"<registration-required xmlns=\"urn:ietf:params:xml:ns:xmpp-stanzas\"/>"
"</error>",
Auth,
RegistrationRequired);
BASIC(
"<error type=\"cancel\">"
"<remote-server-not-found xmlns=\"urn:ietf:params:xml:ns:xmpp-stanzas\"/>"
"</error>",
Cancel,
RemoteServerNotFound);
BASIC(
"<error type=\"wait\">"
"<remote-server-timeout xmlns=\"urn:ietf:params:xml:ns:xmpp-stanzas\"/>"
"</error>",
Wait,
RemoteServerTimeout);
BASIC(
"<error type=\"wait\">"
"<resource-constraint xmlns=\"urn:ietf:params:xml:ns:xmpp-stanzas\"/>"
"</error>",
Wait,
ResourceConstraint);
BASIC(
"<error type=\"cancel\">"
"<service-unavailable xmlns=\"urn:ietf:params:xml:ns:xmpp-stanzas\"/>"
"</error>",
Cancel,
ServiceUnavailable);
BASIC(
"<error type=\"auth\">"
"<subscription-required xmlns=\"urn:ietf:params:xml:ns:xmpp-stanzas\"/>"
"</error>",
Auth,
SubscriptionRequired);
BASIC(
"<error type=\"modify\">"
"<undefined-condition xmlns=\"urn:ietf:params:xml:ns:xmpp-stanzas\"/>"
"</error>",
Modify,
UndefinedCondition);
#undef BASIC
#undef ROW
}
void tst_QXmppStanza::testErrorCases()
{
QFETCH(QByteArray, xml);
QFETCH(QXmppStanza::Error::Type, type);
QFETCH(QXmppStanza::Error::Condition, condition);
QFETCH(QString, text);
QFETCH(QString, redirectionUri);
QFETCH(QString, by);
// parsing
QXmppStanza::Error error;
parsePacket(error, xml);
QCOMPARE(error.type(), type);
QCOMPARE(error.condition(), condition);
QCOMPARE(error.text(), text);
QCOMPARE(error.redirectionUri(), redirectionUri);
QCOMPARE(error.by(), by);
// check parsed error results in the same xml
serializePacket(error, xml);
// serialization from setters
error = QXmppStanza::Error();
error.setType(type);
error.setCondition(condition);
error.setText(text);
error.setRedirectionUri(redirectionUri);
error.setBy(by);
serializePacket(error, xml);
}
void tst_QXmppStanza::testErrorFileTooLarge()
{
const QByteArray xml(
"<error type=\"modify\">"
"<not-acceptable xmlns=\"urn:ietf:params:xml:ns:xmpp-stanzas\"/>"
"<text xml:lang=\"en\" "
"xmlns=\"urn:ietf:params:xml:ns:xmpp-stanzas\">"
"File too large. The maximum file size is 20000 bytes"
"</text>"
"<file-too-large xmlns=\"urn:xmpp:http:upload:0\">"
"<max-file-size>20000</max-file-size>"
"</file-too-large>"
"</error>");
QXmppStanza::Error error;
parsePacket(error, xml);
QCOMPARE(error.type(), QXmppStanza::Error::Modify);
QCOMPARE(error.text(), QString("File too large. The maximum file size is "
"20000 bytes"));
QCOMPARE(error.condition(), QXmppStanza::Error::NotAcceptable);
QVERIFY(error.fileTooLarge());
QCOMPARE(error.maxFileSize(), 20000);
serializePacket(error, xml);
// test setters
error.setMaxFileSize(60000);
QCOMPARE(error.maxFileSize(), 60000);
error.setFileTooLarge(false);
QVERIFY(!error.fileTooLarge());
QXmppStanza::Error e2;
e2.setMaxFileSize(123000);
QVERIFY(e2.fileTooLarge());
}
void tst_QXmppStanza::testErrorRetry()
{
const QByteArray xml(
"<error type=\"wait\">"
"<resource-constraint "
"xmlns=\"urn:ietf:params:xml:ns:xmpp-stanzas\"/>"
"<text xml:lang=\"en\" "
"xmlns=\"urn:ietf:params:xml:ns:xmpp-stanzas\">"
"Quota reached. You can only upload 5 files in 5 minutes"
"</text>"
"<retry xmlns=\"urn:xmpp:http:upload:0\" "
"stamp=\"2017-12-03T23:42:05Z\"/>"
"</error>");
QXmppStanza::Error error;
parsePacket(error, xml);
QCOMPARE(error.type(), QXmppStanza::Error::Wait);
QCOMPARE(error.text(), QString("Quota reached. You can only upload 5 "
"files in 5 minutes"));
QCOMPARE(error.condition(), QXmppStanza::Error::ResourceConstraint);
QCOMPARE(error.retryDate(), QDateTime(QDate(2017, 12, 03), QTime(23, 42, 05), Qt::UTC));
serializePacket(error, xml);
// test setter
error.setRetryDate(QDateTime(QDate(1985, 10, 26), QTime(1, 35)));
QCOMPARE(error.retryDate(), QDateTime(QDate(1985, 10, 26), QTime(1, 35)));
}
void tst_QXmppStanza::testErrorEnums()
{
QXmppStanza::Error err;
QCOMPARE(err.condition(), QXmppStanza::Error::NoCondition);
QCOMPARE(err.type(), QXmppStanza::Error::NoType);
err.setCondition(QXmppStanza::Error::BadRequest);
err.setType(QXmppStanza::Error::Cancel);
err.setCondition(QXmppStanza::Error::Condition(-1));
err.setType(QXmppStanza::Error::Type(-1));
QCOMPARE(err.condition(), QXmppStanza::Error::NoCondition);
QCOMPARE(err.type(), QXmppStanza::Error::NoType);
}
void tst_QXmppStanza::testEncryption()
{
QXmppStanzaStub stanza;
QVERIFY(!stanza.e2eeMetadata().has_value());
QXmppE2eeMetadata e2eeMetadata;
e2eeMetadata.setEncryption(QXmpp::Omemo2);
stanza.setE2eeMetadata(e2eeMetadata);
QCOMPARE(stanza.e2eeMetadata()->encryption(), QXmpp::Omemo2);
}
void tst_QXmppStanza::testSenderKey()
{
QXmppStanzaStub stanza;
QVERIFY(!stanza.e2eeMetadata().has_value());
QXmppE2eeMetadata e2eeMetadata;
e2eeMetadata.setSenderKey(QByteArray::fromBase64(QByteArrayLiteral("aFABnX7Q/rbTgjBySYzrT2FsYCVYb49mbca5yB734KQ=")));
stanza.setE2eeMetadata(e2eeMetadata);
QCOMPARE(stanza.e2eeMetadata()->senderKey(), QByteArray::fromBase64(QByteArrayLiteral("aFABnX7Q/rbTgjBySYzrT2FsYCVYb49mbca5yB734KQ=")));
}
void tst_QXmppStanza::testSceTimestamp()
{
QXmppStanzaStub stanza;
QVERIFY(!stanza.e2eeMetadata().has_value());
QXmppE2eeMetadata e2eeMetadata;
QVERIFY(e2eeMetadata.senderKey().isNull());
QVERIFY(e2eeMetadata.sceTimestamp().isNull());
e2eeMetadata.setSceTimestamp(QDateTime(QDate(2022, 01, 01), QTime()));
stanza.setE2eeMetadata(e2eeMetadata);
QCOMPARE(stanza.e2eeMetadata()->sceTimestamp(), QDateTime(QDate(2022, 01, 01), QTime()));
}
QTEST_MAIN(tst_QXmppStanza)
#include "tst_qxmppstanza.moc"
|