1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 22 23 24 25 26 27 28 29 30 31 32 33 34 35 36 37 38 39 40 41 42 43 44 45 46 47 48 49 50 51 52 53 54 55 56 57 58 59 60 61 62 63 64 65 66 67 68 69 70 71 72 73 74 75 76 77 78 79 80 81 82 83 84 85 86 87 88 89 90 91 92 93 94 95 96 97 98 99 100 101 102 103 104 105 106 107 108 109 110 111 112 113 114 115 116 117 118 119 120 121 122 123 124 125 126 127 128 129 130 131 132 133 134 135 136 137 138 139 140 141 142 143 144 145 146 147 148 149 150 151 152 153 154 155 156 157 158 159 160 161 162 163 164 165 166 167 168 169 170 171 172 173 174 175 176 177 178 179 180 181 182 183 184 185 186 187 188 189 190 191 192 193 194 195 196 197 198 199 200 201 202 203 204 205 206 207 208 209 210 211 212 213 214 215 216 217 218 219 220 221 222 223 224 225 226 227 228 229 230 231 232 233 234 235 236 237 238 239 240 241 242 243 244 245 246 247 248 249 250 251 252 253 254 255 256 257 258 259 260 261 262 263 264 265 266 267 268 269 270 271 272 273 274 275 276 277 278 279 280 281 282 283 284 285 286 287 288 289 290 291 292 293 294 295 296 297 298 299 300 301 302 303 304 305 306 307 308 309 310 311 312 313 314 315 316 317 318 319 320 321 322 323 324 325 326 327 328 329 330 331 332 333 334 335 336 337 338 339 340 341 342 343 344 345 346 347 348 349 350 351 352 353 354 355 356 357 358 359 360 361 362 363 364 365 366 367 368 369 370 371 372 373 374 375 376 377 378 379 380 381 382 383 384 385 386 387 388 389 390 391 392 393 394 395 396 397 398 399 400 401 402 403 404 405 406 407 408 409 410 411 412 413 414 415 416 417 418 419 420 421 422 423 424 425 426 427 428 429 430 431 432 433 434 435 436 437 438 439 440 441 442 443 444 445 446 447 448 449 450 451 452 453 454 455 456 457 458 459 460 461 462 463 464 465 466 467 468 469 470 471 472 473 474 475 476 477 478
|
/****************************************************************************
**
** Copyright (C) 2013 Digia Plc and/or its subsidiary(-ies).
** Contact: http://www.qt-project.org/legal
**
** This file is part of the QtBluetooth module of the Qt Toolkit.
**
** $QT_BEGIN_LICENSE:LGPL$
** Commercial License Usage
** Licensees holding valid commercial Qt licenses may use this file in
** accordance with the commercial license agreement provided with the
** Software or, alternatively, in accordance with the terms contained in
** a written agreement between you and Digia. For licensing terms and
** conditions see http://qt.digia.com/licensing. For further information
** use the contact form at http://qt.digia.com/contact-us.
**
** GNU Lesser General Public License Usage
** Alternatively, this file may be used under the terms of the GNU Lesser
** General Public License version 2.1 as published by the Free Software
** Foundation and appearing in the file LICENSE.LGPL included in the
** packaging of this file. Please review the following information to
** ensure the GNU Lesser General Public License version 2.1 requirements
** will be met: http://www.gnu.org/licenses/old-licenses/lgpl-2.1.html.
**
** In addition, as a special exception, Digia gives you certain additional
** rights. These rights are described in the Digia Qt LGPL Exception
** version 1.1, included in the file LGPL_EXCEPTION.txt in this package.
**
** GNU General Public License Usage
** Alternatively, this file may be used under the terms of the GNU
** General Public License version 3.0 as published by the Free Software
** Foundation and appearing in the file LICENSE.GPL included in the
** packaging of this file. Please review the following information to
** ensure the GNU General Public License version 3.0 requirements will be
** met: http://www.gnu.org/copyleft/gpl.html.
**
**
** $QT_END_LICENSE$
**
****************************************************************************/
#include <QtTest/QtTest>
#include <QDebug>
#include <qbluetoothsocket.h>
#include <qbluetoothdeviceinfo.h>
#include <qbluetoothserviceinfo.h>
#include <qbluetoothservicediscoveryagent.h>
#include <qbluetoothlocaldevice.h>
QT_USE_NAMESPACE
Q_DECLARE_METATYPE(QBluetoothSocket::SocketState)
Q_DECLARE_METATYPE(QBluetoothSocket::SocketError)
Q_DECLARE_METATYPE(QBluetoothServiceInfo::Protocol)
//same uuid as tests/bttestui
#define TEST_SERVICE_UUID "e8e10f95-1a70-4b27-9ccf-02010264e9c8"
// Max time to wait for connection
static const int MaxConnectTime = 60 * 1000; // 1 minute in ms
static const int MaxReadWriteTime = 60 * 1000; // 1 minute in ms
class tst_QBluetoothSocket : public QObject
{
Q_OBJECT
public:
enum ClientConnectionShutdown {
Error,
Disconnect,
Close,
Abort,
};
tst_QBluetoothSocket();
~tst_QBluetoothSocket();
private slots:
void initTestCase();
void tst_construction_data();
void tst_construction();
void tst_serviceConnection();
void tst_clientCommunication_data();
void tst_clientCommunication();
void tst_error();
public slots:
void serviceDiscovered(const QBluetoothServiceInfo &info);
void finished();
void error(QBluetoothServiceDiscoveryAgent::Error error);
private:
bool done_discovery;
bool localDeviceFound;
QBluetoothDeviceInfo remoteDevice;
QBluetoothHostInfo localDevice;
QBluetoothServiceInfo remoteServiceInfo;
};
Q_DECLARE_METATYPE(tst_QBluetoothSocket::ClientConnectionShutdown)
tst_QBluetoothSocket::tst_QBluetoothSocket()
{
qRegisterMetaType<QBluetoothSocket::SocketState>("QBluetoothSocket::SocketState");
qRegisterMetaType<QBluetoothSocket::SocketError>("QBluetoothSocket::SocketError");
localDeviceFound = false; // true if we have a local adapter
done_discovery = false; //true if we found remote device
//Enable logging to facilitate debugging in case of error
QLoggingCategory::setFilterRules(QStringLiteral("qt.bluetooth* = true"));
}
tst_QBluetoothSocket::~tst_QBluetoothSocket()
{
}
void tst_QBluetoothSocket::initTestCase()
{
// setup Bluetooth env
const QList<QBluetoothHostInfo> foundDevices = QBluetoothLocalDevice::allDevices();
if (!foundDevices.count()) {
qWarning() << "Missing local device";
return;
} else {
localDevice = foundDevices.at(0);
qDebug() << "Local device" << localDevice.name() << localDevice.address().toString();
}
localDeviceFound = true;
//start the device
QBluetoothLocalDevice device(localDevice.address());
device.powerOn();
//find the remote test server
//the remote test server is tests/bttestui
// Go find the server
QBluetoothServiceDiscoveryAgent *sda = new QBluetoothServiceDiscoveryAgent(this);
connect(sda, SIGNAL(serviceDiscovered(QBluetoothServiceInfo)), this, SLOT(serviceDiscovered(QBluetoothServiceInfo)));
connect(sda, SIGNAL(error(QBluetoothServiceDiscoveryAgent::Error)), this, SLOT(error(QBluetoothServiceDiscoveryAgent::Error)));
connect(sda, SIGNAL(finished()), this, SLOT(finished()));
qDebug() << "Starting discovery";
sda->setUuidFilter(QBluetoothUuid(QString(TEST_SERVICE_UUID)));
sda->start(QBluetoothServiceDiscoveryAgent::MinimalDiscovery);
for (int connectTime = MaxConnectTime; !done_discovery && connectTime > 0; connectTime -= 1000)
QTest::qWait(1000);
sda->stop();
if (!remoteDevice.isValid()) {
qWarning() << "#########################";
qWarning() << "Unable to find test service";
qWarning() << "Remote device may have to be changed into Discoverable mode";
qWarning() << "#########################";
}
delete sda;
}
void tst_QBluetoothSocket::error(QBluetoothServiceDiscoveryAgent::Error error)
{
qDebug() << "Received error" << error;
done_discovery = true;
}
void tst_QBluetoothSocket::finished()
{
qDebug() << "Finished";
done_discovery = true;
}
void tst_QBluetoothSocket::serviceDiscovered(const QBluetoothServiceInfo &info)
{
qDebug() << "Found test service on:" << info.device().name() << info.device().address().toString();
remoteDevice = info.device();
remoteServiceInfo = info;
done_discovery = true;
}
void tst_QBluetoothSocket::tst_construction_data()
{
QTest::addColumn<QBluetoothServiceInfo::Protocol>("socketType");
QTest::newRow("unknown protocol") << QBluetoothServiceInfo::UnknownProtocol;
QTest::newRow("rfcomm socket") << QBluetoothServiceInfo::RfcommProtocol;
QTest::newRow("l2cap socket") << QBluetoothServiceInfo::L2capProtocol;
}
void tst_QBluetoothSocket::tst_construction()
{
QFETCH(QBluetoothServiceInfo::Protocol, socketType);
{
QBluetoothSocket socket;
QCOMPARE(socket.socketType(), QBluetoothServiceInfo::UnknownProtocol);
QCOMPARE(socket.error(), QBluetoothSocket::NoSocketError);
QCOMPARE(socket.errorString(), QString());
QCOMPARE(socket.peerAddress(), QBluetoothAddress());
QCOMPARE(socket.peerName(), QString());
QCOMPARE(socket.peerPort(), quint16(0));
QCOMPARE(socket.state(), QBluetoothSocket::UnconnectedState);
QCOMPARE(socket.socketDescriptor(), -1);
QCOMPARE(socket.bytesAvailable(), 0);
QCOMPARE(socket.bytesToWrite(), 0);
QCOMPARE(socket.canReadLine(), false);
QCOMPARE(socket.isSequential(), true);
QCOMPARE(socket.atEnd(), true);
QCOMPARE(socket.pos(), 0);
QCOMPARE(socket.size(), 0);
QSignalSpy spy(&socket, SIGNAL(error(QBluetoothSocket::SocketError)));
QCOMPARE(spy.count(), 0);
QByteArray array = socket.readAll();
QVERIFY(array.isEmpty());
QCOMPARE(socket.error(), QBluetoothSocket::OperationError);
QCOMPARE(spy.count(), 1);
char buffer[10];
int returnValue = socket.read((char*)&buffer, 10);
QCOMPARE(returnValue, -1);
QCOMPARE(socket.error(), QBluetoothSocket::OperationError);
QVERIFY(spy.count() >= 2);
while (spy.count()) {
QBluetoothSocket::SocketError er = spy.takeFirst().at(0).value<QBluetoothSocket::SocketError>();
QVERIFY(er == QBluetoothSocket::OperationError);
}
}
{
QBluetoothSocket socket(socketType);
QCOMPARE(socket.socketType(), socketType);
}
}
void tst_QBluetoothSocket::tst_serviceConnection()
{
if (!remoteServiceInfo.isValid())
QSKIP("Remote service not found");
/* Construction */
QBluetoothSocket socket;
QSignalSpy stateSpy(&socket, SIGNAL(stateChanged(QBluetoothSocket::SocketState)));
QCOMPARE(socket.socketType(), QBluetoothServiceInfo::UnknownProtocol);
QCOMPARE(socket.state(), QBluetoothSocket::UnconnectedState);
/* Connection */
QSignalSpy connectedSpy(&socket, SIGNAL(connected()));
QSignalSpy errorSpy(&socket, SIGNAL(error(QBluetoothSocket::SocketError)));
socket.connectToService(remoteServiceInfo);
QCOMPARE(stateSpy.count(), 1);
QCOMPARE(stateSpy.takeFirst().at(0).value<QBluetoothSocket::SocketState>(), QBluetoothSocket::ConnectingState);
QCOMPARE(socket.state(), QBluetoothSocket::ConnectingState);
stateSpy.clear();
int connectTime = MaxConnectTime;
while (connectedSpy.count() == 0 && errorSpy.count() == 0 && connectTime > 0) {
QTest::qWait(1000);
connectTime -= 1000;
}
if (errorSpy.count() != 0) {
qDebug() << errorSpy.takeFirst().at(0).toInt();
QSKIP("Connection error");
}
QCOMPARE(connectedSpy.count(), 1);
QCOMPARE(stateSpy.count(), 1);
QCOMPARE(stateSpy.takeFirst().at(0).value<QBluetoothSocket::SocketState>(), QBluetoothSocket::ConnectedState);
QCOMPARE(socket.state(), QBluetoothSocket::ConnectedState);
stateSpy.clear();
//check the peer & local info
QCOMPARE(socket.localAddress(), localDevice.address());
QCOMPARE(socket.localName(), localDevice.name());
QCOMPARE(socket.peerName(), remoteDevice.name());
QCOMPARE(socket.peerAddress(), remoteDevice.address());
/* Disconnection */
QSignalSpy disconnectedSpy(&socket, SIGNAL(disconnected()));
socket.disconnectFromService();
QVERIFY(stateSpy.count() >= 1);
QCOMPARE(stateSpy.takeFirst().at(0).value<QBluetoothSocket::SocketState>(), QBluetoothSocket::ClosingState);
int disconnectTime = MaxConnectTime;
while (disconnectedSpy.count() == 0 && disconnectTime > 0) {
QTest::qWait(1000);
disconnectTime -= 1000;
}
QCOMPARE(disconnectedSpy.count(), 1);
QCOMPARE(stateSpy.count(), 1);
QCOMPARE(stateSpy.takeFirst().at(0).value<QBluetoothSocket::SocketState>(), QBluetoothSocket::UnconnectedState);
// The remote service needs time to close the connection and resume listening
QTest::qSleep(100);
}
void tst_QBluetoothSocket::tst_clientCommunication_data()
{
QTest::addColumn<QStringList>("data");
{
QStringList data;
data << QLatin1String("Echo: Test line one.\n");
data << QLatin1String("Echo: Test line two, with longer data.\n");
QTest::newRow("two line test") << data;
}
}
void tst_QBluetoothSocket::tst_clientCommunication()
{
if (!remoteServiceInfo.isValid())
QSKIP("Remote service not found");
QFETCH(QStringList, data);
/* Construction */
QBluetoothSocket socket(QBluetoothServiceInfo::RfcommProtocol);
QSignalSpy stateSpy(&socket, SIGNAL(stateChanged(QBluetoothSocket::SocketState)));
QCOMPARE(socket.socketType(), QBluetoothServiceInfo::RfcommProtocol);
QCOMPARE(socket.state(), QBluetoothSocket::UnconnectedState);
/* Connection */
QSignalSpy connectedSpy(&socket, SIGNAL(connected()));
socket.connectToService(remoteServiceInfo);
QCOMPARE(stateSpy.count(), 1);
QCOMPARE(qvariant_cast<QBluetoothSocket::SocketState>(stateSpy.takeFirst().at(0)), QBluetoothSocket::ConnectingState);
QCOMPARE(socket.state(), QBluetoothSocket::ConnectingState);
stateSpy.clear();
int connectTime = MaxConnectTime;
while (connectedSpy.count() == 0 && connectTime > 0) {
QTest::qWait(1000);
connectTime -= 1000;
}
QCOMPARE(connectedSpy.count(), 1);
QCOMPARE(stateSpy.count(), 1);
QCOMPARE(qvariant_cast<QBluetoothSocket::SocketState>(stateSpy.takeFirst().at(0)), QBluetoothSocket::ConnectedState);
QCOMPARE(socket.state(), QBluetoothSocket::ConnectedState);
stateSpy.clear();
/* Read / Write */
QCOMPARE(socket.bytesToWrite(), qint64(0));
QCOMPARE(socket.bytesAvailable(), qint64(0));
{
/* Send line by line with event loop */
foreach (const QString &line, data) {
QSignalSpy readyReadSpy(&socket, SIGNAL(readyRead()));
QSignalSpy bytesWrittenSpy(&socket, SIGNAL(bytesWritten(qint64)));
socket.write(line.toUtf8());
// QEXPECT_FAIL("", "TODO: need to implement write buffering", Continue);
QCOMPARE(socket.bytesToWrite(), qint64(line.length()));
int readWriteTime = MaxReadWriteTime;
while ((bytesWrittenSpy.count() == 0 || readyReadSpy.count() == 0) && readWriteTime > 0) {
QTest::qWait(1000);
readWriteTime -= 1000;
}
QCOMPARE(bytesWrittenSpy.count(), 1);
QCOMPARE(bytesWrittenSpy.at(0).at(0).toLongLong(), qint64(line.length()));
readWriteTime = MaxReadWriteTime;
while ((readyReadSpy.count() == 0) && readWriteTime > 0) {
QTest::qWait(1000);
readWriteTime -= 1000;
}
QCOMPARE(readyReadSpy.count(), 1);
QCOMPARE(socket.bytesAvailable(), qint64(line.length()));
QVERIFY(socket.canReadLine());
QByteArray echoed = socket.readAll();
QCOMPARE(line.toUtf8(), echoed);
}
}
{
/* Send all at once */
QSignalSpy readyReadSpy(&socket, SIGNAL(readyRead()));
QSignalSpy bytesWrittenSpy(&socket, SIGNAL(bytesWritten(qint64)));
QString joined = data.join(QString());
socket.write(joined.toUtf8());
// QEXPECT_FAIL("", "TODO: need to implement write buffering", Continue);
QCOMPARE(socket.bytesToWrite(), qint64(joined.length()));
int readWriteTime = MaxReadWriteTime;
while ((bytesWrittenSpy.count() == 0 || readyReadSpy.count() == 0) && readWriteTime > 0) {
QTest::qWait(1000);
readWriteTime -= 1000;
}
QCOMPARE(bytesWrittenSpy.count(), 1);
QCOMPARE(bytesWrittenSpy.at(0).at(0).toLongLong(), qint64(joined.length()));
QVERIFY(readyReadSpy.count() > 0);
QCOMPARE(socket.bytesAvailable(), qint64(joined.length()));
QVERIFY(socket.canReadLine());
QByteArray echoed = socket.readAll();
QCOMPARE(joined.toUtf8(), echoed);
}
/* Disconnection */
QSignalSpy disconnectedSpy(&socket, SIGNAL(disconnected()));
socket.disconnectFromService();
int disconnectTime = MaxConnectTime;
while (disconnectedSpy.count() == 0 && disconnectTime > 0) {
QTest::qWait(1000);
disconnectTime -= 1000;
}
QCOMPARE(disconnectedSpy.count(), 1);
QCOMPARE(stateSpy.count(), 2);
QCOMPARE(qvariant_cast<QBluetoothSocket::SocketState>(stateSpy.takeFirst().at(0)), QBluetoothSocket::ClosingState);
QCOMPARE(qvariant_cast<QBluetoothSocket::SocketState>(stateSpy.takeFirst().at(0)), QBluetoothSocket::UnconnectedState);
// The remote service needs time to close the connection and resume listening
QTest::qSleep(100);
}
void tst_QBluetoothSocket::tst_error()
{
QBluetoothSocket socket;
QSignalSpy errorSpy(&socket, SIGNAL(error(QBluetoothSocket::SocketError)));
QCOMPARE(errorSpy.count(), 0);
const QBluetoothSocket::SocketError e = socket.error();
QVERIFY(e == QBluetoothSocket::NoSocketError);
QVERIFY(socket.errorString() == QString());
}
QTEST_MAIN(tst_QBluetoothSocket)
#include "tst_qbluetoothsocket.moc"
|