Added a test mode to lanlinkprovider

Should fix tests when you run them on networks with real kdeconnects
running.
This commit is contained in:
Albert Vaca 2015-12-02 09:49:00 -08:00
parent cf6cbd2925
commit af5be6e9e0
4 changed files with 27 additions and 13 deletions

View file

@ -43,7 +43,8 @@
#include <QtNetwork/qsslcipher.h>
#include <QtNetwork/qsslconfiguration.h>
LanLinkProvider::LanLinkProvider()
LanLinkProvider::LanLinkProvider(bool testMode)
: mTestMode(testMode)
{
mTcpPort = 0;
@ -72,10 +73,13 @@ LanLinkProvider::~LanLinkProvider()
void LanLinkProvider::onStart()
{
mUdpServer->bind(QHostAddress::Any, port, QUdpSocket::ShareAddress);
const QHostAddress bindAddress = mTestMode? QHostAddress::LocalHost : QHostAddress::Any;
bool success = mUdpServer->bind(bindAddress, port, QUdpSocket::ShareAddress);
Q_ASSERT(success);
mTcpPort = port;
while (!mServer->listen(QHostAddress::Any, mTcpPort)) {
while (!mServer->listen(bindAddress, mTcpPort)) {
mTcpPort++;
if (mTcpPort > 1764) { //No ports available?
qCritical(KDECONNECT_CORE) << "Error opening a port in range 1714-1764";
@ -107,7 +111,7 @@ void LanLinkProvider::onNetworkChange()
NetworkPackage np("");
NetworkPackage::createIdentityPackage(&np);
np.set("tcpPort", mTcpPort);
mUdpSocket.writeDatagram(np.serialize(), QHostAddress("255.255.255.255"), port);
mUdpSocket.writeDatagram(np.serialize(), mTestMode ? QHostAddress::LocalHost : QHostAddress::Any, port);
}
//I'm the existing device, a new device is kindly introducing itself.

View file

@ -38,7 +38,7 @@ class LanLinkProvider
Q_OBJECT
public:
LanLinkProvider();
LanLinkProvider(bool testMode = false);
~LanLinkProvider();
QString name() override { return "LanLinkProvider"; }
@ -81,6 +81,7 @@ private:
};
QMap<QSslSocket*, PendingConnect> receivedIdentityPackages;
QNetworkConfiguration m_lastConfig;
const bool mTestMode;
};
#endif

View file

@ -39,6 +39,7 @@ void Server::incomingConnection(qintptr socketDescriptor) {
pendingConnections.append(serverSocket);
Q_EMIT newConnection();
} else {
qWarning() << "setSocketDescriptor failed " + serverSocket->errorString();
delete serverSocket;
}
}

View file

@ -39,6 +39,12 @@
class LanLinkProviderTest : public QObject
{
Q_OBJECT
public:
explicit LanLinkProviderTest()
: mLanLinkProvider(true) {
QStandardPaths::setTestModeEnabled(true);
}
public Q_SLOTS:
void initTestCase();
@ -94,11 +100,12 @@ void LanLinkProviderTest::pairedDeviceTcpPackageReceived()
addTrustedDevice();
QUdpSocket* mUdpServer = new QUdpSocket;
mUdpServer->bind(QHostAddress::Any, 1714, QUdpSocket::ShareAddress);
bool b = mUdpServer->bind(QHostAddress::LocalHost, 1714, QUdpSocket::ShareAddress);
QVERIFY(b);
QSignalSpy spy(mUdpServer, SIGNAL(readyRead()));
mLanLinkProvider.onNetworkChange();
QVERIFY(spy.wait());
QVERIFY(!spy.isEmpty() || spy.wait());
QByteArray datagram;
datagram.resize(mUdpServer->pendingDatagramSize());
@ -150,7 +157,7 @@ void LanLinkProviderTest::pairedDeviceUdpPackageReceived()
mServer = new Server(this);
mUdpSocket = new QUdpSocket(this);
mServer->listen(QHostAddress::Any, PORT);
mServer->listen(QHostAddress::LocalHost, PORT);
QSignalSpy spy(mServer, SIGNAL(newConnection()));
@ -158,7 +165,7 @@ void LanLinkProviderTest::pairedDeviceUdpPackageReceived()
QCOMPARE(bytesWritten, mIdentityPackage.size());
// We should have an incoming connection now, wait for incoming connection
QVERIFY(spy.wait());
QVERIFY(!spy.isEmpty() || spy.wait());
QSslSocket* serverSocket = mServer->nextPendingConnection();
@ -201,11 +208,12 @@ void LanLinkProviderTest::pairedDeviceUdpPackageReceived()
void LanLinkProviderTest::unpairedDeviceTcpPackageReceived()
{
QUdpSocket* mUdpServer = new QUdpSocket;
mUdpServer->bind(QHostAddress::Any, 1714, QUdpSocket::ShareAddress);
bool b = mUdpServer->bind(QHostAddress::LocalHost, 1714, QUdpSocket::ShareAddress);
QVERIFY(b);
QSignalSpy spy(mUdpServer, SIGNAL(readyRead()));
mLanLinkProvider.onNetworkChange();
QVERIFY(spy.wait());
QVERIFY(!spy.isEmpty() || spy.wait());
QByteArray datagram;
datagram.resize(mUdpServer->pendingDatagramSize());
@ -252,13 +260,13 @@ void LanLinkProviderTest::unpairedDeviceUdpPackageReceived()
mServer = new Server(this);
mUdpSocket = new QUdpSocket(this);
mServer->listen(QHostAddress::Any, PORT);
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
QCOMPARE(bytesWritten, mIdentityPackage.size());
QVERIFY(spy.wait());
QVERIFY(!spy.isEmpty() || spy.wait());
QSslSocket* serverSocket = mServer->nextPendingConnection();