OLD | NEW |
1 // Copyright (c) 2012 The Chromium Authors. All rights reserved. | 1 // Copyright (c) 2012 The Chromium Authors. All rights reserved. |
2 // Use of this source code is governed by a BSD-style license that can be | 2 // Use of this source code is governed by a BSD-style license that can be |
3 // found in the LICENSE file. | 3 // found in the LICENSE file. |
4 | 4 |
5 #include "chrome/browser/net/connection_tester.h" | 5 #include "chrome/browser/net/connection_tester.h" |
6 | 6 |
7 #include "base/prefs/testing_pref_service.h" | 7 #include "base/prefs/testing_pref_service.h" |
8 #include "content/public/browser/cookie_store_factory.h" | 8 #include "content/public/browser/cookie_store_factory.h" |
9 #include "content/public/test/test_browser_thread.h" | 9 #include "content/public/test/test_browser_thread.h" |
10 #include "content/public/browser/cookie_store_factory.h" | 10 #include "content/public/browser/cookie_store_factory.h" |
11 #include "net/cert/mock_cert_verifier.h" | 11 #include "net/cert/mock_cert_verifier.h" |
12 #include "net/dns/mock_host_resolver.h" | 12 #include "net/dns/mock_host_resolver.h" |
13 #include "net/ftp/ftp_network_layer.h" | 13 #include "net/ftp/ftp_network_layer.h" |
14 #include "net/http/http_auth_handler_factory.h" | 14 #include "net/http/http_auth_handler_factory.h" |
15 #include "net/http/http_network_layer.h" | 15 #include "net/http/http_network_layer.h" |
16 #include "net/http/http_network_session.h" | 16 #include "net/http/http_network_session.h" |
17 #include "net/http/http_server_properties_impl.h" | 17 #include "net/http/http_server_properties_impl.h" |
18 #include "net/http/transport_security_state.h" | 18 #include "net/http/transport_security_state.h" |
19 #include "net/proxy/proxy_config_service_fixed.h" | 19 #include "net/proxy/proxy_config_service_fixed.h" |
20 #include "net/proxy/proxy_service.h" | 20 #include "net/proxy/proxy_service.h" |
21 #include "net/ssl/ssl_config_service_defaults.h" | 21 #include "net/ssl/ssl_config_service_defaults.h" |
22 #include "net/test/spawned_test_server/spawned_test_server.h" | 22 #include "net/test/spawned_test_server/spawned_test_server.h" |
23 #include "net/url_request/url_request_context.h" | 23 #include "net/url_request/url_request_context.h" |
| 24 #include "net/url_request/url_request_test_util.h" |
24 #include "testing/gtest/include/gtest/gtest.h" | 25 #include "testing/gtest/include/gtest/gtest.h" |
25 #include "testing/platform_test.h" | 26 #include "testing/platform_test.h" |
26 | 27 |
27 using content::BrowserThread; | 28 using content::BrowserThread; |
28 | 29 |
29 namespace { | 30 namespace { |
30 | 31 |
31 // This is a testing delegate which simply counts how many times each of | 32 // This is a testing delegate which simply counts how many times each of |
32 // the delegate's methods were invoked. | 33 // the delegate's methods were invoked. |
33 class ConnectionTesterDelegate : public ConnectionTester::Delegate { | 34 class ConnectionTesterDelegate : public ConnectionTester::Delegate { |
(...skipping 52 matching lines...) Expand 10 before | Expand all | Expand 10 after Loading... |
86 // - Making sure each test has an IO loop running | 87 // - Making sure each test has an IO loop running |
87 // - Catching any host resolve requests and mapping them to localhost | 88 // - Catching any host resolve requests and mapping them to localhost |
88 // (so the test doesn't use any external network dependencies). | 89 // (so the test doesn't use any external network dependencies). |
89 class ConnectionTesterTest : public PlatformTest { | 90 class ConnectionTesterTest : public PlatformTest { |
90 public: | 91 public: |
91 ConnectionTesterTest() | 92 ConnectionTesterTest() |
92 : io_thread_(BrowserThread::IO, &message_loop_), | 93 : io_thread_(BrowserThread::IO, &message_loop_), |
93 test_server_(net::SpawnedTestServer::TYPE_HTTP, | 94 test_server_(net::SpawnedTestServer::TYPE_HTTP, |
94 net::SpawnedTestServer::kLocalhost, | 95 net::SpawnedTestServer::kLocalhost, |
95 // Nothing is read in this directory. | 96 // Nothing is read in this directory. |
96 base::FilePath(FILE_PATH_LITERAL("chrome"))), | 97 base::FilePath(FILE_PATH_LITERAL("chrome"))) { |
97 proxy_script_fetcher_context_(new net::URLRequestContext) { | |
98 InitializeRequestContext(); | |
99 } | 98 } |
100 | 99 |
101 protected: | 100 protected: |
102 // Destroy last the MessageLoop last to give a chance for objects like | 101 // Destroy last the MessageLoop last to give a chance for objects like |
103 // ObserverListThreadSave to shut down properly. For example, | 102 // ObserverListThreadSave to shut down properly. For example, |
104 // SSLClientAuthCache calls RemoveObserver when destroyed, but if the | 103 // SSLClientAuthCache calls RemoveObserver when destroyed, but if the |
105 // MessageLoop is already destroyed, then the RemoveObserver will be a | 104 // MessageLoop is already destroyed, then the RemoveObserver will be a |
106 // no-op, and the ObserverList will contain invalid entries. | 105 // no-op, and the ObserverList will contain invalid entries. |
107 base::MessageLoopForIO message_loop_; | 106 base::MessageLoopForIO message_loop_; |
108 content::TestBrowserThread io_thread_; | 107 content::TestBrowserThread io_thread_; |
109 net::SpawnedTestServer test_server_; | 108 net::SpawnedTestServer test_server_; |
| 109 net::TestURLRequestContext proxy_script_fetcher_context_; |
110 ConnectionTesterDelegate test_delegate_; | 110 ConnectionTesterDelegate test_delegate_; |
111 net::MockHostResolver host_resolver_; | |
112 scoped_ptr<net::CertVerifier> cert_verifier_; | |
113 scoped_ptr<net::TransportSecurityState> transport_security_state_; | |
114 scoped_ptr<net::ProxyService> proxy_service_; | |
115 scoped_refptr<net::SSLConfigService> ssl_config_service_; | |
116 scoped_ptr<net::HttpTransactionFactory> http_transaction_factory_; | |
117 net::HttpAuthHandlerRegistryFactory http_auth_handler_factory_; | |
118 net::HttpServerPropertiesImpl http_server_properties_impl_; | |
119 scoped_ptr<net::URLRequestContext> proxy_script_fetcher_context_; | |
120 | |
121 private: | |
122 void InitializeRequestContext() { | |
123 proxy_script_fetcher_context_->set_host_resolver(&host_resolver_); | |
124 cert_verifier_.reset(new net::MockCertVerifier); | |
125 transport_security_state_.reset(new net::TransportSecurityState); | |
126 proxy_script_fetcher_context_->set_cert_verifier(cert_verifier_.get()); | |
127 proxy_script_fetcher_context_->set_transport_security_state( | |
128 transport_security_state_.get()); | |
129 proxy_script_fetcher_context_->set_http_auth_handler_factory( | |
130 &http_auth_handler_factory_); | |
131 proxy_service_.reset(net::ProxyService::CreateDirect()); | |
132 proxy_script_fetcher_context_->set_proxy_service(proxy_service_.get()); | |
133 ssl_config_service_ = new net::SSLConfigServiceDefaults; | |
134 net::HttpNetworkSession::Params session_params; | |
135 session_params.host_resolver = &host_resolver_; | |
136 session_params.cert_verifier = cert_verifier_.get(); | |
137 session_params.transport_security_state = transport_security_state_.get(); | |
138 session_params.http_auth_handler_factory = &http_auth_handler_factory_; | |
139 session_params.ssl_config_service = ssl_config_service_.get(); | |
140 session_params.proxy_service = proxy_service_.get(); | |
141 session_params.http_server_properties = | |
142 http_server_properties_impl_.GetWeakPtr(); | |
143 scoped_refptr<net::HttpNetworkSession> network_session( | |
144 new net::HttpNetworkSession(session_params)); | |
145 http_transaction_factory_.reset( | |
146 new net::HttpNetworkLayer(network_session.get())); | |
147 proxy_script_fetcher_context_->set_http_transaction_factory( | |
148 http_transaction_factory_.get()); | |
149 // In-memory cookie store. | |
150 proxy_script_fetcher_context_->set_cookie_store( | |
151 content::CreateCookieStore(content::CookieStoreConfig())); | |
152 } | |
153 }; | 111 }; |
154 | 112 |
155 TEST_F(ConnectionTesterTest, RunAllTests) { | 113 TEST_F(ConnectionTesterTest, RunAllTests) { |
156 ASSERT_TRUE(test_server_.Start()); | 114 ASSERT_TRUE(test_server_.Start()); |
157 | 115 |
158 ConnectionTester tester(&test_delegate_, | 116 ConnectionTester tester(&test_delegate_, |
159 proxy_script_fetcher_context_.get(), | 117 &proxy_script_fetcher_context_, |
160 NULL); | 118 NULL); |
161 | 119 |
162 // Start the test suite on URL "echoall". | 120 // Start the test suite on URL "echoall". |
163 // TODO(eroman): Is this URL right? | 121 // TODO(eroman): Is this URL right? |
164 tester.RunAllTests(test_server_.GetURL("echoall")); | 122 tester.RunAllTests(test_server_.GetURL("echoall")); |
165 | 123 |
166 // Wait for all the tests to complete. | 124 // Wait for all the tests to complete. |
167 base::MessageLoop::current()->Run(); | 125 base::MessageLoop::current()->Run(); |
168 | 126 |
169 const int kNumExperiments = | 127 const int kNumExperiments = |
170 ConnectionTester::PROXY_EXPERIMENT_COUNT * | 128 ConnectionTester::PROXY_EXPERIMENT_COUNT * |
171 ConnectionTester::HOST_RESOLVER_EXPERIMENT_COUNT; | 129 ConnectionTester::HOST_RESOLVER_EXPERIMENT_COUNT; |
172 | 130 |
173 EXPECT_EQ(1, test_delegate_.start_connection_test_suite_count()); | 131 EXPECT_EQ(1, test_delegate_.start_connection_test_suite_count()); |
174 EXPECT_EQ(kNumExperiments, | 132 EXPECT_EQ(kNumExperiments, |
175 test_delegate_.start_connection_test_experiment_count()); | 133 test_delegate_.start_connection_test_experiment_count()); |
176 EXPECT_EQ(kNumExperiments, | 134 EXPECT_EQ(kNumExperiments, |
177 test_delegate_.completed_connection_test_experiment_count()); | 135 test_delegate_.completed_connection_test_experiment_count()); |
178 EXPECT_EQ(1, test_delegate_.completed_connection_test_suite_count()); | 136 EXPECT_EQ(1, test_delegate_.completed_connection_test_suite_count()); |
179 } | 137 } |
180 | 138 |
181 TEST_F(ConnectionTesterTest, DeleteWhileInProgress) { | 139 TEST_F(ConnectionTesterTest, DeleteWhileInProgress) { |
182 ASSERT_TRUE(test_server_.Start()); | 140 ASSERT_TRUE(test_server_.Start()); |
183 | 141 |
184 scoped_ptr<ConnectionTester> tester( | 142 scoped_ptr<ConnectionTester> tester( |
185 new ConnectionTester(&test_delegate_, | 143 new ConnectionTester(&test_delegate_, |
186 proxy_script_fetcher_context_.get(), | 144 &proxy_script_fetcher_context_, |
187 NULL)); | 145 NULL)); |
188 | 146 |
189 // Start the test suite on URL "echoall". | 147 // Start the test suite on URL "echoall". |
190 // TODO(eroman): Is this URL right? | 148 // TODO(eroman): Is this URL right? |
191 tester->RunAllTests(test_server_.GetURL("echoall")); | 149 tester->RunAllTests(test_server_.GetURL("echoall")); |
192 | 150 |
193 // Don't run the message loop at all. Otherwise the experiment's request may | 151 // Don't run the message loop at all. Otherwise the experiment's request may |
194 // complete and post a task to run the next experiment before we quit the | 152 // complete and post a task to run the next experiment before we quit the |
195 // message loop. | 153 // message loop. |
196 | 154 |
(...skipping 12 matching lines...) Expand all Loading... |
209 // net::ClientSocketPoolBaseHelper, since the "Group" holds a pointer | 167 // net::ClientSocketPoolBaseHelper, since the "Group" holds a pointer |
210 // |backup_task| that it will try to deref during the destructor, but | 168 // |backup_task| that it will try to deref during the destructor, but |
211 // depending on the order that pending tasks were deleted in, it might | 169 // depending on the order that pending tasks were deleted in, it might |
212 // already be invalid! See http://crbug.com/43291. | 170 // already be invalid! See http://crbug.com/43291. |
213 base::MessageLoop::current()->PostTask(FROM_HERE, | 171 base::MessageLoop::current()->PostTask(FROM_HERE, |
214 base::MessageLoop::QuitClosure()); | 172 base::MessageLoop::QuitClosure()); |
215 base::MessageLoop::current()->Run(); | 173 base::MessageLoop::current()->Run(); |
216 } | 174 } |
217 | 175 |
218 } // namespace | 176 } // namespace |
OLD | NEW |