Skip to content

Commit c24804c

Browse files
committed
merge bitcoin#24378: make bind() and listen() mockable/testable
1 parent be19868 commit c24804c

File tree

6 files changed

+71
-3
lines changed

6 files changed

+71
-3
lines changed

src/net.cpp

Lines changed: 2 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -3246,8 +3246,7 @@ bool CConnman::BindListenPort(const CService& addrBind, bilingual_str& strError,
32463246
#endif
32473247
}
32483248

3249-
if (::bind(sock->Get(), (struct sockaddr*)&sockaddr, len) == SOCKET_ERROR)
3250-
{
3249+
if (sock->Bind(reinterpret_cast<struct sockaddr*>(&sockaddr), len) == SOCKET_ERROR) {
32513250
int nErr = WSAGetLastError();
32523251
if (nErr == WSAEADDRINUSE)
32533252
strError = strprintf(_("Unable to bind to %s on this computer. %s is probably already running."), addrBind.ToString(), PACKAGE_NAME);
@@ -3259,7 +3258,7 @@ bool CConnman::BindListenPort(const CService& addrBind, bilingual_str& strError,
32593258
LogPrintf("Bound to %s\n", addrBind.ToString());
32603259

32613260
// Listen for incoming connections
3262-
if (listen(sock->Get(), SOMAXCONN) == SOCKET_ERROR)
3261+
if (sock->Listen(SOMAXCONN) == SOCKET_ERROR)
32633262
{
32643263
strError = strprintf(_("Error: Listening for incoming connections failed (listen returned error %s)"), NetworkErrorString(WSAGetLastError()));
32653264
LogPrintf("%s\n", strError.original);

src/test/fuzz/util.cpp

Lines changed: 39 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -157,6 +157,45 @@ int FuzzedSock::Connect(const sockaddr*, socklen_t) const
157157
return 0;
158158
}
159159

160+
int FuzzedSock::Bind(const sockaddr*, socklen_t) const
161+
{
162+
// Have a permanent error at bind_errnos[0] because when the fuzzed data is exhausted
163+
// SetFuzzedErrNo() will always set the global errno to bind_errnos[0]. We want to
164+
// avoid this method returning -1 and setting errno to a temporary error (like EAGAIN)
165+
// repeatedly because proper code should retry on temporary errors, leading to an
166+
// infinite loop.
167+
constexpr std::array bind_errnos{
168+
EACCES,
169+
EADDRINUSE,
170+
EADDRNOTAVAIL,
171+
EAGAIN,
172+
};
173+
if (m_fuzzed_data_provider.ConsumeBool()) {
174+
SetFuzzedErrNo(m_fuzzed_data_provider, bind_errnos);
175+
return -1;
176+
}
177+
return 0;
178+
}
179+
180+
int FuzzedSock::Listen(int) const
181+
{
182+
// Have a permanent error at listen_errnos[0] because when the fuzzed data is exhausted
183+
// SetFuzzedErrNo() will always set the global errno to listen_errnos[0]. We want to
184+
// avoid this method returning -1 and setting errno to a temporary error (like EAGAIN)
185+
// repeatedly because proper code should retry on temporary errors, leading to an
186+
// infinite loop.
187+
constexpr std::array listen_errnos{
188+
EADDRINUSE,
189+
EINVAL,
190+
EOPNOTSUPP,
191+
};
192+
if (m_fuzzed_data_provider.ConsumeBool()) {
193+
SetFuzzedErrNo(m_fuzzed_data_provider, listen_errnos);
194+
return -1;
195+
}
196+
return 0;
197+
}
198+
160199
std::unique_ptr<Sock> FuzzedSock::Accept(sockaddr* addr, socklen_t* addr_len) const
161200
{
162201
constexpr std::array accept_errnos{

src/test/fuzz/util.h

Lines changed: 4 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -66,6 +66,10 @@ class FuzzedSock : public Sock
6666

6767
int Connect(const sockaddr*, socklen_t) const override;
6868

69+
int Bind(const sockaddr*, socklen_t) const override;
70+
71+
int Listen(int backlog) const override;
72+
6973
std::unique_ptr<Sock> Accept(sockaddr* addr, socklen_t* addr_len) const override;
7074

7175
int GetSockOpt(int level, int opt_name, void* opt_val, socklen_t* opt_len) const override;

src/test/util/net.h

Lines changed: 4 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -127,6 +127,10 @@ class StaticContentsSock : public Sock
127127

128128
int Connect(const sockaddr*, socklen_t) const override { return 0; }
129129

130+
int Bind(const sockaddr*, socklen_t) const override { return 0; }
131+
132+
int Listen(int) const override { return 0; }
133+
130134
std::unique_ptr<Sock> Accept(sockaddr* addr, socklen_t* addr_len) const override
131135
{
132136
if (addr != nullptr) {

src/util/sock.cpp

Lines changed: 10 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -74,6 +74,16 @@ int Sock::Connect(const sockaddr* addr, socklen_t addr_len) const
7474
return connect(m_socket, addr, addr_len);
7575
}
7676

77+
int Sock::Bind(const sockaddr* addr, socklen_t addr_len) const
78+
{
79+
return bind(m_socket, addr, addr_len);
80+
}
81+
82+
int Sock::Listen(int backlog) const
83+
{
84+
return listen(m_socket, backlog);
85+
}
86+
7787
std::unique_ptr<Sock> Sock::Accept(sockaddr* addr, socklen_t* addr_len) const
7888
{
7989
#ifdef WIN32

src/util/sock.h

Lines changed: 12 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -145,6 +145,18 @@ class Sock
145145
*/
146146
[[nodiscard]] virtual int Connect(const sockaddr* addr, socklen_t addr_len) const;
147147

148+
/**
149+
* bind(2) wrapper. Equivalent to `bind(this->Get(), addr, addr_len)`. Code that uses this
150+
* wrapper can be unit tested if this method is overridden by a mock Sock implementation.
151+
*/
152+
[[nodiscard]] virtual int Bind(const sockaddr* addr, socklen_t addr_len) const;
153+
154+
/**
155+
* listen(2) wrapper. Equivalent to `listen(this->Get(), backlog)`. Code that uses this
156+
* wrapper can be unit tested if this method is overridden by a mock Sock implementation.
157+
*/
158+
[[nodiscard]] virtual int Listen(int backlog) const;
159+
148160
/**
149161
* accept(2) wrapper. Equivalent to `std::make_unique<Sock>(accept(this->Get(), addr, addr_len))`.
150162
* Code that uses this wrapper can be unit tested if this method is overridden by a mock Sock

0 commit comments

Comments
 (0)