Upstream version 7.36.149.0
[platform/framework/web/crosswalk.git] / src / net / tools / quic / end_to_end_test.cc
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.
4
5 #include <stddef.h>
6 #include <string>
7 #include <sys/epoll.h>
8 #include <vector>
9
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"
46
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;
56 using std::ostream;
57 using std::string;
58 using std::vector;
59
60 namespace net {
61 namespace tools {
62 namespace test {
63 namespace {
64
65 const char* kFooResponseBody = "Artichoke hearts make me happy.";
66 const char* kBarResponseBody = "Palm hearts are pretty delicious, also.";
67
68 // Run all tests with the cross products of all versions.
69 struct TestParams {
70   TestParams(const QuicVersionVector& client_supported_versions,
71              const QuicVersionVector& server_supported_versions,
72              QuicVersion negotiated_version,
73              bool use_pacing)
74       : client_supported_versions(client_supported_versions),
75         server_supported_versions(server_supported_versions),
76         negotiated_version(negotiated_version),
77         use_pacing(use_pacing) {
78   }
79
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 << " }";
87     return os;
88   }
89
90   QuicVersionVector client_supported_versions;
91   QuicVersionVector server_supported_versions;
92   QuicVersion negotiated_version;
93   bool use_pacing;
94 };
95
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],
105                                 use_pacing != 0));
106
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);
119       }
120       params.push_back(TestParams(all_supported_versions,
121                                   server_supported_versions,
122                                   server_supported_versions[0],
123                                   use_pacing != 0));
124     }
125   }
126   return params;
127 }
128
129 class ServerDelegate : public PacketDroppingTestWriter::Delegate {
130  public:
131   explicit ServerDelegate(QuicDispatcher* dispatcher)
132       : dispatcher_(dispatcher) {}
133   virtual ~ServerDelegate() {}
134   virtual void OnCanWrite() OVERRIDE { dispatcher_->OnCanWrite(); }
135  private:
136   QuicDispatcher* dispatcher_;
137 };
138
139 class ClientDelegate : public PacketDroppingTestWriter::Delegate {
140  public:
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);
146   }
147  private:
148   QuicClient* client_;
149 };
150
151 class EndToEndTest : public ::testing::TestWithParam<TestParams> {
152  protected:
153   EndToEndTest()
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);
160
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;
165
166     if (negotiated_version_ >= QUIC_VERSION_17) {
167       FLAGS_enable_quic_stream_flow_control_2 = true;
168     }
169     if (negotiated_version_ >= QUIC_VERSION_19) {
170       FLAGS_enable_quic_connection_flow_control = true;
171     }
172     VLOG(1) << "Using Configuration: " << GetParam();
173
174     client_config_.SetDefaults();
175     server_config_.SetDefaults();
176
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;
182
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);
188   }
189
190   virtual ~EndToEndTest() {
191     // TODO(rtenneti): port RecycleUnusedPort if needed.
192     // RecycleUnusedPort(server_address_.port());
193     QuicInMemoryCachePeer::ResetForTests();
194   }
195
196   QuicTestClient* CreateQuicClient(QuicPacketWriterWrapper* writer) {
197     QuicTestClient* client = new QuicTestClient(
198         server_address_,
199         server_hostname_,
200         false,  // not secure
201         client_config_,
202         client_supported_versions_,
203         client_initial_flow_control_receive_window_);
204     client->UseWriter(writer);
205     client->Connect();
206     return client;
207   }
208
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;
213   }
214
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;
219   }
220
221   bool Initialize() {
222     // Start the server first, because CreateQuicClient() attempts
223     // to connect to the server.
224     StartServer();
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();
233   }
234
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();
240   }
241
242   virtual void TearDown() OVERRIDE {
243     StopServer();
244   }
245
246   void StartServer() {
247     server_thread_.reset(
248         new ServerThread(server_address_,
249                          server_config_,
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;
264   }
265
266   void StopServer() {
267     if (!server_started_)
268       return;
269     if (server_thread_.get()) {
270       server_thread_->Quit();
271       server_thread_->Join();
272     }
273   }
274
275   void AddToCache(StringPiece method,
276                   StringPiece path,
277                   StringPiece version,
278                   StringPiece response_code,
279                   StringPiece response_detail,
280                   StringPiece body) {
281     QuicInMemoryCache::GetInstance()->AddSimpleResponse(
282         method, path, version, response_code, response_detail, body);
283   }
284
285   void SetPacketLossPercentage(int32 loss) {
286     // TODO(rtenneti): enable when we can do random packet loss tests in
287     // chrome's tree.
288     if (loss != 0 && loss != 100)
289       return;
290     client_writer_->set_fake_packet_loss_percentage(loss);
291     server_writer_->set_fake_packet_loss_percentage(loss);
292   }
293
294   void SetPacketSendDelay(QuicTime::Delta delay) {
295     // TODO(rtenneti): enable when we can do random packet send delay tests in
296     // chrome's tree.
297     // client_writer_->set_fake_packet_delay(delay);
298     // server_writer_->set_fake_packet_delay(delay);
299   }
300
301   void SetReorderPercentage(int32 reorder) {
302     // TODO(rtenneti): enable when we can do random packet reorder tests in
303     // chrome's tree.
304     // client_writer_->set_fake_reorder_percentage(reorder);
305     // server_writer_->set_fake_reorder_percentage(reorder);
306   }
307
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
311   // per test.
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);
317     }
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);
321
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);
330     }
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();
336   }
337
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_;
353 };
354
355 // Run all end to end tests with all supported versions.
356 INSTANTIATE_TEST_CASE_P(EndToEndTests,
357                         EndToEndTest,
358                         ::testing::ValuesIn(GetTestParams()));
359
360 TEST_P(EndToEndTest, SimpleRequestResponse) {
361   ASSERT_TRUE(Initialize());
362
363   EXPECT_EQ(kFooResponseBody, client_->SendSynchronousRequest("/foo"));
364   EXPECT_EQ(200u, client_->response_headers()->parsed_response_code());
365 }
366
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) {
370   IPAddressNumber ip;
371   CHECK(net::ParseIPLiteralToNumber("::1", &ip));
372   server_address_ = IPEndPoint(ip, server_address_.port());
373   ASSERT_TRUE(Initialize());
374
375   EXPECT_EQ(kFooResponseBody, client_->SendSynchronousRequest("/foo"));
376   EXPECT_EQ(200u, client_->response_headers()->parsed_response_code());
377 }
378
379 TEST_P(EndToEndTest, SeparateFinPacket) {
380   ASSERT_TRUE(Initialize());
381
382   HTTPMessage request(HttpConstants::HTTP_1_1,
383                       HttpConstants::POST, "/foo");
384   request.set_has_complete_message(false);
385
386   client_->SendMessage(request);
387
388   client_->SendData(string(), true);
389
390   client_->WaitForResponse();
391   EXPECT_EQ(kFooResponseBody, client_->response_body());
392   EXPECT_EQ(200u, client_->response_headers()->parsed_response_code());
393
394   request.AddBody("foo", true);
395
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());
401 }
402
403 TEST_P(EndToEndTest, MultipleRequestResponse) {
404   ASSERT_TRUE(Initialize());
405
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());
410 }
411
412 TEST_P(EndToEndTest, MultipleClients) {
413   ASSERT_TRUE(Initialize());
414   scoped_ptr<QuicTestClient> client2(CreateQuicClient(NULL));
415
416   HTTPMessage request(HttpConstants::HTTP_1_1,
417                       HttpConstants::POST, "/foo");
418   request.AddHeader("content-length", "3");
419   request.set_has_complete_message(false);
420
421   client_->SendMessage(request);
422   client2->SendMessage(request);
423
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());
428
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());
433 }
434
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);
440
441   ASSERT_TRUE(Initialize());
442
443   EXPECT_EQ(kBarResponseBody, client_->SendSynchronousRequest(huge_request));
444   EXPECT_EQ(200u, client_->response_headers()->parsed_response_code());
445 }
446
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);
452
453   ASSERT_TRUE(Initialize());
454   SetPacketSendDelay(QuicTime::Delta::FromMilliseconds(2));
455   SetReorderPercentage(50);
456
457   EXPECT_EQ(kBarResponseBody, client_->SendSynchronousRequest(huge_request));
458   EXPECT_EQ(200u, client_->response_headers()->parsed_response_code());
459 }
460
461 TEST_P(EndToEndTest, PostMissingBytes) {
462   ASSERT_TRUE(Initialize());
463
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);
469
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());
475 }
476
477 // TODO(rtenneti): DISABLED_LargePostNoPacketLoss seems to be flaky.
478 // http://crbug.com/297040.
479 TEST_P(EndToEndTest, DISABLED_LargePostNoPacketLoss) {
480   ASSERT_TRUE(Initialize());
481
482   client_->client()->WaitForCryptoHandshakeConfirmed();
483
484   // 1 Mb body.
485   string body;
486   GenerateBody(&body, 1024 * 1024);
487
488   HTTPMessage request(HttpConstants::HTTP_1_1,
489                       HttpConstants::POST, "/foo");
490   request.AddBody(body, true);
491
492   EXPECT_EQ(kFooResponseBody, client_->SendCustomSynchronousRequest(request));
493   VerifyCleanConnection(false);
494 }
495
496 TEST_P(EndToEndTest, LargePostNoPacketLoss1sRTT) {
497   ASSERT_TRUE(Initialize());
498   SetPacketSendDelay(QuicTime::Delta::FromMilliseconds(1000));
499
500   client_->client()->WaitForCryptoHandshakeConfirmed();
501
502   // 1 Mb body.
503   string body;
504   GenerateBody(&body, 100 * 1024);
505
506   HTTPMessage request(HttpConstants::HTTP_1_1,
507                       HttpConstants::POST, "/foo");
508   request.AddBody(body, true);
509
510   EXPECT_EQ(kFooResponseBody, client_->SendCustomSynchronousRequest(request));
511   VerifyCleanConnection(false);
512 }
513
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());
519
520   // Wait for the server SHLO before upping the packet loss.
521   client_->client()->WaitForCryptoHandshakeConfirmed();
522   SetPacketLossPercentage(30);
523
524   // 10 Kb body.
525   string body;
526   GenerateBody(&body, 1024 * 10);
527
528   HTTPMessage request(HttpConstants::HTTP_1_1,
529                       HttpConstants::POST, "/foo");
530   request.AddBody(body, true);
531
532   EXPECT_EQ(kFooResponseBody, client_->SendCustomSynchronousRequest(request));
533 }
534
535 TEST_P(EndToEndTest, LargePostNoPacketLossWithDelayAndReordering) {
536   ASSERT_TRUE(Initialize());
537
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);
542
543   // 1 Mb body.
544   string body;
545   GenerateBody(&body, 1024 * 1024);
546
547   HTTPMessage request(HttpConstants::HTTP_1_1,
548                       HttpConstants::POST, "/foo");
549   request.AddBody(body, true);
550
551   EXPECT_EQ(kFooResponseBody, client_->SendCustomSynchronousRequest(request));
552   VerifyCleanConnection(true);
553 }
554
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());
560
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);
565
566   // 10 Kb body.
567   string body;
568   GenerateBody(&body, 1024 * 10);
569
570   HTTPMessage request(HttpConstants::HTTP_1_1,
571                       HttpConstants::POST, "/foo");
572   request.AddBody(body, true);
573
574   EXPECT_EQ(kFooResponseBody, client_->SendCustomSynchronousRequest(request));
575 }
576
577 // TODO(rtenneti): rch is investigating the root cause. Will enable after we
578 // find the bug.
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;
582
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());
586
587   string body;
588   GenerateBody(&body, 20480);
589
590   HTTPMessage request(HttpConstants::HTTP_1_1,
591                       HttpConstants::POST, "/foo");
592   request.AddBody(body, true);
593
594   EXPECT_EQ(kFooResponseBody, client_->SendCustomSynchronousRequest(request));
595   EXPECT_EQ(2, client_->client()->session()->GetNumSentClientHellos());
596
597   client_->Disconnect();
598
599   // The 0-RTT handshake should succeed.
600   client_->Connect();
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();
608   }
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());
613
614   client_->Disconnect();
615
616   // Restart the server so that the 0-RTT handshake will take 1 RTT.
617   StopServer();
618   server_writer_ = new PacketDroppingTestWriter();
619   StartServer();
620
621   client_->Connect();
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();
629   }
630   ASSERT_TRUE(client_->client()->connected());
631   EXPECT_EQ(kFooResponseBody, client_->SendCustomSynchronousRequest(request));
632   EXPECT_EQ(2, client_->client()->session()->GetNumSentClientHellos());
633   VerifyCleanConnection(false);
634 }
635
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());
640
641   // Wait for the server SHLO before upping the packet loss.
642   client_->client()->WaitForCryptoHandshakeConfirmed();
643   SetPacketLossPercentage(30);
644
645   client_->options()->max_packets_per_fec_group = 6;
646
647   string body;
648   GenerateBody(&body, 10240);
649
650   HTTPMessage request(HttpConstants::HTTP_1_1,
651                       HttpConstants::POST, "/foo");
652   request.AddBody(body, true);
653
654   EXPECT_EQ(kFooResponseBody, client_->SendCustomSynchronousRequest(request));
655   VerifyCleanConnection(true);
656 }
657
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);
667
668   client_->client()->WaitForCryptoHandshakeConfirmed();
669
670   // 1 Mb body.
671   string body;
672   GenerateBody(&body, 1024 * 1024);
673
674   HTTPMessage request(HttpConstants::HTTP_1_1,
675                       HttpConstants::POST, "/foo");
676   request.AddBody(body, true);
677
678   EXPECT_EQ(kFooResponseBody, client_->SendCustomSynchronousRequest(request));
679   VerifyCleanConnection(false);
680 }
681
682 TEST_P(EndToEndTest, InvalidStream) {
683   ASSERT_TRUE(Initialize());
684   client_->client()->WaitForCryptoHandshakeConfirmed();
685
686   string body;
687   GenerateBody(&body, kMaxPacketSize);
688
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);
695
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());
699 }
700
701 // TODO(rch): this test seems to cause net_unittests timeouts :|
702 TEST_P(EndToEndTest, DISABLED_MultipleTermination) {
703   ASSERT_TRUE(Initialize());
704
705   HTTPMessage request(HttpConstants::HTTP_1_1,
706                       HttpConstants::POST, "/foo");
707   request.AddHeader("content-length", "3");
708   request.set_has_complete_message(false);
709
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);
716
717   client_->SendData("bar", true);
718   client_->WaitForWriteToFlush();
719
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());
724
725   EXPECT_DFATAL(client_->SendData("eep", true), "Fin already buffered");
726 }
727
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.
734   Initialize();
735   while (client_->client()->connected()) {
736     client_->client()->WaitForEvents();
737   }
738 }
739
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);
745
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());
750 }
751
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);
759
760   ASSERT_TRUE(Initialize());
761   client_->client()->WaitForCryptoHandshakeConfirmed();
762   server_thread_->WaitForCryptoHandshakeConfirmed();
763
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();
774
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());
780
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());
785
786   EXPECT_EQ(100000u,
787             client_sent_packet_manager.GetRttStats()->initial_rtt_us());
788   EXPECT_EQ(1u, server_sent_packet_manager.GetRttStats()->initial_rtt_us());
789
790   // Now use the negotiated limits with packet loss.
791   SetPacketLossPercentage(30);
792
793   // 10 Kb body.
794   string body;
795   GenerateBody(&body, 1024 * 10);
796
797   HTTPMessage request(HttpConstants::HTTP_1_1,
798                       HttpConstants::POST, "/foo");
799   request.AddBody(body, true);
800
801   server_thread_->Resume();
802
803   EXPECT_EQ(kFooResponseBody, client_->SendCustomSynchronousRequest(request));
804 }
805
806 TEST_P(EndToEndTest, MaxInitialRTT) {
807   // Client tries to suggest twice the server's max initial rtt and the server
808   // uses the max.
809   client_config_.SetInitialRoundTripTimeUsToSend(
810       2 * kMaxInitialRoundTripTimeUs);
811
812   ASSERT_TRUE(Initialize());
813   client_->client()->WaitForCryptoHandshakeConfirmed();
814   server_thread_->WaitForCryptoHandshakeConfirmed();
815
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();
825
826   // Now that acks have been exchanged, the RTT estimate has decreased on the
827   // server and is not infinite on the client.
828   EXPECT_FALSE(
829       client_sent_packet_manager.GetRttStats()->SmoothedRtt().IsInfinite());
830   EXPECT_EQ(static_cast<int64>(kMaxInitialRoundTripTimeUs),
831             server_sent_packet_manager.GetRttStats()->initial_rtt_us());
832   EXPECT_GE(
833       static_cast<int64>(kMaxInitialRoundTripTimeUs),
834       server_sent_packet_manager.GetRttStats()->SmoothedRtt().ToMicroseconds());
835   server_thread_->Resume();
836 }
837
838 TEST_P(EndToEndTest, MinInitialRTT) {
839   // Client tries to suggest 0 and the server uses the default.
840   client_config_.SetInitialRoundTripTimeUsToSend(0);
841
842   ASSERT_TRUE(Initialize());
843   client_->client()->WaitForCryptoHandshakeConfirmed();
844   server_thread_->WaitForCryptoHandshakeConfirmed();
845
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();
856
857   // Now that acks have been exchanged, the RTT estimate has decreased on the
858   // server and is not infinite on the client.
859   EXPECT_FALSE(
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();
868 }
869
870 TEST_P(EndToEndTest, ResetConnection) {
871   ASSERT_TRUE(Initialize());
872   client_->client()->WaitForCryptoHandshakeConfirmed();
873
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());
879 }
880
881 TEST_P(EndToEndTest, MaxStreamsUberTest) {
882   SetPacketLossPercentage(1);
883   ASSERT_TRUE(Initialize());
884   string large_body;
885   GenerateBody(&large_body, 10240);
886   int max_streams = 100;
887
888   AddToCache("GET", "/large_response", "HTTP/1.1", "200", "OK", large_body);;
889
890   client_->client()->WaitForCryptoHandshakeConfirmed();
891   SetPacketLossPercentage(10);
892
893   for (int i = 0; i < max_streams; ++i) {
894     EXPECT_LT(0, client_->SendRequest("/large_response"));
895   }
896
897   // WaitForEvents waits 50ms and returns true if there are outstanding
898   // requests.
899   while (client_->client()->WaitForEvents() == true) {
900   }
901 }
902
903 TEST_P(EndToEndTest, StreamCancelErrorTest) {
904   ASSERT_TRUE(Initialize());
905   string small_body;
906   GenerateBody(&small_body, 256);
907
908   AddToCache("GET", "/small_response", "HTTP/1.1", "200", "OK", small_body);
909
910   client_->client()->WaitForCryptoHandshakeConfirmed();
911
912   QuicSession* session = client_->client()->session();
913   // Lose the request.
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);
921
922   // WaitForEvents waits 50ms and returns true if there are outstanding
923   // requests.
924   while (client_->client()->WaitForEvents() == true) {
925   }
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());
929 }
930
931 class WrongAddressWriter : public QuicPacketWriterWrapper {
932  public:
933   WrongAddressWriter() {
934     IPAddressNumber ip;
935     CHECK(net::ParseIPLiteralToNumber("127.0.0.2", &ip));
936     self_address_ = IPEndPoint(ip, 0);
937   }
938
939   virtual WriteResult WritePacket(
940       const char* buffer,
941       size_t buf_len,
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);
947   }
948
949   virtual bool IsWriteBlockedDataBuffered() const OVERRIDE {
950     return false;
951   }
952
953   IPEndPoint self_address_;
954 };
955
956 TEST_P(EndToEndTest, ConnectionMigration) {
957   ASSERT_TRUE(Initialize());
958
959   EXPECT_EQ(kFooResponseBody, client_->SendSynchronousRequest("/foo"));
960   EXPECT_EQ(200u, client_->response_headers()->parsed_response_code());
961
962   scoped_ptr<WrongAddressWriter> writer(new WrongAddressWriter());
963
964   writer->set_writer(new QuicDefaultPacketWriter(client_->client()->fd()));
965   QuicConnectionPeer::SetWriter(client_->client()->session()->connection(),
966                                 writer.get());
967
968   client_->SendSynchronousRequest("/bar");
969
970   EXPECT_EQ(QUIC_STREAM_CONNECTION_ERROR, client_->stream_error());
971   EXPECT_EQ(QUIC_ERROR_MIGRATING_ADDRESS, client_->connection_error());
972 }
973
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.
978
979   const uint32 kClientIFCW = 123456;
980   set_client_initial_flow_control_receive_window(kClientIFCW);
981
982   const uint32 kServerIFCW = 654321;
983   set_server_initial_flow_control_receive_window(kServerIFCW);
984
985   ASSERT_TRUE(Initialize());
986
987   // Values are exchanged during crypto handshake, so wait for that to finish.
988   client_->client()->WaitForCryptoHandshakeConfirmed();
989   server_thread_->WaitForCryptoHandshakeConfirmed();
990
991   // Client should have the right value for server's receive window.
992   EXPECT_EQ(kServerIFCW, client_->client()
993                              ->session()
994                              ->config()
995                              ->ReceivedInitialFlowControlWindowBytes());
996
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();
1005 }
1006
1007 }  // namespace
1008 }  // namespace test
1009 }  // namespace tools
1010 }  // namespace net