diff options
author | MarcoFalke <falke.marco@gmail.com> | 2021-03-15 12:04:38 +0100 |
---|---|---|
committer | MarcoFalke <falke.marco@gmail.com> | 2021-03-15 12:05:13 +0100 |
commit | eceb3f7707d6793152a00476adae1c4e268ce6f3 (patch) | |
tree | ea415c0d8a3b7a5efb13c6632f70e140d33d16ef /src/test/fuzz/netbase_dns_lookup.cpp | |
parent | c47979778d647140e8f410cefd5b27d4fbc715ee (diff) | |
parent | e5280751890b02abb558b37eb0e0401493f148b4 (diff) | |
download | bitcoin-eceb3f7707d6793152a00476adae1c4e268ce6f3.tar.xz |
Merge #19415: net: Make DNS lookup mockable, add fuzzing harness
e5280751890b02abb558b37eb0e0401493f148b4 tests: Add fuzzing harness for Lookup(...)/LookupHost(...)/LookupNumeric(...)/LookupSubNet(...) (practicalswift)
c6b4bfb4b3507f1a62290869d7435b0f54032104 net: Make DNS lookup code testable (practicalswift)
Pull request description:
Make DNS lookup mockable/testable/fuzzable.
Add fuzzing harness for `Lookup(…)`/`LookupHost(…)`/`LookupNumeric(…)`/`LookupSubNet(…)`.
See [`doc/fuzzing.md`](https://github.com/bitcoin/bitcoin/blob/master/doc/fuzzing.md) for information on how to fuzz Bitcoin Core. Don't forget to contribute any coverage increasing inputs you find to the [Bitcoin Core fuzzing corpus repo](https://github.com/bitcoin-core/qa-assets).
Happy fuzzing :)
ACKs for top commit:
Crypt-iQ:
cr ACK e5280751890b02abb558b37eb0e0401493f148b4
vasild:
ACK e5280751890b02abb558b37eb0e0401493f148b4
Tree-SHA512: 9984c2e2fedc3c1e1c3dbd701bb739ebd2f01766e6e83543dae5ae43eb8646c452bba0e101dd2f06079e5258bd5846c7d27a60ed5d77c1682b54c9544ffad443
Diffstat (limited to 'src/test/fuzz/netbase_dns_lookup.cpp')
-rw-r--r-- | src/test/fuzz/netbase_dns_lookup.cpp | 77 |
1 files changed, 77 insertions, 0 deletions
diff --git a/src/test/fuzz/netbase_dns_lookup.cpp b/src/test/fuzz/netbase_dns_lookup.cpp new file mode 100644 index 0000000000..786440c8a4 --- /dev/null +++ b/src/test/fuzz/netbase_dns_lookup.cpp @@ -0,0 +1,77 @@ +// Copyright (c) 2021 The Bitcoin Core developers +// Distributed under the MIT software license, see the accompanying +// file COPYING or http://www.opensource.org/licenses/mit-license.php. + +#include <netaddress.h> +#include <netbase.h> +#include <test/fuzz/FuzzedDataProvider.h> +#include <test/fuzz/fuzz.h> +#include <test/fuzz/util.h> + +#include <cstdint> +#include <string> +#include <vector> + +namespace { +FuzzedDataProvider* fuzzed_data_provider_ptr = nullptr; + +std::vector<CNetAddr> fuzzed_dns_lookup_function(const std::string& name, bool allow_lookup) +{ + std::vector<CNetAddr> resolved_addresses; + while (fuzzed_data_provider_ptr->ConsumeBool()) { + resolved_addresses.push_back(ConsumeNetAddr(*fuzzed_data_provider_ptr)); + } + return resolved_addresses; +} +} // namespace + +FUZZ_TARGET(netbase_dns_lookup) +{ + FuzzedDataProvider fuzzed_data_provider{buffer.data(), buffer.size()}; + fuzzed_data_provider_ptr = &fuzzed_data_provider; + const std::string name = fuzzed_data_provider.ConsumeRandomLengthString(512); + const unsigned int max_results = fuzzed_data_provider.ConsumeIntegral<unsigned int>(); + const bool allow_lookup = fuzzed_data_provider.ConsumeBool(); + const int default_port = fuzzed_data_provider.ConsumeIntegral<int>(); + { + std::vector<CNetAddr> resolved_addresses; + if (LookupHost(name, resolved_addresses, max_results, allow_lookup, fuzzed_dns_lookup_function)) { + for (const CNetAddr& resolved_address : resolved_addresses) { + assert(!resolved_address.IsInternal()); + } + } + assert(resolved_addresses.size() <= max_results || max_results == 0); + } + { + CNetAddr resolved_address; + if (LookupHost(name, resolved_address, allow_lookup, fuzzed_dns_lookup_function)) { + assert(!resolved_address.IsInternal()); + } + } + { + std::vector<CService> resolved_services; + if (Lookup(name, resolved_services, default_port, allow_lookup, max_results, fuzzed_dns_lookup_function)) { + for (const CNetAddr& resolved_service : resolved_services) { + assert(!resolved_service.IsInternal()); + } + } + assert(resolved_services.size() <= max_results || max_results == 0); + } + { + CService resolved_service; + if (Lookup(name, resolved_service, default_port, allow_lookup, fuzzed_dns_lookup_function)) { + assert(!resolved_service.IsInternal()); + } + } + { + CService resolved_service = LookupNumeric(name, default_port, fuzzed_dns_lookup_function); + assert(!resolved_service.IsInternal()); + } + { + CSubNet resolved_subnet; + if (LookupSubNet(name, resolved_subnet, fuzzed_dns_lookup_function)) { + assert(resolved_subnet.IsValid()); + } + } + fuzzed_data_provider_ptr = nullptr; +} |