mirror of
https://github.com/bitcoin/bitcoin.git
synced 2026-04-08 22:57:56 +02:00
fuzz: make it possible to mock (fuzz) CThreadInterrupt
* Make the methods of `CThreadInterrupt` virtual and store a pointer to it in `CConnman`, thus making it possible to override with a mocked instance. * Initialize `CConnman::m_interrupt_net` from the constructor, making it possible for callers to supply mocked version. * Introduce `FuzzedThreadInterrupt` and `ConsumeThreadInterrupt()` and use them in `src/test/fuzz/connman.cpp` and `src/test/fuzz/i2p.cpp`. This improves the CPU utilization of the `connman` fuzz test. As a nice side effect, the `std::shared_ptr` used for `CConnman::m_interrupt_net` resolves the possible lifetime issues with it (see the removed comment for that variable).
This commit is contained in:
22
src/test/fuzz/util/threadinterrupt.cpp
Normal file
22
src/test/fuzz/util/threadinterrupt.cpp
Normal file
@@ -0,0 +1,22 @@
|
||||
// Copyright (c) 2024-present 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 <test/fuzz/util.h>
|
||||
#include <test/fuzz/util/threadinterrupt.h>
|
||||
|
||||
FuzzedThreadInterrupt::FuzzedThreadInterrupt(FuzzedDataProvider& fuzzed_data_provider)
|
||||
: m_fuzzed_data_provider{fuzzed_data_provider}
|
||||
{
|
||||
}
|
||||
|
||||
bool FuzzedThreadInterrupt::interrupted() const
|
||||
{
|
||||
return m_fuzzed_data_provider.ConsumeBool();
|
||||
}
|
||||
|
||||
bool FuzzedThreadInterrupt::sleep_for(Clock::duration)
|
||||
{
|
||||
SetMockTime(ConsumeTime(m_fuzzed_data_provider)); // Time could go backwards.
|
||||
return m_fuzzed_data_provider.ConsumeBool();
|
||||
}
|
||||
Reference in New Issue
Block a user