summaryrefslogtreecommitdiffstats
path: root/src/libcommuni/tests/auto/ircnetwork/tst_ircnetwork.cpp
blob: 5237659322e3062fa98e85ae7f9637d928e57916 (plain)
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
/*
 * Copyright (C) 2008-2014 The Communi Project
 *
 * This test is free, and not covered by the BSD license. There is no
 * restriction applied to their modification, redistribution, using and so on.
 * You can study them, modify them, use them in your own program - either
 * completely or partially.
 */

#include "ircnetwork.h"
#include "irccommand.h"
#include "ircconnection.h"
#include <QtTest/QtTest>
#include <QtCore/QRegExp>
#include "tst_ircclientserver.h"
#include "tst_ircdata.h"
#ifdef Q_OS_LINUX
#include "ircnetwork_p.h"
#endif // Q_OS_LINUX

class tst_IrcNetwork : public tst_IrcClientServer
{
    Q_OBJECT

private slots:
    void testDefaults();

    void testInfo_data();
    void testInfo();

    void testCapabilities_data();
    void testCapabilities();

    void testDebug();
};

void tst_IrcNetwork::testDefaults()
{
    IrcConnection connection;
    IrcNetwork* network = connection.network();
    QVERIFY(!network->isInitialized());
    QVERIFY(network->name().isNull());
    QVERIFY(network->modes().isEmpty());
    QVERIFY(network->prefixes().isEmpty());
    QVERIFY(network->channelTypes().isEmpty());
    QVERIFY(network->availableCapabilities().isEmpty());
    QVERIFY(network->requestedCapabilities().isEmpty());
    QVERIFY(network->activeCapabilities().isEmpty());
}

void tst_IrcNetwork::testInfo_data()
{
    QTest::addColumn<QByteArray>("welcome");
    QTest::addColumn<QString>("name");
    QTest::addColumn<QString>("modes");
    QTest::addColumn<QString>("prefixes");
    QTest::addColumn<QString>("channelTypes");

    QTest::newRow("freenode") << tst_IrcData::welcome("freenode") << "freenode" << "ov" << "@+" << "#";
    QTest::newRow("ircnet") << tst_IrcData::welcome("ircnet") << "IRCNet" << "ov" << "@+" << "#&!+";
    QTest::newRow("euirc") << tst_IrcData::welcome("euirc") << "euIRCnet" << "qaohv" << "*!@%+" << "#&+";
}

