summaryrefslogtreecommitdiff
path: root/tests/auto/websockets
diff options
context:
space:
mode:
authorLiang Qi <liang.qi@theqtcompany.com>2015-10-02 13:55:07 +0200
committerLiang Qi <liang.qi@theqtcompany.com>2015-10-02 13:55:07 +0200
commit6484afa4b7645b31e370f4eb8ea0b93fc3bf052a (patch)
tree9ff1eacd8dcb1943613785e3ad6df77bf38523bb /tests/auto/websockets
parentf11862a05591452bdad9c1abd244288a2964a036 (diff)
parent12e424f241b29ef26ad2a3a70740d8b320e9e85a (diff)
downloadqtwebsockets-6484afa4b7645b31e370f4eb8ea0b93fc3bf052a.tar.gz
Merge remote-tracking branch 'origin/5.5' into 5.6
Conflicts: tests/auto/websockets/qwebsocket/tst_qwebsocket.cpp Change-Id: Ie4184b0788b6a0a5a85ca8e4d1547c33f7027ca0
Diffstat (limited to 'tests/auto/websockets')
-rw-r--r--tests/auto/websockets/handshakerequest/tst_handshakerequest.cpp76
-rw-r--r--tests/auto/websockets/handshakeresponse/tst_handshakeresponse.cpp2
-rw-r--r--tests/auto/websockets/qwebsocket/tst_qwebsocket.cpp103
3 files changed, 176 insertions, 5 deletions
diff --git a/tests/auto/websockets/handshakerequest/tst_handshakerequest.cpp b/tests/auto/websockets/handshakerequest/tst_handshakerequest.cpp
index 9c579bd..2921e57 100644
--- a/tests/auto/websockets/handshakerequest/tst_handshakerequest.cpp
+++ b/tests/auto/websockets/handshakerequest/tst_handshakerequest.cpp
@@ -45,6 +45,9 @@ QT_USE_NAMESPACE
Q_DECLARE_METATYPE(QWebSocketProtocol::CloseCode)
Q_DECLARE_METATYPE(QWebSocketProtocol::OpCode)
+const int MAX_HEADERLINE_LENGTH = 8 * 1024;
+const int MAX_HEADERS = 100;
+
class tst_HandshakeRequest : public QObject
{
Q_OBJECT
@@ -67,6 +70,8 @@ private Q_SLOTS:
void tst_multipleVersions();
void tst_qtbug_39355();
+ void tst_qtbug_48123_data();
+ void tst_qtbug_48123();
};
tst_HandshakeRequest::tst_HandshakeRequest()
@@ -203,7 +208,7 @@ void tst_HandshakeRequest::tst_invalidStream()
textStream << dataStream;
textStream.seek(0);
- request.readHandshake(textStream);
+ request.readHandshake(textStream, MAX_HEADERLINE_LENGTH, MAX_HEADERS);
QVERIFY(!request.isValid());
QCOMPARE(request.port(), 80);
@@ -239,7 +244,7 @@ void tst_HandshakeRequest::tst_multipleValuesInConnectionHeader()
textStream << header;
textStream.seek(0);
- request.readHandshake(textStream);
+ request.readHandshake(textStream, MAX_HEADERLINE_LENGTH, MAX_HEADERS);
QVERIFY(request.isValid());
QCOMPARE(request.port(), 80);
@@ -269,7 +274,7 @@ void tst_HandshakeRequest::tst_multipleVersions()
textStream << header;
textStream.seek(0);
- request.readHandshake(textStream);
+ request.readHandshake(textStream, MAX_HEADERLINE_LENGTH, MAX_HEADERS);
QVERIFY(request.isValid());
QCOMPARE(request.port(), 80);
@@ -305,13 +310,76 @@ void tst_HandshakeRequest::tst_qtbug_39355()
textStream << header;
textStream.seek(0);
- request.readHandshake(textStream);
+ request.readHandshake(textStream, MAX_HEADERLINE_LENGTH, MAX_HEADERS);
QVERIFY(request.isValid());
QCOMPARE(request.port(), 1234);
QCOMPARE(request.host(), QStringLiteral("localhost"));
}
+void tst_HandshakeRequest::tst_qtbug_48123_data()
+{
+ QTest::addColumn<QString>("header");
+ QTest::addColumn<bool>("shouldBeValid");
+ const QString header = QStringLiteral("GET /ABC/DEF/ HTTP/1.1\r\nHost: localhost:1234\r\n") +
+ QStringLiteral("Sec-WebSocket-Version: 13\r\n") +
+ QStringLiteral("Sec-WebSocket-Key: 2Wg20829/4ziWlmsUAD8Dg==\r\n") +
+ QStringLiteral("Upgrade: websocket\r\n") +
+ QStringLiteral("Connection: Upgrade\r\n");
+ const int numHeaderLines = header.count(QStringLiteral("\r\n")) - 1; //-1: exclude requestline
+
+ //a headerline should not be larger than MAX_HEADERLINE_LENGTH characters (excluding CRLF)
+ QString illegalHeader = header;
+ illegalHeader.append(QString(MAX_HEADERLINE_LENGTH + 1, QChar::fromAscii('c')));
+ illegalHeader.append(QStringLiteral("\r\n\r\n"));
+
+ QTest::newRow("headerline too long") << illegalHeader << false;
+
+ QString legalHeader = header;
+ const QString headerKey = QStringLiteral("X-CUSTOM-KEY: ");
+ legalHeader.append(headerKey);
+ legalHeader.append(QString(MAX_HEADERLINE_LENGTH - headerKey.length(), QChar::fromAscii('c')));
+ legalHeader.append(QStringLiteral("\r\n\r\n"));
+
+ QTest::newRow("headerline with maximum length") << legalHeader << true;
+
+ //a header should not contain more than MAX_HEADERS header lines (excluding the request line)
+ //test with MAX_HEADERS + 1
+ illegalHeader = header;
+ const QString headerLine(QStringLiteral("Host: localhost:1234\r\n"));
+ for (int i = 0; i < (MAX_HEADERS - numHeaderLines + 1); ++i) {
+ illegalHeader.append(headerLine);
+ }
+ illegalHeader.append(QStringLiteral("\r\n"));
+
+ QTest::newRow("too many headerlines") << illegalHeader << false;
+
+ //test with MAX_HEADERS header lines (excluding the request line)
+ legalHeader = header;
+ for (int i = 0; i < (MAX_HEADERS - numHeaderLines); ++i) {
+ legalHeader.append(headerLine);
+ }
+ legalHeader.append(QStringLiteral("\r\n"));
+
+ QTest::newRow("just enough headerlines") << legalHeader << true;
+}
+
+void tst_HandshakeRequest::tst_qtbug_48123()
+{
+ QFETCH(QString, header);
+ QFETCH(bool, shouldBeValid);
+
+ QByteArray data;
+ QTextStream textStream(&data);
+ QWebSocketHandshakeRequest request(8080, false);
+
+ textStream << header;
+ textStream.seek(0);
+ request.readHandshake(textStream, MAX_HEADERLINE_LENGTH, MAX_HEADERS);
+
+ QCOMPARE(request.isValid(), shouldBeValid);
+}
+
QTEST_MAIN(tst_HandshakeRequest)
#include "tst_handshakerequest.moc"
diff --git a/tests/auto/websockets/handshakeresponse/tst_handshakeresponse.cpp b/tests/auto/websockets/handshakeresponse/tst_handshakeresponse.cpp
index b5f103b..7d35cd0 100644
--- a/tests/auto/websockets/handshakeresponse/tst_handshakeresponse.cpp
+++ b/tests/auto/websockets/handshakeresponse/tst_handshakeresponse.cpp
@@ -91,7 +91,7 @@ void tst_HandshakeResponse::tst_date_response()
QStringLiteral("Sec-WebSocket-Key: AVDFBDDFF\r\n") +
QStringLiteral("Upgrade: websocket\r\n") +
QStringLiteral("Connection: Upgrade\r\n\r\n");
- request.readHandshake(input);
+ request.readHandshake(input, 8 * 1024, 100);
QWebSocketHandshakeResponse response(request, "example.com", true,
QList<QWebSocketProtocol::Version>() << QWebSocketProtocol::Version13,
diff --git a/tests/auto/websockets/qwebsocket/tst_qwebsocket.cpp b/tests/auto/websockets/qwebsocket/tst_qwebsocket.cpp
index f993f84..9cef46a 100644
--- a/tests/auto/websockets/qwebsocket/tst_qwebsocket.cpp
+++ b/tests/auto/websockets/qwebsocket/tst_qwebsocket.cpp
@@ -143,6 +143,8 @@ private Q_SLOTS:
void tst_sendBinaryMessage();
void tst_errorString();
void tst_openRequest();
+ void tst_moveToThread();
+ void tst_moveToThreadNoWarning();
#ifndef QT_NO_NETWORKPROXY
void tst_setProxy();
#endif
@@ -603,6 +605,107 @@ void tst_QWebSocket::tst_openRequest()
socket.close();
}
+class WebSocket : public QWebSocket
+{
+ Q_OBJECT
+
+public:
+ explicit WebSocket()
+ {
+ connect(this, SIGNAL(triggerClose()), SLOT(onClose()), Qt::QueuedConnection);
+ connect(this, SIGNAL(triggerOpen(QUrl)), SLOT(onOpen(QUrl)), Qt::QueuedConnection);
+ connect(this, SIGNAL(triggerSendTextMessage(QString)), SLOT(onSendTextMessage(QString)), Qt::QueuedConnection);
+ connect(this, SIGNAL(textMessageReceived(QString)), this, SLOT(onTextMessageReceived(QString)), Qt::QueuedConnection);
+ }
+
+ void asyncClose() { triggerClose(); }
+ void asyncOpen(const QUrl &url) { triggerOpen(url); }
+ void asyncSendTextMessage(const QString &msg) { triggerSendTextMessage(msg); }
+
+ QString receivedMessage;
+
+Q_SIGNALS:
+ void triggerClose();
+ void triggerOpen(const QUrl &);
+ void triggerSendTextMessage(const QString &);
+ void done();
+
+private Q_SLOTS:
+ void onClose() { close(); }
+ void onOpen(const QUrl &url) { open(url); }
+ void onSendTextMessage(const QString &msg) { sendTextMessage(msg); }
+ void onTextMessageReceived(const QString &msg) { receivedMessage = msg; done(); }
+};
+
+struct Warned
+{
+ static QtMessageHandler origHandler;
+ static bool warned;
+ static void messageHandler(QtMsgType type, const QMessageLogContext& context, const QString& str)
+ {
+ if (type == QtWarningMsg) {
+ warned = true;
+ }
+ if (origHandler)
+ origHandler(type, context, str);
+ }
+};
+QtMessageHandler Warned::origHandler = 0;
+bool Warned::warned = false;
+
+
+void tst_QWebSocket::tst_moveToThread()
+{
+ Warned::origHandler = qInstallMessageHandler(&Warned::messageHandler);
+
+ EchoServer echoServer;
+
+ QThread* thread = new QThread;
+ thread->start();
+
+ WebSocket* socket = new WebSocket;
+ socket->moveToThread(thread);
+
+ const QString textMessage = QStringLiteral("Hello world!");
+ QSignalSpy socketConnectedSpy(socket, SIGNAL(connected()));
+ QUrl url = QUrl(QStringLiteral("ws://") + echoServer.hostAddress().toString() +
+ QStringLiteral(":") + QString::number(echoServer.port()));
+ url.setPath("/segment/with spaces");
+ url.addQueryItem("queryitem", "with encoded characters");
+
+ socket->asyncOpen(url);
+ if (socketConnectedSpy.count() == 0)
+ QVERIFY(socketConnectedSpy.wait(500));
+
+ socket->asyncSendTextMessage(textMessage);
+
+ QTimer timer;
+ timer.setInterval(1000);
+ timer.start();
+ QEventLoop loop;
+ connect(socket, SIGNAL(done()), &loop, SLOT(quit()));
+ connect(socket, SIGNAL(done()), &timer, SLOT(stop()));
+ connect(&timer, SIGNAL(timeout()), &loop, SLOT(quit()));
+ loop.exec();
+
+ socket->asyncClose();
+
+ QCOMPARE(timer.isActive(), false);
+ QCOMPARE(socket->receivedMessage, textMessage);
+
+ socket->deleteLater();
+ thread->quit();
+ thread->deleteLater();
+}
+
+void tst_QWebSocket::tst_moveToThreadNoWarning()
+{
+ // check for warnings in tst_moveToThread()
+ // couldn't done there because warnings are processed after the test run
+ QCOMPARE(Warned::warned, false);
+}
+
+
#ifndef QT_NO_NETWORKPROXY
void tst_QWebSocket::tst_setProxy()
{