Changeset View
Changeset View
Standalone View
Standalone View
tests/lanlinkprovidertest.cpp
Show All 25 Lines | |||||
26 | #include "../core/backends/lan/socketlinereader.h" | 26 | #include "../core/backends/lan/socketlinereader.h" | ||
27 | #include "../core/kdeconnectconfig.h" | 27 | #include "../core/kdeconnectconfig.h" | ||
28 | 28 | | |||
29 | #include <QAbstractSocket> | 29 | #include <QAbstractSocket> | ||
30 | #include <QSslSocket> | 30 | #include <QSslSocket> | ||
31 | #include <QtTest> | 31 | #include <QtTest> | ||
32 | #include <QSslKey> | 32 | #include <QSslKey> | ||
33 | #include <QUdpSocket> | 33 | #include <QUdpSocket> | ||
34 | #include <QtCrypto> | | |||
35 | #include <QMetaEnum> | 34 | #include <QMetaEnum> | ||
36 | 35 | | |||
36 | | ||||
37 | /* | 37 | /* | ||
38 | * This class tests the working of LanLinkProvider under different conditions that when identity packet is received over TCP, over UDP and same when the device is paired. | 38 | * This class tests the working of LanLinkProvider under different conditions that when identity packet is received over TCP, over UDP and same when the device is paired. | ||
39 | * It depends on KdeConnectConfig since LanLinkProvider internally uses it. | 39 | * It depends on KdeConnectConfig since LanLinkProvider internally uses it. | ||
40 | */ | 40 | */ | ||
41 | class LanLinkProviderTest : public QObject | 41 | class LanLinkProviderTest : public QObject | ||
42 | { | 42 | { | ||
43 | Q_OBJECT | 43 | Q_OBJECT | ||
44 | public: | 44 | public: | ||
Show All 35 Lines | 76 | private: | |||
80 | Server* m_server; | 80 | Server* m_server; | ||
81 | SocketLineReader* m_reader; | 81 | SocketLineReader* m_reader; | ||
82 | QUdpSocket* m_udpSocket; | 82 | QUdpSocket* m_udpSocket; | ||
83 | QString m_identityPacket; | 83 | QString m_identityPacket; | ||
84 | 84 | | |||
85 | // Attributes for test device | 85 | // Attributes for test device | ||
86 | QString m_deviceId; | 86 | QString m_deviceId; | ||
87 | QString m_name; | 87 | QString m_name; | ||
88 | QCA::PrivateKey m_privateKey; | | |||
89 | QSslCertificate m_certificate; | 88 | QSslCertificate m_certificate; | ||
89 | QTemporaryFile m_privateKeyFile; | ||||
90 | 90 | | |||
91 | QSslCertificate generateCertificate(QString&, QCA::PrivateKey&); | 91 | QSslCertificate generateCertificate(QString&); | ||
92 | void addTrustedDevice(); | 92 | void addTrustedDevice(); | ||
93 | void removeTrustedDevice(); | 93 | void removeTrustedDevice(); | ||
94 | void setSocketAttributes(QSslSocket* socket); | 94 | void setSocketAttributes(QSslSocket* socket); | ||
95 | void testIdentityPacket(QByteArray& identityPacket); | 95 | void testIdentityPacket(QByteArray& identityPacket); | ||
96 | void socketBindErrorFail(const QUdpSocket& socket); | 96 | void socketBindErrorFail(const QUdpSocket& socket); | ||
97 | }; | 97 | }; | ||
98 | 98 | | |||
99 | void LanLinkProviderTest::initTestCase() | 99 | void LanLinkProviderTest::initTestCase() | ||
100 | { | 100 | { | ||
101 | removeTrustedDevice(); // Remove trusted device if left by chance by any test | 101 | removeTrustedDevice(); // Remove trusted device if left by chance by any test | ||
102 | 102 | | |||
103 | m_deviceId = QStringLiteral("testdevice"); | 103 | m_deviceId = QStringLiteral("testdevice"); | ||
104 | m_name = QStringLiteral("Test Device"); | 104 | m_name = QStringLiteral("Test Device"); | ||
105 | m_privateKey = QCA::KeyGenerator().createRSA(2048); | 105 | m_certificate = generateCertificate(m_deviceId); | ||
106 | m_certificate = generateCertificate(m_deviceId, m_privateKey); | | |||
107 | 106 | | |||
108 | m_identityPacket = QStringLiteral("{\"id\":1439365924847,\"type\":\"kdeconnect.identity\",\"body\":{\"deviceId\":\"testdevice\",\"deviceName\":\"Test Device\",\"protocolVersion\":6,\"deviceType\":\"phone\",\"tcpPort\":") + QString::number(TEST_PORT) + QStringLiteral("}}"); | 107 | m_identityPacket = QStringLiteral("{\"id\":1439365924847,\"type\":\"kdeconnect.identity\",\"body\":{\"deviceId\":\"testdevice\",\"deviceName\":\"Test Device\",\"protocolVersion\":6,\"deviceType\":\"phone\",\"tcpPort\":") + QString::number(TEST_PORT) + QStringLiteral("}}"); | ||
109 | } | 108 | } | ||
110 | 109 | | |||
111 | void LanLinkProviderTest::init() | 110 | void LanLinkProviderTest::init() | ||
112 | { | 111 | { | ||
113 | m_lanLinkProvider.onStart(); | 112 | m_lanLinkProvider.onStart(); | ||
114 | } | 113 | } | ||
▲ Show 20 Lines • Show All 195 Lines • ▼ Show 20 Line(s) | 292 | { | |||
310 | 309 | | |||
311 | QByteArray datagram; | 310 | QByteArray datagram; | ||
312 | datagram.resize(mUdpServer->pendingDatagramSize()); | 311 | datagram.resize(mUdpServer->pendingDatagramSize()); | ||
313 | QHostAddress sender; | 312 | QHostAddress sender; | ||
314 | 313 | | |||
315 | mUdpServer->readDatagram(datagram.data(), datagram.size(), &sender); | 314 | mUdpServer->readDatagram(datagram.data(), datagram.size(), &sender); | ||
316 | 315 | | |||
317 | testIdentityPacket(datagram); | 316 | testIdentityPacket(datagram); | ||
318 | 317 | | |||
pino: this can fail, needs error handling | |||||
319 | QJsonDocument jsonDocument = QJsonDocument::fromJson(datagram); | 318 | QJsonDocument jsonDocument = QJsonDocument::fromJson(datagram); | ||
pino: this can fail, needs error handling | |||||
320 | QJsonObject body = jsonDocument.object().value(QStringLiteral("body")).toObject(); | 319 | QJsonObject body = jsonDocument.object().value(QStringLiteral("body")).toObject(); | ||
321 | int tcpPort = body.value(QStringLiteral("tcpPort")).toInt(); | 320 | int tcpPort = body.value(QStringLiteral("tcpPort")).toInt(); | ||
pino: - missing handling of the return value of the command (what if fails?)
- please do not split… | |||||
322 | 321 | | |||
323 | QSslSocket socket; | 322 | QSslSocket socket; | ||
if QSslCertificate::fromPath fails, the return is an empty list, and this will misbehave pino: if QSslCertificate::fromPath fails, the return is an empty list, and this will misbehave | |||||
324 | QSignalSpy spy2(&socket, SIGNAL(connected())); | 323 | QSignalSpy spy2(&socket, SIGNAL(connected())); | ||
325 | 324 | | |||
326 | socket.connectToHost(sender, tcpPort); | 325 | socket.connectToHost(sender, tcpPort); | ||
327 | QVERIFY(spy2.wait()); | 326 | QVERIFY(spy2.wait()); | ||
328 | 327 | | |||
329 | QVERIFY2(socket.isOpen(), "Socket disconnected immediately"); | 328 | QVERIFY2(socket.isOpen(), "Socket disconnected immediately"); | ||
330 | 329 | | |||
331 | socket.write(m_identityPacket.toLatin1()); | 330 | socket.write(m_identityPacket.toLatin1()); | ||
▲ Show 20 Lines • Show All 68 Lines • ▼ Show 20 Line(s) | 395 | { | |||
400 | 399 | | |||
401 | QCOMPARE(jsonObject.value(QStringLiteral("type")).toString(), QStringLiteral("kdeconnect.identity")); | 400 | QCOMPARE(jsonObject.value(QStringLiteral("type")).toString(), QStringLiteral("kdeconnect.identity")); | ||
402 | QVERIFY2(body.contains(QStringLiteral("deviceName")), "Device name not found in identity packet"); | 401 | QVERIFY2(body.contains(QStringLiteral("deviceName")), "Device name not found in identity packet"); | ||
403 | QVERIFY2(body.contains(QStringLiteral("deviceId")), "Device id not found in identity packet"); | 402 | QVERIFY2(body.contains(QStringLiteral("deviceId")), "Device id not found in identity packet"); | ||
404 | QVERIFY2(body.contains(QStringLiteral("protocolVersion")), "Protocol version not found in identity packet"); | 403 | QVERIFY2(body.contains(QStringLiteral("protocolVersion")), "Protocol version not found in identity packet"); | ||
405 | QVERIFY2(body.contains(QStringLiteral("deviceType")), "Device type not found in identity packet"); | 404 | QVERIFY2(body.contains(QStringLiteral("deviceType")), "Device type not found in identity packet"); | ||
406 | } | 405 | } | ||
407 | 406 | | |||
408 | QSslCertificate LanLinkProviderTest::generateCertificate(QString& commonName, QCA::PrivateKey& privateKey) | 407 | QSslCertificate LanLinkProviderTest::generateCertificate(QString& commonName) | ||
409 | { | 408 | { | ||
410 | QDateTime startTime = QDateTime::currentDateTime(); | 409 | QTemporaryFile cert; | ||
411 | QDateTime endTime = startTime.addYears(10); | 410 | cert.open(); | ||
412 | QCA::CertificateInfo certificateInfo; | 411 | m_privateKeyFile.open(); | ||
413 | certificateInfo.insert(QCA::CommonName,commonName); | 412 | | ||
414 | certificateInfo.insert(QCA::Organization,QStringLiteral("KDE")); | 413 | QProcess::execute(QStringLiteral("openssl"), {QStringLiteral("req"), QStringLiteral("-new"), QStringLiteral("-x509"), QStringLiteral("-sha256"), QStringLiteral("-newkey"), | ||
415 | certificateInfo.insert(QCA::OrganizationalUnit,QStringLiteral("Kde connect")); | 414 | QStringLiteral("rsa:2048"), QStringLiteral("-nodes"), QStringLiteral("-keyout"), m_privateKeyFile.fileName(), QStringLiteral("-days"), | ||
416 | 415 | QStringLiteral("3650"), QStringLiteral("-out"), cert.fileName(), QStringLiteral("-subj"), QStringLiteral("/O=KDE/OU=KDEConnect/CN=%1").arg(commonName)}); | |||
417 | QCA::CertificateOptions certificateOptions(QCA::PKCS10); | | |||
418 | certificateOptions.setSerialNumber(10); | | |||
419 | certificateOptions.setInfo(certificateInfo); | | |||
420 | certificateOptions.setValidityPeriod(startTime, endTime); | | |||
421 | certificateOptions.setFormat(QCA::PKCS10); | | |||
422 | 416 | | |||
423 | QSslCertificate certificate = QSslCertificate(QCA::Certificate(certificateOptions, privateKey).toPEM().toLatin1()); | 417 | QSslCertificate certificate = QSslCertificate::fromPath(cert.fileName()).at(0); | ||
424 | return certificate; | 418 | return certificate; | ||
425 | } | 419 | } | ||
426 | 420 | | |||
427 | void LanLinkProviderTest::setSocketAttributes(QSslSocket* socket) | 421 | void LanLinkProviderTest::setSocketAttributes(QSslSocket* socket) | ||
428 | { | 422 | { | ||
429 | socket->setPrivateKey(QSslKey(m_privateKey.toPEM().toLatin1(), QSsl::Rsa)); | 423 | socket->setPrivateKey(m_privateKeyFile.fileName()); | ||
430 | socket->setLocalCertificate(m_certificate); | 424 | socket->setLocalCertificate(m_certificate); | ||
431 | } | 425 | } | ||
432 | 426 | | |||
433 | void LanLinkProviderTest::addTrustedDevice() | 427 | void LanLinkProviderTest::addTrustedDevice() | ||
434 | { | 428 | { | ||
435 | KdeConnectConfig* kcc = KdeConnectConfig::instance(); | 429 | KdeConnectConfig* kcc = KdeConnectConfig::instance(); | ||
436 | kcc->addTrustedDevice(m_deviceId, m_name, QStringLiteral("phone")); | 430 | kcc->addTrustedDevice(m_deviceId, m_name, QStringLiteral("phone")); | ||
437 | kcc->setDeviceProperty(m_deviceId, QStringLiteral("certificate"), QString::fromLatin1(m_certificate.toPem())); | 431 | kcc->setDeviceProperty(m_deviceId, QStringLiteral("certificate"), QString::fromLatin1(m_certificate.toPem())); | ||
Show All 21 Lines |
this can fail, needs error handling