forked from envoyproxy/envoy
-
Notifications
You must be signed in to change notification settings - Fork 0
/
listener_impl_test.cc
480 lines (407 loc) · 21.5 KB
/
listener_impl_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
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
307
308
309
310
311
312
313
314
315
316
317
318
319
320
321
322
323
324
325
326
327
328
329
330
331
332
333
334
335
336
337
338
339
340
341
342
343
344
345
346
347
348
349
350
351
352
353
354
355
356
357
358
359
360
361
362
363
364
365
366
367
368
369
370
371
372
373
374
375
376
377
378
379
380
381
382
383
384
385
386
387
388
389
390
391
392
393
394
395
396
397
398
399
400
401
402
403
404
405
406
407
408
409
410
411
412
413
414
415
416
417
418
419
420
421
422
423
424
425
426
427
428
429
430
431
432
433
434
435
436
437
438
439
440
441
442
443
444
445
446
447
448
449
450
451
452
453
454
455
456
457
458
459
460
461
462
463
464
465
466
467
468
469
470
471
472
473
474
475
476
477
478
479
480
#include <limits>
#include "envoy/config/core/v3/base.pb.h"
#include "envoy/network/exception.h"
#include "common/network/address_impl.h"
#include "common/network/tcp_listener_impl.h"
#include "common/network/utility.h"
#include "common/stream_info/stream_info_impl.h"
#include "test/common/network/listener_impl_test_base.h"
#include "test/mocks/network/mocks.h"
#include "test/test_common/environment.h"
#include "test/test_common/network_utility.h"
#include "test/test_common/test_runtime.h"
#include "test/test_common/utility.h"
#include "gmock/gmock.h"
#include "gtest/gtest.h"
using testing::_;
using testing::Invoke;
using testing::Return;
namespace Envoy {
namespace Network {
namespace {
static void errorCallbackTest(Address::IpVersion version) {
// Force the error callback to fire by closing the socket under the listener. We run this entire
// test in the forked process to avoid confusion when the fork happens.
Api::ApiPtr api = Api::createApiForTest();
Event::DispatcherPtr dispatcher(api->allocateDispatcher("test_thread"));
auto socket = std::make_shared<Network::TcpListenSocket>(
Network::Test::getCanonicalLoopbackAddress(version), nullptr, true);
Network::MockTcpListenerCallbacks listener_callbacks;
Network::MockConnectionHandler connection_handler;
Network::ListenerPtr listener =
dispatcher->createListener(socket, listener_callbacks, true, ENVOY_TCP_BACKLOG_SIZE);
Network::ClientConnectionPtr client_connection = dispatcher->createClientConnection(
socket->addressProvider().localAddress(), Network::Address::InstanceConstSharedPtr(),
Network::Test::createRawBufferSocket(), nullptr);
client_connection->connect();
StreamInfo::StreamInfoImpl stream_info(dispatcher->timeSource(), nullptr);
EXPECT_CALL(listener_callbacks, onAccept_(_))
.WillOnce(Invoke([&](Network::ConnectionSocketPtr& accepted_socket) -> void {
Network::ConnectionPtr conn = dispatcher->createServerConnection(
std::move(accepted_socket), Network::Test::createRawBufferSocket(), stream_info);
client_connection->close(ConnectionCloseType::NoFlush);
conn->close(ConnectionCloseType::NoFlush);
socket->close();
}));
dispatcher->run(Event::Dispatcher::RunType::Block);
}
class ListenerImplDeathTest : public testing::TestWithParam<Address::IpVersion> {};
INSTANTIATE_TEST_SUITE_P(IpVersions, ListenerImplDeathTest,
testing::ValuesIn(TestEnvironment::getIpVersionsForTest()),
TestUtility::ipTestParamsToString);
TEST_P(ListenerImplDeathTest, ErrorCallback) {
EXPECT_DEATH(errorCallbackTest(GetParam()), ".*listener accept failure.*");
}
class TestTcpListenerImpl : public TcpListenerImpl {
public:
TestTcpListenerImpl(Event::DispatcherImpl& dispatcher, Random::RandomGenerator& random_generator,
SocketSharedPtr socket, TcpListenerCallbacks& cb, bool bind_to_port,
uint32_t tcp_backlog = ENVOY_TCP_BACKLOG_SIZE)
: TcpListenerImpl(dispatcher, random_generator, std::move(socket), cb, bind_to_port,
tcp_backlog) {}
MOCK_METHOD(Address::InstanceConstSharedPtr, getLocalAddress, (os_fd_t fd));
};
using TcpListenerImplTest = ListenerImplTestBase;
INSTANTIATE_TEST_SUITE_P(IpVersions, TcpListenerImplTest,
testing::ValuesIn(TestEnvironment::getIpVersionsForTest()),
TestUtility::ipTestParamsToString);
// Test that socket options are set after the listener is setup.
TEST_P(TcpListenerImplTest, SetListeningSocketOptionsSuccess) {
Network::MockTcpListenerCallbacks listener_callbacks;
Network::MockConnectionHandler connection_handler;
Random::MockRandomGenerator random_generator;
auto socket = std::make_shared<TcpListenSocket>(
Network::Test::getCanonicalLoopbackAddress(version_), nullptr, true);
std::shared_ptr<MockSocketOption> option = std::make_shared<MockSocketOption>();
socket->addOption(option);
EXPECT_CALL(*option, setOption(_, envoy::config::core::v3::SocketOption::STATE_LISTENING))
.WillOnce(Return(true));
TestTcpListenerImpl listener(dispatcherImpl(), random_generator, socket, listener_callbacks,
true);
}
// Test that an exception is thrown if there is an error setting socket options.
TEST_P(TcpListenerImplTest, SetListeningSocketOptionsError) {
Network::MockTcpListenerCallbacks listener_callbacks;
Network::MockConnectionHandler connection_handler;
Random::MockRandomGenerator random_generator;
auto socket = std::make_shared<TcpListenSocket>(
Network::Test::getCanonicalLoopbackAddress(version_), nullptr, true);
std::shared_ptr<MockSocketOption> option = std::make_shared<MockSocketOption>();
socket->addOption(option);
EXPECT_CALL(*option, setOption(_, envoy::config::core::v3::SocketOption::STATE_LISTENING))
.WillOnce(Return(false));
EXPECT_THROW_WITH_MESSAGE(
TestTcpListenerImpl(dispatcherImpl(), random_generator, socket, listener_callbacks, true),
CreateListenerException,
fmt::format("cannot set post-listen socket option on socket: {}",
socket->addressProvider().localAddress()->asString()));
}
TEST_P(TcpListenerImplTest, UseActualDst) {
auto socket = std::make_shared<TcpListenSocket>(
Network::Test::getCanonicalLoopbackAddress(version_), nullptr, true);
auto socketDst = std::make_shared<TcpListenSocket>(alt_address_, nullptr, false);
Network::MockTcpListenerCallbacks listener_callbacks1;
Network::MockConnectionHandler connection_handler;
Random::MockRandomGenerator random_generator;
// Do not redirect since use_original_dst is false.
Network::TestTcpListenerImpl listener(dispatcherImpl(), random_generator, socket,
listener_callbacks1, true);
Network::MockTcpListenerCallbacks listener_callbacks2;
Network::TestTcpListenerImpl listenerDst(dispatcherImpl(), random_generator, socketDst,
listener_callbacks2, false);
Network::ClientConnectionPtr client_connection = dispatcher_->createClientConnection(
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(), 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->addressProvider().localAddress(),
*socket->addressProvider().localAddress());
client_connection->close(ConnectionCloseType::NoFlush);
conn->close(ConnectionCloseType::NoFlush);
dispatcher_->exit();
}));
dispatcher_->run(Event::Dispatcher::RunType::Block);
}
TEST_P(TcpListenerImplTest, GlobalConnectionLimitEnforcement) {
// Required to manipulate runtime values when there is no test server.
TestScopedRuntime scoped_runtime;
Runtime::LoaderSingleton::getExisting()->mergeValues(
{{"overload.global_downstream_max_connections", "2"}});
auto socket = std::make_shared<Network::TcpListenSocket>(
Network::Test::getCanonicalLoopbackAddress(version_), nullptr, true);
Network::MockTcpListenerCallbacks listener_callbacks;
Network::MockConnectionHandler connection_handler;
Network::ListenerPtr listener =
dispatcher_->createListener(socket, listener_callbacks, true, ENVOY_TCP_BACKLOG_SIZE);
std::vector<Network::ClientConnectionPtr> client_connections;
std::vector<Network::ConnectionPtr> server_connections;
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(
std::move(accepted_socket), Network::Test::createRawBufferSocket(), stream_info));
dispatcher_->exit();
}));
auto initiate_connections = [&](const int count) {
for (int i = 0; i < count; ++i) {
client_connections.emplace_back(dispatcher_->createClientConnection(
socket->addressProvider().localAddress(), Network::Address::InstanceConstSharedPtr(),
Network::Test::createRawBufferSocket(), nullptr));
client_connections.back()->connect();
}
};
initiate_connections(5);
EXPECT_CALL(listener_callbacks, onReject(TcpListenerCallbacks::RejectCause::GlobalCxLimit))
.Times(3);
dispatcher_->run(Event::Dispatcher::RunType::Block);
// We expect any server-side connections that get created to populate 'server_connections'.
EXPECT_EQ(2, server_connections.size());
// Let's increase the allowed connections and try sending more connections.
Runtime::LoaderSingleton::getExisting()->mergeValues(
{{"overload.global_downstream_max_connections", "3"}});
initiate_connections(5);
EXPECT_CALL(listener_callbacks, onReject(TcpListenerCallbacks::RejectCause::GlobalCxLimit))
.Times(4);
dispatcher_->run(Event::Dispatcher::RunType::Block);
EXPECT_EQ(3, server_connections.size());
// Clear the limit and verify there's no longer a limit.
Runtime::LoaderSingleton::getExisting()->mergeValues(
{{"overload.global_downstream_max_connections", ""}});
initiate_connections(10);
dispatcher_->run(Event::Dispatcher::RunType::Block);
EXPECT_EQ(13, server_connections.size());
for (const auto& conn : client_connections) {
conn->close(ConnectionCloseType::NoFlush);
}
for (const auto& conn : server_connections) {
conn->close(ConnectionCloseType::NoFlush);
}
Runtime::LoaderSingleton::getExisting()->mergeValues(
{{"overload.global_downstream_max_connections", ""}});
}
TEST_P(TcpListenerImplTest, WildcardListenerUseActualDst) {
auto socket = std::make_shared<TcpListenSocket>(
Network::Test::getCanonicalLoopbackAddress(version_), nullptr, true);
Network::MockTcpListenerCallbacks listener_callbacks;
Network::MockConnectionHandler connection_handler;
Random::MockRandomGenerator random_generator;
// Do not redirect since use_original_dst is false.
Network::TestTcpListenerImpl listener(dispatcherImpl(), random_generator, socket,
listener_callbacks, true);
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(), 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->addressProvider().localAddress(), *local_dst_address);
client_connection->close(ConnectionCloseType::NoFlush);
conn->close(ConnectionCloseType::NoFlush);
dispatcher_->exit();
}));
dispatcher_->run(Event::Dispatcher::RunType::Block);
}
// Test for the correct behavior when a listener is configured with an ANY address that allows
// receiving IPv4 connections on an IPv6 socket. In this case the address instances of both
// local and remote addresses of the connection should be IPv4 instances, as the connection really
// is an IPv4 connection.
TEST_P(TcpListenerImplTest, WildcardListenerIpv4Compat) {
auto option = std::make_unique<MockSocketOption>();
auto options = std::make_shared<std::vector<Network::Socket::OptionConstSharedPtr>>();
EXPECT_CALL(*option, setOption(_, envoy::config::core::v3::SocketOption::STATE_PREBIND))
.WillOnce(Return(true));
options->emplace_back(std::move(option));
auto socket = std::make_shared<TcpListenSocket>(Network::Test::getAnyAddress(version_, true),
options, true);
Network::MockTcpListenerCallbacks listener_callbacks;
Network::MockConnectionHandler connection_handler;
Random::MockRandomGenerator random_generator;
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->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(), 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->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();
}));
dispatcher_->run(Event::Dispatcher::RunType::Block);
}
TEST_P(TcpListenerImplTest, DisableAndEnableListener) {
testing::InSequence s1;
auto socket = std::make_shared<TcpListenSocket>(
Network::Test::getCanonicalLoopbackAddress(version_), nullptr, true);
MockTcpListenerCallbacks listener_callbacks;
MockConnectionCallbacks connection_callbacks;
Random::MockRandomGenerator random_generator;
TestTcpListenerImpl listener(dispatcherImpl(), random_generator, socket, listener_callbacks,
true);
// When listener is disabled, the timer should fire before any connection is accepted.
listener.disable();
ClientConnectionPtr client_connection = dispatcher_->createClientConnection(
socket->addressProvider().localAddress(), Address::InstanceConstSharedPtr(),
Network::Test::createRawBufferSocket(), nullptr);
client_connection->addConnectionCallbacks(connection_callbacks);
client_connection->connect();
EXPECT_CALL(listener_callbacks, onAccept_(_)).Times(0);
EXPECT_CALL(connection_callbacks, onEvent(_))
.WillOnce(Invoke([&](Network::ConnectionEvent event) -> void {
EXPECT_EQ(event, Network::ConnectionEvent::Connected);
dispatcher_->exit();
}));
dispatcher_->run(Event::Dispatcher::RunType::Block);
// When the listener is re-enabled, the pending connection should be accepted.
listener.enable();
EXPECT_CALL(listener_callbacks, onAccept_(_)).WillOnce(Invoke([&](ConnectionSocketPtr&) -> void {
client_connection->close(ConnectionCloseType::NoFlush);
}));
EXPECT_CALL(connection_callbacks, onEvent(_))
.WillOnce(Invoke([&](Network::ConnectionEvent event) -> void {
EXPECT_NE(event, Network::ConnectionEvent::Connected);
dispatcher_->exit();
}));
dispatcher_->run(Event::Dispatcher::RunType::Block);
}
TEST_P(TcpListenerImplTest, SetListenerRejectFractionZero) {
auto socket = std::make_shared<TcpListenSocket>(
Network::Test::getCanonicalLoopbackAddress(version_), nullptr, true);
MockTcpListenerCallbacks listener_callbacks;
MockConnectionCallbacks connection_callbacks;
Random::MockRandomGenerator random_generator;
TestTcpListenerImpl listener(dispatcherImpl(), random_generator, socket, listener_callbacks,
true);
listener.setRejectFraction(UnitFloat(0));
// This connection will be accepted and not rejected.
{
testing::InSequence s1;
EXPECT_CALL(connection_callbacks, onEvent(ConnectionEvent::Connected));
EXPECT_CALL(connection_callbacks, onEvent(ConnectionEvent::LocalClose));
}
EXPECT_CALL(listener_callbacks, onAccept_(_)).WillOnce([&] { dispatcher_->exit(); });
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);
// Now that we've seen that the connection hasn't been closed by the listener, make sure to close
// it.
client_connection->close(ConnectionCloseType::NoFlush);
}
TEST_P(TcpListenerImplTest, SetListenerRejectFractionIntermediate) {
auto socket = std::make_shared<TcpListenSocket>(
Network::Test::getCanonicalLoopbackAddress(version_), nullptr, true);
MockTcpListenerCallbacks listener_callbacks;
MockConnectionCallbacks connection_callbacks;
Random::MockRandomGenerator random_generator;
TestTcpListenerImpl listener(dispatcherImpl(), random_generator, socket, listener_callbacks,
true);
listener.setRejectFraction(UnitFloat(0.5f));
// The first connection will be rejected because the random value is too small.
{
testing::InSequence s1;
EXPECT_CALL(random_generator, random()).WillOnce(Return(0));
EXPECT_CALL(listener_callbacks, onReject(TcpListenerCallbacks::RejectCause::OverloadAction));
}
{
testing::InSequence s2;
EXPECT_CALL(connection_callbacks, onEvent(ConnectionEvent::Connected));
EXPECT_CALL(connection_callbacks, onEvent(ConnectionEvent::RemoteClose)).WillOnce([&] {
dispatcher_->exit();
});
}
{
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);
}
// The second connection rolls better on initiative and is accepted.
{
testing::InSequence s1;
EXPECT_CALL(random_generator, random()).WillOnce(Return(std::numeric_limits<uint64_t>::max()));
// Exiting dispatcher on client side connect event can cause a race, listener accept callback
// may not have been triggered, exit dispatcher here to prevent this.
EXPECT_CALL(listener_callbacks, onAccept_(_)).WillOnce([&] { dispatcher_->exit(); });
}
{
testing::InSequence s2;
EXPECT_CALL(connection_callbacks, onEvent(ConnectionEvent::Connected));
EXPECT_CALL(connection_callbacks, onEvent(ConnectionEvent::RemoteClose)).Times(0);
}
{
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);
EXPECT_CALL(connection_callbacks, onEvent(ConnectionEvent::LocalClose));
// Now that we've seen that the connection hasn't been closed by the listener, make sure to
// close it.
client_connection->close(ConnectionCloseType::NoFlush);
}
}
TEST_P(TcpListenerImplTest, SetListenerRejectFractionAll) {
auto socket = std::make_shared<TcpListenSocket>(
Network::Test::getCanonicalLoopbackAddress(version_), nullptr, true);
MockTcpListenerCallbacks listener_callbacks;
MockConnectionCallbacks connection_callbacks;
Random::MockRandomGenerator random_generator;
TestTcpListenerImpl listener(dispatcherImpl(), random_generator, socket, listener_callbacks,
true);
listener.setRejectFraction(UnitFloat(1));
{
testing::InSequence s1;
EXPECT_CALL(listener_callbacks, onReject(TcpListenerCallbacks::RejectCause::OverloadAction));
}
{
testing::InSequence s2;
EXPECT_CALL(connection_callbacks, onEvent(ConnectionEvent::Connected));
EXPECT_CALL(connection_callbacks, onEvent(ConnectionEvent::RemoteClose)).WillOnce([&] {
dispatcher_->exit();
});
}
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);
}
} // namespace
} // namespace Network
} // namespace Envoy