Restructure tests
This commit is contained in:
parent
a9e79bde97
commit
e36432f905
@ -16,9 +16,6 @@ CONFIG += c++11 console
|
|||||||
QMAKE_CXXFLAGS *= -Werror -std=c++11 -g
|
QMAKE_CXXFLAGS *= -Werror -std=c++11 -g
|
||||||
QMAKE_LFLAGS *= -std=c++11
|
QMAKE_LFLAGS *= -std=c++11
|
||||||
|
|
||||||
INCLUDEPATH += /home/timon/guh/development/cloud/aws-sdk-cpp/build/install/include
|
|
||||||
LIBS += -L/home/timon/guh/development/cloud/aws-sdk-cpp/build/install/lib -laws-cpp-sdk-core -laws-cpp-sdk-lambda
|
|
||||||
|
|
||||||
top_srcdir=$$PWD
|
top_srcdir=$$PWD
|
||||||
top_builddir=$$shadowed($$PWD)
|
top_builddir=$$shadowed($$PWD)
|
||||||
|
|
||||||
|
|||||||
@ -1,4 +1,4 @@
|
|||||||
#include "nymea-remoteproxy-tests.h"
|
#include "nymea-remoteproxy-tests-offline.h"
|
||||||
|
|
||||||
#include "engine.h"
|
#include "engine.h"
|
||||||
#include "loggingcategories.h"
|
#include "loggingcategories.h"
|
||||||
@ -11,200 +11,9 @@
|
|||||||
#include <QWebSocketServer>
|
#include <QWebSocketServer>
|
||||||
|
|
||||||
RemoteProxyTests::RemoteProxyTests(QObject *parent) :
|
RemoteProxyTests::RemoteProxyTests(QObject *parent) :
|
||||||
QObject(parent)
|
BaseTest(parent)
|
||||||
{
|
{
|
||||||
m_configuration = new ProxyConfiguration(this);
|
|
||||||
|
|
||||||
QFile certificateFile(":/test-certificate.crt");
|
|
||||||
if (!certificateFile.open(QIODevice::ReadOnly)) {
|
|
||||||
qWarning() << "Could not open resource file" << certificateFile.fileName();
|
|
||||||
exit(1);
|
|
||||||
}
|
|
||||||
|
|
||||||
QByteArray certificateData = certificateFile.readAll();
|
|
||||||
//qDebug() << "Certificate:" << endl << qUtf8Printable(certificateData);
|
|
||||||
|
|
||||||
QFile keyFile(":/test-certificate.key");
|
|
||||||
if (!keyFile.open(QIODevice::ReadOnly)) {
|
|
||||||
qWarning() << "Could not open resource file" << keyFile.fileName();
|
|
||||||
exit(1);
|
|
||||||
}
|
|
||||||
|
|
||||||
QByteArray keyData = keyFile.readAll();
|
|
||||||
//qDebug() << "Certificate key:" << endl << qUtf8Printable(keyData);
|
|
||||||
m_authenticator = new MockAuthenticator(this);
|
|
||||||
|
|
||||||
m_sslConfiguration.setPrivateKey(QSslKey(keyData, QSsl::Rsa, QSsl::Pem, QSsl::PrivateKey));
|
|
||||||
m_sslConfiguration.setLocalCertificate(QSslCertificate(certificateData, QSsl::Pem));
|
|
||||||
m_sslConfiguration.setPeerVerifyMode(QSslSocket::VerifyNone);
|
|
||||||
m_sslConfiguration.setProtocol(QSsl::TlsV1_2OrLater);
|
|
||||||
|
|
||||||
m_testToken = "eyJraWQiOiJXdnFFT3prVVh5VDlINzFyRUpoNWdxRnkxNFhnR2l3SFAzVEIzUFQ1V3ZrPSIsImFsZyI6IlJT"
|
|
||||||
"MjU2In0.eyJzdWIiOiJmZTJmZDNlNC1hMGJhLTQ1OTUtOWRiZS00ZDkxYjRiMjFlMzUiLCJhdWQiOiI4cmpoZ"
|
|
||||||
"mRsZjlqZjFzdW9rMmpjcmx0ZDZ2IiwiZW1haWxfdmVyaWZpZWQiOnRydWUsImV2ZW50X2lkIjoiN2Y5NTRiNm"
|
|
||||||
"ItOTYyZi0xMWU4LWI0ZjItMzU5NWRiZmRiODVmIiwidG9rZW5fdXNlIjoiaWQiLCJhdXRoX3RpbWUiOjE1MzM"
|
|
||||||
"xOTkxMzgsImlzcyI6Imh0dHBzOlwvXC9jb2duaXRvLWlkcC5ldS13ZXN0LTEuYW1hem9uYXdzLmNvbVwvZXUt"
|
|
||||||
"d2VzdC0xXzZlWDZZam1YciIsImNvZ25pdG86dXNlcm5hbWUiOiIyYzE3YzUwZC0xZDFlLTRhM2UtYmFjOS0zZ"
|
|
||||||
"DI0YjQ1MTFiYWEiLCJleHAiOjE1MzMyMDI3MzgsImlhdCI6MTUzMzE5OTEzOCwiZW1haWwiOiJqZW5raW5zQG"
|
|
||||||
"d1aC5pbyJ9.hMMSvZMx7pMvV70PaUmTZOZgdez5WGX5yagRFPZojBm8jNWZND1lUmi0RFkybeD4HonDiKHxTF"
|
|
||||||
"_psyJoBVndgHbxYBBl3Np4gn0MxECWjvLxYzGxVBBkN24SqNUyAGkr0uFcZKkBecdtJlqNQnZN8Uk49twmODf"
|
|
||||||
"raRaRmGmKmRBAK1qDITpUgP6AWqH9xoJWaoDzt0kwJ3EtPxS7vL1PHqOaN8ggXA8Eq4iTCSfXU1HAXhIWJH9Y"
|
|
||||||
"pQbj58v1vktaAEATdmKmlzmcix-HJK9wWHRSuv3TsNa8DGxvcPOoeTu8Vql7krZ-y7Zu-s2WsgeP4VxyT80VE"
|
|
||||||
"T_xh6pMkOhE6g";
|
|
||||||
|
|
||||||
}
|
|
||||||
|
|
||||||
void RemoteProxyTests::cleanUpEngine()
|
|
||||||
{
|
|
||||||
if (Engine::exists()) {
|
|
||||||
Engine::instance()->stop();
|
|
||||||
Engine::instance()->destroy();
|
|
||||||
QVERIFY(!Engine::exists());
|
|
||||||
}
|
|
||||||
}
|
|
||||||
|
|
||||||
void RemoteProxyTests::restartEngine()
|
|
||||||
{
|
|
||||||
cleanUpEngine();
|
|
||||||
startEngine();
|
|
||||||
}
|
|
||||||
|
|
||||||
void RemoteProxyTests::startEngine()
|
|
||||||
{
|
|
||||||
if (!Engine::exists()) {
|
|
||||||
QString serverName = "nymea-remoteproxy-testserver";
|
|
||||||
Engine::instance()->setAuthenticator(m_authenticator);
|
|
||||||
Engine::instance()->setServerName(serverName);
|
|
||||||
Engine::instance()->setConfiguration(m_configuration);
|
|
||||||
Engine::instance()->setSslConfiguration(m_sslConfiguration);
|
|
||||||
Engine::instance()->setDeveloperModeEnabled(true);
|
|
||||||
|
|
||||||
QVERIFY(Engine::exists());
|
|
||||||
QVERIFY(Engine::instance()->serverName() == serverName);
|
|
||||||
}
|
|
||||||
}
|
|
||||||
|
|
||||||
void RemoteProxyTests::startServer()
|
|
||||||
{
|
|
||||||
startEngine();
|
|
||||||
|
|
||||||
if (!Engine::instance()->running()) {
|
|
||||||
QSignalSpy runningSpy(Engine::instance(), &Engine::runningChanged);
|
|
||||||
Engine::instance()->setConfiguration(m_configuration);
|
|
||||||
Engine::instance()->setDeveloperModeEnabled(true);
|
|
||||||
Engine::instance()->start();
|
|
||||||
runningSpy.wait();
|
|
||||||
QVERIFY(runningSpy.count() == 1);
|
|
||||||
}
|
|
||||||
|
|
||||||
QVERIFY(Engine::instance()->running());
|
|
||||||
QVERIFY(Engine::instance()->webSocketServer()->running());
|
|
||||||
}
|
|
||||||
|
|
||||||
void RemoteProxyTests::stopServer()
|
|
||||||
{
|
|
||||||
if (!Engine::instance()->running())
|
|
||||||
return;
|
|
||||||
|
|
||||||
Engine::instance()->stop();
|
|
||||||
QVERIFY(!Engine::instance()->running());
|
|
||||||
}
|
|
||||||
|
|
||||||
QVariant RemoteProxyTests::invokeApiCall(const QString &method, const QVariantMap params, bool remainsConnected)
|
|
||||||
{
|
|
||||||
Q_UNUSED(remainsConnected)
|
|
||||||
|
|
||||||
QVariantMap request;
|
|
||||||
request.insert("id", m_commandCounter);
|
|
||||||
request.insert("method", method);
|
|
||||||
request.insert("params", params);
|
|
||||||
QJsonDocument jsonDoc = QJsonDocument::fromVariant(request);
|
|
||||||
|
|
||||||
QWebSocket *socket = new QWebSocket("proxy-testclient", QWebSocketProtocol::Version13);
|
|
||||||
connect(socket, &QWebSocket::sslErrors, this, &RemoteProxyTests::sslErrors);
|
|
||||||
QSignalSpy spyConnection(socket, SIGNAL(connected()));
|
|
||||||
socket->open(Engine::instance()->webSocketServer()->serverUrl());
|
|
||||||
spyConnection.wait();
|
|
||||||
if (spyConnection.count() == 0) {
|
|
||||||
return QVariant();
|
|
||||||
}
|
|
||||||
|
|
||||||
QSignalSpy dataSpy(socket, SIGNAL(textMessageReceived(QString)));
|
|
||||||
socket->sendTextMessage(QString(jsonDoc.toJson(QJsonDocument::Compact)));
|
|
||||||
dataSpy.wait();
|
|
||||||
|
|
||||||
socket->close();
|
|
||||||
socket->deleteLater();
|
|
||||||
|
|
||||||
for (int i = 0; i < dataSpy.count(); i++) {
|
|
||||||
// Make sure the response it a valid JSON string
|
|
||||||
QJsonParseError error;
|
|
||||||
jsonDoc = QJsonDocument::fromJson(dataSpy.at(i).last().toByteArray(), &error);
|
|
||||||
if (error.error != QJsonParseError::NoError) {
|
|
||||||
qWarning() << "JSON parser error" << error.errorString();
|
|
||||||
return QVariant();
|
|
||||||
}
|
|
||||||
QVariantMap response = jsonDoc.toVariant().toMap();
|
|
||||||
|
|
||||||
// skip notifications
|
|
||||||
if (response.contains("notification"))
|
|
||||||
continue;
|
|
||||||
|
|
||||||
if (response.value("id").toInt() == m_commandCounter) {
|
|
||||||
m_commandCounter++;
|
|
||||||
return jsonDoc.toVariant();
|
|
||||||
}
|
|
||||||
}
|
|
||||||
m_commandCounter++;
|
|
||||||
return QVariant();
|
|
||||||
}
|
|
||||||
|
|
||||||
QVariant RemoteProxyTests::injectSocketData(const QByteArray &data)
|
|
||||||
{
|
|
||||||
QWebSocket *socket = new QWebSocket("proxy-testclient", QWebSocketProtocol::Version13);
|
|
||||||
connect(socket, &QWebSocket::sslErrors, this, &RemoteProxyTests::sslErrors);
|
|
||||||
QSignalSpy spyConnection(socket, SIGNAL(connected()));
|
|
||||||
socket->open(Engine::instance()->webSocketServer()->serverUrl());
|
|
||||||
spyConnection.wait();
|
|
||||||
if (spyConnection.count() == 0) {
|
|
||||||
return QVariant();
|
|
||||||
}
|
|
||||||
|
|
||||||
QSignalSpy spy(socket, SIGNAL(textMessageReceived(QString)));
|
|
||||||
socket->sendTextMessage(QString(data));
|
|
||||||
spy.wait();
|
|
||||||
|
|
||||||
socket->close();
|
|
||||||
socket->deleteLater();
|
|
||||||
|
|
||||||
for (int i = 0; i < spy.count(); i++) {
|
|
||||||
// Make sure the response it a valid JSON string
|
|
||||||
QJsonParseError error;
|
|
||||||
QJsonDocument jsonDoc = QJsonDocument::fromJson(spy.at(i).last().toByteArray(), &error);
|
|
||||||
if (error.error != QJsonParseError::NoError) {
|
|
||||||
qWarning() << "JSON parser error" << error.errorString();
|
|
||||||
return QVariant();
|
|
||||||
}
|
|
||||||
return jsonDoc.toVariant();
|
|
||||||
}
|
|
||||||
m_commandCounter++;
|
|
||||||
return QVariant();
|
|
||||||
}
|
|
||||||
|
|
||||||
void RemoteProxyTests::initTestCase()
|
|
||||||
{
|
|
||||||
qRegisterMetaType<RemoteProxyConnection::Error>();
|
|
||||||
qRegisterMetaType<RemoteProxyConnection::State>();
|
|
||||||
qRegisterMetaType<RemoteProxyConnection::ConnectionType>();
|
|
||||||
|
|
||||||
qCDebug(dcApplication()) << "Init test case.";
|
|
||||||
restartEngine();
|
|
||||||
}
|
|
||||||
|
|
||||||
void RemoteProxyTests::cleanupTestCase()
|
|
||||||
{
|
|
||||||
qCDebug(dcApplication()) << "Clean up test case.";
|
|
||||||
cleanUpEngine();
|
|
||||||
}
|
}
|
||||||
|
|
||||||
void RemoteProxyTests::startStopServer()
|
void RemoteProxyTests::startStopServer()
|
||||||
@ -213,6 +22,42 @@ void RemoteProxyTests::startStopServer()
|
|||||||
stopServer();
|
stopServer();
|
||||||
}
|
}
|
||||||
|
|
||||||
|
void RemoteProxyTests::dummyAuthenticator()
|
||||||
|
{
|
||||||
|
cleanUpEngine();
|
||||||
|
DummyAuthenticator *dummyAuthenticator = new DummyAuthenticator(this);
|
||||||
|
|
||||||
|
// Start proxy webserver
|
||||||
|
Engine::instance()->setConfiguration(m_configuration);
|
||||||
|
Engine::instance()->setAuthenticator(dummyAuthenticator);
|
||||||
|
Engine::instance()->setSslConfiguration(m_sslConfiguration);
|
||||||
|
|
||||||
|
QSignalSpy runningSpy(Engine::instance(), &Engine::runningChanged);
|
||||||
|
Engine::instance()->start();
|
||||||
|
runningSpy.wait();
|
||||||
|
|
||||||
|
QVERIFY(runningSpy.count() == 1);
|
||||||
|
|
||||||
|
// Make sure the server is not running
|
||||||
|
QVERIFY(Engine::instance()->running());
|
||||||
|
|
||||||
|
// Make sure the websocket server is not running
|
||||||
|
QVERIFY(Engine::instance()->webSocketServer()->running());
|
||||||
|
|
||||||
|
// Create request
|
||||||
|
QVariantMap params;
|
||||||
|
params.insert("uuid", QUuid::createUuid().toString());
|
||||||
|
params.insert("name", "test");
|
||||||
|
params.insert("token", "foobar");
|
||||||
|
|
||||||
|
QVariant response = invokeApiCall("Authentication.Authenticate", params);
|
||||||
|
qDebug() << qUtf8Printable(QJsonDocument::fromVariant(response).toJson(QJsonDocument::Indented));
|
||||||
|
verifyAuthenticationError(response);
|
||||||
|
|
||||||
|
dummyAuthenticator->deleteLater();
|
||||||
|
cleanUpEngine();
|
||||||
|
}
|
||||||
|
|
||||||
void RemoteProxyTests::webserverConnectionBlocked()
|
void RemoteProxyTests::webserverConnectionBlocked()
|
||||||
{
|
{
|
||||||
// Create a dummy server which blocks the port
|
// Create a dummy server which blocks the port
|
||||||
51
tests/offline/nymea-remoteproxy-tests-offline.h
Normal file
51
tests/offline/nymea-remoteproxy-tests-offline.h
Normal file
@ -0,0 +1,51 @@
|
|||||||
|
#ifndef NYMEA_REMOTEPROXY_TESTS_H
|
||||||
|
#define NYMEA_REMOTEPROXY_TESTS_H
|
||||||
|
|
||||||
|
#include <QUrl>
|
||||||
|
#include <QtTest>
|
||||||
|
#include <QSslKey>
|
||||||
|
#include <QObject>
|
||||||
|
#include <QWebSocket>
|
||||||
|
#include <QHostAddress>
|
||||||
|
#include <QSslCertificate>
|
||||||
|
#include <QSslConfiguration>
|
||||||
|
|
||||||
|
#include "basetest.h"
|
||||||
|
|
||||||
|
using namespace remoteproxy;
|
||||||
|
using namespace remoteproxyclient;
|
||||||
|
|
||||||
|
class RemoteProxyTests : public BaseTest
|
||||||
|
{
|
||||||
|
Q_OBJECT
|
||||||
|
public:
|
||||||
|
explicit RemoteProxyTests(QObject *parent = nullptr);
|
||||||
|
|
||||||
|
private slots:
|
||||||
|
// Basic stuff
|
||||||
|
void startStopServer();
|
||||||
|
void dummyAuthenticator();
|
||||||
|
|
||||||
|
// WebSocket connection
|
||||||
|
void webserverConnectionBlocked();
|
||||||
|
void webserverConnection();
|
||||||
|
|
||||||
|
// Api
|
||||||
|
void getIntrospect();
|
||||||
|
void getHello();
|
||||||
|
|
||||||
|
void apiBasicCalls_data();
|
||||||
|
void apiBasicCalls();
|
||||||
|
|
||||||
|
void authenticate_data();
|
||||||
|
void authenticate();
|
||||||
|
|
||||||
|
// Client lib
|
||||||
|
void clientConnection();
|
||||||
|
void remoteConnection();
|
||||||
|
void sslConfigurations();
|
||||||
|
void timeout();
|
||||||
|
|
||||||
|
};
|
||||||
|
|
||||||
|
#endif // NYMEA_REMOTEPROXY_TESTS_H
|
||||||
14
tests/offline/offline.pro
Normal file
14
tests/offline/offline.pro
Normal file
@ -0,0 +1,14 @@
|
|||||||
|
include(../../nymea-remoteproxy.pri)
|
||||||
|
include(../testbase/testbase.pri)
|
||||||
|
|
||||||
|
CONFIG += testcase
|
||||||
|
QT += testlib
|
||||||
|
|
||||||
|
TARGET = nymea-remoteproxy-tests-offline
|
||||||
|
|
||||||
|
HEADERS += nymea-remoteproxy-tests-offline.h
|
||||||
|
|
||||||
|
SOURCES += nymea-remoteproxy-tests-offline.cpp
|
||||||
|
|
||||||
|
target.path = /usr/bin
|
||||||
|
INSTALLS += target
|
||||||
2
tests/online/online.pro
Normal file
2
tests/online/online.pro
Normal file
@ -0,0 +1,2 @@
|
|||||||
|
include(../../nymea-remoteproxy.pri)
|
||||||
|
|
||||||
209
tests/testbase/basetest.cpp
Normal file
209
tests/testbase/basetest.cpp
Normal file
@ -0,0 +1,209 @@
|
|||||||
|
#include "basetest.h"
|
||||||
|
|
||||||
|
#include "engine.h"
|
||||||
|
#include "loggingcategories.h"
|
||||||
|
#include "remoteproxyconnection.h"
|
||||||
|
|
||||||
|
#include <QMetaType>
|
||||||
|
#include <QSignalSpy>
|
||||||
|
#include <QWebSocket>
|
||||||
|
#include <QJsonDocument>
|
||||||
|
#include <QWebSocketServer>
|
||||||
|
|
||||||
|
BaseTest::BaseTest(QObject *parent) :
|
||||||
|
QObject(parent)
|
||||||
|
{
|
||||||
|
m_configuration = new ProxyConfiguration(this);
|
||||||
|
|
||||||
|
QFile certificateFile(":/test-certificate.crt");
|
||||||
|
if (!certificateFile.open(QIODevice::ReadOnly)) {
|
||||||
|
qWarning() << "Could not open resource file" << certificateFile.fileName();
|
||||||
|
exit(1);
|
||||||
|
}
|
||||||
|
|
||||||
|
QByteArray certificateData = certificateFile.readAll();
|
||||||
|
//qDebug() << "Certificate:" << endl << qUtf8Printable(certificateData);
|
||||||
|
|
||||||
|
QFile keyFile(":/test-certificate.key");
|
||||||
|
if (!keyFile.open(QIODevice::ReadOnly)) {
|
||||||
|
qWarning() << "Could not open resource file" << keyFile.fileName();
|
||||||
|
exit(1);
|
||||||
|
}
|
||||||
|
|
||||||
|
QByteArray keyData = keyFile.readAll();
|
||||||
|
//qDebug() << "Certificate key:" << endl << qUtf8Printable(keyData);
|
||||||
|
m_authenticator = new MockAuthenticator(this);
|
||||||
|
|
||||||
|
m_sslConfiguration.setPrivateKey(QSslKey(keyData, QSsl::Rsa, QSsl::Pem, QSsl::PrivateKey));
|
||||||
|
m_sslConfiguration.setLocalCertificate(QSslCertificate(certificateData, QSsl::Pem));
|
||||||
|
m_sslConfiguration.setPeerVerifyMode(QSslSocket::VerifyNone);
|
||||||
|
m_sslConfiguration.setProtocol(QSsl::TlsV1_2OrLater);
|
||||||
|
|
||||||
|
m_testToken = "eyJraWQiOiJXdnFFT3prVVh5VDlINzFyRUpoNWdxRnkxNFhnR2l3SFAzVEIzUFQ1V3ZrPSIsImFsZyI6IlJT"
|
||||||
|
"MjU2In0.eyJzdWIiOiJmZTJmZDNlNC1hMGJhLTQ1OTUtOWRiZS00ZDkxYjRiMjFlMzUiLCJhdWQiOiI4cmpoZ"
|
||||||
|
"mRsZjlqZjFzdW9rMmpjcmx0ZDZ2IiwiZW1haWxfdmVyaWZpZWQiOnRydWUsImV2ZW50X2lkIjoiN2Y5NTRiNm"
|
||||||
|
"ItOTYyZi0xMWU4LWI0ZjItMzU5NWRiZmRiODVmIiwidG9rZW5fdXNlIjoiaWQiLCJhdXRoX3RpbWUiOjE1MzM"
|
||||||
|
"xOTkxMzgsImlzcyI6Imh0dHBzOlwvXC9jb2duaXRvLWlkcC5ldS13ZXN0LTEuYW1hem9uYXdzLmNvbVwvZXUt"
|
||||||
|
"d2VzdC0xXzZlWDZZam1YciIsImNvZ25pdG86dXNlcm5hbWUiOiIyYzE3YzUwZC0xZDFlLTRhM2UtYmFjOS0zZ"
|
||||||
|
"DI0YjQ1MTFiYWEiLCJleHAiOjE1MzMyMDI3MzgsImlhdCI6MTUzMzE5OTEzOCwiZW1haWwiOiJqZW5raW5zQG"
|
||||||
|
"d1aC5pbyJ9.hMMSvZMx7pMvV70PaUmTZOZgdez5WGX5yagRFPZojBm8jNWZND1lUmi0RFkybeD4HonDiKHxTF"
|
||||||
|
"_psyJoBVndgHbxYBBl3Np4gn0MxECWjvLxYzGxVBBkN24SqNUyAGkr0uFcZKkBecdtJlqNQnZN8Uk49twmODf"
|
||||||
|
"raRaRmGmKmRBAK1qDITpUgP6AWqH9xoJWaoDzt0kwJ3EtPxS7vL1PHqOaN8ggXA8Eq4iTCSfXU1HAXhIWJH9Y"
|
||||||
|
"pQbj58v1vktaAEATdmKmlzmcix-HJK9wWHRSuv3TsNa8DGxvcPOoeTu8Vql7krZ-y7Zu-s2WsgeP4VxyT80VE"
|
||||||
|
"T_xh6pMkOhE6g";
|
||||||
|
|
||||||
|
}
|
||||||
|
|
||||||
|
void BaseTest::cleanUpEngine()
|
||||||
|
{
|
||||||
|
if (Engine::exists()) {
|
||||||
|
Engine::instance()->stop();
|
||||||
|
Engine::instance()->destroy();
|
||||||
|
QVERIFY(!Engine::exists());
|
||||||
|
}
|
||||||
|
}
|
||||||
|
|
||||||
|
void BaseTest::restartEngine()
|
||||||
|
{
|
||||||
|
cleanUpEngine();
|
||||||
|
startEngine();
|
||||||
|
}
|
||||||
|
|
||||||
|
void BaseTest::startEngine()
|
||||||
|
{
|
||||||
|
if (!Engine::exists()) {
|
||||||
|
QString serverName = "nymea-remoteproxy-testserver";
|
||||||
|
Engine::instance()->setAuthenticator(m_authenticator);
|
||||||
|
Engine::instance()->setServerName(serverName);
|
||||||
|
Engine::instance()->setConfiguration(m_configuration);
|
||||||
|
Engine::instance()->setSslConfiguration(m_sslConfiguration);
|
||||||
|
Engine::instance()->setDeveloperModeEnabled(true);
|
||||||
|
|
||||||
|
QVERIFY(Engine::exists());
|
||||||
|
QVERIFY(Engine::instance()->serverName() == serverName);
|
||||||
|
}
|
||||||
|
}
|
||||||
|
|
||||||
|
void BaseTest::startServer()
|
||||||
|
{
|
||||||
|
startEngine();
|
||||||
|
|
||||||
|
if (!Engine::instance()->running()) {
|
||||||
|
QSignalSpy runningSpy(Engine::instance(), &Engine::runningChanged);
|
||||||
|
Engine::instance()->setConfiguration(m_configuration);
|
||||||
|
Engine::instance()->setDeveloperModeEnabled(true);
|
||||||
|
Engine::instance()->start();
|
||||||
|
runningSpy.wait();
|
||||||
|
QVERIFY(runningSpy.count() == 1);
|
||||||
|
}
|
||||||
|
|
||||||
|
QVERIFY(Engine::instance()->running());
|
||||||
|
QVERIFY(Engine::instance()->webSocketServer()->running());
|
||||||
|
}
|
||||||
|
|
||||||
|
void BaseTest::stopServer()
|
||||||
|
{
|
||||||
|
if (!Engine::instance()->running())
|
||||||
|
return;
|
||||||
|
|
||||||
|
Engine::instance()->stop();
|
||||||
|
QVERIFY(!Engine::instance()->running());
|
||||||
|
}
|
||||||
|
|
||||||
|
QVariant BaseTest::invokeApiCall(const QString &method, const QVariantMap params, bool remainsConnected)
|
||||||
|
{
|
||||||
|
Q_UNUSED(remainsConnected)
|
||||||
|
|
||||||
|
QVariantMap request;
|
||||||
|
request.insert("id", m_commandCounter);
|
||||||
|
request.insert("method", method);
|
||||||
|
request.insert("params", params);
|
||||||
|
QJsonDocument jsonDoc = QJsonDocument::fromVariant(request);
|
||||||
|
|
||||||
|
QWebSocket *socket = new QWebSocket("proxy-testclient", QWebSocketProtocol::Version13);
|
||||||
|
connect(socket, &QWebSocket::sslErrors, this, &BaseTest::sslErrors);
|
||||||
|
QSignalSpy spyConnection(socket, SIGNAL(connected()));
|
||||||
|
socket->open(Engine::instance()->webSocketServer()->serverUrl());
|
||||||
|
spyConnection.wait();
|
||||||
|
if (spyConnection.count() == 0) {
|
||||||
|
return QVariant();
|
||||||
|
}
|
||||||
|
|
||||||
|
QSignalSpy dataSpy(socket, SIGNAL(textMessageReceived(QString)));
|
||||||
|
socket->sendTextMessage(QString(jsonDoc.toJson(QJsonDocument::Compact)));
|
||||||
|
dataSpy.wait();
|
||||||
|
|
||||||
|
socket->close();
|
||||||
|
socket->deleteLater();
|
||||||
|
|
||||||
|
for (int i = 0; i < dataSpy.count(); i++) {
|
||||||
|
// Make sure the response it a valid JSON string
|
||||||
|
QJsonParseError error;
|
||||||
|
jsonDoc = QJsonDocument::fromJson(dataSpy.at(i).last().toByteArray(), &error);
|
||||||
|
if (error.error != QJsonParseError::NoError) {
|
||||||
|
qWarning() << "JSON parser error" << error.errorString();
|
||||||
|
return QVariant();
|
||||||
|
}
|
||||||
|
QVariantMap response = jsonDoc.toVariant().toMap();
|
||||||
|
|
||||||
|
// skip notifications
|
||||||
|
if (response.contains("notification"))
|
||||||
|
continue;
|
||||||
|
|
||||||
|
if (response.value("id").toInt() == m_commandCounter) {
|
||||||
|
m_commandCounter++;
|
||||||
|
return jsonDoc.toVariant();
|
||||||
|
}
|
||||||
|
}
|
||||||
|
m_commandCounter++;
|
||||||
|
return QVariant();
|
||||||
|
}
|
||||||
|
|
||||||
|
QVariant BaseTest::injectSocketData(const QByteArray &data)
|
||||||
|
{
|
||||||
|
QWebSocket *socket = new QWebSocket("proxy-testclient", QWebSocketProtocol::Version13);
|
||||||
|
connect(socket, &QWebSocket::sslErrors, this, &BaseTest::sslErrors);
|
||||||
|
QSignalSpy spyConnection(socket, SIGNAL(connected()));
|
||||||
|
socket->open(Engine::instance()->webSocketServer()->serverUrl());
|
||||||
|
spyConnection.wait();
|
||||||
|
if (spyConnection.count() == 0) {
|
||||||
|
return QVariant();
|
||||||
|
}
|
||||||
|
|
||||||
|
QSignalSpy spy(socket, SIGNAL(textMessageReceived(QString)));
|
||||||
|
socket->sendTextMessage(QString(data));
|
||||||
|
spy.wait();
|
||||||
|
|
||||||
|
socket->close();
|
||||||
|
socket->deleteLater();
|
||||||
|
|
||||||
|
for (int i = 0; i < spy.count(); i++) {
|
||||||
|
// Make sure the response it a valid JSON string
|
||||||
|
QJsonParseError error;
|
||||||
|
QJsonDocument jsonDoc = QJsonDocument::fromJson(spy.at(i).last().toByteArray(), &error);
|
||||||
|
if (error.error != QJsonParseError::NoError) {
|
||||||
|
qWarning() << "JSON parser error" << error.errorString();
|
||||||
|
return QVariant();
|
||||||
|
}
|
||||||
|
return jsonDoc.toVariant();
|
||||||
|
}
|
||||||
|
m_commandCounter++;
|
||||||
|
return QVariant();
|
||||||
|
}
|
||||||
|
|
||||||
|
void BaseTest::initTestCase()
|
||||||
|
{
|
||||||
|
qRegisterMetaType<RemoteProxyConnection::Error>();
|
||||||
|
qRegisterMetaType<RemoteProxyConnection::State>();
|
||||||
|
qRegisterMetaType<RemoteProxyConnection::ConnectionType>();
|
||||||
|
|
||||||
|
qCDebug(dcApplication()) << "Init test case.";
|
||||||
|
restartEngine();
|
||||||
|
}
|
||||||
|
|
||||||
|
void BaseTest::cleanupTestCase()
|
||||||
|
{
|
||||||
|
qCDebug(dcApplication()) << "Clean up test case.";
|
||||||
|
cleanUpEngine();
|
||||||
|
}
|
||||||
|
|
||||||
@ -1,7 +1,7 @@
|
|||||||
#ifndef NYMEA_REMOTEPROXY_TESTS_H
|
#ifndef BASETEST_H
|
||||||
#define NYMEA_REMOTEPROXY_TESTS_H
|
#define BASETEST_H
|
||||||
|
|
||||||
#include <QUrl>
|
#include <QObject>
|
||||||
#include <QtTest>
|
#include <QtTest>
|
||||||
#include <QSslKey>
|
#include <QSslKey>
|
||||||
#include <QObject>
|
#include <QObject>
|
||||||
@ -14,17 +14,18 @@
|
|||||||
#include "mockauthenticator.h"
|
#include "mockauthenticator.h"
|
||||||
#include "proxyconfiguration.h"
|
#include "proxyconfiguration.h"
|
||||||
#include "remoteproxyconnection.h"
|
#include "remoteproxyconnection.h"
|
||||||
|
#include "authentication/dummyauthenticator.h"
|
||||||
|
|
||||||
using namespace remoteproxy;
|
using namespace remoteproxy;
|
||||||
using namespace remoteproxyclient;
|
using namespace remoteproxyclient;
|
||||||
|
|
||||||
class RemoteProxyTests : public QObject
|
class BaseTest : public QObject
|
||||||
{
|
{
|
||||||
Q_OBJECT
|
Q_OBJECT
|
||||||
public:
|
public:
|
||||||
explicit RemoteProxyTests(QObject *parent = nullptr);
|
explicit BaseTest(QObject *parent = nullptr);
|
||||||
|
|
||||||
private:
|
protected:
|
||||||
ProxyConfiguration *m_configuration = nullptr;
|
ProxyConfiguration *m_configuration = nullptr;
|
||||||
|
|
||||||
quint16 m_port = 1212;
|
quint16 m_port = 1212;
|
||||||
@ -49,31 +50,6 @@ protected slots:
|
|||||||
void initTestCase();
|
void initTestCase();
|
||||||
void cleanupTestCase();
|
void cleanupTestCase();
|
||||||
|
|
||||||
private slots:
|
|
||||||
// Basic stuff
|
|
||||||
void startStopServer();
|
|
||||||
|
|
||||||
// WebSocket connection
|
|
||||||
void webserverConnectionBlocked();
|
|
||||||
void webserverConnection();
|
|
||||||
|
|
||||||
// Api
|
|
||||||
void getIntrospect();
|
|
||||||
void getHello();
|
|
||||||
|
|
||||||
void apiBasicCalls_data();
|
|
||||||
void apiBasicCalls();
|
|
||||||
|
|
||||||
void authenticate_data();
|
|
||||||
void authenticate();
|
|
||||||
|
|
||||||
// Client lib
|
|
||||||
void clientConnection();
|
|
||||||
void remoteConnection();
|
|
||||||
void sslConfigurations();
|
|
||||||
void timeout();
|
|
||||||
|
|
||||||
|
|
||||||
|
|
||||||
public slots:
|
public slots:
|
||||||
void sslErrors(const QList<QSslError> &) {
|
void sslErrors(const QList<QSslError> &) {
|
||||||
@ -103,6 +79,7 @@ public slots:
|
|||||||
}
|
}
|
||||||
|
|
||||||
|
|
||||||
|
|
||||||
};
|
};
|
||||||
|
|
||||||
#endif // NYMEA_REMOTEPROXY_TESTS_H
|
#endif // BASETEST_H
|
||||||
14
tests/testbase/testbase.pri
Normal file
14
tests/testbase/testbase.pri
Normal file
@ -0,0 +1,14 @@
|
|||||||
|
RESOURCES += ../certificate.qrc
|
||||||
|
|
||||||
|
INCLUDEPATH += $${PWD} $$top_srcdir/libnymea-remoteproxy $$top_srcdir/libnymea-remoteproxyclient
|
||||||
|
LIBS += -L$$top_builddir/libnymea-remoteproxy/ -lnymea-remoteproxy \
|
||||||
|
-L$$top_builddir/libnymea-remoteproxyclient/ -lnymea-remoteproxyclient \
|
||||||
|
|
||||||
|
HEADERS += \
|
||||||
|
$${PWD}/basetest.h \
|
||||||
|
$${PWD}/mockauthenticator.h \
|
||||||
|
|
||||||
|
SOURCES += \
|
||||||
|
$${PWD}/basetest.cpp \
|
||||||
|
$${PWD}/mockauthenticator.cpp \
|
||||||
|
|
||||||
@ -1,20 +1,4 @@
|
|||||||
include(../nymea-remoteproxy.pri)
|
TEMPLATE=subdirs
|
||||||
|
SUBDIRS += offline
|
||||||
|
|
||||||
CONFIG += testcase
|
|
||||||
QT += testlib
|
|
||||||
|
|
||||||
TARGET = nymea-remoteproxy-tests
|
|
||||||
|
|
||||||
INCLUDEPATH += ../libnymea-remoteproxy ../libnymea-remoteproxyclient
|
|
||||||
LIBS += -L$$top_builddir/libnymea-remoteproxy/ -lnymea-remoteproxy \
|
|
||||||
-L$$top_builddir/libnymea-remoteproxyclient/ -lnymea-remoteproxyclient \
|
|
||||||
|
|
||||||
RESOURCES += certificate.qrc
|
|
||||||
|
|
||||||
HEADERS += nymea-remoteproxy-tests.h \
|
|
||||||
mockauthenticator.h
|
|
||||||
SOURCES += nymea-remoteproxy-tests.cpp \
|
|
||||||
mockauthenticator.cpp
|
|
||||||
|
|
||||||
target.path = /usr/bin
|
|
||||||
INSTALLS += target
|
|
||||||
|
|||||||
Reference in New Issue
Block a user