From ba8997fb2eda73603ce457bfec668cb7e0acbc89 Mon Sep 17 00:00:00 2001 From: Jon Atack Date: Mon, 12 Oct 2020 09:31:08 +0200 Subject: [PATCH 1/3] net: update GetNetworkName() with all enum Network cases --- src/netbase.cpp | 16 +++++++++++----- 1 file changed, 11 insertions(+), 5 deletions(-) diff --git a/src/netbase.cpp b/src/netbase.cpp index 0273839017..264029d8a2 100644 --- a/src/netbase.cpp +++ b/src/netbase.cpp @@ -52,14 +52,20 @@ enum Network ParseNetwork(const std::string& net_in) { return NET_UNROUTABLE; } -std::string GetNetworkName(enum Network net) { - switch(net) - { +std::string GetNetworkName(enum Network net) +{ + switch (net) { + case NET_UNROUTABLE: return "unroutable"; case NET_IPV4: return "ipv4"; case NET_IPV6: return "ipv6"; case NET_ONION: return "onion"; - default: return ""; - } + case NET_I2P: return "i2p"; + case NET_CJDNS: return "cjdns"; + case NET_INTERNAL: return "internal"; + case NET_MAX: assert(false); + } // no default case, so the compiler can warn about missing cases + + assert(false); } bool static LookupIntern(const std::string& name, std::vector& vIP, unsigned int nMaxSolutions, bool fAllowLookup) From 9a75e1e5697476058b56cd8014a36de31bfecd4c Mon Sep 17 00:00:00 2001 From: Jon Atack Date: Sun, 11 Oct 2020 11:49:08 +0200 Subject: [PATCH 2/3] rpc: update GetNetworksInfo() to not return unsupported networks --- src/rpc/net.cpp | 6 ++---- 1 file changed, 2 insertions(+), 4 deletions(-) diff --git a/src/rpc/net.cpp b/src/rpc/net.cpp index 43c525b6a0..ea08ddfeda 100644 --- a/src/rpc/net.cpp +++ b/src/rpc/net.cpp @@ -486,11 +486,9 @@ static RPCHelpMan getnettotals() static UniValue GetNetworksInfo() { UniValue networks(UniValue::VARR); - for(int n=0; n(n); - if(network == NET_UNROUTABLE || network == NET_INTERNAL) - continue; + if (network == NET_UNROUTABLE || network == NET_I2P || network == NET_CJDNS || network == NET_INTERNAL) continue; proxyType proxy; UniValue obj(UniValue::VOBJ); GetProxy(network, proxy); From 7b5bd3102e06f7ff34b5d0f1d45a005560f265a5 Mon Sep 17 00:00:00 2001 From: Jon Atack Date: Sun, 11 Oct 2020 11:49:25 +0200 Subject: [PATCH 3/3] test: add getnetworkinfo network name regression tests --- test/functional/feature_proxy.py | 30 ++++++++++++++++++++---------- 1 file changed, 20 insertions(+), 10 deletions(-) diff --git a/test/functional/feature_proxy.py b/test/functional/feature_proxy.py index dfae58e860..05b658ed87 100755 --- a/test/functional/feature_proxy.py +++ b/test/functional/feature_proxy.py @@ -26,6 +26,8 @@ addnode connect to IPv6 addnode connect to onion addnode connect to generic DNS name + +- Test getnetworkinfo for each node """ import socket @@ -41,12 +43,16 @@ from test_framework.netutil import test_ipv6_local RANGE_BEGIN = PORT_MIN + 2 * PORT_RANGE # Start after p2p and rpc ports -# From GetNetworkName() in netbase.cpp: -NET_UNROUTABLE = "" + +# Networks returned by RPC getpeerinfo, defined in src/netbase.cpp::GetNetworkName() +NET_UNROUTABLE = "unroutable" NET_IPV4 = "ipv4" NET_IPV6 = "ipv6" NET_ONION = "onion" +# Networks returned by RPC getnetworkinfo, defined in src/rpc/net.cpp::GetNetworksInfo() +NETWORKS = frozenset({NET_IPV4, NET_IPV6, NET_ONION}) + class ProxyTest(BitcoinTestFramework): def set_test_params(self): @@ -84,14 +90,14 @@ def setup_nodes(self): self.serv3 = Socks5Server(self.conf3) self.serv3.start() - # Note: proxies are not used to connect to local nodes - # this is because the proxy to use is based on CService.GetNetwork(), which return NET_UNROUTABLE for localhost + # Note: proxies are not used to connect to local nodes. This is because the proxy to + # use is based on CService.GetNetwork(), which returns NET_UNROUTABLE for localhost. args = [ ['-listen', '-proxy=%s:%i' % (self.conf1.addr),'-proxyrandomize=1'], ['-listen', '-proxy=%s:%i' % (self.conf1.addr),'-onion=%s:%i' % (self.conf2.addr),'-proxyrandomize=0'], ['-listen', '-proxy=%s:%i' % (self.conf2.addr),'-proxyrandomize=1'], [] - ] + ] if self.have_ipv6: args[3] = ['-listen', '-proxy=[%s]:%i' % (self.conf3.addr),'-proxyrandomize=0', '-noonion'] self.add_nodes(self.num_nodes, extra_args=args) @@ -189,15 +195,17 @@ def networks_dict(d): r[x['name']] = x return r - # test RPC getnetworkinfo + self.log.info("Test RPC getnetworkinfo") n0 = networks_dict(self.nodes[0].getnetworkinfo()) - for net in ['ipv4','ipv6','onion']: + assert_equal(NETWORKS, n0.keys()) + for net in NETWORKS: assert_equal(n0[net]['proxy'], '%s:%i' % (self.conf1.addr)) assert_equal(n0[net]['proxy_randomize_credentials'], True) assert_equal(n0['onion']['reachable'], True) n1 = networks_dict(self.nodes[1].getnetworkinfo()) - for net in ['ipv4','ipv6']: + assert_equal(NETWORKS, n1.keys()) + for net in ['ipv4', 'ipv6']: assert_equal(n1[net]['proxy'], '%s:%i' % (self.conf1.addr)) assert_equal(n1[net]['proxy_randomize_credentials'], False) assert_equal(n1['onion']['proxy'], '%s:%i' % (self.conf2.addr)) @@ -205,14 +213,16 @@ def networks_dict(d): assert_equal(n1['onion']['reachable'], True) n2 = networks_dict(self.nodes[2].getnetworkinfo()) - for net in ['ipv4','ipv6','onion']: + assert_equal(NETWORKS, n2.keys()) + for net in NETWORKS: assert_equal(n2[net]['proxy'], '%s:%i' % (self.conf2.addr)) assert_equal(n2[net]['proxy_randomize_credentials'], True) assert_equal(n2['onion']['reachable'], True) if self.have_ipv6: n3 = networks_dict(self.nodes[3].getnetworkinfo()) - for net in ['ipv4','ipv6']: + assert_equal(NETWORKS, n3.keys()) + for net in NETWORKS: assert_equal(n3[net]['proxy'], '[%s]:%i' % (self.conf3.addr)) assert_equal(n3[net]['proxy_randomize_credentials'], False) assert_equal(n3['onion']['reachable'], False)