diff options
author | Vasil Dimov <vd@FreeBSD.org> | 2021-03-05 16:29:43 +0100 |
---|---|---|
committer | Vasil Dimov <vd@FreeBSD.org> | 2021-03-16 13:53:26 +0100 |
commit | b5861100f85fef77b00f55dcdf01ffb4a2a112d8 (patch) | |
tree | 1dfc171b9c334247cd65a994bb771a2f58468b44 /src/test/fuzz/util.h | |
parent | 5a887d49b2b39e59d7cce8e9d5b89c21ad694f8b (diff) |
net: add connect() and getsockopt() wrappers to Sock
Extend the `Sock` class with wrappers to `connect()` and `getsockopt()`.
This will make it possible to mock code which uses those.
Diffstat (limited to 'src/test/fuzz/util.h')
-rw-r--r-- | src/test/fuzz/util.h | 41 |
1 files changed, 41 insertions, 0 deletions
diff --git a/src/test/fuzz/util.h b/src/test/fuzz/util.h index db399abcf1..5294adf48e 100644 --- a/src/test/fuzz/util.h +++ b/src/test/fuzz/util.h @@ -685,6 +685,47 @@ public: return random_bytes.size(); } + int Connect(const sockaddr*, socklen_t) const override + { + // Have a permanent error at connect_errnos[0] because when the fuzzed data is exhausted + // SetFuzzedErrNo() will always return the first element and we want to avoid Connect() + // returning -1 and setting errno to EAGAIN repeatedly. + constexpr std::array connect_errnos{ + ECONNREFUSED, + EAGAIN, + ECONNRESET, + EHOSTUNREACH, + EINPROGRESS, + EINTR, + ENETUNREACH, + ETIMEDOUT, + }; + if (m_fuzzed_data_provider.ConsumeBool()) { + SetFuzzedErrNo(m_fuzzed_data_provider, connect_errnos); + return -1; + } + return 0; + } + + int GetSockOpt(int level, int opt_name, void* opt_val, socklen_t* opt_len) const override + { + constexpr std::array getsockopt_errnos{ + ENOMEM, + ENOBUFS, + }; + if (m_fuzzed_data_provider.ConsumeBool()) { + SetFuzzedErrNo(m_fuzzed_data_provider, getsockopt_errnos); + return -1; + } + if (opt_val == nullptr) { + return 0; + } + std::memcpy(opt_val, + ConsumeFixedLengthByteVector(m_fuzzed_data_provider, *opt_len).data(), + *opt_len); + return 0; + } + bool Wait(std::chrono::milliseconds timeout, Event requested, Event* occurred = nullptr) const override { return m_fuzzed_data_provider.ConsumeBool(); |