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
* along with this program . If not , see < http : //www.gnu.org/licenses/>.
*/
# include <QSocketNotifier>
2016-06-22 16:49:45 +01:00
# include <backends/lan/downloadjob.h>
# 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"
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 ) ;
2016-06-05 18:19:31 +01:00
mDaemon = new TestDaemon ;
2015-09-09 19:12:05 +01:00
}
2015-09-07 13:54:33 +01:00
private Q_SLOTS :
void testSend ( ) {
2016-06-05 18:19:31 +01:00
mDaemon - > acquireDiscoveryMode ( " test " ) ;
2015-09-08 19:11:52 +01:00
Device * d = nullptr ;
2016-06-21 19:19:14 +01:00
Q_FOREACH ( Device * id , mDaemon - > 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
}
2016-06-05 18:19:31 +01:00
mDaemon - > releaseDiscoveryMode ( " test " ) ;
2015-09-08 19:11:52 +01:00
QVERIFY ( d ) ;
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 ( ) ;
KdeConnectPlugin * plugin = d - > plugin ( " kdeconnect_share " ) ;
QVERIFY ( plugin ) ;
plugin - > metaObject ( ) - > invokeMethod ( plugin , " shareUrl " , Q_ARG ( QString , QUrl : : fromLocalFile ( temp . fileName ( ) ) . toString ( ) ) ) ;
2015-12-07 02:27:40 +00:00
QSignalSpy spy ( plugin , SIGNAL ( shareReceived ( QUrl ) ) ) ;
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 " ) ;
const QString destFile = QDir : : tempPath ( ) + " /kdeconnect-test-sentfile " ;
QFile ( destFile ) . remove ( ) ;
const QString deviceId = KdeConnectConfig : : instance ( ) - > deviceId ( )
, deviceName = " testdevice "
, deviceType = KdeConnectConfig : : instance ( ) - > deviceType ( ) ;
KdeConnectConfig * kcc = KdeConnectConfig : : instance ( ) ;
kcc - > addTrustedDevice ( deviceId , deviceName , deviceType ) ;
kcc - > setDeviceProperty ( deviceId , QString ( " certificate " ) , QString : : fromLatin1 ( kcc - > certificate ( ) . toPem ( ) ) ) ; // Using same certificate from kcc, instead of generating
QSharedPointer < QFile > f ( new QFile ( aFile ) ) ;
UploadJob * uj = new UploadJob ( f , deviceId ) ;
QSignalSpy spyUpload ( uj , & KJob : : result ) ;
uj - > start ( ) ;
auto info = uj - > transferInfo ( ) ;
info . insert ( " deviceId " , deviceId ) ;
info . insert ( " size " , aFile . size ( ) ) ;
DownloadJob * dj = new DownloadJob ( QHostAddress : : LocalHost , info ) ;
QVERIFY ( dj - > getPayload ( ) - > open ( QIODevice : : ReadOnly ) ) ;
FileTransferJob * ft = new FileTransferJob ( dj - > getPayload ( ) , uj - > transferInfo ( ) [ " size " ] . toInt ( ) , QUrl : : fromLocalFile ( destFile ) ) ;
QSignalSpy spyDownload ( dj , & KJob : : result ) ;
QSignalSpy spyTransfer ( ft , & KJob : : result ) ;
ft - > start ( ) ;
dj - > start ( ) ;
2016-07-05 11:28:31 +01:00
QVERIFY ( spyTransfer . count ( ) | | spyTransfer . wait ( 1000000000 ) ) ;
2016-06-22 16:49:45 +01:00
if ( ft - > error ( ) ) qWarning ( ) < < " fterror " < < ft - > errorString ( ) ;
QCOMPARE ( ft - > error ( ) , 0 ) ;
QCOMPARE ( spyDownload . count ( ) , 1 ) ;
QCOMPARE ( spyUpload . count ( ) , 1 ) ;
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 :
TestDaemon * mDaemon ;
} ;
QTEST_MAIN ( TestSendFile ) ;
# include "sendfiletest.moc"