/
proxy_protocol_test.cc
2258 lines (1970 loc) · 95.8 KB
/
proxy_protocol_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
481
482
483
484
485
486
487
488
489
490
491
492
493
494
495
496
497
498
499
500
501
502
503
504
505
506
507
508
509
510
511
512
513
514
515
516
517
518
519
520
521
522
523
524
525
526
527
528
529
530
531
532
533
534
535
536
537
538
539
540
541
542
543
544
545
546
547
548
549
550
551
552
553
554
555
556
557
558
559
560
561
562
563
564
565
566
567
568
569
570
571
572
573
574
575
576
577
578
579
580
581
582
583
584
585
586
587
588
589
590
591
592
593
594
595
596
597
598
599
600
601
602
603
604
605
606
607
608
609
610
611
612
613
614
615
616
617
618
619
620
621
622
623
624
625
626
627
628
629
630
631
632
633
634
635
636
637
638
639
640
641
642
643
644
645
646
647
648
649
650
651
652
653
654
655
656
657
658
659
660
661
662
663
664
665
666
667
668
669
670
671
672
673
674
675
676
677
678
679
680
681
682
683
684
685
686
687
688
689
690
691
692
693
694
695
696
697
698
699
700
701
702
703
704
705
706
707
708
709
710
711
712
713
714
715
716
717
718
719
720
721
722
723
724
725
726
727
728
729
730
731
732
733
734
735
736
737
738
739
740
741
742
743
744
745
746
747
748
749
750
751
752
753
754
755
756
757
758
759
760
761
762
763
764
765
766
767
768
769
770
771
772
773
774
775
776
777
778
779
780
781
782
783
784
785
786
787
788
789
790
791
792
793
794
795
796
797
798
799
800
801
802
803
804
805
806
807
808
809
810
811
812
813
814
815
816
817
818
819
820
821
822
823
824
825
826
827
828
829
830
831
832
833
834
835
836
837
838
839
840
841
842
843
844
845
846
847
848
849
850
851
852
853
854
855
856
857
858
859
860
861
862
863
864
865
866
867
868
869
870
871
872
873
874
875
876
877
878
879
880
881
882
883
884
885
886
887
888
889
890
891
892
893
894
895
896
897
898
899
900
901
902
903
904
905
906
907
908
909
910
911
912
913
914
915
916
917
918
919
920
921
922
923
924
925
926
927
928
929
930
931
932
933
934
935
936
937
938
939
940
941
942
943
944
945
946
947
948
949
950
951
952
953
954
955
956
957
958
959
960
961
962
963
964
965
966
967
968
969
970
971
972
973
974
975
976
977
978
979
980
981
982
983
984
985
986
987
988
989
990
991
992
993
994
995
996
997
998
999
1000
#include <functional>
#include <memory>
#include <string>
#include "envoy/common/platform.h"
#include "envoy/config/core/v3/base.pb.h"
#include "envoy/config/core/v3/proxy_protocol.pb.h"
#include "envoy/stats/scope.h"
#include "source/common/api/os_sys_calls_impl.h"
#include "source/common/buffer/buffer_impl.h"
#include "source/common/event/dispatcher_impl.h"
#include "source/common/listener_manager/connection_handler_impl.h"
#include "source/common/network/address_impl.h"
#include "source/common/network/connection_balancer_impl.h"
#include "source/common/network/listen_socket_impl.h"
#include "source/common/network/proxy_protocol_filter_state.h"
#include "source/common/network/raw_buffer_socket.h"
#include "source/common/network/tcp_listener_impl.h"
#include "source/common/network/utility.h"
#include "source/extensions/filters/listener/proxy_protocol/proxy_protocol.h"
#include "test/mocks/api/mocks.h"
#include "test/mocks/buffer/mocks.h"
#include "test/mocks/network/mocks.h"
#include "test/mocks/runtime/mocks.h"
#include "test/mocks/server/listener_factory_context.h"
#include "test/test_common/environment.h"
#include "test/test_common/network_utility.h"
#include "test/test_common/printers.h"
#include "test/test_common/threadsafe_singleton_injector.h"
#include "test/test_common/utility.h"
#include "gmock/gmock.h"
#include "gtest/gtest.h"
using envoy::config::core::v3::ProxyProtocolPassThroughTLVs;
using Envoy::Extensions::Common::ProxyProtocol::PROXY_PROTO_V1_SIGNATURE_LEN;
using Envoy::Extensions::Common::ProxyProtocol::PROXY_PROTO_V2_SIGNATURE_LEN;
using testing::_;
using testing::AnyNumber;
using testing::AtLeast;
using testing::ElementsAre;
using testing::Invoke;
using testing::NiceMock;
using testing::Return;
using testing::ReturnRef;
namespace Envoy {
namespace Extensions {
namespace ListenerFilters {
namespace ProxyProtocol {
namespace {
// Build again on the basis of the connection_handler_test.cc
class ProxyProtocolTest : public testing::TestWithParam<Network::Address::IpVersion>,
public Network::ListenerConfig,
public Network::FilterChainManager,
protected Logger::Loggable<Logger::Id::main> {
public:
ProxyProtocolTest()
: api_(Api::createApiForTest(stats_store_)),
dispatcher_(api_->allocateDispatcher("test_thread")),
socket_(std::make_shared<Network::Test::TcpListenSocketImmediateListen>(
Network::Test::getCanonicalLoopbackAddress(GetParam()))),
connection_handler_(new Server::ConnectionHandlerImpl(*dispatcher_, absl::nullopt)),
name_("proxy"), filter_chain_(Network::Test::createEmptyFilterChainWithRawBufferSockets()),
init_manager_(nullptr),
listener_info_(std::make_shared<NiceMock<Network::MockListenerInfo>>()) {
socket_factories_.emplace_back(std::make_unique<Network::MockListenSocketFactory>());
EXPECT_CALL(*static_cast<Network::MockListenSocketFactory*>(socket_factories_[0].get()),
socketType())
.WillOnce(Return(Network::Socket::Type::Stream));
EXPECT_CALL(*static_cast<Network::MockListenSocketFactory*>(socket_factories_[0].get()),
localAddress())
.WillRepeatedly(ReturnRef(socket_->connectionInfoProvider().localAddress()));
EXPECT_CALL(*static_cast<Network::MockListenSocketFactory*>(socket_factories_[0].get()),
getListenSocket(_))
.WillOnce(Return(socket_));
connection_handler_->addListener(absl::nullopt, *this, runtime_, random_);
conn_ = dispatcher_->createClientConnection(socket_->connectionInfoProvider().localAddress(),
Network::Address::InstanceConstSharedPtr(),
Network::Test::createRawBufferSocket(), nullptr,
nullptr);
conn_->addConnectionCallbacks(connection_callbacks_);
}
// Network::ListenerConfig
Network::FilterChainManager& filterChainManager() override { return *this; }
Network::FilterChainFactory& filterChainFactory() override { return factory_; }
std::vector<Network::ListenSocketFactoryPtr>& listenSocketFactories() override {
return socket_factories_;
}
bool bindToPort() const override { return true; }
bool handOffRestoredDestinationConnections() const override { return false; }
uint32_t perConnectionBufferLimitBytes() const override { return 0; }
std::chrono::milliseconds listenerFiltersTimeout() const override { return {}; }
bool continueOnListenerFiltersTimeout() const override { return false; }
Stats::Scope& listenerScope() override { return *stats_store_.rootScope(); }
uint64_t listenerTag() const override { return 1; }
ResourceLimit& openConnections() override { return open_connections_; }
const std::string& name() const override { return name_; }
Network::UdpListenerConfigOptRef udpListenerConfig() override { return {}; }
Network::InternalListenerConfigOptRef internalListenerConfig() override { return {}; }
const Network::ListenerInfoConstSharedPtr& listenerInfo() const override {
return listener_info_;
}
Network::ConnectionBalancer& connectionBalancer(const Network::Address::Instance&) override {
return connection_balancer_;
}
const std::vector<AccessLog::InstanceSharedPtr>& accessLogs() const override {
return empty_access_logs_;
}
uint32_t tcpBacklogSize() const override { return ENVOY_TCP_BACKLOG_SIZE; }
uint32_t maxConnectionsToAcceptPerSocketEvent() const override {
return Network::DefaultMaxConnectionsToAcceptPerSocketEvent;
}
Init::Manager& initManager() override { return *init_manager_; }
bool ignoreGlobalConnLimit() const override { return false; }
// Network::FilterChainManager
const Network::FilterChain* findFilterChain(const Network::ConnectionSocket&,
const StreamInfo::StreamInfo&) const override {
return filter_chain_.get();
}
void connect(bool read = true,
const envoy::extensions::filters::listener::proxy_protocol::v3::ProxyProtocol*
proto_config = nullptr) {
int expected_callbacks = 2;
auto maybeExitDispatcher = [&]() -> void {
expected_callbacks--;
if (expected_callbacks == 0) {
dispatcher_->exit();
}
};
EXPECT_CALL(factory_, createListenerFilterChain(_))
.WillOnce(Invoke([&](Network::ListenerFilterManager& filter_manager) -> bool {
filter_manager.addAcceptFilter(
nullptr, std::make_unique<Filter>(std::make_shared<Config>(
listenerScope(), (nullptr != proto_config)
? *proto_config
: envoy::extensions::filters::listener::
proxy_protocol::v3::ProxyProtocol())));
maybeExitDispatcher();
return true;
}));
conn_->connect();
if (read) {
read_filter_ = std::make_shared<NiceMock<Network::MockReadFilter>>();
EXPECT_CALL(factory_, createNetworkFilterChain(_, _))
.WillOnce(Invoke([&](Network::Connection& connection,
const Envoy::Filter::NetworkFilterFactoriesList&) -> bool {
server_connection_ = &connection;
connection.addConnectionCallbacks(server_callbacks_);
connection.addReadFilter(read_filter_);
return true;
}));
}
EXPECT_CALL(connection_callbacks_, onEvent(Network::ConnectionEvent::Connected))
.WillOnce(Invoke([&](Network::ConnectionEvent) -> void { maybeExitDispatcher(); }));
dispatcher_->run(Event::Dispatcher::RunType::Block);
}
void write(const uint8_t* s, ssize_t l) {
Buffer::OwnedImpl buf(s, l);
conn_->write(buf, false);
}
void write(const std::string& s) {
Buffer::OwnedImpl buf(s);
conn_->write(buf, false);
}
void expectData(std::string expected) {
EXPECT_CALL(*read_filter_, onNewConnection());
EXPECT_CALL(*read_filter_, onData(_, _))
.WillOnce(Invoke([&](Buffer::Instance& buffer, bool) -> Network::FilterStatus {
EXPECT_EQ(buffer.toString(), expected);
buffer.drain(expected.length());
dispatcher_->exit();
return Network::FilterStatus::Continue;
}));
dispatcher_->run(Event::Dispatcher::RunType::Block);
}
void disconnect() {
EXPECT_CALL(connection_callbacks_, onEvent(Network::ConnectionEvent::LocalClose));
EXPECT_CALL(server_callbacks_, onEvent(Network::ConnectionEvent::RemoteClose))
.WillOnce(Invoke([&](Network::ConnectionEvent) -> void { dispatcher_->exit(); }));
conn_->close(Network::ConnectionCloseType::NoFlush);
dispatcher_->run(Event::Dispatcher::RunType::Block);
}
void expectConnectionError() {
EXPECT_CALL(connection_callbacks_, onEvent(Network::ConnectionEvent::RemoteClose))
.WillOnce(Invoke([&](Network::ConnectionEvent) -> void { dispatcher_->exit(); }));
dispatcher_->run(Event::Dispatcher::RunType::Block);
}
void expectProxyProtoError() {
expectConnectionError();
EXPECT_EQ(stats_store_.counter("downstream_cx_proxy_proto_error").value(), 1);
}
testing::NiceMock<Runtime::MockLoader> runtime_;
testing::NiceMock<Random::MockRandomGenerator> random_;
Stats::TestUtil::TestStore stats_store_;
Api::ApiPtr api_;
BasicResourceLimitImpl open_connections_;
Event::DispatcherPtr dispatcher_;
std::shared_ptr<Network::TcpListenSocket> socket_;
std::vector<Network::ListenSocketFactoryPtr> socket_factories_;
Network::NopConnectionBalancerImpl connection_balancer_;
Network::ConnectionHandlerPtr connection_handler_;
Network::MockFilterChainFactory factory_;
Network::ClientConnectionPtr conn_;
NiceMock<Network::MockConnectionCallbacks> connection_callbacks_;
Network::Connection* server_connection_;
Network::MockConnectionCallbacks server_callbacks_;
std::shared_ptr<Network::MockReadFilter> read_filter_;
std::string name_;
Api::OsSysCallsImpl os_sys_calls_actual_;
const Network::FilterChainSharedPtr filter_chain_;
const std::vector<AccessLog::InstanceSharedPtr> empty_access_logs_;
std::unique_ptr<Init::Manager> init_manager_;
const Network::ListenerInfoConstSharedPtr listener_info_;
};
// Parameterize the listener socket address version.
INSTANTIATE_TEST_SUITE_P(IpVersions, ProxyProtocolTest,
testing::ValuesIn(TestEnvironment::getIpVersionsForTest()),
TestUtility::ipTestParamsToString);
TEST_P(ProxyProtocolTest, V1UnsupportedIPv4) {
connect(false);
Cleanup cleaner = Network::Address::Ipv4Instance::forceProtocolUnsupportedForTest(true);
write("PROXY TCP4 1.2.3.4 253.253.253.253 65535 1234\r\nmore data");
expectProxyProtoError();
}
TEST_P(ProxyProtocolTest, V1UnsupportedIPv6) {
connect(false);
Cleanup cleaner = Network::Address::Ipv6Instance::forceProtocolUnsupportedForTest(true);
write("PROXY TCP6 1:2:3::4 5:6::7:8 65535 1234\r\nmore data");
expectProxyProtoError();
}
TEST_P(ProxyProtocolTest, V1Basic) {
connect();
write("PROXY TCP4 1.2.3.4 253.253.253.253 65535 1234\r\nmore data");
expectData("more data");
EXPECT_EQ(server_connection_->connectionInfoProvider().remoteAddress()->ip()->addressAsString(),
"1.2.3.4");
EXPECT_TRUE(server_connection_->connectionInfoProvider().localAddressRestored());
disconnect();
}
TEST_P(ProxyProtocolTest, AllowTinyNoProxyProtocol) {
// Allows a small request (less bytes than v1/v2 signature) through even though it doesn't use
// proxy protocol
envoy::extensions::filters::listener::proxy_protocol::v3::ProxyProtocol proto_config;
proto_config.set_allow_requests_without_proxy_protocol(true);
connect(true, &proto_config);
std::string msg = "data";
ASSERT_GT(PROXY_PROTO_V1_SIGNATURE_LEN,
msg.length()); // Ensure we attempt parsing byte by byte using `search_index_`
ASSERT_GT(PROXY_PROTO_V2_SIGNATURE_LEN, msg.length());
write(msg);
expectData(msg);
disconnect();
}
TEST_P(ProxyProtocolTest, AllowTinyNoProxyProtocolPartialMatchesV1First) {
// Allows a small request (less bytes than v1/v2 signature) through even though it doesn't use
// proxy protocol v1/v2 (but it does match parts of both signatures)
envoy::extensions::filters::listener::proxy_protocol::v3::ProxyProtocol proto_config;
proto_config.set_allow_requests_without_proxy_protocol(true);
connect(true, &proto_config);
// First two bytes are proxy protocol v1, second two bytes are proxy protocol v2.
// This ensures our byte by byte parsing (`search_index_`) has persistence built-in to
// remember whether the previous bytes were also valid for the signature
std::string msg = "PR\r\n";
ASSERT_GT(PROXY_PROTO_V1_SIGNATURE_LEN, msg.length());
ASSERT_GT(PROXY_PROTO_V2_SIGNATURE_LEN, msg.length());
write(msg);
expectData(msg);
disconnect();
}
TEST_P(ProxyProtocolTest, AllowTinyNoProxyProtocolPartialMatchesV2First) {
// Allows a small request (less bytes than v1/v2 signature) through even though it doesn't use
// proxy protocol v1/v2 (but it does match parts of both signatures)
envoy::extensions::filters::listener::proxy_protocol::v3::ProxyProtocol proto_config;
proto_config.set_allow_requests_without_proxy_protocol(true);
connect(true, &proto_config);
// First two bytes are proxy protocol v2, second two bytes are proxy protocol v1.
// This ensures our byte by byte parsing (`search_index_`) has persistence built-in to
// remember whether the previous bytes were also valid for the signature
std::string msg = "\r\nOX";
ASSERT_GT(PROXY_PROTO_V1_SIGNATURE_LEN, msg.length());
ASSERT_GT(PROXY_PROTO_V2_SIGNATURE_LEN, msg.length());
write(msg);
expectData(msg);
disconnect();
}
TEST_P(ProxyProtocolTest, AllowLargeNoProxyProtocol) {
// Allows a large request (more bytes than v1/v2 signature) through even though it doesn't use
// proxy protocol
envoy::extensions::filters::listener::proxy_protocol::v3::ProxyProtocol proto_config;
proto_config.set_allow_requests_without_proxy_protocol(true);
connect(true, &proto_config);
std::string msg = "more data more data more data";
ASSERT_GT(msg.length(),
PROXY_PROTO_V2_HEADER_LEN); // Ensure we attempt parsing as v2 proxy protocol up front
// rather than parsing byte by byte using `search_index_`
write(msg);
expectData(msg);
disconnect();
}
TEST_P(ProxyProtocolTest, V1Minimal) {
connect();
write("PROXY UNKNOWN\r\nmore data");
expectData("more data");
if (GetParam() == Envoy::Network::Address::IpVersion::v4) {
EXPECT_EQ(server_connection_->connectionInfoProvider().remoteAddress()->ip()->addressAsString(),
"127.0.0.1");
} else {
EXPECT_EQ(server_connection_->connectionInfoProvider().remoteAddress()->ip()->addressAsString(),
"::1");
}
EXPECT_FALSE(server_connection_->connectionInfoProvider().localAddressRestored());
disconnect();
}
TEST_P(ProxyProtocolTest, V2Basic) {
// A well-formed ipv4/tcp message, no extensions
constexpr uint8_t buffer[] = {0x0d, 0x0a, 0x0d, 0x0a, 0x00, 0x0d, 0x0a, 0x51, 0x55, 0x49,
0x54, 0x0a, 0x21, 0x11, 0x00, 0x0c, 0x01, 0x02, 0x03, 0x04,
0x00, 0x01, 0x01, 0x02, 0x03, 0x05, 0x00, 0x02, 'm', 'o',
'r', 'e', ' ', 'd', 'a', 't', 'a'};
connect();
write(buffer, sizeof(buffer));
expectData("more data");
EXPECT_EQ(server_connection_->connectionInfoProvider().remoteAddress()->ip()->addressAsString(),
"1.2.3.4");
EXPECT_TRUE(server_connection_->connectionInfoProvider().localAddressRestored());
disconnect();
}
TEST_P(ProxyProtocolTest, BasicV6) {
connect();
write("PROXY TCP6 1:2:3::4 5:6::7:8 65535 1234\r\nmore data");
expectData("more data");
EXPECT_EQ(server_connection_->connectionInfoProvider().remoteAddress()->ip()->addressAsString(),
"1:2:3::4");
EXPECT_TRUE(server_connection_->connectionInfoProvider().localAddressRestored());
disconnect();
}
TEST_P(ProxyProtocolTest, V2BasicV6) {
// A well-formed ipv6/tcp message, no extensions
constexpr uint8_t buffer[] = {0x0d, 0x0a, 0x0d, 0x0a, 0x00, 0x0d, 0x0a, 0x51, 0x55, 0x49, 0x54,
0x0a, 0x21, 0x22, 0x00, 0x24, 0x00, 0x01, 0x00, 0x02, 0x00, 0x03,
0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x04, 0x00,
0x01, 0x01, 0x00, 0x02, 0x00, 0x00, 0x03, 0x00, 0x00, 0x00, 0x00,
0x00, 0x00, 0x00, 0x00, 0x00, 0x08, 0x00, 0x02, 'm', 'o', 'r',
'e', ' ', 'd', 'a', 't', 'a'};
connect();
write(buffer, sizeof(buffer));
expectData("more data");
EXPECT_EQ(server_connection_->connectionInfoProvider().remoteAddress()->ip()->addressAsString(),
"1:2:3::4");
EXPECT_TRUE(server_connection_->connectionInfoProvider().localAddressRestored());
disconnect();
}
TEST_P(ProxyProtocolTest, V2UnsupportedIPv4) {
// A well-formed ipv4/tcp message, no extensions
constexpr uint8_t buffer[] = {0x0d, 0x0a, 0x0d, 0x0a, 0x00, 0x0d, 0x0a, 0x51, 0x55, 0x49,
0x54, 0x0a, 0x21, 0x11, 0x00, 0x0c, 0x01, 0x02, 0x03, 0x04,
0x00, 0x01, 0x01, 0x02, 0x03, 0x05, 0x00, 0x02, 'm', 'o',
'r', 'e', ' ', 'd', 'a', 't', 'a'};
connect(false);
Cleanup cleaner = Network::Address::Ipv4Instance::forceProtocolUnsupportedForTest(true);
write(buffer, sizeof(buffer));
expectProxyProtoError();
}
TEST_P(ProxyProtocolTest, V2UnsupportedIPv6) {
// A well-formed ipv6/tcp message, no extensions
constexpr uint8_t buffer[] = {0x0d, 0x0a, 0x0d, 0x0a, 0x00, 0x0d, 0x0a, 0x51, 0x55, 0x49, 0x54,
0x0a, 0x21, 0x22, 0x00, 0x24, 0x00, 0x01, 0x00, 0x02, 0x00, 0x03,
0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x04, 0x00,
0x01, 0x01, 0x00, 0x02, 0x00, 0x00, 0x03, 0x00, 0x00, 0x00, 0x00,
0x00, 0x00, 0x00, 0x00, 0x00, 0x08, 0x00, 0x02, 'm', 'o', 'r',
'e', ' ', 'd', 'a', 't', 'a'};
connect(false);
Cleanup cleaner = Network::Address::Ipv6Instance::forceProtocolUnsupportedForTest(true);
write(buffer, sizeof(buffer));
expectProxyProtoError();
}
TEST_P(ProxyProtocolTest, V2UnsupportedAF) {
// A well-formed message with an unsupported address family
constexpr uint8_t buffer[] = {0x0d, 0x0a, 0x0d, 0x0a, 0x00, 0x0d, 0x0a, 0x51, 0x55, 0x49,
0x54, 0x0a, 0x21, 0x41, 0x00, 0x0c, 0x01, 0x02, 0x03, 0x04,
0x00, 0x01, 0x01, 0x02, 0x03, 0x05, 0x00, 0x02, 'm', 'o',
'r', 'e', ' ', 'd', 'a', 't', 'a'};
connect(false);
write(buffer, sizeof(buffer));
expectProxyProtoError();
}
TEST_P(ProxyProtocolTest, ErrorRecv_2) {
// A well formed v4/tcp message, no extensions, but introduce an error on recv (e.g. socket close)
constexpr uint8_t buffer[] = {0x0d, 0x0a, 0x0d, 0x0a, 0x00, 0x0d, 0x0a, 0x51, 0x55, 0x49,
0x54, 0x0a, 0x21, 0x11, 0x00, 0x0c, 0x01, 0x02, 0x03, 0x04,
0x00, 0x01, 0x01, 0x02, 0x03, 0x05, 0x00, 0x02, 'm', 'o',
'r', 'e', ' ', 'd', 'a', 't', 'a'};
Api::MockOsSysCalls os_sys_calls;
TestThreadsafeSingletonInjector<Api::OsSysCallsImpl> os_calls(&os_sys_calls);
// TODO(davinci26): Mocking should not be used to provide real system calls.
#ifdef WIN32
EXPECT_CALL(os_sys_calls, readv(_, _, _))
.Times(AnyNumber())
.WillRepeatedly(Return(Api::SysCallSizeResult{-1, 0}));
#else
EXPECT_CALL(os_sys_calls, readv(_, _, _))
.Times(AnyNumber())
.WillRepeatedly(Invoke([this](os_fd_t fd, const iovec* iov, int iovcnt) {
return os_sys_calls_actual_.readv(fd, iov, iovcnt);
}));
#endif
EXPECT_CALL(os_sys_calls, recv(_, _, _, _))
.Times(AnyNumber())
.WillRepeatedly(Return(Api::SysCallSizeResult{-1, 0}));
EXPECT_CALL(os_sys_calls, connect(_, _, _))
.Times(AnyNumber())
.WillRepeatedly(Invoke([this](os_fd_t sockfd, const sockaddr* addr, socklen_t addrlen) {
return os_sys_calls_actual_.connect(sockfd, addr, addrlen);
}));
EXPECT_CALL(os_sys_calls, writev(_, _, _))
.Times(AnyNumber())
.WillRepeatedly(Invoke([this](os_fd_t fd, const iovec* iov, int iovcnt) {
return os_sys_calls_actual_.writev(fd, iov, iovcnt);
}));
EXPECT_CALL(os_sys_calls, send(_, _, _, _))
.Times(AnyNumber())
.WillRepeatedly(Invoke([this](os_fd_t socket, void* buffer, size_t length, int flags) {
return os_sys_calls_actual_.send(socket, buffer, length, flags);
}));
EXPECT_CALL(os_sys_calls, getsockopt_(_, _, _, _, _))
.Times(AnyNumber())
.WillRepeatedly(Invoke(
[this](os_fd_t sockfd, int level, int optname, void* optval, socklen_t* optlen) -> int {
return os_sys_calls_actual_.getsockopt(sockfd, level, optname, optval, optlen)
.return_value_;
}));
EXPECT_CALL(os_sys_calls, getsockname(_, _, _))
.Times(AnyNumber())
.WillRepeatedly(Invoke(
[this](os_fd_t sockfd, sockaddr* name, socklen_t* namelen) -> Api::SysCallIntResult {
return os_sys_calls_actual_.getsockname(sockfd, name, namelen);
}));
EXPECT_CALL(os_sys_calls, shutdown(_, _))
.Times(AnyNumber())
.WillRepeatedly(Invoke(
[this](os_fd_t sockfd, int how) { return os_sys_calls_actual_.shutdown(sockfd, how); }));
EXPECT_CALL(os_sys_calls, close(_)).Times(AnyNumber()).WillRepeatedly(Invoke([this](os_fd_t fd) {
return os_sys_calls_actual_.close(fd);
}));
EXPECT_CALL(os_sys_calls, accept(_, _, _))
.Times(AnyNumber())
.WillRepeatedly(Invoke(
[this](os_fd_t sockfd, sockaddr* addr, socklen_t* addrlen) -> Api::SysCallSocketResult {
return os_sys_calls_actual_.accept(sockfd, addr, addrlen);
}));
EXPECT_CALL(os_sys_calls, supportsGetifaddrs())
.Times(AnyNumber())
.WillRepeatedly(
Invoke([this]() -> bool { return os_sys_calls_actual_.supportsGetifaddrs(); }));
EXPECT_CALL(os_sys_calls, getifaddrs(_))
.Times(AnyNumber())
.WillRepeatedly(Invoke([this](Api::InterfaceAddressVector& vector) -> Api::SysCallIntResult {
return os_sys_calls_actual_.getifaddrs(vector);
}));
EXPECT_CALL(os_sys_calls, socketTcpInfo(_, _))
.Times(AnyNumber())
.WillRepeatedly(Invoke([this](os_fd_t sockfd, Api::EnvoyTcpInfo* tcp_info) {
return os_sys_calls_actual_.socketTcpInfo(sockfd, tcp_info);
}));
connect(false);
write(buffer, sizeof(buffer));
expectConnectionError();
}
TEST_P(ProxyProtocolTest, ErrorRecv_1) {
// A well formed v4/tcp message, no extensions, but introduce an error on recv()
constexpr uint8_t buffer[] = {0x0d, 0x0a, 0x0d, 0x0a, 0x00, 0x0d, 0x0a, 0x51, 0x55, 0x49,
0x54, 0x0a, 0x21, 0x11, 0x00, 0x0c, 0x01, 0x02, 0x03, 0x04,
0x00, 0x01, 0x01, 0x02, 0x03, 0x05, 0x00, 0x02, 'm', 'o',
'r', 'e', ' ', 'd', 'a', 't', 'a'};
Api::MockOsSysCalls os_sys_calls;
TestThreadsafeSingletonInjector<Api::OsSysCallsImpl> os_calls(&os_sys_calls);
// TODO(davinci26): Mocking should not be used to provide real system calls.
#ifdef WIN32
EXPECT_CALL(os_sys_calls, readv(_, _, _))
.Times(AnyNumber())
.WillRepeatedly(Return(Api::SysCallSizeResult{-1, 0}));
#else
EXPECT_CALL(os_sys_calls, readv(_, _, _))
.Times(AnyNumber())
.WillRepeatedly(Invoke([this](os_fd_t fd, const iovec* iov, int iovcnt) {
return os_sys_calls_actual_.readv(fd, iov, iovcnt);
}));
#endif
EXPECT_CALL(os_sys_calls, recv(_, _, _, _))
.Times(AnyNumber())
.WillRepeatedly(Return(Api::SysCallSizeResult{-1, 0}));
EXPECT_CALL(os_sys_calls, connect(_, _, _))
.Times(AnyNumber())
.WillRepeatedly(Invoke([this](os_fd_t sockfd, const sockaddr* addr, socklen_t addrlen) {
return os_sys_calls_actual_.connect(sockfd, addr, addrlen);
}));
EXPECT_CALL(os_sys_calls, writev(_, _, _))
.Times(AnyNumber())
.WillRepeatedly(Invoke([this](os_fd_t fd, const iovec* iov, int iovcnt) {
return os_sys_calls_actual_.writev(fd, iov, iovcnt);
}));
EXPECT_CALL(os_sys_calls, send(_, _, _, _))
.Times(AnyNumber())
.WillRepeatedly(Invoke([this](os_fd_t socket, void* buffer, size_t length, int flags) {
return os_sys_calls_actual_.send(socket, buffer, length, flags);
}));
EXPECT_CALL(os_sys_calls, getsockopt_(_, _, _, _, _))
.Times(AnyNumber())
.WillRepeatedly(Invoke(
[this](os_fd_t sockfd, int level, int optname, void* optval, socklen_t* optlen) -> int {
return os_sys_calls_actual_.getsockopt(sockfd, level, optname, optval, optlen)
.return_value_;
}));
EXPECT_CALL(os_sys_calls, getsockname(_, _, _))
.Times(AnyNumber())
.WillRepeatedly(Invoke(
[this](os_fd_t sockfd, sockaddr* name, socklen_t* namelen) -> Api::SysCallIntResult {
return os_sys_calls_actual_.getsockname(sockfd, name, namelen);
}));
EXPECT_CALL(os_sys_calls, shutdown(_, _))
.Times(AnyNumber())
.WillRepeatedly(Invoke(
[this](os_fd_t sockfd, int how) { return os_sys_calls_actual_.shutdown(sockfd, how); }));
EXPECT_CALL(os_sys_calls, close(_)).Times(AnyNumber()).WillRepeatedly(Invoke([this](os_fd_t fd) {
return os_sys_calls_actual_.close(fd);
}));
EXPECT_CALL(os_sys_calls, accept(_, _, _))
.Times(AnyNumber())
.WillRepeatedly(Invoke(
[this](os_fd_t sockfd, sockaddr* addr, socklen_t* addrlen) -> Api::SysCallSocketResult {
return os_sys_calls_actual_.accept(sockfd, addr, addrlen);
}));
EXPECT_CALL(os_sys_calls, supportsGetifaddrs())
.Times(AnyNumber())
.WillRepeatedly(
Invoke([this]() -> bool { return os_sys_calls_actual_.supportsGetifaddrs(); }));
EXPECT_CALL(os_sys_calls, getifaddrs(_))
.Times(AnyNumber())
.WillRepeatedly(Invoke([this](Api::InterfaceAddressVector& vector) -> Api::SysCallIntResult {
return os_sys_calls_actual_.getifaddrs(vector);
}));
EXPECT_CALL(os_sys_calls, socketTcpInfo(_, _))
.Times(AnyNumber())
.WillRepeatedly(Invoke([this](os_fd_t sockfd, Api::EnvoyTcpInfo* tcp_info) {
return os_sys_calls_actual_.socketTcpInfo(sockfd, tcp_info);
}));
connect(false);
write(buffer, sizeof(buffer));
expectConnectionError();
}
TEST_P(ProxyProtocolTest, V2NotLocalOrOnBehalf) {
// An illegal command type: neither 'local' nor 'proxy' command
constexpr uint8_t buffer[] = {0x0d, 0x0a, 0x0d, 0x0a, 0x00, 0x0d, 0x0a, 0x51, 0x55, 0x49,
0x54, 0x0a, 0x23, 0x1f, 0x00, 0x0c, 0x01, 0x02, 0x03, 0x04,
0x00, 0x01, 0x01, 0x02, 0x03, 0x05, 0x00, 0x02, 'm', 'o',
'r', 'e', ' ', 'd', 'a', 't', 'a'};
connect(false);
write(buffer, sizeof(buffer));
expectProxyProtoError();
}
TEST_P(ProxyProtocolTest, V2LocalConnection) {
// A 'local' connection, e.g. health-checking, no address, no extensions
constexpr uint8_t buffer[] = {0x0d, 0x0a, 0x0d, 0x0a, 0x00, 0x0d, 0x0a, 0x51, 0x55,
0x49, 0x54, 0x0a, 0x20, 0x00, 0x00, 0x00, 'm', 'o',
'r', 'e', ' ', 'd', 'a', 't', 'a'};
connect();
write(buffer, sizeof(buffer));
expectData("more data");
if (server_connection_->connectionInfoProvider().remoteAddress()->ip()->version() ==
Envoy::Network::Address::IpVersion::v6) {
EXPECT_EQ(server_connection_->connectionInfoProvider().remoteAddress()->ip()->addressAsString(),
"::1");
} else if (server_connection_->connectionInfoProvider().remoteAddress()->ip()->version() ==
Envoy::Network::Address::IpVersion::v4) {
EXPECT_EQ(server_connection_->connectionInfoProvider().remoteAddress()->ip()->addressAsString(),
"127.0.0.1");
}
EXPECT_FALSE(server_connection_->connectionInfoProvider().localAddressRestored());
disconnect();
}
TEST_P(ProxyProtocolTest, V2LocalConnectionExtension) {
// A 'local' connection, e.g. health-checking, no address, 1 TLV (0x00,0x00,0x01,0xff) is present.
constexpr uint8_t buffer[] = {0x0d, 0x0a, 0x0d, 0x0a, 0x00, 0x0d, 0x0a, 0x51, 0x55, 0x49,
0x54, 0x0a, 0x20, 0x00, 0x00, 0x04, 0x00, 0x00, 0x01, 0xff,
'm', 'o', 'r', 'e', ' ', 'd', 'a', 't', 'a'};
connect();
write(buffer, sizeof(buffer));
expectData("more data");
if (server_connection_->connectionInfoProvider().remoteAddress()->ip()->version() ==
Envoy::Network::Address::IpVersion::v6) {
EXPECT_EQ(server_connection_->connectionInfoProvider().remoteAddress()->ip()->addressAsString(),
"::1");
} else if (server_connection_->connectionInfoProvider().remoteAddress()->ip()->version() ==
Envoy::Network::Address::IpVersion::v4) {
EXPECT_EQ(server_connection_->connectionInfoProvider().remoteAddress()->ip()->addressAsString(),
"127.0.0.1");
}
EXPECT_FALSE(server_connection_->connectionInfoProvider().localAddressRestored());
disconnect();
}
TEST_P(ProxyProtocolTest, V2LocalConnectionFilterState) {
// A well-formed local proxy protocol v2 header sampled from an AWS NLB healthcheck request,
// no address, 1 TLV is present.
constexpr uint8_t buffer[] = {0x0d, 0x0a, 0x0d, 0x0a, 0x00, 0x0d, 0x0a, 0x51, 0x55, 0x49, 0x54,
0x0a, 0x20, 0x00, 0x00, 0x07, 0x00, 0x00, 0x04, 0x0a, 0x0b, 0x0c,
0x0d, 'm', 'o', 'r', 'e', 'd', 'a', 't', 'a'};
envoy::extensions::filters::listener::proxy_protocol::v3::ProxyProtocol proto_config;
connect(true, &proto_config);
write(buffer, sizeof(buffer));
expectData("moredata");
auto& filter_state = server_connection_->streamInfo().filterState();
const auto& proxy_proto_data = filter_state
->getDataReadOnly<Network::ProxyProtocolFilterState>(
Network::ProxyProtocolFilterState::key())
->value();
if (server_connection_->connectionInfoProvider().remoteAddress()->ip()->version() ==
Envoy::Network::Address::IpVersion::v6) {
EXPECT_EQ(proxy_proto_data.dst_addr_->ip()->addressAsString(), "::1");
} else if (server_connection_->connectionInfoProvider().remoteAddress()->ip()->version() ==
Envoy::Network::Address::IpVersion::v4) {
EXPECT_EQ(proxy_proto_data.dst_addr_->ip()->addressAsString(), "127.0.0.1");
}
EXPECT_FALSE(server_connection_->connectionInfoProvider().localAddressRestored());
disconnect();
}
TEST_P(ProxyProtocolTest, V2ShortV4) {
// An ipv4/tcp connection that has incorrect addr-len encoded
constexpr uint8_t buffer[] = {0x0d, 0x0a, 0x0d, 0x0a, 0x00, 0x0d, 0x0a, 0x51, 0x55, 0x49,
0x54, 0x0a, 0x21, 0x21, 0x00, 0x04, 0x00, 0x08, 0x00, 0x02,
'm', 'o', 'r', 'e', ' ', 'd', 'a', 't', 'a'};
connect(false);
write(buffer, sizeof(buffer));
expectProxyProtoError();
}
TEST_P(ProxyProtocolTest, V2ShortV4WithAllowNoProxyProtocol) {
// An ipv4/tcp PROXY header that has incorrect addr-len encoded
constexpr uint8_t buffer[] = {0x0d, 0x0a, 0x0d, 0x0a, 0x00, 0x0d, 0x0a, 0x51, 0x55, 0x49,
0x54, 0x0a, 0x21, 0x21, 0x00, 0x04, 0x00, 0x08, 0x00, 0x02,
'm', 'o', 'r', 'e', ' ', 'd', 'a', 't', 'a'};
envoy::extensions::filters::listener::proxy_protocol::v3::ProxyProtocol proto_config;
proto_config.set_allow_requests_without_proxy_protocol(true);
connect(false, &proto_config);
write(buffer, sizeof(buffer));
expectProxyProtoError();
}
TEST_P(ProxyProtocolTest, V2ShortAddrV4) {
// An ipv4/tcp connection that has insufficient header-length encoded
constexpr uint8_t buffer[] = {0x0d, 0x0a, 0x0d, 0x0a, 0x00, 0x0d, 0x0a, 0x51, 0x55, 0x49,
0x54, 0x0a, 0x21, 0x11, 0x00, 0x0b, 0x01, 0x02, 0x03, 0x04,
0x00, 0x01, 0x01, 0x02, 0x03, 0x05, 0x00, 0x02, 'm', 'o',
'r', 'e', ' ', 'd', 'a', 't', 'a'};
connect(false);
write(buffer, sizeof(buffer));
expectProxyProtoError();
}
TEST_P(ProxyProtocolTest, V2ShortV6) {
// An ipv6/tcp connection that has incorrect addr-len encoded
constexpr uint8_t buffer[] = {
0x0d, 0x0a, 0x0d, 0x0a, 0x00, 0x0d, 0x0a, 0x51, 0x55, 0x49, 0x54, 0x0a, 0x21, 0x22, 0x00,
0x14, 0x00, 0x01, 0x01, 0x00, 0x02, 0x00, 0x00, 0x03, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00,
0x00, 0x00, 0x00, 0x08, 0x00, 0x02, 'm', 'o', 'r', 'e', ' ', 'd', 'a', 't', 'a'};
connect(false);
write(buffer, sizeof(buffer));
expectProxyProtoError();
}
TEST_P(ProxyProtocolTest, V2ShortAddrV6) {
// An ipv6/tcp connection that has insufficient header-length encoded
constexpr uint8_t buffer[] = {0x0d, 0x0a, 0x0d, 0x0a, 0x00, 0x0d, 0x0a, 0x51, 0x55, 0x49, 0x54,
0x0a, 0x21, 0x22, 0x00, 0x23, 0x00, 0x01, 0x00, 0x02, 0x00, 0x03,
0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x04, 0x00,
0x01, 0x01, 0x00, 0x02, 0x00, 0x00, 0x03, 0x00, 0x00, 0x00, 0x00,
0x00, 0x00, 0x00, 0x00, 0x00, 0x08, 0x00, 0x02, 'm', 'o', 'r',
'e', ' ', 'd', 'a', 't', 'a'};
connect(false);
write(buffer, sizeof(buffer));
expectProxyProtoError();
}
TEST_P(ProxyProtocolTest, V2AF_UNIX) {
// A well-formed AF_UNIX (0x32 in b14) connection is rejected
constexpr uint8_t buffer[] = {
0x0d, 0x0a, 0x0d, 0x0a, 0x00, 0x0d, 0x0a, 0x51, 0x55, 0x49, 0x54, 0x0a, 0x21, 0x32, 0x00,
0x14, 0x00, 0x01, 0x01, 0x00, 0x02, 0x00, 0x00, 0x03, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00,
0x00, 0x00, 0x00, 0x08, 0x00, 0x02, 'm', 'o', 'r', 'e', ' ', 'd', 'a', 't', 'a'};
connect(false);
write(buffer, sizeof(buffer));
expectProxyProtoError();
}
TEST_P(ProxyProtocolTest, V2BadCommand) {
// A non local/proxy command (0x29 in b13) is rejected
constexpr uint8_t buffer[] = {
0x0d, 0x0a, 0x0d, 0x0a, 0x00, 0x0d, 0x0a, 0x51, 0x55, 0x49, 0x54, 0x0a, 0x29, 0x32, 0x00,
0x14, 0x00, 0x01, 0x01, 0x00, 0x02, 0x00, 0x00, 0x03, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00,
0x00, 0x00, 0x00, 0x08, 0x00, 0x02, 'm', 'o', 'r', 'e', ' ', 'd', 'a', 't', 'a'};
connect(false);
write(buffer, sizeof(buffer));
expectProxyProtoError();
}
TEST_P(ProxyProtocolTest, V2WrongVersion) {
// A non '2' version is rejected (0x93 in b13)
constexpr uint8_t buffer[] = {
0x0d, 0x0a, 0x0d, 0x0a, 0x00, 0x0d, 0x0a, 0x51, 0x55, 0x49, 0x54, 0x0a, 0x21, 0x93, 0x00,
0x14, 0x00, 0x01, 0x01, 0x00, 0x02, 0x00, 0x00, 0x03, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00,
0x00, 0x00, 0x00, 0x08, 0x00, 0x02, 'm', 'o', 'r', 'e', ' ', 'd', 'a', 't', 'a'};
connect(false);
write(buffer, sizeof(buffer));
expectProxyProtoError();
}
TEST_P(ProxyProtocolTest, V1TooLong) {
constexpr uint8_t buffer[] = {' ', ' ', ' ', ' ', ' ', ' ', ' ', ' '};
connect(false);
write("PROXY TCP4 1.2.3.4 2.3.4.5 100 100");
for (size_t i = 0; i < 256; i += sizeof(buffer)) {
write(buffer, sizeof(buffer));
}
expectProxyProtoError();
}
TEST_P(ProxyProtocolTest, V1TooLongWithAllowNoProxyProtocol) {
constexpr uint8_t buffer[] = {' ', ' ', ' ', ' ', ' ', ' ', ' ', ' '};
envoy::extensions::filters::listener::proxy_protocol::v3::ProxyProtocol proto_config;
proto_config.set_allow_requests_without_proxy_protocol(true);
connect(false, &proto_config);
write("PROXY TCP4 1.2.3.4 2.3.4.5 100 100");
for (size_t i = 0; i < 256; i += sizeof(buffer)) {
write(buffer, sizeof(buffer));
}
expectProxyProtoError();
}
TEST_P(ProxyProtocolTest, V2ParseExtensions) {
// A well-formed ipv4/tcp with a pair of TLV extensions is accepted
constexpr uint8_t buffer[] = {0x0d, 0x0a, 0x0d, 0x0a, 0x00, 0x0d, 0x0a, 0x51, 0x55, 0x49,
0x54, 0x0a, 0x21, 0x11, 0x00, 0x14, 0x01, 0x02, 0x03, 0x04,
0x00, 0x01, 0x01, 0x02, 0x03, 0x05, 0x00, 0x02};
constexpr uint8_t tlv[] = {0x0, 0x0, 0x1, 0xff};
constexpr uint8_t data[] = {'D', 'A', 'T', 'A'};
connect();
write(buffer, sizeof(buffer));
dispatcher_->run(Event::Dispatcher::RunType::NonBlock);
for (int i = 0; i < 2; i++) {
write(tlv, sizeof(tlv));
}
write(data, sizeof(data));
expectData("DATA");
disconnect();
}
TEST_P(ProxyProtocolTest, V2ParseExtensionsRecvError) {
// A well-formed ipv4/tcp with a TLV extension. An error is returned on tlv recv()
constexpr uint8_t buffer[] = {0x0d, 0x0a, 0x0d, 0x0a, 0x00, 0x0d, 0x0a, 0x51, 0x55, 0x49,
0x54, 0x0a, 0x21, 0x11, 0x00, 0x10, 0x01, 0x02, 0x03, 0x04,
0x00, 0x01, 0x01, 0x02, 0x03, 0x05, 0x00, 0x02};
constexpr uint8_t tlv[] = {0x0, 0x0, 0x1, 0xff};
Api::MockOsSysCalls os_sys_calls;
TestThreadsafeSingletonInjector<Api::OsSysCallsImpl> os_calls(&os_sys_calls);
bool header_writed = false;
// TODO(davinci26): Mocking should not be used to provide real system calls.
#ifdef WIN32
EXPECT_CALL(os_sys_calls, readv(_, _, _))
.Times(AnyNumber())
.WillRepeatedly(Invoke([&](os_fd_t fd, const iovec* iov, int iovcnt) {
const Api::SysCallSizeResult x = os_sys_calls_actual_.readv(fd, iov, iovcnt);
if (header_writed) {
return Api::SysCallSizeResult{-1, 0};
}
return x;
}));
#else
EXPECT_CALL(os_sys_calls, readv(_, _, _))
.Times(AnyNumber())
.WillRepeatedly(Invoke([this](os_fd_t fd, const iovec* iov, int iovcnt) {
return os_sys_calls_actual_.readv(fd, iov, iovcnt);
}));
#endif
EXPECT_CALL(os_sys_calls, recv(_, _, _, _))
.Times(AnyNumber())
.WillRepeatedly(Invoke([&](os_fd_t fd, void* buf, size_t n, int flags) {
const Api::SysCallSizeResult x = os_sys_calls_actual_.recv(fd, buf, n, flags);
if (header_writed) {
return Api::SysCallSizeResult{-1, 0};
}
return x;
}));
EXPECT_CALL(os_sys_calls, connect(_, _, _))
.Times(AnyNumber())
.WillRepeatedly(Invoke([this](os_fd_t sockfd, const sockaddr* addr, socklen_t addrlen) {
return os_sys_calls_actual_.connect(sockfd, addr, addrlen);
}));
EXPECT_CALL(os_sys_calls, writev(_, _, _))
.Times(AnyNumber())
.WillRepeatedly(Invoke([this](os_fd_t fd, const iovec* iov, int iovcnt) {
return os_sys_calls_actual_.writev(fd, iov, iovcnt);
}));
EXPECT_CALL(os_sys_calls, send(_, _, _, _))
.Times(AnyNumber())
.WillRepeatedly(Invoke([this](os_fd_t socket, void* buffer, size_t length, int flags) {
return os_sys_calls_actual_.send(socket, buffer, length, flags);
}));
EXPECT_CALL(os_sys_calls, getsockopt_(_, _, _, _, _))
.Times(AnyNumber())
.WillRepeatedly(Invoke(
[this](os_fd_t sockfd, int level, int optname, void* optval, socklen_t* optlen) -> int {
return os_sys_calls_actual_.getsockopt(sockfd, level, optname, optval, optlen)
.return_value_;
}));
EXPECT_CALL(os_sys_calls, getsockname(_, _, _))
.Times(AnyNumber())
.WillRepeatedly(Invoke(
[this](os_fd_t sockfd, sockaddr* name, socklen_t* namelen) -> Api::SysCallIntResult {
return os_sys_calls_actual_.getsockname(sockfd, name, namelen);
}));
EXPECT_CALL(os_sys_calls, shutdown(_, _))
.Times(AnyNumber())
.WillRepeatedly(Invoke(
[this](os_fd_t sockfd, int how) { return os_sys_calls_actual_.shutdown(sockfd, how); }));
EXPECT_CALL(os_sys_calls, close(_)).Times(AnyNumber()).WillRepeatedly(Invoke([this](os_fd_t fd) {
return os_sys_calls_actual_.close(fd);
}));
EXPECT_CALL(os_sys_calls, accept(_, _, _))
.Times(AnyNumber())
.WillRepeatedly(Invoke(
[this](os_fd_t sockfd, sockaddr* addr, socklen_t* addrlen) -> Api::SysCallSocketResult {
return os_sys_calls_actual_.accept(sockfd, addr, addrlen);
}));
EXPECT_CALL(os_sys_calls, supportsGetifaddrs())
.Times(AnyNumber())
.WillRepeatedly(
Invoke([this]() -> bool { return os_sys_calls_actual_.supportsGetifaddrs(); }));
EXPECT_CALL(os_sys_calls, getifaddrs(_))
.Times(AnyNumber())
.WillRepeatedly(Invoke([this](Api::InterfaceAddressVector& vector) -> Api::SysCallIntResult {
return os_sys_calls_actual_.getifaddrs(vector);
}));
EXPECT_CALL(os_sys_calls, socketTcpInfo(_, _))
.Times(AnyNumber())
.WillRepeatedly(Invoke([this](os_fd_t sockfd, Api::EnvoyTcpInfo* tcp_info) {
return os_sys_calls_actual_.socketTcpInfo(sockfd, tcp_info);
}));
EXPECT_CALL(os_sys_calls, setsockopt_(_, SOL_SOCKET, SO_LINGER, _, _))
.Times(AnyNumber())
.WillRepeatedly(Invoke([this](os_fd_t sockfd, int level, int optname, const void* optval,
socklen_t optlen) -> int {
return os_sys_calls_actual_.setsockopt(sockfd, level, optname, optval, optlen)
.return_value_;
}));
connect(false);
write(buffer, sizeof(buffer));
dispatcher_->run(Event::Dispatcher::RunType::NonBlock);
header_writed = true;
write(tlv, sizeof(tlv));
expectConnectionError();
}
TEST_P(ProxyProtocolTest, V2ParseExtensionsFrag) {
// A well-formed ipv4/tcp header with 2 TLV/extensions, these are fragmented on delivery
constexpr uint8_t buffer[] = {0x0d, 0x0a, 0x0d, 0x0a, 0x00, 0x0d, 0x0a, 0x51, 0x55, 0x49,
0x54, 0x0a, 0x21, 0x11, 0x00, 0x14, 0x01, 0x02, 0x03, 0x04,
0x00, 0x01, 0x01, 0x02, 0x03, 0x05, 0x00, 0x02};
constexpr uint8_t tlv[] = {0x0, 0x0, 0x1, 0xff};
constexpr uint8_t data[] = {'D', 'A', 'T', 'A'};
connect();
write(buffer, sizeof(buffer));
for (int i = 0; i < 2; i++) {
write(tlv, sizeof(tlv));
}
write(data, sizeof(data));
expectData("DATA");
disconnect();
}
TEST_P(ProxyProtocolTest, Fragmented) {
connect();
write("PROXY TCP4");
write(" 254.254.2");
write("54.254 1.2");
write(".3.4 65535");
write(" 1234\r\n...");
// If there is no data after the PROXY line, the read filter does not receive even the
// onNewConnection() callback. We need this in order to run the dispatcher in blocking
// mode to make sure that proxy protocol processing is completed before we start testing
// the results. Since we must have data we might as well check that we get it.
expectData("...");
EXPECT_EQ(server_connection_->connectionInfoProvider().remoteAddress()->ip()->addressAsString(),
"254.254.254.254");
EXPECT_TRUE(server_connection_->connectionInfoProvider().localAddressRestored());
disconnect();
}
TEST_P(ProxyProtocolTest, V2Fragmented1) {
// A well-formed ipv4/tcp message, delivering part of the signature, then part of
// the address, then the remainder
constexpr uint8_t buffer[] = {0x0d, 0x0a, 0x0d, 0x0a, 0x00, 0x0d, 0x0a, 0x51, 0x55, 0x49,
0x54, 0x0a, 0x21, 0x11, 0x00, 0x0c, 0x01, 0x02, 0x03, 0x04,
0x00, 0x01, 0x01, 0x02, 0x03, 0x05, 0x00, 0x02, 'm', 'o',
'r', 'e', ' ', 'd', 'a', 't', 'a'};
connect();
write(buffer, 10);
dispatcher_->run(Event::Dispatcher::RunType::NonBlock);
write(buffer + 10, 10);
dispatcher_->run(Event::Dispatcher::RunType::NonBlock);
write(buffer + 20, 17);