netaddress.cpp raw
1 // Copyright (c) 2020-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 <netaddress.h>
6 #include <test/fuzz/FuzzedDataProvider.h>
7 #include <test/fuzz/fuzz.h>
8 #include <test/fuzz/util/net.h>
9 #include <test/util/random.h>
10
11 #include <cassert>
12 #include <cstdint>
13 #include <vector>
14
15 FUZZ_TARGET(netaddress)
16 {
17 SeedRandomStateForTest(SeedRand::ZEROS);
18 FuzzedDataProvider fuzzed_data_provider(buffer.data(), buffer.size());
19
20 const CNetAddr net_addr = ConsumeNetAddr(fuzzed_data_provider);
21 (void)net_addr.GetNetClass();
22 if (net_addr.GetNetwork() == Network::NET_IPV4) {
23 assert(net_addr.IsIPv4());
24 }
25 if (net_addr.GetNetwork() == Network::NET_IPV6) {
26 assert(net_addr.IsIPv6());
27 }
28 if (net_addr.GetNetwork() == Network::NET_ONION) {
29 assert(net_addr.IsTor());
30 }
31 if (net_addr.GetNetwork() == Network::NET_I2P) {
32 assert(net_addr.IsI2P());
33 }
34 if (net_addr.GetNetwork() == Network::NET_CJDNS) {
35 assert(net_addr.IsCJDNS());
36 }
37 if (net_addr.GetNetwork() == Network::NET_INTERNAL) {
38 assert(net_addr.IsInternal());
39 }
40 if (net_addr.GetNetwork() == Network::NET_UNROUTABLE) {
41 assert(!net_addr.IsRoutable());
42 }
43 (void)net_addr.IsBindAny();
44 if (net_addr.IsInternal()) {
45 assert(net_addr.GetNetwork() == Network::NET_INTERNAL);
46 }
47 if (net_addr.IsIPv4()) {
48 assert(net_addr.GetNetwork() == Network::NET_IPV4 || net_addr.GetNetwork() == Network::NET_UNROUTABLE);
49 }
50 if (net_addr.IsIPv6()) {
51 assert(net_addr.GetNetwork() == Network::NET_IPV6 || net_addr.GetNetwork() == Network::NET_UNROUTABLE);
52 }
53 (void)net_addr.IsLocal();
54 if (net_addr.IsRFC1918() || net_addr.IsRFC2544() || net_addr.IsRFC6598() || net_addr.IsRFC5737() || net_addr.IsRFC3927()) {
55 assert(net_addr.IsIPv4());
56 }
57 (void)net_addr.IsRFC2544();
58 if (net_addr.IsRFC3849() || net_addr.IsRFC3964() || net_addr.IsRFC4380() || net_addr.IsRFC4843() || net_addr.IsRFC7343() || net_addr.IsRFC4862() || net_addr.IsRFC6052() || net_addr.IsRFC6145()) {
59 assert(net_addr.IsIPv6());
60 }
61 (void)net_addr.IsRFC3927();
62 (void)net_addr.IsRFC3964();
63 if (net_addr.IsRFC4193()) {
64 assert(net_addr.GetNetwork() == Network::NET_INTERNAL || net_addr.GetNetwork() == Network::NET_UNROUTABLE);
65 }
66 (void)net_addr.IsRFC4380();
67 (void)net_addr.IsRFC4843();
68 (void)net_addr.IsRFC4862();
69 (void)net_addr.IsRFC5737();
70 (void)net_addr.IsRFC6052();
71 (void)net_addr.IsRFC6145();
72 (void)net_addr.IsRFC6598();
73 (void)net_addr.IsRFC7343();
74 if (!net_addr.IsRoutable()) {
75 assert(net_addr.GetNetwork() == Network::NET_UNROUTABLE || net_addr.GetNetwork() == Network::NET_INTERNAL);
76 }
77 if (net_addr.IsTor()) {
78 assert(net_addr.GetNetwork() == Network::NET_ONION);
79 }
80 if (net_addr.IsI2P()) {
81 assert(net_addr.GetNetwork() == Network::NET_I2P);
82 }
83 if (net_addr.IsCJDNS()) {
84 assert(net_addr.GetNetwork() == Network::NET_CJDNS);
85 }
86 (void)net_addr.IsValid();
87 (void)net_addr.ToStringAddr();
88
89 const CSubNet sub_net{net_addr, fuzzed_data_provider.ConsumeIntegral<uint8_t>()};
90 (void)sub_net.IsValid();
91 (void)sub_net.ToString();
92
93 const CService service{net_addr, fuzzed_data_provider.ConsumeIntegral<uint16_t>()};
94 (void)service.GetKey();
95 (void)service.GetPort();
96 (void)service.ToStringAddrPort();
97 (void)CServiceHash()(service);
98 (void)CServiceHash(0, 0)(service);
99
100 const CNetAddr other_net_addr = ConsumeNetAddr(fuzzed_data_provider);
101 (void)net_addr.GetReachabilityFrom(other_net_addr);
102 (void)sub_net.Match(other_net_addr);
103
104 const CService other_service{fuzzed_data_provider.ConsumeBool() ? net_addr : other_net_addr, fuzzed_data_provider.ConsumeIntegral<uint16_t>()};
105 assert((service == other_service) != (service != other_service));
106 (void)(service < other_service);
107
108 if (service.ToStringAddrPort() == other_service.ToStringAddrPort()) {
109 assert(static_cast<CNetAddr>(service) == static_cast<CNetAddr>(other_service));
110 }
111
112 const CSubNet sub_net_copy_1{net_addr, other_net_addr};
113 const CSubNet sub_net_copy_2{net_addr};
114
115 CNetAddr mutable_net_addr;
116 mutable_net_addr.SetIP(net_addr);
117 assert(net_addr == mutable_net_addr);
118 }
119