Skip to content

Commit 184e56d

Browse files
committed
net: add new method Sock::SetSockOpt() that wraps setsockopt()
This will help to increase `Sock` usage and make more code mockable.
1 parent e14f0fa commit 184e56d

File tree

5 files changed

+32
-0
lines changed

5 files changed

+32
-0
lines changed

src/test/fuzz/util.cpp

Lines changed: 13 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -193,6 +193,19 @@ int FuzzedSock::GetSockOpt(int level, int opt_name, void* opt_val, socklen_t* op
193193
return 0;
194194
}
195195

196+
int FuzzedSock::SetSockOpt(int, int, const void*, socklen_t) const
197+
{
198+
constexpr std::array setsockopt_errnos{
199+
ENOMEM,
200+
ENOBUFS,
201+
};
202+
if (m_fuzzed_data_provider.ConsumeBool()) {
203+
SetFuzzedErrNo(m_fuzzed_data_provider, setsockopt_errnos);
204+
return -1;
205+
}
206+
return 0;
207+
}
208+
196209
bool FuzzedSock::Wait(std::chrono::milliseconds timeout, Event requested, Event* occurred) const
197210
{
198211
constexpr std::array wait_errnos{

src/test/fuzz/util.h

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

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

71+
int SetSockOpt(int level, int opt_name, const void* opt_val, socklen_t opt_len) const override;
72+
7173
bool Wait(std::chrono::milliseconds timeout, Event requested, Event* occurred = nullptr) const override;
7274

7375
bool IsConnected(std::string& errmsg) const override;

src/test/util/net.h

Lines changed: 2 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -150,6 +150,8 @@ class StaticContentsSock : public Sock
150150
return 0;
151151
}
152152

153+
int SetSockOpt(int, int, const void*, socklen_t) const override { return 0; }
154+
153155
bool Wait(std::chrono::milliseconds timeout,
154156
Event requested,
155157
Event* occurred = nullptr) const override

src/util/sock.cpp

Lines changed: 5 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -105,6 +105,11 @@ int Sock::GetSockOpt(int level, int opt_name, void* opt_val, socklen_t* opt_len)
105105
return getsockopt(m_socket, level, opt_name, static_cast<char*>(opt_val), opt_len);
106106
}
107107

108+
int Sock::SetSockOpt(int level, int opt_name, const void* opt_val, socklen_t opt_len) const
109+
{
110+
return setsockopt(m_socket, level, opt_name, static_cast<const char*>(opt_val), opt_len);
111+
}
112+
108113
bool Sock::Wait(std::chrono::milliseconds timeout, Event requested, Event* occurred) const
109114
{
110115
#ifdef USE_POLL

src/util/sock.h

Lines changed: 10 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -115,6 +115,16 @@ class Sock
115115
void* opt_val,
116116
socklen_t* opt_len) const;
117117

118+
/**
119+
* setsockopt(2) wrapper. Equivalent to
120+
* `setsockopt(this->Get(), level, opt_name, opt_val, opt_len)`. Code that uses this
121+
* wrapper can be unit tested if this method is overridden by a mock Sock implementation.
122+
*/
123+
[[nodiscard]] virtual int SetSockOpt(int level,
124+
int opt_name,
125+
const void* opt_val,
126+
socklen_t opt_len) const;
127+
118128
using Event = uint8_t;
119129

120130
/**

0 commit comments

Comments
 (0)