void tst_IrcNetwork::testInfo()
{
    QFETCH(QByteArray, welcome);
    QFETCH(QString, name);
    QFETCH(QString, modes);
    QFETCH(QString, prefixes);
    QFETCH(QString, channelTypes);

    IrcNetwork* network = connection->network();

    QSignalSpy initSpy(network, SIGNAL(initialized()));
    QSignalSpy nameSpy(network, SIGNAL(nameChanged(QString)));
    QSignalSpy modesSpy(network, SIGNAL(modesChanged(QStringList)));
    QSignalSpy prefixesSpy(network, SIGNAL(prefixesChanged(QStringList)));
    QSignalSpy channelTypesSpy(network, SIGNAL(channelTypesChanged(QStringList)));

    QVERIFY(initSpy.isValid());
    QVERIFY(nameSpy.isValid());
    QVERIFY(modesSpy.isValid());
    QVERIFY(prefixesSpy.isValid());
    QVERIFY(channelTypesSpy.isValid());

    connection->open();
    QVERIFY(waitForOpened());
    QVERIFY(waitForWritten(welcome));

    QVERIFY(network->isInitialized());

    QCOMPARE(network->name(), name);
    QCOMPARE(network->modes(), modes.split("", QString::SkipEmptyParts));
    QCOMPARE(network->prefixes(), prefixes.split("", QString::SkipEmptyParts));
    QCOMPARE(network->channelTypes(), channelTypes.split("", QString::SkipEmptyParts));

    QCOMPARE(network->prefixes().count(), network->modes().count());
    for (int i = 0; i < network->prefixes().count(); ++i) {
        QString prefix = network->prefixes().at(i);
        QString mode = network->modes().at(i);
        QCOMPARE(network->prefixToMode(prefix), mode);
        QCOMPARE(network->modeToPrefix(mode), prefix);
    }

    QVERIFY(!network->channelTypes().isEmpty());
    QVERIFY(!network->isChannel("foo"));
    QVERIFY(network->isChannel(network->channelTypes().at(0) + "foo"));

    QVERIFY(!network->channelModes(IrcNetwork::TypeA).isEmpty());
    QVERIFY(!network->channelModes(IrcNetwork::TypeB).isEmpty());
    QVERIFY(!network->channelModes(IrcNetwork::TypeC).isEmpty());
    QVERIFY(!network->channelModes(IrcNetwork::TypeD).isEmpty());

    if (welcome.contains("NICKLEN="))
        QVERIFY(network->numericLimit(IrcNetwork::NickLength) != -1);
    else
        QCOMPARE(network->numericLimit(IrcNetwork::NickLength), -1);
    if (welcome.contains("CHANNELLEN="))
        QVERIFY(network->numericLimit(IrcNetwork::ChannelLength) != -1);
    else
        QCOMPARE(network->numericLimit(IrcNetwork::ChannelLength), -1);
    if (welcome.contains("TOPICLEN="))
        QVERIFY(network->numericLimit(IrcNetwork::TopicLength) != -1);
    else
        QCOMPARE(network->numericLimit(IrcNetwork::TopicLength), -1);
    if (welcome.contains("KICKLEN="))
        QVERIFY(network->numericLimit(IrcNetwork::KickReasonLength) != -1);
    else
        QCOMPARE(network->numericLimit(IrcNetwork::KickReasonLength), -1);
    if (welcome.contains("AWAYLEN="))
        QVERIFY(network->numericLimit(IrcNetwork::AwayReasonLength) != -1);
    else
        QCOMPARE(network->numericLimit(IrcNetwork::AwayReasonLength), -1);
    if (welcome.contains("MODES="))
        QVERIFY(network->numericLimit(IrcNetwork::ModeCount) != -1);
    else
        QCOMPARE(network->numericLimit(IrcNetwork::ModeCount), -1);

    QCOMPARE(network->numericLimit(IrcNetwork::MessageLength), 512); // hard-coded :/

    if (welcome.contains("MAXLIST=")) {
        bool limited = false;
        foreach (const QString& mode, network->modes())
        if (network->modeLimit(mode) != -1)
            limited = true;
        QVERIFY(limited);
    }

    if (welcome.contains("CHANLIMIT=")) {
        bool limited = false;
        foreach (const QString& type, network->channelTypes())
            if (network->channelLimit(type) != -1)
                limited = true;
        QVERIFY(limited);
    }

    if (welcome.contains("TARGMAX=")) {
        bool limited = false;
        IrcCommand command;
        for (int i = IrcCommand::Admin; i <= IrcCommand::Whowas; ++i) {
            if (i != IrcCommand::Custom) {
                command.setType(static_cast<IrcCommand::Type>(i));
                QString cmd = command.toString().split(" ", QString::SkipEmptyParts).value(0);
                if (network->targetLimit(cmd) != -1)
                    limited = true;
            }
        }
        QVERIFY(limited);
    }

    QCOMPARE(initSpy.count(), 1);
    QCOMPARE(nameSpy.count(), 1);
    QCOMPARE(modesSpy.count(), 1);
    QCOMPARE(prefixesSpy.count(), 1);
    QCOMPARE(channelTypesSpy.count(), 1);

    QCOMPARE(nameSpy.first().first().toString(), name);
    QCOMPARE(modesSpy.first().first().toStringList(), modes.split("", QString::SkipEmptyParts));
    QCOMPARE(prefixesSpy.first().first().toStringList(), prefixes.split("", QString::SkipEmptyParts));
    QCOMPARE(channelTypesSpy.first().first().toStringList(), channelTypes.split("", QString::SkipEmptyParts));
}

