Changeset View
Changeset View
Standalone View
Standalone View
tests/testsslsocketlinereader.cpp
Show All 16 Lines | |||||
17 | * You should have received a copy of the GNU General Public License | 17 | * You should have received a copy of the GNU General Public License | ||
18 | * along with this program. If not, see <https://www.gnu.org/licenses/>. | 18 | * along with this program. If not, see <https://www.gnu.org/licenses/>. | ||
19 | */ | 19 | */ | ||
20 | 20 | | |||
21 | #include "../core/backends/lan/server.h" | 21 | #include "../core/backends/lan/server.h" | ||
22 | #include "../core/backends/lan/socketlinereader.h" | 22 | #include "../core/backends/lan/socketlinereader.h" | ||
23 | 23 | | |||
24 | #include <QSslKey> | 24 | #include <QSslKey> | ||
25 | #include <QtCrypto> | | |||
26 | #include <QTest> | 25 | #include <QTest> | ||
27 | #include <QTimer> | 26 | #include <QTimer> | ||
27 | #include <QTemporaryFile> | ||||
28 | #include <QProcess> | ||||
28 | 29 | | |||
29 | /* | 30 | /* | ||
30 | * This class tests the behaviour of socket line reader when the connection if over ssl. Since SSL takes part below application layer, | 31 | * This class tests the behaviour of socket line reader when the connection if over ssl. Since SSL takes part below application layer, | ||
31 | * working of SocketLineReader should be same. | 32 | * working of SocketLineReader should be same. | ||
32 | */ | 33 | */ | ||
33 | class TestSslSocketLineReader : public QObject | 34 | class TestSslSocketLineReader : public QObject | ||
34 | { | 35 | { | ||
35 | Q_OBJECT | 36 | Q_OBJECT | ||
Show All 10 Lines | 40 | private Q_SLOTS: | |||
46 | void testTrustedDevice(); | 47 | void testTrustedDevice(); | ||
47 | void testUntrustedDevice(); | 48 | void testUntrustedDevice(); | ||
48 | void testTrustedDeviceWithWrongCertificate(); | 49 | void testTrustedDeviceWithWrongCertificate(); | ||
49 | 50 | | |||
50 | 51 | | |||
51 | private: | 52 | private: | ||
52 | const int PORT = 7894; | 53 | const int PORT = 7894; | ||
53 | QTimer m_timer; | 54 | QTimer m_timer; | ||
54 | QCA::Initializer m_qcaInitializer; | | |||
55 | QEventLoop m_loop; | 55 | QEventLoop m_loop; | ||
56 | QList<QByteArray> m_packets; | 56 | QList<QByteArray> m_packets; | ||
57 | Server* m_server; | 57 | Server* m_server; | ||
58 | QSslSocket* m_clientSocket; | 58 | QSslSocket* m_clientSocket; | ||
59 | SocketLineReader* m_reader; | 59 | SocketLineReader* m_reader; | ||
60 | 60 | | |||
61 | private: | 61 | private: | ||
62 | void setSocketAttributes(QSslSocket* socket, QString deviceName); | 62 | void setSocketAttributes(QSslSocket* socket, QString deviceName); | ||
▲ Show 20 Lines • Show All 115 Lines • ▼ Show 20 Line(s) | 172 | { | |||
178 | QCOMPARE(true, m_server->hasPendingConnections()); | 178 | QCOMPARE(true, m_server->hasPendingConnections()); | ||
179 | 179 | | |||
180 | QSslSocket* serverSocket = m_server->nextPendingConnection(); | 180 | QSslSocket* serverSocket = m_server->nextPendingConnection(); | ||
181 | 181 | | |||
182 | QVERIFY2(serverSocket != 0, "Null socket returned by server"); | 182 | QVERIFY2(serverSocket != 0, "Null socket returned by server"); | ||
183 | QVERIFY2(serverSocket->isOpen(), "Server socket already closed"); | 183 | QVERIFY2(serverSocket->isOpen(), "Server socket already closed"); | ||
184 | 184 | | |||
185 | setSocketAttributes(serverSocket, QStringLiteral("Test Server")); | 185 | setSocketAttributes(serverSocket, QStringLiteral("Test Server")); | ||
186 | setSocketAttributes(m_clientSocket, QStringLiteral("Test Client")); | 186 | setSocketAttributes(m_clientSocket, QStringLiteral("Test Client")); | ||
nicolasfella: ???? | |||||
187 | 187 | | |||
188 | serverSocket->setPeerVerifyName(QStringLiteral("Test Client")); | 188 | serverSocket->setPeerVerifyName(QStringLiteral("Test Client")); | ||
189 | serverSocket->setPeerVerifyMode(QSslSocket::QueryPeer); | 189 | serverSocket->setPeerVerifyMode(QSslSocket::QueryPeer); | ||
190 | 190 | | |||
191 | m_clientSocket->setPeerVerifyName(QStringLiteral("Test Server")); | 191 | m_clientSocket->setPeerVerifyName(QStringLiteral("Test Server")); | ||
192 | m_clientSocket->setPeerVerifyMode(QSslSocket::QueryPeer); | 192 | m_clientSocket->setPeerVerifyMode(QSslSocket::QueryPeer); | ||
193 | 193 | | |||
194 | int connected_sockets = 0; | 194 | int connected_sockets = 0; | ||
▲ Show 20 Lines • Show All 84 Lines • ▼ Show 20 Line(s) | |||||
279 | } | 279 | } | ||
280 | 280 | | |||
281 | void TestSslSocketLineReader::newPacket() | 281 | void TestSslSocketLineReader::newPacket() | ||
282 | { | 282 | { | ||
283 | if (!m_reader->bytesAvailable()) { | 283 | if (!m_reader->bytesAvailable()) { | ||
284 | return; | 284 | return; | ||
285 | } | 285 | } | ||
286 | 286 | | |||
287 | int maxLoops = 5; | 287 | int maxLoops = 5; | ||
pino: this can fail, needs error handling | |||||
288 | while(m_reader->bytesAvailable() > 0 && maxLoops > 0) { | 288 | while(m_reader->bytesAvailable() > 0 && maxLoops > 0) { | ||
289 | --maxLoops; | 289 | --maxLoops; | ||
pino: this can fail, needs error handling | |||||
290 | const QByteArray packet = m_reader->readLine(); | 290 | const QByteArray packet = m_reader->readLine(); | ||
291 | if (!packet.isEmpty()) { | 291 | if (!packet.isEmpty()) { | ||
pino: - missing handling of the return value of the command (what if fails?)
- please do not split… | |||||
292 | m_packets.append(packet); | 292 | m_packets.append(packet); | ||
293 | } | 293 | } | ||
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 | |||||
294 | 294 | | |||
295 | if (m_packets.count() == 5) { | 295 | if (m_packets.count() == 5) { | ||
296 | m_loop.exit(); | 296 | m_loop.exit(); | ||
297 | } | 297 | } | ||
298 | } | 298 | } | ||
299 | } | 299 | } | ||
300 | 300 | | |||
301 | void TestSslSocketLineReader::setSocketAttributes(QSslSocket* socket, QString deviceName) { | 301 | void TestSslSocketLineReader::setSocketAttributes(QSslSocket* socket, QString deviceName) { | ||
302 | 302 | | |||
303 | QDateTime startTime = QDateTime::currentDateTime(); | 303 | QTemporaryFile cert; | ||
304 | QDateTime endTime = startTime.addYears(10); | 304 | cert.open(); | ||
305 | QCA::CertificateInfo certificateInfo; | 305 | QTemporaryFile priv; | ||
306 | certificateInfo.insert(QCA::CommonName,deviceName); | 306 | priv.open(); | ||
307 | certificateInfo.insert(QCA::Organization,QStringLiteral("KDE")); | 307 | | ||
308 | certificateInfo.insert(QCA::OrganizationalUnit,QStringLiteral("Kde connect")); | 308 | QProcess::execute("openssl", {QStringLiteral("req"), QStringLiteral("-new"), QStringLiteral("-x509"), QStringLiteral("-sha256"), QStringLiteral("-newkey"), | ||
309 | 309 | QStringLiteral("rsa:2048"), QStringLiteral("-nodes"), QStringLiteral("-keyout"), priv.fileName(), QStringLiteral("-days"), | |||
310 | QCA::CertificateOptions certificateOptions(QCA::PKCS10); | 310 | QStringLiteral("3650"), QStringLiteral("-out"), cert.fileName(), QStringLiteral("-subj"), QStringLiteral("/O=KDE/OU=KDEConnect/CN=%1").arg(deviceName)}); | ||
311 | certificateOptions.setSerialNumber(10); | 311 | QSslCertificate certificate = QSslCertificate::fromPath(cert.fileName()).at(0); | ||
nicolasfella: break into a couple of lines. | |||||
312 | certificateOptions.setInfo(certificateInfo); | | |||
313 | certificateOptions.setValidityPeriod(startTime, endTime); | | |||
314 | certificateOptions.setFormat(QCA::PKCS10); | | |||
315 | 312 | | |||
316 | QCA::PrivateKey privKey = QCA::KeyGenerator().createRSA(2048); | 313 | socket->setPrivateKey(priv.fileName()); | ||
317 | QSslCertificate certificate = QSslCertificate(QCA::Certificate(certificateOptions, privKey).toPEM().toLatin1()); | | |||
318 | | ||||
319 | socket->setPrivateKey(QSslKey(privKey.toPEM().toLatin1(), QSsl::Rsa)); | | |||
320 | socket->setLocalCertificate(certificate); | 314 | socket->setLocalCertificate(certificate); | ||
321 | 315 | | |||
322 | } | 316 | } | ||
323 | 317 | | |||
324 | QTEST_GUILESS_MAIN(TestSslSocketLineReader) | 318 | QTEST_GUILESS_MAIN(TestSslSocketLineReader) | ||
325 | 319 | | |||
326 | #include "testsslsocketlinereader.moc" | 320 | #include "testsslsocketlinereader.moc" | ||
327 | 321 | |
????