From 3ad7de225efce3e76530f56bee8a8f7a75ea0f3c Mon Sep 17 00:00:00 2001 From: Vasil Dimov Date: Tue, 13 Apr 2021 16:31:04 +0200 Subject: net: add new method Sock::Bind() that wraps bind() This will help to increase `Sock` usage and make more code mockable. --- src/test/fuzz/util.cpp | 20 ++++++++++++++++++++ 1 file changed, 20 insertions(+) (limited to 'src/test/fuzz/util.cpp') diff --git a/src/test/fuzz/util.cpp b/src/test/fuzz/util.cpp index 2514636d6e..c5694b0b06 100644 --- a/src/test/fuzz/util.cpp +++ b/src/test/fuzz/util.cpp @@ -160,6 +160,26 @@ int FuzzedSock::Connect(const sockaddr*, socklen_t) const return 0; } +int FuzzedSock::Bind(const sockaddr*, socklen_t) const +{ + // Have a permanent error at bind_errnos[0] because when the fuzzed data is exhausted + // SetFuzzedErrNo() will always set the global errno to bind_errnos[0]. We want to + // avoid this method returning -1 and setting errno to a temporary error (like EAGAIN) + // repeatedly because proper code should retry on temporary errors, leading to an + // infinite loop. + constexpr std::array bind_errnos{ + EACCES, + EADDRINUSE, + EADDRNOTAVAIL, + EAGAIN, + }; + if (m_fuzzed_data_provider.ConsumeBool()) { + SetFuzzedErrNo(m_fuzzed_data_provider, bind_errnos); + return -1; + } + return 0; +} + std::unique_ptr FuzzedSock::Accept(sockaddr* addr, socklen_t* addr_len) const { constexpr std::array accept_errnos{ -- cgit v1.2.3 From b2733ab6a85b234a88b83bdc77a0d043e18385b3 Mon Sep 17 00:00:00 2001 From: Vasil Dimov Date: Tue, 13 Apr 2021 16:43:04 +0200 Subject: net: add new method Sock::Listen() that wraps listen() This will help to increase `Sock` usage and make more code mockable. --- src/test/fuzz/util.cpp | 19 +++++++++++++++++++ 1 file changed, 19 insertions(+) (limited to 'src/test/fuzz/util.cpp') diff --git a/src/test/fuzz/util.cpp b/src/test/fuzz/util.cpp index c5694b0b06..7dbdf61d17 100644 --- a/src/test/fuzz/util.cpp +++ b/src/test/fuzz/util.cpp @@ -180,6 +180,25 @@ int FuzzedSock::Bind(const sockaddr*, socklen_t) const return 0; } +int FuzzedSock::Listen(int) const +{ + // Have a permanent error at listen_errnos[0] because when the fuzzed data is exhausted + // SetFuzzedErrNo() will always set the global errno to listen_errnos[0]. We want to + // avoid this method returning -1 and setting errno to a temporary error (like EAGAIN) + // repeatedly because proper code should retry on temporary errors, leading to an + // infinite loop. + constexpr std::array listen_errnos{ + EADDRINUSE, + EINVAL, + EOPNOTSUPP, + }; + if (m_fuzzed_data_provider.ConsumeBool()) { + SetFuzzedErrNo(m_fuzzed_data_provider, listen_errnos); + return -1; + } + return 0; +} + std::unique_ptr FuzzedSock::Accept(sockaddr* addr, socklen_t* addr_len) const { constexpr std::array accept_errnos{ -- cgit v1.2.3