Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Bug fix: empty topic string passed to (un)subscribed signals #100

Merged
merged 1 commit into from
Jul 4, 2017
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
26 changes: 23 additions & 3 deletions src/mqtt/qmqtt_client_p.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -336,19 +336,22 @@ void QMQTT::ClientPrivate::puback(const quint8 type, const quint16 msgid)
quint16 QMQTT::ClientPrivate::subscribe(const QString& topic, const quint8 qos)
{
quint16 msgid = sendSubscribe(topic, qos);
_midToTopic[msgid] = topic;
return msgid;
}

void QMQTT::ClientPrivate::unsubscribe(const QString& topic)
{
sendUnsubscribe(topic);
quint16 msgid = sendUnsubscribe(topic);
_midToTopic[msgid] = topic;
}

void QMQTT::ClientPrivate::onNetworkDisconnected()
{
Q_Q(Client);

stopKeepAlive();
clearMidToTopic();
emit q->disconnected();
}

Expand Down Expand Up @@ -395,10 +398,11 @@ void QMQTT::ClientPrivate::onNetworkReceived(const QMQTT::Frame& frm)
case SUBACK:
mid = frame.readInt();
qos = frame.readChar();
handleSuback(topic, qos);
handleSuback(midToTopic(mid), qos);
break;
case UNSUBACK:
handleUnsuback(topic);
mid = frame.readInt();
handleUnsuback(midToTopic(mid));
break;
case PINGRESP:

Expand Down Expand Up @@ -468,6 +472,22 @@ void QMQTT::ClientPrivate::handleUnsuback(const QString &topic) {
emit q->unsubscribed(topic);
}

QString QMQTT::ClientPrivate::midToTopic(const quint16 mid)
{
QString result;
QHash<quint16, QString>::Iterator it = _midToTopic.find(mid);
if (it != _midToTopic.end()) {
result = it.value();
_midToTopic.erase(it);
}
return result;
}

void QMQTT::ClientPrivate::clearMidToTopic()
{
_midToTopic.clear();
}

bool QMQTT::ClientPrivate::autoReconnect() const
{
return _network->autoReconnect();
Expand Down
3 changes: 3 additions & 0 deletions src/mqtt/qmqtt_client_p.h
Original file line number Diff line number Diff line change
Expand Up @@ -74,6 +74,7 @@ class ClientPrivate
bool _willRetain;
QString _willMessage;
QHash<QAbstractSocket::SocketError, ClientError> _socketErrorHash;
QHash<quint16, QString> _midToTopic;

Client* const q_ptr;

Expand Down Expand Up @@ -103,6 +104,8 @@ class ClientPrivate
void handleUnsuback(const QString& topic);
void handlePubcomp(const Message& message);
void handlePingresp();
QString midToTopic(const quint16 mid);
void clearMidToTopic();
bool autoReconnect() const;
void setAutoReconnect(const bool autoReconnect);
bool autoReconnectInterval() const;
Expand Down
18 changes: 10 additions & 8 deletions tests/gtest/tests/clienttest.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -426,40 +426,42 @@ TEST_F(ClientTest, networkReceivedSendsPublishEmitsReceivedSignal_Test)
EXPECT_EQ(1, spy.count());
}

// todo: should happen on suback
TEST_F(ClientTest, subscribeEmitsSubscribedSignal_Test)
{
EXPECT_CALL(*_networkMock, sendFrame(_));
QSignalSpy spy(_client.data(), &QMQTT::Client::subscribed);

_client->subscribe("topic", QOS2);
quint16 msgid = _client->subscribe("topic", QOS2);

QByteArray payLoad;
payLoad.append((char)0x12); // message ID
payLoad.append((char)0x23); // message ID
payLoad.append((char)(msgid >> 8)); // message ID MSB
payLoad.append((char)(msgid && 0xFF)); // message ID LSB
payLoad.append((char)QOS2); // QOS
QMQTT::Frame frame(SUBACK_TYPE, payLoad);
emit _networkMock->received(frame);

ASSERT_EQ(1, spy.count());
EXPECT_EQ("topic", spy.at(0).at(0).toString());
EXPECT_EQ(QOS2, spy.at(0).at(1).toInt());
}

// todo: network received sends suback triggers a subscribed signal (other things?)

// todo: should happen on unsuback
TEST_F(ClientTest, unsubscribeEmitsUnsubscribedSignal_Test)
{
EXPECT_CALL(*_networkMock, sendFrame(_));
QSignalSpy spy(_client.data(), &QMQTT::Client::unsubscribed);

_client->unsubscribe("topic");

QMQTT::Frame frame(UNSUBACK_TYPE, QByteArray());
QByteArray payLoad;
payLoad.append((char)0x00); // message ID MSB
payLoad.append((char)0x01); // message ID LSB
QMQTT::Frame frame(UNSUBACK_TYPE, payLoad);
emit _networkMock->received(frame);

EXPECT_EQ(1, spy.count());
// EXPECT_EQ("topic", spy.at(0).at(0).toString());
ASSERT_EQ(1, spy.count());
EXPECT_EQ("topic", spy.at(0).at(0).toString());
}

// todo: network received sends unsuback then emit unsubscribed signal (only then?)
Expand Down