Skip to content

Commit

Permalink
fuzz: add steady clock mocking to FuzzedSock
Browse files Browse the repository at this point in the history
  • Loading branch information
darosior committed Feb 12, 2025
1 parent 6fe1c35 commit 39b7e2b
Show file tree
Hide file tree
Showing 2 changed files with 21 additions and 1 deletion.
12 changes: 11 additions & 1 deletion src/test/fuzz/util/net.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -113,8 +113,10 @@ template CAddress::SerParams ConsumeDeserializationParams(FuzzedDataProvider&) n
FuzzedSock::FuzzedSock(FuzzedDataProvider& fuzzed_data_provider)
: Sock{fuzzed_data_provider.ConsumeIntegralInRange<SOCKET>(INVALID_SOCKET - 1, INVALID_SOCKET)},
m_fuzzed_data_provider{fuzzed_data_provider},
m_selectable{fuzzed_data_provider.ConsumeBool()}
m_selectable{fuzzed_data_provider.ConsumeBool()},
m_time{MockableSteadyClock::INITIAL_MOCK_TIME}
{
ElapseTime(std::chrono::seconds(0)); // start mocking the steady clock.
}

FuzzedSock::~FuzzedSock()
Expand All @@ -126,6 +128,12 @@ FuzzedSock::~FuzzedSock()
m_socket = INVALID_SOCKET;
}

void FuzzedSock::ElapseTime(std::chrono::milliseconds duration) const
{
m_time += duration;
MockableSteadyClock::SetMockTime(m_time);
}

FuzzedSock& FuzzedSock::operator=(Sock&& other)
{
assert(false && "Move of Sock into FuzzedSock not allowed.");
Expand Down Expand Up @@ -388,6 +396,7 @@ bool FuzzedSock::Wait(std::chrono::milliseconds timeout, Event requested, Event*
// FuzzedDataProvider runs out of data.
*occurred = m_fuzzed_data_provider.ConsumeBool() ? 0 : requested;
}
ElapseTime(timeout);
return true;
}

Expand All @@ -400,6 +409,7 @@ bool FuzzedSock::WaitMany(std::chrono::milliseconds timeout, EventsPerSock& even
// FuzzedDataProvider runs out of data.
events.occurred = m_fuzzed_data_provider.ConsumeBool() ? 0 : events.requested;
}
ElapseTime(timeout);
return true;
}

Expand Down
10 changes: 10 additions & 0 deletions src/test/fuzz/util/net.h
Original file line number Diff line number Diff line change
Expand Up @@ -157,6 +157,16 @@ class FuzzedSock : public Sock
*/
const bool m_selectable;

/**
* Used to mock the steady clock in methods waiting for a given duration.
*/
mutable std::chrono::milliseconds m_time;

/**
* Set the value of the mocked steady clock such as that many ms have passed.
*/
void ElapseTime(std::chrono::milliseconds duration) const;

public:
explicit FuzzedSock(FuzzedDataProvider& fuzzed_data_provider);

Expand Down

0 comments on commit 39b7e2b

Please sign in to comment.