OLD | NEW |
1 // Copyright (c) 2010 The Chromium Authors. All rights reserved. | 1 // Copyright (c) 2010 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 "chrome/browser/io_thread.h" | 7 #include "chrome/browser/browser_thread.h" |
8 #include "chrome/test/testing_pref_service.h" | 8 #include "chrome/test/testing_pref_service.h" |
| 9 #include "net/base/cert_verifier.h" |
| 10 #include "net/base/cookie_monster.h" |
| 11 #include "net/base/dnsrr_resolver.h" |
9 #include "net/base/mock_host_resolver.h" | 12 #include "net/base/mock_host_resolver.h" |
| 13 #include "net/base/ssl_config_service_defaults.h" |
| 14 #include "net/ftp/ftp_network_layer.h" |
| 15 #include "net/http/http_auth_handler_factory.h" |
| 16 #include "net/http/http_network_layer.h" |
| 17 #include "net/proxy/proxy_config_service_fixed.h" |
| 18 #include "net/socket/client_socket_factory.h" |
| 19 #include "net/spdy/spdy_session_pool.h" |
10 #include "net/test/test_server.h" | 20 #include "net/test/test_server.h" |
| 21 #include "net/url_request/url_request_context.h" |
11 #include "testing/gtest/include/gtest/gtest.h" | 22 #include "testing/gtest/include/gtest/gtest.h" |
12 #include "testing/platform_test.h" | 23 #include "testing/platform_test.h" |
13 | 24 |
14 namespace { | 25 namespace { |
15 | 26 |
16 // This is a testing delegate which simply counts how many times each of | 27 // This is a testing delegate which simply counts how many times each of |
17 // the delegate's methods were invoked. | 28 // the delegate's methods were invoked. |
18 class ConnectionTesterDelegate : public ConnectionTester::Delegate { | 29 class ConnectionTesterDelegate : public ConnectionTester::Delegate { |
19 public: | 30 public: |
20 ConnectionTesterDelegate() | 31 ConnectionTesterDelegate() |
(...skipping 48 matching lines...) Expand 10 before | Expand all | Expand 10 after Loading... |
69 | 80 |
70 // The test fixture is responsible for: | 81 // The test fixture is responsible for: |
71 // - Making sure each test has an IO loop running | 82 // - Making sure each test has an IO loop running |
72 // - Catching any host resolve requests and mapping them to localhost | 83 // - Catching any host resolve requests and mapping them to localhost |
73 // (so the test doesn't use any external network dependencies). | 84 // (so the test doesn't use any external network dependencies). |
74 class ConnectionTesterTest : public PlatformTest { | 85 class ConnectionTesterTest : public PlatformTest { |
75 public: | 86 public: |
76 ConnectionTesterTest() | 87 ConnectionTesterTest() |
77 : test_server_(net::TestServer::TYPE_HTTP, | 88 : test_server_(net::TestServer::TYPE_HTTP, |
78 FilePath(FILE_PATH_LITERAL("net/data/url_request_unittest"))), | 89 FilePath(FILE_PATH_LITERAL("net/data/url_request_unittest"))), |
| 90 client_socket_factory_(net::ClientSocketFactory::GetDefaultFactory()), |
| 91 proxy_script_fetcher_context_(new URLRequestContext), |
79 message_loop_(MessageLoop::TYPE_IO), | 92 message_loop_(MessageLoop::TYPE_IO), |
80 pref_service(new TestingPrefService()), | 93 io_thread_(BrowserThread::IO, &message_loop_) { |
81 io_thread_(pref_service.get(), NULL) { | 94 InitializeRequestContext(); |
82 scoped_refptr<net::RuleBasedHostResolverProc> catchall_resolver( | |
83 new net::RuleBasedHostResolverProc(NULL)); | |
84 | |
85 catchall_resolver->AddRule("*", "127.0.0.1"); | |
86 | |
87 scoped_host_resolver_proc_.Init(catchall_resolver); | |
88 } | 95 } |
89 | 96 |
90 protected: | 97 protected: |
91 net::ScopedDefaultHostResolverProc scoped_host_resolver_proc_; | |
92 net::TestServer test_server_; | 98 net::TestServer test_server_; |
93 ConnectionTesterDelegate test_delegate_; | 99 ConnectionTesterDelegate test_delegate_; |
| 100 net::ClientSocketFactory* const client_socket_factory_; |
| 101 net::MockHostResolver host_resolver_; |
| 102 net::CertVerifier cert_verifier_; |
| 103 net::DnsRRResolver dnsrr_resolver_; |
| 104 scoped_refptr<net::ProxyService> proxy_service_; |
| 105 scoped_refptr<net::SSLConfigService> ssl_config_service_; |
| 106 scoped_ptr<net::HttpTransactionFactory> http_transaction_factory_; |
| 107 net::HttpAuthHandlerRegistryFactory http_auth_handler_factory_; |
| 108 scoped_refptr<URLRequestContext> proxy_script_fetcher_context_; |
| 109 |
| 110 private: |
| 111 void InitializeRequestContext() { |
| 112 proxy_script_fetcher_context_->set_host_resolver(&host_resolver_); |
| 113 proxy_script_fetcher_context_->set_cert_verifier(&cert_verifier_); |
| 114 proxy_script_fetcher_context_->set_dnsrr_resolver(&dnsrr_resolver_); |
| 115 proxy_script_fetcher_context_->set_http_auth_handler_factory( |
| 116 &http_auth_handler_factory_); |
| 117 proxy_service_ = net::ProxyService::CreateDirect(); |
| 118 proxy_script_fetcher_context_->set_proxy_service(proxy_service_); |
| 119 ssl_config_service_ = net::SSLConfigService::CreateSystemSSLConfigService(); |
| 120 proxy_script_fetcher_context_->set_http_transaction_factory( |
| 121 new net::HttpNetworkLayer( |
| 122 client_socket_factory_, |
| 123 &host_resolver_, |
| 124 &cert_verifier_, |
| 125 &dnsrr_resolver_, |
| 126 NULL /* DNS cert provenance checker */, |
| 127 NULL /* ssl_host_info_factory */, |
| 128 proxy_service_.get(), |
| 129 ssl_config_service_, |
| 130 new net::SpdySessionPool(ssl_config_service_), |
| 131 &http_auth_handler_factory_, |
| 132 NULL /* NetworkDelegate */, |
| 133 NULL /* NetLog */)); |
| 134 // In-memory cookie store. |
| 135 proxy_script_fetcher_context_->set_cookie_store( |
| 136 new net::CookieMonster(NULL, NULL)); |
| 137 } |
| 138 |
94 MessageLoop message_loop_; | 139 MessageLoop message_loop_; |
95 scoped_ptr<PrefService> pref_service; | 140 BrowserThread io_thread_; |
96 IOThread io_thread_; // Needed for creating ProxyScriptFetchers. | |
97 }; | 141 }; |
98 | 142 |
99 TEST_F(ConnectionTesterTest, RunAllTests) { | 143 TEST_F(ConnectionTesterTest, RunAllTests) { |
100 ASSERT_TRUE(test_server_.Start()); | 144 ASSERT_TRUE(test_server_.Start()); |
101 | 145 |
102 ConnectionTester tester(&test_delegate_, &io_thread_); | 146 ConnectionTester tester(&test_delegate_, proxy_script_fetcher_context_); |
103 | 147 |
104 // Start the test suite on URL "echoall". | 148 // Start the test suite on URL "echoall". |
105 // TODO(eroman): Is this URL right? | 149 // TODO(eroman): Is this URL right? |
106 tester.RunAllTests(test_server_.GetURL("echoall")); | 150 tester.RunAllTests(test_server_.GetURL("echoall")); |
107 | 151 |
108 // Wait for all the tests to complete. | 152 // Wait for all the tests to complete. |
109 MessageLoop::current()->Run(); | 153 MessageLoop::current()->Run(); |
110 | 154 |
111 const int kNumExperiments = | 155 const int kNumExperiments = |
112 ConnectionTester::PROXY_EXPERIMENT_COUNT * | 156 ConnectionTester::PROXY_EXPERIMENT_COUNT * |
113 ConnectionTester::HOST_RESOLVER_EXPERIMENT_COUNT; | 157 ConnectionTester::HOST_RESOLVER_EXPERIMENT_COUNT; |
114 | 158 |
115 EXPECT_EQ(1, test_delegate_.start_connection_test_suite_count()); | 159 EXPECT_EQ(1, test_delegate_.start_connection_test_suite_count()); |
116 EXPECT_EQ(kNumExperiments, | 160 EXPECT_EQ(kNumExperiments, |
117 test_delegate_.start_connection_test_experiment_count()); | 161 test_delegate_.start_connection_test_experiment_count()); |
118 EXPECT_EQ(kNumExperiments, | 162 EXPECT_EQ(kNumExperiments, |
119 test_delegate_.completed_connection_test_experiment_count()); | 163 test_delegate_.completed_connection_test_experiment_count()); |
120 EXPECT_EQ(1, test_delegate_.completed_connection_test_suite_count()); | 164 EXPECT_EQ(1, test_delegate_.completed_connection_test_suite_count()); |
121 } | 165 } |
122 | 166 |
123 TEST_F(ConnectionTesterTest, DeleteWhileInProgress) { | 167 TEST_F(ConnectionTesterTest, DeleteWhileInProgress) { |
124 ASSERT_TRUE(test_server_.Start()); | 168 ASSERT_TRUE(test_server_.Start()); |
125 | 169 |
126 scoped_ptr<ConnectionTester> tester( | 170 scoped_ptr<ConnectionTester> tester( |
127 new ConnectionTester(&test_delegate_, &io_thread_)); | 171 new ConnectionTester(&test_delegate_, proxy_script_fetcher_context_)); |
128 | 172 |
129 // Start the test suite on URL "echoall". | 173 // Start the test suite on URL "echoall". |
130 // TODO(eroman): Is this URL right? | 174 // TODO(eroman): Is this URL right? |
131 tester->RunAllTests(test_server_.GetURL("echoall")); | 175 tester->RunAllTests(test_server_.GetURL("echoall")); |
132 | 176 |
133 MessageLoop::current()->RunAllPending(); | 177 MessageLoop::current()->RunAllPending(); |
134 | 178 |
135 EXPECT_EQ(1, test_delegate_.start_connection_test_suite_count()); | 179 EXPECT_EQ(1, test_delegate_.start_connection_test_suite_count()); |
136 EXPECT_EQ(1, test_delegate_.start_connection_test_experiment_count()); | 180 EXPECT_EQ(1, test_delegate_.start_connection_test_experiment_count()); |
137 EXPECT_EQ(0, test_delegate_.completed_connection_test_experiment_count()); | 181 EXPECT_EQ(0, test_delegate_.completed_connection_test_experiment_count()); |
138 EXPECT_EQ(0, test_delegate_.completed_connection_test_suite_count()); | 182 EXPECT_EQ(0, test_delegate_.completed_connection_test_suite_count()); |
139 | 183 |
140 // Delete the ConnectionTester while it is in progress. | 184 // Delete the ConnectionTester while it is in progress. |
141 tester.reset(); | 185 tester.reset(); |
142 | 186 |
143 // Drain the tasks on the message loop. | 187 // Drain the tasks on the message loop. |
144 // | 188 // |
145 // Note that we cannot simply stop the message loop, since that will delete | 189 // Note that we cannot simply stop the message loop, since that will delete |
146 // any pending tasks instead of running them. This causes a problem with | 190 // any pending tasks instead of running them. This causes a problem with |
147 // net::ClientSocketPoolBaseHelper, since the "Group" holds a pointer | 191 // net::ClientSocketPoolBaseHelper, since the "Group" holds a pointer |
148 // |backup_task| that it will try to deref during the destructor, but | 192 // |backup_task| that it will try to deref during the destructor, but |
149 // depending on the order that pending tasks were deleted in, it might | 193 // depending on the order that pending tasks were deleted in, it might |
150 // already be invalid! See http://crbug.com/43291. | 194 // already be invalid! See http://crbug.com/43291. |
151 MessageLoop::current()->PostTask(FROM_HERE, new MessageLoop::QuitTask()); | 195 MessageLoop::current()->PostTask(FROM_HERE, new MessageLoop::QuitTask()); |
152 MessageLoop::current()->Run(); | 196 MessageLoop::current()->Run(); |
153 } | 197 } |
154 | 198 |
155 } // namespace | 199 } // namespace |
156 | |
OLD | NEW |