2015-09-07 13:54:33 +01:00
|
|
|
/**
|
2020-08-17 10:48:10 +01:00
|
|
|
* SPDX-FileCopyrightText: 2015 Aleix Pol Gonzalez <aleixpol@kde.org>
|
2015-09-07 13:54:33 +01:00
|
|
|
*
|
2020-08-17 10:48:10 +01:00
|
|
|
* SPDX-License-Identifier: GPL-2.0-only OR GPL-3.0-only OR LicenseRef-KDE-Accepted-GPL
|
2015-09-07 13:54:33 +01:00
|
|
|
*/
|
|
|
|
|
2019-12-09 22:14:19 +00:00
|
|
|
#include <QCoreApplication>
|
2015-09-07 13:54:33 +01:00
|
|
|
#include <QSignalSpy>
|
2022-09-10 22:23:52 +01:00
|
|
|
#include <QSocketNotifier>
|
2015-09-09 19:12:05 +01:00
|
|
|
#include <QStandardPaths>
|
2022-09-10 22:23:52 +01:00
|
|
|
#include <QTemporaryFile>
|
|
|
|
#include <QTest>
|
|
|
|
#include <backends/lan/uploadjob.h>
|
|
|
|
#include <core/filetransferjob.h>
|
|
|
|
#include <kdeconnectconfig.h>
|
2015-09-07 13:54:33 +01:00
|
|
|
|
|
|
|
#include "core/daemon.h"
|
|
|
|
#include "core/device.h"
|
|
|
|
#include "core/kdeconnectplugin.h"
|
|
|
|
#include "kdeconnect-version.h"
|
2015-12-05 23:08:02 +00:00
|
|
|
#include "testdaemon.h"
|
2018-11-28 18:56:22 +00:00
|
|
|
#include <backends/lan/compositeuploadjob.h>
|
2022-09-10 22:23:52 +01:00
|
|
|
#include <backends/pairinghandler.h>
|
|
|
|
#include <plugins/share/shareplugin.h>
|
2015-09-07 13:54:33 +01:00
|
|
|
|
|
|
|
class TestSendFile : public QObject
|
|
|
|
{
|
|
|
|
Q_OBJECT
|
2022-09-10 22:23:52 +01:00
|
|
|
public:
|
|
|
|
TestSendFile()
|
|
|
|
{
|
|
|
|
QStandardPaths::setTestModeEnabled(true);
|
|
|
|
m_daemon = new TestDaemon;
|
|
|
|
}
|
|
|
|
|
|
|
|
private Q_SLOTS:
|
|
|
|
void testSend()
|
|
|
|
{
|
|
|
|
if (!(m_daemon->getLinkProviders().size() > 0)) {
|
|
|
|
QFAIL("No links available, but loopback should have been provided by the test");
|
2015-09-09 19:12:05 +01:00
|
|
|
}
|
2015-09-07 13:54:33 +01:00
|
|
|
|
2022-09-10 22:23:52 +01:00
|
|
|
Device *d = nullptr;
|
|
|
|
const QList<Device *> devicesList = m_daemon->devicesList();
|
|
|
|
for (Device *id : devicesList) {
|
|
|
|
if (id->isReachable()) {
|
2023-06-01 01:25:37 +01:00
|
|
|
if (!id->isPaired())
|
|
|
|
id->requestPairing();
|
2022-09-10 22:23:52 +01:00
|
|
|
d = id;
|
2019-06-02 22:26:47 +01:00
|
|
|
}
|
2022-09-10 22:23:52 +01:00
|
|
|
}
|
|
|
|
if (d == nullptr) {
|
|
|
|
QFAIL("Unable to determine device");
|
|
|
|
}
|
|
|
|
QCOMPARE(d->isReachable(), true);
|
2023-06-01 01:25:37 +01:00
|
|
|
QCOMPARE(d->isPaired(), true);
|
2015-09-07 13:54:33 +01:00
|
|
|
|
2022-09-10 22:23:52 +01:00
|
|
|
QByteArray content("12312312312313213123213123");
|
2015-09-07 13:54:33 +01:00
|
|
|
|
2022-09-10 22:23:52 +01:00
|
|
|
QTemporaryFile temp;
|
|
|
|
temp.open();
|
|
|
|
temp.write(content);
|
|
|
|
temp.close();
|
2015-09-07 13:54:33 +01:00
|
|
|
|
2022-09-10 22:23:52 +01:00
|
|
|
KdeConnectPlugin *plugin = d->plugin(QStringLiteral("kdeconnect_share"));
|
|
|
|
QVERIFY(plugin);
|
|
|
|
plugin->metaObject()->invokeMethod(plugin, "shareUrl", Q_ARG(QString, QUrl::fromLocalFile(temp.fileName()).toString()));
|
2015-09-07 13:54:33 +01:00
|
|
|
|
2022-09-10 22:23:52 +01:00
|
|
|
QSignalSpy spy(plugin, SIGNAL(shareReceived(QString)));
|
|
|
|
QVERIFY(spy.wait(2000));
|
2015-09-07 13:54:33 +01:00
|
|
|
|
2022-09-10 22:23:52 +01:00
|
|
|
QVariantList args = spy.takeFirst();
|
|
|
|
QUrl sentFile(args.first().toUrl());
|
2015-09-07 13:54:33 +01:00
|
|
|
|
2022-09-10 22:23:52 +01:00
|
|
|
QFile file(sentFile.toLocalFile());
|
|
|
|
QCOMPARE(file.size(), content.size());
|
|
|
|
QVERIFY(file.open(QIODevice::ReadOnly));
|
|
|
|
QCOMPARE(file.readAll(), content);
|
|
|
|
}
|
2015-09-07 13:54:33 +01:00
|
|
|
|
2022-09-10 22:23:52 +01:00
|
|
|
void testSslJobs()
|
|
|
|
{
|
|
|
|
const QString aFile = QFINDTESTDATA("sendfiletest.cpp");
|
|
|
|
const QString destFile = QDir::tempPath() + QStringLiteral("/kdeconnect-test-sentfile");
|
|
|
|
QFile(destFile).remove();
|
2016-06-22 16:49:45 +01:00
|
|
|
|
2023-06-27 12:10:59 +01:00
|
|
|
DeviceInfo deviceInfo = KdeConnectConfig::instance().deviceInfo();
|
|
|
|
KdeConnectConfig::instance().addTrustedDevice(deviceInfo);
|
2016-06-22 16:49:45 +01:00
|
|
|
|
2023-06-27 12:10:59 +01:00
|
|
|
Device *device = new Device(this, deviceInfo.id);
|
2022-09-10 22:23:52 +01:00
|
|
|
m_daemon->addDevice(device);
|
2019-02-12 23:09:04 +00:00
|
|
|
|
2022-09-10 22:23:52 +01:00
|
|
|
QSharedPointer<QFile> f(new QFile(aFile));
|
|
|
|
NetworkPacket np(PACKET_TYPE_SHARE_REQUEST);
|
|
|
|
np.setPayload(f, f->size());
|
2019-02-12 23:09:57 +00:00
|
|
|
|
2024-06-03 12:52:44 +01:00
|
|
|
CompositeUploadJob *job = new CompositeUploadJob(device, false);
|
2022-09-10 22:23:52 +01:00
|
|
|
UploadJob *uj = new UploadJob(np);
|
|
|
|
job->addSubjob(uj);
|
2019-09-08 16:09:52 +01:00
|
|
|
|
2022-09-10 22:23:52 +01:00
|
|
|
QSignalSpy spyUpload(job, &KJob::result);
|
|
|
|
job->start();
|
2016-06-22 16:49:45 +01:00
|
|
|
|
2022-09-10 22:23:52 +01:00
|
|
|
f->open(QIODevice::ReadWrite);
|
2016-06-22 16:49:45 +01:00
|
|
|
|
2022-09-10 22:23:52 +01:00
|
|
|
FileTransferJob *ft = np.createPayloadTransferJob(QUrl::fromLocalFile(destFile));
|
2016-06-22 16:49:45 +01:00
|
|
|
|
2022-09-10 22:23:52 +01:00
|
|
|
QSignalSpy spyTransfer(ft, &KJob::result);
|
2016-06-22 16:49:45 +01:00
|
|
|
|
2022-09-10 22:23:52 +01:00
|
|
|
ft->start();
|
2016-06-22 16:49:45 +01:00
|
|
|
|
2022-09-10 22:23:52 +01:00
|
|
|
QVERIFY(spyTransfer.count() || spyTransfer.wait());
|
2016-06-22 16:49:45 +01:00
|
|
|
|
2022-09-10 22:23:52 +01:00
|
|
|
if (ft->error()) {
|
|
|
|
qWarning() << "fterror" << ft->errorString();
|
|
|
|
}
|
2016-06-22 16:49:45 +01:00
|
|
|
|
2022-09-10 22:23:52 +01:00
|
|
|
QCOMPARE(ft->error(), 0);
|
2019-02-12 23:10:30 +00:00
|
|
|
|
2022-09-10 22:23:52 +01:00
|
|
|
QCOMPARE(spyUpload.count(), 1);
|
2016-06-22 16:49:45 +01:00
|
|
|
|
2022-09-10 22:23:52 +01:00
|
|
|
QFile resultFile(destFile), originFile(aFile);
|
|
|
|
QVERIFY(resultFile.open(QIODevice::ReadOnly));
|
|
|
|
QVERIFY(originFile.open(QIODevice::ReadOnly));
|
2016-06-22 16:49:45 +01:00
|
|
|
|
2022-09-10 22:23:52 +01:00
|
|
|
const QByteArray resultContents = resultFile.readAll(), originContents = originFile.readAll();
|
|
|
|
QCOMPARE(resultContents.size(), originContents.size());
|
|
|
|
QCOMPARE(resultFile.readAll(), originFile.readAll());
|
|
|
|
}
|
2016-06-22 16:49:45 +01:00
|
|
|
|
2022-09-10 22:23:52 +01:00
|
|
|
private:
|
|
|
|
TestDaemon *m_daemon;
|
2015-09-07 13:54:33 +01:00
|
|
|
};
|
|
|
|
|
|
|
|
QTEST_MAIN(TestSendFile);
|
|
|
|
|
|
|
|
#include "sendfiletest.moc"
|