Coverage Report

Created: 2023-11-12 09:30

/proc/self/cwd/test/mocks/stream_info/mocks.cc
Line
Count
Source (jump to first uncovered line)
1
#include "test/mocks/stream_info/mocks.h"
2
3
#include "source/common/network/address_impl.h"
4
5
#include "test/mocks/ssl/mocks.h"
6
7
#include "gmock/gmock.h"
8
#include "gtest/gtest.h"
9
10
using testing::_;
11
using testing::Const;
12
using testing::Invoke;
13
using testing::ReturnPointee;
14
using testing::ReturnRef;
15
16
namespace Envoy {
17
namespace StreamInfo {
18
19
MockUpstreamInfo::MockUpstreamInfo()
20
    : upstream_local_address_(new Network::Address::Ipv4Instance("127.1.2.3", 58443)),
21
      upstream_remote_address_(new Network::Address::Ipv4Instance("10.0.0.1", 443)),
22
197k
      upstream_host_(new testing::NiceMock<Upstream::MockHostDescription>()) {
23
197k
  ON_CALL(*this, dumpState(_, _)).WillByDefault(Invoke([](std::ostream& os, int indent_level) {
24
0
    os << "MockUpstreamInfo test dumpState with indent: " << indent_level << std::endl;
25
0
  }));
26
197k
  ON_CALL(*this, setUpstreamConnectionId(_)).WillByDefault(Invoke([this](uint64_t id) {
27
0
    upstream_connection_id_ = id;
28
0
  }));
29
197k
  ON_CALL(*this, upstreamConnectionId()).WillByDefault(ReturnPointee(&upstream_connection_id_));
30
197k
  ON_CALL(*this, setUpstreamInterfaceName(_))
31
197k
      .WillByDefault(
32
197k
          Invoke([this](absl::string_view interface_name) { interface_name_ = interface_name; }));
33
197k
  ON_CALL(*this, upstreamInterfaceName()).WillByDefault(ReturnPointee(&interface_name_));
34
197k
  ON_CALL(*this, setUpstreamSslConnection(_))
35
197k
      .WillByDefault(Invoke([this](const Ssl::ConnectionInfoConstSharedPtr& ssl_connection_info) {
36
0
        ssl_connection_info_ = ssl_connection_info;
37
0
      }));
38
197k
  ON_CALL(*this, upstreamSslConnection()).WillByDefault(ReturnPointee(&ssl_connection_info_));
39
197k
  ON_CALL(*this, upstreamTiming()).WillByDefault(ReturnRef(upstream_timing_));
40
197k
  ON_CALL(Const(*this), upstreamTiming()).WillByDefault(ReturnRef(upstream_timing_));
41
197k
  ON_CALL(*this, setUpstreamLocalAddress(_))
42
197k
      .WillByDefault(
43
197k
          Invoke([this](const Network::Address::InstanceConstSharedPtr& upstream_local_address) {
44
0
            upstream_local_address_ = upstream_local_address;
45
0
          }));
46
197k
  ON_CALL(*this, upstreamLocalAddress()).WillByDefault(ReturnRef(upstream_local_address_));
47
197k
  ON_CALL(*this, setUpstreamTransportFailureReason(_))
48
197k
      .WillByDefault(Invoke([this](absl::string_view failure_reason) {
49
0
        failure_reason_ = std::string(failure_reason);
50
0
      }));
51
197k
  ON_CALL(*this, upstreamTransportFailureReason()).WillByDefault(ReturnRef(failure_reason_));
52
197k
  ON_CALL(*this, setUpstreamHost(_))
53
197k
      .WillByDefault(Invoke([this](Upstream::HostDescriptionConstSharedPtr upstream_host) {
54
0
        upstream_host_ = upstream_host;
55
0
      }));
56
197k
  ON_CALL(*this, upstreamHost()).WillByDefault(ReturnPointee(&upstream_host_));
57
197k
  ON_CALL(*this, setUpstreamFilterState(_))
58
197k
      .WillByDefault(Invoke(
59
197k
          [this](const FilterStateSharedPtr& filter_state) { filter_state_ = filter_state; }));
60
197k
  ON_CALL(*this, upstreamFilterState()).WillByDefault(ReturnRef(filter_state_));
61
197k
  ON_CALL(*this, setUpstreamNumStreams(_)).WillByDefault(Invoke([this](uint64_t num_streams) {
62
22.3k
    num_streams_ = num_streams;
63
22.3k
  }));
64
197k
  ON_CALL(*this, upstreamNumStreams()).WillByDefault(ReturnPointee(&num_streams_));
65
197k
  ON_CALL(*this, setUpstreamProtocol(_)).WillByDefault(Invoke([this](Http::Protocol protocol) {
66
0
    upstream_protocol_ = protocol;
67
0
  }));
68
197k
  ON_CALL(*this, upstreamProtocol()).WillByDefault(ReturnPointee(&upstream_protocol_));
69
197k
  ON_CALL(*this, upstreamRemoteAddress()).WillByDefault(ReturnRef(upstream_remote_address_));
70
197k
}
71
72
197k
MockUpstreamInfo::~MockUpstreamInfo() = default;
73
74
MockStreamInfo::MockStreamInfo()
75
    : start_time_(ts_.systemTime()),
76
      // upstream
77
      upstream_info_(std::make_shared<testing::NiceMock<MockUpstreamInfo>>()),
78
      filter_state_(std::make_shared<FilterStateImpl>(FilterState::LifeSpan::FilterChain)),
79
      downstream_connection_info_provider_(std::make_shared<Network::ConnectionInfoSetterImpl>(
80
          std::make_shared<Network::Address::Ipv4Instance>("127.0.0.2"),
81
197k
          std::make_shared<Network::Address::Ipv4Instance>("127.0.0.1"))) {
82
  // downstream:direct_remote
83
197k
  auto downstream_direct_remote_address = Network::Address::InstanceConstSharedPtr{
84
197k
      new Network::Address::Ipv4Instance("127.0.0.3", 63443)};
85
197k
  downstream_connection_info_provider_->setDirectRemoteAddressForTest(
86
197k
      downstream_direct_remote_address);
87
88
197k
  ON_CALL(*this, setResponseFlag(_)).WillByDefault(Invoke([this](ResponseFlag response_flag) {
89
1.71k
    response_flags_ |= response_flag;
90
1.71k
  }));
91
197k
  ON_CALL(*this, setResponseCode(_)).WillByDefault(Invoke([this](uint32_t code) {
92
0
    response_code_ = code;
93
0
  }));
94
197k
  ON_CALL(*this, setResponseCodeDetails(_)).WillByDefault(Invoke([this](absl::string_view details) {
95
8.19k
    response_code_details_ = std::string(details);
96
8.19k
  }));
97
197k
  ON_CALL(*this, setConnectionTerminationDetails(_))
98
197k
      .WillByDefault(Invoke([this](absl::string_view details) {
99
52
        connection_termination_details_ = std::string(details);
100
52
      }));
101
197k
  ON_CALL(*this, startTime()).WillByDefault(ReturnPointee(&start_time_));
102
197k
  ON_CALL(*this, startTimeMonotonic()).WillByDefault(ReturnPointee(&start_time_monotonic_));
103
197k
  ON_CALL(*this, currentDuration()).WillByDefault(ReturnPointee(&end_time_));
104
197k
  ON_CALL(*this, requestComplete()).WillByDefault(ReturnPointee(&end_time_));
105
197k
  ON_CALL(*this, onRequestComplete()).WillByDefault(Invoke([this]() {
106
0
    end_time_ = absl::make_optional<std::chrono::nanoseconds>(
107
0
        std::chrono::duration_cast<std::chrono::nanoseconds>(ts_.systemTime() - start_time_)
108
0
            .count());
109
0
  }));
110
197k
  ON_CALL(*this, downstreamTiming()).WillByDefault(Invoke([this]() -> DownstreamTiming& {
111
0
    return downstream_timing_;
112
0
  }));
113
197k
  ON_CALL(Const(*this), downstreamTiming())
114
197k
      .WillByDefault(
115
197k
          Invoke([this]() -> OptRef<const DownstreamTiming> { return downstream_timing_; }));
116
197k
  ON_CALL(*this, upstreamInfo()).WillByDefault(Invoke([this]() { return upstream_info_; }));
117
197k
  ON_CALL(testing::Const(*this), upstreamInfo())
118
197k
      .WillByDefault(Invoke([this]() -> OptRef<const UpstreamInfo> {
119
10.2k
        if (!upstream_info_) {
120
0
          return {};
121
0
        }
122
10.2k
        return *upstream_info_;
123
10.2k
      }));
124
197k
  ON_CALL(*this, downstreamAddressProvider())
125
197k
      .WillByDefault(ReturnPointee(downstream_connection_info_provider_));
126
197k
  ON_CALL(*this, protocol()).WillByDefault(ReturnPointee(&protocol_));
127
197k
  ON_CALL(*this, responseCode()).WillByDefault(ReturnPointee(&response_code_));
128
197k
  ON_CALL(*this, responseCodeDetails()).WillByDefault(ReturnPointee(&response_code_details_));
129
197k
  ON_CALL(*this, connectionTerminationDetails())
130
197k
      .WillByDefault(ReturnPointee(&connection_termination_details_));
131
197k
  ON_CALL(*this, addBytesReceived(_)).WillByDefault(Invoke([this](uint64_t bytes_received) {
132
0
    bytes_received_ += bytes_received;
133
0
  }));
134
197k
  ON_CALL(*this, bytesReceived()).WillByDefault(ReturnPointee(&bytes_received_));
135
197k
  ON_CALL(*this, addBytesSent(_)).WillByDefault(Invoke([this](uint64_t bytes_sent) {
136
0
    bytes_sent_ += bytes_sent;
137
0
  }));
138
197k
  ON_CALL(*this, bytesSent()).WillByDefault(ReturnPointee(&bytes_sent_));
139
197k
  ON_CALL(*this, hasResponseFlag(_)).WillByDefault(Invoke([this](ResponseFlag flag) {
140
3.06k
    return response_flags_ & flag;
141
3.06k
  }));
142
197k
  ON_CALL(*this, intersectResponseFlags(_)).WillByDefault(Invoke([this](uint64_t response_flags) {
143
0
    return (response_flags_ & response_flags) != 0;
144
0
  }));
145
197k
  ON_CALL(*this, hasAnyResponseFlag()).WillByDefault(Invoke([this]() {
146
0
    return response_flags_ != 0;
147
0
  }));
148
197k
  ON_CALL(*this, responseFlags()).WillByDefault(Invoke([this]() -> uint64_t {
149
0
    return response_flags_;
150
0
  }));
151
197k
  ON_CALL(*this, dynamicMetadata()).WillByDefault(ReturnRef(metadata_));
152
197k
  ON_CALL(Const(*this), dynamicMetadata()).WillByDefault(ReturnRef(metadata_));
153
197k
  ON_CALL(*this, filterState()).WillByDefault(ReturnRef(filter_state_));
154
197k
  ON_CALL(Const(*this), filterState()).WillByDefault(Invoke([this]() -> const FilterState& {
155
2.85k
    return *filter_state_;
156
2.85k
  }));
157
197k
  ON_CALL(*this, setVirtualClusterName(_))
158
197k
      .WillByDefault(Invoke([this](const absl::optional<std::string>& virtual_cluster_name) {
159
0
        virtual_cluster_name_ = virtual_cluster_name;
160
0
      }));
161
197k
  ON_CALL(*this, getRouteName()).WillByDefault(ReturnRef(route_name_));
162
197k
  ON_CALL(*this, setUpstreamInfo(_))
163
197k
      .WillByDefault(Invoke([this](std::shared_ptr<UpstreamInfo> info) { upstream_info_ = info; }));
164
197k
  ON_CALL(*this, virtualClusterName()).WillByDefault(ReturnRef(virtual_cluster_name_));
165
197k
  ON_CALL(*this, setAttemptCount(_)).WillByDefault(Invoke([this](uint32_t attempt_count) {
166
0
    attempt_count_ = attempt_count;
167
0
  }));
168
197k
  ON_CALL(*this, attemptCount()).WillByDefault(Invoke([this]() { return attempt_count_; }));
169
197k
  ON_CALL(*this, getUpstreamBytesMeter()).WillByDefault(ReturnPointee(&upstream_bytes_meter_));
170
197k
  ON_CALL(*this, getDownstreamBytesMeter()).WillByDefault(ReturnPointee(&downstream_bytes_meter_));
171
197k
  ON_CALL(*this, setUpstreamBytesMeter(_))
172
197k
      .WillByDefault(Invoke([this](const BytesMeterSharedPtr& upstream_bytes_meter) {
173
79.8k
        upstream_bytes_meter_ = upstream_bytes_meter;
174
79.8k
      }));
175
197k
  ON_CALL(*this, setDownstreamBytesMeter(_))
176
197k
      .WillByDefault(Invoke([this](const BytesMeterSharedPtr& downstream_bytes_meter) {
177
0
        downstream_bytes_meter_ = downstream_bytes_meter;
178
0
      }));
179
197k
  ON_CALL(*this, setDownstreamTransportFailureReason(_))
180
197k
      .WillByDefault(Invoke([this](absl::string_view failure_reason) {
181
0
        downstream_transport_failure_reason_ = std::string(failure_reason);
182
0
      }));
183
197k
  ON_CALL(*this, downstreamTransportFailureReason())
184
197k
      .WillByDefault(ReturnPointee(&downstream_transport_failure_reason_));
185
197k
  ON_CALL(*this, setUpstreamClusterInfo(_))
186
197k
      .WillByDefault(Invoke([this](const Upstream::ClusterInfoConstSharedPtr& cluster_info) {
187
0
        upstream_cluster_info_ = std::move(cluster_info);
188
0
      }));
189
197k
  ON_CALL(*this, upstreamClusterInfo()).WillByDefault(ReturnPointee(&upstream_cluster_info_));
190
197k
}
191
192
197k
MockStreamInfo::~MockStreamInfo() = default;
193
194
} // namespace StreamInfo
195
} // namespace Envoy