Skip to content

Commit 5c8b4ba

Browse files
amitiuttarwarmzumsandevasild
committed
tests: add addrman_select_by_network test
this adds coverage for the 7 different cases of which table should be selected when the network is specified. the different cases are the result of new_only being true or false and whether there are network addresses on both, neither, or one of new vs tried tables. the only case not covered is when new_only is false and the only network addresses are on the new table. Co-authored-by: Martin Zumsande <[email protected]> Co-authored-by: Vasil Dimov <[email protected]>
1 parent 6b22928 commit 5c8b4ba

File tree

1 file changed

+60
-0
lines changed

1 file changed

+60
-0
lines changed

src/test/addrman_tests.cpp

Lines changed: 60 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -192,6 +192,66 @@ BOOST_AUTO_TEST_CASE(addrman_select)
192192
BOOST_CHECK_EQUAL(ports.size(), 3U);
193193
}
194194

195+
BOOST_AUTO_TEST_CASE(addrman_select_by_network)
196+
{
197+
auto addrman = std::make_unique<AddrMan>(EMPTY_NETGROUPMAN, DETERMINISTIC, GetCheckRatio(m_node));
198+
BOOST_CHECK(!addrman->Select(/*new_only=*/true, NET_IPV4).first.IsValid());
199+
BOOST_CHECK(!addrman->Select(/*new_only=*/false, NET_IPV4).first.IsValid());
200+
201+
// add ipv4 address to the new table
202+
CNetAddr source = ResolveIP("252.2.2.2");
203+
CService addr1 = ResolveService("250.1.1.1", 8333);
204+
BOOST_CHECK(addrman->Add({CAddress(addr1, NODE_NONE)}, source));
205+
206+
BOOST_CHECK(addrman->Select(/*new_only=*/true, NET_IPV4).first == addr1);
207+
BOOST_CHECK(addrman->Select(/*new_only=*/false, NET_IPV4).first == addr1);
208+
BOOST_CHECK(!addrman->Select(/*new_only=*/false, NET_IPV6).first.IsValid());
209+
BOOST_CHECK(!addrman->Select(/*new_only=*/false, NET_ONION).first.IsValid());
210+
BOOST_CHECK(!addrman->Select(/*new_only=*/false, NET_I2P).first.IsValid());
211+
BOOST_CHECK(!addrman->Select(/*new_only=*/false, NET_CJDNS).first.IsValid());
212+
BOOST_CHECK(!addrman->Select(/*new_only=*/true, NET_CJDNS).first.IsValid());
213+
BOOST_CHECK(addrman->Select(/*new_only=*/false).first == addr1);
214+
215+
// add I2P address to the new table
216+
CAddress i2p_addr;
217+
i2p_addr.SetSpecial("udhdrtrcetjm5sxzskjyr5ztpeszydbh4dpl3pl4utgqqw2v4jna.b32.i2p");
218+
BOOST_CHECK(addrman->Add({i2p_addr}, source));
219+
220+
BOOST_CHECK(addrman->Select(/*new_only=*/true, NET_I2P).first == i2p_addr);
221+
BOOST_CHECK(addrman->Select(/*new_only=*/false, NET_I2P).first == i2p_addr);
222+
BOOST_CHECK(addrman->Select(/*new_only=*/false, NET_IPV4).first == addr1);
223+
BOOST_CHECK(!addrman->Select(/*new_only=*/false, NET_IPV6).first.IsValid());
224+
BOOST_CHECK(!addrman->Select(/*new_only=*/false, NET_ONION).first.IsValid());
225+
BOOST_CHECK(!addrman->Select(/*new_only=*/false, NET_CJDNS).first.IsValid());
226+
227+
// bump I2P address to tried table
228+
BOOST_CHECK(addrman->Good(i2p_addr));
229+
230+
BOOST_CHECK(!addrman->Select(/*new_only=*/true, NET_I2P).first.IsValid());
231+
BOOST_CHECK(addrman->Select(/*new_only=*/false, NET_I2P).first == i2p_addr);
232+
233+
// add another I2P address to the new table
234+
CAddress i2p_addr2;
235+
i2p_addr2.SetSpecial("c4gfnttsuwqomiygupdqqqyy5y5emnk5c73hrfvatri67prd7vyq.b32.i2p");
236+
BOOST_CHECK(addrman->Add({i2p_addr2}, source));
237+
238+
BOOST_CHECK(addrman->Select(/*new_only=*/true, NET_I2P).first == i2p_addr2);
239+
240+
// ensure that both new and tried table are selected from
241+
bool new_selected{false};
242+
bool tried_selected{false};
243+
244+
while (!new_selected || !tried_selected) {
245+
const CAddress selected{addrman->Select(/*new_only=*/false, NET_I2P).first};
246+
BOOST_REQUIRE(selected == i2p_addr || selected == i2p_addr2);
247+
if (selected == i2p_addr) {
248+
tried_selected = true;
249+
} else {
250+
new_selected = true;
251+
}
252+
}
253+
}
254+
195255
BOOST_AUTO_TEST_CASE(addrman_new_collisions)
196256
{
197257
auto addrman = std::make_unique<AddrMan>(EMPTY_NETGROUPMAN, DETERMINISTIC, GetCheckRatio(m_node));

0 commit comments

Comments
 (0)