Chromium Code Reviews
chromiumcodereview-hr@appspot.gserviceaccount.com (chromiumcodereview-hr) | Please choose your nickname with Settings | Help | Chromium Project | Gerrit Changes | Sign out
(67)

Side by Side Diff: chrome/browser/net/connection_tester_unittest.cc

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

Powered by Google App Engine
This is Rietveld 408576698