From 993d1ecd191a7d9161082d4026f020cbf00835bb Mon Sep 17 00:00:00 2001 From: Jon Atack Date: Thu, 17 Dec 2020 16:33:56 +0100 Subject: test, fuzz: fix constructing CNode with invalid inbound_onion as CNode ctor should only be passed inbound_onion = true when the connection is inbound --- src/test/fuzz/util.h | 2 +- src/test/net_tests.cpp | 2 +- 2 files changed, 2 insertions(+), 2 deletions(-) (limited to 'src') diff --git a/src/test/fuzz/util.h b/src/test/fuzz/util.h index cf666a8b93..8a2de0f9c9 100644 --- a/src/test/fuzz/util.h +++ b/src/test/fuzz/util.h @@ -298,7 +298,7 @@ CNode ConsumeNode(FuzzedDataProvider& fuzzed_data_provider) noexcept const CAddress addr_bind = ConsumeAddress(fuzzed_data_provider); const std::string addr_name = fuzzed_data_provider.ConsumeRandomLengthString(64); const ConnectionType conn_type = fuzzed_data_provider.PickValueInArray({ConnectionType::INBOUND, ConnectionType::OUTBOUND_FULL_RELAY, ConnectionType::MANUAL, ConnectionType::FEELER, ConnectionType::BLOCK_RELAY, ConnectionType::ADDR_FETCH}); - const bool inbound_onion = fuzzed_data_provider.ConsumeBool(); + const bool inbound_onion{conn_type == ConnectionType::INBOUND ? fuzzed_data_provider.ConsumeBool() : false}; return {node_id, local_services, my_starting_height, socket, address, keyed_net_group, local_host_nonce, addr_bind, addr_name, conn_type, inbound_onion}; } diff --git a/src/test/net_tests.cpp b/src/test/net_tests.cpp index beac65942e..eb2f4cee69 100644 --- a/src/test/net_tests.cpp +++ b/src/test/net_tests.cpp @@ -221,7 +221,7 @@ BOOST_AUTO_TEST_CASE(cnode_simple_test) /* nKeyedNetGroupIn = */ 0, /* nLocalHostNonceIn = */ 0, CAddress(), pszDest, ConnectionType::OUTBOUND_FULL_RELAY, - /* inbound_onion = */ true); + /* inbound_onion = */ false); BOOST_CHECK(pnode3->IsFullOutboundConn() == true); BOOST_CHECK(pnode3->IsManualConn() == false); BOOST_CHECK(pnode3->IsBlockOnlyConn() == false); -- cgit v1.2.3 From 6609eb8cb50fe92c7317b5db9e72d4333b3aab1b Mon Sep 17 00:00:00 2001 From: Jon Atack Date: Wed, 21 Oct 2020 11:52:19 +0200 Subject: net: assert CNode::m_inbound_onion is inbound in ctor and drop an unneeded check in CNode::ConnectedThroughNetwork() --- src/net.cpp | 3 ++- src/net.h | 2 +- 2 files changed, 3 insertions(+), 2 deletions(-) (limited to 'src') diff --git a/src/net.cpp b/src/net.cpp index b3c521116b..cf1ac35712 100644 --- a/src/net.cpp +++ b/src/net.cpp @@ -555,7 +555,7 @@ void CNode::SetAddrLocal(const CService& addrLocalIn) { Network CNode::ConnectedThroughNetwork() const { - return IsInboundConn() && m_inbound_onion ? NET_ONION : addr.GetNetClass(); + return m_inbound_onion ? NET_ONION : addr.GetNetClass(); } #undef X @@ -2954,6 +2954,7 @@ CNode::CNode(NodeId idIn, ServiceFlags nLocalServicesIn, int nMyStartingHeightIn nMyStartingHeight(nMyStartingHeightIn), m_inbound_onion(inbound_onion) { + if (inbound_onion) assert(conn_type_in == ConnectionType::INBOUND); hSocket = hSocketIn; addrName = addrNameIn == "" ? addr.ToStringIPPort() : addrNameIn; hashContinue = uint256(); diff --git a/src/net.h b/src/net.h index b7c45abb09..ca5944d46b 100644 --- a/src/net.h +++ b/src/net.h @@ -1110,7 +1110,7 @@ private: CService addrLocal GUARDED_BY(cs_addrLocal); mutable RecursiveMutex cs_addrLocal; - //! Whether this peer connected via our Tor onion service. + //! Whether this peer is an inbound onion, e.g. connected via our Tor onion service. const bool m_inbound_onion{false}; public: -- cgit v1.2.3 From 86c495223f048e5ca2cf0d8730af7db3b76f7aba Mon Sep 17 00:00:00 2001 From: Jon Atack Date: Wed, 21 Oct 2020 11:53:38 +0200 Subject: net: add CNode::IsInboundOnion() public getter and unit tests --- src/net.h | 3 +++ src/test/net_tests.cpp | 4 ++++ 2 files changed, 7 insertions(+) (limited to 'src') diff --git a/src/net.h b/src/net.h index ca5944d46b..0b7d0f406c 100644 --- a/src/net.h +++ b/src/net.h @@ -1234,6 +1234,9 @@ public: void MaybeSetAddrName(const std::string& addrNameIn); std::string ConnectionTypeAsString() const; + + /** Whether this peer is an inbound onion, e.g. connected via our Tor onion service. */ + bool IsInboundOnion() const { return m_inbound_onion; } }; /** Return a timestamp in the future (in microseconds) for exponentially distributed events. */ diff --git a/src/test/net_tests.cpp b/src/test/net_tests.cpp index eb2f4cee69..f6adc6a28d 100644 --- a/src/test/net_tests.cpp +++ b/src/test/net_tests.cpp @@ -200,6 +200,7 @@ BOOST_AUTO_TEST_CASE(cnode_simple_test) BOOST_CHECK(pnode1->IsFeelerConn() == false); BOOST_CHECK(pnode1->IsAddrFetchConn() == false); BOOST_CHECK(pnode1->IsInboundConn() == false); + BOOST_CHECK(pnode1->IsInboundOnion() == false); BOOST_CHECK_EQUAL(pnode1->ConnectedThroughNetwork(), Network::NET_IPV4); std::unique_ptr pnode2 = MakeUnique( @@ -214,6 +215,7 @@ BOOST_AUTO_TEST_CASE(cnode_simple_test) BOOST_CHECK(pnode2->IsFeelerConn() == false); BOOST_CHECK(pnode2->IsAddrFetchConn() == false); BOOST_CHECK(pnode2->IsInboundConn() == true); + BOOST_CHECK(pnode2->IsInboundOnion() == false); BOOST_CHECK_EQUAL(pnode2->ConnectedThroughNetwork(), Network::NET_IPV4); std::unique_ptr pnode3 = MakeUnique( @@ -228,6 +230,7 @@ BOOST_AUTO_TEST_CASE(cnode_simple_test) BOOST_CHECK(pnode3->IsFeelerConn() == false); BOOST_CHECK(pnode3->IsAddrFetchConn() == false); BOOST_CHECK(pnode3->IsInboundConn() == false); + BOOST_CHECK(pnode3->IsInboundOnion() == false); BOOST_CHECK_EQUAL(pnode3->ConnectedThroughNetwork(), Network::NET_IPV4); std::unique_ptr pnode4 = MakeUnique( @@ -242,6 +245,7 @@ BOOST_AUTO_TEST_CASE(cnode_simple_test) BOOST_CHECK(pnode4->IsFeelerConn() == false); BOOST_CHECK(pnode4->IsAddrFetchConn() == false); BOOST_CHECK(pnode4->IsInboundConn() == true); + BOOST_CHECK(pnode4->IsInboundOnion() == true); BOOST_CHECK_EQUAL(pnode4->ConnectedThroughNetwork(), Network::NET_ONION); } -- cgit v1.2.3