forked from envoyproxy/envoy
-
Notifications
You must be signed in to change notification settings - Fork 2
/
fake_upstream.cc
1136 lines (1016 loc) · 43.7 KB
/
fake_upstream.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 "test/integration/fake_upstream.h"
#include <chrono>
#include <cstdint>
#include <memory>
#include <string>
#include "source/common/buffer/buffer_impl.h"
#include "source/common/config/utility.h"
#include "source/common/http/header_map_impl.h"
#include "source/common/http/http1/codec_impl.h"
#include "source/common/http/http2/codec_impl.h"
#include "source/common/network/address_impl.h"
#include "source/common/network/connection_impl.h"
#include "source/common/network/listen_socket_impl.h"
#include "source/common/network/socket_option_factory.h"
#include "source/common/network/utility.h"
#include "source/common/runtime/runtime_features.h"
#ifdef ENVOY_ENABLE_QUIC
#include "source/common/quic/server_codec_impl.h"
#include "quiche/quic/test_tools/quic_session_peer.h"
#endif
#include "source/common/listener_manager/connection_handler_impl.h"
#include "test/integration/utility.h"
#include "test/test_common/network_utility.h"
#include "test/test_common/utility.h"
#include "absl/strings/str_cat.h"
#include "absl/synchronization/notification.h"
using namespace std::chrono_literals;
using std::chrono::milliseconds;
using testing::AssertionFailure;
using testing::AssertionResult;
using testing::AssertionSuccess;
namespace Envoy {
FakeStream::FakeStream(FakeHttpConnection& parent, Http::ResponseEncoder& encoder,
Event::TestTimeSystem& time_system)
: parent_(parent), encoder_(encoder), time_system_(time_system),
header_validator_(parent.makeHeaderValidator()) {
encoder.getStream().addCallbacks(*this);
}
void FakeStream::decodeHeaders(Http::RequestHeaderMapSharedPtr&& headers, bool end_stream) {
absl::MutexLock lock(&lock_);
headers_ = std::move(headers);
if (header_validator_) {
header_validator_->transformRequestHeaders(*headers_);
}
setEndStream(end_stream);
}
void FakeStream::decodeData(Buffer::Instance& data, bool end_stream) {
received_data_ = true;
absl::MutexLock lock(&lock_);
body_.add(data);
setEndStream(end_stream);
}
void FakeStream::decodeTrailers(Http::RequestTrailerMapPtr&& trailers) {
absl::MutexLock lock(&lock_);
setEndStream(true);
trailers_ = std::move(trailers);
}
void FakeStream::decodeMetadata(Http::MetadataMapPtr&& metadata_map_ptr) {
for (const auto& metadata : *metadata_map_ptr) {
duplicated_metadata_key_count_[metadata.first]++;
metadata_map_.insert(metadata);
}
}
void FakeStream::postToConnectionThread(std::function<void()> cb) {
parent_.postToConnectionThread(cb);
}
void FakeStream::encode1xxHeaders(const Http::ResponseHeaderMap& headers) {
std::shared_ptr<Http::ResponseHeaderMap> headers_copy(
Http::createHeaderMap<Http::ResponseHeaderMapImpl>(headers));
postToConnectionThread([this, headers_copy]() -> void {
{
absl::MutexLock lock(&lock_);
if (!parent_.connected() || saw_reset_) {
// Encoded already deleted.
return;
}
}
encoder_.encode1xxHeaders(*headers_copy);
});
}
void FakeStream::encodeHeaders(const Http::HeaderMap& headers, bool end_stream) {
std::shared_ptr<Http::ResponseHeaderMap> headers_copy(
Http::createHeaderMap<Http::ResponseHeaderMapImpl>(headers));
if (add_served_by_header_) {
headers_copy->addCopy(Http::LowerCaseString("x-served-by"),
parent_.connection().connectionInfoProvider().localAddress()->asString());
}
if (header_validator_) {
// Ignore validation results
auto result = header_validator_->transformResponseHeaders(*headers_copy);
if (result.new_headers) {
headers_copy = std::move(result.new_headers);
}
}
postToConnectionThread([this, headers_copy = std::move(headers_copy), end_stream]() -> void {
{
absl::MutexLock lock(&lock_);
if (!parent_.connected() || saw_reset_) {
// Encoded already deleted.
return;
}
}
encoder_.encodeHeaders(*headers_copy, end_stream);
});
}
void FakeStream::encodeData(std::string data, bool end_stream) {
postToConnectionThread([this, data, end_stream]() -> void {
{
absl::MutexLock lock(&lock_);
if (!parent_.connected() || saw_reset_) {
// Encoded already deleted.
return;
}
}
Buffer::OwnedImpl fake_data(data.data(), data.size());
encoder_.encodeData(fake_data, end_stream);
});
}
void FakeStream::encodeData(uint64_t size, bool end_stream) {
postToConnectionThread([this, size, end_stream]() -> void {
{
absl::MutexLock lock(&lock_);
if (!parent_.connected() || saw_reset_) {
// Encoded already deleted.
return;
}
}
Buffer::OwnedImpl data(std::string(size, 'a'));
encoder_.encodeData(data, end_stream);
});
}
void FakeStream::encodeData(Buffer::Instance& data, bool end_stream) {
std::shared_ptr<Buffer::Instance> data_copy = std::make_shared<Buffer::OwnedImpl>(data);
postToConnectionThread([this, data_copy, end_stream]() -> void {
{
absl::MutexLock lock(&lock_);
if (!parent_.connected() || saw_reset_) {
// Encoded already deleted.
return;
}
}
encoder_.encodeData(*data_copy, end_stream);
});
}
void FakeStream::encodeTrailers(const Http::HeaderMap& trailers) {
std::shared_ptr<Http::ResponseTrailerMap> trailers_copy(
Http::createHeaderMap<Http::ResponseTrailerMapImpl>(trailers));
postToConnectionThread([this, trailers_copy]() -> void {
{
absl::MutexLock lock(&lock_);
if (!parent_.connected() || saw_reset_) {
// Encoded already deleted.
return;
}
}
encoder_.encodeTrailers(*trailers_copy);
});
}
void FakeStream::encodeResetStream() {
postToConnectionThread([this]() -> void {
{
absl::MutexLock lock(&lock_);
if (!parent_.connected() || saw_reset_) {
// Encoded already deleted.
return;
}
}
if (parent_.type() == Http::CodecType::HTTP1) {
parent_.connection().close(Network::ConnectionCloseType::FlushWrite);
} else {
encoder_.getStream().resetStream(Http::StreamResetReason::LocalReset);
}
});
}
void FakeStream::encodeMetadata(const Http::MetadataMapVector& metadata_map_vector) {
postToConnectionThread([this, &metadata_map_vector]() -> void {
{
absl::MutexLock lock(&lock_);
if (!parent_.connected() || saw_reset_) {
// Encoded already deleted.
return;
}
}
encoder_.encodeMetadata(metadata_map_vector);
});
}
void FakeStream::readDisable(bool disable) {
postToConnectionThread([this, disable]() -> void {
{
absl::MutexLock lock(&lock_);
if (!parent_.connected() || saw_reset_) {
// Encoded already deleted.
return;
}
}
encoder_.getStream().readDisable(disable);
});
}
void FakeStream::onResetStream(Http::StreamResetReason, absl::string_view) {
absl::MutexLock lock(&lock_);
saw_reset_ = true;
}
AssertionResult FakeStream::waitForHeadersComplete(milliseconds timeout) {
absl::MutexLock lock(&lock_);
const auto reached = [this]()
ABSL_EXCLUSIVE_LOCKS_REQUIRED(lock_) { return headers_ != nullptr; };
if (!time_system_.waitFor(lock_, absl::Condition(&reached), timeout)) {
return AssertionFailure() << "Timed out waiting for headers.";
}
return AssertionSuccess();
}
namespace {
// Perform a wait on a condition while still allowing for periodic client dispatcher runs that
// occur on the current thread.
bool waitForWithDispatcherRun(Event::TestTimeSystem& time_system, absl::Mutex& lock,
const std::function<bool()>& condition,
Event::Dispatcher& client_dispatcher, milliseconds timeout)
ABSL_EXCLUSIVE_LOCKS_REQUIRED(lock) {
Event::TestTimeSystem::RealTimeBound bound(timeout);
while (bound.withinBound()) {
// Wake up periodically to run the client dispatcher.
if (time_system.waitFor(lock, absl::Condition(&condition), 5ms * TIMEOUT_FACTOR)) {
return true;
}
// Run the client dispatcher since we may need to process window updates, etc.
client_dispatcher.run(Event::Dispatcher::RunType::NonBlock);
}
return false;
}
} // namespace
AssertionResult FakeStream::waitForData(Event::Dispatcher& client_dispatcher, uint64_t body_length,
milliseconds timeout) {
absl::MutexLock lock(&lock_);
if (!waitForWithDispatcherRun(
time_system_, lock_,
[this, body_length]()
ABSL_EXCLUSIVE_LOCKS_REQUIRED(lock_) { return (body_.length() >= body_length); },
client_dispatcher, timeout)) {
return AssertionFailure() << "Timed out waiting for data.";
}
return AssertionSuccess();
}
AssertionResult FakeStream::waitForData(Event::Dispatcher& client_dispatcher,
absl::string_view data, milliseconds timeout) {
auto succeeded = waitForData(client_dispatcher, data.length(), timeout);
if (succeeded) {
Buffer::OwnedImpl buffer(data.data(), data.length());
if (!TestUtility::buffersEqual(body(), buffer)) {
return AssertionFailure() << body().toString() << " not equal to " << data;
}
}
return succeeded;
}
AssertionResult FakeStream::waitForData(Event::Dispatcher& client_dispatcher,
const FakeStream::ValidatorFunction& data_validator,
std::chrono::milliseconds timeout) {
absl::MutexLock lock(&lock_);
if (!waitForWithDispatcherRun(
time_system_, lock_,
[this, data_validator]()
ABSL_EXCLUSIVE_LOCKS_REQUIRED(lock_) { return data_validator(body_.toString()); },
client_dispatcher, timeout)) {
return AssertionFailure() << "Timed out waiting for data.";
}
return AssertionSuccess();
}
AssertionResult FakeStream::waitForEndStream(Event::Dispatcher& client_dispatcher,
milliseconds timeout) {
absl::MutexLock lock(&lock_);
if (!waitForWithDispatcherRun(
time_system_, lock_,
[this]() ABSL_EXCLUSIVE_LOCKS_REQUIRED(lock_) { return end_stream_; }, client_dispatcher,
timeout)) {
return AssertionFailure() << "Timed out waiting for end of stream.";
}
return AssertionSuccess();
}
AssertionResult FakeStream::waitForReset(milliseconds timeout) {
absl::MutexLock lock(&lock_);
if (!time_system_.waitFor(lock_, absl::Condition(&saw_reset_), timeout)) {
return AssertionFailure() << "Timed out waiting for reset.";
}
return AssertionSuccess();
}
void FakeStream::startGrpcStream(bool send_headers) {
ASSERT(!grpc_stream_started_, "gRPC stream should not be started more than once");
grpc_stream_started_ = true;
if (send_headers) {
encodeHeaders(Http::TestResponseHeaderMapImpl{{":status", "200"}}, false);
}
}
void FakeStream::finishGrpcStream(Grpc::Status::GrpcStatus status) {
encodeTrailers(Http::TestResponseTrailerMapImpl{
{"grpc-status", std::to_string(static_cast<uint32_t>(status))}});
}
class TestHttp1ServerConnectionImpl : public Http::Http1::ServerConnectionImpl {
public:
using Http::Http1::ServerConnectionImpl::ServerConnectionImpl;
};
class TestHttp2ServerConnectionImpl : public Http::Http2::ServerConnectionImpl {
public:
TestHttp2ServerConnectionImpl(
Network::Connection& connection, Http::ServerConnectionCallbacks& callbacks,
Http::Http2::CodecStats& stats, Random::RandomGenerator& random_generator,
const envoy::config::core::v3::Http2ProtocolOptions& http2_options,
const uint32_t max_request_headers_kb, const uint32_t max_request_headers_count,
envoy::config::core::v3::HttpProtocolOptions::HeadersWithUnderscoresAction
headers_with_underscores_action,
Server::OverloadManager& overload_manager)
: ServerConnectionImpl(connection, callbacks, stats, random_generator, http2_options,
max_request_headers_kb, max_request_headers_count,
headers_with_underscores_action, overload_manager) {}
void updateConcurrentStreams(uint32_t max_streams) {
absl::InlinedVector<http2::adapter::Http2Setting, 1> settings;
settings.push_back({http2::adapter::MAX_CONCURRENT_STREAMS, max_streams});
adapter_->SubmitSettings(settings);
const int rc = adapter_->Send();
ASSERT(rc == 0);
}
};
namespace {
// Fake upstream codec will not do path normalization, so the tests can observe
// the path forwarded by Envoy.
::envoy::extensions::http::header_validators::envoy_default::v3::HeaderValidatorConfig
fakeUpstreamHeaderValidatorConfig() {
::envoy::extensions::http::header_validators::envoy_default::v3::HeaderValidatorConfig config;
config.mutable_uri_path_normalization_options()->set_skip_path_normalization(true);
config.mutable_uri_path_normalization_options()->set_skip_merging_slashes(true);
config.mutable_uri_path_normalization_options()->set_path_with_escaped_slashes_action(
::envoy::extensions::http::header_validators::envoy_default::v3::HeaderValidatorConfig::
UriPathNormalizationOptions::KEEP_UNCHANGED);
return config;
}
} // namespace
FakeHttpConnection::FakeHttpConnection(
FakeUpstream& fake_upstream, SharedConnectionWrapper& shared_connection, Http::CodecType type,
Event::TestTimeSystem& time_system, uint32_t max_request_headers_kb,
uint32_t max_request_headers_count,
envoy::config::core::v3::HttpProtocolOptions::HeadersWithUnderscoresAction
headers_with_underscores_action)
: FakeConnectionBase(shared_connection, time_system), type_(type),
header_validator_factory_(
IntegrationUtil::makeHeaderValidationFactory(fakeUpstreamHeaderValidatorConfig())) {
ASSERT(max_request_headers_count != 0);
if (type == Http::CodecType::HTTP1) {
Http::Http1Settings http1_settings;
http1_settings.use_balsa_parser_ =
Runtime::runtimeFeatureEnabled("envoy.reloadable_features.http1_use_balsa_parser");
// For the purpose of testing, we always have the upstream encode the trailers if any
http1_settings.enable_trailers_ = true;
Http::Http1::CodecStats& stats = fake_upstream.http1CodecStats();
codec_ = std::make_unique<TestHttp1ServerConnectionImpl>(
shared_connection_.connection(), stats, *this, http1_settings, max_request_headers_kb,
max_request_headers_count, headers_with_underscores_action, overload_manager_);
} else if (type == Http::CodecType::HTTP2) {
envoy::config::core::v3::Http2ProtocolOptions http2_options = fake_upstream.http2Options();
Http::Http2::CodecStats& stats = fake_upstream.http2CodecStats();
codec_ = std::make_unique<TestHttp2ServerConnectionImpl>(
shared_connection_.connection(), *this, stats, random_, http2_options,
max_request_headers_kb, max_request_headers_count, headers_with_underscores_action,
overload_manager_);
} else {
ASSERT(type == Http::CodecType::HTTP3);
#ifdef ENVOY_ENABLE_QUIC
Http::Http3::CodecStats& stats = fake_upstream.http3CodecStats();
codec_ = std::make_unique<Quic::QuicHttpServerConnectionImpl>(
dynamic_cast<Quic::EnvoyQuicServerSession&>(shared_connection_.connection()), *this, stats,
fake_upstream.http3Options(), max_request_headers_kb, max_request_headers_count,
headers_with_underscores_action);
#else
ASSERT(false, "running a QUIC integration test without compiling QUIC");
#endif
}
shared_connection_.connection().addReadFilter(
Network::ReadFilterSharedPtr{new ReadFilter(*this)});
}
AssertionResult FakeConnectionBase::close(std::chrono::milliseconds timeout) {
ENVOY_LOG(trace, "FakeConnectionBase close");
if (!shared_connection_.connected()) {
return AssertionSuccess();
}
return shared_connection_.executeOnDispatcher(
[](Network::Connection& connection) {
connection.close(Network::ConnectionCloseType::FlushWrite);
},
timeout);
}
AssertionResult FakeConnectionBase::close(Network::ConnectionCloseType close_type,
std::chrono::milliseconds timeout) {
ENVOY_LOG(trace, "FakeConnectionBase close type={}", static_cast<int>(close_type));
if (!shared_connection_.connected()) {
return AssertionSuccess();
}
return shared_connection_.executeOnDispatcher(
[&close_type](Network::Connection& connection) { connection.close(close_type); }, timeout);
}
AssertionResult FakeConnectionBase::readDisable(bool disable, std::chrono::milliseconds timeout) {
return shared_connection_.executeOnDispatcher(
[disable](Network::Connection& connection) { connection.readDisable(disable); }, timeout);
}
namespace {
Http::Protocol codeTypeToProtocol(Http::CodecType codec_type) {
switch (codec_type) {
case Http::CodecType::HTTP1:
return Http::Protocol::Http11;
case Http::CodecType::HTTP2:
return Http::Protocol::Http2;
case Http::CodecType::HTTP3:
return Http::Protocol::Http3;
}
PANIC_DUE_TO_CORRUPT_ENUM;
}
} // namespace
Http::ServerHeaderValidatorPtr FakeHttpConnection::makeHeaderValidator() {
return header_validator_factory_ ? header_validator_factory_->createServerHeaderValidator(
codeTypeToProtocol(type_), header_validator_stats_)
: nullptr;
}
Http::RequestDecoder& FakeHttpConnection::newStream(Http::ResponseEncoder& encoder, bool) {
absl::MutexLock lock(&lock_);
new_streams_.emplace_back(new FakeStream(*this, encoder, time_system_));
return *new_streams_.back();
}
void FakeHttpConnection::onGoAway(Http::GoAwayErrorCode code) {
ASSERT(type_ != Http::CodecType::HTTP1);
// Usually indicates connection level errors, no operations are needed since
// the connection will be closed soon.
ENVOY_LOG(info, "FakeHttpConnection receives GOAWAY: ", static_cast<int>(code));
}
void FakeHttpConnection::encodeGoAway() {
ASSERT(type_ != Http::CodecType::HTTP1);
postToConnectionThread([this]() { codec_->goAway(); });
}
void FakeHttpConnection::updateConcurrentStreams(uint64_t max_streams) {
ASSERT(type_ != Http::CodecType::HTTP1);
if (type_ == Http::CodecType::HTTP2) {
postToConnectionThread([this, max_streams]() {
auto codec = dynamic_cast<TestHttp2ServerConnectionImpl*>(codec_.get());
codec->updateConcurrentStreams(max_streams);
});
} else {
#ifdef ENVOY_ENABLE_QUIC
postToConnectionThread([this, max_streams]() {
auto codec = dynamic_cast<Quic::QuicHttpServerConnectionImpl*>(codec_.get());
quic::test::QuicSessionPeer::SetMaxOpenIncomingBidirectionalStreams(
&codec->quicServerSession(), max_streams);
codec->quicServerSession().SendMaxStreams(1, false);
});
#else
UNREFERENCED_PARAMETER(max_streams);
#endif
}
}
void FakeHttpConnection::encodeProtocolError() {
ASSERT(type_ != Http::CodecType::HTTP1);
Http::Http2::ServerConnectionImpl* codec =
dynamic_cast<Http::Http2::ServerConnectionImpl*>(codec_.get());
ASSERT(codec != nullptr);
postToConnectionThread([codec]() {
Http::Status status = codec->protocolErrorForTest();
ASSERT(Http::getStatusCode(status) == Http::StatusCode::CodecProtocolError);
});
}
AssertionResult FakeConnectionBase::waitForDisconnect(milliseconds timeout) {
ENVOY_LOG(trace, "FakeConnectionBase waiting for disconnect");
absl::MutexLock lock(&lock_);
const auto reached = [this]() ABSL_EXCLUSIVE_LOCKS_REQUIRED(lock_) {
return !shared_connection_.connectedLockHeld();
};
if (!time_system_.waitFor(lock_, absl::Condition(&reached), timeout)) {
if (timeout == TestUtility::DefaultTimeout) {
ADD_FAILURE() << "Please don't waitForDisconnect with a 5s timeout if failure is expected\n";
}
return AssertionFailure() << "Timed out waiting for disconnect.";
}
ENVOY_LOG(trace, "FakeConnectionBase done waiting for disconnect");
return AssertionSuccess();
}
AssertionResult FakeConnectionBase::waitForRstDisconnect(std::chrono::milliseconds timeout) {
ENVOY_LOG(trace, "FakeConnectionBase waiting for RST disconnect");
absl::MutexLock lock(&lock_);
const auto reached = [this]() ABSL_EXCLUSIVE_LOCKS_REQUIRED(lock_) {
return shared_connection_.rstDisconnected();
};
if (!time_system_.waitFor(lock_, absl::Condition(&reached), timeout)) {
if (timeout == TestUtility::DefaultTimeout) {
ADD_FAILURE()
<< "Please don't waitForRstDisconnect with a 5s timeout if failure is expected\n";
}
return AssertionFailure() << "Timed out waiting for RST disconnect.";
}
ENVOY_LOG(trace, "FakeConnectionBase done waiting for RST disconnect");
return AssertionSuccess();
}
AssertionResult FakeConnectionBase::waitForHalfClose(milliseconds timeout) {
absl::MutexLock lock(&lock_);
if (!time_system_.waitFor(lock_, absl::Condition(&half_closed_), timeout)) {
return AssertionFailure() << "Timed out waiting for half close.";
}
return AssertionSuccess();
}
AssertionResult FakeConnectionBase::waitForNoPost(milliseconds timeout) {
absl::MutexLock lock(&lock_);
if (!time_system_.waitFor(
lock_,
absl::Condition(
[](void* fake_connection) -> bool {
return static_cast<FakeConnectionBase*>(fake_connection)->pending_cbs_ == 0;
},
this),
timeout)) {
return AssertionFailure() << "Timed out waiting for ops on this connection";
}
return AssertionSuccess();
}
void FakeConnectionBase::postToConnectionThread(std::function<void()> cb) {
++pending_cbs_;
dispatcher_.post([this, cb]() {
cb();
{
// Snag this lock not because it's needed but so waitForNoPost doesn't stall
absl::MutexLock lock(&lock_);
--pending_cbs_;
}
});
}
AssertionResult FakeHttpConnection::waitForNewStream(Event::Dispatcher& client_dispatcher,
FakeStreamPtr& stream,
std::chrono::milliseconds timeout) {
absl::MutexLock lock(&lock_);
if (!waitForWithDispatcherRun(
time_system_, lock_,
[this]() ABSL_EXCLUSIVE_LOCKS_REQUIRED(lock_) { return !new_streams_.empty(); },
client_dispatcher, timeout)) {
return AssertionFailure() << "Timed out waiting for new stream.";
}
stream = std::move(new_streams_.front());
new_streams_.pop_front();
return AssertionSuccess();
}
FakeUpstream::FakeUpstream(Network::DownstreamTransportSocketFactoryPtr&& transport_socket_factory,
const std::string& uds_path, const FakeUpstreamConfig& config)
: FakeUpstream(std::move(transport_socket_factory),
Network::SocketPtr{new Network::UdsListenSocket(
*Network::Address::PipeInstance::create(uds_path))},
config) {}
static Network::SocketPtr
makeTcpListenSocket(const Network::Address::InstanceConstSharedPtr& address) {
return std::make_unique<Network::TcpListenSocket>(address, nullptr, true);
}
static Network::Address::InstanceConstSharedPtr makeAddress(uint32_t port,
Network::Address::IpVersion version) {
return Network::Utility::parseInternetAddressNoThrow(
Network::Test::getLoopbackAddressString(version), port);
}
static Network::SocketPtr
makeUdpListenSocket(const Network::Address::InstanceConstSharedPtr& address) {
auto socket = std::make_unique<Network::UdpListenSocket>(address, nullptr, true);
// TODO(mattklein123): These options are set in multiple locations. We should centralize them for
// UDP listeners.
socket->addOptions(Network::SocketOptionFactory::buildIpPacketInfoOptions());
socket->addOptions(Network::SocketOptionFactory::buildRxQueueOverFlowOptions());
return socket;
}
static Network::SocketPtr
makeListenSocket(const FakeUpstreamConfig& config,
const Network::Address::InstanceConstSharedPtr& address) {
return (config.udp_fake_upstream_.has_value() ? makeUdpListenSocket(address)
: makeTcpListenSocket(address));
}
FakeUpstream::FakeUpstream(uint32_t port, Network::Address::IpVersion version,
const FakeUpstreamConfig& config, const bool defer_initialization)
: FakeUpstream(Network::Test::createRawBufferDownstreamSocketFactory(),
makeListenSocket(config, makeAddress(port, version)), config,
defer_initialization) {}
FakeUpstream::FakeUpstream(Network::DownstreamTransportSocketFactoryPtr&& transport_socket_factory,
const Network::Address::InstanceConstSharedPtr& address,
const FakeUpstreamConfig& config)
: FakeUpstream(std::move(transport_socket_factory), makeListenSocket(config, address), config) {
}
FakeUpstream::FakeUpstream(Network::DownstreamTransportSocketFactoryPtr&& transport_socket_factory,
uint32_t port, Network::Address::IpVersion version,
const FakeUpstreamConfig& config)
: FakeUpstream(std::move(transport_socket_factory),
makeListenSocket(config, makeAddress(port, version)), config) {}
FakeUpstream::FakeUpstream(Network::DownstreamTransportSocketFactoryPtr&& transport_socket_factory,
Network::SocketPtr&& listen_socket, const FakeUpstreamConfig& config,
const bool defer_initialization)
: http_type_(config.upstream_protocol_), http2_options_(config.http2_options_),
http3_options_(config.http3_options_), quic_options_(config.quic_options_),
socket_(Network::SocketSharedPtr(listen_socket.release())),
api_(Api::createApiForTest(stats_store_)), time_system_(config.time_system_),
dispatcher_(api_->allocateDispatcher("fake_upstream")),
handler_(new Server::ConnectionHandlerImpl(*dispatcher_, 0)), config_(config),
read_disable_on_new_connection_(true), enable_half_close_(config.enable_half_close_),
listener_(*this, http_type_ == Http::CodecType::HTTP3),
filter_chain_(Network::Test::createEmptyFilterChain(std::move(transport_socket_factory))),
stats_scope_(stats_store_.createScope("test_server_scope")) {
socket_factories_.emplace_back(std::make_unique<FakeListenSocketFactory>(socket_));
ENVOY_LOG(info, "starting fake server at {}. UDP={} codec={}", localAddress()->asString(),
config.udp_fake_upstream_.has_value(), FakeHttpConnection::typeToString(http_type_));
if (config.udp_fake_upstream_.has_value() &&
config.udp_fake_upstream_->max_rx_datagram_size_.has_value()) {
listener_.udp_listener_config_.config_.mutable_downstream_socket_config()
->mutable_max_rx_datagram_size()
->set_value(config.udp_fake_upstream_->max_rx_datagram_size_.value());
}
if (!defer_initialization) {
initializeServer();
}
}
FakeUpstream::~FakeUpstream() { cleanUp(); };
void FakeUpstream::initializeServer() {
if (initialized_) {
// Already initialized.
return;
}
dispatcher_->post([this]() -> void {
EXPECT_TRUE(socket_factories_[0]->doFinalPreWorkerInit().ok());
handler_->addListener(absl::nullopt, listener_, runtime_, random_);
server_initialized_.setReady();
});
thread_ = api_->threadFactory().createThread([this]() -> void { threadRoutine(); });
server_initialized_.waitReady();
initialized_ = true;
}
void FakeUpstream::cleanUp() {
if (thread_.get()) {
dispatcher_->exit();
thread_->join();
thread_.reset();
}
}
bool FakeUpstream::createNetworkFilterChain(Network::Connection& connection,
const Filter::NetworkFilterFactoriesList&) {
absl::MutexLock lock(&lock_);
if (read_disable_on_new_connection_ && http_type_ != Http::CodecType::HTTP3) {
// Disable early close detection to avoid closing the network connection before full
// initialization is complete.
connection.detectEarlyCloseWhenReadDisabled(false);
connection.readDisable(true);
if (disable_and_do_not_enable_) {
dynamic_cast<Network::ConnectionImpl*>(&connection)->ioHandle().enableFileEvents(0);
}
}
auto connection_wrapper = std::make_unique<SharedConnectionWrapper>(connection);
LinkedList::moveIntoListBack(std::move(connection_wrapper), new_connections_);
// Normally we don't associate a logical network connection with a FakeHttpConnection until
// waitForHttpConnection is called, but QUIC needs to be set up as packets come in, so we do
// not lazily create for HTTP/3
if (http_type_ == Http::CodecType::HTTP3) {
quic_connections_.push_back(std::make_unique<FakeHttpConnection>(
*this, consumeConnection(), http_type_, time_system_, config_.max_request_headers_kb_,
config_.max_request_headers_count_, config_.headers_with_underscores_action_));
quic_connections_.back()->initialize();
}
return true;
}
bool FakeUpstream::createListenerFilterChain(Network::ListenerFilterManager&) { return true; }
void FakeUpstream::createUdpListenerFilterChain(Network::UdpListenerFilterManager& udp_listener,
Network::UdpReadFilterCallbacks& callbacks) {
udp_listener.addReadFilter(std::make_unique<FakeUdpFilter>(*this, callbacks));
}
bool FakeUpstream::createQuicListenerFilterChain(Network::QuicListenerFilterManager&) {
return true;
}
void FakeUpstream::threadRoutine() {
dispatcher_->run(Event::Dispatcher::RunType::Block);
handler_.reset();
{
absl::MutexLock lock(&lock_);
new_connections_.clear();
quic_connections_.clear();
consumed_connections_.clear();
}
}
AssertionResult FakeUpstream::waitForHttpConnection(Event::Dispatcher& client_dispatcher,
FakeHttpConnectionPtr& connection,
milliseconds timeout) {
if (!initialized_) {
return AssertionFailure()
<< "Must initialize the FakeUpstream first by calling initializeServer().";
}
{
absl::MutexLock lock(&lock_);
// As noted in createNetworkFilterChain, HTTP3 FakeHttpConnections are not
// lazily created, so HTTP3 needs a different wait path here.
if (http_type_ == Http::CodecType::HTTP3) {
if (quic_connections_.empty() &&
!waitForWithDispatcherRun(
time_system_, lock_,
[this]() ABSL_EXCLUSIVE_LOCKS_REQUIRED(lock_) { return !quic_connections_.empty(); },
client_dispatcher, timeout)) {
return AssertionFailure() << "Timed out waiting for new quic connection.";
}
if (!quic_connections_.empty()) {
connection = std::move(quic_connections_.front());
quic_connections_.pop_front();
return AssertionSuccess();
}
}
if (!waitForWithDispatcherRun(
time_system_, lock_,
[this]() ABSL_EXCLUSIVE_LOCKS_REQUIRED(lock_) { return !new_connections_.empty(); },
client_dispatcher, timeout)) {
if (timeout == TestUtility::DefaultTimeout) {
ADD_FAILURE()
<< "Please don't waitForHttpConnection with a 5s timeout if failure is expected\n";
}
return AssertionFailure() << "Timed out waiting for new connection.";
}
}
return runOnDispatcherThreadAndWait([&]() {
absl::MutexLock lock(&lock_);
connection = std::make_unique<FakeHttpConnection>(
*this, consumeConnection(), http_type_, time_system_, config_.max_request_headers_kb_,
config_.max_request_headers_count_, config_.headers_with_underscores_action_);
connection->initialize();
return AssertionSuccess();
});
}
absl::StatusOr<int>
FakeUpstream::waitForHttpConnection(Event::Dispatcher& client_dispatcher,
std::vector<std::unique_ptr<FakeUpstream>>& upstreams,
FakeHttpConnectionPtr& connection, milliseconds timeout) {
if (upstreams.empty()) {
return absl::InternalError("No upstreams configured.");
}
Event::TestTimeSystem::RealTimeBound bound(timeout);
while (bound.withinBound()) {
for (size_t i = 0; i < upstreams.size(); ++i) {
FakeUpstream& upstream = *upstreams[i];
{
absl::MutexLock lock(&upstream.lock_);
if (!upstream.isInitialized()) {
return absl::InternalError(
"Must initialize the FakeUpstream first by calling initializeServer().");
}
if (!waitForWithDispatcherRun(
upstream.time_system_, upstream.lock_,
[&upstream]() ABSL_EXCLUSIVE_LOCKS_REQUIRED(upstream.lock_) {
return !upstream.new_connections_.empty();
},
client_dispatcher, 5ms)) {
continue;
}
}
EXPECT_TRUE(upstream.runOnDispatcherThreadAndWait([&]() {
absl::MutexLock lock(&upstream.lock_);
connection = std::make_unique<FakeHttpConnection>(
upstream, upstream.consumeConnection(), upstream.http_type_, upstream.timeSystem(),
Http::DEFAULT_MAX_REQUEST_HEADERS_KB, Http::DEFAULT_MAX_HEADERS_COUNT,
envoy::config::core::v3::HttpProtocolOptions::ALLOW);
connection->initialize();
return AssertionSuccess();
}));
return i;
}
}
return absl::InternalError("Timed out waiting for HTTP connection.");
}
ABSL_MUST_USE_RESULT
AssertionResult FakeUpstream::assertPendingConnectionsEmpty() {
return runOnDispatcherThreadAndWait([&]() {
absl::MutexLock lock(&lock_);
return new_connections_.empty() ? AssertionSuccess() : AssertionFailure();
});
}
AssertionResult FakeUpstream::waitForRawConnection(FakeRawConnectionPtr& connection,
milliseconds timeout) {
if (!initialized_) {
return AssertionFailure()
<< "Must initialize the FakeUpstream first by calling initializeServer().";
}
{
absl::MutexLock lock(&lock_);
const auto reached = [this]() ABSL_EXCLUSIVE_LOCKS_REQUIRED(lock_) {
return !new_connections_.empty();
};
ENVOY_LOG(debug, "waiting for raw connection");
if (!time_system_.waitFor(lock_, absl::Condition(&reached), timeout)) {
return AssertionFailure() << "Timed out waiting for raw connection";
}
}
return runOnDispatcherThreadAndWait([&]() {
absl::MutexLock lock(&lock_);
connection = makeRawConnection(consumeConnection(), timeSystem());
connection->initialize();
// Skip enableHalfClose if the connection is already disconnected.
if (connection->connected()) {
connection->connection().enableHalfClose(enable_half_close_);
}
return AssertionSuccess();
});
}
void FakeUpstream::convertFromRawToHttp(FakeRawConnectionPtr& raw_connection,
FakeHttpConnectionPtr& connection) {
absl::MutexLock lock(&lock_);
SharedConnectionWrapper& shared_connection = raw_connection->sharedConnection();
connection = std::make_unique<FakeHttpConnection>(
*this, shared_connection, http_type_, time_system_, config_.max_request_headers_kb_,
config_.max_request_headers_count_, config_.headers_with_underscores_action_);
connection->initialize();
raw_connection.release();
}
SharedConnectionWrapper& FakeUpstream::consumeConnection() {
ASSERT(!new_connections_.empty());
auto* const connection_wrapper = new_connections_.front().get();
// Skip the thread safety check if the network connection has already been freed since there's no
// alternate way to get access to the dispatcher.
ASSERT(!connection_wrapper->connected() || connection_wrapper->dispatcher().isThreadSafe());
connection_wrapper->setParented();
connection_wrapper->moveBetweenLists(new_connections_, consumed_connections_);
if (read_disable_on_new_connection_ && connection_wrapper->connected() &&
http_type_ != Http::CodecType::HTTP3 && !disable_and_do_not_enable_) {
// Re-enable read and early close detection.
auto& connection = connection_wrapper->connection();
connection.detectEarlyCloseWhenReadDisabled(true);
connection.readDisable(false);
}
return *connection_wrapper;
}
AssertionResult FakeUpstream::waitForUdpDatagram(Network::UdpRecvData& data_to_fill,
std::chrono::milliseconds timeout) {
if (!initialized_) {
return AssertionFailure()
<< "Must initialize the FakeUpstream first by calling initializeServer().";
}
absl::MutexLock lock(&lock_);
const auto reached = [this]() ABSL_EXCLUSIVE_LOCKS_REQUIRED(lock_) {
return !received_datagrams_.empty();
};
if (!time_system_.waitFor(lock_, absl::Condition(&reached), timeout)) {
return AssertionFailure() << "Timed out waiting for UDP datagram.";
}
data_to_fill = std::move(received_datagrams_.front());
received_datagrams_.pop_front();
return AssertionSuccess();
}
Network::FilterStatus FakeUpstream::onRecvDatagram(Network::UdpRecvData& data) {
absl::MutexLock lock(&lock_);
received_datagrams_.emplace_back(std::move(data));
return Network::FilterStatus::StopIteration;
}
AssertionResult FakeUpstream::runOnDispatcherThreadAndWait(std::function<AssertionResult()> cb,
std::chrono::milliseconds timeout) {
auto result = std::make_shared<AssertionResult>(AssertionSuccess());
auto done = std::make_shared<absl::Notification>();
ASSERT(!dispatcher_->isThreadSafe());
dispatcher_->post([&]() {
*result = cb();
done->Notify();
});
RELEASE_ASSERT(done->WaitForNotificationWithTimeout(absl::FromChrono(timeout)),
"Timed out waiting for cb to run on dispatcher");
return *result;
}
void FakeUpstream::runOnDispatcherThread(std::function<void()> cb) {
ASSERT(!dispatcher_->isThreadSafe());
dispatcher_->post([&]() { cb(); });
}
void FakeUpstream::sendUdpDatagram(const std::string& buffer,
const Network::Address::InstanceConstSharedPtr& peer) {
dispatcher_->post([this, buffer, peer] {
const auto rc = Network::Utility::writeToSocket(socket_->ioHandle(), Buffer::OwnedImpl(buffer),
nullptr, *peer);
EXPECT_TRUE(rc.return_value_ == buffer.length());
});
}
AssertionResult FakeUpstream::rawWriteConnection(uint32_t index, const std::string& data,
bool end_stream,
std::chrono::milliseconds timeout) {
if (!initialized_) {
return AssertionFailure()
<< "Must initialize the FakeUpstream first by calling initializeServer().";
}
absl::MutexLock lock(&lock_);
auto iter = consumed_connections_.begin();
std::advance(iter, index);
return (*iter)->executeOnDispatcher(
[data, end_stream](Network::Connection& connection) {
ASSERT(connection.state() == Network::Connection::State::Open);
Buffer::OwnedImpl buffer(data);
connection.write(buffer, end_stream);
},
timeout);
}
absl::Status FakeUpstream::FakeListenSocketFactory::doFinalPreWorkerInit() {
if (socket_->socketType() == Network::Socket::Type::Stream) {
EXPECT_EQ(0, socket_->ioHandle().listen(ENVOY_TCP_BACKLOG_SIZE).return_value_);