Skip to content

Commit 748dbcd

Browse files
committed
net: add new method Sock::GetSockName() that wraps getsockname()
This will help to increase `Sock` usage and make more code mockable.
1 parent 8e7eeb5 commit 748dbcd

File tree

5 files changed

+34
-0
lines changed

5 files changed

+34
-0
lines changed

src/test/fuzz/util.cpp

Lines changed: 14 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -206,6 +206,20 @@ int FuzzedSock::SetSockOpt(int, int, const void*, socklen_t) const
206206
return 0;
207207
}
208208

209+
int FuzzedSock::GetSockName(sockaddr* name, socklen_t* name_len) const
210+
{
211+
constexpr std::array getsockname_errnos{
212+
ECONNRESET,
213+
ENOBUFS,
214+
};
215+
if (m_fuzzed_data_provider.ConsumeBool()) {
216+
SetFuzzedErrNo(m_fuzzed_data_provider, getsockname_errnos);
217+
return -1;
218+
}
219+
*name_len = m_fuzzed_data_provider.ConsumeData(name, *name_len);
220+
return 0;
221+
}
222+
209223
bool FuzzedSock::Wait(std::chrono::milliseconds timeout, Event requested, Event* occurred) const
210224
{
211225
constexpr std::array wait_errnos{

src/test/fuzz/util.h

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

7070
int SetSockOpt(int level, int opt_name, const void* opt_val, socklen_t opt_len) const override;
7171

72+
int GetSockName(sockaddr* name, socklen_t* name_len) const override;
73+
7274
bool Wait(std::chrono::milliseconds timeout, Event requested, Event* occurred = nullptr) const override;
7375

7476
bool WaitMany(std::chrono::milliseconds timeout, EventsPerSock& events_per_sock) const override;

src/test/util/net.h

Lines changed: 6 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -152,6 +152,12 @@ class StaticContentsSock : public Sock
152152

153153
int SetSockOpt(int, int, const void*, socklen_t) const override { return 0; }
154154

155+
int GetSockName(sockaddr* name, socklen_t* name_len) const override
156+
{
157+
std::memset(name, 0x0, *name_len);
158+
return 0;
159+
}
160+
155161
bool Wait(std::chrono::milliseconds timeout,
156162
Event requested,
157163
Event* occurred = nullptr) const override

src/util/sock.cpp

Lines changed: 5 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -111,6 +111,11 @@ int Sock::SetSockOpt(int level, int opt_name, const void* opt_val, socklen_t opt
111111
return setsockopt(m_socket, level, opt_name, static_cast<const char*>(opt_val), opt_len);
112112
}
113113

114+
int Sock::GetSockName(sockaddr* name, socklen_t* name_len) const
115+
{
116+
return getsockname(m_socket, name, name_len);
117+
}
118+
114119
bool Sock::Wait(std::chrono::milliseconds timeout, Event requested, Event* occurred) const
115120
{
116121
// We need a `shared_ptr` owning `this` for `WaitMany()`, but don't want

src/util/sock.h

Lines changed: 7 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -126,6 +126,13 @@ class Sock
126126
const void* opt_val,
127127
socklen_t opt_len) const;
128128

129+
/**
130+
* getsockname(2) wrapper. Equivalent to
131+
* `getsockname(this->Get(), name, name_len)`. Code that uses this
132+
* wrapper can be unit tested if this method is overridden by a mock Sock implementation.
133+
*/
134+
[[nodiscard]] virtual int GetSockName(sockaddr* name, socklen_t* name_len) const;
135+
129136
using Event = uint8_t;
130137

131138
/**

0 commit comments

Comments
 (0)