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 | 34 | | |||
36 | /* | 35 | /* | ||
37 | * 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. | 36 | * 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 | * It depends on KdeConnectConfig since LanLinkProvider internally uses it. | 37 | * It depends on KdeConnectConfig since LanLinkProvider internally uses it. | ||
39 | */ | 38 | */ | ||
40 | class LanLinkProviderTest : public QObject | 39 | class LanLinkProviderTest : public QObject | ||
41 | { | 40 | { | ||
42 | Q_OBJECT | 41 | Q_OBJECT | ||
Show All 22 Lines | 60 | private: | |||
65 | Server* m_server; | 64 | Server* m_server; | ||
66 | SocketLineReader* m_reader; | 65 | SocketLineReader* m_reader; | ||
67 | QUdpSocket* m_udpSocket; | 66 | QUdpSocket* m_udpSocket; | ||
68 | QString m_identityPacket; | 67 | QString m_identityPacket; | ||
69 | 68 | | |||
70 | // Attributes for test device | 69 | // Attributes for test device | ||
71 | QString m_deviceId; | 70 | QString m_deviceId; | ||
72 | QString m_name; | 71 | QString m_name; | ||
73 | QCA::PrivateKey m_privateKey; | | |||
74 | QSslCertificate m_certificate; | 72 | QSslCertificate m_certificate; | ||
73 | QTemporaryFile m_privateKeyFile; | ||||
75 | 74 | | |||
76 | QSslCertificate generateCertificate(QString&, QCA::PrivateKey&); | 75 | QSslCertificate generateCertificate(QString&); | ||
77 | void addTrustedDevice(); | 76 | void addTrustedDevice(); | ||
78 | void removeTrustedDevice(); | 77 | void removeTrustedDevice(); | ||
79 | void setSocketAttributes(QSslSocket* socket); | 78 | void setSocketAttributes(QSslSocket* socket); | ||
80 | void testIdentityPacket(QByteArray& identityPacket); | 79 | void testIdentityPacket(QByteArray& identityPacket); | ||
81 | 80 | | |||
82 | }; | 81 | }; | ||
83 | 82 | | |||
84 | void LanLinkProviderTest::initTestCase() | 83 | void LanLinkProviderTest::initTestCase() | ||
85 | { | 84 | { | ||
86 | removeTrustedDevice(); // Remove trusted device if left by chance by any test | 85 | removeTrustedDevice(); // Remove trusted device if left by chance by any test | ||
87 | 86 | | |||
88 | m_deviceId = QStringLiteral("testdevice"); | 87 | m_deviceId = QStringLiteral("testdevice"); | ||
89 | m_name = QStringLiteral("Test Device"); | 88 | m_name = QStringLiteral("Test Device"); | ||
90 | m_privateKey = QCA::KeyGenerator().createRSA(2048); | 89 | m_certificate = generateCertificate(m_deviceId); | ||
91 | m_certificate = generateCertificate(m_deviceId, m_privateKey); | | |||
92 | 90 | | |||
93 | m_lanLinkProvider.onStart(); | 91 | m_lanLinkProvider.onStart(); | ||
94 | 92 | | |||
95 | m_identityPacket = QStringLiteral("{\"id\":1439365924847,\"type\":\"kdeconnect.identity\",\"body\":{\"deviceId\":\"testdevice\",\"deviceName\":\"Test Device\",\"protocolVersion\":6,\"deviceType\":\"phone\",\"tcpPort\":") + QString::number(TEST_PORT) + QStringLiteral("}}"); | 93 | m_identityPacket = QStringLiteral("{\"id\":1439365924847,\"type\":\"kdeconnect.identity\",\"body\":{\"deviceId\":\"testdevice\",\"deviceName\":\"Test Device\",\"protocolVersion\":6,\"deviceType\":\"phone\",\"tcpPort\":") + QString::number(TEST_PORT) + QStringLiteral("}}"); | ||
96 | } | 94 | } | ||
97 | 95 | | |||
98 | void LanLinkProviderTest::pairedDeviceTcpPacketReceived() | 96 | void LanLinkProviderTest::pairedDeviceTcpPacketReceived() | ||
99 | { | 97 | { | ||
▲ Show 20 Lines • Show All 208 Lines • ▼ Show 20 Line(s) | 302 | { | |||
308 | 306 | | |||
309 | QCOMPARE(jsonObject.value("type").toString(), QString("kdeconnect.identity")); | 307 | QCOMPARE(jsonObject.value("type").toString(), QString("kdeconnect.identity")); | ||
310 | QVERIFY2(body.contains("deviceName"), "Device name not found in identity packet"); | 308 | QVERIFY2(body.contains("deviceName"), "Device name not found in identity packet"); | ||
311 | QVERIFY2(body.contains("deviceId"), "Device id not found in identity packet"); | 309 | QVERIFY2(body.contains("deviceId"), "Device id not found in identity packet"); | ||
312 | QVERIFY2(body.contains("protocolVersion"), "Protocol version not found in identity packet"); | 310 | QVERIFY2(body.contains("protocolVersion"), "Protocol version not found in identity packet"); | ||
313 | QVERIFY2(body.contains("deviceType"), "Device type not found in identity packet"); | 311 | QVERIFY2(body.contains("deviceType"), "Device type not found in identity packet"); | ||
314 | } | 312 | } | ||
315 | 313 | | |||
316 | QSslCertificate LanLinkProviderTest::generateCertificate(QString& commonName, QCA::PrivateKey& privateKey) | 314 | QSslCertificate LanLinkProviderTest::generateCertificate(QString& commonName) | ||
317 | { | 315 | { | ||
318 | QDateTime startTime = QDateTime::currentDateTime(); | 316 | QTemporaryFile cert; | ||
319 | QDateTime endTime = startTime.addYears(10); | 317 | cert.open(); | ||
pino: this can fail, needs error handling | |||||
320 | QCA::CertificateInfo certificateInfo; | 318 | m_privateKeyFile.open(); | ||
pino: this can fail, needs error handling | |||||
321 | certificateInfo.insert(QCA::CommonName,commonName); | | |||
322 | certificateInfo.insert(QCA::Organization,QStringLiteral("KDE")); | | |||
323 | certificateInfo.insert(QCA::OrganizationalUnit,QStringLiteral("Kde connect")); | | |||
324 | | ||||
325 | QCA::CertificateOptions certificateOptions(QCA::PKCS10); | | |||
326 | certificateOptions.setSerialNumber(10); | | |||
327 | certificateOptions.setInfo(certificateInfo); | | |||
328 | certificateOptions.setValidityPeriod(startTime, endTime); | | |||
329 | certificateOptions.setFormat(QCA::PKCS10); | | |||
330 | 319 | | |||
331 | QSslCertificate certificate = QSslCertificate(QCA::Certificate(certificateOptions, privateKey).toPEM().toLatin1()); | 320 | QProcess::execute("openssl", {QStringLiteral("req"), QStringLiteral("-new"), QStringLiteral("-x509"), QStringLiteral("-sha256"), QStringLiteral("-newkey"), | ||
pino: - missing handling of the return value of the command (what if fails?)
- please do not split… | |||||
321 | QStringLiteral("rsa:2048"), QStringLiteral("-nodes"), QStringLiteral("-keyout"), m_privateKeyFile.fileName(), QStringLiteral("-days"), | ||||
322 | QStringLiteral("3650"), QStringLiteral("-out"), cert.fileName(), QStringLiteral("-subj"), QStringLiteral("/O=KDE/OU=KDEConnect/CN=%1").arg(commonName)}); | ||||
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 | |||||
323 | | ||||
324 | QSslCertificate certificate = QSslCertificate::fromPath(cert.fileName()).at(0); | ||||
332 | return certificate; | 325 | return certificate; | ||
333 | } | 326 | } | ||
334 | 327 | | |||
335 | void LanLinkProviderTest::setSocketAttributes(QSslSocket* socket) | 328 | void LanLinkProviderTest::setSocketAttributes(QSslSocket* socket) | ||
336 | { | 329 | { | ||
337 | socket->setPrivateKey(QSslKey(m_privateKey.toPEM().toLatin1(), QSsl::Rsa)); | 330 | socket->setPrivateKey(m_privateKeyFile.fileName()); | ||
338 | socket->setLocalCertificate(m_certificate); | 331 | socket->setLocalCertificate(m_certificate); | ||
339 | } | 332 | } | ||
340 | 333 | | |||
341 | void LanLinkProviderTest::addTrustedDevice() | 334 | void LanLinkProviderTest::addTrustedDevice() | ||
342 | { | 335 | { | ||
343 | KdeConnectConfig* kcc = KdeConnectConfig::instance(); | 336 | KdeConnectConfig* kcc = KdeConnectConfig::instance(); | ||
344 | kcc->addTrustedDevice(m_deviceId, m_name, QStringLiteral("phone")); | 337 | kcc->addTrustedDevice(m_deviceId, m_name, QStringLiteral("phone")); | ||
345 | kcc->setDeviceProperty(m_deviceId, QStringLiteral("certificate"), QString::fromLatin1(m_certificate.toPem())); | 338 | kcc->setDeviceProperty(m_deviceId, QStringLiteral("certificate"), QString::fromLatin1(m_certificate.toPem())); | ||
Show All 12 Lines |
this can fail, needs error handling