2015-09-07 13:54:33 +01:00
/**
* Copyright 2015 Aleix Pol Gonzalez < aleixpol @ kde . org >
*
* 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-09-07 13:54:33 +01:00
*/
# include <QSocketNotifier>
2016-06-22 16:49:45 +01:00
# include <kdeconnectconfig.h>
# include <backends/lan/uploadjob.h>
# include <core/filetransferjob.h>
2015-09-07 13:54:33 +01:00
# include <QApplication>
# include <QNetworkAccessManager>
# include <QTest>
# include <QTemporaryFile>
# include <QSignalSpy>
2015-09-09 19:12:05 +01:00
# include <QStandardPaths>
2015-09-07 13:54:33 +01:00
# include <KIO/AccessManager>
# include "core/daemon.h"
# include "core/device.h"
# include "core/kdeconnectplugin.h"
2015-12-01 18:45:14 +00:00
# include <backends/pairinghandler.h>
2015-09-07 13:54:33 +01:00
# include "kdeconnect-version.h"
2015-12-05 23:08:02 +00:00
# include "testdaemon.h"
2018-11-28 18:56:22 +00:00
# include <plugins/share/shareplugin.h>
# include <backends/lan/compositeuploadjob.h>
2015-09-07 13:54:33 +01:00
class TestSendFile : public QObject
{
Q_OBJECT
public :
2016-06-05 18:19:31 +01:00
TestSendFile ( ) {
2015-09-09 19:12:05 +01:00
QStandardPaths : : setTestModeEnabled ( true ) ;
2017-09-03 20:39:44 +01:00
m_daemon = new TestDaemon ;
2015-09-09 19:12:05 +01:00
}
2015-09-07 13:54:33 +01:00
private Q_SLOTS :
void testSend ( ) {
2019-06-02 22:26:47 +01:00
if ( ! ( m_daemon - > getLinkProviders ( ) . size ( ) > 0 ) ) {
QFAIL ( " No links available, but loopback should have been provided by the test " ) ;
}
2017-09-03 20:39:44 +01:00
m_daemon - > acquireDiscoveryMode ( QStringLiteral ( " test " ) ) ;
2015-09-08 19:11:52 +01:00
Device * d = nullptr ;
2017-09-03 20:39:44 +01:00
const QList < Device * > devicesList = m_daemon - > devicesList ( ) ;
2017-07-20 15:14:07 +01:00
for ( Device * id : devicesList ) {
2015-09-09 19:12:05 +01:00
if ( id - > isReachable ( ) ) {
2016-06-05 18:19:31 +01:00
if ( ! id - > isTrusted ( ) )
id - > requestPair ( ) ;
2015-09-08 19:11:52 +01:00
d = id ;
2015-09-09 19:12:05 +01:00
}
2015-09-08 19:11:52 +01:00
}
2019-06-02 22:26:47 +01:00
if ( d = = nullptr ) {
QFAIL ( " Unable to determine device " ) ;
}
2017-09-03 20:39:44 +01:00
m_daemon - > releaseDiscoveryMode ( QStringLiteral ( " test " ) ) ;
2015-09-07 13:54:33 +01:00
QCOMPARE ( d - > isReachable ( ) , true ) ;
2015-12-01 18:45:14 +00:00
QCOMPARE ( d - > isTrusted ( ) , true ) ;
2015-09-07 13:54:33 +01:00
QByteArray content ( " 12312312312313213123213123 " ) ;
QTemporaryFile temp ;
temp . open ( ) ;
temp . write ( content ) ;
temp . close ( ) ;
2016-11-26 14:38:08 +00:00
KdeConnectPlugin * plugin = d - > plugin ( QStringLiteral ( " kdeconnect_share " ) ) ;
2015-09-07 13:54:33 +01:00
QVERIFY ( plugin ) ;
plugin - > metaObject ( ) - > invokeMethod ( plugin , " shareUrl " , Q_ARG ( QString , QUrl : : fromLocalFile ( temp . fileName ( ) ) . toString ( ) ) ) ;
2017-08-01 22:57:50 +01:00
QSignalSpy spy ( plugin , SIGNAL ( shareReceived ( QString ) ) ) ;
2015-09-07 13:54:33 +01:00
QVERIFY ( spy . wait ( 2000 ) ) ;
QVariantList args = spy . takeFirst ( ) ;
2015-12-07 02:27:40 +00:00
QUrl sentFile ( args . first ( ) . toUrl ( ) ) ;
2015-09-07 13:54:33 +01:00
QFile file ( sentFile . toLocalFile ( ) ) ;
QCOMPARE ( file . size ( ) , content . size ( ) ) ;
QVERIFY ( file . open ( QIODevice : : ReadOnly ) ) ;
QCOMPARE ( file . readAll ( ) , content ) ;
}
2016-06-22 16:49:45 +01:00
void testSslJobs ( )
{
const QString aFile = QFINDTESTDATA ( " sendfiletest.cpp " ) ;
2019-06-10 15:40:28 +01:00
const QString destFile = QDir : : tempPath ( ) + QStringLiteral ( " /kdeconnect-test-sentfile " ) ;
2016-06-22 16:49:45 +01:00
QFile ( destFile ) . remove ( ) ;
const QString deviceId = KdeConnectConfig : : instance ( ) - > deviceId ( )
2016-11-26 14:38:08 +00:00
, deviceName = QStringLiteral ( " testdevice " )
2016-06-22 16:49:45 +01:00
, deviceType = KdeConnectConfig : : instance ( ) - > deviceType ( ) ;
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
2016-06-22 16:49:45 +01:00
2019-02-12 23:09:04 +00:00
//We need the device to be loaded on the daemon, otherwise CompositeUploadJob will get a null device
Device * device = new Device ( this , deviceId ) ;
m_daemon - > addDevice ( device ) ;
2016-06-22 16:49:45 +01:00
QSharedPointer < QFile > f ( new QFile ( aFile ) ) ;
2018-11-28 18:56:22 +00:00
NetworkPacket np ( PACKET_TYPE_SHARE_REQUEST ) ;
2019-02-12 23:09:57 +00:00
np . setPayload ( f , f - > size ( ) ) ;
2018-11-28 18:56:22 +00:00
CompositeUploadJob * job = new CompositeUploadJob ( deviceId , false ) ;
UploadJob * uj = new UploadJob ( np ) ;
job - > addSubjob ( uj ) ;
QSignalSpy spyUpload ( job , & KJob : : result ) ;
job - > start ( ) ;
2016-06-22 16:49:45 +01:00
2018-08-15 16:18:32 +01:00
f - > open ( QIODevice : : ReadWrite ) ;
2016-06-22 16:49:45 +01:00
2019-02-12 23:09:57 +00:00
FileTransferJob * ft = np . createPayloadTransferJob ( QUrl : : fromLocalFile ( destFile ) ) ;
2016-06-22 16:49:45 +01:00
QSignalSpy spyTransfer ( ft , & KJob : : result ) ;
ft - > start ( ) ;
2019-02-12 23:10:30 +00:00
QVERIFY ( spyTransfer . count ( ) | | spyTransfer . wait ( ) ) ;
2016-06-22 16:49:45 +01:00
2019-02-12 23:10:30 +00:00
if ( ft - > error ( ) ) {
qWarning ( ) < < " fterror " < < ft - > errorString ( ) ;
}
2016-06-22 16:49:45 +01:00
QCOMPARE ( ft - > error ( ) , 0 ) ;
2019-02-12 23:10:30 +00:00
QCOMPARE ( spyUpload . count ( ) , 1 ) ;
2016-06-22 16:49:45 +01:00
QFile resultFile ( destFile ) , originFile ( aFile ) ;
QVERIFY ( resultFile . open ( QIODevice : : ReadOnly ) ) ;
QVERIFY ( originFile . open ( QIODevice : : ReadOnly ) ) ;
const QByteArray resultContents = resultFile . readAll ( ) , originContents = originFile . readAll ( ) ;
QCOMPARE ( resultContents . size ( ) , originContents . size ( ) ) ;
QCOMPARE ( resultFile . readAll ( ) , originFile . readAll ( ) ) ;
}
2015-09-07 13:54:33 +01:00
private :
2017-09-03 20:39:44 +01:00
TestDaemon * m_daemon ;
2015-09-07 13:54:33 +01:00
} ;
QTEST_MAIN ( TestSendFile ) ;
# include "sendfiletest.moc"