Branch data Line data Source code
1 : : // Copyright (c) 2021-2022 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 <netbase.h>
7 : : #include <test/fuzz/FuzzedDataProvider.h>
8 : : #include <test/fuzz/fuzz.h>
9 : : #include <test/fuzz/util/net.h>
10 : :
11 : : #include <cstdint>
12 : : #include <string>
13 : : #include <vector>
14 : :
15 [ + - ]: 810 : FUZZ_TARGET(netbase_dns_lookup)
16 : : {
17 : 398 : FuzzedDataProvider fuzzed_data_provider{buffer.data(), buffer.size()};
18 : 398 : const std::string name = fuzzed_data_provider.ConsumeRandomLengthString(512);
19 : 398 : const unsigned int max_results = fuzzed_data_provider.ConsumeIntegral<unsigned int>();
20 : 398 : const bool allow_lookup = fuzzed_data_provider.ConsumeBool();
21 : 398 : const uint16_t default_port = fuzzed_data_provider.ConsumeIntegral<uint16_t>();
22 : :
23 : 2333 : auto fuzzed_dns_lookup_function = [&](const std::string&, bool) {
24 : 1935 : std::vector<CNetAddr> resolved_addresses;
25 [ + + + + ]: 292956 : LIMITED_WHILE(fuzzed_data_provider.ConsumeBool(), 10000) {
26 [ + - ]: 291021 : resolved_addresses.push_back(ConsumeNetAddr(fuzzed_data_provider));
27 : : }
28 : 1935 : return resolved_addresses;
29 : 398 : };
30 : :
31 : 398 : {
32 [ + - ]: 398 : const std::vector<CNetAddr> resolved_addresses{LookupHost(name, max_results, allow_lookup, fuzzed_dns_lookup_function)};
33 [ + + ]: 73265 : for (const CNetAddr& resolved_address : resolved_addresses) {
34 [ + - - + ]: 72867 : assert(!resolved_address.IsInternal());
35 : : }
36 [ + + - + ]: 398 : assert(resolved_addresses.size() <= max_results || max_results == 0);
37 : 398 : }
38 : 398 : {
39 [ + - ]: 398 : const std::optional<CNetAddr> resolved_address{LookupHost(name, allow_lookup, fuzzed_dns_lookup_function)};
40 [ + + ]: 398 : if (resolved_address.has_value()) {
41 [ + - - + ]: 112 : assert(!resolved_address.value().IsInternal());
42 : : }
43 : 0 : }
44 : 398 : {
45 [ + - ]: 398 : const std::vector<CService> resolved_services{Lookup(name, default_port, allow_lookup, max_results, fuzzed_dns_lookup_function)};
46 [ + + ]: 80856 : for (const CNetAddr& resolved_service : resolved_services) {
47 [ + - - + ]: 80458 : assert(!resolved_service.IsInternal());
48 : : }
49 [ + + - + ]: 398 : assert(resolved_services.size() <= max_results || max_results == 0);
50 : 398 : }
51 : 398 : {
52 [ + - ]: 398 : const std::optional<CService> resolved_service{Lookup(name, default_port, allow_lookup, fuzzed_dns_lookup_function)};
53 [ + + ]: 398 : if (resolved_service.has_value()) {
54 [ + - - + ]: 83 : assert(!resolved_service.value().IsInternal());
55 : : }
56 : 0 : }
57 : 398 : {
58 [ + - ]: 398 : CService resolved_service = LookupNumeric(name, default_port, fuzzed_dns_lookup_function);
59 [ + - - + ]: 398 : assert(!resolved_service.IsInternal());
60 : 0 : }
61 : 398 : {
62 [ + - ]: 796 : (void)LookupSubNet(name);
63 : : }
64 : 398 : }
|