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:
|
|
|
|
TestMessageProcessor(){};
|
|
|
|
|
|
|
|
private slots:
|
|
|
|
void testSelfMention();
|
|
|
|
void testOutgoingMessage();
|
|
|
|
void testIncomingMessage();
|
|
|
|
};
|
|
|
|
|
|
|
|
|
|
|
|
/**
|
|
|
|
* @brief Tests detection of username
|
|
|
|
*/
|
|
|
|
void TestMessageProcessor::testSelfMention()
|
|
|
|
{
|
|
|
|
MessageProcessor::SharedParams sharedParams;
|
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
|
|
|
|
auto processedMessage = messageProcessor.processIncomingMessage(false, str % " hi");
|
|
|
|
QVERIFY(messageHasSelfMention(processedMessage));
|
2019-06-06 23:41:21 +08:00
|
|
|
|
2019-08-11 20:38:24 +08:00
|
|
|
// Action messages should match too
|
|
|
|
processedMessage = messageProcessor.processIncomingMessage(true, str % " hi");
|
|
|
|
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
|
|
|
|
processedMessage = messageProcessor.processIncomingMessage(false, str % "2");
|
|
|
|
QVERIFY(!messageHasSelfMention(processedMessage));
|
2019-06-06 23:41:21 +08:00
|
|
|
|
2019-08-11 20:38:24 +08:00
|
|
|
// Unless it's a colon
|
|
|
|
processedMessage = messageProcessor.processIncomingMessage(false, str % ": test");
|
|
|
|
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
|
|
|
|
processedMessage = messageProcessor.processIncomingMessage(false, chopped);
|
|
|
|
QVERIFY(!messageHasSelfMention(processedMessage));
|
|
|
|
|
|
|
|
// make lower case
|
|
|
|
QString lower = QString(str).toLower();
|
|
|
|
|
|
|
|
// The regex should be case insensitive
|
|
|
|
processedMessage = messageProcessor.processIncomingMessage(false, lower % " hi");
|
|
|
|
QVERIFY(messageHasSelfMention(processedMessage));
|
|
|
|
}
|
2019-06-06 23:41:21 +08:00
|
|
|
|
|
|
|
// New user name changes should be detected
|
|
|
|
sharedParams.onUserNameSet("NewUserName");
|
2019-08-11 20:38:24 +08:00
|
|
|
auto processedMessage = messageProcessor.processIncomingMessage(false, "NewUserName: hi");
|
2019-06-06 23:41:21 +08:00
|
|
|
QVERIFY(messageHasSelfMention(processedMessage));
|
|
|
|
|
|
|
|
// Special characters should be removed
|
|
|
|
sharedParams.onUserNameSet("New\nUserName");
|
|
|
|
processedMessage = messageProcessor.processIncomingMessage(false, "NewUserName: hi");
|
|
|
|
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("");
|
|
|
|
processedMessage = messageProcessor.processIncomingMessage(false, "");
|
|
|
|
QVERIFY(!messageHasSelfMention(processedMessage));
|
|
|
|
|
|
|
|
// Empty usernames matched on everything, ensure this is not the case
|
|
|
|
processedMessage = messageProcessor.processIncomingMessage(false, "a");
|
|
|
|
QVERIFY(!messageHasSelfMention(processedMessage));
|
|
|
|
}
|
2019-06-06 23:41:21 +08:00
|
|
|
}
|
|
|
|
|
|
|
|
/**
|
|
|
|
* @brief Tests behavior of the processor for outgoing messages
|
|
|
|
*/
|
|
|
|
void TestMessageProcessor::testOutgoingMessage()
|
|
|
|
{
|
|
|
|
auto sharedParams = MessageProcessor::SharedParams();
|
|
|
|
auto messageProcessor = MessageProcessor(sharedParams);
|
|
|
|
|
|
|
|
QString testStr;
|
|
|
|
|
|
|
|
for (size_t i = 0; i < tox_max_message_length() + 50; ++i) {
|
|
|
|
testStr += "a";
|
|
|
|
}
|
|
|
|
|
|
|
|
auto messages = messageProcessor.processOutgoingMessage(false, testStr);
|
|
|
|
|
|
|
|
// The message processor should split our messages
|
|
|
|
QVERIFY(messages.size() == 2);
|
|
|
|
}
|
|
|
|
|
|
|
|
/**
|
|
|
|
* @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
|
|
|
|
auto sharedParams = MessageProcessor::SharedParams();
|
|
|
|
auto messageProcessor = MessageProcessor(sharedParams);
|
|
|
|
auto message = messageProcessor.processIncomingMessage(false, "test");
|
|
|
|
|
|
|
|
QVERIFY(message.isAction == false);
|
|
|
|
QVERIFY(message.content == "test");
|
|
|
|
QVERIFY(message.timestamp.isValid());
|
|
|
|
}
|
|
|
|
|
|
|
|
QTEST_GUILESS_MAIN(TestMessageProcessor)
|
|
|
|
#include "messageprocessor_test.moc"
|