Skip to content

Commit 39b7e2b

Browse files
committed
fuzz: add steady clock mocking to FuzzedSock
1 parent 6fe1c35 commit 39b7e2b

File tree

2 files changed

+21
-1
lines changed

2 files changed

+21
-1
lines changed

src/test/fuzz/util/net.cpp

Lines changed: 11 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -113,8 +113,10 @@ template CAddress::SerParams ConsumeDeserializationParams(FuzzedDataProvider&) n
113113
FuzzedSock::FuzzedSock(FuzzedDataProvider& fuzzed_data_provider)
114114
: Sock{fuzzed_data_provider.ConsumeIntegralInRange<SOCKET>(INVALID_SOCKET - 1, INVALID_SOCKET)},
115115
m_fuzzed_data_provider{fuzzed_data_provider},
116-
m_selectable{fuzzed_data_provider.ConsumeBool()}
116+
m_selectable{fuzzed_data_provider.ConsumeBool()},
117+
m_time{MockableSteadyClock::INITIAL_MOCK_TIME}
117118
{
119+
ElapseTime(std::chrono::seconds(0)); // start mocking the steady clock.
118120
}
119121

120122
FuzzedSock::~FuzzedSock()
@@ -126,6 +128,12 @@ FuzzedSock::~FuzzedSock()
126128
m_socket = INVALID_SOCKET;
127129
}
128130

131+
void FuzzedSock::ElapseTime(std::chrono::milliseconds duration) const
132+
{
133+
m_time += duration;
134+
MockableSteadyClock::SetMockTime(m_time);
135+
}
136+
129137
FuzzedSock& FuzzedSock::operator=(Sock&& other)
130138
{
131139
assert(false && "Move of Sock into FuzzedSock not allowed.");
@@ -388,6 +396,7 @@ bool FuzzedSock::Wait(std::chrono::milliseconds timeout, Event requested, Event*
388396
// FuzzedDataProvider runs out of data.
389397
*occurred = m_fuzzed_data_provider.ConsumeBool() ? 0 : requested;
390398
}
399+
ElapseTime(timeout);
391400
return true;
392401
}
393402

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

src/test/fuzz/util/net.h

Lines changed: 10 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -157,6 +157,16 @@ class FuzzedSock : public Sock
157157
*/
158158
const bool m_selectable;
159159

160+
/**
161+
* Used to mock the steady clock in methods waiting for a given duration.
162+
*/
163+
mutable std::chrono::milliseconds m_time;
164+
165+
/**
166+
* Set the value of the mocked steady clock such as that many ms have passed.
167+
*/
168+
void ElapseTime(std::chrono::milliseconds duration) const;
169+
160170
public:
161171
explicit FuzzedSock(FuzzedDataProvider& fuzzed_data_provider);
162172

0 commit comments

Comments
 (0)