tst_QSslSocket::qtbug18498_peek() - fix several problems

It all started from the compiler's warnings about 'this' captured but
not used in lambdas. While fixing this it was noticed that 'client' socket
has a lifetime longer than the test case itself (the socket has a parent,
which is tst_QSslSocket object). The 'server' socket was simply leaked.
So there is no guarantee that some of them (or both) later, after the
test failed in one of QVERIFY, for example, does not emit 'encrypted'
upon receiving more data; this will result: in reading/writing from/to
invalid memory location (captured local 'encryptedCount') and/or probably
exiting event loop when it's not expected to do so.

Change-Id: I51de0493d989a5ba36de2cef58d35526c0e26cda
Reviewed-by: Mårten Nordheim <marten.nordheim@qt.io>
bb10
Timur Pocheptsov 2018-06-26 14:48:53 +02:00
parent 92666ff521
commit 73b9242d7e
1 changed files with 16 additions and 18 deletions

View File

@ -2809,13 +2809,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 +2829,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);