summaryrefslogtreecommitdiffstats
path: root/src/libcommuni/tests/auto/shared/tst_ircclientserver.cpp
blob: 2a76ead55e20d97d29cfc9f5f13c5897f6c2ccf0 (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
/*
 * 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 "tst_ircclientserver.h"

tst_IrcClientServer::tst_IrcClientServer()
{
    server = new QTcpServer(this);
}

void tst_IrcClientServer::initTestCase()
{
    QVERIFY(server->listen());
}

void tst_IrcClientServer::cleanupTestCase()
{
    server->close();
}

void tst_IrcClientServer::init()
{
    connection = new IrcConnection(this);
    connection->setUserName("user");
    connection->setNickName("nick");
    connection->setRealName("real");
    connection->setPassword("secret");
    connection->setHost("127.0.0.1");
    connection->setPort(server->serverPort());
}

void tst_IrcClientServer::cleanup()
{
    delete connection;
}

bool tst_IrcClientServer::waitForOpened(int timeout)
{
    if (!server->waitForNewConnection(timeout))
        return false;
    serverSocket = server->nextPendingConnection();
    clientSocket = connection->socket();
    return serverSocket && clientSocket && clientSocket->waitForConnected(1000);
}

bool tst_IrcClientServer::waitForWritten(const QByteArray& data, int timeout)
{
    if (!data.isNull()) {
        if (data.count('\n') > 1) {
            bool success = true;
            foreach (const QByteArray& line, data.split('\n'))
                success &= waitForWritten(line + '\n', timeout);
            return success;
        }
        if (data.endsWith('\r') || data.endsWith('\n'))
            serverSocket->write(data);
        else
            serverSocket->write(data + "\r\n");
    }
    return serverSocket->waitForBytesWritten(timeout) && clientSocket->waitForReadyRead(timeout);
}