Skip to content

Commit fa02243

Browse files
committed
fuzz: verify has_priv_key
Use HasPrivKey to ensure the correct return value for has_priv_key. Add coverage for it in the harness.
1 parent b3026b1 commit fa02243

File tree

1 file changed

+20
-8
lines changed

1 file changed

+20
-8
lines changed

src/test/fuzz/miniscript.cpp

Lines changed: 20 additions & 8 deletions
Original file line numberDiff line numberDiff line change
@@ -132,13 +132,10 @@ struct ParserContext {
132132

133133
std::optional<std::string> ToString(const Key& key, bool& has_priv_key) const
134134
{
135-
has_priv_key = false;
136-
auto it = TEST_DATA.dummy_key_idx_map.find(key);
137-
if (it == TEST_DATA.dummy_key_idx_map.end()) {
138-
return HexStr(key);
139-
}
140-
has_priv_key = true;
141-
uint8_t idx = it->second;
135+
const auto it = TEST_DATA.dummy_key_idx_map.find(key);
136+
assert(it != TEST_DATA.dummy_key_idx_map.end());
137+
has_priv_key = TEST_DATA.HasPrivKey(script_ctx, key);
138+
const uint8_t idx = it->second;
142139
return HexStr(std::span{&idx, 1});
143140
}
144141

@@ -1043,8 +1040,23 @@ void TestNode(const MsCtx script_ctx, const NodeRef& node, FuzzedDataProvider& p
10431040

10441041
// Check that it roundtrips to text representation
10451042
const ParserContext parser_ctx{script_ctx};
1046-
std::optional<std::string> str{node->ToString(parser_ctx)};
1043+
bool has_priv_key{false};
1044+
std::optional<std::string> str{node->ToString(parser_ctx, has_priv_key)};
10471045
assert(str);
1046+
1047+
// Check has_priv_key against expectation
1048+
bool expected_has_priv_key{false};
1049+
std::vector<const Node*> nodes{node.get()};
1050+
while (!nodes.empty() && !expected_has_priv_key) {
1051+
const Node* n{nodes.back()};
1052+
nodes.pop_back();
1053+
expected_has_priv_key = std::any_of(n->keys.begin(), n->keys.end(), [&](const auto& key) {
1054+
return TEST_DATA.HasPrivKey(script_ctx, key);
1055+
});
1056+
for (const auto& sub : n->subs) nodes.push_back(sub.get());
1057+
}
1058+
assert(has_priv_key == expected_has_priv_key);
1059+
10481060
auto parsed = miniscript::FromString(*str, parser_ctx);
10491061
assert(parsed);
10501062
assert(*parsed == *node);

0 commit comments

Comments
 (0)