Upstream version 5.34.104.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 "net/base/ip_endpoint.h"
16 #include "net/quic/congestion_control/tcp_cubic_sender.h"
17 #include "net/quic/crypto/aes_128_gcm_12_encrypter.h"
18 #include "net/quic/crypto/null_encrypter.h"
19 #include "net/quic/quic_framer.h"
20 #include "net/quic/quic_packet_creator.h"
21 #include "net/quic/quic_protocol.h"
22 #include "net/quic/quic_sent_packet_manager.h"
23 #include "net/quic/test_tools/quic_connection_peer.h"
24 #include "net/quic/test_tools/quic_session_peer.h"
25 #include "net/quic/test_tools/quic_test_utils.h"
26 #include "net/quic/test_tools/reliable_quic_stream_peer.h"
27 #include "net/test/gtest_util.h"
28 #include "net/tools/quic/quic_epoll_connection_helper.h"
29 #include "net/tools/quic/quic_in_memory_cache.h"
30 #include "net/tools/quic/quic_packet_writer_wrapper.h"
31 #include "net/tools/quic/quic_server.h"
32 #include "net/tools/quic/quic_socket_utils.h"
33 #include "net/tools/quic/quic_spdy_client_stream.h"
34 #include "net/tools/quic/test_tools/http_message_test_utils.h"
35 #include "net/tools/quic/test_tools/packet_dropping_test_writer.h"
36 #include "net/tools/quic/test_tools/quic_client_peer.h"
37 #include "net/tools/quic/test_tools/quic_dispatcher_peer.h"
38 #include "net/tools/quic/test_tools/quic_in_memory_cache_peer.h"
39 #include "net/tools/quic/test_tools/quic_server_peer.h"
40 #include "net/tools/quic/test_tools/quic_test_client.h"
41 #include "net/tools/quic/test_tools/server_thread.h"
42 #include "testing/gtest/include/gtest/gtest.h"
43
44 using base::StringPiece;
45 using base::WaitableEvent;
46 using net::test::QuicConnectionPeer;
47 using net::test::QuicSessionPeer;
48 using net::test::ReliableQuicStreamPeer;
49 using net::tools::test::PacketDroppingTestWriter;
50 using net::tools::test::QuicDispatcherPeer;
51 using net::tools::test::QuicServerPeer;
52 using std::ostream;
53 using std::string;
54 using std::vector;
55
56 namespace net {
57 namespace tools {
58 namespace test {
59 namespace {
60
61 const char* kFooResponseBody = "Artichoke hearts make me happy.";
62 const char* kBarResponseBody = "Palm hearts are pretty delicious, also.";
63
64 void GenerateBody(string* body, int length) {
65   body->clear();
66   body->reserve(length);
67   for (int i = 0; i < length; ++i) {
68     body->append(1, static_cast<char>(32 + i % (126 - 32)));
69   }
70 }
71
72 // Run all tests with the cross products of all versions.
73 struct TestParams {
74   TestParams(const QuicVersionVector& client_supported_versions,
75              const QuicVersionVector& server_supported_versions,
76              QuicVersion negotiated_version,
77              bool use_pacing)
78       : client_supported_versions(client_supported_versions),
79         server_supported_versions(server_supported_versions),
80         negotiated_version(negotiated_version),
81         use_pacing(use_pacing) {
82   }
83
84   friend ostream& operator<<(ostream& os, const TestParams& p) {
85     os << "{ server_supported_versions: "
86        << QuicVersionVectorToString(p.server_supported_versions);
87     os << " client_supported_versions: "
88        << QuicVersionVectorToString(p.client_supported_versions);
89     os << " negotiated_version: " << QuicVersionToString(p.negotiated_version);
90     os << " use_pacing: " << p.use_pacing << " }";
91     return os;
92   }
93
94   QuicVersionVector client_supported_versions;
95   QuicVersionVector server_supported_versions;
96   QuicVersion negotiated_version;
97   bool use_pacing;
98 };
99
100 // Constructs various test permutations.
101 vector<TestParams> GetTestParams() {
102   vector<TestParams> params;
103   QuicVersionVector all_supported_versions = QuicSupportedVersions();
104   for (int use_pacing = 0; use_pacing < 2; ++use_pacing) {
105     // TODO(rch): since 13 is not 0-RTT compatible with 12, we can not
106     // have the client support both at the same time.
107 #if 0
108     // Add an entry for server and client supporting all versions.
109     params.push_back(TestParams(all_supported_versions,
110                                 all_supported_versions,
111                                 all_supported_versions[0],
112                                 use_pacing != 0));
113 #endif
114
115     // Test client supporting 1 version and server supporting all versions.
116     // Simulate an old client and exercise version downgrade in the server.
117     // No protocol negotiation should occur. Skip the i = 0 case because it
118     // is essentially the same as the default case.
119     // TODO(rch): When QUIC_VERSION_12 is removed, change the intialization
120     // of i from 0 back to 1.
121     for (size_t i = 0; i < all_supported_versions.size(); ++i) {
122       QuicVersionVector client_supported_versions;
123       client_supported_versions.push_back(all_supported_versions[i]);
124       params.push_back(TestParams(client_supported_versions,
125                                   all_supported_versions,
126                                   client_supported_versions[0],
127                                   use_pacing != 0));
128     }
129
130     // TODO(rch): since 13 is not 0-RTT compatible with 12, we can not
131     // have the client support both at the same time.
132 #if 0
133     // Test client supporting all versions and server supporting 1 version.
134     // Simulate an old server and exercise version downgrade in the client.
135     // Protocol negotiation should occur. Skip the i = 0 case because it is
136     // essentially the same as the default case.
137     for (size_t i = 1; i < all_supported_versions.size(); ++i) {
138       QuicVersionVector server_supported_versions;
139       server_supported_versions.push_back(all_supported_versions[i]);
140       params.push_back(TestParams(all_supported_versions,
141                                   server_supported_versions,
142                                   server_supported_versions[0],
143                                   use_pacing != 0));
144     }
145 #endif
146   }
147   return params;
148 }
149
150 class ServerDelegate : public PacketDroppingTestWriter::Delegate {
151  public:
152   explicit ServerDelegate(QuicDispatcher* dispatcher)
153       : dispatcher_(dispatcher) {}
154   virtual ~ServerDelegate() {}
155   virtual void OnCanWrite() OVERRIDE { dispatcher_->OnCanWrite(); }
156  private:
157   QuicDispatcher* dispatcher_;
158 };
159
160 class ClientDelegate : public PacketDroppingTestWriter::Delegate {
161  public:
162   explicit ClientDelegate(QuicClient* client) : client_(client) {}
163   virtual ~ClientDelegate() {}
164   virtual void OnCanWrite() OVERRIDE {
165     EpollEvent event(EPOLLOUT, false);
166     client_->OnEvent(client_->fd(), &event);
167   }
168  private:
169   QuicClient* client_;
170 };
171
172 class EndToEndTest : public ::testing::TestWithParam<TestParams> {
173  protected:
174   EndToEndTest()
175       : server_hostname_("example.com"),
176         server_started_(false),
177         strike_register_no_startup_period_(false) {
178     net::IPAddressNumber ip;
179     CHECK(net::ParseIPLiteralToNumber("127.0.0.1", &ip));
180     server_address_ = IPEndPoint(ip, 0);
181
182     client_supported_versions_ = GetParam().client_supported_versions;
183     server_supported_versions_ = GetParam().server_supported_versions;
184     negotiated_version_ = GetParam().negotiated_version;
185     FLAGS_enable_quic_pacing = GetParam().use_pacing;
186     VLOG(1) << "Using Configuration: " << GetParam();
187
188     client_config_.SetDefaults();
189     server_config_.SetDefaults();
190     server_config_.set_initial_round_trip_time_us(kMaxInitialRoundTripTimeUs,
191                                                   0);
192
193     QuicInMemoryCachePeer::ResetForTests();
194     AddToCache("GET", "https://www.google.com/foo",
195                "HTTP/1.1", "200", "OK", kFooResponseBody);
196     AddToCache("GET", "https://www.google.com/bar",
197                "HTTP/1.1", "200", "OK", kBarResponseBody);
198   }
199
200   virtual ~EndToEndTest() {
201     // TODO(rtenneti): port RecycleUnusedPort if needed.
202     // RecycleUnusedPort(server_address_.port());
203     QuicInMemoryCachePeer::ResetForTests();
204   }
205
206   QuicTestClient* CreateQuicClient(QuicPacketWriterWrapper* writer) {
207     QuicTestClient* client = new QuicTestClient(server_address_,
208                                                 server_hostname_,
209                                                 false,  // not secure
210                                                 client_config_,
211                                                 client_supported_versions_);
212     client->UseWriter(writer);
213     client->Connect();
214     return client;
215   }
216
217   bool Initialize() {
218     // Start the server first, because CreateQuicClient() attempts
219     // to connect to the server.
220     StartServer();
221     client_.reset(CreateQuicClient(client_writer_));
222     static EpollEvent event(EPOLLOUT, false);
223     client_writer_->Initialize(
224         reinterpret_cast<QuicEpollConnectionHelper*>(
225             QuicConnectionPeer::GetHelper(
226                 client_->client()->session()->connection())),
227         new ClientDelegate(client_->client()));
228     return client_->client()->connected();
229   }
230
231   virtual void SetUp() OVERRIDE {
232     // The ownership of these gets transferred to the QuicPacketWriterWrapper
233     // and QuicDispatcher when Initialize() is executed.
234     client_writer_ = new PacketDroppingTestWriter();
235     server_writer_ = new PacketDroppingTestWriter();
236   }
237
238   virtual void TearDown() OVERRIDE {
239     StopServer();
240   }
241
242   void StartServer() {
243     server_thread_.reset(new ServerThread(server_address_, server_config_,
244                                           server_supported_versions_,
245                                           strike_register_no_startup_period_));
246     server_thread_->Initialize();
247     server_address_ = IPEndPoint(server_address_.address(),
248                                  server_thread_->GetPort());
249     QuicDispatcher* dispatcher =
250         QuicServerPeer::GetDispatcher(server_thread_->server());
251     QuicDispatcherPeer::UseWriter(dispatcher, server_writer_);
252     server_writer_->Initialize(
253         QuicDispatcherPeer::GetHelper(dispatcher),
254         new ServerDelegate(dispatcher));
255     server_thread_->Start();
256     server_started_ = true;
257   }
258
259   void StopServer() {
260     if (!server_started_)
261       return;
262     if (server_thread_.get()) {
263       server_thread_->Quit();
264       server_thread_->Join();
265     }
266   }
267
268   void AddToCache(StringPiece method,
269                   StringPiece path,
270                   StringPiece version,
271                   StringPiece response_code,
272                   StringPiece response_detail,
273                   StringPiece body) {
274     QuicInMemoryCache::GetInstance()->AddSimpleResponse(
275         method, path, version, response_code, response_detail, body);
276   }
277
278   void SetPacketLossPercentage(int32 loss) {
279     // TODO(rtenneti): enable when we can do random packet loss tests in
280     // chrome's tree.
281     if (loss != 0 && loss != 100)
282       return;
283     client_writer_->set_fake_packet_loss_percentage(loss);
284     server_writer_->set_fake_packet_loss_percentage(loss);
285   }
286
287   void SetPacketSendDelay(QuicTime::Delta delay) {
288     // TODO(rtenneti): enable when we can do random packet send delay tests in
289     // chrome's tree.
290     // client_writer_->set_fake_packet_delay(delay);
291     // server_writer_->set_fake_packet_delay(delay);
292   }
293
294   void SetReorderPercentage(int32 reorder) {
295     // TODO(rtenneti): enable when we can do random packet reorder tests in
296     // chrome's tree.
297     // client_writer_->set_fake_reorder_percentage(reorder);
298     // server_writer_->set_fake_reorder_percentage(reorder);
299   }
300
301   IPEndPoint server_address_;
302   string server_hostname_;
303   scoped_ptr<ServerThread> server_thread_;
304   scoped_ptr<QuicTestClient> client_;
305   PacketDroppingTestWriter* client_writer_;
306   PacketDroppingTestWriter* server_writer_;
307   bool server_started_;
308   QuicConfig client_config_;
309   QuicConfig server_config_;
310   QuicVersionVector client_supported_versions_;
311   QuicVersionVector server_supported_versions_;
312   QuicVersion negotiated_version_;
313   bool strike_register_no_startup_period_;
314 };
315
316 // Run all end to end tests with all supported versions.
317 INSTANTIATE_TEST_CASE_P(EndToEndTests,
318                         EndToEndTest,
319                         ::testing::ValuesIn(GetTestParams()));
320
321 TEST_P(EndToEndTest, SimpleRequestResponse) {
322   ASSERT_TRUE(Initialize());
323
324   EXPECT_EQ(kFooResponseBody, client_->SendSynchronousRequest("/foo"));
325   EXPECT_EQ(200u, client_->response_headers()->parsed_response_code());
326 }
327
328 // TODO(rch): figure out how to detect missing v6 supprt (like on the linux
329 // try bots) and selectively disable this test.
330 TEST_P(EndToEndTest, DISABLED_SimpleRequestResponsev6) {
331   IPAddressNumber ip;
332   CHECK(net::ParseIPLiteralToNumber("::1", &ip));
333   server_address_ = IPEndPoint(ip, server_address_.port());
334   ASSERT_TRUE(Initialize());
335
336   EXPECT_EQ(kFooResponseBody, client_->SendSynchronousRequest("/foo"));
337   EXPECT_EQ(200u, client_->response_headers()->parsed_response_code());
338 }
339
340 TEST_P(EndToEndTest, SeparateFinPacket) {
341   ASSERT_TRUE(Initialize());
342
343   HTTPMessage request(HttpConstants::HTTP_1_1,
344                       HttpConstants::POST, "/foo");
345   request.set_has_complete_message(false);
346
347   client_->SendMessage(request);
348
349   client_->SendData(string(), true);
350
351   client_->WaitForResponse();
352   EXPECT_EQ(kFooResponseBody, client_->response_body());
353   EXPECT_EQ(200u, client_->response_headers()->parsed_response_code());
354
355   request.AddBody("foo", true);
356
357   client_->SendMessage(request);
358   client_->SendData(string(), true);
359   client_->WaitForResponse();
360   EXPECT_EQ(kFooResponseBody, client_->response_body());
361   EXPECT_EQ(200u, client_->response_headers()->parsed_response_code());
362 }
363
364 TEST_P(EndToEndTest, MultipleRequestResponse) {
365   ASSERT_TRUE(Initialize());
366
367   EXPECT_EQ(kFooResponseBody, client_->SendSynchronousRequest("/foo"));
368   EXPECT_EQ(200u, client_->response_headers()->parsed_response_code());
369   EXPECT_EQ(kBarResponseBody, client_->SendSynchronousRequest("/bar"));
370   EXPECT_EQ(200u, client_->response_headers()->parsed_response_code());
371 }
372
373 TEST_P(EndToEndTest, MultipleClients) {
374   ASSERT_TRUE(Initialize());
375   scoped_ptr<QuicTestClient> client2(CreateQuicClient(NULL));
376
377   HTTPMessage request(HttpConstants::HTTP_1_1,
378                       HttpConstants::POST, "/foo");
379   request.AddHeader("content-length", "3");
380   request.set_has_complete_message(false);
381
382   client_->SendMessage(request);
383   client2->SendMessage(request);
384
385   client_->SendData("bar", true);
386   client_->WaitForResponse();
387   EXPECT_EQ(kFooResponseBody, client_->response_body());
388   EXPECT_EQ(200u, client_->response_headers()->parsed_response_code());
389
390   client2->SendData("eep", true);
391   client2->WaitForResponse();
392   EXPECT_EQ(kFooResponseBody, client2->response_body());
393   EXPECT_EQ(200u, client2->response_headers()->parsed_response_code());
394 }
395
396 TEST_P(EndToEndTest, RequestOverMultiplePackets) {
397   // Send a large enough request to guarantee fragmentation.
398   string huge_request =
399       "https://www.google.com/some/path?query=" + string(kMaxPacketSize, '.');
400   AddToCache("GET", huge_request, "HTTP/1.1", "200", "OK", kBarResponseBody);
401
402   ASSERT_TRUE(Initialize());
403
404   EXPECT_EQ(kBarResponseBody, client_->SendSynchronousRequest(huge_request));
405   EXPECT_EQ(200u, client_->response_headers()->parsed_response_code());
406 }
407
408 TEST_P(EndToEndTest, MultiplePacketsRandomOrder) {
409   // Send a large enough request to guarantee fragmentation.
410   string huge_request =
411       "https://www.google.com/some/path?query=" + string(kMaxPacketSize, '.');
412   AddToCache("GET", huge_request, "HTTP/1.1", "200", "OK", kBarResponseBody);
413
414   ASSERT_TRUE(Initialize());
415   SetPacketSendDelay(QuicTime::Delta::FromMilliseconds(2));
416   SetReorderPercentage(50);
417
418   EXPECT_EQ(kBarResponseBody, client_->SendSynchronousRequest(huge_request));
419   EXPECT_EQ(200u, client_->response_headers()->parsed_response_code());
420 }
421
422 TEST_P(EndToEndTest, PostMissingBytes) {
423   ASSERT_TRUE(Initialize());
424
425   // Add a content length header with no body.
426   HTTPMessage request(HttpConstants::HTTP_1_1,
427                       HttpConstants::POST, "/foo");
428   request.AddHeader("content-length", "3");
429   request.set_skip_message_validation(true);
430
431   // This should be detected as stream fin without complete request,
432   // triggering an error response.
433   client_->SendCustomSynchronousRequest(request);
434   EXPECT_EQ("bad", client_->response_body());
435   EXPECT_EQ(500u, client_->response_headers()->parsed_response_code());
436 }
437
438 TEST_P(EndToEndTest, LargePostNoPacketLoss) {
439   ASSERT_TRUE(Initialize());
440
441   client_->client()->WaitForCryptoHandshakeConfirmed();
442
443   // 1 Mb body.
444   string body;
445   GenerateBody(&body, 1024 * 1024);
446
447   HTTPMessage request(HttpConstants::HTTP_1_1,
448                       HttpConstants::POST, "/foo");
449   request.AddBody(body, true);
450
451   EXPECT_EQ(kFooResponseBody, client_->SendCustomSynchronousRequest(request));
452   QuicConnectionStats stats =
453       client_->client()->session()->connection()->GetStats();
454   // TODO(ianswett): Restore the packets_lost expectation when fixing b/12887145
455   // EXPECT_EQ(0u, stats.packets_lost);
456   EXPECT_EQ(0u, stats.rto_count);
457 }
458
459 TEST_P(EndToEndTest, LargePostNoPacketLoss1sRTT) {
460   ASSERT_TRUE(Initialize());
461   SetPacketSendDelay(QuicTime::Delta::FromMilliseconds(1000));
462
463   client_->client()->WaitForCryptoHandshakeConfirmed();
464
465   // 1 Mb body.
466   string body;
467   GenerateBody(&body, 100 * 1024);
468
469   HTTPMessage request(HttpConstants::HTTP_1_1,
470                       HttpConstants::POST, "/foo");
471   request.AddBody(body, true);
472
473   EXPECT_EQ(kFooResponseBody, client_->SendCustomSynchronousRequest(request));
474   QuicConnectionStats stats =
475       client_->client()->session()->connection()->GetStats();
476   EXPECT_EQ(0u, stats.packets_lost);
477 }
478
479 TEST_P(EndToEndTest, LargePostWithPacketLoss) {
480   // Connect with lower fake packet loss than we'd like to test.  Until
481   // b/10126687 is fixed, losing handshake packets is pretty brutal.
482   SetPacketLossPercentage(5);
483   ASSERT_TRUE(Initialize());
484
485   // Wait for the server SHLO before upping the packet loss.
486   client_->client()->WaitForCryptoHandshakeConfirmed();
487   SetPacketLossPercentage(30);
488
489   // 10 Kb body.
490   string body;
491   GenerateBody(&body, 1024 * 10);
492
493   HTTPMessage request(HttpConstants::HTTP_1_1,
494                       HttpConstants::POST, "/foo");
495   request.AddBody(body, true);
496
497   EXPECT_EQ(kFooResponseBody, client_->SendCustomSynchronousRequest(request));
498 }
499
500 TEST_P(EndToEndTest, LargePostNoPacketLossWithDelayAndReordering) {
501   ASSERT_TRUE(Initialize());
502
503   client_->client()->WaitForCryptoHandshakeConfirmed();
504   // Both of these must be called when the writer is not actively used.
505   SetPacketSendDelay(QuicTime::Delta::FromMilliseconds(2));
506   SetReorderPercentage(30);
507
508   // 1 Mb body.
509   string body;
510   GenerateBody(&body, 1024 * 1024);
511
512   HTTPMessage request(HttpConstants::HTTP_1_1,
513                       HttpConstants::POST, "/foo");
514   request.AddBody(body, true);
515
516   EXPECT_EQ(kFooResponseBody, client_->SendCustomSynchronousRequest(request));
517 }
518
519 TEST_P(EndToEndTest, LargePostWithPacketLossAndBlockedSocket) {
520   // Connect with lower fake packet loss than we'd like to test.  Until
521   // b/10126687 is fixed, losing handshake packets is pretty brutal.
522   SetPacketLossPercentage(5);
523   ASSERT_TRUE(Initialize());
524
525   // Wait for the server SHLO before upping the packet loss.
526   client_->client()->WaitForCryptoHandshakeConfirmed();
527   SetPacketLossPercentage(10);
528   client_writer_->set_fake_blocked_socket_percentage(10);
529
530   // 10 Kb body.
531   string body;
532   GenerateBody(&body, 1024 * 10);
533
534   HTTPMessage request(HttpConstants::HTTP_1_1,
535                       HttpConstants::POST, "/foo");
536   request.AddBody(body, true);
537
538   EXPECT_EQ(kFooResponseBody, client_->SendCustomSynchronousRequest(request));
539 }
540
541 // TODO(rtenneti): rch is investigating the root cause. Will enable after we
542 // find the bug.
543 TEST_P(EndToEndTest, DISABLED_LargePostZeroRTTFailure) {
544   // Have the server accept 0-RTT without waiting a startup period.
545   strike_register_no_startup_period_ = true;
546
547   // Send a request and then disconnect. This prepares the client to attempt
548   // a 0-RTT handshake for the next request.
549   ASSERT_TRUE(Initialize());
550
551   string body;
552   GenerateBody(&body, 20480);
553
554   HTTPMessage request(HttpConstants::HTTP_1_1,
555                       HttpConstants::POST, "/foo");
556   request.AddBody(body, true);
557
558   EXPECT_EQ(kFooResponseBody, client_->SendCustomSynchronousRequest(request));
559   EXPECT_EQ(2, client_->client()->session()->GetNumSentClientHellos());
560
561   client_->Disconnect();
562
563   // The 0-RTT handshake should succeed.
564   client_->Connect();
565   client_->WaitForResponseForMs(-1);
566   ASSERT_TRUE(client_->client()->connected());
567   EXPECT_EQ(kFooResponseBody, client_->SendCustomSynchronousRequest(request));
568   EXPECT_EQ(1, client_->client()->session()->GetNumSentClientHellos());
569
570   client_->Disconnect();
571
572   // Restart the server so that the 0-RTT handshake will take 1 RTT.
573   StopServer();
574   server_writer_ = new PacketDroppingTestWriter();
575   StartServer();
576
577   client_->Connect();
578   ASSERT_TRUE(client_->client()->connected());
579   EXPECT_EQ(kFooResponseBody, client_->SendCustomSynchronousRequest(request));
580   EXPECT_EQ(2, client_->client()->session()->GetNumSentClientHellos());
581 }
582
583 // TODO(ianswett): Enable once b/9295090 is fixed.
584 TEST_P(EndToEndTest, DISABLED_LargePostFEC) {
585   SetPacketLossPercentage(30);
586   ASSERT_TRUE(Initialize());
587   client_->options()->max_packets_per_fec_group = 6;
588
589   string body;
590   GenerateBody(&body, 10240);
591
592   HTTPMessage request(HttpConstants::HTTP_1_1,
593                       HttpConstants::POST, "/foo");
594   request.AddBody(body, true);
595
596   EXPECT_EQ(kFooResponseBody, client_->SendCustomSynchronousRequest(request));
597 }
598
599 TEST_P(EndToEndTest, LargePostLargeBuffer) {
600   ASSERT_TRUE(Initialize());
601   SetPacketSendDelay(QuicTime::Delta::FromMicroseconds(1));
602   // 1Mbit per second with a 128k buffer from server to client.  Wireless
603   // clients commonly have larger buffers, but our max CWND is 200.
604   server_writer_->set_max_bandwidth_and_buffer_size(
605       QuicBandwidth::FromBytesPerSecond(256 * 1024), 128 * 1024);
606
607   client_->client()->WaitForCryptoHandshakeConfirmed();
608
609   // 1 Mb body.
610   string body;
611   GenerateBody(&body, 1024 * 1024);
612
613   HTTPMessage request(HttpConstants::HTTP_1_1,
614                       HttpConstants::POST, "/foo");
615   request.AddBody(body, true);
616
617   EXPECT_EQ(kFooResponseBody, client_->SendCustomSynchronousRequest(request));
618 }
619
620 TEST_P(EndToEndTest, InvalidStream) {
621   ASSERT_TRUE(Initialize());
622   client_->client()->WaitForCryptoHandshakeConfirmed();
623
624   string body;
625   GenerateBody(&body, kMaxPacketSize);
626
627   HTTPMessage request(HttpConstants::HTTP_1_1,
628                       HttpConstants::POST, "/foo");
629   request.AddBody(body, true);
630   // Force the client to write with a stream ID belonging to a nonexistent
631   // server-side stream.
632   QuicSessionPeer::SetNextStreamId(client_->client()->session(), 2);
633
634   client_->SendCustomSynchronousRequest(request);
635   // EXPECT_EQ(QUIC_STREAM_CONNECTION_ERROR, client_->stream_error());
636   EXPECT_EQ(QUIC_PACKET_FOR_NONEXISTENT_STREAM, client_->connection_error());
637 }
638
639 // TODO(rch): this test seems to cause net_unittests timeouts :|
640 TEST_P(EndToEndTest, DISABLED_MultipleTermination) {
641   ASSERT_TRUE(Initialize());
642
643   HTTPMessage request(HttpConstants::HTTP_1_1,
644                       HttpConstants::POST, "/foo");
645   request.AddHeader("content-length", "3");
646   request.set_has_complete_message(false);
647
648   // Set the offset so we won't frame.  Otherwise when we pick up termination
649   // before HTTP framing is complete, we send an error and close the stream,
650   // and the second write is picked up as writing on a closed stream.
651   QuicSpdyClientStream* stream = client_->GetOrCreateStream();
652   ASSERT_TRUE(stream != NULL);
653   ReliableQuicStreamPeer::SetStreamBytesWritten(3, stream);
654
655   client_->SendData("bar", true);
656   client_->WaitForWriteToFlush();
657
658   // By default the stream protects itself from writes after terminte is set.
659   // Override this to test the server handling buggy clients.
660   ReliableQuicStreamPeer::SetWriteSideClosed(
661       false, client_->GetOrCreateStream());
662
663   EXPECT_DFATAL(client_->SendData("eep", true), "Fin already buffered");
664 }
665
666 TEST_P(EndToEndTest, Timeout) {
667   client_config_.set_idle_connection_state_lifetime(
668       QuicTime::Delta::FromMicroseconds(500),
669       QuicTime::Delta::FromMicroseconds(500));
670   // Note: we do NOT ASSERT_TRUE: we may time out during initial handshake:
671   // that's enough to validate timeout in this case.
672   Initialize();
673   while (client_->client()->connected()) {
674     client_->client()->WaitForEvents();
675   }
676 }
677
678 TEST_P(EndToEndTest, LimitMaxOpenStreams) {
679   // Server limits the number of max streams to 2.
680   server_config_.set_max_streams_per_connection(2, 2);
681   // Client tries to negotiate for 10.
682   client_config_.set_max_streams_per_connection(10, 5);
683
684   ASSERT_TRUE(Initialize());
685   client_->client()->WaitForCryptoHandshakeConfirmed();
686   QuicConfig* client_negotiated_config = client_->client()->session()->config();
687   EXPECT_EQ(2u, client_negotiated_config->max_streams_per_connection());
688 }
689
690 // TODO(rtenneti): DISABLED_LimitCongestionWindowAndRTT seems to be flaky.
691 // http://crbug.com/321870.
692 TEST_P(EndToEndTest, DISABLED_LimitCongestionWindowAndRTT) {
693   server_config_.set_server_initial_congestion_window(kMaxInitialWindow,
694                                                       kDefaultInitialWindow);
695   // Client tries to negotiate twice the server's max and negotiation settles
696   // on the max.
697   client_config_.set_server_initial_congestion_window(2 * kMaxInitialWindow,
698                                                       kDefaultInitialWindow);
699   client_config_.set_initial_round_trip_time_us(1, 1);
700
701   ASSERT_TRUE(Initialize());
702   client_->client()->WaitForCryptoHandshakeConfirmed();
703   server_thread_->WaitForCryptoHandshakeConfirmed();
704
705   // Pause the server so we can access the server's internals without races.
706   server_thread_->Pause();
707   QuicDispatcher* dispatcher =
708       QuicServerPeer::GetDispatcher(server_thread_->server());
709   ASSERT_EQ(1u, dispatcher->session_map().size());
710   QuicSession* session = dispatcher->session_map().begin()->second;
711   QuicConfig* client_negotiated_config = client_->client()->session()->config();
712   QuicConfig* server_negotiated_config = session->config();
713   const QuicSentPacketManager& client_sent_packet_manager =
714       client_->client()->session()->connection()->sent_packet_manager();
715   const QuicSentPacketManager& server_sent_packet_manager =
716       session->connection()->sent_packet_manager();
717
718   EXPECT_EQ(kMaxInitialWindow,
719             client_negotiated_config->server_initial_congestion_window());
720   EXPECT_EQ(kMaxInitialWindow,
721             server_negotiated_config->server_initial_congestion_window());
722   // The client shouldn't set it's initial window based on the negotiated value.
723   EXPECT_EQ(kDefaultInitialWindow * kDefaultTCPMSS,
724             client_sent_packet_manager.GetCongestionWindow());
725   EXPECT_EQ(kMaxInitialWindow * kDefaultTCPMSS,
726             server_sent_packet_manager.GetCongestionWindow());
727
728   EXPECT_EQ(FLAGS_enable_quic_pacing,
729             server_sent_packet_manager.using_pacing());
730   EXPECT_EQ(FLAGS_enable_quic_pacing,
731             client_sent_packet_manager.using_pacing());
732
733   EXPECT_EQ(1u, client_negotiated_config->initial_round_trip_time_us());
734   EXPECT_EQ(1u, server_negotiated_config->initial_round_trip_time_us());
735
736   // Now use the negotiated limits with packet loss.
737   SetPacketLossPercentage(30);
738
739   // 10 Kb body.
740   string body;
741   GenerateBody(&body, 1024 * 10);
742
743   HTTPMessage request(HttpConstants::HTTP_1_1,
744                       HttpConstants::POST, "/foo");
745   request.AddBody(body, true);
746
747   server_thread_->Resume();
748
749   EXPECT_EQ(kFooResponseBody, client_->SendCustomSynchronousRequest(request));
750 }
751
752 TEST_P(EndToEndTest, InitialRTT) {
753   // Client tries to negotiate twice the server's max and negotiation settles
754   // on the max.
755   client_config_.set_initial_round_trip_time_us(2 * kMaxInitialRoundTripTimeUs,
756                                                 0);
757
758   ASSERT_TRUE(Initialize());
759   client_->client()->WaitForCryptoHandshakeConfirmed();
760   server_thread_->WaitForCryptoHandshakeConfirmed();
761
762   server_thread_->Pause();
763   QuicDispatcher* dispatcher =
764       QuicServerPeer::GetDispatcher(server_thread_->server());
765   ASSERT_EQ(1u, dispatcher->session_map().size());
766   QuicSession* session = dispatcher->session_map().begin()->second;
767   QuicConfig* client_negotiated_config = client_->client()->session()->config();
768   QuicConfig* server_negotiated_config = session->config();
769   const QuicSentPacketManager& client_sent_packet_manager =
770       client_->client()->session()->connection()->sent_packet_manager();
771   const QuicSentPacketManager& server_sent_packet_manager =
772       session->connection()->sent_packet_manager();
773
774   EXPECT_EQ(kMaxInitialRoundTripTimeUs,
775             client_negotiated_config->initial_round_trip_time_us());
776   EXPECT_EQ(kMaxInitialRoundTripTimeUs,
777             server_negotiated_config->initial_round_trip_time_us());
778   // Now that acks have been exchanged, the RTT estimate has decreased on the
779   // server and is not infinite on the client.
780   EXPECT_FALSE(client_sent_packet_manager.SmoothedRtt().IsInfinite());
781   EXPECT_GE(static_cast<int64>(kMaxInitialRoundTripTimeUs),
782             server_sent_packet_manager.SmoothedRtt().ToMicroseconds());
783   server_thread_->Resume();
784 }
785
786 TEST_P(EndToEndTest, ResetConnection) {
787   ASSERT_TRUE(Initialize());
788   client_->client()->WaitForCryptoHandshakeConfirmed();
789
790   EXPECT_EQ(kFooResponseBody, client_->SendSynchronousRequest("/foo"));
791   EXPECT_EQ(200u, client_->response_headers()->parsed_response_code());
792   client_->ResetConnection();
793   EXPECT_EQ(kBarResponseBody, client_->SendSynchronousRequest("/bar"));
794   EXPECT_EQ(200u, client_->response_headers()->parsed_response_code());
795 }
796
797 TEST_P(EndToEndTest, MaxStreamsUberTest) {
798   SetPacketLossPercentage(1);
799   ASSERT_TRUE(Initialize());
800   string large_body;
801   GenerateBody(&large_body, 10240);
802   int max_streams = 100;
803
804   AddToCache("GET", "/large_response", "HTTP/1.1", "200", "OK", large_body);;
805
806   client_->client()->WaitForCryptoHandshakeConfirmed();
807   SetPacketLossPercentage(10);
808
809   for (int i = 0; i < max_streams; ++i) {
810     EXPECT_LT(0, client_->SendRequest("/large_response"));
811   }
812
813   // WaitForEvents waits 50ms and returns true if there are outstanding
814   // requests.
815   while (client_->client()->WaitForEvents() == true) {
816   }
817 }
818
819 TEST_P(EndToEndTest, StreamCancelErrorTest) {
820   ASSERT_TRUE(Initialize());
821   string small_body;
822   GenerateBody(&small_body, 256);
823
824   AddToCache("GET", "/small_response", "HTTP/1.1", "200", "OK", small_body);
825
826   client_->client()->WaitForCryptoHandshakeConfirmed();
827
828   QuicSession* session = client_->client()->session();
829   // Lose the request.
830   SetPacketLossPercentage(100);
831   EXPECT_LT(0, client_->SendRequest("/small_response"));
832   client_->client()->WaitForEvents();
833   // Transmit the cancel, and ensure the connection is torn down properly.
834   SetPacketLossPercentage(0);
835   QuicStreamId stream_id = negotiated_version_ > QUIC_VERSION_12 ? 5 : 3;
836   session->SendRstStream(stream_id, QUIC_STREAM_CANCELLED, 0);
837
838   // WaitForEvents waits 50ms and returns true if there are outstanding
839   // requests.
840   while (client_->client()->WaitForEvents() == true) {
841   }
842   if (negotiated_version_ > QUIC_VERSION_12) {
843     // It should be completely fine to RST a stream before any data has bee
844     // received for that stream.
845     EXPECT_EQ(QUIC_NO_ERROR, client_->connection_error());
846   } else {
847     // Check that the connection is always properly closed
848     // from a stream being RST before headers are decompressed.
849     EXPECT_EQ(QUIC_STREAM_RST_BEFORE_HEADERS_DECOMPRESSED,
850               client_->connection_error());
851   }
852 }
853
854 class WrongAddressWriter : public QuicPacketWriterWrapper {
855  public:
856   WrongAddressWriter() {
857     IPAddressNumber ip;
858     CHECK(net::ParseIPLiteralToNumber("127.0.0.2", &ip));
859     self_address_ = IPEndPoint(ip, 0);
860   }
861
862   virtual WriteResult WritePacket(
863       const char* buffer,
864       size_t buf_len,
865       const IPAddressNumber& real_self_address,
866       const IPEndPoint& peer_address) OVERRIDE {
867     // Use wrong address!
868     return QuicPacketWriterWrapper::WritePacket(
869         buffer, buf_len, self_address_.address(), peer_address);
870   }
871
872   virtual bool IsWriteBlockedDataBuffered() const OVERRIDE {
873     return false;
874   }
875
876   IPEndPoint self_address_;
877 };
878
879 TEST_P(EndToEndTest, ConnectionMigration) {
880   ASSERT_TRUE(Initialize());
881
882   EXPECT_EQ(kFooResponseBody, client_->SendSynchronousRequest("/foo"));
883   EXPECT_EQ(200u, client_->response_headers()->parsed_response_code());
884
885   scoped_ptr<WrongAddressWriter> writer(new WrongAddressWriter());
886
887   writer->set_writer(new QuicDefaultPacketWriter(
888       QuicClientPeer::GetFd(client_->client())));
889   QuicConnectionPeer::SetWriter(client_->client()->session()->connection(),
890                                 writer.get());
891
892   client_->SendSynchronousRequest("/bar");
893
894   EXPECT_EQ(QUIC_STREAM_CONNECTION_ERROR, client_->stream_error());
895   EXPECT_EQ(QUIC_ERROR_MIGRATING_ADDRESS, client_->connection_error());
896 }
897
898 }  // namespace
899 }  // namespace test
900 }  // namespace tools
901 }  // namespace net