/proc/self/cwd/test/mocks/api/mocks.cc
Line | Count | Source (jump to first uncovered line) |
1 | | #include "mocks.h" |
2 | | |
3 | | #include <cstring> |
4 | | |
5 | | #include "source/common/common/assert.h" |
6 | | #include "source/common/common/lock_guard.h" |
7 | | |
8 | | #include "gmock/gmock.h" |
9 | | #include "gtest/gtest.h" |
10 | | |
11 | | using testing::_; |
12 | | using testing::Invoke; |
13 | | using testing::ReturnRef; |
14 | | |
15 | | namespace Envoy { |
16 | | namespace Api { |
17 | | |
18 | 30.1k | MockApi::MockApi() { |
19 | 30.1k | ON_CALL(*this, fileSystem()).WillByDefault(ReturnRef(file_system_)); |
20 | 30.1k | ON_CALL(*this, rootScope()).WillByDefault(ReturnRef(*stats_store_.rootScope())); |
21 | 30.1k | ON_CALL(*this, randomGenerator()).WillByDefault(ReturnRef(random_)); |
22 | 30.1k | ON_CALL(*this, bootstrap()).WillByDefault(ReturnRef(empty_bootstrap_)); |
23 | 30.1k | } |
24 | | |
25 | 30.1k | MockApi::~MockApi() = default; |
26 | | |
27 | 0 | Event::DispatcherPtr MockApi::allocateDispatcher(const std::string& name) { |
28 | 0 | return Event::DispatcherPtr{allocateDispatcher_(name, time_system_)}; |
29 | 0 | } |
30 | | |
31 | | Event::DispatcherPtr |
32 | | MockApi::allocateDispatcher(const std::string& name, |
33 | 0 | const Event::ScaledRangeTimerManagerFactory& scaled_timer_factory) { |
34 | 0 | return Event::DispatcherPtr{allocateDispatcher_(name, scaled_timer_factory, {}, time_system_)}; |
35 | 0 | } |
36 | | Event::DispatcherPtr MockApi::allocateDispatcher(const std::string& name, |
37 | 0 | Buffer::WatermarkFactoryPtr&& watermark_factory) { |
38 | 0 | return Event::DispatcherPtr{ |
39 | 0 | allocateDispatcher_(name, {}, std::move(watermark_factory), time_system_)}; |
40 | 0 | } |
41 | | |
42 | 0 | MockOsSysCalls::MockOsSysCalls() { |
43 | 0 | ON_CALL(*this, close(_)).WillByDefault(Invoke([](os_fd_t fd) { |
44 | | #ifdef WIN32 |
45 | | int rc = ::closesocket(fd); |
46 | | int last_error = ::GetLastError(); |
47 | | // It might be the case that the fd is not actually a socket. In that case Winsock api is |
48 | | // failing with error `WSAENOTSOCK`. In that case we fall back to a regular close. |
49 | | if (last_error == WSAENOTSOCK) { |
50 | | rc = ::close(fd); |
51 | | last_error = ::GetLastError(); |
52 | | } |
53 | | return SysCallIntResult{rc, last_error}; |
54 | | #else |
55 | 0 | const int rc = ::close(fd); |
56 | 0 | return SysCallIntResult{rc, errno}; |
57 | 0 | #endif |
58 | 0 | })); |
59 | 0 | } |
60 | | |
61 | 0 | MockOsSysCalls::~MockOsSysCalls() = default; |
62 | | |
63 | | SysCallIntResult MockOsSysCalls::setsockopt(os_fd_t sockfd, int level, int optname, |
64 | 0 | const void* optval, socklen_t optlen) { |
65 | | // Allow mocking system call failure. |
66 | 0 | if (setsockopt_(sockfd, level, optname, optval, optlen) != 0) { |
67 | 0 | return SysCallIntResult{-1, 0}; |
68 | 0 | } |
69 | | |
70 | 0 | if (optlen >= sizeof(int)) { |
71 | 0 | int val = 0; |
72 | 0 | memcpy(&val, optval, sizeof(int)); |
73 | 0 | boolsockopts_[SockOptKey(sockfd, level, optname)] = (val != 0); |
74 | 0 | } |
75 | 0 | return SysCallIntResult{0, 0}; |
76 | 0 | }; |
77 | | |
78 | | SysCallIntResult MockOsSysCalls::getsockopt(os_fd_t sockfd, int level, int optname, void* optval, |
79 | 0 | socklen_t* optlen) { |
80 | 0 | int val = 0; |
81 | 0 | const auto& it = boolsockopts_.find(SockOptKey(sockfd, level, optname)); |
82 | 0 | if (it != boolsockopts_.end()) { |
83 | 0 | val = it->second; |
84 | 0 | } |
85 | | // Allow mocking system call failure. |
86 | 0 | if (getsockopt_(sockfd, level, optname, optval, optlen) != 0) { |
87 | 0 | return {-1, 0}; |
88 | 0 | } |
89 | 0 | *reinterpret_cast<int*>(optval) = val; |
90 | 0 | return {0, 0}; |
91 | 0 | } |
92 | | |
93 | | } // namespace Api |
94 | | } // namespace Envoy |