File: conferencemessagedispatcher_test.cpp

package info (click to toggle)
qtox 1.18.3-2
  • links: PTS, VCS
  • area: main
  • in suites: forky
  • size: 28,996 kB
  • sloc: cpp: 48,067; xml: 8,560; python: 704; sh: 232; makefile: 14
file content (252 lines) | stat: -rw-r--r-- 8,605 bytes parent folder | download
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
/* SPDX-License-Identifier: GPL-3.0-or-later
 * Copyright © 2019 by The qTox Project Contributors
 * Copyright © 2024-2025 The TokTok team.
 */

#include "src/model/conferencemessagedispatcher.h"

#include "mock/mockconferencequery.h"
#include "mock/mockcoreidhandler.h"
#include "src/core/icoreconferencemessagesender.h"
#include "src/friendlist.h"
#include "src/model/conference.h"
#include "src/model/message.h"
#include "src/persistence/iconferencesettings.h"
#include "util/interface.h"

#include <QObject>
#include <QtTest/QtTest>

#include <deque>
#include <memory>
#include <set>
#include <tox/tox.h> // tox_max_message_length

class MockConferenceMessageSender : public ICoreConferenceMessageSender
{
public:
    void sendConferenceAction(int conferenceId, const QString& action) override;

    void sendConferenceMessage(int conferenceId, const QString& message) override;

    size_t numSentActions = 0;
    size_t numSentMessages = 0;
};

void MockConferenceMessageSender::sendConferenceAction(int conferenceId, const QString& action)
{
    std::ignore = conferenceId;
    std::ignore = action;
    numSentActions++;
}

void MockConferenceMessageSender::sendConferenceMessage(int conferenceId, const QString& message)
{
    std::ignore = conferenceId;
    std::ignore = message;
    numSentMessages++;
}

class MockConferenceSettings : public QObject, public IConferenceSettings
{
    Q_OBJECT

public:
    QStringList getBlockList() const override;
    void setBlockList(const QStringList& blist) override;
    SIGNAL_IMPL(MockConferenceSettings, blockListChanged, const QStringList& blist)

    bool getShowConferenceJoinLeaveMessages() const override
    {
        return true;
    }
    void setShowConferenceJoinLeaveMessages(bool newValue) override
    {
        std::ignore = newValue;
    }
    SIGNAL_IMPL(MockConferenceSettings, showConferenceJoinLeaveMessagesChanged, bool show)

private:
    QStringList blockList;
};

QStringList MockConferenceSettings::getBlockList() const
{
    return blockList;
}

void MockConferenceSettings::setBlockList(const QStringList& blist)
{
    blockList = blist;
}

class TestConferenceMessageDispatcher : public QObject
{
    Q_OBJECT

public:
    TestConferenceMessageDispatcher();

private slots:
    void init();
    void testSignals();
    void testMessageSending();
    void testEmptyConference();
    void testSelfReceive();
    void testBlockList();

    void onMessageSent(DispatchedMessageId id, Message message)
    {
        auto it = outgoingMessages.find(id);
        QVERIFY(it == outgoingMessages.end());
        outgoingMessages.emplace(id);
        sentMessages.push_back(std::move(message));
    }

    void onMessageComplete(DispatchedMessageId id)
    {
        auto it = outgoingMessages.find(id);
        QVERIFY(it != outgoingMessages.end());
        outgoingMessages.erase(it);
    }

    void onMessageReceived(const ToxPk& sender, Message message)
    {
        std::ignore = sender;
        receivedMessages.push_back(std::move(message));
    }

private:
    // All unique_ptrs to make construction/init() easier to manage
    std::unique_ptr<MockConferenceSettings> conferenceSettings;
    std::unique_ptr<MockConferenceQuery> conferenceQuery;
    std::unique_ptr<MockCoreIdHandler> coreIdHandler;
    std::unique_ptr<Conference> g;
    std::unique_ptr<MockConferenceMessageSender> messageSender;
    std::unique_ptr<MessageProcessor::SharedParams> sharedProcessorParams;
    std::unique_ptr<MessageProcessor> messageProcessor;
    std::unique_ptr<ConferenceMessageDispatcher> conferenceMessageDispatcher;
    std::set<DispatchedMessageId> outgoingMessages;
    std::deque<Message> sentMessages;
    std::deque<Message> receivedMessages;
    std::unique_ptr<FriendList> friendList;
};

TestConferenceMessageDispatcher::TestConferenceMessageDispatcher() = default;

/**
 * @brief Test initialization. Resets all members to initial state
 */
