2019-06-24 22:01:18 +08:00
|
|
|
/*
|
|
|
|
Copyright © 2019 by The qTox Project Contributors
|
|
|
|
|
|
|
|
This file is part of qTox, a Qt-based graphical interface for Tox.
|
|
|
|
|
|
|
|
qTox is libre software: you can redistribute it and/or modify
|
|
|
|
it under the terms of the GNU General Public License as published by
|
|
|
|
the Free Software Foundation, either version 3 of the License, or
|
|
|
|
(at your option) any later version.
|
|
|
|
|
|
|
|
qTox is distributed in the hope that it will be useful,
|
|
|
|
but WITHOUT ANY WARRANTY; without even the implied warranty of
|
|
|
|
MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
|
|
|
|
GNU General Public License for more details.
|
|
|
|
|
|
|
|
You should have received a copy of the GNU General Public License
|
|
|
|
along with qTox. If not, see <http://www.gnu.org/licenses/>.
|
|
|
|
*/
|
|
|
|
|
2019-06-06 23:41:21 +08:00
|
|
|
#include "src/model/message.h"
|
|
|
|
|
|
|
|
#include <tox/tox.h>
|
|
|
|
|
|
|
|
#include <QObject>
|
2019-08-11 20:38:24 +08:00
|
|
|
#include <QStringBuilder>
|
2019-06-06 23:41:21 +08:00
|
|
|
#include <QtTest/QtTest>
|
|
|
|
|
|
|
|
namespace {
|
|
|
|
bool messageHasSelfMention(const Message& message)
|
|
|
|
{
|
|
|
|
return std::any_of(message.metadata.begin(), message.metadata.end(), [](MessageMetadata meta) {
|
|
|
|
return meta.type == MessageMetadataType::selfMention;
|
|
|
|
});
|
|
|
|
}
|
|
|
|
} // namespace
|
|
|
|
|
|
|
|
class TestMessageProcessor : public QObject
|
|
|
|
{
|
|
|
|
Q_OBJECT
|
|
|
|
|
|
|
|
public:
|
2020-04-16 23:59:29 +08:00
|
|
|
TestMessageProcessor(){}
|
2019-06-06 23:41:21 +08:00
|
|
|
|
|
|
|
private slots:
|
|
|
|
void testSelfMention();
|
|
|
|
void testOutgoingMessage();
|
|
|
|
void testIncomingMessage();
|
|
|
|
};
|
|
|
|
|
|
|
|
|
|
|
|
/**
|
|
|
|
* @brief Tests detection of username
|
|
|
|
*/
|
|
|
|
void TestMessageProcessor::testSelfMention()
|
|
|
|
{
|
2021-01-25 06:49:24 +08:00
|
|
|
MessageProcessor::SharedParams sharedParams(tox_max_message_length(), 10 * 1024 * 1024);;
|
2019-09-25 17:19:18 +08:00
|
|
|
const QLatin1String testUserName{"MyUserName"};
|
|
|
|
const QLatin1String testToxPk{
|
2019-08-11 20:38:24 +08:00
|
|
|
"0123456789ABCDEF0123456789ABCDEF0123456789ABCDEF0123456789ABCDEF"};
|
|
|
|
sharedParams.onUserNameSet(testUserName);
|
|
|
|
sharedParams.setPublicKey(testToxPk);
|
2019-06-06 23:41:21 +08:00
|
|
|
|
|
|
|
auto messageProcessor = MessageProcessor(sharedParams);
|
|
|
|
messageProcessor.enableMentions();
|
|
|
|
|
2019-08-11 20:38:24 +08:00
|
|
|
for (const auto& str : {testUserName, testToxPk}) {
|
2019-06-06 23:41:21 +08:00
|
|
|
|
2019-08-11 20:38:24 +08:00
|
|
|
// Using my name or public key should match
|
2019-11-13 12:12:05 +08:00
|
|
|
auto processedMessage = messageProcessor.processIncomingCoreMessage(false, str % " hi");
|
2019-08-11 20:38:24 +08:00
|
|
|
QVERIFY(messageHasSelfMention(processedMessage));
|
2019-06-06 23:41:21 +08:00
|
|
|
|
2019-08-11 20:38:24 +08:00
|
|
|
// Action messages should match too
|
2019-11-13 12:12:05 +08:00
|
|
|
processedMessage = messageProcessor.processIncomingCoreMessage(true, str % " hi");
|
2019-08-11 20:38:24 +08:00
|
|
|
QVERIFY(messageHasSelfMention(processedMessage));
|
2019-06-06 23:41:21 +08:00
|
|
|
|
2019-08-11 20:38:24 +08:00
|
|
|
// Too much text shouldn't match
|
2019-11-13 12:12:05 +08:00
|
|
|
processedMessage = messageProcessor.processIncomingCoreMessage(false, str % "2");
|
2019-08-11 20:38:24 +08:00
|
|
|
QVERIFY(!messageHasSelfMention(processedMessage));
|
2019-06-06 23:41:21 +08:00
|
|
|
|
2019-08-11 20:38:24 +08:00
|
|
|
// Unless it's a colon
|
2019-11-13 12:12:05 +08:00
|
|
|
processedMessage = messageProcessor.processIncomingCoreMessage(false, str % ": test");
|
2019-08-11 20:38:24 +08:00
|
|
|
QVERIFY(messageHasSelfMention(processedMessage));
|
2019-06-06 23:41:21 +08:00
|
|
|
|
2019-08-11 20:38:24 +08:00
|
|
|
// remove last character
|
|
|
|
QString chopped = str;
|
|
|
|
chopped.chop(1);
|
|
|
|
// Too little text shouldn't match
|
2019-11-13 12:12:05 +08:00
|
|
|
processedMessage = messageProcessor.processIncomingCoreMessage(false, chopped);
|
2019-08-11 20:38:24 +08:00
|
|
|
QVERIFY(!messageHasSelfMention(processedMessage));
|
|
|
|
|
|
|
|
// make lower case
|
|
|
|
QString lower = QString(str).toLower();
|
|
|
|
|
|
|
|
// The regex should be case insensitive
|
2019-11-13 12:12:05 +08:00
|
|
|
processedMessage = messageProcessor.processIncomingCoreMessage(false, lower % " hi");
|
2019-08-11 20:38:24 +08:00
|
|
|
QVERIFY(messageHasSelfMention(processedMessage));
|
|
|
|
}
|
2019-06-06 23:41:21 +08:00
|
|
|
|
|
|
|
// New user name changes should be detected
|
|
|
|
sharedParams.onUserNameSet("NewUserName");
|
2019-11-13 12:12:05 +08:00
|
|
|
auto processedMessage = messageProcessor.processIncomingCoreMessage(false, "NewUserName: hi");
|
2019-06-06 23:41:21 +08:00
|
|
|
QVERIFY(messageHasSelfMention(processedMessage));
|
|
|
|
|
|
|
|
// Special characters should be removed
|
|
|
|
sharedParams.onUserNameSet("New\nUserName");
|
2019-11-13 12:12:05 +08:00
|
|
|
processedMessage = messageProcessor.processIncomingCoreMessage(false, "NewUserName: hi");
|
2019-06-06 23:41:21 +08:00
|
|
|
QVERIFY(messageHasSelfMention(processedMessage));
|
2019-08-11 20:38:24 +08:00
|
|
|
|
|
|
|
// Regression tests for: https://github.com/qTox/qTox/issues/2119
|
|
|
|
{
|
|
|
|
// Empty usernames should not match
|
|
|
|
sharedParams.onUserNameSet("");
|
2019-11-13 12:12:05 +08:00
|
|
|
processedMessage = messageProcessor.processIncomingCoreMessage(false, "");
|
2019-08-11 20:38:24 +08:00
|
|
|
QVERIFY(!messageHasSelfMention(processedMessage));
|
|
|
|
|
|
|
|
// Empty usernames matched on everything, ensure this is not the case
|
2019-11-13 12:12:05 +08:00
|
|
|
processedMessage = messageProcessor.processIncomingCoreMessage(false, "a");
|
2019-08-11 20:38:24 +08:00
|
|
|
QVERIFY(!messageHasSelfMention(processedMessage));
|
|
|
|
}
|
2019-06-06 23:41:21 +08:00
|
|
|
}
|
|
|
|
|
|
|
|
/**
|
|
|
|
* @brief Tests behavior of the processor for outgoing messages
|
|
|
|
*/
|
|
|
|
void TestMessageProcessor::testOutgoingMessage()
|
|
|
|
{
|
2021-01-25 06:49:24 +08:00
|
|
|
auto sharedParams = MessageProcessor::SharedParams(tox_max_message_length(), 10 * 1024 * 1024);
|
2019-06-06 23:41:21 +08:00
|
|
|
auto messageProcessor = MessageProcessor(sharedParams);
|
|
|
|
|
|
|
|
QString testStr;
|
|
|
|
|
|
|
|
for (size_t i = 0; i < tox_max_message_length() + 50; ++i) {
|
|
|
|
testStr += "a";
|
|
|
|
}
|
|
|
|
|
2019-11-17 18:19:57 +08:00
|
|
|
auto messages = messageProcessor.processOutgoingMessage(false, testStr, ExtensionSet());
|
2019-06-06 23:41:21 +08:00
|
|
|
|
|
|
|
// The message processor should split our messages
|
|
|
|
QVERIFY(messages.size() == 2);
|
2019-11-17 18:19:57 +08:00
|
|
|
|
|
|
|
auto extensionSet = ExtensionSet();
|
|
|
|
extensionSet[ExtensionType::messages] = true;
|
|
|
|
messages = messageProcessor.processOutgoingMessage(false, testStr, extensionSet);
|
|
|
|
|
|
|
|
// If we have multipart messages we shouldn't split our messages
|
|
|
|
QVERIFY(messages.size() == 1);
|
2019-06-06 23:41:21 +08:00
|
|
|
}
|
|
|
|
|
|
|
|
/**
|
|
|
|
* @brief Tests behavior of the processor for incoming messages
|
|
|
|
*/
|
|
|
|
void TestMessageProcessor::testIncomingMessage()
|
|
|
|
{
|
|
|
|
// Nothing too special happening on the incoming side if we aren't looking for self mentions
|
2021-01-25 06:49:24 +08:00
|
|
|
auto sharedParams = MessageProcessor::SharedParams(tox_max_message_length(), 10 * 1024 * 1024);
|
2019-06-06 23:41:21 +08:00
|
|
|
auto messageProcessor = MessageProcessor(sharedParams);
|
2019-11-13 12:12:05 +08:00
|
|
|
auto message = messageProcessor.processIncomingCoreMessage(false, "test");
|
2019-06-06 23:41:21 +08:00
|
|
|
|
|
|
|
QVERIFY(message.isAction == false);
|
|
|
|
QVERIFY(message.content == "test");
|
|
|
|
QVERIFY(message.timestamp.isValid());
|
|
|
|
}
|
|
|
|
|
|
|
|
QTEST_GUILESS_MAIN(TestMessageProcessor)
|
|
|
|
#include "messageprocessor_test.moc"
|