1 /****************************************************************************
3 ** Copyright (C) 2013 Digia Plc and/or its subsidiary(-ies).
4 ** Contact: http://www.qt-project.org/legal
6 ** This file is part of the test suite of the Qt Toolkit.
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.
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.
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.
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.
40 ****************************************************************************/
43 #include <QtWebSockets/QWebSocket>
44 #include <QtWebSockets/qwebsocketprotocol.h>
48 Q_DECLARE_METATYPE(QWebSocketProtocol::Version)
50 class tst_QWebSocket : public QObject
60 void cleanupTestCase();
61 void tst_initialisation_data();
62 void tst_initialisation();
63 void tst_settersAndGetters();
64 void tst_invalidOpen();
67 tst_QWebSocket::tst_QWebSocket()
71 void tst_QWebSocket::init()
73 qRegisterMetaType<QWebSocketProtocol::Version>("QWebSocketProtocol::Version");
76 void tst_QWebSocket::initTestCase()
80 void tst_QWebSocket::cleanupTestCase()
84 void tst_QWebSocket::tst_initialisation_data()
86 QTest::addColumn<QString>("origin");
87 QTest::addColumn<QString>("expectedOrigin");
88 QTest::addColumn<QWebSocketProtocol::Version>("version");
89 QTest::addColumn<QWebSocketProtocol::Version>("expectedVersion");
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;
102 void tst_QWebSocket::tst_initialisation()
104 QFETCH(QString, origin);
105 QFETCH(QString, expectedOrigin);
106 QFETCH(QWebSocketProtocol::Version, version);
107 QFETCH(QWebSocketProtocol::Version, expectedVersion);
109 QScopedPointer<QWebSocket> socket;
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));
116 socket.reset(new QWebSocket(origin, version));
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);
141 void tst_QWebSocket::tst_settersAndGetters()
145 socket.setPauseMode(QAbstractSocket::PauseNever);
146 QCOMPARE(socket.pauseMode(), QAbstractSocket::PauseNever);
147 socket.setPauseMode(QAbstractSocket::PauseOnSslErrors);
148 QCOMPARE(socket.pauseMode(), QAbstractSocket::PauseOnSslErrors);
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);
158 void tst_QWebSocket::tst_invalidOpen()
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)));
174 socket.open(QUrl(QStringLiteral("ws://127.0.0.1:1/")), true);
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);
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);
224 QTEST_MAIN(tst_QWebSocket)
226 #include "tst_qwebsocket.moc"