From 2b7a8180a94738c2fcb21232a2eca07a7b27656d Mon Sep 17 00:00:00 2001 From: Jon Atack Date: Sun, 30 Jan 2022 22:46:25 +0100 Subject: [PATCH 1/7] net, init: assert each network reachability is true by default The default network reachability values are implicitly set by this line in net.cpp: static bool vfLimited[NET_MAX] GUARDED_BY(g_maplocalhost_mutex) = {}; This commit asserts that each network is reachable during the first loop through them during bitcoind init. --- src/init.cpp | 1 + 1 file changed, 1 insertion(+) diff --git a/src/init.cpp b/src/init.cpp index ce666534aed..89418bc1f70 100644 --- a/src/init.cpp +++ b/src/init.cpp @@ -1301,6 +1301,7 @@ bool AppInitMain(NodeContext& node, interfaces::BlockAndHeaderTipInfo* tip_info) } for (int n = 0; n < NET_MAX; n++) { enum Network net = (enum Network)n; + assert(IsReachable(net)); if (!nets.count(net)) SetReachable(net, false); } From afdf2de28296660fd0284453a241aece8494eea8 Mon Sep 17 00:00:00 2001 From: Jon Atack Date: Sun, 30 Jan 2022 22:47:10 +0100 Subject: [PATCH 2/7] test: add CJDNS to LimitedAndReachable_Network unit tests --- src/test/net_tests.cpp | 5 +++++ 1 file changed, 5 insertions(+) diff --git a/src/test/net_tests.cpp b/src/test/net_tests.cpp index 908b030eeac..c6bffe2f39e 100644 --- a/src/test/net_tests.cpp +++ b/src/test/net_tests.cpp @@ -655,26 +655,31 @@ BOOST_AUTO_TEST_CASE(LimitedAndReachable_Network) BOOST_CHECK(IsReachable(NET_IPV6)); BOOST_CHECK(IsReachable(NET_ONION)); BOOST_CHECK(IsReachable(NET_I2P)); + BOOST_CHECK(IsReachable(NET_CJDNS)); SetReachable(NET_IPV4, false); SetReachable(NET_IPV6, false); SetReachable(NET_ONION, false); SetReachable(NET_I2P, false); + SetReachable(NET_CJDNS, false); BOOST_CHECK(!IsReachable(NET_IPV4)); BOOST_CHECK(!IsReachable(NET_IPV6)); BOOST_CHECK(!IsReachable(NET_ONION)); BOOST_CHECK(!IsReachable(NET_I2P)); + BOOST_CHECK(!IsReachable(NET_CJDNS)); SetReachable(NET_IPV4, true); SetReachable(NET_IPV6, true); SetReachable(NET_ONION, true); SetReachable(NET_I2P, true); + SetReachable(NET_CJDNS, true); BOOST_CHECK(IsReachable(NET_IPV4)); BOOST_CHECK(IsReachable(NET_IPV6)); BOOST_CHECK(IsReachable(NET_ONION)); BOOST_CHECK(IsReachable(NET_I2P)); + BOOST_CHECK(IsReachable(NET_CJDNS)); } BOOST_AUTO_TEST_CASE(LimitedAndReachable_NetworkCaseUnroutableAndInternal) From bd57dcbaf2b5e5f50833912c894a1f1239ceb25b Mon Sep 17 00:00:00 2001 From: Jon Atack Date: Sun, 30 Jan 2022 22:48:37 +0100 Subject: [PATCH 3/7] test: hoist proxy out of 2 network loops in feature_proxy.py --- test/functional/feature_proxy.py | 9 ++++----- 1 file changed, 4 insertions(+), 5 deletions(-) diff --git a/test/functional/feature_proxy.py b/test/functional/feature_proxy.py index fb0f6d7cb7b..3a6831cd1cc 100755 --- a/test/functional/feature_proxy.py +++ b/test/functional/feature_proxy.py @@ -263,12 +263,13 @@ class ProxyTest(BitcoinTestFramework): n2 = networks_dict(self.nodes[2].getnetworkinfo()) assert_equal(NETWORKS, n2.keys()) + proxy = f'{self.conf2.addr[0]}:{self.conf2.addr[1]}' for net in NETWORKS: if net == NET_I2P: expected_proxy = '' expected_randomize = False else: - expected_proxy = f'{self.conf2.addr[0]}:{self.conf2.addr[1]}' + expected_proxy = proxy expected_randomize = True assert_equal(n2[net]['proxy'], expected_proxy) assert_equal(n2[net]['proxy_randomize_credentials'], expected_randomize) @@ -279,11 +280,9 @@ class ProxyTest(BitcoinTestFramework): if self.have_ipv6: n3 = networks_dict(self.nodes[3].getnetworkinfo()) assert_equal(NETWORKS, n3.keys()) + proxy = f'[{self.conf3.addr[0]}]:{self.conf3.addr[1]}' for net in NETWORKS: - if net == NET_I2P or net == NET_ONION: - expected_proxy = '' - else: - expected_proxy = f'[{self.conf3.addr[0]}]:{self.conf3.addr[1]}' + expected_proxy = '' if net == NET_I2P or net == NET_ONION else proxy assert_equal(n3[net]['proxy'], expected_proxy) assert_equal(n3[net]['proxy_randomize_credentials'], False) assert_equal(n3['onion']['reachable'], False) From d5edb087082a50e6f7d413c3b43fdf1e6a20d29b Mon Sep 17 00:00:00 2001 From: Jon Atack Date: Tue, 1 Mar 2022 22:18:32 +0100 Subject: [PATCH 4/7] test: passing invalid -proxy raises expected init error --- test/functional/feature_proxy.py | 9 +++++++++ 1 file changed, 9 insertions(+) diff --git a/test/functional/feature_proxy.py b/test/functional/feature_proxy.py index 3a6831cd1cc..7387c763678 100755 --- a/test/functional/feature_proxy.py +++ b/test/functional/feature_proxy.py @@ -30,6 +30,8 @@ addnode connect to generic DNS name addnode connect to a CJDNS address - Test getnetworkinfo for each node + +- Test passing invalid -proxy """ import socket @@ -304,6 +306,13 @@ class ProxyTest(BitcoinTestFramework): assert_equal(n4['i2p']['reachable'], False) assert_equal(n4['cjdns']['reachable'], True) + self.stop_node(1) + + self.log.info("Test passing invalid -proxy raises expected init error") + self.nodes[1].extra_args = ["-proxy=abc:def"] + msg = "Error: Invalid -proxy address or hostname: 'abc:def'" + self.nodes[1].assert_start_raises_init_error(expected_msg=msg) + if __name__ == '__main__': ProxyTest().main() From 8332e6e4cf45455fea0bf1f7527256cdb7bb1e6d Mon Sep 17 00:00:00 2001 From: Jon Atack Date: Tue, 1 Mar 2022 22:45:48 +0100 Subject: [PATCH 5/7] test: passing invalid -onion raises expected init error --- test/functional/feature_proxy.py | 6 ++++++ 1 file changed, 6 insertions(+) diff --git a/test/functional/feature_proxy.py b/test/functional/feature_proxy.py index 7387c763678..fa047ba9289 100755 --- a/test/functional/feature_proxy.py +++ b/test/functional/feature_proxy.py @@ -32,6 +32,7 @@ addnode connect to a CJDNS address - Test getnetworkinfo for each node - Test passing invalid -proxy +- Test passing invalid -onion """ import socket @@ -313,6 +314,11 @@ class ProxyTest(BitcoinTestFramework): msg = "Error: Invalid -proxy address or hostname: 'abc:def'" self.nodes[1].assert_start_raises_init_error(expected_msg=msg) + self.log.info("Test passing invalid -onion raises expected init error") + self.nodes[1].extra_args = ["-onion=xyz:abc"] + msg = "Error: Invalid -onion address or hostname: 'xyz:abc'" + self.nodes[1].assert_start_raises_init_error(expected_msg=msg) + if __name__ == '__main__': ProxyTest().main() From 7000f66d367123d1de303fc15ce2ce60df379c11 Mon Sep 17 00:00:00 2001 From: Jon Atack Date: Tue, 1 Mar 2022 22:47:23 +0100 Subject: [PATCH 6/7] test: passing -onlynet=onion without -proxy/-onion raises expected init error --- test/functional/feature_proxy.py | 10 ++++++++++ 1 file changed, 10 insertions(+) diff --git a/test/functional/feature_proxy.py b/test/functional/feature_proxy.py index fa047ba9289..d23733c5d38 100755 --- a/test/functional/feature_proxy.py +++ b/test/functional/feature_proxy.py @@ -33,6 +33,7 @@ addnode connect to a CJDNS address - Test passing invalid -proxy - Test passing invalid -onion +- Test passing -onlynet=onion without -proxy or -onion """ import socket @@ -319,6 +320,15 @@ class ProxyTest(BitcoinTestFramework): msg = "Error: Invalid -onion address or hostname: 'xyz:abc'" self.nodes[1].assert_start_raises_init_error(expected_msg=msg) + msg = ( + "Error: Outbound connections restricted to Tor (-onlynet=onion) but " + "the proxy for reaching the Tor network is not provided (no -proxy= " + "and no -onion= given) or it is explicitly forbidden (-onion=0)" + ) + self.log.info("Test passing -onlynet=onion without -proxy or -onion raises expected init error") + self.nodes[1].extra_args = ["-onlynet=onion"] + self.nodes[1].assert_start_raises_init_error(expected_msg=msg) + if __name__ == '__main__': ProxyTest().main() From 58a14795b89a6bd812e0b71cb8b3088b8ab55c11 Mon Sep 17 00:00:00 2001 From: Jon Atack Date: Tue, 1 Mar 2022 22:49:02 +0100 Subject: [PATCH 7/7] test: passing -onlynet=onion with -onion=0/-noonion raises expected init error --- test/functional/feature_proxy.py | 6 ++++++ 1 file changed, 6 insertions(+) diff --git a/test/functional/feature_proxy.py b/test/functional/feature_proxy.py index d23733c5d38..14308b3fd15 100755 --- a/test/functional/feature_proxy.py +++ b/test/functional/feature_proxy.py @@ -34,6 +34,7 @@ addnode connect to a CJDNS address - Test passing invalid -proxy - Test passing invalid -onion - Test passing -onlynet=onion without -proxy or -onion +- Test passing -onlynet=onion with -onion=0 and with -noonion """ import socket @@ -329,6 +330,11 @@ class ProxyTest(BitcoinTestFramework): self.nodes[1].extra_args = ["-onlynet=onion"] self.nodes[1].assert_start_raises_init_error(expected_msg=msg) + self.log.info("Test passing -onlynet=onion with -onion=0/-noonion raises expected init error") + for arg in ["-onion=0", "-noonion"]: + self.nodes[1].extra_args = ["-onlynet=onion", arg] + self.nodes[1].assert_start_raises_init_error(expected_msg=msg) + if __name__ == '__main__': ProxyTest().main()