|
| 1 | +// Copyright (c) 2019 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 <arith_uint256.h> |
| 6 | +#include <compressor.h> |
| 7 | +#include <consensus/merkle.h> |
| 8 | +#include <core_io.h> |
| 9 | +#include <crypto/common.h> |
| 10 | +#include <crypto/siphash.h> |
| 11 | +#include <key_io.h> |
| 12 | +#include <memusage.h> |
| 13 | +#include <netbase.h> |
| 14 | +#include <policy/settings.h> |
| 15 | +#include <pow.h> |
| 16 | +#include <pubkey.h> |
| 17 | +#include <rpc/util.h> |
| 18 | +#include <script/signingprovider.h> |
| 19 | +#include <script/standard.h> |
| 20 | +#include <serialize.h> |
| 21 | +#include <test/fuzz/FuzzedDataProvider.h> |
| 22 | +#include <test/fuzz/fuzz.h> |
| 23 | +#include <uint256.h> |
| 24 | +#include <util/strencodings.h> |
| 25 | +#include <util/system.h> |
| 26 | +#include <util/time.h> |
| 27 | + |
| 28 | +#include <cassert> |
| 29 | +#include <limits> |
| 30 | +#include <vector> |
| 31 | + |
| 32 | +void initialize() |
| 33 | +{ |
| 34 | + SelectParams(CBaseChainParams::REGTEST); |
| 35 | +} |
| 36 | + |
| 37 | +void test_one_input(const std::vector<uint8_t>& buffer) |
| 38 | +{ |
| 39 | + if (buffer.size() < sizeof(uint256) + sizeof(uint160)) { |
| 40 | + return; |
| 41 | + } |
| 42 | + FuzzedDataProvider fuzzed_data_provider(buffer.data(), buffer.size()); |
| 43 | + const uint256 u256(fuzzed_data_provider.ConsumeBytes<unsigned char>(sizeof(uint256))); |
| 44 | + const uint160 u160(fuzzed_data_provider.ConsumeBytes<unsigned char>(sizeof(uint160))); |
| 45 | + const uint64_t u64 = fuzzed_data_provider.ConsumeIntegral<uint64_t>(); |
| 46 | + const int64_t i64 = fuzzed_data_provider.ConsumeIntegral<int64_t>(); |
| 47 | + const uint32_t u32 = fuzzed_data_provider.ConsumeIntegral<uint32_t>(); |
| 48 | + const int32_t i32 = fuzzed_data_provider.ConsumeIntegral<int32_t>(); |
| 49 | + const uint16_t u16 = fuzzed_data_provider.ConsumeIntegral<uint16_t>(); |
| 50 | + const int16_t i16 = fuzzed_data_provider.ConsumeIntegral<int16_t>(); |
| 51 | + const uint8_t u8 = fuzzed_data_provider.ConsumeIntegral<uint8_t>(); |
| 52 | + const int8_t i8 = fuzzed_data_provider.ConsumeIntegral<int8_t>(); |
| 53 | + // We cannot assume a specific value of std::is_signed<char>::value: |
| 54 | + // ConsumeIntegral<char>() instead of casting from {u,}int8_t. |
| 55 | + const char ch = fuzzed_data_provider.ConsumeIntegral<char>(); |
| 56 | + |
| 57 | + const Consensus::Params& consensus_params = Params().GetConsensus(); |
| 58 | + (void)CheckProofOfWork(u256, u32, consensus_params); |
| 59 | + (void)CompressAmount(u64); |
| 60 | + static const uint256 u256_min(uint256S("0000000000000000000000000000000000000000000000000000000000000000")); |
| 61 | + static const uint256 u256_max(uint256S("ffffffffffffffffffffffffffffffffffffffffffffffffffffffffffffffff")); |
| 62 | + const std::vector<uint256> v256{u256, u256_min, u256_max}; |
| 63 | + (void)ComputeMerkleRoot(v256); |
| 64 | + (void)CountBits(u64); |
| 65 | + (void)DecompressAmount(u64); |
| 66 | + (void)FormatISO8601Date(i64); |
| 67 | + (void)FormatISO8601DateTime(i64); |
| 68 | + (void)GetSizeOfCompactSize(u64); |
| 69 | + (void)GetSpecialScriptSize(u32); |
| 70 | + // (void)GetVirtualTransactionSize(i64, i64); // function defined only for a subset of int64_t inputs |
| 71 | + // (void)GetVirtualTransactionSize(i64, i64, u32); // function defined only for a subset of int64_t/uint32_t inputs |
| 72 | + (void)HexDigit(ch); |
| 73 | + (void)i64tostr(i64); |
| 74 | + (void)IsDigit(ch); |
| 75 | + (void)IsSpace(ch); |
| 76 | + (void)IsSwitchChar(ch); |
| 77 | + (void)itostr(i32); |
| 78 | + (void)memusage::DynamicUsage(ch); |
| 79 | + (void)memusage::DynamicUsage(i16); |
| 80 | + (void)memusage::DynamicUsage(i32); |
| 81 | + (void)memusage::DynamicUsage(i64); |
| 82 | + (void)memusage::DynamicUsage(i8); |
| 83 | + (void)memusage::DynamicUsage(u16); |
| 84 | + (void)memusage::DynamicUsage(u32); |
| 85 | + (void)memusage::DynamicUsage(u64); |
| 86 | + (void)memusage::DynamicUsage(u8); |
| 87 | + const unsigned char uch = static_cast<unsigned char>(u8); |
| 88 | + (void)memusage::DynamicUsage(uch); |
| 89 | + (void)MillisToTimeval(i64); |
| 90 | + const double d = ser_uint64_to_double(u64); |
| 91 | + assert(ser_double_to_uint64(d) == u64); |
| 92 | + const float f = ser_uint32_to_float(u32); |
| 93 | + assert(ser_float_to_uint32(f) == u32); |
| 94 | + (void)SighashToStr(uch); |
| 95 | + (void)SipHashUint256(u64, u64, u256); |
| 96 | + (void)SipHashUint256Extra(u64, u64, u256, u32); |
| 97 | + (void)ToLower(ch); |
| 98 | + |
| 99 | + const arith_uint256 au256 = UintToArith256(u256); |
| 100 | + assert(ArithToUint256(au256) == u256); |
| 101 | + assert(uint256S(au256.GetHex()) == u256); |
| 102 | + (void)au256.bits(); |
| 103 | + (void)au256.GetCompact(/* fNegative= */ false); |
| 104 | + (void)au256.GetCompact(/* fNegative= */ true); |
| 105 | + (void)au256.getdouble(); |
| 106 | + (void)au256.GetHex(); |
| 107 | + (void)au256.GetLow64(); |
| 108 | + (void)au256.size(); |
| 109 | + (void)au256.ToString(); |
| 110 | + |
| 111 | + const CKeyID key_id{u160}; |
| 112 | + const CScriptID script_id{u160}; |
| 113 | + // CTxDestination = CNoDestination ∪ PKHash ∪ ScriptHash ∪ WitnessV0ScriptHash ∪ WitnessV0KeyHash ∪ WitnessUnknown |
| 114 | + const PKHash pk_hash{u160}; |
| 115 | + const ScriptHash script_hash{u160}; |
| 116 | + const WitnessV0KeyHash witness_v0_key_hash{u160}; |
| 117 | + const WitnessV0ScriptHash witness_v0_script_hash{u256}; |
| 118 | + const std::vector<CTxDestination> destinations{pk_hash, script_hash, witness_v0_key_hash, witness_v0_script_hash}; |
| 119 | + const SigningProvider store; |
| 120 | + for (const CTxDestination& destination : destinations) { |
| 121 | + (void)DescribeAddress(destination); |
| 122 | + (void)EncodeDestination(destination); |
| 123 | + (void)GetKeyForDestination(store, destination); |
| 124 | + (void)GetScriptForDestination(destination); |
| 125 | + (void)IsValidDestination(destination); |
| 126 | + } |
| 127 | +} |
0 commit comments