From 21e7cb1bafdfc06c263e10067d02f9b103ff660f Mon Sep 17 00:00:00 2001 From: Timur Pocheptsov Date: Tue, 2 Aug 2016 10:36:56 +0200 Subject: QBluetoothSocket::connectToService - check a socket type first Attempt to connect to a service using a socket with UnknownProtocol socket type will fail, but can result in an incorrect UnknownSocketError reported and also can start a device discovery (then finally failing to connect after all). Check this condition early before trying to actually connect/do device discovery and report error properly as UnsupportedProtocolError. Task-number: QTBUG-55073 Change-Id: Ib39e1ca7ad401e07d6387201a4664a1185d38d39 Reviewed-by: Alex Blasche --- .../auto/qbluetoothsocket/tst_qbluetoothsocket.cpp | 47 ++++++++++++++++++++++ 1 file changed, 47 insertions(+) (limited to 'tests') diff --git a/tests/auto/qbluetoothsocket/tst_qbluetoothsocket.cpp b/tests/auto/qbluetoothsocket/tst_qbluetoothsocket.cpp index 7af7cf5e..37dce573 100644 --- a/tests/auto/qbluetoothsocket/tst_qbluetoothsocket.cpp +++ b/tests/auto/qbluetoothsocket/tst_qbluetoothsocket.cpp @@ -83,6 +83,8 @@ private slots: void tst_preferredSecurityFlags(); + void tst_unsupportedProtocolError(); + public slots: void serviceDiscovered(const QBluetoothServiceInfo &info); void finished(); @@ -524,6 +526,51 @@ void tst_QBluetoothSocket::tst_preferredSecurityFlags() #endif } +void tst_QBluetoothSocket::tst_unsupportedProtocolError() +{ +#if defined(QT_ANDROID_BLUETOOTH) + QSKIP("Android platform (re)sets RFCOMM socket type, nothing to test"); +#endif + // This socket has 'UnknownProtocol' socketType. + // Any attempt to connectToService should end in + // UnsupportedProtocolError. + QBluetoothSocket socket; + QCOMPARE(socket.socketType(), QBluetoothServiceInfo::UnknownProtocol); + QVERIFY(socket.error() == QBluetoothSocket::NoSocketError); + QVERIFY(socket.errorString() == QString()); + + QSignalSpy errorSpy(&socket, SIGNAL(error(QBluetoothSocket::SocketError))); + + // 1. Stop early with 'UnsupportedProtocolError'. + QBluetoothServiceInfo dummyServiceInfo; + socket.connectToService(dummyServiceInfo, QIODevice::ReadWrite); + QTRY_COMPARE_WITH_TIMEOUT(errorSpy.size(), 1, 1000); + QCOMPARE(errorSpy.size(), 1); + QCOMPARE(errorSpy.takeFirst().at(0).toInt(), int(QBluetoothSocket::UnsupportedProtocolError)); + QVERIFY(socket.errorString().size() != 0); + QCOMPARE(socket.state(), QBluetoothSocket::UnconnectedState); + + errorSpy.clear(); + + // 2. Stop early with UnsupportedProtocolError (before testing an invalid address/port). + socket.connectToService(QBluetoothAddress(), 1, QIODevice::ReadWrite); + QTRY_COMPARE_WITH_TIMEOUT(errorSpy.size(), 1, 1000); + QCOMPARE(errorSpy.size(), 1); + QCOMPARE(errorSpy.takeFirst().at(0).toInt(), int(QBluetoothSocket::UnsupportedProtocolError)); + QVERIFY(socket.errorString().size() != 0); + QCOMPARE(socket.state(), QBluetoothSocket::UnconnectedState); + + errorSpy.clear(); + + // 3. Stop early (ignoring an invalid address/uuid). + socket.connectToService(QBluetoothAddress(), QBluetoothUuid(), QIODevice::ReadWrite); + QTRY_COMPARE_WITH_TIMEOUT(errorSpy.size(), 1, 1000); + QCOMPARE(errorSpy.size(), 1); + QCOMPARE(errorSpy.takeFirst().at(0).toInt(), int(QBluetoothSocket::UnsupportedProtocolError)); + QVERIFY(socket.errorString().size() != 0); + QCOMPARE(socket.state(), QBluetoothSocket::UnconnectedState); +} + QTEST_MAIN(tst_QBluetoothSocket) #include "tst_qbluetoothsocket.moc" -- cgit v1.2.3