Revert "Removed the experimental flag for Chromecast support and the corresponding...
[chromium-blink-merge.git] / net / quic / quic_end_to_end_unittest.cc
blob75ed8e35486aa614bf418c91f0d6facd314f0b7c
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 extern int FLAGS_fake_packet_loss_percentage;
32 using base::StringPiece;
33 using net::tools::QuicInMemoryCache;
34 using net::tools::test::QuicInMemoryCachePeer;
35 using net::tools::test::ServerThread;
37 namespace net {
38 namespace test {
40 namespace {
42 const char kResponseBody[] = "some arbitrary response body";
44 // Factory for creating HttpTransactions, used by TestTransactionConsumer.
45 class TestTransactionFactory : public HttpTransactionFactory {
46 public:
47 TestTransactionFactory(const HttpNetworkSession::Params& params)
48 : session_(new HttpNetworkSession(params)) {}
50 virtual ~TestTransactionFactory() {
51 FLAGS_fake_packet_loss_percentage = 0;
54 // HttpTransactionFactory methods
55 virtual int CreateTransaction(RequestPriority priority,
56 scoped_ptr<HttpTransaction>* trans,
57 HttpTransactionDelegate* delegate) OVERRIDE {
58 EXPECT_TRUE(delegate == NULL);
59 trans->reset(new HttpNetworkTransaction(priority, session_));
60 return OK;
63 virtual HttpCache* GetCache() OVERRIDE {
64 return NULL;
67 virtual HttpNetworkSession* GetSession() OVERRIDE {
68 return session_;
71 private:
72 scoped_refptr<HttpNetworkSession> session_;
75 } // namespace
77 class QuicEndToEndTest : public PlatformTest {
78 protected:
79 QuicEndToEndTest()
80 : host_resolver_impl_(CreateResolverImpl()),
81 host_resolver_(host_resolver_impl_.PassAs<HostResolver>()),
82 ssl_config_service_(new SSLConfigServiceDefaults),
83 proxy_service_(ProxyService::CreateDirect()),
84 auth_handler_factory_(
85 HttpAuthHandlerFactory::CreateDefault(&host_resolver_)),
86 strike_register_no_startup_period_(false) {
87 request_.method = "GET";
88 request_.url = GURL("http://www.google.com/");
89 request_.load_flags = 0;
91 params_.enable_quic = true;
92 params_.quic_clock = NULL;
93 params_.quic_random = NULL;
94 params_.host_resolver = &host_resolver_;
95 params_.cert_verifier = &cert_verifier_;
96 params_.transport_security_state = &transport_security_state_;
97 params_.proxy_service = proxy_service_.get();
98 params_.ssl_config_service = ssl_config_service_.get();
99 params_.http_auth_handler_factory = auth_handler_factory_.get();
100 params_.http_server_properties = http_server_properties.GetWeakPtr();
103 // Creates a mock host resolver in which www.google.com
104 // resolves to localhost.
105 static MockHostResolver* CreateResolverImpl() {
106 MockHostResolver* resolver = new MockHostResolver();
107 resolver->rules()->AddRule("www.google.com", "127.0.0.1");
108 return resolver;
111 virtual void SetUp() {
112 QuicInMemoryCachePeer::ResetForTests();
113 StartServer();
115 // Use a mapped host resolver so that request for www.google.com (port 80)
116 // reach the server running on localhost.
117 std::string map_rule = "MAP www.google.com www.google.com:" +
118 base::IntToString(server_thread_->GetPort());
119 EXPECT_TRUE(host_resolver_.AddRuleFromString(map_rule));
121 // To simplify the test, and avoid the race with the HTTP request, we force
122 // QUIC for these requests.
123 params_.origin_to_force_quic_on =
124 HostPortPair::FromString("www.google.com:80");
126 transaction_factory_.reset(new TestTransactionFactory(params_));
129 virtual void TearDown() {
130 StopServer();
131 QuicInMemoryCachePeer::ResetForTests();
134 // Starts the QUIC server listening on a random port.
135 void StartServer() {
136 net::IPAddressNumber ip;
137 CHECK(net::ParseIPLiteralToNumber("127.0.0.1", &ip));
138 server_address_ = IPEndPoint(ip, 0);
139 server_config_.SetDefaults();
140 server_thread_.reset(new ServerThread(server_address_, server_config_,
141 strike_register_no_startup_period_));
142 server_thread_->Start();
143 server_thread_->listening()->Wait();
144 server_address_ = IPEndPoint(server_address_.address(),
145 server_thread_->GetPort());
146 server_started_ = true;
149 // Stops the QUIC server.
150 void StopServer() {
151 if (!server_started_) {
152 return;
154 if (server_thread_.get()) {
155 server_thread_->quit()->Signal();
156 server_thread_->Join();
160 // Adds an entry to the cache used by the QUIC server to serve
161 // responses.
162 void AddToCache(const StringPiece& method,
163 const StringPiece& path,
164 const StringPiece& version,
165 const StringPiece& response_code,
166 const StringPiece& response_detail,
167 const StringPiece& body) {
168 QuicInMemoryCache::GetInstance()->AddSimpleResponse(
169 method, path, version, response_code, response_detail, body);
172 // Populates |request_body_| with |length_| ASCII bytes.
173 void GenerateBody(size_t length) {
174 request_body_.clear();
175 request_body_.reserve(length);
176 for (size_t i = 0; i < length; ++i) {
177 request_body_.append(1, static_cast<char>(32 + i % (126 - 32)));
181 // Initializes |request_| for a post of |length| bytes.
182 void InitializePostRequest(size_t length) {
183 GenerateBody(length);
184 ScopedVector<UploadElementReader> element_readers;
185 element_readers.push_back(
186 new UploadBytesElementReader(request_body_.data(),
187 request_body_.length()));
188 upload_data_stream_.reset(new UploadDataStream(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("GET", request_.url.spec(),
231 "HTTP/1.1", "200", "OK",
232 response);
234 TestTransactionConsumer consumer(DEFAULT_PRIORITY,
235 transaction_factory_.get());
236 consumer.Start(&request_, BoundNetLog());
238 // Will terminate when the last consumer completes.
239 base::MessageLoop::current()->Run();
241 CheckResponse(consumer, "HTTP/1.1 200 OK", response);
244 TEST_F(QuicEndToEndTest, LargePostWithNoPacketLoss) {
245 InitializePostRequest(10 * 1024 * 1024);
247 AddToCache("POST", request_.url.spec(),
248 "HTTP/1.1", "200", "OK",
249 kResponseBody);
251 TestTransactionConsumer consumer(DEFAULT_PRIORITY,
252 transaction_factory_.get());
253 consumer.Start(&request_, BoundNetLog());
255 // Will terminate when the last consumer completes.
256 base::MessageLoop::current()->Run();
258 CheckResponse(consumer, "HTTP/1.1 200 OK", kResponseBody);
261 TEST_F(QuicEndToEndTest, LargePostWithPacketLoss) {
262 // FLAGS_fake_packet_loss_percentage = 30;
263 InitializePostRequest(1024 * 1024);
265 const char kResponseBody[] = "some really big response body";
266 AddToCache("POST", request_.url.spec(),
267 "HTTP/1.1", "200", "OK",
268 kResponseBody);
270 TestTransactionConsumer consumer(DEFAULT_PRIORITY,
271 transaction_factory_.get());
272 consumer.Start(&request_, BoundNetLog());
274 // Will terminate when the last consumer completes.
275 base::MessageLoop::current()->Run();
277 CheckResponse(consumer, "HTTP/1.1 200 OK", kResponseBody);
280 TEST_F(QuicEndToEndTest, UberTest) {
281 // FLAGS_fake_packet_loss_percentage = 30;
283 const char kResponseBody[] = "some really big response body";
284 AddToCache("GET", request_.url.spec(),
285 "HTTP/1.1", "200", "OK",
286 kResponseBody);
288 std::vector<TestTransactionConsumer*> consumers;
289 size_t num_requests = 100;
290 for (size_t i = 0; i < num_requests; ++i) {
291 TestTransactionConsumer* consumer =
292 new TestTransactionConsumer(DEFAULT_PRIORITY,
293 transaction_factory_.get());
294 consumers.push_back(consumer);
295 consumer->Start(&request_, BoundNetLog());
298 // Will terminate when the last consumer completes.
299 base::MessageLoop::current()->Run();
301 for (size_t i = 0; i < num_requests; ++i) {
302 CheckResponse(*consumers[i], "HTTP/1.1 200 OK", kResponseBody);
304 STLDeleteElements(&consumers);
307 } // namespace test
308 } // namespace net