From 91f952f5f1660f297093893adcb4a3d2c2243a59 Mon Sep 17 00:00:00 2001 From: Aleix Pol Date: Mon, 8 Aug 2016 19:38:58 +0200 Subject: [PATCH] Fix test Use the right port Store the port in a public variable, so it can be used from the test. --- core/backends/lan/lanlinkprovider.cpp | 12 ++++++------ core/backends/lan/lanlinkprovider.h | 3 ++- tests/lanlinkprovidertest.cpp | 8 ++++---- 3 files changed, 12 insertions(+), 11 deletions(-) diff --git a/core/backends/lan/lanlinkprovider.cpp b/core/backends/lan/lanlinkprovider.cpp index d969e23d9..275d4e073 100644 --- a/core/backends/lan/lanlinkprovider.cpp +++ b/core/backends/lan/lanlinkprovider.cpp @@ -79,12 +79,12 @@ void LanLinkProvider::onStart() { const QHostAddress bindAddress = mTestMode? QHostAddress::LocalHost : QHostAddress::Any; - bool success = mUdpSocket.bind(bindAddress, port, QUdpSocket::ShareAddress); + bool success = mUdpSocket.bind(bindAddress, PORT, QUdpSocket::ShareAddress); Q_ASSERT(success); qCDebug(KDECONNECT_CORE) << "onStart"; - mTcpPort = port; + mTcpPort = PORT; while (!mServer->listen(bindAddress, mTcpPort)) { mTcpPort++; if (mTcpPort > 1764) { //No ports available? @@ -128,7 +128,7 @@ void LanLinkProvider::broadcastToNetwork() NetworkPackage np(""); NetworkPackage::createIdentityPackage(&np); np.set("tcpPort", mTcpPort); - mUdpSocket.writeDatagram(np.serialize(), mTestMode ? QHostAddress::LocalHost : QHostAddress("255.255.255.255"), port); + mUdpSocket.writeDatagram(np.serialize(), mTestMode ? QHostAddress::LocalHost : QHostAddress("255.255.255.255"), PORT); } //I'm the existing device, a new device is kindly introducing itself. @@ -164,7 +164,7 @@ void LanLinkProvider::newUdpConnection() //udpBroadcastReceived continue; } - int tcpPort = receivedPackage->get("tcpPort", port); + int tcpPort = receivedPackage->get("tcpPort", PORT); //qCDebug(KDECONNECT_CORE) << "Received Udp identity package from" << sender << " asking for a tcp connection on port " << tcpPort; @@ -188,7 +188,7 @@ void LanLinkProvider::connectError() NetworkPackage np(""); NetworkPackage::createIdentityPackage(&np); np.set("tcpPort", mTcpPort); - mUdpSocket.writeDatagram(np.serialize(), receivedIdentityPackages[socket].sender, port); + mUdpSocket.writeDatagram(np.serialize(), receivedIdentityPackages[socket].sender, PORT); //The socket we created didn't work, and we didn't manage //to create a LanDeviceLink from it, deleting everything. @@ -251,7 +251,7 @@ void LanLinkProvider::connected() //I think this will never happen, but if it happens the deviceLink //(or the socket that is now inside it) might not be valid. Delete them. qCDebug(KDECONNECT_CORE) << "Fallback (2), try reverse connection (send udp packet)"; - mUdpSocket.writeDatagram(np2.serialize(), receivedIdentityPackages[socket].sender, port); + mUdpSocket.writeDatagram(np2.serialize(), receivedIdentityPackages[socket].sender, PORT); } delete receivedIdentityPackages.take(socket).np; diff --git a/core/backends/lan/lanlinkprovider.h b/core/backends/lan/lanlinkprovider.h index 133fc691b..de51f72df 100644 --- a/core/backends/lan/lanlinkprovider.h +++ b/core/backends/lan/lanlinkprovider.h @@ -54,6 +54,8 @@ public: static void configureSslSocket(QSslSocket* socket, const QString& deviceId, bool isDeviceTrusted); static void configureSocket(QSslSocket* socket); + const static quint16 PORT = 1716; + public Q_SLOTS: void onNetworkChange() override; void onStart() override; @@ -78,7 +80,6 @@ private: Server* mServer; QUdpSocket mUdpSocket; - const static quint16 port = 1716; quint16 mTcpPort; QMap mLinks; diff --git a/tests/lanlinkprovidertest.cpp b/tests/lanlinkprovidertest.cpp index fc5667841..c7e283404 100644 --- a/tests/lanlinkprovidertest.cpp +++ b/tests/lanlinkprovidertest.cpp @@ -100,7 +100,7 @@ void LanLinkProviderTest::pairedDeviceTcpPackageReceived() addTrustedDevice(); QUdpSocket* mUdpServer = new QUdpSocket; - bool b = mUdpServer->bind(QHostAddress::LocalHost, 1714, QUdpSocket::ShareAddress); + bool b = mUdpServer->bind(QHostAddress::LocalHost, LanLinkProvider::PORT, QUdpSocket::ShareAddress); QVERIFY(b); QSignalSpy spy(mUdpServer, SIGNAL(readyRead())); @@ -161,7 +161,7 @@ void LanLinkProviderTest::pairedDeviceUdpPackageReceived() QSignalSpy spy(mServer, SIGNAL(newConnection())); - qint64 bytesWritten = mUdpSocket->writeDatagram(mIdentityPackage.toLatin1(), QHostAddress::LocalHost, 1714); // write an identity package to udp socket here, we do not broadcast it here + qint64 bytesWritten = mUdpSocket->writeDatagram(mIdentityPackage.toLatin1(), QHostAddress::LocalHost, LanLinkProvider::PORT); // write an identity package to udp socket here, we do not broadcast it here QCOMPARE(bytesWritten, mIdentityPackage.size()); // We should have an incoming connection now, wait for incoming connection @@ -208,7 +208,7 @@ void LanLinkProviderTest::pairedDeviceUdpPackageReceived() void LanLinkProviderTest::unpairedDeviceTcpPackageReceived() { QUdpSocket* mUdpServer = new QUdpSocket; - bool b = mUdpServer->bind(QHostAddress::LocalHost, 1714, QUdpSocket::ShareAddress); + bool b = mUdpServer->bind(QHostAddress::LocalHost, LanLinkProvider::PORT, QUdpSocket::ShareAddress); QVERIFY(b); QSignalSpy spy(mUdpServer, SIGNAL(readyRead())); @@ -263,7 +263,7 @@ void LanLinkProviderTest::unpairedDeviceUdpPackageReceived() mServer->listen(QHostAddress::LocalHost, PORT); QSignalSpy spy(mServer, &Server::newConnection); - qint64 bytesWritten = mUdpSocket->writeDatagram(mIdentityPackage.toLatin1(), QHostAddress::LocalHost, 1714); // write an identity package to udp socket here, we do not broadcast it here + qint64 bytesWritten = mUdpSocket->writeDatagram(mIdentityPackage.toLatin1(), QHostAddress::LocalHost, LanLinkProvider::PORT); // write an identity package to udp socket here, we do not broadcast it here QCOMPARE(bytesWritten, mIdentityPackage.size()); QVERIFY(!spy.isEmpty() || spy.wait());