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/time/time.h"
16 #include "net/base/ip_endpoint.h"
17 #include "net/quic/congestion_control/tcp_cubic_sender.h"
18 #include "net/quic/crypto/aes_128_gcm_12_encrypter.h"
19 #include "net/quic/crypto/null_encrypter.h"
20 #include "net/quic/quic_flags.h"
21 #include "net/quic/quic_framer.h"
22 #include "net/quic/quic_packet_creator.h"
23 #include "net/quic/quic_protocol.h"
24 #include "net/quic/quic_sent_packet_manager.h"
25 #include "net/quic/quic_server_id.h"
26 #include "net/quic/test_tools/quic_connection_peer.h"
27 #include "net/quic/test_tools/quic_session_peer.h"
28 #include "net/quic/test_tools/quic_test_utils.h"
29 #include "net/quic/test_tools/reliable_quic_stream_peer.h"
30 #include "net/test/gtest_util.h"
31 #include "net/tools/quic/quic_epoll_connection_helper.h"
32 #include "net/tools/quic/quic_in_memory_cache.h"
33 #include "net/tools/quic/quic_packet_writer_wrapper.h"
34 #include "net/tools/quic/quic_server.h"
35 #include "net/tools/quic/quic_socket_utils.h"
36 #include "net/tools/quic/quic_spdy_client_stream.h"
37 #include "net/tools/quic/test_tools/http_message.h"
38 #include "net/tools/quic/test_tools/packet_dropping_test_writer.h"
39 #include "net/tools/quic/test_tools/quic_client_peer.h"
40 #include "net/tools/quic/test_tools/quic_dispatcher_peer.h"
41 #include "net/tools/quic/test_tools/quic_in_memory_cache_peer.h"
42 #include "net/tools/quic/test_tools/quic_server_peer.h"
43 #include "net/tools/quic/test_tools/quic_test_client.h"
44 #include "net/tools/quic/test_tools/server_thread.h"
45 #include "testing/gtest/include/gtest/gtest.h"
47 using base::StringPiece;
48 using base::WaitableEvent;
49 using net::test::GenerateBody;
50 using net::test::QuicConnectionPeer;
51 using net::test::QuicSessionPeer;
52 using net::test::ReliableQuicStreamPeer;
53 using net::tools::test::PacketDroppingTestWriter;
54 using net::tools::test::QuicDispatcherPeer;
55 using net::tools::test::QuicServerPeer;
65 const char* kFooResponseBody = "Artichoke hearts make me happy.";
66 const char* kBarResponseBody = "Palm hearts are pretty delicious, also.";
68 // Run all tests with the cross products of all versions.
70 TestParams(const QuicVersionVector& client_supported_versions,
71 const QuicVersionVector& server_supported_versions,
72 QuicVersion negotiated_version,
74 : client_supported_versions(client_supported_versions),
75 server_supported_versions(server_supported_versions),
76 negotiated_version(negotiated_version),
77 use_pacing(use_pacing) {
80 friend ostream& operator<<(ostream& os, const TestParams& p) {
81 os << "{ server_supported_versions: "
82 << QuicVersionVectorToString(p.server_supported_versions);
83 os << " client_supported_versions: "
84 << QuicVersionVectorToString(p.client_supported_versions);
85 os << " negotiated_version: " << QuicVersionToString(p.negotiated_version);
86 os << " use_pacing: " << p.use_pacing << " }";
90 QuicVersionVector client_supported_versions;
91 QuicVersionVector server_supported_versions;
92 QuicVersion negotiated_version;
96 // Constructs various test permutations.
97 vector<TestParams> GetTestParams() {
98 vector<TestParams> params;
99 QuicVersionVector all_supported_versions = QuicSupportedVersions();
100 for (int use_pacing = 0; use_pacing < 2; ++use_pacing) {
101 // Add an entry for server and client supporting all versions.
102 params.push_back(TestParams(all_supported_versions,
103 all_supported_versions,
104 all_supported_versions[0],
107 // Test client supporting all versions and server supporting 1 version.
108 // Simulate an old server and exercise version downgrade in the client.
109 // Protocol negotiation should occur. Skip the i = 0 case because it is
110 // essentially the same as the default case.
111 for (size_t i = 1; i < all_supported_versions.size(); ++i) {
112 QuicVersionVector server_supported_versions;
113 server_supported_versions.push_back(all_supported_versions[i]);
114 if (all_supported_versions[i] >= QUIC_VERSION_17) {
115 // Until flow control is globally rolled out and we remove
116 // QUIC_VERSION_16, the server MUST support at least one QUIC version
117 // that does not use flow control.
118 server_supported_versions.push_back(QUIC_VERSION_16);
120 params.push_back(TestParams(all_supported_versions,
121 server_supported_versions,
122 server_supported_versions[0],
129 class ServerDelegate : public PacketDroppingTestWriter::Delegate {
131 explicit ServerDelegate(QuicDispatcher* dispatcher)
132 : dispatcher_(dispatcher) {}
133 virtual ~ServerDelegate() {}
134 virtual void OnCanWrite() OVERRIDE { dispatcher_->OnCanWrite(); }
136 QuicDispatcher* dispatcher_;
139 class ClientDelegate : public PacketDroppingTestWriter::Delegate {
141 explicit ClientDelegate(QuicClient* client) : client_(client) {}
142 virtual ~ClientDelegate() {}
143 virtual void OnCanWrite() OVERRIDE {
144 EpollEvent event(EPOLLOUT, false);
145 client_->OnEvent(client_->fd(), &event);
151 class EndToEndTest : public ::testing::TestWithParam<TestParams> {
154 : server_hostname_("example.com"),
155 server_started_(false),
156 strike_register_no_startup_period_(false) {
157 net::IPAddressNumber ip;
158 CHECK(net::ParseIPLiteralToNumber("127.0.0.1", &ip));
159 server_address_ = IPEndPoint(ip, 0);
161 client_supported_versions_ = GetParam().client_supported_versions;
162 server_supported_versions_ = GetParam().server_supported_versions;
163 negotiated_version_ = GetParam().negotiated_version;
164 FLAGS_enable_quic_pacing = GetParam().use_pacing;
166 if (negotiated_version_ >= QUIC_VERSION_17) {
167 FLAGS_enable_quic_stream_flow_control_2 = true;
169 if (negotiated_version_ >= QUIC_VERSION_19) {
170 FLAGS_enable_quic_connection_flow_control = true;
172 VLOG(1) << "Using Configuration: " << GetParam();
174 client_config_.SetDefaults();
175 server_config_.SetDefaults();
177 // Use different flow control windows for client/server.
178 client_initial_flow_control_receive_window_ =
179 2 * kInitialFlowControlWindowForTest;
180 server_initial_flow_control_receive_window_ =
181 3 * kInitialFlowControlWindowForTest;
183 QuicInMemoryCachePeer::ResetForTests();
184 AddToCache("GET", "https://www.google.com/foo",
185 "HTTP/1.1", "200", "OK", kFooResponseBody);
186 AddToCache("GET", "https://www.google.com/bar",
187 "HTTP/1.1", "200", "OK", kBarResponseBody);
190 virtual ~EndToEndTest() {
191 // TODO(rtenneti): port RecycleUnusedPort if needed.
192 // RecycleUnusedPort(server_address_.port());
193 QuicInMemoryCachePeer::ResetForTests();
196 QuicTestClient* CreateQuicClient(QuicPacketWriterWrapper* writer) {
197 QuicTestClient* client = new QuicTestClient(
202 client_supported_versions_,
203 client_initial_flow_control_receive_window_);
204 client->UseWriter(writer);
209 void set_client_initial_flow_control_receive_window(uint32 window) {
210 CHECK(client_.get() == NULL);
211 DVLOG(1) << "Setting client initial flow control window: " << window;
212 client_initial_flow_control_receive_window_ = window;
215 void set_server_initial_flow_control_receive_window(uint32 window) {
216 CHECK(server_thread_.get() == NULL);
217 DVLOG(1) << "Setting server initial flow control window: " << window;
218 server_initial_flow_control_receive_window_ = window;
222 // Start the server first, because CreateQuicClient() attempts
223 // to connect to the server.
225 client_.reset(CreateQuicClient(client_writer_));
226 static EpollEvent event(EPOLLOUT, false);
227 client_writer_->Initialize(
228 reinterpret_cast<QuicEpollConnectionHelper*>(
229 QuicConnectionPeer::GetHelper(
230 client_->client()->session()->connection())),
231 new ClientDelegate(client_->client()));
232 return client_->client()->connected();
235 virtual void SetUp() OVERRIDE {
236 // The ownership of these gets transferred to the QuicPacketWriterWrapper
237 // and QuicDispatcher when Initialize() is executed.
238 client_writer_ = new PacketDroppingTestWriter();
239 server_writer_ = new PacketDroppingTestWriter();
242 virtual void TearDown() OVERRIDE {
247 server_thread_.reset(
248 new ServerThread(server_address_,
250 server_supported_versions_,
251 strike_register_no_startup_period_,
252 server_initial_flow_control_receive_window_));
253 server_thread_->Initialize();
254 server_address_ = IPEndPoint(server_address_.address(),
255 server_thread_->GetPort());
256 QuicDispatcher* dispatcher =
257 QuicServerPeer::GetDispatcher(server_thread_->server());
258 QuicDispatcherPeer::UseWriter(dispatcher, server_writer_);
259 server_writer_->Initialize(
260 QuicDispatcherPeer::GetHelper(dispatcher),
261 new ServerDelegate(dispatcher));
262 server_thread_->Start();
263 server_started_ = true;
267 if (!server_started_)
269 if (server_thread_.get()) {
270 server_thread_->Quit();
271 server_thread_->Join();
275 void AddToCache(StringPiece method,
278 StringPiece response_code,
279 StringPiece response_detail,
281 QuicInMemoryCache::GetInstance()->AddSimpleResponse(
282 method, path, version, response_code, response_detail, body);
285 void SetPacketLossPercentage(int32 loss) {
286 // TODO(rtenneti): enable when we can do random packet loss tests in
288 if (loss != 0 && loss != 100)
290 client_writer_->set_fake_packet_loss_percentage(loss);
291 server_writer_->set_fake_packet_loss_percentage(loss);
294 void SetPacketSendDelay(QuicTime::Delta delay) {
295 // TODO(rtenneti): enable when we can do random packet send delay tests in
297 // client_writer_->set_fake_packet_delay(delay);
298 // server_writer_->set_fake_packet_delay(delay);
301 void SetReorderPercentage(int32 reorder) {
302 // TODO(rtenneti): enable when we can do random packet reorder tests in
304 // client_writer_->set_fake_reorder_percentage(reorder);
305 // server_writer_->set_fake_reorder_percentage(reorder);
308 // Verifies that the client and server connections were both free of packets
309 // being discarded, based on connection stats.
310 // Calls server_thread_ Pause() and Resume(), which may only be called once
312 void VerifyCleanConnection(bool had_packet_loss) {
313 QuicConnectionStats client_stats =
314 client_->client()->session()->connection()->GetStats();
315 if (!had_packet_loss) {
316 EXPECT_EQ(0u, client_stats.packets_lost);
318 EXPECT_EQ(0u, client_stats.packets_discarded);
319 EXPECT_EQ(0u, client_stats.packets_dropped);
320 EXPECT_EQ(client_stats.packets_received, client_stats.packets_processed);
322 server_thread_->Pause();
323 QuicDispatcher* dispatcher =
324 QuicServerPeer::GetDispatcher(server_thread_->server());
325 ASSERT_EQ(1u, dispatcher->session_map().size());
326 QuicSession* session = dispatcher->session_map().begin()->second;
327 QuicConnectionStats server_stats = session->connection()->GetStats();
328 if (!had_packet_loss) {
329 EXPECT_EQ(0u, server_stats.packets_lost);
331 EXPECT_EQ(0u, server_stats.packets_discarded);
332 // TODO(ianswett): Restore the check for packets_dropped equals 0.
333 // The expect for packets received is equal to packets processed fails
334 // due to version negotiation packets.
335 server_thread_->Resume();
338 IPEndPoint server_address_;
339 string server_hostname_;
340 scoped_ptr<ServerThread> server_thread_;
341 scoped_ptr<QuicTestClient> client_;
342 PacketDroppingTestWriter* client_writer_;
343 PacketDroppingTestWriter* server_writer_;
344 bool server_started_;
345 QuicConfig client_config_;
346 QuicConfig server_config_;
347 QuicVersionVector client_supported_versions_;
348 QuicVersionVector server_supported_versions_;
349 QuicVersion negotiated_version_;
350 bool strike_register_no_startup_period_;
351 uint32 client_initial_flow_control_receive_window_;
352 uint32 server_initial_flow_control_receive_window_;
355 // Run all end to end tests with all supported versions.
356 INSTANTIATE_TEST_CASE_P(EndToEndTests,
358 ::testing::ValuesIn(GetTestParams()));
360 TEST_P(EndToEndTest, SimpleRequestResponse) {
361 ASSERT_TRUE(Initialize());
363 EXPECT_EQ(kFooResponseBody, client_->SendSynchronousRequest("/foo"));
364 EXPECT_EQ(200u, client_->response_headers()->parsed_response_code());
367 // TODO(rch): figure out how to detect missing v6 supprt (like on the linux
368 // try bots) and selectively disable this test.
369 TEST_P(EndToEndTest, DISABLED_SimpleRequestResponsev6) {
371 CHECK(net::ParseIPLiteralToNumber("::1", &ip));
372 server_address_ = IPEndPoint(ip, server_address_.port());
373 ASSERT_TRUE(Initialize());
375 EXPECT_EQ(kFooResponseBody, client_->SendSynchronousRequest("/foo"));
376 EXPECT_EQ(200u, client_->response_headers()->parsed_response_code());
379 TEST_P(EndToEndTest, SeparateFinPacket) {
380 ASSERT_TRUE(Initialize());
382 HTTPMessage request(HttpConstants::HTTP_1_1,
383 HttpConstants::POST, "/foo");
384 request.set_has_complete_message(false);
386 client_->SendMessage(request);
388 client_->SendData(string(), true);
390 client_->WaitForResponse();
391 EXPECT_EQ(kFooResponseBody, client_->response_body());
392 EXPECT_EQ(200u, client_->response_headers()->parsed_response_code());
394 request.AddBody("foo", true);
396 client_->SendMessage(request);
397 client_->SendData(string(), true);
398 client_->WaitForResponse();
399 EXPECT_EQ(kFooResponseBody, client_->response_body());
400 EXPECT_EQ(200u, client_->response_headers()->parsed_response_code());
403 TEST_P(EndToEndTest, MultipleRequestResponse) {
404 ASSERT_TRUE(Initialize());
406 EXPECT_EQ(kFooResponseBody, client_->SendSynchronousRequest("/foo"));
407 EXPECT_EQ(200u, client_->response_headers()->parsed_response_code());
408 EXPECT_EQ(kBarResponseBody, client_->SendSynchronousRequest("/bar"));
409 EXPECT_EQ(200u, client_->response_headers()->parsed_response_code());
412 TEST_P(EndToEndTest, MultipleClients) {
413 ASSERT_TRUE(Initialize());
414 scoped_ptr<QuicTestClient> client2(CreateQuicClient(NULL));
416 HTTPMessage request(HttpConstants::HTTP_1_1,
417 HttpConstants::POST, "/foo");
418 request.AddHeader("content-length", "3");
419 request.set_has_complete_message(false);
421 client_->SendMessage(request);
422 client2->SendMessage(request);
424 client_->SendData("bar", true);
425 client_->WaitForResponse();
426 EXPECT_EQ(kFooResponseBody, client_->response_body());
427 EXPECT_EQ(200u, client_->response_headers()->parsed_response_code());
429 client2->SendData("eep", true);
430 client2->WaitForResponse();
431 EXPECT_EQ(kFooResponseBody, client2->response_body());
432 EXPECT_EQ(200u, client2->response_headers()->parsed_response_code());
435 TEST_P(EndToEndTest, RequestOverMultiplePackets) {
436 // Send a large enough request to guarantee fragmentation.
437 string huge_request =
438 "https://www.google.com/some/path?query=" + string(kMaxPacketSize, '.');
439 AddToCache("GET", huge_request, "HTTP/1.1", "200", "OK", kBarResponseBody);
441 ASSERT_TRUE(Initialize());
443 EXPECT_EQ(kBarResponseBody, client_->SendSynchronousRequest(huge_request));
444 EXPECT_EQ(200u, client_->response_headers()->parsed_response_code());
447 TEST_P(EndToEndTest, MultiplePacketsRandomOrder) {
448 // Send a large enough request to guarantee fragmentation.
449 string huge_request =
450 "https://www.google.com/some/path?query=" + string(kMaxPacketSize, '.');
451 AddToCache("GET", huge_request, "HTTP/1.1", "200", "OK", kBarResponseBody);
453 ASSERT_TRUE(Initialize());
454 SetPacketSendDelay(QuicTime::Delta::FromMilliseconds(2));
455 SetReorderPercentage(50);
457 EXPECT_EQ(kBarResponseBody, client_->SendSynchronousRequest(huge_request));
458 EXPECT_EQ(200u, client_->response_headers()->parsed_response_code());
461 TEST_P(EndToEndTest, PostMissingBytes) {
462 ASSERT_TRUE(Initialize());
464 // Add a content length header with no body.
465 HTTPMessage request(HttpConstants::HTTP_1_1,
466 HttpConstants::POST, "/foo");
467 request.AddHeader("content-length", "3");
468 request.set_skip_message_validation(true);
470 // This should be detected as stream fin without complete request,
471 // triggering an error response.
472 client_->SendCustomSynchronousRequest(request);
473 EXPECT_EQ("bad", client_->response_body());
474 EXPECT_EQ(500u, client_->response_headers()->parsed_response_code());
477 // TODO(rtenneti): DISABLED_LargePostNoPacketLoss seems to be flaky.
478 // http://crbug.com/297040.
479 TEST_P(EndToEndTest, DISABLED_LargePostNoPacketLoss) {
480 ASSERT_TRUE(Initialize());
482 client_->client()->WaitForCryptoHandshakeConfirmed();
486 GenerateBody(&body, 1024 * 1024);
488 HTTPMessage request(HttpConstants::HTTP_1_1,
489 HttpConstants::POST, "/foo");
490 request.AddBody(body, true);
492 EXPECT_EQ(kFooResponseBody, client_->SendCustomSynchronousRequest(request));
493 VerifyCleanConnection(false);
496 TEST_P(EndToEndTest, LargePostNoPacketLoss1sRTT) {
497 ASSERT_TRUE(Initialize());
498 SetPacketSendDelay(QuicTime::Delta::FromMilliseconds(1000));
500 client_->client()->WaitForCryptoHandshakeConfirmed();
504 GenerateBody(&body, 100 * 1024);
506 HTTPMessage request(HttpConstants::HTTP_1_1,
507 HttpConstants::POST, "/foo");
508 request.AddBody(body, true);
510 EXPECT_EQ(kFooResponseBody, client_->SendCustomSynchronousRequest(request));
511 VerifyCleanConnection(false);
514 TEST_P(EndToEndTest, LargePostWithPacketLoss) {
515 // Connect with lower fake packet loss than we'd like to test. Until
516 // b/10126687 is fixed, losing handshake packets is pretty brutal.
517 SetPacketLossPercentage(5);
518 ASSERT_TRUE(Initialize());
520 // Wait for the server SHLO before upping the packet loss.
521 client_->client()->WaitForCryptoHandshakeConfirmed();
522 SetPacketLossPercentage(30);
526 GenerateBody(&body, 1024 * 10);
528 HTTPMessage request(HttpConstants::HTTP_1_1,
529 HttpConstants::POST, "/foo");
530 request.AddBody(body, true);
532 EXPECT_EQ(kFooResponseBody, client_->SendCustomSynchronousRequest(request));
535 TEST_P(EndToEndTest, LargePostNoPacketLossWithDelayAndReordering) {
536 ASSERT_TRUE(Initialize());
538 client_->client()->WaitForCryptoHandshakeConfirmed();
539 // Both of these must be called when the writer is not actively used.
540 SetPacketSendDelay(QuicTime::Delta::FromMilliseconds(2));
541 SetReorderPercentage(30);
545 GenerateBody(&body, 1024 * 1024);
547 HTTPMessage request(HttpConstants::HTTP_1_1,
548 HttpConstants::POST, "/foo");
549 request.AddBody(body, true);
551 EXPECT_EQ(kFooResponseBody, client_->SendCustomSynchronousRequest(request));
552 VerifyCleanConnection(true);
555 TEST_P(EndToEndTest, LargePostWithPacketLossAndBlockedSocket) {
556 // Connect with lower fake packet loss than we'd like to test. Until
557 // b/10126687 is fixed, losing handshake packets is pretty brutal.
558 SetPacketLossPercentage(5);
559 ASSERT_TRUE(Initialize());
561 // Wait for the server SHLO before upping the packet loss.
562 client_->client()->WaitForCryptoHandshakeConfirmed();
563 SetPacketLossPercentage(10);
564 client_writer_->set_fake_blocked_socket_percentage(10);
568 GenerateBody(&body, 1024 * 10);
570 HTTPMessage request(HttpConstants::HTTP_1_1,
571 HttpConstants::POST, "/foo");
572 request.AddBody(body, true);
574 EXPECT_EQ(kFooResponseBody, client_->SendCustomSynchronousRequest(request));
577 // TODO(rtenneti): rch is investigating the root cause. Will enable after we
579 TEST_P(EndToEndTest, DISABLED_LargePostZeroRTTFailure) {
580 // Have the server accept 0-RTT without waiting a startup period.
581 strike_register_no_startup_period_ = true;
583 // Send a request and then disconnect. This prepares the client to attempt
584 // a 0-RTT handshake for the next request.
585 ASSERT_TRUE(Initialize());
588 GenerateBody(&body, 20480);
590 HTTPMessage request(HttpConstants::HTTP_1_1,
591 HttpConstants::POST, "/foo");
592 request.AddBody(body, true);
594 EXPECT_EQ(kFooResponseBody, client_->SendCustomSynchronousRequest(request));
595 EXPECT_EQ(2, client_->client()->session()->GetNumSentClientHellos());
597 client_->Disconnect();
599 // The 0-RTT handshake should succeed.
601 if (client_supported_versions_[0] >= QUIC_VERSION_17 &&
602 negotiated_version_ < QUIC_VERSION_17) {
603 // If the version negotiation has resulted in a downgrade, then the client
604 // must wait for the handshake to complete before sending any data.
605 // Otherwise it may have queued QUIC_VERSION_17 frames which will trigger a
606 // DFATAL when they are serialized after the downgrade.
607 client_->client()->WaitForCryptoHandshakeConfirmed();
609 client_->WaitForResponseForMs(-1);
610 ASSERT_TRUE(client_->client()->connected());
611 EXPECT_EQ(kFooResponseBody, client_->SendCustomSynchronousRequest(request));
612 EXPECT_EQ(1, client_->client()->session()->GetNumSentClientHellos());
614 client_->Disconnect();
616 // Restart the server so that the 0-RTT handshake will take 1 RTT.
618 server_writer_ = new PacketDroppingTestWriter();
622 if (client_supported_versions_[0] >= QUIC_VERSION_17 &&
623 negotiated_version_ < QUIC_VERSION_17) {
624 // If the version negotiation has resulted in a downgrade, then the client
625 // must wait for the handshake to complete before sending any data.
626 // Otherwise it may have queued QUIC_VERSION_17 frames which will trigger a
627 // DFATAL when they are serialized after the downgrade.
628 client_->client()->WaitForCryptoHandshakeConfirmed();
630 ASSERT_TRUE(client_->client()->connected());
631 EXPECT_EQ(kFooResponseBody, client_->SendCustomSynchronousRequest(request));
632 EXPECT_EQ(2, client_->client()->session()->GetNumSentClientHellos());
633 VerifyCleanConnection(false);
636 TEST_P(EndToEndTest, LargePostFEC) {
637 // Connect without packet loss to avoid issues with losing handshake packets,
638 // and then up the packet loss rate (b/10126687).
639 ASSERT_TRUE(Initialize());
641 // Wait for the server SHLO before upping the packet loss.
642 client_->client()->WaitForCryptoHandshakeConfirmed();
643 SetPacketLossPercentage(30);
645 client_->options()->max_packets_per_fec_group = 6;
648 GenerateBody(&body, 10240);
650 HTTPMessage request(HttpConstants::HTTP_1_1,
651 HttpConstants::POST, "/foo");
652 request.AddBody(body, true);
654 EXPECT_EQ(kFooResponseBody, client_->SendCustomSynchronousRequest(request));
655 VerifyCleanConnection(true);
658 // TODO(rtenneti): DISABLED_LargePostLargeBuffer seems to be flaky.
659 // http://crbug.com/370087.
660 TEST_P(EndToEndTest, DISABLED_LargePostLargeBuffer) {
661 ASSERT_TRUE(Initialize());
662 SetPacketSendDelay(QuicTime::Delta::FromMicroseconds(1));
663 // 1Mbit per second with a 128k buffer from server to client. Wireless
664 // clients commonly have larger buffers, but our max CWND is 200.
665 server_writer_->set_max_bandwidth_and_buffer_size(
666 QuicBandwidth::FromBytesPerSecond(256 * 1024), 128 * 1024);
668 client_->client()->WaitForCryptoHandshakeConfirmed();
672 GenerateBody(&body, 1024 * 1024);
674 HTTPMessage request(HttpConstants::HTTP_1_1,
675 HttpConstants::POST, "/foo");
676 request.AddBody(body, true);
678 EXPECT_EQ(kFooResponseBody, client_->SendCustomSynchronousRequest(request));
679 VerifyCleanConnection(false);
682 TEST_P(EndToEndTest, InvalidStream) {
683 ASSERT_TRUE(Initialize());
684 client_->client()->WaitForCryptoHandshakeConfirmed();
687 GenerateBody(&body, kMaxPacketSize);
689 HTTPMessage request(HttpConstants::HTTP_1_1,
690 HttpConstants::POST, "/foo");
691 request.AddBody(body, true);
692 // Force the client to write with a stream ID belonging to a nonexistent
693 // server-side stream.
694 QuicSessionPeer::SetNextStreamId(client_->client()->session(), 2);
696 client_->SendCustomSynchronousRequest(request);
697 // EXPECT_EQ(QUIC_STREAM_CONNECTION_ERROR, client_->stream_error());
698 EXPECT_EQ(QUIC_PACKET_FOR_NONEXISTENT_STREAM, client_->connection_error());
701 // TODO(rch): this test seems to cause net_unittests timeouts :|
702 TEST_P(EndToEndTest, DISABLED_MultipleTermination) {
703 ASSERT_TRUE(Initialize());
705 HTTPMessage request(HttpConstants::HTTP_1_1,
706 HttpConstants::POST, "/foo");
707 request.AddHeader("content-length", "3");
708 request.set_has_complete_message(false);
710 // Set the offset so we won't frame. Otherwise when we pick up termination
711 // before HTTP framing is complete, we send an error and close the stream,
712 // and the second write is picked up as writing on a closed stream.
713 QuicSpdyClientStream* stream = client_->GetOrCreateStream();
714 ASSERT_TRUE(stream != NULL);
715 ReliableQuicStreamPeer::SetStreamBytesWritten(3, stream);
717 client_->SendData("bar", true);
718 client_->WaitForWriteToFlush();
720 // By default the stream protects itself from writes after terminte is set.
721 // Override this to test the server handling buggy clients.
722 ReliableQuicStreamPeer::SetWriteSideClosed(
723 false, client_->GetOrCreateStream());
725 EXPECT_DFATAL(client_->SendData("eep", true), "Fin already buffered");
728 TEST_P(EndToEndTest, Timeout) {
729 client_config_.set_idle_connection_state_lifetime(
730 QuicTime::Delta::FromMicroseconds(500),
731 QuicTime::Delta::FromMicroseconds(500));
732 // Note: we do NOT ASSERT_TRUE: we may time out during initial handshake:
733 // that's enough to validate timeout in this case.
735 while (client_->client()->connected()) {
736 client_->client()->WaitForEvents();
740 TEST_P(EndToEndTest, LimitMaxOpenStreams) {
741 // Server limits the number of max streams to 2.
742 server_config_.set_max_streams_per_connection(2, 2);
743 // Client tries to negotiate for 10.
744 client_config_.set_max_streams_per_connection(10, 5);
746 ASSERT_TRUE(Initialize());
747 client_->client()->WaitForCryptoHandshakeConfirmed();
748 QuicConfig* client_negotiated_config = client_->client()->session()->config();
749 EXPECT_EQ(2u, client_negotiated_config->max_streams_per_connection());
752 // TODO(rtenneti): DISABLED_LimitCongestionWindowAndRTT seems to be flaky.
753 // http://crbug.com/321870.
754 TEST_P(EndToEndTest, DISABLED_LimitCongestionWindowAndRTT) {
755 // Client tries to request twice the server's max initial window, and the
756 // server limits it to the max.
757 client_config_.SetInitialCongestionWindowToSend(2 * kMaxInitialWindow);
758 client_config_.SetInitialRoundTripTimeUsToSend(1);
760 ASSERT_TRUE(Initialize());
761 client_->client()->WaitForCryptoHandshakeConfirmed();
762 server_thread_->WaitForCryptoHandshakeConfirmed();
764 // Pause the server so we can access the server's internals without races.
765 server_thread_->Pause();
766 QuicDispatcher* dispatcher =
767 QuicServerPeer::GetDispatcher(server_thread_->server());
768 ASSERT_EQ(1u, dispatcher->session_map().size());
769 QuicSession* session = dispatcher->session_map().begin()->second;
770 const QuicSentPacketManager& client_sent_packet_manager =
771 client_->client()->session()->connection()->sent_packet_manager();
772 const QuicSentPacketManager& server_sent_packet_manager =
773 session->connection()->sent_packet_manager();
775 // The client shouldn't set it's initial window based on the negotiated value.
776 EXPECT_EQ(kDefaultInitialWindow * kDefaultTCPMSS,
777 client_sent_packet_manager.GetCongestionWindow());
778 EXPECT_EQ(kMaxInitialWindow * kDefaultTCPMSS,
779 server_sent_packet_manager.GetCongestionWindow());
781 EXPECT_EQ(FLAGS_enable_quic_pacing,
782 server_sent_packet_manager.using_pacing());
783 EXPECT_EQ(FLAGS_enable_quic_pacing,
784 client_sent_packet_manager.using_pacing());
787 client_sent_packet_manager.GetRttStats()->initial_rtt_us());
788 EXPECT_EQ(1u, server_sent_packet_manager.GetRttStats()->initial_rtt_us());
790 // Now use the negotiated limits with packet loss.
791 SetPacketLossPercentage(30);
795 GenerateBody(&body, 1024 * 10);
797 HTTPMessage request(HttpConstants::HTTP_1_1,
798 HttpConstants::POST, "/foo");
799 request.AddBody(body, true);
801 server_thread_->Resume();
803 EXPECT_EQ(kFooResponseBody, client_->SendCustomSynchronousRequest(request));
806 TEST_P(EndToEndTest, MaxInitialRTT) {
807 // Client tries to suggest twice the server's max initial rtt and the server
809 client_config_.SetInitialRoundTripTimeUsToSend(
810 2 * kMaxInitialRoundTripTimeUs);
812 ASSERT_TRUE(Initialize());
813 client_->client()->WaitForCryptoHandshakeConfirmed();
814 server_thread_->WaitForCryptoHandshakeConfirmed();
816 server_thread_->Pause();
817 QuicDispatcher* dispatcher =
818 QuicServerPeer::GetDispatcher(server_thread_->server());
819 ASSERT_EQ(1u, dispatcher->session_map().size());
820 QuicSession* session = dispatcher->session_map().begin()->second;
821 const QuicSentPacketManager& client_sent_packet_manager =
822 client_->client()->session()->connection()->sent_packet_manager();
823 const QuicSentPacketManager& server_sent_packet_manager =
824 session->connection()->sent_packet_manager();
826 // Now that acks have been exchanged, the RTT estimate has decreased on the
827 // server and is not infinite on the client.
829 client_sent_packet_manager.GetRttStats()->SmoothedRtt().IsInfinite());
830 EXPECT_EQ(static_cast<int64>(kMaxInitialRoundTripTimeUs),
831 server_sent_packet_manager.GetRttStats()->initial_rtt_us());
833 static_cast<int64>(kMaxInitialRoundTripTimeUs),
834 server_sent_packet_manager.GetRttStats()->SmoothedRtt().ToMicroseconds());
835 server_thread_->Resume();
838 TEST_P(EndToEndTest, MinInitialRTT) {
839 // Client tries to suggest 0 and the server uses the default.
840 client_config_.SetInitialRoundTripTimeUsToSend(0);
842 ASSERT_TRUE(Initialize());
843 client_->client()->WaitForCryptoHandshakeConfirmed();
844 server_thread_->WaitForCryptoHandshakeConfirmed();
846 // Pause the server so we can access the server's internals without races.
847 server_thread_->Pause();
848 QuicDispatcher* dispatcher =
849 QuicServerPeer::GetDispatcher(server_thread_->server());
850 ASSERT_EQ(1u, dispatcher->session_map().size());
851 QuicSession* session = dispatcher->session_map().begin()->second;
852 const QuicSentPacketManager& client_sent_packet_manager =
853 client_->client()->session()->connection()->sent_packet_manager();
854 const QuicSentPacketManager& server_sent_packet_manager =
855 session->connection()->sent_packet_manager();
857 // Now that acks have been exchanged, the RTT estimate has decreased on the
858 // server and is not infinite on the client.
860 client_sent_packet_manager.GetRttStats()->SmoothedRtt().IsInfinite());
861 // Expect the default rtt of 100ms.
862 EXPECT_EQ(static_cast<int64>(100 * base::Time::kMicrosecondsPerMillisecond),
863 server_sent_packet_manager.GetRttStats()->initial_rtt_us());
864 // Ensure the bandwidth is valid.
865 client_sent_packet_manager.BandwidthEstimate();
866 server_sent_packet_manager.BandwidthEstimate();
867 server_thread_->Resume();
870 TEST_P(EndToEndTest, ResetConnection) {
871 ASSERT_TRUE(Initialize());
872 client_->client()->WaitForCryptoHandshakeConfirmed();
874 EXPECT_EQ(kFooResponseBody, client_->SendSynchronousRequest("/foo"));
875 EXPECT_EQ(200u, client_->response_headers()->parsed_response_code());
876 client_->ResetConnection();
877 EXPECT_EQ(kBarResponseBody, client_->SendSynchronousRequest("/bar"));
878 EXPECT_EQ(200u, client_->response_headers()->parsed_response_code());
881 TEST_P(EndToEndTest, MaxStreamsUberTest) {
882 SetPacketLossPercentage(1);
883 ASSERT_TRUE(Initialize());
885 GenerateBody(&large_body, 10240);
886 int max_streams = 100;
888 AddToCache("GET", "/large_response", "HTTP/1.1", "200", "OK", large_body);;
890 client_->client()->WaitForCryptoHandshakeConfirmed();
891 SetPacketLossPercentage(10);
893 for (int i = 0; i < max_streams; ++i) {
894 EXPECT_LT(0, client_->SendRequest("/large_response"));
897 // WaitForEvents waits 50ms and returns true if there are outstanding
899 while (client_->client()->WaitForEvents() == true) {
903 TEST_P(EndToEndTest, StreamCancelErrorTest) {
904 ASSERT_TRUE(Initialize());
906 GenerateBody(&small_body, 256);
908 AddToCache("GET", "/small_response", "HTTP/1.1", "200", "OK", small_body);
910 client_->client()->WaitForCryptoHandshakeConfirmed();
912 QuicSession* session = client_->client()->session();
914 SetPacketLossPercentage(100);
915 EXPECT_LT(0, client_->SendRequest("/small_response"));
916 client_->client()->WaitForEvents();
917 // Transmit the cancel, and ensure the connection is torn down properly.
918 SetPacketLossPercentage(0);
919 QuicStreamId stream_id = 5;
920 session->SendRstStream(stream_id, QUIC_STREAM_CANCELLED, 0);
922 // WaitForEvents waits 50ms and returns true if there are outstanding
924 while (client_->client()->WaitForEvents() == true) {
926 // It should be completely fine to RST a stream before any data has been
927 // received for that stream.
928 EXPECT_EQ(QUIC_NO_ERROR, client_->connection_error());
931 class WrongAddressWriter : public QuicPacketWriterWrapper {
933 WrongAddressWriter() {
935 CHECK(net::ParseIPLiteralToNumber("127.0.0.2", &ip));
936 self_address_ = IPEndPoint(ip, 0);
939 virtual WriteResult WritePacket(
942 const IPAddressNumber& real_self_address,
943 const IPEndPoint& peer_address) OVERRIDE {
944 // Use wrong address!
945 return QuicPacketWriterWrapper::WritePacket(
946 buffer, buf_len, self_address_.address(), peer_address);
949 virtual bool IsWriteBlockedDataBuffered() const OVERRIDE {
953 IPEndPoint self_address_;
956 TEST_P(EndToEndTest, ConnectionMigration) {
957 ASSERT_TRUE(Initialize());
959 EXPECT_EQ(kFooResponseBody, client_->SendSynchronousRequest("/foo"));
960 EXPECT_EQ(200u, client_->response_headers()->parsed_response_code());
962 scoped_ptr<WrongAddressWriter> writer(new WrongAddressWriter());
964 writer->set_writer(new QuicDefaultPacketWriter(client_->client()->fd()));
965 QuicConnectionPeer::SetWriter(client_->client()->session()->connection(),
968 client_->SendSynchronousRequest("/bar");
970 EXPECT_EQ(QUIC_STREAM_CONNECTION_ERROR, client_->stream_error());
971 EXPECT_EQ(QUIC_ERROR_MIGRATING_ADDRESS, client_->connection_error());
974 TEST_P(EndToEndTest, DifferentFlowControlWindows) {
975 // Client and server can set different initial flow control receive windows.
976 // These are sent in CHLO/SHLO. Tests that these values are exchanged properly
977 // in the crypto handshake.
979 const uint32 kClientIFCW = 123456;
980 set_client_initial_flow_control_receive_window(kClientIFCW);
982 const uint32 kServerIFCW = 654321;
983 set_server_initial_flow_control_receive_window(kServerIFCW);
985 ASSERT_TRUE(Initialize());
987 // Values are exchanged during crypto handshake, so wait for that to finish.
988 client_->client()->WaitForCryptoHandshakeConfirmed();
989 server_thread_->WaitForCryptoHandshakeConfirmed();
991 // Client should have the right value for server's receive window.
992 EXPECT_EQ(kServerIFCW, client_->client()
995 ->ReceivedInitialFlowControlWindowBytes());
997 // Server should have the right value for client's receive window.
998 server_thread_->Pause();
999 QuicDispatcher* dispatcher =
1000 QuicServerPeer::GetDispatcher(server_thread_->server());
1001 QuicSession* session = dispatcher->session_map().begin()->second;
1002 EXPECT_EQ(kClientIFCW,
1003 session->config()->ReceivedInitialFlowControlWindowBytes());
1004 server_thread_->Resume();
1009 } // namespace tools