diff options
author | Liang Qi <liang.qi@theqtcompany.com> | 2015-10-20 21:12:53 +0200 |
---|---|---|
committer | Liang Qi <liang.qi@theqtcompany.com> | 2015-10-20 21:12:53 +0200 |
commit | c2201fad153a64f1c6da84390de4316aedf3a2cd (patch) | |
tree | 0a499309b34087ff7390d77f3a6e8c0266cdd213 /tests/auto | |
parent | d0f71b3feaca6923e89f85993f13cc9da0923412 (diff) | |
parent | b59b0634715c13a191b678a0652ea4e3274865fa (diff) | |
download | qtwebsockets-c2201fad153a64f1c6da84390de4316aedf3a2cd.tar.gz |
Merge remote-tracking branch 'origin/5.6' into dev
Change-Id: I8d2c4b0982623839b880a9d8fbe7f5ee69d33ad3
Diffstat (limited to 'tests/auto')
4 files changed, 192 insertions, 21 deletions
diff --git a/tests/auto/websockets/handshakerequest/tst_handshakerequest.cpp b/tests/auto/websockets/handshakerequest/tst_handshakerequest.cpp index 9c579bd..668bbe1 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, QLatin1Char('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(), QLatin1Char('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 789d1fa..d0f22af 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 @@ -607,6 +609,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(); + + QTRY_COMPARE_WITH_TIMEOUT(loop.isRunning(), false, 200); + 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() { diff --git a/tests/auto/websockets/websocketframe/tst_websocketframe.cpp b/tests/auto/websockets/websocketframe/tst_websocketframe.cpp index 5db82d5..ecfcf3b 100644 --- a/tests/auto/websockets/websocketframe/tst_websocketframe.cpp +++ b/tests/auto/websockets/websocketframe/tst_websocketframe.cpp @@ -244,9 +244,9 @@ void tst_WebSocketFrame::tst_copyConstructorAndAssignment() void tst_WebSocketFrame::tst_goodFrames_data() { - QTest::addColumn<int>("rsv1"); - QTest::addColumn<int>("rsv2"); - QTest::addColumn<int>("rsv3"); + QTest::addColumn<bool>("rsv1"); + QTest::addColumn<bool>("rsv2"); + QTest::addColumn<bool>("rsv3"); QTest::addColumn<quint32>("mask"); QTest::addColumn<QWebSocketProtocol::OpCode>("opCode"); QTest::addColumn<bool>("isFinal"); @@ -256,54 +256,54 @@ void tst_WebSocketFrame::tst_goodFrames_data() QTest::addColumn<bool>("isContinuationFrame"); QTest::newRow("Non masked final text frame with small payload") - << 0 << 0 << 0 + << false << false << false << 0U << QWebSocketProtocol::OpCodeText << true << QStringLiteral("Hello world!").toUtf8() << false << true << false; QTest::newRow("Non masked final binary frame with small payload") - << 0 << 0 << 0 + << false << false << false << 0U << QWebSocketProtocol::OpCodeBinary << true << QByteArrayLiteral("\x00\x01\x02\x03\x04") << false << true << false; QTest::newRow("Non masked final text frame with no payload") - << 0 << 0 << 0 + << false << false << false << 0U << QWebSocketProtocol::OpCodeText << true << QByteArray() << false << true << false; QTest::newRow("Non masked final binary frame with no payload") - << 0 << 0 << 0 + << false << false << false << 0U << QWebSocketProtocol::OpCodeBinary << true << QByteArray() << false << true << false; QTest::newRow("Non masked final close frame with small payload") - << 0 << 0 << 0 + << false << false << false << 0U << QWebSocketProtocol::OpCodeClose << true << QStringLiteral("Hello world!").toUtf8() << true << false << false; QTest::newRow("Non masked final close frame with no payload") - << 0 << 0 << 0 + << false << false << false << 0U << QWebSocketProtocol::OpCodeClose << true << QByteArray() << true << false << false; QTest::newRow("Non masked final ping frame with small payload") - << 0 << 0 << 0 + << false << false << false << 0U << QWebSocketProtocol::OpCodePing << true << QStringLiteral("Hello world!").toUtf8() << true << false << false; QTest::newRow("Non masked final pong frame with no payload") - << 0 << 0 << 0 + << false << false << false << 0U << QWebSocketProtocol::OpCodePong << true << QByteArray() << true << false << false; QTest::newRow("Non masked final continuation frame with small payload") - << 0 << 0 << 0 + << false << false << false << 0U << QWebSocketProtocol::OpCodeContinue << true << QStringLiteral("Hello world!").toUtf8() << false << true << true; QTest::newRow("Non masked non-final continuation frame with small payload") - << 0 << 0 << 0 + << false << false << false << 0U << QWebSocketProtocol::OpCodeContinue << false << QStringLiteral("Hello world!").toUtf8() << false << true << true; @@ -311,9 +311,9 @@ void tst_WebSocketFrame::tst_goodFrames_data() void tst_WebSocketFrame::tst_goodFrames() { - QFETCH(int, rsv1); - QFETCH(int, rsv2); - QFETCH(int, rsv3); + QFETCH(bool, rsv1); + QFETCH(bool, rsv2); + QFETCH(bool, rsv3); QFETCH(quint32, mask); QFETCH(QWebSocketProtocol::OpCode, opCode); QFETCH(bool, isFinal); |