Bitcoin Core 28.99.0
P2P Digital Currency
Public Member Functions | Private Member Functions | Private Attributes | List of all members
PCPTestSock Class Referencefinal

Simple scripted UDP server emulation for testing. More...

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

Public Member Functions

 PCPTestSock (const CNetAddr &local_ip, const CNetAddr &gateway_ip, const std::vector< TestOp > &script)
 
PCPTestSockoperator= (Sock &&other) override
 Move assignment operator, grab the socket from another object and close ours (if set). More...
 
ssize_t Send (const void *data, size_t len, int) 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 *sa, socklen_t sa_len) const override
 connect(2) wrapper. More...
 
int Bind (const sockaddr *sa, socklen_t sa_len) const override
 bind(2) wrapper. More...
 
int Listen (int) const override
 listen(2) wrapper. More...
 
std::unique_ptr< SockAccept (sockaddr *addr, socklen_t *addr_len) const override
 accept(2) wrapper. More...
 
int GetSockOpt (int level, int opt_name, void *opt_val, socklen_t *opt_len) const override
 getsockopt(2) wrapper. More...
 
int SetSockOpt (int, int, const void *, socklen_t) const override
 setsockopt(2) wrapper. More...
 
int GetSockName (sockaddr *name, socklen_t *name_len) const override
 getsockname(2) wrapper. More...
 
bool SetNonBlocking () const override
 Set the non-blocking option on the socket. More...
 
bool IsSelectable () const override
 Check if the underlying socket can be used for select(2) (or the Wait() method). 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 WaitMany (std::chrono::milliseconds timeout, EventsPerSock &events_per_sock) const override
 Same as Wait(), but wait on many sockets within the same timeout. More...
 
bool IsConnected (std::string &) const override
 Check if still connected. More...
 
- Public Member Functions inherited from Sock
 Sock ()=delete
 
 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 Sockoperator= (Sock &&other)
 Move assignment operator, grab the socket from another object and close ours (if set). More...
 
virtual ssize_t Send (const void *data, size_t len, int flags) const
 send(2) wrapper. More...
 
virtual ssize_t Recv (void *buf, size_t len, int flags) const
 recv(2) wrapper. More...
 
virtual int Connect (const sockaddr *addr, socklen_t addr_len) const
 connect(2) wrapper. More...
 
virtual int Bind (const sockaddr *addr, socklen_t addr_len) const
 bind(2) wrapper. More...
 
virtual int Listen (int backlog) const
 listen(2) wrapper. More...
 
virtual std::unique_ptr< SockAccept (sockaddr *addr, socklen_t *addr_len) const
 accept(2) wrapper. More...
 
virtual int GetSockOpt (int level, int opt_name, void *opt_val, socklen_t *opt_len) const
 getsockopt(2) wrapper. More...
 
virtual int SetSockOpt (int level, int opt_name, const void *opt_val, socklen_t opt_len) const
 setsockopt(2) wrapper. More...
 
virtual int GetSockName (sockaddr *name, socklen_t *name_len) const
 getsockname(2) wrapper. More...
 
virtual bool SetNonBlocking () const
 Set the non-blocking option on the socket. More...
 
virtual bool IsSelectable () const
 Check if the underlying socket can be used for select(2) (or the Wait() method). More...
 
virtual bool Wait (std::chrono::milliseconds timeout, Event requested, Event *occurred=nullptr) const
 Wait for readiness for input (recv) or output (send). More...
 
virtual bool WaitMany (std::chrono::milliseconds timeout, EventsPerSock &events_per_sock) const
 Same as Wait(), but wait on many sockets within the same timeout. More...
 
virtual void SendComplete (Span< const unsigned char > data, std::chrono::milliseconds timeout, CThreadInterrupt &interrupt) const
 Send the given data, retrying on transient errors. More...
 
virtual void SendComplete (Span< const char > data, std::chrono::milliseconds timeout, CThreadInterrupt &interrupt) const
 Convenience method, equivalent to SendComplete(MakeUCharSpan(data), timeout, interrupt). 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...
 
virtual bool IsConnected (std::string &errmsg) const
 Check if still connected. More...
 
bool operator== (SOCKET s) const
 Check if the internal socket is equal to s. More...
 

Private Member Functions

void ElapseTime (std::chrono::milliseconds duration) const
 
bool AtEndOfScript () const
 
const TestOpCurOp () const
 
void PrepareOp () const
 
void AdvanceOp () const
 
void FailScript () const
 

Private Attributes

const std::vector< TestOpm_script
 
