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 <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 ) ;
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 ( ) {
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
}
2017-09-03 20:39:44 +01:00
m_daemon - > releaseDiscoveryMode ( QStringLiteral ( " 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 ( ) ;
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 " ) ;
const QString destFile = QDir : : tempPath ( ) + " /kdeconnect-test-sentfile " ;
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
QSharedPointer < QFile > f ( new QFile ( aFile ) ) ;
UploadJob * uj = new UploadJob ( f , deviceId ) ;
QSignalSpy spyUpload ( uj , & KJob : : result ) ;
uj - > start ( ) ;
auto info = uj - > transferInfo ( ) ;
2016-11-26 14:38:08 +00:00
info . insert ( QStringLiteral ( " deviceId " ) , deviceId ) ;
info . insert ( QStringLiteral ( " size " ) , aFile . size ( ) ) ;
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
2018-08-15 16:18:32 +01:00
FileTransferJob * ft = new FileTransferJob ( f , uj - > transferInfo ( ) [ QStringLiteral ( " size " ) ] . toInt ( ) , QUrl : : fromLocalFile ( destFile ) ) ;
2016-06-22 16:49:45 +01:00
QSignalSpy spyTransfer ( ft , & KJob : : result ) ;
ft - > 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 ) ;
2018-08-15 16:18:32 +01:00
// HACK | FIXME: Why does this break the test?
//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"