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
Anthony Bilinski bc751c8e1c
refactor(test): Move mocks into their own library
Avoid re-compiling sources for each test that uses them.
2022-03-02 23:30:04 -08:00

128 lines
3.4 KiB
C++

/*
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 "src/persistence/settings.h"
#include "mock/mockcoresettings.h"
#include <QtTest/QtTest>
#include <QtGlobal>
#include <limits>
#include <QSignalSpy>
#include <iostream>
#include <memory>
Q_DECLARE_METATYPE(QList<DhtServer>)
class MockNodeListGenerator : public IBootstrapListGenerator
{
QList<DhtServer> getBootstrapnodes() const override;
};
QList<DhtServer> MockNodeListGenerator::getBootstrapnodes() const {
return BootstrapNodeUpdater::loadDefaultBootstrapNodes();
}
class TestCoreProxy : public QObject
{
Q_OBJECT
private slots:
void startup_without_proxy();
void startup_with_invalid_proxy();
private:
/* Test Variables */
Core::ToxCoreErrors* err = nullptr;
std::unique_ptr<MockSettings> settings;
QByteArray savedata{};
ToxCorePtr test_core;
};
namespace {
const int timeout = 90000; //90 seconds timeout allowed for test
}
void TestCoreProxy::startup_without_proxy()
{
settings = std::unique_ptr<MockSettings>(new MockSettings());
// No proxy
settings->setProxyAddr("");
settings->setProxyPort(0);
settings->setProxyType(MockSettings::ProxyType::ptNone);
MockNodeListGenerator nodesGenerator{};
test_core = Core::makeToxCore(savedata, *settings, nodesGenerator, err);
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 TestCoreProxy::startup_with_invalid_proxy()
{
settings = std::unique_ptr<MockSettings>(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);
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);
if (test_core != nullptr) {
QFAIL("ToxCore initialisation passed with invalid HTTP proxy address");
}
}
QTEST_GUILESS_MAIN(TestCoreProxy)
#include "core_test.moc"