Coverage Report

Created: 2024-09-19 09:45

/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
193k
      upstream_host_(new testing::NiceMock<Upstream::MockHostDescription>()) {
23
193k
  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
193k
  ON_CALL(*this, setUpstreamConnectionId(_)).WillByDefault(Invoke([this](uint64_t id) {
27
0
    upstream_connection_id_ = id;
28
0
  }));
29
193k
  ON_CALL(*this, upstreamConnectionId()).WillByDefault(ReturnPointee(&upstream_connection_id_));
30
193k
  ON_CALL(*this, setUpstreamInterfaceName(_))
31
193k
      .WillByDefault(
32
193k
          Invoke([this](absl::string_view interface_name) { interface_name_ = interface_name; }));
33
193k
  ON_CALL(*this, upstreamInterfaceName()).WillByDefault(ReturnPointee(&interface_name_));
34
193k
  ON_CALL(*this, setUpstreamSslConnection(_))
35
193k
      .WillByDefault(Invoke([this](const Ssl::ConnectionInfoConstSharedPtr& ssl_connection_info) {
36
0
        ssl_connection_info_ = ssl_connection_info;
37
0
      }));
38
193k
  ON_CALL(*this, upstreamSslConnection()).WillByDefault(ReturnPointee(&ssl_connection_info_));
39
193k
  ON_CALL(*this, upstreamTiming()).WillByDefault(ReturnRef(upstream_timing_));
40
193k
  ON_CALL(Const(*this), upstreamTiming()).WillByDefault(ReturnRef(upstream_timing_));
41
193k
  ON_CALL(*this, setUpstreamLocalAddress(_))
42
193k
      .WillByDefault(
43
193k
          Invoke([this](const Network::Address::InstanceConstSharedPtr& upstream_local_address) {
44
0
            upstream_local_address_ = upstream_local_address;
45
0
          }));
46
193k
  ON_CALL(*this, setUpstreamRemoteAddress(_))
47
193k
      .WillByDefault(
48
193k
          Invoke([this](const Network::Address::InstanceConstSharedPtr& upstream_remote_address) {
49
0
            upstream_remote_address_ = upstream_remote_address;
50
0
          }));
51
193k
  ON_CALL(*this, upstreamLocalAddress()).WillByDefault(ReturnRef(upstream_local_address_));
52
193k
  ON_CALL(*this, setUpstreamTransportFailureReason(_))
53
193k
      .WillByDefault(Invoke([this](absl::string_view failure_reason) {
54
0
        failure_reason_ = std::string(failure_reason);
55
0
      }));
56
193k
  ON_CALL(*this, upstreamTransportFailureReason()).WillByDefault(ReturnRef(failure_reason_));
57
193k
  ON_CALL(*this, setUpstreamHost(_))
58
193k
      .WillByDefault(Invoke([this](Upstream::HostDescriptionConstSharedPtr upstream_host) {
59
22.4k
        upstream_host_ = upstream_host;
60
22.4k
      }));
61
193k
  ON_CALL(*this, upstreamHost()).WillByDefault(ReturnPointee(&upstream_host_));
62
193k
  ON_CALL(*this, setUpstreamFilterState(_))
63
193k
      .WillByDefault(Invoke(
64
193k
          [this](const FilterStateSharedPtr& filter_state) { filter_state_ = filter_state; }));
65
193k
  ON_CALL(*this, upstreamFilterState()).WillByDefault(ReturnRef(filter_state_));
66
193k
  ON_CALL(*this, setUpstreamNumStreams(_)).WillByDefault(Invoke([this](uint64_t num_streams) {
67
22.3k
    num_streams_ = num_streams;
68
22.3k
  }));
69
193k
  ON_CALL(*this, upstreamNumStreams()).WillByDefault(ReturnPointee(&num_streams_));
70
193k
  ON_CALL(*this, setUpstreamProtocol(_)).WillByDefault(Invoke([this](Http::Protocol protocol) {
71
0
    upstream_protocol_ = protocol;
72
0
  }));
73
193k
  ON_CALL(*this, upstreamProtocol()).WillByDefault(ReturnPointee(&upstream_protocol_));
74
193k
  ON_CALL(*this, upstreamRemoteAddress()).WillByDefault(ReturnRef(upstream_remote_address_));
75
193k
}
76
77
193k
MockUpstreamInfo::~MockUpstreamInfo() = default;
78
79
MockStreamInfo::MockStreamInfo()
80
    : start_time_(ts_.systemTime()),
81
      // upstream
82
      upstream_info_(std::make_shared<testing::NiceMock<MockUpstreamInfo>>()),
83
      filter_state_(std::make_shared<FilterStateImpl>(FilterState::LifeSpan::FilterChain)),
