From f044384c15d709b3ef9ede73d26809a18d7b6984 Mon Sep 17 00:00:00 2001 From: =?UTF-8?q?Sandro=20Knau=C3=9F?= Date: Fri, 5 Jul 2019 12:53:06 +0000 Subject: [PATCH] fix race condition on tests. The test expects the SSL server socket to be encrypted, but was not waiting for it to happen. Use a QSignalSpy to wait for the signal that ensures us we can go ahead. --- tests/testsslsocketlinereader.cpp | 24 ++++++++++++++++++++++++ 1 file changed, 24 insertions(+) diff --git a/tests/testsslsocketlinereader.cpp b/tests/testsslsocketlinereader.cpp index 125f22118..8ae8ac6d4 100644 --- a/tests/testsslsocketlinereader.cpp +++ b/tests/testsslsocketlinereader.cpp @@ -25,6 +25,7 @@ #include #include #include +#include /* * This class tests the behaviour of socket line reader when the connection if over ssl. Since SSL takes part below application layer, @@ -112,6 +113,8 @@ void TestSslSocketLineReader::testTrustedDevice() QSslSocket* serverSocket = m_server->nextPendingConnection(); + QSignalSpy serverEncryptedSpy(serverSocket, SIGNAL(encrypted())); + QVERIFY2(serverSocket != 0, "Null socket returned by server"); QVERIFY2(serverSocket->isOpen(), "Server socket already closed"); @@ -135,10 +138,20 @@ void TestSslSocketLineReader::testTrustedDevice() }; connect(serverSocket, &QSslSocket::encrypted, connected_lambda); connect(m_clientSocket, &QSslSocket::encrypted, connected_lambda); + serverSocket->startServerEncryption(); m_clientSocket->startClientEncryption(); m_loop.exec(); //Block until QEventLoop::quit gets called by the lambda + if (serverEncryptedSpy.count() < 1) { + for(int x = 0;x < 50; ++x) { + QVERIFY(serverEncryptedSpy.wait(100)); + if (serverEncryptedSpy.count() > 0) { + break; + } + } + } + // Both client and server socket should be encrypted here and should have remote certificate because VerifyPeer is used QVERIFY2(m_clientSocket->isOpen(), "Client socket already closed"); QVERIFY2(serverSocket->isOpen(), "Server socket already closed"); @@ -182,6 +195,8 @@ void TestSslSocketLineReader::testUntrustedDevice() QSslSocket* serverSocket = m_server->nextPendingConnection(); + QSignalSpy serverEncryptedSpy(serverSocket, SIGNAL(encrypted())); + QVERIFY2(serverSocket != 0, "Null socket returned by server"); QVERIFY2(serverSocket->isOpen(), "Server socket already closed"); @@ -207,6 +222,15 @@ void TestSslSocketLineReader::testUntrustedDevice() m_clientSocket->startClientEncryption(); m_loop.exec(); //Block until QEventLoop::quit gets called by the lambda + if (serverEncryptedSpy.count() < 1) { + for(int x = 0;x < 50; ++x) { + QVERIFY(serverEncryptedSpy.wait(100)); + if (serverEncryptedSpy.count() > 0) { + break; + } + } + } + QVERIFY2(m_clientSocket->isOpen(), "Client socket already closed"); QVERIFY2(serverSocket->isOpen(), "Server socket already closed"); QVERIFY2(m_clientSocket->isEncrypted(), "Client is not encrypted");