Coverage Report

Created: 2023-11-12 09:30

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