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
;
34 using net::tools::QuicInMemoryCache
;
35 using net::tools::QuicServer
;
36 using net::tools::test::QuicInMemoryCachePeer
;
37 using net::tools::test::ServerThread
;
44 const char kResponseBody
[] = "some arbitrary response body";
46 // Factory for creating HttpTransactions, used by TestTransactionConsumer.
47 class TestTransactionFactory
: public HttpTransactionFactory
{
49 TestTransactionFactory(const HttpNetworkSession::Params
& params
)
50 : session_(new HttpNetworkSession(params
)) {}
52 virtual ~TestTransactionFactory() {
55 // HttpTransactionFactory methods
56 virtual int CreateTransaction(RequestPriority priority
,
57 scoped_ptr
<HttpTransaction
>* trans
) override
{
58 trans
->reset(new HttpNetworkTransaction(priority
, session_
.get()));
62 virtual HttpCache
* GetCache() override
{
66 virtual HttpNetworkSession
* GetSession() override
{ return session_
.get(); };
69 scoped_refptr
<HttpNetworkSession
> session_
;
74 class QuicEndToEndTest
: public PlatformTest
{
77 : host_resolver_impl_(CreateResolverImpl()),
78 host_resolver_(host_resolver_impl_
.Pass()),
79 ssl_config_service_(new SSLConfigServiceDefaults
),
80 proxy_service_(ProxyService::CreateDirect()),
81 auth_handler_factory_(
82 HttpAuthHandlerFactory::CreateDefault(&host_resolver_
)),
83 strike_register_no_startup_period_(false) {
84 request_
.method
= "GET";
85 request_
.url
= GURL("http://www.google.com/");
86 request_
.load_flags
= 0;
88 params_
.enable_quic
= true;
89 params_
.quic_clock
= nullptr;
90 params_
.quic_random
= nullptr;
91 params_
.host_resolver
= &host_resolver_
;
92 params_
.cert_verifier
= &cert_verifier_
;
93 params_
.transport_security_state
= &transport_security_state_
;
94 params_
.proxy_service
= proxy_service_
.get();
95 params_
.ssl_config_service
= ssl_config_service_
.get();
96 params_
.http_auth_handler_factory
= auth_handler_factory_
.get();
97 params_
.http_server_properties
= http_server_properties
.GetWeakPtr();
100 // Creates a mock host resolver in which www.google.com
101 // resolves to localhost.
102 static MockHostResolver
* CreateResolverImpl() {
103 MockHostResolver
* resolver
= new MockHostResolver();
104 resolver
->rules()->AddRule("www.google.com", "127.0.0.1");
108 virtual void SetUp() {
109 QuicInMemoryCachePeer::ResetForTests();
112 // Use a mapped host resolver so that request for www.google.com (port 80)
113 // reach the server running on localhost.
114 std::string map_rule
= "MAP www.google.com www.google.com:" +
115 base::IntToString(server_thread_
->GetPort());
116 EXPECT_TRUE(host_resolver_
.AddRuleFromString(map_rule
));
118 // To simplify the test, and avoid the race with the HTTP request, we force
119 // QUIC for these requests.
120 params_
.origin_to_force_quic_on
=
121 HostPortPair::FromString("www.google.com:80");
123 transaction_factory_
.reset(new TestTransactionFactory(params_
));
126 virtual void TearDown() {
128 QuicInMemoryCachePeer::ResetForTests();
131 // Starts the QUIC server listening on a random port.
133 net::IPAddressNumber ip
;
134 CHECK(net::ParseIPLiteralToNumber("127.0.0.1", &ip
));
135 server_address_
= IPEndPoint(ip
, 0);
136 server_config_
.SetInitialFlowControlWindowToSend(
137 kInitialSessionFlowControlWindowForTest
);
138 server_config_
.SetInitialStreamFlowControlWindowToSend(
139 kInitialStreamFlowControlWindowForTest
);
140 server_config_
.SetInitialSessionFlowControlWindowToSend(
141 kInitialSessionFlowControlWindowForTest
);
142 server_thread_
.reset(new ServerThread(
143 new QuicServer(server_config_
, QuicSupportedVersions()),
145 strike_register_no_startup_period_
));
146 server_thread_
->Initialize();
147 server_address_
= IPEndPoint(server_address_
.address(),
148 server_thread_
->GetPort());
149 server_thread_
->Start();
150 server_started_
= true;
153 // Stops the QUIC server.
155 if (!server_started_
) {
158 if (server_thread_
.get()) {
159 server_thread_
->Quit();
160 server_thread_
->Join();
164 // Adds an entry to the cache used by the QUIC server to serve
166 void AddToCache(const StringPiece
& method
,
167 const StringPiece
& path
,
168 const StringPiece
& version
,
169 const StringPiece
& response_code
,
170 const StringPiece
& response_detail
,
171 const StringPiece
& body
) {
172 QuicInMemoryCache::GetInstance()->AddSimpleResponse(
173 method
, path
, version
, response_code
, response_detail
, body
);
176 // Populates |request_body_| with |length_| ASCII bytes.
177 void GenerateBody(size_t length
) {
178 request_body_
.clear();
179 request_body_
.reserve(length
);
180 for (size_t i
= 0; i
< length
; ++i
) {
181 request_body_
.append(1, static_cast<char>(32 + i
% (126 - 32)));
185 // Initializes |request_| for a post of |length| bytes.
186 void InitializePostRequest(size_t length
) {
187 GenerateBody(length
);
188 ScopedVector
<UploadElementReader
> element_readers
;
189 element_readers
.push_back(
190 new UploadBytesElementReader(request_body_
.data(),
191 request_body_
.length()));
192 upload_data_stream_
.reset(
193 new ElementsUploadDataStream(element_readers
.Pass(), 0));
194 request_
.method
= "POST";
195 request_
.url
= GURL("http://www.google.com/");
196 request_
.upload_data_stream
= upload_data_stream_
.get();
197 ASSERT_EQ(OK
, request_
.upload_data_stream
->Init(CompletionCallback()));
200 // Checks that |consumer| completed and received |status_line| and |body|.
201 void CheckResponse(const TestTransactionConsumer
& consumer
,
202 const std::string
& status_line
,
203 const std::string
& body
) {
204 ASSERT_TRUE(consumer
.is_done());
205 EXPECT_EQ(OK
, consumer
.error());
206 EXPECT_EQ(status_line
,
207 consumer
.response_info()->headers
->GetStatusLine());
208 EXPECT_EQ(body
, consumer
.content());
211 scoped_ptr
<MockHostResolver
> host_resolver_impl_
;
212 MappedHostResolver host_resolver_
;
213 MockCertVerifier cert_verifier_
;
214 TransportSecurityState transport_security_state_
;
215 scoped_refptr
<SSLConfigServiceDefaults
> ssl_config_service_
;
216 scoped_ptr
<ProxyService
> proxy_service_
;
217 scoped_ptr
<HttpAuthHandlerFactory
> auth_handler_factory_
;
218 HttpServerPropertiesImpl http_server_properties
;
219 HttpNetworkSession::Params params_
;
220 scoped_ptr
<TestTransactionFactory
> transaction_factory_
;
221 HttpRequestInfo request_
;
222 std::string request_body_
;
223 scoped_ptr
<UploadDataStream
> upload_data_stream_
;
224 scoped_ptr
<ServerThread
> server_thread_
;
225 IPEndPoint server_address_
;
226 std::string server_hostname_
;
227 QuicConfig server_config_
;
228 bool server_started_
;
229 bool strike_register_no_startup_period_
;
232 TEST_F(QuicEndToEndTest
, LargeGetWithNoPacketLoss
) {
233 std::string
response(10 * 1024, 'x');
235 AddToCache("GET", request_
.url
.spec(),
236 "HTTP/1.1", "200", "OK",
239 TestTransactionConsumer
consumer(DEFAULT_PRIORITY
,
240 transaction_factory_
.get());
241 consumer
.Start(&request_
, BoundNetLog());
243 // Will terminate when the last consumer completes.
244 base::MessageLoop::current()->Run();
246 CheckResponse(consumer
, "HTTP/1.1 200 OK", response
);
249 // http://crbug.com/307284
250 TEST_F(QuicEndToEndTest
, DISABLED_LargePostWithNoPacketLoss
) {
251 InitializePostRequest(10 * 1024 * 1024);
253 AddToCache("POST", request_
.url
.spec(),
254 "HTTP/1.1", "200", "OK",
257 TestTransactionConsumer
consumer(DEFAULT_PRIORITY
,
258 transaction_factory_
.get());
259 consumer
.Start(&request_
, BoundNetLog());
261 // Will terminate when the last consumer completes.
262 base::MessageLoop::current()->Run();
264 CheckResponse(consumer
, "HTTP/1.1 200 OK", kResponseBody
);
267 TEST_F(QuicEndToEndTest
, LargePostWithPacketLoss
) {
268 // FLAGS_fake_packet_loss_percentage = 30;
269 InitializePostRequest(1024 * 1024);
271 const char kResponseBody
[] = "some really big response body";
272 AddToCache("POST", request_
.url
.spec(),
273 "HTTP/1.1", "200", "OK",
276 TestTransactionConsumer
consumer(DEFAULT_PRIORITY
,
277 transaction_factory_
.get());
278 consumer
.Start(&request_
, BoundNetLog());
280 // Will terminate when the last consumer completes.
281 base::MessageLoop::current()->Run();
283 CheckResponse(consumer
, "HTTP/1.1 200 OK", kResponseBody
);
286 TEST_F(QuicEndToEndTest
, UberTest
) {
287 // FLAGS_fake_packet_loss_percentage = 30;
289 const char kResponseBody
[] = "some really big response body";
290 AddToCache("GET", request_
.url
.spec(),
291 "HTTP/1.1", "200", "OK",
294 std::vector
<TestTransactionConsumer
*> consumers
;
295 size_t num_requests
= 100;
296 for (size_t i
= 0; i
< num_requests
; ++i
) {
297 TestTransactionConsumer
* consumer
=
298 new TestTransactionConsumer(DEFAULT_PRIORITY
,
299 transaction_factory_
.get());
300 consumers
.push_back(consumer
);
301 consumer
->Start(&request_
, BoundNetLog());
304 // Will terminate when the last consumer completes.
305 base::MessageLoop::current()->Run();
307 for (size_t i
= 0; i
< num_requests
; ++i
) {
308 CheckResponse(*consumers
[i
], "HTTP/1.1 200 OK", kResponseBody
);
310 STLDeleteElements(&consumers
);