File: core_test.cpp

package info (click to toggle)
qtox 1.17.3-1
  • links: PTS, VCS
  • area: main
  • in suites: bookworm, bullseye, sid
  • size: 25,628 kB
  • sloc: cpp: 40,089; xml: 10,393; sh: 2,401; ansic: 1,520; objc: 208; perl: 28; makefile: 13
file content (153 lines) | stat: -rw-r--r-- 4,421 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
/*
    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 <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 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);

    test_core = Core::makeToxCore(savedata, settings, 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 TestCore::startup_with_invalid_proxy()
{
    settings = new MockSettings();


    // Test invalid proxy SOCKS5
    settings->setProxyAddr("Test");
    settings->setProxyPort(9985);
    settings->setProxyType(MockSettings::ProxyType::ptSOCKS5);

    test_core = Core::makeToxCore(savedata, settings, 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, err);

    if(test_core != nullptr) {
        QFAIL("ToxCore initialisation passed with invalid HTTP proxy address");
    }
}

QTEST_GUILESS_MAIN(TestCore)
#include "core_test.moc"