Skip to content

Commit f8bd13f

Browse files
committed
net: add new method Sock::Accept() that wraps accept()
This will help to increase `Sock` usage and make more code mockable.
1 parent e7507f3 commit f8bd13f

File tree

5 files changed

+72
-0
lines changed

5 files changed

+72
-0
lines changed

src/test/fuzz/util.cpp

Lines changed: 16 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -10,6 +10,8 @@
1010
#include <util/time.h>
1111
#include <version.h>
1212

13+
#include <memory>
14+
1315
FuzzedSock::FuzzedSock(FuzzedDataProvider& fuzzed_data_provider)
1416
: m_fuzzed_data_provider{fuzzed_data_provider}
1517
{
@@ -155,6 +157,20 @@ int FuzzedSock::Connect(const sockaddr*, socklen_t) const
155157
return 0;
156158
}
157159

160+
std::unique_ptr<Sock> FuzzedSock::Accept(sockaddr* addr, socklen_t* addr_len) const
161+
{
162+
constexpr std::array accept_errnos{
163+
ECONNABORTED,
164+
EINTR,
165+
ENOMEM,
166+
};
167+
if (m_fuzzed_data_provider.ConsumeBool()) {
168+
SetFuzzedErrNo(m_fuzzed_data_provider, accept_errnos);
169+
return std::unique_ptr<FuzzedSock>();
170+
}
171+
return std::make_unique<FuzzedSock>(m_fuzzed_data_provider);
172+
}
173+
158174
int FuzzedSock::GetSockOpt(int level, int opt_name, void* opt_val, socklen_t* opt_len) const
159175
{
160176
constexpr std::array getsockopt_errnos{

src/test/fuzz/util.h

Lines changed: 2 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -410,6 +410,8 @@ class FuzzedSock : public Sock
410410

411411
int Connect(const sockaddr*, socklen_t) const override;
412412

413+
std::unique_ptr<Sock> Accept(sockaddr* addr, socklen_t* addr_len) const override;
414+
413415
int GetSockOpt(int level, int opt_name, void* opt_val, socklen_t* opt_len) const override;
414416

415417
bool Wait(std::chrono::milliseconds timeout, Event requested, Event* occurred = nullptr) const override;

src/test/util/net.h

Lines changed: 18 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -13,6 +13,7 @@
1313
#include <array>
1414
#include <cassert>
1515
#include <cstring>
16+
#include <memory>
1617
#include <string>
1718

1819
struct ConnmanTestMsg : public CConnman {
@@ -126,6 +127,23 @@ class StaticContentsSock : public Sock
126127

127128
int Connect(const sockaddr*, socklen_t) const override { return 0; }
128129

130+
std::unique_ptr<Sock> Accept(sockaddr* addr, socklen_t* addr_len) const override
131+
{
132+
if (addr != nullptr) {
133+
// Pretend all connections come from 5.5.5.5:6789
134+
memset(addr, 0x00, *addr_len);
135+
const socklen_t write_len = static_cast<socklen_t>(sizeof(sockaddr_in));
136+
if (*addr_len >= write_len) {
137+
*addr_len = write_len;
138+
sockaddr_in* addr_in = reinterpret_cast<sockaddr_in*>(addr);
139+
addr_in->sin_family = AF_INET;
140+
memset(&addr_in->sin_addr, 0x05, sizeof(addr_in->sin_addr));
141+
addr_in->sin_port = htons(6789);
142+
}
143+
}
144+
return std::make_unique<StaticContentsSock>("");
145+
};
146+
129147
int GetSockOpt(int level, int opt_name, void* opt_val, socklen_t* opt_len) const override
130148
{
131149
std::memset(opt_val, 0x0, *opt_len);

src/util/sock.cpp

Lines changed: 27 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -10,6 +10,7 @@
1010
#include <util/system.h>
1111
#include <util/time.h>
1212

13+
#include <memory>
1314
#include <stdexcept>
1415
#include <string>
1516

@@ -73,6 +74,32 @@ int Sock::Connect(const sockaddr* addr, socklen_t addr_len) const
7374
return connect(m_socket, addr, addr_len);
7475
}
7576

77+
std::unique_ptr<Sock> Sock::Accept(sockaddr* addr, socklen_t* addr_len) const
78+
{
79+
#ifdef WIN32
80+
static constexpr auto ERR = INVALID_SOCKET;
81+
#else
82+
static constexpr auto ERR = SOCKET_ERROR;
83+
#endif
84+
85+
std::unique_ptr<Sock> sock;
86+
87+
const auto socket = accept(m_socket, addr, addr_len);
88+
if (socket != ERR) {
89+
try {
90+
sock = std::make_unique<Sock>(socket);
91+
} catch (const std::exception&) {
92+
#ifdef WIN32
93+
closesocket(socket);
94+
#else
95+
close(socket);
96+
#endif
97+
}
98+
}
99+
100+
return sock;
101+
}
102+
76103
int Sock::GetSockOpt(int level, int opt_name, void* opt_val, socklen_t* opt_len) const
77104
{
78105
return getsockopt(m_socket, level, opt_name, static_cast<char*>(opt_val), opt_len);

src/util/sock.h

Lines changed: 9 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -10,6 +10,7 @@
1010
#include <util/time.h>
1111

1212
#include <chrono>
13+
#include <memory>
1314
#include <string>
1415

1516
/**
@@ -96,6 +97,14 @@ class Sock
9697
*/
9798
[[nodiscard]] virtual int Connect(const sockaddr* addr, socklen_t addr_len) const;
9899

100+
/**
101+
* accept(2) wrapper. Equivalent to `std::make_unique<Sock>(accept(this->Get(), addr, addr_len))`.
102+
* Code that uses this wrapper can be unit tested if this method is overridden by a mock Sock
103+
* implementation.
104+
* The returned unique_ptr is empty if `accept()` failed in which case errno will be set.
105+
*/
106+
[[nodiscard]] virtual std::unique_ptr<Sock> Accept(sockaddr* addr, socklen_t* addr_len) const;
107+
99108
/**
100109
* getsockopt(2) wrapper. Equivalent to
101110
* `getsockopt(this->Get(), level, opt_name, opt_val, opt_len)`. Code that uses this

0 commit comments

Comments
 (0)