Bitcoin Core  22.99.0
P2P Digital Currency
Public Member Functions | Private Attributes | List of all members
FuzzedSock Class Reference

#include <util.h>

Inheritance diagram for FuzzedSock:
[legend]
Collaboration diagram for FuzzedSock:
[legend]

Public Member Functions

 FuzzedSock (FuzzedDataProvider &fuzzed_data_provider)
 
 ~FuzzedSock () override
 
FuzzedSockoperator= (Sock &&other) override
 Move assignment operator, grab the socket from another object and close ours (if set). More...
 
void Reset () override
 Close if non-empty. More...
 
ssize_t Send (const void *data, size_t len, int flags) const override
 send(2) wrapper. More...
 
ssize_t Recv (void *buf, size_t len, int flags) const override
 recv(2) wrapper. More...
 
int Connect (const sockaddr *, socklen_t) const override
 connect(2) wrapper. More...
 
int GetSockOpt (int level, int opt_name, void *opt_val, socklen_t *opt_len) const override
 getsockopt(2) wrapper. More...
 
bool Wait (std::chrono::milliseconds timeout, Event requested, Event *occurred=nullptr) const override
 Wait for readiness for input (recv) or output (send). More...
 
bool IsConnected (std::string &errmsg) const override
 Check if still connected. More...
 
- Public Member Functions inherited from Sock
 Sock ()
 Default constructor, creates an empty object that does nothing when destroyed. More...
 
 Sock (SOCKET s)
 Take ownership of an existent socket. More...
 
 Sock (const Sock &)=delete
 Copy constructor, disabled because closing the same socket twice is undesirable. More...
 
 Sock (Sock &&other)
 Move constructor, grab the socket from another object and close ours (if set). More...
 
virtual ~Sock ()
 Destructor, close the socket or do nothing if empty. More...
 
Sockoperator= (const Sock &)=delete
 Copy assignment operator, disabled because closing the same socket twice is undesirable. More...
 
virtual SOCKET Get () const
 Get the value of the contained socket. More...
 
virtual SOCKET Release ()
 Get the value of the contained socket and drop ownership. More...
 
virtual void SendComplete (const std::string &data, std::chrono::milliseconds timeout, CThreadInterrupt &interrupt) const
 Send the given data, retrying on transient errors. More...
 
virtual std::string RecvUntilTerminator (uint8_t terminator, std::chrono::milliseconds timeout, CThreadInterrupt &interrupt, size_t max_data) const
 Read from socket until a terminator character is encountered. More...
 

Private Attributes

FuzzedDataProviderm_fuzzed_data_provider
 
std::optional< uint8_t > m_peek_data
 Data to return when MSG_PEEK is used as a Recv() flag. More...
 

Additional Inherited Members

- Public Types inherited from Sock
using Event = uint8_t
 
- Static Public Attributes inherited from Sock
static constexpr Event RECV = 0b01
 If passed to Wait(), then it will wait for readiness to read from the socket. More...
 
static constexpr Event SEND = 0b10
 If passed to Wait(), then it will wait for readiness to send to the socket. More...
 
- Protected Attributes inherited from Sock
SOCKET m_socket
 Contained socket. More...
 

Detailed Description

Definition at line 387 of file util.h.

Constructor & Destructor Documentation

◆ FuzzedSock()

FuzzedSock::FuzzedSock ( FuzzedDataProvider fuzzed_data_provider)
explicit

Definition at line 12 of file util.cpp.

◆ ~FuzzedSock()

FuzzedSock::~FuzzedSock ( )
override

Definition at line 18 of file util.cpp.

Here is the call graph for this function:

Member Function Documentation

◆ Connect()

int FuzzedSock::Connect ( const sockaddr *  addr,
socklen_t  addr_len 
) const
overridevirtual

connect(2) wrapper.

Equivalent to connect(this->Get(), addr, addrlen). Code that uses this wrapper can be unit tested if this method is overridden by a mock Sock implementation.

Reimplemented from Sock.

Definition at line 135 of file util.cpp.

Here is the call graph for this function:

◆ GetSockOpt()

int FuzzedSock::GetSockOpt ( int  level,
int  opt_name,
void *  opt_val,
socklen_t *  opt_len 
) const
overridevirtual

getsockopt(2) wrapper.

Equivalent to getsockopt(this->Get(), level, opt_name, opt_val, opt_len). Code that uses this wrapper can be unit tested if this method is overridden by a mock Sock implementation.

Reimplemented from Sock.

Definition at line 157 of file util.cpp.

Here is the call graph for this function:

◆ IsConnected()

bool FuzzedSock::IsConnected ( std::string &  errmsg) const
overridevirtual

Check if still connected.

Parameters
[out]errmsgThe error string, if the socket has been disconnected.
Returns
true if connected

Reimplemented from Sock.

Definition at line 193 of file util.cpp.

Here is the call graph for this function:

◆ operator=()

FuzzedSock & FuzzedSock::operator= ( Sock &&  other)
overridevirtual

Move assignment operator, grab the socket from another object and close ours (if set).

Reimplemented from Sock.

Definition at line 27 of file util.cpp.

Here is the call graph for this function:

◆ Recv()

ssize_t FuzzedSock::Recv ( void *  buf,
size_t  len,
int  flags 
) const
overridevirtual

recv(2) wrapper.

Equivalent to recv(this->Get(), buf, len, flags);. Code that uses this wrapper can be unit tested if this method is overridden by a mock Sock implementation.

Reimplemented from Sock.

Definition at line 70 of file util.cpp.

Here is the call graph for this function:

◆ Reset()

void FuzzedSock::Reset ( )
overridevirtual

Close if non-empty.

Reimplemented from Sock.

Definition at line 33 of file util.cpp.

Here is the caller graph for this function:

◆ Send()

ssize_t FuzzedSock::Send ( const void *  data,
size_t  len,
int  flags 
) const
overridevirtual

send(2) wrapper.

Equivalent to send(this->Get(), data, len, flags);. Code that uses this wrapper can be unit tested if this method is overridden by a mock Sock implementation.

Reimplemented from Sock.

Definition at line 38 of file util.cpp.

Here is the call graph for this function:

◆ Wait()

bool FuzzedSock::Wait ( std::chrono::milliseconds  timeout,
Event  requested,
Event occurred = nullptr 
) const
overridevirtual

Wait for readiness for input (recv) or output (send).

Parameters
[in]timeoutWait this much for at least one of the requested events to occur.
[in]requestedWait for those events, bitwise-or of RECV and SEND.
[out]occurredIf not nullptr and true is returned, then upon return this indicates which of the requested events occurred. A timeout is indicated by return value of true and occurred being set to 0.
Returns
true on success and false otherwise

Reimplemented from Sock.

Definition at line 176 of file util.cpp.

Here is the call graph for this function:

Member Data Documentation

◆ m_fuzzed_data_provider

FuzzedDataProvider& FuzzedSock::m_fuzzed_data_provider
private

Definition at line 389 of file util.h.

◆ m_peek_data

std::optional<uint8_t> FuzzedSock::m_peek_data
mutableprivate

Data to return when MSG_PEEK is used as a Recv() flag.

If MSG_PEEK is used, then our Recv() returns some random data as usual, but on the next Recv() call we must return the same data, thus we remember it here.

Definition at line 396 of file util.h.


The documentation for this class was generated from the following files: