mirror of
https://github.com/bitcoin/bitcoin.git
synced 2025-02-02 09:46:52 -05:00
fuzz: rename and improve the Miniscript Script roundtrip target
Parse also key hashes using the Key type. Make this target the first of the 4 Miniscript fuzz targets in a single `miniscript` file. Co-authored-by: Pieter Wuille <pieter.wuille@gmail.com>
This commit is contained in:
parent
7eb70f0ac0
commit
be34d5077b
3 changed files with 71 additions and 72 deletions
|
@ -269,7 +269,7 @@ test_fuzz_fuzz_SOURCES = \
|
|||
test/fuzz/locale.cpp \
|
||||
test/fuzz/merkleblock.cpp \
|
||||
test/fuzz/message.cpp \
|
||||
test/fuzz/miniscript_decode.cpp \
|
||||
test/fuzz/miniscript.cpp \
|
||||
test/fuzz/minisketch.cpp \
|
||||
test/fuzz/muhash.cpp \
|
||||
test/fuzz/multiplication_overflow.cpp \
|
||||
|
|
70
src/test/fuzz/miniscript.cpp
Normal file
70
src/test/fuzz/miniscript.cpp
Normal file
|
@ -0,0 +1,70 @@
|
|||
// 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 <core_io.h>
|
||||
#include <hash.h>
|
||||
#include <key.h>
|
||||
#include <script/miniscript.h>
|
||||
#include <script/script.h>
|
||||
#include <test/fuzz/FuzzedDataProvider.h>
|
||||
#include <test/fuzz/fuzz.h>
|
||||
#include <test/fuzz/util.h>
|
||||
#include <util/strencodings.h>
|
||||
|
||||
namespace {
|
||||
|
||||
//! Context that implements naive conversion from/to script only, for roundtrip testing.
|
||||
struct ScriptParserContext {
|
||||
//! For Script roundtrip we never need the key from a key hash.
|
||||
struct Key {
|
||||
bool is_hash;
|
||||
std::vector<unsigned char> data;
|
||||
};
|
||||
|
||||
const std::vector<unsigned char>& ToPKBytes(const Key& key) const
|
||||
{
|
||||
assert(!key.is_hash);
|
||||
return key.data;
|
||||
}
|
||||
|
||||
const std::vector<unsigned char> ToPKHBytes(const Key& key) const
|
||||
{
|
||||
if (key.is_hash) return key.data;
|
||||
const auto h = Hash160(key.data);
|
||||
return {h.begin(), h.end()};
|
||||
}
|
||||
|
||||
template<typename I>
|
||||
std::optional<Key> FromPKBytes(I first, I last) const
|
||||
{
|
||||
Key key;
|
||||
key.data.assign(first, last);
|
||||
key.is_hash = false;
|
||||
return key;
|
||||
}
|
||||
|
||||
template<typename I>
|
||||
std::optional<Key> FromPKHBytes(I first, I last) const
|
||||
{
|
||||
Key key;
|
||||
key.data.assign(first, last);
|
||||
key.is_hash = true;
|
||||
return key;
|
||||
}
|
||||
} SCRIPT_PARSER_CONTEXT;
|
||||
|
||||
}
|
||||
|
||||
/* Fuzz tests that test parsing from a script, and roundtripping via script. */
|
||||
FUZZ_TARGET(miniscript_script)
|
||||
{
|
||||
FuzzedDataProvider fuzzed_data_provider(buffer.data(), buffer.size());
|
||||
const std::optional<CScript> script = ConsumeDeserializable<CScript>(fuzzed_data_provider);
|
||||
if (!script) return;
|
||||
|
||||
const auto ms = miniscript::FromScript(*script, SCRIPT_PARSER_CONTEXT);
|
||||
if (!ms) return;
|
||||
|
||||
assert(ms->ToScript(SCRIPT_PARSER_CONTEXT) == *script);
|
||||
}
|
|
@ -1,71 +0,0 @@
|
|||
// Copyright (c) 2022 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 <core_io.h>
|
||||
#include <hash.h>
|
||||
#include <key.h>
|
||||
#include <script/miniscript.h>
|
||||
#include <script/script.h>
|
||||
#include <span.h>
|
||||
#include <test/fuzz/FuzzedDataProvider.h>
|
||||
#include <test/fuzz/fuzz.h>
|
||||
#include <test/fuzz/util.h>
|
||||
#include <util/strencodings.h>
|
||||
|
||||
#include <optional>
|
||||
|
||||
using miniscript::operator""_mst;
|
||||
|
||||
|
||||
struct Converter {
|
||||
typedef CPubKey Key;
|
||||
|
||||
std::optional<std::string> ToString(const Key& key) const {
|
||||
return HexStr(key);
|
||||
}
|
||||
const std::vector<unsigned char> ToPKBytes(const Key& key) const {
|
||||
return {key.begin(), key.end()};
|
||||
}
|
||||
const std::vector<unsigned char> ToPKHBytes(const Key& key) const {
|
||||
const auto h = Hash160(key);
|
||||
return {h.begin(), h.end()};
|
||||
}
|
||||
|
||||
template<typename I>
|
||||
std::optional<Key> FromString(I first, I last) const {
|
||||
const auto bytes = ParseHex(std::string(first, last));
|
||||
Key key{bytes.begin(), bytes.end()};
|
||||
if (key.IsValid()) return key;
|
||||
return {};
|
||||
}
|
||||
template<typename I>
|
||||
std::optional<Key> FromPKBytes(I first, I last) const {
|
||||
Key key{first, last};
|
||||
if (key.IsValid()) return key;
|
||||
return {};
|
||||
}
|
||||
template<typename I>
|
||||
std::optional<Key> FromPKHBytes(I first, I last) const {
|
||||
return {};
|
||||
}
|
||||
};
|
||||
|
||||
const Converter CONVERTER;
|
||||
|
||||
FUZZ_TARGET(miniscript_decode)
|
||||
{
|
||||
FuzzedDataProvider fuzzed_data_provider(buffer.data(), buffer.size());
|
||||
const std::optional<CScript> script = ConsumeDeserializable<CScript>(fuzzed_data_provider);
|
||||
if (!script) return;
|
||||
|
||||
const auto ms = miniscript::FromScript(*script, CONVERTER);
|
||||
if (!ms) return;
|
||||
|
||||
// We can roundtrip it to its string representation.
|
||||
std::string ms_str = *ms->ToString(CONVERTER);
|
||||
assert(*miniscript::FromString(ms_str, CONVERTER) == *ms);
|
||||
// The Script representation must roundtrip since we parsed it this way the first time.
|
||||
const CScript ms_script = ms->ToScript(CONVERTER);
|
||||
assert(ms_script == *script);
|
||||
}
|
Loading…
Add table
Reference in a new issue