84
      downstream_connection_info_provider_(std::make_shared<Network::ConnectionInfoSetterImpl>(
85
          std::make_shared<Network::Address::Ipv4Instance>("127.0.0.2"),
86
193k
          std::make_shared<Network::Address::Ipv4Instance>("127.0.0.1"))) {
87
  // downstream:direct_remote
88
193k
  auto downstream_direct_remote_address = Network::Address::InstanceConstSharedPtr{
89
193k
      new Network::Address::Ipv4Instance("127.0.0.3", 63443)};
90
193k
  downstream_connection_info_provider_->setDirectRemoteAddressForTest(
91
193k
      downstream_direct_remote_address);
92
93
193k
  ON_CALL(*this, setResponseFlag(_)).WillByDefault(Invoke([this](ResponseFlag response_flag) {
94
1.84k
    auto iter = std::find(response_flags_.begin(), response_flags_.end(), response_flag);
95
1.84k
    if (iter == response_flags_.end()) {
96
157
      response_flags_.push_back(response_flag);
97
157
    }
98
1.84k
  }));
99
193k
  ON_CALL(*this, setResponseCode(_)).WillByDefault(Invoke([this](uint32_t code) {
100
0
    response_code_ = code;
101
0
  }));
102
193k
  ON_CALL(*this, setResponseCodeDetails(_)).WillByDefault(Invoke([this](absl::string_view details) {
103
15.6k
    response_code_details_ = std::string(details);
104
15.6k
  }));
105
193k
  ON_CALL(*this, setConnectionTerminationDetails(_))
106
193k
      .WillByDefault(Invoke([this](absl::string_view details) {
107
63
        connection_termination_details_ = std::string(details);
108
63
      }));
109
193k
  ON_CALL(*this, startTime()).WillByDefault(ReturnPointee(&start_time_));
110
193k
  ON_CALL(*this, startTimeMonotonic()).WillByDefault(ReturnPointee(&start_time_monotonic_));
111
193k
  ON_CALL(*this, timeSource()).WillByDefault(ReturnPointee(&ts_));
112
193k
  ON_CALL(*this, currentDuration()).WillByDefault(ReturnPointee(&end_time_));
113
193k
  ON_CALL(*this, requestComplete()).WillByDefault(ReturnPointee(&end_time_));
114
193k
  ON_CALL(*this, onRequestComplete()).WillByDefault(Invoke([this]() {
115
0
    end_time_ = absl::make_optional<std::chrono::nanoseconds>(
116
0
        std::chrono::duration_cast<std::chrono::nanoseconds>(ts_.systemTime() - start_time_)
117
0
            .count());
118
0
  }));
119
193k
  ON_CALL(*this, downstreamTiming()).WillByDefault(Invoke([this]() -> DownstreamTiming& {
120
0
    return downstream_timing_;
121
0
  }));
122
193k
  ON_CALL(Const(*this), downstreamTiming())
123
193k
      .WillByDefault(
124
193k
          Invoke([this]() -> OptRef<const DownstreamTiming> { return downstream_timing_; }));
125
193k
  ON_CALL(*this, upstreamInfo()).WillByDefault(Invoke([this]() { return upstream_info_; }));
126
193k
  ON_CALL(testing::Const(*this), upstreamInfo())
127
193k
      .WillByDefault(Invoke([this]() -> OptRef<const UpstreamInfo> {
128
9.07k
        if (!upstream_info_) {
129
0
          return {};
130
0
        }
131
9.07k
        return *upstream_info_;
132
9.07k
      }));
133
193k
  ON_CALL(*this, downstreamAddressProvider())
134
193k
      .WillByDefault(ReturnPointee(downstream_connection_info_provider_));
135
193k
  ON_CALL(*this, protocol()).WillByDefault(ReturnPointee(&protocol_));
136
193k
  ON_CALL(*this, responseCode()).WillByDefault(ReturnPointee(&response_code_));
137
193k
  ON_CALL(*this, responseCodeDetails()).WillByDefault(ReturnPointee(&response_code_details_));
138
193k
  ON_CALL(*this, connectionTerminationDetails())
139
193k
      .WillByDefault(ReturnPointee(&connection_termination_details_));
140
193k
  ON_CALL(*this, addBytesReceived(_)).WillByDefault(Invoke([this](uint64_t bytes_received) {
141
0
    bytes_received_ += bytes_received;
142
0
  }));
143
193k
  ON_CALL(*this, bytesReceived()).WillByDefault(ReturnPointee(&bytes_received_));
144
193k
  ON_CALL(*this, addBytesSent(_)).WillByDefault(Invoke([this](uint64_t bytes_sent) {
145
0
    bytes_sent_ += bytes_sent;
146
0
  }));
147
193k
  ON_CALL(*this, bytesSent()).WillByDefault(ReturnPointee(&bytes_sent_));
148
193k
  ON_CALL(*this, hasResponseFlag(_)).WillByDefault(Invoke([this](ResponseFlag flag) {
149
0
    auto iter = std::find(response_flags_.begin(), response_flags_.end(), flag);
150
0
    return iter != response_flags_.end();
151
0
  }));
152
193k
  ON_CALL(*this, hasAnyResponseFlag()).WillByDefault(Invoke([this]() {
153
0
    return !response_flags_.empty();
154
0
  }));
155
193k
  ON_CALL(*this, responseFlags()).WillByDefault(Invoke([this]() -> absl::Span<const ResponseFlag> {
156
0
    return response_flags_;
157
0
  }));
158
193k
  ON_CALL(*this, legacyResponseFlags()).WillByDefault(Invoke([this]() -> uint64_t {
159
347
    uint64_t legacy_flags = 0;
160
347
    for (ResponseFlag flag : response_flags_) {
161
0
      if (flag.value() <= static_cast<uint16_t>(CoreResponseFlag::LastFlag)) {
162
0
        ASSERT(flag.value() < 64, "Legacy response flag out of range");
163
0
        legacy_flags |= (1UL << flag.value());
164
0
      }
165
0
    }
166
347
    return legacy_flags;
167
347
  }));
168
169
193k
  ON_CALL(*this, dynamicMetadata()).WillByDefault(ReturnRef(metadata_));
170
193k
  ON_CALL(Const(*this), dynamicMetadata()).WillByDefault(ReturnRef(metadata_));
171
193k
  ON_CALL(*this, filterState()).WillByDefault(ReturnRef(filter_state_));
172
193k
  ON_CALL(Const(*this), filterState()).WillByDefault(Invoke([this]() -> const FilterState& {
173
1.37k
    return *filter_state_;
174
1.37k
  }));
175
193k
  ON_CALL(*this, setVirtualClusterName(_))
176
193k
      .WillByDefault(Invoke([this](const absl::optional<std::string>& virtual_cluster_name) {
177
0
        virtual_cluster_name_ = virtual_cluster_name;
178
0
      }));
179
193k
  ON_CALL(*this, getRouteName()).WillByDefault(ReturnRef(route_name_));
180
193k
  ON_CALL(*this, setUpstreamInfo(_))
181
193k
      .WillByDefault(Invoke([this](std::shared_ptr<UpstreamInfo> info) { upstream_info_ = info; }));
182
193k
  ON_CALL(*this, virtualClusterName()).WillByDefault(ReturnRef(virtual_cluster_name_));
183
193k
  ON_CALL(*this, setAttemptCount(_)).WillByDefault(Invoke([this](uint32_t attempt_count) {
184
0
    attempt_count_ = attempt_count;
185
0
  }));
186
193k
  ON_CALL(*this, attemptCount()).WillByDefault(Invoke([this]() { return attempt_count_; }));
187
193k
  ON_CALL(*this, getUpstreamBytesMeter()).WillByDefault(ReturnPointee(&upstream_bytes_meter_));
188
193k
  ON_CALL(*this, getDownstreamBytesMeter()).WillByDefault(ReturnPointee(&downstream_bytes_meter_));
189
193k
  ON_CALL(*this, setUpstreamBytesMeter(_))
190
193k
      .WillByDefault(Invoke([this](const BytesMeterSharedPtr& upstream_bytes_meter) {
191
87.9k
        upstream_bytes_meter_ = upstream_bytes_meter;
192
87.9k
      }));
193
193k
  ON_CALL(*this, setDownstreamBytesMeter(_))
194
193k
      .WillByDefault(Invoke([this](const BytesMeterSharedPtr& downstream_bytes_meter) {
195
0
        downstream_bytes_meter_ = downstream_bytes_meter;
196
0
      }));
197
193k
  ON_CALL(*this, setDownstreamTransportFailureReason(_))
198
193k
      .WillByDefault(Invoke([this](absl::string_view failure_reason) {
199
0
        downstream_transport_failure_reason_ = std::string(failure_reason);
200
0
      }));
201
193k
  ON_CALL(*this, downstreamTransportFailureReason())
202
193k
      .WillByDefault(ReturnPointee(&downstream_transport_failure_reason_));
203
193k
  ON_CALL(*this, setUpstreamClusterInfo(_))
204
193k
      .WillByDefault(Invoke([this](const Upstream::ClusterInfoConstSharedPtr& cluster_info) {
205
0
        upstream_cluster_info_ = std::move(cluster_info);
206
0
      }));
207
193k
  ON_CALL(*this, upstreamClusterInfo()).WillByDefault(ReturnPointee(&upstream_cluster_info_));
208
193k
}
209
210
193k
MockStreamInfo::~MockStreamInfo() = default;
211
212
} // namespace StreamInfo
213
} // namespace Envoy