1
0
mirror of https://github.com/qTox/qTox.git synced 2024-03-22 14:00:36 +08:00
qTox/test/core/core_test.cpp

167 lines
4.7 KiB
C++
Raw Normal View History

/*
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/>.
*/
#include "src/core/core.h"
#include "src/core/toxoptions.h"
#include "src/core/icoresettings.h"
#include "src/net/bootstrapnodeupdater.h"
#include "src/model/ibootstraplistgenerator.h"
#include <QtTest/QtTest>
#include <QtGlobal>
#include <limits>
#include <QSignalSpy>
#include <src/persistence/settings.h>
#include <iostream>
Q_DECLARE_METATYPE(QList<DhtServer>)
class MockSettings : public QObject, public ICoreSettings
{
Q_OBJECT
public:
MockSettings() {
Q_INIT_RESOURCE(res);
qRegisterMetaType<QList<DhtServer>>("QList<DhtServer>");
}
bool getEnableIPv6() const override { return false; }
void setEnableIPv6(bool) override { }
bool getForceTCP() const override { return false; }
void setForceTCP(bool) override { }
bool getEnableLanDiscovery() const override { return false; }
void setEnableLanDiscovery(bool) override { }
QString getProxyAddr() const override { return Addr; }
void setProxyAddr(const QString &Addr) override { this->Addr = Addr; }
ProxyType getProxyType() const override { return type; }
void setProxyType(ProxyType type) override { this->type = type; }
quint16 getProxyPort() const override { return port; }
void setProxyPort(quint16 port) override { this->port = port; }
QNetworkProxy getProxy() const override { return QNetworkProxy(QNetworkProxy::ProxyType::NoProxy); }
SIGNAL_IMPL(MockSettings, enableIPv6Changed, bool enabled)
SIGNAL_IMPL(MockSettings, forceTCPChanged, bool enabled)
SIGNAL_IMPL(MockSettings, enableLanDiscoveryChanged, bool enabled)
SIGNAL_IMPL(MockSettings, proxyTypeChanged, ICoreSettings::ProxyType type)
SIGNAL_IMPL(MockSettings, proxyAddressChanged, const QString& address)
SIGNAL_IMPL(MockSettings, proxyPortChanged, quint16 port)
private:
QList<DhtServer> dhtServerList;
QString Addr;
ProxyType type;
quint16 port;
};
class MockNodeListGenerator : public IBootstrapListGenerator
{
QList<DhtServer> getBootstrapnodes();
};
QList<DhtServer> MockNodeListGenerator::getBootstrapnodes() {
return BootstrapNodeUpdater::loadDefaultBootstrapNodes();
}
class TestCore : public QObject
{
Q_OBJECT
private slots:
void startup_without_proxy();
void startup_with_invalid_proxy();
private:
/* Test Variables */
Core::ToxCoreErrors* err = nullptr;
MockSettings* settings;
QByteArray savedata{};
ToxCorePtr test_core;
};
namespace {
const int timeout = 90000; //90 seconds timeout allowed for test
}
void TestCore::startup_without_proxy()
{
settings = new MockSettings();
// No proxy
settings->setProxyAddr("");
settings->setProxyPort(0);
settings->setProxyType(MockSettings::ProxyType::ptNone);
MockNodeListGenerator nodesGenerator{};
test_core = Core::makeToxCore(savedata, settings, nodesGenerator, err);
chore: Various code cleanups. * Reorder class data members and/or constructor initialisers to match, reducing confusion about when members will be initialised. * Remove (most) unused variables. Not removed: some global variables with `TODO(sudden6)` on them for using them in the future. I don't know how far into the future sudden6 wants to use them, so I left them there for now. * Distinguish different bootstrap nodes in the logs by index in the bootstrap node list. Originally, we used to log the address/port of the node we're bootstrapping to. This was removed out of privacy concerns (even though the bootstrap nodes are public). This made the logs much less useful when debugging why the client isn't connecting. Having indices makes it easier to see that different nodes are being selected, and makes it possible to determine which node was selected. * Explicitly cast unused results of Tox API functions to `void` when all we want is to know whether the function succeeds or not. * Don't try to `#include <unistd.h>` on Windows. It does not exist on MSVC. * Remove extra `;` after function definitions. * Remove reference indirection of QJsonValueRef, since a copy of that ref (small pointer-like object) has to be made anyway when iterating over QJsonArrays. * Make some file-scope global state `static`. * Use `nullptr` instead of `NULL`. * Add `#if DESKTOP_NOTIFICATIONS` around the code that implements desktop notifications, so it becomes a bit easier to compile everything with a single compiler command - useful for manually running static analysers. * Fix an error on MSVC where `disconnect` is looked up to be a non-static member function and the `this` capture is missing. * Consistently use `struct` and `class` tags for types. * Use references in ranged-for where it reduces copies. * Move private static data members out of the Style class and into file-local scope. There is no need for them to be in the class. Also marked them `const` where possible. * Removed unused lambda capture. * Ensure qTox can compile under NDEBUG with `-Wunused-variable` by inlining the unused variable into the `assert` that was its only target. * Minor reformatting in core_test.cpp.
2020-04-16 23:59:29 +08:00
if (test_core == nullptr) {
QFAIL("ToxCore initialisation failed");
}
QSignalSpy spyCore(test_core.get(), &Core::connected);
test_core->start();
QVERIFY(spyCore.wait(timeout)); //wait 90seconds
QCOMPARE(spyCore.count(), 1); // make sure the signal was emitted exactly one time
}
void TestCore::startup_with_invalid_proxy()
{
settings = new MockSettings();
// Test invalid proxy SOCKS5
settings->setProxyAddr("Test");
settings->setProxyPort(9985);
settings->setProxyType(MockSettings::ProxyType::ptSOCKS5);
MockNodeListGenerator nodesGenerator{};
test_core = Core::makeToxCore(savedata, settings, nodesGenerator, err);
chore: Various code cleanups. * Reorder class data members and/or constructor initialisers to match, reducing confusion about when members will be initialised. * Remove (most) unused variables. Not removed: some global variables with `TODO(sudden6)` on them for using them in the future. I don't know how far into the future sudden6 wants to use them, so I left them there for now. * Distinguish different bootstrap nodes in the logs by index in the bootstrap node list. Originally, we used to log the address/port of the node we're bootstrapping to. This was removed out of privacy concerns (even though the bootstrap nodes are public). This made the logs much less useful when debugging why the client isn't connecting. Having indices makes it easier to see that different nodes are being selected, and makes it possible to determine which node was selected. * Explicitly cast unused results of Tox API functions to `void` when all we want is to know whether the function succeeds or not. * Don't try to `#include <unistd.h>` on Windows. It does not exist on MSVC. * Remove extra `;` after function definitions. * Remove reference indirection of QJsonValueRef, since a copy of that ref (small pointer-like object) has to be made anyway when iterating over QJsonArrays. * Make some file-scope global state `static`. * Use `nullptr` instead of `NULL`. * Add `#if DESKTOP_NOTIFICATIONS` around the code that implements desktop notifications, so it becomes a bit easier to compile everything with a single compiler command - useful for manually running static analysers. * Fix an error on MSVC where `disconnect` is looked up to be a non-static member function and the `this` capture is missing. * Consistently use `struct` and `class` tags for types. * Use references in ranged-for where it reduces copies. * Move private static data members out of the Style class and into file-local scope. There is no need for them to be in the class. Also marked them `const` where possible. * Removed unused lambda capture. * Ensure qTox can compile under NDEBUG with `-Wunused-variable` by inlining the unused variable into the `assert` that was its only target. * Minor reformatting in core_test.cpp.
2020-04-16 23:59:29 +08:00
if (test_core != nullptr) {
QFAIL("ToxCore initialisation passed with invalid SOCKS5 proxy address");
}
// Test invalid proxy HTTP
settings->setProxyAddr("Test");
settings->setProxyPort(9985);
settings->setProxyType(MockSettings::ProxyType::ptHTTP);
test_core = Core::makeToxCore(savedata, settings, nodesGenerator, err);
chore: Various code cleanups. * Reorder class data members and/or constructor initialisers to match, reducing confusion about when members will be initialised. * Remove (most) unused variables. Not removed: some global variables with `TODO(sudden6)` on them for using them in the future. I don't know how far into the future sudden6 wants to use them, so I left them there for now. * Distinguish different bootstrap nodes in the logs by index in the bootstrap node list. Originally, we used to log the address/port of the node we're bootstrapping to. This was removed out of privacy concerns (even though the bootstrap nodes are public). This made the logs much less useful when debugging why the client isn't connecting. Having indices makes it easier to see that different nodes are being selected, and makes it possible to determine which node was selected. * Explicitly cast unused results of Tox API functions to `void` when all we want is to know whether the function succeeds or not. * Don't try to `#include <unistd.h>` on Windows. It does not exist on MSVC. * Remove extra `;` after function definitions. * Remove reference indirection of QJsonValueRef, since a copy of that ref (small pointer-like object) has to be made anyway when iterating over QJsonArrays. * Make some file-scope global state `static`. * Use `nullptr` instead of `NULL`. * Add `#if DESKTOP_NOTIFICATIONS` around the code that implements desktop notifications, so it becomes a bit easier to compile everything with a single compiler command - useful for manually running static analysers. * Fix an error on MSVC where `disconnect` is looked up to be a non-static member function and the `this` capture is missing. * Consistently use `struct` and `class` tags for types. * Use references in ranged-for where it reduces copies. * Move private static data members out of the Style class and into file-local scope. There is no need for them to be in the class. Also marked them `const` where possible. * Removed unused lambda capture. * Ensure qTox can compile under NDEBUG with `-Wunused-variable` by inlining the unused variable into the `assert` that was its only target. * Minor reformatting in core_test.cpp.
2020-04-16 23:59:29 +08:00
if (test_core != nullptr) {
QFAIL("ToxCore initialisation passed with invalid HTTP proxy address");
}
}
QTEST_GUILESS_MAIN(TestCore)
#include "core_test.moc"