-
-
Notifications
You must be signed in to change notification settings - Fork 197
/
sendfiletest.cpp
137 lines (107 loc) · 3.97 KB
/
sendfiletest.cpp
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
/**
* SPDX-FileCopyrightText: 2015 Aleix Pol Gonzalez <[email protected]>
*
* SPDX-License-Identifier: GPL-2.0-only OR GPL-3.0-only OR LicenseRef-KDE-Accepted-GPL
*/
#include <QCoreApplication>
#include <QSignalSpy>
#include <QSocketNotifier>
#include <QStandardPaths>
#include <QTemporaryFile>
#include <QTest>
#include <backends/lan/uploadjob.h>
#include <core/filetransferjob.h>
#include <kdeconnectconfig.h>
#include "core/daemon.h"
#include "core/device.h"
#include "core/kdeconnectplugin.h"
#include "kdeconnect-version.h"
#include "testdaemon.h"
#include <backends/lan/compositeuploadjob.h>
#include <backends/pairinghandler.h>
#include <plugins/share/shareplugin.h>
class TestSendFile : public QObject
{
Q_OBJECT
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");
}
Device *d = nullptr;
const QList<Device *> devicesList = m_daemon->devicesList();
for (Device *id : devicesList) {
if (id->isReachable()) {
if (!id->isPaired())
id->requestPairing();
d = id;
}
}
if (d == nullptr) {
QFAIL("Unable to determine device");
}
QCOMPARE(d->isReachable(), true);
QCOMPARE(d->isPaired(), true);
QByteArray content("12312312312313213123213123");
QTemporaryFile temp;
temp.open();
temp.write(content);
temp.close();
KdeConnectPlugin *plugin = d->plugin(QStringLiteral("kdeconnect_share"));
QVERIFY(plugin);
plugin->metaObject()->invokeMethod(plugin, "shareUrl", Q_ARG(QString, QUrl::fromLocalFile(temp.fileName()).toString()));
QSignalSpy spy(plugin, SIGNAL(shareReceived(QString)));
QVERIFY(spy.wait(2000));
QVariantList args = spy.takeFirst();
QUrl sentFile(args.first().toUrl());
QFile file(sentFile.toLocalFile());
QCOMPARE(file.size(), content.size());
QVERIFY(file.open(QIODevice::ReadOnly));
QCOMPARE(file.readAll(), content);
}
void testSslJobs()
{
const QString aFile = QFINDTESTDATA("sendfiletest.cpp");
const QString destFile = QDir::tempPath() + QStringLiteral("/kdeconnect-test-sentfile");
QFile(destFile).remove();
DeviceInfo deviceInfo = KdeConnectConfig::instance().deviceInfo();
KdeConnectConfig::instance().addTrustedDevice(deviceInfo);
Device *device = new Device(this, deviceInfo.id);
m_daemon->addDevice(device);
QSharedPointer<QFile> f(new QFile(aFile));
NetworkPacket np(PACKET_TYPE_SHARE_REQUEST);
np.setPayload(f, f->size());
CompositeUploadJob *job = new CompositeUploadJob(device, false);
UploadJob *uj = new UploadJob(np);
job->addSubjob(uj);
QSignalSpy spyUpload(job, &KJob::result);
job->start();
f->open(QIODevice::ReadWrite);
FileTransferJob *ft = np.createPayloadTransferJob(QUrl::fromLocalFile(destFile));
QSignalSpy spyTransfer(ft, &KJob::result);
ft->start();
QVERIFY(spyTransfer.count() || spyTransfer.wait());
if (ft->error()) {
qWarning() << "fterror" << ft->errorString();
}
QCOMPARE(ft->error(), 0);
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());
}
private:
TestDaemon *m_daemon;
};
QTEST_MAIN(TestSendFile);
#include "sendfiletest.moc"