d1ad0a9af8d2ce0ac806c8b38307e904a27375ea
[contrib/qtwebsockets.git] / tests / auto / qwebsocket / tst_qwebsocket.cpp
1 /****************************************************************************
2 **
3 ** Copyright (C) 2013 Digia Plc and/or its subsidiary(-ies).
4 ** Contact: http://www.qt-project.org/legal
5 **
6 ** This file is part of the test suite of the Qt Toolkit.
7 **
8 ** $QT_BEGIN_LICENSE:LGPL$
9 ** Commercial License Usage
10 ** Licensees holding valid commercial Qt licenses may use this file in
11 ** accordance with the commercial license agreement provided with the
12 ** Software or, alternatively, in accordance with the terms contained in
13 ** a written agreement between you and Digia.  For licensing terms and
14 ** conditions see http://qt.digia.com/licensing.  For further information
15 ** use the contact form at http://qt.digia.com/contact-us.
16 **
17 ** GNU Lesser General Public License Usage
18 ** Alternatively, this file may be used under the terms of the GNU Lesser
19 ** General Public License version 2.1 as published by the Free Software
20 ** Foundation and appearing in the file LICENSE.LGPL included in the
21 ** packaging of this file.  Please review the following information to
22 ** ensure the GNU Lesser General Public License version 2.1 requirements
23 ** will be met: http://www.gnu.org/licenses/old-licenses/lgpl-2.1.html.
24 **
25 ** In addition, as a special exception, Digia gives you certain additional
26 ** rights.  These rights are described in the Digia Qt LGPL Exception
27 ** version 1.1, included in the file LGPL_EXCEPTION.txt in this package.
28 **
29 ** GNU General Public License Usage
30 ** Alternatively, this file may be used under the terms of the GNU
31 ** General Public License version 3.0 as published by the Free Software
32 ** Foundation and appearing in the file LICENSE.GPL included in the
33 ** packaging of this file.  Please review the following information to
34 ** ensure the GNU General Public License version 3.0 requirements will be
35 ** met: http://www.gnu.org/copyleft/gpl.html.
36 **
37 **
38 ** $QT_END_LICENSE$
39 **
40 ****************************************************************************/
41 #include <QString>
42 #include <QtTest>
43 #include <QtWebSockets/QWebSocket>
44 #include <QtWebSockets/qwebsocketprotocol.h>
45
46 QT_USE_NAMESPACE
47
48 Q_DECLARE_METATYPE(QWebSocketProtocol::Version)
49
50 class tst_QWebSocket : public QObject
51 {
52     Q_OBJECT
53
54 public:
55     tst_QWebSocket();
56
57 private Q_SLOTS:
58     void init();
59     void initTestCase();
60     void cleanupTestCase();
61     void tst_initialisation_data();
62     void tst_initialisation();
63     void tst_settersAndGetters();
64     void tst_invalidOpen();
65 };
66
67 tst_QWebSocket::tst_QWebSocket()
68 {
69 }
70
71 void tst_QWebSocket::init()
72 {
73     qRegisterMetaType<QWebSocketProtocol::Version>("QWebSocketProtocol::Version");
74 }
75
76 void tst_QWebSocket::initTestCase()
77 {
78 }
79
80 void tst_QWebSocket::cleanupTestCase()
81 {
82 }
83
84 void tst_QWebSocket::tst_initialisation_data()
85 {
86     QTest::addColumn<QString>("origin");
87     QTest::addColumn<QString>("expectedOrigin");
88     QTest::addColumn<QWebSocketProtocol::Version>("version");
89     QTest::addColumn<QWebSocketProtocol::Version>("expectedVersion");
90
91     QTest::newRow("Default origin and version")
92             << QString() << QString()
93             << QWebSocketProtocol::VersionUnknown << QWebSocketProtocol::VersionLatest;
94     QTest::newRow("Specific origin and default version")
95             << QStringLiteral("qt-project.org") << QStringLiteral("qt-project.org")
96             << QWebSocketProtocol::VersionUnknown << QWebSocketProtocol::VersionLatest;
97     QTest::newRow("Specific origin and specific version")
98             << QStringLiteral("qt-project.org") << QStringLiteral("qt-project.org")
99             << QWebSocketProtocol::Version7 << QWebSocketProtocol::Version7;
100 }
101
102 void tst_QWebSocket::tst_initialisation()
103 {
104     QFETCH(QString, origin);
105     QFETCH(QString, expectedOrigin);
106     QFETCH(QWebSocketProtocol::Version, version);
107     QFETCH(QWebSocketProtocol::Version, expectedVersion);
108
109     QScopedPointer<QWebSocket> socket;
110
111     if (origin.isEmpty() && (version == QWebSocketProtocol::VersionUnknown))
112         socket.reset(new QWebSocket);
113     else if (!origin.isEmpty() && (version == QWebSocketProtocol::VersionUnknown))
114         socket.reset(new QWebSocket(origin));
115     else
116         socket.reset(new QWebSocket(origin, version));
117
118     QCOMPARE(socket->origin(), expectedOrigin);
119     QCOMPARE(socket->version(), expectedVersion);
120     QCOMPARE(socket->error(), QAbstractSocket::UnknownSocketError);
121     //error string defaults to "Unknown error" (localised)
122     QVERIFY(!socket->errorString().isEmpty());
123     QVERIFY(!socket->isValid());
124     QVERIFY(socket->localAddress().isNull());
125     QCOMPARE(socket->localPort(), quint16(0));
126     QCOMPARE(socket->pauseMode(), QAbstractSocket::PauseNever);
127     QVERIFY(socket->peerAddress().isNull());
128     QCOMPARE(socket->peerPort(), quint16(0));
129     QVERIFY(socket->peerName().isEmpty());
130     QCOMPARE(socket->state(), QAbstractSocket::UnconnectedState);
131     QCOMPARE(socket->readBufferSize(), 0);
132     QVERIFY(socket->resourceName().isEmpty());
133     QVERIFY(!socket->requestUrl().isValid());
134     QCOMPARE(socket->closeCode(), QWebSocketProtocol::CloseCodeNormal);
135     QVERIFY(socket->closeReason().isEmpty());
136     QVERIFY(socket->flush());
137     QCOMPARE(socket->sendTextMessage(QStringLiteral("A text message")), 0);
138     QCOMPARE(socket->sendBinaryMessage(QByteArrayLiteral("A binary message")), 0);
139 }
140
141 void tst_QWebSocket::tst_settersAndGetters()
142 {
143     QWebSocket socket;
144
145     socket.setPauseMode(QAbstractSocket::PauseNever);
146     QCOMPARE(socket.pauseMode(), QAbstractSocket::PauseNever);
147     socket.setPauseMode(QAbstractSocket::PauseOnSslErrors);
148     QCOMPARE(socket.pauseMode(), QAbstractSocket::PauseOnSslErrors);
149
150     socket.setReadBufferSize(0);
151     QCOMPARE(socket.readBufferSize(), 0);
152     socket.setReadBufferSize(128);
153     QCOMPARE(socket.readBufferSize(), 128);
154     socket.setReadBufferSize(-1);
155     QCOMPARE(socket.readBufferSize(), -1);
156 }
157
158 void tst_QWebSocket::tst_invalidOpen()
159 {
160     QWebSocket socket;
161     QSignalSpy errorSpy(&socket, SIGNAL(error(QAbstractSocket::SocketError)));
162     QSignalSpy aboutToCloseSpy(&socket, SIGNAL(aboutToClose()));
163     QSignalSpy connectedSpy(&socket, SIGNAL(connected()));
164     QSignalSpy disconnectedSpy(&socket, SIGNAL(disconnected()));
165     QSignalSpy stateChangedSpy(&socket, SIGNAL(stateChanged(QAbstractSocket::SocketState)));
166     QSignalSpy readChannelFinishedSpy(&socket, SIGNAL(readChannelFinished()));
167     QSignalSpy textFrameReceivedSpy(&socket, SIGNAL(textFrameReceived(QString,bool)));
168     QSignalSpy binaryFrameReceivedSpy(&socket, SIGNAL(binaryFrameReceived(QByteArray,bool)));
169     QSignalSpy textMessageReceivedSpy(&socket, SIGNAL(textMessageReceived(QString)));
170     QSignalSpy binaryMessageReceivedSpy(&socket, SIGNAL(binaryMessageReceived(QByteArray)));
171     QSignalSpy pongSpy(&socket, SIGNAL(pong(quint64,QByteArray)));
172     QSignalSpy bytesWrittenSpy(&socket, SIGNAL(bytesWritten(qint64)));
173
174     socket.open(QUrl(QStringLiteral("ws://127.0.0.1:1/")), true);
175
176     QVERIFY(socket.origin().isEmpty());
177     QCOMPARE(socket.version(), QWebSocketProtocol::VersionLatest);
178     //at this point the socket is in a connecting state
179     //so, there should no error at this point
180     QCOMPARE(socket.error(), QAbstractSocket::UnknownSocketError);
181     QVERIFY(!socket.errorString().isEmpty());
182     QVERIFY(!socket.isValid());
183     QVERIFY(socket.localAddress().isNull());
184     QCOMPARE(socket.localPort(), quint16(0));
185     QCOMPARE(socket.pauseMode(), QAbstractSocket::PauseNever);
186     QVERIFY(socket.peerAddress().isNull());
187     QCOMPARE(socket.peerPort(), quint16(0));
188     QCOMPARE(socket.peerName(), QStringLiteral("127.0.0.1"));
189     QCOMPARE(socket.state(), QAbstractSocket::ConnectingState);
190     QCOMPARE(socket.readBufferSize(), 0);
191     QCOMPARE(socket.resourceName(), QStringLiteral("/"));
192     QCOMPARE(socket.requestUrl(), QUrl(QStringLiteral("ws://127.0.0.1:1/")));
193     QCOMPARE(socket.closeCode(), QWebSocketProtocol::CloseCodeNormal);
194     QVERIFY(socket.closeReason().isEmpty());
195     QVERIFY(!socket.flush());   //flush should fail if socket is in connecting state
196     QCOMPARE(socket.sendTextMessage(QStringLiteral("A text message")), 0);
197     QCOMPARE(socket.sendBinaryMessage(QByteArrayLiteral("A text message")), 0);
198
199     QVERIFY(errorSpy.wait());
200     QCOMPARE(errorSpy.count(), 1);
201     QList<QVariant> arguments = errorSpy.takeFirst();
202     QAbstractSocket::SocketError socketError =
203             qvariant_cast<QAbstractSocket::SocketError>(arguments.at(0));
204     QCOMPARE(socketError, QAbstractSocket::ConnectionRefusedError);
205     QCOMPARE(aboutToCloseSpy.count(), 0);
206     QCOMPARE(connectedSpy.count(), 0);
207     QCOMPARE(disconnectedSpy.count(), 1);
208     QCOMPARE(stateChangedSpy.count(), 2);   //connectingstate, unconnectedstate
209     arguments = stateChangedSpy.takeFirst();
210     QAbstractSocket::SocketState socketState =
211             qvariant_cast<QAbstractSocket::SocketState>(arguments.at(0));
212     arguments = stateChangedSpy.takeFirst();
213     socketState = qvariant_cast<QAbstractSocket::SocketState>(arguments.at(0));
214     QCOMPARE(socketState, QAbstractSocket::UnconnectedState);
215     QCOMPARE(readChannelFinishedSpy.count(), 0);
216     QCOMPARE(textFrameReceivedSpy.count(), 0);
217     QCOMPARE(binaryFrameReceivedSpy.count(), 0);
218     QCOMPARE(textMessageReceivedSpy.count(), 0);
219     QCOMPARE(binaryMessageReceivedSpy.count(), 0);
220     QCOMPARE(pongSpy.count(), 0);
221     QCOMPARE(bytesWrittenSpy.count(), 0);
222 }
223
224 QTEST_MAIN(tst_QWebSocket)
225
226 #include "tst_qwebsocket.moc"