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