Skip to content

Commit fb1416f

Browse files
committed
merge bitcoin#24205: improve network reachability test coverage and safety
1 parent 7cb7479 commit fb1416f

File tree

3 files changed

+41
-5
lines changed

3 files changed

+41
-5
lines changed

src/init.cpp

+1
Original file line numberDiff line numberDiff line change
@@ -1781,6 +1781,7 @@ bool AppInitMain(const CoreContext& context, NodeContext& node, interfaces::Bloc
17811781
}
17821782
for (int n = 0; n < NET_MAX; n++) {
17831783
enum Network net = (enum Network)n;
1784+
assert(IsReachable(net));
17841785
if (!nets.count(net))
17851786
SetReachable(net, false);
17861787
}

src/test/net_tests.cpp

+5
Original file line numberDiff line numberDiff line change
@@ -765,26 +765,31 @@ BOOST_AUTO_TEST_CASE(LimitedAndReachable_Network)
765765
BOOST_CHECK(IsReachable(NET_IPV6));
766766
BOOST_CHECK(IsReachable(NET_ONION));
767767
BOOST_CHECK(IsReachable(NET_I2P));
768+
BOOST_CHECK(IsReachable(NET_CJDNS));
768769

769770
SetReachable(NET_IPV4, false);
770771
SetReachable(NET_IPV6, false);
771772
SetReachable(NET_ONION, false);
772773
SetReachable(NET_I2P, false);
774+
SetReachable(NET_CJDNS, false);
773775

774776
BOOST_CHECK(!IsReachable(NET_IPV4));
775777
BOOST_CHECK(!IsReachable(NET_IPV6));
776778
BOOST_CHECK(!IsReachable(NET_ONION));
777779
BOOST_CHECK(!IsReachable(NET_I2P));
780+
BOOST_CHECK(!IsReachable(NET_CJDNS));
778781

779782
SetReachable(NET_IPV4, true);
780783
SetReachable(NET_IPV6, true);
781784
SetReachable(NET_ONION, true);
782785
SetReachable(NET_I2P, true);
786+
SetReachable(NET_CJDNS, true);
783787

784788
BOOST_CHECK(IsReachable(NET_IPV4));
785789
BOOST_CHECK(IsReachable(NET_IPV6));
786790
BOOST_CHECK(IsReachable(NET_ONION));
787791
BOOST_CHECK(IsReachable(NET_I2P));
792+
BOOST_CHECK(IsReachable(NET_CJDNS));
788793
}
789794

790795
BOOST_AUTO_TEST_CASE(LimitedAndReachable_NetworkCaseUnroutableAndInternal)

test/functional/feature_proxy.py

+35-5
Original file line numberDiff line numberDiff line change
@@ -30,6 +30,11 @@
3030
addnode connect to a CJDNS address
3131
3232
- Test getnetworkinfo for each node
33+
34+
- Test passing invalid -proxy
35+
- Test passing invalid -onion
36+
- Test passing -onlynet=onion without -proxy or -onion
37+
- Test passing -onlynet=onion with -onion=0 and with -noonion
3338
"""
3439

3540
import socket
@@ -263,12 +268,13 @@ def networks_dict(d):
263268

264269
n2 = networks_dict(self.nodes[2].getnetworkinfo())
265270
assert_equal(NETWORKS, n2.keys())
271+
proxy = f'{self.conf2.addr[0]}:{self.conf2.addr[1]}'
266272
for net in NETWORKS:
267273
if net == NET_I2P:
268274
expected_proxy = ''
269275
expected_randomize = False
270276
else:
271-
expected_proxy = f'{self.conf2.addr[0]}:{self.conf2.addr[1]}'
277+
expected_proxy = proxy
272278
expected_randomize = True
273279
assert_equal(n2[net]['proxy'], expected_proxy)
274280
assert_equal(n2[net]['proxy_randomize_credentials'], expected_randomize)
@@ -279,11 +285,9 @@ def networks_dict(d):
279285
if self.have_ipv6:
280286
n3 = networks_dict(self.nodes[3].getnetworkinfo())
281287
assert_equal(NETWORKS, n3.keys())
288+
proxy = f'[{self.conf3.addr[0]}]:{self.conf3.addr[1]}'
282289
for net in NETWORKS:
283-
if net == NET_I2P or net == NET_ONION:
284-
expected_proxy = ''
285-
else:
286-
expected_proxy = f'[{self.conf3.addr[0]}]:{self.conf3.addr[1]}'
290+
expected_proxy = '' if net == NET_I2P or net == NET_ONION else proxy
287291
assert_equal(n3[net]['proxy'], expected_proxy)
288292
assert_equal(n3[net]['proxy_randomize_credentials'], False)
289293
assert_equal(n3['onion']['reachable'], False)
@@ -305,6 +309,32 @@ def networks_dict(d):
305309
assert_equal(n4['i2p']['reachable'], False)
306310
assert_equal(n4['cjdns']['reachable'], True)
307311

312+
self.stop_node(1)
313+
314+
self.log.info("Test passing invalid -proxy raises expected init error")
315+
self.nodes[1].extra_args = ["-proxy=abc:def"]
316+
msg = "Error: Invalid -proxy address or hostname: 'abc:def'"
317+
self.nodes[1].assert_start_raises_init_error(expected_msg=msg)
318+
319+
self.log.info("Test passing invalid -onion raises expected init error")
320+
self.nodes[1].extra_args = ["-onion=xyz:abc"]
321+
msg = "Error: Invalid -onion address or hostname: 'xyz:abc'"
322+
self.nodes[1].assert_start_raises_init_error(expected_msg=msg)
323+
324+
msg = (
325+
"Error: Outbound connections restricted to Tor (-onlynet=onion) but "
326+
"the proxy for reaching the Tor network is not provided (no -proxy= "
327+
"and no -onion= given) or it is explicitly forbidden (-onion=0)"
328+
)
329+
self.log.info("Test passing -onlynet=onion without -proxy or -onion raises expected init error")
330+
self.nodes[1].extra_args = ["-onlynet=onion"]
331+
self.nodes[1].assert_start_raises_init_error(expected_msg=msg)
332+
333+
self.log.info("Test passing -onlynet=onion with -onion=0/-noonion raises expected init error")
334+
for arg in ["-onion=0", "-noonion"]:
335+
self.nodes[1].extra_args = ["-onlynet=onion", arg]
336+
self.nodes[1].assert_start_raises_init_error(expected_msg=msg)
337+
308338

309339
if __name__ == '__main__':
310340
ProxyTest().main()

0 commit comments

Comments
 (0)