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 ~TestTransactionFactory() override
{}
54 // HttpTransactionFactory methods
55 int CreateTransaction(RequestPriority priority
,
56 scoped_ptr
<HttpTransaction
>* trans
) override
{
57 trans
->reset(new HttpNetworkTransaction(priority
, session_
.get()));
61 HttpCache
* GetCache() override
{ return nullptr; }
63 HttpNetworkSession
* GetSession() override
{ return session_
.get(); };
66 scoped_refptr
<HttpNetworkSession
> session_
;
71 class QuicEndToEndTest
: public PlatformTest
{
74 : host_resolver_impl_(CreateResolverImpl()),
75 host_resolver_(host_resolver_impl_
.Pass()),
76 ssl_config_service_(new SSLConfigServiceDefaults
),
77 proxy_service_(ProxyService::CreateDirect()),
78 auth_handler_factory_(
79 HttpAuthHandlerFactory::CreateDefault(&host_resolver_
)),
80 strike_register_no_startup_period_(false) {
81 request_
.method
= "GET";
82 request_
.url
= GURL("http://www.google.com/");
83 request_
.load_flags
= 0;
85 params_
.enable_quic
= true;
86 params_
.quic_clock
= nullptr;
87 params_
.quic_random
= nullptr;
88 params_
.host_resolver
= &host_resolver_
;
89 params_
.cert_verifier
= &cert_verifier_
;
90 params_
.transport_security_state
= &transport_security_state_
;
91 params_
.proxy_service
= proxy_service_
.get();
92 params_
.ssl_config_service
= ssl_config_service_
.get();
93 params_
.http_auth_handler_factory
= auth_handler_factory_
.get();
94 params_
.http_server_properties
= http_server_properties
.GetWeakPtr();
97 // Creates a mock host resolver in which www.google.com
98 // resolves to localhost.
99 static MockHostResolver
* CreateResolverImpl() {
100 MockHostResolver
* resolver
= new MockHostResolver();
101 resolver
->rules()->AddRule("www.google.com", "127.0.0.1");
105 void SetUp() override
{
106 QuicInMemoryCachePeer::ResetForTests();
109 // Use a mapped host resolver so that request for www.google.com (port 80)
110 // reach the server running on localhost.
111 std::string map_rule
= "MAP www.google.com www.google.com:" +
112 base::IntToString(server_thread_
->GetPort());
113 EXPECT_TRUE(host_resolver_
.AddRuleFromString(map_rule
));
115 // To simplify the test, and avoid the race with the HTTP request, we force
116 // QUIC for these requests.
117 params_
.origin_to_force_quic_on
=
118 HostPortPair::FromString("www.google.com:80");
120 transaction_factory_
.reset(new TestTransactionFactory(params_
));
123 void TearDown() override
{
125 QuicInMemoryCachePeer::ResetForTests();
128 // Starts the QUIC server listening on a random port.
130 net::IPAddressNumber ip
;
131 CHECK(net::ParseIPLiteralToNumber("127.0.0.1", &ip
));
132 server_address_
= IPEndPoint(ip
, 0);
133 server_config_
.SetInitialStreamFlowControlWindowToSend(
134 kInitialStreamFlowControlWindowForTest
);
135 server_config_
.SetInitialSessionFlowControlWindowToSend(
136 kInitialSessionFlowControlWindowForTest
);
137 server_thread_
.reset(new ServerThread(
138 new QuicServer(server_config_
, QuicSupportedVersions()),
140 strike_register_no_startup_period_
));
141 server_thread_
->Initialize();
142 server_address_
= IPEndPoint(server_address_
.address(),
143 server_thread_
->GetPort());
144 server_thread_
->Start();
145 server_started_
= true;
148 // Stops the QUIC server.
150 if (!server_started_
) {
153 if (server_thread_
.get()) {
154 server_thread_
->Quit();
155 server_thread_
->Join();
159 // Adds an entry to the cache used by the QUIC server to serve
161 void AddToCache(StringPiece path
,
163 StringPiece response_detail
,
165 QuicInMemoryCache::GetInstance()->AddSimpleResponse(
166 "www.google.com", path
, response_code
, response_detail
, body
);
169 // Populates |request_body_| with |length_| ASCII bytes.
170 void GenerateBody(size_t length
) {
171 request_body_
.clear();
172 request_body_
.reserve(length
);
173 for (size_t i
= 0; i
< length
; ++i
) {
174 request_body_
.append(1, static_cast<char>(32 + i
% (126 - 32)));
178 // Initializes |request_| for a post of |length| bytes.
179 void InitializePostRequest(size_t length
) {
180 GenerateBody(length
);
181 ScopedVector
<UploadElementReader
> element_readers
;
182 element_readers
.push_back(
183 new UploadBytesElementReader(request_body_
.data(),
184 request_body_
.length()));
185 upload_data_stream_
.reset(
186 new ElementsUploadDataStream(element_readers
.Pass(), 0));
187 request_
.method
= "POST";
188 request_
.url
= GURL("http://www.google.com/");
189 request_
.upload_data_stream
= upload_data_stream_
.get();
190 ASSERT_EQ(OK
, request_
.upload_data_stream
->Init(CompletionCallback()));
193 // Checks that |consumer| completed and received |status_line| and |body|.
194 void CheckResponse(const TestTransactionConsumer
& consumer
,
195 const std::string
& status_line
,
196 const std::string
& body
) {
197 ASSERT_TRUE(consumer
.is_done());
198 EXPECT_EQ(OK
, consumer
.error());
199 EXPECT_EQ(status_line
,
200 consumer
.response_info()->headers
->GetStatusLine());
201 EXPECT_EQ(body
, consumer
.content());
204 scoped_ptr
<MockHostResolver
> host_resolver_impl_
;
205 MappedHostResolver host_resolver_
;
206 MockCertVerifier cert_verifier_
;
207 TransportSecurityState transport_security_state_
;
208 scoped_refptr
<SSLConfigServiceDefaults
> ssl_config_service_
;
209 scoped_ptr
<ProxyService
> proxy_service_
;
210 scoped_ptr
<HttpAuthHandlerFactory
> auth_handler_factory_
;
211 HttpServerPropertiesImpl http_server_properties
;
212 HttpNetworkSession::Params params_
;
213 scoped_ptr
<TestTransactionFactory
> transaction_factory_
;
214 HttpRequestInfo request_
;
215 std::string request_body_
;
216 scoped_ptr
<UploadDataStream
> upload_data_stream_
;
217 scoped_ptr
<ServerThread
> server_thread_
;
218 IPEndPoint server_address_
;
219 std::string server_hostname_
;
220 QuicConfig server_config_
;
221 bool server_started_
;
222 bool strike_register_no_startup_period_
;
225 TEST_F(QuicEndToEndTest
, LargeGetWithNoPacketLoss
) {
226 std::string
response(10 * 1024, 'x');
228 AddToCache(request_
.url
.PathForRequest(), 200, "OK", response
);
230 TestTransactionConsumer
consumer(DEFAULT_PRIORITY
,
231 transaction_factory_
.get());
232 consumer
.Start(&request_
, BoundNetLog());
234 // Will terminate when the last consumer completes.
235 base::MessageLoop::current()->Run();
237 CheckResponse(consumer
, "HTTP/1.1 200 OK", response
);
240 // http://crbug.com/307284
241 TEST_F(QuicEndToEndTest
, DISABLED_LargePostWithNoPacketLoss
) {
242 InitializePostRequest(10 * 1024 * 1024);
244 AddToCache(request_
.url
.PathForRequest(), 200, "OK", kResponseBody
);
246 TestTransactionConsumer
consumer(DEFAULT_PRIORITY
,
247 transaction_factory_
.get());
248 consumer
.Start(&request_
, BoundNetLog());
250 // Will terminate when the last consumer completes.
251 base::MessageLoop::current()->Run();
253 CheckResponse(consumer
, "HTTP/1.1 200 OK", kResponseBody
);
256 TEST_F(QuicEndToEndTest
, LargePostWithPacketLoss
) {
257 // FLAGS_fake_packet_loss_percentage = 30;
258 InitializePostRequest(1024 * 1024);
260 const char kResponseBody
[] = "some really big response body";
261 AddToCache(request_
.url
.PathForRequest(), 200, "OK", kResponseBody
);
263 TestTransactionConsumer
consumer(DEFAULT_PRIORITY
,
264 transaction_factory_
.get());
265 consumer
.Start(&request_
, BoundNetLog());
267 // Will terminate when the last consumer completes.
268 base::MessageLoop::current()->Run();
270 CheckResponse(consumer
, "HTTP/1.1 200 OK", kResponseBody
);
273 TEST_F(QuicEndToEndTest
, UberTest
) {
274 // FLAGS_fake_packet_loss_percentage = 30;
276 const char kResponseBody
[] = "some really big response body";
277 AddToCache(request_
.url
.PathForRequest(), 200, "OK", kResponseBody
);
279 std::vector
<TestTransactionConsumer
*> consumers
;
280 size_t num_requests
= 100;
281 for (size_t i
= 0; i
< num_requests
; ++i
) {
282 TestTransactionConsumer
* consumer
=
283 new TestTransactionConsumer(DEFAULT_PRIORITY
,
284 transaction_factory_
.get());
285 consumers
.push_back(consumer
);
286 consumer
->Start(&request_
, BoundNetLog());
289 // Will terminate when the last consumer completes.
290 base::MessageLoop::current()->Run();
292 for (size_t i
= 0; i
< num_requests
; ++i
) {
293 CheckResponse(*consumers
[i
], "HTTP/1.1 200 OK", kResponseBody
);
295 STLDeleteElements(&consumers
);