|
| 1 | +// Copyright (c) 2026 The Bitcoin Core developers |
| 2 | +// Distributed under the MIT software license, see the accompanying |
| 3 | +// file COPYING or http://www.opensource.org/licenses/mit-license.php. |
| 4 | + |
| 5 | +#include <QtTest/QtTest> |
| 6 | + |
| 7 | +#include <QSet> |
| 8 | + |
| 9 | +#include <qml/models/banlistmodel.h> |
| 10 | +#include <test/mocks/mocknode.h> |
| 11 | +#include <util/translation.h> |
| 12 | + |
| 13 | +#include <gmock/gmock.h> |
| 14 | +#include <netbase.h> |
| 15 | + |
| 16 | +#include <map> |
| 17 | +#include <optional> |
| 18 | +#include <stdexcept> |
| 19 | +#include <utility> |
| 20 | + |
| 21 | +const TranslateFn G_TRANSLATION_FUN{nullptr}; |
| 22 | + |
| 23 | +namespace { |
| 24 | +CSubNet ParseSubnet(const std::string& subnet) |
| 25 | +{ |
| 26 | + const std::optional<CSubNet> parsed = LookupSubNet(subnet, /*allow_lookup=*/false); |
| 27 | + if (!parsed.has_value()) { |
| 28 | + throw std::runtime_error("failed to parse subnet test fixture"); |
| 29 | + } |
| 30 | + return *parsed; |
| 31 | +} |
| 32 | + |
| 33 | +CBanEntry MakeBanEntry(int64_t ban_until) |
| 34 | +{ |
| 35 | + CBanEntry entry; |
| 36 | + entry.nBanUntil = ban_until; |
| 37 | + return entry; |
| 38 | +} |
| 39 | +} // namespace |
| 40 | + |
| 41 | +class BanListModelTests : public QObject |
| 42 | +{ |
| 43 | + Q_OBJECT |
| 44 | + |
| 45 | +private Q_SLOTS: |
| 46 | + void refreshPopulatesRolesAndRows(); |
| 47 | + void unbanAtTargetsSelectedSubnet(); |
| 48 | + void unbanAtIgnoresInvalidRows(); |
| 49 | +}; |
| 50 | + |
| 51 | +void BanListModelTests::refreshPopulatesRolesAndRows() |
| 52 | +{ |
| 53 | + using ::testing::_; |
| 54 | + using ::testing::DoAll; |
| 55 | + using ::testing::NiceMock; |
| 56 | + using ::testing::Return; |
| 57 | + using ::testing::SetArgReferee; |
| 58 | + |
| 59 | + banmap_t banned; |
| 60 | + const CSubNet subnet_a = ParseSubnet("10.0.0.0/8"); |
| 61 | + const CSubNet subnet_b = ParseSubnet("127.0.0.1/32"); |
| 62 | + banned.emplace(subnet_a, MakeBanEntry(1'900'000'000)); |
| 63 | + banned.emplace(subnet_b, MakeBanEntry(2'000'000'000)); |
| 64 | + |
| 65 | + NiceMock<MockNode> node; |
| 66 | + EXPECT_CALL(node, getBanned(_)) |
| 67 | + .Times(1) |
| 68 | + .WillOnce(DoAll(SetArgReferee<0>(banned), Return(true))); |
| 69 | + |
| 70 | + BanListModel model{node, nullptr}; |
| 71 | + QSignalSpy count_spy(&model, &BanListModel::countChanged); |
| 72 | + |
| 73 | + model.refresh(); |
| 74 | + |
| 75 | + QCOMPARE(model.count(), 2); |
| 76 | + QCOMPARE(model.rowCount(), 2); |
| 77 | + QCOMPARE(model.rowCount(model.index(0, 0)), 0); |
| 78 | + QCOMPARE(count_spy.count(), 1); |
| 79 | + |
| 80 | + const auto roles = model.roleNames(); |
| 81 | + QCOMPARE(roles.value(static_cast<int>(BanListModel::BanRoles::AddressRole)), QByteArray{"address"}); |
| 82 | + QCOMPARE(roles.value(static_cast<int>(BanListModel::BanRoles::BanUntilRole)), QByteArray{"banUntil"}); |
| 83 | + |
| 84 | + QSet<QString> addresses; |
| 85 | + for (int row = 0; row < model.rowCount(); ++row) { |
| 86 | + const QModelIndex index = model.index(row, 0); |
| 87 | + QVERIFY(index.isValid()); |
| 88 | + |
| 89 | + const QString address = model.data(index, static_cast<int>(BanListModel::BanRoles::AddressRole)).toString(); |
| 90 | + QVERIFY(!address.isEmpty()); |
| 91 | + addresses.insert(address); |
| 92 | + |
| 93 | + const QString ban_until = model.data(index, static_cast<int>(BanListModel::BanRoles::BanUntilRole)).toString(); |
| 94 | + QVERIFY(!ban_until.isEmpty()); |
| 95 | + } |
| 96 | + |
| 97 | + QVERIFY(addresses.contains(QString::fromStdString(subnet_a.ToString()))); |
| 98 | + QVERIFY(addresses.contains(QString::fromStdString(subnet_b.ToString()))); |
| 99 | +} |
| 100 | + |
| 101 | +void BanListModelTests::unbanAtTargetsSelectedSubnet() |
| 102 | +{ |
| 103 | + using ::testing::_; |
| 104 | + using ::testing::DoAll; |
| 105 | + using ::testing::NiceMock; |
| 106 | + using ::testing::Return; |
| 107 | + using ::testing::SetArgReferee; |
| 108 | + using ::testing::Truly; |
| 109 | + |
| 110 | + banmap_t banned; |
| 111 | + const CSubNet subnet = ParseSubnet("10.0.0.0/8"); |
| 112 | + banned.emplace(subnet, MakeBanEntry(1'900'000'000)); |
| 113 | + |
| 114 | + NiceMock<MockNode> node; |
| 115 | + EXPECT_CALL(node, getBanned(_)) |
| 116 | + .Times(1) |
| 117 | + .WillOnce(DoAll(SetArgReferee<0>(banned), Return(true))); |
| 118 | + |
| 119 | + BanListModel model{node, nullptr}; |
| 120 | + model.refresh(); |
| 121 | + |
| 122 | + EXPECT_CALL(node, unban(Truly([&](const CSubNet& value) { |
| 123 | + return value.ToString() == subnet.ToString(); |
| 124 | + }))).Times(1).WillOnce(Return(true)); |
| 125 | + model.unbanAt(0); |
| 126 | +} |
| 127 | + |
| 128 | +void BanListModelTests::unbanAtIgnoresInvalidRows() |
| 129 | +{ |
| 130 | + using ::testing::_; |
| 131 | + using ::testing::DoAll; |
| 132 | + using ::testing::NiceMock; |
| 133 | + using ::testing::Return; |
| 134 | + using ::testing::SetArgReferee; |
| 135 | + |
| 136 | + banmap_t banned; |
| 137 | + banned.emplace(ParseSubnet("10.0.0.0/8"), MakeBanEntry(1'900'000'000)); |
| 138 | + |
| 139 | + NiceMock<MockNode> node; |
| 140 | + EXPECT_CALL(node, getBanned(_)) |
| 141 | + .Times(1) |
| 142 | + .WillOnce(DoAll(SetArgReferee<0>(banned), Return(true))); |
| 143 | + |
| 144 | + BanListModel model{node, nullptr}; |
| 145 | + model.refresh(); |
| 146 | + |
| 147 | + EXPECT_CALL(node, unban(_)).Times(0); |
| 148 | + model.unbanAt(-1); |
| 149 | + model.unbanAt(42); |
| 150 | +} |
| 151 | + |
| 152 | +int RunBanListModelTests(int argc, char* argv[]) |
| 153 | +{ |
| 154 | + BanListModelTests tests; |
| 155 | + return QTest::qExec(&tests, argc, argv); |
| 156 | +} |
| 157 | + |
| 158 | +#ifndef BITCOINQML_NO_TEST_MAIN |
| 159 | +QTEST_MAIN(BanListModelTests) |
| 160 | +#endif |
| 161 | +#include "test_banlistmodel.moc" |
0 commit comments