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

Side by Side Diff: content/browser/loader/resource_loader_unittest.cc

Issue 42773002: Get ClientCertStore through ResourceContext. (Closed) Base URL: svn://svn.chromium.org/chrome/trunk/src
Patch Set: changes for comment #9 Created 7 years, 1 month 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
« no previous file with comments | « content/browser/loader/resource_loader.cc ('k') | content/browser/resource_context_impl.cc » ('j') | no next file with comments »
Toggle Intra-line Diffs ('i') | Expand Comments ('e') | Collapse Comments ('c') | Show Comments Hide Comments ('s')
OLDNEW
1 // Copyright (c) 2013 The Chromium Authors. All rights reserved. 1 // Copyright (c) 2013 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 "content/browser/loader/resource_loader.h" 5 #include "content/browser/loader/resource_loader.h"
6 6
7 #include "base/run_loop.h" 7 #include "base/run_loop.h"
8 #include "content/browser/browser_thread_impl.h" 8 #include "content/browser/browser_thread_impl.h"
9 #include "content/browser/loader/resource_loader_delegate.h" 9 #include "content/browser/loader/resource_loader_delegate.h"
10 #include "content/public/browser/resource_request_info.h" 10 #include "content/public/browser/resource_request_info.h"
(...skipping 30 matching lines...) Expand all
41 std::vector<std::string> requested_authorities() { 41 std::vector<std::string> requested_authorities() {
42 return requested_authorities_; 42 return requested_authorities_;
43 } 43 }
44 44
45 // Returns the number of calls to GetClientCerts(). 45 // Returns the number of calls to GetClientCerts().
46 int request_count() { 46 int request_count() {
47 return request_count_; 47 return request_count_;
48 } 48 }
49 49
50 // net::ClientCertStore: 50 // net::ClientCertStore:
51 virtual bool GetClientCerts(const net::SSLCertRequestInfo& cert_request_info, 51 virtual void GetClientCerts(const net::SSLCertRequestInfo& cert_request_info,
52 net::CertificateList* selected_certs) OVERRIDE { 52 net::CertificateList* selected_certs,
53 const base::Closure& callback) OVERRIDE {
53 ++request_count_; 54 ++request_count_;
54 requested_authorities_ = cert_request_info.cert_authorities; 55 requested_authorities_ = cert_request_info.cert_authorities;
55 *selected_certs = response_; 56 *selected_certs = response_;
56 return true; 57 callback.Run();
57 } 58 }
58 59
59 private: 60 private:
60 const net::CertificateList response_; 61 const net::CertificateList response_;
61 int request_count_; 62 int request_count_;
62 std::vector<std::string> requested_authorities_; 63 std::vector<std::string> requested_authorities_;
63 }; 64 };
64 65
65 // Dummy implementation of ResourceHandler, instance of which is needed to 66 // Dummy implementation of ResourceHandler, instance of which is needed to
66 // initialize ResourceLoader. 67 // initialize ResourceLoader.
(...skipping 69 matching lines...) Expand 10 before | Expand all | Expand 10 after
136 137
137 net::CertificateList passed_certs() { 138 net::CertificateList passed_certs() {
138 return passed_certs_; 139 return passed_certs_;
139 } 140 }
140 141
141 private: 142 private:
142 net::CertificateList passed_certs_; 143 net::CertificateList passed_certs_;
143 int call_count_; 144 int call_count_;
144 }; 145 };
145 146
147 class ResourceContextStub : public MockResourceContext {
148 public:
149 explicit ResourceContextStub(net::URLRequestContext* test_request_context)
150 : MockResourceContext(test_request_context) {}
151
152 virtual scoped_ptr<net::ClientCertStore> CreateClientCertStore() OVERRIDE {
153 return dummy_cert_store_.Pass();
154 }
155
156 void SetClientCertStore(scoped_ptr<net::ClientCertStore> store) {
157 dummy_cert_store_ = store.Pass();
158 }
159
160 private:
161 scoped_ptr<net::ClientCertStore> dummy_cert_store_;
162 };
163
146 } // namespace 164 } // namespace
147 165
148 class ResourceLoaderTest : public testing::Test, 166 class ResourceLoaderTest : public testing::Test,
149 public ResourceLoaderDelegate { 167 public ResourceLoaderDelegate {
150 protected: 168 protected:
151 ResourceLoaderTest() 169 ResourceLoaderTest()
152 : thread_bundle_(content::TestBrowserThreadBundle::IO_MAINLOOP), 170 : thread_bundle_(content::TestBrowserThreadBundle::IO_MAINLOOP),
153 resource_context_(&test_url_request_context_) { 171 resource_context_(&test_url_request_context_) {
154 } 172 }
155 173
(...skipping 19 matching lines...) Expand all
175 } 193 }
176 virtual void DidStartRequest(ResourceLoader* loader) OVERRIDE {} 194 virtual void DidStartRequest(ResourceLoader* loader) OVERRIDE {}
177 virtual void DidReceiveRedirect(ResourceLoader* loader, 195 virtual void DidReceiveRedirect(ResourceLoader* loader,
178 const GURL& new_url) OVERRIDE {} 196 const GURL& new_url) OVERRIDE {}
179 virtual void DidReceiveResponse(ResourceLoader* loader) OVERRIDE {} 197 virtual void DidReceiveResponse(ResourceLoader* loader) OVERRIDE {}
180 virtual void DidFinishLoading(ResourceLoader* loader) OVERRIDE {} 198 virtual void DidFinishLoading(ResourceLoader* loader) OVERRIDE {}
181 199
182 content::TestBrowserThreadBundle thread_bundle_; 200 content::TestBrowserThreadBundle thread_bundle_;
183 201
184 net::TestURLRequestContext test_url_request_context_; 202 net::TestURLRequestContext test_url_request_context_;
185 content::MockResourceContext resource_context_; 203 ResourceContextStub resource_context_;
186 }; 204 };
187 205
188 // When OpenSSL is used, client cert store is not being queried in
189 // ResourceLoader.
190 #if !defined(USE_OPENSSL)
191 // Verifies if a call to net::UrlRequest::Delegate::OnCertificateRequested() 206 // Verifies if a call to net::UrlRequest::Delegate::OnCertificateRequested()
192 // causes client cert store to be queried for certificates and if the returned 207 // causes client cert store to be queried for certificates and if the returned
193 // certificates are correctly passed to the content browser client for 208 // certificates are correctly passed to the content browser client for
194 // selection. 209 // selection.
195 TEST_F(ResourceLoaderTest, ClientCertStoreLookup) { 210 TEST_F(ResourceLoaderTest, ClientCertStoreLookup) {
196 const int kRenderProcessId = 1; 211 const int kRenderProcessId = 1;
197 const int kRenderViewId = 2; 212 const int kRenderViewId = 2;
198 213
199 scoped_ptr<net::URLRequest> request(new net::URLRequest( 214 scoped_ptr<net::URLRequest> request(new net::URLRequest(
200 GURL("dummy"), NULL, 215 GURL("dummy"), NULL,
(...skipping 10 matching lines...) Expand all
211 new net::X509Certificate("test", "test", base::Time(), base::Time()))); 226 new net::X509Certificate("test", "test", base::Time(), base::Time())));
212 scoped_ptr<ClientCertStoreStub> test_store( 227 scoped_ptr<ClientCertStoreStub> test_store(
213 new ClientCertStoreStub(dummy_certs)); 228 new ClientCertStoreStub(dummy_certs));
214 EXPECT_EQ(0, test_store->request_count()); 229 EXPECT_EQ(0, test_store->request_count());
215 230
216 // Ownership of the |request| and |test_store| is about to be turned over to 231 // Ownership of the |request| and |test_store| is about to be turned over to
217 // ResourceLoader. We need to keep raw pointer copies to access these objects 232 // ResourceLoader. We need to keep raw pointer copies to access these objects
218 // later. 233 // later.
219 net::URLRequest* raw_ptr_to_request = request.get(); 234 net::URLRequest* raw_ptr_to_request = request.get();
220 ClientCertStoreStub* raw_ptr_to_store = test_store.get(); 235 ClientCertStoreStub* raw_ptr_to_store = test_store.get();
236 resource_context_.SetClientCertStore(
237 test_store.PassAs<net::ClientCertStore>());
221 238
222 scoped_ptr<ResourceHandler> resource_handler(new ResourceHandlerStub()); 239 scoped_ptr<ResourceHandler> resource_handler(new ResourceHandlerStub());
223 ResourceLoader loader(request.Pass(), resource_handler.Pass(), this, 240 ResourceLoader loader(request.Pass(), resource_handler.Pass(), this);
224 test_store.PassAs<net::ClientCertStore>());
225 241
226 // Prepare a dummy certificate request. 242 // Prepare a dummy certificate request.
227 scoped_refptr<net::SSLCertRequestInfo> cert_request_info( 243 scoped_refptr<net::SSLCertRequestInfo> cert_request_info(
228 new net::SSLCertRequestInfo()); 244 new net::SSLCertRequestInfo());
229 std::vector<std::string> dummy_authority(1, "dummy"); 245 std::vector<std::string> dummy_authority(1, "dummy");
230 cert_request_info->cert_authorities = dummy_authority; 246 cert_request_info->cert_authorities = dummy_authority;
231 247
232 // Plug in test content browser client. 248 // Plug in test content browser client.
233 SelectCertificateBrowserClient test_client; 249 SelectCertificateBrowserClient test_client;
234 ContentBrowserClient* old_client = SetBrowserClientForTesting(&test_client); 250 ContentBrowserClient* old_client = SetBrowserClientForTesting(&test_client);
235 251
236 // Everything is set up. Trigger the resource loader certificate request event 252 // Everything is set up. Trigger the resource loader certificate request event
237 // and run the message loop. 253 // and run the message loop.
238 loader.OnCertificateRequested(raw_ptr_to_request, cert_request_info.get()); 254 loader.OnCertificateRequested(raw_ptr_to_request, cert_request_info.get());
239 base::RunLoop().RunUntilIdle(); 255 base::RunLoop().RunUntilIdle();
240 256
241 // Restore the original content browser client. 257 // Restore the original content browser client.
242 SetBrowserClientForTesting(old_client); 258 SetBrowserClientForTesting(old_client);
243 259
244 // Check if the test store was queried against correct |cert_authorities|. 260 // Check if the test store was queried against correct |cert_authorities|.
245 EXPECT_EQ(1, raw_ptr_to_store->request_count()); 261 EXPECT_EQ(1, raw_ptr_to_store->request_count());
246 EXPECT_EQ(dummy_authority, raw_ptr_to_store->requested_authorities()); 262 EXPECT_EQ(dummy_authority, raw_ptr_to_store->requested_authorities());
247 263
248 // Check if the retrieved certificates were passed to the content browser 264 // Check if the retrieved certificates were passed to the content browser
249 // client. 265 // client.
250 EXPECT_EQ(1, test_client.call_count()); 266 EXPECT_EQ(1, test_client.call_count());
251 EXPECT_EQ(dummy_certs, test_client.passed_certs()); 267 EXPECT_EQ(dummy_certs, test_client.passed_certs());
252 } 268 }
253 #endif // !defined(OPENSSL) 269
270 // Verifies if a call to net::URLRequest::Delegate::OnCertificateRequested()
271 // on a platform with a NULL client cert store still calls the content browser
272 // client for selection.
273 TEST_F(ResourceLoaderTest, ClientCertStoreNull) {
274 const int kRenderProcessId = 1;
275 const int kRenderViewId = 2;
276
277 scoped_ptr<net::URLRequest> request(new net::URLRequest(
278 GURL("dummy"), NULL, resource_context_.GetRequestContext()));
279 ResourceRequestInfo::AllocateForTesting(request.get(),
280 ResourceType::MAIN_FRAME,
281 &resource_context_,
282 kRenderProcessId,
283 kRenderViewId,
284 false);
285
286 // Ownership of the |request| is about to be turned over to ResourceLoader. We
287 // need to keep a raw pointer copy to access this object later.
288 net::URLRequest* raw_ptr_to_request = request.get();
289
290 scoped_ptr<ResourceHandler> resource_handler(new ResourceHandlerStub());
291 ResourceLoader loader(request.Pass(), resource_handler.Pass(), this);
292
293 // Prepare a dummy certificate request.
294 scoped_refptr<net::SSLCertRequestInfo> cert_request_info(
295 new net::SSLCertRequestInfo());
296 std::vector<std::string> dummy_authority(1, "dummy");
297 cert_request_info->cert_authorities = dummy_authority;
298
299 // Plug in test content browser client.
300 SelectCertificateBrowserClient test_client;
301 ContentBrowserClient* old_client = SetBrowserClientForTesting(&test_client);
302
303 // Everything is set up. Trigger the resource loader certificate request event
304 // and run the message loop.
305 loader.OnCertificateRequested(raw_ptr_to_request, cert_request_info.get());
306 base::RunLoop().RunUntilIdle();
307
308 // Restore the original content browser client.
309 SetBrowserClientForTesting(old_client);
310
311 // Check if the SelectClientCertificate was called on the content browser
312 // client.
313 EXPECT_EQ(1, test_client.call_count());
314 EXPECT_EQ(net::CertificateList(), test_client.passed_certs());
315 }
254 316
255 } // namespace content 317 } // namespace content
OLDNEW
« no previous file with comments | « content/browser/loader/resource_loader.cc ('k') | content/browser/resource_context_impl.cc » ('j') | no next file with comments »

Powered by Google App Engine
This is Rietveld 408576698