blob: 194d5ceca755394eeec5c144996ac2e8f981abbd [file] [log] [blame]
QUICHE teama6ef0a62019-03-07 20:34:33 -05001// Copyright (c) 2012 The Chromium Authors. All rights reserved.
2// Use of this source code is governed by a BSD-style license that can be
3// found in the LICENSE file.
4
5#include <cstddef>
6#include <cstdint>
7#include <list>
8#include <memory>
9#include <ostream>
vasilvv872e7a32019-03-12 16:42:44 -070010#include <string>
QUICHE teama6ef0a62019-03-07 20:34:33 -050011#include <utility>
12#include <vector>
13
14#include "net/third_party/quiche/src/quic/core/crypto/null_encrypter.h"
15#include "net/third_party/quiche/src/quic/core/http/quic_spdy_client_stream.h"
16#include "net/third_party/quiche/src/quic/core/quic_epoll_connection_helper.h"
17#include "net/third_party/quiche/src/quic/core/quic_error_codes.h"
18#include "net/third_party/quiche/src/quic/core/quic_framer.h"
19#include "net/third_party/quiche/src/quic/core/quic_packet_creator.h"
20#include "net/third_party/quiche/src/quic/core/quic_packet_writer_wrapper.h"
21#include "net/third_party/quiche/src/quic/core/quic_packets.h"
22#include "net/third_party/quiche/src/quic/core/quic_session.h"
23#include "net/third_party/quiche/src/quic/core/quic_utils.h"
24#include "net/third_party/quiche/src/quic/platform/api/quic_epoll.h"
25#include "net/third_party/quiche/src/quic/platform/api/quic_error_code_wrappers.h"
26#include "net/third_party/quiche/src/quic/platform/api/quic_expect_bug.h"
27#include "net/third_party/quiche/src/quic/platform/api/quic_flags.h"
28#include "net/third_party/quiche/src/quic/platform/api/quic_logging.h"
29#include "net/third_party/quiche/src/quic/platform/api/quic_port_utils.h"
30#include "net/third_party/quiche/src/quic/platform/api/quic_ptr_util.h"
31#include "net/third_party/quiche/src/quic/platform/api/quic_sleep.h"
32#include "net/third_party/quiche/src/quic/platform/api/quic_socket_address.h"
33#include "net/third_party/quiche/src/quic/platform/api/quic_str_cat.h"
QUICHE teama6ef0a62019-03-07 20:34:33 -050034#include "net/third_party/quiche/src/quic/platform/api/quic_string_piece.h"
35#include "net/third_party/quiche/src/quic/platform/api/quic_test.h"
36#include "net/third_party/quiche/src/quic/platform/api/quic_test_loopback.h"
37#include "net/third_party/quiche/src/quic/platform/api/quic_text_utils.h"
38#include "net/quic/platform/impl/quic_socket_utils.h"
39#include "net/third_party/quiche/src/quic/test_tools/bad_packet_writer.h"
40#include "net/third_party/quiche/src/quic/test_tools/crypto_test_utils.h"
41#include "net/third_party/quiche/src/quic/test_tools/packet_dropping_test_writer.h"
42#include "net/third_party/quiche/src/quic/test_tools/packet_reordering_writer.h"
43#include "net/third_party/quiche/src/quic/test_tools/quic_client_peer.h"
44#include "net/third_party/quiche/src/quic/test_tools/quic_config_peer.h"
45#include "net/third_party/quiche/src/quic/test_tools/quic_connection_peer.h"
46#include "net/third_party/quiche/src/quic/test_tools/quic_dispatcher_peer.h"
47#include "net/third_party/quiche/src/quic/test_tools/quic_flow_controller_peer.h"
48#include "net/third_party/quiche/src/quic/test_tools/quic_sent_packet_manager_peer.h"
49#include "net/third_party/quiche/src/quic/test_tools/quic_server_peer.h"
50#include "net/third_party/quiche/src/quic/test_tools/quic_session_peer.h"
51#include "net/third_party/quiche/src/quic/test_tools/quic_spdy_session_peer.h"
52#include "net/third_party/quiche/src/quic/test_tools/quic_stream_id_manager_peer.h"
53#include "net/third_party/quiche/src/quic/test_tools/quic_stream_peer.h"
54#include "net/third_party/quiche/src/quic/test_tools/quic_stream_sequencer_peer.h"
55#include "net/third_party/quiche/src/quic/test_tools/quic_test_client.h"
56#include "net/third_party/quiche/src/quic/test_tools/quic_test_server.h"
57#include "net/third_party/quiche/src/quic/test_tools/quic_test_utils.h"
58#include "net/third_party/quiche/src/quic/test_tools/server_thread.h"
59#include "net/third_party/quiche/src/quic/tools/quic_backend_response.h"
60#include "net/third_party/quiche/src/quic/tools/quic_client.h"
61#include "net/third_party/quiche/src/quic/tools/quic_memory_cache_backend.h"
62#include "net/third_party/quiche/src/quic/tools/quic_server.h"
63#include "net/third_party/quiche/src/quic/tools/quic_simple_client_stream.h"
64#include "net/third_party/quiche/src/quic/tools/quic_simple_server_stream.h"
65
66using spdy::kV3LowestPriority;
67using spdy::SETTINGS_MAX_HEADER_LIST_SIZE;
68using spdy::SpdyFramer;
69using spdy::SpdyHeaderBlock;
70using spdy::SpdySerializedFrame;
71using spdy::SpdySettingsIR;
72
73namespace quic {
74namespace test {
75namespace {
76
77const char kFooResponseBody[] = "Artichoke hearts make me happy.";
78const char kBarResponseBody[] = "Palm hearts are pretty delicious, also.";
79const float kSessionToStreamRatio = 1.5;
80
81// Run all tests with the cross products of all versions.
82struct TestParams {
83 TestParams(const ParsedQuicVersionVector& client_supported_versions,
84 const ParsedQuicVersionVector& server_supported_versions,
85 ParsedQuicVersion negotiated_version,
86 bool client_supports_stateless_rejects,
87 bool server_uses_stateless_rejects_if_peer_supported,
88 QuicTag congestion_control_tag,
89 bool use_cheap_stateless_reject)
90 : client_supported_versions(client_supported_versions),
91 server_supported_versions(server_supported_versions),
92 negotiated_version(negotiated_version),
93 client_supports_stateless_rejects(client_supports_stateless_rejects),
94 server_uses_stateless_rejects_if_peer_supported(
95 server_uses_stateless_rejects_if_peer_supported),
96 congestion_control_tag(congestion_control_tag),
97 use_cheap_stateless_reject(use_cheap_stateless_reject) {}
98
99 friend std::ostream& operator<<(std::ostream& os, const TestParams& p) {
100 os << "{ server_supported_versions: "
101 << ParsedQuicVersionVectorToString(p.server_supported_versions);
102 os << " client_supported_versions: "
103 << ParsedQuicVersionVectorToString(p.client_supported_versions);
104 os << " negotiated_version: "
105 << ParsedQuicVersionToString(p.negotiated_version);
106 os << " client_supports_stateless_rejects: "
107 << p.client_supports_stateless_rejects;
108 os << " server_uses_stateless_rejects_if_peer_supported: "
109 << p.server_uses_stateless_rejects_if_peer_supported;
110 os << " congestion_control_tag: "
111 << QuicTagToString(p.congestion_control_tag);
112 os << " use_cheap_stateless_reject: " << p.use_cheap_stateless_reject
113 << " }";
114 return os;
115 }
116
117 ParsedQuicVersionVector client_supported_versions;
118 ParsedQuicVersionVector server_supported_versions;
119 ParsedQuicVersion negotiated_version;
120 bool client_supports_stateless_rejects;
121 bool server_uses_stateless_rejects_if_peer_supported;
122 QuicTag congestion_control_tag;
123 bool use_cheap_stateless_reject;
124};
125
126// Constructs various test permutations.
127std::vector<TestParams> GetTestParams(bool use_tls_handshake,
128 bool test_stateless_rejects) {
129 QuicFlagSaver flags;
130 // Divide the versions into buckets in which the intra-frame format
131 // is compatible. When clients encounter QUIC version negotiation
132 // they simply retransmit all packets using the new version's
133 // QUIC framing. However, they are unable to change the intra-frame
134 // layout (for example to change HTTP/2 headers to SPDY/3, or a change in the
135 // handshake protocol). So these tests need to ensure that clients are never
136 // attempting to do 0-RTT across incompatible versions. Chromium only
137 // supports a single version at a time anyway. :)
wub05787cf2019-05-01 10:26:55 -0700138 SetQuicFlag(&FLAGS_quic_supports_tls_handshake, use_tls_handshake);
QUICHE teama6ef0a62019-03-07 20:34:33 -0500139 ParsedQuicVersionVector all_supported_versions =
140 FilterSupportedVersions(AllSupportedVersions());
141
142 // Buckets are separated by versions: versions prior to QUIC_VERSION_47 use
143 // STREAM frames for the handshake, and only have QUIC crypto as the handshake
144 // protocol. Version 47 and greater use CRYPTO frames for the handshake, and
145 // must also be split based on the handshake protocol. If the handshake
146 // protocol (QUIC crypto or TLS) changes, the ClientHello/CHLO must be
147 // reconstructed for the correct protocol.
148 ParsedQuicVersionVector version_buckets[3];
149
150 for (const ParsedQuicVersion& version : all_supported_versions) {
QUICHE teamea740082019-03-11 17:58:43 -0700151 if (!QuicVersionUsesCryptoFrames(version.transport_version)) {
QUICHE teama6ef0a62019-03-07 20:34:33 -0500152 version_buckets[0].push_back(version);
153 } else if (version.handshake_protocol == PROTOCOL_QUIC_CRYPTO) {
154 version_buckets[1].push_back(version);
155 } else {
156 version_buckets[2].push_back(version);
157 }
158 }
159
160 // This must be kept in sync with the number of nested for-loops below as it
161 // is used to prune the number of tests that are run.
162 const int kMaxEnabledOptions = 4;
163 int max_enabled_options = 0;
164 std::vector<TestParams> params;
165 for (const QuicTag congestion_control_tag : {kRENO, kTBBR, kQBIC, kTPCC}) {
166 for (bool server_uses_stateless_rejects_if_peer_supported : {true, false}) {
167 for (bool client_supports_stateless_rejects : {true, false}) {
168 for (bool use_cheap_stateless_reject : {true, false}) {
169 int enabled_options = 0;
170 if (congestion_control_tag != kQBIC) {
171 ++enabled_options;
172 }
173 if (client_supports_stateless_rejects) {
174 ++enabled_options;
175 }
176 if (server_uses_stateless_rejects_if_peer_supported) {
177 ++enabled_options;
178 }
179 if (use_cheap_stateless_reject) {
180 ++enabled_options;
181 }
182 CHECK_GE(kMaxEnabledOptions, enabled_options);
183 if (enabled_options > max_enabled_options) {
184 max_enabled_options = enabled_options;
185 }
186
187 // Run tests with no options, a single option, or all the
188 // options enabled to avoid a combinatorial explosion.
189 if (enabled_options > 1 && enabled_options < kMaxEnabledOptions) {
190 continue;
191 }
192
193 // There are many stateless reject combinations, so don't test them
194 // unless requested.
195 if ((server_uses_stateless_rejects_if_peer_supported ||
196 client_supports_stateless_rejects ||
197 use_cheap_stateless_reject) &&
198 !test_stateless_rejects) {
199 continue;
200 }
201
202 for (const ParsedQuicVersionVector& client_versions :
203 version_buckets) {
204 if (FilterSupportedVersions(client_versions).empty()) {
205 continue;
206 }
207 // Add an entry for server and client supporting all
208 // versions.
209 params.push_back(TestParams(
210 client_versions, all_supported_versions,
211 client_versions.front(), client_supports_stateless_rejects,
212 server_uses_stateless_rejects_if_peer_supported,
213 congestion_control_tag, use_cheap_stateless_reject));
214
215 // Run version negotiation tests tests with no options, or
216 // all the options enabled to avoid a combinatorial
217 // explosion.
218 if (enabled_options > 1 && enabled_options < kMaxEnabledOptions) {
219 continue;
220 }
221
222 // Test client supporting all versions and server supporting
223 // 1 version. Simulate an old server and exercise version
224 // downgrade in the client. Protocol negotiation should
225 // occur. Skip the i = 0 case because it is essentially the
226 // same as the default case.
227 for (size_t i = 1; i < client_versions.size(); ++i) {
228 ParsedQuicVersionVector server_supported_versions;
229 server_supported_versions.push_back(client_versions[i]);
230 if (FilterSupportedVersions(server_supported_versions).empty()) {
231 continue;
232 }
233 params.push_back(TestParams(
234 client_versions, server_supported_versions,
235 server_supported_versions.front(),
236 client_supports_stateless_rejects,
237 server_uses_stateless_rejects_if_peer_supported,
238 congestion_control_tag, use_cheap_stateless_reject));
239 } // End of inner version loop.
240 } // End of outer version loop.
241 } // End of use_cheap_stateless_reject loop.
242 } // End of client_supports_stateless_rejects loop.
243 } // End of server_uses_stateless_rejects_if_peer_supported loop.
244 } // End of congestion_control_tag loop.
245 CHECK_EQ(kMaxEnabledOptions, max_enabled_options);
246 return params;
247}
248
249class ServerDelegate : public PacketDroppingTestWriter::Delegate {
250 public:
251 explicit ServerDelegate(QuicDispatcher* dispatcher)
252 : dispatcher_(dispatcher) {}
253 ~ServerDelegate() override = default;
254 void OnCanWrite() override { dispatcher_->OnCanWrite(); }
255
256 private:
257 QuicDispatcher* dispatcher_;
258};
259
260class ClientDelegate : public PacketDroppingTestWriter::Delegate {
261 public:
262 explicit ClientDelegate(QuicClient* client) : client_(client) {}
263 ~ClientDelegate() override = default;
264 void OnCanWrite() override {
265 QuicEpollEvent event(EPOLLOUT);
266 client_->epoll_network_helper()->OnEvent(client_->GetLatestFD(), &event);
267 }
268
269 private:
270 QuicClient* client_;
271};
272
273class EndToEndTest : public QuicTestWithParam<TestParams> {
274 protected:
275 EndToEndTest()
276 : initialized_(false),
277 connect_to_server_on_initialize_(true),
278 server_address_(
279 QuicSocketAddress(TestLoopback(), QuicPickUnusedPortOrDie())),
280 server_hostname_("test.example.com"),
281 client_writer_(nullptr),
282 server_writer_(nullptr),
283 negotiated_version_(UnsupportedQuicVersion()),
284 chlo_multiplier_(0),
285 stream_factory_(nullptr),
286 support_server_push_(false),
QUICHE team8e2e4532019-03-14 14:37:56 -0700287 override_connection_id_(nullptr),
288 expected_connection_id_length_(kQuicDefaultConnectionIdLength) {
wub05787cf2019-05-01 10:26:55 -0700289 SetQuicFlag(&FLAGS_quic_supports_tls_handshake, true);
QUICHE teama6ef0a62019-03-07 20:34:33 -0500290 SetQuicRestartFlag(quic_no_server_conn_ver_negotiation2, true);
291 SetQuicReloadableFlag(quic_no_client_conn_ver_negotiation, true);
292 client_supported_versions_ = GetParam().client_supported_versions;
293 server_supported_versions_ = GetParam().server_supported_versions;
294 negotiated_version_ = GetParam().negotiated_version;
295
296 QUIC_LOG(INFO) << "Using Configuration: " << GetParam();
297
298 // Use different flow control windows for client/server.
299 client_config_.SetInitialStreamFlowControlWindowToSend(
300 2 * kInitialStreamFlowControlWindowForTest);
301 client_config_.SetInitialSessionFlowControlWindowToSend(
302 2 * kInitialSessionFlowControlWindowForTest);
303 server_config_.SetInitialStreamFlowControlWindowToSend(
304 3 * kInitialStreamFlowControlWindowForTest);
305 server_config_.SetInitialSessionFlowControlWindowToSend(
306 3 * kInitialSessionFlowControlWindowForTest);
307
308 // The default idle timeouts can be too strict when running on a busy
309 // machine.
310 const QuicTime::Delta timeout = QuicTime::Delta::FromSeconds(30);
311 client_config_.set_max_time_before_crypto_handshake(timeout);
312 client_config_.set_max_idle_time_before_crypto_handshake(timeout);
313 server_config_.set_max_time_before_crypto_handshake(timeout);
314 server_config_.set_max_idle_time_before_crypto_handshake(timeout);
315
316 AddToCache("/foo", 200, kFooResponseBody);
317 AddToCache("/bar", 200, kBarResponseBody);
318 }
319
320 ~EndToEndTest() override { QuicRecyclePort(server_address_.port()); }
321
322 virtual void CreateClientWithWriter() {
323 client_.reset(CreateQuicClient(client_writer_));
324 }
325
326 QuicTestClient* CreateQuicClient(QuicPacketWriterWrapper* writer) {
327 QuicTestClient* client =
328 new QuicTestClient(server_address_, server_hostname_, client_config_,
329 client_supported_versions_,
330 crypto_test_utils::ProofVerifierForTesting());
331 client->UseWriter(writer);
332 if (!pre_shared_key_client_.empty()) {
333 client->client()->SetPreSharedKey(pre_shared_key_client_);
334 }
335 if (override_connection_id_ != nullptr) {
336 client->UseConnectionId(*override_connection_id_);
337 }
338 client->Connect();
339 return client;
340 }
341
342 void set_smaller_flow_control_receive_window() {
343 const uint32_t kClientIFCW = 64 * 1024;
344 const uint32_t kServerIFCW = 1024 * 1024;
345 set_client_initial_stream_flow_control_receive_window(kClientIFCW);
346 set_client_initial_session_flow_control_receive_window(
347 kSessionToStreamRatio * kClientIFCW);
348 set_server_initial_stream_flow_control_receive_window(kServerIFCW);
349 set_server_initial_session_flow_control_receive_window(
350 kSessionToStreamRatio * kServerIFCW);
351 }
352
353 void set_client_initial_stream_flow_control_receive_window(uint32_t window) {
354 CHECK(client_ == nullptr);
355 QUIC_DLOG(INFO) << "Setting client initial stream flow control window: "
356 << window;
357 client_config_.SetInitialStreamFlowControlWindowToSend(window);
358 }
359
360 void set_client_initial_session_flow_control_receive_window(uint32_t window) {
361 CHECK(client_ == nullptr);
362 QUIC_DLOG(INFO) << "Setting client initial session flow control window: "
363 << window;
364 client_config_.SetInitialSessionFlowControlWindowToSend(window);
365 }
366
367 void set_server_initial_stream_flow_control_receive_window(uint32_t window) {
368 CHECK(server_thread_ == nullptr);
369 QUIC_DLOG(INFO) << "Setting server initial stream flow control window: "
370 << window;
371 server_config_.SetInitialStreamFlowControlWindowToSend(window);
372 }
373
374 void set_server_initial_session_flow_control_receive_window(uint32_t window) {
375 CHECK(server_thread_ == nullptr);
376 QUIC_DLOG(INFO) << "Setting server initial session flow control window: "
377 << window;
378 server_config_.SetInitialSessionFlowControlWindowToSend(window);
379 }
380
381 const QuicSentPacketManager* GetSentPacketManagerFromFirstServerSession() {
382 return &GetServerConnection()->sent_packet_manager();
383 }
384
385 QuicConnection* GetServerConnection() {
386 return GetServerSession()->connection();
387 }
388
389 QuicSession* GetServerSession() {
390 QuicDispatcher* dispatcher =
391 QuicServerPeer::GetDispatcher(server_thread_->server());
392 EXPECT_EQ(1u, dispatcher->session_map().size());
393 return dispatcher->session_map().begin()->second.get();
394 }
395
396 bool Initialize() {
397 QuicTagVector copt;
398 server_config_.SetConnectionOptionsToSend(copt);
399 copt = client_extra_copts_;
400
401 // TODO(nimia): Consider setting the congestion control algorithm for the
402 // client as well according to the test parameter.
403 copt.push_back(GetParam().congestion_control_tag);
404 if (GetParam().congestion_control_tag == kTPCC &&
405 GetQuicReloadableFlag(quic_enable_pcc3)) {
406 copt.push_back(kTPCC);
407 }
408
409 if (GetParam().client_supports_stateless_rejects) {
410 copt.push_back(kSREJ);
411 }
412 client_config_.SetConnectionOptionsToSend(copt);
413
414 // Start the server first, because CreateQuicClient() attempts
415 // to connect to the server.
416 StartServer();
417
418 if (!connect_to_server_on_initialize_) {
419 initialized_ = true;
420 return true;
421 }
422
423 CreateClientWithWriter();
424 static QuicEpollEvent event(EPOLLOUT);
425 if (client_writer_ != nullptr) {
426 client_writer_->Initialize(
427 QuicConnectionPeer::GetHelper(
428 client_->client()->client_session()->connection()),
429 QuicConnectionPeer::GetAlarmFactory(
430 client_->client()->client_session()->connection()),
431 QuicMakeUnique<ClientDelegate>(client_->client()));
432 }
433 initialized_ = true;
434 return client_->client()->connected();
435 }
436
437 void SetUp() override {
438 // The ownership of these gets transferred to the QuicPacketWriterWrapper
439 // when Initialize() is executed.
440 client_writer_ = new PacketDroppingTestWriter();
441 server_writer_ = new PacketDroppingTestWriter();
442 }
443
444 void TearDown() override {
445 ASSERT_TRUE(initialized_) << "You must call Initialize() in every test "
446 << "case. Otherwise, your test will leak memory.";
447 StopServer();
448 }
449
450 void StartServer() {
451 SetQuicReloadableFlag(quic_use_cheap_stateless_rejects,
452 GetParam().use_cheap_stateless_reject);
453
QUICHE team8e2e4532019-03-14 14:37:56 -0700454 auto* test_server = new QuicTestServer(
455 crypto_test_utils::ProofSourceForTesting(), server_config_,
456 server_supported_versions_, &memory_cache_backend_,
457 expected_connection_id_length_);
QUICHE teama6ef0a62019-03-07 20:34:33 -0500458 server_thread_ = QuicMakeUnique<ServerThread>(test_server, server_address_);
459 if (chlo_multiplier_ != 0) {
460 server_thread_->server()->SetChloMultiplier(chlo_multiplier_);
461 }
462 if (!pre_shared_key_server_.empty()) {
463 server_thread_->server()->SetPreSharedKey(pre_shared_key_server_);
464 }
465 server_thread_->Initialize();
466 QuicDispatcher* dispatcher =
467 QuicServerPeer::GetDispatcher(server_thread_->server());
468 QuicDispatcherPeer::UseWriter(dispatcher, server_writer_);
469
470 SetQuicReloadableFlag(
471 enable_quic_stateless_reject_support,
472 GetParam().server_uses_stateless_rejects_if_peer_supported);
473
474 server_writer_->Initialize(QuicDispatcherPeer::GetHelper(dispatcher),
475 QuicDispatcherPeer::GetAlarmFactory(dispatcher),
476 QuicMakeUnique<ServerDelegate>(dispatcher));
477 if (stream_factory_ != nullptr) {
478 static_cast<QuicTestServer*>(server_thread_->server())
479 ->SetSpdyStreamFactory(stream_factory_);
480 }
481
482 server_thread_->Start();
483 }
484
485 void StopServer() {
486 if (server_thread_) {
487 server_thread_->Quit();
488 server_thread_->Join();
489 }
490 }
491
492 void AddToCache(QuicStringPiece path,
493 int response_code,
494 QuicStringPiece body) {
495 memory_cache_backend_.AddSimpleResponse(server_hostname_, path,
496 response_code, body);
497 }
498
499 void SetPacketLossPercentage(int32_t loss) {
500 client_writer_->set_fake_packet_loss_percentage(loss);
501 server_writer_->set_fake_packet_loss_percentage(loss);
502 }
503
504 void SetPacketSendDelay(QuicTime::Delta delay) {
505 client_writer_->set_fake_packet_delay(delay);
506 server_writer_->set_fake_packet_delay(delay);
507 }
508
509 void SetReorderPercentage(int32_t reorder) {
510 client_writer_->set_fake_reorder_percentage(reorder);
511 server_writer_->set_fake_reorder_percentage(reorder);
512 }
513
514 // Verifies that the client and server connections were both free of packets
515 // being discarded, based on connection stats.
516 // Calls server_thread_ Pause() and Resume(), which may only be called once
517 // per test.
518 void VerifyCleanConnection(bool had_packet_loss) {
519 QuicConnectionStats client_stats =
520 client_->client()->client_session()->connection()->GetStats();
521 // TODO(ianswett): Determine why this becomes even more flaky with BBR
522 // enabled. b/62141144
523 if (!had_packet_loss && !GetQuicReloadableFlag(quic_default_to_bbr)) {
524 EXPECT_EQ(0u, client_stats.packets_lost);
525 }
526 EXPECT_EQ(0u, client_stats.packets_discarded);
527 // When doing 0-RTT with stateless rejects, the encrypted requests cause
528 // a retranmission of the SREJ packets which are dropped by the client.
529 // When client starts with an unsupported version, the version negotiation
530 // packet sent by server for the old connection (respond for the connection
531 // close packet) will be dropped by the client.
532 if (!BothSidesSupportStatelessRejects() &&
533 !ServerSendsVersionNegotiation()) {
534 EXPECT_EQ(0u, client_stats.packets_dropped);
535 }
536 if (!ClientSupportsIetfQuicNotSupportedByServer()) {
537 // In this case, if client sends 0-RTT POST with v99, receives IETF
538 // version negotiation packet and speaks a GQUIC version. Server processes
539 // this connection in time wait list and keeps sending IETF version
540 // negotiation packet for incoming packets. But these version negotiation
541 // packets cannot be processed by the client speaking GQUIC.
542 EXPECT_EQ(client_stats.packets_received, client_stats.packets_processed);
543 }
544
545 const int num_expected_stateless_rejects =
546 (BothSidesSupportStatelessRejects() &&
547 client_->client()->client_session()->GetNumSentClientHellos() > 0)
548 ? 1
549 : 0;
550 EXPECT_EQ(num_expected_stateless_rejects,
551 client_->client()->num_stateless_rejects_received());
552
553 server_thread_->Pause();
554 QuicConnectionStats server_stats = GetServerConnection()->GetStats();
555 if (!had_packet_loss) {
556 EXPECT_EQ(0u, server_stats.packets_lost);
557 }
558 EXPECT_EQ(0u, server_stats.packets_discarded);
559 // TODO(ianswett): Restore the check for packets_dropped equals 0.
560 // The expect for packets received is equal to packets processed fails
561 // due to version negotiation packets.
562 server_thread_->Resume();
563 }
564
565 bool BothSidesSupportStatelessRejects() {
566 return (GetParam().server_uses_stateless_rejects_if_peer_supported &&
567 GetParam().client_supports_stateless_rejects);
568 }
569
570 // Client supports IETF QUIC, while it is not supported by server.
571 bool ClientSupportsIetfQuicNotSupportedByServer() {
572 return GetParam().client_supported_versions[0].transport_version >
573 QUIC_VERSION_43 &&
574 FilterSupportedVersions(GetParam().server_supported_versions)[0]
575 .transport_version <= QUIC_VERSION_43;
576 }
577
578 // Returns true when client starts with an unsupported version, and client
579 // closes connection when version negotiation is received.
580 bool ServerSendsVersionNegotiation() {
581 return GetQuicReloadableFlag(quic_no_client_conn_ver_negotiation) &&
582 GetParam().client_supported_versions[0] !=
583 GetParam().negotiated_version;
584 }
585
586 bool SupportsIetfQuicWithTls(ParsedQuicVersion version) {
587 return version.transport_version > QUIC_VERSION_43 &&
588 version.handshake_protocol == PROTOCOL_TLS1_3;
589 }
590
591 void ExpectFlowControlsSynced(QuicFlowController* client,
592 QuicFlowController* server) {
593 EXPECT_EQ(QuicFlowControllerPeer::SendWindowSize(client),
594 QuicFlowControllerPeer::ReceiveWindowSize(server));
595 EXPECT_EQ(QuicFlowControllerPeer::ReceiveWindowSize(client),
596 QuicFlowControllerPeer::SendWindowSize(server));
597 }
598
599 // Must be called before Initialize to have effect.
600 void SetSpdyStreamFactory(QuicTestServer::StreamFactory* factory) {
601 stream_factory_ = factory;
602 }
603
604 QuicStreamId GetNthClientInitiatedBidirectionalId(int n) {
605 return GetNthClientInitiatedBidirectionalStreamId(
606 client_->client()->client_session()->connection()->transport_version(),
607 n);
608 }
609
610 QuicStreamId GetNthServerInitiatedBidirectionalId(int n) {
611 return GetNthServerInitiatedBidirectionalStreamId(
612 client_->client()->client_session()->connection()->transport_version(),
613 n);
614 }
615
616 ScopedEnvironmentForThreads environment_;
617 bool initialized_;
618 // If true, the Initialize() function will create |client_| and starts to
619 // connect to the server.
620 // Default is true.
621 bool connect_to_server_on_initialize_;
622 QuicSocketAddress server_address_;
vasilvvc48c8712019-03-11 13:38:16 -0700623 std::string server_hostname_;
QUICHE teama6ef0a62019-03-07 20:34:33 -0500624 QuicMemoryCacheBackend memory_cache_backend_;
625 std::unique_ptr<ServerThread> server_thread_;
626 std::unique_ptr<QuicTestClient> client_;
627 PacketDroppingTestWriter* client_writer_;
628 PacketDroppingTestWriter* server_writer_;
629 QuicConfig client_config_;
630 QuicConfig server_config_;
631 ParsedQuicVersionVector client_supported_versions_;
632 ParsedQuicVersionVector server_supported_versions_;
633 QuicTagVector client_extra_copts_;
634 ParsedQuicVersion negotiated_version_;
635 size_t chlo_multiplier_;
636 QuicTestServer::StreamFactory* stream_factory_;
637 bool support_server_push_;
vasilvvc48c8712019-03-11 13:38:16 -0700638 std::string pre_shared_key_client_;
639 std::string pre_shared_key_server_;
QUICHE teama6ef0a62019-03-07 20:34:33 -0500640 QuicConnectionId* override_connection_id_;
QUICHE team8e2e4532019-03-14 14:37:56 -0700641 uint8_t expected_connection_id_length_;
QUICHE teama6ef0a62019-03-07 20:34:33 -0500642};
643
644// Run all end to end tests with all supported versions.
645INSTANTIATE_TEST_SUITE_P(EndToEndTests,
646 EndToEndTest,
647 ::testing::ValuesIn(GetTestParams(false, false)));
648
649class EndToEndTestWithTls : public EndToEndTest {};
650
651INSTANTIATE_TEST_SUITE_P(EndToEndTestsWithTls,
652 EndToEndTestWithTls,
653 ::testing::ValuesIn(GetTestParams(true, false)));
654
655class EndToEndTestWithStatelessReject : public EndToEndTest {};
656
657INSTANTIATE_TEST_SUITE_P(WithStatelessReject,
658 EndToEndTestWithStatelessReject,
659 ::testing::ValuesIn(GetTestParams(false, true)));
660
661TEST_P(EndToEndTestWithTls, HandshakeSuccessful) {
662 ASSERT_TRUE(Initialize());
663 EXPECT_TRUE(client_->client()->WaitForCryptoHandshakeConfirmed());
664 server_thread_->WaitForCryptoHandshakeConfirmed();
665 // There have been occasions where it seemed that negotiated_version_ and the
666 // version in the connection are not in sync. If it is happening, it has not
667 // been recreatable; this assert is here just to check and raise a flag if it
668 // happens.
669 ASSERT_EQ(
670 client_->client()->client_session()->connection()->transport_version(),
671 negotiated_version_.transport_version);
672
673 QuicCryptoStream* crypto_stream = QuicSessionPeer::GetMutableCryptoStream(
674 client_->client()->client_session());
675 QuicStreamSequencer* sequencer = QuicStreamPeer::sequencer(crypto_stream);
676 EXPECT_FALSE(QuicStreamSequencerPeer::IsUnderlyingBufferAllocated(sequencer));
677 server_thread_->Pause();
678 crypto_stream = QuicSessionPeer::GetMutableCryptoStream(GetServerSession());
679 sequencer = QuicStreamPeer::sequencer(crypto_stream);
680 EXPECT_FALSE(QuicStreamSequencerPeer::IsUnderlyingBufferAllocated(sequencer));
681}
682
683TEST_P(EndToEndTestWithStatelessReject, SimpleRequestResponseStatless) {
684 ASSERT_TRUE(Initialize());
685
686 EXPECT_EQ(kFooResponseBody, client_->SendSynchronousRequest("/foo"));
687 EXPECT_EQ("200", client_->response_headers()->find(":status")->second);
688 int expected_num_client_hellos = 2;
689 if (ServerSendsVersionNegotiation()) {
690 ++expected_num_client_hellos;
691 if (BothSidesSupportStatelessRejects()) {
692 ++expected_num_client_hellos;
693 }
694 }
695 EXPECT_EQ(expected_num_client_hellos,
696 client_->client()->GetNumSentClientHellos());
697}
698
699TEST_P(EndToEndTest, SimpleRequestResponse) {
700 ASSERT_TRUE(Initialize());
701
702 EXPECT_EQ(kFooResponseBody, client_->SendSynchronousRequest("/foo"));
703 EXPECT_EQ("200", client_->response_headers()->find(":status")->second);
704 int expected_num_client_hellos = 2;
705 if (ServerSendsVersionNegotiation()) {
706 ++expected_num_client_hellos;
707 if (BothSidesSupportStatelessRejects()) {
708 ++expected_num_client_hellos;
709 }
710 }
711 EXPECT_EQ(expected_num_client_hellos,
712 client_->client()->GetNumSentClientHellos());
713}
714
715TEST_P(EndToEndTest, SimpleRequestResponseZeroConnectionID) {
716 QuicConnectionId connection_id = QuicUtils::CreateZeroConnectionId(
717 GetParam().negotiated_version.transport_version);
718 override_connection_id_ = &connection_id;
QUICHE team8e2e4532019-03-14 14:37:56 -0700719 expected_connection_id_length_ = connection_id.length();
QUICHE teama6ef0a62019-03-07 20:34:33 -0500720 ASSERT_TRUE(Initialize());
721
722 EXPECT_EQ(kFooResponseBody, client_->SendSynchronousRequest("/foo"));
723 EXPECT_EQ("200", client_->response_headers()->find(":status")->second);
724 int expected_num_client_hellos = 2;
725 if (ServerSendsVersionNegotiation()) {
726 ++expected_num_client_hellos;
727 if (BothSidesSupportStatelessRejects()) {
728 ++expected_num_client_hellos;
729 }
730 }
731 EXPECT_EQ(expected_num_client_hellos,
732 client_->client()->GetNumSentClientHellos());
733 EXPECT_EQ(client_->client()->client_session()->connection()->connection_id(),
734 QuicUtils::CreateZeroConnectionId(
735 GetParam().negotiated_version.transport_version));
736}
737
QUICHE team8e2e4532019-03-14 14:37:56 -0700738TEST_P(EndToEndTest, BadConnectionIdLength) {
739 if (!QuicUtils::VariableLengthConnectionIdAllowedForVersion(
740 GetParam().negotiated_version.transport_version)) {
741 ASSERT_TRUE(Initialize());
742 return;
743 }
QUICHE teamc65d1d12019-03-19 20:58:04 -0700744 QuicConnectionId connection_id =
745 TestConnectionIdNineBytesLong(UINT64_C(0xBADbadBADbad));
QUICHE team8e2e4532019-03-14 14:37:56 -0700746 override_connection_id_ = &connection_id;
QUICHE teamc65d1d12019-03-19 20:58:04 -0700747 ASSERT_TRUE(Initialize());
748 EXPECT_EQ(kFooResponseBody, client_->SendSynchronousRequest("/foo"));
749 EXPECT_EQ("200", client_->response_headers()->find(":status")->second);
750 EXPECT_EQ(kQuicDefaultConnectionIdLength, client_->client()
751 ->client_session()
752 ->connection()
753 ->connection_id()
754 .length());
QUICHE team8e2e4532019-03-14 14:37:56 -0700755}
756
757TEST_P(EndToEndTest, MixGoodAndBadConnectionIdLengths) {
758 if (!QuicUtils::VariableLengthConnectionIdAllowedForVersion(
759 GetParam().negotiated_version.transport_version)) {
760 ASSERT_TRUE(Initialize());
761 return;
762 }
763
QUICHE teamc65d1d12019-03-19 20:58:04 -0700764 // Start client_ which will use a bad connection ID length.
765 QuicConnectionId connection_id =
766 TestConnectionIdNineBytesLong(UINT64_C(0xBADbadBADbad));
QUICHE team8e2e4532019-03-14 14:37:56 -0700767 override_connection_id_ = &connection_id;
QUICHE teamc65d1d12019-03-19 20:58:04 -0700768 ASSERT_TRUE(Initialize());
QUICHE team8e2e4532019-03-14 14:37:56 -0700769 override_connection_id_ = nullptr;
770
QUICHE teamc65d1d12019-03-19 20:58:04 -0700771 // Start client2 which will use a good connection ID length.
QUICHE team8e2e4532019-03-14 14:37:56 -0700772 std::unique_ptr<QuicTestClient> client2(CreateQuicClient(nullptr));
773 SpdyHeaderBlock headers;
774 headers[":method"] = "POST";
775 headers[":path"] = "/foo";
776 headers[":scheme"] = "https";
777 headers[":authority"] = server_hostname_;
778 headers["content-length"] = "3";
779 client2->SendMessage(headers, "", /*fin=*/false);
780 client2->SendData("eep", true);
QUICHE teamc65d1d12019-03-19 20:58:04 -0700781
782 EXPECT_EQ(kFooResponseBody, client_->SendSynchronousRequest("/foo"));
783 EXPECT_EQ("200", client_->response_headers()->find(":status")->second);
784 EXPECT_EQ(kQuicDefaultConnectionIdLength, client_->client()
785 ->client_session()
786 ->connection()
787 ->connection_id()
788 .length());
789
QUICHE team8e2e4532019-03-14 14:37:56 -0700790 client2->WaitForResponse();
791 EXPECT_EQ(kFooResponseBody, client2->response_body());
792 EXPECT_EQ("200", client2->response_headers()->find(":status")->second);
QUICHE teamc65d1d12019-03-19 20:58:04 -0700793 EXPECT_EQ(kQuicDefaultConnectionIdLength, client2->client()
794 ->client_session()
795 ->connection()
796 ->connection_id()
797 .length());
QUICHE team8e2e4532019-03-14 14:37:56 -0700798}
799
QUICHE teama6ef0a62019-03-07 20:34:33 -0500800TEST_P(EndToEndTest, SimpleRequestResponseWithLargeReject) {
801 chlo_multiplier_ = 1;
802 ASSERT_TRUE(Initialize());
803
804 EXPECT_EQ(kFooResponseBody, client_->SendSynchronousRequest("/foo"));
805 EXPECT_EQ("200", client_->response_headers()->find(":status")->second);
806 if (ServerSendsVersionNegotiation()) {
807 EXPECT_EQ(4, client_->client()->GetNumSentClientHellos());
808 } else {
809 EXPECT_EQ(3, client_->client()->GetNumSentClientHellos());
810 }
811}
812
813TEST_P(EndToEndTestWithTls, SimpleRequestResponsev6) {
814 server_address_ =
815 QuicSocketAddress(QuicIpAddress::Loopback6(), server_address_.port());
816 ASSERT_TRUE(Initialize());
817
818 EXPECT_EQ(kFooResponseBody, client_->SendSynchronousRequest("/foo"));
819 EXPECT_EQ("200", client_->response_headers()->find(":status")->second);
820}
821
822TEST_P(EndToEndTestWithTls, SeparateFinPacket) {
823 ASSERT_TRUE(Initialize());
824
825 // Send a request in two parts: the request and then an empty packet with FIN.
826 SpdyHeaderBlock headers;
827 headers[":method"] = "POST";
828 headers[":path"] = "/foo";
829 headers[":scheme"] = "https";
830 headers[":authority"] = server_hostname_;
831 client_->SendMessage(headers, "", /*fin=*/false);
832 client_->SendData("", true);
833 client_->WaitForResponse();
834 EXPECT_EQ(kFooResponseBody, client_->response_body());
835 EXPECT_EQ("200", client_->response_headers()->find(":status")->second);
836
837 // Now do the same thing but with a content length.
838 headers["content-length"] = "3";
839 client_->SendMessage(headers, "", /*fin=*/false);
840 client_->SendData("foo", true);
841 client_->WaitForResponse();
842 EXPECT_EQ(kFooResponseBody, client_->response_body());
843 EXPECT_EQ("200", client_->response_headers()->find(":status")->second);
844}
845
846TEST_P(EndToEndTestWithTls, MultipleRequestResponse) {
847 ASSERT_TRUE(Initialize());
848
849 EXPECT_EQ(kFooResponseBody, client_->SendSynchronousRequest("/foo"));
850 EXPECT_EQ("200", client_->response_headers()->find(":status")->second);
851 EXPECT_EQ(kBarResponseBody, client_->SendSynchronousRequest("/bar"));
852 EXPECT_EQ("200", client_->response_headers()->find(":status")->second);
853}
854
855TEST_P(EndToEndTest, MultipleRequestResponseZeroConnectionID) {
856 QuicConnectionId connection_id = QuicUtils::CreateZeroConnectionId(
857 GetParam().negotiated_version.transport_version);
858 override_connection_id_ = &connection_id;
QUICHE team8e2e4532019-03-14 14:37:56 -0700859 expected_connection_id_length_ = connection_id.length();
QUICHE teama6ef0a62019-03-07 20:34:33 -0500860 ASSERT_TRUE(Initialize());
861
862 EXPECT_EQ(kFooResponseBody, client_->SendSynchronousRequest("/foo"));
863 EXPECT_EQ("200", client_->response_headers()->find(":status")->second);
864 EXPECT_EQ(kBarResponseBody, client_->SendSynchronousRequest("/bar"));
865 EXPECT_EQ("200", client_->response_headers()->find(":status")->second);
866}
867
868TEST_P(EndToEndTestWithTls, MultipleStreams) {
869 // Verifies quic_test_client can track responses of all active streams.
870 ASSERT_TRUE(Initialize());
871
872 const int kNumRequests = 10;
873
874 SpdyHeaderBlock headers;
875 headers[":method"] = "POST";
876 headers[":path"] = "/foo";
877 headers[":scheme"] = "https";
878 headers[":authority"] = server_hostname_;
879 headers["content-length"] = "3";
880
881 for (int i = 0; i < kNumRequests; ++i) {
882 client_->SendMessage(headers, "bar", /*fin=*/true);
883 }
884
885 while (kNumRequests > client_->num_responses()) {
886 client_->ClearPerRequestState();
887 client_->WaitForResponse();
888 EXPECT_EQ(kFooResponseBody, client_->response_body());
889 EXPECT_EQ("200", client_->response_headers()->find(":status")->second);
890 }
891}
892
893TEST_P(EndToEndTestWithTls, MultipleClients) {
894 ASSERT_TRUE(Initialize());
895 std::unique_ptr<QuicTestClient> client2(CreateQuicClient(nullptr));
896
897 SpdyHeaderBlock headers;
898 headers[":method"] = "POST";
899 headers[":path"] = "/foo";
900 headers[":scheme"] = "https";
901 headers[":authority"] = server_hostname_;
902 headers["content-length"] = "3";
903
904 client_->SendMessage(headers, "", /*fin=*/false);
905 client2->SendMessage(headers, "", /*fin=*/false);
906
907 client_->SendData("bar", true);
908 client_->WaitForResponse();
909 EXPECT_EQ(kFooResponseBody, client_->response_body());
910 EXPECT_EQ("200", client_->response_headers()->find(":status")->second);
911
912 client2->SendData("eep", true);
913 client2->WaitForResponse();
914 EXPECT_EQ(kFooResponseBody, client2->response_body());
915 EXPECT_EQ("200", client2->response_headers()->find(":status")->second);
916}
917
918TEST_P(EndToEndTestWithTls, RequestOverMultiplePackets) {
919 // Send a large enough request to guarantee fragmentation.
vasilvvc48c8712019-03-11 13:38:16 -0700920 std::string huge_request =
dschinazi66dea072019-04-09 11:41:06 -0700921 "/some/path?query=" + std::string(kMaxOutgoingPacketSize, '.');
QUICHE teama6ef0a62019-03-07 20:34:33 -0500922 AddToCache(huge_request, 200, kBarResponseBody);
923
924 ASSERT_TRUE(Initialize());
925
926 EXPECT_EQ(kBarResponseBody, client_->SendSynchronousRequest(huge_request));
927 EXPECT_EQ("200", client_->response_headers()->find(":status")->second);
928}
929
930TEST_P(EndToEndTestWithTls, MultiplePacketsRandomOrder) {
931 // Send a large enough request to guarantee fragmentation.
vasilvvc48c8712019-03-11 13:38:16 -0700932 std::string huge_request =
dschinazi66dea072019-04-09 11:41:06 -0700933 "/some/path?query=" + std::string(kMaxOutgoingPacketSize, '.');
QUICHE teama6ef0a62019-03-07 20:34:33 -0500934 AddToCache(huge_request, 200, kBarResponseBody);
935
936 ASSERT_TRUE(Initialize());
937 SetPacketSendDelay(QuicTime::Delta::FromMilliseconds(2));
938 SetReorderPercentage(50);
939
940 EXPECT_EQ(kBarResponseBody, client_->SendSynchronousRequest(huge_request));
941 EXPECT_EQ("200", client_->response_headers()->find(":status")->second);
942}
943
944TEST_P(EndToEndTestWithTls, PostMissingBytes) {
945 ASSERT_TRUE(Initialize());
946
947 // Add a content length header with no body.
948 SpdyHeaderBlock headers;
949 headers[":method"] = "POST";
950 headers[":path"] = "/foo";
951 headers[":scheme"] = "https";
952 headers[":authority"] = server_hostname_;
953 headers["content-length"] = "3";
954
955 // This should be detected as stream fin without complete request,
956 // triggering an error response.
957 client_->SendCustomSynchronousRequest(headers, "");
958 EXPECT_EQ(QuicSimpleServerStream::kErrorResponseBody,
959 client_->response_body());
960 EXPECT_EQ("500", client_->response_headers()->find(":status")->second);
961}
962
963TEST_P(EndToEndTest, LargePostNoPacketLoss) {
964 ASSERT_TRUE(Initialize());
965
966 EXPECT_TRUE(client_->client()->WaitForCryptoHandshakeConfirmed());
967
968 // 1 MB body.
vasilvvc48c8712019-03-11 13:38:16 -0700969 std::string body(1024 * 1024, 'a');
QUICHE teama6ef0a62019-03-07 20:34:33 -0500970 SpdyHeaderBlock headers;
971 headers[":method"] = "POST";
972 headers[":path"] = "/foo";
973 headers[":scheme"] = "https";
974 headers[":authority"] = server_hostname_;
975
976 EXPECT_EQ(kFooResponseBody,
977 client_->SendCustomSynchronousRequest(headers, body));
978 // TODO(ianswett): There should not be packet loss in this test, but on some
979 // platforms the receive buffer overflows.
980 VerifyCleanConnection(true);
981}
982
983TEST_P(EndToEndTest, LargePostNoPacketLoss1sRTT) {
984 ASSERT_TRUE(Initialize());
985 SetPacketSendDelay(QuicTime::Delta::FromMilliseconds(1000));
986
987 EXPECT_TRUE(client_->client()->WaitForCryptoHandshakeConfirmed());
988
989 // 100 KB body.
vasilvvc48c8712019-03-11 13:38:16 -0700990 std::string body(100 * 1024, 'a');
QUICHE teama6ef0a62019-03-07 20:34:33 -0500991 SpdyHeaderBlock headers;
992 headers[":method"] = "POST";
993 headers[":path"] = "/foo";
994 headers[":scheme"] = "https";
995 headers[":authority"] = server_hostname_;
996
997 EXPECT_EQ(kFooResponseBody,
998 client_->SendCustomSynchronousRequest(headers, body));
999 VerifyCleanConnection(false);
1000}
1001
1002TEST_P(EndToEndTest, LargePostWithPacketLoss) {
1003 if (!BothSidesSupportStatelessRejects()) {
1004 // Connect with lower fake packet loss than we'd like to test.
1005 // Until b/10126687 is fixed, losing handshake packets is pretty
1006 // brutal.
1007 // TODO(jokulik): Until we support redundant SREJ packets, don't
1008 // drop handshake packets for stateless rejects.
1009 SetPacketLossPercentage(5);
1010 }
1011 ASSERT_TRUE(Initialize());
1012
1013 // Wait for the server SHLO before upping the packet loss.
1014 EXPECT_TRUE(client_->client()->WaitForCryptoHandshakeConfirmed());
1015 SetPacketLossPercentage(30);
1016
1017 // 10 KB body.
vasilvvc48c8712019-03-11 13:38:16 -07001018 std::string body(1024 * 10, 'a');
QUICHE teama6ef0a62019-03-07 20:34:33 -05001019 SpdyHeaderBlock headers;
1020 headers[":method"] = "POST";
1021 headers[":path"] = "/foo";
1022 headers[":scheme"] = "https";
1023 headers[":authority"] = server_hostname_;
1024
1025 EXPECT_EQ(kFooResponseBody,
1026 client_->SendCustomSynchronousRequest(headers, body));
1027 VerifyCleanConnection(true);
1028}
1029
1030// Regression test for b/80090281.
1031TEST_P(EndToEndTest, LargePostWithPacketLossAndAlwaysBundleWindowUpdates) {
1032 ASSERT_TRUE(Initialize());
1033
1034 // Wait for the server SHLO before upping the packet loss.
1035 EXPECT_TRUE(client_->client()->WaitForCryptoHandshakeConfirmed());
1036 server_thread_->WaitForCryptoHandshakeConfirmed();
1037
1038 // Normally server only bundles a retransmittable frame once every other
1039 // kMaxConsecutiveNonRetransmittablePackets ack-only packets. Setting the max
1040 // to 0 to reliably reproduce b/80090281.
1041 server_thread_->Schedule([this]() {
1042 QuicConnectionPeer::SetMaxConsecutiveNumPacketsWithNoRetransmittableFrames(
1043 GetServerConnection(), 0);
1044 });
1045
1046 SetPacketLossPercentage(30);
1047
1048 // 10 KB body.
vasilvvc48c8712019-03-11 13:38:16 -07001049 std::string body(1024 * 10, 'a');
QUICHE teama6ef0a62019-03-07 20:34:33 -05001050 SpdyHeaderBlock headers;
1051 headers[":method"] = "POST";
1052 headers[":path"] = "/foo";
1053 headers[":scheme"] = "https";
1054 headers[":authority"] = server_hostname_;
1055
1056 EXPECT_EQ(kFooResponseBody,
1057 client_->SendCustomSynchronousRequest(headers, body));
1058 VerifyCleanConnection(true);
1059}
1060
1061TEST_P(EndToEndTest, LargePostWithPacketLossAndBlockedSocket) {
1062 if (!BothSidesSupportStatelessRejects()) {
1063 // Connect with lower fake packet loss than we'd like to test. Until
1064 // b/10126687 is fixed, losing handshake packets is pretty brutal.
1065 // TODO(jokulik): Until we support redundant SREJ packets, don't
1066 // drop handshake packets for stateless rejects.
1067 SetPacketLossPercentage(5);
1068 }
1069 ASSERT_TRUE(Initialize());
1070
1071 // Wait for the server SHLO before upping the packet loss.
1072 EXPECT_TRUE(client_->client()->WaitForCryptoHandshakeConfirmed());
1073 SetPacketLossPercentage(10);
1074 client_writer_->set_fake_blocked_socket_percentage(10);
1075
1076 // 10 KB body.
vasilvvc48c8712019-03-11 13:38:16 -07001077 std::string body(1024 * 10, 'a');
QUICHE teama6ef0a62019-03-07 20:34:33 -05001078 SpdyHeaderBlock headers;
1079 headers[":method"] = "POST";
1080 headers[":path"] = "/foo";
1081 headers[":scheme"] = "https";
1082 headers[":authority"] = server_hostname_;
1083
1084 EXPECT_EQ(kFooResponseBody,
1085 client_->SendCustomSynchronousRequest(headers, body));
1086}
1087
1088TEST_P(EndToEndTest, LargePostNoPacketLossWithDelayAndReordering) {
1089 ASSERT_TRUE(Initialize());
1090
1091 EXPECT_TRUE(client_->client()->WaitForCryptoHandshakeConfirmed());
1092 // Both of these must be called when the writer is not actively used.
1093 SetPacketSendDelay(QuicTime::Delta::FromMilliseconds(2));
1094 SetReorderPercentage(30);
1095
1096 // 1 MB body.
vasilvvc48c8712019-03-11 13:38:16 -07001097 std::string body(1024 * 1024, 'a');
QUICHE teama6ef0a62019-03-07 20:34:33 -05001098 SpdyHeaderBlock headers;
1099 headers[":method"] = "POST";
1100 headers[":path"] = "/foo";
1101 headers[":scheme"] = "https";
1102 headers[":authority"] = server_hostname_;
1103
1104 EXPECT_EQ(kFooResponseBody,
1105 client_->SendCustomSynchronousRequest(headers, body));
1106}
1107
1108TEST_P(EndToEndTest, LargePostZeroRTTFailure) {
1109 // Send a request and then disconnect. This prepares the client to attempt
1110 // a 0-RTT handshake for the next request.
1111 ASSERT_TRUE(Initialize());
1112
vasilvvc48c8712019-03-11 13:38:16 -07001113 std::string body(20480, 'a');
QUICHE teama6ef0a62019-03-07 20:34:33 -05001114 SpdyHeaderBlock headers;
1115 headers[":method"] = "POST";
1116 headers[":path"] = "/foo";
1117 headers[":scheme"] = "https";
1118 headers[":authority"] = server_hostname_;
1119
1120 EXPECT_EQ(kFooResponseBody,
1121 client_->SendCustomSynchronousRequest(headers, body));
1122 // In the non-stateless case, the same session is used for both
1123 // hellos, so the number of hellos sent on that session is 2. In
1124 // the stateless case, the first client session will be completely
1125 // torn down after the reject. The number of hellos on the latest
1126 // session is 1.
1127 const int expected_num_hellos_latest_session =
1128 (BothSidesSupportStatelessRejects() && !ServerSendsVersionNegotiation())
1129 ? 1
1130 : 2;
1131 EXPECT_EQ(expected_num_hellos_latest_session,
1132 client_->client()->client_session()->GetNumSentClientHellos());
1133 if (ServerSendsVersionNegotiation()) {
1134 EXPECT_EQ(3, client_->client()->GetNumSentClientHellos());
1135 } else {
1136 EXPECT_EQ(2, client_->client()->GetNumSentClientHellos());
1137 }
1138
1139 client_->Disconnect();
1140
1141 // The 0-RTT handshake should succeed.
1142 client_->Connect();
1143 EXPECT_TRUE(client_->client()->WaitForCryptoHandshakeConfirmed());
1144 ASSERT_TRUE(client_->client()->connected());
1145 EXPECT_EQ(kFooResponseBody,
1146 client_->SendCustomSynchronousRequest(headers, body));
1147
1148 EXPECT_EQ(1, client_->client()->client_session()->GetNumSentClientHellos());
1149 if (ServerSendsVersionNegotiation()) {
1150 EXPECT_EQ(2, client_->client()->GetNumSentClientHellos());
1151 } else {
1152 EXPECT_EQ(1, client_->client()->GetNumSentClientHellos());
1153 }
1154
1155 client_->Disconnect();
1156
1157 // Restart the server so that the 0-RTT handshake will take 1 RTT.
1158 StopServer();
1159 server_writer_ = new PacketDroppingTestWriter();
1160 StartServer();
1161
1162 client_->Connect();
1163 EXPECT_TRUE(client_->client()->WaitForCryptoHandshakeConfirmed());
1164 ASSERT_TRUE(client_->client()->connected());
1165 EXPECT_EQ(kFooResponseBody,
1166 client_->SendCustomSynchronousRequest(headers, body));
1167 // In the non-stateless case, the same session is used for both
1168 // hellos, so the number of hellos sent on that session is 2. In
1169 // the stateless case, the first client session will be completely
1170 // torn down after the reject. The number of hellos sent on the
1171 // latest session is 1.
1172 EXPECT_EQ(expected_num_hellos_latest_session,
1173 client_->client()->client_session()->GetNumSentClientHellos());
1174 if (ServerSendsVersionNegotiation()) {
1175 EXPECT_EQ(3, client_->client()->GetNumSentClientHellos());
1176 } else {
1177 EXPECT_EQ(2, client_->client()->GetNumSentClientHellos());
1178 }
1179
1180 VerifyCleanConnection(false);
1181}
1182
1183TEST_P(EndToEndTest, SynchronousRequestZeroRTTFailure) {
1184 // Send a request and then disconnect. This prepares the client to attempt
1185 // a 0-RTT handshake for the next request.
1186 ASSERT_TRUE(Initialize());
1187
1188 EXPECT_EQ(kFooResponseBody, client_->SendSynchronousRequest("/foo"));
1189 // In the non-stateless case, the same session is used for both
1190 // hellos, so the number of hellos sent on that session is 2. In
1191 // the stateless case, the first client session will be completely
1192 // torn down after the reject. The number of hellos on that second
1193 // latest session is 1.
1194 const int expected_num_hellos_latest_session =
1195 (BothSidesSupportStatelessRejects() && !ServerSendsVersionNegotiation())
1196 ? 1
1197 : 2;
1198 EXPECT_EQ(expected_num_hellos_latest_session,
1199 client_->client()->client_session()->GetNumSentClientHellos());
1200 if (ServerSendsVersionNegotiation()) {
1201 EXPECT_EQ(3, client_->client()->GetNumSentClientHellos());
1202 } else {
1203 EXPECT_EQ(2, client_->client()->GetNumSentClientHellos());
1204 }
1205
1206 client_->Disconnect();
1207
1208 // The 0-RTT handshake should succeed.
1209 client_->Connect();
1210 EXPECT_TRUE(client_->client()->WaitForCryptoHandshakeConfirmed());
1211 ASSERT_TRUE(client_->client()->connected());
1212 EXPECT_EQ(kFooResponseBody, client_->SendSynchronousRequest("/foo"));
1213
1214 EXPECT_EQ(1, client_->client()->client_session()->GetNumSentClientHellos());
1215 if (ServerSendsVersionNegotiation()) {
1216 EXPECT_EQ(2, client_->client()->GetNumSentClientHellos());
1217 } else {
1218 EXPECT_EQ(1, client_->client()->GetNumSentClientHellos());
1219 }
1220
1221 client_->Disconnect();
1222
1223 // Restart the server so that the 0-RTT handshake will take 1 RTT.
1224 StopServer();
1225 server_writer_ = new PacketDroppingTestWriter();
1226 StartServer();
1227
1228 client_->Connect();
1229 EXPECT_TRUE(client_->client()->WaitForCryptoHandshakeConfirmed());
1230 ASSERT_TRUE(client_->client()->connected());
1231 EXPECT_EQ(kFooResponseBody, client_->SendSynchronousRequest("/foo"));
1232 // In the non-stateless case, the same session is used for both
1233 // hellos, so the number of hellos sent on that session is 2. In
1234 // the stateless case, the first client session will be completely
1235 // torn down after the reject. The number of hellos sent on the
1236 // latest session is 1.
1237 EXPECT_EQ(expected_num_hellos_latest_session,
1238 client_->client()->client_session()->GetNumSentClientHellos());
1239 if (ServerSendsVersionNegotiation()) {
1240 EXPECT_EQ(3, client_->client()->GetNumSentClientHellos());
1241 } else {
1242 EXPECT_EQ(2, client_->client()->GetNumSentClientHellos());
1243 }
1244
1245 VerifyCleanConnection(false);
1246}
1247
1248TEST_P(EndToEndTest, LargePostSynchronousRequest) {
1249 // Send a request and then disconnect. This prepares the client to attempt
1250 // a 0-RTT handshake for the next request.
1251 ASSERT_TRUE(Initialize());
1252
vasilvvc48c8712019-03-11 13:38:16 -07001253 std::string body(20480, 'a');
QUICHE teama6ef0a62019-03-07 20:34:33 -05001254 SpdyHeaderBlock headers;
1255 headers[":method"] = "POST";
1256 headers[":path"] = "/foo";
1257 headers[":scheme"] = "https";
1258 headers[":authority"] = server_hostname_;
1259
1260 EXPECT_EQ(kFooResponseBody,
1261 client_->SendCustomSynchronousRequest(headers, body));
1262 // In the non-stateless case, the same session is used for both
1263 // hellos, so the number of hellos sent on that session is 2. In
1264 // the stateless case, the first client session will be completely
1265 // torn down after the reject. The number of hellos on the latest
1266 // session is 1.
1267 const int expected_num_hellos_latest_session =
1268 (BothSidesSupportStatelessRejects() && !ServerSendsVersionNegotiation())
1269 ? 1
1270 : 2;
1271 EXPECT_EQ(expected_num_hellos_latest_session,
1272 client_->client()->client_session()->GetNumSentClientHellos());
1273 if (ServerSendsVersionNegotiation()) {
1274 EXPECT_EQ(3, client_->client()->GetNumSentClientHellos());
1275 } else {
1276 EXPECT_EQ(2, client_->client()->GetNumSentClientHellos());
1277 }
1278
1279 client_->Disconnect();
1280
1281 // The 0-RTT handshake should succeed.
1282 client_->Connect();
1283 EXPECT_TRUE(client_->client()->WaitForCryptoHandshakeConfirmed());
1284 ASSERT_TRUE(client_->client()->connected());
1285 EXPECT_EQ(kFooResponseBody,
1286 client_->SendCustomSynchronousRequest(headers, body));
1287
1288 EXPECT_EQ(1, client_->client()->client_session()->GetNumSentClientHellos());
1289 if (ServerSendsVersionNegotiation()) {
1290 EXPECT_EQ(2, client_->client()->GetNumSentClientHellos());
1291 } else {
1292 EXPECT_EQ(1, client_->client()->GetNumSentClientHellos());
1293 }
1294
1295 client_->Disconnect();
1296
1297 // Restart the server so that the 0-RTT handshake will take 1 RTT.
1298 StopServer();
1299 server_writer_ = new PacketDroppingTestWriter();
1300 StartServer();
1301
1302 client_->Connect();
1303 EXPECT_TRUE(client_->client()->WaitForCryptoHandshakeConfirmed());
1304 ASSERT_TRUE(client_->client()->connected());
1305 EXPECT_EQ(kFooResponseBody,
1306 client_->SendCustomSynchronousRequest(headers, body));
1307 // In the non-stateless case, the same session is used for both
1308 // hellos, so the number of hellos sent on that session is 2. In
1309 // the stateless case, the first client session will be completely
1310 // torn down after the reject. The number of hellos sent on the
1311 // latest session is 1.
1312 EXPECT_EQ(expected_num_hellos_latest_session,
1313 client_->client()->client_session()->GetNumSentClientHellos());
1314 if (ServerSendsVersionNegotiation()) {
1315 EXPECT_EQ(3, client_->client()->GetNumSentClientHellos());
1316 } else {
1317 EXPECT_EQ(2, client_->client()->GetNumSentClientHellos());
1318 }
1319
1320 VerifyCleanConnection(false);
1321}
1322
1323TEST_P(EndToEndTest, StatelessRejectWithPacketLoss) {
1324 // In this test, we intentionally drop the first packet from the
1325 // server, which corresponds with the initial REJ/SREJ response from
1326 // the server.
1327 server_writer_->set_fake_drop_first_n_packets(1);
1328 ASSERT_TRUE(Initialize());
1329}
1330
1331TEST_P(EndToEndTest, SetInitialReceivedConnectionOptions) {
1332 QuicTagVector initial_received_options;
1333 initial_received_options.push_back(kTBBR);
1334 initial_received_options.push_back(kIW10);
1335 initial_received_options.push_back(kPRST);
1336 EXPECT_TRUE(server_config_.SetInitialReceivedConnectionOptions(
1337 initial_received_options));
1338
1339 ASSERT_TRUE(Initialize());
1340 EXPECT_TRUE(client_->client()->WaitForCryptoHandshakeConfirmed());
1341 server_thread_->WaitForCryptoHandshakeConfirmed();
1342
1343 EXPECT_FALSE(server_config_.SetInitialReceivedConnectionOptions(
1344 initial_received_options));
1345
1346 // Verify that server's configuration is correct.
1347 server_thread_->Pause();
1348 EXPECT_TRUE(server_config_.HasReceivedConnectionOptions());
1349 EXPECT_TRUE(
1350 ContainsQuicTag(server_config_.ReceivedConnectionOptions(), kTBBR));
1351 EXPECT_TRUE(
1352 ContainsQuicTag(server_config_.ReceivedConnectionOptions(), kIW10));
1353 EXPECT_TRUE(
1354 ContainsQuicTag(server_config_.ReceivedConnectionOptions(), kPRST));
1355}
1356
1357TEST_P(EndToEndTest, LargePostSmallBandwidthLargeBuffer) {
1358 ASSERT_TRUE(Initialize());
1359 SetPacketSendDelay(QuicTime::Delta::FromMicroseconds(1));
1360 // 256KB per second with a 256KB buffer from server to client. Wireless
1361 // clients commonly have larger buffers, but our max CWND is 200.
1362 server_writer_->set_max_bandwidth_and_buffer_size(
1363 QuicBandwidth::FromBytesPerSecond(256 * 1024), 256 * 1024);
1364
1365 EXPECT_TRUE(client_->client()->WaitForCryptoHandshakeConfirmed());
1366
1367 // 1 MB body.
vasilvvc48c8712019-03-11 13:38:16 -07001368 std::string body(1024 * 1024, 'a');
QUICHE teama6ef0a62019-03-07 20:34:33 -05001369 SpdyHeaderBlock headers;
1370 headers[":method"] = "POST";
1371 headers[":path"] = "/foo";
1372 headers[":scheme"] = "https";
1373 headers[":authority"] = server_hostname_;
1374
1375 EXPECT_EQ(kFooResponseBody,
1376 client_->SendCustomSynchronousRequest(headers, body));
1377 // This connection may drop packets, because the buffer is smaller than the
1378 // max CWND.
1379 VerifyCleanConnection(true);
1380}
1381
1382TEST_P(EndToEndTestWithTls, DoNotSetSendAlarmIfConnectionFlowControlBlocked) {
1383 // Regression test for b/14677858.
1384 // Test that the resume write alarm is not set in QuicConnection::OnCanWrite
1385 // if currently connection level flow control blocked. If set, this results in
1386 // an infinite loop in the EpollServer, as the alarm fires and is immediately
1387 // rescheduled.
1388 ASSERT_TRUE(Initialize());
1389 EXPECT_TRUE(client_->client()->WaitForCryptoHandshakeConfirmed());
1390
1391 // Ensure both stream and connection level are flow control blocked by setting
1392 // the send window offset to 0.
1393 const uint64_t flow_control_window =
1394 server_config_.GetInitialStreamFlowControlWindowToSend();
1395 QuicSpdyClientStream* stream = client_->GetOrCreateStream();
1396 QuicSession* session = client_->client()->client_session();
1397 QuicFlowControllerPeer::SetSendWindowOffset(stream->flow_controller(), 0);
1398 QuicFlowControllerPeer::SetSendWindowOffset(session->flow_controller(), 0);
1399 EXPECT_TRUE(stream->flow_controller()->IsBlocked());
1400 EXPECT_TRUE(session->flow_controller()->IsBlocked());
1401
1402 // Make sure that the stream has data pending so that it will be marked as
1403 // write blocked when it receives a stream level WINDOW_UPDATE.
1404 stream->WriteOrBufferBody("hello", false);
1405
1406 // The stream now attempts to write, fails because it is still connection
1407 // level flow control blocked, and is added to the write blocked list.
1408 QuicWindowUpdateFrame window_update(kInvalidControlFrameId, stream->id(),
1409 2 * flow_control_window);
1410 stream->OnWindowUpdateFrame(window_update);
1411
1412 // Prior to fixing b/14677858 this call would result in an infinite loop in
1413 // Chromium. As a proxy for detecting this, we now check whether the
1414 // send alarm is set after OnCanWrite. It should not be, as the
1415 // connection is still flow control blocked.
1416 session->connection()->OnCanWrite();
1417
1418 QuicAlarm* send_alarm =
1419 QuicConnectionPeer::GetSendAlarm(session->connection());
1420 EXPECT_FALSE(send_alarm->IsSet());
1421}
1422
1423// TODO(nharper): Needs to get turned back to EndToEndTestWithTls
1424// when we figure out why the test doesn't work on chrome.
1425TEST_P(EndToEndTest, InvalidStream) {
1426 ASSERT_TRUE(Initialize());
1427 EXPECT_TRUE(client_->client()->WaitForCryptoHandshakeConfirmed());
1428
dschinazi66dea072019-04-09 11:41:06 -07001429 std::string body(kMaxOutgoingPacketSize, 'a');
QUICHE teama6ef0a62019-03-07 20:34:33 -05001430 SpdyHeaderBlock headers;
1431 headers[":method"] = "POST";
1432 headers[":path"] = "/foo";
1433 headers[":scheme"] = "https";
1434 headers[":authority"] = server_hostname_;
1435
1436 // Force the client to write with a stream ID belonging to a nonexistent
1437 // server-side stream.
1438 QuicSpdySession* session = client_->client()->client_session();
1439 QuicSessionPeer::SetNextOutgoingBidirectionalStreamId(
1440 session, GetNthServerInitiatedBidirectionalId(0));
1441
1442 client_->SendCustomSynchronousRequest(headers, body);
1443 EXPECT_EQ(QUIC_STREAM_CONNECTION_ERROR, client_->stream_error());
1444 EXPECT_EQ(QUIC_INVALID_STREAM_ID, client_->connection_error());
1445}
1446
1447// Test that if the server will close the connection if the client attempts
1448// to send a request with overly large headers.
1449TEST_P(EndToEndTest, LargeHeaders) {
1450 ASSERT_TRUE(Initialize());
1451 EXPECT_TRUE(client_->client()->WaitForCryptoHandshakeConfirmed());
1452
dschinazi66dea072019-04-09 11:41:06 -07001453 std::string body(kMaxOutgoingPacketSize, 'a');
QUICHE teama6ef0a62019-03-07 20:34:33 -05001454 SpdyHeaderBlock headers;
1455 headers[":method"] = "POST";
1456 headers[":path"] = "/foo";
1457 headers[":scheme"] = "https";
1458 headers[":authority"] = server_hostname_;
vasilvvc48c8712019-03-11 13:38:16 -07001459 headers["key1"] = std::string(15 * 1024, 'a');
1460 headers["key2"] = std::string(15 * 1024, 'a');
1461 headers["key3"] = std::string(15 * 1024, 'a');
QUICHE teama6ef0a62019-03-07 20:34:33 -05001462
1463 client_->SendCustomSynchronousRequest(headers, body);
1464 EXPECT_EQ(QUIC_HEADERS_TOO_LARGE, client_->stream_error());
1465 EXPECT_EQ(QUIC_NO_ERROR, client_->connection_error());
1466}
1467
1468TEST_P(EndToEndTest, EarlyResponseWithQuicStreamNoError) {
1469 ASSERT_TRUE(Initialize());
1470 EXPECT_TRUE(client_->client()->WaitForCryptoHandshakeConfirmed());
1471
vasilvvc48c8712019-03-11 13:38:16 -07001472 std::string large_body(1024 * 1024, 'a');
QUICHE teama6ef0a62019-03-07 20:34:33 -05001473 SpdyHeaderBlock headers;
1474 headers[":method"] = "POST";
1475 headers[":path"] = "/foo";
1476 headers[":scheme"] = "https";
1477 headers[":authority"] = server_hostname_;
1478 // Insert an invalid content_length field in request to trigger an early
1479 // response from server.
1480 headers["content-length"] = "-3";
1481
1482 client_->SendCustomSynchronousRequest(headers, large_body);
1483 EXPECT_EQ("bad", client_->response_body());
1484 EXPECT_EQ("500", client_->response_headers()->find(":status")->second);
1485 EXPECT_EQ(QUIC_STREAM_NO_ERROR, client_->stream_error());
1486 EXPECT_EQ(QUIC_NO_ERROR, client_->connection_error());
1487}
1488
1489// TODO(rch): this test seems to cause net_unittests timeouts :|
1490TEST_P(EndToEndTestWithTls, QUIC_TEST_DISABLED_IN_CHROME(MultipleTermination)) {
1491 ASSERT_TRUE(Initialize());
1492
1493 // Set the offset so we won't frame. Otherwise when we pick up termination
1494 // before HTTP framing is complete, we send an error and close the stream,
1495 // and the second write is picked up as writing on a closed stream.
1496 QuicSpdyClientStream* stream = client_->GetOrCreateStream();
1497 ASSERT_TRUE(stream != nullptr);
1498 QuicStreamPeer::SetStreamBytesWritten(3, stream);
1499
1500 client_->SendData("bar", true);
1501 client_->WaitForWriteToFlush();
1502
1503 // By default the stream protects itself from writes after terminte is set.
1504 // Override this to test the server handling buggy clients.
1505 QuicStreamPeer::SetWriteSideClosed(false, client_->GetOrCreateStream());
1506
1507 EXPECT_QUIC_BUG(client_->SendData("eep", true), "Fin already buffered");
1508}
1509
1510// TODO(nharper): Needs to get turned back to EndToEndTestWithTls
1511// when we figure out why the test doesn't work on chrome.
1512TEST_P(EndToEndTest, Timeout) {
1513 client_config_.SetIdleNetworkTimeout(QuicTime::Delta::FromMicroseconds(500),
1514 QuicTime::Delta::FromMicroseconds(500));
1515 // Note: we do NOT ASSERT_TRUE: we may time out during initial handshake:
1516 // that's enough to validate timeout in this case.
1517 Initialize();
1518 while (client_->client()->connected()) {
1519 client_->client()->WaitForEvents();
1520 }
1521}
1522
1523TEST_P(EndToEndTestWithTls, MaxIncomingDynamicStreamsLimitRespected) {
1524 // Set a limit on maximum number of incoming dynamic streams.
1525 // Make sure the limit is respected.
1526 const uint32_t kServerMaxIncomingDynamicStreams = 1;
1527 server_config_.SetMaxIncomingDynamicStreamsToSend(
1528 kServerMaxIncomingDynamicStreams);
1529 ASSERT_TRUE(Initialize());
fkastenholz3c4eabf2019-04-22 07:49:59 -07001530 if (GetParam().negotiated_version.transport_version == QUIC_VERSION_99) {
1531 // Do not run this test for version 99/IETF QUIC. Note that the test needs
1532 // to be here, after calling Initialize(), because all tests end up calling
1533 // EndToEndTest::TearDown(), which asserts that Initialize has been called
1534 // and then proceeds to tear things down -- which fails if they are not
1535 // properly set up.
1536 return;
1537 }
QUICHE teama6ef0a62019-03-07 20:34:33 -05001538 EXPECT_TRUE(client_->client()->WaitForCryptoHandshakeConfirmed());
QUICHE teama6ef0a62019-03-07 20:34:33 -05001539
1540 // Make the client misbehave after negotiation.
1541 const int kServerMaxStreams = kMaxStreamsMinimumIncrement + 1;
1542 QuicSessionPeer::SetMaxOpenOutgoingStreams(
1543 client_->client()->client_session(), kServerMaxStreams + 1);
1544
1545 SpdyHeaderBlock headers;
1546 headers[":method"] = "POST";
1547 headers[":path"] = "/foo";
1548 headers[":scheme"] = "https";
1549 headers[":authority"] = server_hostname_;
1550 headers["content-length"] = "3";
1551
1552 // The server supports a small number of additional streams beyond the
1553 // negotiated limit. Open enough streams to go beyond that limit.
1554 for (int i = 0; i < kServerMaxStreams + 1; ++i) {
1555 client_->SendMessage(headers, "", /*fin=*/false);
1556 }
1557 client_->WaitForResponse();
fkastenholz3c4eabf2019-04-22 07:49:59 -07001558
1559 EXPECT_TRUE(client_->connected());
1560 EXPECT_EQ(QUIC_REFUSED_STREAM, client_->stream_error());
1561 EXPECT_EQ(QUIC_NO_ERROR, client_->connection_error());
QUICHE teama6ef0a62019-03-07 20:34:33 -05001562}
1563
1564TEST_P(EndToEndTest, SetIndependentMaxIncomingDynamicStreamsLimits) {
1565 // Each endpoint can set max incoming dynamic streams independently.
1566 const uint32_t kClientMaxIncomingDynamicStreams = 2;
1567 const uint32_t kServerMaxIncomingDynamicStreams = 1;
1568 client_config_.SetMaxIncomingDynamicStreamsToSend(
1569 kClientMaxIncomingDynamicStreams);
1570 server_config_.SetMaxIncomingDynamicStreamsToSend(
1571 kServerMaxIncomingDynamicStreams);
1572 ASSERT_TRUE(Initialize());
1573 EXPECT_TRUE(client_->client()->WaitForCryptoHandshakeConfirmed());
1574
1575 // The client has received the server's limit and vice versa.
1576 QuicSpdyClientSession* client_session = client_->client()->client_session();
fkastenholz3c4eabf2019-04-22 07:49:59 -07001577 // The value returned by max_allowed... includes the Crypto and Header
1578 // stream (created as a part of initialization). The config. values,
1579 // above, are treated as "number of requests/responses" - that is, they do
1580 // not include the static Crypto and Header streams. Reduce the value
1581 // returned by max_allowed... by 2 to remove the static streams from the
1582 // count.
QUICHE teama6ef0a62019-03-07 20:34:33 -05001583 size_t client_max_open_outgoing_bidirectional_streams =
1584 client_session->connection()->transport_version() == QUIC_VERSION_99
1585 ? QuicSessionPeer::v99_streamid_manager(client_session)
fkastenholz3c4eabf2019-04-22 07:49:59 -07001586 ->max_allowed_outgoing_bidirectional_streams() -
1587 QuicSessionPeer::v99_bidirectional_stream_id_manager(
1588 client_session)
1589 ->outgoing_static_stream_count()
QUICHE teama6ef0a62019-03-07 20:34:33 -05001590 : QuicSessionPeer::GetStreamIdManager(client_session)
1591 ->max_open_outgoing_streams();
1592 size_t client_max_open_outgoing_unidirectional_streams =
1593 client_session->connection()->transport_version() == QUIC_VERSION_99
1594 ? QuicSessionPeer::v99_streamid_manager(client_session)
1595 ->max_allowed_outgoing_unidirectional_streams()
1596 : QuicSessionPeer::GetStreamIdManager(client_session)
1597 ->max_open_outgoing_streams();
1598 EXPECT_EQ(kServerMaxIncomingDynamicStreams,
1599 client_max_open_outgoing_bidirectional_streams);
1600 EXPECT_EQ(kServerMaxIncomingDynamicStreams,
1601 client_max_open_outgoing_unidirectional_streams);
1602 server_thread_->Pause();
1603 QuicSession* server_session = GetServerSession();
1604 size_t server_max_open_outgoing_bidirectional_streams =
1605 server_session->connection()->transport_version() == QUIC_VERSION_99
1606 ? QuicSessionPeer::v99_streamid_manager(server_session)
1607 ->max_allowed_outgoing_bidirectional_streams()
1608 : QuicSessionPeer::GetStreamIdManager(server_session)
1609 ->max_open_outgoing_streams();
1610 size_t server_max_open_outgoing_unidirectional_streams =
1611 server_session->connection()->transport_version() == QUIC_VERSION_99
1612 ? QuicSessionPeer::v99_streamid_manager(server_session)
1613 ->max_allowed_outgoing_unidirectional_streams()
1614 : QuicSessionPeer::GetStreamIdManager(server_session)
1615 ->max_open_outgoing_streams();
1616 EXPECT_EQ(kClientMaxIncomingDynamicStreams,
1617 server_max_open_outgoing_bidirectional_streams);
1618 EXPECT_EQ(kClientMaxIncomingDynamicStreams,
1619 server_max_open_outgoing_unidirectional_streams);
1620 server_thread_->Resume();
1621}
1622
1623TEST_P(EndToEndTest, NegotiateCongestionControl) {
1624 ASSERT_TRUE(Initialize());
1625
1626 // For PCC, the underlying implementation may be a stub with a
1627 // different name-tag. Skip the rest of this test.
1628 if (GetParam().congestion_control_tag == kTPCC) {
1629 return;
1630 }
1631
1632 EXPECT_TRUE(client_->client()->WaitForCryptoHandshakeConfirmed());
1633
1634 CongestionControlType expected_congestion_control_type = kRenoBytes;
1635 switch (GetParam().congestion_control_tag) {
1636 case kRENO:
1637 expected_congestion_control_type = kRenoBytes;
1638 break;
1639 case kTBBR:
1640 expected_congestion_control_type = kBBR;
1641 break;
1642 case kQBIC:
1643 expected_congestion_control_type = kCubicBytes;
1644 break;
1645 default:
1646 QUIC_DLOG(FATAL) << "Unexpected congestion control tag";
1647 }
1648
1649 server_thread_->Pause();
1650 EXPECT_EQ(expected_congestion_control_type,
1651 QuicSentPacketManagerPeer::GetSendAlgorithm(
1652 *GetSentPacketManagerFromFirstServerSession())
1653 ->GetCongestionControlType());
1654 server_thread_->Resume();
1655}
1656
1657TEST_P(EndToEndTest, ClientSuggestsRTT) {
1658 // Client suggests initial RTT, verify it is used.
1659 const QuicTime::Delta kInitialRTT = QuicTime::Delta::FromMicroseconds(20000);
1660 client_config_.SetInitialRoundTripTimeUsToSend(kInitialRTT.ToMicroseconds());
1661
1662 ASSERT_TRUE(Initialize());
1663 EXPECT_TRUE(client_->client()->WaitForCryptoHandshakeConfirmed());
1664 server_thread_->WaitForCryptoHandshakeConfirmed();
1665
1666 // Pause the server so we can access the server's internals without races.
1667 server_thread_->Pause();
1668 QuicDispatcher* dispatcher =
1669 QuicServerPeer::GetDispatcher(server_thread_->server());
1670 ASSERT_EQ(1u, dispatcher->session_map().size());
1671 const QuicSentPacketManager& client_sent_packet_manager =
1672 client_->client()->client_session()->connection()->sent_packet_manager();
1673 const QuicSentPacketManager* server_sent_packet_manager =
1674 GetSentPacketManagerFromFirstServerSession();
1675
1676 EXPECT_EQ(kInitialRTT,
1677 client_sent_packet_manager.GetRttStats()->initial_rtt());
1678 EXPECT_EQ(kInitialRTT,
1679 server_sent_packet_manager->GetRttStats()->initial_rtt());
1680 server_thread_->Resume();
1681}
1682
1683TEST_P(EndToEndTest, ClientSuggestsIgnoredRTT) {
1684 // Client suggests initial RTT, but also specifies NRTT, so it's not used.
1685 const QuicTime::Delta kInitialRTT = QuicTime::Delta::FromMicroseconds(20000);
1686 client_config_.SetInitialRoundTripTimeUsToSend(kInitialRTT.ToMicroseconds());
1687 QuicTagVector options;
1688 options.push_back(kNRTT);
1689 client_config_.SetConnectionOptionsToSend(options);
1690
1691 ASSERT_TRUE(Initialize());
1692 EXPECT_TRUE(client_->client()->WaitForCryptoHandshakeConfirmed());
1693 server_thread_->WaitForCryptoHandshakeConfirmed();
1694
1695 // Pause the server so we can access the server's internals without races.
1696 server_thread_->Pause();
1697 QuicDispatcher* dispatcher =
1698 QuicServerPeer::GetDispatcher(server_thread_->server());
1699 ASSERT_EQ(1u, dispatcher->session_map().size());
1700 const QuicSentPacketManager& client_sent_packet_manager =
1701 client_->client()->client_session()->connection()->sent_packet_manager();
1702 const QuicSentPacketManager* server_sent_packet_manager =
1703 GetSentPacketManagerFromFirstServerSession();
1704
1705 EXPECT_EQ(kInitialRTT,
1706 client_sent_packet_manager.GetRttStats()->initial_rtt());
1707 EXPECT_EQ(kInitialRTT,
1708 server_sent_packet_manager->GetRttStats()->initial_rtt());
1709 server_thread_->Resume();
1710}
1711
1712TEST_P(EndToEndTest, MaxInitialRTT) {
1713 // Client tries to suggest twice the server's max initial rtt and the server
1714 // uses the max.
1715 client_config_.SetInitialRoundTripTimeUsToSend(2 *
1716 kMaxInitialRoundTripTimeUs);
1717
1718 ASSERT_TRUE(Initialize());
1719 EXPECT_TRUE(client_->client()->WaitForCryptoHandshakeConfirmed());
1720 server_thread_->WaitForCryptoHandshakeConfirmed();
1721
1722 // Pause the server so we can access the server's internals without races.
1723 server_thread_->Pause();
1724 const QuicSentPacketManager& client_sent_packet_manager =
1725 client_->client()->client_session()->connection()->sent_packet_manager();
1726
1727 // Now that acks have been exchanged, the RTT estimate has decreased on the
1728 // server and is not infinite on the client.
1729 EXPECT_FALSE(
1730 client_sent_packet_manager.GetRttStats()->smoothed_rtt().IsInfinite());
1731 const RttStats& server_rtt_stats =
1732 *GetServerConnection()->sent_packet_manager().GetRttStats();
1733 EXPECT_EQ(static_cast<int64_t>(kMaxInitialRoundTripTimeUs),
1734 server_rtt_stats.initial_rtt().ToMicroseconds());
1735 EXPECT_GE(static_cast<int64_t>(kMaxInitialRoundTripTimeUs),
1736 server_rtt_stats.smoothed_rtt().ToMicroseconds());
1737 server_thread_->Resume();
1738}
1739
1740TEST_P(EndToEndTest, MinInitialRTT) {
1741 // Client tries to suggest 0 and the server uses the default.
1742 client_config_.SetInitialRoundTripTimeUsToSend(0);
1743
1744 ASSERT_TRUE(Initialize());
1745 EXPECT_TRUE(client_->client()->WaitForCryptoHandshakeConfirmed());
1746 server_thread_->WaitForCryptoHandshakeConfirmed();
1747
1748 // Pause the server so we can access the server's internals without races.
1749 server_thread_->Pause();
1750 const QuicSentPacketManager& client_sent_packet_manager =
1751 client_->client()->client_session()->connection()->sent_packet_manager();
1752 const QuicSentPacketManager& server_sent_packet_manager =
1753 GetServerConnection()->sent_packet_manager();
1754
1755 // Now that acks have been exchanged, the RTT estimate has decreased on the
1756 // server and is not infinite on the client.
1757 EXPECT_FALSE(
1758 client_sent_packet_manager.GetRttStats()->smoothed_rtt().IsInfinite());
1759 // Expect the default rtt of 100ms.
1760 EXPECT_EQ(QuicTime::Delta::FromMilliseconds(100),
1761 server_sent_packet_manager.GetRttStats()->initial_rtt());
1762 // Ensure the bandwidth is valid.
1763 client_sent_packet_manager.BandwidthEstimate();
1764 server_sent_packet_manager.BandwidthEstimate();
1765 server_thread_->Resume();
1766}
1767
1768TEST_P(EndToEndTest, 0ByteConnectionId) {
1769 client_config_.SetBytesForConnectionIdToSend(0);
1770 ASSERT_TRUE(Initialize());
1771
1772 EXPECT_EQ(kFooResponseBody, client_->SendSynchronousRequest("/foo"));
1773 EXPECT_EQ("200", client_->response_headers()->find(":status")->second);
1774 QuicConnection* client_connection =
1775 client_->client()->client_session()->connection();
1776 QuicPacketHeader* header =
1777 QuicConnectionPeer::GetLastHeader(client_connection);
1778 EXPECT_EQ(CONNECTION_ID_ABSENT, header->destination_connection_id_included);
1779}
1780
1781TEST_P(EndToEndTestWithTls, 8ByteConnectionId) {
1782 client_config_.SetBytesForConnectionIdToSend(8);
1783 ASSERT_TRUE(Initialize());
1784
1785 EXPECT_EQ(kFooResponseBody, client_->SendSynchronousRequest("/foo"));
1786 EXPECT_EQ("200", client_->response_headers()->find(":status")->second);
1787 QuicConnection* client_connection =
1788 client_->client()->client_session()->connection();
1789 QuicPacketHeader* header =
1790 QuicConnectionPeer::GetLastHeader(client_connection);
1791 if (client_connection->transport_version() > QUIC_VERSION_43) {
1792 EXPECT_EQ(CONNECTION_ID_ABSENT, header->destination_connection_id_included);
1793 } else {
1794 EXPECT_EQ(CONNECTION_ID_PRESENT,
1795 header->destination_connection_id_included);
1796 }
1797}
1798
1799TEST_P(EndToEndTestWithTls, 15ByteConnectionId) {
1800 client_config_.SetBytesForConnectionIdToSend(15);
1801 ASSERT_TRUE(Initialize());
1802
1803 // Our server is permissive and allows for out of bounds values.
1804 EXPECT_EQ(kFooResponseBody, client_->SendSynchronousRequest("/foo"));
1805 EXPECT_EQ("200", client_->response_headers()->find(":status")->second);
1806 QuicConnection* client_connection =
1807 client_->client()->client_session()->connection();
1808 QuicPacketHeader* header =
1809 QuicConnectionPeer::GetLastHeader(client_connection);
1810 if (client_connection->transport_version() > QUIC_VERSION_43) {
1811 EXPECT_EQ(CONNECTION_ID_ABSENT, header->destination_connection_id_included);
1812 } else {
1813 EXPECT_EQ(CONNECTION_ID_PRESENT,
1814 header->destination_connection_id_included);
1815 }
1816}
1817
1818TEST_P(EndToEndTestWithTls, ResetConnection) {
1819 ASSERT_TRUE(Initialize());
1820
1821 EXPECT_EQ(kFooResponseBody, client_->SendSynchronousRequest("/foo"));
1822 EXPECT_EQ("200", client_->response_headers()->find(":status")->second);
1823 client_->ResetConnection();
1824 EXPECT_TRUE(client_->client()->WaitForCryptoHandshakeConfirmed());
1825 EXPECT_EQ(kBarResponseBody, client_->SendSynchronousRequest("/bar"));
1826 EXPECT_EQ("200", client_->response_headers()->find(":status")->second);
1827}
1828
1829// TODO(nharper): Needs to get turned back to EndToEndTestWithTls
1830// when we figure out why the test doesn't work on chrome.
1831TEST_P(EndToEndTest, MaxStreamsUberTest) {
1832 if (!BothSidesSupportStatelessRejects()) {
1833 // Connect with lower fake packet loss than we'd like to test. Until
1834 // b/10126687 is fixed, losing handshake packets is pretty brutal.
1835 // TODO(jokulik): Until we support redundant SREJ packets, don't
1836 // drop handshake packets for stateless rejects.
1837 SetPacketLossPercentage(1);
1838 }
1839 ASSERT_TRUE(Initialize());
vasilvvc48c8712019-03-11 13:38:16 -07001840 std::string large_body(10240, 'a');
QUICHE teama6ef0a62019-03-07 20:34:33 -05001841 int max_streams = 100;
1842
1843 AddToCache("/large_response", 200, large_body);
1844
1845 EXPECT_TRUE(client_->client()->WaitForCryptoHandshakeConfirmed());
1846 SetPacketLossPercentage(10);
1847
1848 for (int i = 0; i < max_streams; ++i) {
1849 EXPECT_LT(0, client_->SendRequest("/large_response"));
1850 }
1851
1852 // WaitForEvents waits 50ms and returns true if there are outstanding
1853 // requests.
1854 while (client_->client()->WaitForEvents() == true) {
1855 }
1856}
1857
1858TEST_P(EndToEndTestWithTls, StreamCancelErrorTest) {
1859 ASSERT_TRUE(Initialize());
vasilvvc48c8712019-03-11 13:38:16 -07001860 std::string small_body(256, 'a');
QUICHE teama6ef0a62019-03-07 20:34:33 -05001861
1862 AddToCache("/small_response", 200, small_body);
1863
1864 EXPECT_TRUE(client_->client()->WaitForCryptoHandshakeConfirmed());
1865
1866 QuicSession* session = client_->client()->client_session();
1867 // Lose the request.
1868 SetPacketLossPercentage(100);
1869 EXPECT_LT(0, client_->SendRequest("/small_response"));
1870 client_->client()->WaitForEvents();
1871 // Transmit the cancel, and ensure the connection is torn down properly.
1872 SetPacketLossPercentage(0);
1873 QuicStreamId stream_id = GetNthClientInitiatedBidirectionalId(0);
1874 session->SendRstStream(stream_id, QUIC_STREAM_CANCELLED, 0);
1875
1876 // WaitForEvents waits 50ms and returns true if there are outstanding
1877 // requests.
1878 while (client_->client()->WaitForEvents() == true) {
1879 }
1880 // It should be completely fine to RST a stream before any data has been
1881 // received for that stream.
1882 EXPECT_EQ(QUIC_NO_ERROR, client_->connection_error());
1883}
1884
1885TEST_P(EndToEndTest, ConnectionMigrationClientIPChanged) {
1886 ASSERT_TRUE(Initialize());
1887 EXPECT_EQ(kFooResponseBody, client_->SendSynchronousRequest("/foo"));
1888 EXPECT_EQ("200", client_->response_headers()->find(":status")->second);
1889
1890 // Store the client IP address which was used to send the first request.
1891 QuicIpAddress old_host =
1892 client_->client()->network_helper()->GetLatestClientAddress().host();
1893
1894 // Migrate socket to the new IP address.
1895 QuicIpAddress new_host = TestLoopback(2);
1896 EXPECT_NE(old_host, new_host);
1897 ASSERT_TRUE(client_->client()->MigrateSocket(new_host));
1898
1899 // Send a request using the new socket.
1900 EXPECT_EQ(kBarResponseBody, client_->SendSynchronousRequest("/bar"));
1901 EXPECT_EQ("200", client_->response_headers()->find(":status")->second);
1902}
1903
1904TEST_P(EndToEndTest, ConnectionMigrationClientPortChanged) {
1905 // Tests that the client's port can change during an established QUIC
1906 // connection, and that doing so does not result in the connection being
1907 // closed by the server.
1908 ASSERT_TRUE(Initialize());
1909
1910 EXPECT_EQ(kFooResponseBody, client_->SendSynchronousRequest("/foo"));
1911 EXPECT_EQ("200", client_->response_headers()->find(":status")->second);
1912
1913 // Store the client address which was used to send the first request.
1914 QuicSocketAddress old_address =
1915 client_->client()->network_helper()->GetLatestClientAddress();
1916 int old_fd = client_->client()->GetLatestFD();
1917
1918 // Create a new socket before closing the old one, which will result in a new
1919 // ephemeral port.
1920 QuicClientPeer::CreateUDPSocketAndBind(client_->client());
1921
1922 // Stop listening and close the old FD.
1923 QuicClientPeer::CleanUpUDPSocket(client_->client(), old_fd);
1924
1925 // The packet writer needs to be updated to use the new FD.
1926 client_->client()->network_helper()->CreateQuicPacketWriter();
1927
1928 // Change the internal state of the client and connection to use the new port,
1929 // this is done because in a real NAT rebinding the client wouldn't see any
1930 // port change, and so expects no change to incoming port.
1931 // This is kind of ugly, but needed as we are simply swapping out the client
1932 // FD rather than any more complex NAT rebinding simulation.
1933 int new_port =
1934 client_->client()->network_helper()->GetLatestClientAddress().port();
1935 QuicClientPeer::SetClientPort(client_->client(), new_port);
1936 QuicConnectionPeer::SetSelfAddress(
1937 client_->client()->client_session()->connection(),
1938 QuicSocketAddress(client_->client()
1939 ->client_session()
1940 ->connection()
1941 ->self_address()
1942 .host(),
1943 new_port));
1944
1945 // Register the new FD for epoll events.
1946 int new_fd = client_->client()->GetLatestFD();
1947 QuicEpollServer* eps = client_->epoll_server();
1948 eps->RegisterFD(new_fd, client_->client()->epoll_network_helper(),
1949 EPOLLIN | EPOLLOUT | EPOLLET);
1950
1951 // Send a second request, using the new FD.
1952 EXPECT_EQ(kBarResponseBody, client_->SendSynchronousRequest("/bar"));
1953 EXPECT_EQ("200", client_->response_headers()->find(":status")->second);
1954
1955 // Verify that the client's ephemeral port is different.
1956 QuicSocketAddress new_address =
1957 client_->client()->network_helper()->GetLatestClientAddress();
1958 EXPECT_EQ(old_address.host(), new_address.host());
1959 EXPECT_NE(old_address.port(), new_address.port());
1960}
1961
1962TEST_P(EndToEndTest, NegotiatedInitialCongestionWindow) {
1963 SetQuicReloadableFlag(quic_unified_iw_options, true);
1964 client_extra_copts_.push_back(kIW03);
1965
1966 ASSERT_TRUE(Initialize());
1967
1968 // Values are exchanged during crypto handshake, so wait for that to finish.
1969 EXPECT_TRUE(client_->client()->WaitForCryptoHandshakeConfirmed());
1970 server_thread_->WaitForCryptoHandshakeConfirmed();
1971 server_thread_->Pause();
1972
1973 QuicPacketCount cwnd =
1974 GetServerConnection()->sent_packet_manager().initial_congestion_window();
1975 EXPECT_EQ(3u, cwnd);
1976}
1977
1978TEST_P(EndToEndTest, DifferentFlowControlWindows) {
1979 // Client and server can set different initial flow control receive windows.
1980 // These are sent in CHLO/SHLO. Tests that these values are exchanged properly
1981 // in the crypto handshake.
1982 const uint32_t kClientStreamIFCW = 123456;
1983 const uint32_t kClientSessionIFCW = 234567;
1984 set_client_initial_stream_flow_control_receive_window(kClientStreamIFCW);
1985 set_client_initial_session_flow_control_receive_window(kClientSessionIFCW);
1986
1987 uint32_t kServerStreamIFCW = 32 * 1024;
1988 uint32_t kServerSessionIFCW = 48 * 1024;
1989 set_server_initial_stream_flow_control_receive_window(kServerStreamIFCW);
1990 set_server_initial_session_flow_control_receive_window(kServerSessionIFCW);
1991
1992 ASSERT_TRUE(Initialize());
1993
1994 // Values are exchanged during crypto handshake, so wait for that to finish.
1995 EXPECT_TRUE(client_->client()->WaitForCryptoHandshakeConfirmed());
1996 server_thread_->WaitForCryptoHandshakeConfirmed();
1997
1998 // Open a data stream to make sure the stream level flow control is updated.
1999 QuicSpdyClientStream* stream = client_->GetOrCreateStream();
2000 stream->WriteOrBufferBody("hello", false);
2001
2002 // Client should have the right values for server's receive window.
2003 EXPECT_EQ(kServerStreamIFCW,
2004 client_->client()
2005 ->client_session()
2006 ->config()
2007 ->ReceivedInitialStreamFlowControlWindowBytes());
2008 EXPECT_EQ(kServerSessionIFCW,
2009 client_->client()
2010 ->client_session()
2011 ->config()
2012 ->ReceivedInitialSessionFlowControlWindowBytes());
2013 EXPECT_EQ(kServerStreamIFCW, QuicFlowControllerPeer::SendWindowOffset(
2014 stream->flow_controller()));
2015 EXPECT_EQ(kServerSessionIFCW,
2016 QuicFlowControllerPeer::SendWindowOffset(
2017 client_->client()->client_session()->flow_controller()));
2018
2019 // Server should have the right values for client's receive window.
2020 server_thread_->Pause();
2021 QuicSession* session = GetServerSession();
2022 EXPECT_EQ(kClientStreamIFCW,
2023 session->config()->ReceivedInitialStreamFlowControlWindowBytes());
2024 EXPECT_EQ(kClientSessionIFCW,
2025 session->config()->ReceivedInitialSessionFlowControlWindowBytes());
2026 EXPECT_EQ(kClientSessionIFCW, QuicFlowControllerPeer::SendWindowOffset(
2027 session->flow_controller()));
2028 server_thread_->Resume();
2029}
2030
2031// Test negotiation of IFWA connection option.
2032TEST_P(EndToEndTest, NegotiatedServerInitialFlowControlWindow) {
2033 const uint32_t kClientStreamIFCW = 123456;
2034 const uint32_t kClientSessionIFCW = 234567;
2035 set_client_initial_stream_flow_control_receive_window(kClientStreamIFCW);
2036 set_client_initial_session_flow_control_receive_window(kClientSessionIFCW);
2037
2038 uint32_t kServerStreamIFCW = 32 * 1024;
2039 uint32_t kServerSessionIFCW = 48 * 1024;
2040 set_server_initial_stream_flow_control_receive_window(kServerStreamIFCW);
2041 set_server_initial_session_flow_control_receive_window(kServerSessionIFCW);
2042
2043 // Bump the window.
2044 const uint32_t kExpectedStreamIFCW = 1024 * 1024;
2045 const uint32_t kExpectedSessionIFCW = 1.5 * 1024 * 1024;
2046 client_extra_copts_.push_back(kIFWA);
2047
2048 ASSERT_TRUE(Initialize());
2049
2050 // Values are exchanged during crypto handshake, so wait for that to finish.
2051 EXPECT_TRUE(client_->client()->WaitForCryptoHandshakeConfirmed());
2052 server_thread_->WaitForCryptoHandshakeConfirmed();
2053
2054 // Open a data stream to make sure the stream level flow control is updated.
2055 QuicSpdyClientStream* stream = client_->GetOrCreateStream();
2056 stream->WriteOrBufferBody("hello", false);
2057
2058 // Client should have the right values for server's receive window.
2059 EXPECT_EQ(kExpectedStreamIFCW,
2060 client_->client()
2061 ->client_session()
2062 ->config()
2063 ->ReceivedInitialStreamFlowControlWindowBytes());
2064 EXPECT_EQ(kExpectedSessionIFCW,
2065 client_->client()
2066 ->client_session()
2067 ->config()
2068 ->ReceivedInitialSessionFlowControlWindowBytes());
2069 EXPECT_EQ(kExpectedStreamIFCW, QuicFlowControllerPeer::SendWindowOffset(
2070 stream->flow_controller()));
2071 EXPECT_EQ(kExpectedSessionIFCW,
2072 QuicFlowControllerPeer::SendWindowOffset(
2073 client_->client()->client_session()->flow_controller()));
2074}
2075
2076TEST_P(EndToEndTest, HeadersAndCryptoStreamsNoConnectionFlowControl) {
2077 // The special headers and crypto streams should be subject to per-stream flow
2078 // control limits, but should not be subject to connection level flow control
2079 const uint32_t kStreamIFCW = 32 * 1024;
2080 const uint32_t kSessionIFCW = 48 * 1024;
2081 set_client_initial_stream_flow_control_receive_window(kStreamIFCW);
2082 set_client_initial_session_flow_control_receive_window(kSessionIFCW);
2083 set_server_initial_stream_flow_control_receive_window(kStreamIFCW);
2084 set_server_initial_session_flow_control_receive_window(kSessionIFCW);
2085
2086 ASSERT_TRUE(Initialize());
2087
2088 // Wait for crypto handshake to finish. This should have contributed to the
2089 // crypto stream flow control window, but not affected the session flow
2090 // control window.
2091 EXPECT_TRUE(client_->client()->WaitForCryptoHandshakeConfirmed());
2092 server_thread_->WaitForCryptoHandshakeConfirmed();
2093
2094 QuicCryptoStream* crypto_stream = QuicSessionPeer::GetMutableCryptoStream(
2095 client_->client()->client_session());
2096 // In v47 and later, the crypto handshake (sent in CRYPTO frames) is not
2097 // subject to flow control.
QUICHE teamea740082019-03-11 17:58:43 -07002098 if (!QuicVersionUsesCryptoFrames(client_->client()
2099 ->client_session()
2100 ->connection()
2101 ->transport_version())) {
QUICHE teama6ef0a62019-03-07 20:34:33 -05002102 EXPECT_LT(QuicFlowControllerPeer::SendWindowSize(
2103 crypto_stream->flow_controller()),
2104 kStreamIFCW);
2105 }
2106 EXPECT_EQ(kSessionIFCW,
2107 QuicFlowControllerPeer::SendWindowSize(
2108 client_->client()->client_session()->flow_controller()));
2109
2110 // Send a request with no body, and verify that the connection level window
2111 // has not been affected.
2112 EXPECT_EQ(kFooResponseBody, client_->SendSynchronousRequest("/foo"));
2113
2114 QuicHeadersStream* headers_stream = QuicSpdySessionPeer::GetHeadersStream(
2115 client_->client()->client_session());
2116 EXPECT_LT(
2117 QuicFlowControllerPeer::SendWindowSize(headers_stream->flow_controller()),
2118 kStreamIFCW);
2119 EXPECT_EQ(kSessionIFCW,
2120 QuicFlowControllerPeer::SendWindowSize(
2121 client_->client()->client_session()->flow_controller()));
2122
2123 // Server should be in a similar state: connection flow control window should
2124 // not have any bytes marked as received.
2125 server_thread_->Pause();
2126 QuicSession* session = GetServerSession();
2127 QuicFlowController* server_connection_flow_controller =
2128 session->flow_controller();
2129 EXPECT_EQ(kSessionIFCW, QuicFlowControllerPeer::ReceiveWindowSize(
2130 server_connection_flow_controller));
2131 server_thread_->Resume();
2132}
2133
2134TEST_P(EndToEndTest, FlowControlsSynced) {
2135 set_smaller_flow_control_receive_window();
2136
2137 ASSERT_TRUE(Initialize());
2138
2139 EXPECT_TRUE(client_->client()->WaitForCryptoHandshakeConfirmed());
2140 server_thread_->WaitForCryptoHandshakeConfirmed();
2141
2142 server_thread_->Pause();
2143 QuicSpdySession* const client_session = client_->client()->client_session();
2144 auto* server_session = static_cast<QuicSpdySession*>(GetServerSession());
2145 ExpectFlowControlsSynced(client_session->flow_controller(),
2146 server_session->flow_controller());
2147 ExpectFlowControlsSynced(
2148 QuicSessionPeer::GetMutableCryptoStream(client_session)
2149 ->flow_controller(),
2150 QuicSessionPeer::GetMutableCryptoStream(server_session)
2151 ->flow_controller());
2152 SpdyFramer spdy_framer(SpdyFramer::ENABLE_COMPRESSION);
2153 SpdySettingsIR settings_frame;
2154 settings_frame.AddSetting(SETTINGS_MAX_HEADER_LIST_SIZE,
2155 kDefaultMaxUncompressedHeaderSize);
2156 SpdySerializedFrame frame(spdy_framer.SerializeFrame(settings_frame));
2157 QuicFlowController* client_header_stream_flow_controller =
2158 QuicSpdySessionPeer::GetHeadersStream(client_session)->flow_controller();
2159 QuicFlowController* server_header_stream_flow_controller =
2160 QuicSpdySessionPeer::GetHeadersStream(server_session)->flow_controller();
2161 // Both client and server are sending this SETTINGS frame, and the send
2162 // window is consumed. But because of timing issue, the server may send or
2163 // not send the frame, and the client may send/ not send / receive / not
2164 // receive the frame.
2165 // TODO(fayang): Rewrite this part because it is hacky.
2166 QuicByteCount win_difference1 = QuicFlowControllerPeer::ReceiveWindowSize(
2167 server_header_stream_flow_controller) -
2168 QuicFlowControllerPeer::SendWindowSize(
2169 client_header_stream_flow_controller);
2170 QuicByteCount win_difference2 = QuicFlowControllerPeer::ReceiveWindowSize(
2171 client_header_stream_flow_controller) -
2172 QuicFlowControllerPeer::SendWindowSize(
2173 server_header_stream_flow_controller);
2174 EXPECT_TRUE(win_difference1 == 0 || win_difference1 == frame.size());
2175 EXPECT_TRUE(win_difference2 == 0 || win_difference2 == frame.size());
2176
2177 // Client *may* have received the SETTINGs frame.
2178 // TODO(fayang): Rewrite this part because it is hacky.
2179 float ratio1 = static_cast<float>(QuicFlowControllerPeer::ReceiveWindowSize(
2180 client_session->flow_controller())) /
2181 QuicFlowControllerPeer::ReceiveWindowSize(
2182 QuicSpdySessionPeer::GetHeadersStream(client_session)
2183 ->flow_controller());
2184 float ratio2 = static_cast<float>(QuicFlowControllerPeer::ReceiveWindowSize(
2185 client_session->flow_controller())) /
2186 (QuicFlowControllerPeer::ReceiveWindowSize(
2187 QuicSpdySessionPeer::GetHeadersStream(client_session)
2188 ->flow_controller()) +
2189 frame.size());
2190 EXPECT_TRUE(ratio1 == kSessionToStreamRatio ||
2191 ratio2 == kSessionToStreamRatio);
2192
2193 server_thread_->Resume();
2194}
2195
2196TEST_P(EndToEndTestWithTls, RequestWithNoBodyWillNeverSendStreamFrameWithFIN) {
2197 // A stream created on receipt of a simple request with no body will never get
2198 // a stream frame with a FIN. Verify that we don't keep track of the stream in
2199 // the locally closed streams map: it will never be removed if so.
2200 ASSERT_TRUE(Initialize());
2201
2202 // Send a simple headers only request, and receive response.
2203 EXPECT_EQ(kFooResponseBody, client_->SendSynchronousRequest("/foo"));
2204 EXPECT_EQ("200", client_->response_headers()->find(":status")->second);
2205
2206 // Now verify that the server is not waiting for a final FIN or RST.
2207 server_thread_->Pause();
2208 QuicSession* session = GetServerSession();
2209 EXPECT_EQ(
2210 0u,
2211 QuicSessionPeer::GetLocallyClosedStreamsHighestOffset(session).size());
2212 server_thread_->Resume();
2213}
2214
2215// A TestAckListener verifies that its OnAckNotification method has been
2216// called exactly once on destruction.
2217class TestAckListener : public QuicAckListenerInterface {
2218 public:
2219 explicit TestAckListener(int bytes_to_ack) : bytes_to_ack_(bytes_to_ack) {}
2220
2221 void OnPacketAcked(int acked_bytes,
2222 QuicTime::Delta /*delta_largest_observed*/) override {
2223 ASSERT_LE(acked_bytes, bytes_to_ack_);
2224 bytes_to_ack_ -= acked_bytes;
2225 }
2226
2227 void OnPacketRetransmitted(int /*retransmitted_bytes*/) override {}
2228
2229 bool has_been_notified() const { return bytes_to_ack_ == 0; }
2230
2231 protected:
2232 // Object is ref counted.
2233 ~TestAckListener() override { EXPECT_EQ(0, bytes_to_ack_); }
2234
2235 private:
2236 int bytes_to_ack_;
2237};
2238
2239class TestResponseListener : public QuicSpdyClientBase::ResponseListener {
2240 public:
2241 void OnCompleteResponse(QuicStreamId id,
2242 const SpdyHeaderBlock& response_headers,
vasilvvc48c8712019-03-11 13:38:16 -07002243 const std::string& response_body) override {
QUICHE teama6ef0a62019-03-07 20:34:33 -05002244 QUIC_DVLOG(1) << "response for stream " << id << " "
2245 << response_headers.DebugString() << "\n"
2246 << response_body;
2247 }
2248};
2249
2250TEST_P(EndToEndTest, AckNotifierWithPacketLossAndBlockedSocket) {
2251 // Verify that even in the presence of packet loss and occasionally blocked
2252 // socket, an AckNotifierDelegate will get informed that the data it is
2253 // interested in has been ACKed. This tests end-to-end ACK notification, and
2254 // demonstrates that retransmissions do not break this functionality.
2255 if (!BothSidesSupportStatelessRejects()) {
2256 // TODO(jokulik): Until we support redundant SREJ packets, don't
2257 // drop handshake packets for stateless rejects.
2258 SetPacketLossPercentage(5);
2259 }
2260 ASSERT_TRUE(Initialize());
2261
2262 // Wait for the server SHLO before upping the packet loss.
2263 EXPECT_TRUE(client_->client()->WaitForCryptoHandshakeConfirmed());
2264 SetPacketLossPercentage(30);
2265 client_writer_->set_fake_blocked_socket_percentage(10);
2266
2267 // Create a POST request and send the headers only.
2268 SpdyHeaderBlock headers;
2269 headers[":method"] = "POST";
2270 headers[":path"] = "/foo";
2271 headers[":scheme"] = "https";
2272 headers[":authority"] = server_hostname_;
2273
2274 client_->SendMessage(headers, "", /*fin=*/false);
2275
2276 // Test the AckNotifier's ability to track multiple packets by making the
2277 // request body exceed the size of a single packet.
vasilvvc48c8712019-03-11 13:38:16 -07002278 std::string request_string = "a request body bigger than one packet" +
dschinazi66dea072019-04-09 11:41:06 -07002279 std::string(kMaxOutgoingPacketSize, '.');
QUICHE teama6ef0a62019-03-07 20:34:33 -05002280
2281 // The TestAckListener will cause a failure if not notified.
2282 QuicReferenceCountedPointer<TestAckListener> ack_listener(
2283 new TestAckListener(request_string.length()));
2284
2285 // Send the request, and register the delegate for ACKs.
2286 client_->SendData(request_string, true, ack_listener);
2287 client_->WaitForResponse();
2288 EXPECT_EQ(kFooResponseBody, client_->response_body());
2289 EXPECT_EQ("200", client_->response_headers()->find(":status")->second);
2290
2291 // Send another request to flush out any pending ACKs on the server.
2292 client_->SendSynchronousRequest("/bar");
2293
2294 // Make sure the delegate does get the notification it expects.
2295 while (!ack_listener->has_been_notified()) {
2296 // Waits for up to 50 ms.
2297 client_->client()->WaitForEvents();
2298 }
2299}
2300
2301// Send a public reset from the server.
2302TEST_P(EndToEndTestWithTls, ServerSendPublicReset) {
2303 ASSERT_TRUE(Initialize());
2304
2305 EXPECT_TRUE(client_->client()->WaitForCryptoHandshakeConfirmed());
2306 QuicConnection* client_connection =
2307 client_->client()->client_session()->connection();
2308 if (SupportsIetfQuicWithTls(client_connection->version())) {
2309 // TLS handshake does not support stateless reset token yet.
2310 return;
2311 }
2312 QuicUint128 stateless_reset_token = 0;
2313 if (client_connection->version().handshake_protocol == PROTOCOL_QUIC_CRYPTO) {
2314 QuicConfig* config = client_->client()->session()->config();
2315 EXPECT_TRUE(config->HasReceivedStatelessResetToken());
2316 stateless_reset_token = config->ReceivedStatelessResetToken();
2317 }
2318
2319 // Send the public reset.
2320 QuicConnectionId connection_id = client_connection->connection_id();
2321 QuicPublicResetPacket header;
2322 header.connection_id = connection_id;
2323 QuicFramer framer(server_supported_versions_, QuicTime::Zero(),
2324 Perspective::IS_SERVER, kQuicDefaultConnectionIdLength);
2325 std::unique_ptr<QuicEncryptedPacket> packet;
2326 if (client_connection->transport_version() > QUIC_VERSION_43) {
2327 packet = framer.BuildIetfStatelessResetPacket(connection_id,
2328 stateless_reset_token);
2329 } else {
2330 packet = framer.BuildPublicResetPacket(header);
2331 }
2332 // We must pause the server's thread in order to call WritePacket without
2333 // race conditions.
2334 server_thread_->Pause();
2335 server_writer_->WritePacket(
2336 packet->data(), packet->length(), server_address_.host(),
2337 client_->client()->network_helper()->GetLatestClientAddress(), nullptr);
2338 server_thread_->Resume();
2339
2340 // The request should fail.
2341 EXPECT_EQ("", client_->SendSynchronousRequest("/foo"));
2342 EXPECT_TRUE(client_->response_headers()->empty());
2343 EXPECT_EQ(QUIC_PUBLIC_RESET, client_->connection_error());
2344}
2345
2346// Send a public reset from the server for a different connection ID.
2347// It should be ignored.
2348TEST_P(EndToEndTestWithTls, ServerSendPublicResetWithDifferentConnectionId) {
2349 ASSERT_TRUE(Initialize());
2350
2351 EXPECT_TRUE(client_->client()->WaitForCryptoHandshakeConfirmed());
2352 QuicConnection* client_connection =
2353 client_->client()->client_session()->connection();
2354 if (SupportsIetfQuicWithTls(client_connection->version())) {
2355 // TLS handshake does not support stateless reset token yet.
2356 return;
2357 }
2358 QuicUint128 stateless_reset_token = 0;
2359 if (client_connection->version().handshake_protocol == PROTOCOL_QUIC_CRYPTO) {
2360 QuicConfig* config = client_->client()->session()->config();
2361 EXPECT_TRUE(config->HasReceivedStatelessResetToken());
2362 stateless_reset_token = config->ReceivedStatelessResetToken();
2363 }
2364 // Send the public reset.
2365 QuicConnectionId incorrect_connection_id = TestConnectionId(
2366 TestConnectionIdToUInt64(client_connection->connection_id()) + 1);
2367 QuicPublicResetPacket header;
2368 header.connection_id = incorrect_connection_id;
2369 QuicFramer framer(server_supported_versions_, QuicTime::Zero(),
2370 Perspective::IS_SERVER, kQuicDefaultConnectionIdLength);
2371 std::unique_ptr<QuicEncryptedPacket> packet;
2372 testing::NiceMock<MockQuicConnectionDebugVisitor> visitor;
2373 client_->client()->client_session()->connection()->set_debug_visitor(
2374 &visitor);
2375 if (client_connection->transport_version() > QUIC_VERSION_43) {
2376 packet = framer.BuildIetfStatelessResetPacket(incorrect_connection_id,
2377 stateless_reset_token);
2378 EXPECT_CALL(visitor, OnIncorrectConnectionId(incorrect_connection_id))
2379 .Times(0);
2380 } else {
2381 packet = framer.BuildPublicResetPacket(header);
2382 EXPECT_CALL(visitor, OnIncorrectConnectionId(incorrect_connection_id))
2383 .Times(1);
2384 }
2385 // We must pause the server's thread in order to call WritePacket without
2386 // race conditions.
2387 server_thread_->Pause();
2388 server_writer_->WritePacket(
2389 packet->data(), packet->length(), server_address_.host(),
2390 client_->client()->network_helper()->GetLatestClientAddress(), nullptr);
2391 server_thread_->Resume();
2392
2393 if (client_connection->transport_version() > QUIC_VERSION_43) {
2394 // The request should fail. IETF stateless reset does not include connection
2395 // ID.
2396 EXPECT_EQ("", client_->SendSynchronousRequest("/foo"));
2397 EXPECT_TRUE(client_->response_headers()->empty());
2398 EXPECT_EQ(QUIC_PUBLIC_RESET, client_->connection_error());
2399 return;
2400 }
2401 // The connection should be unaffected.
2402 EXPECT_EQ(kFooResponseBody, client_->SendSynchronousRequest("/foo"));
2403 EXPECT_EQ("200", client_->response_headers()->find(":status")->second);
2404
2405 client_->client()->client_session()->connection()->set_debug_visitor(nullptr);
2406}
2407
2408// Send a public reset from the client for a different connection ID.
2409// It should be ignored.
2410TEST_P(EndToEndTestWithTls, ClientSendPublicResetWithDifferentConnectionId) {
2411 ASSERT_TRUE(Initialize());
2412
2413 // Send the public reset.
2414 QuicConnectionId incorrect_connection_id = TestConnectionId(
2415 TestConnectionIdToUInt64(
2416 client_->client()->client_session()->connection()->connection_id()) +
2417 1);
2418 QuicPublicResetPacket header;
2419 header.connection_id = incorrect_connection_id;
2420 QuicFramer framer(server_supported_versions_, QuicTime::Zero(),
2421 Perspective::IS_CLIENT, kQuicDefaultConnectionIdLength);
2422 std::unique_ptr<QuicEncryptedPacket> packet(
2423 framer.BuildPublicResetPacket(header));
2424 client_writer_->WritePacket(
2425 packet->data(), packet->length(),
2426 client_->client()->network_helper()->GetLatestClientAddress().host(),
2427 server_address_, nullptr);
2428
2429 // The connection should be unaffected.
2430 EXPECT_EQ(kFooResponseBody, client_->SendSynchronousRequest("/foo"));
2431 EXPECT_EQ("200", client_->response_headers()->find(":status")->second);
2432}
2433
2434// Send a version negotiation packet from the server for a different
2435// connection ID. It should be ignored.
2436TEST_P(EndToEndTestWithTls,
2437 ServerSendVersionNegotiationWithDifferentConnectionId) {
2438 ASSERT_TRUE(Initialize());
2439
2440 EXPECT_TRUE(client_->client()->WaitForCryptoHandshakeConfirmed());
2441
2442 // Send the version negotiation packet.
2443 QuicConnection* client_connection =
2444 client_->client()->client_session()->connection();
2445 QuicConnectionId incorrect_connection_id = TestConnectionId(
2446 TestConnectionIdToUInt64(client_connection->connection_id()) + 1);
2447 std::unique_ptr<QuicEncryptedPacket> packet(
2448 QuicFramer::BuildVersionNegotiationPacket(
2449 incorrect_connection_id,
2450 client_connection->transport_version() > QUIC_VERSION_43,
2451 server_supported_versions_));
2452 testing::NiceMock<MockQuicConnectionDebugVisitor> visitor;
2453 client_connection->set_debug_visitor(&visitor);
2454 EXPECT_CALL(visitor, OnIncorrectConnectionId(incorrect_connection_id))
2455 .Times(1);
2456 // We must pause the server's thread in order to call WritePacket without
2457 // race conditions.
2458 server_thread_->Pause();
2459 server_writer_->WritePacket(
2460 packet->data(), packet->length(), server_address_.host(),
2461 client_->client()->network_helper()->GetLatestClientAddress(), nullptr);
2462 server_thread_->Resume();
2463
2464 // The connection should be unaffected.
2465 EXPECT_EQ(kFooResponseBody, client_->SendSynchronousRequest("/foo"));
2466 EXPECT_EQ("200", client_->response_headers()->find(":status")->second);
2467
2468 client_connection->set_debug_visitor(nullptr);
2469}
2470
2471// A bad header shouldn't tear down the connection, because the receiver can't
2472// tell the connection ID.
2473TEST_P(EndToEndTestWithTls, BadPacketHeaderTruncated) {
2474 ASSERT_TRUE(Initialize());
2475
2476 // Start the connection.
2477 EXPECT_EQ(kFooResponseBody, client_->SendSynchronousRequest("/foo"));
2478 EXPECT_EQ("200", client_->response_headers()->find(":status")->second);
2479
2480 // Packet with invalid public flags.
2481 char packet[] = {// public flags (8 byte connection_id)
2482 0x3C,
2483 // truncated connection ID
2484 0x11};
2485 client_writer_->WritePacket(
2486 &packet[0], sizeof(packet),
2487 client_->client()->network_helper()->GetLatestClientAddress().host(),
2488 server_address_, nullptr);
2489 // Give the server time to process the packet.
2490 QuicSleep(QuicTime::Delta::FromMilliseconds(100));
2491 // Pause the server so we can access the server's internals without races.
2492 server_thread_->Pause();
2493 QuicDispatcher* dispatcher =
2494 QuicServerPeer::GetDispatcher(server_thread_->server());
2495 EXPECT_EQ(QUIC_INVALID_PACKET_HEADER,
2496 QuicDispatcherPeer::GetAndClearLastError(dispatcher));
2497 server_thread_->Resume();
2498
2499 // The connection should not be terminated.
2500 EXPECT_EQ(kFooResponseBody, client_->SendSynchronousRequest("/foo"));
2501 EXPECT_EQ("200", client_->response_headers()->find(":status")->second);
2502}
2503
2504// A bad header shouldn't tear down the connection, because the receiver can't
2505// tell the connection ID.
2506TEST_P(EndToEndTestWithTls, BadPacketHeaderFlags) {
2507 ASSERT_TRUE(Initialize());
2508
2509 // Start the connection.
2510 EXPECT_EQ(kFooResponseBody, client_->SendSynchronousRequest("/foo"));
2511 EXPECT_EQ("200", client_->response_headers()->find(":status")->second);
2512
2513 // Packet with invalid public flags.
2514 char packet[] = {
2515 // invalid public flags
2516 0xFF,
2517 // connection_id
2518 0x10,
2519 0x32,
2520 0x54,
2521 0x76,
2522 0x98,
2523 0xBA,
2524 0xDC,
2525 0xFE,
2526 // packet sequence number
2527 0xBC,
2528 0x9A,
2529 0x78,
2530 0x56,
2531 0x34,
2532 0x12,
2533 // private flags
2534 0x00,
2535 };
2536 client_writer_->WritePacket(
2537 &packet[0], sizeof(packet),
2538 client_->client()->network_helper()->GetLatestClientAddress().host(),
2539 server_address_, nullptr);
2540 // Give the server time to process the packet.
2541 QuicSleep(QuicTime::Delta::FromMilliseconds(100));
2542 // Pause the server so we can access the server's internals without races.
2543 server_thread_->Pause();
2544 QuicDispatcher* dispatcher =
2545 QuicServerPeer::GetDispatcher(server_thread_->server());
2546 EXPECT_EQ(QUIC_INVALID_PACKET_HEADER,
2547 QuicDispatcherPeer::GetAndClearLastError(dispatcher));
2548 server_thread_->Resume();
2549
2550 // The connection should not be terminated.
2551 EXPECT_EQ(kFooResponseBody, client_->SendSynchronousRequest("/foo"));
2552 EXPECT_EQ("200", client_->response_headers()->find(":status")->second);
2553}
2554
2555// Send a packet from the client with bad encrypted data. The server should not
2556// tear down the connection.
2557TEST_P(EndToEndTestWithTls, BadEncryptedData) {
2558 ASSERT_TRUE(Initialize());
2559
2560 // Start the connection.
2561 EXPECT_EQ(kFooResponseBody, client_->SendSynchronousRequest("/foo"));
2562 EXPECT_EQ("200", client_->response_headers()->find(":status")->second);
2563
2564 std::unique_ptr<QuicEncryptedPacket> packet(ConstructEncryptedPacket(
2565 client_->client()->client_session()->connection()->connection_id(),
2566 EmptyQuicConnectionId(), false, false, 1, "At least 20 characters.",
2567 CONNECTION_ID_PRESENT, CONNECTION_ID_ABSENT, PACKET_4BYTE_PACKET_NUMBER));
2568 // Damage the encrypted data.
vasilvvc48c8712019-03-11 13:38:16 -07002569 std::string damaged_packet(packet->data(), packet->length());
QUICHE teama6ef0a62019-03-07 20:34:33 -05002570 damaged_packet[30] ^= 0x01;
2571 QUIC_DLOG(INFO) << "Sending bad packet.";
2572 client_writer_->WritePacket(
2573 damaged_packet.data(), damaged_packet.length(),
2574 client_->client()->network_helper()->GetLatestClientAddress().host(),
2575 server_address_, nullptr);
2576 // Give the server time to process the packet.
2577 QuicSleep(QuicTime::Delta::FromMilliseconds(100));
2578 // This error is sent to the connection's OnError (which ignores it), so the
2579 // dispatcher doesn't see it.
2580 // Pause the server so we can access the server's internals without races.
2581 server_thread_->Pause();
2582 QuicDispatcher* dispatcher =
2583 QuicServerPeer::GetDispatcher(server_thread_->server());
2584 EXPECT_EQ(QUIC_NO_ERROR,
2585 QuicDispatcherPeer::GetAndClearLastError(dispatcher));
2586 server_thread_->Resume();
2587
2588 // The connection should not be terminated.
2589 EXPECT_EQ(kFooResponseBody, client_->SendSynchronousRequest("/foo"));
2590 EXPECT_EQ("200", client_->response_headers()->find(":status")->second);
2591}
2592
2593TEST_P(EndToEndTestWithTls, CanceledStreamDoesNotBecomeZombie) {
2594 ASSERT_TRUE(Initialize());
2595 EXPECT_TRUE(client_->client()->WaitForCryptoHandshakeConfirmed());
2596 // Lose the request.
2597 SetPacketLossPercentage(100);
2598 SpdyHeaderBlock headers;
2599 headers[":method"] = "POST";
2600 headers[":path"] = "/foo";
2601 headers[":scheme"] = "https";
2602 headers[":authority"] = server_hostname_;
2603 client_->SendMessage(headers, "test_body", /*fin=*/false);
2604 QuicSpdyClientStream* stream = client_->GetOrCreateStream();
2605
2606 // Cancel the stream.
2607 stream->Reset(QUIC_STREAM_CANCELLED);
2608 QuicSession* session = client_->client()->client_session();
2609 // Verify canceled stream does not become zombie.
2610 EXPECT_TRUE(QuicSessionPeer::zombie_streams(session).empty());
2611 EXPECT_EQ(1u, QuicSessionPeer::closed_streams(session).size());
2612}
2613
2614// A test stream that gives |response_body_| as an error response body.
2615class ServerStreamWithErrorResponseBody : public QuicSimpleServerStream {
2616 public:
2617 ServerStreamWithErrorResponseBody(
2618 QuicStreamId id,
2619 QuicSpdySession* session,
2620 QuicSimpleServerBackend* quic_simple_server_backend,
vasilvvc48c8712019-03-11 13:38:16 -07002621 std::string response_body)
QUICHE teama6ef0a62019-03-07 20:34:33 -05002622 : QuicSimpleServerStream(id,
2623 session,
2624 BIDIRECTIONAL,
2625 quic_simple_server_backend),
2626 response_body_(std::move(response_body)) {}
2627
2628 ~ServerStreamWithErrorResponseBody() override = default;
2629
2630 protected:
2631 void SendErrorResponse() override {
2632 QUIC_DLOG(INFO) << "Sending error response for stream " << id();
2633 SpdyHeaderBlock headers;
2634 headers[":status"] = "500";
2635 headers["content-length"] =
2636 QuicTextUtils::Uint64ToString(response_body_.size());
2637 // This method must call CloseReadSide to cause the test case, StopReading
2638 // is not sufficient.
2639 QuicStreamPeer::CloseReadSide(this);
2640 SendHeadersAndBody(std::move(headers), response_body_);
2641 }
2642
vasilvvc48c8712019-03-11 13:38:16 -07002643 std::string response_body_;
QUICHE teama6ef0a62019-03-07 20:34:33 -05002644};
2645
2646class StreamWithErrorFactory : public QuicTestServer::StreamFactory {
2647 public:
vasilvvc48c8712019-03-11 13:38:16 -07002648 explicit StreamWithErrorFactory(std::string response_body)
QUICHE teama6ef0a62019-03-07 20:34:33 -05002649 : response_body_(std::move(response_body)) {}
2650
2651 ~StreamWithErrorFactory() override = default;
2652
2653 QuicSimpleServerStream* CreateStream(
2654 QuicStreamId id,
2655 QuicSpdySession* session,
2656 QuicSimpleServerBackend* quic_simple_server_backend) override {
2657 return new ServerStreamWithErrorResponseBody(
2658 id, session, quic_simple_server_backend, response_body_);
2659 }
2660
2661 private:
vasilvvc48c8712019-03-11 13:38:16 -07002662 std::string response_body_;
QUICHE teama6ef0a62019-03-07 20:34:33 -05002663};
2664
2665// A test server stream that drops all received body.
2666class ServerStreamThatDropsBody : public QuicSimpleServerStream {
2667 public:
2668 ServerStreamThatDropsBody(QuicStreamId id,
2669 QuicSpdySession* session,
2670 QuicSimpleServerBackend* quic_simple_server_backend)
2671 : QuicSimpleServerStream(id,
2672 session,
2673 BIDIRECTIONAL,
2674 quic_simple_server_backend) {}
2675
2676 ~ServerStreamThatDropsBody() override = default;
2677
2678 protected:
2679 void OnBodyAvailable() override {
2680 while (HasBytesToRead()) {
2681 struct iovec iov;
2682 if (GetReadableRegions(&iov, 1) == 0) {
2683 // No more data to read.
2684 break;
2685 }
2686 QUIC_DVLOG(1) << "Processed " << iov.iov_len << " bytes for stream "
2687 << id();
2688 MarkConsumed(iov.iov_len);
2689 }
2690
2691 if (!sequencer()->IsClosed()) {
2692 sequencer()->SetUnblocked();
2693 return;
2694 }
2695
2696 // If the sequencer is closed, then all the body, including the fin, has
2697 // been consumed.
2698 OnFinRead();
2699
2700 if (write_side_closed() || fin_buffered()) {
2701 return;
2702 }
2703
2704 SendResponse();
2705 }
2706};
2707
2708class ServerStreamThatDropsBodyFactory : public QuicTestServer::StreamFactory {
2709 public:
2710 ServerStreamThatDropsBodyFactory() = default;
2711
2712 ~ServerStreamThatDropsBodyFactory() override = default;
2713
2714 QuicSimpleServerStream* CreateStream(
2715 QuicStreamId id,
2716 QuicSpdySession* session,
2717 QuicSimpleServerBackend* quic_simple_server_backend) override {
2718 return new ServerStreamThatDropsBody(id, session,
2719 quic_simple_server_backend);
2720 }
2721};
2722
2723// A test server stream that sends response with body size greater than 4GB.
2724class ServerStreamThatSendsHugeResponse : public QuicSimpleServerStream {
2725 public:
2726 ServerStreamThatSendsHugeResponse(
2727 QuicStreamId id,
2728 QuicSpdySession* session,
2729 QuicSimpleServerBackend* quic_simple_server_backend,
2730 int64_t body_bytes)
2731 : QuicSimpleServerStream(id,
2732 session,
2733 BIDIRECTIONAL,
2734 quic_simple_server_backend),
2735 body_bytes_(body_bytes) {}
2736
2737 ~ServerStreamThatSendsHugeResponse() override = default;
2738
2739 protected:
2740 void SendResponse() override {
2741 QuicBackendResponse response;
vasilvvc48c8712019-03-11 13:38:16 -07002742 std::string body(body_bytes_, 'a');
QUICHE teama6ef0a62019-03-07 20:34:33 -05002743 response.set_body(body);
2744 SendHeadersAndBodyAndTrailers(response.headers().Clone(), response.body(),
2745 response.trailers().Clone());
2746 }
2747
2748 private:
2749 // Use a explicit int64_t rather than size_t to simulate a 64-bit server
2750 // talking to a 32-bit client.
2751 int64_t body_bytes_;
2752};
2753
2754class ServerStreamThatSendsHugeResponseFactory
2755 : public QuicTestServer::StreamFactory {
2756 public:
2757 explicit ServerStreamThatSendsHugeResponseFactory(int64_t body_bytes)
2758 : body_bytes_(body_bytes) {}
2759
2760 ~ServerStreamThatSendsHugeResponseFactory() override = default;
2761
2762 QuicSimpleServerStream* CreateStream(
2763 QuicStreamId id,
2764 QuicSpdySession* session,
2765 QuicSimpleServerBackend* quic_simple_server_backend) override {
2766 return new ServerStreamThatSendsHugeResponse(
2767 id, session, quic_simple_server_backend, body_bytes_);
2768 }
2769
2770 int64_t body_bytes_;
2771};
2772
2773TEST_P(EndToEndTest, EarlyResponseFinRecording) {
2774 set_smaller_flow_control_receive_window();
2775
2776 // Verify that an incoming FIN is recorded in a stream object even if the read
2777 // side has been closed. This prevents an entry from being made in
2778 // locally_close_streams_highest_offset_ (which will never be deleted).
2779 // To set up the test condition, the server must do the following in order:
2780 // start sending the response and call CloseReadSide
2781 // receive the FIN of the request
2782 // send the FIN of the response
2783
2784 // The response body must be larger than the flow control window so the server
2785 // must receive a window update from the client before it can finish sending
2786 // it.
2787 uint32_t response_body_size =
2788 2 * client_config_.GetInitialStreamFlowControlWindowToSend();
vasilvvc48c8712019-03-11 13:38:16 -07002789 std::string response_body(response_body_size, 'a');
QUICHE teama6ef0a62019-03-07 20:34:33 -05002790
2791 StreamWithErrorFactory stream_factory(response_body);
2792 SetSpdyStreamFactory(&stream_factory);
2793
2794 ASSERT_TRUE(Initialize());
2795
2796 EXPECT_TRUE(client_->client()->WaitForCryptoHandshakeConfirmed());
2797
2798 // A POST that gets an early error response, after the headers are received
2799 // and before the body is received, due to invalid content-length.
2800 // Set an invalid content-length, so the request will receive an early 500
2801 // response.
2802 SpdyHeaderBlock headers;
2803 headers[":method"] = "POST";
2804 headers[":path"] = "/garbage";
2805 headers[":scheme"] = "https";
2806 headers[":authority"] = server_hostname_;
2807 headers["content-length"] = "-1";
2808
2809 // The body must be large enough that the FIN will be in a different packet
2810 // than the end of the headers, but short enough to not require a flow control
2811 // update. This allows headers processing to trigger the error response
2812 // before the request FIN is processed but receive the request FIN before the
2813 // response is sent completely.
dschinazi66dea072019-04-09 11:41:06 -07002814 const uint32_t kRequestBodySize = kMaxOutgoingPacketSize + 10;
vasilvvc48c8712019-03-11 13:38:16 -07002815 std::string request_body(kRequestBodySize, 'a');
QUICHE teama6ef0a62019-03-07 20:34:33 -05002816
2817 // Send the request.
2818 client_->SendMessage(headers, request_body);
2819 client_->WaitForResponse();
2820 EXPECT_EQ("500", client_->response_headers()->find(":status")->second);
2821
2822 // Pause the server so we can access the server's internals without races.
2823 server_thread_->Pause();
2824
2825 QuicDispatcher* dispatcher =
2826 QuicServerPeer::GetDispatcher(server_thread_->server());
2827 QuicDispatcher::SessionMap const& map =
2828 QuicDispatcherPeer::session_map(dispatcher);
2829 auto it = map.begin();
2830 EXPECT_TRUE(it != map.end());
2831 QuicSession* server_session = it->second.get();
2832
2833 // The stream is not waiting for the arrival of the peer's final offset.
2834 EXPECT_EQ(
2835 0u, QuicSessionPeer::GetLocallyClosedStreamsHighestOffset(server_session)
2836 .size());
2837
2838 server_thread_->Resume();
2839}
2840
2841TEST_P(EndToEndTestWithTls, Trailers) {
2842 // Test sending and receiving HTTP/2 Trailers (trailing HEADERS frames).
2843 ASSERT_TRUE(Initialize());
2844 EXPECT_TRUE(client_->client()->WaitForCryptoHandshakeConfirmed());
2845
2846 // Set reordering to ensure that Trailers arriving before body is ok.
2847 SetPacketSendDelay(QuicTime::Delta::FromMilliseconds(2));
2848 SetReorderPercentage(30);
2849
2850 // Add a response with headers, body, and trailers.
vasilvvc48c8712019-03-11 13:38:16 -07002851 const std::string kBody = "body content";
QUICHE teama6ef0a62019-03-07 20:34:33 -05002852
2853 SpdyHeaderBlock headers;
2854 headers[":status"] = "200";
2855 headers[":version"] = "HTTP/1.1";
2856 headers["content-length"] = QuicTextUtils::Uint64ToString(kBody.size());
2857
2858 SpdyHeaderBlock trailers;
2859 trailers["some-trailing-header"] = "trailing-header-value";
2860
2861 memory_cache_backend_.AddResponse(server_hostname_, "/trailer_url",
2862 std::move(headers), kBody,
2863 trailers.Clone());
2864
2865 EXPECT_EQ(kBody, client_->SendSynchronousRequest("/trailer_url"));
2866 EXPECT_EQ("200", client_->response_headers()->find(":status")->second);
2867 EXPECT_EQ(trailers, client_->response_trailers());
2868}
2869
2870class EndToEndTestServerPush : public EndToEndTest {
2871 protected:
2872 const size_t kNumMaxStreams = 10;
2873
2874 EndToEndTestServerPush() : EndToEndTest() {
2875 client_config_.SetMaxIncomingDynamicStreamsToSend(kNumMaxStreams);
2876 server_config_.SetMaxIncomingDynamicStreamsToSend(kNumMaxStreams);
2877 support_server_push_ = true;
2878 }
2879
2880 // Add a request with its response and |num_resources| push resources into
2881 // cache.
2882 // If |resource_size| == 0, response body of push resources use default string
2883 // concatenating with resource url. Otherwise, generate a string of
2884 // |resource_size| as body.
vasilvvc48c8712019-03-11 13:38:16 -07002885 void AddRequestAndResponseWithServerPush(std::string host,
2886 std::string path,
2887 std::string response_body,
2888 std::string* push_urls,
QUICHE teama6ef0a62019-03-07 20:34:33 -05002889 const size_t num_resources,
2890 const size_t resource_size) {
2891 bool use_large_response = resource_size != 0;
vasilvvc48c8712019-03-11 13:38:16 -07002892 std::string large_resource;
QUICHE teama6ef0a62019-03-07 20:34:33 -05002893 if (use_large_response) {
2894 // Generate a response common body larger than flow control window for
2895 // push response.
vasilvvc48c8712019-03-11 13:38:16 -07002896 large_resource = std::string(resource_size, 'a');
QUICHE teama6ef0a62019-03-07 20:34:33 -05002897 }
2898 std::list<QuicBackendResponse::ServerPushInfo> push_resources;
2899 for (size_t i = 0; i < num_resources; ++i) {
vasilvvc48c8712019-03-11 13:38:16 -07002900 std::string url = push_urls[i];
QUICHE teama6ef0a62019-03-07 20:34:33 -05002901 QuicUrl resource_url(url);
vasilvvc48c8712019-03-11 13:38:16 -07002902 std::string body =
QUICHE teama6ef0a62019-03-07 20:34:33 -05002903 use_large_response
2904 ? large_resource
2905 : QuicStrCat("This is server push response body for ", url);
2906 SpdyHeaderBlock response_headers;
2907 response_headers[":version"] = "HTTP/1.1";
2908 response_headers[":status"] = "200";
2909 response_headers["content-length"] =
2910 QuicTextUtils::Uint64ToString(body.size());
2911 push_resources.push_back(QuicBackendResponse::ServerPushInfo(
2912 resource_url, std::move(response_headers), kV3LowestPriority, body));
2913 }
2914
2915 memory_cache_backend_.AddSimpleResponseWithServerPushResources(
2916 host, path, 200, response_body, push_resources);
2917 }
2918};
2919
2920// Run all server push end to end tests with all supported versions.
2921INSTANTIATE_TEST_SUITE_P(EndToEndTestsServerPush,
2922 EndToEndTestServerPush,
2923 ::testing::ValuesIn(GetTestParams(false, false)));
2924
2925TEST_P(EndToEndTestServerPush, ServerPush) {
2926 ASSERT_TRUE(Initialize());
2927 EXPECT_TRUE(client_->client()->WaitForCryptoHandshakeConfirmed());
2928
2929 // Set reordering to ensure that body arriving before PUSH_PROMISE is ok.
2930 SetPacketSendDelay(QuicTime::Delta::FromMilliseconds(2));
2931 SetReorderPercentage(30);
2932
2933 // Add a response with headers, body, and push resources.
vasilvvc48c8712019-03-11 13:38:16 -07002934 const std::string kBody = "body content";
QUICHE teama6ef0a62019-03-07 20:34:33 -05002935 size_t kNumResources = 4;
vasilvvc48c8712019-03-11 13:38:16 -07002936 std::string push_urls[] = {"https://example.com/font.woff",
2937 "https://example.com/script.js",
2938 "https://fonts.example.com/font.woff",
2939 "https://example.com/logo-hires.jpg"};
QUICHE teama6ef0a62019-03-07 20:34:33 -05002940 AddRequestAndResponseWithServerPush("example.com", "/push_example", kBody,
2941 push_urls, kNumResources, 0);
2942
2943 client_->client()->set_response_listener(
2944 std::unique_ptr<QuicSpdyClientBase::ResponseListener>(
2945 new TestResponseListener));
2946
2947 QUIC_DVLOG(1) << "send request for /push_example";
2948 EXPECT_EQ(kBody, client_->SendSynchronousRequest(
2949 "https://example.com/push_example"));
2950 QuicHeadersStream* headers_stream = QuicSpdySessionPeer::GetHeadersStream(
2951 client_->client()->client_session());
2952 QuicStreamSequencer* sequencer = QuicStreamPeer::sequencer(headers_stream);
2953 // Headers stream's sequencer buffer shouldn't be released because server push
2954 // hasn't finished yet.
2955 EXPECT_TRUE(QuicStreamSequencerPeer::IsUnderlyingBufferAllocated(sequencer));
2956
vasilvvc48c8712019-03-11 13:38:16 -07002957 for (const std::string& url : push_urls) {
QUICHE teama6ef0a62019-03-07 20:34:33 -05002958 QUIC_DVLOG(1) << "send request for pushed stream on url " << url;
vasilvvc48c8712019-03-11 13:38:16 -07002959 std::string expected_body =
QUICHE teama6ef0a62019-03-07 20:34:33 -05002960 QuicStrCat("This is server push response body for ", url);
vasilvvc48c8712019-03-11 13:38:16 -07002961 std::string response_body = client_->SendSynchronousRequest(url);
QUICHE teama6ef0a62019-03-07 20:34:33 -05002962 QUIC_DVLOG(1) << "response body " << response_body;
2963 EXPECT_EQ(expected_body, response_body);
2964 }
2965 EXPECT_FALSE(QuicStreamSequencerPeer::IsUnderlyingBufferAllocated(sequencer));
2966}
2967
2968TEST_P(EndToEndTestServerPush, ServerPushUnderLimit) {
2969 // Tests that sending a request which has 4 push resources will trigger server
2970 // to push those 4 resources and client can handle pushed resources and match
2971 // them with requests later.
2972 ASSERT_TRUE(Initialize());
2973
2974 EXPECT_TRUE(client_->client()->WaitForCryptoHandshakeConfirmed());
2975
2976 // Set reordering to ensure that body arriving before PUSH_PROMISE is ok.
2977 SetPacketSendDelay(QuicTime::Delta::FromMilliseconds(2));
2978 SetReorderPercentage(30);
2979
2980 // Add a response with headers, body, and push resources.
vasilvvc48c8712019-03-11 13:38:16 -07002981 const std::string kBody = "body content";
QUICHE teama6ef0a62019-03-07 20:34:33 -05002982 size_t const kNumResources = 4;
vasilvvc48c8712019-03-11 13:38:16 -07002983 std::string push_urls[] = {
QUICHE teama6ef0a62019-03-07 20:34:33 -05002984 "https://example.com/font.woff",
2985 "https://example.com/script.js",
2986 "https://fonts.example.com/font.woff",
2987 "https://example.com/logo-hires.jpg",
2988 };
2989 AddRequestAndResponseWithServerPush("example.com", "/push_example", kBody,
2990 push_urls, kNumResources, 0);
2991 client_->client()->set_response_listener(
2992 std::unique_ptr<QuicSpdyClientBase::ResponseListener>(
2993 new TestResponseListener));
2994
2995 // Send the first request: this will trigger the server to send all the push
2996 // resources associated with this request, and these will be cached by the
2997 // client.
2998 EXPECT_EQ(kBody, client_->SendSynchronousRequest(
2999 "https://example.com/push_example"));
3000
vasilvvc48c8712019-03-11 13:38:16 -07003001 for (const std::string& url : push_urls) {
QUICHE teama6ef0a62019-03-07 20:34:33 -05003002 // Sending subsequent requesets will not actually send anything on the wire,
3003 // as the responses are already in the client's cache.
3004 QUIC_DVLOG(1) << "send request for pushed stream on url " << url;
vasilvvc48c8712019-03-11 13:38:16 -07003005 std::string expected_body =
QUICHE teama6ef0a62019-03-07 20:34:33 -05003006 QuicStrCat("This is server push response body for ", url);
vasilvvc48c8712019-03-11 13:38:16 -07003007 std::string response_body = client_->SendSynchronousRequest(url);
QUICHE teama6ef0a62019-03-07 20:34:33 -05003008 QUIC_DVLOG(1) << "response body " << response_body;
3009 EXPECT_EQ(expected_body, response_body);
3010 }
3011 // Expect only original request has been sent and push responses have been
3012 // received as normal response.
3013 EXPECT_EQ(1u, client_->num_requests());
3014 EXPECT_EQ(1u + kNumResources, client_->num_responses());
3015}
3016
3017TEST_P(EndToEndTestServerPush, ServerPushOverLimitNonBlocking) {
3018 // Tests that when streams are not blocked by flow control or congestion
3019 // control, pushing even more resources than max number of open outgoing
3020 // streams should still work because all response streams get closed
3021 // immediately after pushing resources.
3022 ASSERT_TRUE(Initialize());
3023 EXPECT_TRUE(client_->client()->WaitForCryptoHandshakeConfirmed());
3024
3025 // Set reordering to ensure that body arriving before PUSH_PROMISE is ok.
3026 SetPacketSendDelay(QuicTime::Delta::FromMilliseconds(2));
3027 SetReorderPercentage(30);
3028
3029 // Add a response with headers, body, and push resources.
vasilvvc48c8712019-03-11 13:38:16 -07003030 const std::string kBody = "body content";
QUICHE teama6ef0a62019-03-07 20:34:33 -05003031
3032 // One more resource than max number of outgoing stream of this session.
3033 const size_t kNumResources = 1 + kNumMaxStreams; // 11.
vasilvvc48c8712019-03-11 13:38:16 -07003034 std::string push_urls[11];
QUICHE teama6ef0a62019-03-07 20:34:33 -05003035 for (size_t i = 0; i < kNumResources; ++i) {
3036 push_urls[i] = QuicStrCat("https://example.com/push_resources", i);
3037 }
3038 AddRequestAndResponseWithServerPush("example.com", "/push_example", kBody,
3039 push_urls, kNumResources, 0);
3040 client_->client()->set_response_listener(
3041 std::unique_ptr<QuicSpdyClientBase::ResponseListener>(
3042 new TestResponseListener));
3043
3044 // Send the first request: this will trigger the server to send all the push
3045 // resources associated with this request, and these will be cached by the
3046 // client.
3047 EXPECT_EQ(kBody, client_->SendSynchronousRequest(
3048 "https://example.com/push_example"));
3049
vasilvvc48c8712019-03-11 13:38:16 -07003050 for (const std::string& url : push_urls) {
QUICHE teama6ef0a62019-03-07 20:34:33 -05003051 // Sending subsequent requesets will not actually send anything on the wire,
3052 // as the responses are already in the client's cache.
3053 EXPECT_EQ(QuicStrCat("This is server push response body for ", url),
3054 client_->SendSynchronousRequest(url));
3055 }
3056
3057 // Only 1 request should have been sent.
3058 EXPECT_EQ(1u, client_->num_requests());
3059 // The responses to the original request and all the promised resources
3060 // should have been received.
3061 EXPECT_EQ(12u, client_->num_responses());
3062}
3063
3064TEST_P(EndToEndTestServerPush, ServerPushOverLimitWithBlocking) {
3065 // Tests that when server tries to send more large resources(large enough to
3066 // be blocked by flow control window or congestion control window) than max
3067 // open outgoing streams , server can open upto max number of outgoing
3068 // streams for them, and the rest will be queued up.
3069
3070 // Reset flow control windows.
3071 size_t kFlowControlWnd = 20 * 1024; // 20KB.
3072 // Response body is larger than 1 flow controlblock window.
3073 size_t kBodySize = kFlowControlWnd * 2;
3074 set_client_initial_stream_flow_control_receive_window(kFlowControlWnd);
3075 // Make sure conntection level flow control window is large enough not to
3076 // block data being sent out though they will be blocked by stream level one.
3077 set_client_initial_session_flow_control_receive_window(
3078 kBodySize * kNumMaxStreams + 1024);
3079
3080 ASSERT_TRUE(Initialize());
3081 EXPECT_TRUE(client_->client()->WaitForCryptoHandshakeConfirmed());
3082
3083 // Set reordering to ensure that body arriving before PUSH_PROMISE is ok.
3084 SetPacketSendDelay(QuicTime::Delta::FromMilliseconds(2));
3085 SetReorderPercentage(30);
3086
3087 // Add a response with headers, body, and push resources.
vasilvvc48c8712019-03-11 13:38:16 -07003088 const std::string kBody = "body content";
QUICHE teama6ef0a62019-03-07 20:34:33 -05003089
3090 const size_t kNumResources = kNumMaxStreams + 1;
vasilvvc48c8712019-03-11 13:38:16 -07003091 std::string push_urls[11];
QUICHE teama6ef0a62019-03-07 20:34:33 -05003092 for (size_t i = 0; i < kNumResources; ++i) {
3093 push_urls[i] = QuicStrCat("http://example.com/push_resources", i);
3094 }
3095 AddRequestAndResponseWithServerPush("example.com", "/push_example", kBody,
3096 push_urls, kNumResources, kBodySize);
3097
3098 client_->client()->set_response_listener(
3099 std::unique_ptr<QuicSpdyClientBase::ResponseListener>(
3100 new TestResponseListener));
3101
3102 client_->SendRequest("https://example.com/push_example");
3103
3104 // Pause after the first response arrives.
3105 while (!client_->response_complete()) {
3106 // Because of priority, the first response arrived should be to original
3107 // request.
3108 client_->WaitForResponse();
3109 }
3110
3111 // Check server session to see if it has max number of outgoing streams opened
3112 // though more resources need to be pushed.
3113 server_thread_->Pause();
3114 EXPECT_EQ(kNumMaxStreams, GetServerSession()->GetNumOpenOutgoingStreams());
3115 server_thread_->Resume();
3116
3117 EXPECT_EQ(1u, client_->num_requests());
3118 EXPECT_EQ(1u, client_->num_responses());
3119 EXPECT_EQ(kBody, client_->response_body());
3120
3121 // "Send" request for a promised resources will not really send out it because
3122 // its response is being pushed(but blocked). And the following ack and
3123 // flow control behavior of SendSynchronousRequests()
3124 // will unblock the stream to finish receiving response.
3125 client_->SendSynchronousRequest(push_urls[0]);
3126 EXPECT_EQ(1u, client_->num_requests());
3127 EXPECT_EQ(2u, client_->num_responses());
3128
3129 // Do same thing for the rest 10 resources.
3130 for (size_t i = 1; i < kNumResources; ++i) {
3131 client_->SendSynchronousRequest(push_urls[i]);
3132 }
3133
3134 // Because of server push, client gets all pushed resources without actually
3135 // sending requests for them.
3136 EXPECT_EQ(1u, client_->num_requests());
3137 // Including response to original request, 12 responses in total were
3138 // received.
3139 EXPECT_EQ(12u, client_->num_responses());
3140}
3141
3142// TODO(fayang): this test seems to cause net_unittests timeouts :|
3143TEST_P(EndToEndTest, DISABLED_TestHugePostWithPacketLoss) {
3144 // This test tests a huge post with introduced packet loss from client to
3145 // server and body size greater than 4GB, making sure QUIC code does not break
3146 // for 32-bit builds.
3147 ServerStreamThatDropsBodyFactory stream_factory;
3148 SetSpdyStreamFactory(&stream_factory);
3149 ASSERT_TRUE(Initialize());
3150 // Set client's epoll server's time out to 0 to make this test be finished
3151 // within a short time.
3152 client_->epoll_server()->set_timeout_in_us(0);
3153
3154 EXPECT_TRUE(client_->client()->WaitForCryptoHandshakeConfirmed());
3155 SetPacketLossPercentage(1);
3156 // To avoid storing the whole request body in memory, use a loop to repeatedly
3157 // send body size of kSizeBytes until the whole request body size is reached.
3158 const int kSizeBytes = 128 * 1024;
3159 // Request body size is 4G plus one more kSizeBytes.
3160 int64_t request_body_size_bytes = pow(2, 32) + kSizeBytes;
3161 ASSERT_LT(INT64_C(4294967296), request_body_size_bytes);
vasilvvc48c8712019-03-11 13:38:16 -07003162 std::string body(kSizeBytes, 'a');
QUICHE teama6ef0a62019-03-07 20:34:33 -05003163
3164 SpdyHeaderBlock headers;
3165 headers[":method"] = "POST";
3166 headers[":path"] = "/foo";
3167 headers[":scheme"] = "https";
3168 headers[":authority"] = server_hostname_;
3169 headers["content-length"] =
3170 QuicTextUtils::Uint64ToString(request_body_size_bytes);
3171
3172 client_->SendMessage(headers, "", /*fin=*/false);
3173
3174 for (int i = 0; i < request_body_size_bytes / kSizeBytes; ++i) {
3175 bool fin = (i == request_body_size_bytes - 1);
vasilvvc48c8712019-03-11 13:38:16 -07003176 client_->SendData(std::string(body.data(), kSizeBytes), fin);
QUICHE teama6ef0a62019-03-07 20:34:33 -05003177 client_->client()->WaitForEvents();
3178 }
3179 VerifyCleanConnection(true);
3180}
3181
3182// TODO(fayang): this test seems to cause net_unittests timeouts :|
3183TEST_P(EndToEndTest, DISABLED_TestHugeResponseWithPacketLoss) {
3184 // This test tests a huge response with introduced loss from server to client
3185 // and body size greater than 4GB, making sure QUIC code does not break for
3186 // 32-bit builds.
3187 const int kSizeBytes = 128 * 1024;
3188 int64_t response_body_size_bytes = pow(2, 32) + kSizeBytes;
3189 ASSERT_LT(4294967296, response_body_size_bytes);
3190 ServerStreamThatSendsHugeResponseFactory stream_factory(
3191 response_body_size_bytes);
3192 SetSpdyStreamFactory(&stream_factory);
3193
3194 StartServer();
3195
3196 // Use a quic client that drops received body.
3197 QuicTestClient* client =
3198 new QuicTestClient(server_address_, server_hostname_, client_config_,
3199 client_supported_versions_);
3200 client->client()->set_drop_response_body(true);
3201 client->UseWriter(client_writer_);
3202 client->Connect();
3203 client_.reset(client);
3204 static QuicEpollEvent event(EPOLLOUT);
3205 client_writer_->Initialize(
3206 QuicConnectionPeer::GetHelper(
3207 client_->client()->client_session()->connection()),
3208 QuicConnectionPeer::GetAlarmFactory(
3209 client_->client()->client_session()->connection()),
3210 QuicMakeUnique<ClientDelegate>(client_->client()));
3211 initialized_ = true;
3212 ASSERT_TRUE(client_->client()->connected());
3213
3214 EXPECT_TRUE(client_->client()->WaitForCryptoHandshakeConfirmed());
3215 SetPacketLossPercentage(1);
3216 client_->SendRequest("/huge_response");
3217 client_->WaitForResponse();
3218 // TODO(fayang): Fix this test to work with stateless rejects.
3219 if (!BothSidesSupportStatelessRejects()) {
3220 VerifyCleanConnection(true);
3221 }
3222}
3223
3224// Regression test for b/111515567
3225TEST_P(EndToEndTest, AgreeOnStopWaiting) {
3226 ASSERT_TRUE(Initialize());
3227 EXPECT_TRUE(client_->client()->WaitForCryptoHandshakeConfirmed());
3228
3229 QuicConnection* client_connection =
3230 client_->client()->client_session()->connection();
3231 server_thread_->Pause();
3232 QuicConnection* server_connection = GetServerConnection();
3233 // Verify client and server connections agree on the value of
3234 // no_stop_waiting_frames.
3235 EXPECT_EQ(QuicConnectionPeer::GetNoStopWaitingFrames(client_connection),
3236 QuicConnectionPeer::GetNoStopWaitingFrames(server_connection));
3237 server_thread_->Resume();
3238}
3239
3240// Regression test for b/111515567
3241TEST_P(EndToEndTest, AgreeOnStopWaitingWithNoStopWaitingOption) {
3242 QuicTagVector options;
3243 options.push_back(kNSTP);
3244 client_config_.SetConnectionOptionsToSend(options);
3245 ASSERT_TRUE(Initialize());
3246 EXPECT_TRUE(client_->client()->WaitForCryptoHandshakeConfirmed());
3247
3248 QuicConnection* client_connection =
3249 client_->client()->client_session()->connection();
3250 server_thread_->Pause();
3251 QuicConnection* server_connection = GetServerConnection();
3252 // Verify client and server connections agree on the value of
3253 // no_stop_waiting_frames.
3254 EXPECT_EQ(QuicConnectionPeer::GetNoStopWaitingFrames(client_connection),
3255 QuicConnectionPeer::GetNoStopWaitingFrames(server_connection));
3256 server_thread_->Resume();
3257}
3258
3259TEST_P(EndToEndTest, ReleaseHeadersStreamBufferWhenIdle) {
3260 // Tests that when client side has no active request and no waiting
3261 // PUSH_PROMISE, its headers stream's sequencer buffer should be released.
3262 ASSERT_TRUE(Initialize());
3263 client_->SendSynchronousRequest("/foo");
3264 QuicHeadersStream* headers_stream = QuicSpdySessionPeer::GetHeadersStream(
3265 client_->client()->client_session());
3266 QuicStreamSequencer* sequencer = QuicStreamPeer::sequencer(headers_stream);
3267 EXPECT_FALSE(QuicStreamSequencerPeer::IsUnderlyingBufferAllocated(sequencer));
3268}
3269
3270TEST_P(EndToEndTest, WayTooLongRequestHeaders) {
3271 ASSERT_TRUE(Initialize());
3272 SpdyHeaderBlock headers;
3273 headers[":method"] = "GET";
3274 headers[":path"] = "/foo";
3275 headers[":scheme"] = "https";
3276 headers[":authority"] = server_hostname_;
vasilvvc48c8712019-03-11 13:38:16 -07003277 headers["key"] = std::string(64 * 1024, 'a');
QUICHE teama6ef0a62019-03-07 20:34:33 -05003278
3279 client_->SendMessage(headers, "");
3280 client_->WaitForResponse();
3281 EXPECT_EQ(QUIC_HEADERS_STREAM_DATA_DECOMPRESS_FAILURE,
3282 client_->connection_error());
3283}
3284
3285class WindowUpdateObserver : public QuicConnectionDebugVisitor {
3286 public:
3287 WindowUpdateObserver() : num_window_update_frames_(0), num_ping_frames_(0) {}
3288
3289 size_t num_window_update_frames() const { return num_window_update_frames_; }
3290
3291 size_t num_ping_frames() const { return num_ping_frames_; }
3292
3293 void OnWindowUpdateFrame(const QuicWindowUpdateFrame& frame,
3294 const QuicTime& receive_time) override {
3295 ++num_window_update_frames_;
3296 }
3297
3298 void OnPingFrame(const QuicPingFrame& frame) override { ++num_ping_frames_; }
3299
3300 private:
3301 size_t num_window_update_frames_;
3302 size_t num_ping_frames_;
3303};
3304
3305TEST_P(EndToEndTest, WindowUpdateInAck) {
3306 ASSERT_TRUE(Initialize());
3307 EXPECT_TRUE(client_->client()->WaitForCryptoHandshakeConfirmed());
3308 WindowUpdateObserver observer;
3309 QuicConnection* client_connection =
3310 client_->client()->client_session()->connection();
3311 client_connection->set_debug_visitor(&observer);
3312 // 100KB body.
vasilvvc48c8712019-03-11 13:38:16 -07003313 std::string body(100 * 1024, 'a');
QUICHE teama6ef0a62019-03-07 20:34:33 -05003314 SpdyHeaderBlock headers;
3315 headers[":method"] = "POST";
3316 headers[":path"] = "/foo";
3317 headers[":scheme"] = "https";
3318 headers[":authority"] = server_hostname_;
3319
3320 EXPECT_EQ(kFooResponseBody,
3321 client_->SendCustomSynchronousRequest(headers, body));
3322 client_->Disconnect();
3323 EXPECT_LT(0u, observer.num_window_update_frames());
3324 EXPECT_EQ(0u, observer.num_ping_frames());
3325}
3326
3327TEST_P(EndToEndTest, SendStatelessResetTokenInShlo) {
3328 ASSERT_TRUE(Initialize());
3329 EXPECT_TRUE(client_->client()->WaitForCryptoHandshakeConfirmed());
3330 QuicConfig* config = client_->client()->session()->config();
3331 EXPECT_TRUE(config->HasReceivedStatelessResetToken());
3332 EXPECT_EQ(QuicUtils::GenerateStatelessResetToken(
3333 client_->client()->session()->connection()->connection_id()),
3334 config->ReceivedStatelessResetToken());
3335 client_->Disconnect();
3336}
3337
3338// Regression test for b/116200989.
3339TEST_P(EndToEndTest,
3340 SendStatelessResetIfServerConnectionClosedLocallyDuringHandshake) {
3341 connect_to_server_on_initialize_ = false;
3342 ASSERT_TRUE(Initialize());
3343
3344 server_thread_->Pause();
3345 QuicDispatcher* dispatcher =
3346 QuicServerPeer::GetDispatcher(server_thread_->server());
3347 ASSERT_EQ(0u, dispatcher->session_map().size());
3348 // Note: this writer will only used by the server connection, not the time
3349 // wait list.
3350 QuicDispatcherPeer::UseWriter(
3351 dispatcher,
3352 // This cause the first server-sent packet, a.k.a REJ, to fail.
3353 new BadPacketWriter(/*packet_causing_write_error=*/0, EPERM));
3354 server_thread_->Resume();
3355
3356 client_.reset(CreateQuicClient(client_writer_));
3357 EXPECT_EQ("", client_->SendSynchronousRequest("/foo"));
3358
3359 if (client_->client()->client_session()->connection()->transport_version() >
fayang1de67892019-04-19 05:59:45 -07003360 QUIC_VERSION_43 ||
3361 GetQuicReloadableFlag(quic_terminate_gquic_connection_as_ietf)) {
QUICHE teama6ef0a62019-03-07 20:34:33 -05003362 EXPECT_EQ(QUIC_HANDSHAKE_FAILED, client_->connection_error());
3363 } else {
3364 EXPECT_EQ(QUIC_PUBLIC_RESET, client_->connection_error());
3365 }
3366}
3367
3368// Regression test for b/116200989.
3369TEST_P(EndToEndTest,
3370 SendStatelessResetIfServerConnectionClosedLocallyAfterHandshake) {
3371 // Prevent the connection from expiring in the time wait list.
3372 FLAGS_quic_time_wait_list_seconds = 10000;
3373 connect_to_server_on_initialize_ = false;
3374 ASSERT_TRUE(Initialize());
3375
3376 // big_response_body is 64K, which is about 48 full-sized packets.
3377 const size_t kBigResponseBodySize = 65536;
3378 QuicData big_response_body(new char[kBigResponseBodySize](),
3379 kBigResponseBodySize, /*owns_buffer=*/true);
3380 AddToCache("/big_response", 200, big_response_body.AsStringPiece());
3381
3382 server_thread_->Pause();
3383 QuicDispatcher* dispatcher =
3384 QuicServerPeer::GetDispatcher(server_thread_->server());
3385 ASSERT_EQ(0u, dispatcher->session_map().size());
3386 QuicDispatcherPeer::UseWriter(
3387 dispatcher,
3388 // This will cause an server write error with EPERM, while sending the
3389 // response for /big_response.
3390 new BadPacketWriter(/*packet_causing_write_error=*/20, EPERM));
3391 server_thread_->Resume();
3392
3393 client_.reset(CreateQuicClient(client_writer_));
3394
3395 // First, a /foo request with small response should succeed.
3396 EXPECT_EQ(kFooResponseBody, client_->SendSynchronousRequest("/foo"));
3397 EXPECT_EQ("200", client_->response_headers()->find(":status")->second);
3398
3399 // Second, a /big_response request with big response should fail.
3400 EXPECT_LT(client_->SendSynchronousRequest("/big_response").length(),
3401 kBigResponseBodySize);
3402 EXPECT_EQ(QUIC_PUBLIC_RESET, client_->connection_error());
3403}
3404
3405// Regression test of b/70782529.
3406TEST_P(EndToEndTest, DoNotCrashOnPacketWriteError) {
3407 ASSERT_TRUE(Initialize());
3408 BadPacketWriter* bad_writer =
3409 new BadPacketWriter(/*packet_causing_write_error=*/5,
3410 /*error_code=*/90);
3411 std::unique_ptr<QuicTestClient> client(CreateQuicClient(bad_writer));
3412
3413 // 1 MB body.
vasilvvc48c8712019-03-11 13:38:16 -07003414 std::string body(1024 * 1024, 'a');
QUICHE teama6ef0a62019-03-07 20:34:33 -05003415 SpdyHeaderBlock headers;
3416 headers[":method"] = "POST";
3417 headers[":path"] = "/foo";
3418 headers[":scheme"] = "https";
3419 headers[":authority"] = server_hostname_;
3420
3421 client->SendCustomSynchronousRequest(headers, body);
3422}
3423
3424// Regression test for b/71711996. This test sends a connectivity probing packet
3425// as its last sent packet, and makes sure the server's ACK of that packet does
3426// not cause the client to fail.
3427TEST_P(EndToEndTest, LastPacketSentIsConnectivityProbing) {
3428 ASSERT_TRUE(Initialize());
3429
3430 EXPECT_EQ(kFooResponseBody, client_->SendSynchronousRequest("/foo"));
3431 EXPECT_EQ("200", client_->response_headers()->find(":status")->second);
3432
3433 // Wait for the client's ACK (of the response) to be received by the server.
3434 client_->WaitForDelayedAcks();
3435
3436 // We are sending a connectivity probing packet from an unchanged client
3437 // address, so the server will not respond to us with a connectivity probing
3438 // packet, however the server should send an ack-only packet to us.
3439 client_->SendConnectivityProbing();
3440
3441 // Wait for the server's last ACK to be received by the client.
3442 client_->WaitForDelayedAcks();
3443}
3444
3445TEST_P(EndToEndTest, PreSharedKey) {
3446 client_config_.set_max_time_before_crypto_handshake(
3447 QuicTime::Delta::FromSeconds(1));
3448 client_config_.set_max_idle_time_before_crypto_handshake(
3449 QuicTime::Delta::FromSeconds(1));
3450 pre_shared_key_client_ = "foobar";
3451 pre_shared_key_server_ = "foobar";
3452 ASSERT_TRUE(Initialize());
3453
3454 ASSERT_EQ(kFooResponseBody, client_->SendSynchronousRequest("/foo"));
3455 EXPECT_EQ("200", client_->response_headers()->find(":status")->second);
3456}
3457
3458// TODO: reenable once we have a way to make this run faster.
3459TEST_P(EndToEndTest, QUIC_TEST_DISABLED_IN_CHROME(PreSharedKeyMismatch)) {
3460 client_config_.set_max_time_before_crypto_handshake(
3461 QuicTime::Delta::FromSeconds(1));
3462 client_config_.set_max_idle_time_before_crypto_handshake(
3463 QuicTime::Delta::FromSeconds(1));
3464 pre_shared_key_client_ = "foo";
3465 pre_shared_key_server_ = "bar";
3466 // One of two things happens when Initialize() returns:
3467 // 1. Crypto handshake has completed, and it is unsuccessful. Initialize()
3468 // returns false.
3469 // 2. Crypto handshake has not completed, Initialize() returns true. The call
3470 // to WaitForCryptoHandshakeConfirmed() will wait for the handshake and
3471 // return whether it is successful.
3472 ASSERT_FALSE(Initialize() &&
3473 client_->client()->WaitForCryptoHandshakeConfirmed());
3474 EXPECT_EQ(QUIC_HANDSHAKE_TIMEOUT, client_->connection_error());
3475}
3476
3477// TODO: reenable once we have a way to make this run faster.
3478TEST_P(EndToEndTest, QUIC_TEST_DISABLED_IN_CHROME(PreSharedKeyNoClient)) {
3479 client_config_.set_max_time_before_crypto_handshake(
3480 QuicTime::Delta::FromSeconds(1));
3481 client_config_.set_max_idle_time_before_crypto_handshake(
3482 QuicTime::Delta::FromSeconds(1));
3483 pre_shared_key_server_ = "foobar";
3484 ASSERT_FALSE(Initialize() &&
3485 client_->client()->WaitForCryptoHandshakeConfirmed());
3486 EXPECT_EQ(QUIC_HANDSHAKE_TIMEOUT, client_->connection_error());
3487}
3488
3489// TODO: reenable once we have a way to make this run faster.
3490TEST_P(EndToEndTest, QUIC_TEST_DISABLED_IN_CHROME(PreSharedKeyNoServer)) {
3491 client_config_.set_max_time_before_crypto_handshake(
3492 QuicTime::Delta::FromSeconds(1));
3493 client_config_.set_max_idle_time_before_crypto_handshake(
3494 QuicTime::Delta::FromSeconds(1));
3495 pre_shared_key_client_ = "foobar";
3496 ASSERT_FALSE(Initialize() &&
3497 client_->client()->WaitForCryptoHandshakeConfirmed());
3498 EXPECT_EQ(QUIC_HANDSHAKE_TIMEOUT, client_->connection_error());
3499}
3500
3501TEST_P(EndToEndTest, RequestAndStreamRstInOnePacket) {
3502 // Regression test for b/80234898.
3503 ASSERT_TRUE(Initialize());
3504
3505 // INCOMPLETE_RESPONSE will cause the server to not to send the trailer
3506 // (and the FIN) after the response body.
vasilvvc48c8712019-03-11 13:38:16 -07003507 std::string response_body(1305, 'a');
QUICHE teama6ef0a62019-03-07 20:34:33 -05003508 SpdyHeaderBlock response_headers;
3509 response_headers[":status"] = QuicTextUtils::Uint64ToString(200);
3510 response_headers["content-length"] =
3511 QuicTextUtils::Uint64ToString(response_body.length());
3512 memory_cache_backend_.AddSpecialResponse(
3513 server_hostname_, "/test_url", std::move(response_headers), response_body,
3514 QuicBackendResponse::INCOMPLETE_RESPONSE);
3515
3516 EXPECT_TRUE(client_->client()->WaitForCryptoHandshakeConfirmed());
3517 client_->WaitForDelayedAcks();
3518
3519 QuicSession* session = client_->client()->client_session();
3520 const QuicPacketCount packets_sent_before =
3521 session->connection()->GetStats().packets_sent;
3522
3523 client_->SendRequestAndRstTogether("/test_url");
3524
3525 // Expect exactly one packet is sent from the block above.
3526 ASSERT_EQ(packets_sent_before + 1,
3527 session->connection()->GetStats().packets_sent);
3528
3529 // Wait for the connection to become idle.
3530 client_->WaitForDelayedAcks();
3531
3532 // The real expectation is the test does not crash or timeout.
3533 EXPECT_EQ(QUIC_NO_ERROR, client_->connection_error());
3534}
3535
3536TEST_P(EndToEndTest, ResetStreamOnTtlExpires) {
3537 ASSERT_TRUE(Initialize());
3538 EXPECT_TRUE(client_->client()->WaitForCryptoHandshakeConfirmed());
3539 if (!client_->client()->client_session()->session_decides_what_to_write()) {
3540 return;
3541 }
3542 SetPacketLossPercentage(30);
3543
3544 QuicSpdyClientStream* stream = client_->GetOrCreateStream();
3545 // Set a TTL which expires immediately.
3546 stream->MaybeSetTtl(QuicTime::Delta::FromMicroseconds(1));
3547
3548 // 1 MB body.
vasilvvc48c8712019-03-11 13:38:16 -07003549 std::string body(1024 * 1024, 'a');
QUICHE teama6ef0a62019-03-07 20:34:33 -05003550 stream->WriteOrBufferBody(body, true);
3551 client_->WaitForResponse();
3552 EXPECT_EQ(QUIC_STREAM_TTL_EXPIRED, client_->stream_error());
3553}
3554
3555TEST_P(EndToEndTest, SendMessages) {
3556 ASSERT_TRUE(Initialize());
3557 EXPECT_TRUE(client_->client()->WaitForCryptoHandshakeConfirmed());
3558 QuicSession* client_session = client_->client()->client_session();
3559 QuicConnection* client_connection = client_session->connection();
3560 if (client_connection->transport_version() <= QUIC_VERSION_44) {
3561 return;
3562 }
3563
3564 SetPacketLossPercentage(30);
dschinazi66dea072019-04-09 11:41:06 -07003565 ASSERT_GT(kMaxOutgoingPacketSize,
3566 client_session->GetCurrentLargestMessagePayload());
ianswettb239f862019-04-05 09:15:06 -07003567 ASSERT_LT(0, client_session->GetCurrentLargestMessagePayload());
QUICHE teama6ef0a62019-03-07 20:34:33 -05003568
dschinazi66dea072019-04-09 11:41:06 -07003569 std::string message_string(kMaxOutgoingPacketSize, 'a');
QUICHE teama6ef0a62019-03-07 20:34:33 -05003570 QuicStringPiece message_buffer(message_string);
3571 QuicRandom* random =
3572 QuicConnectionPeer::GetHelper(client_connection)->GetRandomGenerator();
3573 QuicMemSliceStorage storage(nullptr, 0, nullptr, 0);
3574 {
3575 QuicConnection::ScopedPacketFlusher flusher(
3576 client_session->connection(), QuicConnection::SEND_ACK_IF_PENDING);
3577 // Verify the largest message gets successfully sent.
ianswettb239f862019-04-05 09:15:06 -07003578 EXPECT_EQ(
3579 MessageResult(MESSAGE_STATUS_SUCCESS, 1),
3580 client_session->SendMessage(MakeSpan(
3581 client_session->connection()
3582 ->helper()
3583 ->GetStreamSendBufferAllocator(),
3584 QuicStringPiece(message_buffer.data(),
3585 client_session->GetCurrentLargestMessagePayload()),
3586 &storage)));
QUICHE teama6ef0a62019-03-07 20:34:33 -05003587 // Send more messages with size (0, largest_payload] until connection is
3588 // write blocked.
3589 const int kTestMaxNumberOfMessages = 100;
3590 for (size_t i = 2; i <= kTestMaxNumberOfMessages; ++i) {
3591 size_t message_length =
ianswettb239f862019-04-05 09:15:06 -07003592 random->RandUint64() %
3593 client_session->GetCurrentLargestMessagePayload() +
3594 1;
QUICHE teama6ef0a62019-03-07 20:34:33 -05003595 MessageResult result = client_session->SendMessage(MakeSpan(
3596 client_session->connection()
3597 ->helper()
3598 ->GetStreamSendBufferAllocator(),
3599 QuicStringPiece(message_buffer.data(), message_length), &storage));
3600 if (result.status == MESSAGE_STATUS_BLOCKED) {
3601 // Connection is write blocked.
3602 break;
3603 }
3604 EXPECT_EQ(MessageResult(MESSAGE_STATUS_SUCCESS, i), result);
3605 }
3606 }
3607
3608 client_->WaitForDelayedAcks();
ianswettb239f862019-04-05 09:15:06 -07003609 EXPECT_EQ(MESSAGE_STATUS_TOO_LARGE,
3610 client_session
3611 ->SendMessage(MakeSpan(
3612 client_session->connection()
3613 ->helper()
3614 ->GetStreamSendBufferAllocator(),
3615 QuicStringPiece(
3616 message_buffer.data(),
3617 client_session->GetCurrentLargestMessagePayload() + 1),
3618 &storage))
3619 .status);
QUICHE teama6ef0a62019-03-07 20:34:33 -05003620 EXPECT_EQ(QUIC_NO_ERROR, client_->connection_error());
3621}
3622
3623class EndToEndPacketReorderingTest : public EndToEndTest {
3624 public:
3625 void CreateClientWithWriter() override {
3626 QUIC_LOG(ERROR) << "create client with reorder_writer_";
3627 reorder_writer_ = new PacketReorderingWriter();
3628 client_.reset(EndToEndTest::CreateQuicClient(reorder_writer_));
3629 }
3630
3631 void SetUp() override {
3632 // Don't initialize client writer in base class.
3633 server_writer_ = new PacketDroppingTestWriter();
3634 }
3635
3636 protected:
3637 PacketReorderingWriter* reorder_writer_;
3638};
3639
3640INSTANTIATE_TEST_SUITE_P(EndToEndPacketReorderingTests,
3641 EndToEndPacketReorderingTest,
3642 testing::ValuesIn(GetTestParams(false, false)));
3643
3644TEST_P(EndToEndPacketReorderingTest, ReorderedConnectivityProbing) {
3645 ASSERT_TRUE(Initialize());
3646
3647 // Finish one request to make sure handshake established.
3648 EXPECT_EQ(kFooResponseBody, client_->SendSynchronousRequest("/foo"));
3649
3650 // Wait for the connection to become idle, to make sure the packet gets
3651 // delayed is the connectivity probing packet.
3652 client_->WaitForDelayedAcks();
3653
3654 QuicSocketAddress old_addr =
3655 client_->client()->network_helper()->GetLatestClientAddress();
3656
3657 // Migrate socket to the new IP address.
3658 QuicIpAddress new_host = TestLoopback(2);
3659 EXPECT_NE(old_addr.host(), new_host);
3660 ASSERT_TRUE(client_->client()->MigrateSocket(new_host));
3661
3662 // Write a connectivity probing after the next /foo request.
3663 reorder_writer_->SetDelay(1);
3664 client_->SendConnectivityProbing();
3665
3666 ASSERT_TRUE(client_->MigrateSocketWithSpecifiedPort(old_addr.host(),
3667 old_addr.port()));
3668
3669 // The (delayed) connectivity probing will be sent after this request.
3670 EXPECT_EQ(kFooResponseBody, client_->SendSynchronousRequest("/foo"));
3671
3672 // Send yet another request after the connectivity probing, when this request
3673 // returns, the probing is guaranteed to have been received by the server, and
3674 // the server's response to probing is guaranteed to have been received by the
3675 // client.
3676 EXPECT_EQ(kFooResponseBody, client_->SendSynchronousRequest("/foo"));
3677
3678 server_thread_->Pause();
3679 QuicConnection* server_connection = GetServerConnection();
3680 EXPECT_EQ(1u,
3681 server_connection->GetStats().num_connectivity_probing_received);
3682 server_thread_->Resume();
3683
3684 QuicConnection* client_connection =
3685 client_->client()->client_session()->connection();
3686 EXPECT_EQ(1u,
3687 client_connection->GetStats().num_connectivity_probing_received);
3688}
3689
3690TEST_P(EndToEndPacketReorderingTest, Buffer0RttRequest) {
3691 ASSERT_TRUE(Initialize());
3692 // Finish one request to make sure handshake established.
3693 client_->SendSynchronousRequest("/foo");
3694 // Disconnect for next 0-rtt request.
3695 client_->Disconnect();
3696
3697 // Client get valid STK now. Do a 0-rtt request.
3698 // Buffer a CHLO till another packets sent out.
3699 reorder_writer_->SetDelay(1);
3700 // Only send out a CHLO.
3701 client_->client()->Initialize();
3702 client_->client()->StartConnect();
3703 EXPECT_TRUE(client_->client()->WaitForCryptoHandshakeConfirmed());
3704 ASSERT_TRUE(client_->client()->connected());
3705
3706 // Send a request before handshake finishes.
3707 SpdyHeaderBlock headers;
3708 headers[":method"] = "POST";
3709 headers[":path"] = "/bar";
3710 headers[":scheme"] = "https";
3711 headers[":authority"] = server_hostname_;
3712
3713 client_->SendMessage(headers, "");
3714 client_->WaitForResponse();
3715 EXPECT_EQ(kBarResponseBody, client_->response_body());
3716 QuicConnectionStats client_stats =
3717 client_->client()->client_session()->connection()->GetStats();
3718 EXPECT_EQ(0u, client_stats.packets_lost);
3719 if (ServerSendsVersionNegotiation()) {
3720 EXPECT_EQ(2, client_->client()->GetNumSentClientHellos());
3721 } else {
3722 EXPECT_EQ(1, client_->client()->GetNumSentClientHellos());
3723 }
3724}
3725
3726// Test that STOP_SENDING makes it to the other side. Set up a client & server,
3727// create a stream (do not close it), and then send a STOP_SENDING from one
3728// side. The other side should get a call to QuicStream::OnStopSending.
3729// (aside, test cribbed from RequestAndStreamRstInOnePacket)
3730TEST_P(EndToEndTest, SimpleStopSendingTest) {
3731 const uint16_t kStopSendingTestCode = 123;
3732 ASSERT_TRUE(Initialize());
3733 if (negotiated_version_.transport_version != QUIC_VERSION_99) {
3734 return;
3735 }
3736 QuicSession* client_session = client_->client()->client_session();
3737 ASSERT_NE(nullptr, client_session);
3738 QuicConnection* client_connection = client_session->connection();
3739 ASSERT_NE(nullptr, client_connection);
3740
3741 // STOP_SENDING will cause the server to not to send the trailer
3742 // (and the FIN) after the response body. Instead, it sends a STOP_SENDING
3743 // frame for the stream.
vasilvvc48c8712019-03-11 13:38:16 -07003744 std::string response_body(1305, 'a');
QUICHE teama6ef0a62019-03-07 20:34:33 -05003745 SpdyHeaderBlock response_headers;
3746 response_headers[":status"] = QuicTextUtils::Uint64ToString(200);
3747 response_headers["content-length"] =
3748 QuicTextUtils::Uint64ToString(response_body.length());
3749 memory_cache_backend_.AddStopSendingResponse(
3750 server_hostname_, "/test_url", std::move(response_headers), response_body,
3751 kStopSendingTestCode);
3752
3753 EXPECT_TRUE(client_->client()->WaitForCryptoHandshakeConfirmed());
3754 client_->WaitForDelayedAcks();
3755
3756 QuicSession* session = client_->client()->client_session();
3757 const QuicPacketCount packets_sent_before =
3758 session->connection()->GetStats().packets_sent;
3759
3760 QuicStreamId stream_id = session->next_outgoing_bidirectional_stream_id();
3761 client_->SendRequest("/test_url");
3762
3763 // Expect exactly one packet is sent from the block above.
3764 ASSERT_EQ(packets_sent_before + 1,
3765 session->connection()->GetStats().packets_sent);
3766
3767 // Wait for the connection to become idle.
3768 client_->WaitForDelayedAcks();
3769
3770 // The real expectation is the test does not crash or timeout.
3771 EXPECT_EQ(QUIC_NO_ERROR, client_->connection_error());
3772 // And that the stop-sending code is received.
3773 QuicSimpleClientStream* client_stream =
3774 static_cast<QuicSimpleClientStream*>(client_->latest_created_stream());
3775 ASSERT_NE(nullptr, client_stream);
3776 // Make sure we have the correct stream
3777 EXPECT_EQ(stream_id, client_stream->id());
3778 EXPECT_EQ(kStopSendingTestCode, client_stream->last_stop_sending_code());
3779}
3780
3781TEST_P(EndToEndTest, SimpleStopSendingRstStreamTest) {
3782 ASSERT_TRUE(Initialize());
3783
3784 // Send a request without a fin, to keep the stream open
3785 SpdyHeaderBlock headers;
3786 headers[":method"] = "POST";
3787 headers[":path"] = "/foo";
3788 headers[":scheme"] = "https";
3789 headers[":authority"] = server_hostname_;
3790 client_->SendMessage(headers, "", /*fin=*/false);
3791 // Stream should be open
3792 ASSERT_NE(nullptr, client_->latest_created_stream());
bncc7d9e0c2019-04-16 10:22:15 -07003793 EXPECT_FALSE(client_->latest_created_stream()->write_side_closed());
QUICHE teama6ef0a62019-03-07 20:34:33 -05003794 EXPECT_FALSE(
3795 QuicStreamPeer::read_side_closed(client_->latest_created_stream()));
3796
3797 // Send a RST_STREAM+STOP_SENDING on the stream
3798 // Code is not important.
3799 client_->latest_created_stream()->Reset(QUIC_BAD_APPLICATION_PAYLOAD);
3800 client_->WaitForResponse();
3801
3802 // Stream should be gone.
3803 ASSERT_EQ(nullptr, client_->latest_created_stream());
3804}
3805
3806class BadShloPacketWriter : public QuicPacketWriterWrapper {
3807 public:
3808 BadShloPacketWriter() : error_returned_(false) {}
3809 ~BadShloPacketWriter() override {}
3810
3811 WriteResult WritePacket(const char* buffer,
3812 size_t buf_len,
3813 const QuicIpAddress& self_address,
3814 const QuicSocketAddress& peer_address,
3815 quic::PerPacketOptions* options) override {
3816 const WriteResult result = QuicPacketWriterWrapper::WritePacket(
3817 buffer, buf_len, self_address, peer_address, options);
3818 const uint8_t type_byte = buffer[0];
3819 if (!error_returned_ && (type_byte & FLAGS_LONG_HEADER) &&
3820 (((type_byte & 0x30) >> 4) == 1 || (type_byte & 0x7F) == 0x7C)) {
3821 QUIC_DVLOG(1) << "Return write error for ZERO_RTT_PACKET";
3822 error_returned_ = true;
3823 return WriteResult(WRITE_STATUS_ERROR, QUIC_EMSGSIZE);
3824 }
3825 return result;
3826 }
3827
3828 private:
3829 bool error_returned_;
3830};
3831
3832TEST_P(EndToEndTest, ZeroRttProtectedConnectionClose) {
3833 // This test ensures ZERO_RTT_PROTECTED connection close could close a client
3834 // which has switched to forward secure.
3835 connect_to_server_on_initialize_ =
3836 negotiated_version_.transport_version <= QUIC_VERSION_43;
3837 ASSERT_TRUE(Initialize());
3838 if (negotiated_version_.transport_version <= QUIC_VERSION_43) {
3839 // Only runs for IETF QUIC header.
3840 return;
3841 }
3842 server_thread_->Pause();
3843 QuicDispatcher* dispatcher =
3844 QuicServerPeer::GetDispatcher(server_thread_->server());
3845 ASSERT_EQ(0u, dispatcher->session_map().size());
3846 // Note: this writer will only used by the server connection, not the time
3847 // wait list.
3848 QuicDispatcherPeer::UseWriter(
3849 dispatcher,
3850 // This causes the first server sent ZERO_RTT_PROTECTED packet (i.e.,
3851 // SHLO) to be sent, but WRITE_ERROR is returned. Such that a
3852 // ZERO_RTT_PROTECTED connection close would be sent to a client with
3853 // encryption level FORWARD_SECURE.
3854 new BadShloPacketWriter());
3855 server_thread_->Resume();
3856
3857 client_.reset(CreateQuicClient(client_writer_));
3858 EXPECT_EQ("", client_->SendSynchronousRequest("/foo"));
3859 // Verify ZERO_RTT_PROTECTED connection close is successfully processed by
3860 // client which switches to FORWARD_SECURE.
3861 EXPECT_EQ(QUIC_PACKET_WRITE_ERROR, client_->connection_error());
3862}
3863
3864class BadShloPacketWriter2 : public QuicPacketWriterWrapper {
3865 public:
3866 BadShloPacketWriter2() : error_returned_(false) {}
3867 ~BadShloPacketWriter2() override {}
3868
3869 WriteResult WritePacket(const char* buffer,
3870 size_t buf_len,
3871 const QuicIpAddress& self_address,
3872 const QuicSocketAddress& peer_address,
3873 quic::PerPacketOptions* options) override {
3874 const uint8_t type_byte = buffer[0];
3875 if ((type_byte & FLAGS_LONG_HEADER) &&
3876 (((type_byte & 0x30) >> 4) == 1 || (type_byte & 0x7F) == 0x7C)) {
3877 QUIC_DVLOG(1) << "Dropping ZERO_RTT_PACKET packet";
3878 return WriteResult(WRITE_STATUS_OK, buf_len);
3879 }
3880 if (!error_returned_ && !(type_byte & FLAGS_LONG_HEADER)) {
3881 QUIC_DVLOG(1) << "Return write error for short header packet";
3882 error_returned_ = true;
3883 return WriteResult(WRITE_STATUS_ERROR, QUIC_EMSGSIZE);
3884 }
3885 return QuicPacketWriterWrapper::WritePacket(buffer, buf_len, self_address,
3886 peer_address, options);
3887 }
3888
3889 private:
3890 bool error_returned_;
3891};
3892
3893TEST_P(EndToEndTest, ForwardSecureConnectionClose) {
3894 // This test ensures ZERO_RTT_PROTECTED connection close is sent to a client
3895 // which has ZERO_RTT_PROTECTED encryption level.
3896 SetQuicReloadableFlag(quic_fix_termination_packets, true);
3897 connect_to_server_on_initialize_ =
3898 negotiated_version_.transport_version <= QUIC_VERSION_43;
3899 ASSERT_TRUE(Initialize());
3900 if (negotiated_version_.transport_version <= QUIC_VERSION_43) {
3901 // Only runs for IETF QUIC header.
3902 return;
3903 }
3904 server_thread_->Pause();
3905 QuicDispatcher* dispatcher =
3906 QuicServerPeer::GetDispatcher(server_thread_->server());
3907 ASSERT_EQ(0u, dispatcher->session_map().size());
3908 // Note: this writer will only used by the server connection, not the time
3909 // wait list.
3910 QuicDispatcherPeer::UseWriter(
3911 dispatcher,
3912 // This causes the all server sent ZERO_RTT_PROTECTED packets to be
3913 // dropped, and first short header packet causes write error.
3914 new BadShloPacketWriter2());
3915 server_thread_->Resume();
3916 client_.reset(CreateQuicClient(client_writer_));
3917 EXPECT_EQ("", client_->SendSynchronousRequest("/foo"));
3918 // Verify ZERO_RTT_PROTECTED connection close is successfully processed by
3919 // client.
3920 EXPECT_EQ(QUIC_PACKET_WRITE_ERROR, client_->connection_error());
3921}
3922
fkastenholz3c4eabf2019-04-22 07:49:59 -07003923// Test that the stream id manager closes the connection if a stream
3924// in excess of the allowed maximum.
3925TEST_P(EndToEndTest, TooBigStreamIdClosesConnection) {
3926 // Has to be before version test, see EndToEndTest::TearDown()
3927 ASSERT_TRUE(Initialize());
3928 if (negotiated_version_.transport_version != QUIC_VERSION_99) {
3929 // Only runs for IETF QUIC.
3930 return;
3931 }
3932 EXPECT_TRUE(client_->client()->WaitForCryptoHandshakeConfirmed());
3933
3934 std::string body(kMaxOutgoingPacketSize, 'a');
3935 SpdyHeaderBlock headers;
3936 headers[":method"] = "POST";
3937 headers[":path"] = "/foo";
3938 headers[":scheme"] = "https";
3939 headers[":authority"] = server_hostname_;
3940
3941 // Force the client to write with a stream ID that exceeds the limit.
3942 QuicSpdySession* session = client_->client()->client_session();
3943 QuicStreamIdManager* stream_id_manager =
3944 QuicSessionPeer::v99_bidirectional_stream_id_manager(session);
3945 QuicStreamCount max_number_of_streams =
3946 stream_id_manager->outgoing_max_streams();
3947 QuicSessionPeer::SetNextOutgoingBidirectionalStreamId(
3948 session, GetNthClientInitiatedBidirectionalId(max_number_of_streams + 1));
3949 client_->SendCustomSynchronousRequest(headers, body);
3950 EXPECT_EQ(QUIC_STREAM_CONNECTION_ERROR, client_->stream_error());
3951 EXPECT_EQ(QUIC_INVALID_STREAM_ID, client_->connection_error());
3952}
3953
QUICHE teama6ef0a62019-03-07 20:34:33 -05003954} // namespace
3955} // namespace test
3956} // namespace quic