Branch data Line data Source code
1 : : // Copyright (c) 2012-present The Bitcoin Core developers
2 : : // Distributed under the MIT software license, see the accompanying
3 : : // file COPYING or http://www.opensource.org/licenses/mit-license.php.
4 : :
5 : : #include <addrman.h>
6 : : #include <bip324.h>
7 : : #include <chainparams.h>
8 : : #include <clientversion.h>
9 : : #include <common/args.h>
10 : : #include <compat/compat.h>
11 : : #include <net.h>
12 : : #include <net_processing.h>
13 : : #include <netaddress.h>
14 : : #include <netbase.h>
15 : : #include <netmessagemaker.h>
16 : : #include <node/protocol_version.h>
17 : : #include <serialize.h>
18 : : #include <span.h>
19 : : #include <streams.h>
20 : : #include <test/util/common.h>
21 : : #include <test/util/net.h>
22 : : #include <test/util/random.h>
23 : : #include <test/util/setup_common.h>
24 : : #include <test/util/validation.h>
25 : : #include <util/strencodings.h>
26 : : #include <util/string.h>
27 : : #include <validation.h>
28 : :
29 : : #include <boost/test/unit_test.hpp>
30 : :
31 : : #include <algorithm>
32 : : #include <cstdint>
33 : : #include <ios>
34 : : #include <memory>
35 : : #include <optional>
36 : : #include <string>
37 : :
38 : : using namespace std::literals;
39 : : using namespace util::hex_literals;
40 : : using util::ToString;
41 : :
42 : : BOOST_FIXTURE_TEST_SUITE(net_tests, RegTestingSetup)
43 : :
44 [ + - + - : 7 : BOOST_AUTO_TEST_CASE(cnode_listen_port)
+ - + - -
+ + - + -
+ - + - +
- + - - +
+ - + - +
- + - + -
+ - - + +
- + - + -
+ - + - +
- + - - +
+ - + - +
- + - + -
+ - - + +
- ]
45 : : {
46 : : // test default
47 : 1 : uint16_t port{GetListenPort()};
48 [ + - + - ]: 2 : BOOST_CHECK(port == Params().GetDefaultPort());
49 : : // test set port
50 : 1 : uint16_t altPort = 12345;
51 [ + - + - : 2 : BOOST_CHECK(gArgs.SoftSetArg("-port", ToString(altPort)));
+ - + - ]
52 : 1 : port = GetListenPort();
53 [ + - ]: 2 : BOOST_CHECK(port == altPort);
54 : 1 : }
55 : :
56 [ + - + - : 7 : BOOST_AUTO_TEST_CASE(cnode_simple_test)
+ - + - -
+ + - + -
+ - + - +
- + - - +
+ - + - +
- + - + -
+ - - + +
- + - + -
+ - + - +
- + - - +
+ - + - +
- + - + -
+ - - + +
- ]
57 : : {
58 : 1 : NodeId id = 0;
59 : :
60 : 1 : in_addr ipv4Addr;
61 : 1 : ipv4Addr.s_addr = 0xa0b0c001;
62 : :
63 : 1 : CAddress addr = CAddress(CService(ipv4Addr, 7777), NODE_NETWORK);
64 [ + - ]: 1 : std::string pszDest;
65 : :
66 : 2 : std::unique_ptr<CNode> pnode1 = std::make_unique<CNode>(id++,
67 : 1 : /*sock=*/nullptr,
68 : : addr,
69 : 1 : /*nKeyedNetGroupIn=*/0,
70 : 1 : /*nLocalHostNonceIn=*/0,
71 : 0 : CAddress(),
72 : : pszDest,
73 : 1 : ConnectionType::OUTBOUND_FULL_RELAY,
74 : 1 : /*inbound_onion=*/false,
75 [ + - + - ]: 1 : /*network_key=*/0);
76 [ + - + - : 2 : BOOST_CHECK(pnode1->IsFullOutboundConn() == true);
+ - ]
77 [ + - + - : 2 : BOOST_CHECK(pnode1->IsManualConn() == false);
+ - ]
78 [ + - + - : 2 : BOOST_CHECK(pnode1->IsBlockOnlyConn() == false);
+ - ]
79 [ + - + - : 2 : BOOST_CHECK(pnode1->IsFeelerConn() == false);
+ - ]
80 [ + - + - : 2 : BOOST_CHECK(pnode1->IsAddrFetchConn() == false);
+ - ]
81 [ + - + - : 2 : BOOST_CHECK(pnode1->IsInboundConn() == false);
+ - ]
82 [ + - + - : 2 : BOOST_CHECK(pnode1->m_inbound_onion == false);
+ - ]
83 [ + - + - : 1 : BOOST_CHECK_EQUAL(pnode1->ConnectedThroughNetwork(), Network::NET_IPV4);
+ - + - ]
84 : :
85 : 2 : std::unique_ptr<CNode> pnode2 = std::make_unique<CNode>(id++,
86 : 1 : /*sock=*/nullptr,
87 : : addr,
88 : 1 : /*nKeyedNetGroupIn=*/1,
89 : 1 : /*nLocalHostNonceIn=*/1,
90 : 0 : CAddress(),
91 : : pszDest,
92 : 1 : ConnectionType::INBOUND,
93 : 1 : /*inbound_onion=*/false,
94 [ + - + - ]: 1 : /*network_key=*/1);
95 [ + - + - : 2 : BOOST_CHECK(pnode2->IsFullOutboundConn() == false);
+ - ]
96 [ + - + - : 2 : BOOST_CHECK(pnode2->IsManualConn() == false);
+ - ]
97 [ + - + - : 2 : BOOST_CHECK(pnode2->IsBlockOnlyConn() == false);
+ - ]
98 [ + - + - : 2 : BOOST_CHECK(pnode2->IsFeelerConn() == false);
+ - ]
99 [ + - + - : 2 : BOOST_CHECK(pnode2->IsAddrFetchConn() == false);
+ - ]
100 [ + - + - : 2 : BOOST_CHECK(pnode2->IsInboundConn() == true);
+ - ]
101 [ + - + - : 2 : BOOST_CHECK(pnode2->m_inbound_onion == false);
+ - ]
102 [ + - + - : 1 : BOOST_CHECK_EQUAL(pnode2->ConnectedThroughNetwork(), Network::NET_IPV4);
+ - ]
103 : :
104 : 2 : std::unique_ptr<CNode> pnode3 = std::make_unique<CNode>(id++,
105 : 1 : /*sock=*/nullptr,
106 : : addr,
107 : 1 : /*nKeyedNetGroupIn=*/0,
108 : 1 : /*nLocalHostNonceIn=*/0,
109 : 0 : CAddress(),
110 : : pszDest,
111 : 1 : ConnectionType::OUTBOUND_FULL_RELAY,
112 : 1 : /*inbound_onion=*/false,
113 [ + - + - ]: 1 : /*network_key=*/2);
114 [ + - + - : 2 : BOOST_CHECK(pnode3->IsFullOutboundConn() == true);
+ - ]
115 [ + - + - : 2 : BOOST_CHECK(pnode3->IsManualConn() == false);
+ - ]
116 [ + - + - : 2 : BOOST_CHECK(pnode3->IsBlockOnlyConn() == false);
+ - ]
117 [ + - + - : 2 : BOOST_CHECK(pnode3->IsFeelerConn() == false);
+ - ]
118 [ + - + - : 2 : BOOST_CHECK(pnode3->IsAddrFetchConn() == false);
+ - ]
119 [ + - + - : 2 : BOOST_CHECK(pnode3->IsInboundConn() == false);
+ - ]
120 [ + - + - : 2 : BOOST_CHECK(pnode3->m_inbound_onion == false);
+ - ]
121 [ + - + - : 1 : BOOST_CHECK_EQUAL(pnode3->ConnectedThroughNetwork(), Network::NET_IPV4);
+ - ]
122 : :
123 : 2 : std::unique_ptr<CNode> pnode4 = std::make_unique<CNode>(id++,
124 : 1 : /*sock=*/nullptr,
125 : : addr,
126 : 1 : /*nKeyedNetGroupIn=*/1,
127 : 1 : /*nLocalHostNonceIn=*/1,
128 : 0 : CAddress(),
129 : : pszDest,
130 : 1 : ConnectionType::INBOUND,
131 : 1 : /*inbound_onion=*/true,
132 [ + - + - ]: 1 : /*network_key=*/3);
133 [ + - + - : 2 : BOOST_CHECK(pnode4->IsFullOutboundConn() == false);
+ - ]
134 [ + - + - : 2 : BOOST_CHECK(pnode4->IsManualConn() == false);
+ - ]
135 [ + - + - : 2 : BOOST_CHECK(pnode4->IsBlockOnlyConn() == false);
+ - ]
136 [ + - + - : 2 : BOOST_CHECK(pnode4->IsFeelerConn() == false);
+ - ]
137 [ + - + - : 2 : BOOST_CHECK(pnode4->IsAddrFetchConn() == false);
+ - ]
138 [ + - + - : 2 : BOOST_CHECK(pnode4->IsInboundConn() == true);
+ - ]
139 [ + - + - : 2 : BOOST_CHECK(pnode4->m_inbound_onion == true);
+ - ]
140 [ + - + - : 1 : BOOST_CHECK_EQUAL(pnode4->ConnectedThroughNetwork(), Network::NET_ONION);
+ - ]
141 : 1 : }
142 : :
143 [ + - + - : 7 : BOOST_AUTO_TEST_CASE(cnetaddr_basic)
+ - + - -
+ + - + -
+ - + - +
- + - - +
+ - + - +
- + - + -
+ - - + +
- + - + -
+ - + - +
- + - - +
+ - + - +
- + - + -
+ - - + +
- ]
144 : : {
145 : 1 : CNetAddr addr;
146 : :
147 : : // IPv4, INADDR_ANY
148 [ + - + - : 2 : addr = LookupHost("0.0.0.0", false).value();
+ - ]
149 [ + - + - : 2 : BOOST_REQUIRE(!addr.IsValid());
+ - + - ]
150 [ + - + - : 2 : BOOST_REQUIRE(addr.IsIPv4());
+ - ]
151 : :
152 [ + - + - : 2 : BOOST_CHECK(addr.IsBindAny());
+ - + - ]
153 [ + - + - : 2 : BOOST_CHECK(addr.IsAddrV1Compatible());
+ - + - ]
154 [ + - + - : 1 : BOOST_CHECK_EQUAL(addr.ToStringAddr(), "0.0.0.0");
+ - ]
155 : :
156 : : // IPv4, INADDR_NONE
157 [ + - + - : 2 : addr = LookupHost("255.255.255.255", false).value();
+ - ]
158 [ + - + - : 2 : BOOST_REQUIRE(!addr.IsValid());
+ - + - ]
159 [ + - + - : 2 : BOOST_REQUIRE(addr.IsIPv4());
+ - ]
160 : :
161 [ + - + - : 2 : BOOST_CHECK(!addr.IsBindAny());
+ - + - ]
162 [ + - + - : 2 : BOOST_CHECK(addr.IsAddrV1Compatible());
+ - + - ]
163 [ + - + - : 1 : BOOST_CHECK_EQUAL(addr.ToStringAddr(), "255.255.255.255");
+ - ]
164 : :
165 : : // IPv4, casual
166 [ + - + - : 2 : addr = LookupHost("12.34.56.78", false).value();
+ - ]
167 [ + - + - : 2 : BOOST_REQUIRE(addr.IsValid());
+ - + - ]
168 [ + - + - : 2 : BOOST_REQUIRE(addr.IsIPv4());
+ - ]
169 : :
170 [ + - + - : 2 : BOOST_CHECK(!addr.IsBindAny());
+ - + - ]
171 [ + - + - : 2 : BOOST_CHECK(addr.IsAddrV1Compatible());
+ - + - ]
172 [ + - + - : 1 : BOOST_CHECK_EQUAL(addr.ToStringAddr(), "12.34.56.78");
+ - ]
173 : :
174 : : // IPv6, in6addr_any
175 [ + - + - : 2 : addr = LookupHost("::", false).value();
+ - ]
176 [ + - + - : 2 : BOOST_REQUIRE(!addr.IsValid());
+ - + - ]
177 [ + - + - : 2 : BOOST_REQUIRE(addr.IsIPv6());
+ - ]
178 : :
179 [ + - + - : 2 : BOOST_CHECK(addr.IsBindAny());
+ - + - ]
180 [ + - + - : 2 : BOOST_CHECK(addr.IsAddrV1Compatible());
+ - + - ]
181 [ + - + - : 1 : BOOST_CHECK_EQUAL(addr.ToStringAddr(), "::");
+ - ]
182 : :
183 : : // IPv6, casual
184 [ + - + - : 2 : addr = LookupHost("1122:3344:5566:7788:9900:aabb:ccdd:eeff", false).value();
+ - ]
185 [ + - + - : 2 : BOOST_REQUIRE(addr.IsValid());
+ - + - ]
186 [ + - + - : 2 : BOOST_REQUIRE(addr.IsIPv6());
+ - ]
187 : :
188 [ + - + - : 2 : BOOST_CHECK(!addr.IsBindAny());
+ - + - ]
189 [ + - + - : 2 : BOOST_CHECK(addr.IsAddrV1Compatible());
+ - + - ]
190 [ + - + - : 1 : BOOST_CHECK_EQUAL(addr.ToStringAddr(), "1122:3344:5566:7788:9900:aabb:ccdd:eeff");
+ - ]
191 : :
192 : : // IPv6, scoped/link-local. See https://tools.ietf.org/html/rfc4007
193 : : // We support non-negative decimal integers (uint32_t) as zone id indices.
194 : : // Normal link-local scoped address functionality is to append "%" plus the
195 : : // zone id, for example, given a link-local address of "fe80::1" and a zone
196 : : // id of "32", return the address as "fe80::1%32".
197 [ + - ]: 1 : const std::string link_local{"fe80::1"};
198 [ + - ]: 1 : const std::string scoped_addr{link_local + "%32"};
199 [ + - + - ]: 2 : addr = LookupHost(scoped_addr, false).value();
200 [ + - + - : 2 : BOOST_REQUIRE(addr.IsValid());
+ - + - ]
201 [ + - + - : 2 : BOOST_REQUIRE(addr.IsIPv6());
+ - ]
202 [ + - + - : 2 : BOOST_CHECK(!addr.IsBindAny());
+ - + - ]
203 [ + - + - : 1 : BOOST_CHECK_EQUAL(addr.ToStringAddr(), scoped_addr);
+ - ]
204 : :
205 : : // TORv2, no longer supported
206 [ + - + - : 2 : BOOST_CHECK(!addr.SetSpecial("6hzph5hv6337r6p2.onion"));
+ - + - ]
207 : :
208 : : // TORv3
209 : 1 : const char* torv3_addr = "pg6mmjiyjmcrsslvykfwnntlaru7p5svn6y2ymmju6nubxndf4pscryd.onion";
210 [ + - + - : 2 : BOOST_REQUIRE(addr.SetSpecial(torv3_addr));
+ - + - ]
211 [ + - + - : 2 : BOOST_REQUIRE(addr.IsValid());
+ - + - ]
212 [ + - + - : 2 : BOOST_REQUIRE(addr.IsTor());
+ - ]
213 : :
214 [ + - + - : 2 : BOOST_CHECK(!addr.IsI2P());
+ - ]
215 [ + - + - : 2 : BOOST_CHECK(!addr.IsBindAny());
+ - + - ]
216 [ + - + - : 2 : BOOST_CHECK(!addr.IsAddrV1Compatible());
+ - + - ]
217 [ + - + - : 1 : BOOST_CHECK_EQUAL(addr.ToStringAddr(), torv3_addr);
+ - ]
218 : :
219 : : // TORv3, broken, with wrong checksum
220 [ + - + - : 2 : BOOST_CHECK(!addr.SetSpecial("pg6mmjiyjmcrsslvykfwnntlaru7p5svn6y2ymmju6nubxndf4pscsad.onion"));
+ - + - ]
221 : :
222 : : // TORv3, broken, with wrong version
223 [ + - + - : 2 : BOOST_CHECK(!addr.SetSpecial("pg6mmjiyjmcrsslvykfwnntlaru7p5svn6y2ymmju6nubxndf4pscrye.onion"));
+ - + - ]
224 : :
225 : : // TORv3, malicious
226 [ + - + - : 3 : BOOST_CHECK(!addr.SetSpecial(std::string{
+ - + - +
- ]
227 : : "pg6mmjiyjmcrsslvykfwnntlaru7p5svn6y2ymmju6nubxndf4pscryd\0wtf.onion", 66}));
228 : :
229 : : // TOR, bogus length
230 [ + - + - : 3 : BOOST_CHECK(!addr.SetSpecial(std::string{"mfrggzak.onion"}));
+ - + - +
- ]
231 : :
232 : : // TOR, invalid base32
233 [ + - + - : 3 : BOOST_CHECK(!addr.SetSpecial(std::string{"mf*g zak.onion"}));
+ - + - +
- ]
234 : :
235 : : // I2P
236 : 1 : const char* i2p_addr = "UDHDrtrcetjm5sxzskjyr5ztpeszydbh4dpl3pl4utgqqw2v4jna.b32.I2P";
237 [ + - + - : 2 : BOOST_REQUIRE(addr.SetSpecial(i2p_addr));
+ - + - ]
238 [ + - + - : 2 : BOOST_REQUIRE(addr.IsValid());
+ - + - ]
239 [ + - + - : 2 : BOOST_REQUIRE(addr.IsI2P());
+ - ]
240 : :
241 [ + - + - : 2 : BOOST_CHECK(!addr.IsTor());
+ - ]
242 [ + - + - : 2 : BOOST_CHECK(!addr.IsBindAny());
+ - + - ]
243 [ + - + - : 2 : BOOST_CHECK(!addr.IsAddrV1Compatible());
+ - + - ]
244 [ + - + - : 1 : BOOST_CHECK_EQUAL(addr.ToStringAddr(), ToLower(i2p_addr));
+ - + - ]
245 : :
246 : : // I2P, correct length, but decodes to less than the expected number of bytes.
247 [ + - + - : 2 : BOOST_CHECK(!addr.SetSpecial("udhdrtrcetjm5sxzskjyr5ztpeszydbh4dpl3pl4utgqqw2v4jn=.b32.i2p"));
+ - + - ]
248 : :
249 : : // I2P, extra unnecessary padding
250 [ + - + - : 2 : BOOST_CHECK(!addr.SetSpecial("udhdrtrcetjm5sxzskjyr5ztpeszydbh4dpl3pl4utgqqw2v4jna=.b32.i2p"));
+ - + - ]
251 : :
252 : : // I2P, malicious
253 [ + - + - : 2 : BOOST_CHECK(!addr.SetSpecial("udhdrtrcetjm5sxzskjyr5ztpeszydbh4dpl3pl4utgqqw2v\0wtf.b32.i2p"s));
- + + - +
- + - ]
254 : :
255 : : // I2P, valid but unsupported (56 Base32 characters)
256 : : // See "Encrypted LS with Base 32 Addresses" in
257 : : // https://geti2p.net/spec/encryptedleaseset.txt
258 [ + - + - : 2 : BOOST_CHECK(
+ - + - ]
259 : : !addr.SetSpecial("pg6mmjiyjmcrsslvykfwnntlaru7p5svn6y2ymmju6nubxndf4pscsad.b32.i2p"));
260 : :
261 : : // I2P, invalid base32
262 [ + - + - : 3 : BOOST_CHECK(!addr.SetSpecial(std::string{"tp*szydbh4dp.b32.i2p"}));
+ - + - +
- ]
263 : :
264 : : // Internal
265 [ + - + - ]: 1 : addr.SetInternal("esffpp");
266 [ + - + - : 2 : BOOST_REQUIRE(!addr.IsValid()); // "internal" is considered invalid
+ - + - ]
267 [ + - + - : 2 : BOOST_REQUIRE(addr.IsInternal());
+ - + - ]
268 : :
269 [ + - + - : 2 : BOOST_CHECK(!addr.IsBindAny());
+ - + - ]
270 [ + - + - : 2 : BOOST_CHECK(addr.IsAddrV1Compatible());
+ - + - ]
271 [ + - + - : 1 : BOOST_CHECK_EQUAL(addr.ToStringAddr(), "esffpvrt3wpeaygy.internal");
+ - ]
272 : :
273 : : // Totally bogus
274 [ + - + - : 2 : BOOST_CHECK(!addr.SetSpecial("totally bogus"));
+ - ]
275 : 1 : }
276 : :
277 [ + - + - : 7 : BOOST_AUTO_TEST_CASE(cnetaddr_tostring_canonical_ipv6)
+ - + - -
+ + - + -
+ - + - +
- + - - +
+ - + - +
- + - + -
+ - - + +
- + - + -
+ - + - +
- + - - +
+ - + - +
- + - + -
+ - - + +
- ]
278 : : {
279 : : // Test that CNetAddr::ToString formats IPv6 addresses with zero compression as described in
280 : : // RFC 5952 ("A Recommendation for IPv6 Address Text Representation").
281 : 1 : const std::map<std::string, std::string> canonical_representations_ipv6{
282 : : {"0000:0000:0000:0000:0000:0000:0000:0000", "::"},
283 : : {"000:0000:000:00:0:00:000:0000", "::"},
284 : : {"000:000:000:000:000:000:000:000", "::"},
285 : : {"00:00:00:00:00:00:00:00", "::"},
286 : : {"0:0:0:0:0:0:0:0", "::"},
287 : : {"0:0:0:0:0:0:0:1", "::1"},
288 : : {"2001:0:0:1:0:0:0:1", "2001:0:0:1::1"},
289 : : {"2001:0db8:0:0:1:0:0:1", "2001:db8::1:0:0:1"},
290 : : {"2001:0db8:85a3:0000:0000:8a2e:0370:7334", "2001:db8:85a3::8a2e:370:7334"},
291 : : {"2001:0db8::0001", "2001:db8::1"},
292 : : {"2001:0db8::0001:0000", "2001:db8::1:0"},
293 : : {"2001:0db8::1:0:0:1", "2001:db8::1:0:0:1"},
294 : : {"2001:db8:0000:0:1::1", "2001:db8::1:0:0:1"},
295 : : {"2001:db8:0000:1:1:1:1:1", "2001:db8:0:1:1:1:1:1"},
296 : : {"2001:db8:0:0:0:0:2:1", "2001:db8::2:1"},
297 : : {"2001:db8:0:0:0::1", "2001:db8::1"},
298 : : {"2001:db8:0:0:1:0:0:1", "2001:db8::1:0:0:1"},
299 : : {"2001:db8:0:0:1::1", "2001:db8::1:0:0:1"},
300 : : {"2001:DB8:0:0:1::1", "2001:db8::1:0:0:1"},
301 : : {"2001:db8:0:0::1", "2001:db8::1"},
302 : : {"2001:db8:0:0:aaaa::1", "2001:db8::aaaa:0:0:1"},
303 : : {"2001:db8:0:1:1:1:1:1", "2001:db8:0:1:1:1:1:1"},
304 : : {"2001:db8:0::1", "2001:db8::1"},
305 : : {"2001:db8:85a3:0:0:8a2e:370:7334", "2001:db8:85a3::8a2e:370:7334"},
306 : : {"2001:db8::0:1", "2001:db8::1"},
307 : : {"2001:db8::0:1:0:0:1", "2001:db8::1:0:0:1"},
308 : : {"2001:DB8::1", "2001:db8::1"},
309 : : {"2001:db8::1", "2001:db8::1"},
310 : : {"2001:db8::1:0:0:1", "2001:db8::1:0:0:1"},
311 : : {"2001:db8::1:1:1:1:1", "2001:db8:0:1:1:1:1:1"},
312 : : {"2001:db8::aaaa:0:0:1", "2001:db8::aaaa:0:0:1"},
313 : : {"2001:db8:aaaa:bbbb:cccc:dddd:0:1", "2001:db8:aaaa:bbbb:cccc:dddd:0:1"},
314 : : {"2001:db8:aaaa:bbbb:cccc:dddd::1", "2001:db8:aaaa:bbbb:cccc:dddd:0:1"},
315 : : {"2001:db8:aaaa:bbbb:cccc:dddd:eeee:0001", "2001:db8:aaaa:bbbb:cccc:dddd:eeee:1"},
316 : : {"2001:db8:aaaa:bbbb:cccc:dddd:eeee:001", "2001:db8:aaaa:bbbb:cccc:dddd:eeee:1"},
317 : : {"2001:db8:aaaa:bbbb:cccc:dddd:eeee:01", "2001:db8:aaaa:bbbb:cccc:dddd:eeee:1"},
318 : : {"2001:db8:aaaa:bbbb:cccc:dddd:eeee:1", "2001:db8:aaaa:bbbb:cccc:dddd:eeee:1"},
319 : : {"2001:db8:aaaa:bbbb:cccc:dddd:eeee:aaaa", "2001:db8:aaaa:bbbb:cccc:dddd:eeee:aaaa"},
320 : : {"2001:db8:aaaa:bbbb:cccc:dddd:eeee:AAAA", "2001:db8:aaaa:bbbb:cccc:dddd:eeee:aaaa"},
321 : : {"2001:db8:aaaa:bbbb:cccc:dddd:eeee:AaAa", "2001:db8:aaaa:bbbb:cccc:dddd:eeee:aaaa"},
322 [ - + + + : 41 : };
- - ]
323 [ + - + + ]: 41 : for (const auto& [input_address, expected_canonical_representation_output] : canonical_representations_ipv6) {
324 [ + - + - ]: 40 : const std::optional<CNetAddr> net_addr{LookupHost(input_address, false)};
325 [ + - + - : 80 : BOOST_REQUIRE(net_addr.value().IsIPv6());
+ - + - ]
326 [ + - + - : 40 : BOOST_CHECK_EQUAL(net_addr.value().ToStringAddr(), expected_canonical_representation_output);
+ - + - ]
327 : 40 : }
328 [ + - + - : 2 : }
+ - + - +
- + - + -
+ - + - +
- + - + -
+ - + - +
- + - + -
+ - + - +
- + - + -
+ - + - +
- + - + -
+ - + - +
- + - + -
+ - + - +
- + - + -
+ - + - +
- - - ]
329 : :
330 [ + - + - : 7 : BOOST_AUTO_TEST_CASE(cnetaddr_serialize_v1)
+ - + - -
+ + - + -
+ - + - +
- + - - +
+ - + - +
- + - + -
+ - - + +
- + - + -
+ - + - +
- + - - +
+ - + - +
- + - + -
+ - - + +
- ]
331 : : {
332 : 1 : CNetAddr addr;
333 : 1 : DataStream s{};
334 : 1 : const auto ser_params{CAddress::V1_NETWORK};
335 : :
336 [ + - ]: 1 : s << ser_params(addr);
337 [ + - - + : 1 : BOOST_CHECK_EQUAL(HexStr(s), "00000000000000000000000000000000");
+ - + - ]
338 [ + - ]: 1 : s.clear();
339 : :
340 [ + - + - : 2 : addr = LookupHost("1.2.3.4", false).value();
+ - ]
341 [ + - ]: 1 : s << ser_params(addr);
342 [ + - - + : 1 : BOOST_CHECK_EQUAL(HexStr(s), "00000000000000000000ffff01020304");
+ - + - ]
343 [ + - ]: 1 : s.clear();
344 : :
345 [ + - + - : 2 : addr = LookupHost("1a1b:2a2b:3a3b:4a4b:5a5b:6a6b:7a7b:8a8b", false).value();
+ - ]
346 [ + - ]: 1 : s << ser_params(addr);
347 [ + - - + : 1 : BOOST_CHECK_EQUAL(HexStr(s), "1a1b2a2b3a3b4a4b5a5b6a6b7a7b8a8b");
+ - + - ]
348 [ + - ]: 1 : s.clear();
349 : :
350 : : // TORv2, no longer supported
351 [ + - + - : 2 : BOOST_CHECK(!addr.SetSpecial("6hzph5hv6337r6p2.onion"));
+ - + - ]
352 : :
353 [ + - + - : 2 : BOOST_REQUIRE(addr.SetSpecial("pg6mmjiyjmcrsslvykfwnntlaru7p5svn6y2ymmju6nubxndf4pscryd.onion"));
+ - + - ]
354 [ + - ]: 1 : s << ser_params(addr);
355 [ + - - + : 1 : BOOST_CHECK_EQUAL(HexStr(s), "00000000000000000000000000000000");
+ - + - ]
356 [ + - ]: 1 : s.clear();
357 : :
358 [ + - + - ]: 1 : addr.SetInternal("a");
359 [ + - ]: 1 : s << ser_params(addr);
360 [ + - - + : 1 : BOOST_CHECK_EQUAL(HexStr(s), "fd6b88c08724ca978112ca1bbdcafac2");
+ - + - ]
361 [ + - ]: 2 : s.clear();
362 : 1 : }
363 : :
364 [ + - + - : 7 : BOOST_AUTO_TEST_CASE(cnetaddr_serialize_v2)
+ - + - -
+ + - + -
+ - + - +
- + - - +
+ - + - +
- + - + -
+ - - + +
- + - + -
+ - + - +
- + - - +
+ - + - +
- + - + -
+ - - + +
- ]
365 : : {
366 : 1 : CNetAddr addr;
367 : 1 : DataStream s{};
368 : 1 : const auto ser_params{CAddress::V2_NETWORK};
369 : :
370 [ + - ]: 1 : s << ser_params(addr);
371 [ + - - + : 1 : BOOST_CHECK_EQUAL(HexStr(s), "021000000000000000000000000000000000");
+ - + - ]
372 [ + - ]: 1 : s.clear();
373 : :
374 [ + - + - : 2 : addr = LookupHost("1.2.3.4", false).value();
+ - ]
375 [ + - ]: 1 : s << ser_params(addr);
376 [ + - - + : 1 : BOOST_CHECK_EQUAL(HexStr(s), "010401020304");
+ - + - ]
377 [ + - ]: 1 : s.clear();
378 : :
379 [ + - + - : 2 : addr = LookupHost("1a1b:2a2b:3a3b:4a4b:5a5b:6a6b:7a7b:8a8b", false).value();
+ - ]
380 [ + - ]: 1 : s << ser_params(addr);
381 [ + - - + : 1 : BOOST_CHECK_EQUAL(HexStr(s), "02101a1b2a2b3a3b4a4b5a5b6a6b7a7b8a8b");
+ - + - ]
382 [ + - ]: 1 : s.clear();
383 : :
384 : : // TORv2, no longer supported
385 [ + - + - : 2 : BOOST_CHECK(!addr.SetSpecial("6hzph5hv6337r6p2.onion"));
+ - + - ]
386 : :
387 [ + - + - : 2 : BOOST_REQUIRE(addr.SetSpecial("kpgvmscirrdqpekbqjsvw5teanhatztpp2gl6eee4zkowvwfxwenqaid.onion"));
+ - + - ]
388 [ + - ]: 1 : s << ser_params(addr);
389 [ + - - + : 1 : BOOST_CHECK_EQUAL(HexStr(s), "042053cd5648488c4707914182655b7664034e09e66f7e8cbf1084e654eb56c5bd88");
+ - + - ]
390 [ + - ]: 1 : s.clear();
391 : :
392 [ + - + - : 2 : BOOST_REQUIRE(addr.SetInternal("a"));
+ - + - +
- ]
393 [ + - ]: 1 : s << ser_params(addr);
394 [ + - - + : 1 : BOOST_CHECK_EQUAL(HexStr(s), "0210fd6b88c08724ca978112ca1bbdcafac2");
+ - + - ]
395 [ + - ]: 2 : s.clear();
396 : 1 : }
397 : :
398 [ + - + - : 7 : BOOST_AUTO_TEST_CASE(cnetaddr_unserialize_v2)
+ - + - -
+ + - + -
+ - + - +
- + - - +
+ - + - +
- + - + -
+ - - + +
- + - + -
+ - + - +
- + - - +
+ - + - +
- + - + -
+ - - + +
- ]
399 : : {
400 : 1 : CNetAddr addr;
401 : 1 : DataStream s{};
402 : 1 : const auto ser_params{CAddress::V2_NETWORK};
403 : :
404 : : // Valid IPv4.
405 [ + - ]: 1 : s << "01" // network type (IPv4)
406 : : "04" // address length
407 : 1 : "01020304"_hex; // address
408 [ + - ]: 1 : s >> ser_params(addr);
409 [ + - + - : 2 : BOOST_CHECK(addr.IsValid());
+ - + - ]
410 [ + - + - : 2 : BOOST_CHECK(addr.IsIPv4());
+ - ]
411 [ + - + - : 2 : BOOST_CHECK(addr.IsAddrV1Compatible());
+ - + - ]
412 [ + - + - : 1 : BOOST_CHECK_EQUAL(addr.ToStringAddr(), "1.2.3.4");
+ - ]
413 [ + - - + : 2 : BOOST_REQUIRE(s.empty());
+ - + - ]
414 : :
415 : : // Invalid IPv4, valid length but address itself is shorter.
416 [ + - ]: 1 : s << "01" // network type (IPv4)
417 : : "04" // address length
418 : 1 : "0102"_hex; // address
419 [ + - - + : 2 : BOOST_CHECK_EXCEPTION(s >> ser_params(addr), std::ios_base::failure, HasReason("end of data"));
- - - - -
+ + - + -
+ - ]
420 [ + - - + : 2 : BOOST_REQUIRE(!s.empty()); // The stream is not consumed on invalid input.
+ - + - ]
421 [ + - ]: 1 : s.clear();
422 : :
423 : : // Invalid IPv4, with bogus length.
424 [ + - ]: 1 : s << "01" // network type (IPv4)
425 : : "05" // address length
426 : 1 : "01020304"_hex; // address
427 [ + - - + : 2 : BOOST_CHECK_EXCEPTION(s >> ser_params(addr), std::ios_base::failure,
- - - - -
+ + - + -
+ - ]
428 : : HasReason("BIP155 IPv4 address with length 5 (should be 4)"));
429 [ + - - + : 2 : BOOST_REQUIRE(!s.empty()); // The stream is not consumed on invalid input.
+ - + - ]
430 [ + - ]: 1 : s.clear();
431 : :
432 : : // Invalid IPv4, with extreme length.
433 [ + - ]: 1 : s << "01" // network type (IPv4)
434 : : "fd0102" // address length (513 as CompactSize)
435 : 1 : "01020304"_hex; // address
436 [ + - - + : 2 : BOOST_CHECK_EXCEPTION(s >> ser_params(addr), std::ios_base::failure,
- - - - -
+ + - + -
+ - ]
437 : : HasReason("Address too long: 513 > 512"));
438 [ + - - + : 2 : BOOST_REQUIRE(!s.empty()); // The stream is not consumed on invalid input.
+ - + - ]
439 [ + - ]: 1 : s.clear();
440 : :
441 : : // Valid IPv6.
442 [ + - ]: 1 : s << "02" // network type (IPv6)
443 : : "10" // address length
444 : 1 : "0102030405060708090a0b0c0d0e0f10"_hex; // address
445 [ + - ]: 1 : s >> ser_params(addr);
446 [ + - + - : 2 : BOOST_CHECK(addr.IsValid());
+ - + - ]
447 [ + - + - : 2 : BOOST_CHECK(addr.IsIPv6());
+ - ]
448 [ + - + - : 2 : BOOST_CHECK(addr.IsAddrV1Compatible());
+ - + - ]
449 [ + - + - : 1 : BOOST_CHECK_EQUAL(addr.ToStringAddr(), "102:304:506:708:90a:b0c:d0e:f10");
+ - ]
450 [ + - - + : 2 : BOOST_REQUIRE(s.empty());
+ - + - ]
451 : :
452 : : // Valid IPv6, contains embedded "internal".
453 [ + - ]: 1 : s << "02" // network type (IPv6)
454 : : "10" // address length
455 : 1 : "fd6b88c08724ca978112ca1bbdcafac2"_hex; // address: 0xfd + sha256("bitcoin")[0:5] +
456 : : // sha256(name)[0:10]
457 [ + - ]: 1 : s >> ser_params(addr);
458 [ + - + - : 2 : BOOST_CHECK(addr.IsInternal());
+ - + - ]
459 [ + - + - : 2 : BOOST_CHECK(addr.IsAddrV1Compatible());
+ - + - ]
460 [ + - + - : 1 : BOOST_CHECK_EQUAL(addr.ToStringAddr(), "zklycewkdo64v6wc.internal");
+ - ]
461 [ + - - + : 2 : BOOST_REQUIRE(s.empty());
+ - + - ]
462 : :
463 : : // Invalid IPv6, with bogus length.
464 [ + - ]: 1 : s << "02" // network type (IPv6)
465 : : "04" // address length
466 : 1 : "00"_hex; // address
467 [ + - - + : 2 : BOOST_CHECK_EXCEPTION(s >> ser_params(addr), std::ios_base::failure,
- - - - -
+ + - + -
+ - ]
468 : : HasReason("BIP155 IPv6 address with length 4 (should be 16)"));
469 [ + - - + : 2 : BOOST_REQUIRE(!s.empty()); // The stream is not consumed on invalid input.
+ - + - ]
470 [ + - ]: 1 : s.clear();
471 : :
472 : : // Invalid IPv6, contains embedded IPv4.
473 [ + - ]: 1 : s << "02" // network type (IPv6)
474 : : "10" // address length
475 : 1 : "00000000000000000000ffff01020304"_hex; // address
476 [ + - ]: 1 : s >> ser_params(addr);
477 [ + - + - : 2 : BOOST_CHECK(!addr.IsValid());
+ - + - ]
478 [ + - - + : 2 : BOOST_REQUIRE(s.empty());
+ - + - ]
479 : :
480 : : // Invalid IPv6, contains embedded TORv2.
481 [ + - ]: 1 : s << "02" // network type (IPv6)
482 : : "10" // address length
483 : 1 : "fd87d87eeb430102030405060708090a"_hex; // address
484 [ + - ]: 1 : s >> ser_params(addr);
485 [ + - + - : 2 : BOOST_CHECK(!addr.IsValid());
+ - + - ]
486 [ + - - + : 2 : BOOST_REQUIRE(s.empty());
+ - + - ]
487 : :
488 : : // TORv2, no longer supported.
489 [ + - ]: 1 : s << "03" // network type (TORv2)
490 : : "0a" // address length
491 : 1 : "f1f2f3f4f5f6f7f8f9fa"_hex; // address
492 [ + - ]: 1 : s >> ser_params(addr);
493 [ + - + - : 2 : BOOST_CHECK(!addr.IsValid());
+ - + - ]
494 [ + - - + : 2 : BOOST_REQUIRE(s.empty());
+ - + - ]
495 : :
496 : : // Valid TORv3.
497 [ + - ]: 1 : s << "04" // network type (TORv3)
498 : : "20" // address length
499 : : "79bcc625184b05194975c28b66b66b04" // address
500 : 1 : "69f7f6556fb1ac3189a79b40dda32f1f"_hex;
501 [ + - ]: 1 : s >> ser_params(addr);
502 [ + - + - : 2 : BOOST_CHECK(addr.IsValid());
+ - + - ]
503 [ + - + - : 2 : BOOST_CHECK(addr.IsTor());
+ - ]
504 [ + - + - : 2 : BOOST_CHECK(!addr.IsAddrV1Compatible());
+ - + - ]
505 [ + - + - : 1 : BOOST_CHECK_EQUAL(addr.ToStringAddr(),
+ - ]
506 : : "pg6mmjiyjmcrsslvykfwnntlaru7p5svn6y2ymmju6nubxndf4pscryd.onion");
507 [ + - - + : 2 : BOOST_REQUIRE(s.empty());
+ - + - ]
508 : :
509 : : // Invalid TORv3, with bogus length.
510 [ + - ]: 1 : s << "04" // network type (TORv3)
511 : : "00" // address length
512 : 1 : "00"_hex; // address
513 [ + - - + : 2 : BOOST_CHECK_EXCEPTION(s >> ser_params(addr), std::ios_base::failure,
- - - - -
+ + - + -
+ - ]
514 : : HasReason("BIP155 TORv3 address with length 0 (should be 32)"));
515 [ + - - + : 2 : BOOST_REQUIRE(!s.empty()); // The stream is not consumed on invalid input.
+ - + - ]
516 [ + - ]: 1 : s.clear();
517 : :
518 : : // Valid I2P.
519 [ + - ]: 1 : s << "05" // network type (I2P)
520 : : "20" // address length
521 : : "a2894dabaec08c0051a481a6dac88b64" // address
522 : 1 : "f98232ae42d4b6fd2fa81952dfe36a87"_hex;
523 [ + - ]: 1 : s >> ser_params(addr);
524 [ + - + - : 2 : BOOST_CHECK(addr.IsValid());
+ - + - ]
525 [ + - + - : 2 : BOOST_CHECK(addr.IsI2P());
+ - ]
526 [ + - + - : 2 : BOOST_CHECK(!addr.IsAddrV1Compatible());
+ - + - ]
527 [ + - + - : 1 : BOOST_CHECK_EQUAL(addr.ToStringAddr(),
+ - ]
528 : : "ukeu3k5oycgaauneqgtnvselmt4yemvoilkln7jpvamvfx7dnkdq.b32.i2p");
529 [ + - - + : 2 : BOOST_REQUIRE(s.empty());
+ - + - ]
530 : :
531 : : // Invalid I2P, with bogus length.
532 [ + - ]: 1 : s << "05" // network type (I2P)
533 : : "03" // address length
534 : 1 : "00"_hex; // address
535 [ + - - + : 2 : BOOST_CHECK_EXCEPTION(s >> ser_params(addr), std::ios_base::failure,
- - - - -
+ + - + -
+ - ]
536 : : HasReason("BIP155 I2P address with length 3 (should be 32)"));
537 [ + - - + : 2 : BOOST_REQUIRE(!s.empty()); // The stream is not consumed on invalid input.
+ - + - ]
538 [ + - ]: 1 : s.clear();
539 : :
540 : : // Valid CJDNS.
541 [ + - ]: 1 : s << "06" // network type (CJDNS)
542 : : "10" // address length
543 : 1 : "fc000001000200030004000500060007"_hex; // address
544 [ + - ]: 1 : s >> ser_params(addr);
545 [ + - + - : 2 : BOOST_CHECK(addr.IsValid());
+ - + - ]
546 [ + - + - : 2 : BOOST_CHECK(addr.IsCJDNS());
+ - ]
547 [ + - + - : 2 : BOOST_CHECK(!addr.IsAddrV1Compatible());
+ - + - ]
548 [ + - + - : 1 : BOOST_CHECK_EQUAL(addr.ToStringAddr(), "fc00:1:2:3:4:5:6:7");
+ - ]
549 [ + - - + : 2 : BOOST_REQUIRE(s.empty());
+ - + - ]
550 : :
551 : : // Invalid CJDNS, wrong prefix.
552 [ + - ]: 1 : s << "06" // network type (CJDNS)
553 : : "10" // address length
554 : 1 : "aa000001000200030004000500060007"_hex; // address
555 [ + - ]: 1 : s >> ser_params(addr);
556 [ + - + - : 2 : BOOST_CHECK(addr.IsCJDNS());
+ - ]
557 [ + - + - : 2 : BOOST_CHECK(!addr.IsValid());
+ - + - ]
558 [ + - - + : 2 : BOOST_REQUIRE(s.empty());
+ - + - ]
559 : :
560 : : // Invalid CJDNS, with bogus length.
561 [ + - ]: 1 : s << "06" // network type (CJDNS)
562 : : "01" // address length
563 : 1 : "00"_hex; // address
564 [ + - - + : 2 : BOOST_CHECK_EXCEPTION(s >> ser_params(addr), std::ios_base::failure,
- - - - -
+ + - + -
+ - ]
565 : : HasReason("BIP155 CJDNS address with length 1 (should be 16)"));
566 [ + - - + : 2 : BOOST_REQUIRE(!s.empty()); // The stream is not consumed on invalid input.
+ - + - ]
567 [ + - ]: 1 : s.clear();
568 : :
569 : : // Unknown, with extreme length.
570 [ + - ]: 1 : s << "aa" // network type (unknown)
571 : : "fe00000002" // address length (CompactSize's MAX_SIZE)
572 : 1 : "01020304050607"_hex; // address
573 [ + - - + : 2 : BOOST_CHECK_EXCEPTION(s >> ser_params(addr), std::ios_base::failure,
- - - - -
+ + - + -
+ - ]
574 : : HasReason("Address too long: 33554432 > 512"));
575 [ + - - + : 2 : BOOST_REQUIRE(!s.empty()); // The stream is not consumed on invalid input.
+ - + - ]
576 [ + - ]: 1 : s.clear();
577 : :
578 : : // Unknown, with reasonable length.
579 [ + - ]: 1 : s << "aa" // network type (unknown)
580 : : "04" // address length
581 : 1 : "01020304"_hex; // address
582 [ + - ]: 1 : s >> ser_params(addr);
583 [ + - + - : 2 : BOOST_CHECK(!addr.IsValid());
+ - + - ]
584 [ + - - + : 2 : BOOST_REQUIRE(s.empty());
+ - + - ]
585 : :
586 : : // Unknown, with zero length.
587 [ + - ]: 1 : s << "aa" // network type (unknown)
588 : : "00" // address length
589 : 1 : ""_hex; // address
590 [ + - ]: 1 : s >> ser_params(addr);
591 [ + - + - : 2 : BOOST_CHECK(!addr.IsValid());
+ - + - ]
592 [ + - - + : 2 : BOOST_REQUIRE(s.empty());
+ - ]
593 : 1 : }
594 : :
595 : : // prior to PR #14728, this test triggers an undefined behavior
596 [ + - + - : 7 : BOOST_AUTO_TEST_CASE(ipv4_peer_with_ipv6_addrMe_test)
+ - + - -
+ + - + -
+ - + - +
- + - - +
+ - + - +
- + - + -
+ - - + +
- + - + -
+ - + - +
- + - - +
+ - + - +
- + - + -
+ - - + +
- ]
597 : : {
598 : : // set up local addresses; all that's necessary to reproduce the bug is
599 : : // that a normal IPv4 address is among the entries, but if this address is
600 : : // !IsRoutable the undefined behavior is easier to trigger deterministically
601 : 1 : in_addr raw_addr;
602 : 1 : raw_addr.s_addr = htonl(0x7f000001);
603 : 1 : const CNetAddr mapLocalHost_entry = CNetAddr(raw_addr);
604 : 1 : {
605 [ + - ]: 1 : LOCK(g_maplocalhost_mutex);
606 : 1 : LocalServiceInfo lsi;
607 : 1 : lsi.nScore = 23;
608 : 1 : lsi.nPort = 42;
609 [ + - + - ]: 1 : mapLocalHost[mapLocalHost_entry] = lsi;
610 : 0 : }
611 : :
612 : : // create a peer with an IPv4 address
613 : 1 : in_addr ipv4AddrPeer;
614 : 1 : ipv4AddrPeer.s_addr = 0xa0b0c001;
615 [ + - ]: 2 : CAddress addr = CAddress(CService(ipv4AddrPeer, 7777), NODE_NETWORK);
616 : 2 : std::unique_ptr<CNode> pnode = std::make_unique<CNode>(/*id=*/0,
617 : 1 : /*sock=*/nullptr,
618 : : addr,
619 : 1 : /*nKeyedNetGroupIn=*/0,
620 : 1 : /*nLocalHostNonceIn=*/0,
621 : 1 : CAddress{},
622 [ + - ]: 1 : /*pszDest=*/std::string{},
623 : 1 : ConnectionType::OUTBOUND_FULL_RELAY,
624 : 1 : /*inbound_onion=*/false,
625 [ + - + - ]: 1 : /*network_key=*/0);
626 [ + - ]: 1 : pnode->fSuccessfullyConnected.store(true);
627 : :
628 : : // the peer claims to be reaching us via IPv6
629 : 1 : in6_addr ipv6AddrLocal;
630 [ + - ]: 1 : memset(ipv6AddrLocal.s6_addr, 0, 16);
631 : 1 : ipv6AddrLocal.s6_addr[0] = 0xcc;
632 [ + - ]: 2 : CAddress addrLocal = CAddress(CService(ipv6AddrLocal, 7777), NODE_NETWORK);
633 [ + - ]: 1 : pnode->SetAddrLocal(addrLocal);
634 : :
635 : : // before patch, this causes undefined behavior detectable with clang's -fsanitize=memory
636 [ + - ]: 1 : GetLocalAddrForPeer(*pnode);
637 : :
638 : : // suppress no-checks-run warning; if this test fails, it's by triggering a sanitizer
639 [ + - + - : 2 : BOOST_CHECK(1);
+ - ]
640 : :
641 : : // Cleanup, so that we don't confuse other tests.
642 : 1 : {
643 [ + - ]: 1 : LOCK(g_maplocalhost_mutex);
644 [ + - + - ]: 1 : mapLocalHost.erase(mapLocalHost_entry);
645 : 1 : }
646 : 1 : }
647 : :
648 [ + - + - : 7 : BOOST_AUTO_TEST_CASE(get_local_addr_for_peer_port)
+ - + - -
+ + - + -
+ - + - +
- + - - +
+ - + - +
- + - + -
+ - - + +
- + - + -
+ - + - +
- + - - +
+ - + - +
- + - + -
+ - - + +
- ]
649 : : {
650 : : // Test that GetLocalAddrForPeer() properly selects the address to self-advertise:
651 : : //
652 : : // 1. GetLocalAddrForPeer() calls GetLocalAddress() which returns an address that is
653 : : // not routable.
654 : : // 2. GetLocalAddrForPeer() overrides the address with whatever the peer has told us
655 : : // he sees us as.
656 : : // 2.1. For inbound connections we must override both the address and the port.
657 : : // 2.2. For outbound connections we must override only the address.
658 : :
659 : : // Pretend that we bound to this port.
660 : 1 : const uint16_t bind_port = 20001;
661 [ + - + - ]: 2 : m_node.args->ForceSetArg("-bind", strprintf("3.4.5.6:%u", bind_port));
662 : :
663 : : // Our address:port as seen from the peer, completely different from the above.
664 : 1 : in_addr peer_us_addr;
665 : 1 : peer_us_addr.s_addr = htonl(0x02030405);
666 : 1 : const CService peer_us{peer_us_addr, 20002};
667 : :
668 : : // Create a peer with a routable IPv4 address (outbound).
669 : 1 : in_addr peer_out_in_addr;
670 : 1 : peer_out_in_addr.s_addr = htonl(0x01020304);
671 : 1 : CNode peer_out{/*id=*/0,
672 : : /*sock=*/nullptr,
673 [ - + ]: 1 : /*addrIn=*/CAddress{CService{peer_out_in_addr, 8333}, NODE_NETWORK},
674 : : /*nKeyedNetGroupIn=*/0,
675 : : /*nLocalHostNonceIn=*/0,
676 [ + - ]: 2 : /*addrBindIn=*/CService{},
677 : 1 : /*addrNameIn=*/std::string{},
678 : : /*conn_type_in=*/ConnectionType::OUTBOUND_FULL_RELAY,
679 : : /*inbound_onion=*/false,
680 [ + - + - ]: 2 : /*network_key=*/0};
681 [ + - ]: 1 : peer_out.fSuccessfullyConnected = true;
682 [ + - ]: 1 : peer_out.SetAddrLocal(peer_us);
683 : :
684 : : // Without the fix peer_us:8333 is chosen instead of the proper peer_us:bind_port.
685 [ + - ]: 1 : auto chosen_local_addr = GetLocalAddrForPeer(peer_out);
686 [ + - + - : 2 : BOOST_REQUIRE(chosen_local_addr);
+ - ]
687 [ + - ]: 1 : const CService expected{peer_us_addr, bind_port};
688 [ + - + - : 2 : BOOST_CHECK(*chosen_local_addr == expected);
+ - + - ]
689 : :
690 : : // Create a peer with a routable IPv4 address (inbound).
691 : 1 : in_addr peer_in_in_addr;
692 : 1 : peer_in_in_addr.s_addr = htonl(0x05060708);
693 : 1 : CNode peer_in{/*id=*/0,
694 : : /*sock=*/nullptr,
695 [ - + ]: 1 : /*addrIn=*/CAddress{CService{peer_in_in_addr, 8333}, NODE_NETWORK},
696 : : /*nKeyedNetGroupIn=*/0,
697 : : /*nLocalHostNonceIn=*/0,
698 [ + - ]: 2 : /*addrBindIn=*/CService{},
699 : 1 : /*addrNameIn=*/std::string{},
700 : : /*conn_type_in=*/ConnectionType::INBOUND,
701 : : /*inbound_onion=*/false,
702 [ + - + - ]: 2 : /*network_key=*/1};
703 [ + - ]: 1 : peer_in.fSuccessfullyConnected = true;
704 [ + - ]: 1 : peer_in.SetAddrLocal(peer_us);
705 : :
706 : : // Without the fix peer_us:8333 is chosen instead of the proper peer_us:peer_us.GetPort().
707 [ + - ]: 2 : chosen_local_addr = GetLocalAddrForPeer(peer_in);
708 [ + - + - : 2 : BOOST_REQUIRE(chosen_local_addr);
+ - ]
709 [ + - + - : 2 : BOOST_CHECK(*chosen_local_addr == peer_us);
+ - + - ]
710 : :
711 [ + - + - : 2 : m_node.args->ForceSetArg("-bind", "");
+ - ]
712 : 1 : }
713 : :
714 [ + - + - : 7 : BOOST_AUTO_TEST_CASE(LimitedAndReachable_Network)
+ - + - -
+ + - + -
+ - + - +
- + - - +
+ - + - +
- + - + -
+ - - + +
- + - + -
+ - + - +
- + - - +
+ - + - +
- + - + -
+ - - + +
- ]
715 : : {
716 [ + - + - ]: 2 : BOOST_CHECK(g_reachable_nets.Contains(NET_IPV4));
717 [ + - + - ]: 2 : BOOST_CHECK(g_reachable_nets.Contains(NET_IPV6));
718 [ + - + - ]: 2 : BOOST_CHECK(g_reachable_nets.Contains(NET_ONION));
719 [ + - + - ]: 2 : BOOST_CHECK(g_reachable_nets.Contains(NET_I2P));
720 [ + - + - ]: 2 : BOOST_CHECK(g_reachable_nets.Contains(NET_CJDNS));
721 : :
722 : 1 : g_reachable_nets.Remove(NET_IPV4);
723 : 1 : g_reachable_nets.Remove(NET_IPV6);
724 : 1 : g_reachable_nets.Remove(NET_ONION);
725 : 1 : g_reachable_nets.Remove(NET_I2P);
726 : 1 : g_reachable_nets.Remove(NET_CJDNS);
727 : :
728 [ + - + - ]: 2 : BOOST_CHECK(!g_reachable_nets.Contains(NET_IPV4));
729 [ + - + - ]: 2 : BOOST_CHECK(!g_reachable_nets.Contains(NET_IPV6));
730 [ + - + - ]: 2 : BOOST_CHECK(!g_reachable_nets.Contains(NET_ONION));
731 [ + - + - ]: 2 : BOOST_CHECK(!g_reachable_nets.Contains(NET_I2P));
732 [ + - + - ]: 2 : BOOST_CHECK(!g_reachable_nets.Contains(NET_CJDNS));
733 : :
734 : 1 : g_reachable_nets.Add(NET_IPV4);
735 : 1 : g_reachable_nets.Add(NET_IPV6);
736 : 1 : g_reachable_nets.Add(NET_ONION);
737 : 1 : g_reachable_nets.Add(NET_I2P);
738 : 1 : g_reachable_nets.Add(NET_CJDNS);
739 : :
740 [ + - + - ]: 2 : BOOST_CHECK(g_reachable_nets.Contains(NET_IPV4));
741 [ + - + - ]: 2 : BOOST_CHECK(g_reachable_nets.Contains(NET_IPV6));
742 [ + - + - ]: 2 : BOOST_CHECK(g_reachable_nets.Contains(NET_ONION));
743 [ + - + - ]: 2 : BOOST_CHECK(g_reachable_nets.Contains(NET_I2P));
744 [ + - + - ]: 2 : BOOST_CHECK(g_reachable_nets.Contains(NET_CJDNS));
745 : 1 : }
746 : :
747 [ + - + - : 7 : BOOST_AUTO_TEST_CASE(LimitedAndReachable_NetworkCaseUnroutableAndInternal)
+ - + - -
+ + - + -
+ - + - +
- + - - +
+ - + - +
- + - + -
+ - - + +
- + - + -
+ - + - +
- + - - +
+ - + - +
- + - + -
+ - - + +
- ]
748 : : {
749 : : // Should be reachable by default.
750 [ + - + - ]: 2 : BOOST_CHECK(g_reachable_nets.Contains(NET_UNROUTABLE));
751 [ + - + - ]: 2 : BOOST_CHECK(g_reachable_nets.Contains(NET_INTERNAL));
752 : :
753 : 1 : g_reachable_nets.RemoveAll();
754 : :
755 [ + - + - ]: 2 : BOOST_CHECK(!g_reachable_nets.Contains(NET_UNROUTABLE));
756 [ + - + - ]: 2 : BOOST_CHECK(!g_reachable_nets.Contains(NET_INTERNAL));
757 : :
758 : 1 : g_reachable_nets.Add(NET_IPV4);
759 : 1 : g_reachable_nets.Add(NET_IPV6);
760 : 1 : g_reachable_nets.Add(NET_ONION);
761 : 1 : g_reachable_nets.Add(NET_I2P);
762 : 1 : g_reachable_nets.Add(NET_CJDNS);
763 : 1 : g_reachable_nets.Add(NET_UNROUTABLE);
764 : 1 : g_reachable_nets.Add(NET_INTERNAL);
765 : 1 : }
766 : :
767 : 3 : CNetAddr UtilBuildAddress(unsigned char p1, unsigned char p2, unsigned char p3, unsigned char p4)
768 : : {
769 : 3 : unsigned char ip[] = {p1, p2, p3, p4};
770 : :
771 : 3 : struct sockaddr_in sa;
772 : 3 : memset(&sa, 0, sizeof(sockaddr_in)); // initialize the memory block
773 : 3 : memcpy(&(sa.sin_addr), &ip, sizeof(ip));
774 : 3 : return CNetAddr(sa.sin_addr);
775 : : }
776 : :
777 : :
778 [ + - + - : 7 : BOOST_AUTO_TEST_CASE(LimitedAndReachable_CNetAddr)
+ - + - -
+ + - + -
+ - + - +
- + - - +
+ - + - +
- + - + -
+ - - + +
- + - + -
+ - + - +
- + - - +
+ - + - +
- + - + -
+ - - + +
- ]
779 : : {
780 : 1 : CNetAddr addr = UtilBuildAddress(0x001, 0x001, 0x001, 0x001); // 1.1.1.1
781 : :
782 [ + - ]: 1 : g_reachable_nets.Add(NET_IPV4);
783 [ + - + - : 2 : BOOST_CHECK(g_reachable_nets.Contains(addr));
+ - + - ]
784 : :
785 [ + - ]: 1 : g_reachable_nets.Remove(NET_IPV4);
786 [ + - + - : 2 : BOOST_CHECK(!g_reachable_nets.Contains(addr));
+ - + - ]
787 : :
788 [ + - ]: 1 : g_reachable_nets.Add(NET_IPV4); // have to reset this, because this is stateful.
789 : 1 : }
790 : :
791 : :
792 [ + - + - : 7 : BOOST_AUTO_TEST_CASE(LocalAddress_BasicLifecycle)
+ - + - -
+ + - + -
+ - + - +
- + - - +
+ - + - +
- + - + -
+ - - + +
- + - + -
+ - + - +
- + - - +
+ - + - +
- + - + -
+ - - + +
- ]
793 : : {
794 [ + - ]: 1 : CService addr = CService(UtilBuildAddress(0x002, 0x001, 0x001, 0x001), 1000); // 2.1.1.1:1000
795 : :
796 [ + - ]: 1 : g_reachable_nets.Add(NET_IPV4);
797 : :
798 [ + - + - : 2 : BOOST_CHECK(!IsLocal(addr));
+ - + - ]
799 [ + - + - : 2 : BOOST_CHECK(AddLocal(addr, 1000));
+ - + - ]
800 [ + - + - : 2 : BOOST_CHECK(IsLocal(addr));
+ - + - ]
801 : :
802 [ + - ]: 1 : RemoveLocal(addr);
803 [ + - + - : 2 : BOOST_CHECK(!IsLocal(addr));
+ - ]
804 : 1 : }
805 : :
806 [ + - + - : 7 : BOOST_AUTO_TEST_CASE(LocalAddress_nScore_Overflow)
+ - + - -
+ + - + -
+ - + - +
- + - - +
+ - + - +
- + - + -
+ - - + +
- + - + -
+ - + - +
- + - - +
+ - + - +
- + - + -
+ - - + +
- ]
807 : : {
808 : 1 : g_reachable_nets.Add(NET_IPV4);
809 [ + - ]: 1 : const CService addr{UtilBuildAddress(0x002, 0x001, 0x001, 0x001), 1000}; // 2.1.1.1:1000
810 : :
811 : 6 : const auto get_score = [](const CService& service) -> int {
812 : 5 : LOCK(g_maplocalhost_mutex);
813 [ + - ]: 5 : const auto it = mapLocalHost.find(service);
814 [ + - ]: 5 : return it != mapLocalHost.end() ? it->second.nScore : 0;
815 : 5 : };
816 : :
817 : 1 : const int initial_score = 1000;
818 [ + - + - : 2 : BOOST_REQUIRE(AddLocal(addr, initial_score));
+ - + - ]
819 [ + - + - : 2 : BOOST_REQUIRE(IsLocal(addr));
+ - + - ]
820 [ + - + - : 1 : BOOST_CHECK_EQUAL(get_score(addr), initial_score);
+ - ]
821 : :
822 : : // SeenLocal should increment nScore by 1.
823 [ + - + - : 2 : BOOST_CHECK(SeenLocal(addr));
+ - + - ]
824 [ + - + - : 1 : BOOST_CHECK_EQUAL(get_score(addr), initial_score + 1);
+ - ]
825 : :
826 : : // AddLocal() saturates nScore when updating an existing entry at INT_MAX.
827 [ + - + - : 2 : BOOST_REQUIRE(AddLocal(addr, std::numeric_limits<int>::max()));
+ - + - ]
828 [ + - + - : 1 : BOOST_CHECK_EQUAL(get_score(addr), std::numeric_limits<int>::max());
+ - ]
829 : :
830 [ + - + - : 2 : BOOST_CHECK(AddLocal(addr, std::numeric_limits<int>::max()));
+ - + - ]
831 [ + - + - : 1 : BOOST_CHECK_EQUAL(get_score(addr), std::numeric_limits<int>::max());
+ - ]
832 : :
833 : : // SeenLocal() also saturates at INT_MAX.
834 [ + - + - : 2 : BOOST_CHECK(SeenLocal(addr));
+ - + - ]
835 [ + - + - : 1 : BOOST_CHECK_EQUAL(get_score(addr), std::numeric_limits<int>::max());
+ - ]
836 : :
837 [ + - ]: 1 : RemoveLocal(addr);
838 [ + - + - : 2 : BOOST_CHECK(!IsLocal(addr));
+ - ]
839 : 1 : }
840 : :
841 [ + - + - : 7 : BOOST_AUTO_TEST_CASE(initial_advertise_from_version_message)
+ - + - -
+ + - + -
+ - + - +
- + - - +
+ - + - +
- + - + -
+ - - + +
- + - + -
+ - + - +
- + - - +
+ - + - +
- + - + -
+ - - + +
- ]
842 : : {
843 : 1 : LOCK(NetEventsInterface::g_msgproc_mutex);
844 [ + - ]: 1 : auto& connman{static_cast<ConnmanTestMsg&>(*m_node.connman)};
845 : :
846 : : // Tests the following scenario:
847 : : // * -bind=3.4.5.6:20001 is specified
848 : : // * we make an outbound connection to a peer
849 : : // * the peer reports he sees us as 2.3.4.5:20002 in the version message
850 : : // (20002 is a random port assigned by our OS for the outgoing TCP connection,
851 : : // we cannot accept connections to it)
852 : : // * we should self-advertise to that peer as 2.3.4.5:20001
853 : :
854 : : // Pretend that we bound to this port.
855 : 1 : const uint16_t bind_port = 20001;
856 [ + - + - : 2 : m_node.args->ForceSetArg("-bind", strprintf("3.4.5.6:%u", bind_port));
+ - ]
857 [ + - ]: 1 : m_node.connman->SetCaptureMessages(true);
858 : :
859 : : // Our address:port as seen from the peer - 2.3.4.5:20002 (different from the above).
860 : 1 : in_addr peer_us_addr;
861 : 1 : peer_us_addr.s_addr = htonl(0x02030405);
862 [ + - ]: 1 : const CService peer_us{peer_us_addr, 20002};
863 : :
864 : : // Create a peer with a routable IPv4 address.
865 : 1 : in_addr peer_in_addr;
866 : 1 : peer_in_addr.s_addr = htonl(0x01020304);
867 : 1 : CNode peer{/*id=*/0,
868 : : /*sock=*/nullptr,
869 [ - + ]: 1 : /*addrIn=*/CAddress{CService{peer_in_addr, 8333}, NODE_NETWORK},
870 : : /*nKeyedNetGroupIn=*/0,
871 : : /*nLocalHostNonceIn=*/0,
872 [ + - ]: 2 : /*addrBindIn=*/CService{},
873 : 1 : /*addrNameIn=*/std::string{},
874 : : /*conn_type_in=*/ConnectionType::OUTBOUND_FULL_RELAY,
875 : : /*inbound_onion=*/false,
876 [ + - + - ]: 2 : /*network_key=*/2};
877 : :
878 : 1 : const uint64_t services{NODE_NETWORK | NODE_WITNESS};
879 : 1 : const int64_t time{0};
880 : :
881 : : // Force ChainstateManager::IsInitialBlockDownload() to return false.
882 : : // Otherwise PushAddress() isn't called by PeerManager::ProcessMessage().
883 [ + - ]: 1 : auto& chainman = static_cast<TestChainstateManager&>(*m_node.chainman);
884 [ + - ]: 1 : chainman.JumpOutOfIbd();
885 : :
886 [ + - ]: 1 : m_node.peerman->InitializeNode(peer, NODE_NETWORK);
887 : :
888 [ + - ]: 1 : m_node.peerman->SendMessages(peer);
889 [ + - ]: 1 : connman.FlushSendBuffer(peer); // Drop sent version message
890 : :
891 [ + - ]: 1 : auto msg_version_receive =
892 [ + - + - ]: 1 : NetMsg::Make(NetMsgType::VERSION, PROTOCOL_VERSION, services, time, services, CAddress::V1_NETWORK(peer_us));
893 [ + - - + ]: 1 : Assert(connman.ReceiveMsgFrom(peer, std::move(msg_version_receive)));
894 [ + - ]: 1 : peer.fPauseSend = false;
895 [ + - ]: 1 : bool more_work{connman.ProcessMessagesOnce(peer)};
896 [ - + ]: 1 : Assert(!more_work);
897 : :
898 [ + - ]: 1 : m_node.peerman->SendMessages(peer);
899 [ + - ]: 1 : connman.FlushSendBuffer(peer); // Drop sent verack message
900 : :
901 [ + - + - : 2 : Assert(connman.ReceiveMsgFrom(peer, NetMsg::Make(NetMsgType::VERACK)));
+ - - + ]
902 [ + - ]: 1 : peer.fPauseSend = false;
903 : : // Will set peer.fSuccessfullyConnected to true (necessary in SendMessages()).
904 [ + - ]: 1 : more_work = connman.ProcessMessagesOnce(peer);
905 [ - + ]: 1 : Assert(!more_work);
906 : :
907 : : // Ensure that peer_us_addr:bind_port is sent to the peer.
908 [ + - ]: 1 : const CService expected{peer_us_addr, bind_port};
909 : 1 : bool sent{false};
910 : :
911 [ + - ]: 1 : const auto CaptureMessageOrig = CaptureMessage;
912 : 5 : CaptureMessage = [&sent, &expected](const CAddress& addr,
913 : : const std::string& msg_type,
914 : : std::span<const unsigned char> data,
915 : : bool is_incoming) -> void {
916 [ + - + + ]: 4 : if (!is_incoming && msg_type == "addr") {
917 : 1 : std::vector<CAddress> addresses;
918 : :
919 [ + - ]: 1 : SpanReader{data} >> CAddress::V1_NETWORK(addresses);
920 : :
921 [ + - ]: 1 : for (const auto& addr : addresses) {
922 [ + - + - ]: 1 : if (addr == expected) {
923 : 1 : sent = true;
924 : 1 : return;
925 : : }
926 : : }
927 : 1 : }
928 : 1 : };
929 : :
930 [ + - ]: 1 : m_node.peerman->SendMessages(peer);
931 : :
932 [ + - + - : 2 : BOOST_CHECK(sent);
+ - ]
933 : :
934 [ + - ]: 1 : CaptureMessage = CaptureMessageOrig;
935 [ + - ]: 1 : chainman.ResetIbd();
936 [ + - ]: 1 : m_node.connman->SetCaptureMessages(false);
937 [ + - + - : 2 : m_node.args->ForceSetArg("-bind", "");
+ - ]
938 [ + - ]: 3 : }
939 : :
940 : :
941 [ + - + - : 7 : BOOST_AUTO_TEST_CASE(advertise_local_address)
+ - + - -
+ + - + -
+ - + - +
- + - - +
+ - + - +
- + - + -
+ - - + +
- + - + -
+ - + - +
- + - - +
+ - + - +
- + - + -
+ - - + +
- ]
942 : : {
943 : 8 : auto CreatePeer = [](const CAddress& addr) {
944 : 14 : return std::make_unique<CNode>(/*id=*/0,
945 : 7 : /*sock=*/nullptr,
946 : : addr,
947 : 7 : /*nKeyedNetGroupIn=*/0,
948 : 7 : /*nLocalHostNonceIn=*/0,
949 : 7 : CAddress{},
950 [ + - ]: 7 : /*pszDest=*/std::string{},
951 : 7 : ConnectionType::OUTBOUND_FULL_RELAY,
952 : 7 : /*inbound_onion=*/false,
953 [ + - + - ]: 14 : /*network_key=*/0);
954 : : };
955 : 1 : g_reachable_nets.Add(NET_CJDNS);
956 : :
957 [ + - + - ]: 2 : CAddress addr_ipv4{Lookup("1.2.3.4", 8333, false).value(), NODE_NONE};
958 [ + - + - : 2 : BOOST_REQUIRE(addr_ipv4.IsValid());
+ - + - ]
959 [ + - + - : 2 : BOOST_REQUIRE(addr_ipv4.IsIPv4());
+ - ]
960 : :
961 [ + - + - : 2 : CAddress addr_ipv6{Lookup("1122:3344:5566:7788:9900:aabb:ccdd:eeff", 8333, false).value(), NODE_NONE};
+ - ]
962 [ + - + - : 2 : BOOST_REQUIRE(addr_ipv6.IsValid());
+ - + - ]
963 [ + - + - : 2 : BOOST_REQUIRE(addr_ipv6.IsIPv6());
+ - ]
964 : :
965 [ + - + - : 2 : CAddress addr_ipv6_tunnel{Lookup("2002:3344:5566:7788:9900:aabb:ccdd:eeff", 8333, false).value(), NODE_NONE};
+ - ]
966 [ + - + - : 2 : BOOST_REQUIRE(addr_ipv6_tunnel.IsValid());
+ - + - ]
967 [ + - + - : 2 : BOOST_REQUIRE(addr_ipv6_tunnel.IsIPv6());
+ - ]
968 [ + - + - : 2 : BOOST_REQUIRE(addr_ipv6_tunnel.IsRFC3964());
+ - + - ]
969 : :
970 [ + - + - : 2 : CAddress addr_teredo{Lookup("2001:0000:5566:7788:9900:aabb:ccdd:eeff", 8333, false).value(), NODE_NONE};
+ - ]
971 [ + - + - : 2 : BOOST_REQUIRE(addr_teredo.IsValid());
+ - + - ]
972 [ + - + - : 2 : BOOST_REQUIRE(addr_teredo.IsIPv6());
+ - ]
973 [ + - + - : 2 : BOOST_REQUIRE(addr_teredo.IsRFC4380());
+ - + - ]
974 : :
975 [ + - ]: 1 : CAddress addr_onion;
976 [ + - + - : 2 : BOOST_REQUIRE(addr_onion.SetSpecial("pg6mmjiyjmcrsslvykfwnntlaru7p5svn6y2ymmju6nubxndf4pscryd.onion"));
+ - + - ]
977 [ + - + - : 2 : BOOST_REQUIRE(addr_onion.IsValid());
+ - + - ]
978 [ + - + - : 2 : BOOST_REQUIRE(addr_onion.IsTor());
+ - ]
979 : :
980 [ + - ]: 1 : CAddress addr_i2p;
981 [ + - + - : 2 : BOOST_REQUIRE(addr_i2p.SetSpecial("udhdrtrcetjm5sxzskjyr5ztpeszydbh4dpl3pl4utgqqw2v4jna.b32.i2p"));
+ - + - ]
982 [ + - + - : 2 : BOOST_REQUIRE(addr_i2p.IsValid());
+ - + - ]
983 [ + - + - : 2 : BOOST_REQUIRE(addr_i2p.IsI2P());
+ - ]
984 : :
985 [ + - + - : 3 : CService service_cjdns{Lookup("fc00:3344:5566:7788:9900:aabb:ccdd:eeff", 8333, false).value(), NODE_NONE};
+ - + - ]
986 [ + - ]: 2 : CAddress addr_cjdns{MaybeFlipIPv6toCJDNS(service_cjdns), NODE_NONE};
987 [ + - + - : 2 : BOOST_REQUIRE(addr_cjdns.IsValid());
+ - + - ]
988 [ + - + - : 2 : BOOST_REQUIRE(addr_cjdns.IsCJDNS());
+ - ]
989 : :
990 [ + - ]: 1 : const auto peer_ipv4{CreatePeer(addr_ipv4)};
991 [ + - ]: 1 : const auto peer_ipv6{CreatePeer(addr_ipv6)};
992 [ + - ]: 1 : const auto peer_ipv6_tunnel{CreatePeer(addr_ipv6_tunnel)};
993 [ + - ]: 1 : const auto peer_teredo{CreatePeer(addr_teredo)};
994 [ + - ]: 1 : const auto peer_onion{CreatePeer(addr_onion)};
995 [ + - ]: 1 : const auto peer_i2p{CreatePeer(addr_i2p)};
996 [ + - ]: 1 : const auto peer_cjdns{CreatePeer(addr_cjdns)};
997 : :
998 : : // one local clearnet address - advertise to all but privacy peers
999 [ + - ]: 1 : AddLocal(addr_ipv4);
1000 [ + - + - : 2 : BOOST_CHECK(GetLocalAddress(*peer_ipv4) == addr_ipv4);
+ - + - +
- ]
1001 [ + - + - : 2 : BOOST_CHECK(GetLocalAddress(*peer_ipv6) == addr_ipv4);
+ - + - +
- ]
1002 [ + - + - : 2 : BOOST_CHECK(GetLocalAddress(*peer_ipv6_tunnel) == addr_ipv4);
+ - + - +
- ]
1003 [ + - + - : 2 : BOOST_CHECK(GetLocalAddress(*peer_teredo) == addr_ipv4);
+ - + - +
- ]
1004 [ + - + - : 2 : BOOST_CHECK(GetLocalAddress(*peer_cjdns) == addr_ipv4);
+ - + - +
- ]
1005 [ + - + - : 2 : BOOST_CHECK(!GetLocalAddress(*peer_onion).IsValid());
+ - + - +
- ]
1006 [ + - + - : 2 : BOOST_CHECK(!GetLocalAddress(*peer_i2p).IsValid());
+ - + - +
- ]
1007 [ + - ]: 1 : RemoveLocal(addr_ipv4);
1008 : :
1009 : : // local privacy addresses - don't advertise to clearnet peers
1010 [ + - ]: 1 : AddLocal(addr_onion);
1011 [ + - ]: 1 : AddLocal(addr_i2p);
1012 [ + - + - : 2 : BOOST_CHECK(!GetLocalAddress(*peer_ipv4).IsValid());
+ - + - +
- ]
1013 [ + - + - : 2 : BOOST_CHECK(!GetLocalAddress(*peer_ipv6).IsValid());
+ - + - +
- ]
1014 [ + - + - : 2 : BOOST_CHECK(!GetLocalAddress(*peer_ipv6_tunnel).IsValid());
+ - + - +
- ]
1015 [ + - + - : 2 : BOOST_CHECK(!GetLocalAddress(*peer_teredo).IsValid());
+ - + - +
- ]
1016 [ + - + - : 2 : BOOST_CHECK(!GetLocalAddress(*peer_cjdns).IsValid());
+ - + - +
- ]
1017 [ + - + - : 2 : BOOST_CHECK(GetLocalAddress(*peer_onion) == addr_onion);
+ - + - +
- ]
1018 [ + - + - : 2 : BOOST_CHECK(GetLocalAddress(*peer_i2p) == addr_i2p);
+ - + - +
- ]
1019 [ + - ]: 1 : RemoveLocal(addr_onion);
1020 [ + - ]: 1 : RemoveLocal(addr_i2p);
1021 : :
1022 : : // local addresses from all networks
1023 [ + - ]: 1 : AddLocal(addr_ipv4);
1024 [ + - ]: 1 : AddLocal(addr_ipv6);
1025 [ + - ]: 1 : AddLocal(addr_ipv6_tunnel);
1026 [ + - ]: 1 : AddLocal(addr_teredo);
1027 [ + - ]: 1 : AddLocal(addr_onion);
1028 [ + - ]: 1 : AddLocal(addr_i2p);
1029 [ + - ]: 1 : AddLocal(addr_cjdns);
1030 [ + - + - : 2 : BOOST_CHECK(GetLocalAddress(*peer_ipv4) == addr_ipv4);
+ - + - +
- ]
1031 [ + - + - : 2 : BOOST_CHECK(GetLocalAddress(*peer_ipv6) == addr_ipv6);
+ - + - +
- ]
1032 [ + - + - : 2 : BOOST_CHECK(GetLocalAddress(*peer_ipv6_tunnel) == addr_ipv6);
+ - + - +
- ]
1033 [ + - + - : 2 : BOOST_CHECK(GetLocalAddress(*peer_teredo) == addr_ipv4);
+ - + - +
- ]
1034 [ + - + - : 2 : BOOST_CHECK(GetLocalAddress(*peer_onion) == addr_onion);
+ - + - +
- ]
1035 [ + - + - : 2 : BOOST_CHECK(GetLocalAddress(*peer_i2p) == addr_i2p);
+ - + - +
- ]
1036 [ + - + - : 2 : BOOST_CHECK(GetLocalAddress(*peer_cjdns) == addr_cjdns);
+ - + - +
- ]
1037 [ + - ]: 1 : RemoveLocal(addr_ipv4);
1038 [ + - ]: 1 : RemoveLocal(addr_ipv6);
1039 [ + - ]: 1 : RemoveLocal(addr_ipv6_tunnel);
1040 [ + - ]: 1 : RemoveLocal(addr_teredo);
1041 [ + - ]: 1 : RemoveLocal(addr_onion);
1042 [ + - ]: 1 : RemoveLocal(addr_i2p);
1043 [ + - ]: 1 : RemoveLocal(addr_cjdns);
1044 : 1 : }
1045 : :
1046 : : namespace {
1047 : :
1048 : 75 : CKey GenerateRandomTestKey(FastRandomContext& rng) noexcept
1049 : : {
1050 : 75 : CKey key;
1051 : 75 : uint256 key_data = rng.rand256();
1052 : 75 : key.Set(key_data.begin(), key_data.end(), true);
1053 : 75 : return key;
1054 : : }
1055 : :
1056 : : /** A class for scenario-based tests of V2Transport
1057 : : *
1058 : : * Each V2TransportTester encapsulates a V2Transport (the one being tested), and can be told to
1059 : : * interact with it. To do so, it also encapsulates a BIP324Cipher to act as the other side. A
1060 : : * second V2Transport is not used, as doing so would not permit scenarios that involve sending
1061 : : * invalid data, or ones using BIP324 features that are not implemented on the sending
1062 : : * side (like decoy packets).
1063 : : */
1064 : : class V2TransportTester
1065 : : {
1066 : : FastRandomContext& m_rng;
1067 : : V2Transport m_transport; //!< V2Transport being tested
1068 : : BIP324Cipher m_cipher; //!< Cipher to help with the other side
1069 : : bool m_test_initiator; //!< Whether m_transport is the initiator (true) or responder (false)
1070 : :
1071 : : std::vector<uint8_t> m_sent_garbage; //!< The garbage we've sent to m_transport.
1072 : : std::vector<uint8_t> m_recv_garbage; //!< The garbage we've received from m_transport.
1073 : : std::vector<uint8_t> m_to_send; //!< Bytes we have queued up to send to m_transport.
1074 : : std::vector<uint8_t> m_received; //!< Bytes we have received from m_transport.
1075 : : std::deque<CSerializedNetMsg> m_msg_to_send; //!< Messages to be sent *by* m_transport to us.
1076 : : bool m_sent_aad{false};
1077 : :
1078 : : public:
1079 : : /** Construct a tester object. test_initiator: whether the tested transport is initiator. */
1080 : 75 : explicit V2TransportTester(FastRandomContext& rng, bool test_initiator)
1081 : 75 : : m_rng{rng},
1082 : 75 : m_transport{0, test_initiator},
1083 : 75 : m_cipher{GenerateRandomTestKey(m_rng), MakeByteSpan(m_rng.rand256())},
1084 [ + - ]: 75 : m_test_initiator(test_initiator) {}
1085 : :
1086 : : /** Data type returned by Interact:
1087 : : *
1088 : : * - std::nullopt: transport error occurred
1089 : : * - otherwise: a vector of
1090 : : * - std::nullopt: invalid message received
1091 : : * - otherwise: a CNetMessage retrieved
1092 : : */
1093 : : using InteractResult = std::optional<std::vector<std::optional<CNetMessage>>>;
1094 : :
1095 : : /** Send/receive scheduled/available bytes and messages.
1096 : : *
1097 : : * This is the only function that interacts with the transport being tested; everything else is
1098 : : * scheduling things done by Interact(), or processing things learned by it.
1099 : : */
1100 : 239 : InteractResult Interact()
1101 : : {
1102 : 239 : std::vector<std::optional<CNetMessage>> ret;
1103 : 2882 : while (true) {
1104 : 2882 : bool progress{false};
1105 : : // Send bytes from m_to_send to the transport.
1106 [ + + ]: 2882 : if (!m_to_send.empty()) {
1107 [ - + ]: 2199 : std::span<const uint8_t> to_send = std::span{m_to_send}.first(1 + m_rng.randrange(m_to_send.size()));
1108 : 2199 : size_t old_len = to_send.size();
1109 [ + + ]: 2199 : if (!m_transport.ReceivedBytes(to_send)) {
1110 : 23 : return std::nullopt; // transport error occurred
1111 : : }
1112 [ + + ]: 2176 : if (old_len != to_send.size()) {
1113 : 2087 : progress = true;
1114 : 2087 : m_to_send.erase(m_to_send.begin(), m_to_send.begin() + (old_len - to_send.size()));
1115 : : }
1116 : : }
1117 : : // Retrieve messages received by the transport.
1118 [ + + + + : 2859 : if (m_transport.ReceivedMessageComplete() && (!progress || m_rng.randbool())) {
+ + ]
1119 : 252 : bool reject{false};
1120 : 252 : auto msg = m_transport.GetReceivedMessage({}, reject);
1121 [ + + ]: 252 : if (reject) {
1122 [ + - ]: 61 : ret.emplace_back(std::nullopt);
1123 : : } else {
1124 [ + - ]: 191 : ret.emplace_back(std::move(msg));
1125 : : }
1126 : 252 : progress = true;
1127 : 252 : }
1128 : : // Enqueue a message to be sent by the transport to us.
1129 [ + + + - : 2859 : if (!m_msg_to_send.empty() && (!progress || m_rng.randbool())) {
+ + ]
1130 [ + - ]: 51 : if (m_transport.SetMessageToSend(m_msg_to_send.front())) {
1131 : 51 : m_msg_to_send.pop_front();
1132 : 51 : progress = true;
1133 : : }
1134 : : }
1135 : : // Receive bytes from the transport.
1136 [ + + ]: 2859 : const auto& [recv_bytes, _more, _msg_type] = m_transport.GetBytesToSend(!m_msg_to_send.empty());
1137 [ + + + + : 2859 : if (!recv_bytes.empty() && (!progress || m_rng.randbool())) {
+ + ]
1138 : 889 : size_t to_receive = 1 + m_rng.randrange(recv_bytes.size());
1139 [ + - ]: 889 : m_received.insert(m_received.end(), recv_bytes.begin(), recv_bytes.begin() + to_receive);
1140 : 889 : progress = true;
1141 : 889 : m_transport.MarkBytesSent(to_receive);
1142 : : }
1143 [ + + ]: 2859 : if (!progress) break;
1144 : : }
1145 : 216 : return ret;
1146 : 239 : }
1147 : :
1148 : : /** Expose the cipher. */
1149 : : BIP324Cipher& GetCipher() { return m_cipher; }
1150 : :
1151 : : /** Schedule bytes to be sent to the transport. */
1152 : 48177 : void Send(std::span<const uint8_t> data)
1153 : : {
1154 : 48177 : m_to_send.insert(m_to_send.end(), data.begin(), data.end());
1155 : 48177 : }
1156 : :
1157 : : /** Send V1 version message header to the transport. */
1158 : 2 : void SendV1Version(const MessageStartChars& magic)
1159 : : {
1160 : 2 : CMessageHeader hdr(magic, "version", 126 + m_rng.randrange(11));
1161 : 2 : DataStream ser{};
1162 [ + - ]: 2 : ser << hdr;
1163 [ - + + - ]: 2 : m_to_send.insert(m_to_send.end(), UCharCast(ser.data()), UCharCast(ser.data() + ser.size()));
1164 : 2 : }
1165 : :
1166 : : /** Schedule bytes to be sent to the transport. */
1167 : 48104 : void Send(std::span<const std::byte> data) { Send(MakeUCharSpan(data)); }
1168 : :
1169 : : /** Schedule our ellswift key to be sent to the transport. */
1170 : 73 : void SendKey() { Send(m_cipher.GetOurPubKey()); }
1171 : :
1172 : : /** Schedule specified garbage to be sent to the transport. */
1173 : 73 : void SendGarbage(std::span<const uint8_t> garbage)
1174 : : {
1175 : : // Remember the specified garbage (so we can use it as AAD).
1176 : 73 : m_sent_garbage.assign(garbage.begin(), garbage.end());
1177 : : // Schedule it for sending.
1178 [ - + ]: 73 : Send(m_sent_garbage);
1179 : 73 : }
1180 : :
1181 : : /** Schedule garbage (of specified length) to be sent to the transport. */
1182 : 72 : void SendGarbage(size_t garbage_len)
1183 : : {
1184 : : // Generate random garbage and send it.
1185 [ - + + - ]: 72 : SendGarbage(m_rng.randbytes<uint8_t>(garbage_len));
1186 : 72 : }
1187 : :
1188 : : /** Schedule garbage (with valid random length) to be sent to the transport. */
1189 : 20 : void SendGarbage()
1190 : : {
1191 : 20 : SendGarbage(m_rng.randrange(V2Transport::MAX_GARBAGE_LEN + 1));
1192 : 20 : }
1193 : :
1194 : : /** Schedule a message to be sent to us by the transport. */
1195 : 51 : void AddMessage(std::string m_type, std::vector<uint8_t> payload)
1196 : : {
1197 : 51 : CSerializedNetMsg msg;
1198 : 51 : msg.m_type = std::move(m_type);
1199 : 51 : msg.data = std::move(payload);
1200 [ + - ]: 51 : m_msg_to_send.push_back(std::move(msg));
1201 : 51 : }
1202 : :
1203 : : /** Expect ellswift key to have been received from transport and process it.
1204 : : *
1205 : : * Many other V2TransportTester functions cannot be called until after ReceiveKey() has been
1206 : : * called, as no encryption keys are set up before that point.
1207 : : */
1208 : 73 : void ReceiveKey()
1209 : : {
1210 : : // When processing a key, enough bytes need to have been received already.
1211 [ - + + - ]: 146 : BOOST_REQUIRE(m_received.size() >= EllSwiftPubKey::size());
1212 : : // Initialize the cipher using it (acting as the opposite side of the tested transport).
1213 : 73 : m_cipher.Initialize(MakeByteSpan(m_received).first(EllSwiftPubKey::size()), !m_test_initiator);
1214 : : // Strip the processed bytes off the front of the receive buffer.
1215 : 73 : m_received.erase(m_received.begin(), m_received.begin() + EllSwiftPubKey::size());
1216 : 73 : }
1217 : :
1218 : : /** Schedule an encrypted packet with specified content/aad/ignore to be sent to transport
1219 : : * (only after ReceiveKey). */
1220 : 47958 : void SendPacket(std::span<const uint8_t> content, std::span<const uint8_t> aad = {}, bool ignore = false)
1221 : : {
1222 : : // Use cipher to construct ciphertext.
1223 : 47958 : std::vector<std::byte> ciphertext;
1224 [ + - ]: 47958 : ciphertext.resize(content.size() + BIP324Cipher::EXPANSION);
1225 [ - + ]: 47958 : m_cipher.Encrypt(
1226 : : /*contents=*/MakeByteSpan(content),
1227 : : /*aad=*/MakeByteSpan(aad),
1228 : : /*ignore=*/ignore,
1229 : 47958 : /*output=*/ciphertext);
1230 : : // Schedule it for sending.
1231 [ - + + - ]: 47958 : Send(ciphertext);
1232 : 47958 : }
1233 : :
1234 : : /** Schedule garbage terminator to be sent to the transport (only after ReceiveKey). */
1235 : 73 : void SendGarbageTerm()
1236 : : {
1237 : : // Schedule the garbage terminator to be sent.
1238 : 73 : Send(m_cipher.GetSendGarbageTerminator());
1239 : 73 : }
1240 : :
1241 : : /** Schedule version packet to be sent to the transport (only after ReceiveKey). */
1242 : 290 : void SendVersion(std::span<const uint8_t> version_data = {}, bool vers_ignore = false)
1243 : : {
1244 : 290 : std::span<const std::uint8_t> aad;
1245 : : // Set AAD to garbage only for first packet.
1246 [ + + - + ]: 290 : if (!m_sent_aad) aad = m_sent_garbage;
1247 : 290 : SendPacket(/*content=*/version_data, /*aad=*/aad, /*ignore=*/vers_ignore);
1248 : 290 : m_sent_aad = true;
1249 : 290 : }
1250 : :
1251 : : /** Expect a packet to have been received from transport, process it, and return its contents
1252 : : * (only after ReceiveKey). Decoys are skipped. Optional associated authenticated data (AAD) is
1253 : : * expected in the first received packet, no matter if that is a decoy or not. */
1254 : 122 : std::vector<uint8_t> ReceivePacket(std::span<const std::byte> aad = {})
1255 : : {
1256 : 122 : std::vector<uint8_t> contents;
1257 : : // Loop as long as there are ignored packets that are to be skipped.
1258 : 122 : while (true) {
1259 : : // When processing a packet, at least enough bytes for its length descriptor must be received.
1260 [ + - - + : 244 : BOOST_REQUIRE(m_received.size() >= BIP324Cipher::LENGTH_LEN);
+ - - + ]
1261 : : // Decrypt the content length.
1262 [ - + ]: 122 : size_t size = m_cipher.DecryptLength(MakeByteSpan(std::span{m_received}.first(BIP324Cipher::LENGTH_LEN)));
1263 : : // Check that the full packet is in the receive buffer.
1264 [ + - - + : 244 : BOOST_REQUIRE(m_received.size() >= size + BIP324Cipher::EXPANSION);
+ - + - ]
1265 : : // Decrypt the packet contents.
1266 [ + - ]: 122 : contents.resize(size);
1267 : 122 : bool ignore{false};
1268 : 122 : bool ret = m_cipher.Decrypt(
1269 : : /*input=*/MakeByteSpan(
1270 [ - + ]: 122 : std::span{m_received}.first(size + BIP324Cipher::EXPANSION).subspan(BIP324Cipher::LENGTH_LEN)),
1271 : : /*aad=*/aad,
1272 : : /*ignore=*/ignore,
1273 : : /*contents=*/MakeWritableByteSpan(contents));
1274 [ + - + - ]: 244 : BOOST_CHECK(ret);
1275 : : // Don't expect AAD in further packets.
1276 : 122 : aad = {};
1277 : : // Strip the processed packet's bytes off the front of the receive buffer.
1278 : 122 : m_received.erase(m_received.begin(), m_received.begin() + size + BIP324Cipher::EXPANSION);
1279 : : // Stop if the ignore bit is not set on this packet.
1280 [ - + ]: 122 : if (!ignore) break;
1281 : : }
1282 : 122 : return contents;
1283 : 0 : }
1284 : :
1285 : : /** Expect garbage and garbage terminator to have been received, and process them (only after
1286 : : * ReceiveKey). */
1287 : 71 : void ReceiveGarbage()
1288 : : {
1289 : : // Figure out the garbage length.
1290 : 71 : size_t garblen;
1291 [ + - ]: 137293 : for (garblen = 0; garblen <= V2Transport::MAX_GARBAGE_LEN; ++garblen) {
1292 [ - + + - : 274586 : BOOST_REQUIRE(m_received.size() >= garblen + BIP324Cipher::GARBAGE_TERMINATOR_LEN);
- + ]
1293 [ - + + + ]: 137293 : auto term_span = MakeByteSpan(std::span{m_received}.subspan(garblen, BIP324Cipher::GARBAGE_TERMINATOR_LEN));
1294 [ + + ]: 137293 : if (std::ranges::equal(term_span, m_cipher.GetReceiveGarbageTerminator())) break;
1295 : : }
1296 : : // Copy the garbage to a buffer.
1297 : 71 : m_recv_garbage.assign(m_received.begin(), m_received.begin() + garblen);
1298 : : // Strip garbage + garbage terminator off the front of the receive buffer.
1299 : 71 : m_received.erase(m_received.begin(), m_received.begin() + garblen + BIP324Cipher::GARBAGE_TERMINATOR_LEN);
1300 : 71 : }
1301 : :
1302 : : /** Expect version packet to have been received, and process it (only after ReceiveKey). */
1303 : 71 : void ReceiveVersion()
1304 : : {
1305 : 71 : auto contents = ReceivePacket(/*aad=*/MakeByteSpan(m_recv_garbage));
1306 : : // Version packets from real BIP324 peers are expected to be empty, despite the fact that
1307 : : // this class supports *sending* non-empty version packets (to test that BIP324 peers
1308 : : // correctly ignore version packet contents).
1309 [ + - + - ]: 142 : BOOST_CHECK(contents.empty());
1310 : 71 : }
1311 : :
1312 : : /** Expect application packet to have been received, with specified short id and payload.
1313 : : * (only after ReceiveKey). */
1314 : 1 : void ReceiveMessage(uint8_t short_id, std::span<const uint8_t> payload)
1315 : : {
1316 : 1 : auto ret = ReceivePacket();
1317 [ + - - + : 2 : BOOST_CHECK(ret.size() == payload.size() + 1);
+ - + - ]
1318 [ + - + - : 2 : BOOST_CHECK(ret[0] == short_id);
+ - ]
1319 [ + - - + : 2 : BOOST_CHECK(std::ranges::equal(std::span{ret}.subspan(1), payload));
+ - ]
1320 : 1 : }
1321 : :
1322 : : /** Expect application packet to have been received, with specified 12-char message type and
1323 : : * payload (only after ReceiveKey). */
1324 : 50 : void ReceiveMessage(const std::string& m_type, std::span<const uint8_t> payload)
1325 : : {
1326 : 50 : auto ret = ReceivePacket();
1327 [ + - - + : 100 : BOOST_REQUIRE(ret.size() == payload.size() + 1 + CMessageHeader::MESSAGE_TYPE_SIZE);
+ - + - ]
1328 [ + - + - ]: 100 : BOOST_CHECK(ret[0] == 0);
1329 [ + + ]: 650 : for (unsigned i = 0; i < 12; ++i) {
1330 [ - + + + ]: 600 : if (i < m_type.size()) {
1331 [ + - + - ]: 600 : BOOST_CHECK(ret[1 + i] == m_type[i]);
1332 : : } else {
1333 [ + - + - ]: 900 : BOOST_CHECK(ret[1 + i] == 0);
1334 : : }
1335 : : }
1336 [ + - - + : 100 : BOOST_CHECK(std::ranges::equal(std::span{ret}.subspan(1 + CMessageHeader::MESSAGE_TYPE_SIZE), payload));
+ - ]
1337 : 50 : }
1338 : :
1339 : : /** Schedule an encrypted packet with specified message type and payload to be sent to
1340 : : * transport (only after ReceiveKey). */
1341 : 120 : void SendMessage(std::string mtype, std::span<const uint8_t> payload)
1342 : : {
1343 : : // Construct contents consisting of 0x00 + 12-byte message type + payload.
1344 : 120 : std::vector<uint8_t> contents(1 + CMessageHeader::MESSAGE_TYPE_SIZE + payload.size());
1345 [ - + ]: 120 : std::copy(mtype.begin(), mtype.end(), contents.begin() + 1);
1346 : 120 : std::copy(payload.begin(), payload.end(), contents.begin() + 1 + CMessageHeader::MESSAGE_TYPE_SIZE);
1347 : : // Send a packet with that as contents.
1348 [ - + + - ]: 120 : SendPacket(contents);
1349 : 120 : }
1350 : :
1351 : : /** Schedule an encrypted packet with specified short message id and payload to be sent to
1352 : : * transport (only after ReceiveKey). */
1353 : 152 : void SendMessage(uint8_t short_id, std::span<const uint8_t> payload)
1354 : : {
1355 : : // Construct contents consisting of short_id + payload.
1356 : 152 : std::vector<uint8_t> contents(1 + payload.size());
1357 : 152 : contents[0] = short_id;
1358 : 152 : std::copy(payload.begin(), payload.end(), contents.begin() + 1);
1359 : : // Send a packet with that as contents.
1360 [ - + + - ]: 152 : SendPacket(contents);
1361 : 152 : }
1362 : :
1363 : : /** Test whether the transport's session ID matches the session ID we expect. */
1364 : 71 : void CompareSessionIDs() const
1365 : : {
1366 : 71 : auto info = m_transport.GetInfo();
1367 [ + - ]: 142 : BOOST_CHECK(info.session_id);
1368 [ + - ]: 142 : BOOST_CHECK(uint256(MakeUCharSpan(m_cipher.GetSessionID())) == *info.session_id);
1369 : 71 : }
1370 : :
1371 : : /** Introduce a bit error in the data scheduled to be sent. */
1372 : 10 : void Damage()
1373 : : {
1374 [ - + ]: 10 : m_to_send[m_rng.randrange(m_to_send.size())] ^= (uint8_t{1} << m_rng.randrange(8));
1375 : 10 : }
1376 : : };
1377 : :
1378 : : } // namespace
1379 : :
1380 [ + - + - : 7 : BOOST_AUTO_TEST_CASE(v2transport_test)
+ - + - -
+ + - + -
+ - + - +
- + - - +
+ - + - +
- + - + -
+ - - + +
- + - + -
+ - + - +
- + - - +
+ - + - +
- + - + -
+ - - + +
- ]
1381 : : {
1382 : : // A mostly normal scenario, testing a transport in initiator mode.
1383 [ + + ]: 11 : for (int i = 0; i < 10; ++i) {
1384 : 10 : V2TransportTester tester(m_rng, true);
1385 [ + - ]: 10 : auto ret = tester.Interact();
1386 [ + - + - : 20 : BOOST_REQUIRE(ret && ret->empty());
- + + - +
- ]
1387 [ + - ]: 10 : tester.SendKey();
1388 [ + - ]: 10 : tester.SendGarbage();
1389 [ + - ]: 10 : tester.ReceiveKey();
1390 [ + - ]: 10 : tester.SendGarbageTerm();
1391 [ + - ]: 10 : tester.SendVersion();
1392 [ + - ]: 20 : ret = tester.Interact();
1393 [ + - + - : 20 : BOOST_REQUIRE(ret && ret->empty());
- + + - +
- ]
1394 [ + - ]: 10 : tester.ReceiveGarbage();
1395 [ + - ]: 10 : tester.ReceiveVersion();
1396 [ + - ]: 10 : tester.CompareSessionIDs();
1397 : 10 : auto msg_data_1 = m_rng.randbytes<uint8_t>(m_rng.randrange(100000));
1398 : 10 : auto msg_data_2 = m_rng.randbytes<uint8_t>(m_rng.randrange(1000));
1399 [ - + + - ]: 10 : tester.SendMessage(uint8_t(4), msg_data_1); // cmpctblock short id
1400 [ + - ]: 10 : tester.SendMessage(0, {}); // Invalidly encoded message
1401 [ - + + - : 10 : tester.SendMessage("tx", msg_data_2); // 12-character encoded message type
+ - ]
1402 [ + - ]: 20 : ret = tester.Interact();
1403 [ + - + - : 20 : BOOST_REQUIRE(ret && ret->size() == 3);
- + - + +
- + - ]
1404 [ + - + - : 20 : BOOST_CHECK((*ret)[0] && (*ret)[0]->m_type == "cmpctblock" && std::ranges::equal((*ret)[0]->m_recv, MakeByteSpan(msg_data_1)));
+ - - + +
- + - ]
1405 [ + - + - : 20 : BOOST_CHECK(!(*ret)[1]);
+ - ]
1406 [ + - + - : 20 : BOOST_CHECK((*ret)[2] && (*ret)[2]->m_type == "tx" && std::ranges::equal((*ret)[2]->m_recv, MakeByteSpan(msg_data_2)));
+ - - + +
- - + ]
1407 : :
1408 : : // Then send a message with a bit error, expecting failure. It's possible this failure does
1409 : : // not occur immediately (when the length descriptor was modified), but it should come
1410 : : // eventually, and no messages can be delivered anymore.
1411 [ - + + - : 10 : tester.SendMessage("bad", msg_data_1);
+ - ]
1412 : 10 : tester.Damage();
1413 : 10 : while (true) {
1414 [ + - ]: 20 : ret = tester.Interact();
1415 [ - + ]: 10 : if (!ret) break; // failure
1416 [ # # # # : 0 : BOOST_CHECK(ret->size() == 0); // no message can be delivered
# # ]
1417 : : // Send another message.
1418 : 0 : auto msg_data_3 = m_rng.randbytes<uint8_t>(m_rng.randrange(10000));
1419 [ # # # # ]: 0 : tester.SendMessage(uint8_t(12), msg_data_3); // getheaders short id
1420 : 0 : }
1421 : 10 : }
1422 : :
1423 : : // Normal scenario, with a transport in responder node.
1424 [ + + ]: 11 : for (int i = 0; i < 10; ++i) {
1425 : 10 : V2TransportTester tester(m_rng, false);
1426 [ + - ]: 10 : tester.SendKey();
1427 [ + - ]: 10 : tester.SendGarbage();
1428 [ + - ]: 10 : auto ret = tester.Interact();
1429 [ + - + - : 20 : BOOST_REQUIRE(ret && ret->empty());
- + + - +
- ]
1430 [ + - ]: 10 : tester.ReceiveKey();
1431 [ + - ]: 10 : tester.SendGarbageTerm();
1432 [ + - ]: 10 : tester.SendVersion();
1433 [ + - ]: 20 : ret = tester.Interact();
1434 [ + - + - : 20 : BOOST_REQUIRE(ret && ret->empty());
- + + - +
- ]
1435 [ + - ]: 10 : tester.ReceiveGarbage();
1436 [ + - ]: 10 : tester.ReceiveVersion();
1437 [ + - ]: 10 : tester.CompareSessionIDs();
1438 : 10 : auto msg_data_1 = m_rng.randbytes<uint8_t>(m_rng.randrange(100000));
1439 : 10 : auto msg_data_2 = m_rng.randbytes<uint8_t>(m_rng.randrange(1000));
1440 [ - + + - ]: 10 : tester.SendMessage(uint8_t(14), msg_data_1); // inv short id
1441 [ - + + - ]: 10 : tester.SendMessage(uint8_t(19), msg_data_2); // pong short id
1442 [ + - ]: 20 : ret = tester.Interact();
1443 [ + - + - : 20 : BOOST_REQUIRE(ret && ret->size() == 2);
- + - + +
- + - ]
1444 [ + - + - : 20 : BOOST_CHECK((*ret)[0] && (*ret)[0]->m_type == "inv" && std::ranges::equal((*ret)[0]->m_recv, MakeByteSpan(msg_data_1)));
+ - - + +
- + - ]
1445 [ + - + - : 20 : BOOST_CHECK((*ret)[1] && (*ret)[1]->m_type == "pong" && std::ranges::equal((*ret)[1]->m_recv, MakeByteSpan(msg_data_2)));
+ - - + +
- ]
1446 : :
1447 : : // Then send a too-large message.
1448 : 10 : auto msg_data_3 = m_rng.randbytes<uint8_t>(4005000);
1449 [ - + + - ]: 10 : tester.SendMessage(uint8_t(11), msg_data_3); // getdata short id
1450 [ + - ]: 20 : ret = tester.Interact();
1451 [ + - + - ]: 20 : BOOST_CHECK(!ret);
1452 : 10 : }
1453 : :
1454 : : // Various valid but unusual scenarios.
1455 [ + + ]: 51 : for (int i = 0; i < 50; ++i) {
1456 : : /** Whether an initiator or responder is being tested. */
1457 : 50 : bool initiator = m_rng.randbool();
1458 : : /** Use either 0 bytes or the maximum possible (4095 bytes) garbage length. */
1459 [ + + ]: 50 : size_t garb_len = m_rng.randbool() ? 0 : V2Transport::MAX_GARBAGE_LEN;
1460 : : /** How many decoy packets to send before the version packet. */
1461 : 50 : unsigned num_ignore_version = m_rng.randrange(10);
1462 : : /** What data to send in the version packet (ignored by BIP324 peers, but reserved for future extensions). */
1463 [ + + ]: 50 : auto ver_data = m_rng.randbytes<uint8_t>(m_rng.randbool() ? 0 : m_rng.randrange(1000));
1464 : : /** Whether to immediately send key and garbage out (required for responders, optional otherwise). */
1465 [ + + + + ]: 50 : bool send_immediately = !initiator || m_rng.randbool();
1466 : : /** How many decoy packets to send before the first and second real message. */
1467 : 50 : unsigned num_decoys_1 = m_rng.randrange(1000), num_decoys_2 = m_rng.randrange(1000);
1468 [ + - ]: 50 : V2TransportTester tester(m_rng, initiator);
1469 [ + + ]: 50 : if (send_immediately) {
1470 [ + - ]: 36 : tester.SendKey();
1471 [ + - ]: 36 : tester.SendGarbage(garb_len);
1472 : : }
1473 [ + - ]: 50 : auto ret = tester.Interact();
1474 [ + - + - : 100 : BOOST_REQUIRE(ret && ret->empty());
- + + - +
+ ]
1475 [ + + ]: 50 : if (!send_immediately) {
1476 [ + - ]: 14 : tester.SendKey();
1477 [ + - ]: 14 : tester.SendGarbage(garb_len);
1478 : : }
1479 [ + - ]: 50 : tester.ReceiveKey();
1480 [ + - ]: 50 : tester.SendGarbageTerm();
1481 [ + + ]: 269 : for (unsigned v = 0; v < num_ignore_version; ++v) {
1482 [ + + ]: 219 : size_t ver_ign_data_len = m_rng.randbool() ? 0 : m_rng.randrange(1000);
1483 : 219 : auto ver_ign_data = m_rng.randbytes<uint8_t>(ver_ign_data_len);
1484 [ - + + - ]: 219 : tester.SendVersion(ver_ign_data, true);
1485 : 219 : }
1486 [ - + + - ]: 50 : tester.SendVersion(ver_data, false);
1487 [ + - ]: 100 : ret = tester.Interact();
1488 [ + - + - : 100 : BOOST_REQUIRE(ret && ret->empty());
- + + - +
- ]
1489 [ + - ]: 50 : tester.ReceiveGarbage();
1490 [ + - ]: 50 : tester.ReceiveVersion();
1491 [ + - ]: 50 : tester.CompareSessionIDs();
1492 [ + + ]: 22809 : for (unsigned d = 0; d < num_decoys_1; ++d) {
1493 : 22759 : auto decoy_data = m_rng.randbytes<uint8_t>(m_rng.randrange(1000));
1494 [ - + + - ]: 22759 : tester.SendPacket(/*content=*/decoy_data, /*aad=*/{}, /*ignore=*/true);
1495 : 22759 : }
1496 : 50 : auto msg_data_1 = m_rng.randbytes<uint8_t>(m_rng.randrange(4000000));
1497 [ - + + - ]: 50 : tester.SendMessage(uint8_t(28), msg_data_1);
1498 [ + + ]: 24687 : for (unsigned d = 0; d < num_decoys_2; ++d) {
1499 : 24637 : auto decoy_data = m_rng.randbytes<uint8_t>(m_rng.randrange(1000));
1500 [ - + + - ]: 24637 : tester.SendPacket(/*content=*/decoy_data, /*aad=*/{}, /*ignore=*/true);
1501 : 24637 : }
1502 : 50 : auto msg_data_2 = m_rng.randbytes<uint8_t>(m_rng.randrange(1000));
1503 [ - + + - ]: 50 : tester.SendMessage(uint8_t(13), msg_data_2); // headers short id
1504 : : // Send invalidly-encoded message
1505 [ + - + - ]: 50 : tester.SendMessage(std::string("blocktxn\x00\x00\x00a", CMessageHeader::MESSAGE_TYPE_SIZE), {});
1506 [ + - + - ]: 50 : tester.SendMessage("foobar", {}); // test receiving unknown message type
1507 [ + - + - ]: 100 : tester.AddMessage("barfoo", {}); // test sending unknown message type
1508 [ + - ]: 100 : ret = tester.Interact();
1509 [ + - + - : 100 : BOOST_REQUIRE(ret && ret->size() == 4);
- + - + +
- + - ]
1510 [ + - + - : 100 : BOOST_CHECK((*ret)[0] && (*ret)[0]->m_type == "addrv2" && std::ranges::equal((*ret)[0]->m_recv, MakeByteSpan(msg_data_1)));
+ - - + +
- + - ]
1511 [ + - + - : 100 : BOOST_CHECK((*ret)[1] && (*ret)[1]->m_type == "headers" && std::ranges::equal((*ret)[1]->m_recv, MakeByteSpan(msg_data_2)));
+ - - + +
- + - ]
1512 [ + - + - : 100 : BOOST_CHECK(!(*ret)[2]);
+ - ]
1513 [ + - + - : 100 : BOOST_CHECK((*ret)[3] && (*ret)[3]->m_type == "foobar" && (*ret)[3]->m_recv.empty());
+ - - + -
+ + - +
- ]
1514 [ + - + - ]: 100 : tester.ReceiveMessage("barfoo", {});
1515 : 50 : }
1516 : :
1517 : : // Too long garbage (initiator).
1518 : 1 : {
1519 : 1 : V2TransportTester tester(m_rng, true);
1520 [ + - ]: 1 : auto ret = tester.Interact();
1521 [ + - + - : 2 : BOOST_REQUIRE(ret && ret->empty());
- + + - +
- ]
1522 [ + - ]: 1 : tester.SendKey();
1523 [ + - ]: 1 : tester.SendGarbage(V2Transport::MAX_GARBAGE_LEN + 1);
1524 [ + - ]: 1 : tester.ReceiveKey();
1525 [ + - ]: 1 : tester.SendGarbageTerm();
1526 [ + - ]: 2 : ret = tester.Interact();
1527 [ + - + - ]: 2 : BOOST_CHECK(!ret);
1528 : 1 : }
1529 : :
1530 : : // Too long garbage (responder).
1531 : 1 : {
1532 : 1 : V2TransportTester tester(m_rng, false);
1533 [ + - ]: 1 : tester.SendKey();
1534 [ + - ]: 1 : tester.SendGarbage(V2Transport::MAX_GARBAGE_LEN + 1);
1535 [ + - ]: 1 : auto ret = tester.Interact();
1536 [ + - + - : 2 : BOOST_REQUIRE(ret && ret->empty());
- + + - +
- ]
1537 [ + - ]: 1 : tester.ReceiveKey();
1538 [ + - ]: 1 : tester.SendGarbageTerm();
1539 [ + - ]: 2 : ret = tester.Interact();
1540 [ + - + - ]: 2 : BOOST_CHECK(!ret);
1541 : 1 : }
1542 : :
1543 : : // Send garbage that includes the first 15 garbage terminator bytes somewhere.
1544 : 1 : {
1545 : 1 : V2TransportTester tester(m_rng, true);
1546 [ + - ]: 1 : auto ret = tester.Interact();
1547 [ + - + - : 2 : BOOST_REQUIRE(ret && ret->empty());
- + + - +
- ]
1548 [ + - ]: 1 : tester.SendKey();
1549 [ + - ]: 1 : tester.ReceiveKey();
1550 : : /** The number of random garbage bytes before the included first 15 bytes of terminator. */
1551 : 1 : size_t len_before = m_rng.randrange(V2Transport::MAX_GARBAGE_LEN - 16 + 1);
1552 : : /** The number of random garbage bytes after it. */
1553 : 1 : size_t len_after = m_rng.randrange(V2Transport::MAX_GARBAGE_LEN - 16 - len_before + 1);
1554 : : // Construct len_before + 16 + len_after random bytes.
1555 : 1 : auto garbage = m_rng.randbytes<uint8_t>(len_before + 16 + len_after);
1556 : : // Replace the designed 16 bytes in the middle with the to-be-sent garbage terminator.
1557 : 1 : auto garb_term = MakeUCharSpan(tester.GetCipher().GetSendGarbageTerminator());
1558 : 1 : std::copy(garb_term.begin(), garb_term.begin() + 16, garbage.begin() + len_before);
1559 : : // Introduce a bit error in the last byte of that copied garbage terminator, making only
1560 : : // the first 15 of them match.
1561 [ - + ]: 1 : garbage[len_before + 15] ^= (uint8_t(1) << m_rng.randrange(8));
1562 [ - + + - ]: 1 : tester.SendGarbage(garbage);
1563 [ + - ]: 1 : tester.SendGarbageTerm();
1564 [ + - ]: 1 : tester.SendVersion();
1565 [ + - ]: 2 : ret = tester.Interact();
1566 [ + - + - : 2 : BOOST_REQUIRE(ret && ret->empty());
- + + - +
- ]
1567 [ + - ]: 1 : tester.ReceiveGarbage();
1568 [ + - ]: 1 : tester.ReceiveVersion();
1569 [ + - ]: 1 : tester.CompareSessionIDs();
1570 : 1 : auto msg_data_1 = m_rng.randbytes<uint8_t>(4000000); // test that receiving 4M payload works
1571 : 1 : auto msg_data_2 = m_rng.randbytes<uint8_t>(4000000); // test that sending 4M payload works
1572 [ + - ]: 1 : tester.SendMessage(uint8_t(m_rng.randrange(256 - BIP324_SHORTIDS_IMPLEMENTED) + BIP324_SHORTIDS_IMPLEMENTED), {}); // unknown short id
1573 [ - + + - ]: 1 : tester.SendMessage(uint8_t(2), msg_data_1); // "block" short id
1574 [ + - + - : 2 : tester.AddMessage("blocktxn", msg_data_2); // schedule blocktxn to be sent to us
+ - ]
1575 [ + - ]: 2 : ret = tester.Interact();
1576 [ + - + - : 2 : BOOST_REQUIRE(ret && ret->size() == 2);
- + - + +
- + - ]
1577 [ + - + - : 2 : BOOST_CHECK(!(*ret)[0]);
+ - ]
1578 [ + - + - : 2 : BOOST_CHECK((*ret)[1] && (*ret)[1]->m_type == "block" && std::ranges::equal((*ret)[1]->m_recv, MakeByteSpan(msg_data_1)));
+ - - + +
- - + ]
1579 [ - + + - ]: 1 : tester.ReceiveMessage(uint8_t(3), msg_data_2); // "blocktxn" short id
1580 : 1 : }
1581 : :
1582 : : // Send correct network's V1 header
1583 : 1 : {
1584 : 1 : V2TransportTester tester(m_rng, false);
1585 [ + - + - ]: 1 : tester.SendV1Version(Params().MessageStart());
1586 [ + - ]: 1 : auto ret = tester.Interact();
1587 [ + - + - ]: 2 : BOOST_CHECK(ret);
1588 : 1 : }
1589 : :
1590 : : // Send wrong network's V1 header
1591 : 1 : {
1592 : 1 : V2TransportTester tester(m_rng, false);
1593 [ + - + - ]: 1 : tester.SendV1Version(CChainParams::Main()->MessageStart());
1594 [ + - ]: 1 : auto ret = tester.Interact();
1595 [ + - + - ]: 2 : BOOST_CHECK(!ret);
1596 : 1 : }
1597 : 1 : }
1598 : :
1599 [ + - + - : 7 : BOOST_AUTO_TEST_CASE(private_broadcast_version_does_not_update_addrman_services)
+ - + - -
+ + - + -
+ - + - +
- + - - +
+ - + - +
- + - + -
+ - - + +
- + - + -
+ - + - +
- + - - +
+ - + - +
- + - + -
+ - - + +
- ]
1600 : : {
1601 : 1 : LOCK(NetEventsInterface::g_msgproc_mutex);
1602 : :
1603 [ + - + - : 2 : const CNetAddr source{LookupHost("2.3.4.5", /*fAllowLookup=*/false).value()};
+ - ]
1604 [ + - + - : 2 : const CAddress addr{Lookup("1.2.3.4", 8333, /*fAllowLookup=*/false).value(), NODE_NONE};
+ - ]
1605 [ + - + - : 3 : BOOST_REQUIRE(m_node.addrman->Add({addr}, source));
+ - + - +
+ - - ]
1606 : 1 : CNode node{/*id=*/0,
1607 : : /*sock=*/nullptr,
1608 : : /*addrIn=*/addr,
1609 : : /*nKeyedNetGroupIn=*/0,
1610 : : /*nLocalHostNonceIn=*/0,
1611 [ - + ]: 1 : /*addrBindIn=*/CService{},
1612 [ + - ]: 2 : /*addrNameIn=*/"",
1613 : : /*conn_type_in=*/ConnectionType::PRIVATE_BROADCAST,
1614 : : /*inbound_onion=*/false,
1615 [ + - + - ]: 2 : /*network_key=*/0};
1616 : :
1617 [ + - ]: 1 : auto& connman = static_cast<ConnmanTestMsg&>(*m_node.connman);
1618 [ + - ]: 1 : connman.Handshake(node,
1619 : : /*successfully_connected=*/false,
1620 : : /*remote_services=*/NODE_NETWORK,
1621 : : /*local_services=*/NODE_NONE,
1622 : : /*version=*/PROTOCOL_VERSION,
1623 : : /*relay_txs=*/true);
1624 : :
1625 [ + - + - : 1 : BOOST_CHECK_EQUAL(m_node.addrman->Select().first.nServices, NODE_NONE);
+ - ]
1626 [ + - ]: 1 : m_node.peerman->FinalizeNode(node);
1627 [ + - ]: 3 : }
1628 : :
1629 : : BOOST_AUTO_TEST_SUITE_END()
|