diff options
author | Kurt Pattyn <pattyn.kurt@gmail.com> | 2013-08-25 00:52:43 +0200 |
---|---|---|
committer | Kurt Pattyn <pattyn.kurt@gmail.com> | 2013-08-25 00:52:43 +0200 |
commit | b43feeee2b5959ac1a81fb4c15058b915a268f71 (patch) | |
tree | 9cd3db8fce7fcc7481ea068aabf18ba3dd5428eb /tests | |
parent | af7b65d80c245cddc781d1a6c130bf4b7c7c1556 (diff) | |
download | qtwebsockets-b43feeee2b5959ac1a81fb4c15058b915a268f71.tar.gz |
Renamed test directory to tests (to be inline with Qt)
Diffstat (limited to 'tests')
-rw-r--r-- | tests/echoserver.py | 58 | ||||
-rw-r--r-- | tests/fuzzingclient.json | 17 | ||||
-rw-r--r-- | tests/fuzzingserver.json | 12 | ||||
-rw-r--r-- | tests/main.cpp | 3 | ||||
-rwxr-xr-x | tests/start_basic_sockettests.sh | 10 | ||||
-rwxr-xr-x | tests/start_echo_server.sh | 1 | ||||
-rwxr-xr-x | tests/stop_echo_server.sh | 1 | ||||
-rw-r--r-- | tests/tst_compliance.cpp | 126 | ||||
-rw-r--r-- | tests/tst_websockets.cpp | 166 | ||||
-rw-r--r-- | tests/unittests.h | 86 | ||||
-rw-r--r-- | tests/unittests.pro | 54 |
11 files changed, 534 insertions, 0 deletions
diff --git a/tests/echoserver.py b/tests/echoserver.py new file mode 100644 index 0000000..5478eb2 --- /dev/null +++ b/tests/echoserver.py @@ -0,0 +1,58 @@ +############################################################################### +## +## Copyright 2011,2012 Tavendo GmbH +## +## Licensed under the Apache License, Version 2.0 (the "License"); +## you may not use this file except in compliance with the License. +## You may obtain a copy of the License at +## +## http://www.apache.org/licenses/LICENSE-2.0 +## +## Unless required by applicable law or agreed to in writing, software +## distributed under the License is distributed on an "AS IS" BASIS, +## WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. +## See the License for the specific language governing permissions and +## limitations under the License. +## +############################################################################### + +import sys + +from twisted.internet import reactor +from twisted.python import log +from twisted.web.server import Site +from twisted.web.static import File + +from autobahn.websocket import WebSocketServerFactory, \ + WebSocketServerProtocol, \ + listenWS + + +class EchoServerProtocol(WebSocketServerProtocol): + + def onMessage(self, msg, binary): + self.sendMessage(msg, binary) + + +if __name__ == '__main__': + + if len(sys.argv) > 1 and sys.argv[1] == 'debug': + log.startLogging(sys.stdout) + debug = True + else: + debug = False + + factory = WebSocketServerFactory("ws://localhost:9000", + debug = debug, + debugCodePaths = debug) + + factory.protocol = EchoServerProtocol + factory.setProtocolOptions(allowHixie76 = True) + listenWS(factory) + + webdir = File(".") + web = Site(webdir) + reactor.listenTCP(8080, web) + + reactor.run() + diff --git a/tests/fuzzingclient.json b/tests/fuzzingclient.json new file mode 100644 index 0000000..c196070 --- /dev/null +++ b/tests/fuzzingclient.json @@ -0,0 +1,17 @@ + +{ + "options": {"failByDrop": false}, + "outdir": "./reports/servers", + + "servers": [ + { + "agent": "QWebSockets/0.9", + "url": "ws://127.0.0.1:1234", + "options": {"version": 13} + } + ], + + "cases": ["*"], + "exclude-cases": [], + "exclude-agent-cases": {} +} diff --git a/tests/fuzzingserver.json b/tests/fuzzingserver.json new file mode 100644 index 0000000..24aaca0 --- /dev/null +++ b/tests/fuzzingserver.json @@ -0,0 +1,12 @@ + +{ + "url": "ws://127.0.0.1:9001", + + "options": {"failByDrop": false}, + "outdir": "./reports/clients", + "webport": 8090, + + "cases": ["*"], + "exclude-cases": [], + "exclude-agent-cases": {} +} diff --git a/tests/main.cpp b/tests/main.cpp new file mode 100644 index 0000000..68e1242 --- /dev/null +++ b/tests/main.cpp @@ -0,0 +1,3 @@ +#include "unittests.h" + +TEST_MAIN diff --git a/tests/start_basic_sockettests.sh b/tests/start_basic_sockettests.sh new file mode 100755 index 0000000..4ba4896 --- /dev/null +++ b/tests/start_basic_sockettests.sh @@ -0,0 +1,10 @@ +#cd test +#/usr/local/bin/wstest -m echoserver -w ws://localhost:9000 & +#/usr/bin/python echoserver.py & + +./unittests -xunitxml -o ./unittest_result.xml + +#stop server +#pid=$(ps -eo pid,command,lstart | grep '/usr/bin/python' | tail -1 | grep -e '^ (\d+)' -E -o | grep -e '(\d+)' -E -o) +#kill -9 $pid + diff --git a/tests/start_echo_server.sh b/tests/start_echo_server.sh new file mode 100755 index 0000000..c101287 --- /dev/null +++ b/tests/start_echo_server.sh @@ -0,0 +1 @@ +/usr/bin/python echoserver.py -w & diff --git a/tests/stop_echo_server.sh b/tests/stop_echo_server.sh new file mode 100755 index 0000000..8c8a2a4 --- /dev/null +++ b/tests/stop_echo_server.sh @@ -0,0 +1 @@ +#wstest -m echoserver -w ws://localhost:9000 & diff --git a/tests/tst_compliance.cpp b/tests/tst_compliance.cpp new file mode 100644 index 0000000..b450887 --- /dev/null +++ b/tests/tst_compliance.cpp @@ -0,0 +1,126 @@ +#include <QtTest/QtTest> +#include <QtTest/qtestcase.h> +#include <QSignalSpy> +#include <QHostInfo> +#include <QDebug> +#include "qwebsocket.h" +#include "unittests.h" + +class ComplianceTest : public QObject +{ + Q_OBJECT + +public: + ComplianceTest(); + +private Q_SLOTS: + void initTestCase(); + void cleanupTestCase(); + void init(); + void cleanup(); + /** + * @brief Runs the autobahn tests against our implementation + */ + void autobahnTest(); + +private: + QUrl m_url; + + void runTestCases(int startNbr, int stopNbr = -1); + void runTestCase(int nbr, int total); +}; + +ComplianceTest::ComplianceTest() : + m_url("ws://localhost:9001") +{ +} + +void ComplianceTest::initTestCase() +{ +} + +void ComplianceTest::cleanupTestCase() +{ +} + +void ComplianceTest::init() +{ +} + +void ComplianceTest::cleanup() +{ +} + +void ComplianceTest::runTestCase(int nbr, int total) +{ + if (nbr == total) + { + return; + } + QWebSocket *pWebSocket = new QWebSocket; + QSignalSpy spy(pWebSocket, SIGNAL(disconnected())); + + //next for every case, connect to url + //ws://ipaddress:port/runCase?case=<number>&agent=<agentname> + //where agent name will be QWebSocket + QObject::connect(pWebSocket, &QWebSocket::textMessageReceived, [=](QString message) { + pWebSocket->send(message); + }); + QObject::connect(pWebSocket, &QWebSocket::binaryMessageReceived, [=](QByteArray message) { + pWebSocket->send(message); + }); + + qDebug() << "Executing test" << (nbr + 1) << "/" << total; + QUrl url = m_url; + url.setPath("/runCase?"); + QUrlQuery query; + query.addQueryItem("case", QString::number(nbr + 1)); + query.addQueryItem("agent", "QWebSockets/0.9"); + url.setQuery(query); + pWebSocket->open(url); + spy.wait(60000); + pWebSocket->close(); + delete pWebSocket; + pWebSocket = 0; + runTestCase(nbr + 1, total); +} + +void ComplianceTest::runTestCases(int startNbr, int stopNbr) +{ + runTestCase(startNbr, stopNbr); +} + +void ComplianceTest::autobahnTest() +{ + //connect to autobahn server at url ws://ipaddress:port/getCaseCount + QWebSocket *pWebSocket = new QWebSocket; + QUrl url = m_url; + int numberOfTestCases = 0; + QSignalSpy spy(pWebSocket, SIGNAL(disconnected())); + QObject::connect(pWebSocket, &QWebSocket::textMessageReceived, [&](QString message) { + numberOfTestCases = message.toInt(); + }); + + url.setPath("/getCaseCount"); + pWebSocket->open(url); + spy.wait(60000); + QVERIFY(numberOfTestCases > 0); + + QObject::disconnect(pWebSocket, &QWebSocket::textMessageReceived, 0, 0); + + runTestCases(0, numberOfTestCases); + + url.setPath("/updateReports?"); + QUrlQuery query; + query.addQueryItem("agent", "QWebSockets"); + url.setQuery(query); + pWebSocket->open(url); + spy.wait(60000); + delete pWebSocket; + pWebSocket = 0; +} + +DECLARE_TEST(ComplianceTest) + +#include "tst_compliance.moc" + diff --git a/tests/tst_websockets.cpp b/tests/tst_websockets.cpp new file mode 100644 index 0000000..fe914dc --- /dev/null +++ b/tests/tst_websockets.cpp @@ -0,0 +1,166 @@ +#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 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->send(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->send(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->send(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()); +} + +//DECLARE_TEST(WebSocketsTest) + +#include "tst_websockets.moc" + diff --git a/tests/unittests.h b/tests/unittests.h new file mode 100644 index 0000000..a06fd24 --- /dev/null +++ b/tests/unittests.h @@ -0,0 +1,86 @@ +#ifndef UNITTESTT_H +#define UNITTESTT_H + +#include <QTest> +#include <QObject> +#include <QList> +#include <QString> +#include <QSharedPointer> +#include <QCoreApplication> + +namespace AutoTest +{ + +typedef QList<QObject*> TestList; + +inline TestList& testList() +{ + static TestList list; + return list; +} + +inline bool findObject(QObject* object) +{ + TestList& list = testList(); + if (list.contains(object)) + { + return true; + } + Q_FOREACH (QObject* test, list) + { + if (test->objectName() == object->objectName()) + { + return true; + } + } + return false; +} + +inline void addTest(QObject* object) +{ + TestList& list = testList(); + if (!findObject(object)) + { + list.append(object); + } +} + +inline int run(int argc, char *argv[]) +{ + int ret = 0; + + Q_FOREACH (QObject* test, testList()) + { + ret += QTest::qExec(test, argc, argv); + } + testList().clear(); + return ret; +} + +} // end namespace + +template <class T> +class Test +{ +public: + QSharedPointer<T> child; + + Test(const QString& name) : child(new T) + { + child->setObjectName(name); + AutoTest::addTest(child.data()); + } +}; + +#define DECLARE_TEST(className) static Test<className> t(#className); + +#define TEST_MAIN \ +int main(int argc, char *argv[]) \ +{ \ + QCoreApplication app(argc, argv); \ + int ret = AutoTest::run(argc, argv); \ + return ret; \ +} +//return app.exec(); + +#endif diff --git a/tests/unittests.pro b/tests/unittests.pro new file mode 100644 index 0000000..743cc21 --- /dev/null +++ b/tests/unittests.pro @@ -0,0 +1,54 @@ +cache() +# Determine the platform: if using a cross-compiler -> add it to the config flags. +!contains(QMAKE_CXX, g++) { + CONFIG += embedded +} + +QT += core network +CONFIG += c++11 + +embedded { # Vanilla Qt libraries are differently deployed then the dev libraries of Debian. + QT += multimedia +} +CONFIG += mobility +MOBILITY = multimedia + +unix:!macx { + QT += dbus +} + + +include(../src/websocket.pri) + +#include(../source/buildsystem/buildsystem.pri) + +# Remove the main.cpp file from the sources. +S = $$SOURCES +SOURCES = \ + tst_compliance.cpp +for(F, S) { + M = $$find(F, main.cpp) + count(M, 0) { + SOURCES += $$F + } +} + +SOURCES += \ + main.cpp \ + tst_websockets.cpp + +HEADERS += \ + unittests.h + +INCLUDEPATH += +DEPENDPATH += + +QT += testlib + +TARGET = unittests +CONFIG += console +CONFIG -= app_bundle + +TEMPLATE = app + +DEFINES += SRCDIR=\\\"$$PWD/\\\" |