Add explicit |forceOnlineSignin| to user pod status
[chromium-blink-merge.git] / net / quic / quic_end_to_end_unittest.cc
blob1f5cc0dfb9667f85ea670297c67a379053ff8598
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/test_completion_callback.h"
11 #include "net/base/upload_bytes_element_reader.h"
12 #include "net/base/upload_data_stream.h"
13 #include "net/cert/mock_cert_verifier.h"
14 #include "net/dns/mapped_host_resolver.h"
15 #include "net/dns/mock_host_resolver.h"
16 #include "net/http/http_auth_handler_factory.h"
17 #include "net/http/http_network_session.h"
18 #include "net/http/http_network_transaction.h"
19 #include "net/http/http_server_properties_impl.h"
20 #include "net/http/http_transaction_unittest.h"
21 #include "net/http/transport_security_state.h"
22 #include "net/proxy/proxy_service.h"
23 #include "net/ssl/ssl_config_service_defaults.h"
24 #include "net/tools/quic/quic_in_memory_cache.h"
25 #include "net/tools/quic/test_tools/quic_in_memory_cache_peer.h"
26 #include "net/tools/quic/test_tools/server_thread.h"
27 #include "testing/gtest/include/gtest/gtest.h"
28 #include "testing/platform_test.h"
30 using base::StringPiece;
31 using net::tools::QuicInMemoryCache;
32 using net::tools::test::QuicInMemoryCachePeer;
33 using net::tools::test::ServerThread;
35 namespace net {
36 namespace test {
38 namespace {
40 const char kResponseBody[] = "some arbitrary response body";
42 // Factory for creating HttpTransactions, used by TestTransactionConsumer.
43 class TestTransactionFactory : public HttpTransactionFactory {
44 public:
45 TestTransactionFactory(const HttpNetworkSession::Params& params)
46 : session_(new HttpNetworkSession(params)) {}
48 virtual ~TestTransactionFactory() {
51 // HttpTransactionFactory methods
52 virtual int CreateTransaction(RequestPriority priority,
53 scoped_ptr<HttpTransaction>* trans) OVERRIDE {
54 trans->reset(new HttpNetworkTransaction(priority, session_));
55 return OK;
58 virtual HttpCache* GetCache() OVERRIDE {
59 return NULL;
62 virtual HttpNetworkSession* GetSession() OVERRIDE {
63 return session_;
66 private:
67 scoped_refptr<HttpNetworkSession> session_;
70 } // namespace
72 class QuicEndToEndTest : public PlatformTest {
73 protected:
74 QuicEndToEndTest()
75 : host_resolver_impl_(CreateResolverImpl()),
76 host_resolver_(host_resolver_impl_.PassAs<HostResolver>()),
77 ssl_config_service_(new SSLConfigServiceDefaults),
78 proxy_service_(ProxyService::CreateDirect()),
79 auth_handler_factory_(
80 HttpAuthHandlerFactory::CreateDefault(&host_resolver_)),
81 strike_register_no_startup_period_(false) {
82 request_.method = "GET";
83 request_.url = GURL("http://www.google.com/");
84 request_.load_flags = 0;
86 params_.enable_quic = true;
87 params_.quic_clock = NULL;
88 params_.quic_random = NULL;
89 params_.host_resolver = &host_resolver_;
90 params_.cert_verifier = &cert_verifier_;
91 params_.transport_security_state = &transport_security_state_;
92 params_.proxy_service = proxy_service_.get();
93 params_.ssl_config_service = ssl_config_service_.get();
94 params_.http_auth_handler_factory = auth_handler_factory_.get();
95 params_.http_server_properties = http_server_properties.GetWeakPtr();
98 // Creates a mock host resolver in which www.google.com
99 // resolves to localhost.
100 static MockHostResolver* CreateResolverImpl() {
101 MockHostResolver* resolver = new MockHostResolver();
102 resolver->rules()->AddRule("www.google.com", "127.0.0.1");
103 return resolver;
106 virtual void SetUp() {
107 QuicInMemoryCachePeer::ResetForTests();
108 StartServer();
110 // Use a mapped host resolver so that request for www.google.com (port 80)
111 // reach the server running on localhost.
112 std::string map_rule = "MAP www.google.com www.google.com:" +
113 base::IntToString(server_thread_->GetPort());
114 EXPECT_TRUE(host_resolver_.AddRuleFromString(map_rule));
116 // To simplify the test, and avoid the race with the HTTP request, we force
117 // QUIC for these requests.
118 params_.origin_to_force_quic_on =
119 HostPortPair::FromString("www.google.com:80");
121 transaction_factory_.reset(new TestTransactionFactory(params_));
124 virtual void TearDown() {
125 StopServer();
126 QuicInMemoryCachePeer::ResetForTests();
129 // Starts the QUIC server listening on a random port.
130 void StartServer() {
131 net::IPAddressNumber ip;
132 CHECK(net::ParseIPLiteralToNumber("127.0.0.1", &ip));
133 server_address_ = IPEndPoint(ip, 0);
134 server_config_.SetDefaults();
135 server_thread_.reset(new ServerThread(server_address_, server_config_,
136 QuicSupportedVersions(),
137 strike_register_no_startup_period_));
138 server_thread_->Initialize();
139 server_address_ = IPEndPoint(server_address_.address(),
140 server_thread_->GetPort());
141 server_thread_->Start();
142 server_started_ = true;
145 // Stops the QUIC server.
146 void StopServer() {
147 if (!server_started_) {
148 return;
150 if (server_thread_.get()) {
151 server_thread_->Quit();
152 server_thread_->Join();
156 // Adds an entry to the cache used by the QUIC server to serve
157 // responses.
158 void AddToCache(const StringPiece& method,
159 const StringPiece& path,
160 const StringPiece& version,
161 const StringPiece& response_code,
162 const StringPiece& response_detail,
163 const StringPiece& body) {
164 QuicInMemoryCache::GetInstance()->AddSimpleResponse(
165 method, path, version, response_code, response_detail, body);
168 // Populates |request_body_| with |length_| ASCII bytes.
169 void GenerateBody(size_t length) {
170 request_body_.clear();
171 request_body_.reserve(length);
172 for (size_t i = 0; i < length; ++i) {
173 request_body_.append(1, static_cast<char>(32 + i % (126 - 32)));
177 // Initializes |request_| for a post of |length| bytes.
178 void InitializePostRequest(size_t length) {
179 GenerateBody(length);
180 ScopedVector<UploadElementReader> element_readers;
181 element_readers.push_back(
182 new UploadBytesElementReader(request_body_.data(),
183 request_body_.length()));
184 upload_data_stream_.reset(new UploadDataStream(element_readers.Pass(), 0));
185 request_.method = "POST";
186 request_.url = GURL("http://www.google.com/");
187 request_.upload_data_stream = upload_data_stream_.get();
188 ASSERT_EQ(OK, request_.upload_data_stream->Init(CompletionCallback()));
191 // Checks that |consumer| completed and received |status_line| and |body|.
192 void CheckResponse(const TestTransactionConsumer& consumer,
193 const std::string& status_line,
194 const std::string& body) {
195 ASSERT_TRUE(consumer.is_done());
196 EXPECT_EQ(OK, consumer.error());
197 EXPECT_EQ(status_line,
198 consumer.response_info()->headers->GetStatusLine());
199 EXPECT_EQ(body, consumer.content());
202 scoped_ptr<MockHostResolver> host_resolver_impl_;
203 MappedHostResolver host_resolver_;
204 MockCertVerifier cert_verifier_;
205 TransportSecurityState transport_security_state_;
206 scoped_refptr<SSLConfigServiceDefaults> ssl_config_service_;
207 scoped_ptr<ProxyService> proxy_service_;
208 scoped_ptr<HttpAuthHandlerFactory> auth_handler_factory_;
209 HttpServerPropertiesImpl http_server_properties;
210 HttpNetworkSession::Params params_;
211 scoped_ptr<TestTransactionFactory> transaction_factory_;
212 HttpRequestInfo request_;
213 std::string request_body_;
214 scoped_ptr<UploadDataStream> upload_data_stream_;
215 scoped_ptr<ServerThread> server_thread_;
216 IPEndPoint server_address_;
217 std::string server_hostname_;
218 QuicConfig server_config_;
219 bool server_started_;
220 bool strike_register_no_startup_period_;
223 TEST_F(QuicEndToEndTest, LargeGetWithNoPacketLoss) {
224 std::string response(10 * 1024, 'x');
226 AddToCache("GET", request_.url.spec(),
227 "HTTP/1.1", "200", "OK",
228 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("POST", request_.url.spec(),
245 "HTTP/1.1", "200", "OK",
246 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("POST", request_.url.spec(),
264 "HTTP/1.1", "200", "OK",
265 kResponseBody);
267 TestTransactionConsumer consumer(DEFAULT_PRIORITY,
268 transaction_factory_.get());
269 consumer.Start(&request_, BoundNetLog());
271 // Will terminate when the last consumer completes.
272 base::MessageLoop::current()->Run();
274 CheckResponse(consumer, "HTTP/1.1 200 OK", kResponseBody);
277 TEST_F(QuicEndToEndTest, UberTest) {
278 // FLAGS_fake_packet_loss_percentage = 30;
280 const char kResponseBody[] = "some really big response body";
281 AddToCache("GET", request_.url.spec(),
282 "HTTP/1.1", "200", "OK",
283 kResponseBody);
285 std::vector<TestTransactionConsumer*> consumers;
286 size_t num_requests = 100;
287 for (size_t i = 0; i < num_requests; ++i) {
288 TestTransactionConsumer* consumer =
289 new TestTransactionConsumer(DEFAULT_PRIORITY,
290 transaction_factory_.get());
291 consumers.push_back(consumer);
292 consumer->Start(&request_, BoundNetLog());
295 // Will terminate when the last consumer completes.
296 base::MessageLoop::current()->Run();
298 for (size_t i = 0; i < num_requests; ++i) {
299 CheckResponse(*consumers[i], "HTTP/1.1 200 OK", kResponseBody);
301 STLDeleteElements(&consumers);
304 } // namespace test
305 } // namespace net