Call InvalidationController#refreshRegisteredTypes() on Chrome startup
[chromium-blink-merge.git] / net / tools / quic / end_to_end_test.cc
blob430dbe03944e039586b5f9d315268c3604160ace
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.
5 #include <stddef.h>
6 #include <string>
7 #include <sys/epoll.h>
8 #include <vector>
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_test_utils.h"
32 #include "net/quic/test_tools/reliable_quic_stream_peer.h"
33 #include "net/test/gtest_util.h"
34 #include "net/tools/epoll_server/epoll_server.h"
35 #include "net/tools/quic/quic_epoll_connection_helper.h"
36 #include "net/tools/quic/quic_in_memory_cache.h"
37 #include "net/tools/quic/quic_packet_writer_wrapper.h"
38 #include "net/tools/quic/quic_server.h"
39 #include "net/tools/quic/quic_socket_utils.h"
40 #include "net/tools/quic/quic_spdy_client_stream.h"
41 #include "net/tools/quic/test_tools/http_message.h"
42 #include "net/tools/quic/test_tools/packet_dropping_test_writer.h"
43 #include "net/tools/quic/test_tools/quic_client_peer.h"
44 #include "net/tools/quic/test_tools/quic_dispatcher_peer.h"
45 #include "net/tools/quic/test_tools/quic_in_memory_cache_peer.h"
46 #include "net/tools/quic/test_tools/quic_server_peer.h"
47 #include "net/tools/quic/test_tools/quic_test_client.h"
48 #include "net/tools/quic/test_tools/server_thread.h"
49 #include "testing/gtest/include/gtest/gtest.h"
51 using base::StringPiece;
52 using base::WaitableEvent;
53 using net::EpollServer;
54 using net::test::ConstructEncryptedPacket;
55 using net::test::GenerateBody;
56 using net::test::MockQuicConnectionDebugVisitor;
57 using net::test::QuicConnectionPeer;
58 using net::test::QuicFlowControllerPeer;
59 using net::test::QuicSentPacketManagerPeer;
60 using net::test::QuicSessionPeer;
61 using net::test::ReliableQuicStreamPeer;
62 using net::test::ValueRestore;
63 using net::test::kClientDataStreamId1;
64 using net::tools::test::PacketDroppingTestWriter;
65 using net::tools::test::QuicDispatcherPeer;
66 using net::tools::test::QuicServerPeer;
67 using std::ostream;
68 using std::string;
69 using std::vector;
71 namespace net {
72 namespace tools {
73 namespace test {
74 namespace {
76 const char kFooResponseBody[] = "Artichoke hearts make me happy.";
77 const char kBarResponseBody[] = "Palm hearts are pretty delicious, also.";
79 // Run all tests with the cross products of all versions.
80 struct TestParams {
81 TestParams(const QuicVersionVector& client_supported_versions,
82 const QuicVersionVector& server_supported_versions,
83 QuicVersion negotiated_version,
84 bool use_fec,
85 QuicTag congestion_control_tag)
86 : client_supported_versions(client_supported_versions),
87 server_supported_versions(server_supported_versions),
88 negotiated_version(negotiated_version),
89 use_fec(use_fec),
90 congestion_control_tag(congestion_control_tag) {
93 friend ostream& operator<<(ostream& os, const TestParams& p) {
94 os << "{ server_supported_versions: "
95 << QuicVersionVectorToString(p.server_supported_versions);
96 os << " client_supported_versions: "
97 << QuicVersionVectorToString(p.client_supported_versions);
98 os << " negotiated_version: " << QuicVersionToString(p.negotiated_version);
99 os << " use_fec: " << p.use_fec;
100 os << " congestion_control_tag: "
101 << QuicUtils::TagToString(p.congestion_control_tag) << " }";
102 return os;
105 QuicVersionVector client_supported_versions;
106 QuicVersionVector server_supported_versions;
107 QuicVersion negotiated_version;
108 bool use_fec;
109 QuicTag congestion_control_tag;
112 // Constructs various test permutations.
113 vector<TestParams> GetTestParams() {
114 // Divide the versions into buckets in which the intra-frame format
115 // is compatible. When clients encounter QUIC version negotiation
116 // they simply retransmit all packets using the new version's
117 // QUIC framing. However, they are unable to change the intra-frame
118 // layout (for example to change SPDY/4 headers to SPDY/3). So
119 // these tests need to ensure that clients are never attempting
120 // to do 0-RTT across incompatible versions. Chromium only supports
121 // a single version at a time anyway. :)
122 QuicVersionVector all_supported_versions = QuicSupportedVersions();
123 QuicVersionVector client_version_buckets[2];
124 for (const QuicVersion version : all_supported_versions) {
125 if (version <= QUIC_VERSION_24) {
126 // SPDY/4 compression but SPDY/3 headers
127 client_version_buckets[0].push_back(version);
128 } else {
129 // SPDY/4
130 client_version_buckets[1].push_back(version);
134 vector<TestParams> params;
135 // TODO(rtenneti): Add kTBBR after BBR code is checked in.
136 // for (const QuicTag congestion_control_tag : {kRENO, kTBBR, kQBIC}) {
137 for (const QuicTag congestion_control_tag : {kRENO, kQBIC}) {
138 for (const bool use_fec : {false, true}) {
139 for (const QuicVersionVector& client_versions : client_version_buckets) {
140 CHECK(!client_versions.empty());
141 // Add an entry for server and client supporting all versions.
142 params.push_back(TestParams(client_versions, all_supported_versions,
143 client_versions.front(), use_fec != 0,
144 congestion_control_tag));
146 // Test client supporting all versions and server supporting 1
147 // version. Simulate an old server and exercise version downgrade in
148 // the client. Protocol negotiation should occur. Skip the i = 0 case
149 // because it is essentially the same as the default case.
150 for (const QuicVersion version : client_versions) {
151 QuicVersionVector server_supported_versions;
152 server_supported_versions.push_back(version);
153 params.push_back(TestParams(client_versions,
154 server_supported_versions,
155 server_supported_versions.front(),
156 use_fec != 0, congestion_control_tag));
161 return params;
164 class ServerDelegate : public PacketDroppingTestWriter::Delegate {
165 public:
166 ServerDelegate(TestWriterFactory* writer_factory,
167 QuicDispatcher* dispatcher)
168 : writer_factory_(writer_factory),
169 dispatcher_(dispatcher) {}
170 ~ServerDelegate() override {}
171 void OnPacketSent(WriteResult result) override {
172 writer_factory_->OnPacketSent(result);
174 void OnCanWrite() override { dispatcher_->OnCanWrite(); }
176 private:
177 TestWriterFactory* writer_factory_;
178 QuicDispatcher* dispatcher_;
181 class ClientDelegate : public PacketDroppingTestWriter::Delegate {
182 public:
183 explicit ClientDelegate(QuicClient* client) : client_(client) {}
184 ~ClientDelegate() override {}
185 void OnPacketSent(WriteResult result) override {}
186 void OnCanWrite() override {
187 EpollEvent event(EPOLLOUT, false);
188 client_->OnEvent(client_->fd(), &event);
191 private:
192 QuicClient* client_;
195 class EndToEndTest : public ::testing::TestWithParam<TestParams> {
196 protected:
197 EndToEndTest()
198 : server_hostname_("example.com"),
199 server_started_(false),
200 strike_register_no_startup_period_(false) {
201 net::IPAddressNumber ip;
202 CHECK(net::ParseIPLiteralToNumber("127.0.0.1", &ip));
203 server_address_ = IPEndPoint(ip, 0);
205 client_supported_versions_ = GetParam().client_supported_versions;
206 server_supported_versions_ = GetParam().server_supported_versions;
207 negotiated_version_ = GetParam().negotiated_version;
208 FLAGS_enable_quic_fec = GetParam().use_fec;
210 VLOG(1) << "Using Configuration: " << GetParam();
212 // Use different flow control windows for client/server.
213 client_config_.SetInitialStreamFlowControlWindowToSend(
214 2 * kInitialStreamFlowControlWindowForTest);
215 client_config_.SetInitialSessionFlowControlWindowToSend(
216 2 * kInitialSessionFlowControlWindowForTest);
217 server_config_.SetInitialStreamFlowControlWindowToSend(
218 3 * kInitialStreamFlowControlWindowForTest);
219 server_config_.SetInitialSessionFlowControlWindowToSend(
220 3 * kInitialSessionFlowControlWindowForTest);
222 QuicInMemoryCachePeer::ResetForTests();
223 AddToCache("/foo", 200, "OK", kFooResponseBody);
224 AddToCache("/bar", 200, "OK", kBarResponseBody);
227 ~EndToEndTest() override {
228 // TODO(rtenneti): port RecycleUnusedPort if needed.
229 // RecycleUnusedPort(server_address_.port());
230 QuicInMemoryCachePeer::ResetForTests();
233 QuicTestClient* CreateQuicClient(QuicPacketWriterWrapper* writer) {
234 QuicTestClient* client = new QuicTestClient(
235 server_address_,
236 server_hostname_,
237 false, // not secure
238 client_config_,
239 client_supported_versions_);
240 client->UseWriter(writer);
241 client->Connect();
242 return client;
245 void set_client_initial_stream_flow_control_receive_window(uint32 window) {
246 CHECK(client_.get() == nullptr);
247 DVLOG(1) << "Setting client initial stream flow control window: " << window;
248 client_config_.SetInitialStreamFlowControlWindowToSend(window);
251 void set_client_initial_session_flow_control_receive_window(uint32 window) {
252 CHECK(client_.get() == nullptr);
253 DVLOG(1) << "Setting client initial session flow control window: "
254 << window;
255 client_config_.SetInitialSessionFlowControlWindowToSend(window);
258 void set_server_initial_stream_flow_control_receive_window(uint32 window) {
259 CHECK(server_thread_.get() == nullptr);
260 DVLOG(1) << "Setting server initial stream flow control window: "
261 << window;
262 server_config_.SetInitialStreamFlowControlWindowToSend(window);
265 void set_server_initial_session_flow_control_receive_window(uint32 window) {
266 CHECK(server_thread_.get() == nullptr);
267 DVLOG(1) << "Setting server initial session flow control window: "
268 << window;
269 server_config_.SetInitialSessionFlowControlWindowToSend(window);
272 const QuicSentPacketManager *
273 GetSentPacketManagerFromFirstServerSession() const {
274 QuicDispatcher* dispatcher =
275 QuicServerPeer::GetDispatcher(server_thread_->server());
276 QuicSession* session = dispatcher->session_map().begin()->second;
277 return &session->connection()->sent_packet_manager();
280 bool Initialize() {
281 QuicTagVector copt;
282 server_config_.SetConnectionOptionsToSend(copt);
284 // TODO(nimia): Consider setting the congestion control algorithm for the
285 // client as well according to the test parameter.
286 copt.push_back(GetParam().congestion_control_tag);
288 if (GetParam().use_fec) {
289 // Set FEC config in client's connection options and in client session.
290 copt.push_back(kFHDR);
293 client_config_.SetConnectionOptionsToSend(copt);
295 // Start the server first, because CreateQuicClient() attempts
296 // to connect to the server.
297 StartServer();
298 client_.reset(CreateQuicClient(client_writer_));
299 if (GetParam().use_fec) {
300 // Set FecPolicy to always protect data on all streams.
301 client_->SetFecPolicy(FEC_PROTECT_ALWAYS);
303 static EpollEvent event(EPOLLOUT, false);
304 client_writer_->Initialize(
305 reinterpret_cast<QuicEpollConnectionHelper*>(
306 QuicConnectionPeer::GetHelper(
307 client_->client()->session()->connection())),
308 new ClientDelegate(client_->client()));
309 return client_->client()->connected();
312 void SetUp() override {
313 // The ownership of these gets transferred to the QuicPacketWriterWrapper
314 // and TestWriterFactory when Initialize() is executed.
315 client_writer_ = new PacketDroppingTestWriter();
316 server_writer_ = new PacketDroppingTestWriter();
319 void TearDown() override { StopServer(); }
321 void StartServer() {
322 server_thread_.reset(
323 new ServerThread(
324 new QuicServer(server_config_, server_supported_versions_),
325 server_address_,
326 strike_register_no_startup_period_));
327 server_thread_->Initialize();
328 server_address_ = IPEndPoint(server_address_.address(),
329 server_thread_->GetPort());
330 QuicDispatcher* dispatcher =
331 QuicServerPeer::GetDispatcher(server_thread_->server());
332 TestWriterFactory* packet_writer_factory = new TestWriterFactory();
333 QuicDispatcherPeer::SetPacketWriterFactory(dispatcher,
334 packet_writer_factory);
335 QuicDispatcherPeer::UseWriter(dispatcher, server_writer_);
336 server_writer_->Initialize(
337 QuicDispatcherPeer::GetHelper(dispatcher),
338 new ServerDelegate(packet_writer_factory, dispatcher));
339 server_thread_->Start();
340 server_started_ = true;
343 void StopServer() {
344 if (!server_started_)
345 return;
346 if (server_thread_.get()) {
347 server_thread_->Quit();
348 server_thread_->Join();
352 void AddToCache(StringPiece path,
353 int response_code,
354 StringPiece response_detail,
355 StringPiece body) {
356 QuicInMemoryCache::GetInstance()->AddSimpleResponse(
357 "www.google.com", path, response_code, response_detail, body);
360 void SetPacketLossPercentage(int32 loss) {
361 // TODO(rtenneti): enable when we can do random packet loss tests in
362 // chrome's tree.
363 if (loss != 0 && loss != 100)
364 return;
365 client_writer_->set_fake_packet_loss_percentage(loss);
366 server_writer_->set_fake_packet_loss_percentage(loss);
369 void SetPacketSendDelay(QuicTime::Delta delay) {
370 // TODO(rtenneti): enable when we can do random packet send delay tests in
371 // chrome's tree.
372 // client_writer_->set_fake_packet_delay(delay);
373 // server_writer_->set_fake_packet_delay(delay);
376 void SetReorderPercentage(int32 reorder) {
377 // TODO(rtenneti): enable when we can do random packet reorder tests in
378 // chrome's tree.
379 // client_writer_->set_fake_reorder_percentage(reorder);
380 // server_writer_->set_fake_reorder_percentage(reorder);
383 // Verifies that the client and server connections were both free of packets
384 // being discarded, based on connection stats.
385 // Calls server_thread_ Pause() and Resume(), which may only be called once
386 // per test.
387 void VerifyCleanConnection(bool had_packet_loss) {
388 QuicConnectionStats client_stats =
389 client_->client()->session()->connection()->GetStats();
390 // TODO(ianswett): Re-enable this check once b/19572432 is fixed.
391 // if (!had_packet_loss) {
392 // EXPECT_EQ(0u, client_stats.packets_lost);
393 // }
394 EXPECT_EQ(0u, client_stats.packets_discarded);
395 EXPECT_EQ(0u, client_stats.packets_dropped);
396 EXPECT_EQ(client_stats.packets_received, client_stats.packets_processed);
398 server_thread_->Pause();
399 QuicDispatcher* dispatcher =
400 QuicServerPeer::GetDispatcher(server_thread_->server());
401 ASSERT_EQ(1u, dispatcher->session_map().size());
402 QuicSession* session = dispatcher->session_map().begin()->second;
403 QuicConnectionStats server_stats = session->connection()->GetStats();
404 // TODO(ianswett): Re-enable this check once b/19572432 is fixed.
405 // if (!had_packet_loss) {
406 // EXPECT_EQ(0u, server_stats.packets_lost);
407 // }
408 EXPECT_EQ(0u, server_stats.packets_discarded);
409 // TODO(ianswett): Restore the check for packets_dropped equals 0.
410 // The expect for packets received is equal to packets processed fails
411 // due to version negotiation packets.
412 server_thread_->Resume();
415 IPEndPoint server_address_;
416 string server_hostname_;
417 scoped_ptr<ServerThread> server_thread_;
418 scoped_ptr<QuicTestClient> client_;
419 PacketDroppingTestWriter* client_writer_;
420 PacketDroppingTestWriter* server_writer_;
421 bool server_started_;
422 QuicConfig client_config_;
423 QuicConfig server_config_;
424 QuicVersionVector client_supported_versions_;
425 QuicVersionVector server_supported_versions_;
426 QuicVersion negotiated_version_;
427 bool strike_register_no_startup_period_;
430 // Run all end to end tests with all supported versions.
431 INSTANTIATE_TEST_CASE_P(EndToEndTests,
432 EndToEndTest,
433 ::testing::ValuesIn(GetTestParams()));
435 TEST_P(EndToEndTest, SimpleRequestResponse) {
436 ASSERT_TRUE(Initialize());
438 EXPECT_EQ(kFooResponseBody, client_->SendSynchronousRequest("/foo"));
439 EXPECT_EQ(200u, client_->response_headers()->parsed_response_code());
442 // TODO(rch): figure out how to detect missing v6 supprt (like on the linux
443 // try bots) and selectively disable this test.
444 TEST_P(EndToEndTest, DISABLED_SimpleRequestResponsev6) {
445 IPAddressNumber ip;
446 CHECK(net::ParseIPLiteralToNumber("::1", &ip));
447 server_address_ = IPEndPoint(ip, server_address_.port());
448 ASSERT_TRUE(Initialize());
450 EXPECT_EQ(kFooResponseBody, client_->SendSynchronousRequest("/foo"));
451 EXPECT_EQ(200u, client_->response_headers()->parsed_response_code());
454 TEST_P(EndToEndTest, SeparateFinPacket) {
455 ASSERT_TRUE(Initialize());
457 HTTPMessage request(HttpConstants::HTTP_1_1,
458 HttpConstants::POST, "/foo");
459 request.set_has_complete_message(false);
461 // Send a request in two parts: the request and then an empty packet with FIN.
462 client_->SendMessage(request);
463 client_->SendData("", true);
464 client_->WaitForResponse();
465 EXPECT_EQ(kFooResponseBody, client_->response_body());
466 EXPECT_EQ(200u, client_->response_headers()->parsed_response_code());
468 // Now do the same thing but with a content length.
469 request.AddBody("foo", true);
470 client_->SendMessage(request);
471 client_->SendData("", true);
472 client_->WaitForResponse();
473 EXPECT_EQ(kFooResponseBody, client_->response_body());
474 EXPECT_EQ(200u, client_->response_headers()->parsed_response_code());
477 TEST_P(EndToEndTest, MultipleRequestResponse) {
478 ASSERT_TRUE(Initialize());
480 EXPECT_EQ(kFooResponseBody, client_->SendSynchronousRequest("/foo"));
481 EXPECT_EQ(200u, client_->response_headers()->parsed_response_code());
482 EXPECT_EQ(kBarResponseBody, client_->SendSynchronousRequest("/bar"));
483 EXPECT_EQ(200u, client_->response_headers()->parsed_response_code());
486 TEST_P(EndToEndTest, MultipleClients) {
487 ASSERT_TRUE(Initialize());
488 scoped_ptr<QuicTestClient> client2(CreateQuicClient(nullptr));
490 HTTPMessage request(HttpConstants::HTTP_1_1,
491 HttpConstants::POST, "/foo");
492 request.AddHeader("content-length", "3");
493 request.set_has_complete_message(false);
495 client_->SendMessage(request);
496 client2->SendMessage(request);
498 client_->SendData("bar", true);
499 client_->WaitForResponse();
500 EXPECT_EQ(kFooResponseBody, client_->response_body());
501 EXPECT_EQ(200u, client_->response_headers()->parsed_response_code());
503 client2->SendData("eep", true);
504 client2->WaitForResponse();
505 EXPECT_EQ(kFooResponseBody, client2->response_body());
506 EXPECT_EQ(200u, client2->response_headers()->parsed_response_code());
509 TEST_P(EndToEndTest, RequestOverMultiplePackets) {
510 // Send a large enough request to guarantee fragmentation.
511 string huge_request = "/some/path?query=" + string(kMaxPacketSize, '.');
512 AddToCache(huge_request, 200, "OK", kBarResponseBody);
514 ASSERT_TRUE(Initialize());
516 EXPECT_EQ(kBarResponseBody, client_->SendSynchronousRequest(huge_request));
517 EXPECT_EQ(200u, client_->response_headers()->parsed_response_code());
520 TEST_P(EndToEndTest, MultiplePacketsRandomOrder) {
521 // Send a large enough request to guarantee fragmentation.
522 string huge_request = "/some/path?query=" + string(kMaxPacketSize, '.');
523 AddToCache(huge_request, 200, "OK", kBarResponseBody);
525 ASSERT_TRUE(Initialize());
526 SetPacketSendDelay(QuicTime::Delta::FromMilliseconds(2));
527 SetReorderPercentage(50);
529 EXPECT_EQ(kBarResponseBody, client_->SendSynchronousRequest(huge_request));
530 EXPECT_EQ(200u, client_->response_headers()->parsed_response_code());
533 TEST_P(EndToEndTest, PostMissingBytes) {
534 ASSERT_TRUE(Initialize());
536 // Add a content length header with no body.
537 HTTPMessage request(HttpConstants::HTTP_1_1,
538 HttpConstants::POST, "/foo");
539 request.AddHeader("content-length", "3");
540 request.set_skip_message_validation(true);
542 // This should be detected as stream fin without complete request,
543 // triggering an error response.
544 client_->SendCustomSynchronousRequest(request);
545 EXPECT_EQ("bad", client_->response_body());
546 EXPECT_EQ(500u, client_->response_headers()->parsed_response_code());
549 // TODO(rtenneti): DISABLED_LargePostNoPacketLoss seems to be flaky.
550 // http://crbug.com/297040.
551 TEST_P(EndToEndTest, DISABLED_LargePostNoPacketLoss) {
552 ASSERT_TRUE(Initialize());
554 client_->client()->WaitForCryptoHandshakeConfirmed();
556 // 1 MB body.
557 string body;
558 GenerateBody(&body, 1024 * 1024);
560 HTTPMessage request(HttpConstants::HTTP_1_1,
561 HttpConstants::POST, "/foo");
562 request.AddBody(body, true);
564 EXPECT_EQ(kFooResponseBody, client_->SendCustomSynchronousRequest(request));
565 VerifyCleanConnection(false);
568 TEST_P(EndToEndTest, LargePostNoPacketLoss1sRTT) {
569 ASSERT_TRUE(Initialize());
570 SetPacketSendDelay(QuicTime::Delta::FromMilliseconds(1000));
572 client_->client()->WaitForCryptoHandshakeConfirmed();
574 // 100 KB body.
575 string body;
576 GenerateBody(&body, 100 * 1024);
578 HTTPMessage request(HttpConstants::HTTP_1_1,
579 HttpConstants::POST, "/foo");
580 request.AddBody(body, true);
582 EXPECT_EQ(kFooResponseBody, client_->SendCustomSynchronousRequest(request));
583 VerifyCleanConnection(false);
586 TEST_P(EndToEndTest, LargePostWithPacketLoss) {
587 // Connect with lower fake packet loss than we'd like to test. Until
588 // b/10126687 is fixed, losing handshake packets is pretty brutal.
589 SetPacketLossPercentage(5);
590 ASSERT_TRUE(Initialize());
592 // Wait for the server SHLO before upping the packet loss.
593 client_->client()->WaitForCryptoHandshakeConfirmed();
594 SetPacketLossPercentage(30);
596 // 10 KB body.
597 string body;
598 GenerateBody(&body, 1024 * 10);
600 HTTPMessage request(HttpConstants::HTTP_1_1,
601 HttpConstants::POST, "/foo");
602 request.AddBody(body, true);
604 EXPECT_EQ(kFooResponseBody, client_->SendCustomSynchronousRequest(request));
605 VerifyCleanConnection(true);
608 TEST_P(EndToEndTest, LargePostWithPacketLossAndBlockedSocket) {
609 // Connect with lower fake packet loss than we'd like to test. Until
610 // b/10126687 is fixed, losing handshake packets is pretty brutal.
611 SetPacketLossPercentage(5);
612 ASSERT_TRUE(Initialize());
614 // Wait for the server SHLO before upping the packet loss.
615 client_->client()->WaitForCryptoHandshakeConfirmed();
616 SetPacketLossPercentage(10);
617 client_writer_->set_fake_blocked_socket_percentage(10);
619 // 10 KB body.
620 string body;
621 GenerateBody(&body, 1024 * 10);
623 HTTPMessage request(HttpConstants::HTTP_1_1,
624 HttpConstants::POST, "/foo");
625 request.AddBody(body, true);
627 EXPECT_EQ(kFooResponseBody, client_->SendCustomSynchronousRequest(request));
630 TEST_P(EndToEndTest, LargePostNoPacketLossWithDelayAndReordering) {
631 ASSERT_TRUE(Initialize());
633 client_->client()->WaitForCryptoHandshakeConfirmed();
634 // Both of these must be called when the writer is not actively used.
635 SetPacketSendDelay(QuicTime::Delta::FromMilliseconds(2));
636 SetReorderPercentage(30);
638 // 1 MB body.
639 string body;
640 GenerateBody(&body, 1024 * 1024);
642 HTTPMessage request(HttpConstants::HTTP_1_1,
643 HttpConstants::POST, "/foo");
644 request.AddBody(body, true);
646 EXPECT_EQ(kFooResponseBody, client_->SendCustomSynchronousRequest(request));
649 TEST_P(EndToEndTest, LargePostZeroRTTFailure) {
650 // Have the server accept 0-RTT without waiting a startup period.
651 strike_register_no_startup_period_ = true;
653 // Send a request and then disconnect. This prepares the client to attempt
654 // a 0-RTT handshake for the next request.
655 ASSERT_TRUE(Initialize());
657 string body;
658 GenerateBody(&body, 20480);
660 HTTPMessage request(HttpConstants::HTTP_1_1, HttpConstants::POST, "/foo");
661 request.AddBody(body, true);
663 EXPECT_EQ(kFooResponseBody, client_->SendCustomSynchronousRequest(request));
664 EXPECT_EQ(2, client_->client()->session()->GetNumSentClientHellos());
666 client_->Disconnect();
668 // The 0-RTT handshake should succeed.
669 client_->Connect();
670 client_->WaitForResponseForMs(-1);
671 ASSERT_TRUE(client_->client()->connected());
672 EXPECT_EQ(kFooResponseBody, client_->SendCustomSynchronousRequest(request));
673 EXPECT_EQ(1, client_->client()->session()->GetNumSentClientHellos());
675 client_->Disconnect();
677 // Restart the server so that the 0-RTT handshake will take 1 RTT.
678 StopServer();
679 server_writer_ = new PacketDroppingTestWriter();
680 StartServer();
682 client_->Connect();
683 ASSERT_TRUE(client_->client()->connected());
684 EXPECT_EQ(kFooResponseBody, client_->SendCustomSynchronousRequest(request));
685 EXPECT_EQ(2, client_->client()->session()->GetNumSentClientHellos());
686 VerifyCleanConnection(false);
689 TEST_P(EndToEndTest, CorrectlyConfiguredFec) {
690 ASSERT_TRUE(Initialize());
691 client_->client()->WaitForCryptoHandshakeConfirmed();
692 server_thread_->WaitForCryptoHandshakeConfirmed();
694 FecPolicy expected_policy =
695 GetParam().use_fec ? FEC_PROTECT_ALWAYS : FEC_PROTECT_OPTIONAL;
697 // Verify that server's FEC configuration is correct.
698 server_thread_->Pause();
699 QuicDispatcher* dispatcher =
700 QuicServerPeer::GetDispatcher(server_thread_->server());
701 ASSERT_EQ(1u, dispatcher->session_map().size());
702 QuicSession* session = dispatcher->session_map().begin()->second;
703 EXPECT_EQ(expected_policy,
704 QuicSessionPeer::GetHeadersStream(session)->fec_policy());
705 server_thread_->Resume();
707 // Verify that client's FEC configuration is correct.
708 EXPECT_EQ(expected_policy,
709 QuicSessionPeer::GetHeadersStream(
710 client_->client()->session())->fec_policy());
711 EXPECT_EQ(expected_policy,
712 client_->GetOrCreateStream()->fec_policy());
715 TEST_P(EndToEndTest, LargePostSmallBandwidthLargeBuffer) {
716 ASSERT_TRUE(Initialize());
717 SetPacketSendDelay(QuicTime::Delta::FromMicroseconds(1));
718 // 256KB per second with a 256KB buffer from server to client. Wireless
719 // clients commonly have larger buffers, but our max CWND is 200.
720 server_writer_->set_max_bandwidth_and_buffer_size(
721 QuicBandwidth::FromBytesPerSecond(256 * 1024), 256 * 1024);
723 client_->client()->WaitForCryptoHandshakeConfirmed();
725 // 1 MB body.
726 string body;
727 GenerateBody(&body, 1024 * 1024);
729 HTTPMessage request(HttpConstants::HTTP_1_1, HttpConstants::POST, "/foo");
730 request.AddBody(body, true);
732 EXPECT_EQ(kFooResponseBody, client_->SendCustomSynchronousRequest(request));
733 // This connection will not drop packets, because the buffer size is larger
734 // than the default receive window.
735 VerifyCleanConnection(false);
738 TEST_P(EndToEndTest, DoNotSetResumeWriteAlarmIfConnectionFlowControlBlocked) {
739 // Regression test for b/14677858.
740 // Test that the resume write alarm is not set in QuicConnection::OnCanWrite
741 // if currently connection level flow control blocked. If set, this results in
742 // an infinite loop in the EpollServer, as the alarm fires and is immediately
743 // rescheduled.
744 ASSERT_TRUE(Initialize());
745 client_->client()->WaitForCryptoHandshakeConfirmed();
747 // Ensure both stream and connection level are flow control blocked by setting
748 // the send window offset to 0.
749 const uint64 flow_control_window =
750 server_config_.GetInitialStreamFlowControlWindowToSend();
751 QuicSpdyClientStream* stream = client_->GetOrCreateStream();
752 QuicSession* session = client_->client()->session();
753 QuicFlowControllerPeer::SetSendWindowOffset(stream->flow_controller(), 0);
754 QuicFlowControllerPeer::SetSendWindowOffset(session->flow_controller(), 0);
755 EXPECT_TRUE(stream->flow_controller()->IsBlocked());
756 EXPECT_TRUE(session->flow_controller()->IsBlocked());
758 // Make sure that the stream has data pending so that it will be marked as
759 // write blocked when it receives a stream level WINDOW_UPDATE.
760 stream->SendBody("hello", false);
762 // The stream now attempts to write, fails because it is still connection
763 // level flow control blocked, and is added to the write blocked list.
764 QuicWindowUpdateFrame window_update(stream->id(), 2 * flow_control_window);
765 stream->OnWindowUpdateFrame(window_update);
767 // Prior to fixing b/14677858 this call would result in an infinite loop in
768 // Chromium. As a proxy for detecting this, we now check whether the
769 // resume_writes_alarm is set after OnCanWrite. It should not be, as the
770 // connection is still flow control blocked.
771 session->connection()->OnCanWrite();
773 QuicAlarm* resume_writes_alarm =
774 QuicConnectionPeer::GetResumeWritesAlarm(session->connection());
775 EXPECT_FALSE(resume_writes_alarm->IsSet());
778 TEST_P(EndToEndTest, InvalidStream) {
779 ASSERT_TRUE(Initialize());
780 client_->client()->WaitForCryptoHandshakeConfirmed();
782 string body;
783 GenerateBody(&body, kMaxPacketSize);
785 HTTPMessage request(HttpConstants::HTTP_1_1, HttpConstants::POST, "/foo");
786 request.AddBody(body, true);
787 // Force the client to write with a stream ID belonging to a nonexistent
788 // server-side stream.
789 QuicSessionPeer::SetNextStreamId(client_->client()->session(), 2);
791 client_->SendCustomSynchronousRequest(request);
792 // EXPECT_EQ(QUIC_STREAM_CONNECTION_ERROR, client_->stream_error());
793 EXPECT_EQ(QUIC_PACKET_FOR_NONEXISTENT_STREAM, client_->connection_error());
796 // TODO(rch): this test seems to cause net_unittests timeouts :|
797 TEST_P(EndToEndTest, DISABLED_MultipleTermination) {
798 ASSERT_TRUE(Initialize());
800 HTTPMessage request(HttpConstants::HTTP_1_1,
801 HttpConstants::POST, "/foo");
802 request.AddHeader("content-length", "3");
803 request.set_has_complete_message(false);
805 // Set the offset so we won't frame. Otherwise when we pick up termination
806 // before HTTP framing is complete, we send an error and close the stream,
807 // and the second write is picked up as writing on a closed stream.
808 QuicSpdyClientStream* stream = client_->GetOrCreateStream();
809 ASSERT_TRUE(stream != nullptr);
810 ReliableQuicStreamPeer::SetStreamBytesWritten(3, stream);
812 client_->SendData("bar", true);
813 client_->WaitForWriteToFlush();
815 // By default the stream protects itself from writes after terminte is set.
816 // Override this to test the server handling buggy clients.
817 ReliableQuicStreamPeer::SetWriteSideClosed(
818 false, client_->GetOrCreateStream());
820 EXPECT_DFATAL(client_->SendData("eep", true), "Fin already buffered");
823 TEST_P(EndToEndTest, Timeout) {
824 client_config_.SetIdleConnectionStateLifetime(
825 QuicTime::Delta::FromMicroseconds(500),
826 QuicTime::Delta::FromMicroseconds(500));
827 // Note: we do NOT ASSERT_TRUE: we may time out during initial handshake:
828 // that's enough to validate timeout in this case.
829 Initialize();
830 while (client_->client()->connected()) {
831 client_->client()->WaitForEvents();
835 TEST_P(EndToEndTest, NegotiateMaxOpenStreams) {
836 // Negotiate 1 max open stream.
837 client_config_.SetMaxStreamsPerConnection(1, 1);
838 ASSERT_TRUE(Initialize());
839 client_->client()->WaitForCryptoHandshakeConfirmed();
841 // Make the client misbehave after negotiation.
842 const int kServerMaxStreams = kMaxStreamsMinimumIncrement + 1;
843 QuicSessionPeer::SetMaxOpenStreams(client_->client()->session(),
844 kServerMaxStreams + 1);
846 HTTPMessage request(HttpConstants::HTTP_1_1, HttpConstants::POST, "/foo");
847 request.AddHeader("content-length", "3");
848 request.set_has_complete_message(false);
850 // The server supports a small number of additional streams beyond the
851 // negotiated limit. Open enough streams to go beyond that limit.
852 for (int i = 0; i < kServerMaxStreams + 1; ++i) {
853 client_->SendMessage(request);
855 client_->WaitForResponse();
857 EXPECT_FALSE(client_->connected());
858 EXPECT_EQ(QUIC_STREAM_CONNECTION_ERROR, client_->stream_error());
859 EXPECT_EQ(QUIC_TOO_MANY_OPEN_STREAMS, client_->connection_error());
862 TEST_P(EndToEndTest, NegotiateCongestionControl) {
863 ValueRestore<bool> old_flag(&FLAGS_quic_allow_bbr, true);
864 ASSERT_TRUE(Initialize());
865 client_->client()->WaitForCryptoHandshakeConfirmed();
867 CongestionControlType expected_congestion_control_type = kReno;
868 switch (GetParam().congestion_control_tag) {
869 case kRENO:
870 expected_congestion_control_type = kReno;
871 break;
872 case kTBBR:
873 expected_congestion_control_type = kBBR;
874 break;
875 case kQBIC:
876 expected_congestion_control_type = kCubic;
877 break;
878 default:
879 DLOG(FATAL) << "Unexpected congestion control tag";
882 EXPECT_EQ(expected_congestion_control_type,
883 QuicSentPacketManagerPeer::GetSendAlgorithm(
884 *GetSentPacketManagerFromFirstServerSession())
885 ->GetCongestionControlType());
888 TEST_P(EndToEndTest, LimitMaxOpenStreams) {
889 // Server limits the number of max streams to 2.
890 server_config_.SetMaxStreamsPerConnection(2, 2);
891 // Client tries to negotiate for 10.
892 client_config_.SetMaxStreamsPerConnection(10, 5);
894 ASSERT_TRUE(Initialize());
895 client_->client()->WaitForCryptoHandshakeConfirmed();
896 QuicConfig* client_negotiated_config = client_->client()->session()->config();
897 EXPECT_EQ(2u, client_negotiated_config->MaxStreamsPerConnection());
900 TEST_P(EndToEndTest, ClientSuggestsRTT) {
901 // Client suggests initial RTT, verify it is used.
902 const uint32 kInitialRTT = 20000;
903 client_config_.SetInitialRoundTripTimeUsToSend(kInitialRTT);
905 ASSERT_TRUE(Initialize());
906 client_->client()->WaitForCryptoHandshakeConfirmed();
907 server_thread_->WaitForCryptoHandshakeConfirmed();
909 // Pause the server so we can access the server's internals without races.
910 server_thread_->Pause();
911 QuicDispatcher* dispatcher =
912 QuicServerPeer::GetDispatcher(server_thread_->server());
913 ASSERT_EQ(1u, dispatcher->session_map().size());
914 const QuicSentPacketManager& client_sent_packet_manager =
915 client_->client()->session()->connection()->sent_packet_manager();
916 const QuicSentPacketManager& server_sent_packet_manager =
917 *GetSentPacketManagerFromFirstServerSession();
919 EXPECT_EQ(kInitialRTT,
920 client_sent_packet_manager.GetRttStats()->initial_rtt_us());
921 EXPECT_EQ(kInitialRTT,
922 server_sent_packet_manager.GetRttStats()->initial_rtt_us());
923 server_thread_->Resume();
926 TEST_P(EndToEndTest, MaxInitialRTT) {
927 // Client tries to suggest twice the server's max initial rtt and the server
928 // uses the max.
929 client_config_.SetInitialRoundTripTimeUsToSend(
930 2 * kMaxInitialRoundTripTimeUs);
932 ASSERT_TRUE(Initialize());
933 client_->client()->WaitForCryptoHandshakeConfirmed();
934 server_thread_->WaitForCryptoHandshakeConfirmed();
936 // Pause the server so we can access the server's internals without races.
937 server_thread_->Pause();
938 QuicDispatcher* dispatcher =
939 QuicServerPeer::GetDispatcher(server_thread_->server());
940 ASSERT_EQ(1u, dispatcher->session_map().size());
941 QuicSession* session = dispatcher->session_map().begin()->second;
942 const QuicSentPacketManager& client_sent_packet_manager =
943 client_->client()->session()->connection()->sent_packet_manager();
945 // Now that acks have been exchanged, the RTT estimate has decreased on the
946 // server and is not infinite on the client.
947 EXPECT_FALSE(
948 client_sent_packet_manager.GetRttStats()->smoothed_rtt().IsInfinite());
949 const RttStats& server_rtt_stats =
950 *session->connection()->sent_packet_manager().GetRttStats();
951 EXPECT_EQ(static_cast<int64>(kMaxInitialRoundTripTimeUs),
952 server_rtt_stats.initial_rtt_us());
953 EXPECT_GE(static_cast<int64>(kMaxInitialRoundTripTimeUs),
954 server_rtt_stats.smoothed_rtt().ToMicroseconds());
955 server_thread_->Resume();
958 TEST_P(EndToEndTest, MinInitialRTT) {
959 // Client tries to suggest 0 and the server uses the default.
960 client_config_.SetInitialRoundTripTimeUsToSend(0);
962 ASSERT_TRUE(Initialize());
963 client_->client()->WaitForCryptoHandshakeConfirmed();
964 server_thread_->WaitForCryptoHandshakeConfirmed();
966 // Pause the server so we can access the server's internals without races.
967 server_thread_->Pause();
968 QuicDispatcher* dispatcher =
969 QuicServerPeer::GetDispatcher(server_thread_->server());
970 ASSERT_EQ(1u, dispatcher->session_map().size());
971 QuicSession* session = dispatcher->session_map().begin()->second;
972 const QuicSentPacketManager& client_sent_packet_manager =
973 client_->client()->session()->connection()->sent_packet_manager();
974 const QuicSentPacketManager& server_sent_packet_manager =
975 session->connection()->sent_packet_manager();
977 // Now that acks have been exchanged, the RTT estimate has decreased on the
978 // server and is not infinite on the client.
979 EXPECT_FALSE(
980 client_sent_packet_manager.GetRttStats()->smoothed_rtt().IsInfinite());
981 // Expect the default rtt of 100ms.
982 EXPECT_EQ(static_cast<int64>(100 * kNumMicrosPerMilli),
983 server_sent_packet_manager.GetRttStats()->initial_rtt_us());
984 // Ensure the bandwidth is valid.
985 client_sent_packet_manager.BandwidthEstimate();
986 server_sent_packet_manager.BandwidthEstimate();
987 server_thread_->Resume();
990 TEST_P(EndToEndTest, 0ByteConnectionId) {
991 client_config_.SetBytesForConnectionIdToSend(0);
992 ASSERT_TRUE(Initialize());
994 EXPECT_EQ(kFooResponseBody, client_->SendSynchronousRequest("/foo"));
995 EXPECT_EQ(200u, client_->response_headers()->parsed_response_code());
997 QuicPacketHeader* header = QuicConnectionPeer::GetLastHeader(
998 client_->client()->session()->connection());
999 EXPECT_EQ(PACKET_0BYTE_CONNECTION_ID,
1000 header->public_header.connection_id_length);
1003 TEST_P(EndToEndTest, 1ByteConnectionId) {
1004 client_config_.SetBytesForConnectionIdToSend(1);
1005 ASSERT_TRUE(Initialize());
1007 EXPECT_EQ(kFooResponseBody, client_->SendSynchronousRequest("/foo"));
1008 EXPECT_EQ(200u, client_->response_headers()->parsed_response_code());
1009 QuicPacketHeader* header = QuicConnectionPeer::GetLastHeader(
1010 client_->client()->session()->connection());
1011 EXPECT_EQ(PACKET_1BYTE_CONNECTION_ID,
1012 header->public_header.connection_id_length);
1015 TEST_P(EndToEndTest, 4ByteConnectionId) {
1016 client_config_.SetBytesForConnectionIdToSend(4);
1017 ASSERT_TRUE(Initialize());
1019 EXPECT_EQ(kFooResponseBody, client_->SendSynchronousRequest("/foo"));
1020 EXPECT_EQ(200u, client_->response_headers()->parsed_response_code());
1021 QuicPacketHeader* header = QuicConnectionPeer::GetLastHeader(
1022 client_->client()->session()->connection());
1023 EXPECT_EQ(PACKET_4BYTE_CONNECTION_ID,
1024 header->public_header.connection_id_length);
1027 TEST_P(EndToEndTest, 8ByteConnectionId) {
1028 client_config_.SetBytesForConnectionIdToSend(8);
1029 ASSERT_TRUE(Initialize());
1031 EXPECT_EQ(kFooResponseBody, client_->SendSynchronousRequest("/foo"));
1032 EXPECT_EQ(200u, client_->response_headers()->parsed_response_code());
1033 QuicPacketHeader* header = QuicConnectionPeer::GetLastHeader(
1034 client_->client()->session()->connection());
1035 EXPECT_EQ(PACKET_8BYTE_CONNECTION_ID,
1036 header->public_header.connection_id_length);
1039 TEST_P(EndToEndTest, 15ByteConnectionId) {
1040 client_config_.SetBytesForConnectionIdToSend(15);
1041 ASSERT_TRUE(Initialize());
1043 // Our server is permissive and allows for out of bounds values.
1044 EXPECT_EQ(kFooResponseBody, client_->SendSynchronousRequest("/foo"));
1045 EXPECT_EQ(200u, client_->response_headers()->parsed_response_code());
1046 QuicPacketHeader* header = QuicConnectionPeer::GetLastHeader(
1047 client_->client()->session()->connection());
1048 EXPECT_EQ(PACKET_8BYTE_CONNECTION_ID,
1049 header->public_header.connection_id_length);
1052 TEST_P(EndToEndTest, ResetConnection) {
1053 ASSERT_TRUE(Initialize());
1054 client_->client()->WaitForCryptoHandshakeConfirmed();
1056 EXPECT_EQ(kFooResponseBody, client_->SendSynchronousRequest("/foo"));
1057 EXPECT_EQ(200u, client_->response_headers()->parsed_response_code());
1058 client_->ResetConnection();
1059 EXPECT_EQ(kBarResponseBody, client_->SendSynchronousRequest("/bar"));
1060 EXPECT_EQ(200u, client_->response_headers()->parsed_response_code());
1063 TEST_P(EndToEndTest, MaxStreamsUberTest) {
1064 SetPacketLossPercentage(1);
1065 ASSERT_TRUE(Initialize());
1066 string large_body;
1067 GenerateBody(&large_body, 10240);
1068 int max_streams = 100;
1070 AddToCache("/large_response", 200, "OK", large_body);;
1072 client_->client()->WaitForCryptoHandshakeConfirmed();
1073 SetPacketLossPercentage(10);
1075 for (int i = 0; i < max_streams; ++i) {
1076 EXPECT_LT(0, client_->SendRequest("/large_response"));
1079 // WaitForEvents waits 50ms and returns true if there are outstanding
1080 // requests.
1081 while (client_->client()->WaitForEvents() == true) {
1085 TEST_P(EndToEndTest, StreamCancelErrorTest) {
1086 ASSERT_TRUE(Initialize());
1087 string small_body;
1088 GenerateBody(&small_body, 256);
1090 AddToCache("/small_response", 200, "OK", small_body);
1092 client_->client()->WaitForCryptoHandshakeConfirmed();
1094 QuicSession* session = client_->client()->session();
1095 // Lose the request.
1096 SetPacketLossPercentage(100);
1097 EXPECT_LT(0, client_->SendRequest("/small_response"));
1098 client_->client()->WaitForEvents();
1099 // Transmit the cancel, and ensure the connection is torn down properly.
1100 SetPacketLossPercentage(0);
1101 QuicStreamId stream_id = kClientDataStreamId1;
1102 session->SendRstStream(stream_id, QUIC_STREAM_CANCELLED, 0);
1104 // WaitForEvents waits 50ms and returns true if there are outstanding
1105 // requests.
1106 while (client_->client()->WaitForEvents() == true) {
1108 // It should be completely fine to RST a stream before any data has been
1109 // received for that stream.
1110 EXPECT_EQ(QUIC_NO_ERROR, client_->connection_error());
1113 class WrongAddressWriter : public QuicPacketWriterWrapper {
1114 public:
1115 WrongAddressWriter() {
1116 IPAddressNumber ip;
1117 CHECK(net::ParseIPLiteralToNumber("127.0.0.2", &ip));
1118 self_address_ = IPEndPoint(ip, 0);
1121 WriteResult WritePacket(const char* buffer,
1122 size_t buf_len,
1123 const IPAddressNumber& real_self_address,
1124 const IPEndPoint& peer_address) override {
1125 // Use wrong address!
1126 return QuicPacketWriterWrapper::WritePacket(
1127 buffer, buf_len, self_address_.address(), peer_address);
1130 bool IsWriteBlockedDataBuffered() const override { return false; }
1132 IPEndPoint self_address_;
1135 TEST_P(EndToEndTest, ConnectionMigrationClientIPChanged) {
1136 // Tests that the client's IP can not change during an established QUIC
1137 // connection. If it changes, the connection is closed by the server as we do
1138 // not yet support IP migration.
1139 ASSERT_TRUE(Initialize());
1141 EXPECT_EQ(kFooResponseBody, client_->SendSynchronousRequest("/foo"));
1142 EXPECT_EQ(200u, client_->response_headers()->parsed_response_code());
1144 WrongAddressWriter* writer = new WrongAddressWriter();
1146 writer->set_writer(new QuicDefaultPacketWriter(client_->client()->fd()));
1147 QuicConnectionPeer::SetWriter(client_->client()->session()->connection(),
1148 writer,
1149 /* owns_writer= */ true);
1151 client_->SendSynchronousRequest("/bar");
1153 EXPECT_EQ(QUIC_STREAM_CONNECTION_ERROR, client_->stream_error());
1154 EXPECT_EQ(QUIC_ERROR_MIGRATING_ADDRESS, client_->connection_error());
1157 TEST_P(EndToEndTest, ConnectionMigrationClientPortChanged) {
1158 // Tests that the client's port can change during an established QUIC
1159 // connection, and that doing so does not result in the connection being
1160 // closed by the server.
1161 ASSERT_TRUE(Initialize());
1163 EXPECT_EQ(kFooResponseBody, client_->SendSynchronousRequest("/foo"));
1164 EXPECT_EQ(200u, client_->response_headers()->parsed_response_code());
1166 // Store the client address which was used to send the first request.
1167 IPEndPoint old_address = client_->client()->client_address();
1169 // Stop listening on the old FD.
1170 EpollServer* eps = client_->epoll_server();
1171 int old_fd = client_->client()->fd();
1172 eps->UnregisterFD(old_fd);
1173 // Create a new socket before closing the old one, which will result in a new
1174 // ephemeral port.
1175 QuicClientPeer::CreateUDPSocket(client_->client());
1176 close(old_fd);
1178 // The packet writer needs to be updated to use the new FD.
1179 client_->client()->CreateQuicPacketWriter();
1181 // Change the internal state of the client and connection to use the new port,
1182 // this is done because in a real NAT rebinding the client wouldn't see any
1183 // port change, and so expects no change to incoming port.
1184 // This is kind of ugly, but needed as we are simply swapping out the client
1185 // FD rather than any more complex NAT rebinding simulation.
1186 int new_port = client_->client()->client_address().port();
1187 QuicClientPeer::SetClientPort(client_->client(), new_port);
1188 QuicConnectionPeer::SetSelfAddress(
1189 client_->client()->session()->connection(),
1190 IPEndPoint(
1191 client_->client()->session()->connection()->self_address().address(),
1192 new_port));
1194 // Register the new FD for epoll events.
1195 int new_fd = client_->client()->fd();
1196 eps->RegisterFD(new_fd, client_->client(), EPOLLIN | EPOLLOUT | EPOLLET);
1198 // Send a second request, using the new FD.
1199 EXPECT_EQ(kBarResponseBody, client_->SendSynchronousRequest("/bar"));
1200 EXPECT_EQ(200u, client_->response_headers()->parsed_response_code());
1202 // Verify that the client's ephemeral port is different.
1203 IPEndPoint new_address = client_->client()->client_address();
1204 EXPECT_EQ(old_address.address(), new_address.address());
1205 EXPECT_NE(old_address.port(), new_address.port());
1208 TEST_P(EndToEndTest, DifferentFlowControlWindows) {
1209 // Client and server can set different initial flow control receive windows.
1210 // These are sent in CHLO/SHLO. Tests that these values are exchanged properly
1211 // in the crypto handshake.
1212 const uint32 kClientStreamIFCW = 123456;
1213 const uint32 kClientSessionIFCW = 234567;
1214 set_client_initial_stream_flow_control_receive_window(kClientStreamIFCW);
1215 set_client_initial_session_flow_control_receive_window(kClientSessionIFCW);
1217 const uint32 kServerStreamIFCW = 654321;
1218 const uint32 kServerSessionIFCW = 765432;
1219 set_server_initial_stream_flow_control_receive_window(kServerStreamIFCW);
1220 set_server_initial_session_flow_control_receive_window(kServerSessionIFCW);
1222 ASSERT_TRUE(Initialize());
1224 // Values are exchanged during crypto handshake, so wait for that to finish.
1225 client_->client()->WaitForCryptoHandshakeConfirmed();
1226 server_thread_->WaitForCryptoHandshakeConfirmed();
1228 // Open a data stream to make sure the stream level flow control is updated.
1229 QuicSpdyClientStream* stream = client_->GetOrCreateStream();
1230 stream->SendBody("hello", false);
1232 // Client should have the right values for server's receive window.
1233 EXPECT_EQ(kServerStreamIFCW,
1234 client_->client()
1235 ->session()
1236 ->config()
1237 ->ReceivedInitialStreamFlowControlWindowBytes());
1238 EXPECT_EQ(kServerSessionIFCW,
1239 client_->client()
1240 ->session()
1241 ->config()
1242 ->ReceivedInitialSessionFlowControlWindowBytes());
1243 EXPECT_EQ(kServerStreamIFCW, QuicFlowControllerPeer::SendWindowOffset(
1244 stream->flow_controller()));
1245 EXPECT_EQ(kServerSessionIFCW,
1246 QuicFlowControllerPeer::SendWindowOffset(
1247 client_->client()->session()->flow_controller()));
1249 // Server should have the right values for client's receive window.
1250 server_thread_->Pause();
1251 QuicDispatcher* dispatcher =
1252 QuicServerPeer::GetDispatcher(server_thread_->server());
1253 QuicSession* session = dispatcher->session_map().begin()->second;
1254 EXPECT_EQ(kClientStreamIFCW,
1255 session->config()->ReceivedInitialStreamFlowControlWindowBytes());
1256 EXPECT_EQ(kClientSessionIFCW,
1257 session->config()->ReceivedInitialSessionFlowControlWindowBytes());
1258 EXPECT_EQ(kClientSessionIFCW, QuicFlowControllerPeer::SendWindowOffset(
1259 session->flow_controller()));
1260 server_thread_->Resume();
1263 TEST_P(EndToEndTest, HeadersAndCryptoStreamsNoConnectionFlowControl) {
1264 // The special headers and crypto streams should be subject to per-stream flow
1265 // control limits, but should not be subject to connection level flow control.
1266 const uint32 kStreamIFCW = 123456;
1267 const uint32 kSessionIFCW = 234567;
1268 set_client_initial_stream_flow_control_receive_window(kStreamIFCW);
1269 set_client_initial_session_flow_control_receive_window(kSessionIFCW);
1270 set_server_initial_stream_flow_control_receive_window(kStreamIFCW);
1271 set_server_initial_session_flow_control_receive_window(kSessionIFCW);
1273 ASSERT_TRUE(Initialize());
1275 // Wait for crypto handshake to finish. This should have contributed to the
1276 // crypto stream flow control window, but not affected the session flow
1277 // control window.
1278 client_->client()->WaitForCryptoHandshakeConfirmed();
1279 server_thread_->WaitForCryptoHandshakeConfirmed();
1281 QuicCryptoStream* crypto_stream =
1282 QuicSessionPeer::GetCryptoStream(client_->client()->session());
1283 EXPECT_LT(
1284 QuicFlowControllerPeer::SendWindowSize(crypto_stream->flow_controller()),
1285 kStreamIFCW);
1286 EXPECT_EQ(kSessionIFCW, QuicFlowControllerPeer::SendWindowSize(
1287 client_->client()->session()->flow_controller()));
1289 // Send a request with no body, and verify that the connection level window
1290 // has not been affected.
1291 EXPECT_EQ(kFooResponseBody, client_->SendSynchronousRequest("/foo"));
1293 QuicHeadersStream* headers_stream =
1294 QuicSessionPeer::GetHeadersStream(client_->client()->session());
1295 EXPECT_LT(
1296 QuicFlowControllerPeer::SendWindowSize(headers_stream->flow_controller()),
1297 kStreamIFCW);
1298 EXPECT_EQ(kSessionIFCW, QuicFlowControllerPeer::SendWindowSize(
1299 client_->client()->session()->flow_controller()));
1301 // Server should be in a similar state: connection flow control window should
1302 // not have any bytes marked as received.
1303 server_thread_->Pause();
1304 QuicDispatcher* dispatcher =
1305 QuicServerPeer::GetDispatcher(server_thread_->server());
1306 QuicSession* session = dispatcher->session_map().begin()->second;
1307 QuicFlowController* server_connection_flow_controller =
1308 session->flow_controller();
1309 EXPECT_EQ(kSessionIFCW, QuicFlowControllerPeer::ReceiveWindowSize(
1310 server_connection_flow_controller));
1311 server_thread_->Resume();
1314 TEST_P(EndToEndTest, RequestWithNoBodyWillNeverSendStreamFrameWithFIN) {
1315 // Regression test for b/16010251.
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(
1331 session).size());
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 {
1338 public:
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_; }
1350 protected:
1351 // Object is ref counted.
1352 ~TestAckNotifierDelegate() override { EXPECT_TRUE(has_been_notified_); }
1354 private:
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))
1422 .Times(1);
1423 // We must pause the server's thread in order to call WritePacket without
1424 // race conditions.
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(),
1456 server_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))
1482 .Times(1);
1483 // We must pause the server's thread in order to call WritePacket without
1484 // race conditions.
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)
1509 0x3C,
1510 // truncated connection ID
1511 0x11};
1512 client_writer_->WritePacket(&packet[0], sizeof(packet),
1513 client_->client()->client_address().address(),
1514 server_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.
1540 char packet[] = {
1541 // invalid public flags
1542 0xFF,
1543 // connection_id
1544 0x10,
1545 0x32,
1546 0x54,
1547 0x76,
1548 0x98,
1549 0xBA,
1550 0xDC,
1551 0xFE,
1552 // packet sequence number
1553 0xBC,
1554 0x9A,
1555 0x78,
1556 0x56,
1557 0x34,
1558 0x12,
1559 // private flags
1560 0x00,
1562 client_writer_->WritePacket(&packet[0], sizeof(packet),
1563 client_->client()->client_address().address(),
1564 server_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(),
1599 server_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());
1617 } // namespace
1618 } // namespace test
1619 } // namespace tools
1620 } // namespace net