/ src / test / fuzz / kitchen_sink.cpp
kitchen_sink.cpp
 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 <common/messages.h>
 6  #include <merkleblock.h>
 7  #include <node/types.h>
 8  #include <policy/fees/block_policy_estimator.h>
 9  #include <rpc/util.h>
10  #include <test/fuzz/FuzzedDataProvider.h>
11  #include <test/fuzz/fuzz.h>
12  #include <test/fuzz/util.h>
13  #include <util/translation.h>
14  
15  #include <array>
16  #include <cstdint>
17  #include <optional>
18  #include <vector>
19  
20  using common::TransactionErrorString;
21  using node::TransactionError;
22  
23  namespace {
24  constexpr TransactionError ALL_TRANSACTION_ERROR[] = {
25      TransactionError::MISSING_INPUTS,
26      TransactionError::ALREADY_IN_UTXO_SET,
27      TransactionError::MEMPOOL_REJECTED,
28      TransactionError::MEMPOOL_ERROR,
29      TransactionError::MAX_FEE_EXCEEDED,
30  };
31  }; // namespace
32  
33  // The fuzzing kitchen sink: Fuzzing harness for functions that need to be
34  // fuzzed but a.) don't belong in any existing fuzzing harness file, and
35  // b.) are not important enough to warrant their own fuzzing harness file.
36  FUZZ_TARGET(kitchen_sink)
37  {
38      FuzzedDataProvider fuzzed_data_provider(buffer.data(), buffer.size());
39  
40      const TransactionError transaction_error = fuzzed_data_provider.PickValueInArray(ALL_TRANSACTION_ERROR);
41      (void)JSONRPCTransactionError(transaction_error);
42      (void)RPCErrorFromTransactionError(transaction_error);
43      (void)TransactionErrorString(transaction_error);
44  
45      (void)StringForFeeEstimateHorizon(fuzzed_data_provider.PickValueInArray(ALL_FEE_ESTIMATE_HORIZONS));
46  
47      const OutputType output_type = fuzzed_data_provider.PickValueInArray(OUTPUT_TYPES);
48      const std::string& output_type_string = FormatOutputType(output_type);
49      const std::optional<OutputType> parsed = ParseOutputType(output_type_string);
50      assert(parsed);
51      assert(output_type == parsed.value());
52      (void)ParseOutputType(fuzzed_data_provider.ConsumeRandomLengthString(64));
53  
54      const std::vector<uint8_t> bytes = ConsumeRandomLengthByteVector(fuzzed_data_provider);
55      const std::vector<bool> bits = BytesToBits(bytes);
56      const std::vector<uint8_t> bytes_decoded = BitsToBytes(bits);
57      assert(bytes == bytes_decoded);
58  }