void tst_IrcNetwork::testCapabilities_data()
{
    QTest::addColumn<QString>("initialCaps");
    QTest::addColumn<QString>("requestedCaps");
    QTest::addColumn<QString>("ackedCaps");
    QTest::addColumn<QString>("nakedCaps");
    QTest::addColumn<QString>("listedCaps");
    QTest::addColumn<QString>("availableCaps");
    QTest::addColumn<QString>("activeCaps");

    QTest::newRow("empty")     << QString()
                               << QString()
                               << QString()
                               << QString()
                               << QString()
                               << QString()
                               << QString();

    QTest::newRow("sasl")      << QString("multi-prefix sasl identify-msg") // initial
                               << QString("sasl") // requested
                               << QString("sasl") // acked
                               << QString() // naked
                               << QString("sasl identify-msg multi-prefix") // listed
                               << QString("sasl identify-msg multi-prefix") // available
                               << QString("sasl"); // active

    QTest::newRow("unk")       << QString("multi-prefix sasl identify-msg") // initial
                               << QString("unk") // requested
                               << QString() // acked
                               << QString("nak") // naked
                               << QString("multi-prefix sasl identify-msg") // listed
                               << QString("multi-prefix sasl identify-msg") // available
                               << QString(); // active

    QTest::newRow("nak all")   << QString("multi-prefix sasl identify-msg") // initial
                               << QString("sasl identify-msg multi-prefix") // requested
                               << QString() // acked
                               << QString("sasl identify-msg multi-prefix") // naked
                               << QString("sasl identify-msg multi-prefix") // listed
                               << QString("sasl identify-msg multi-prefix") // available
                               << QString(); // active

    QTest::newRow("sticky")    << QString("=sticky") // initial
                               << QString("sticky") // requested
                               << QString("=sticky") // acked
                               << QString() // naked
                               << QString() // listed
                               << QString("sticky") // available
                               << QString("sticky"); // active

    QTest::newRow("ackmod")    << QString("~ackmod") // initial
                               << QString("ackmod") // requested
                               << QString("~ackmod") // acked
                               << QString() // naked
                               << QString() // listed
                               << QString("ackmod") // available
                               << QString("ackmod"); // active

    QTest::newRow("acksticky") << QString("~=acksticky") // initial
                               << QString("acksticky") // requested
                               << QString("=~acksticky") // acked
                               << QString() // naked
                               << QString() // listed
                               << QString("acksticky") // available
                               << QString("acksticky"); // active
}

static bool equalCaps(const QString& left, const QString& right)
{
    return left.split(" ", QString::SkipEmptyParts).toSet() == right.split(" ", QString::SkipEmptyParts).toSet();
}

void tst_IrcNetwork::testCapabilities()
{
    QFETCH(QString, initialCaps);
    QFETCH(QString, requestedCaps);
    QFETCH(QString, ackedCaps);
    QFETCH(QString, nakedCaps);
    QFETCH(QString, listedCaps);
    QFETCH(QString, availableCaps);
    QFETCH(QString, activeCaps);

    IrcNetwork* network = connection->network();

    QSignalSpy availableSpy(network, SIGNAL(availableCapabilitiesChanged(QStringList)));
    QSignalSpy requestedSpy(network, SIGNAL(requestedCapabilitiesChanged(QStringList)));
    QSignalSpy activeSpy(network, SIGNAL(activeCapabilitiesChanged(QStringList)));
    QSignalSpy requestingSpy(network, SIGNAL(requestingCapabilities()));

    QVERIFY(availableSpy.isValid());
    QVERIFY(requestedSpy.isValid());
    QVERIFY(activeSpy.isValid());
    QVERIFY(requestingSpy.isValid());

    int availableCount = 0;
    int requestedCount = 0;
    int activeCount = 0;
    int requestingCount = 0;

    if (!requestedCaps.isEmpty()) {
        network->setRequestedCapabilities(requestedCaps.split(" ", QString::SkipEmptyParts));
        ++requestedCount;
    }
    QCOMPARE(requestedSpy.count(), requestedCount);

    foreach (const QString& cap, availableCaps.split(" ", QString::SkipEmptyParts))
        QVERIFY(!network->hasCapability(cap));
    foreach (const QString& cap, activeCaps.split(" ", QString::SkipEmptyParts))
        QVERIFY(!network->isCapable(cap));

    connection->open();
    QVERIFY(waitForOpened());

    if (!initialCaps.isEmpty()) {
        // typical ircd: "*", znc: "unknown-nick"
        QVERIFY(waitForWritten(":irc.ser.ver CAP unknown-nick LS :" + initialCaps.toUtf8()));
        ++availableCount;
        ++requestingCount;
    }
    QCOMPARE(availableSpy.count(), availableCount);
    QCOMPARE(requestingSpy.count(), requestingCount);

    foreach (const QString& cap, availableCaps.split(" ", QString::SkipEmptyParts))
        QVERIFY(network->hasCapability(cap));
    foreach (const QString& cap, activeCaps.split(" ", QString::SkipEmptyParts))
        QVERIFY(!network->isCapable(cap));

    if (!ackedCaps.isEmpty()) {
        QVERIFY(waitForWritten(":irc.ser.ver CAP jpnurmi ACK :" + ackedCaps.toUtf8()));
        ++activeCount;
    }
    QCOMPARE(activeSpy.count(), activeCount);

    foreach (const QString& cap, availableCaps.split(" ", QString::SkipEmptyParts))
        QVERIFY(network->hasCapability(cap));
    foreach (const QString& cap, activeCaps.split(" ", QString::SkipEmptyParts))
        QVERIFY(network->isCapable(cap));

    if (!nakedCaps.isEmpty())
        QVERIFY(waitForWritten(":irc.ser.ver CAP jpnurmi NAK :" + nakedCaps.toUtf8()));

    QVERIFY(waitForWritten(tst_IrcData::welcome()));

    if (!listedCaps.isEmpty()) {
        QVERIFY(waitForWritten(":irc.ser.ver CAP jpnurmi LS :" + listedCaps.toUtf8()));
        if (!equalCaps(listedCaps, initialCaps))
            ++availableCount;
    }
    QCOMPARE(availableSpy.count(), availableCount);

    QVERIFY(equalCaps(network->availableCapabilities().join(" "), availableCaps));
    QVERIFY(equalCaps(network->activeCapabilities().join(" "), activeCaps));
    QVERIFY(equalCaps(network->requestedCapabilities().join(" "), requestedCaps));

    QCOMPARE(requestedSpy.count(), requestedCount);
    QCOMPARE(requestingSpy.count(), requestingCount);
    QCOMPARE(availableSpy.count(), availableCount);
    QCOMPARE(activeSpy.count(), activeCount);

    foreach (const QString& cap, availableCaps.split(" ", QString::SkipEmptyParts))
        QVERIFY(network->hasCapability(cap));
    foreach (const QString& cap, activeCaps.split(" ", QString::SkipEmptyParts))
        QVERIFY(network->isCapable(cap));

    // -> CLEAR
    QString clearCaps;
    foreach (const QString& cap, activeCaps.split(" ", QString::SkipEmptyParts))
        clearCaps += QString("-") + cap;
    QVERIFY(waitForWritten(":irc.ser.ver CAP jpnurmi ACK :" + clearCaps.toUtf8()));

    if (!activeCaps.isEmpty())
        ++activeCount;
    QCOMPARE(activeSpy.count(), activeCount);
}

