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 4 : FUZZ_TARGET(netbase_dns_lookup) 16 : { 17 2 : FuzzedDataProvider fuzzed_data_provider{buffer.data(), buffer.size()}; 18 2 : const std::string name = fuzzed_data_provider.ConsumeRandomLengthString(512); 19 0 : const unsigned int max_results = fuzzed_data_provider.ConsumeIntegral<unsigned int>(); 20 0 : const bool allow_lookup = fuzzed_data_provider.ConsumeBool(); 21 0 : const uint16_t default_port = fuzzed_data_provider.ConsumeIntegral<uint16_t>(); 22 : 23 0 : auto fuzzed_dns_lookup_function = [&](const std::string&, bool) { 24 0 : std::vector<CNetAddr> resolved_addresses; 25 2 : LIMITED_WHILE(fuzzed_data_provider.ConsumeBool(), 10000) { 26 0 : resolved_addresses.push_back(ConsumeNetAddr(fuzzed_data_provider)); 27 0 : } 28 0 : return resolved_addresses; 29 0 : }; 30 : 31 : { 32 0 : const std::vector<CNetAddr> resolved_addresses{LookupHost(name, max_results, allow_lookup, fuzzed_dns_lookup_function)}; 33 0 : for (const CNetAddr& resolved_address : resolved_addresses) { 34 0 : assert(!resolved_address.IsInternal()); 35 : } 36 0 : assert(resolved_addresses.size() <= max_results || max_results == 0); 37 0 : } 38 : { 39 0 : const std::optional<CNetAddr> resolved_address{LookupHost(name, allow_lookup, fuzzed_dns_lookup_function)}; 40 0 : if (resolved_address.has_value()) { 41 0 : assert(!resolved_address.value().IsInternal()); 42 0 : } 43 0 : } 44 : { 45 0 : const std::vector<CService> resolved_services{Lookup(name, default_port, allow_lookup, max_results, fuzzed_dns_lookup_function)}; 46 0 : for (const CNetAddr& resolved_service : resolved_services) { 47 0 : assert(!resolved_service.IsInternal()); 48 : } 49 0 : assert(resolved_services.size() <= max_results || max_results == 0); 50 0 : } 51 : { 52 0 : const std::optional<CService> resolved_service{Lookup(name, default_port, allow_lookup, fuzzed_dns_lookup_function)}; 53 0 : if (resolved_service.has_value()) { 54 0 : assert(!resolved_service.value().IsInternal()); 55 0 : } 56 0 : } 57 : { 58 0 : CService resolved_service = LookupNumeric(name, default_port, fuzzed_dns_lookup_function); 59 0 : assert(!resolved_service.IsInternal()); 60 0 : } 61 : { 62 0 : CSubNet resolved_subnet; 63 0 : if (LookupSubNet(name, resolved_subnet)) { 64 0 : assert(resolved_subnet.IsValid()); 65 0 : } 66 0 : } 67 0 : }