File indexing completed on 2025-01-05 04:37:21

0001 /*
0002     SPDX-FileCopyrightText: 2012 Joris Guisson <joris.guisson@gmail.com>
0003 
0004     SPDX-License-Identifier: GPL-2.0-or-later
0005 */
0006 
0007 #include <QObject>
0008 #include <QtTest>
0009 #include <interfaces/blocklistinterface.h>
0010 #include <peer/accessmanager.h>
0011 #include <torrent/server.h>
0012 #include <tracker/tracker.h>
0013 #include <util/log.h>
0014 
0015 class TestBlockList : public bt::BlockListInterface
0016 {
0017 public:
0018     TestBlockList()
0019     {
0020     }
0021 
0022     ~TestBlockList() override
0023     {
0024     }
0025 
0026     bool blocked(const net::Address &addr) const override
0027     {
0028         return addr.toString() == "8.8.8.8";
0029     }
0030 };
0031 
0032 class AccessManagerTest : public QObject
0033 {
0034     Q_OBJECT
0035 public:
0036 private Q_SLOTS:
0037     void initTestCase()
0038     {
0039         bt::InitLog("accessmanagertest.log");
0040     }
0041 
0042     void cleanupTestCase()
0043     {
0044     }
0045 
0046     void testCustomIP()
0047     {
0048         bt::Tracker::setCustomIP("123.123.123.123");
0049         bt::Server::setPort(7777);
0050         QVERIFY(!bt::AccessManager::instance().allowed(net::Address("123.123.123.123", 7777)));
0051         QVERIFY(bt::AccessManager::instance().allowed(net::Address("123.123.123.123", 7776)));
0052     }
0053 
0054     void testExternalAddress()
0055     {
0056         bt::Server::setPort(7777);
0057         bt::AccessManager::instance().addExternalIP("12.12.12.12");
0058         QVERIFY(!bt::AccessManager::instance().allowed(net::Address("12.12.12.12", 7777)));
0059         QVERIFY(bt::AccessManager::instance().allowed(net::Address("12.12.12.12", 7776)));
0060     }
0061 
0062     void testBlockList()
0063     {
0064         bt::AccessManager::instance().addBlockList(new TestBlockList());
0065         QVERIFY(!bt::AccessManager::instance().allowed(net::Address("8.8.8.8", 7777)));
0066         QVERIFY(bt::AccessManager::instance().allowed(net::Address("8.8.8.9", 7776)));
0067     }
0068 
0069 private:
0070 };
0071 
0072 QTEST_MAIN(AccessManagerTest)
0073 
0074 #include "accessmanagertest.moc"