Add Qt6 support

This commit is contained in:
Simon Stürz 2023-11-22 22:16:32 +01:00
parent 462007c3d7
commit df77adc96d
12 changed files with 81 additions and 40 deletions

View File

@ -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 \

View File

@ -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;

View File

@ -69,7 +69,8 @@
#include <QDataStream>
#include <QUuid>
#include <QtGlobal>
#include <QRegExp>
#include <QRegularExpression>
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);

View File

@ -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<QSslError> &);
connect(m_socket, static_cast<sslErrorsSignal>(&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<errorSignal>(&QSslSocket::error), this, &MqttClientTransport::errorSignal);
connect(m_socket, &QTcpSocket::readyRead, this, &MqttTcpClientTransport::onReadyRead);
#endif
}
void MqttTcpClientTransport::connectToHost()

View File

@ -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<errorSignal>(&QWebSocket::error), this, &MqttClientTransport::errorSignal);
#endif
connect(m_socket, &QWebSocket::textMessageReceived, this, &MqttWebSocketClientTransport::onTextMessageReceived);
connect(m_socket, &QWebSocket::binaryMessageReceived, this, &MqttWebSocketClientTransport::onBinaryMessageReceived);

View File

@ -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)

View File

@ -9,5 +9,3 @@ client.depends = libnymea-mqtt
} else {
message("Build without tests")
}

View File

@ -34,6 +34,7 @@
#include <QDebug>
#include <QSaveFile>
#include <QUuid>
#include <QRegularExpression>
// 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);

View File

@ -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

View File

@ -61,7 +61,7 @@ QPair<MqttClient*, QSignalSpy*> MqttTests::connectToServer(const QString &client
connectClientToServer(client, cleanSession);
return qMakePair<MqttClient*, QSignalSpy*>(client, spy);
return QPair<MqttClient*, QSignalSpy*>(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<Mqtt::SubscribeReturnCodes>().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");

View File

@ -27,7 +27,6 @@
#include "mqttserver.h"
#include "mqttclient.h"
#include "mqttclient_p.h"
#include <QTest>
#include <QSignalSpy>

View File

@ -27,7 +27,6 @@
#include "mqttserver.h"
#include "mqttclient.h"
#include "mqttclient_p.h"
#include <QTest>
#include <QSignalSpy>