void TestConferenceMessageDispatcher::init()
{
    friendList = std::make_unique<FriendList>();
    conferenceSettings = std::make_unique<MockConferenceSettings>();
    conferenceQuery = std::make_unique<MockConferenceQuery>();
    coreIdHandler = std::make_unique<MockCoreIdHandler>();
    g = std::make_unique<Conference>(0, ConferenceId(), "TestConference", false, "me",
                                     *conferenceQuery, *coreIdHandler, *friendList);
    messageSender = std::make_unique<MockConferenceMessageSender>();
    sharedProcessorParams = std::make_unique<MessageProcessor::SharedParams>(tox_max_message_length());
    messageProcessor = std::make_unique<MessageProcessor>(*sharedProcessorParams);
    conferenceMessageDispatcher =
        std::make_unique<ConferenceMessageDispatcher>(*g, *messageProcessor, *coreIdHandler,
                                                      *messageSender, *conferenceSettings);

    connect(conferenceMessageDispatcher.get(), &ConferenceMessageDispatcher::messageSent, this,
            &TestConferenceMessageDispatcher::onMessageSent);
    connect(conferenceMessageDispatcher.get(), &ConferenceMessageDispatcher::messageComplete, this,
            &TestConferenceMessageDispatcher::onMessageComplete);
    connect(conferenceMessageDispatcher.get(), &ConferenceMessageDispatcher::messageReceived, this,
            &TestConferenceMessageDispatcher::onMessageReceived);

    outgoingMessages = std::set<DispatchedMessageId>();
    sentMessages = std::deque<Message>();
    receivedMessages = std::deque<Message>();
}

/**
 * @brief Tests that the signals emitted by the dispatcher are all emitted at the correct times
 */
void TestConferenceMessageDispatcher::testSignals()
{
    conferenceMessageDispatcher->sendMessage(false, "test");

    // For conferences we pair our sent and completed signals since we have no receiver reports
    QVERIFY(outgoingMessages.empty());
    QVERIFY(!sentMessages.empty());
    QVERIFY(sentMessages.front().isAction == false);
    QVERIFY(sentMessages.front().content == "test");

    // If signals are emitted correctly we should have one message in our received message buffer
    QVERIFY(receivedMessages.empty());
    conferenceMessageDispatcher->onMessageReceived(ToxPk(), false, "test2");

    QVERIFY(!receivedMessages.empty());
    QVERIFY(receivedMessages.front().isAction == false);
    QVERIFY(receivedMessages.front().content == "test2");
}

/**
 * @brief Tests that sent messages actually go through to core
 */
void TestConferenceMessageDispatcher::testMessageSending()
{
    conferenceMessageDispatcher->sendMessage(false, "Test");

    QVERIFY(messageSender->numSentMessages == 1);
    QVERIFY(messageSender->numSentActions == 0);

    conferenceMessageDispatcher->sendMessage(true, "Test");

    QVERIFY(messageSender->numSentMessages == 1);
    QVERIFY(messageSender->numSentActions == 1);
}

/**
 * @brief Tests that if we are the only member in a conference we do _not_ send messages to core.
 * Toxcore isn't too happy if we send messages and we're the only one in the conference
 */
void TestConferenceMessageDispatcher::testEmptyConference()
{
    conferenceQuery->setAsEmptyConference();
    g->regeneratePeerList();

    conferenceMessageDispatcher->sendMessage(false, "Test");
    conferenceMessageDispatcher->sendMessage(true, "Test");

    QVERIFY(messageSender->numSentMessages == 0);
    QVERIFY(messageSender->numSentActions == 0);
}

/**
 * @brief Tests that we do not emit any signals if we receive a message from ourself. Toxcore will send us back messages we sent
 */
void TestConferenceMessageDispatcher::testSelfReceive()
{
    uint8_t selfId[ToxPk::size] = {0};
    conferenceMessageDispatcher->onMessageReceived(ToxPk(selfId), false, "Test");
    QVERIFY(receivedMessages.empty());

    uint8_t id[ToxPk::size] = {1};
    conferenceMessageDispatcher->onMessageReceived(ToxPk(id), false, "Test");
    QVERIFY(receivedMessages.size() == 1);
}

/**
 * @brief Tests that messages from block-listed peers do not get propagated from the dispatcher
 */
void TestConferenceMessageDispatcher::testBlockList()
{
    uint8_t id[ToxPk::size] = {1};
    auto otherPk = ToxPk(id);
    conferenceMessageDispatcher->onMessageReceived(otherPk, false, "Test");
    QVERIFY(receivedMessages.size() == 1);

    conferenceSettings->setBlockList({otherPk.toString()});
    conferenceMessageDispatcher->onMessageReceived(otherPk, false, "Test");
    QVERIFY(receivedMessages.size() == 1);
}

// Cannot be guiless due to a settings instance in ConferenceMessageDispatcher
QTEST_GUILESS_MAIN(TestConferenceMessageDispatcher)
#include "conferencemessagedispatcher_test.moc"