2015-08-20 18:45:12 +01:00
/**
* Copyright 2015 Vineet Garg < grgvineet @ gmail . com >
*
* This program is free software ; you can redistribute it and / or
* modify it under the terms of the GNU General Public License as
* published by the Free Software Foundation ; either version 2 of
* the License or ( at your option ) version 3 or any later version
* accepted by the membership of KDE e . V . ( or its successor approved
* by the membership of KDE e . V . ) , which shall act as a proxy
* defined in Section 14 of version 3 of the license .
*
* This program is distributed in the hope that it will be useful ,
* but WITHOUT ANY WARRANTY ; without even the implied warranty of
* MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE . See the
* GNU General Public License for more details .
*
* You should have received a copy of the GNU General Public License
2019-03-23 16:29:26 +00:00
* along with this program . If not , see < https : //www.gnu.org/licenses/>.
2015-08-20 18:45:12 +01:00
*/
2015-12-01 15:25:34 +00:00
# include "../core/device.h"
2015-08-20 18:45:12 +01:00
# include "../core/backends/lan/lanlinkprovider.h"
# include "../core/kdeconnectconfig.h"
# include <QtTest>
/**
* This class tests the working of device class
*/
class DeviceTest : public QObject
{
Q_OBJECT
private Q_SLOTS :
void initTestCase ( ) ;
void testUnpairedDevice ( ) ;
void testPairedDevice ( ) ;
void cleanupTestCase ( ) ;
private :
QString deviceId ;
QString deviceName ;
QString deviceType ;
2018-03-04 19:48:51 +00:00
NetworkPacket * identityPacket ;
2015-08-20 18:45:12 +01:00
} ;
void DeviceTest : : initTestCase ( )
{
2016-11-26 14:38:08 +00:00
deviceId = QStringLiteral ( " testdevice " ) ;
deviceName = QStringLiteral ( " Test Device " ) ;
deviceType = QStringLiteral ( " smartphone " ) ;
2018-03-04 19:48:51 +00:00
QString stringPacket = QStringLiteral ( " { \" id \" :1439365924847, \" type \" : \" kdeconnect.identity \" , \" body \" :{ \" deviceId \" : \" testdevice \" , \" deviceName \" : \" Test Device \" , \" protocolVersion \" :6, \" deviceType \" : \" phone \" }} " ) ;
identityPacket = new NetworkPacket ( QStringLiteral ( " kdeconnect.identity " ) ) ;
NetworkPacket : : unserialize ( stringPacket . toLatin1 ( ) , identityPacket ) ;
2015-08-20 18:45:12 +01:00
}
void DeviceTest : : testPairedDevice ( )
{
KdeConnectConfig * kcc = KdeConnectConfig : : instance ( ) ;
kcc - > addTrustedDevice ( deviceId , deviceName , deviceType ) ;
2016-11-26 14:38:08 +00:00
kcc - > setDeviceProperty ( deviceId , QStringLiteral ( " certificate " ) , QString : : fromLatin1 ( kcc - > certificate ( ) . toPem ( ) ) ) ; // Using same certificate from kcc, instead of generating one
2015-08-20 18:45:12 +01:00
Device device ( this , deviceId ) ;
QCOMPARE ( device . id ( ) , deviceId ) ;
QCOMPARE ( device . name ( ) , deviceName ) ;
QCOMPARE ( device . type ( ) , deviceType ) ;
2015-12-01 18:45:14 +00:00
QCOMPARE ( device . isTrusted ( ) , true ) ;
2015-08-20 18:45:12 +01:00
QCOMPARE ( device . isReachable ( ) , false ) ;
// Add link
LanLinkProvider linkProvider ;
QSslSocket socket ;
2016-01-10 15:12:13 +00:00
LanDeviceLink * link = new LanDeviceLink ( deviceId , & linkProvider , & socket , LanDeviceLink : : Locally ) ;
2018-03-04 19:48:51 +00:00
device . addLink ( * identityPacket , link ) ;
2015-08-20 18:45:12 +01:00
QCOMPARE ( device . isReachable ( ) , true ) ;
QCOMPARE ( device . availableLinks ( ) . contains ( linkProvider . name ( ) ) , true ) ;
// Remove link
device . removeLink ( link ) ;
QCOMPARE ( device . isReachable ( ) , false ) ;
QCOMPARE ( device . availableLinks ( ) . contains ( linkProvider . name ( ) ) , false ) ;
device . unpair ( ) ;
2015-12-01 18:45:14 +00:00
QCOMPARE ( device . isTrusted ( ) , false ) ;
2015-08-20 18:45:12 +01:00
}
void DeviceTest : : testUnpairedDevice ( )
{
2018-07-12 15:14:32 +01:00
KdeConnectConfig * kcc = KdeConnectConfig : : instance ( ) ;
kcc - > removeTrustedDevice ( deviceId ) ;
2015-08-20 18:45:12 +01:00
LanLinkProvider linkProvider ;
QSslSocket socket ;
2016-01-10 15:12:13 +00:00
LanDeviceLink * link = new LanDeviceLink ( deviceId , & linkProvider , & socket , LanDeviceLink : : Locally ) ;
2015-08-20 18:45:12 +01:00
2018-03-04 19:48:51 +00:00
Device device ( this , * identityPacket , link ) ;
2015-08-20 18:45:12 +01:00
QCOMPARE ( device . id ( ) , deviceId ) ;
QCOMPARE ( device . name ( ) , deviceName ) ;
QCOMPARE ( device . type ( ) , deviceType ) ;
2015-12-01 18:45:14 +00:00
QCOMPARE ( device . isTrusted ( ) , false ) ;
2015-08-20 18:45:12 +01:00
QCOMPARE ( device . isReachable ( ) , true ) ;
QCOMPARE ( device . availableLinks ( ) . contains ( linkProvider . name ( ) ) , true ) ;
// Remove link
device . removeLink ( link ) ;
QCOMPARE ( device . isReachable ( ) , false ) ;
QCOMPARE ( device . availableLinks ( ) . contains ( linkProvider . name ( ) ) , false ) ;
}
void DeviceTest : : cleanupTestCase ( )
{
2018-03-04 19:48:51 +00:00
delete identityPacket ;
2015-08-20 18:45:12 +01:00
}
2016-01-10 15:12:13 +00:00
2015-08-20 18:45:12 +01:00
QTEST_GUILESS_MAIN ( DeviceTest )
# include "devicetest.moc"