Bitcoin Core  22.99.0
P2P Digital Currency
kitchen_sink.cpp
Go to the documentation of this file.
1 // Copyright (c) 2020-2021 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 <merkleblock.h>
6 #include <policy/fees.h>
7 #include <rpc/util.h>
9 #include <test/fuzz/fuzz.h>
10 #include <test/fuzz/util.h>
11 #include <util/error.h>
12 #include <util/translation.h>
13 
14 #include <array>
15 #include <cstdint>
16 #include <optional>
17 #include <vector>
18 
19 namespace {
20 constexpr TransactionError ALL_TRANSACTION_ERROR[] = {
31 };
32 }; // namespace
33 
34 // The fuzzing kitchen sink: Fuzzing harness for functions that need to be
35 // fuzzed but a.) don't belong in any existing fuzzing harness file, and
36 // b.) are not important enough to warrant their own fuzzing harness file.
37 FUZZ_TARGET(kitchen_sink)
38 {
39  FuzzedDataProvider fuzzed_data_provider(buffer.data(), buffer.size());
40 
41  const TransactionError transaction_error = fuzzed_data_provider.PickValueInArray(ALL_TRANSACTION_ERROR);
42  (void)JSONRPCTransactionError(transaction_error);
43  (void)RPCErrorFromTransactionError(transaction_error);
44  (void)TransactionErrorString(transaction_error);
45 
47 
48  const OutputType output_type = fuzzed_data_provider.PickValueInArray(OUTPUT_TYPES);
49  const std::string& output_type_string = FormatOutputType(output_type);
50  const std::optional<OutputType> parsed = ParseOutputType(output_type_string);
51  assert(parsed);
52  assert(output_type == parsed.value());
53  (void)ParseOutputType(fuzzed_data_provider.ConsumeRandomLengthString(64));
54 
55  const std::vector<uint8_t> bytes = ConsumeRandomLengthByteVector(fuzzed_data_provider);
56  const std::vector<bool> bits = BytesToBits(bytes);
57  const std::vector<uint8_t> bytes_decoded = BitsToBytes(bits);
58  assert(bytes == bytes_decoded);
59 }
merkleblock.h
OutputType
OutputType
Definition: outputtype.h:18
assert
assert(!tx.IsCoinBase())
OUTPUT_TYPES
static constexpr auto OUTPUT_TYPES
Definition: outputtype.h:25
TransactionError::MEMPOOL_REJECTED
@ MEMPOOL_REJECTED
TransactionErrorString
bilingual_str TransactionErrorString(const TransactionError err)
Definition: error.cpp:11
FormatOutputType
const std::string & FormatOutputType(OutputType type)
Definition: outputtype.cpp:38
util.h
FUZZ_TARGET
FUZZ_TARGET(kitchen_sink)
Definition: kitchen_sink.cpp:37
FuzzedDataProvider::ConsumeRandomLengthString
std::string ConsumeRandomLengthString(size_t max_length)
Definition: FuzzedDataProvider.h:152
FuzzedDataProvider::PickValueInArray
T PickValueInArray(const T(&array)[size])
Definition: FuzzedDataProvider.h:303
TransactionError::MEMPOOL_ERROR
@ MEMPOOL_ERROR
TransactionError::PSBT_MISMATCH
@ PSBT_MISMATCH
BitsToBytes
std::vector< unsigned char > BitsToBytes(const std::vector< bool > &bits)
Definition: merkleblock.cpp:12
TransactionError
TransactionError
Definition: error.h:22
RPCErrorFromTransactionError
RPCErrorCode RPCErrorFromTransactionError(TransactionError terr)
Definition: util.cpp:339
FuzzedDataProvider.h
TransactionError::INVALID_PSBT
@ INVALID_PSBT
TransactionError::SIGHASH_MISMATCH
@ SIGHASH_MISMATCH
ConsumeRandomLengthByteVector
std::vector< uint8_t > ConsumeRandomLengthByteVector(FuzzedDataProvider &fuzzed_data_provider, const std::optional< size_t > &max_length=std::nullopt) noexcept
Definition: util.h:61
error.h
TransactionError::P2P_DISABLED
@ P2P_DISABLED
BytesToBits
std::vector< bool > BytesToBits(const std::vector< unsigned char > &bytes)
Definition: merkleblock.cpp:21
JSONRPCTransactionError
UniValue JSONRPCTransactionError(TransactionError terr, const std::string &err_string)
Definition: util.cpp:358
StringForFeeEstimateHorizon
std::string StringForFeeEstimateHorizon(FeeEstimateHorizon horizon)
Definition: fees.cpp:20
fees.h
translation.h
fuzz.h
FuzzedDataProvider
Definition: FuzzedDataProvider.h:31
TransactionError::OK
@ OK
No error.
util.h
TransactionError::MISSING_INPUTS
@ MISSING_INPUTS
ALL_FEE_ESTIMATE_HORIZONS
static constexpr auto ALL_FEE_ESTIMATE_HORIZONS
Definition: fees.h:34
TransactionError::MAX_FEE_EXCEEDED
@ MAX_FEE_EXCEEDED
ParseOutputType
std::optional< OutputType > ParseOutputType(const std::string &type)
Definition: outputtype.cpp:24
TransactionError::ALREADY_IN_CHAIN
@ ALREADY_IN_CHAIN