diff options
author | Timur Pocheptsov <timur.pocheptsov@theqtcompany.com> | 2016-08-02 10:36:56 +0200 |
---|---|---|
committer | Timur Pocheptsov <timur.pocheptsov@theqtcompany.com> | 2016-08-26 13:41:27 +0000 |
commit | 21e7cb1bafdfc06c263e10067d02f9b103ff660f (patch) | |
tree | f6f5df73ef23e5d1cebcb9347dbd40be91a673cc /tests | |
parent | 95513185fd90cb9372a8cb8b541de7a3f41b12d4 (diff) |
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 <alexander.blasche@qt.io>
Diffstat (limited to 'tests')
-rw-r--r-- | tests/auto/qbluetoothsocket/tst_qbluetoothsocket.cpp | 47 |
1 files changed, 47 insertions, 0 deletions
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" |