summaryrefslogtreecommitdiffstats
path: root/tests/auto/network/ssl/qsslsocket/tst_qsslsocket.cpp
diff options
context:
space:
mode:
Diffstat (limited to 'tests/auto/network/ssl/qsslsocket/tst_qsslsocket.cpp')
-rw-r--r--tests/auto/network/ssl/qsslsocket/tst_qsslsocket.cpp111
1 files changed, 89 insertions, 22 deletions
diff --git a/tests/auto/network/ssl/qsslsocket/tst_qsslsocket.cpp b/tests/auto/network/ssl/qsslsocket/tst_qsslsocket.cpp
index e32fa7c724..f45a5af5a1 100644
--- a/tests/auto/network/ssl/qsslsocket/tst_qsslsocket.cpp
+++ b/tests/auto/network/ssl/qsslsocket/tst_qsslsocket.cpp
@@ -202,6 +202,9 @@ private slots:
void verifyDepth();
void disconnectFromHostWhenConnecting();
void disconnectFromHostWhenConnected();
+#ifndef QT_NO_OPENSSL
+ void closeWhileEmittingSocketError();
+#endif
void resetProxy();
void ignoreSslErrorsList_data();
void ignoreSslErrorsList();
@@ -1340,13 +1343,19 @@ void tst_QSslSocket::protocolServerSide()
QAbstractSocket::SocketState expectedState = (works) ? QAbstractSocket::ConnectedState : QAbstractSocket::UnconnectedState;
// Determine whether the client or the server caused the event loop
// to quit due to a socket error, and investigate the culprit.
- if (server.socket->error() != QAbstractSocket::UnknownSocketError) {
+ if (client.error() != QAbstractSocket::UnknownSocketError) {
+ // It can happen that the client, after TCP connection established, before
+ // incomingConnection() slot fired, hits TLS initialization error and stops
+ // the loop, so the server socket is not created yet.
+ if (server.socket)
+ QVERIFY(server.socket->error() == QAbstractSocket::UnknownSocketError);
+
+ QCOMPARE(int(client.state()), int(expectedState));
+ } else if (server.socket->error() != QAbstractSocket::UnknownSocketError) {
QVERIFY(client.error() == QAbstractSocket::UnknownSocketError);
QCOMPARE(int(server.socket->state()), int(expectedState));
- } else if (client.error() != QAbstractSocket::UnknownSocketError) {
- QVERIFY(server.socket->error() == QAbstractSocket::UnknownSocketError);
- QCOMPARE(int(client.state()), int(expectedState));
}
+
QCOMPARE(client.isEncrypted(), works);
}
@@ -2336,6 +2345,66 @@ void tst_QSslSocket::disconnectFromHostWhenConnected()
QCOMPARE(socket->bytesToWrite(), qint64(0));
}
+#ifndef QT_NO_OPENSSL
+
+class BrokenPskHandshake : public QTcpServer
+{
+public:
+ void socketError(QAbstractSocket::SocketError error)
+ {
+ Q_UNUSED(error);
+ QSslSocket *clientSocket = qobject_cast<QSslSocket *>(sender());
+ Q_ASSERT(clientSocket);
+ clientSocket->close();
+ QTestEventLoop::instance().exitLoop();
+ }
+private:
+
+ void incomingConnection(qintptr handle) override
+ {
+ if (!socket.setSocketDescriptor(handle))
+ return;
+
+ QSslConfiguration serverConfig(QSslConfiguration::defaultConfiguration());
+ serverConfig.setPreSharedKeyIdentityHint("abcdefghijklmnop");
+ socket.setSslConfiguration(serverConfig);
+ socket.startServerEncryption();
+ }
+
+ QSslSocket socket;
+};
+
+void tst_QSslSocket::closeWhileEmittingSocketError()
+{
+ QFETCH_GLOBAL(bool, setProxy);
+ if (setProxy)
+ return;
+
+ BrokenPskHandshake handshake;
+ if (!handshake.listen())
+ QSKIP("failed to start TLS server");
+
+ QSslSocket clientSocket;
+ QSslConfiguration clientConfig(QSslConfiguration::defaultConfiguration());
+ clientConfig.setPeerVerifyMode(QSslSocket::VerifyNone);
+ clientSocket.setSslConfiguration(clientConfig);
+
+ QSignalSpy socketErrorSpy(&clientSocket, SIGNAL(error(QAbstractSocket::SocketError)));
+ void (QSslSocket::*errorSignal)(QAbstractSocket::SocketError) = &QSslSocket::error;
+ connect(&clientSocket, errorSignal, &handshake, &BrokenPskHandshake::socketError);
+
+ clientSocket.connectToHostEncrypted(QStringLiteral("127.0.0.1"), handshake.serverPort());
+ // Make sure we have some data buffered so that close will try to flush:
+ clientSocket.write(QByteArray(1000000, Qt::Uninitialized));
+
+ QTestEventLoop::instance().enterLoopMSecs(1000);
+ QVERIFY(!QTestEventLoop::instance().timeout());
+
+ QCOMPARE(socketErrorSpy.count(), 1);
+}
+
+#endif // QT_NO_OPENSSL
+
void tst_QSslSocket::resetProxy()
{
#ifndef QT_NO_NETWORKPROXY
@@ -2809,13 +2878,13 @@ class SslServer4 : public QTcpServer
{
Q_OBJECT
public:
- SslServer4() : socket(0) {}
- WebSocket *socket;
+
+ QScopedPointer<WebSocket> socket;
protected:
- void incomingConnection(qintptr socketDescriptor)
+ void incomingConnection(qintptr socketDescriptor) override
{
- socket = new WebSocket(socketDescriptor);
+ socket.reset(new WebSocket(socketDescriptor));
}
};
@@ -2829,38 +2898,36 @@ void tst_QSslSocket::qtbug18498_peek()
return;
SslServer4 server;
- QSslSocket *client = new QSslSocket(this);
-
QVERIFY(server.listen(QHostAddress::LocalHost));
- client->connectToHost("127.0.0.1", server.serverPort());
- QVERIFY(client->waitForConnected(5000));
+
+ QSslSocket client;
+ client.connectToHost("127.0.0.1", server.serverPort());
+ QVERIFY(client.waitForConnected(5000));
QVERIFY(server.waitForNewConnection(1000));
- client->setObjectName("client");
- client->ignoreSslErrors();
+ client.ignoreSslErrors();
int encryptedCounter = 2;
- connect(client, &QSslSocket::encrypted, this, [&encryptedCounter, this](){
+ connect(&client, &QSslSocket::encrypted, this, [&encryptedCounter](){
if (!--encryptedCounter)
exitLoop();
});
- WebSocket *serversocket = server.socket;
- connect(serversocket, &QSslSocket::encrypted, this, [&encryptedCounter, this](){
+ WebSocket *serversocket = server.socket.data();
+ connect(serversocket, &QSslSocket::encrypted, this, [&encryptedCounter](){
if (!--encryptedCounter)
exitLoop();
});
- connect(client, SIGNAL(disconnected()), this, SLOT(exitLoop()));
+ connect(&client, SIGNAL(disconnected()), this, SLOT(exitLoop()));
- client->startClientEncryption();
+ client.startClientEncryption();
QVERIFY(serversocket);
- serversocket->setObjectName("server");
enterLoop(1);
QVERIFY(!timeout());
QVERIFY(serversocket->isEncrypted());
- QVERIFY(client->isEncrypted());
+ QVERIFY(client.isEncrypted());
QByteArray data("abc123");
- client->write(data.data());
+ client.write(data.data());
connect(serversocket, SIGNAL(readyRead()), this, SLOT(exitLoop()));
enterLoop(1);