void tst_IrcNetwork::testDebug()
{
    QString str;
    QDebug dbg(&str);

    dbg << static_cast<IrcNetwork*>(0);
    QCOMPARE(str.trimmed(), QString::fromLatin1("IrcNetwork(0x0)"));
    str.clear();

    IrcConnection connection;
    dbg << connection.network();
    QVERIFY(QRegExp("IrcNetwork\\(0x[0-9A-Fa-f]+\\) ").exactMatch(str));
    str.clear();

    connection.network()->setObjectName("obj");
    dbg << connection.network();
    QVERIFY(QRegExp("IrcNetwork\\(0x[0-9A-Fa-f]+, name=obj\\) ").exactMatch(str));
    str.clear();

#ifdef Q_OS_LINUX
    // others have problems with symbols (win) or private headers (osx frameworks)
    IrcNetworkPrivate::get(connection.network())->name = "net";
    dbg << connection.network();
    QVERIFY(QRegExp("IrcNetwork\\(0x[0-9A-Fa-f]+, name=obj, network=net\\) ").exactMatch(str));
    str.clear();
#endif // Q_OS_LINUX

    dbg << IrcNetwork::MessageLength;
    QCOMPARE(str.trimmed(), QString::fromLatin1("MessageLength"));
    str.clear();

    dbg << IrcNetwork::AllTypes;
    QCOMPARE(str.trimmed(), QString::fromLatin1("AllTypes"));
    str.clear();

    dbg << (IrcNetwork::TypeA | IrcNetwork::TypeD);
    QCOMPARE(str.trimmed(), QString::fromLatin1("(TypeA|TypeD)"));
    str.clear();

    dbg << (IrcNetwork::TypeB | IrcNetwork::TypeC);
    QCOMPARE(str.trimmed(), QString::fromLatin1("(TypeB|TypeC)"));
    str.clear();

    dbg << (IrcNetwork::TypeA | IrcNetwork::TypeB | IrcNetwork::TypeC | IrcNetwork::TypeD);
    QCOMPARE(str.trimmed(), QString::fromLatin1("(AllTypes)"));
    str.clear();
}

QTEST_MAIN(tst_IrcNetwork)

#include "tst_ircnetwork.moc"