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