size_t m_script_ptr = 0
 
std::chrono::milliseconds m_time_left
 
std::chrono::milliseconds m_time {MockableSteadyClock::INITIAL_MOCK_TIME}
 
bool m_connected {false}
 
CService m_bound
 
CNetAddr m_local_ip
 
CNetAddr m_gateway_ip
 

Additional Inherited Members

- Public Types inherited from Sock
using Event = uint8_t
 
using EventsPerSock = std::unordered_map< std::shared_ptr< const Sock >, Events, HashSharedPtrSock, EqualSharedPtrSock >
 On which socket to wait for what events in WaitMany(). More...
 
- Static Public Attributes inherited from Sock
static constexpr Event RECV = 0b001
 If passed to Wait(), then it will wait for readiness to read from the socket. More...
 
static constexpr Event SEND = 0b010
 If passed to Wait(), then it will wait for readiness to send to the socket. More...
 
static constexpr Event ERR = 0b100
 Ignored if passed to Wait(), but could be set in the occurred events if an exceptional condition has occurred on the socket or if it has been disconnected. More...
 
- Protected Attributes inherited from Sock
SOCKET m_socket
 Contained socket. More...
 

Detailed Description

Simple scripted UDP server emulation for testing.

Definition at line 81 of file pcp_tests.cpp.

Constructor & Destructor Documentation

◆ PCPTestSock()

PCPTestSock::PCPTestSock ( const CNetAddr local_ip,
const CNetAddr gateway_ip,
const std::vector< TestOp > &  script 
)
inlineexplicit

Definition at line 86 of file pcp_tests.cpp.

Here is the call graph for this function:

Member Function Documentation

◆ Accept()

std::unique_ptr< Sock > PCPTestSock::Accept ( sockaddr *  addr,
socklen_t *  addr_len 
) const
inlineoverridevirtual

accept(2) wrapper.

Equivalent to std::make_unique<Sock>(accept(m_socket, addr, addr_len)). Code that uses this wrapper can be unit tested if this method is overridden by a mock Sock implementation. The returned unique_ptr is empty if accept() failed in which case errno will be set.

Reimplemented from Sock.

Definition at line 165 of file pcp_tests.cpp.

◆ AdvanceOp()

void PCPTestSock::AdvanceOp ( ) const
inlineprivate

Definition at line 246 of file pcp_tests.cpp.

Here is the call graph for this function:
Here is the caller graph for this function:

◆ AtEndOfScript()

bool PCPTestSock::AtEndOfScript ( ) const
inlineprivate

Definition at line 235 of file pcp_tests.cpp.

Here is the caller graph for this function:

◆ Bind()

int PCPTestSock::Bind ( const sockaddr *  addr,
socklen_t  addr_len 
) const
inlineoverridevirtual

bind(2) wrapper.

