Update V8 to version 4.6.61.
[chromium-blink-merge.git] / net / quic / quic_end_to_end_unittest.cc
bloba07d32d3bc6873b4ec44cbfca11ea5cd85212b74
1 // Copyright 2013 The Chromium Authors. All rights reserved.
2 // Use of this source code is governed by a BSD-style license that can be
3 // found in the LICENSE file.
5 #include "base/basictypes.h"
6 #include "base/compiler_specific.h"
7 #include "base/memory/scoped_ptr.h"
8 #include "base/stl_util.h"
9 #include "base/strings/string_number_conversions.h"
10 #include "net/base/elements_upload_data_stream.h"
11 #include "net/base/test_completion_callback.h"
12 #include "net/base/upload_bytes_element_reader.h"
13 #include "net/base/upload_data_stream.h"
14 #include "net/cert/mock_cert_verifier.h"
15 #include "net/dns/mapped_host_resolver.h"
16 #include "net/dns/mock_host_resolver.h"
17 #include "net/http/http_auth_handler_factory.h"
18 #include "net/http/http_network_session.h"
19 #include "net/http/http_network_transaction.h"
20 #include "net/http/http_server_properties_impl.h"
21 #include "net/http/http_transaction_test_util.h"
22 #include "net/http/transport_security_state.h"
23 #include "net/proxy/proxy_service.h"
24 #include "net/quic/test_tools/quic_test_utils.h"
25 #include "net/ssl/ssl_config_service_defaults.h"
26 #include "net/tools/quic/quic_in_memory_cache.h"
27 #include "net/tools/quic/quic_server.h"
28 #include "net/tools/quic/test_tools/quic_in_memory_cache_peer.h"
29 #include "net/tools/quic/test_tools/server_thread.h"
30 #include "testing/gtest/include/gtest/gtest.h"
31 #include "testing/platform_test.h"
33 using base::StringPiece;
35 namespace net {
37 using tools::QuicInMemoryCache;
38 using tools::QuicServer;
39 using tools::test::QuicInMemoryCachePeer;
40 using tools::test::ServerThread;
42 namespace test {
44 namespace {
46 const char kResponseBody[] = "some arbitrary response body";
48 // Factory for creating HttpTransactions, used by TestTransactionConsumer.
49 class TestTransactionFactory : public HttpTransactionFactory {
50 public:
51 TestTransactionFactory(const HttpNetworkSession::Params& params)
52 : session_(new HttpNetworkSession(params)) {}
54 ~TestTransactionFactory() override {}
56 // HttpTransactionFactory methods
57 int CreateTransaction(RequestPriority priority,
58 scoped_ptr<HttpTransaction>* trans) override {
59 trans->reset(new HttpNetworkTransaction(priority, session_.get()));
60 return OK;
63 HttpCache* GetCache() override { return nullptr; }
65 HttpNetworkSession* GetSession() override { return session_.get(); };
67 private:
68 scoped_refptr<HttpNetworkSession> session_;
71 } // namespace
73 class QuicEndToEndTest : public PlatformTest {
74 protected:
75 QuicEndToEndTest()
76 : host_resolver_impl_(CreateResolverImpl()),
77 host_resolver_(host_resolver_impl_.Pass()),
78 ssl_config_service_(new SSLConfigServiceDefaults),
79 proxy_service_(ProxyService::CreateDirect()),
80 auth_handler_factory_(
81 HttpAuthHandlerFactory::CreateDefault(&host_resolver_)),
82 strike_register_no_startup_period_(false) {
83 request_.method = "GET";
84 request_.url = GURL("http://www.google.com/");
85 request_.load_flags = 0;
87 params_.enable_quic = true;
88 params_.quic_clock = nullptr;
89 params_.quic_random = nullptr;
90 params_.host_resolver = &host_resolver_;
91 params_.cert_verifier = &cert_verifier_;
92 params_.transport_security_state = &transport_security_state_;
93 params_.proxy_service = proxy_service_.get();
94 params_.ssl_config_service = ssl_config_service_.get();
95 params_.http_auth_handler_factory = auth_handler_factory_.get();
96 params_.http_server_properties = http_server_properties.GetWeakPtr();
99 // Creates a mock host resolver in which www.google.com
100 // resolves to localhost.
101 static MockHostResolver* CreateResolverImpl() {
102 MockHostResolver* resolver = new MockHostResolver();
103 resolver->rules()->AddRule("www.google.com", "127.0.0.1");
104 return resolver;
107 void SetUp() override {
108 QuicInMemoryCachePeer::ResetForTests();
109 StartServer();
111 // Use a mapped host resolver so that request for www.google.com (port 80)
112 // reach the server running on localhost.
113 std::string map_rule = "MAP www.google.com www.google.com:" +
114 base::IntToString(server_thread_->GetPort());
115 EXPECT_TRUE(host_resolver_.AddRuleFromString(map_rule));
117 // To simplify the test, and avoid the race with the HTTP request, we force
118 // QUIC for these requests.
119 params_.origin_to_force_quic_on =
120 HostPortPair::FromString("www.google.com:80");
122 transaction_factory_.reset(new TestTransactionFactory(params_));
125 void TearDown() override {
126 StopServer();
127 QuicInMemoryCachePeer::ResetForTests();
130 // Starts the QUIC server listening on a random port.
131 void StartServer() {
132 IPAddressNumber ip;
133 CHECK(ParseIPLiteralToNumber("127.0.0.1", &ip));
134 server_address_ = IPEndPoint(ip, 0);
135 server_config_.SetInitialStreamFlowControlWindowToSend(
136 kInitialStreamFlowControlWindowForTest);
137 server_config_.SetInitialSessionFlowControlWindowToSend(
138 kInitialSessionFlowControlWindowForTest);
139 server_thread_.reset(new ServerThread(
140 new QuicServer(server_config_, QuicSupportedVersions()),
141 server_address_,
142 strike_register_no_startup_period_));
143 server_thread_->Initialize();
144 server_address_ = IPEndPoint(server_address_.address(),
145 server_thread_->GetPort());
146 server_thread_->Start();
147 server_started_ = true;
150 // Stops the QUIC server.
151 void StopServer() {
152 if (!server_started_) {
153 return;
155 if (server_thread_.get()) {
156 server_thread_->Quit();
157 server_thread_->Join();
161 // Adds an entry to the cache used by the QUIC server to serve
162 // responses.
163 void AddToCache(StringPiece path,
164 int response_code,
165 StringPiece response_detail,
166 StringPiece body) {
167 QuicInMemoryCache::GetInstance()->AddSimpleResponse(
168 "www.google.com", path, response_code, response_detail, body);
171 // Populates |request_body_| with |length_| ASCII bytes.
172 void GenerateBody(size_t length) {
173 request_body_.clear();
174 request_body_.reserve(length);
175 for (size_t i = 0; i < length; ++i) {
176 request_body_.append(1, static_cast<char>(32 + i % (126 - 32)));
180 // Initializes |request_| for a post of |length| bytes.
181 void InitializePostRequest(size_t length) {
182 GenerateBody(length);
183 ScopedVector<UploadElementReader> element_readers;
184 element_readers.push_back(
185 new UploadBytesElementReader(request_body_.data(),
186 request_body_.length()));
187 upload_data_stream_.reset(
188 new ElementsUploadDataStream(element_readers.Pass(), 0));
189 request_.method = "POST";
190 request_.url = GURL("http://www.google.com/");
191 request_.upload_data_stream = upload_data_stream_.get();
192 ASSERT_EQ(OK, request_.upload_data_stream->Init(CompletionCallback()));
195 // Checks that |consumer| completed and received |status_line| and |body|.
196 void CheckResponse(const TestTransactionConsumer& consumer,
197 const std::string& status_line,
198 const std::string& body) {
199 ASSERT_TRUE(consumer.is_done());
200 EXPECT_EQ(OK, consumer.error());
201 EXPECT_EQ(status_line,
202 consumer.response_info()->headers->GetStatusLine());
203 EXPECT_EQ(body, consumer.content());
206 scoped_ptr<MockHostResolver> host_resolver_impl_;
207 MappedHostResolver host_resolver_;
208 MockCertVerifier cert_verifier_;
209 TransportSecurityState transport_security_state_;
210 scoped_refptr<SSLConfigServiceDefaults> ssl_config_service_;
211 scoped_ptr<ProxyService> proxy_service_;
212 scoped_ptr<HttpAuthHandlerFactory> auth_handler_factory_;
213 HttpServerPropertiesImpl http_server_properties;
214 HttpNetworkSession::Params params_;
215 scoped_ptr<TestTransactionFactory> transaction_factory_;
216 HttpRequestInfo request_;
217 std::string request_body_;
218 scoped_ptr<UploadDataStream> upload_data_stream_;
219 scoped_ptr<ServerThread> server_thread_;
220 IPEndPoint server_address_;
221 std::string server_hostname_;
222 QuicConfig server_config_;
223 bool server_started_;
224 bool strike_register_no_startup_period_;
227 TEST_F(QuicEndToEndTest, LargeGetWithNoPacketLoss) {
228 std::string response(10 * 1024, 'x');
230 AddToCache(request_.url.PathForRequest(), 200, "OK", response);
232 TestTransactionConsumer consumer(DEFAULT_PRIORITY,
233 transaction_factory_.get());
234 consumer.Start(&request_, BoundNetLog());
236 // Will terminate when the last consumer completes.
237 base::MessageLoop::current()->Run();
239 CheckResponse(consumer, "HTTP/1.1 200 OK", response);
242 // http://crbug.com/307284
243 TEST_F(QuicEndToEndTest, DISABLED_LargePostWithNoPacketLoss) {
244 InitializePostRequest(10 * 1024 * 1024);
246 AddToCache(request_.url.PathForRequest(), 200, "OK", kResponseBody);
248 TestTransactionConsumer consumer(DEFAULT_PRIORITY,
249 transaction_factory_.get());
250 consumer.Start(&request_, BoundNetLog());
252 // Will terminate when the last consumer completes.
253 base::MessageLoop::current()->Run();
255 CheckResponse(consumer, "HTTP/1.1 200 OK", kResponseBody);
258 TEST_F(QuicEndToEndTest, LargePostWithPacketLoss) {
259 // FLAGS_fake_packet_loss_percentage = 30;
260 InitializePostRequest(1024 * 1024);
262 const char kResponseBody[] = "some really big response body";
263 AddToCache(request_.url.PathForRequest(), 200, "OK", kResponseBody);
265 TestTransactionConsumer consumer(DEFAULT_PRIORITY,
266 transaction_factory_.get());
267 consumer.Start(&request_, BoundNetLog());
269 // Will terminate when the last consumer completes.
270 base::MessageLoop::current()->Run();
272 CheckResponse(consumer, "HTTP/1.1 200 OK", kResponseBody);
275 TEST_F(QuicEndToEndTest, UberTest) {
276 // FLAGS_fake_packet_loss_percentage = 30;
278 const char kResponseBody[] = "some really big response body";
279 AddToCache(request_.url.PathForRequest(), 200, "OK", kResponseBody);
281 std::vector<TestTransactionConsumer*> consumers;
282 size_t num_requests = 100;
283 for (size_t i = 0; i < num_requests; ++i) {
284 TestTransactionConsumer* consumer =
285 new TestTransactionConsumer(DEFAULT_PRIORITY,
286 transaction_factory_.get());
287 consumers.push_back(consumer);
288 consumer->Start(&request_, BoundNetLog());
291 // Will terminate when the last consumer completes.
292 base::MessageLoop::current()->Run();
294 for (size_t i = 0; i < num_requests; ++i) {
295 CheckResponse(*consumers[i], "HTTP/1.1 200 OK", kResponseBody);
297 STLDeleteElements(&consumers);
300 } // namespace test
301 } // namespace net