Skip to content

Commit

Permalink
[socket] PassthroughSocket: Add implementation for connect. (envoyp…
Browse files Browse the repository at this point in the history
…roxy#21678)

Socket extensions like proxy_protocol socket extends PassthroughSocket which currently does not
delegate the connect call to the wrapped socket. This renders the wrapped sockets unable to
intercept connect.

This change fixes this problem by adding an override for connect method in PassthroughSocket.

Risk Level: Low
Testing: Unit tests, Manual
Docs Changes: N/A
Release Notes: N/A
Platform Specific Features: N/A

Signed-off-by: Jojy George Varghese <[email protected]>
  • Loading branch information
conqerAtapple authored Jun 15, 2022
1 parent 18c2d5a commit 83c4192
Show file tree
Hide file tree
Showing 6 changed files with 24 additions and 0 deletions.
4 changes: 4 additions & 0 deletions source/extensions/transport_sockets/common/passthrough.cc
Original file line number Diff line number Diff line change
Expand Up @@ -24,6 +24,10 @@ absl::string_view PassthroughSocket::failureReason() const {

bool PassthroughSocket::canFlushClose() { return transport_socket_->canFlushClose(); }

Api::SysCallIntResult PassthroughSocket::connect(Network::ConnectionSocket& socket) {
return transport_socket_->connect(socket);
}

void PassthroughSocket::closeSocket(Network::ConnectionEvent event) {
transport_socket_->closeSocket(event);
}
Expand Down
1 change: 1 addition & 0 deletions source/extensions/transport_sockets/common/passthrough.h
Original file line number Diff line number Diff line change
Expand Up @@ -38,6 +38,7 @@ class PassthroughSocket : public Network::TransportSocket {
std::string protocol() const override;
absl::string_view failureReason() const override;
bool canFlushClose() override;
Api::SysCallIntResult connect(Network::ConnectionSocket& socket) override;
void closeSocket(Network::ConnectionEvent event) override;
Network::IoResult doRead(Buffer::Instance& buffer) override;
Network::IoResult doWrite(Buffer::Instance& buffer, bool end_stream) override;
Expand Down
11 changes: 11 additions & 0 deletions test/extensions/transport_sockets/common/passthrough_test.cc
Original file line number Diff line number Diff line change
@@ -1,3 +1,4 @@
#include "source/common/network/io_socket_handle_impl.h"
#include "source/extensions/transport_sockets/common/passthrough.h"

#include "test/mocks/buffer/mocks.h"
Expand Down Expand Up @@ -45,6 +46,16 @@ TEST_F(PassthroughTest, FailureReasonDefersToInnerSocket) {
passthrough_socket_->failureReason();
}

// Test connect method defers to inner socket
TEST_F(PassthroughTest, ConnectDefersToInnerSocket) {
auto io_handle = std::make_unique<Network::IoSocketHandleImpl>();
Network::ConnectionSocketImpl socket(std::move(io_handle), nullptr, nullptr);
ON_CALL(*inner_socket_, connect(_)).WillByDefault(testing::Return(Api::SysCallIntResult{0, 0}));

EXPECT_CALL(*inner_socket_, connect(testing::Ref(socket)));
passthrough_socket_->connect(socket);
}

// Test canFlushClose method defers to inner socket
TEST_F(PassthroughTest, CanFlushCloseDefersToInnerSocket) {
EXPECT_CALL(*inner_socket_, canFlushClose());
Expand Down
2 changes: 2 additions & 0 deletions test/mocks/network/BUILD
Original file line number Diff line number Diff line change
Expand Up @@ -74,7 +74,9 @@ envoy_cc_mock(
srcs = ["transport_socket.cc"],
hdrs = ["transport_socket.h"],
deps = [
"//envoy/network:io_handle_interface",
"//envoy/network:transport_socket_interface",
"//source/common/network:listen_socket_lib",
"//source/common/network:utility_lib",
],
)
3 changes: 3 additions & 0 deletions test/mocks/network/transport_socket.cc
Original file line number Diff line number Diff line change
Expand Up @@ -16,6 +16,9 @@ namespace Network {
MockTransportSocket::MockTransportSocket() {
ON_CALL(*this, setTransportSocketCallbacks(_))
.WillByDefault(Invoke([&](TransportSocketCallbacks& callbacks) { callbacks_ = &callbacks; }));
ON_CALL(*this, connect(_)).WillByDefault(Invoke([&](Network::ConnectionSocket& socket) {
return TransportSocket::connect(socket);
}));
}
MockTransportSocket::~MockTransportSocket() = default;

Expand Down
3 changes: 3 additions & 0 deletions test/mocks/network/transport_socket.h
Original file line number Diff line number Diff line change
Expand Up @@ -7,6 +7,8 @@

#include "envoy/network/transport_socket.h"

#include "source/common/network/listen_socket_impl.h"

#include "gmock/gmock.h"

namespace Envoy {
Expand All @@ -21,6 +23,7 @@ class MockTransportSocket : public TransportSocket {
MOCK_METHOD(std::string, protocol, (), (const));
MOCK_METHOD(absl::string_view, failureReason, (), (const));
MOCK_METHOD(bool, canFlushClose, ());
MOCK_METHOD(Api::SysCallIntResult, connect, (Network::ConnectionSocket & socket));
MOCK_METHOD(void, closeSocket, (Network::ConnectionEvent event));
MOCK_METHOD(IoResult, doRead, (Buffer::Instance & buffer));
MOCK_METHOD(IoResult, doWrite, (Buffer::Instance & buffer, bool end_stream));
Expand Down

0 comments on commit 83c4192

Please sign in to comment.