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

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

Issue 12035105: Move client certificates retrieval logic out of the SSL sockets. (Closed) Base URL: https://chromium.googlesource.com/chromium/src.git@master
Patch Set: Add missing license header Created 7 years, 10 months 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
« no previous file with comments | « content/browser/loader/resource_loader.cc ('k') | content/browser/ssl/ssl_error_handler.h » ('j') | no next file with comments »
Toggle Intra-line Diffs ('i') | Expand Comments ('e') | Collapse Comments ('c') | Show Comments Hide Comments ('s')
OLDNEW
(Empty)
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
3 // found in the LICENSE file.
4
5 #include "content/browser/loader/resource_loader.h"
6
7 #include "base/message_loop.h"
8 #include "content/browser/browser_thread_impl.h"
9 #include "content/browser/loader/resource_loader_delegate.h"
10 #include "content/public/browser/resource_request_info.h"
11 #include "content/public/test/mock_resource_context.h"
12 #include "content/test/test_content_browser_client.h"
13 #include "net/base/client_cert_store.h"
14 #include "net/base/ssl_cert_request_info.h"
15 #include "net/base/x509_certificate.h"
16 #include "net/url_request/url_request.h"
17 #include "testing/gtest/include/gtest/gtest.h"
18
19 namespace content {
20 namespace {
21
22 // Stub client certificate store that returns a preset list of certificates for
23 // each request and records the arguments of the most recent request for later
24 // inspection.
25 class ClientCertStoreStub : public net::ClientCertStore {
26 public:
27 ClientCertStoreStub(const net::CertificateList& certs)
28 : response_(certs),
29 request_count_(0) {}
30
31 virtual ~ClientCertStoreStub() {}
32
33 // Returns |cert_authorities| field of the certificate request passed in the
34 // most recent call to GetClientCerts().
35 // TODO(ppi): Make the stub independent from the internal representation of
36 // SSLCertRequestInfo. For now it seems that we cannot neither save the
37 // scoped_refptr<> (since it is never passed to us) nor copy the entire
38 // CertificateRequestInfo (since there is no copy constructor).
39 std::vector<std::string> requested_authorities() {
40 return requested_authorities_;
41 }
42
43 // Returns the number of calls to GetClientCerts().
44 int request_count() {
45 return request_count_;
46 }
47
48 // net::ClientCertStore:
49 virtual bool GetClientCerts(const net::SSLCertRequestInfo& cert_request_info,
50 net::CertificateList* selected_certs) OVERRIDE {
51 ++request_count_;
52 requested_authorities_ = cert_request_info.cert_authorities;
53 *selected_certs = response_;
54 return true;
55 }
56
57 private:
58 const net::CertificateList response_;
59 int request_count_;
60 std::vector<std::string> requested_authorities_;
61 };
62
63 // Dummy implementation of ResourceHandler, instance of which is needed to
64 // initialize ResourceLoader.
65 class ResourceHandlerStub : public ResourceHandler {
66 public:
67 virtual bool OnUploadProgress(int request_id,
68 uint64 position,
69 uint64 size) OVERRIDE {
70 return true;
71 }
72
73 virtual bool OnRequestRedirected(int request_id,
74 const GURL& url,
75 ResourceResponse* response,
76 bool* defer) OVERRIDE {
77 return true;
78 }
79
80 virtual bool OnResponseStarted(int request_id,
81 ResourceResponse* response,
82 bool* defer) OVERRIDE { return true; }
83
84 virtual bool OnWillStart(int request_id,
85 const GURL& url,
86 bool* defer) OVERRIDE {
87 return true;
88 }
89
90 virtual bool OnWillRead(int request_id,
91 net::IOBuffer** buf,
92 int* buf_size,
93 int min_size) OVERRIDE {
94 return true;
95 }
96
97 virtual bool OnReadCompleted(int request_id,
98 int bytes_read,
99 bool* defer) OVERRIDE {
100 return true;
101 }
102
103 virtual bool OnResponseCompleted(int request_id,
104 const net::URLRequestStatus& status,
105 const std::string& security_info) OVERRIDE {
106 return true;
107 }
108
109 virtual void OnDataDownloaded(int request_id,
110 int bytes_downloaded) OVERRIDE {}
111 };
112
113 // Test browser client that captures calls to SelectClientCertificates and
114 // records the arguments of the most recent call for later inspection.
115 class SelectCertificateBrowserClient : public TestContentBrowserClient {
116 public:
117 SelectCertificateBrowserClient() : call_count_(0) {}
118
119 virtual void SelectClientCertificate(
120 int render_process_id,
121 int render_view_id,
122 const net::HttpNetworkSession* network_session,
123 net::SSLCertRequestInfo* cert_request_info,
124 const base::Callback<void(net::X509Certificate*)>& callback) OVERRIDE {
125 ++call_count_;
126 passed_certs_ = cert_request_info->client_certs;
127 }
128
129 int call_count() {
130 return call_count_;
131 }
132
133 net::CertificateList passed_certs() {
134 return passed_certs_;
135 }
136
137 private:
138 net::CertificateList passed_certs_;
139 int call_count_;
140 };
141
142 } // namespace
143
144 class ResourceLoaderTest : public testing::Test,
145 public ResourceLoaderDelegate {
146 protected:
147 // testing::Test:
148 virtual void SetUp() OVERRIDE {
149 message_loop_.reset(new MessageLoop(MessageLoop::TYPE_IO));
150 ui_thread_.reset(new BrowserThreadImpl(BrowserThread::UI,
151 message_loop_.get()));
152 io_thread_.reset(new BrowserThreadImpl(BrowserThread::IO,
153 message_loop_.get()));
154 }
155
156 // ResourceLoaderDelegate:
157 virtual ResourceDispatcherHostLoginDelegate* CreateLoginDelegate(
158 ResourceLoader* loader,
159 net::AuthChallengeInfo* auth_info) OVERRIDE {
160 return NULL;
161 }
162 virtual bool AcceptAuthRequest(
163 ResourceLoader* loader,
164 net::AuthChallengeInfo* auth_info) OVERRIDE {
165 return false;
166 };
167 virtual bool AcceptSSLClientCertificateRequest(
168 ResourceLoader* loader,
169 net::SSLCertRequestInfo* cert_info) OVERRIDE {
170 return true;
171 }
172 virtual bool HandleExternalProtocol(ResourceLoader* loader,
173 const GURL& url) OVERRIDE {
174 return false;
175 }
176 virtual void DidStartRequest(ResourceLoader* loader) OVERRIDE {}
177 virtual void DidReceiveRedirect(ResourceLoader* loader,
178 const GURL& new_url) OVERRIDE {}
179 virtual void DidReceiveResponse(ResourceLoader* loader) OVERRIDE {}
180 virtual void DidFinishLoading(ResourceLoader* loader) OVERRIDE {}
181
182 scoped_ptr<MessageLoop> message_loop_;
183 scoped_ptr<BrowserThreadImpl> ui_thread_;
184 scoped_ptr<BrowserThreadImpl> io_thread_;
185
186 content::MockResourceContext resource_context_;
187 };
188
189 // When OpenSSL is used, client cert store is not being queried in
190 // ResourceLoader.
191 #if !defined(USE_OPENSSL)
192 // Verifies if a call to net::UrlRequest::Delegate::OnCertificateRequested()
193 // causes client cert store to be queried for certificates and if the returned
194 // certificates are correctly passed to the content browser client for
195 // selection.
196 TEST_F(ResourceLoaderTest, ClientCertStoreLookup) {
197 const int kRenderProcessId = 1;
198 const int kRenderViewId = 2;
199
200 scoped_ptr<net::URLRequest> request(new net::URLRequest(
201 GURL("dummy"), NULL,
202 resource_context_.GetRequestContext()));
203 ResourceRequestInfo::AllocateForTesting(request.get(),
204 ResourceType::MAIN_FRAME,
205 &resource_context_,
206 kRenderProcessId,
207 kRenderViewId);
208
209 // Set up the test client cert store.
210 net::CertificateList dummy_certs(1, scoped_refptr<net::X509Certificate>(
211 new net::X509Certificate("test", "test", base::Time(), base::Time())));
212 scoped_ptr<ClientCertStoreStub> test_store(
213 new ClientCertStoreStub(dummy_certs));
214 EXPECT_EQ(0, test_store->request_count());
215
216 // 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
218 // later.
219 net::URLRequest* raw_ptr_to_request = request.get();
220 ClientCertStoreStub* raw_ptr_to_store = test_store.get();
221
222 scoped_ptr<ResourceHandler> resource_handler(new ResourceHandlerStub());
223 ResourceLoader loader(request.Pass(), resource_handler.Pass(), this,
224 test_store.PassAs<net::ClientCertStore>());
225
226 // Prepare a dummy certificate request.
227 scoped_refptr<net::SSLCertRequestInfo> cert_request_info(
228 new net::SSLCertRequestInfo());
229 std::vector<std::string> dummy_authority(1, "dummy");
230 cert_request_info->cert_authorities = dummy_authority;
231
232 // Plug in test content browser client.
233 ContentBrowserClient* old_client = GetContentClient()->browser();
234 SelectCertificateBrowserClient test_client;
235 GetContentClient()->set_browser_for_testing(&test_client);
236
237 // Everything is set up. Trigger the resource loader certificate request event
238 // and run the message loop.
239 loader.OnCertificateRequested(raw_ptr_to_request, cert_request_info.get());
240 message_loop_->RunUntilIdle();
241
242 // Restore the original content browser client.
243 GetContentClient()->set_browser_for_testing(old_client);
244
245 // Check if the test store was queried against correct |cert_authorities|.
246 EXPECT_EQ(1, raw_ptr_to_store->request_count());
247 EXPECT_EQ(dummy_authority, raw_ptr_to_store->requested_authorities());
248
249 // Check if the retrieved certificates were passed to the content browser
250 // client.
251 EXPECT_EQ(1, test_client.call_count());
252 EXPECT_EQ(dummy_certs, test_client.passed_certs());
253 }
254 #endif // !defined(OPENSSL)
255
256 } // namespace content
OLDNEW
« no previous file with comments | « content/browser/loader/resource_loader.cc ('k') | content/browser/ssl/ssl_error_handler.h » ('j') | no next file with comments »

Powered by Google App Engine
This is Rietveld 408576698