summaryrefslogtreecommitdiff
path: root/tests/tst_websockets.cpp
blob: f330a8809426c6c7c5c6efddad84779b2cf1e2ea (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
#include <QtTest/QtTest>
#include <QtTest/qtestcase.h>
#include <QSignalSpy>
#include <QHostInfo>
#include <QDebug>
#include "qwebsocket.h"
#include "unittests.h"

class WebSocketsTest : public QObject
{
    Q_OBJECT

public:
    WebSocketsTest();

private Q_SLOTS:
    void initTestCase();
    void cleanupTestCase();
    void init();
    void cleanup();

    /**
      * @brief Test isValid() with an unoped socket
      */
    void testInvalidWithUnopenedSocket();

    /**
     * @brief testTextMessage Tests sending and receiving a text message
     */
    void testTextMessage();

    void testBinaryMessage();

    /**
     * @brief Tests the method localAddress and localPort
     */
    void testLocalAddress();

    /**
     * @brief Test the methods peerAddress, peerName and peerPort
     */
    void testPeerAddress();

    /**
     * @brief Test the methods proxy() and setProxy() and check if it can be correctly set
     */
    void testProxy();

    /**
     * @brief Runs the autobahn tests against our implementation
     */
    //void autobahnTest();

private:
    QWebSocket *m_pWebSocket;
    QUrl m_url;
};

WebSocketsTest::WebSocketsTest() :
    m_pWebSocket(0),
    m_url("ws://localhost:9000")
{
}

void WebSocketsTest::initTestCase()
{
    m_pWebSocket = new QWebSocket();
    /*m_pWebSocket->open(m_url, true);
    QTRY_VERIFY_WITH_TIMEOUT(m_pWebSocket->state() == QAbstractSocket::ConnectedState, 1000);
    QVERIFY(m_pWebSocket->isValid());*/
}

void WebSocketsTest::cleanupTestCase()
{
    if (m_pWebSocket)
    {
        m_pWebSocket->close();
        //QVERIFY2(m_pWebSocket->waitForDisconnected(1000), "Disconnection failed.");
        delete m_pWebSocket;
        m_pWebSocket = 0;
    }
}

void WebSocketsTest::init()
{
}

void WebSocketsTest::cleanup()
{
}

void WebSocketsTest::testTextMessage()
{
    const char *message = "Hello world!";

    QSignalSpy spy(m_pWebSocket, SIGNAL(textMessageReceived(QString)));

    QCOMPARE(m_pWebSocket->write(message), (qint64)strlen(message));

    QTRY_VERIFY_WITH_TIMEOUT(spy.count() != 0, 1000);
    QCOMPARE(spy.count(), 1);
    QCOMPARE(spy.at(0).count(), 1);
    QCOMPARE(spy.takeFirst().at(0).toString(), QString(message));

    spy.clear();
    QString qMessage(message);
    QCOMPARE(m_pWebSocket->write(qMessage), (qint64)qMessage.length());
    QTRY_VERIFY_WITH_TIMEOUT(spy.count() != 0, 1000);
    QCOMPARE(spy.count(), 1);
    QCOMPARE(spy.at(0).count(), 1);
    QCOMPARE(spy.takeFirst().at(0).toString(), qMessage);
}

void WebSocketsTest::testBinaryMessage()
{
    QSignalSpy spy(m_pWebSocket, SIGNAL(binaryMessageReceived(QByteArray)));

    QByteArray data("Hello world!");

    QCOMPARE(m_pWebSocket->write(data), (qint64)data.size());

    QTRY_VERIFY_WITH_TIMEOUT(spy.count() != 0, 1000);
    QCOMPARE(spy.count(), 1);
    QCOMPARE(spy.at(0).count(), 1);
    QCOMPARE(spy.takeFirst().at(0).toByteArray(), data);
}

void WebSocketsTest::testLocalAddress()
{
    QCOMPARE(m_pWebSocket->localAddress().toString(), QString("127.0.0.1"));
    quint16 localPort = m_pWebSocket->localPort();
    QVERIFY2(localPort > 0, "Local port is invalid.");
}

void WebSocketsTest::testPeerAddress()
{
    QHostInfo hostInfo = QHostInfo::fromName(m_url.host());
    QList<QHostAddress> addresses = hostInfo.addresses();
    QVERIFY(addresses.length() > 0);
    QHostAddress peer = m_pWebSocket->peerAddress();
    bool found = false;
    Q_FOREACH(QHostAddress a, addresses)
    {
        if (a == peer)
        {
            found = true;
            break;
        }
    }

    if (!found)
    {
        QFAIL("PeerAddress is not found as a result of a reverse lookup");
    }
    QCOMPARE(m_pWebSocket->peerName(), m_url.host());
    QCOMPARE(m_pWebSocket->peerPort(), (quint16)m_url.port(80));
}

void WebSocketsTest::testProxy()
{
    QNetworkProxy oldProxy = m_pWebSocket->proxy();
    QNetworkProxy proxy(QNetworkProxy::HttpProxy, QString("proxy.network.com"), 80);
    m_pWebSocket->setProxy(proxy);
    QCOMPARE(proxy, m_pWebSocket->proxy());
    m_pWebSocket->setProxy(oldProxy);
    QCOMPARE(oldProxy, m_pWebSocket->proxy());
}

void WebSocketsTest::testInvalidWithUnopenedSocket()
{
    QWebSocket qws;
    QCOMPARE(qws.isValid(), false);
}

//DECLARE_TEST(WebSocketsTest)

#include "tst_websockets.moc"