diff --git a/libnymea-mqtt/libnymea-mqtt.pri b/libnymea-mqtt/libnymea-mqtt.pri index ff262b8..340fe94 100644 --- a/libnymea-mqtt/libnymea-mqtt.pri +++ b/libnymea-mqtt/libnymea-mqtt.pri @@ -3,10 +3,13 @@ TEMPLATE = lib TARGET = nymea-mqtt + QT -= gui QT += network websockets -CONFIG += c++11 console static +include(../nymea-mqtt.pri) + +CONFIG += console static SOURCES += \ mqttpacket.cpp \ diff --git a/libnymea-mqtt/mqttpacket.cpp b/libnymea-mqtt/mqttpacket.cpp index 51d0fb1..33ac966 100644 --- a/libnymea-mqtt/mqttpacket.cpp +++ b/libnymea-mqtt/mqttpacket.cpp @@ -629,7 +629,11 @@ int MqttPacket::parse(const QByteArray &buffer) QByteArray MqttPacket::serialize() const { QByteArray ret; +#if QT_VERSION >= QT_VERSION_CHECK(6, 0, 0) + QDataStream stream(&ret, QDataStream::WriteOnly); +#else QDataStream stream(&ret, QIODevice::WriteOnly); +#endif stream << d_ptr->header; quint16 remainingLength = 0; diff --git a/libnymea-mqtt/mqttserver.cpp b/libnymea-mqtt/mqttserver.cpp index afd9498..fb42189 100644 --- a/libnymea-mqtt/mqttserver.cpp +++ b/libnymea-mqtt/mqttserver.cpp @@ -69,7 +69,8 @@ #include #include #include -#include +#include + Q_LOGGING_CATEGORY(dbgServer, "nymea.mqtt.server") @@ -349,7 +350,12 @@ void MqttServerPrivate::processPacket(const MqttPacket &packet, MqttServerClient cleanupClient(client); return; } +#if QT_VERSION >= QT_VERSION_CHECK(6, 0, 0) + clientId = QUuid::createUuid().toString().remove(QRegularExpression("[{}-]*")); +#else clientId = QUuid::createUuid().toString().remove(QRegExp("[{}-]*")); +#endif + } if (authorizer) { @@ -442,7 +448,12 @@ void MqttServerPrivate::processPacket(const MqttPacket &packet, MqttServerClient << ", Will Message: \"" << packet.willMessage() << '\"' << ", Will Retain: " << packet.willRetain() << ", Username: " << packet.username() - << ", Password: " << QString(packet.password()).replace(QRegExp("."), "*"); +#if QT_VERSION >= QT_VERSION_CHECK(6, 0, 0) + << ", Password: " << QString(packet.password()).replace(QRegularExpression("."), "*"); +#else + << ", Password: " << QString(packet.password()).replace(QRegExp("."), "*"); +#endif + if (ctx->keepAlive > 0) { ctx->keepAliveTimer.start(ctx->keepAlive * 1500); diff --git a/libnymea-mqtt/transports/mqtttcpclienttransport.cpp b/libnymea-mqtt/transports/mqtttcpclienttransport.cpp index 7f8d9af..d839548 100644 --- a/libnymea-mqtt/transports/mqtttcpclienttransport.cpp +++ b/libnymea-mqtt/transports/mqtttcpclienttransport.cpp @@ -39,12 +39,21 @@ MqttTcpClientTransport::MqttTcpClientTransport(const QString &hostName, quint16 connect(m_socket, &QTcpSocket::connected, this, &MqttClientTransport::connected); connect(m_socket, &QTcpSocket::disconnected, this, &MqttClientTransport::disconnected); connect(m_socket, &QTcpSocket::stateChanged, this, &MqttClientTransport::stateChanged); + connect(m_socket, &QTcpSocket::readyRead, this, &MqttTcpClientTransport::onReadyRead); + +#if QT_VERSION >= QT_VERSION_CHECK(6, 0, 0) + connect(m_socket, &QSslSocket::sslErrors, this, &MqttClientTransport::sslErrors); +#else typedef void (QSslSocket:: *sslErrorsSignal)(const QList &); connect(m_socket, static_cast(&QSslSocket::sslErrors), this, &MqttClientTransport::sslErrors); +#endif + +#if QT_VERSION >= QT_VERSION_CHECK(5, 15, 0) + connect(m_socket, &QTcpSocket::errorOccurred, this, &MqttClientTransport::errorSignal); +#else typedef void (QSslSocket:: *errorSignal)(QAbstractSocket::SocketError); connect(m_socket, static_cast(&QSslSocket::error), this, &MqttClientTransport::errorSignal); - - connect(m_socket, &QTcpSocket::readyRead, this, &MqttTcpClientTransport::onReadyRead); +#endif } void MqttTcpClientTransport::connectToHost() diff --git a/libnymea-mqtt/transports/mqttwebsocketclienttransport.cpp b/libnymea-mqtt/transports/mqttwebsocketclienttransport.cpp index 1486aab..cf2c1ce 100644 --- a/libnymea-mqtt/transports/mqttwebsocketclienttransport.cpp +++ b/libnymea-mqtt/transports/mqttwebsocketclienttransport.cpp @@ -37,8 +37,13 @@ MqttWebSocketClientTransport::MqttWebSocketClientTransport(const QNetworkRequest connect(m_socket, &QWebSocket::disconnected, this, &MqttClientTransport::disconnected); connect(m_socket, &QWebSocket::stateChanged, this, &MqttClientTransport::stateChanged); connect(m_socket, &QWebSocket::sslErrors, this, &MqttClientTransport::sslErrors); + +#if QT_VERSION >= QT_VERSION_CHECK(6, 0, 0) + connect(m_socket, &QWebSocket::errorOccurred, this, &MqttClientTransport::errorSignal); +#else typedef void (QWebSocket:: *errorSignal)(QAbstractSocket::SocketError); connect(m_socket, static_cast(&QWebSocket::error), this, &MqttClientTransport::errorSignal); +#endif connect(m_socket, &QWebSocket::textMessageReceived, this, &MqttWebSocketClientTransport::onTextMessageReceived); connect(m_socket, &QWebSocket::binaryMessageReceived, this, &MqttWebSocketClientTransport::onBinaryMessageReceived); diff --git a/nymea-mqtt.pri b/nymea-mqtt.pri index e83601a..8428774 100644 --- a/nymea-mqtt.pri +++ b/nymea-mqtt.pri @@ -1,5 +1,17 @@ -QMAKE_CXXFLAGS *= -Werror -std=c++11 -g -QMAKE_LFLAGS *= -std=c++11 +QMAKE_CXXFLAGS *= -Werror -g + +greaterThan(QT_MAJOR_VERSION, 5) { + message("Building with Qt6 support") + CONFIG *= c++17 + QMAKE_LFLAGS *= -std=c++17 + QMAKE_CXXFLAGS *= -std=c++17 +} else { + message("Building with Qt5 support") + CONFIG *= c++11 + QMAKE_LFLAGS *= -std=c++11 + QMAKE_CXXFLAGS *= -std=c++11 + DEFINES += QT_DISABLE_DEPRECATED_UP_TO=0x050F00 +} top_srcdir=$$PWD top_builddir=$$shadowed($$PWD) diff --git a/nymea-mqtt.pro b/nymea-mqtt.pro index b861247..4b9d70f 100644 --- a/nymea-mqtt.pro +++ b/nymea-mqtt.pro @@ -9,5 +9,3 @@ client.depends = libnymea-mqtt } else { message("Build without tests") } - - diff --git a/server/certificateloader.cpp b/server/certificateloader.cpp index 5720962..72c6e29 100644 --- a/server/certificateloader.cpp +++ b/server/certificateloader.cpp @@ -34,6 +34,7 @@ #include #include #include +#include // Disabling deprecation warnings for openssl 3.0 #pragma GCC diagnostic push @@ -132,8 +133,12 @@ bool CertificateLoader::generateCertificate(const QString &certificateKeyFileNam q_check_ptr(x509); // Randomize serial number in case a previous one is stuck in a browser (Chromium // completely rejects reused serial numbers and doesn't even allow to bypass it by an exception) +#if QT_VERSION >= QT_VERSION_CHECK(6, 0, 0) + std::srand(QUuid::createUuid().toString().remove(QRegularExpression("[a-zA-Z{}-]")).left(5).toInt()); +#else qsrand(QUuid::createUuid().toString().remove(QRegExp("[a-zA-Z{}-]")).left(5).toInt()); - ASN1_INTEGER_set(X509_get_serialNumber(x509), qrand()); +#endif + ASN1_INTEGER_set(X509_get_serialNumber(x509), std::rand()); X509_gmtime_adj(X509_get_notBefore(x509), 0); // not before current time X509_gmtime_adj(X509_get_notAfter(x509), 31536000L*10); // not after 10 years from this point X509_set_pubkey(x509, pkey); diff --git a/tests/common/common.pri b/tests/common/common.pri index 09eb5a5..688b1f5 100644 --- a/tests/common/common.pri +++ b/tests/common/common.pri @@ -15,7 +15,3 @@ HEADERS += $${top_srcdir}/tests/common/mqtttests.h SOURCES += $${top_srcdir}/tests/common/mqtttests.cpp LIBS += -L$$top_builddir/libnymea-mqtt/ -lnymea-mqtt - -target.path = $$[QT_INSTALL_PREFIX]/bin -INSTALLS += target - diff --git a/tests/common/mqtttests.cpp b/tests/common/mqtttests.cpp index 9ecd44d..7b21865 100644 --- a/tests/common/mqtttests.cpp +++ b/tests/common/mqtttests.cpp @@ -61,7 +61,7 @@ QPair MqttTests::connectToServer(const QString &client connectClientToServer(client, cleanSession); - return qMakePair(client, spy); + return QPair(client, spy); } void MqttTests::disconnectAndWait(MqttClient* client) @@ -197,12 +197,12 @@ void MqttTests::subscribeAndPublish() quint16 packetId = client1->subscribe("#", qosClient1); QTRY_VERIFY2(serverSubscribeSpy.count() == 1, "Server did not emit clientSubscribed"); - QVERIFY2(serverSubscribeSpy.first().first() == clientId1, "Client Id not matching"); - QVERIFY2(serverSubscribeSpy.first().at(1) == "#", "Topic not matching"); - QVERIFY2(serverSubscribeSpy.first().at(2) == qosClient1, "QoS not matching"); + QVERIFY2(serverSubscribeSpy.first().first().toString() == clientId1, "Client Id not matching"); + QVERIFY2(serverSubscribeSpy.first().at(1).toString() == "#", "Topic not matching"); + QVERIFY2(serverSubscribeSpy.first().at(2).toInt() == qosClient1, "QoS not matching"); QTRY_VERIFY2(clientSubscribeSpy.count() == 1, "Client did not emit subscribed"); - QVERIFY2(clientSubscribeSpy.first().first() == packetId, "Packet ID not matching"); + QVERIFY2(clientSubscribeSpy.first().first().toInt() == packetId, "Packet ID not matching"); QVERIFY2(clientSubscribeSpy.first().at(1).value().count() == 1, "Subscribe return code count not matching"); QSignalSpy serverPublishReceivedSpy(m_server, &MqttServer::publishReceived); @@ -213,20 +213,20 @@ void MqttTests::subscribeAndPublish() packetId = client2->publish("/testtopic/", "Hello world", qosClient2); QTRY_VERIFY2(serverPublishReceivedSpy.count() == 1, "Server did not emit publishReceived"); - QVERIFY2(serverPublishReceivedSpy.first().at(0) == clientId2, "Server did emit publishReceived signal but client ID is not matching"); - QVERIFY2(serverPublishReceivedSpy.first().at(1) == packetId, QString("Server did emit publishReceived signal but Packet ID is not matching:\nActual: %1\nExpected: %2").arg(serverPublishReceivedSpy.first().at(1).toInt()).arg(packetId).toUtf8().data()); - QVERIFY2(serverPublishReceivedSpy.first().at(2) == "/testtopic/", "Server did emit publishReceived signal but topic is not matching"); - QVERIFY2(serverPublishReceivedSpy.first().at(3) == "Hello world", "Server did emit publishReceived signal but payload is not matching"); + QVERIFY2(serverPublishReceivedSpy.first().at(0).toString() == clientId2, "Server did emit publishReceived signal but client ID is not matching"); + QVERIFY2(serverPublishReceivedSpy.first().at(1).toInt() == packetId, QString("Server did emit publishReceived signal but Packet ID is not matching:\nActual: %1\nExpected: %2").arg(serverPublishReceivedSpy.first().at(1).toInt()).arg(packetId).toUtf8().data()); + QVERIFY2(serverPublishReceivedSpy.first().at(2).toString() == "/testtopic/", "Server did emit publishReceived signal but topic is not matching"); + QVERIFY2(serverPublishReceivedSpy.first().at(3).toByteArray() == QByteArray("Hello world"), "Server did emit publishReceived signal but payload is not matching"); QTRY_VERIFY2(serverPublishedSpy.count() == 1, "Server did not emit published"); - QVERIFY2(serverPublishedSpy.first().at(0) == clientId1, "Server did emit published signal but client ID is not matching"); + QVERIFY2(serverPublishedSpy.first().at(0).toString() == clientId1, "Server did emit published signal but client ID is not matching"); QTRY_VERIFY2(client1PublishReceivedSpy.count() == 1, "Subscribing client did not emit publishReceived signal"); - QVERIFY2(client1PublishReceivedSpy.first().at(0) == "/testtopic/", "Subscribing client did emit publishReceived signal but topic is not matching"); - QVERIFY2(client1PublishReceivedSpy.first().at(1) == "Hello world", "Subscribing client did emit publishReceived signal but payload is not matching"); + QVERIFY2(client1PublishReceivedSpy.first().at(0).toString() == "/testtopic/", "Subscribing client did emit publishReceived signal but topic is not matching"); + QVERIFY2(client1PublishReceivedSpy.first().at(1).toByteArray() == QByteArray("Hello world"), "Subscribing client did emit publishReceived signal but payload is not matching"); QTRY_VERIFY2(client2PublishedSpy.count() == 1, "Publishing client did not emit published signal"); - QVERIFY2(client2PublishedSpy.first().first() == packetId, "Publishing client did emit published signal but packet ID not matching"); + QVERIFY2(client2PublishedSpy.first().first().toInt() == packetId, "Publishing client did emit published signal but packet ID not matching"); } @@ -242,8 +242,8 @@ void MqttTests::willIsSentOnClientDisappearing() client2->d_ptr->transport->abort(); QTRY_VERIFY2(publishSpy.count() == 1, "Will has not been sent"); - QVERIFY2(publishSpy.first().at(0) == "/testtopic", "Will topic not matching"); - QVERIFY2(publishSpy.first().at(1) == "Bye bye", "Will message not matching"); + QVERIFY2(publishSpy.first().at(0).toString() == "/testtopic", "Will topic not matching"); + QVERIFY2(publishSpy.first().at(1).toByteArray() == QByteArray("Bye bye"), "Will message not matching"); } void MqttTests::willIsNotSentOnClientDisconnecting() @@ -278,18 +278,18 @@ void MqttTests::testWillRetain() client2->d_ptr->transport->abort(); QTRY_VERIFY2(publishSpy.count() == 1, "Will has not been sent"); - QVERIFY2(publishSpy.first().at(0) == "/testtopic", QString("Will topic not matching: %1").arg(publishSpy.first().at(0).toString()).toUtf8().data()); - QVERIFY2(publishSpy.first().at(1) == "Bye bye", "Will message not matching"); - QVERIFY2(publishSpy.first().at(2) == false, "Retain flag not matching"); + QVERIFY2(publishSpy.first().at(0).toString() == "/testtopic", QString("Will topic not matching: %1").arg(publishSpy.first().at(0).toString()).toUtf8().data()); + QVERIFY2(publishSpy.first().at(1).toByteArray() == QByteArray("Bye bye"), "Will message not matching"); + QVERIFY2(publishSpy.first().at(2).toBool() == false, "Retain flag not matching"); MqttClient *client3 = connectAndWait("subWill-client2"); QSignalSpy retainedWillSpy(client3, &MqttClient::publishReceived); client3->subscribe("#"); QTRY_VERIFY2(retainedWillSpy.count() == 1, "Retained Will has not been sent"); - QVERIFY2(retainedWillSpy.first().at(0) == "/testtopic", "Will topic not matching"); - QVERIFY2(retainedWillSpy.first().at(1) == "Bye bye", "Will message not matching"); - QVERIFY2(retainedWillSpy.first().at(2) == true, "Retain flag not matching"); + QVERIFY2(retainedWillSpy.first().at(0).toString() == "/testtopic", "Will topic not matching"); + QVERIFY2(retainedWillSpy.first().at(1).toByteArray() == QByteArray("Bye bye"), "Will message not matching"); + QVERIFY2(retainedWillSpy.first().at(2).toBool() == true, "Retain flag not matching"); // Clear retain on /testtopic QSignalSpy clearRetainSpy(client3, &MqttClient::published); @@ -331,12 +331,12 @@ void MqttTests::testQoS1Retransmissions() QCOMPARE(serverSpy.at(0).at(0).toString(), QString("client1")); QCOMPARE(serverSpy.at(0).at(1).toInt(), packetId); QCOMPARE(serverSpy.at(0).at(2).toString(), QString("/testtopic")); - QCOMPARE(serverSpy.at(0).at(3).toString(), QString("Hello world")); + QCOMPARE(serverSpy.at(0).at(3).toByteArray(), QByteArray("Hello world")); QCOMPARE(serverSpy.at(1).at(0).toString(), QString("client1")); QCOMPARE(serverSpy.at(1).at(1).toInt(), packetId); QCOMPARE(serverSpy.at(1).at(2).toString(), QString("/testtopic")); - QCOMPARE(serverSpy.at(1).at(3).toString(), QString("Hello world")); + QCOMPARE(serverSpy.at(1).at(3).toByteArray(), QByteArray("Hello world")); } void MqttTests::testMultiSubscription() @@ -588,7 +588,7 @@ void MqttTests::testQoS1PublishToClientIsDeliveredOnSessionResume() QTRY_VERIFY(publishedSpy.count() == 1); // Resume (take over) old session and make sure we got the publish - MqttClient *newClient1 = connectToServer("client1", false).first;; + MqttClient *newClient1 = connectToServer("client1", false).first; QSignalSpy publishReceivedSpy(newClient1, &MqttClient::publishReceived); QTRY_VERIFY2(publishReceivedSpy.count() == 1, "Client did not receive publish packet upon session resume"); diff --git a/tests/tcp/test_tcp.cpp b/tests/tcp/test_tcp.cpp index 9e11dda..d477ee7 100644 --- a/tests/tcp/test_tcp.cpp +++ b/tests/tcp/test_tcp.cpp @@ -27,7 +27,6 @@ #include "mqttserver.h" #include "mqttclient.h" -#include "mqttclient_p.h" #include #include diff --git a/tests/websocket/test_websocket.cpp b/tests/websocket/test_websocket.cpp index 4eba487..fdde46e 100644 --- a/tests/websocket/test_websocket.cpp +++ b/tests/websocket/test_websocket.cpp @@ -27,7 +27,6 @@ #include "mqttserver.h" #include "mqttclient.h" -#include "mqttclient_p.h" #include #include