1 // 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.
10 #include "base/basictypes.h"
11 #include "base/memory/scoped_ptr.h"
12 #include "base/memory/singleton.h"
13 #include "base/strings/string_number_conversions.h"
14 #include "base/synchronization/waitable_event.h"
15 #include "base/threading/platform_thread.h"
16 #include "base/time/time.h"
17 #include "net/base/ip_endpoint.h"
18 #include "net/quic/congestion_control/tcp_cubic_sender.h"
19 #include "net/quic/crypto/aes_128_gcm_12_encrypter.h"
20 #include "net/quic/crypto/null_encrypter.h"
21 #include "net/quic/quic_flags.h"
22 #include "net/quic/quic_framer.h"
23 #include "net/quic/quic_packet_creator.h"
24 #include "net/quic/quic_protocol.h"
25 #include "net/quic/quic_server_id.h"
26 #include "net/quic/quic_utils.h"
27 #include "net/quic/test_tools/quic_connection_peer.h"
28 #include "net/quic/test_tools/quic_flow_controller_peer.h"
29 #include "net/quic/test_tools/quic_sent_packet_manager_peer.h"
30 #include "net/quic/test_tools/quic_session_peer.h"
31 #include "net/quic/test_tools/quic_spdy_session_peer.h"
32 #include "net/quic/test_tools/quic_test_utils.h"
33 #include "net/quic/test_tools/reliable_quic_stream_peer.h"
34 #include "net/test/gtest_util.h"
35 #include "net/tools/epoll_server/epoll_server.h"
36 #include "net/tools/quic/quic_epoll_connection_helper.h"
37 #include "net/tools/quic/quic_in_memory_cache.h"
38 #include "net/tools/quic/quic_packet_writer_wrapper.h"
39 #include "net/tools/quic/quic_server.h"
40 #include "net/tools/quic/quic_socket_utils.h"
41 #include "net/tools/quic/quic_spdy_client_stream.h"
42 #include "net/tools/quic/test_tools/http_message.h"
43 #include "net/tools/quic/test_tools/packet_dropping_test_writer.h"
44 #include "net/tools/quic/test_tools/quic_client_peer.h"
45 #include "net/tools/quic/test_tools/quic_dispatcher_peer.h"
46 #include "net/tools/quic/test_tools/quic_in_memory_cache_peer.h"
47 #include "net/tools/quic/test_tools/quic_server_peer.h"
48 #include "net/tools/quic/test_tools/quic_test_client.h"
49 #include "net/tools/quic/test_tools/server_thread.h"
50 #include "testing/gtest/include/gtest/gtest.h"
52 using base::StringPiece
;
53 using base::WaitableEvent
;
54 using net::EpollServer
;
55 using net::test::ConstructEncryptedPacket
;
56 using net::test::GenerateBody
;
57 using net::test::MockQuicConnectionDebugVisitor
;
58 using net::test::QuicConnectionPeer
;
59 using net::test::QuicFlowControllerPeer
;
60 using net::test::QuicSentPacketManagerPeer
;
61 using net::test::QuicSessionPeer
;
62 using net::test::QuicSpdySessionPeer
;
63 using net::test::ReliableQuicStreamPeer
;
64 using net::test::ValueRestore
;
65 using net::test::kClientDataStreamId1
;
66 using net::tools::test::PacketDroppingTestWriter
;
67 using net::tools::test::QuicDispatcherPeer
;
68 using net::tools::test::QuicServerPeer
;
78 const char kFooResponseBody
[] = "Artichoke hearts make me happy.";
79 const char kBarResponseBody
[] = "Palm hearts are pretty delicious, also.";
81 // Run all tests with the cross products of all versions.
83 TestParams(const QuicVersionVector
& client_supported_versions
,
84 const QuicVersionVector
& server_supported_versions
,
85 QuicVersion negotiated_version
,
87 QuicTag congestion_control_tag
)
88 : client_supported_versions(client_supported_versions
),
89 server_supported_versions(server_supported_versions
),
90 negotiated_version(negotiated_version
),
92 congestion_control_tag(congestion_control_tag
) {
95 friend ostream
& operator<<(ostream
& os
, const TestParams
& p
) {
96 os
<< "{ server_supported_versions: "
97 << QuicVersionVectorToString(p
.server_supported_versions
);
98 os
<< " client_supported_versions: "
99 << QuicVersionVectorToString(p
.client_supported_versions
);
100 os
<< " negotiated_version: " << QuicVersionToString(p
.negotiated_version
);
101 os
<< " use_fec: " << p
.use_fec
;
102 os
<< " congestion_control_tag: "
103 << QuicUtils::TagToString(p
.congestion_control_tag
) << " }";
107 QuicVersionVector client_supported_versions
;
108 QuicVersionVector server_supported_versions
;
109 QuicVersion negotiated_version
;
111 QuicTag congestion_control_tag
;
114 // Constructs various test permutations.
115 vector
<TestParams
> GetTestParams() {
116 // Divide the versions into buckets in which the intra-frame format
117 // is compatible. When clients encounter QUIC version negotiation
118 // they simply retransmit all packets using the new version's
119 // QUIC framing. However, they are unable to change the intra-frame
120 // layout (for example to change SPDY/4 headers to SPDY/3). So
121 // these tests need to ensure that clients are never attempting
122 // to do 0-RTT across incompatible versions. Chromium only supports
123 // a single version at a time anyway. :)
124 QuicVersionVector all_supported_versions
= QuicSupportedVersions();
125 QuicVersionVector client_version_buckets
[2];
126 for (const QuicVersion version
: all_supported_versions
) {
127 if (version
<= QUIC_VERSION_24
) {
128 // SPDY/4 compression but SPDY/3 headers
129 client_version_buckets
[0].push_back(version
);
132 client_version_buckets
[1].push_back(version
);
136 vector
<TestParams
> params
;
137 // TODO(rtenneti): Add kTBBR after BBR code is checked in.
138 // for (const QuicTag congestion_control_tag : {kRENO, kTBBR, kQBIC}) {
139 for (const QuicTag congestion_control_tag
: {kRENO
, kQBIC
}) {
140 for (const bool use_fec
: {false, true}) {
141 for (const QuicVersionVector
& client_versions
: client_version_buckets
) {
142 CHECK(!client_versions
.empty());
143 // Add an entry for server and client supporting all versions.
144 params
.push_back(TestParams(client_versions
, all_supported_versions
,
145 client_versions
.front(), use_fec
!= 0,
146 congestion_control_tag
));
148 // Test client supporting all versions and server supporting 1
149 // version. Simulate an old server and exercise version downgrade in
150 // the client. Protocol negotiation should occur. Skip the i = 0 case
151 // because it is essentially the same as the default case.
152 for (const QuicVersion version
: client_versions
) {
153 QuicVersionVector server_supported_versions
;
154 server_supported_versions
.push_back(version
);
155 params
.push_back(TestParams(client_versions
,
156 server_supported_versions
,
157 server_supported_versions
.front(),
158 use_fec
!= 0, congestion_control_tag
));
166 class ServerDelegate
: public PacketDroppingTestWriter::Delegate
{
168 ServerDelegate(TestWriterFactory
* writer_factory
,
169 QuicDispatcher
* dispatcher
)
170 : writer_factory_(writer_factory
),
171 dispatcher_(dispatcher
) {}
172 ~ServerDelegate() override
{}
173 void OnPacketSent(WriteResult result
) override
{
174 writer_factory_
->OnPacketSent(result
);
176 void OnCanWrite() override
{ dispatcher_
->OnCanWrite(); }
179 TestWriterFactory
* writer_factory_
;
180 QuicDispatcher
* dispatcher_
;
183 class ClientDelegate
: public PacketDroppingTestWriter::Delegate
{
185 explicit ClientDelegate(QuicClient
* client
) : client_(client
) {}
186 ~ClientDelegate() override
{}
187 void OnPacketSent(WriteResult result
) override
{}
188 void OnCanWrite() override
{
189 EpollEvent
event(EPOLLOUT
, false);
190 client_
->OnEvent(client_
->fd(), &event
);
197 class EndToEndTest
: public ::testing::TestWithParam
<TestParams
> {
200 : server_hostname_("example.com"),
201 server_started_(false),
202 strike_register_no_startup_period_(false) {
203 net::IPAddressNumber ip
;
204 CHECK(net::ParseIPLiteralToNumber("127.0.0.1", &ip
));
205 server_address_
= IPEndPoint(ip
, 0);
207 client_supported_versions_
= GetParam().client_supported_versions
;
208 server_supported_versions_
= GetParam().server_supported_versions
;
209 negotiated_version_
= GetParam().negotiated_version
;
210 FLAGS_enable_quic_fec
= GetParam().use_fec
;
212 VLOG(1) << "Using Configuration: " << GetParam();
214 // Use different flow control windows for client/server.
215 client_config_
.SetInitialStreamFlowControlWindowToSend(
216 2 * kInitialStreamFlowControlWindowForTest
);
217 client_config_
.SetInitialSessionFlowControlWindowToSend(
218 2 * kInitialSessionFlowControlWindowForTest
);
219 server_config_
.SetInitialStreamFlowControlWindowToSend(
220 3 * kInitialStreamFlowControlWindowForTest
);
221 server_config_
.SetInitialSessionFlowControlWindowToSend(
222 3 * kInitialSessionFlowControlWindowForTest
);
224 QuicInMemoryCachePeer::ResetForTests();
225 AddToCache("/foo", 200, "OK", kFooResponseBody
);
226 AddToCache("/bar", 200, "OK", kBarResponseBody
);
229 ~EndToEndTest() override
{
230 // TODO(rtenneti): port RecycleUnusedPort if needed.
231 // RecycleUnusedPort(server_address_.port());
232 QuicInMemoryCachePeer::ResetForTests();
235 QuicTestClient
* CreateQuicClient(QuicPacketWriterWrapper
* writer
) {
236 QuicTestClient
* client
= new QuicTestClient(
241 client_supported_versions_
);
242 client
->UseWriter(writer
);
247 void set_client_initial_stream_flow_control_receive_window(uint32 window
) {
248 CHECK(client_
.get() == nullptr);
249 DVLOG(1) << "Setting client initial stream flow control window: " << window
;
250 client_config_
.SetInitialStreamFlowControlWindowToSend(window
);
253 void set_client_initial_session_flow_control_receive_window(uint32 window
) {
254 CHECK(client_
.get() == nullptr);
255 DVLOG(1) << "Setting client initial session flow control window: "
257 client_config_
.SetInitialSessionFlowControlWindowToSend(window
);
260 void set_server_initial_stream_flow_control_receive_window(uint32 window
) {
261 CHECK(server_thread_
.get() == nullptr);
262 DVLOG(1) << "Setting server initial stream flow control window: "
264 server_config_
.SetInitialStreamFlowControlWindowToSend(window
);
267 void set_server_initial_session_flow_control_receive_window(uint32 window
) {
268 CHECK(server_thread_
.get() == nullptr);
269 DVLOG(1) << "Setting server initial session flow control window: "
271 server_config_
.SetInitialSessionFlowControlWindowToSend(window
);
274 const QuicSentPacketManager
*
275 GetSentPacketManagerFromFirstServerSession() const {
276 QuicDispatcher
* dispatcher
=
277 QuicServerPeer::GetDispatcher(server_thread_
->server());
278 QuicSession
* session
= dispatcher
->session_map().begin()->second
;
279 return &session
->connection()->sent_packet_manager();
284 server_config_
.SetConnectionOptionsToSend(copt
);
286 // TODO(nimia): Consider setting the congestion control algorithm for the
287 // client as well according to the test parameter.
288 copt
.push_back(GetParam().congestion_control_tag
);
290 if (GetParam().use_fec
) {
291 // Set FEC config in client's connection options and in client session.
292 copt
.push_back(kFHDR
);
295 client_config_
.SetConnectionOptionsToSend(copt
);
297 // Start the server first, because CreateQuicClient() attempts
298 // to connect to the server.
300 client_
.reset(CreateQuicClient(client_writer_
));
301 if (GetParam().use_fec
) {
302 // Set FecPolicy to always protect data on all streams.
303 client_
->SetFecPolicy(FEC_PROTECT_ALWAYS
);
305 static EpollEvent
event(EPOLLOUT
, false);
306 client_writer_
->Initialize(
307 reinterpret_cast<QuicEpollConnectionHelper
*>(
308 QuicConnectionPeer::GetHelper(
309 client_
->client()->session()->connection())),
310 new ClientDelegate(client_
->client()));
311 return client_
->client()->connected();
314 void SetUp() override
{
315 // The ownership of these gets transferred to the QuicPacketWriterWrapper
316 // and TestWriterFactory when Initialize() is executed.
317 client_writer_
= new PacketDroppingTestWriter();
318 server_writer_
= new PacketDroppingTestWriter();
321 void TearDown() override
{ StopServer(); }
324 server_thread_
.reset(
326 new QuicServer(server_config_
, server_supported_versions_
),
328 strike_register_no_startup_period_
));
329 server_thread_
->Initialize();
330 server_address_
= IPEndPoint(server_address_
.address(),
331 server_thread_
->GetPort());
332 QuicDispatcher
* dispatcher
=
333 QuicServerPeer::GetDispatcher(server_thread_
->server());
334 TestWriterFactory
* packet_writer_factory
= new TestWriterFactory();
335 QuicDispatcherPeer::SetPacketWriterFactory(dispatcher
,
336 packet_writer_factory
);
337 QuicDispatcherPeer::UseWriter(dispatcher
, server_writer_
);
338 server_writer_
->Initialize(
339 QuicDispatcherPeer::GetHelper(dispatcher
),
340 new ServerDelegate(packet_writer_factory
, dispatcher
));
341 server_thread_
->Start();
342 server_started_
= true;
346 if (!server_started_
)
348 if (server_thread_
.get()) {
349 server_thread_
->Quit();
350 server_thread_
->Join();
354 void AddToCache(StringPiece path
,
356 StringPiece response_detail
,
358 QuicInMemoryCache::GetInstance()->AddSimpleResponse(
359 "www.google.com", path
, response_code
, response_detail
, body
);
362 void SetPacketLossPercentage(int32 loss
) {
363 // TODO(rtenneti): enable when we can do random packet loss tests in
365 if (loss
!= 0 && loss
!= 100)
367 client_writer_
->set_fake_packet_loss_percentage(loss
);
368 server_writer_
->set_fake_packet_loss_percentage(loss
);
371 void SetPacketSendDelay(QuicTime::Delta delay
) {
372 // TODO(rtenneti): enable when we can do random packet send delay tests in
374 // client_writer_->set_fake_packet_delay(delay);
375 // server_writer_->set_fake_packet_delay(delay);
378 void SetReorderPercentage(int32 reorder
) {
379 // TODO(rtenneti): enable when we can do random packet reorder tests in
381 // client_writer_->set_fake_reorder_percentage(reorder);
382 // server_writer_->set_fake_reorder_percentage(reorder);
385 // Verifies that the client and server connections were both free of packets
386 // being discarded, based on connection stats.
387 // Calls server_thread_ Pause() and Resume(), which may only be called once
389 void VerifyCleanConnection(bool had_packet_loss
) {
390 QuicConnectionStats client_stats
=
391 client_
->client()->session()->connection()->GetStats();
392 // TODO(ianswett): Re-enable this check once b/19572432 is fixed.
393 // if (!had_packet_loss) {
394 // EXPECT_EQ(0u, client_stats.packets_lost);
396 EXPECT_EQ(0u, client_stats
.packets_discarded
);
397 EXPECT_EQ(0u, client_stats
.packets_dropped
);
398 EXPECT_EQ(client_stats
.packets_received
, client_stats
.packets_processed
);
400 server_thread_
->Pause();
401 QuicDispatcher
* dispatcher
=
402 QuicServerPeer::GetDispatcher(server_thread_
->server());
403 ASSERT_EQ(1u, dispatcher
->session_map().size());
404 QuicSession
* session
= dispatcher
->session_map().begin()->second
;
405 QuicConnectionStats server_stats
= session
->connection()->GetStats();
406 // TODO(ianswett): Re-enable this check once b/19572432 is fixed.
407 // if (!had_packet_loss) {
408 // EXPECT_EQ(0u, server_stats.packets_lost);
410 EXPECT_EQ(0u, server_stats
.packets_discarded
);
411 // TODO(ianswett): Restore the check for packets_dropped equals 0.
412 // The expect for packets received is equal to packets processed fails
413 // due to version negotiation packets.
414 server_thread_
->Resume();
417 IPEndPoint server_address_
;
418 string server_hostname_
;
419 scoped_ptr
<ServerThread
> server_thread_
;
420 scoped_ptr
<QuicTestClient
> client_
;
421 PacketDroppingTestWriter
* client_writer_
;
422 PacketDroppingTestWriter
* server_writer_
;
423 bool server_started_
;
424 QuicConfig client_config_
;
425 QuicConfig server_config_
;
426 QuicVersionVector client_supported_versions_
;
427 QuicVersionVector server_supported_versions_
;
428 QuicVersion negotiated_version_
;
429 bool strike_register_no_startup_period_
;
432 // Run all end to end tests with all supported versions.
433 INSTANTIATE_TEST_CASE_P(EndToEndTests
,
435 ::testing::ValuesIn(GetTestParams()));
437 TEST_P(EndToEndTest
, SimpleRequestResponse
) {
438 ASSERT_TRUE(Initialize());
440 EXPECT_EQ(kFooResponseBody
, client_
->SendSynchronousRequest("/foo"));
441 EXPECT_EQ(200u, client_
->response_headers()->parsed_response_code());
444 // TODO(rch): figure out how to detect missing v6 supprt (like on the linux
445 // try bots) and selectively disable this test.
446 TEST_P(EndToEndTest
, DISABLED_SimpleRequestResponsev6
) {
448 CHECK(net::ParseIPLiteralToNumber("::1", &ip
));
449 server_address_
= IPEndPoint(ip
, server_address_
.port());
450 ASSERT_TRUE(Initialize());
452 EXPECT_EQ(kFooResponseBody
, client_
->SendSynchronousRequest("/foo"));
453 EXPECT_EQ(200u, client_
->response_headers()->parsed_response_code());
456 TEST_P(EndToEndTest
, SeparateFinPacket
) {
457 ASSERT_TRUE(Initialize());
459 HTTPMessage
request(HttpConstants::HTTP_1_1
,
460 HttpConstants::POST
, "/foo");
461 request
.set_has_complete_message(false);
463 // Send a request in two parts: the request and then an empty packet with FIN.
464 client_
->SendMessage(request
);
465 client_
->SendData("", true);
466 client_
->WaitForResponse();
467 EXPECT_EQ(kFooResponseBody
, client_
->response_body());
468 EXPECT_EQ(200u, client_
->response_headers()->parsed_response_code());
470 // Now do the same thing but with a content length.
471 request
.AddBody("foo", true);
472 client_
->SendMessage(request
);
473 client_
->SendData("", true);
474 client_
->WaitForResponse();
475 EXPECT_EQ(kFooResponseBody
, client_
->response_body());
476 EXPECT_EQ(200u, client_
->response_headers()->parsed_response_code());
479 TEST_P(EndToEndTest
, MultipleRequestResponse
) {
480 ASSERT_TRUE(Initialize());
482 EXPECT_EQ(kFooResponseBody
, client_
->SendSynchronousRequest("/foo"));
483 EXPECT_EQ(200u, client_
->response_headers()->parsed_response_code());
484 EXPECT_EQ(kBarResponseBody
, client_
->SendSynchronousRequest("/bar"));
485 EXPECT_EQ(200u, client_
->response_headers()->parsed_response_code());
488 TEST_P(EndToEndTest
, MultipleClients
) {
489 ASSERT_TRUE(Initialize());
490 scoped_ptr
<QuicTestClient
> client2(CreateQuicClient(nullptr));
492 HTTPMessage
request(HttpConstants::HTTP_1_1
,
493 HttpConstants::POST
, "/foo");
494 request
.AddHeader("content-length", "3");
495 request
.set_has_complete_message(false);
497 client_
->SendMessage(request
);
498 client2
->SendMessage(request
);
500 client_
->SendData("bar", true);
501 client_
->WaitForResponse();
502 EXPECT_EQ(kFooResponseBody
, client_
->response_body());
503 EXPECT_EQ(200u, client_
->response_headers()->parsed_response_code());
505 client2
->SendData("eep", true);
506 client2
->WaitForResponse();
507 EXPECT_EQ(kFooResponseBody
, client2
->response_body());
508 EXPECT_EQ(200u, client2
->response_headers()->parsed_response_code());
511 TEST_P(EndToEndTest
, RequestOverMultiplePackets
) {
512 // Send a large enough request to guarantee fragmentation.
513 string huge_request
= "/some/path?query=" + string(kMaxPacketSize
, '.');
514 AddToCache(huge_request
, 200, "OK", kBarResponseBody
);
516 ASSERT_TRUE(Initialize());
518 EXPECT_EQ(kBarResponseBody
, client_
->SendSynchronousRequest(huge_request
));
519 EXPECT_EQ(200u, client_
->response_headers()->parsed_response_code());
522 TEST_P(EndToEndTest
, MultiplePacketsRandomOrder
) {
523 // Send a large enough request to guarantee fragmentation.
524 string huge_request
= "/some/path?query=" + string(kMaxPacketSize
, '.');
525 AddToCache(huge_request
, 200, "OK", kBarResponseBody
);
527 ASSERT_TRUE(Initialize());
528 SetPacketSendDelay(QuicTime::Delta::FromMilliseconds(2));
529 SetReorderPercentage(50);
531 EXPECT_EQ(kBarResponseBody
, client_
->SendSynchronousRequest(huge_request
));
532 EXPECT_EQ(200u, client_
->response_headers()->parsed_response_code());
535 TEST_P(EndToEndTest
, PostMissingBytes
) {
536 ASSERT_TRUE(Initialize());
538 // Add a content length header with no body.
539 HTTPMessage
request(HttpConstants::HTTP_1_1
,
540 HttpConstants::POST
, "/foo");
541 request
.AddHeader("content-length", "3");
542 request
.set_skip_message_validation(true);
544 // This should be detected as stream fin without complete request,
545 // triggering an error response.
546 client_
->SendCustomSynchronousRequest(request
);
547 EXPECT_EQ("bad", client_
->response_body());
548 EXPECT_EQ(500u, client_
->response_headers()->parsed_response_code());
551 // TODO(rtenneti): DISABLED_LargePostNoPacketLoss seems to be flaky.
552 // http://crbug.com/297040.
553 TEST_P(EndToEndTest
, DISABLED_LargePostNoPacketLoss
) {
554 ASSERT_TRUE(Initialize());
556 client_
->client()->WaitForCryptoHandshakeConfirmed();
560 GenerateBody(&body
, 1024 * 1024);
562 HTTPMessage
request(HttpConstants::HTTP_1_1
,
563 HttpConstants::POST
, "/foo");
564 request
.AddBody(body
, true);
566 EXPECT_EQ(kFooResponseBody
, client_
->SendCustomSynchronousRequest(request
));
567 VerifyCleanConnection(false);
570 TEST_P(EndToEndTest
, LargePostNoPacketLoss1sRTT
) {
571 ASSERT_TRUE(Initialize());
572 SetPacketSendDelay(QuicTime::Delta::FromMilliseconds(1000));
574 client_
->client()->WaitForCryptoHandshakeConfirmed();
578 GenerateBody(&body
, 100 * 1024);
580 HTTPMessage
request(HttpConstants::HTTP_1_1
,
581 HttpConstants::POST
, "/foo");
582 request
.AddBody(body
, true);
584 EXPECT_EQ(kFooResponseBody
, client_
->SendCustomSynchronousRequest(request
));
585 VerifyCleanConnection(false);
588 TEST_P(EndToEndTest
, LargePostWithPacketLoss
) {
589 // Connect with lower fake packet loss than we'd like to test. Until
590 // b/10126687 is fixed, losing handshake packets is pretty brutal.
591 SetPacketLossPercentage(5);
592 ASSERT_TRUE(Initialize());
594 // Wait for the server SHLO before upping the packet loss.
595 client_
->client()->WaitForCryptoHandshakeConfirmed();
596 SetPacketLossPercentage(30);
600 GenerateBody(&body
, 1024 * 10);
602 HTTPMessage
request(HttpConstants::HTTP_1_1
,
603 HttpConstants::POST
, "/foo");
604 request
.AddBody(body
, true);
606 EXPECT_EQ(kFooResponseBody
, client_
->SendCustomSynchronousRequest(request
));
607 VerifyCleanConnection(true);
610 TEST_P(EndToEndTest
, LargePostWithPacketLossAndBlockedSocket
) {
611 // Connect with lower fake packet loss than we'd like to test. Until
612 // b/10126687 is fixed, losing handshake packets is pretty brutal.
613 SetPacketLossPercentage(5);
614 ASSERT_TRUE(Initialize());
616 // Wait for the server SHLO before upping the packet loss.
617 client_
->client()->WaitForCryptoHandshakeConfirmed();
618 SetPacketLossPercentage(10);
619 client_writer_
->set_fake_blocked_socket_percentage(10);
623 GenerateBody(&body
, 1024 * 10);
625 HTTPMessage
request(HttpConstants::HTTP_1_1
,
626 HttpConstants::POST
, "/foo");
627 request
.AddBody(body
, true);
629 EXPECT_EQ(kFooResponseBody
, client_
->SendCustomSynchronousRequest(request
));
632 TEST_P(EndToEndTest
, LargePostNoPacketLossWithDelayAndReordering
) {
633 ASSERT_TRUE(Initialize());
635 client_
->client()->WaitForCryptoHandshakeConfirmed();
636 // Both of these must be called when the writer is not actively used.
637 SetPacketSendDelay(QuicTime::Delta::FromMilliseconds(2));
638 SetReorderPercentage(30);
642 GenerateBody(&body
, 1024 * 1024);
644 HTTPMessage
request(HttpConstants::HTTP_1_1
,
645 HttpConstants::POST
, "/foo");
646 request
.AddBody(body
, true);
648 EXPECT_EQ(kFooResponseBody
, client_
->SendCustomSynchronousRequest(request
));
651 TEST_P(EndToEndTest
, LargePostZeroRTTFailure
) {
652 // Have the server accept 0-RTT without waiting a startup period.
653 strike_register_no_startup_period_
= true;
655 // Send a request and then disconnect. This prepares the client to attempt
656 // a 0-RTT handshake for the next request.
657 ASSERT_TRUE(Initialize());
660 GenerateBody(&body
, 20480);
662 HTTPMessage
request(HttpConstants::HTTP_1_1
, HttpConstants::POST
, "/foo");
663 request
.AddBody(body
, true);
665 EXPECT_EQ(kFooResponseBody
, client_
->SendCustomSynchronousRequest(request
));
666 EXPECT_EQ(2, client_
->client()->session()->GetNumSentClientHellos());
668 client_
->Disconnect();
670 // The 0-RTT handshake should succeed.
672 client_
->WaitForResponseForMs(-1);
673 ASSERT_TRUE(client_
->client()->connected());
674 EXPECT_EQ(kFooResponseBody
, client_
->SendCustomSynchronousRequest(request
));
675 EXPECT_EQ(1, client_
->client()->session()->GetNumSentClientHellos());
677 client_
->Disconnect();
679 // Restart the server so that the 0-RTT handshake will take 1 RTT.
681 server_writer_
= new PacketDroppingTestWriter();
685 ASSERT_TRUE(client_
->client()->connected());
686 EXPECT_EQ(kFooResponseBody
, client_
->SendCustomSynchronousRequest(request
));
687 EXPECT_EQ(2, client_
->client()->session()->GetNumSentClientHellos());
688 VerifyCleanConnection(false);
691 TEST_P(EndToEndTest
, CorrectlyConfiguredFec
) {
692 ASSERT_TRUE(Initialize());
693 client_
->client()->WaitForCryptoHandshakeConfirmed();
694 server_thread_
->WaitForCryptoHandshakeConfirmed();
696 FecPolicy expected_policy
=
697 GetParam().use_fec
? FEC_PROTECT_ALWAYS
: FEC_PROTECT_OPTIONAL
;
699 // Verify that server's FEC configuration is correct.
700 server_thread_
->Pause();
701 QuicDispatcher
* dispatcher
=
702 QuicServerPeer::GetDispatcher(server_thread_
->server());
703 ASSERT_EQ(1u, dispatcher
->session_map().size());
704 QuicSpdySession
* session
= dispatcher
->session_map().begin()->second
;
705 EXPECT_EQ(expected_policy
,
706 QuicSpdySessionPeer::GetHeadersStream(session
)->fec_policy());
707 server_thread_
->Resume();
709 // Verify that client's FEC configuration is correct.
710 EXPECT_EQ(expected_policy
, QuicSpdySessionPeer::GetHeadersStream(
711 client_
->client()->session())->fec_policy());
712 EXPECT_EQ(expected_policy
,
713 client_
->GetOrCreateStream()->fec_policy());
716 TEST_P(EndToEndTest
, LargePostSmallBandwidthLargeBuffer
) {
717 ASSERT_TRUE(Initialize());
718 SetPacketSendDelay(QuicTime::Delta::FromMicroseconds(1));
719 // 256KB per second with a 256KB buffer from server to client. Wireless
720 // clients commonly have larger buffers, but our max CWND is 200.
721 server_writer_
->set_max_bandwidth_and_buffer_size(
722 QuicBandwidth::FromBytesPerSecond(256 * 1024), 256 * 1024);
724 client_
->client()->WaitForCryptoHandshakeConfirmed();
728 GenerateBody(&body
, 1024 * 1024);
730 HTTPMessage
request(HttpConstants::HTTP_1_1
, HttpConstants::POST
, "/foo");
731 request
.AddBody(body
, true);
733 EXPECT_EQ(kFooResponseBody
, client_
->SendCustomSynchronousRequest(request
));
734 // This connection will not drop packets, because the buffer size is larger
735 // than the default receive window.
736 VerifyCleanConnection(false);
739 TEST_P(EndToEndTest
, DoNotSetResumeWriteAlarmIfConnectionFlowControlBlocked
) {
740 // Regression test for b/14677858.
741 // Test that the resume write alarm is not set in QuicConnection::OnCanWrite
742 // if currently connection level flow control blocked. If set, this results in
743 // an infinite loop in the EpollServer, as the alarm fires and is immediately
745 ASSERT_TRUE(Initialize());
746 client_
->client()->WaitForCryptoHandshakeConfirmed();
748 // Ensure both stream and connection level are flow control blocked by setting
749 // the send window offset to 0.
750 const uint64 flow_control_window
=
751 server_config_
.GetInitialStreamFlowControlWindowToSend();
752 QuicSpdyClientStream
* stream
= client_
->GetOrCreateStream();
753 QuicSession
* session
= client_
->client()->session();
754 QuicFlowControllerPeer::SetSendWindowOffset(stream
->flow_controller(), 0);
755 QuicFlowControllerPeer::SetSendWindowOffset(session
->flow_controller(), 0);
756 EXPECT_TRUE(stream
->flow_controller()->IsBlocked());
757 EXPECT_TRUE(session
->flow_controller()->IsBlocked());
759 // Make sure that the stream has data pending so that it will be marked as
760 // write blocked when it receives a stream level WINDOW_UPDATE.
761 stream
->SendBody("hello", false);
763 // The stream now attempts to write, fails because it is still connection
764 // level flow control blocked, and is added to the write blocked list.
765 QuicWindowUpdateFrame
window_update(stream
->id(), 2 * flow_control_window
);
766 stream
->OnWindowUpdateFrame(window_update
);
768 // Prior to fixing b/14677858 this call would result in an infinite loop in
769 // Chromium. As a proxy for detecting this, we now check whether the
770 // resume_writes_alarm is set after OnCanWrite. It should not be, as the
771 // connection is still flow control blocked.
772 session
->connection()->OnCanWrite();
774 QuicAlarm
* resume_writes_alarm
=
775 QuicConnectionPeer::GetResumeWritesAlarm(session
->connection());
776 EXPECT_FALSE(resume_writes_alarm
->IsSet());
779 TEST_P(EndToEndTest
, InvalidStream
) {
780 ASSERT_TRUE(Initialize());
781 client_
->client()->WaitForCryptoHandshakeConfirmed();
784 GenerateBody(&body
, kMaxPacketSize
);
786 HTTPMessage
request(HttpConstants::HTTP_1_1
, HttpConstants::POST
, "/foo");
787 request
.AddBody(body
, true);
788 // Force the client to write with a stream ID belonging to a nonexistent
789 // server-side stream.
790 QuicSessionPeer::SetNextStreamId(client_
->client()->session(), 2);
792 client_
->SendCustomSynchronousRequest(request
);
793 // EXPECT_EQ(QUIC_STREAM_CONNECTION_ERROR, client_->stream_error());
794 EXPECT_EQ(QUIC_PACKET_FOR_NONEXISTENT_STREAM
, client_
->connection_error());
797 // TODO(rch): this test seems to cause net_unittests timeouts :|
798 TEST_P(EndToEndTest
, DISABLED_MultipleTermination
) {
799 ASSERT_TRUE(Initialize());
801 HTTPMessage
request(HttpConstants::HTTP_1_1
,
802 HttpConstants::POST
, "/foo");
803 request
.AddHeader("content-length", "3");
804 request
.set_has_complete_message(false);
806 // Set the offset so we won't frame. Otherwise when we pick up termination
807 // before HTTP framing is complete, we send an error and close the stream,
808 // and the second write is picked up as writing on a closed stream.
809 QuicSpdyClientStream
* stream
= client_
->GetOrCreateStream();
810 ASSERT_TRUE(stream
!= nullptr);
811 ReliableQuicStreamPeer::SetStreamBytesWritten(3, stream
);
813 client_
->SendData("bar", true);
814 client_
->WaitForWriteToFlush();
816 // By default the stream protects itself from writes after terminte is set.
817 // Override this to test the server handling buggy clients.
818 ReliableQuicStreamPeer::SetWriteSideClosed(
819 false, client_
->GetOrCreateStream());
821 EXPECT_DFATAL(client_
->SendData("eep", true), "Fin already buffered");
824 TEST_P(EndToEndTest
, Timeout
) {
825 client_config_
.SetIdleConnectionStateLifetime(
826 QuicTime::Delta::FromMicroseconds(500),
827 QuicTime::Delta::FromMicroseconds(500));
828 // Note: we do NOT ASSERT_TRUE: we may time out during initial handshake:
829 // that's enough to validate timeout in this case.
831 while (client_
->client()->connected()) {
832 client_
->client()->WaitForEvents();
836 TEST_P(EndToEndTest
, NegotiateMaxOpenStreams
) {
837 // Negotiate 1 max open stream.
838 client_config_
.SetMaxStreamsPerConnection(1, 1);
839 ASSERT_TRUE(Initialize());
840 client_
->client()->WaitForCryptoHandshakeConfirmed();
842 // Make the client misbehave after negotiation.
843 const int kServerMaxStreams
= kMaxStreamsMinimumIncrement
+ 1;
844 QuicSessionPeer::SetMaxOpenStreams(client_
->client()->session(),
845 kServerMaxStreams
+ 1);
847 HTTPMessage
request(HttpConstants::HTTP_1_1
, HttpConstants::POST
, "/foo");
848 request
.AddHeader("content-length", "3");
849 request
.set_has_complete_message(false);
851 // The server supports a small number of additional streams beyond the
852 // negotiated limit. Open enough streams to go beyond that limit.
853 for (int i
= 0; i
< kServerMaxStreams
+ 1; ++i
) {
854 client_
->SendMessage(request
);
856 client_
->WaitForResponse();
858 EXPECT_FALSE(client_
->connected());
859 EXPECT_EQ(QUIC_STREAM_CONNECTION_ERROR
, client_
->stream_error());
860 EXPECT_EQ(QUIC_TOO_MANY_OPEN_STREAMS
, client_
->connection_error());
863 TEST_P(EndToEndTest
, NegotiateCongestionControl
) {
864 ValueRestore
<bool> old_flag(&FLAGS_quic_allow_bbr
, true);
865 ASSERT_TRUE(Initialize());
866 client_
->client()->WaitForCryptoHandshakeConfirmed();
868 CongestionControlType expected_congestion_control_type
= kReno
;
869 switch (GetParam().congestion_control_tag
) {
871 expected_congestion_control_type
= kReno
;
874 expected_congestion_control_type
= kBBR
;
877 expected_congestion_control_type
= kCubic
;
880 DLOG(FATAL
) << "Unexpected congestion control tag";
883 EXPECT_EQ(expected_congestion_control_type
,
884 QuicSentPacketManagerPeer::GetSendAlgorithm(
885 *GetSentPacketManagerFromFirstServerSession())
886 ->GetCongestionControlType());
889 TEST_P(EndToEndTest
, LimitMaxOpenStreams
) {
890 // Server limits the number of max streams to 2.
891 server_config_
.SetMaxStreamsPerConnection(2, 2);
892 // Client tries to negotiate for 10.
893 client_config_
.SetMaxStreamsPerConnection(10, 5);
895 ASSERT_TRUE(Initialize());
896 client_
->client()->WaitForCryptoHandshakeConfirmed();
897 QuicConfig
* client_negotiated_config
= client_
->client()->session()->config();
898 EXPECT_EQ(2u, client_negotiated_config
->MaxStreamsPerConnection());
901 TEST_P(EndToEndTest
, ClientSuggestsRTT
) {
902 // Client suggests initial RTT, verify it is used.
903 const uint32 kInitialRTT
= 20000;
904 client_config_
.SetInitialRoundTripTimeUsToSend(kInitialRTT
);
906 ASSERT_TRUE(Initialize());
907 client_
->client()->WaitForCryptoHandshakeConfirmed();
908 server_thread_
->WaitForCryptoHandshakeConfirmed();
910 // Pause the server so we can access the server's internals without races.
911 server_thread_
->Pause();
912 QuicDispatcher
* dispatcher
=
913 QuicServerPeer::GetDispatcher(server_thread_
->server());
914 ASSERT_EQ(1u, dispatcher
->session_map().size());
915 const QuicSentPacketManager
& client_sent_packet_manager
=
916 client_
->client()->session()->connection()->sent_packet_manager();
917 const QuicSentPacketManager
& server_sent_packet_manager
=
918 *GetSentPacketManagerFromFirstServerSession();
920 EXPECT_EQ(kInitialRTT
,
921 client_sent_packet_manager
.GetRttStats()->initial_rtt_us());
922 EXPECT_EQ(kInitialRTT
,
923 server_sent_packet_manager
.GetRttStats()->initial_rtt_us());
924 server_thread_
->Resume();
927 TEST_P(EndToEndTest
, MaxInitialRTT
) {
928 // Client tries to suggest twice the server's max initial rtt and the server
930 client_config_
.SetInitialRoundTripTimeUsToSend(
931 2 * kMaxInitialRoundTripTimeUs
);
933 ASSERT_TRUE(Initialize());
934 client_
->client()->WaitForCryptoHandshakeConfirmed();
935 server_thread_
->WaitForCryptoHandshakeConfirmed();
937 // Pause the server so we can access the server's internals without races.
938 server_thread_
->Pause();
939 QuicDispatcher
* dispatcher
=
940 QuicServerPeer::GetDispatcher(server_thread_
->server());
941 ASSERT_EQ(1u, dispatcher
->session_map().size());
942 QuicSession
* session
= dispatcher
->session_map().begin()->second
;
943 const QuicSentPacketManager
& client_sent_packet_manager
=
944 client_
->client()->session()->connection()->sent_packet_manager();
946 // Now that acks have been exchanged, the RTT estimate has decreased on the
947 // server and is not infinite on the client.
949 client_sent_packet_manager
.GetRttStats()->smoothed_rtt().IsInfinite());
950 const RttStats
& server_rtt_stats
=
951 *session
->connection()->sent_packet_manager().GetRttStats();
952 EXPECT_EQ(static_cast<int64
>(kMaxInitialRoundTripTimeUs
),
953 server_rtt_stats
.initial_rtt_us());
954 EXPECT_GE(static_cast<int64
>(kMaxInitialRoundTripTimeUs
),
955 server_rtt_stats
.smoothed_rtt().ToMicroseconds());
956 server_thread_
->Resume();
959 TEST_P(EndToEndTest
, MinInitialRTT
) {
960 // Client tries to suggest 0 and the server uses the default.
961 client_config_
.SetInitialRoundTripTimeUsToSend(0);
963 ASSERT_TRUE(Initialize());
964 client_
->client()->WaitForCryptoHandshakeConfirmed();
965 server_thread_
->WaitForCryptoHandshakeConfirmed();
967 // Pause the server so we can access the server's internals without races.
968 server_thread_
->Pause();
969 QuicDispatcher
* dispatcher
=
970 QuicServerPeer::GetDispatcher(server_thread_
->server());
971 ASSERT_EQ(1u, dispatcher
->session_map().size());
972 QuicSession
* session
= dispatcher
->session_map().begin()->second
;
973 const QuicSentPacketManager
& client_sent_packet_manager
=
974 client_
->client()->session()->connection()->sent_packet_manager();
975 const QuicSentPacketManager
& server_sent_packet_manager
=
976 session
->connection()->sent_packet_manager();
978 // Now that acks have been exchanged, the RTT estimate has decreased on the
979 // server and is not infinite on the client.
981 client_sent_packet_manager
.GetRttStats()->smoothed_rtt().IsInfinite());
982 // Expect the default rtt of 100ms.
983 EXPECT_EQ(static_cast<int64
>(100 * kNumMicrosPerMilli
),
984 server_sent_packet_manager
.GetRttStats()->initial_rtt_us());
985 // Ensure the bandwidth is valid.
986 client_sent_packet_manager
.BandwidthEstimate();
987 server_sent_packet_manager
.BandwidthEstimate();
988 server_thread_
->Resume();
991 TEST_P(EndToEndTest
, 0ByteConnectionId
) {
992 client_config_
.SetBytesForConnectionIdToSend(0);
993 ASSERT_TRUE(Initialize());
995 EXPECT_EQ(kFooResponseBody
, client_
->SendSynchronousRequest("/foo"));
996 EXPECT_EQ(200u, client_
->response_headers()->parsed_response_code());
998 QuicPacketHeader
* header
= QuicConnectionPeer::GetLastHeader(
999 client_
->client()->session()->connection());
1000 EXPECT_EQ(PACKET_0BYTE_CONNECTION_ID
,
1001 header
->public_header
.connection_id_length
);
1004 TEST_P(EndToEndTest
, 1ByteConnectionId
) {
1005 client_config_
.SetBytesForConnectionIdToSend(1);
1006 ASSERT_TRUE(Initialize());
1008 EXPECT_EQ(kFooResponseBody
, client_
->SendSynchronousRequest("/foo"));
1009 EXPECT_EQ(200u, client_
->response_headers()->parsed_response_code());
1010 QuicPacketHeader
* header
= QuicConnectionPeer::GetLastHeader(
1011 client_
->client()->session()->connection());
1012 EXPECT_EQ(PACKET_1BYTE_CONNECTION_ID
,
1013 header
->public_header
.connection_id_length
);
1016 TEST_P(EndToEndTest
, 4ByteConnectionId
) {
1017 client_config_
.SetBytesForConnectionIdToSend(4);
1018 ASSERT_TRUE(Initialize());
1020 EXPECT_EQ(kFooResponseBody
, client_
->SendSynchronousRequest("/foo"));
1021 EXPECT_EQ(200u, client_
->response_headers()->parsed_response_code());
1022 QuicPacketHeader
* header
= QuicConnectionPeer::GetLastHeader(
1023 client_
->client()->session()->connection());
1024 EXPECT_EQ(PACKET_4BYTE_CONNECTION_ID
,
1025 header
->public_header
.connection_id_length
);
1028 TEST_P(EndToEndTest
, 8ByteConnectionId
) {
1029 client_config_
.SetBytesForConnectionIdToSend(8);
1030 ASSERT_TRUE(Initialize());
1032 EXPECT_EQ(kFooResponseBody
, client_
->SendSynchronousRequest("/foo"));
1033 EXPECT_EQ(200u, client_
->response_headers()->parsed_response_code());
1034 QuicPacketHeader
* header
= QuicConnectionPeer::GetLastHeader(
1035 client_
->client()->session()->connection());
1036 EXPECT_EQ(PACKET_8BYTE_CONNECTION_ID
,
1037 header
->public_header
.connection_id_length
);
1040 TEST_P(EndToEndTest
, 15ByteConnectionId
) {
1041 client_config_
.SetBytesForConnectionIdToSend(15);
1042 ASSERT_TRUE(Initialize());
1044 // Our server is permissive and allows for out of bounds values.
1045 EXPECT_EQ(kFooResponseBody
, client_
->SendSynchronousRequest("/foo"));
1046 EXPECT_EQ(200u, client_
->response_headers()->parsed_response_code());
1047 QuicPacketHeader
* header
= QuicConnectionPeer::GetLastHeader(
1048 client_
->client()->session()->connection());
1049 EXPECT_EQ(PACKET_8BYTE_CONNECTION_ID
,
1050 header
->public_header
.connection_id_length
);
1053 TEST_P(EndToEndTest
, ResetConnection
) {
1054 ASSERT_TRUE(Initialize());
1055 client_
->client()->WaitForCryptoHandshakeConfirmed();
1057 EXPECT_EQ(kFooResponseBody
, client_
->SendSynchronousRequest("/foo"));
1058 EXPECT_EQ(200u, client_
->response_headers()->parsed_response_code());
1059 client_
->ResetConnection();
1060 EXPECT_EQ(kBarResponseBody
, client_
->SendSynchronousRequest("/bar"));
1061 EXPECT_EQ(200u, client_
->response_headers()->parsed_response_code());
1064 TEST_P(EndToEndTest
, MaxStreamsUberTest
) {
1065 SetPacketLossPercentage(1);
1066 ASSERT_TRUE(Initialize());
1068 GenerateBody(&large_body
, 10240);
1069 int max_streams
= 100;
1071 AddToCache("/large_response", 200, "OK", large_body
);;
1073 client_
->client()->WaitForCryptoHandshakeConfirmed();
1074 SetPacketLossPercentage(10);
1076 for (int i
= 0; i
< max_streams
; ++i
) {
1077 EXPECT_LT(0, client_
->SendRequest("/large_response"));
1080 // WaitForEvents waits 50ms and returns true if there are outstanding
1082 while (client_
->client()->WaitForEvents() == true) {
1086 TEST_P(EndToEndTest
, StreamCancelErrorTest
) {
1087 ASSERT_TRUE(Initialize());
1089 GenerateBody(&small_body
, 256);
1091 AddToCache("/small_response", 200, "OK", small_body
);
1093 client_
->client()->WaitForCryptoHandshakeConfirmed();
1095 QuicSession
* session
= client_
->client()->session();
1096 // Lose the request.
1097 SetPacketLossPercentage(100);
1098 EXPECT_LT(0, client_
->SendRequest("/small_response"));
1099 client_
->client()->WaitForEvents();
1100 // Transmit the cancel, and ensure the connection is torn down properly.
1101 SetPacketLossPercentage(0);
1102 QuicStreamId stream_id
= kClientDataStreamId1
;
1103 session
->SendRstStream(stream_id
, QUIC_STREAM_CANCELLED
, 0);
1105 // WaitForEvents waits 50ms and returns true if there are outstanding
1107 while (client_
->client()->WaitForEvents() == true) {
1109 // It should be completely fine to RST a stream before any data has been
1110 // received for that stream.
1111 EXPECT_EQ(QUIC_NO_ERROR
, client_
->connection_error());
1114 class WrongAddressWriter
: public QuicPacketWriterWrapper
{
1116 WrongAddressWriter() {
1118 CHECK(net::ParseIPLiteralToNumber("127.0.0.2", &ip
));
1119 self_address_
= IPEndPoint(ip
, 0);
1122 WriteResult
WritePacket(const char* buffer
,
1124 const IPAddressNumber
& real_self_address
,
1125 const IPEndPoint
& peer_address
) override
{
1126 // Use wrong address!
1127 return QuicPacketWriterWrapper::WritePacket(
1128 buffer
, buf_len
, self_address_
.address(), peer_address
);
1131 bool IsWriteBlockedDataBuffered() const override
{ return false; }
1133 IPEndPoint self_address_
;
1136 TEST_P(EndToEndTest
, ConnectionMigrationClientIPChanged
) {
1137 // Tests that the client's IP can not change during an established QUIC
1138 // connection. If it changes, the connection is closed by the server as we do
1139 // not yet support IP migration.
1140 ASSERT_TRUE(Initialize());
1142 EXPECT_EQ(kFooResponseBody
, client_
->SendSynchronousRequest("/foo"));
1143 EXPECT_EQ(200u, client_
->response_headers()->parsed_response_code());
1145 WrongAddressWriter
* writer
= new WrongAddressWriter();
1147 writer
->set_writer(new QuicDefaultPacketWriter(client_
->client()->fd()));
1148 QuicConnectionPeer::SetWriter(client_
->client()->session()->connection(),
1150 /* owns_writer= */ true);
1152 client_
->SendSynchronousRequest("/bar");
1154 EXPECT_EQ(QUIC_STREAM_CONNECTION_ERROR
, client_
->stream_error());
1155 EXPECT_EQ(QUIC_ERROR_MIGRATING_ADDRESS
, client_
->connection_error());
1158 TEST_P(EndToEndTest
, ConnectionMigrationClientPortChanged
) {
1159 // Tests that the client's port can change during an established QUIC
1160 // connection, and that doing so does not result in the connection being
1161 // closed by the server.
1162 ASSERT_TRUE(Initialize());
1164 EXPECT_EQ(kFooResponseBody
, client_
->SendSynchronousRequest("/foo"));
1165 EXPECT_EQ(200u, client_
->response_headers()->parsed_response_code());
1167 // Store the client address which was used to send the first request.
1168 IPEndPoint old_address
= client_
->client()->client_address();
1170 // Stop listening on the old FD.
1171 EpollServer
* eps
= client_
->epoll_server();
1172 int old_fd
= client_
->client()->fd();
1173 eps
->UnregisterFD(old_fd
);
1174 // Create a new socket before closing the old one, which will result in a new
1176 QuicClientPeer::CreateUDPSocket(client_
->client());
1179 // The packet writer needs to be updated to use the new FD.
1180 client_
->client()->CreateQuicPacketWriter();
1182 // Change the internal state of the client and connection to use the new port,
1183 // this is done because in a real NAT rebinding the client wouldn't see any
1184 // port change, and so expects no change to incoming port.
1185 // This is kind of ugly, but needed as we are simply swapping out the client
1186 // FD rather than any more complex NAT rebinding simulation.
1187 int new_port
= client_
->client()->client_address().port();
1188 QuicClientPeer::SetClientPort(client_
->client(), new_port
);
1189 QuicConnectionPeer::SetSelfAddress(
1190 client_
->client()->session()->connection(),
1192 client_
->client()->session()->connection()->self_address().address(),
1195 // Register the new FD for epoll events.
1196 int new_fd
= client_
->client()->fd();
1197 eps
->RegisterFD(new_fd
, client_
->client(), EPOLLIN
| EPOLLOUT
| EPOLLET
);
1199 // Send a second request, using the new FD.
1200 EXPECT_EQ(kBarResponseBody
, client_
->SendSynchronousRequest("/bar"));
1201 EXPECT_EQ(200u, client_
->response_headers()->parsed_response_code());
1203 // Verify that the client's ephemeral port is different.
1204 IPEndPoint new_address
= client_
->client()->client_address();
1205 EXPECT_EQ(old_address
.address(), new_address
.address());
1206 EXPECT_NE(old_address
.port(), new_address
.port());
1209 TEST_P(EndToEndTest
, DifferentFlowControlWindows
) {
1210 // Client and server can set different initial flow control receive windows.
1211 // These are sent in CHLO/SHLO. Tests that these values are exchanged properly
1212 // in the crypto handshake.
1213 const uint32 kClientStreamIFCW
= 123456;
1214 const uint32 kClientSessionIFCW
= 234567;
1215 set_client_initial_stream_flow_control_receive_window(kClientStreamIFCW
);
1216 set_client_initial_session_flow_control_receive_window(kClientSessionIFCW
);
1218 const uint32 kServerStreamIFCW
= 654321;
1219 const uint32 kServerSessionIFCW
= 765432;
1220 set_server_initial_stream_flow_control_receive_window(kServerStreamIFCW
);
1221 set_server_initial_session_flow_control_receive_window(kServerSessionIFCW
);
1223 ASSERT_TRUE(Initialize());
1225 // Values are exchanged during crypto handshake, so wait for that to finish.
1226 client_
->client()->WaitForCryptoHandshakeConfirmed();
1227 server_thread_
->WaitForCryptoHandshakeConfirmed();
1229 // Open a data stream to make sure the stream level flow control is updated.
1230 QuicSpdyClientStream
* stream
= client_
->GetOrCreateStream();
1231 stream
->SendBody("hello", false);
1233 // Client should have the right values for server's receive window.
1234 EXPECT_EQ(kServerStreamIFCW
,
1238 ->ReceivedInitialStreamFlowControlWindowBytes());
1239 EXPECT_EQ(kServerSessionIFCW
,
1243 ->ReceivedInitialSessionFlowControlWindowBytes());
1244 EXPECT_EQ(kServerStreamIFCW
, QuicFlowControllerPeer::SendWindowOffset(
1245 stream
->flow_controller()));
1246 EXPECT_EQ(kServerSessionIFCW
,
1247 QuicFlowControllerPeer::SendWindowOffset(
1248 client_
->client()->session()->flow_controller()));
1250 // Server should have the right values for client's receive window.
1251 server_thread_
->Pause();
1252 QuicDispatcher
* dispatcher
=
1253 QuicServerPeer::GetDispatcher(server_thread_
->server());
1254 QuicSession
* session
= dispatcher
->session_map().begin()->second
;
1255 EXPECT_EQ(kClientStreamIFCW
,
1256 session
->config()->ReceivedInitialStreamFlowControlWindowBytes());
1257 EXPECT_EQ(kClientSessionIFCW
,
1258 session
->config()->ReceivedInitialSessionFlowControlWindowBytes());
1259 EXPECT_EQ(kClientSessionIFCW
, QuicFlowControllerPeer::SendWindowOffset(
1260 session
->flow_controller()));
1261 server_thread_
->Resume();
1264 TEST_P(EndToEndTest
, HeadersAndCryptoStreamsNoConnectionFlowControl
) {
1265 // The special headers and crypto streams should be subject to per-stream flow
1266 // control limits, but should not be subject to connection level flow control.
1267 const uint32 kStreamIFCW
= 123456;
1268 const uint32 kSessionIFCW
= 234567;
1269 set_client_initial_stream_flow_control_receive_window(kStreamIFCW
);
1270 set_client_initial_session_flow_control_receive_window(kSessionIFCW
);
1271 set_server_initial_stream_flow_control_receive_window(kStreamIFCW
);
1272 set_server_initial_session_flow_control_receive_window(kSessionIFCW
);
1274 ASSERT_TRUE(Initialize());
1276 // Wait for crypto handshake to finish. This should have contributed to the
1277 // crypto stream flow control window, but not affected the session flow
1279 client_
->client()->WaitForCryptoHandshakeConfirmed();
1280 server_thread_
->WaitForCryptoHandshakeConfirmed();
1282 QuicCryptoStream
* crypto_stream
=
1283 QuicSessionPeer::GetCryptoStream(client_
->client()->session());
1285 QuicFlowControllerPeer::SendWindowSize(crypto_stream
->flow_controller()),
1287 EXPECT_EQ(kSessionIFCW
, QuicFlowControllerPeer::SendWindowSize(
1288 client_
->client()->session()->flow_controller()));
1290 // Send a request with no body, and verify that the connection level window
1291 // has not been affected.
1292 EXPECT_EQ(kFooResponseBody
, client_
->SendSynchronousRequest("/foo"));
1294 QuicHeadersStream
* headers_stream
=
1295 QuicSpdySessionPeer::GetHeadersStream(client_
->client()->session());
1297 QuicFlowControllerPeer::SendWindowSize(headers_stream
->flow_controller()),
1299 EXPECT_EQ(kSessionIFCW
, QuicFlowControllerPeer::SendWindowSize(
1300 client_
->client()->session()->flow_controller()));
1302 // Server should be in a similar state: connection flow control window should
1303 // not have any bytes marked as received.
1304 server_thread_
->Pause();
1305 QuicDispatcher
* dispatcher
=
1306 QuicServerPeer::GetDispatcher(server_thread_
->server());
1307 QuicSession
* session
= dispatcher
->session_map().begin()->second
;
1308 QuicFlowController
* server_connection_flow_controller
=
1309 session
->flow_controller();
1310 EXPECT_EQ(kSessionIFCW
, QuicFlowControllerPeer::ReceiveWindowSize(
1311 server_connection_flow_controller
));
1312 server_thread_
->Resume();
1315 TEST_P(EndToEndTest
, RequestWithNoBodyWillNeverSendStreamFrameWithFIN
) {
1316 // A stream created on receipt of a simple request with no body will never get
1317 // a stream frame with a FIN. Verify that we don't keep track of the stream in
1318 // the locally closed streams map: it will never be removed if so.
1319 ASSERT_TRUE(Initialize());
1321 // Send a simple headers only request, and receive response.
1322 EXPECT_EQ(kFooResponseBody
, client_
->SendSynchronousRequest("/foo"));
1323 EXPECT_EQ(200u, client_
->response_headers()->parsed_response_code());
1325 // Now verify that the server is not waiting for a final FIN or RST.
1326 server_thread_
->Pause();
1327 QuicDispatcher
* dispatcher
=
1328 QuicServerPeer::GetDispatcher(server_thread_
->server());
1329 QuicSession
* session
= dispatcher
->session_map().begin()->second
;
1330 EXPECT_EQ(0u, QuicSessionPeer::GetLocallyClosedStreamsHighestOffset(
1332 server_thread_
->Resume();
1335 // A TestAckNotifierDelegate verifies that its OnAckNotification method has been
1336 // called exactly once on destruction.
1337 class TestAckNotifierDelegate
: public QuicAckNotifier::DelegateInterface
{
1339 TestAckNotifierDelegate() {}
1341 void OnAckNotification(int /*num_retransmitted_packets*/,
1342 int /*num_retransmitted_bytes*/,
1343 QuicTime::Delta
/*delta_largest_observed*/) override
{
1344 ASSERT_FALSE(has_been_notified_
);
1345 has_been_notified_
= true;
1348 bool has_been_notified() const { return has_been_notified_
; }
1351 // Object is ref counted.
1352 ~TestAckNotifierDelegate() override
{ EXPECT_TRUE(has_been_notified_
); }
1355 bool has_been_notified_
= false;
1358 TEST_P(EndToEndTest
, AckNotifierWithPacketLossAndBlockedSocket
) {
1359 // Verify that even in the presence of packet loss and occasionally blocked
1360 // socket, an AckNotifierDelegate will get informed that the data it is
1361 // interested in has been ACKed. This tests end-to-end ACK notification, and
1362 // demonstrates that retransmissions do not break this functionality.
1363 SetPacketLossPercentage(5);
1364 ASSERT_TRUE(Initialize());
1366 // Wait for the server SHLO before upping the packet loss.
1367 client_
->client()->WaitForCryptoHandshakeConfirmed();
1368 SetPacketLossPercentage(30);
1369 client_writer_
->set_fake_blocked_socket_percentage(10);
1371 // Create a POST request and send the headers only.
1372 HTTPMessage
request(HttpConstants::HTTP_1_1
, HttpConstants::POST
, "/foo");
1373 request
.set_has_complete_message(false);
1374 client_
->SendMessage(request
);
1376 // The TestAckNotifierDelegate will cause a failure if not notified.
1377 scoped_refptr
<TestAckNotifierDelegate
> delegate(new TestAckNotifierDelegate
);
1379 // Test the AckNotifier's ability to track multiple packets by making the
1380 // request body exceed the size of a single packet.
1381 string request_string
=
1382 "a request body bigger than one packet" + string(kMaxPacketSize
, '.');
1384 // Send the request, and register the delegate for ACKs.
1385 client_
->SendData(request_string
, true, delegate
.get());
1386 client_
->WaitForResponse();
1387 EXPECT_EQ(kFooResponseBody
, client_
->response_body());
1388 EXPECT_EQ(200u, client_
->response_headers()->parsed_response_code());
1390 // Send another request to flush out any pending ACKs on the server.
1391 client_
->SendSynchronousRequest(request_string
);
1393 // Pause the server to avoid races.
1394 server_thread_
->Pause();
1395 // Make sure the delegate does get the notification it expects.
1396 while (!delegate
->has_been_notified()) {
1397 // Waits for up to 50 ms.
1398 client_
->client()->WaitForEvents();
1400 server_thread_
->Resume();
1403 // Send a public reset from the server for a different connection ID.
1404 // It should be ignored.
1405 TEST_P(EndToEndTest
, ServerSendPublicResetWithDifferentConnectionId
) {
1406 ASSERT_TRUE(Initialize());
1408 // Send the public reset.
1409 QuicConnectionId incorrect_connection_id
=
1410 client_
->client()->session()->connection()->connection_id() + 1;
1411 QuicPublicResetPacket header
;
1412 header
.public_header
.connection_id
= incorrect_connection_id
;
1413 header
.public_header
.reset_flag
= true;
1414 header
.public_header
.version_flag
= false;
1415 header
.rejected_sequence_number
= 10101;
1416 QuicFramer
framer(server_supported_versions_
, QuicTime::Zero(),
1417 Perspective::IS_SERVER
);
1418 scoped_ptr
<QuicEncryptedPacket
> packet(framer
.BuildPublicResetPacket(header
));
1419 testing::NiceMock
<MockQuicConnectionDebugVisitor
> visitor
;
1420 client_
->client()->session()->connection()->set_debug_visitor(&visitor
);
1421 EXPECT_CALL(visitor
, OnIncorrectConnectionId(incorrect_connection_id
))
1423 // We must pause the server's thread in order to call WritePacket without
1425 server_thread_
->Pause();
1426 server_writer_
->WritePacket(packet
->data(), packet
->length(),
1427 server_address_
.address(),
1428 client_
->client()->client_address());
1429 server_thread_
->Resume();
1431 // The connection should be unaffected.
1432 EXPECT_EQ(kFooResponseBody
, client_
->SendSynchronousRequest("/foo"));
1433 EXPECT_EQ(200u, client_
->response_headers()->parsed_response_code());
1435 client_
->client()->session()->connection()->set_debug_visitor(nullptr);
1438 // Send a public reset from the client for a different connection ID.
1439 // It should be ignored.
1440 TEST_P(EndToEndTest
, ClientSendPublicResetWithDifferentConnectionId
) {
1441 ASSERT_TRUE(Initialize());
1443 // Send the public reset.
1444 QuicConnectionId incorrect_connection_id
=
1445 client_
->client()->session()->connection()->connection_id() + 1;
1446 QuicPublicResetPacket header
;
1447 header
.public_header
.connection_id
= incorrect_connection_id
;
1448 header
.public_header
.reset_flag
= true;
1449 header
.public_header
.version_flag
= false;
1450 header
.rejected_sequence_number
= 10101;
1451 QuicFramer
framer(server_supported_versions_
, QuicTime::Zero(),
1452 Perspective::IS_CLIENT
);
1453 scoped_ptr
<QuicEncryptedPacket
> packet(framer
.BuildPublicResetPacket(header
));
1454 client_writer_
->WritePacket(packet
->data(), packet
->length(),
1455 client_
->client()->client_address().address(),
1458 // The connection should be unaffected.
1459 EXPECT_EQ(kFooResponseBody
, client_
->SendSynchronousRequest("/foo"));
1460 EXPECT_EQ(200u, client_
->response_headers()->parsed_response_code());
1463 // Send a version negotiation packet from the server for a different
1464 // connection ID. It should be ignored.
1465 TEST_P(EndToEndTest
, ServerSendVersionNegotiationWithDifferentConnectionId
) {
1466 ASSERT_TRUE(Initialize());
1468 // Send the version negotiation packet.
1469 QuicConnectionId incorrect_connection_id
=
1470 client_
->client()->session()->connection()->connection_id() + 1;
1471 QuicVersionNegotiationPacket header
;
1472 header
.connection_id
= incorrect_connection_id
;
1473 header
.reset_flag
= true;
1474 header
.version_flag
= true;
1475 QuicFramer
framer(server_supported_versions_
, QuicTime::Zero(),
1476 Perspective::IS_SERVER
);
1477 scoped_ptr
<QuicEncryptedPacket
> packet(
1478 framer
.BuildVersionNegotiationPacket(header
, server_supported_versions_
));
1479 testing::NiceMock
<MockQuicConnectionDebugVisitor
> visitor
;
1480 client_
->client()->session()->connection()->set_debug_visitor(&visitor
);
1481 EXPECT_CALL(visitor
, OnIncorrectConnectionId(incorrect_connection_id
))
1483 // We must pause the server's thread in order to call WritePacket without
1485 server_thread_
->Pause();
1486 server_writer_
->WritePacket(packet
->data(), packet
->length(),
1487 server_address_
.address(),
1488 client_
->client()->client_address());
1489 server_thread_
->Resume();
1491 // The connection should be unaffected.
1492 EXPECT_EQ(kFooResponseBody
, client_
->SendSynchronousRequest("/foo"));
1493 EXPECT_EQ(200u, client_
->response_headers()->parsed_response_code());
1495 client_
->client()->session()->connection()->set_debug_visitor(nullptr);
1498 // A bad header shouldn't tear down the connection, because the receiver can't
1499 // tell the connection ID.
1500 TEST_P(EndToEndTest
, BadPacketHeaderTruncated
) {
1501 ASSERT_TRUE(Initialize());
1503 // Start the connection.
1504 EXPECT_EQ(kFooResponseBody
, client_
->SendSynchronousRequest("/foo"));
1505 EXPECT_EQ(200u, client_
->response_headers()->parsed_response_code());
1507 // Packet with invalid public flags.
1508 char packet
[] = {// public flags (8 byte connection_id)
1510 // truncated connection ID
1512 client_writer_
->WritePacket(&packet
[0], sizeof(packet
),
1513 client_
->client()->client_address().address(),
1515 // Give the server time to process the packet.
1516 base::PlatformThread::Sleep(base::TimeDelta::FromMilliseconds(100));
1517 // Pause the server so we can access the server's internals without races.
1518 server_thread_
->Pause();
1519 QuicDispatcher
* dispatcher
=
1520 QuicServerPeer::GetDispatcher(server_thread_
->server());
1521 EXPECT_EQ(QUIC_INVALID_PACKET_HEADER
,
1522 QuicDispatcherPeer::GetAndClearLastError(dispatcher
));
1523 server_thread_
->Resume();
1525 // The connection should not be terminated.
1526 EXPECT_EQ(kFooResponseBody
, client_
->SendSynchronousRequest("/foo"));
1527 EXPECT_EQ(200u, client_
->response_headers()->parsed_response_code());
1530 // A bad header shouldn't tear down the connection, because the receiver can't
1531 // tell the connection ID.
1532 TEST_P(EndToEndTest
, BadPacketHeaderFlags
) {
1533 ASSERT_TRUE(Initialize());
1535 // Start the connection.
1536 EXPECT_EQ(kFooResponseBody
, client_
->SendSynchronousRequest("/foo"));
1537 EXPECT_EQ(200u, client_
->response_headers()->parsed_response_code());
1539 // Packet with invalid public flags.
1541 // invalid public flags
1552 // packet sequence number
1562 client_writer_
->WritePacket(&packet
[0], sizeof(packet
),
1563 client_
->client()->client_address().address(),
1565 // Give the server time to process the packet.
1566 base::PlatformThread::Sleep(base::TimeDelta::FromMilliseconds(100));
1567 // Pause the server so we can access the server's internals without races.
1568 server_thread_
->Pause();
1569 QuicDispatcher
* dispatcher
=
1570 QuicServerPeer::GetDispatcher(server_thread_
->server());
1571 EXPECT_EQ(QUIC_INVALID_PACKET_HEADER
,
1572 QuicDispatcherPeer::GetAndClearLastError(dispatcher
));
1573 server_thread_
->Resume();
1575 // The connection should not be terminated.
1576 EXPECT_EQ(kFooResponseBody
, client_
->SendSynchronousRequest("/foo"));
1577 EXPECT_EQ(200u, client_
->response_headers()->parsed_response_code());
1580 // Send a packet from the client with bad encrypted data. The server should not
1581 // tear down the connection.
1582 TEST_P(EndToEndTest
, BadEncryptedData
) {
1583 ASSERT_TRUE(Initialize());
1585 // Start the connection.
1586 EXPECT_EQ(kFooResponseBody
, client_
->SendSynchronousRequest("/foo"));
1587 EXPECT_EQ(200u, client_
->response_headers()->parsed_response_code());
1589 scoped_ptr
<QuicEncryptedPacket
> packet(ConstructEncryptedPacket(
1590 client_
->client()->session()->connection()->connection_id(), false, false,
1591 1, "At least 20 characters.", PACKET_8BYTE_CONNECTION_ID
,
1592 PACKET_6BYTE_SEQUENCE_NUMBER
));
1593 // Damage the encrypted data.
1594 string
damaged_packet(packet
->data(), packet
->length());
1595 damaged_packet
[30] ^= 0x01;
1596 DVLOG(1) << "Sending bad packet.";
1597 client_writer_
->WritePacket(damaged_packet
.data(), damaged_packet
.length(),
1598 client_
->client()->client_address().address(),
1600 // Give the server time to process the packet.
1601 base::PlatformThread::Sleep(base::TimeDelta::FromMilliseconds(100));
1602 // This error is sent to the connection's OnError (which ignores it), so the
1603 // dispatcher doesn't see it.
1604 // Pause the server so we can access the server's internals without races.
1605 server_thread_
->Pause();
1606 QuicDispatcher
* dispatcher
=
1607 QuicServerPeer::GetDispatcher(server_thread_
->server());
1608 EXPECT_EQ(QUIC_NO_ERROR
,
1609 QuicDispatcherPeer::GetAndClearLastError(dispatcher
));
1610 server_thread_
->Resume();
1612 // The connection should not be terminated.
1613 EXPECT_EQ(kFooResponseBody
, client_
->SendSynchronousRequest("/foo"));
1614 EXPECT_EQ(200u, client_
->response_headers()->parsed_response_code());
1619 } // namespace tools