diff options
author | Sergio Ahumada <sergio.ahumada@nokia.com> | 2012-01-31 18:50:20 +0100 |
---|---|---|
committer | Sergio Ahumada <sergio.ahumada@nokia.com> | 2012-01-31 18:50:20 +0100 |
commit | 0c64b72daf5462270f5e087494caaee7d2fdbc5d (patch) | |
tree | c5b2dcd3e6edb42025c581ec5829f93e8e120f96 /tests |
Long live Qt Json Stream!
Diffstat (limited to 'tests')
-rw-r--r-- | tests/auto/auto.pro | 2 | ||||
-rw-r--r-- | tests/auto/jsonschema/create-test.json | 15 | ||||
-rw-r--r-- | tests/auto/jsonschema/jsonschema.pro | 5 | ||||
-rw-r--r-- | tests/auto/jsonschema/tst_jsonschema.cpp | 83 | ||||
-rw-r--r-- | tests/auto/jsonstream/.gitignore | 1 | ||||
-rw-r--r-- | tests/auto/jsonstream/jsonstream.pro | 3 | ||||
-rw-r--r-- | tests/auto/jsonstream/test/test.pro | 9 | ||||
-rw-r--r-- | tests/auto/jsonstream/testClient/.gitignore | 1 | ||||
-rw-r--r-- | tests/auto/jsonstream/testClient/main.cpp | 154 | ||||
-rw-r--r-- | tests/auto/jsonstream/testClient/testClient.pro | 7 | ||||
-rw-r--r-- | tests/auto/jsonstream/tst_jsonstream.cpp | 374 | ||||
-rw-r--r-- | tests/auto/testsuite/.gitignore | 1 | ||||
-rw-r--r-- | tests/auto/testsuite/testsuite.pro | 11 | ||||
-rw-r--r-- | tests/auto/testsuite/tst_jsonclient.cpp | 86 | ||||
-rw-r--r-- | tests/auto/testsuite/tst_jsonclient.h | 68 | ||||
-rw-r--r-- | tests/auto/testsuite/tst_jsonserver.cpp | 196 | ||||
-rw-r--r-- | tests/auto/testsuite/tst_jsonserver.h | 87 | ||||
-rw-r--r-- | tests/auto/testsuite/tst_jsonstream.cpp | 142 | ||||
-rw-r--r-- | tests/auto/testsuite/tst_jsonstream.h | 51 | ||||
-rw-r--r-- | tests/tests.pro | 2 |
20 files changed, 1298 insertions, 0 deletions
diff --git a/tests/auto/auto.pro b/tests/auto/auto.pro new file mode 100644 index 0000000..430c8f3 --- /dev/null +++ b/tests/auto/auto.pro @@ -0,0 +1,2 @@ +TEMPLATE = subdirs +SUBDIRS = jsonstream jsonschema diff --git a/tests/auto/jsonschema/create-test.json b/tests/auto/jsonschema/create-test.json new file mode 100644 index 0000000..742909f --- /dev/null +++ b/tests/auto/jsonschema/create-test.json @@ -0,0 +1,15 @@ +{ + "title": "SchemaTestObject", + "type": "object", + "properties": { + "create-test": { + "type": "number", + "required": true + }, + "another-field": { + "type": "string", + "format": "uri", + "required": true + } + } +} diff --git a/tests/auto/jsonschema/jsonschema.pro b/tests/auto/jsonschema/jsonschema.pro new file mode 100644 index 0000000..07d25a9 --- /dev/null +++ b/tests/auto/jsonschema/jsonschema.pro @@ -0,0 +1,5 @@ +CONFIG -= app_bundle +QT += testlib jsonstream + +SOURCES = tst_jsonschema.cpp +TARGET = tst_jsonschema diff --git a/tests/auto/jsonschema/tst_jsonschema.cpp b/tests/auto/jsonschema/tst_jsonschema.cpp new file mode 100644 index 0000000..fcd3c4e --- /dev/null +++ b/tests/auto/jsonschema/tst_jsonschema.cpp @@ -0,0 +1,83 @@ +/**************************************************************************** +** +** Copyright (C) 2011 Nokia Corporation and/or its subsidiary(-ies). +** All rights reserved. +** Contact: Nokia Corporation (qt-info@nokia.com) +** +** This file is part of the QtAddOn.JsonStream module of the Qt Toolkit. +** +** $QT_BEGIN_LICENSE:LGPL$ +** GNU Lesser General Public License Usage +** This file may be used under the terms of the GNU Lesser General Public +** License version 2.1 as published by the Free Software Foundation and +** appearing in the file LICENSE.LGPL included in the packaging of this +** file. Please review the following information to ensure the GNU Lesser +** General Public License version 2.1 requirements will be met: +** http://www.gnu.org/licenses/old-licenses/lgpl-2.1.html. +** +** In addition, as a special exception, Nokia gives you certain additional +** rights. These rights are described in the Nokia Qt LGPL Exception +** version 1.1, included in the file LGPL_EXCEPTION.txt in this package. +** +** GNU General Public License Usage +** Alternatively, this file may be used under the terms of the GNU General +** Public License version 3.0 as published by the Free Software Foundation +** and appearing in the file LICENSE.GPL included in the packaging of this +** file. Please review the following information to ensure the GNU General +** Public License version 3.0 requirements will be met: +** http://www.gnu.org/copyleft/gpl.html. +** +** Other Usage +** Alternatively, this file may be used in accordance with the terms and +** conditions contained in a signed written agreement between you and Nokia. +** +** +** +** +** +** $QT_END_LICENSE$ +** +****************************************************************************/ + +#include <QtTest/QtTest> + +#include "schemavalidator.h" + +QT_USE_NAMESPACE_JSONSTREAM + +class tst_JsonSchema : public QObject +{ + Q_OBJECT + +private slots: + void schemaTest(); +}; + +void tst_JsonSchema::schemaTest() +{ + QJsonObject result; + SchemaValidator validator; + result = validator.initializeFromFolder(QDir::currentPath(), "title"); + QVERIFY(result.isEmpty()); + + // Create an item that matches the schema + QJsonObject item; + item.insert("create-test", 22); + item.insert("another-field", QLatin1String("a string")); + + result = validator.validateSchema("SchemaTestObject", item); + qDebug() << "VALID validation result: " << result; + QVERIFY(result.isEmpty()); + + // Create an item that does not match the schema + QJsonObject noncompliant; + noncompliant.insert("create-test", 22); + + result = validator.validateSchema("SchemaTestObject", noncompliant); + qDebug() << "INVALID validation result: " << result; + QVERIFY(!result.isEmpty()); +} + +QTEST_MAIN(tst_JsonSchema) + +#include "tst_jsonschema.moc" diff --git a/tests/auto/jsonstream/.gitignore b/tests/auto/jsonstream/.gitignore new file mode 100644 index 0000000..b7c731a --- /dev/null +++ b/tests/auto/jsonstream/.gitignore @@ -0,0 +1 @@ +tst_jsonstream diff --git a/tests/auto/jsonstream/jsonstream.pro b/tests/auto/jsonstream/jsonstream.pro new file mode 100644 index 0000000..fba387f --- /dev/null +++ b/tests/auto/jsonstream/jsonstream.pro @@ -0,0 +1,3 @@ +TEMPLATE = subdirs + +SUBDIRS = test testClient diff --git a/tests/auto/jsonstream/test/test.pro b/tests/auto/jsonstream/test/test.pro new file mode 100644 index 0000000..54f5fbe --- /dev/null +++ b/tests/auto/jsonstream/test/test.pro @@ -0,0 +1,9 @@ +CONFIG += testcase +CONFIG -= app_bundle + +QT = jsonstream testlib + +include(../../../../src/src.pri) + +SOURCES = ../tst_jsonstream.cpp +TARGET = ../tst_jsonstream diff --git a/tests/auto/jsonstream/testClient/.gitignore b/tests/auto/jsonstream/testClient/.gitignore new file mode 100644 index 0000000..9640c79 --- /dev/null +++ b/tests/auto/jsonstream/testClient/.gitignore @@ -0,0 +1 @@ +testClient diff --git a/tests/auto/jsonstream/testClient/main.cpp b/tests/auto/jsonstream/testClient/main.cpp new file mode 100644 index 0000000..52db020 --- /dev/null +++ b/tests/auto/jsonstream/testClient/main.cpp @@ -0,0 +1,154 @@ +/**************************************************************************** +** +** Copyright (C) 2011 Nokia Corporation and/or its subsidiary(-ies). +** All rights reserved. +** Contact: Nokia Corporation (qt-info@nokia.com) +** +** This file is part of the QtAddOn.JsonStream module of the Qt Toolkit. +** +** $QT_BEGIN_LICENSE:LGPL$ +** GNU Lesser General Public License Usage +** This file may be used under the terms of the GNU Lesser General Public +** License version 2.1 as published by the Free Software Foundation and +** appearing in the file LICENSE.LGPL included in the packaging of this +** file. Please review the following information to ensure the GNU Lesser +** General Public License version 2.1 requirements will be met: +** http://www.gnu.org/licenses/old-licenses/lgpl-2.1.html. +** +** In addition, as a special exception, Nokia gives you certain additional +** rights. These rights are described in the Nokia Qt LGPL Exception +** version 1.1, included in the file LGPL_EXCEPTION.txt in this package. +** +** GNU General Public License Usage +** Alternatively, this file may be used under the terms of the GNU General +** Public License version 3.0 as published by the Free Software Foundation +** and appearing in the file LICENSE.GPL included in the packaging of this +** file. Please review the following information to ensure the GNU General +** Public License version 3.0 requirements will be met: +** http://www.gnu.org/copyleft/gpl.html. +** +** Other Usage +** Alternatively, this file may be used in accordance with the terms and +** conditions contained in a signed written agreement between you and Nokia. +** +** +** +** +** +** $QT_END_LICENSE$ +** +****************************************************************************/ + +#include <QCoreApplication> +#include <QStringList> +#include <QDebug> +#include <QJsonArray> + +#include "jsonclient.h" + +QT_USE_NAMESPACE_JSONSTREAM + +QString gFormat; +QString gSocketname = "/tmp/tst_jsonstream"; + +class Container : public QObject +{ + Q_OBJECT + +public: + Container(); + void sendMessage(); + +public slots: + void received(const QJsonObject& message); + void disconnected(); + +private: + JsonClient *mClient; + int mCounter; +}; + +Container::Container() + : mCounter(0) +{ + qDebug() << "Creating new json client with format" << gFormat; + mClient = new JsonClient; + connect(mClient, SIGNAL(messageReceived(const QJsonObject&)), + SLOT(received(const QJsonObject&))); + connect(mClient, SIGNAL(disconnected()), SLOT(disconnected())); + if (gFormat == "qbjs") + mClient->setFormat(FormatQBJS); + else if (gFormat == "bson") + mClient->setFormat(FormatBSON); + else if (gFormat == "utf" || gFormat == "utf8") + mClient->setFormat(FormatUTF8); + + if (!mClient->connectLocal(gSocketname)) { + qWarning() << "Unable to connect to" << gSocketname; + exit(2); + } +} + +void Container::sendMessage() +{ + QJsonObject msg; + msg.insert("text", QLatin1String("Standard text")); + msg.insert("number", mCounter++); + msg.insert("int", 100); + msg.insert("float", 100.0); + msg.insert("true", true); + msg.insert("false", false); + msg.insert("array", QJsonArray::fromStringList(QStringList() << "one" << "two" << "three")); + QJsonObject obj; + obj.insert("item1", QLatin1String("This is item 1")); + obj.insert("item2", QLatin1String("This is item 2")); + msg.insert("object", obj); + mClient->send(msg); +} + +void Container::received(const QJsonObject& message) +{ + QString command = message.value("command").toString(); + if (!command.isEmpty()) { + qDebug() << "Received command" << command; + if (command == "exit") + exit(0); + else if (command == "crash") + exit(1); + else if (command == "reply") + sendMessage(); + else if (command == "flurry") { + for (int count = message.value("count").toDouble() ; count ; --count ) + sendMessage(); + } + } +} + +void Container::disconnected() +{ + exit(0); +} + +int +main(int argc, char **argv) +{ + QCoreApplication app(argc, argv); + QStringList args = QCoreApplication::arguments(); + QString progname = args.takeFirst(); + while (args.size()) { + QString arg = args.at(0); + if (!arg.startsWith("-")) + break; + args.removeFirst(); + if (arg == "-format") + gFormat = args.takeFirst(); + else if (arg == "-socket") + gSocketname = args.takeFirst(); + } + + Container c; + c.sendMessage(); + return app.exec(); +} + +#include "main.moc" diff --git a/tests/auto/jsonstream/testClient/testClient.pro b/tests/auto/jsonstream/testClient/testClient.pro new file mode 100644 index 0000000..474e812 --- /dev/null +++ b/tests/auto/jsonstream/testClient/testClient.pro @@ -0,0 +1,7 @@ +CONFIG -= app_bundle +QT += testlib jsonstream + +include(../../../../src/src.pri) + +SOURCES = main.cpp +TARGET = testClient diff --git a/tests/auto/jsonstream/tst_jsonstream.cpp b/tests/auto/jsonstream/tst_jsonstream.cpp new file mode 100644 index 0000000..e2db673 --- /dev/null +++ b/tests/auto/jsonstream/tst_jsonstream.cpp @@ -0,0 +1,374 @@ +/**************************************************************************** +** +** Copyright (C) 2011 Nokia Corporation and/or its subsidiary(-ies). +** All rights reserved. +** Contact: Nokia Corporation (qt-info@nokia.com) +** +** This file is part of the QtAddOn.JsonStream module of the Qt Toolkit. +** +** $QT_BEGIN_LICENSE:LGPL$ +** GNU Lesser General Public License Usage +** This file may be used under the terms of the GNU Lesser General Public +** License version 2.1 as published by the Free Software Foundation and +** appearing in the file LICENSE.LGPL included in the packaging of this +** file. Please review the following information to ensure the GNU Lesser +** General Public License version 2.1 requirements will be met: +** http://www.gnu.org/licenses/old-licenses/lgpl-2.1.html. +** +** In addition, as a special exception, Nokia gives you certain additional +** rights. These rights are described in the Nokia Qt LGPL Exception +** version 1.1, included in the file LGPL_EXCEPTION.txt in this package. +** +** GNU General Public License Usage +** Alternatively, this file may be used under the terms of the GNU General +** Public License version 3.0 as published by the Free Software Foundation +** and appearing in the file LICENSE.GPL included in the packaging of this +** file. Please review the following information to ensure the GNU General +** Public License version 3.0 requirements will be met: +** http://www.gnu.org/copyleft/gpl.html. +** +** Other Usage +** Alternatively, this file may be used in accordance with the terms and +** conditions contained in a signed written agreement between you and Nokia. +** +** +** +** +** +** $QT_END_LICENSE$ +** +****************************************************************************/ + +#include <QtTest> +#include <QLocalSocket> +#include <QLocalServer> +#include "jsonserver.h" +#include "jsonstream.h" +#include "jsonuidauthority.h" + +QT_USE_NAMESPACE_JSONSTREAM + +Q_DECLARE_METATYPE(QJsonObject); + +class Spy { +public: + Spy(JsonServer *server) + : addedSpy(server, SIGNAL(connectionAdded(const QString&))) + , removedSpy(server, SIGNAL(connectionRemoved(const QString&))) + , receivedSpy(server, SIGNAL(messageReceived(const QString&, const QJsonObject&))) + , failedSpy(server, SIGNAL(authorizationFailed())) + {} + + void waitAdded(int timeout=5000) { + int oldCount = addedSpy.count(); + stopWatch.restart(); + forever { + QTestEventLoop::instance().enterLoop(1); + if (stopWatch.elapsed() >= timeout) + QFAIL("Timed out"); + if (addedSpy.count() > oldCount) + break; + } + } + + void waitRemoved(int timeout=5000) { + int oldCount = removedSpy.count(); + stopWatch.restart(); + forever { + QTestEventLoop::instance().enterLoop(1); + if (stopWatch.elapsed() >= timeout) + QFAIL("Timed out"); + if (removedSpy.count() > oldCount) + break; + } + } + + void waitReceived(int timeout=5000) { + int oldCount = receivedSpy.count(); + stopWatch.restart(); + forever { + QTestEventLoop::instance().enterLoop(1); + if (stopWatch.elapsed() >= timeout) + QFAIL("Timed out"); + if (receivedSpy.count() > oldCount) + break; + } + } + + void waitFailed(int timeout=5000) { + stopWatch.restart(); + forever { + QTestEventLoop::instance().enterLoop(1); + if (stopWatch.elapsed() >= timeout) + QFAIL("Timed out"); + if (failedSpy.count()) + break; + } + } + + QString lastSender() { return receivedSpy.last().at(0).toString(); } + QJsonObject lastMessage() { return qvariant_cast<QJsonObject>(receivedSpy.last().at(1));} + + QTime stopWatch; + QSignalSpy addedSpy, removedSpy, receivedSpy, failedSpy; +}; + +class Child : public QObject { + Q_OBJECT +public: + Child(const QString& progname, const QStringList& arguments) { + process = new QProcess; + process->setProcessChannelMode(QProcess::MergedChannels); + connect(process, SIGNAL(readyReadStandardOutput()), this, SLOT(readyread())); + connect(process, SIGNAL(finished(int, QProcess::ExitStatus)), + this, SLOT(finished(int, QProcess::ExitStatus))); + connect(process, SIGNAL(stateChanged(QProcess::ProcessState)), + SLOT(stateChanged(QProcess::ProcessState))); + connect(process, SIGNAL(error(QProcess::ProcessError)), + SLOT(error(QProcess::ProcessError))); + process->start(progname, arguments); + QVERIFY(process->waitForStarted(5000)); + } + + ~Child() { + if (process) + delete process; + process = 0; + } + + void waitForFinished() { + if (process->state() == QProcess::Running) + QVERIFY(process->waitForFinished(5000)); + delete process; + process = 0; + } + +protected slots: + void readyread() { + QByteArray ba = process->readAllStandardOutput(); + QList<QByteArray> list = ba.split('\n'); + foreach (const QByteArray& s, list) + if (s.size()) + qDebug() << "PROCESS" << s; + } + void stateChanged(QProcess::ProcessState state) { + qDebug() << "Process state" << state; + } + void error(QProcess::ProcessError err) { + qDebug() << "Process error" << err; + } + void finished( int exitcode, QProcess::ExitStatus status ) { + qDebug() << Q_FUNC_INFO << exitcode << status; + } + +private: + QProcess *process; +}; + +/****************************/ +class BasicServer : public QObject { + Q_OBJECT + +public: + BasicServer(const QString& socketname) : socket(0), stream(0) { + QLocalServer::removeServer(socketname); + server = new QLocalServer(this); + connect(server, SIGNAL(newConnection()), SLOT(handleConnection())); + QVERIFY(server->listen(socketname)); + } + + ~BasicServer() { + QVERIFY(server); + delete server; + server = NULL; + } + + void send(const QJsonObject& message) { + QVERIFY(stream); + stream->send(message); + } + + void waitDisconnect(int timeout=5000) { + QTime stopWatch; + stopWatch.start(); + forever { + QTestEventLoop::instance().enterLoop(1); + if (stopWatch.elapsed() >= timeout) + QFAIL("Timed out"); + if (!socket) + break; + } + } + + EncodingFormat format() { + return stream->format(); + } + +private slots: + void handleConnection() { + socket = server->nextPendingConnection(); + QVERIFY(socket); + stream = new JsonStream(socket); + stream->setParent(socket); + connect(socket, SIGNAL(disconnected()), SLOT(handleDisconnection())); + connect(stream, SIGNAL(messageReceived(const QJsonObject&)), + SIGNAL(messageReceived(const QJsonObject&))); + } + + void handleDisconnection() { + QVERIFY(socket); + socket->deleteLater(); + socket = NULL; + stream = NULL; + } + +signals: + void messageReceived(const QJsonObject&); + +private: + QLocalServer *server; + QLocalSocket *socket; + JsonStream *stream; +}; + +/****************************/ + +class tst_JsonStream : public QObject +{ + Q_OBJECT + +private slots: + void initTestCase(); + + void noAuthTest(); + void authTest(); + void authFail(); + void formatTest(); +}; + +void tst_JsonStream::initTestCase() +{ + qRegisterMetaType<QJsonObject>(); +} + + +void tst_JsonStream::noAuthTest() +{ + QString socketname = "/tmp/tst_socket"; + JsonServer server; + Spy spy(&server); + QVERIFY(server.listen(socketname)); + + Child child("testClient/testClient", QStringList() << "-socket" << socketname); + + spy.waitReceived(); + qDebug() << "Got note=" << spy.lastMessage() << "from" << spy.lastSender(); + QJsonObject msg; + msg.insert("command", QLatin1String("exit")); + QVERIFY(server.hasConnection(spy.lastSender())); + QVERIFY(server.send(spy.lastSender(), msg)); + + spy.waitRemoved(); + child.waitForFinished(); +} + +void tst_JsonStream::authTest() +{ + QString socketname = "/tmp/tst_socket"; + JsonServer server; + Spy spy(&server); + JsonUIDAuthority *authority = new JsonUIDAuthority; + QVERIFY(server.listen(socketname, authority)); + + authority->authorize(geteuid()); + Child child("testClient/testClient", QStringList() << "-socket" << socketname); + + spy.waitReceived(); + qDebug() << "Got note=" << spy.lastMessage() << "from" << spy.lastSender(); + QJsonObject msg; + msg.insert("command", QLatin1String("exit")); + QVERIFY(server.hasConnection(spy.lastSender())); + QVERIFY(server.send(spy.lastSender(), msg)); + + spy.waitRemoved(); + child.waitForFinished(); + delete authority; +} + +void tst_JsonStream::authFail() +{ + QString socketname = "/tmp/tst_socket"; + JsonServer server; + Spy spy(&server); + JsonUIDAuthority *authority = new JsonUIDAuthority; + QVERIFY(server.listen(socketname, authority)); + + // authority->authorize(geteuid()); + Child child("testClient/testClient", QStringList() << "-socket" << socketname); + + spy.waitFailed(); + child.waitForFinished(); + delete authority; +} + + +void tst_JsonStream::formatTest() +{ + QString socketname = "/tmp/tst_socket"; + + QStringList formats = QStringList() << "qbjs" << "bson" << "utf8"; + + foreach (const QString& format, formats) { + BasicServer server(socketname); + QSignalSpy spy(&server, SIGNAL(messageReceived(const QJsonObject&))); + QTime stopWatch; + + Child child("testClient/testClient", + QStringList() << "-socket" << socketname << "-format" << format); + + stopWatch.start(); + forever { + QTestEventLoop::instance().enterLoop(1); + if (stopWatch.elapsed() >= 5000) + QFAIL("Timed out"); + if (spy.count()) + break; + } + + if (format == "qbjs") + QVERIFY(server.format() == FormatQBJS); + else if (format == "bson") + QVERIFY(server.format() == FormatBSON); + else if (format == "utf8") + QVERIFY(server.format() == FormatUTF8); + else + QFAIL("Unrecognized format"); + + QJsonObject msg = qvariant_cast<QJsonObject>(spy.last().at(0)); + QVERIFY(msg.value("text").isString() && msg.value("text").toString() == QLatin1String("Standard text")); + QVERIFY(msg.value("int").isDouble() && msg.value("int").toDouble() == 100); + QVERIFY(msg.value("float").isDouble() && msg.value("float").toDouble() == 100.0); + QVERIFY(msg.value("true").isBool() && msg.value("true").toBool() == true); + QVERIFY(msg.value("false").isBool() && msg.value("false").toBool() == false); + QVERIFY(msg.value("array").isArray()); + QJsonArray array = msg.value("array").toArray(); + QVERIFY(array.size() == 3); + QVERIFY(array.at(0).toString() == "one"); + QVERIFY(array.at(1).toString() == "two"); + QVERIFY(array.at(2).toString() == "three"); + QVERIFY(msg.value("object").isObject()); + QJsonObject object = msg.value("object").toObject(); + QVERIFY(object.value("item1").toString() == "This is item 1"); + QVERIFY(object.value("item2").toString() == "This is item 2"); + + msg.insert("command", QLatin1String("exit")); + server.send(msg); + server.waitDisconnect(); + child.waitForFinished(); + } +} + + +QTEST_MAIN(tst_JsonStream) + +#include "tst_jsonstream.moc" diff --git a/tests/auto/testsuite/.gitignore b/tests/auto/testsuite/.gitignore new file mode 100644 index 0000000..b7c731a --- /dev/null +++ b/tests/auto/testsuite/.gitignore @@ -0,0 +1 @@ +tst_jsonstream diff --git a/tests/auto/testsuite/testsuite.pro b/tests/auto/testsuite/testsuite.pro new file mode 100644 index 0000000..8d9b07a --- /dev/null +++ b/tests/auto/testsuite/testsuite.pro @@ -0,0 +1,11 @@ +CONFIG += testcase +CONFIG -= app_bundle + +QT = core testlib network + +include(../../../src/server/server.pri) +include(../../../src/client/client.pri) + +HEADERS = tst_jsonclient.h tst_jsonserver.h +SOURCES = tst_jsonserver.cpp tst_jsonclient.cpp tst_jsonstream.cpp +TARGET = test_jsonstream_api diff --git a/tests/auto/testsuite/tst_jsonclient.cpp b/tests/auto/testsuite/tst_jsonclient.cpp new file mode 100644 index 0000000..f2a3954 --- /dev/null +++ b/tests/auto/testsuite/tst_jsonclient.cpp @@ -0,0 +1,86 @@ +/**************************************************************************** +** +** Copyright (C) 2011 Nokia Corporation and/or its subsidiary(-ies). +** All rights reserved. +** Contact: Nokia Corporation (qt-info@nokia.com) +** +** This file is part of the QtAddOn.JsonStream module of the Qt Toolkit. +** +** $QT_BEGIN_LICENSE:LGPL$ +** GNU Lesser General Public License Usage +** This file may be used under the terms of the GNU Lesser General Public +** License version 2.1 as published by the Free Software Foundation and +** appearing in the file LICENSE.LGPL included in the packaging of this +** file. Please review the following information to ensure the GNU Lesser +** General Public License version 2.1 requirements will be met: +** http://www.gnu.org/licenses/old-licenses/lgpl-2.1.html. +** +** In addition, as a special exception, Nokia gives you certain additional +** rights. These rights are described in the Nokia Qt LGPL Exception +** version 1.1, included in the file LGPL_EXCEPTION.txt in this package. +** +** GNU General Public License Usage +** Alternatively, this file may be used under the terms of the GNU General +** Public License version 3.0 as published by the Free Software Foundation +** and appearing in the file LICENSE.GPL included in the packaging of this +** file. Please review the following information to ensure the GNU General +** Public License version 3.0 requirements will be met: +** http://www.gnu.org/copyleft/gpl.html. +** +** Other Usage +** Alternatively, this file may be used in accordance with the terms and +** conditions contained in a signed written agreement between you and Nokia. +** +** +** +** +** +** $QT_END_LICENSE$ +** +****************************************************************************/ + +#include "tst_jsonclient.h" + +#include <QtTest/QtTest> + +tst_JsonClient::tst_JsonClient(const QString& socketname, const QString& strMsg) + : mMsg(strMsg) +{ + qDebug() << Q_FUNC_INFO; + + mClient = new JsonClient; + connect(mClient, SIGNAL(messageReceived(const QJsonObject&)), + this, SLOT(messageReceived(const QJsonObject&))); + mSpyMessageReceived = new QSignalSpy(mClient, SIGNAL(messageReceived(const QJsonObject&))); + + qWarning() << "Connecting to " << socketname; + QVERIFY(mClient->connectLocal(socketname)); + + QJsonObject msg; + msg.insert("note", mMsg); + + qDebug() << "Sending message: " << mMsg; + mClient->send(msg); +} + +tst_JsonClient::~tst_JsonClient() +{ + qDebug() << Q_FUNC_INFO; + + delete mClient; + + delete mSpyMessageReceived; +} + + +void tst_JsonClient::messageReceived(const QJsonObject& message) +{ + qDebug() << Q_FUNC_INFO; + + QString str = message.value("note").toString(); + qDebug() << "Received" << message << str; + + QVERIFY(str == mMsg); + + deleteLater(); +} diff --git a/tests/auto/testsuite/tst_jsonclient.h b/tests/auto/testsuite/tst_jsonclient.h new file mode 100644 index 0000000..a0898df --- /dev/null +++ b/tests/auto/testsuite/tst_jsonclient.h @@ -0,0 +1,68 @@ +/**************************************************************************** +** +** Copyright (C) 2011 Nokia Corporation and/or its subsidiary(-ies). +** All rights reserved. +** Contact: Nokia Corporation (qt-info@nokia.com) +** +** This file is part of the QtAddOn.JsonStream module of the Qt Toolkit. +** +** $QT_BEGIN_LICENSE:LGPL$ +** GNU Lesser General Public License Usage +** This file may be used under the terms of the GNU Lesser General Public +** License version 2.1 as published by the Free Software Foundation and +** appearing in the file LICENSE.LGPL included in the packaging of this +** file. Please review the following information to ensure the GNU Lesser +** General Public License version 2.1 requirements will be met: +** http://www.gnu.org/licenses/old-licenses/lgpl-2.1.html. +** +** In addition, as a special exception, Nokia gives you certain additional +** rights. These rights are described in the Nokia Qt LGPL Exception +** version 1.1, included in the file LGPL_EXCEPTION.txt in this package. +** +** GNU General Public License Usage +** Alternatively, this file may be used under the terms of the GNU General +** Public License version 3.0 as published by the Free Software Foundation +** and appearing in the file LICENSE.GPL included in the packaging of this +** file. Please review the following information to ensure the GNU General +** Public License version 3.0 requirements will be met: +** http://www.gnu.org/copyleft/gpl.html. +** +** Other Usage +** Alternatively, this file may be used in accordance with the terms and +** conditions contained in a signed written agreement between you and Nokia. +** +** +** +** +** +** $QT_END_LICENSE$ +** +****************************************************************************/ + +#ifndef TST_JSONCLIENT_H +#define TST_JSONCLIENT_H + +#include "jsonclient.h" + +class QSignalSpy; + +class tst_JsonClient : public QObject +{ + Q_OBJECT + + void run(); + +public: + tst_JsonClient(const QString& socketname, const QString& message); + ~tst_JsonClient(); + +private slots: + void messageReceived(const QJsonObject& message); + +private: + JsonClient *mClient; + QString mMsg; + QSignalSpy *mSpyMessageReceived; +}; + +#endif diff --git a/tests/auto/testsuite/tst_jsonserver.cpp b/tests/auto/testsuite/tst_jsonserver.cpp new file mode 100644 index 0000000..aa8c400 --- /dev/null +++ b/tests/auto/testsuite/tst_jsonserver.cpp @@ -0,0 +1,196 @@ +/**************************************************************************** +** +** Copyright (C) 2011 Nokia Corporation and/or its subsidiary(-ies). +** All rights reserved. +** Contact: Nokia Corporation (qt-info@nokia.com) +** +** This file is part of the QtAddOn.JsonStream module of the Qt Toolkit. +** +** $QT_BEGIN_LICENSE:LGPL$ +** GNU Lesser General Public License Usage +** This file may be used under the terms of the GNU Lesser General Public +** License version 2.1 as published by the Free Software Foundation and +** appearing in the file LICENSE.LGPL included in the packaging of this +** file. Please review the following information to ensure the GNU Lesser +** General Public License version 2.1 requirements will be met: +** http://www.gnu.org/licenses/old-licenses/lgpl-2.1.html. +** +** In addition, as a special exception, Nokia gives you certain additional +** rights. These rights are described in the Nokia Qt LGPL Exception +** version 1.1, included in the file LGPL_EXCEPTION.txt in this package. +** +** GNU General Public License Usage +** Alternatively, this file may be used under the terms of the GNU General +** Public License version 3.0 as published by the Free Software Foundation +** and appearing in the file LICENSE.GPL included in the packaging of this +** file. Please review the following information to ensure the GNU General +** Public License version 3.0 requirements will be met: +** http://www.gnu.org/copyleft/gpl.html. +** +** Other Usage +** Alternatively, this file may be used in accordance with the terms and +** conditions contained in a signed written agreement between you and Nokia. +** +** +** +** +** +** $QT_END_LICENSE$ +** +****************************************************************************/ + +#include "tst_jsonserver.h" + +#include "jsonpidauthority.h" +#include "jsontokenauthority.h" + +#include <QtTest/QtTest> + +tst_JsonServer::~tst_JsonServer() +{ + qDebug() << Q_FUNC_INFO; + + QCOMPARE(mSpyServerConnectionAdded->count(), 1); // make sure the signal was emitted exactly one time + QCOMPARE(mSpyServerConnectionRemoved->count(), 1); // make sure the signal was emitted exactly one time + // QCOMPARE(mSpyMessageReceived->count(), 1); // make sure the signal was emitted exactly one time + + + delete mServer; + + delete mSpyServerConnectionAdded; + delete mSpyServerConnectionRemoved; + delete mSpyMessageReceived; +} + +void tst_JsonServer::connectionAdded(const QString& id) +{ + qDebug() << Q_FUNC_INFO << id; + QVERIFY(mServer->connections().count() == 1); +} + +void tst_JsonServer::connectionRemoved(const QString& id) +{ + qDebug() << Q_FUNC_INFO << id << (id == mId); + QVERIFY(mServer->connections().count() == 0); + if (id.isEmpty()) + QFAIL("tst_JsonServer::connectionRemoved: empty id"); + if (id != mId) + QFAIL("tst_JsonServer::connectionRemoved: unknown id"); + + mId = ""; +} + +void tst_JsonServer::messageReceived(const QString& id, const QJsonObject& msg) +{ + qDebug() << Q_FUNC_INFO << id << msg; + mId = id; + if (msg.contains("note")) + { + mNote = msg.value("note").toString(); + + qDebug() << "Got note=" << note(); + + QJsonObject msg_reply; + msg_reply.insert("note", mNote); + qDebug() << "Sending" << msg_reply << "to" << mId; + QVERIFY(mServer->hasConnection(mId)); + QVERIFY(mServer->send(mId, msg_reply)); + } +} + +void tst_JsonServer::noAuthTest() +{ + qDebug() << "Running" << Q_FUNC_INFO; + + mServer = new JsonServer; + connect(mServer, SIGNAL(connectionAdded(const QString&)), this, SLOT(connectionAdded(const QString&))); + mSpyServerConnectionAdded = new QSignalSpy(mServer, SIGNAL(connectionAdded(const QString&))); + + connect(mServer, SIGNAL(connectionRemoved(const QString&)), this, SLOT(connectionRemoved(const QString&))); + mSpyServerConnectionRemoved = new QSignalSpy(mServer, SIGNAL(connectionRemoved(const QString&))); + + connect(mServer, SIGNAL(messageReceived(const QString&, const QJsonObject&)), + this, SLOT(messageReceived(const QString&, const QJsonObject&))); + mSpyMessageReceived = new QSignalSpy(mServer, SIGNAL(messageReceived(const QString&, const QJsonObject&))); + + QVERIFY(mServer->connections().count() == 0); + + QVERIFY(mServer->listen(mSocketname)); +} + +void tst_JsonServer::pidAuthorityTest() +{ + qDebug() << "Running" << Q_FUNC_INFO; + +#ifdef MMM // not implemented + JsonPIDAuthority *auth = new JsonPIDAuthority(this); + + pid_t clientPid = getpid(); + QString clientId = "pid_auth_tester"; + + // should fail with empty pid or identifier + QVERIFY(auth->authorize(0, "pid_auth_tester") == false); + // QVERIFY(auth->authorize(pid(), QString::null) == false); + + QVERIFY(auth->authorize(clientPid, clientId)); + // should fail second time with same id + QVERIFY(auth->authorize(clientPid, clientId) == false); + + QVERIFY(auth->deauthorize(clientPid)); + QVERIFY(auth->deauthorize(clientPid) == false); +#endif + + mServer = new JsonServer; + connect(mServer, SIGNAL(connectionAdded(const QString&)), this, SLOT(connectionAdded(const QString&))); + mSpyServerConnectionAdded = new QSignalSpy(mServer, SIGNAL(connectionAdded(const QString&))); + + connect(mServer, SIGNAL(connectionRemoved(const QString&)), this, SLOT(connectionRemoved(const QString&))); + mSpyServerConnectionRemoved = new QSignalSpy(mServer, SIGNAL(connectionRemoved(const QString&))); + + connect(mServer, SIGNAL(messageReceived(const QString&, const QJsonObject&)), + this, SLOT(messageReceived(const QString&, const QJsonObject&))); + mSpyMessageReceived = new QSignalSpy(mServer, SIGNAL(messageReceived(const QString&, const QJsonObject&))); + + QVERIFY(mServer->connections().count() == 0); + + QVERIFY(mServer->listen(mSocketname)); +} + +void tst_JsonServer::tokenAuthorityTest() +{ + qDebug() << "Running" << Q_FUNC_INFO; + + JsonTokenAuthority *auth = new JsonTokenAuthority(this); + + QString clientId = "token_auth_tester"; + QString clientToken = QUuid::createUuid().toString(); + + // should fail with empty token or identifier + QVERIFY(auth->authorize(QString::null, clientId) == false); + QVERIFY(auth->authorize(clientToken, QString::null) == false); + + + QVERIFY(auth->authorize(clientToken, clientId)); + // should fail second time with same id + QVERIFY(auth->authorize(clientToken, clientId) == false); + + QVERIFY(auth->deauthorize(clientToken)); + QVERIFY(auth->deauthorize(clientToken) == false); + + QVERIFY(auth->authorize(clientToken, clientId)); + + mServer = new JsonServer; + connect(mServer, SIGNAL(connectionAdded(const QString&)), this, SLOT(connectionAdded(const QString&))); + mSpyServerConnectionAdded = new QSignalSpy(mServer, SIGNAL(connectionAdded(const QString&))); + + connect(mServer, SIGNAL(connectionRemoved(const QString&)), this, SLOT(connectionRemoved(const QString&))); + mSpyServerConnectionRemoved = new QSignalSpy(mServer, SIGNAL(connectionRemoved(const QString&))); + + connect(mServer, SIGNAL(messageReceived(const QString&, const QJsonObject&)), + this, SLOT(messageReceived(const QString&, const QJsonObject&))); + mSpyMessageReceived = new QSignalSpy(mServer, SIGNAL(messageReceived(const QString&, const QJsonObject&))); + + QVERIFY(mServer->connections().count() == 0); + + QVERIFY(mServer->listen(mSocketname, auth)); +} diff --git a/tests/auto/testsuite/tst_jsonserver.h b/tests/auto/testsuite/tst_jsonserver.h new file mode 100644 index 0000000..6725caa --- /dev/null +++ b/tests/auto/testsuite/tst_jsonserver.h @@ -0,0 +1,87 @@ +/**************************************************************************** +** +** Copyright (C) 2011 Nokia Corporation and/or its subsidiary(-ies). +** All rights reserved. +** Contact: Nokia Corporation (qt-info@nokia.com) +** +** This file is part of the QtAddOn.JsonStream module of the Qt Toolkit. +** +** $QT_BEGIN_LICENSE:LGPL$ +** GNU Lesser General Public License Usage +** This file may be used under the terms of the GNU Lesser General Public +** License version 2.1 as published by the Free Software Foundation and +** appearing in the file LICENSE.LGPL included in the packaging of this +** file. Please review the following information to ensure the GNU Lesser +** General Public License version 2.1 requirements will be met: +** http://www.gnu.org/licenses/old-licenses/lgpl-2.1.html. +** +** In addition, as a special exception, Nokia gives you certain additional +** rights. These rights are described in the Nokia Qt LGPL Exception +** version 1.1, included in the file LGPL_EXCEPTION.txt in this package. +** +** GNU General Public License Usage +** Alternatively, this file may be used under the terms of the GNU General +** Public License version 3.0 as published by the Free Software Foundation +** and appearing in the file LICENSE.GPL included in the packaging of this +** file. Please review the following information to ensure the GNU General +** Public License version 3.0 requirements will be met: +** http://www.gnu.org/copyleft/gpl.html. +** +** Other Usage +** Alternatively, this file may be used in accordance with the terms and +** conditions contained in a signed written agreement between you and Nokia. +** +** +** +** +** +** $QT_END_LICENSE$ +** +****************************************************************************/ + +#ifndef TST_JSONSERVER_H +#define TST_JSONSERVER_H + +#include "jsonserver.h" + +class QSignalSpy; + +class tst_JsonServer : public QObject +{ + Q_OBJECT + +public: + tst_JsonServer(const QString & socketname) + : mSocketname(socketname) + { + } + + ~tst_JsonServer(); + +public: + void noAuthTest(); + void pidAuthorityTest(); + void tokenAuthorityTest(); + + QString note() { return mNote; } + QString id() { return mId; } + +protected slots: + void connectionAdded(const QString& id); + void connectionRemoved(const QString& id); + void messageReceived(const QString& id, const QJsonObject& msg); + +private: + // QProcess *process; + QString mSocketname; + QString mNote; + QString mId; + bool mDone; + + JsonServer *mServer; + QSignalSpy *mSpyServerConnectionAdded; + QSignalSpy *mSpyServerConnectionRemoved; + QSignalSpy *mSpyMessageReceived; +}; + +#endif // TST_JSONSERVER_H diff --git a/tests/auto/testsuite/tst_jsonstream.cpp b/tests/auto/testsuite/tst_jsonstream.cpp new file mode 100644 index 0000000..78e0ac2 --- /dev/null +++ b/tests/auto/testsuite/tst_jsonstream.cpp @@ -0,0 +1,142 @@ +/**************************************************************************** +** +** Copyright (C) 2011 Nokia Corporation and/or its subsidiary(-ies). +** All rights reserved. +** Contact: Nokia Corporation (qt-info@nokia.com) +** +** This file is part of the QtAddOn.JsonStream module of the Qt Toolkit. +** +** $QT_BEGIN_LICENSE:LGPL$ +** GNU Lesser General Public License Usage +** This file may be used under the terms of the GNU Lesser General Public +** License version 2.1 as published by the Free Software Foundation and +** appearing in the file LICENSE.LGPL included in the packaging of this +** file. Please review the following information to ensure the GNU Lesser +** General Public License version 2.1 requirements will be met: +** http://www.gnu.org/licenses/old-licenses/lgpl-2.1.html. +** +** In addition, as a special exception, Nokia gives you certain additional +** rights. These rights are described in the Nokia Qt LGPL Exception +** version 1.1, included in the file LGPL_EXCEPTION.txt in this package. +** +** GNU General Public License Usage +** Alternatively, this file may be used under the terms of the GNU General +** Public License version 3.0 as published by the Free Software Foundation +** and appearing in the file LICENSE.GPL included in the packaging of this +** file. Please review the following information to ensure the GNU General +** Public License version 3.0 requirements will be met: +** http://www.gnu.org/copyleft/gpl.html. +** +** Other Usage +** Alternatively, this file may be used in accordance with the terms and +** conditions contained in a signed written agreement between you and Nokia. +** +** +** +** +** +** $QT_END_LICENSE$ +** +****************************************************************************/ + +#include <QtTest/QtTest> + +#include "tst_jsonclient.h" +#include "tst_jsonserver.h" + +class tst_JsonStream : public QObject +{ + Q_OBJECT + +private slots: + void noAuthTest(); + void pidAuthorityTest(); + void tokenAuthorityTest(); + +private: + void waitResult(tst_JsonServer *); + +}; + +void tst_JsonStream::noAuthTest() +{ + qDebug() << "Running" << Q_FUNC_INFO; + QString socketname = "/tmp/tst_socket"; + QString message = "foobar"; + + tst_JsonServer *server = new tst_JsonServer(socketname); + server->noAuthTest(); + + tst_JsonClient *client = new tst_JsonClient(socketname, message); + + waitResult(server); + + delete server; + + qDebug() << Q_FUNC_INFO << " Completed"; +} + +void tst_JsonStream::pidAuthorityTest() +{ + qDebug() << "Running" << Q_FUNC_INFO; + /* QString socketname = "/tmp/tst_socket"; + QString message = "foobar"; + + tst_JsonServer *server = new tst_JsonServer(socketname); + server->pidAuthorityTest(); + + tst_JsonClient *client = new tst_JsonClient(socketname, message); + + waitResult(server); + + delete server;*/ + + qDebug() << Q_FUNC_INFO << " Completed"; +} + +void tst_JsonStream::tokenAuthorityTest() +{ + qDebug() << "Running" << Q_FUNC_INFO; + QString socketname = "/tmp/tst_socket"; + QString message = "foobar"; + + tst_JsonServer *server = new tst_JsonServer(socketname); + server->tokenAuthorityTest(); + + tst_JsonClient *client = new tst_JsonClient(socketname, message); + + waitResult(server); + + delete server; + + qDebug() << Q_FUNC_INFO << " Completed"; +} + +void tst_JsonStream::waitResult(tst_JsonServer *server) +{ + qDebug() << Q_FUNC_INFO; + + QTime stopWatch; + stopWatch.start(); + forever { + QTestEventLoop::instance().enterLoop(1); + if (stopWatch.elapsed() >= 5000) + QFAIL("Timed out"); + if (!server->note().isEmpty()) + break; + } + + stopWatch.restart(); + forever { + qDebug() << "Waiting for process to enter"; + QTestEventLoop::instance().enterLoop(1); + if (stopWatch.elapsed() >= 5000) + QFAIL("Timed out waiting for client to disconnect"); + if (server->id().isEmpty()) + break; + } +} + +QTEST_MAIN(tst_JsonStream) + +#include "tst_jsonstream.moc" diff --git a/tests/auto/testsuite/tst_jsonstream.h b/tests/auto/testsuite/tst_jsonstream.h new file mode 100644 index 0000000..db5d6d3 --- /dev/null +++ b/tests/auto/testsuite/tst_jsonstream.h @@ -0,0 +1,51 @@ +/**************************************************************************** +** +** Copyright (C) 2011 Nokia Corporation and/or its subsidiary(-ies). +** All rights reserved. +** Contact: Nokia Corporation (qt-info@nokia.com) +** +** This file is part of the QtAddOn.JsonStream module of the Qt Toolkit. +** +** $QT_BEGIN_LICENSE:LGPL$ +** GNU Lesser General Public License Usage +** This file may be used under the terms of the GNU Lesser General Public +** License version 2.1 as published by the Free Software Foundation and +** appearing in the file LICENSE.LGPL included in the packaging of this +** file. Please review the following information to ensure the GNU Lesser +** General Public License version 2.1 requirements will be met: +** http://www.gnu.org/licenses/old-licenses/lgpl-2.1.html. +** +** In addition, as a special exception, Nokia gives you certain additional +** rights. These rights are described in the Nokia Qt LGPL Exception +** version 1.1, included in the file LGPL_EXCEPTION.txt in this package. +** +** GNU General Public License Usage +** Alternatively, this file may be used under the terms of the GNU General +** Public License version 3.0 as published by the Free Software Foundation +** and appearing in the file LICENSE.GPL included in the packaging of this +** file. Please review the following information to ensure the GNU General +** Public License version 3.0 requirements will be met: +** http://www.gnu.org/copyleft/gpl.html. +** +** Other Usage +** Alternatively, this file may be used in accordance with the terms and +** conditions contained in a signed written agreement between you and Nokia. +** +** +** +** +** +** $QT_END_LICENSE$ +** +****************************************************************************/ + +#ifndef TST_JSONSTREAM_H +#define TST_JSONSTREAM_H + +class tst_jsonstream +{ +public: + tst_jsonstream(); +}; + +#endif // TST_JSONSTREAM_H diff --git a/tests/tests.pro b/tests/tests.pro new file mode 100644 index 0000000..7fbc8a9 --- /dev/null +++ b/tests/tests.pro @@ -0,0 +1,2 @@ +TEMPLATE = subdirs +SUBDIRS = auto |