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