Equivalent to bind(m_socket, addr, addr_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 150 of file pcp_tests.cpp.

Here is the call graph for this function:

◆ Connect()

int PCPTestSock::Connect ( const sockaddr *  addr,
socklen_t  addr_len 
) const
inlineoverridevirtual

connect(2) wrapper.

Equivalent to connect(m_socket, 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 pcp_tests.cpp.

Here is the call graph for this function:

◆ CurOp()

const TestOp & PCPTestSock::CurOp ( ) const
inlineprivate

Definition at line 236 of file pcp_tests.cpp.

Here is the caller graph for this function:

◆ ElapseTime()

void PCPTestSock::ElapseTime ( std::chrono::milliseconds  duration) const
inlineprivate

Definition at line 229 of file pcp_tests.cpp.

Here is the call graph for this function:
Here is the caller graph for this function:

◆ FailScript()

void PCPTestSock::FailScript ( ) const
inlineprivate

Definition at line 252 of file pcp_tests.cpp.

Here is the caller graph for this function:

◆ GetSockName()

int PCPTestSock::GetSockName ( sockaddr *  name,
socklen_t *  name_len 
) const
inlineoverridevirtual

getsockname(2) wrapper.

Equivalent to getsockname(m_socket, name, name_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 177 of file pcp_tests.cpp.

Here is the call graph for this function:

◆ GetSockOpt()

int PCPTestSock::GetSockOpt ( int  level,
int  opt_name,
void *  opt_val,
socklen_t *  opt_len 
) const
inlineoverridevirtual

getsockopt(2) wrapper.

Equivalent to getsockopt(m_socket, 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 170 of file pcp_tests.cpp.

◆ IsConnected()

bool PCPTestSock::IsConnected ( std::string &  errmsg) const
inlineoverridevirtual

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 214 of file pcp_tests.cpp.

◆ IsSelectable()

bool PCPTestSock::IsSelectable ( ) const
inlineoverridevirtual

Check if the underlying socket can be used for select(2) (or the Wait() method).

Returns
true if selectable

Reimplemented from Sock.

Definition at line 185 of file pcp_tests.cpp.

◆ Listen()

int PCPTestSock::Listen ( int  backlog) const
inlineoverridevirtual

listen(2) wrapper.

Equivalent to listen(m_socket, backlog). 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 163 of file pcp_tests.cpp.

◆ operator=()

PCPTestSock & PCPTestSock::operator= ( Sock &&  other)
inlineoverridevirtual

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

Reimplemented from Sock.

Definition at line 96 of file pcp_tests.cpp.

Here is the call graph for this function:

◆ PrepareOp()

void PCPTestSock::PrepareOp ( ) const
inlineprivate

Definition at line 241 of file pcp_tests.cpp.

Here is the call graph for this function:
Here is the caller graph for this function:

◆ Recv()

ssize_t PCPTestSock::Recv ( void *  buf,
size_t  len,
int  flags 
) const
inlineoverridevirtual

recv(2) wrapper.

Equivalent to recv(m_socket, 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 120 of file pcp_tests.cpp.

Here is the call graph for this function:

◆ Send()

ssize_t PCPTestSock::Send ( const void *  data,
size_t  len,
int  flags 
) const
inlineoverridevirtual

send(2) wrapper.

Equivalent to send(m_socket, 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 102 of file pcp_tests.cpp.

Here is the call graph for this function:

◆ SetNonBlocking()

bool PCPTestSock::SetNonBlocking ( ) const
inlineoverridevirtual

Set the non-blocking option on the socket.

Returns
true if set successfully

Reimplemented from Sock.

Definition at line 183 of file pcp_tests.cpp.

◆ SetSockOpt()

int PCPTestSock::SetSockOpt ( int  level,
int  opt_name,
const void *  opt_val,
socklen_t  opt_len 
) const
inlineoverridevirtual

setsockopt(2) wrapper.

Equivalent to setsockopt(m_socket, 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 175 of file pcp_tests.cpp.

◆ Wait()

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

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 the function returns true, then this indicates which of the requested events occurred (ERR will be added, even if not requested, if an exceptional event occurs on the socket). A timeout is indicated by return value of true and occurred being set to 0.
Returns
true on success (or timeout, if occurred of 0 is returned), false otherwise

Reimplemented from Sock.

Definition at line 187 of file pcp_tests.cpp.

Here is the call graph for this function:

◆ WaitMany()

bool PCPTestSock::WaitMany ( std::chrono::milliseconds  timeout,
EventsPerSock events_per_sock 
) const
inlineoverridevirtual

Same as Wait(), but wait on many sockets within the same timeout.

Parameters
[in]timeoutWait this long for at least one of the requested events to occur.
[in,out]events_per_sockWait for the requested events on these sockets and set occurred for the events that actually occurred.
Returns
true on success (or timeout, if all what[].occurred are returned as 0), false otherwise

Reimplemented from Sock.

Definition at line 209 of file pcp_tests.cpp.

Member Data Documentation

◆ m_bound

CService PCPTestSock::m_bound
mutableprivate

Definition at line 225 of file pcp_tests.cpp.

◆ m_connected

bool PCPTestSock::m_connected {false}
mutableprivate

Definition at line 224 of file pcp_tests.cpp.

◆ m_gateway_ip

CNetAddr PCPTestSock::m_gateway_ip
mutableprivate

Definition at line 227 of file pcp_tests.cpp.

◆ m_local_ip

CNetAddr PCPTestSock::m_local_ip
mutableprivate

Definition at line 226 of file pcp_tests.cpp.

◆ m_script

const std::vector<TestOp> PCPTestSock::m_script
private

Definition at line 220 of file pcp_tests.cpp.

◆ m_script_ptr

size_t PCPTestSock::m_script_ptr = 0
mutableprivate

Definition at line 221 of file pcp_tests.cpp.

◆ m_time

std::chrono::milliseconds PCPTestSock::m_time {MockableSteadyClock::INITIAL_MOCK_TIME}
mutableprivate

Definition at line 223 of file pcp_tests.cpp.

◆ m_time_left

std::chrono::milliseconds PCPTestSock::m_time_left
mutableprivate

Definition at line 222 of file pcp_tests.cpp.


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