Skip to content

Commit 854ca85

Browse files
committed
Merge #15051: Tests: IsReachable is the inverse of IsLimited (DRY). Includes unit tests
6dc4593 IsReachable is the inverse of IsLimited (DRY). Includes unit tests (marcaiaf) Pull request description: IsReachable is the inverse of IsLimited, but the implementation is duplicated (DRY) - Changed the implementation accordingly. - Added unit tests to document behavior and relationship - My modification in net.cpp applies only to IsReachable. - Applied clang-format-diffpy Created new pull request to avoid the mess with: bitcoin/bitcoin#15044 Checked with supposedly conflicting PRs mentioned in the old PR. No conflicts with the specific changes in this PR. Tree-SHA512: b132dec6cc2c788ebe4f63f228d78f441614e156743b17adebc990de0180a5872874d2724c86eeaa470b4521918bd137b0e33ebcaae77c5efc1f0d56104f6c87
2 parents 699d0bd + 6dc4593 commit 854ca85

File tree

2 files changed

+91
-4
lines changed

2 files changed

+91
-4
lines changed

src/net.cpp

Lines changed: 2 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -295,15 +295,13 @@ bool IsLocal(const CService& addr)
295295
/** check whether a given network is one we can probably connect to */
296296
bool IsReachable(enum Network net)
297297
{
298-
LOCK(cs_mapLocalHost);
299-
return !vfLimited[net];
298+
return !IsLimited(net);
300299
}
301300

302301
/** check whether a given address is in a network we can probably connect to */
303302
bool IsReachable(const CNetAddr& addr)
304303
{
305-
enum Network net = addr.GetNetwork();
306-
return IsReachable(net);
304+
return IsReachable(addr.GetNetwork());
307305
}
308306

309307

src/test/net_tests.cpp

Lines changed: 89 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -227,4 +227,93 @@ BOOST_AUTO_TEST_CASE(ipv4_peer_with_ipv6_addrMe_test)
227227
BOOST_CHECK(1);
228228
}
229229

230+
231+
BOOST_AUTO_TEST_CASE(LimitedAndReachable_Network)
232+
{
233+
SetLimited(NET_IPV4, true);
234+
SetLimited(NET_IPV6, true);
235+
SetLimited(NET_ONION, true);
236+
237+
BOOST_CHECK_EQUAL(IsLimited(NET_IPV4), true);
238+
BOOST_CHECK_EQUAL(IsLimited(NET_IPV6), true);
239+
BOOST_CHECK_EQUAL(IsLimited(NET_ONION), true);
240+
241+
BOOST_CHECK_EQUAL(IsReachable(NET_IPV4), false);
242+
BOOST_CHECK_EQUAL(IsReachable(NET_IPV6), false);
243+
BOOST_CHECK_EQUAL(IsReachable(NET_ONION), false);
244+
245+
246+
SetLimited(NET_IPV4, false);
247+
SetLimited(NET_IPV6, false);
248+
SetLimited(NET_ONION, false);
249+
250+
BOOST_CHECK_EQUAL(IsLimited(NET_IPV4), false);
251+
BOOST_CHECK_EQUAL(IsLimited(NET_IPV6), false);
252+
BOOST_CHECK_EQUAL(IsLimited(NET_ONION), false);
253+
254+
BOOST_CHECK_EQUAL(IsReachable(NET_IPV4), true);
255+
BOOST_CHECK_EQUAL(IsReachable(NET_IPV6), true);
256+
BOOST_CHECK_EQUAL(IsReachable(NET_ONION), true);
257+
}
258+
259+
BOOST_AUTO_TEST_CASE(LimitedAndReachable_NetworkCaseUnroutableAndInternal)
260+
{
261+
BOOST_CHECK_EQUAL(IsLimited(NET_UNROUTABLE), false);
262+
BOOST_CHECK_EQUAL(IsLimited(NET_INTERNAL), false);
263+
264+
BOOST_CHECK_EQUAL(IsReachable(NET_UNROUTABLE), true);
265+
BOOST_CHECK_EQUAL(IsReachable(NET_INTERNAL), true);
266+
267+
SetLimited(NET_UNROUTABLE, true);
268+
SetLimited(NET_INTERNAL, true);
269+
270+
BOOST_CHECK_EQUAL(IsLimited(NET_UNROUTABLE), false); // Ignored for both networks
271+
BOOST_CHECK_EQUAL(IsLimited(NET_INTERNAL), false);
272+
273+
BOOST_CHECK_EQUAL(IsReachable(NET_UNROUTABLE), true);
274+
BOOST_CHECK_EQUAL(IsReachable(NET_INTERNAL), true);
275+
}
276+
277+
CNetAddr UtilBuildAddress(unsigned char p1, unsigned char p2, unsigned char p3, unsigned char p4)
278+
{
279+
unsigned char ip[] = {p1, p2, p3, p4};
280+
281+
struct sockaddr_in sa;
282+
memset(&sa, 0, sizeof(sockaddr_in)); // initialize the memory block
283+
memcpy(&(sa.sin_addr), &ip, sizeof(ip));
284+
return CNetAddr(sa.sin_addr);
285+
}
286+
287+
288+
BOOST_AUTO_TEST_CASE(LimitedAndReachable_CNetAddr)
289+
{
290+
CNetAddr addr = UtilBuildAddress(0x001, 0x001, 0x001, 0x001); // 1.1.1.1
291+
292+
SetLimited(NET_IPV4, false);
293+
BOOST_CHECK_EQUAL(IsLimited(addr), false);
294+
BOOST_CHECK_EQUAL(IsReachable(addr), true);
295+
296+
SetLimited(NET_IPV4, true);
297+
BOOST_CHECK_EQUAL(IsLimited(addr), true);
298+
BOOST_CHECK_EQUAL(IsReachable(addr), false);
299+
300+
SetLimited(NET_IPV4, false); // have to reset this, because this is stateful.
301+
}
302+
303+
304+
BOOST_AUTO_TEST_CASE(LocalAddress_BasicLifecycle)
305+
{
306+
CService addr = CService(UtilBuildAddress(0x002, 0x001, 0x001, 0x001), 1000); // 2.1.1.1:1000
307+
308+
SetLimited(NET_IPV4, false);
309+
310+
BOOST_CHECK_EQUAL(IsLocal(addr), false);
311+
BOOST_CHECK_EQUAL(AddLocal(addr, 1000), true);
312+
BOOST_CHECK_EQUAL(IsLocal(addr), true);
313+
314+
RemoveLocal(addr);
315+
BOOST_CHECK_EQUAL(IsLocal(addr), false);
316+
}
317+
318+
230319
BOOST_AUTO_TEST_SUITE_END()

0 commit comments

Comments
 (0)