forked from envoyproxy/envoy
-
Notifications
You must be signed in to change notification settings - Fork 1
/
Copy pathsocket_interface_integration_test.cc
202 lines (170 loc) · 8.12 KB
/
socket_interface_integration_test.cc
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
#include "source/common/buffer/buffer_impl.h"
#include "source/common/network/address_impl.h"
#include "source/common/network/socket_interface.h"
#include "test/integration/integration.h"
#include "test/test_common/environment.h"
#include "test/test_common/utility.h"
#include "gtest/gtest.h"
#ifdef __linux__
#include "source/common/io/io_uring_impl.h"
#endif
namespace Envoy {
namespace {
class SocketInterfaceIntegrationTest : public BaseIntegrationTest,
public testing::TestWithParam<Network::Address::IpVersion> {
public:
SocketInterfaceIntegrationTest(bool enable_io_uring = false)
: BaseIntegrationTest(GetParam(), config(enable_io_uring)) {
use_lds_ = false;
};
static std::string config(bool enable_io_uring = false) {
// At least one empty filter chain needs to be specified.
return absl::StrCat(echoConfig(),
absl::StrFormat(R"EOF(
bootstrap_extensions:
- name: envoy.extensions.network.socket_interface.default_socket_interface
typed_config:
"@type": type.googleapis.com/envoy.extensions.network.socket_interface.v3.DefaultSocketInterface
%s
default_socket_interface: "envoy.extensions.network.socket_interface.default_socket_interface"
)EOF",
enable_io_uring ? "io_uring_options: {}" : ""));
}
static std::string echoConfig() {
return absl::StrCat(ConfigHelper::baseConfig(), R"EOF(
filter_chains:
filters:
name: ratelimit
typed_config:
"@type": type.googleapis.com/envoy.config.filter.network.rate_limit.v2.RateLimit
domain: foo
stats_prefix: name
descriptors: [{"key": "foo", "value": "bar"}]
filters:
name: envoy.filters.network.echo
typed_config:
"@type": type.googleapis.com/envoy.extensions.filters.network.echo.v3.Echo
)EOF");
}
};
INSTANTIATE_TEST_SUITE_P(IpVersions, SocketInterfaceIntegrationTest,
testing::ValuesIn(TestEnvironment::getIpVersionsForTest()),
TestUtility::ipTestParamsToString);
TEST_P(SocketInterfaceIntegrationTest, Basic) {
BaseIntegrationTest::initialize();
const Network::SocketInterface* factory = Network::socketInterface(
"envoy.extensions.network.socket_interface.default_socket_interface");
ASSERT_TRUE(Network::SocketInterfaceSingleton::getExisting() == factory);
std::string response;
auto connection = createConnectionDriver(
lookupPort("listener_0"), "hello",
[&response](Network::ClientConnection& conn, const Buffer::Instance& data) -> void {
response.append(data.toString());
conn.close(Network::ConnectionCloseType::FlushWrite);
});
ASSERT_TRUE(connection->run());
EXPECT_EQ("hello", response);
}
TEST_P(SocketInterfaceIntegrationTest, AddressWithSocketInterface) {
BaseIntegrationTest::initialize();
ConnectionStatusCallbacks connect_callbacks_;
Network::ClientConnectionPtr client_;
const Network::SocketInterface* sock_interface = Network::socketInterface(
"envoy.extensions.network.socket_interface.default_socket_interface");
Network::Address::InstanceConstSharedPtr address =
std::make_shared<Network::Address::Ipv4Instance>(
Network::Test::getLoopbackAddressUrlString(Network::Address::IpVersion::v4),
lookupPort("listener_0"), sock_interface);
client_ =
dispatcher_->createClientConnection(address, Network::Address::InstanceConstSharedPtr(),
Network::Test::createRawBufferSocket(), nullptr, nullptr);
client_->addConnectionCallbacks(connect_callbacks_);
client_->connect();
while (!connect_callbacks_.connected() && !connect_callbacks_.closed()) {
dispatcher_->run(Event::Dispatcher::RunType::NonBlock);
}
client_->close(Network::ConnectionCloseType::FlushWrite);
}
// Test that connecting to internal address will crash if the user space socket extension is not
// linked.
TEST_P(SocketInterfaceIntegrationTest, InternalAddressWithSocketInterface) {
BaseIntegrationTest::initialize();
ConnectionStatusCallbacks connect_callbacks_;
Network::ClientConnectionPtr client_;
const Network::SocketInterface* sock_interface = Network::socketInterface(
"envoy.extensions.network.socket_interface.default_socket_interface");
Network::Address::InstanceConstSharedPtr address =
std::make_shared<Network::Address::EnvoyInternalInstance>("listener_0", "endpoint_id_0",
sock_interface);
ASSERT_DEATH(client_ = dispatcher_->createClientConnection(
address, Network::Address::InstanceConstSharedPtr(),
Network::Test::createRawBufferSocket(), nullptr, nullptr),
"" /* Nullptr dereference */);
}
// Test that recv from internal address will crash.
// TODO(lambdai): Add UDP internal listener implementation to enable the io path.
TEST_P(SocketInterfaceIntegrationTest, UdpRecvFromInternalAddressWithSocketInterface) {
BaseIntegrationTest::initialize();
const Network::SocketInterface* sock_interface = Network::socketInterface(
"envoy.extensions.network.socket_interface.default_socket_interface");
Network::Address::InstanceConstSharedPtr address =
std::make_shared<Network::Address::EnvoyInternalInstance>("listener_0", "endpoint_id_0",
sock_interface);
ASSERT_DEATH(std::make_unique<Network::SocketImpl>(Network::Socket::Type::Datagram, address,
nullptr, Network::SocketCreationOptions{}),
"");
}
// Test that send to internal address will return io error.
TEST_P(SocketInterfaceIntegrationTest, UdpSendToInternalAddressWithSocketInterface) {
BaseIntegrationTest::initialize();
const Network::SocketInterface* sock_interface = Network::socketInterface(
"envoy.extensions.network.socket_interface.default_socket_interface");
Network::Address::InstanceConstSharedPtr peer_internal_address =
std::make_shared<Network::Address::EnvoyInternalInstance>("listener_0", "endpoint_id_0",
sock_interface);
Network::Address::InstanceConstSharedPtr local_valid_address =
Network::Test::getCanonicalLoopbackAddress(version_);
auto socket =
std::make_unique<Network::SocketImpl>(Network::Socket::Type::Datagram, local_valid_address,
nullptr, Network::SocketCreationOptions{});
Buffer::OwnedImpl buffer;
auto reservation = buffer.reserveSingleSlice(100);
auto slice = reservation.slice();
auto result =
socket->ioHandle().sendmsg(&slice, 1, 0, local_valid_address->ip(), *peer_internal_address);
ASSERT_FALSE(result.ok());
ASSERT_EQ(result.err_->getErrorCode(), Api::IoError::IoErrorCode::NoSupport);
}
#ifdef __linux__
class IoUringSocketInterfaceIntegrationTest : public SocketInterfaceIntegrationTest {
public:
IoUringSocketInterfaceIntegrationTest()
: SocketInterfaceIntegrationTest(true), should_skip_(!Io::isIoUringSupported()) {}
void SetUp() override {
if (should_skip_) {
GTEST_SKIP();
}
}
bool should_skip_{false};
};
INSTANTIATE_TEST_SUITE_P(IpVersions, IoUringSocketInterfaceIntegrationTest,
testing::ValuesIn(TestEnvironment::getIpVersionsForTest()),
TestUtility::ipTestParamsToString);
TEST_P(IoUringSocketInterfaceIntegrationTest, Basic) {
BaseIntegrationTest::initialize();
const Network::SocketInterface* factory = Network::socketInterface(
"envoy.extensions.network.socket_interface.default_socket_interface");
ASSERT_TRUE(Network::SocketInterfaceSingleton::getExisting() == factory);
std::string response;
auto connection = createConnectionDriver(
lookupPort("listener_0"), "hello",
[&response](Network::ClientConnection& conn, const Buffer::Instance& data) -> void {
response.append(data.toString());
conn.close(Network::ConnectionCloseType::FlushWrite);
});
ASSERT_TRUE(connection->run());
EXPECT_EQ("hello", response);
}
#endif
} // namespace
} // namespace Envoy