forked from envoyproxy/envoy
-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathsocket_interface_integration_test.cc
147 lines (123 loc) · 6 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
#include "common/buffer/buffer_impl.h"
#include "common/network/address_impl.h"
#include "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"
namespace Envoy {
namespace {
class SocketInterfaceIntegrationTest : public BaseIntegrationTest,
public testing::TestWithParam<Network::Address::IpVersion> {
public:
SocketInterfaceIntegrationTest() : BaseIntegrationTest(GetParam(), config()) {
use_lds_ = false;
};
static std::string config() {
// At least one empty filter chain needs to be specified.
return absl::StrCat(echoConfig(), 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
default_socket_interface: "envoy.extensions.network.socket_interface.default_socket_interface"
)EOF");
}
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
)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);
});
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);
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.
// TODO(lambdai): Add internal connection implementation to enable the connection creation.
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", sock_interface);
ASSERT_DEATH(client_ = dispatcher_->createClientConnection(
address, Network::Address::InstanceConstSharedPtr(),
Network::Test::createRawBufferSocket(), nullptr),
"panic: not implemented");
}
// Test that recv from internal address will crash.
// TODO(lambdai): Add internal socket 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", sock_interface);
ASSERT_DEATH(std::make_unique<Network::SocketImpl>(Network::Socket::Type::Datagram, address), "");
}
// 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", 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);
Buffer::OwnedImpl buffer;
Buffer::RawSlice iovec;
buffer.reserve(100, &iovec, 1);
auto result =
socket->ioHandle().sendmsg(&iovec, 1, 0, local_valid_address->ip(), *peer_internal_address);
ASSERT_FALSE(result.ok());
ASSERT_EQ(result.err_->getErrorCode(), Api::IoError::IoErrorCode::NoSupport);
}
} // namespace
} // namespace Envoy