From 8f8e660eb9239fc7d6f7e1b94476d3c8759be00d Mon Sep 17 00:00:00 2001 From: =?UTF-8?q?Simon=20St=C3=BCrz?= Date: Mon, 4 Apr 2022 08:59:10 +0200 Subject: [PATCH 1/7] Fix authentication reply crash if client already disconnected --- .../authentication/aws/awsauthenticator.cpp | 37 ++++++--------- .../authentication/aws/awsauthenticator.h | 4 -- .../jsonrpc/authenticationhandler.cpp | 46 ++++++++----------- .../jsonrpc/authenticationhandler.h | 7 --- 4 files changed, 33 insertions(+), 61 deletions(-) diff --git a/libnymea-remoteproxy/authentication/aws/awsauthenticator.cpp b/libnymea-remoteproxy/authentication/aws/awsauthenticator.cpp index 3e6f7d4..ead9bf9 100644 --- a/libnymea-remoteproxy/authentication/aws/awsauthenticator.cpp +++ b/libnymea-remoteproxy/authentication/aws/awsauthenticator.cpp @@ -51,28 +51,10 @@ QString AwsAuthenticator::name() const return "AWS authenticator"; } -void AwsAuthenticator::onAuthenticationProcessFinished(Authenticator::AuthenticationError error, const UserInformation &userInformation) -{ - AuthenticationProcess *process = static_cast(sender()); - AuthenticationReply *reply = m_runningProcesses.take(process); - - if (error == AuthenticationErrorNoError) { - qCDebug(dcAuthentication()) << name() << reply->proxyClient() << "finished successfully." << userInformation; - } else { - qCDebug(dcAuthentication()) << name() << reply->proxyClient() << "finished with error" << error; - } - - reply->proxyClient()->setUserName(userInformation.email()); - - setReplyError(reply, error); - setReplyFinished(reply); -} - AuthenticationReply *AwsAuthenticator::authenticate(ProxyClient *proxyClient) { qCDebug(dcAuthentication()) << name() << "Start authenticating" << proxyClient; - AuthenticationReply *reply = createAuthenticationReply(proxyClient, this); - + AuthenticationReply *reply = createAuthenticationReply(proxyClient, proxyClient); if (!m_credentialsProvider->isValid()) { qCWarning(dcAuthentication()) << name() << "There are no credentials for authenticating."; setReplyError(reply, AuthenticationErrorProxyError); @@ -83,12 +65,21 @@ AuthenticationReply *AwsAuthenticator::authenticate(ProxyClient *proxyClient) AuthenticationProcess *process = new AuthenticationProcess(m_manager, m_credentialsProvider->accessKey(), m_credentialsProvider->secretAccessKey(), - m_credentialsProvider->sessionToken(), this); + m_credentialsProvider->sessionToken(), reply); - connect(process, &AuthenticationProcess::authenticationFinished, this, &AwsAuthenticator::onAuthenticationProcessFinished); + connect(process, &AuthenticationProcess::authenticationFinished, proxyClient, [=](Authenticator::AuthenticationError error, const UserInformation &userInformation = UserInformation()){ + if (error == AuthenticationErrorNoError) { + qCDebug(dcAuthentication()) << name() << proxyClient << "finished successfully." << userInformation; + } else { + qCDebug(dcAuthentication()) << name() << proxyClient << "finished with error" << error; + } + + proxyClient->setUserName(userInformation.email()); + + setReplyError(reply, error); + setReplyFinished(reply); + }); - // Configure process - m_runningProcesses.insert(process, reply); // Start authentication process process->authenticate(proxyClient->token()); diff --git a/libnymea-remoteproxy/authentication/aws/awsauthenticator.h b/libnymea-remoteproxy/authentication/aws/awsauthenticator.h index d5d66b0..587d34e 100644 --- a/libnymea-remoteproxy/authentication/aws/awsauthenticator.h +++ b/libnymea-remoteproxy/authentication/aws/awsauthenticator.h @@ -50,10 +50,6 @@ public: private: QNetworkAccessManager *m_manager = nullptr; AwsCredentialProvider *m_credentialsProvider = nullptr; - QHash m_runningProcesses; - -private slots: - void onAuthenticationProcessFinished(Authenticator::AuthenticationError error, const UserInformation &userInformation); public slots: AuthenticationReply *authenticate(ProxyClient *proxyClient) override; diff --git a/libnymea-remoteproxy/jsonrpc/authenticationhandler.cpp b/libnymea-remoteproxy/jsonrpc/authenticationhandler.cpp index b3a049d..fba509f 100644 --- a/libnymea-remoteproxy/jsonrpc/authenticationhandler.cpp +++ b/libnymea-remoteproxy/jsonrpc/authenticationhandler.cpp @@ -79,36 +79,28 @@ JsonReply *AuthenticationHandler::Authenticate(const QVariantMap ¶ms, Transp proxyClient->setNonce(nonce); AuthenticationReply *authReply = Engine::instance()->authenticator()->authenticate(proxyClient); - connect(authReply, &AuthenticationReply::finished, this, &AuthenticationHandler::onAuthenticationFinished); + connect(authReply, &AuthenticationReply::finished, jsonReply, [=](){ + authReply->deleteLater(); - m_runningAuthentications.insert(authReply, jsonReply); + qCDebug(dcJsonRpc()) << "Authentication reply finished"; + if (authReply->error() != Authenticator::AuthenticationErrorNoError) { + qCWarning(dcJsonRpc()) << "Authentication error occurred" << authReply->error(); + jsonReply->setSuccess(false); + } else { + // Successfully authenticated + jsonReply->setSuccess(true); + } + + // Set client authenticated if still there + if (!authReply->proxyClient().isNull()) { + authReply->proxyClient()->setAuthenticated(authReply->error() == Authenticator::AuthenticationErrorNoError); + jsonReply->setData(errorToReply(authReply->error())); + } + + emit jsonReply->finished(); + }); return jsonReply; } -void AuthenticationHandler::onAuthenticationFinished() -{ - AuthenticationReply *authenticationReply = static_cast(sender()); - authenticationReply->deleteLater(); - - qCDebug(dcJsonRpc()) << "Authentication reply finished"; - JsonReply *jsonReply = m_runningAuthentications.take(authenticationReply); - - if (authenticationReply->error() != Authenticator::AuthenticationErrorNoError) { - qCWarning(dcJsonRpc()) << "Authentication error occurred" << authenticationReply->error(); - jsonReply->setSuccess(false); - } else { - // Successfully authenticated - jsonReply->setSuccess(true); - } - - // Set client authenticated if still there - if (!authenticationReply->proxyClient().isNull()) { - authenticationReply->proxyClient()->setAuthenticated(authenticationReply->error() == Authenticator::AuthenticationErrorNoError); - jsonReply->setData(errorToReply(authenticationReply->error())); - } - - emit jsonReply->finished(); -} - } diff --git a/libnymea-remoteproxy/jsonrpc/authenticationhandler.h b/libnymea-remoteproxy/jsonrpc/authenticationhandler.h index 13618c7..6513887 100644 --- a/libnymea-remoteproxy/jsonrpc/authenticationhandler.h +++ b/libnymea-remoteproxy/jsonrpc/authenticationhandler.h @@ -48,13 +48,6 @@ public: Q_INVOKABLE JsonReply *Authenticate(const QVariantMap ¶ms, TransportClient *transportClient); -private: - QHash m_runningAuthentications; - -private slots: - void onAuthenticationFinished(); - - }; } From 7e532865386d9adb92ba6d48dc7df80726977b86 Mon Sep 17 00:00:00 2001 From: =?UTF-8?q?Simon=20St=C3=BCrz?= Date: Thu, 28 Apr 2022 10:26:26 +0200 Subject: [PATCH 2/7] Fix potential crash if the remote client is not available any more --- libnymea-remoteproxy/proxy/proxyserver.cpp | 4 ++++ 1 file changed, 4 insertions(+) diff --git a/libnymea-remoteproxy/proxy/proxyserver.cpp b/libnymea-remoteproxy/proxy/proxyserver.cpp index 42274b5..28681d2 100644 --- a/libnymea-remoteproxy/proxy/proxyserver.cpp +++ b/libnymea-remoteproxy/proxy/proxyserver.cpp @@ -300,6 +300,10 @@ void ProxyServer::onClientDataAvailable(const QUuid &clientId, const QByteArray // Calculate server statisitcs m_troughputCounter += data.count(); proxyClient->addRxDataCount(data.count()); + + if (!remoteClient) + return; + remoteClient->addTxDataCount(data.count()); m_totalTraffic += data.count(); From 84f45d75ef208d42a2dd1ba1da573c6a6b750511 Mon Sep 17 00:00:00 2001 From: =?UTF-8?q?Simon=20St=C3=BCrz?= Date: Thu, 28 Apr 2022 10:45:04 +0200 Subject: [PATCH 3/7] Stabalize tests and improve inactive timout test --- tests/resources/test-configuration.conf | 8 ++++---- tests/test-proxy/remoteproxytestsproxy.cpp | 2 ++ 2 files changed, 6 insertions(+), 4 deletions(-) diff --git a/tests/resources/test-configuration.conf b/tests/resources/test-configuration.conf index 80c5cc1..012ae62 100644 --- a/tests/resources/test-configuration.conf +++ b/tests/resources/test-configuration.conf @@ -3,10 +3,10 @@ name=test-nymea-remoteproxy writeLogs=false logFile=/var/log/nymea-remoteproxy.log monitorSocket=/tmp/nymea-remoteproxy-test.sock -jsonRpcTimeout=2000 -authenticationTimeout=1000 -inactiveTimeout=1500 -aloneTimeout=1500 +jsonRpcTimeout=10000 +authenticationTimeout=10000 +inactiveTimeout=5000 +aloneTimeout=10000 [SSL] certificate=:/test-certificate.crt diff --git a/tests/test-proxy/remoteproxytestsproxy.cpp b/tests/test-proxy/remoteproxytestsproxy.cpp index bbd274f..0582d7a 100644 --- a/tests/test-proxy/remoteproxytestsproxy.cpp +++ b/tests/test-proxy/remoteproxytestsproxy.cpp @@ -1147,6 +1147,8 @@ void RemoteProxyTestsProxy::inactiveTimeout() // Start the server startServer(); + m_configuration->setInactiveTimeout(4000); + RemoteProxyConnection *connection = new RemoteProxyConnection(QUuid::createUuid(), "Sleepy test client", this); connect(connection, &RemoteProxyConnection::sslErrors, this, &BaseTest::ignoreConnectionSslError); From 56948912dd01b67c9f4dde0f5693022b23eb7c64 Mon Sep 17 00:00:00 2001 From: =?UTF-8?q?Simon=20St=C3=BCrz?= Date: Tue, 3 May 2022 15:02:46 +0200 Subject: [PATCH 4/7] Add more timespace between timeout and background timeouts to stabalize tests --- tests/test-proxy/remoteproxytestsproxy.cpp | 12 ++++-------- 1 file changed, 4 insertions(+), 8 deletions(-) diff --git a/tests/test-proxy/remoteproxytestsproxy.cpp b/tests/test-proxy/remoteproxytestsproxy.cpp index 0582d7a..8c6a324 100644 --- a/tests/test-proxy/remoteproxytestsproxy.cpp +++ b/tests/test-proxy/remoteproxytestsproxy.cpp @@ -1116,16 +1116,12 @@ void RemoteProxyTestsProxy::jsonRpcTimeout() // Start the server startServer(); - m_configuration->setAuthenticationTimeout(3000); - m_configuration->setJsonRpcTimeout(1000); - m_configuration->setInactiveTimeout(2000); - // Configure result (authentication takes longer than json rpc timeout m_mockAuthenticator->setExpectedAuthenticationError(); - m_mockAuthenticator->setTimeoutDuration(4000); - m_configuration->setAuthenticationTimeout(4000); + m_mockAuthenticator->setTimeoutDuration(10000); + m_configuration->setAuthenticationTimeout(10000); m_configuration->setJsonRpcTimeout(1000); - m_configuration->setInactiveTimeout(2000); + m_configuration->setInactiveTimeout(10000); // Create request QVariantMap params; @@ -1147,7 +1143,7 @@ void RemoteProxyTestsProxy::inactiveTimeout() // Start the server startServer(); - m_configuration->setInactiveTimeout(4000); + m_configuration->setInactiveTimeout(1000); RemoteProxyConnection *connection = new RemoteProxyConnection(QUuid::createUuid(), "Sleepy test client", this); connect(connection, &RemoteProxyConnection::sslErrors, this, &BaseTest::ignoreConnectionSslError); From 434ed30f5ea9a2bf02e55bb265d7dbec5da71c36 Mon Sep 17 00:00:00 2001 From: =?UTF-8?q?Simon=20St=C3=BCrz?= Date: Thu, 5 May 2022 14:03:38 +0200 Subject: [PATCH 5/7] Fix remote proxy test timing --- tests/test-proxy/remoteproxytestsproxy.cpp | 18 +++++++++++------- tests/testbase/basetest.cpp | 3 +-- 2 files changed, 12 insertions(+), 9 deletions(-) diff --git a/tests/test-proxy/remoteproxytestsproxy.cpp b/tests/test-proxy/remoteproxytestsproxy.cpp index 8c6a324..4011a54 100644 --- a/tests/test-proxy/remoteproxytestsproxy.cpp +++ b/tests/test-proxy/remoteproxytestsproxy.cpp @@ -508,26 +508,26 @@ void RemoteProxyTestsProxy::authenticate_data() QTest::addColumn("expectedError"); QTest::newRow("success") << QUuid::createUuid().toString() << "Testclient, hello form the test!" << m_testToken << "" - << 100 << Authenticator::AuthenticationErrorNoError; + << 500 << Authenticator::AuthenticationErrorNoError; QTest::newRow("success") << QUuid::createUuid().toString() << "Testclient, hello form the test!" << m_testToken << "nonce" - << 100 << Authenticator::AuthenticationErrorNoError; + << 500 << Authenticator::AuthenticationErrorNoError; QTest::newRow("success") << QUuid::createUuid().toString() << "Testclient, hello form the test!" << m_testToken << "nonce" - << 100 << Authenticator::AuthenticationErrorAuthenticationFailed; + << 300 << Authenticator::AuthenticationErrorAuthenticationFailed; QTest::newRow("failed") << QUuid::createUuid().toString() << "Testclient, hello form the test!" << m_testToken << "" - << 100 << Authenticator::AuthenticationErrorAuthenticationFailed; + << 300 << Authenticator::AuthenticationErrorAuthenticationFailed; QTest::newRow("not responding") << QUuid::createUuid().toString() << "Testclient, hello form the test!" << m_testToken << "" - << 200 << Authenticator::AuthenticationErrorProxyError; + << 500 << Authenticator::AuthenticationErrorProxyError; QTest::newRow("aborted") << QUuid::createUuid().toString() << "Testclient, hello form the test!" << m_testToken << "" - << 100 << Authenticator::AuthenticationErrorAborted; + << 300 << Authenticator::AuthenticationErrorAborted; QTest::newRow("unknown") << QUuid::createUuid().toString() << "Testclient, hello form the test!" << m_testToken << "" - << 100 << Authenticator::AuthenticationErrorUnknown; + << 300 << Authenticator::AuthenticationErrorUnknown; } @@ -543,6 +543,10 @@ void RemoteProxyTestsProxy::authenticate() // Start the server startServer(); + m_configuration->setAuthenticationTimeout(8000); + m_configuration->setJsonRpcTimeout(10000); + m_configuration->setInactiveTimeout(10000); + // Configure result m_mockAuthenticator->setExpectedAuthenticationError(expectedError); m_mockAuthenticator->setTimeoutDuration(timeout); diff --git a/tests/testbase/basetest.cpp b/tests/testbase/basetest.cpp index eea50b8..b3d06ac 100644 --- a/tests/testbase/basetest.cpp +++ b/tests/testbase/basetest.cpp @@ -266,8 +266,7 @@ QVariant BaseTest::invokeTcpSocketProxyApiCall(const QString &method, const QVar QSignalSpy dataSpy(socket, &QSslSocket::readyRead); socket->write(jsonDoc.toJson(QJsonDocument::Compact) + '\n'); - // FIXME: check why it waits the full time here - dataSpy.wait(500); + dataSpy.wait(); if (dataSpy.count() != 1) { qWarning() << "No data received"; return QVariant(); From a534582b8c5def1f45f91594979a79d991b6725c Mon Sep 17 00:00:00 2001 From: =?UTF-8?q?Simon=20St=C3=BCrz?= Date: Fri, 6 May 2022 15:10:06 +0200 Subject: [PATCH 6/7] Remove any custom signal spy timeout due to random stress based test failures --- tests/test-proxy/remoteproxytestsproxy.cpp | 55 +++++++++++----------- tests/testbase/basetest.cpp | 20 +++----- 2 files changed, 34 insertions(+), 41 deletions(-) diff --git a/tests/test-proxy/remoteproxytestsproxy.cpp b/tests/test-proxy/remoteproxytestsproxy.cpp index 4011a54..8f9d043 100644 --- a/tests/test-proxy/remoteproxytestsproxy.cpp +++ b/tests/test-proxy/remoteproxytestsproxy.cpp @@ -161,8 +161,8 @@ void RemoteProxyTestsProxy::monitorServer() QVERIFY(connectionTwo->isAuthenticated()); // Wait for both to be connected - remoteConnectionEstablishedOne.wait(500); - remoteConnectionEstablishedTwo.wait(500); + remoteConnectionEstablishedOne.wait(1000); + remoteConnectionEstablishedTwo.wait(1000); QVERIFY(remoteConnectionEstablishedOne.count() == 1); QVERIFY(remoteConnectionEstablishedTwo.count() == 1); @@ -179,7 +179,7 @@ void RemoteProxyTestsProxy::monitorServer() QLocalSocket *monitor = new QLocalSocket(this); QSignalSpy connectedSpy(monitor, &QLocalSocket::connected); monitor->connectToServer(m_configuration->monitorSocketFileName()); - connectedSpy.wait(200); + connectedSpy.wait(1000); QVERIFY(connectedSpy.count() == 1); QSignalSpy dataSpy(monitor, &QLocalSocket::readyRead); @@ -192,7 +192,7 @@ void RemoteProxyTestsProxy::monitorServer() QSignalSpy disconnectedSpy(monitor, &QLocalSocket::connected); monitor->disconnectFromServer(); - disconnectedSpy.wait(200); + disconnectedSpy.wait(1000); // Clean up monitor->deleteLater(); @@ -315,7 +315,7 @@ void RemoteProxyTestsProxy::websocketBinaryData() // Send binary data and make sure the server disconnects this socket QSignalSpy spyDisconnected(client, SIGNAL(disconnected())); client->sendBinaryMessage("trying to upload stuff...stuff...more stuff... other stuff"); - spyConnection.wait(200); + spyConnection.wait(1000); QVERIFY(spyConnection.count() == 1); // Clean up @@ -634,8 +634,8 @@ void RemoteProxyTestsProxy::authenticateNonce() QVERIFY(connectionTwo->isAuthenticated()); // Wait for both to be connected - remoteConnectionEstablishedOne.wait(500); - remoteConnectionEstablishedTwo.wait(500); + remoteConnectionEstablishedOne.wait(); + remoteConnectionEstablishedTwo.wait(); QVERIFY(remoteConnectionEstablishedOne.count() == 1); QVERIFY(remoteConnectionEstablishedTwo.count() == 1); @@ -652,12 +652,12 @@ void RemoteProxyTestsProxy::authenticateNonce() QSignalSpy remoteConnectionDataTwo(connectionTwo, &RemoteProxyConnection::dataReady); connectionOne->sendData(dataOne); - remoteConnectionDataTwo.wait(500); + remoteConnectionDataTwo.wait(); QVERIFY(remoteConnectionDataTwo.count() == 1); QCOMPARE(remoteConnectionDataTwo.at(0).at(0).toByteArray().trimmed(), dataOne); connectionTwo->sendData(dataTwo); - remoteConnectionDataOne.wait(500); + remoteConnectionDataOne.wait(); QVERIFY(remoteConnectionDataOne.count() == 1); QCOMPARE(remoteConnectionDataOne.at(0).at(0).toByteArray().trimmed(), dataTwo); @@ -754,8 +754,7 @@ void RemoteProxyTestsProxy::clientConnectionWebSocket() // Disconnect and clean up QSignalSpy spyDisconnected(connection, &RemoteProxyConnection::disconnected); connection->disconnectServer(); - // FIXME: check why it waits the full time here - spyDisconnected.wait(500); + spyDisconnected.wait(); QVERIFY(spyDisconnected.count() >= 1); QVERIFY(!connection->isConnected()); @@ -805,7 +804,7 @@ void RemoteProxyTestsProxy::clientConnectionTcpSocket() QSignalSpy spyDisconnected(connection, &RemoteProxyConnection::disconnected); connection->disconnectServer(); // FIXME: check why it waits the full time here - spyDisconnected.wait(500); + spyDisconnected.wait(); QVERIFY(spyDisconnected.count() >= 1); QVERIFY(!connection->isConnected()); @@ -873,8 +872,8 @@ void RemoteProxyTestsProxy::remoteConnection() QVERIFY(connectionTwo->isAuthenticated()); // Wait for both to be connected - remoteConnectionEstablishedOne.wait(500); - remoteConnectionEstablishedTwo.wait(500); + remoteConnectionEstablishedOne.wait(); + remoteConnectionEstablishedTwo.wait(); QVERIFY(remoteConnectionEstablishedOne.count() == 1); QVERIFY(remoteConnectionEstablishedTwo.count() == 1); @@ -891,12 +890,12 @@ void RemoteProxyTestsProxy::remoteConnection() QSignalSpy remoteConnectionDataTwo(connectionTwo, &RemoteProxyConnection::dataReady); connectionOne->sendData(dataOne); - remoteConnectionDataTwo.wait(500); + remoteConnectionDataTwo.wait(); QVERIFY(remoteConnectionDataTwo.count() == 1); QCOMPARE(remoteConnectionDataTwo.at(0).at(0).toByteArray().trimmed(), dataOne); connectionTwo->sendData(dataTwo); - remoteConnectionDataOne.wait(500); + remoteConnectionDataOne.wait(); QVERIFY(remoteConnectionDataOne.count() == 1); QCOMPARE(remoteConnectionDataOne.at(0).at(0).toByteArray().trimmed(), dataTwo); @@ -993,7 +992,7 @@ void RemoteProxyTestsProxy::trippleConnection() QVERIFY(connectionTwo->isAuthenticated()); // Wait for both to be connected - remoteConnectionEstablishedOne.wait(500); + remoteConnectionEstablishedOne.wait(); // Now connect a third connection and make sure the client will be closed @@ -1011,7 +1010,7 @@ void RemoteProxyTestsProxy::trippleConnection() connectionThreeAuthenticatedSpy.wait(); QVERIFY(connectionOneAuthenticatedSpy.count() == 1); - connectionThreeDisconnectedSpy.wait(200); + connectionThreeDisconnectedSpy.wait(); QVERIFY(connectionThreeDisconnectedSpy.count() >= 1); // Make sure the one and two are still connected @@ -1071,10 +1070,10 @@ void RemoteProxyTestsProxy::duplicateUuid() QSignalSpy disconnectSpyTwo(connectionTwo, &RemoteProxyConnection::disconnected); QVERIFY(connectionTwo->authenticate(m_testToken)); - disconnectSpyOne.wait(200); + disconnectSpyOne.wait(); QVERIFY(disconnectSpyOne.count() >= 1); - disconnectSpyTwo.wait(200); + disconnectSpyTwo.wait(); QVERIFY(disconnectSpyTwo.count() >= 1); connectionOne->deleteLater(); @@ -1287,8 +1286,8 @@ void RemoteProxyTestsProxy::tcpRemoteConnection() QVERIFY(connectionTwo->isAuthenticated()); // Wait for both to be connected - remoteConnectionEstablishedOne.wait(500); - remoteConnectionEstablishedTwo.wait(500); + remoteConnectionEstablishedOne.wait(); + remoteConnectionEstablishedTwo.wait(); QVERIFY(remoteConnectionEstablishedOne.count() == 1); QVERIFY(remoteConnectionEstablishedTwo.count() == 1); @@ -1305,12 +1304,12 @@ void RemoteProxyTestsProxy::tcpRemoteConnection() QSignalSpy remoteConnectionDataTwo(connectionTwo, &RemoteProxyConnection::dataReady); connectionOne->sendData(dataOne); - remoteConnectionDataTwo.wait(500); + remoteConnectionDataTwo.wait(); QVERIFY(remoteConnectionDataTwo.count() == 1); QCOMPARE(remoteConnectionDataTwo.at(0).at(0).toByteArray().trimmed(), dataOne); connectionTwo->sendData(dataTwo); - remoteConnectionDataOne.wait(500); + remoteConnectionDataOne.wait(); QVERIFY(remoteConnectionDataOne.count() == 1); QCOMPARE(remoteConnectionDataOne.at(0).at(0).toByteArray().trimmed(), dataTwo); @@ -1381,8 +1380,8 @@ void RemoteProxyTestsProxy::tcpWebsocketRemoteConnection() QVERIFY(connectionTwo->isAuthenticated()); // Wait for both to be connected - remoteConnectionEstablishedOne.wait(500); - remoteConnectionEstablishedTwo.wait(500); + remoteConnectionEstablishedOne.wait(); + remoteConnectionEstablishedTwo.wait(); QVERIFY(remoteConnectionEstablishedOne.count() == 1); QVERIFY(remoteConnectionEstablishedTwo.count() == 1); @@ -1399,12 +1398,12 @@ void RemoteProxyTestsProxy::tcpWebsocketRemoteConnection() QSignalSpy remoteConnectionDataTwo(connectionTwo, &RemoteProxyConnection::dataReady); connectionOne->sendData(dataOne); - remoteConnectionDataTwo.wait(500); + remoteConnectionDataTwo.wait(); QVERIFY(remoteConnectionDataTwo.count() == 1); QCOMPARE(remoteConnectionDataTwo.at(0).at(0).toByteArray().trimmed(), dataOne); connectionTwo->sendData(dataTwo); - remoteConnectionDataOne.wait(500); + remoteConnectionDataOne.wait(); QVERIFY(remoteConnectionDataOne.count() == 1); QCOMPARE(remoteConnectionDataOne.at(0).at(0).toByteArray().trimmed(), dataTwo); diff --git a/tests/testbase/basetest.cpp b/tests/testbase/basetest.cpp index b3d06ac..25a40ca 100644 --- a/tests/testbase/basetest.cpp +++ b/tests/testbase/basetest.cpp @@ -125,7 +125,7 @@ void BaseTest::startServer() QSignalSpy runningSpy(Engine::instance(), &Engine::runningChanged); Engine::instance()->setDeveloperModeEnabled(true); Engine::instance()->start(m_configuration); - runningSpy.wait(200); + runningSpy.wait(); QVERIFY(runningSpy.count() == 1); } @@ -367,7 +367,7 @@ bool BaseTest::createRemoteConnection(const QString &token, const QString &nonce return false; } - connectionOneAuthenticatedSpy.wait(500); + connectionOneAuthenticatedSpy.wait(); if (connectionOneAuthenticatedSpy.count() != 1) { qWarning() << "Could not authenticate client one"; return false; @@ -386,7 +386,7 @@ bool BaseTest::createRemoteConnection(const QString &token, const QString &nonce return false; } - connectionTwoAuthenticatedSpy.wait(500); + connectionTwoAuthenticatedSpy.wait(); if (connectionTwoAuthenticatedSpy.count() != 1) { qWarning() << "Could not authenticate client two"; return false; @@ -398,8 +398,8 @@ bool BaseTest::createRemoteConnection(const QString &token, const QString &nonce } // Wait for both to be connected - remoteConnectionEstablishedOne.wait(500); - remoteConnectionEstablishedTwo.wait(500); + remoteConnectionEstablishedOne.wait(); + remoteConnectionEstablishedTwo.wait(); if (remoteConnectionEstablishedOne.count() != 1 || remoteConnectionEstablishedTwo.count() != 1) { qWarning() << "Could not establish remote connection"; @@ -432,8 +432,6 @@ QVariant BaseTest::injectTcpSocketProxyData(const QByteArray &data) QSignalSpy dataSpy(socket, &QSslSocket::readyRead); socket->write(data + '\n'); dataSpy.wait(); - // FIXME: check why it waits the full time here - dataSpy.wait(500); if (dataSpy.count() != 1) { qWarning() << "No data received"; return QVariant(); @@ -577,8 +575,7 @@ QVariant BaseTest::invokeTcpSocketTunnelProxyApiCall(const QString &method, cons QSignalSpy dataSpy(socket, &QSslSocket::readyRead); socket->write(jsonDoc.toJson(QJsonDocument::Compact) + '\n'); - // FIXME: check why it waits the full time here - dataSpy.wait(500); + dataSpy.wait(); if (dataSpy.count() != 1) { qWarning() << "No data received"; return QVariant(); @@ -630,8 +627,6 @@ QVariant BaseTest::injectTcpSocketTunnelProxyData(const QByteArray &data) QSignalSpy dataSpy(socket, &QSslSocket::readyRead); socket->write(data + '\n'); dataSpy.wait(); - // FIXME: check why it waits the full time here - dataSpy.wait(500); if (dataSpy.count() != 1) { qWarning() << "No data received"; return QVariant(); @@ -696,8 +691,7 @@ QPair BaseTest::invokeTcpSocketTunnelProxyApiCallPersist socket->write(payload); } - // FIXME: check why it waits the full time here - dataSpy.wait(500); + dataSpy.wait(); if (dataSpy.count() < 1) { qWarning() << "No data received"; return QPair(QVariant(), socket); From bd9823a0bdf7fbb2b1352e93c6fd84e1151712f8 Mon Sep 17 00:00:00 2001 From: =?UTF-8?q?Simon=20St=C3=BCrz?= Date: Fri, 6 May 2022 15:38:17 +0200 Subject: [PATCH 7/7] Speed up and stabalize tests --- tests/test-proxy/remoteproxytestsproxy.cpp | 89 +++++++++++++--------- tests/testbase/basetest.cpp | 7 +- 2 files changed, 58 insertions(+), 38 deletions(-) diff --git a/tests/test-proxy/remoteproxytestsproxy.cpp b/tests/test-proxy/remoteproxytestsproxy.cpp index 8f9d043..d510499 100644 --- a/tests/test-proxy/remoteproxytestsproxy.cpp +++ b/tests/test-proxy/remoteproxytestsproxy.cpp @@ -130,14 +130,14 @@ void RemoteProxyTestsProxy::monitorServer() // Connect one QSignalSpy connectionOneReadySpy(connectionOne, &RemoteProxyConnection::ready); QVERIFY(connectionOne->connectServer(m_serverUrlProxyTcp)); - connectionOneReadySpy.wait(); + if (connectionOneReadySpy.count() < 1) connectionOneReadySpy.wait(); QVERIFY(connectionOneReadySpy.count() == 1); QVERIFY(connectionOne->isConnected()); // Connect two QSignalSpy connectionTwoReadySpy(connectionTwo, &RemoteProxyConnection::ready); QVERIFY(connectionTwo->connectServer(m_serverUrlProxyTcp)); - connectionTwoReadySpy.wait(); + if (connectionTwoReadySpy.count() < 1) connectionTwoReadySpy.wait(); QVERIFY(connectionTwoReadySpy.count() == 1); QVERIFY(connectionTwo->isConnected()); @@ -145,7 +145,7 @@ void RemoteProxyTestsProxy::monitorServer() QSignalSpy remoteConnectionEstablishedOne(connectionOne, &RemoteProxyConnection::remoteConnectionEstablished); QSignalSpy connectionOneAuthenticatedSpy(connectionOne, &RemoteProxyConnection::authenticated); QVERIFY(connectionOne->authenticate(m_testToken)); - connectionOneAuthenticatedSpy.wait(); + if (connectionOneAuthenticatedSpy.count() < 1) connectionOneAuthenticatedSpy.wait(); QVERIFY(connectionOneAuthenticatedSpy.count() == 1); QVERIFY(connectionOne->isConnected()); QVERIFY(connectionOne->isAuthenticated()); @@ -155,14 +155,17 @@ void RemoteProxyTestsProxy::monitorServer() QSignalSpy remoteConnectionEstablishedTwo(connectionTwo, &RemoteProxyConnection::remoteConnectionEstablished); QSignalSpy connectionTwoAuthenticatedSpy(connectionTwo, &RemoteProxyConnection::authenticated); QVERIFY(connectionTwo->authenticate(m_testToken)); - connectionTwoAuthenticatedSpy.wait(); + if (connectionTwoAuthenticatedSpy.count() < 1) connectionTwoAuthenticatedSpy.wait(); QVERIFY(connectionTwoAuthenticatedSpy.count() == 1); QVERIFY(connectionTwo->isConnected()); QVERIFY(connectionTwo->isAuthenticated()); // Wait for both to be connected - remoteConnectionEstablishedOne.wait(1000); - remoteConnectionEstablishedTwo.wait(1000); + if (remoteConnectionEstablishedOne.count() < 1) + remoteConnectionEstablishedOne.wait(); + + if (remoteConnectionEstablishedTwo.count() < 1) + remoteConnectionEstablishedTwo.wait(); QVERIFY(remoteConnectionEstablishedOne.count() == 1); QVERIFY(remoteConnectionEstablishedTwo.count() == 1); @@ -174,16 +177,15 @@ void RemoteProxyTestsProxy::monitorServer() QCOMPARE(connectionTwo->tunnelPartnerName(), nameConnectionOne); QCOMPARE(QUuid(connectionTwo->tunnelPartnerUuid()), uuidConnectionOne); - // Get monitor data QLocalSocket *monitor = new QLocalSocket(this); QSignalSpy connectedSpy(monitor, &QLocalSocket::connected); monitor->connectToServer(m_configuration->monitorSocketFileName()); - connectedSpy.wait(1000); + if (connectedSpy.count() < 1) connectedSpy.wait(); QVERIFY(connectedSpy.count() == 1); QSignalSpy dataSpy(monitor, &QLocalSocket::readyRead); - dataSpy.wait(); + if (dataSpy.count() < 1) dataSpy.wait(); QVERIFY(dataSpy.count() == 1); QByteArray data = monitor->readAll(); qDebug() << data; @@ -192,7 +194,7 @@ void RemoteProxyTestsProxy::monitorServer() QSignalSpy disconnectedSpy(monitor, &QLocalSocket::connected); monitor->disconnectFromServer(); - disconnectedSpy.wait(1000); + if (disconnectedSpy.count() < 1) disconnectedSpy.wait(); // Clean up monitor->deleteLater(); @@ -634,8 +636,11 @@ void RemoteProxyTestsProxy::authenticateNonce() QVERIFY(connectionTwo->isAuthenticated()); // Wait for both to be connected - remoteConnectionEstablishedOne.wait(); - remoteConnectionEstablishedTwo.wait(); + if (remoteConnectionEstablishedOne.count() < 1) + remoteConnectionEstablishedOne.wait(); + + if (remoteConnectionEstablishedTwo.count() < 1) + remoteConnectionEstablishedTwo.wait(); QVERIFY(remoteConnectionEstablishedOne.count() == 1); QVERIFY(remoteConnectionEstablishedTwo.count() == 1); @@ -754,7 +759,7 @@ void RemoteProxyTestsProxy::clientConnectionWebSocket() // Disconnect and clean up QSignalSpy spyDisconnected(connection, &RemoteProxyConnection::disconnected); connection->disconnectServer(); - spyDisconnected.wait(); + if (spyDisconnected.count() < 1) spyDisconnected.wait(); QVERIFY(spyDisconnected.count() >= 1); QVERIFY(!connection->isConnected()); @@ -803,8 +808,8 @@ void RemoteProxyTestsProxy::clientConnectionTcpSocket() // Disconnect and clean up QSignalSpy spyDisconnected(connection, &RemoteProxyConnection::disconnected); connection->disconnectServer(); - // FIXME: check why it waits the full time here - spyDisconnected.wait(); + + if (spyDisconnected.count() < 1) spyDisconnected.wait(); QVERIFY(spyDisconnected.count() >= 1); QVERIFY(!connection->isConnected()); @@ -872,8 +877,11 @@ void RemoteProxyTestsProxy::remoteConnection() QVERIFY(connectionTwo->isAuthenticated()); // Wait for both to be connected - remoteConnectionEstablishedOne.wait(); - remoteConnectionEstablishedTwo.wait(); + if (remoteConnectionEstablishedOne.count() < 1) + remoteConnectionEstablishedOne.wait(); + + if (remoteConnectionEstablishedTwo.count() < 1) + remoteConnectionEstablishedTwo.wait(); QVERIFY(remoteConnectionEstablishedOne.count() == 1); QVERIFY(remoteConnectionEstablishedTwo.count() == 1); @@ -960,21 +968,21 @@ void RemoteProxyTestsProxy::trippleConnection() // Connect one QSignalSpy connectionOneReadySpy(connectionOne, &RemoteProxyConnection::ready); QVERIFY(connectionOne->connectServer(m_serverUrlProxyTcp)); - connectionOneReadySpy.wait(); + if (connectionOneReadySpy.count() < 1) connectionOneReadySpy.wait(); QVERIFY(connectionOneReadySpy.count() == 1); QVERIFY(connectionOne->isConnected()); // Connect two QSignalSpy connectionTwoReadySpy(connectionTwo, &RemoteProxyConnection::ready); QVERIFY(connectionTwo->connectServer(m_serverUrlProxyTcp)); - connectionTwoReadySpy.wait(); + if (connectionTwoReadySpy.count() < 1) connectionTwoReadySpy.wait(); QVERIFY(connectionTwoReadySpy.count() == 1); QVERIFY(connectionTwo->isConnected()); // Authenticate one QSignalSpy connectionOneAuthenticatedSpy(connectionOne, &RemoteProxyConnection::authenticated); QVERIFY(connectionOne->authenticate(m_testToken, nonce)); - connectionOneAuthenticatedSpy.wait(); + if (connectionOneAuthenticatedSpy.count() < 1) connectionOneAuthenticatedSpy.wait(); QVERIFY(connectionOneAuthenticatedSpy.count() == 1); QVERIFY(connectionOne->isConnected()); QVERIFY(connectionOne->isAuthenticated()); @@ -986,13 +994,14 @@ void RemoteProxyTestsProxy::trippleConnection() QSignalSpy remoteConnectionEstablishedTwo(connectionTwo, &RemoteProxyConnection::remoteConnectionEstablished); QSignalSpy connectionTwoAuthenticatedSpy(connectionTwo, &RemoteProxyConnection::authenticated); QVERIFY(connectionTwo->authenticate(m_testToken, nonce)); - connectionTwoAuthenticatedSpy.wait(); + if (connectionTwoAuthenticatedSpy.count() < 1) connectionTwoAuthenticatedSpy.wait(); QVERIFY(connectionTwoAuthenticatedSpy.count() == 1); QVERIFY(connectionTwo->isConnected()); QVERIFY(connectionTwo->isAuthenticated()); // Wait for both to be connected - remoteConnectionEstablishedOne.wait(); + if (remoteConnectionEstablishedOne.count() < 1) + remoteConnectionEstablishedOne.wait(); // Now connect a third connection and make sure the client will be closed @@ -1000,17 +1009,17 @@ void RemoteProxyTestsProxy::trippleConnection() QSignalSpy connectionThreeReadySpy(connectionThree, &RemoteProxyConnection::ready); QSignalSpy connectionThreeDisconnectedSpy(connectionThree, &RemoteProxyConnection::disconnected); QVERIFY(connectionThree->connectServer(m_serverUrlProxyTcp)); - connectionThreeReadySpy.wait(); + if (connectionThreeReadySpy.count() < 1) connectionThreeReadySpy.wait(); QVERIFY(connectionThreeReadySpy.count() == 1); QVERIFY(connectionThree->isConnected()); // Authenticate three QSignalSpy connectionThreeAuthenticatedSpy(connectionThree, &RemoteProxyConnection::authenticated); QVERIFY(connectionThree->authenticate(m_testToken, nonce)); - connectionThreeAuthenticatedSpy.wait(); + if (connectionThreeAuthenticatedSpy.count() < 1) connectionThreeAuthenticatedSpy.wait(); QVERIFY(connectionOneAuthenticatedSpy.count() == 1); - connectionThreeDisconnectedSpy.wait(); + if (connectionThreeDisconnectedSpy.count() < 1) connectionThreeDisconnectedSpy.wait(); QVERIFY(connectionThreeDisconnectedSpy.count() >= 1); // Make sure the one and two are still connected @@ -1045,21 +1054,21 @@ void RemoteProxyTestsProxy::duplicateUuid() // Connect one QSignalSpy connectionOneReadySpy(connectionOne, &RemoteProxyConnection::ready); QVERIFY(connectionOne->connectServer(m_serverUrlProxyTcp)); - connectionOneReadySpy.wait(); + if (connectionOneReadySpy.count() < 1) connectionOneReadySpy.wait(); QVERIFY(connectionOneReadySpy.count() == 1); QVERIFY(connectionOne->isConnected()); // Connect two QSignalSpy connectionTwoReadySpy(connectionTwo, &RemoteProxyConnection::ready); QVERIFY(connectionTwo->connectServer(m_serverUrlProxyTcp)); - connectionTwoReadySpy.wait(); + if (connectionTwoReadySpy.count() < 1) connectionTwoReadySpy.wait(); QVERIFY(connectionTwoReadySpy.count() == 1); QVERIFY(connectionTwo->isConnected()); // Authenticate one QSignalSpy connectionOneAuthenticatedSpy(connectionOne, &RemoteProxyConnection::authenticated); QVERIFY(connectionOne->authenticate(m_testToken)); - connectionOneAuthenticatedSpy.wait(); + if (connectionOneAuthenticatedSpy.count() < 1) connectionOneAuthenticatedSpy.wait(); QVERIFY(connectionOneAuthenticatedSpy.count() == 1); QVERIFY(connectionOne->isConnected()); QVERIFY(connectionOne->isAuthenticated()); @@ -1070,10 +1079,10 @@ void RemoteProxyTestsProxy::duplicateUuid() QSignalSpy disconnectSpyTwo(connectionTwo, &RemoteProxyConnection::disconnected); QVERIFY(connectionTwo->authenticate(m_testToken)); - disconnectSpyOne.wait(); + if (disconnectSpyOne.count() < 1 ) disconnectSpyOne.wait(); QVERIFY(disconnectSpyOne.count() >= 1); - disconnectSpyTwo.wait(); + if (disconnectSpyTwo.count() < 1 ) disconnectSpyTwo.wait(); QVERIFY(disconnectSpyTwo.count() >= 1); connectionOne->deleteLater(); @@ -1155,11 +1164,13 @@ void RemoteProxyTestsProxy::inactiveTimeout() QSignalSpy connectionReadySpy(connection, &RemoteProxyConnection::ready); QSignalSpy connectionDisconnectedSpy(connection, &RemoteProxyConnection::disconnected); QVERIFY(connection->connectServer(m_serverUrlProxyTcp)); - connectionReadySpy.wait(); + if (connectionReadySpy.count() < 1) connectionReadySpy.wait(); QVERIFY(connectionReadySpy.count() == 1); // Now wait for disconnected - connectionDisconnectedSpy.wait(); + if (connectionDisconnectedSpy.count() < 1) + connectionDisconnectedSpy.wait(); + QVERIFY(connectionDisconnectedSpy.count() >= 1); // Clean up @@ -1286,8 +1297,11 @@ void RemoteProxyTestsProxy::tcpRemoteConnection() QVERIFY(connectionTwo->isAuthenticated()); // Wait for both to be connected - remoteConnectionEstablishedOne.wait(); - remoteConnectionEstablishedTwo.wait(); + if (remoteConnectionEstablishedOne.count() < 1) + remoteConnectionEstablishedOne.wait(); + + if (remoteConnectionEstablishedTwo.count() < 1) + remoteConnectionEstablishedTwo.wait(); QVERIFY(remoteConnectionEstablishedOne.count() == 1); QVERIFY(remoteConnectionEstablishedTwo.count() == 1); @@ -1380,8 +1394,11 @@ void RemoteProxyTestsProxy::tcpWebsocketRemoteConnection() QVERIFY(connectionTwo->isAuthenticated()); // Wait for both to be connected - remoteConnectionEstablishedOne.wait(); - remoteConnectionEstablishedTwo.wait(); + if (remoteConnectionEstablishedOne.count() < 1) + remoteConnectionEstablishedOne.wait(); + + if (remoteConnectionEstablishedTwo.count() < 1) + remoteConnectionEstablishedTwo.wait(); QVERIFY(remoteConnectionEstablishedOne.count() == 1); QVERIFY(remoteConnectionEstablishedTwo.count() == 1); diff --git a/tests/testbase/basetest.cpp b/tests/testbase/basetest.cpp index 25a40ca..db1a370 100644 --- a/tests/testbase/basetest.cpp +++ b/tests/testbase/basetest.cpp @@ -398,8 +398,11 @@ bool BaseTest::createRemoteConnection(const QString &token, const QString &nonce } // Wait for both to be connected - remoteConnectionEstablishedOne.wait(); - remoteConnectionEstablishedTwo.wait(); + if (remoteConnectionEstablishedOne.count() < 1) + remoteConnectionEstablishedOne.wait(); + + if (remoteConnectionEstablishedTwo.count() < 1) + remoteConnectionEstablishedTwo.wait(); if (remoteConnectionEstablishedOne.count() != 1 || remoteConnectionEstablishedTwo.count() != 1) { qWarning() << "Could not establish remote connection";