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