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
|
// SPDX-FileCopyrightText: 2012 Jeremy Lainé <jeremy.laine@m4x.org>
// SPDX-FileCopyrightText: 2012 Manjeet Dahiya <manjeetdahiya@gmail.com>
//
// SPDX-License-Identifier: LGPL-2.1-or-later
#include "QXmppUtils.h"
#include "util.h"
#include <QObject>
class tst_QXmppUtils : public QObject
{
Q_OBJECT
private slots:
void testCrc32();
void testHmac();
void testJid();
void testMime();
void testTimezoneOffset();
void testStanzaHash();
};
void tst_QXmppUtils::testCrc32()
{
quint32 crc = QXmppUtils::generateCrc32(QByteArray());
QCOMPARE(crc, 0u);
crc = QXmppUtils::generateCrc32(QByteArray("Hi There"));
QCOMPARE(crc, 0xDB143BBEu);
}
void tst_QXmppUtils::testHmac()
{
QByteArray hmac = QXmppUtils::generateHmacMd5(QByteArray(16, '\x0b'), QByteArray("Hi There"));
QCOMPARE(hmac, QByteArray::fromHex("9294727a3638bb1c13f48ef8158bfc9d"));
hmac = QXmppUtils::generateHmacMd5(QByteArray("Jefe"), QByteArray("what do ya want for nothing?"));
QCOMPARE(hmac, QByteArray::fromHex("750c783e6ab0b503eaa86e310a5db738"));
hmac = QXmppUtils::generateHmacMd5(QByteArray(16, '\xaa'), QByteArray(50, '\xdd'));
QCOMPARE(hmac, QByteArray::fromHex("56be34521d144c88dbb8c733f0e8b3f6"));
}
void tst_QXmppUtils::testJid()
{
QCOMPARE(QXmppUtils::jidToBareJid("foo@example.com/resource"), QLatin1String("foo@example.com"));
QCOMPARE(QXmppUtils::jidToBareJid("foo@example.com"), QLatin1String("foo@example.com"));
QCOMPARE(QXmppUtils::jidToBareJid("example.com"), QLatin1String("example.com"));
QCOMPARE(QXmppUtils::jidToBareJid(QString()), QString());
QCOMPARE(QXmppUtils::jidToDomain("foo@example.com/resource"), QLatin1String("example.com"));
QCOMPARE(QXmppUtils::jidToDomain("foo@example.com"), QLatin1String("example.com"));
QCOMPARE(QXmppUtils::jidToDomain("example.com"), QLatin1String("example.com"));
QCOMPARE(QXmppUtils::jidToDomain(QString()), QString());
QCOMPARE(QXmppUtils::jidToResource("foo@example.com/resource"), QLatin1String("resource"));
QCOMPARE(QXmppUtils::jidToResource("foo@example.com"), QString());
QCOMPARE(QXmppUtils::jidToResource("example.com"), QString());
QCOMPARE(QXmppUtils::jidToResource(QString()), QString());
QCOMPARE(QXmppUtils::jidToUser("foo@example.com/resource"), QLatin1String("foo"));
QCOMPARE(QXmppUtils::jidToUser("foo@example.com"), QLatin1String("foo"));
QCOMPARE(QXmppUtils::jidToUser("example.com"), QString());
QCOMPARE(QXmppUtils::jidToUser(QString()), QString());
}
// FIXME: how should we test MIME detection without expose getImageType?
#if 0
QString getImageType(const QByteArray &contents);
static void testMimeType(const QString &fileName, const QString fileType)
{
// load file from resources
QFile file(":/" + fileName);
QCOMPARE(file.open(QIODevice::ReadOnly), true);
QCOMPARE(getImageType(file.readAll()), fileType);
file.close();
}
void tst_QXmppUtils::testMime()
{
testMimeType("test.bmp", "image/bmp");
testMimeType("test.gif", "image/gif");
testMimeType("test.jpg", "image/jpeg");
testMimeType("test.mng", "video/x-mng");
testMimeType("test.png", "image/png");
testMimeType("test.svg", "image/svg+xml");
testMimeType("test.xpm", "image/x-xpm");
}
#else
void tst_QXmppUtils::testMime()
{
}
#endif
void tst_QXmppUtils::testTimezoneOffset()
{
// parsing
QCOMPARE(QXmppUtils::timezoneOffsetFromString("Z"), 0);
QCOMPARE(QXmppUtils::timezoneOffsetFromString("+00:00"), 0);
QCOMPARE(QXmppUtils::timezoneOffsetFromString("-00:00"), 0);
QCOMPARE(QXmppUtils::timezoneOffsetFromString("+01:30"), 5400);
QCOMPARE(QXmppUtils::timezoneOffsetFromString("-01:30"), -5400);
// serialization
QCOMPARE(QXmppUtils::timezoneOffsetToString(0), QLatin1String("Z"));
QCOMPARE(QXmppUtils::timezoneOffsetToString(5400), QLatin1String("+01:30"));
QCOMPARE(QXmppUtils::timezoneOffsetToString(-5400), QLatin1String("-01:30"));
}
void tst_QXmppUtils::testStanzaHash()
{
for (int i = 0; i < 100; i++) {
const QString hash = QXmppUtils::generateStanzaHash(i);
QCOMPARE(hash.size(), i);
if (i == 36) {
QCOMPARE(hash.count('-'), 4);
}
}
const QString hash = QXmppUtils::generateStanzaUuid();
QCOMPARE(hash.size(), 36);
QCOMPARE(hash.count('-'), 4);
}
QTEST_MAIN(tst_QXmppUtils)
#include "tst_qxmpputils.moc"
|