From d38f9635cff32332e92e774d5cc86199ff9a8118 Mon Sep 17 00:00:00 2001 From: Matt Klein Date: Fri, 18 Dec 2020 23:26:37 +0000 Subject: [PATCH] more Signed-off-by: Matt Klein --- include/envoy/network/connection.h | 3 +- include/envoy/network/socket.h | 4 +- source/common/http/async_client_impl.cc | 1 - source/common/http/conn_manager_impl.cc | 16 ++-- source/common/http/conn_manager_utility.cc | 8 +- source/common/http/filter_manager.h | 3 +- source/common/network/connection_impl.h | 3 + source/common/network/filter_matcher.h | 2 +- source/common/network/socket_impl.cc | 6 +- source/common/network/socket_impl.h | 2 +- source/common/stream_info/stream_info_impl.h | 13 ++- source/common/tcp_proxy/tcp_proxy.cc | 17 ++-- source/common/tcp_proxy/tcp_proxy.h | 5 +- source/common/tcp_proxy/upstream.cc | 2 +- .../quic_listeners/quiche/envoy_quic_utils.cc | 2 +- .../upstreams/http/tcp/upstream_request.cc | 2 +- source/server/api_listener_impl.h | 10 -- source/server/filter_chain_manager_impl.cc | 6 +- source/server/listener_impl.cc | 2 +- test/common/http/codec_client_test.cc | 5 +- .../http/conn_manager_impl_fuzz_test.cc | 8 +- test/common/http/conn_manager_impl_test.cc | 32 ++++--- .../http/conn_manager_impl_test_base.cc | 8 +- test/common/http/conn_manager_utility_test.cc | 83 +++++++++++------ test/common/network/address_impl_test.cc | 10 +- test/common/network/connection_impl_test.cc | 29 +++--- test/common/network/dns_impl_test.cc | 14 ++- test/common/network/filter_matcher_test.cc | 5 +- .../common/network/listen_socket_impl_test.cc | 20 ++-- test/common/network/listener_impl_test.cc | 70 +++++++------- .../network/socket_option_factory_test.cc | 8 +- test/common/network/socket_option_test.h | 2 +- .../udp_listener_impl_batch_writer_test.cc | 11 ++- test/common/network/udp_listener_impl_test.cc | 7 +- .../network/udp_listener_impl_test_base.h | 14 +-- test/common/network/utility_test.cc | 72 ++++++++------ test/common/stream_info/test_util.h | 26 +----- test/common/tcp_proxy/tcp_proxy_test.cc | 93 ++++++++++++------- test/fuzz/utility.h | 6 +- test/mocks/network/connection.cc | 4 +- test/mocks/network/connection.h | 44 +-------- test/mocks/network/mocks.h | 16 ++-- test/mocks/network/socket.h | 9 +- test/mocks/stream_info/mocks.h | 1 - 44 files changed, 376 insertions(+), 328 deletions(-) diff --git a/include/envoy/network/connection.h b/include/envoy/network/connection.h index d082c679ec5c..db6820c439a8 100644 --- a/include/envoy/network/connection.h +++ b/include/envoy/network/connection.h @@ -11,6 +11,7 @@ #include "envoy/network/address.h" #include "envoy/network/filter.h" #include "envoy/network/listen_socket.h" +#include "envoy/network/socket.h" #include "envoy/ssl/connection.h" #include "envoy/stream_info/stream_info.h" @@ -189,6 +190,7 @@ class Connection : public Event::DeferredDeletable, public FilterManager { // fixfix virtual const SocketAddressProvider& addressProvider() const PURE; + virtual SocketAddressProviderConstSharedPtr addressProviderSharedPtr() const PURE; /** * Credentials of the peer of a socket as decided by SO_PEERCRED. @@ -270,7 +272,6 @@ class Connection : public Event::DeferredDeletable, public FilterManager { */ virtual uint32_t bufferLimit() const PURE; - /** * @return boolean telling if the connection is currently above the high watermark. */ diff --git a/include/envoy/network/socket.h b/include/envoy/network/socket.h index e0beff5445df..24ab937fd228 100644 --- a/include/envoy/network/socket.h +++ b/include/envoy/network/socket.h @@ -63,7 +63,7 @@ class SocketAddressProvider { */ virtual void setLocalAddress(const Address::InstanceConstSharedPtr& local_address) PURE; - /** + /** * Restores the local address of the socket. On accepted sockets the local address defaults to the * one at which the connection was received at, which is the same as the listener's address, if * the listener is bound to a specific address. Call this to restore the address to a value @@ -139,7 +139,7 @@ class Socket { */ virtual Socket::Type socketType() const PURE; -/** + /** * @return the type (IP or pipe) of addresses used by the socket (subset of socket domain) */ virtual Address::Type addressType() const PURE; diff --git a/source/common/http/async_client_impl.cc b/source/common/http/async_client_impl.cc index f2a35c3246d7..361fdfb173ac 100644 --- a/source/common/http/async_client_impl.cc +++ b/source/common/http/async_client_impl.cc @@ -6,7 +6,6 @@ #include #include -#include "common/network/socket_impl.h" #include "envoy/config/core/v3/base.pb.h" #include "common/grpc/common.h" diff --git a/source/common/http/conn_manager_impl.cc b/source/common/http/conn_manager_impl.cc index 0c7d1af2d330..631a842d633c 100644 --- a/source/common/http/conn_manager_impl.cc +++ b/source/common/http/conn_manager_impl.cc @@ -618,12 +618,6 @@ ConnectionManagerImpl::ActiveStream::ActiveStream(ConnectionManagerImpl& connect } else { connection_manager_.stats_.named_.downstream_rq_http1_total_.inc(); } - // Initially, the downstream remote address is the source address of the - // downstream connection. That can change later in the request's lifecycle, - // based on XFF processing, but setting the downstream remote address here - // prevents surprises for logging code in edge cases. - filter_manager_.streamInfo().setDownstreamAddresses( - connection_manager_.read_callbacks_->connection()); filter_manager_.streamInfo().setDownstreamSslConnection( connection_manager_.read_callbacks_->connection().ssl()); @@ -822,7 +816,7 @@ const Network::Connection* ConnectionManagerImpl::ActiveStream::connection() { } uint32_t ConnectionManagerImpl::ActiveStream::localPort() { - auto ip = connection()->localAddress()->ip(); + auto ip = connection()->addressProvider().localAddress()->ip(); if (ip == nullptr) { return 0; } @@ -1010,12 +1004,14 @@ void ConnectionManagerImpl::ActiveStream::decodeHeaders(RequestHeaderMapPtr&& he if (!state_.is_internally_created_) { // Only sanitize headers on first pass. // Modify the downstream remote address depending on configuration and headers. - filter_manager_.streamInfo().setDownstreamRemoteAddress( + ASSERT(false); // fixfix + /*filter_manager_.streamInfo().setDownstreamRemoteAddress( ConnectionManagerUtility::mutateRequestHeaders( *request_headers_, connection_manager_.read_callbacks_->connection(), - connection_manager_.config_, *snapped_route_config_, connection_manager_.local_info_)); + connection_manager_.config_, *snapped_route_config_, + connection_manager_.local_info_));*/ } - ASSERT(filter_manager_.streamInfo().downstreamRemoteAddress() != nullptr); + ASSERT(filter_manager_.streamInfo().downstreamAddressProvider().remoteAddress() != nullptr); ASSERT(!cached_route_); refreshCachedRoute(); diff --git a/source/common/http/conn_manager_utility.cc b/source/common/http/conn_manager_utility.cc index 6042af59adcc..8ed108a4fc6e 100644 --- a/source/common/http/conn_manager_utility.cc +++ b/source/common/http/conn_manager_utility.cc @@ -110,13 +110,13 @@ Network::Address::InstanceConstSharedPtr ConnectionManagerUtility::mutateRequest // are but they didn't populate XFF properly, the trusted client address is the // source address of the immediate downstream's connection to us. if (final_remote_address == nullptr) { - final_remote_address = connection.remoteAddress(); + final_remote_address = connection.addressProvider().remoteAddress(); } if (!config.skipXffAppend()) { - if (Network::Utility::isLoopbackAddress(*connection.remoteAddress())) { + if (Network::Utility::isLoopbackAddress(*connection.addressProvider().remoteAddress())) { Utility::appendXff(request_headers, config.localAddress()); } else { - Utility::appendXff(request_headers, *connection.remoteAddress()); + Utility::appendXff(request_headers, *connection.addressProvider().remoteAddress()); } } // If the prior hop is not a trusted proxy, overwrite any x-forwarded-proto value it set as @@ -157,7 +157,7 @@ Network::Address::InstanceConstSharedPtr ConnectionManagerUtility::mutateRequest // After determining internal request status, if there is no final remote address, due to no XFF, // busted XFF, etc., use the direct connection remote address for logging. if (final_remote_address == nullptr) { - final_remote_address = connection.remoteAddress(); + final_remote_address = connection.addressProvider().remoteAddress(); } // Edge request is the request from external clients to front Envoy. diff --git a/source/common/http/filter_manager.h b/source/common/http/filter_manager.h index 21109a82d774..8800de157607 100644 --- a/source/common/http/filter_manager.h +++ b/source/common/http/filter_manager.h @@ -619,7 +619,8 @@ class FilterManager : public ScopeTrackedObject, connection_(connection), stream_id_(stream_id), proxy_100_continue_(proxy_100_continue), buffer_limit_(buffer_limit), filter_chain_factory_(filter_chain_factory), local_reply_(local_reply), - stream_info_(protocol, time_source, parent_filter_state, filter_state_life_span) {} + stream_info_(protocol, time_source, connection.addressProviderSharedPtr(), + parent_filter_state, filter_state_life_span) {} ~FilterManager() override { ASSERT(state_.destroyed_); ASSERT(state_.filter_call_state_ == 0); diff --git a/source/common/network/connection_impl.h b/source/common/network/connection_impl.h index 2a76afb9644f..15e3b6e032d4 100644 --- a/source/common/network/connection_impl.h +++ b/source/common/network/connection_impl.h @@ -71,6 +71,9 @@ class ConnectionImpl : public ConnectionImplBase, public TransportSocketCallback const SocketAddressProvider& addressProvider() const override { return socket_->addressProvider(); } + SocketAddressProviderConstSharedPtr addressProviderSharedPtr() const override { + return socket_->addressProviderSharedPtr(); + } absl::optional unixSocketPeerCredentials() const override; Ssl::ConnectionInfoConstSharedPtr ssl() const override { return transport_socket_->ssl(); } State state() const override; diff --git a/source/common/network/filter_matcher.h b/source/common/network/filter_matcher.h index ab2dd5f8068a..be2114bdabe4 100644 --- a/source/common/network/filter_matcher.h +++ b/source/common/network/filter_matcher.h @@ -45,7 +45,7 @@ class ListenerFilterDstPortMatcher final : public ListenerFilterMatcher { explicit ListenerFilterDstPortMatcher(const ::envoy::type::v3::Int32Range& range) : start_(range.start()), end_(range.end()) {} bool matches(ListenerFilterCallbacks& cb) const override { - const auto& address = cb.socket().localAddress(); + const auto& address = cb.socket().addressProvider().localAddress(); // Match on destination port (only for IP addresses). if (address->type() == Address::Type::Ip) { const auto port = address->ip()->port(); diff --git a/source/common/network/socket_impl.cc b/source/common/network/socket_impl.cc index 46ecd4e04cf4..8c44690f2789 100644 --- a/source/common/network/socket_impl.cc +++ b/source/common/network/socket_impl.cc @@ -12,9 +12,9 @@ namespace Network { SocketImpl::SocketImpl(Socket::Type sock_type, const Address::InstanceConstSharedPtr& address_for_io_handle, const Address::InstanceConstSharedPtr& remote_address) - : io_handle_(ioHandleForAddr(sock_type, address_for_io_handle)), sock_type_(sock_type), - addr_type_(address_for_io_handle->type()), - address_provider_(std::make_shared(nullptr, remote_address)) {} + : io_handle_(ioHandleForAddr(sock_type, address_for_io_handle)), + address_provider_(std::make_shared(nullptr, remote_address)), + sock_type_(sock_type), addr_type_(address_for_io_handle->type()) {} SocketImpl::SocketImpl(IoHandlePtr&& io_handle, const Address::InstanceConstSharedPtr& local_address, diff --git a/source/common/network/socket_impl.h b/source/common/network/socket_impl.h index da996ae66da0..8bc4a2a799d5 100644 --- a/source/common/network/socket_impl.h +++ b/source/common/network/socket_impl.h @@ -97,10 +97,10 @@ class SocketImpl : public virtual Socket { const Address::InstanceConstSharedPtr& remote_address); const IoHandlePtr io_handle_; + const SocketAddressProviderSharedPtr address_provider_; OptionsSharedPtr options_; Socket::Type sock_type_; Address::Type addr_type_; - const SocketAddressProviderSharedPtr address_provider_; }; } // namespace Network diff --git a/source/common/stream_info/stream_info_impl.h b/source/common/stream_info/stream_info_impl.h index c0170aeeb015..4fbf37fe54b1 100644 --- a/source/common/stream_info/stream_info_impl.h +++ b/source/common/stream_info/stream_info_impl.h @@ -3,6 +3,8 @@ #include #include +#include "common/common/macros.h" +#include "common/network/socket_impl.h" #include "envoy/common/time.h" #include "envoy/config/core/v3/base.pb.h" #include "envoy/http/header_map.h" @@ -301,14 +303,21 @@ struct StreamInfoImpl : public StreamInfo { std::string route_name_; private: - // fixfix null check + static Network::SocketAddressProviderConstSharedPtr emptyDownstreamAddressProvider() { + MUTABLE_CONSTRUCT_ON_FIRST_USE( + Network::SocketAddressProviderConstSharedPtr, + std::make_shared(nullptr, nullptr)); + } + StreamInfoImpl(absl::optional protocol, TimeSource& time_source, const Network::SocketAddressProviderConstSharedPtr& downstream_address_provider, FilterStateSharedPtr filter_state) : time_source_(time_source), start_time_(time_source.systemTime()), start_time_monotonic_(time_source.monotonicTime()), protocol_(protocol), filter_state_(std::move(filter_state)), - downstream_address_provider_(downstream_address_provider), + downstream_address_provider_(downstream_address_provider != nullptr + ? downstream_address_provider + : emptyDownstreamAddressProvider()), request_id_extension_(Http::RequestIDExtensionFactory::noopInstance()) {} uint64_t bytes_received_{}; diff --git a/source/common/tcp_proxy/tcp_proxy.cc b/source/common/tcp_proxy/tcp_proxy.cc index fe64127a0aa1..31efb90c282b 100644 --- a/source/common/tcp_proxy/tcp_proxy.cc +++ b/source/common/tcp_proxy/tcp_proxy.cc @@ -56,20 +56,24 @@ Config::RouteImpl::RouteImpl( bool Config::RouteImpl::matches(Network::Connection& connection) const { if (!source_port_ranges_.empty() && - !Network::Utility::portInRangeList(*connection.remoteAddress(), source_port_ranges_)) { + !Network::Utility::portInRangeList(*connection.addressProvider().remoteAddress(), + source_port_ranges_)) { return false; } - if (!source_ips_.empty() && !source_ips_.contains(*connection.remoteAddress())) { + if (!source_ips_.empty() && + !source_ips_.contains(*connection.addressProvider().remoteAddress())) { return false; } if (!destination_port_ranges_.empty() && - !Network::Utility::portInRangeList(*connection.localAddress(), destination_port_ranges_)) { + !Network::Utility::portInRangeList(*connection.addressProvider().localAddress(), + destination_port_ranges_)) { return false; } - if (!destination_ips_.empty() && !destination_ips_.contains(*connection.localAddress())) { + if (!destination_ips_.empty() && + !destination_ips_.contains(*connection.addressProvider().localAddress())) { return false; } @@ -425,8 +429,9 @@ Network::FilterStatus Filter::initializeUpstreamConnection() { Network::ProxyProtocolFilterState::key())) { read_callbacks_->connection().streamInfo().filterState()->setData( Network::ProxyProtocolFilterState::key(), - std::make_unique(Network::ProxyProtocolData{ - downstreamConnection()->remoteAddress(), downstreamConnection()->localAddress()}), + std::make_unique( + Network::ProxyProtocolData{downstreamConnection()->addressProvider().remoteAddress(), + downstreamConnection()->addressProvider().localAddress()}), StreamInfo::FilterState::StateType::ReadOnly, StreamInfo::FilterState::LifeSpan::Connection); } diff --git a/source/common/tcp_proxy/tcp_proxy.h b/source/common/tcp_proxy/tcp_proxy.h index a5e43e6e8ca5..3ffa00e71a61 100644 --- a/source/common/tcp_proxy/tcp_proxy.h +++ b/source/common/tcp_proxy/tcp_proxy.h @@ -266,8 +266,9 @@ class Filter : public Network::ReadFilter, absl::optional computeHashKey() override { auto hash_policy = config_->hashPolicy(); if (hash_policy) { - return hash_policy->generateHash(downstreamConnection()->remoteAddress().get(), - downstreamConnection()->localAddress().get()); + return hash_policy->generateHash( + downstreamConnection()->addressProvider().remoteAddress().get(), + downstreamConnection()->addressProvider().localAddress().get()); } return {}; diff --git a/source/common/tcp_proxy/upstream.cc b/source/common/tcp_proxy/upstream.cc index 5b286f64dbc7..6b0f9446ad15 100644 --- a/source/common/tcp_proxy/upstream.cc +++ b/source/common/tcp_proxy/upstream.cc @@ -175,7 +175,7 @@ void TcpConnPool::onPoolReady(Tcp::ConnectionPool::ConnectionDataPtr&& conn_data auto upstream = std::make_unique(std::move(conn_data), upstream_callbacks_); callbacks_->onGenericPoolReady(&connection.streamInfo(), std::move(upstream), host, - latched_data->connection().localAddress(), + latched_data->connection().addressProvider().localAddress(), latched_data->connection().streamInfo().downstreamSslConnection()); } diff --git a/source/extensions/quic_listeners/quiche/envoy_quic_utils.cc b/source/extensions/quic_listeners/quiche/envoy_quic_utils.cc index bd0a8a657ccd..611a7a28d6f3 100644 --- a/source/extensions/quic_listeners/quiche/envoy_quic_utils.cc +++ b/source/extensions/quic_listeners/quiche/envoy_quic_utils.cc @@ -132,7 +132,7 @@ createConnectionSocket(Network::Address::InstanceConstSharedPtr& peer_addr, } connection_socket->bind(local_addr); ASSERT(local_addr->ip()); - local_addr = connection_socket->localAddress(); + local_addr = connection_socket->addressProvider().localAddress(); if (!Network::Socket::applyOptions(connection_socket->options(), *connection_socket, envoy::config::core::v3::SocketOption::STATE_BOUND)) { ENVOY_LOG_MISC(error, "Fail to apply post-bind options"); diff --git a/source/extensions/upstreams/http/tcp/upstream_request.cc b/source/extensions/upstreams/http/tcp/upstream_request.cc index 8d0d4b25a4b5..0ecc732e507a 100644 --- a/source/extensions/upstreams/http/tcp/upstream_request.cc +++ b/source/extensions/upstreams/http/tcp/upstream_request.cc @@ -29,7 +29,7 @@ void TcpConnPool::onPoolReady(Envoy::Tcp::ConnectionPool::ConnectionDataPtr&& co Network::Connection& latched_conn = conn_data->connection(); auto upstream = std::make_unique(&callbacks_->upstreamToDownstream(), std::move(conn_data)); - callbacks_->onPoolReady(std::move(upstream), host, latched_conn.localAddress(), + callbacks_->onPoolReady(std::move(upstream), host, latched_conn.addressProvider().localAddress(), latched_conn.streamInfo(), {}); } diff --git a/source/server/api_listener_impl.h b/source/server/api_listener_impl.h index bbda83bb6559..3452864a79d5 100644 --- a/source/server/api_listener_impl.h +++ b/source/server/api_listener_impl.h @@ -111,19 +111,10 @@ class ApiListenerImplBase : public ApiListener, void readDisable(bool) override {} void detectEarlyCloseWhenReadDisabled(bool) override { NOT_IMPLEMENTED_GCOVR_EXCL_LINE; } bool readEnabled() const override { return true; } - const Network::Address::InstanceConstSharedPtr& remoteAddress() const override { - return parent_.parent_.address(); - } - const Network::Address::InstanceConstSharedPtr& directRemoteAddress() const override { - return parent_.parent_.address(); - } absl::optional unixSocketPeerCredentials() const override { return absl::nullopt; } - const Network::Address::InstanceConstSharedPtr& localAddress() const override { - return parent_.parent_.address(); - } void setConnectionStats(const Network::Connection::ConnectionStats&) override {} Ssl::ConnectionInfoConstSharedPtr ssl() const override { return nullptr; } absl::string_view requestedServerName() const override { return EMPTY_STRING; } @@ -132,7 +123,6 @@ class ApiListenerImplBase : public ApiListener, void write(Buffer::Instance&, bool) override { NOT_IMPLEMENTED_GCOVR_EXCL_LINE; } void setBufferLimits(uint32_t) override { NOT_IMPLEMENTED_GCOVR_EXCL_LINE; } uint32_t bufferLimit() const override { return 65000; } - bool localAddressRestored() const override { return false; } bool aboveHighWatermark() const override { return false; } const Network::ConnectionSocket::OptionsSharedPtr& socketOptions() const override { return options_; diff --git a/source/server/filter_chain_manager_impl.cc b/source/server/filter_chain_manager_impl.cc index 3caf91fc01c6..6ec4a9f0832f 100644 --- a/source/server/filter_chain_manager_impl.cc +++ b/source/server/filter_chain_manager_impl.cc @@ -419,7 +419,7 @@ std::pair> makeCidrListEntry(const s const Network::FilterChain* FilterChainManagerImpl::findFilterChain(const Network::ConnectionSocket& socket) const { - const auto& address = socket.localAddress(); + const auto& address = socket.addressProvider().localAddress(); const Network::FilterChain* best_match_filter_chain = nullptr; // Match on destination port (only for IP addresses). @@ -449,7 +449,7 @@ FilterChainManagerImpl::findFilterChain(const Network::ConnectionSocket& socket) const Network::FilterChain* FilterChainManagerImpl::findFilterChainForDestinationIP( const DestinationIPsTrie& destination_ips_trie, const Network::ConnectionSocket& socket) const { - auto address = socket.localAddress(); + auto address = socket.addressProvider().localAddress(); if (address->type() != Network::Address::Type::Ip) { address = fakeAddress(); } @@ -570,7 +570,7 @@ const Network::FilterChain* FilterChainManagerImpl::findFilterChainForSourceType const Network::FilterChain* FilterChainManagerImpl::findFilterChainForSourceIpAndPort( const SourceIPsTrie& source_ips_trie, const Network::ConnectionSocket& socket) const { - auto address = socket.remoteAddress(); + auto address = socket.addressProvider().remoteAddress(); if (address->type() != Network::Address::Type::Ip) { address = fakeAddress(); } diff --git a/source/server/listener_impl.cc b/source/server/listener_impl.cc index 879386ce1c04..2745a53f8913 100644 --- a/source/server/listener_impl.cc +++ b/source/server/listener_impl.cc @@ -109,7 +109,7 @@ ListenSocketFactoryImpl::ListenSocketFactoryImpl(ListenerComponentFactory& facto } if (socket_ && local_address_->ip() && local_address_->ip()->port() == 0) { - local_address_ = socket_->localAddress(); + local_address_ = socket_->addressProvider().localAddress(); } ENVOY_LOG(debug, "Set listener {} socket factory local address to {}", listener_name_, local_address_->asString()); diff --git a/test/common/http/codec_client_test.cc b/test/common/http/codec_client_test.cc index ae92e74fe6f0..9c6cd18a7168 100644 --- a/test/common/http/codec_client_test.cc +++ b/test/common/http/codec_client_test.cc @@ -296,12 +296,13 @@ TEST_F(CodecClientTest, SSLConnectionInfo) { class CodecNetworkTest : public Event::TestUsingSimulatedTime, public testing::TestWithParam { public: - CodecNetworkTest() : api_(Api::createApiForTest()), stream_info_(api_->timeSource()) { + CodecNetworkTest() : api_(Api::createApiForTest()), stream_info_(api_->timeSource(), nullptr) { dispatcher_ = api_->allocateDispatcher("test_thread"); auto socket = std::make_shared( Network::Test::getCanonicalLoopbackAddress(GetParam()), nullptr, true); Network::ClientConnectionPtr client_connection = dispatcher_->createClientConnection( - socket->localAddress(), source_address_, Network::Test::createRawBufferSocket(), nullptr); + socket->addressProvider().localAddress(), source_address_, + Network::Test::createRawBufferSocket(), nullptr); upstream_listener_ = dispatcher_->createListener(std::move(socket), listener_callbacks_, true, ENVOY_TCP_BACKLOG_SIZE); client_connection_ = client_connection.get(); diff --git a/test/common/http/conn_manager_impl_fuzz_test.cc b/test/common/http/conn_manager_impl_fuzz_test.cc index fdf1bbe7ef2a..1f188c544faa 100644 --- a/test/common/http/conn_manager_impl_fuzz_test.cc +++ b/test/common/http/conn_manager_impl_fuzz_test.cc @@ -570,10 +570,10 @@ DEFINE_PROTO_FUZZER(const test::common::http::ConnManagerImplTestCase& input) { ON_CALL(Const(filter_callbacks.connection_), ssl()).WillByDefault(Return(ssl_connection)); ON_CALL(filter_callbacks.connection_, close(_)) .WillByDefault(InvokeWithoutArgs([&connection_alive] { connection_alive = false; })); - filter_callbacks.connection_.local_address_ = - std::make_shared("127.0.0.1"); - filter_callbacks.connection_.remote_address_ = - std::make_shared("0.0.0.0"); + filter_callbacks.connection_.stream_info_.downstream_address_provider_->setLocalAddress( + std::make_shared("127.0.0.1")); + filter_callbacks.connection_.stream_info_.downstream_address_provider_->setRemoteAddress( + std::make_shared("0.0.0.0")); ConnectionManagerImpl conn_manager(config, drain_close, random, http_context, runtime, local_info, cluster_manager, overload_manager, config.time_system_); diff --git a/test/common/http/conn_manager_impl_test.cc b/test/common/http/conn_manager_impl_test.cc index 811a4140be6c..c2e3486dd503 100644 --- a/test/common/http/conn_manager_impl_test.cc +++ b/test/common/http/conn_manager_impl_test.cc @@ -1561,14 +1561,16 @@ TEST_F(HttpConnectionManagerImplTest, TestAccessLog) { const StreamInfo::StreamInfo& stream_info) { EXPECT_TRUE(stream_info.responseCode()); EXPECT_EQ(stream_info.responseCode().value(), uint32_t(200)); - EXPECT_NE(nullptr, stream_info.downstreamLocalAddress()); - EXPECT_NE(nullptr, stream_info.downstreamRemoteAddress()); - EXPECT_NE(nullptr, stream_info.downstreamDirectRemoteAddress()); + EXPECT_NE(nullptr, stream_info.downstreamAddressProvider().localAddress()); + EXPECT_NE(nullptr, stream_info.downstreamAddressProvider().remoteAddress()); + EXPECT_NE(nullptr, stream_info.downstreamAddressProvider().directRemoteAddress()); EXPECT_NE(nullptr, stream_info.routeEntry()); - EXPECT_EQ(stream_info.downstreamRemoteAddress()->ip()->addressAsString(), xff_address); - EXPECT_EQ(stream_info.downstreamDirectRemoteAddress()->ip()->addressAsString(), - local_address); + EXPECT_EQ(stream_info.downstreamAddressProvider().remoteAddress()->ip()->addressAsString(), + xff_address); + EXPECT_EQ( + stream_info.downstreamAddressProvider().directRemoteAddress()->ip()->addressAsString(), + local_address); })); EXPECT_CALL(*codec_, dispatch(_)) @@ -1700,9 +1702,9 @@ TEST_F(HttpConnectionManagerImplTest, TestAccessLogWithTrailers) { const StreamInfo::StreamInfo& stream_info) { EXPECT_TRUE(stream_info.responseCode()); EXPECT_EQ(stream_info.responseCode().value(), uint32_t(200)); - EXPECT_NE(nullptr, stream_info.downstreamLocalAddress()); - EXPECT_NE(nullptr, stream_info.downstreamRemoteAddress()); - EXPECT_NE(nullptr, stream_info.downstreamDirectRemoteAddress()); + EXPECT_NE(nullptr, stream_info.downstreamAddressProvider().localAddress()); + EXPECT_NE(nullptr, stream_info.downstreamAddressProvider().remoteAddress()); + EXPECT_NE(nullptr, stream_info.downstreamAddressProvider().directRemoteAddress()); EXPECT_NE(nullptr, stream_info.routeEntry()); })); @@ -1750,9 +1752,9 @@ TEST_F(HttpConnectionManagerImplTest, TestAccessLogWithInvalidRequest) { EXPECT_TRUE(stream_info.responseCode()); EXPECT_EQ(stream_info.responseCode().value(), uint32_t(400)); EXPECT_EQ("missing_host_header", stream_info.responseCodeDetails().value()); - EXPECT_NE(nullptr, stream_info.downstreamLocalAddress()); - EXPECT_NE(nullptr, stream_info.downstreamRemoteAddress()); - EXPECT_NE(nullptr, stream_info.downstreamDirectRemoteAddress()); + EXPECT_NE(nullptr, stream_info.downstreamAddressProvider().localAddress()); + EXPECT_NE(nullptr, stream_info.downstreamAddressProvider().remoteAddress()); + EXPECT_NE(nullptr, stream_info.downstreamAddressProvider().directRemoteAddress()); EXPECT_EQ(nullptr, stream_info.routeEntry()); })); @@ -1847,9 +1849,9 @@ TEST_F(HttpConnectionManagerImplTest, TestAccessLogSsl) { const StreamInfo::StreamInfo& stream_info) { EXPECT_TRUE(stream_info.responseCode()); EXPECT_EQ(stream_info.responseCode().value(), uint32_t(200)); - EXPECT_NE(nullptr, stream_info.downstreamLocalAddress()); - EXPECT_NE(nullptr, stream_info.downstreamRemoteAddress()); - EXPECT_NE(nullptr, stream_info.downstreamDirectRemoteAddress()); + EXPECT_NE(nullptr, stream_info.downstreamAddressProvider().localAddress()); + EXPECT_NE(nullptr, stream_info.downstreamAddressProvider().remoteAddress()); + EXPECT_NE(nullptr, stream_info.downstreamAddressProvider().directRemoteAddress()); EXPECT_NE(nullptr, stream_info.downstreamSslConnection()); EXPECT_NE(nullptr, stream_info.routeEntry()); })); diff --git a/test/common/http/conn_manager_impl_test_base.cc b/test/common/http/conn_manager_impl_test_base.cc index f32f39b70258..91e674ef82c8 100644 --- a/test/common/http/conn_manager_impl_test_base.cc +++ b/test/common/http/conn_manager_impl_test_base.cc @@ -57,10 +57,10 @@ void HttpConnectionManagerImplTest::setup(bool ssl, const std::string& server_na .WillByDefault([&](auto, auto callback) { return filter_callbacks_.connection_.dispatcher_.createTimer(callback).release(); }); - filter_callbacks_.connection_.local_address_ = - std::make_shared("127.0.0.1", 443); - filter_callbacks_.connection_.remote_address_ = - std::make_shared("0.0.0.0"); + filter_callbacks_.connection_.stream_info_.downstream_address_provider_->setLocalAddress( + std::make_shared("127.0.0.1", 443)); + filter_callbacks_.connection_.stream_info_.downstream_address_provider_->setRemoteAddress( + std::make_shared("0.0.0.0")); conn_manager_ = std::make_unique( *this, drain_close_, random_, http_context_, runtime_, local_info_, cluster_manager_, overload_manager_, test_time_.timeSystem()); diff --git a/test/common/http/conn_manager_utility_test.cc b/test/common/http/conn_manager_utility_test.cc index 24ae4608e4cb..cc3dabe3ebf4 100644 --- a/test/common/http/conn_manager_utility_test.cc +++ b/test/common/http/conn_manager_utility_test.cc @@ -197,13 +197,16 @@ TEST_F(ConnectionManagerUtilityTest, DetermineNextProtocol) { // Verify external request and XFF is set when we are using remote address and the address is // external. TEST_F(ConnectionManagerUtilityTest, UseRemoteAddressWhenNotLocalHostRemoteAddress) { - connection_.remote_address_ = std::make_shared("12.12.12.12"); + connection_.stream_info_.downstream_address_provider_->setRemoteAddress( + std::make_shared("12.12.12.12")); ON_CALL(config_, useRemoteAddress()).WillByDefault(Return(true)); TestRequestHeaderMapImpl headers; EXPECT_EQ((MutateRequestRet{"12.12.12.12:0", false}), callMutateRequestHeaders(headers, Protocol::Http2)); - EXPECT_EQ(connection_.remote_address_->ip()->addressAsString(), + EXPECT_EQ(connection_.stream_info_.downstream_address_provider_->remoteAddress() + ->ip() + ->addressAsString(), headers.get_(Headers::get().ForwardedFor)); } @@ -211,7 +214,8 @@ TEST_F(ConnectionManagerUtilityTest, UseRemoteAddressWhenNotLocalHostRemoteAddre // address and where the address is external. TEST_F(ConnectionManagerUtilityTest, SkipXffAppendUseRemoteAddress) { EXPECT_CALL(config_, skipXffAppend()).WillOnce(Return(true)); - connection_.remote_address_ = std::make_shared("12.12.12.12"); + connection_.stream_info_.downstream_address_provider_->setRemoteAddress( + std::make_shared("12.12.12.12")); ON_CALL(config_, useRemoteAddress()).WillByDefault(Return(true)); TestRequestHeaderMapImpl headers; @@ -224,7 +228,8 @@ TEST_F(ConnectionManagerUtilityTest, SkipXffAppendUseRemoteAddress) { // address and where the address is external. TEST_F(ConnectionManagerUtilityTest, SkipXffAppendPassThruUseRemoteAddress) { EXPECT_CALL(config_, skipXffAppend()).WillOnce(Return(true)); - connection_.remote_address_ = std::make_shared("12.12.12.12"); + connection_.stream_info_.downstream_address_provider_->setRemoteAddress( + std::make_shared("12.12.12.12")); ON_CALL(config_, useRemoteAddress()).WillByDefault(Return(true)); TestRequestHeaderMapImpl headers{{"x-forwarded-for", "198.51.100.1"}}; @@ -239,7 +244,8 @@ TEST_F(ConnectionManagerUtilityTest, PreserveForwardedProtoWhenInternal) { ON_CALL(config_, useRemoteAddress()).WillByDefault(Return(true)); ON_CALL(config_, xffNumTrustedHops()).WillByDefault(Return(1)); EXPECT_CALL(config_, skipXffAppend()).WillOnce(Return(true)); - connection_.remote_address_ = std::make_shared("12.12.12.12"); + connection_.stream_info_.downstream_address_provider_->setRemoteAddress( + std::make_shared("12.12.12.12")); ON_CALL(config_, useRemoteAddress()).WillByDefault(Return(true)); TestRequestHeaderMapImpl headers{{"x-forwarded-proto", "https"}}; @@ -250,7 +256,8 @@ TEST_F(ConnectionManagerUtilityTest, PreserveForwardedProtoWhenInternal) { TEST_F(ConnectionManagerUtilityTest, OverwriteForwardedProtoWhenExternal) { ON_CALL(config_, useRemoteAddress()).WillByDefault(Return(true)); ON_CALL(config_, xffNumTrustedHops()).WillByDefault(Return(0)); - connection_.remote_address_ = std::make_shared("127.0.0.1"); + connection_.stream_info_.downstream_address_provider_->setRemoteAddress( + std::make_shared("127.0.0.1")); TestRequestHeaderMapImpl headers{{"x-forwarded-proto", "https"}}; Network::Address::Ipv4Instance local_address("10.3.2.1"); ON_CALL(config_, localAddress()).WillByDefault(ReturnRef(local_address)); @@ -270,7 +277,8 @@ TEST_F(ConnectionManagerUtilityTest, UseRemoteAddressWhenUserConfiguredRemoteAdd ON_CALL(config_, useRemoteAddress()).WillByDefault(Return(true)); ON_CALL(config_, localAddress()).WillByDefault(ReturnRef(local_address)); - connection_.remote_address_ = std::make_shared("12.12.12.12"); + connection_.stream_info_.downstream_address_provider_->setRemoteAddress( + std::make_shared("12.12.12.12")); TestRequestHeaderMapImpl headers; EXPECT_EQ((MutateRequestRet{"12.12.12.12:0", true}), @@ -280,7 +288,8 @@ TEST_F(ConnectionManagerUtilityTest, UseRemoteAddressWhenUserConfiguredRemoteAdd // Verify internal request and XFF is set when we are using remote address the address is internal. TEST_F(ConnectionManagerUtilityTest, UseRemoteAddressWhenLocalHostRemoteAddress) { - connection_.remote_address_ = std::make_shared("127.0.0.1"); + connection_.stream_info_.downstream_address_provider_->setRemoteAddress( + std::make_shared("127.0.0.1")); Network::Address::Ipv4Instance local_address("10.3.2.1"); ON_CALL(config_, useRemoteAddress()).WillByDefault(Return(true)); ON_CALL(config_, localAddress()).WillByDefault(ReturnRef(local_address)); @@ -293,7 +302,8 @@ TEST_F(ConnectionManagerUtilityTest, UseRemoteAddressWhenLocalHostRemoteAddress) // Verify that we trust Nth address from XFF when using remote address with xff_num_trusted_hops. TEST_F(ConnectionManagerUtilityTest, UseRemoteAddressWithXFFTrustedHops) { - connection_.remote_address_ = std::make_shared("203.0.113.128"); + connection_.stream_info_.downstream_address_provider_->setRemoteAddress( + std::make_shared("203.0.113.128")); ON_CALL(config_, useRemoteAddress()).WillByDefault(Return(true)); ON_CALL(config_, xffNumTrustedHops()).WillByDefault(Return(1)); TestRequestHeaderMapImpl headers{{"x-forwarded-for", "198.51.100.1"}}; @@ -304,7 +314,8 @@ TEST_F(ConnectionManagerUtilityTest, UseRemoteAddressWithXFFTrustedHops) { // Verify that xff_num_trusted_hops works when not using remote address. TEST_F(ConnectionManagerUtilityTest, UseXFFTrustedHopsWithoutRemoteAddress) { - connection_.remote_address_ = std::make_shared("127.0.0.1"); + connection_.stream_info_.downstream_address_provider_->setRemoteAddress( + std::make_shared("127.0.0.1")); ON_CALL(config_, useRemoteAddress()).WillByDefault(Return(false)); ON_CALL(config_, xffNumTrustedHops()).WillByDefault(Return(1)); TestRequestHeaderMapImpl headers{{"x-forwarded-for", "198.51.100.2, 198.51.100.1"}}; @@ -315,7 +326,8 @@ TEST_F(ConnectionManagerUtilityTest, UseXFFTrustedHopsWithoutRemoteAddress) { // Verify that we don't set the via header on requests/responses when empty. TEST_F(ConnectionManagerUtilityTest, ViaEmpty) { - connection_.remote_address_ = std::make_shared("10.0.0.1"); + connection_.stream_info_.downstream_address_provider_->setRemoteAddress( + std::make_shared("10.0.0.1")); ON_CALL(config_, useRemoteAddress()).WillByDefault(Return(true)); TestRequestHeaderMapImpl request_headers; @@ -332,7 +344,8 @@ TEST_F(ConnectionManagerUtilityTest, ViaEmpty) { // Verify that we append a non-empty via header on requests/responses. TEST_F(ConnectionManagerUtilityTest, ViaAppend) { via_ = "foo"; - connection_.remote_address_ = std::make_shared("10.0.0.1"); + connection_.stream_info_.downstream_address_provider_->setRemoteAddress( + std::make_shared("10.0.0.1")); ON_CALL(config_, useRemoteAddress()).WillByDefault(Return(true)); TestRequestHeaderMapImpl request_headers; @@ -351,7 +364,8 @@ TEST_F(ConnectionManagerUtilityTest, ViaAppend) { // Verify that we don't set user agent when it is already set. TEST_F(ConnectionManagerUtilityTest, UserAgentDontSet) { - connection_.remote_address_ = std::make_shared("10.0.0.1"); + connection_.stream_info_.downstream_address_provider_->setRemoteAddress( + std::make_shared("10.0.0.1")); ON_CALL(config_, useRemoteAddress()).WillByDefault(Return(true)); TestRequestHeaderMapImpl headers{{"user-agent", "foo"}}; @@ -364,7 +378,8 @@ TEST_F(ConnectionManagerUtilityTest, UserAgentDontSet) { // Verify that we do set user agent when it is empty. TEST_F(ConnectionManagerUtilityTest, UserAgentSetWhenIncomingEmpty) { - connection_.remote_address_ = std::make_shared("10.0.0.1"); + connection_.stream_info_.downstream_address_provider_->setRemoteAddress( + std::make_shared("10.0.0.1")); ON_CALL(config_, useRemoteAddress()).WillByDefault(Return(true)); ON_CALL(local_info_, nodeName()).WillByDefault(ReturnRef(canary_node_)); user_agent_ = "bar"; @@ -422,7 +437,8 @@ TEST_F(ConnectionManagerUtilityTest, InternalServiceForceTrace) { // Test generating request-id in various edge request scenarios. TEST_F(ConnectionManagerUtilityTest, EdgeRequestRegenerateRequestIdAndWipeDownstream) { - connection_.remote_address_ = std::make_shared("34.0.0.1"); + connection_.stream_info_.downstream_address_provider_->setRemoteAddress( + std::make_shared("34.0.0.1")); ON_CALL(config_, useRemoteAddress()).WillByDefault(Return(true)); ON_CALL(runtime_.snapshot_, featureEnabled("tracing.global_enabled", An(), _)) @@ -482,7 +498,6 @@ TEST_F(ConnectionManagerUtilityTest, EdgeRequestRegenerateRequestIdAndWipeDownst // does not overwrite x-request-id. This happens in the internal ingress case. TEST_F(ConnectionManagerUtilityTest, ExternalRequestPreserveRequestIdAndDownstream) { ON_CALL(config_, useRemoteAddress()).WillByDefault(Return(false)); - EXPECT_CALL(connection_, remoteAddress()).Times(0); TestRequestHeaderMapImpl headers{{"x-envoy-downstream-service-cluster", "foo"}, {"x-request-id", "id"}, {"x-forwarded-for", "34.0.0.1"}}; @@ -499,7 +514,8 @@ TEST_F(ConnectionManagerUtilityTest, ExternalRequestPreserveRequestIdAndDownstre // Verify that we don't overwrite user agent, but do set x-envoy-downstream-service-cluster // correctly. TEST_F(ConnectionManagerUtilityTest, UserAgentSetIncomingUserAgent) { - connection_.remote_address_ = std::make_shared("10.0.0.1"); + connection_.stream_info_.downstream_address_provider_->setRemoteAddress( + std::make_shared("10.0.0.1")); ON_CALL(config_, useRemoteAddress()).WillByDefault(Return(true)); user_agent_ = "bar"; @@ -516,7 +532,8 @@ TEST_F(ConnectionManagerUtilityTest, UserAgentSetIncomingUserAgent) { // Verify that we set both user agent and x-envoy-downstream-service-cluster. TEST_F(ConnectionManagerUtilityTest, UserAgentSetNoIncomingUserAgent) { - connection_.remote_address_ = std::make_shared("10.0.0.1"); + connection_.stream_info_.downstream_address_provider_->setRemoteAddress( + std::make_shared("10.0.0.1")); ON_CALL(config_, useRemoteAddress()).WillByDefault(Return(true)); user_agent_ = "bar"; TestRequestHeaderMapImpl headers; @@ -553,7 +570,8 @@ TEST_F(ConnectionManagerUtilityTest, RequestIdGeneratedWhenItsNotPresent) { // Make sure we do not overwrite x-request-id if the request is internal. TEST_F(ConnectionManagerUtilityTest, DoNotOverrideRequestIdIfPresentWhenInternalRequest) { - connection_.remote_address_ = std::make_shared("10.0.0.1"); + connection_.stream_info_.downstream_address_provider_->setRemoteAddress( + std::make_shared("10.0.0.1")); ON_CALL(config_, useRemoteAddress()).WillByDefault(Return(true)); TestRequestHeaderMapImpl headers{{"x-request-id", "original_request_id"}}; EXPECT_CALL(random_, uuid()).Times(0); @@ -565,7 +583,8 @@ TEST_F(ConnectionManagerUtilityTest, DoNotOverrideRequestIdIfPresentWhenInternal // Make sure that we do overwrite x-request-id for "edge" external requests. TEST_F(ConnectionManagerUtilityTest, OverrideRequestIdForExternalRequests) { - connection_.remote_address_ = std::make_shared("134.2.2.11"); + connection_.stream_info_.downstream_address_provider_->setRemoteAddress( + std::make_shared("134.2.2.11")); ON_CALL(config_, useRemoteAddress()).WillByDefault(Return(true)); TestRequestHeaderMapImpl headers{{"x-request-id", "original"}}; EXPECT_CALL(random_, uuid()).WillOnce(Return("override")); @@ -578,7 +597,8 @@ TEST_F(ConnectionManagerUtilityTest, OverrideRequestIdForExternalRequests) { // A request that uses remote address and is from an external address should be treated as an // external request with all internal only headers cleaned. TEST_F(ConnectionManagerUtilityTest, ExternalAddressExternalRequestUseRemote) { - connection_.remote_address_ = std::make_shared("50.0.0.1"); + connection_.stream_info_.downstream_address_provider_->setRemoteAddress( + std::make_shared("50.0.0.1")); ON_CALL(config_, useRemoteAddress()).WillByDefault(Return(true)); route_config_.internal_only_headers_.push_back(LowerCaseString("custom_header")); TestRequestHeaderMapImpl headers{{"x-envoy-decorator-operation", "foo"}, @@ -618,7 +638,8 @@ TEST_F(ConnectionManagerUtilityTest, ExternalAddressExternalRequestUseRemote) { // A request that is from an external address, but does not use remote address, should pull the // address from XFF. TEST_F(ConnectionManagerUtilityTest, ExternalAddressExternalRequestDontUseRemote) { - connection_.remote_address_ = std::make_shared("60.0.0.2"); + connection_.stream_info_.downstream_address_provider_->setRemoteAddress( + std::make_shared("60.0.0.2")); ON_CALL(config_, useRemoteAddress()).WillByDefault(Return(false)); TestRequestHeaderMapImpl headers{{"x-envoy-external-address", "60.0.0.1"}, {"x-forwarded-for", "60.0.0.1"}}; @@ -631,7 +652,8 @@ TEST_F(ConnectionManagerUtilityTest, ExternalAddressExternalRequestDontUseRemote // Verify that if XFF is invalid we fall back to remote address, even if it is a pipe. TEST_F(ConnectionManagerUtilityTest, PipeAddressInvalidXFFtDontUseRemote) { - connection_.remote_address_ = std::make_shared("/blah"); + connection_.stream_info_.downstream_address_provider_->setRemoteAddress( + std::make_shared("/blah")); ON_CALL(config_, useRemoteAddress()).WillByDefault(Return(false)); TestRequestHeaderMapImpl headers{{"x-forwarded-for", "blah"}}; @@ -643,7 +665,8 @@ TEST_F(ConnectionManagerUtilityTest, PipeAddressInvalidXFFtDontUseRemote) { // includes only internal addresses. Note that this is legacy behavior. See the comments // in mutateRequestHeaders() for more info. TEST_F(ConnectionManagerUtilityTest, AppendInternalAddressXffNotInternalRequest) { - connection_.remote_address_ = std::make_shared("10.0.0.1"); + connection_.stream_info_.downstream_address_provider_->setRemoteAddress( + std::make_shared("10.0.0.1")); ON_CALL(config_, useRemoteAddress()).WillByDefault(Return(true)); TestRequestHeaderMapImpl headers{{"x-forwarded-for", "10.0.0.2"}}; @@ -655,7 +678,8 @@ TEST_F(ConnectionManagerUtilityTest, AppendInternalAddressXffNotInternalRequest) // A request that is from an internal address and uses remote address should be an internal request. // It should also preserve x-envoy-external-address. TEST_F(ConnectionManagerUtilityTest, ExternalAddressInternalRequestUseRemote) { - connection_.remote_address_ = std::make_shared("10.0.0.1"); + connection_.stream_info_.downstream_address_provider_->setRemoteAddress( + std::make_shared("10.0.0.1")); ON_CALL(config_, useRemoteAddress()).WillByDefault(Return(true)); TestRequestHeaderMapImpl headers{{"x-envoy-external-address", "60.0.0.1"}, {"x-envoy-expected-rq-timeout-ms", "10"}}; @@ -1438,7 +1462,8 @@ TEST_F(ConnectionManagerUtilityTest, RemovePort) { // test preserve_external_request_id true does not reset the passed requestId if passed TEST_F(ConnectionManagerUtilityTest, PreserveExternalRequestId) { - connection_.remote_address_ = std::make_shared("134.2.2.11"); + connection_.stream_info_.downstream_address_provider_->setRemoteAddress( + std::make_shared("134.2.2.11")); ON_CALL(config_, useRemoteAddress()).WillByDefault(Return(true)); ON_CALL(config_, preserveExternalRequestId()).WillByDefault(Return(true)); TestRequestHeaderMapImpl headers{{"x-request-id", "my-request-id"}, @@ -1453,7 +1478,8 @@ TEST_F(ConnectionManagerUtilityTest, PreserveExternalRequestId) { // test preserve_external_request_id true but generates new request id when not passed TEST_F(ConnectionManagerUtilityTest, PreseverExternalRequestIdNoReqId) { - connection_.remote_address_ = std::make_shared("134.2.2.11"); + connection_.stream_info_.downstream_address_provider_->setRemoteAddress( + std::make_shared("134.2.2.11")); ON_CALL(config_, useRemoteAddress()).WillByDefault(Return(true)); ON_CALL(config_, preserveExternalRequestId()).WillByDefault(Return(true)); TestRequestHeaderMapImpl headers{{"x-forwarded-for", "198.51.100.1"}}; @@ -1489,7 +1515,8 @@ TEST_F(ConnectionManagerUtilityTest, PreserveExternalRequestIdNoEdgeRequestGener // test preserve_external_request_id false edge request generates new request id TEST_F(ConnectionManagerUtilityTest, NoPreserveExternalRequestIdEdgeRequestGenerateRequestId) { ON_CALL(config_, preserveExternalRequestId()).WillByDefault(Return(false)); - connection_.remote_address_ = std::make_shared("134.2.2.11"); + connection_.stream_info_.downstream_address_provider_->setRemoteAddress( + std::make_shared("134.2.2.11")); // with request id { diff --git a/test/common/network/address_impl_test.cc b/test/common/network/address_impl_test.cc index d1a01ca6734b..5284464bb165 100644 --- a/test/common/network/address_impl_test.cc +++ b/test/common/network/address_impl_test.cc @@ -49,7 +49,7 @@ void testSocketBindAndConnect(Network::Address::IpVersion ip_version, bool v6onl ASSERT_NE(addr_port->ip(), nullptr); // Create a socket on which we'll listen for connections from clients. - SocketImpl sock(Socket::Type::Stream, addr_port); + SocketImpl sock(Socket::Type::Stream, addr_port, nullptr); EXPECT_TRUE(sock.ioHandle().isOpen()) << addr_port->asString(); // Check that IPv6 sockets accept IPv6 connections only. @@ -71,7 +71,7 @@ void testSocketBindAndConnect(Network::Address::IpVersion ip_version, bool v6onl auto client_connect = [](Address::InstanceConstSharedPtr addr_port) { // Create a client socket and connect to the server. - SocketImpl client_sock(Socket::Type::Stream, addr_port); + SocketImpl client_sock(Socket::Type::Stream, addr_port, nullptr); EXPECT_TRUE(client_sock.ioHandle().isOpen()) << addr_port->asString(); @@ -344,7 +344,7 @@ TEST(PipeInstanceTest, BasicPermission) { const mode_t mode = 0777; PipeInstance pipe(path, mode); InstanceConstSharedPtr address = std::make_shared(pipe); - SocketImpl sock(Socket::Type::Stream, address); + SocketImpl sock(Socket::Type::Stream, address, nullptr); EXPECT_TRUE(sock.ioHandle().isOpen()) << pipe.asString(); @@ -371,7 +371,7 @@ TEST(PipeInstanceTest, PermissionFail) { const mode_t mode = 0777; PipeInstance pipe(path, mode); InstanceConstSharedPtr address = std::make_shared(pipe); - SocketImpl sock(Socket::Type::Stream, address); + SocketImpl sock(Socket::Type::Stream, address, nullptr); EXPECT_TRUE(sock.ioHandle().isOpen()) << pipe.asString(); @@ -443,7 +443,7 @@ TEST(PipeInstanceTest, UnlinksExistingFile) { const auto bind_uds_socket = [](const std::string& path) { PipeInstance pipe(path); InstanceConstSharedPtr address = std::make_shared(pipe); - SocketImpl sock(Socket::Type::Stream, address); + SocketImpl sock(Socket::Type::Stream, address, nullptr); EXPECT_TRUE(sock.ioHandle().isOpen()) << pipe.asString(); diff --git a/test/common/network/connection_impl_test.cc b/test/common/network/connection_impl_test.cc index 9aff2cac0007..ed323277d899 100644 --- a/test/common/network/connection_impl_test.cc +++ b/test/common/network/connection_impl_test.cc @@ -136,13 +136,13 @@ class ConnectionImplTest : public testing::TestWithParam { listener_ = dispatcher_->createListener(socket_, listener_callbacks_, true, ENVOY_TCP_BACKLOG_SIZE); client_connection_ = std::make_unique( - *dispatcher_, socket_->localAddress(), source_address_, + *dispatcher_, socket_->addressProvider().localAddress(), source_address_, Network::Test::createRawBufferSocket(), socket_options_); client_connection_->addConnectionCallbacks(client_callbacks_); EXPECT_EQ(nullptr, client_connection_->ssl()); const Network::ClientConnection& const_connection = *client_connection_; EXPECT_EQ(nullptr, const_connection.ssl()); - EXPECT_FALSE(client_connection_->localAddressRestored()); + EXPECT_FALSE(client_connection_->addressProvider().localAddressRestored()); } void connect() { @@ -376,7 +376,7 @@ TEST_P(ConnectionImplTest, ImmediateConnectError) { Address::InstanceConstSharedPtr broadcast_address; socket_ = std::make_shared( Network::Test::getCanonicalLoopbackAddress(GetParam()), nullptr, true); - if (socket_->localAddress()->ip()->version() == Address::IpVersion::v4) { + if (socket_->addressProvider().localAddress()->ip()->version() == Address::IpVersion::v4) { broadcast_address = std::make_shared("224.0.0.1", 0); } else { broadcast_address = std::make_shared("ff02::1", 0); @@ -483,8 +483,8 @@ TEST_P(ConnectionImplTest, SocketOptions) { server_connection_->addReadFilter(read_filter_); upstream_connection_ = dispatcher_->createClientConnection( - socket_->localAddress(), source_address_, Network::Test::createRawBufferSocket(), - server_connection_->socketOptions()); + socket_->addressProvider().localAddress(), source_address_, + Network::Test::createRawBufferSocket(), server_connection_->socketOptions()); })); EXPECT_CALL(server_callbacks_, onEvent(ConnectionEvent::RemoteClose)) @@ -532,8 +532,8 @@ TEST_P(ConnectionImplTest, SocketOptionsFailureTest) { server_connection_->addReadFilter(read_filter_); upstream_connection_ = dispatcher_->createClientConnection( - socket_->localAddress(), source_address_, Network::Test::createRawBufferSocket(), - server_connection_->socketOptions()); + socket_->addressProvider().localAddress(), source_address_, + Network::Test::createRawBufferSocket(), server_connection_->socketOptions()); upstream_connection_->addConnectionCallbacks(upstream_callbacks_); })); @@ -1268,7 +1268,8 @@ TEST_P(ConnectionImplTest, BindTest) { } setUpBasicConnection(); connect(); - EXPECT_EQ(address_string, server_connection_->remoteAddress()->ip()->addressAsString()); + EXPECT_EQ(address_string, + server_connection_->addressProvider().remoteAddress()->ip()->addressAsString()); disconnect(true); } @@ -1287,7 +1288,7 @@ TEST_P(ConnectionImplTest, BindFromSocketTest) { auto option = std::make_shared>(); EXPECT_CALL(*option, setOption(_, Eq(envoy::config::core::v3::SocketOption::STATE_PREBIND))) .WillOnce(Invoke([&](Socket& socket, envoy::config::core::v3::SocketOption::SocketState) { - socket.setLocalAddress(new_source_address); + socket.addressProvider().setLocalAddress(new_source_address); return true; })); @@ -1295,7 +1296,8 @@ TEST_P(ConnectionImplTest, BindFromSocketTest) { socket_options_->emplace_back(std::move(option)); setUpBasicConnection(); connect(); - EXPECT_EQ(address_string, server_connection_->remoteAddress()->ip()->addressAsString()); + EXPECT_EQ(address_string, + server_connection_->addressProvider().remoteAddress()->ip()->addressAsString()); disconnect(true); } @@ -1317,7 +1319,8 @@ TEST_P(ConnectionImplTest, BindFailureTest) { dispatcher_->createListener(socket_, listener_callbacks_, true, ENVOY_TCP_BACKLOG_SIZE); client_connection_ = dispatcher_->createClientConnection( - socket_->localAddress(), source_address_, Network::Test::createRawBufferSocket(), nullptr); + socket_->addressProvider().localAddress(), source_address_, + Network::Test::createRawBufferSocket(), nullptr); MockConnectionStats connection_stats; client_connection_->setConnectionStats(connection_stats.toBufferStats()); @@ -1879,7 +1882,7 @@ class FakeReadFilter : public Network::ReadFilter { class MockTransportConnectionImplTest : public testing::Test { public: - MockTransportConnectionImplTest() : stream_info_(dispatcher_.timeSource()) { + MockTransportConnectionImplTest() : stream_info_(dispatcher_.timeSource(), nullptr) { EXPECT_CALL(dispatcher_.buffer_factory_, create_(_, _, _)) .WillRepeatedly(Invoke([](std::function below_low, std::function above_high, std::function above_overflow) -> Buffer::Instance* { @@ -2684,7 +2687,7 @@ class ReadBufferLimitTest : public ConnectionImplTest { dispatcher_->createListener(socket_, listener_callbacks_, true, ENVOY_TCP_BACKLOG_SIZE); client_connection_ = dispatcher_->createClientConnection( - socket_->localAddress(), Network::Address::InstanceConstSharedPtr(), + socket_->addressProvider().localAddress(), Network::Address::InstanceConstSharedPtr(), Network::Test::createRawBufferSocket(), nullptr); client_connection_->addConnectionCallbacks(client_callbacks_); client_connection_->connect(); diff --git a/test/common/network/dns_impl_test.cc b/test/common/network/dns_impl_test.cc index e6282f2e6405..6fc0703641e6 100644 --- a/test/common/network/dns_impl_test.cc +++ b/test/common/network/dns_impl_test.cc @@ -271,7 +271,7 @@ class TestDnsServerQuery { class TestDnsServer : public TcpListenerCallbacks { public: TestDnsServer(Event::Dispatcher& dispatcher) - : dispatcher_(dispatcher), record_ttl_(0), stream_info_(dispatcher.timeSource()) {} + : dispatcher_(dispatcher), record_ttl_(0), stream_info_(dispatcher.timeSource(), nullptr) {} void onAccept(ConnectionSocketPtr&& socket) override { Network::ConnectionPtr new_connection = dispatcher_.createServerConnection( @@ -441,7 +441,8 @@ class DnsImplTest : public testing::TestWithParam { listener_ = dispatcher_->createListener(socket_, *server_, true, ENVOY_TCP_BACKLOG_SIZE); if (setResolverInConstructor()) { - resolver_ = dispatcher_->createDnsResolver({socket_->localAddress()}, useTcpForDnsLookups()); + resolver_ = dispatcher_->createDnsResolver({socket_->addressProvider().localAddress()}, + useTcpForDnsLookups()); } else { resolver_ = dispatcher_->createDnsResolver({}, useTcpForDnsLookups()); } @@ -451,7 +452,8 @@ class DnsImplTest : public testing::TestWithParam { if (tcpOnly()) { peer_->resetChannelTcpOnly(zeroTimeout()); } - ares_set_servers_ports_csv(peer_->channel(), socket_->localAddress()->asString().c_str()); + ares_set_servers_ports_csv(peer_->channel(), + socket_->addressProvider().localAddress()->asString().c_str()); } void TearDown() override { @@ -585,7 +587,8 @@ TEST_P(DnsImplTest, DestructCallback) { // This simulates destruction thanks to another query setting the dirty_channel_ bit, thus causing // a subsequent result to call ares_destroy. peer_->resetChannelTcpOnly(zeroTimeout()); - ares_set_servers_ports_csv(peer_->channel(), socket_->localAddress()->asString().c_str()); + ares_set_servers_ports_csv(peer_->channel(), + socket_->addressProvider().localAddress()->asString().c_str()); dispatcher_->run(Event::Dispatcher::RunType::Block); } @@ -712,7 +715,8 @@ TEST_P(DnsImplTest, DestroyChannelOnRefused) { if (tcpOnly()) { peer_->resetChannelTcpOnly(zeroTimeout()); } - ares_set_servers_ports_csv(peer_->channel(), socket_->localAddress()->asString().c_str()); + ares_set_servers_ports_csv(peer_->channel(), + socket_->addressProvider().localAddress()->asString().c_str()); EXPECT_NE(nullptr, resolveWithExpectations("some.good.domain", DnsLookupFamily::Auto, DnsResolver::ResolutionStatus::Success, diff --git a/test/common/network/filter_matcher_test.cc b/test/common/network/filter_matcher_test.cc index cd00d5cc7174..0039642c0cba 100644 --- a/test/common/network/filter_matcher_test.cc +++ b/test/common/network/filter_matcher_test.cc @@ -14,17 +14,16 @@ namespace { struct CallbackHandle { std::unique_ptr callback_; std::unique_ptr socket_; - Address::InstanceConstSharedPtr address_; }; } // namespace class ListenerFilterMatcherTest : public testing::Test { public: std::unique_ptr createCallbackOnPort(int port) { auto handle = std::make_unique(); - handle->address_ = std::make_shared("127.0.0.1", port); handle->socket_ = std::make_unique(); handle->callback_ = std::make_unique(); - EXPECT_CALL(*(handle->socket_), localAddress()).WillRepeatedly(ReturnRef(handle->address_)); + handle->socket_->address_provider_->setLocalAddress( + std::make_shared("127.0.0.1", port)); EXPECT_CALL(*(handle->callback_), socket()).WillRepeatedly(ReturnRef(*(handle->socket_))); return handle; } diff --git a/test/common/network/listen_socket_impl_test.cc b/test/common/network/listen_socket_impl_test.cc index f252b2c6d063..b8155b927dea 100644 --- a/test/common/network/listen_socket_impl_test.cc +++ b/test/common/network/listen_socket_impl_test.cc @@ -87,8 +87,9 @@ class ListenSocketImplTest : public testing::TestWithParam { EXPECT_EQ(0, socket1->listen(0).rc_); } - EXPECT_EQ(addr->ip()->port(), socket1->localAddress()->ip()->port()); - EXPECT_EQ(addr->ip()->addressAsString(), socket1->localAddress()->ip()->addressAsString()); + EXPECT_EQ(addr->ip()->port(), socket1->addressProvider().localAddress()->ip()->port()); + EXPECT_EQ(addr->ip()->addressAsString(), + socket1->addressProvider().localAddress()->ip()->addressAsString()); EXPECT_EQ(Type, socket1->socketType()); auto option2 = std::make_unique(); @@ -108,7 +109,7 @@ class ListenSocketImplTest : public testing::TestWithParam { EXPECT_TRUE(SOCKET_VALID(socket_result.rc_)); Network::IoHandlePtr io_handle = std::make_unique(socket_result.rc_); auto socket3 = createListenSocketPtr(std::move(io_handle), addr, nullptr); - EXPECT_EQ(socket3->localAddress()->asString(), addr->asString()); + EXPECT_EQ(socket3->addressProvider().localAddress()->asString(), addr->asString()); // Test successful. return; @@ -118,10 +119,11 @@ class ListenSocketImplTest : public testing::TestWithParam { void testBindPortZero() { auto loopback = Network::Test::getCanonicalLoopbackAddress(version_); auto socket = createListenSocketPtr(loopback, nullptr, true); - EXPECT_EQ(Address::Type::Ip, socket->localAddress()->type()); - EXPECT_EQ(version_, socket->localAddress()->ip()->version()); - EXPECT_EQ(loopback->ip()->addressAsString(), socket->localAddress()->ip()->addressAsString()); - EXPECT_GT(socket->localAddress()->ip()->port(), 0U); + EXPECT_EQ(Address::Type::Ip, socket->addressProvider().localAddress()->type()); + EXPECT_EQ(version_, socket->addressProvider().localAddress()->ip()->version()); + EXPECT_EQ(loopback->ip()->addressAsString(), + socket->addressProvider().localAddress()->ip()->addressAsString()); + EXPECT_GT(socket->addressProvider().localAddress()->ip()->port(), 0U); EXPECT_EQ(Type, socket->socketType()); } }; @@ -160,9 +162,9 @@ TEST_P(ListenSocketImplTestTcp, SetLocalAddress) { TestListenSocket socket(Utility::getIpv4AnyAddress()); - socket.setLocalAddress(address); + socket.addressProvider().setLocalAddress(address); - EXPECT_EQ(socket.localAddress(), address); + EXPECT_EQ(socket.addressProvider().localAddress(), address); } TEST_P(ListenSocketImplTestUdp, BindSpecificPort) { testBindSpecificPort(); } diff --git a/test/common/network/listener_impl_test.cc b/test/common/network/listener_impl_test.cc index 9bbfe6643c42..38dc232a4b7f 100644 --- a/test/common/network/listener_impl_test.cc +++ b/test/common/network/listener_impl_test.cc @@ -40,11 +40,11 @@ static void errorCallbackTest(Address::IpVersion version) { dispatcher->createListener(socket, listener_callbacks, true, ENVOY_TCP_BACKLOG_SIZE); Network::ClientConnectionPtr client_connection = dispatcher->createClientConnection( - socket->localAddress(), Network::Address::InstanceConstSharedPtr(), + socket->addressProvider().localAddress(), Network::Address::InstanceConstSharedPtr(), Network::Test::createRawBufferSocket(), nullptr); client_connection->connect(); - StreamInfo::StreamInfoImpl stream_info(dispatcher->timeSource()); + StreamInfo::StreamInfoImpl stream_info(dispatcher->timeSource(), nullptr); EXPECT_CALL(listener_callbacks, onAccept_(_)) .WillOnce(Invoke([&](Network::ConnectionSocketPtr& accepted_socket) -> void { Network::ConnectionPtr conn = dispatcher->createServerConnection( @@ -113,7 +113,7 @@ TEST_P(TcpListenerImplTest, SetListeningSocketOptionsError) { TestTcpListenerImpl(dispatcherImpl(), random_generator, socket, listener_callbacks, true), CreateListenerException, fmt::format("cannot set post-listen socket option on socket: {}", - socket->localAddress()->asString())); + socket->addressProvider().localAddress()->asString())); } TEST_P(TcpListenerImplTest, UseActualDst) { @@ -131,19 +131,20 @@ TEST_P(TcpListenerImplTest, UseActualDst) { listener_callbacks2, false); Network::ClientConnectionPtr client_connection = dispatcher_->createClientConnection( - socket->localAddress(), Network::Address::InstanceConstSharedPtr(), + socket->addressProvider().localAddress(), Network::Address::InstanceConstSharedPtr(), Network::Test::createRawBufferSocket(), nullptr); client_connection->connect(); EXPECT_CALL(listener, getLocalAddress(_)).Times(0); - StreamInfo::StreamInfoImpl stream_info(dispatcher_->timeSource()); + StreamInfo::StreamInfoImpl stream_info(dispatcher_->timeSource(), nullptr); EXPECT_CALL(listener_callbacks2, onAccept_(_)).Times(0); EXPECT_CALL(listener_callbacks1, onAccept_(_)) .WillOnce(Invoke([&](Network::ConnectionSocketPtr& accepted_socket) -> void { Network::ConnectionPtr conn = dispatcher_->createServerConnection( std::move(accepted_socket), Network::Test::createRawBufferSocket(), stream_info); - EXPECT_EQ(*conn->localAddress(), *socket->localAddress()); + EXPECT_EQ(*conn->addressProvider().localAddress(), + *socket->addressProvider().localAddress()); client_connection->close(ConnectionCloseType::NoFlush); conn->close(ConnectionCloseType::NoFlush); dispatcher_->exit(); @@ -167,7 +168,7 @@ TEST_P(TcpListenerImplTest, GlobalConnectionLimitEnforcement) { std::vector client_connections; std::vector server_connections; - StreamInfo::StreamInfoImpl stream_info(dispatcher_->timeSource()); + StreamInfo::StreamInfoImpl stream_info(dispatcher_->timeSource(), nullptr); EXPECT_CALL(listener_callbacks, onAccept_(_)) .WillRepeatedly(Invoke([&](Network::ConnectionSocketPtr& accepted_socket) -> void { server_connections.emplace_back(dispatcher_->createServerConnection( @@ -178,7 +179,7 @@ TEST_P(TcpListenerImplTest, GlobalConnectionLimitEnforcement) { auto initiate_connections = [&](const int count) { for (int i = 0; i < count; ++i) { client_connections.emplace_back(dispatcher_->createClientConnection( - socket->localAddress(), Network::Address::InstanceConstSharedPtr(), + socket->addressProvider().localAddress(), Network::Address::InstanceConstSharedPtr(), Network::Test::createRawBufferSocket(), nullptr)); client_connections.back()->connect(); } @@ -231,19 +232,20 @@ TEST_P(TcpListenerImplTest, WildcardListenerUseActualDst) { Network::TestTcpListenerImpl listener(dispatcherImpl(), random_generator, socket, listener_callbacks, true); - auto local_dst_address = Network::Utility::getAddressWithPort( - *Network::Test::getCanonicalLoopbackAddress(version_), socket->localAddress()->ip()->port()); + auto local_dst_address = + Network::Utility::getAddressWithPort(*Network::Test::getCanonicalLoopbackAddress(version_), + socket->addressProvider().localAddress()->ip()->port()); Network::ClientConnectionPtr client_connection = dispatcher_->createClientConnection( local_dst_address, Network::Address::InstanceConstSharedPtr(), Network::Test::createRawBufferSocket(), nullptr); client_connection->connect(); - StreamInfo::StreamInfoImpl stream_info(dispatcher_->timeSource()); + StreamInfo::StreamInfoImpl stream_info(dispatcher_->timeSource(), nullptr); EXPECT_CALL(listener_callbacks, onAccept_(_)) .WillOnce(Invoke([&](Network::ConnectionSocketPtr& socket) -> void { Network::ConnectionPtr conn = dispatcher_->createServerConnection( std::move(socket), Network::Test::createRawBufferSocket(), stream_info); - EXPECT_EQ(*conn->localAddress(), *local_dst_address); + EXPECT_EQ(*conn->addressProvider().localAddress(), *local_dst_address); client_connection->close(ConnectionCloseType::NoFlush); conn->close(ConnectionCloseType::NoFlush); dispatcher_->exit(); @@ -269,29 +271,33 @@ TEST_P(TcpListenerImplTest, WildcardListenerIpv4Compat) { Network::MockConnectionHandler connection_handler; Random::MockRandomGenerator random_generator; - ASSERT_TRUE(socket->localAddress()->ip()->isAnyAddress()); + ASSERT_TRUE(socket->addressProvider().localAddress()->ip()->isAnyAddress()); // Do not redirect since use_original_dst is false. Network::TestTcpListenerImpl listener(dispatcherImpl(), random_generator, socket, listener_callbacks, true); - auto listener_address = Network::Utility::getAddressWithPort( - *Network::Test::getCanonicalLoopbackAddress(version_), socket->localAddress()->ip()->port()); - auto local_dst_address = Network::Utility::getAddressWithPort( - *Network::Utility::getCanonicalIpv4LoopbackAddress(), socket->localAddress()->ip()->port()); + auto listener_address = + Network::Utility::getAddressWithPort(*Network::Test::getCanonicalLoopbackAddress(version_), + socket->addressProvider().localAddress()->ip()->port()); + auto local_dst_address = + Network::Utility::getAddressWithPort(*Network::Utility::getCanonicalIpv4LoopbackAddress(), + socket->addressProvider().localAddress()->ip()->port()); Network::ClientConnectionPtr client_connection = dispatcher_->createClientConnection( local_dst_address, Network::Address::InstanceConstSharedPtr(), Network::Test::createRawBufferSocket(), nullptr); client_connection->connect(); - StreamInfo::StreamInfoImpl stream_info(dispatcher_->timeSource()); + StreamInfo::StreamInfoImpl stream_info(dispatcher_->timeSource(), nullptr); EXPECT_CALL(listener_callbacks, onAccept_(_)) .WillOnce(Invoke([&](Network::ConnectionSocketPtr& socket) -> void { Network::ConnectionPtr conn = dispatcher_->createServerConnection( std::move(socket), Network::Test::createRawBufferSocket(), stream_info); - EXPECT_EQ(conn->localAddress()->ip()->version(), conn->remoteAddress()->ip()->version()); - EXPECT_EQ(conn->localAddress()->asString(), local_dst_address->asString()); - EXPECT_EQ(*conn->localAddress(), *local_dst_address); + EXPECT_EQ(conn->addressProvider().localAddress()->ip()->version(), + conn->addressProvider().remoteAddress()->ip()->version()); + EXPECT_EQ(conn->addressProvider().localAddress()->asString(), + local_dst_address->asString()); + EXPECT_EQ(*conn->addressProvider().localAddress(), *local_dst_address); client_connection->close(ConnectionCloseType::NoFlush); conn->close(ConnectionCloseType::NoFlush); dispatcher_->exit(); @@ -314,9 +320,9 @@ TEST_P(TcpListenerImplTest, DisableAndEnableListener) { // When listener is disabled, the timer should fire before any connection is accepted. listener.disable(); - ClientConnectionPtr client_connection = - dispatcher_->createClientConnection(socket->localAddress(), Address::InstanceConstSharedPtr(), - Network::Test::createRawBufferSocket(), nullptr); + ClientConnectionPtr client_connection = dispatcher_->createClientConnection( + socket->addressProvider().localAddress(), Address::InstanceConstSharedPtr(), + Network::Test::createRawBufferSocket(), nullptr); client_connection->addConnectionCallbacks(connection_callbacks); client_connection->connect(); @@ -362,9 +368,9 @@ TEST_P(TcpListenerImplTest, SetListenerRejectFractionZero) { } EXPECT_CALL(listener_callbacks, onAccept_(_)).WillOnce([&] { dispatcher_->exit(); }); - ClientConnectionPtr client_connection = - dispatcher_->createClientConnection(socket->localAddress(), Address::InstanceConstSharedPtr(), - Network::Test::createRawBufferSocket(), nullptr); + ClientConnectionPtr client_connection = dispatcher_->createClientConnection( + socket->addressProvider().localAddress(), Address::InstanceConstSharedPtr(), + Network::Test::createRawBufferSocket(), nullptr); client_connection->addConnectionCallbacks(connection_callbacks); client_connection->connect(); dispatcher_->run(Event::Dispatcher::RunType::Block); @@ -401,7 +407,7 @@ TEST_P(TcpListenerImplTest, SetListenerRejectFractionIntermediate) { { ClientConnectionPtr client_connection = dispatcher_->createClientConnection( - socket->localAddress(), Address::InstanceConstSharedPtr(), + socket->addressProvider().localAddress(), Address::InstanceConstSharedPtr(), Network::Test::createRawBufferSocket(), nullptr); client_connection->addConnectionCallbacks(connection_callbacks); client_connection->connect(); @@ -424,7 +430,7 @@ TEST_P(TcpListenerImplTest, SetListenerRejectFractionIntermediate) { { ClientConnectionPtr client_connection = dispatcher_->createClientConnection( - socket->localAddress(), Address::InstanceConstSharedPtr(), + socket->addressProvider().localAddress(), Address::InstanceConstSharedPtr(), Network::Test::createRawBufferSocket(), nullptr); client_connection->addConnectionCallbacks(connection_callbacks); client_connection->connect(); @@ -461,9 +467,9 @@ TEST_P(TcpListenerImplTest, SetListenerRejectFractionAll) { }); } - ClientConnectionPtr client_connection = - dispatcher_->createClientConnection(socket->localAddress(), Address::InstanceConstSharedPtr(), - Network::Test::createRawBufferSocket(), nullptr); + ClientConnectionPtr client_connection = dispatcher_->createClientConnection( + socket->addressProvider().localAddress(), Address::InstanceConstSharedPtr(), + Network::Test::createRawBufferSocket(), nullptr); client_connection->addConnectionCallbacks(connection_callbacks); client_connection->connect(); dispatcher_->run(Event::Dispatcher::RunType::Block); diff --git a/test/common/network/socket_option_factory_test.cc b/test/common/network/socket_option_factory_test.cc index b25894f27cb5..30ee22cd15c1 100644 --- a/test/common/network/socket_option_factory_test.cc +++ b/test/common/network/socket_option_factory_test.cc @@ -33,12 +33,14 @@ class SocketOptionFactoryTest : public testing::Test { testing::NiceMock socket_mock_; Api::MockOsSysCalls os_sys_calls_mock_; - void SetUp() override { socket_mock_.local_address_.reset(); } + void SetUp() override { socket_mock_.address_provider_->setLocalAddress(nullptr); } void makeSocketV4() { - socket_mock_.local_address_ = std::make_unique("1.2.3.4", 5678); + socket_mock_.address_provider_->setLocalAddress( + std::make_unique("1.2.3.4", 5678)); } void makeSocketV6() { - socket_mock_.local_address_ = std::make_unique("::1:2:3:4", 5678); + socket_mock_.address_provider_->setLocalAddress( + std::make_unique("::1:2:3:4", 5678)); } }; diff --git a/test/common/network/socket_option_test.h b/test/common/network/socket_option_test.h index ed6c4568061d..e780c3e2ec2a 100644 --- a/test/common/network/socket_option_test.h +++ b/test/common/network/socket_option_test.h @@ -25,7 +25,7 @@ namespace { class SocketOptionTest : public testing::Test { public: SocketOptionTest() { - socket_.local_address_.reset(); + socket_.address_provider_->setLocalAddress(nullptr); EXPECT_CALL(os_sys_calls_, socket(_, _, _)) .Times(AnyNumber()) diff --git a/test/common/network/udp_listener_impl_batch_writer_test.cc b/test/common/network/udp_listener_impl_batch_writer_test.cc index 959fd52515f1..40e4c0a87671 100644 --- a/test/common/network/udp_listener_impl_batch_writer_test.cc +++ b/test/common/network/udp_listener_impl_batch_writer_test.cc @@ -196,8 +196,8 @@ TEST_P(UdpListenerImplBatchWriterTest, WriteBlocked) { // First have initial payload added to the udp_packet_writer's internal buffer. Buffer::InstancePtr initial_buffer(new Buffer::OwnedImpl()); initial_buffer->add(initial_payload); - UdpSendData initial_send_data{send_to_addr_->ip(), *server_socket_->localAddress(), - *initial_buffer}; + UdpSendData initial_send_data{ + send_to_addr_->ip(), *server_socket_->addressProvider().localAddress(), *initial_buffer}; auto send_result = listener_->send(initial_send_data); internal_buffer.append(initial_payload); EXPECT_TRUE(send_result.ok()); @@ -220,8 +220,8 @@ TEST_P(UdpListenerImplBatchWriterTest, WriteBlocked) { // Now send the following payload Buffer::InstancePtr following_buffer(new Buffer::OwnedImpl()); following_buffer->add(following_payload); - UdpSendData following_send_data{send_to_addr_->ip(), *server_socket_->localAddress(), - *following_buffer}; + UdpSendData following_send_data{ + send_to_addr_->ip(), *server_socket_->addressProvider().localAddress(), *following_buffer}; send_result = listener_->send(following_send_data); if (following_payload.length() < initial_payload.length()) { @@ -238,7 +238,8 @@ TEST_P(UdpListenerImplBatchWriterTest, WriteBlocked) { return -1; })); following_buffer->add(following_payload); - UdpSendData final_send_data{send_to_addr_->ip(), *server_socket_->localAddress(), + UdpSendData final_send_data{send_to_addr_->ip(), + *server_socket_->addressProvider().localAddress(), *following_buffer}; send_result = listener_->send(final_send_data); } diff --git a/test/common/network/udp_listener_impl_test.cc b/test/common/network/udp_listener_impl_test.cc index b6e4bd670510..caddb62bc4ce 100644 --- a/test/common/network/udp_listener_impl_test.cc +++ b/test/common/network/udp_listener_impl_test.cc @@ -258,7 +258,7 @@ TEST_P(UdpListenerImplTest, UdpEcho) { * Tests UDP listener's `enable` and `disable` APIs. */ TEST_P(UdpListenerImplTest, UdpListenerEnableDisable) { - auto const* server_ip = server_socket_->localAddress()->ip(); + auto const* server_ip = server_socket_->addressProvider().localAddress()->ip(); ASSERT_NE(server_ip, nullptr); // We first disable the listener and then send two packets. @@ -304,7 +304,7 @@ TEST_P(UdpListenerImplTest, UdpListenerEnableDisable) { * Tests UDP listener's error callback. */ TEST_P(UdpListenerImplTest, UdpListenerRecvMsgError) { - auto const* server_ip = server_socket_->localAddress()->ip(); + auto const* server_ip = server_socket_->addressProvider().localAddress()->ip(); ASSERT_NE(server_ip, nullptr); // When the `receive` system call returns an error, we expect the `onReceiveError` @@ -377,7 +377,8 @@ TEST_P(UdpListenerImplTest, SendDataError) { Buffer::InstancePtr buffer(new Buffer::OwnedImpl()); buffer->add(payload); // send data to itself - UdpSendData send_data{send_to_addr_->ip(), *server_socket_->localAddress(), *buffer}; + UdpSendData send_data{send_to_addr_->ip(), *server_socket_->addressProvider().localAddress(), + *buffer}; // Inject mocked OsSysCalls implementation to mock a write failure. Api::MockOsSysCalls os_sys_calls; diff --git a/test/common/network/udp_listener_impl_test_base.h b/test/common/network/udp_listener_impl_test_base.h index f3b57a1dd136..b3dc07dc30b4 100644 --- a/test/common/network/udp_listener_impl_test_base.h +++ b/test/common/network/udp_listener_impl_test_base.h @@ -38,11 +38,13 @@ class UdpListenerImplTestBase : public ListenerImplTestBase { protected: Address::Instance* getServerLoopbackAddress() { if (version_ == Address::IpVersion::v4) { - return new Address::Ipv4Instance(Network::Test::getLoopbackAddressString(version_), - server_socket_->localAddress()->ip()->port()); + return new Address::Ipv4Instance( + Network::Test::getLoopbackAddressString(version_), + server_socket_->addressProvider().localAddress()->ip()->port()); } - return new Address::Ipv6Instance(Network::Test::getLoopbackAddressString(version_), - server_socket_->localAddress()->ip()->port()); + return new Address::Ipv6Instance( + Network::Test::getLoopbackAddressString(version_), + server_socket_->addressProvider().localAddress()->ip()->port()); } SocketSharedPtr createServerSocket(bool bind) { @@ -64,7 +66,7 @@ class UdpListenerImplTestBase : public ListenerImplTestBase { if (version_ == Address::IpVersion::v4) { // Linux kernel regards any 127.x.x.x as local address. But Mac OS doesn't. send_from_addr = std::make_shared( - "127.0.0.1", server_socket_->localAddress()->ip()->port()); + "127.0.0.1", server_socket_->addressProvider().localAddress()->ip()->port()); } else { // Only use non-local v6 address if IP_FREEBIND is supported. Otherwise use // ::1 to avoid EINVAL error. Unfortunately this can't verify that sendmsg with @@ -76,7 +78,7 @@ class UdpListenerImplTestBase : public ListenerImplTestBase { #else "::1", #endif - server_socket_->localAddress()->ip()->port()); + server_socket_->addressProvider().localAddress()->ip()->port()); } return send_from_addr; } diff --git a/test/common/network/utility_test.cc b/test/common/network/utility_test.cc index 9588f02bb364..b2005e530ea1 100644 --- a/test/common/network/utility_test.cc +++ b/test/common/network/utility_test.cc @@ -183,60 +183,76 @@ TEST(NetworkUtility, GetOriginalDst) { } TEST(NetworkUtility, LocalConnection) { - Network::Address::InstanceConstSharedPtr local_addr; - Network::Address::InstanceConstSharedPtr remote_addr; - testing::NiceMock socket; - EXPECT_CALL(socket, localAddress()).WillRepeatedly(testing::ReturnRef(local_addr)); - EXPECT_CALL(socket, remoteAddress()).WillRepeatedly(testing::ReturnRef(remote_addr)); - - local_addr = std::make_shared("127.0.0.1"); - remote_addr = std::make_shared("/pipe/path"); + socket.address_provider_->setLocalAddress( + std::make_shared("127.0.0.1")); + socket.address_provider_->setRemoteAddress( + std::make_shared("/pipe/path")); EXPECT_TRUE(Utility::isSameIpOrLoopback(socket)); - local_addr = std::make_shared("/pipe/path"); - remote_addr = std::make_shared("/pipe/path"); + socket.address_provider_->setLocalAddress( + std::make_shared("/pipe/path")); + socket.address_provider_->setRemoteAddress( + std::make_shared("/pipe/path")); EXPECT_TRUE(Utility::isSameIpOrLoopback(socket)); - local_addr = std::make_shared("127.0.0.1"); - remote_addr = std::make_shared("127.0.0.1"); + socket.address_provider_->setLocalAddress( + std::make_shared("127.0.0.1")); + socket.address_provider_->setRemoteAddress( + std::make_shared("127.0.0.1")); EXPECT_TRUE(Utility::isSameIpOrLoopback(socket)); - local_addr = std::make_shared("127.0.0.2"); + socket.address_provider_->setLocalAddress( + std::make_shared("127.0.0.2")); EXPECT_TRUE(Utility::isSameIpOrLoopback(socket)); - local_addr = std::make_shared("4.4.4.4"); - remote_addr = std::make_shared("8.8.8.8"); + socket.address_provider_->setLocalAddress( + std::make_shared("4.4.4.4")); + socket.address_provider_->setRemoteAddress( + std::make_shared("8.8.8.8")); EXPECT_FALSE(Utility::isSameIpOrLoopback(socket)); - local_addr = std::make_shared("4.4.4.4"); - remote_addr = std::make_shared("4.4.4.4"); + socket.address_provider_->setLocalAddress( + std::make_shared("4.4.4.4")); + socket.address_provider_->setRemoteAddress( + std::make_shared("4.4.4.4")); EXPECT_TRUE(Utility::isSameIpOrLoopback(socket)); - local_addr = std::make_shared("4.4.4.4", 1234); - remote_addr = std::make_shared("4.4.4.4", 4321); + socket.address_provider_->setLocalAddress( + std::make_shared("4.4.4.4", 1234)); + socket.address_provider_->setRemoteAddress( + std::make_shared("4.4.4.4", 4321)); EXPECT_TRUE(Utility::isSameIpOrLoopback(socket)); - local_addr = std::make_shared("::1"); - remote_addr = std::make_shared("::1"); + socket.address_provider_->setLocalAddress( + std::make_shared("::1")); + socket.address_provider_->setRemoteAddress( + std::make_shared("::1")); EXPECT_TRUE(Utility::isSameIpOrLoopback(socket)); - local_addr = std::make_shared("::2"); - remote_addr = std::make_shared("::1"); + socket.address_provider_->setLocalAddress( + std::make_shared("::2")); + socket.address_provider_->setRemoteAddress( + std::make_shared("::1")); EXPECT_TRUE(Utility::isSameIpOrLoopback(socket)); - remote_addr = std::make_shared("::3"); + socket.address_provider_->setRemoteAddress( + std::make_shared("::3")); EXPECT_FALSE(Utility::isSameIpOrLoopback(socket)); - remote_addr = std::make_shared("::2"); + socket.address_provider_->setRemoteAddress( + std::make_shared("::2")); EXPECT_TRUE(Utility::isSameIpOrLoopback(socket)); - remote_addr = std::make_shared("::2", 4321); - local_addr = std::make_shared("::2", 1234); + socket.address_provider_->setRemoteAddress( + std::make_shared("::2", 4321)); + socket.address_provider_->setLocalAddress( + std::make_shared("::2", 1234)); EXPECT_TRUE(Utility::isSameIpOrLoopback(socket)); - remote_addr = std::make_shared("fd00::"); + socket.address_provider_->setRemoteAddress( + std::make_shared("fd00::")); EXPECT_FALSE(Utility::isSameIpOrLoopback(socket)); } diff --git a/test/common/stream_info/test_util.h b/test/common/stream_info/test_util.h index e42913a2d72e..88c73b8dd90d 100644 --- a/test/common/stream_info/test_util.h +++ b/test/common/stream_info/test_util.h @@ -6,6 +6,7 @@ #include "common/common/assert.h" #include "common/common/random_generator.h" #include "common/http/request_id_extension_impl.h" +#include "common/network/socket_impl.h" #include "common/stream_info/filter_state_impl.h" #include "test/test_common/simulated_time_system.h" @@ -74,24 +75,8 @@ class TestStreamInfo : public StreamInfo::StreamInfo { } bool healthCheck() const override { return health_check_request_; } void healthCheck(bool is_health_check) override { health_check_request_ = is_health_check; } - const Network::Address::InstanceConstSharedPtr& downstreamLocalAddress() const override { - return downstream_local_address_; - } - const Network::Address::InstanceConstSharedPtr& downstreamDirectRemoteAddress() const override { - return downstream_direct_remote_address_; - } - void setDownstreamRemoteAddress( - const Network::Address::InstanceConstSharedPtr& downstream_remote_address) override { - downstream_remote_address_ = downstream_remote_address; - } - const Network::Address::InstanceConstSharedPtr& downstreamRemoteAddress() const override { - return downstream_remote_address_; - } - void - setDownstreamAddresses(const Network::ConnectedSocketAddressProvider& address_provider) override { - downstream_local_address_ = address_provider.localAddress(); - downstream_direct_remote_address_ = address_provider.directRemoteAddress(); - downstream_remote_address_ = address_provider.remoteAddress(); + const Network::SocketAddressProvider& downstreamAddressProvider() const override { + return *downstream_address_provider_; } void setDownstreamSslConnection(const Ssl::ConnectionInfoConstSharedPtr& connection_info) override { @@ -256,9 +241,8 @@ class TestStreamInfo : public StreamInfo::StreamInfo { bool health_check_request_{}; std::string route_name_; Network::Address::InstanceConstSharedPtr upstream_local_address_; - Network::Address::InstanceConstSharedPtr downstream_local_address_; - Network::Address::InstanceConstSharedPtr downstream_direct_remote_address_; - Network::Address::InstanceConstSharedPtr downstream_remote_address_; + Network::SocketAddressProviderSharedPtr downstream_address_provider_{ + std::make_shared(nullptr, nullptr)}; Ssl::ConnectionInfoConstSharedPtr downstream_connection_info_; Ssl::ConnectionInfoConstSharedPtr upstream_connection_info_; const Router::RouteEntry* route_entry_{}; diff --git a/test/common/tcp_proxy/tcp_proxy_test.cc b/test/common/tcp_proxy/tcp_proxy_test.cc index ac3bc5ae8f37..88e992faca2d 100644 --- a/test/common/tcp_proxy/tcp_proxy_test.cc +++ b/test/common/tcp_proxy/tcp_proxy_test.cc @@ -208,7 +208,8 @@ TEST(ConfigTest, DEPRECATED_FEATURE_TEST(Routes)) { { // hit route with destination_ip (10.10.10.10/32) NiceMock connection; - connection.local_address_ = std::make_shared("10.10.10.10"); + connection.stream_info_.downstream_address_provider_->setLocalAddress( + std::make_shared("10.10.10.10")); EXPECT_EQ(std::string("with_destination_ip_list"), config_obj.getRouteFromEntries(connection)->clusterName()); } @@ -216,15 +217,18 @@ TEST(ConfigTest, DEPRECATED_FEATURE_TEST(Routes)) { { // fall-through NiceMock connection; - connection.local_address_ = std::make_shared("10.10.10.11"); - connection.remote_address_ = std::make_shared("0.0.0.0"); + connection.stream_info_.downstream_address_provider_->setLocalAddress( + std::make_shared("10.10.10.11")); + connection.stream_info_.downstream_address_provider_->setRemoteAddress( + std::make_shared("0.0.0.0")); EXPECT_EQ(std::string("catch_all"), config_obj.getRouteFromEntries(connection)->clusterName()); } { // hit route with destination_ip (10.10.11.0/24) NiceMock connection; - connection.local_address_ = std::make_shared("10.10.11.11"); + connection.stream_info_.downstream_address_provider_->setLocalAddress( + std::make_shared("10.10.11.11")); EXPECT_EQ(std::string("with_destination_ip_list"), config_obj.getRouteFromEntries(connection)->clusterName()); } @@ -232,15 +236,18 @@ TEST(ConfigTest, DEPRECATED_FEATURE_TEST(Routes)) { { // fall-through NiceMock connection; - connection.local_address_ = std::make_shared("10.10.12.12"); - connection.remote_address_ = std::make_shared("0.0.0.0"); + connection.stream_info_.downstream_address_provider_->setLocalAddress( + std::make_shared("10.10.12.12")); + connection.stream_info_.downstream_address_provider_->setRemoteAddress( + std::make_shared("0.0.0.0")); EXPECT_EQ(std::string("catch_all"), config_obj.getRouteFromEntries(connection)->clusterName()); } { // hit route with destination_ip (10.11.0.0/16) NiceMock connection; - connection.local_address_ = std::make_shared("10.11.11.11"); + connection.stream_info_.downstream_address_provider_->setLocalAddress( + std::make_shared("10.11.11.11")); EXPECT_EQ(std::string("with_destination_ip_list"), config_obj.getRouteFromEntries(connection)->clusterName()); } @@ -248,15 +255,18 @@ TEST(ConfigTest, DEPRECATED_FEATURE_TEST(Routes)) { { // fall-through NiceMock connection; - connection.local_address_ = std::make_shared("10.12.12.12"); - connection.remote_address_ = std::make_shared("0.0.0.0"); + connection.stream_info_.downstream_address_provider_->setLocalAddress( + std::make_shared("10.12.12.12")); + connection.stream_info_.downstream_address_provider_->setRemoteAddress( + std::make_shared("0.0.0.0")); EXPECT_EQ(std::string("catch_all"), config_obj.getRouteFromEntries(connection)->clusterName()); } { // hit route with destination_ip (11.0.0.0/8) NiceMock connection; - connection.local_address_ = std::make_shared("11.11.11.11"); + connection.stream_info_.downstream_address_provider_->setLocalAddress( + std::make_shared("11.11.11.11")); EXPECT_EQ(std::string("with_destination_ip_list"), config_obj.getRouteFromEntries(connection)->clusterName()); } @@ -264,15 +274,18 @@ TEST(ConfigTest, DEPRECATED_FEATURE_TEST(Routes)) { { // fall-through NiceMock connection; - connection.local_address_ = std::make_shared("12.12.12.12"); - connection.remote_address_ = std::make_shared("0.0.0.0"); + connection.stream_info_.downstream_address_provider_->setLocalAddress( + std::make_shared("12.12.12.12")); + connection.stream_info_.downstream_address_provider_->setRemoteAddress( + std::make_shared("0.0.0.0")); EXPECT_EQ(std::string("catch_all"), config_obj.getRouteFromEntries(connection)->clusterName()); } { // hit route with destination_ip (128.0.0.0/8) NiceMock connection; - connection.local_address_ = std::make_shared("128.255.255.255"); + connection.stream_info_.downstream_address_provider_->setLocalAddress( + std::make_shared("128.255.255.255")); EXPECT_EQ(std::string("with_destination_ip_list"), config_obj.getRouteFromEntries(connection)->clusterName()); } @@ -280,7 +293,8 @@ TEST(ConfigTest, DEPRECATED_FEATURE_TEST(Routes)) { { // hit route with destination port range NiceMock connection; - connection.local_address_ = std::make_shared("1.2.3.4", 12345); + connection.stream_info_.downstream_address_provider_->setLocalAddress( + std::make_shared("1.2.3.4", 12345)); EXPECT_EQ(std::string("with_destination_ports"), config_obj.getRouteFromEntries(connection)->clusterName()); } @@ -288,16 +302,20 @@ TEST(ConfigTest, DEPRECATED_FEATURE_TEST(Routes)) { { // fall through NiceMock connection; - connection.local_address_ = std::make_shared("1.2.3.4", 23456); - connection.remote_address_ = std::make_shared("0.0.0.0"); + connection.stream_info_.downstream_address_provider_->setLocalAddress( + std::make_shared("1.2.3.4", 23456)); + connection.stream_info_.downstream_address_provider_->setRemoteAddress( + std::make_shared("0.0.0.0")); EXPECT_EQ(std::string("catch_all"), config_obj.getRouteFromEntries(connection)->clusterName()); } { // hit route with source port range NiceMock connection; - connection.local_address_ = std::make_shared("1.2.3.4", 23456); - connection.remote_address_ = std::make_shared("0.0.0.0", 23459); + connection.stream_info_.downstream_address_provider_->setLocalAddress( + std::make_shared("1.2.3.4", 23456)); + connection.stream_info_.downstream_address_provider_->setRemoteAddress( + std::make_shared("0.0.0.0", 23459)); EXPECT_EQ(std::string("with_source_ports"), config_obj.getRouteFromEntries(connection)->clusterName()); } @@ -305,17 +323,20 @@ TEST(ConfigTest, DEPRECATED_FEATURE_TEST(Routes)) { { // fall through NiceMock connection; - connection.local_address_ = std::make_shared("1.2.3.4", 23456); - connection.remote_address_ = std::make_shared("0.0.0.0", 23458); + connection.stream_info_.downstream_address_provider_->setLocalAddress( + std::make_shared("1.2.3.4", 23456)); + connection.stream_info_.downstream_address_provider_->setRemoteAddress( + std::make_shared("0.0.0.0", 23458)); EXPECT_EQ(std::string("catch_all"), config_obj.getRouteFromEntries(connection)->clusterName()); } { // hit the route with all criteria present NiceMock connection; - connection.local_address_ = std::make_shared("10.0.0.0", 10000); - connection.remote_address_ = - std::make_shared("20.0.0.0", 20000); + connection.stream_info_.downstream_address_provider_->setLocalAddress( + std::make_shared("10.0.0.0", 10000)); + connection.stream_info_.downstream_address_provider_->setRemoteAddress( + std::make_shared("20.0.0.0", 20000)); EXPECT_EQ(std::string("with_everything"), config_obj.getRouteFromEntries(connection)->clusterName()); } @@ -323,16 +344,18 @@ TEST(ConfigTest, DEPRECATED_FEATURE_TEST(Routes)) { { // fall through NiceMock connection; - connection.local_address_ = std::make_shared("10.0.0.0", 10000); - connection.remote_address_ = - std::make_shared("30.0.0.0", 20000); + connection.stream_info_.downstream_address_provider_->setLocalAddress( + std::make_shared("10.0.0.0", 10000)); + connection.stream_info_.downstream_address_provider_->setRemoteAddress( + std::make_shared("30.0.0.0", 20000)); EXPECT_EQ(std::string("catch_all"), config_obj.getRouteFromEntries(connection)->clusterName()); } { // hit route with destination_ip (::1/128) NiceMock connection; - connection.local_address_ = std::make_shared("::1"); + connection.stream_info_.downstream_address_provider_->setLocalAddress( + std::make_shared("::1")); EXPECT_EQ(std::string("with_v6_destination"), config_obj.getRouteFromEntries(connection)->clusterName()); } @@ -340,8 +363,8 @@ TEST(ConfigTest, DEPRECATED_FEATURE_TEST(Routes)) { { // hit route with destination_ip ("2001:abcd/64") NiceMock connection; - connection.local_address_ = - std::make_shared("2001:abcd:0:0:1::"); + connection.stream_info_.downstream_address_provider_->setLocalAddress( + std::make_shared("2001:abcd:0:0:1::")); EXPECT_EQ(std::string("with_v6_destination"), config_obj.getRouteFromEntries(connection)->clusterName()); } @@ -349,9 +372,9 @@ TEST(ConfigTest, DEPRECATED_FEATURE_TEST(Routes)) { { // hit route with destination_ip ("2002::/32") and source_ip ("2003::/64") NiceMock connection; - connection.local_address_ = - std::make_shared("2002:0:0:0:0:0::1"); - connection.remote_address_ = + connection.stream_info_.downstream_address_provider_->setLocalAddress( + std::make_shared("2002:0:0:0:0:0::1")); + connection.stream_info_.downstream_address_provider_->setRemoteAddress( std::make_shared("2003:0:0:0:0::5"); EXPECT_EQ(std::string("with_v6_source_and_destination"), config_obj.getRouteFromEntries(connection)->clusterName()); @@ -360,8 +383,10 @@ TEST(ConfigTest, DEPRECATED_FEATURE_TEST(Routes)) { { // fall through NiceMock connection; - connection.local_address_ = std::make_shared("2004::"); - connection.remote_address_ = std::make_shared("::"); + connection.stream_info_.downstream_address_provider_->setLocalAddress( + std::make_shared("2004::")); + connection.stream_info_.downstream_address_provider_->setRemoteAddress( + std::make_shared("::")); EXPECT_EQ(std::string("catch_all"), config_obj.getRouteFromEntries(connection)->clusterName()); } } diff --git a/test/fuzz/utility.h b/test/fuzz/utility.h index 332e6ae01fce..4962fe2d782a 100644 --- a/test/fuzz/utility.h +++ b/test/fuzz/utility.h @@ -4,6 +4,7 @@ #include "common/common/empty_string.h" #include "common/network/resolver_impl.h" +#include "common/network/socket_impl.h" #include "common/network/utility.h" #include "test/common/stream_info/test_util.h" @@ -165,9 +166,8 @@ inline std::unique_ptr fromStreamInfo(const test::fuzz::StreamIn ? Envoy::Network::Address::resolveProtoAddress(stream_info.upstream_local_address()) : Network::Utility::resolveUrl("tcp://10.0.0.1:10000"); test_stream_info->upstream_local_address_ = upstream_local_address; - test_stream_info->downstream_local_address_ = address; - test_stream_info->downstream_direct_remote_address_ = address; - test_stream_info->downstream_remote_address_ = address; + test_stream_info->downstream_address_provider_ = + std::make_shared(address, address); auto connection_info = std::make_shared>(); ON_CALL(*connection_info, subjectPeerCertificate()) .WillByDefault(testing::ReturnRef(TestSubjectPeer)); diff --git a/test/mocks/network/connection.cc b/test/mocks/network/connection.cc index a0fe1bca4612..ad6327d58d88 100644 --- a/test/mocks/network/connection.cc +++ b/test/mocks/network/connection.cc @@ -54,6 +54,8 @@ void MockConnectionBase::runLowWatermarkCallbacks() { } template static void initializeMockConnection(T& connection) { + ON_CALL(connection, addressProvider()) + .WillByDefault(ReturnPointee(connection.stream_info_.downstream_address_provider_)); ON_CALL(connection, dispatcher()).WillByDefault(ReturnRef(connection.dispatcher_)); ON_CALL(connection, readEnabled()).WillByDefault(ReturnPointee(&connection.read_enabled_)); ON_CALL(connection, addConnectionCallbacks(_)) @@ -109,7 +111,7 @@ MockClientConnection::MockClientConnection() { stream_info_.downstream_address_provider_->setRemoteAddress( Utility::resolveUrl("tcp://10.0.0.1:443")); stream_info_.downstream_address_provider_->setLocalAddress( - Utility::resolveUrl("tcp://10.0.0.2:40000")); + Utility::resolveUrl("tcp://10.0.0.2:40000")); initializeMockConnection(*this); } diff --git a/test/mocks/network/connection.h b/test/mocks/network/connection.h index 2fcc5eb8b701..8e1b1e1012ce 100644 --- a/test/mocks/network/connection.h +++ b/test/mocks/network/connection.h @@ -65,6 +65,7 @@ class MockConnectionBase { MOCK_METHOD(void, detectEarlyCloseWhenReadDisabled, (bool)); \ MOCK_METHOD(bool, readEnabled, (), (const)); \ MOCK_METHOD(const SocketAddressProvider&, addressProvider, (), (const)); \ + MOCK_METHOD(SocketAddressProviderConstSharedPtr, addressProviderSharedPtr, (), (const)); \ MOCK_METHOD(absl::optional, \ unixSocketPeerCredentials, (), (const)); \ MOCK_METHOD(void, setConnectionStats, (const ConnectionStats& stats)); \ @@ -126,48 +127,7 @@ class MockFilterManagerConnection : public FilterManagerConnection, public MockC MockFilterManagerConnection(); ~MockFilterManagerConnection() override; - // Network::Connection - MOCK_METHOD(void, addConnectionCallbacks, (ConnectionCallbacks & cb)); - MOCK_METHOD(void, removeConnectionCallbacks, (ConnectionCallbacks & cb)); - MOCK_METHOD(void, addBytesSentCallback, (BytesSentCb cb)); - MOCK_METHOD(void, addWriteFilter, (WriteFilterSharedPtr filter)); - MOCK_METHOD(void, addFilter, (FilterSharedPtr filter)); - MOCK_METHOD(void, addReadFilter, (ReadFilterSharedPtr filter)); - MOCK_METHOD(void, removeReadFilter, (ReadFilterSharedPtr filter)); - MOCK_METHOD(void, enableHalfClose, (bool enabled)); - MOCK_METHOD(bool, isHalfCloseEnabled, ()); - MOCK_METHOD(void, close, (ConnectionCloseType type)); - MOCK_METHOD(Event::Dispatcher&, dispatcher, ()); - MOCK_METHOD(uint64_t, id, (), (const)); - MOCK_METHOD(void, hashKey, (std::vector&), (const)); - MOCK_METHOD(bool, initializeReadFilters, ()); - MOCK_METHOD(std::string, nextProtocol, (), (const)); - MOCK_METHOD(void, noDelay, (bool enable)); - MOCK_METHOD(void, readDisable, (bool disable)); - MOCK_METHOD(void, detectEarlyCloseWhenReadDisabled, (bool)); - MOCK_METHOD(bool, readEnabled, (), (const)); - MOCK_METHOD(const Address::InstanceConstSharedPtr&, remoteAddress, (), (const)); - MOCK_METHOD(const Address::InstanceConstSharedPtr&, directRemoteAddress, (), (const)); - MOCK_METHOD(absl::optional, - unixSocketPeerCredentials, (), (const)); - MOCK_METHOD(const Address::InstanceConstSharedPtr&, localAddress, (), (const)); - MOCK_METHOD(void, setConnectionStats, (const ConnectionStats& stats)); - MOCK_METHOD(Ssl::ConnectionInfoConstSharedPtr, ssl, (), (const)); - MOCK_METHOD(absl::string_view, requestedServerName, (), (const)); - MOCK_METHOD(State, state, (), (const)); - MOCK_METHOD(bool, connecting, (), (const)); - MOCK_METHOD(void, write, (Buffer::Instance & data, bool end_stream)); - MOCK_METHOD(void, setBufferLimits, (uint32_t limit)); - MOCK_METHOD(uint32_t, bufferLimit, (), (const)); - MOCK_METHOD(bool, localAddressRestored, (), (const)); - MOCK_METHOD(bool, aboveHighWatermark, (), (const)); - MOCK_METHOD(const Network::ConnectionSocket::OptionsSharedPtr&, socketOptions, (), (const)); - MOCK_METHOD(StreamInfo::StreamInfo&, streamInfo, ()); - MOCK_METHOD(const StreamInfo::StreamInfo&, streamInfo, (), (const)); - MOCK_METHOD(void, setDelayedCloseTimeout, (std::chrono::milliseconds)); - MOCK_METHOD(absl::string_view, transportFailureReason, (), (const)); - MOCK_METHOD(bool, startSecureTransport, ()); - MOCK_METHOD(absl::optional, lastRoundTripTime, (), (const)); + DEFINE_MOCK_CONNECTION_MOCK_METHODS; // Network::FilterManagerConnection MOCK_METHOD(StreamBuffer, getReadBuffer, ()); diff --git a/test/mocks/network/mocks.h b/test/mocks/network/mocks.h index 7dadc4dfdb0c..82a3d0890cbf 100644 --- a/test/mocks/network/mocks.h +++ b/test/mocks/network/mocks.h @@ -223,9 +223,11 @@ class MockListenSocket : public Socket { void addOption(const Socket::OptionConstSharedPtr& option) override { addOption_(option); } void addOptions(const Socket::OptionsSharedPtr& options) override { addOptions_(options); } - SocketAddressProvider& addressProvider() override; - const SocketAddressProvider& addressProvider() const override; - SocketAddressProviderConstSharedPtr addressProviderSharedPtr() const override; + SocketAddressProvider& addressProvider() override { return *address_provider_; } + const SocketAddressProvider& addressProvider() const override { return *address_provider_; } + SocketAddressProviderConstSharedPtr addressProviderSharedPtr() const override { + return address_provider_; + } MOCK_METHOD(IoHandle&, ioHandle, ()); MOCK_METHOD(SocketPtr, duplicate, ()); MOCK_METHOD(const IoHandle&, ioHandle, (), (const)); @@ -273,9 +275,11 @@ class MockConnectionSocket : public ConnectionSocket { void addOption(const Socket::OptionConstSharedPtr& option) override { addOption_(option); } void addOptions(const Socket::OptionsSharedPtr& options) override { addOptions_(options); } - SocketAddressProvider& addressProvider() override; - const SocketAddressProvider& addressProvider() const override; - SocketAddressProviderConstSharedPtr addressProviderSharedPtr() const override; + SocketAddressProvider& addressProvider() override { return *address_provider_; } + const SocketAddressProvider& addressProvider() const override { return *address_provider_; } + SocketAddressProviderConstSharedPtr addressProviderSharedPtr() const override { + return address_provider_; + } MOCK_METHOD(void, setDetectedTransportProtocol, (absl::string_view)); MOCK_METHOD(absl::string_view, detectedTransportProtocol, (), (const)); MOCK_METHOD(void, setRequestedApplicationProtocols, (const std::vector&)); diff --git a/test/mocks/network/socket.h b/test/mocks/network/socket.h index d6ca57d3bb8d..f7db519ff5d4 100644 --- a/test/mocks/network/socket.h +++ b/test/mocks/network/socket.h @@ -14,9 +14,11 @@ class MockSocket : public Socket { MockSocket(); ~MockSocket() override; - SocketAddressProvider& addressProvider() override; - const SocketAddressProvider& addressProvider() const override; - SocketAddressProviderConstSharedPtr addressProviderSharedPtr() const override; + SocketAddressProvider& addressProvider() override { return *address_provider_; } + const SocketAddressProvider& addressProvider() const override { return *address_provider_; } + SocketAddressProviderConstSharedPtr addressProviderSharedPtr() const override { + return address_provider_; + } IoHandle& ioHandle() override; const IoHandle& ioHandle() const override; Api::SysCallIntResult setSocketOption(int level, int optname, const void* optval, @@ -39,6 +41,7 @@ class MockSocket : public Socket { MOCK_METHOD(void, addOptions, (const Socket::OptionsSharedPtr&), (override)); const std::unique_ptr io_handle_; + Network::SocketAddressProviderSharedPtr address_provider_; }; } // namespace Network diff --git a/test/mocks/stream_info/mocks.h b/test/mocks/stream_info/mocks.h index 5a75e0583c62..c3f319f674de 100644 --- a/test/mocks/stream_info/mocks.h +++ b/test/mocks/stream_info/mocks.h @@ -2,7 +2,6 @@ #include "envoy/config/core/v3/base.pb.h" #include "envoy/http/request_id_extension.h" -#include "envoy/network/listen_socket.h" #include "envoy/network/socket.h" #include "envoy/stream_info/stream_info.h"