OLD | NEW |
1 // Copyright 2015 The Chromium Authors. All rights reserved. | 1 // Copyright 2015 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/chromeos/certificate_provider/certificate_provider_serv
ice.h" | 5 #include "chrome/browser/chromeos/certificate_provider/certificate_provider_serv
ice.h" |
6 | 6 |
7 #include <set> | 7 #include <set> |
8 | 8 |
9 #include "base/bind.h" | 9 #include "base/bind.h" |
10 #include "base/stl_util.h" | 10 #include "base/stl_util.h" |
(...skipping 166 matching lines...) Expand 10 before | Expand all | Expand 10 after Loading... |
177 void SetCertificateProvidedByExtension( | 177 void SetCertificateProvidedByExtension( |
178 const std::string& extension_id, | 178 const std::string& extension_id, |
179 int cert_request_id, | 179 int cert_request_id, |
180 const certificate_provider::CertificateInfo& cert_info) { | 180 const certificate_provider::CertificateInfo& cert_info) { |
181 certificate_provider::CertificateInfoList infos; | 181 certificate_provider::CertificateInfoList infos; |
182 infos.push_back(cert_info); | 182 infos.push_back(cert_info); |
183 service_->SetCertificatesProvidedByExtension(extension_id, cert_request_id, | 183 service_->SetCertificatesProvidedByExtension(extension_id, cert_request_id, |
184 infos); | 184 infos); |
185 } | 185 } |
186 | 186 |
| 187 bool CheckLookUpCertificate( |
| 188 const certificate_provider::CertificateInfo& cert_info, |
| 189 bool expected_is_certificate_known, |
| 190 bool expected_is_currently_provided, |
| 191 const std::string& expected_extension_id) { |
| 192 bool is_currently_provided = !expected_is_currently_provided; |
| 193 std::string extension_id; |
| 194 if (expected_is_certificate_known != |
| 195 service_->LookUpCertificate(*cert_info.certificate, |
| 196 &is_currently_provided, &extension_id)) { |
| 197 LOG(ERROR) << "Wrong return value."; |
| 198 return false; |
| 199 } |
| 200 if (expected_is_currently_provided != is_currently_provided) { |
| 201 LOG(ERROR) << "Wrong |is_currently_provided|."; |
| 202 return false; |
| 203 } |
| 204 if (expected_extension_id != extension_id) { |
| 205 LOG(ERROR) << "Wrong extension id. Got " << extension_id; |
| 206 return false; |
| 207 } |
| 208 return true; |
| 209 } |
| 210 |
187 scoped_refptr<base::TestMockTimeTaskRunner> task_runner_; | 211 scoped_refptr<base::TestMockTimeTaskRunner> task_runner_; |
188 base::ThreadTaskRunnerHandle task_runner_handle_; | 212 base::ThreadTaskRunnerHandle task_runner_handle_; |
189 TestDelegate* test_delegate_ = nullptr; | 213 TestDelegate* test_delegate_ = nullptr; |
190 net::ClientKeyStore* const client_key_store_; | 214 net::ClientKeyStore* const client_key_store_; |
191 scoped_ptr<CertificateProvider> certificate_provider_; | 215 scoped_ptr<CertificateProvider> certificate_provider_; |
192 scoped_ptr<CertificateProviderService> service_; | 216 scoped_ptr<CertificateProviderService> service_; |
193 const certificate_provider::CertificateInfo cert_info1_; | 217 const certificate_provider::CertificateInfo cert_info1_; |
194 const certificate_provider::CertificateInfo cert_info2_; | 218 const certificate_provider::CertificateInfo cert_info2_; |
195 | 219 |
196 private: | 220 private: |
(...skipping 14 matching lines...) Expand all Loading... |
211 | 235 |
212 task_runner_->RunUntilIdle(); | 236 task_runner_->RunUntilIdle(); |
213 // No certificates set until all registered extensions replied. | 237 // No certificates set until all registered extensions replied. |
214 EXPECT_TRUE(certs.empty()); | 238 EXPECT_TRUE(certs.empty()); |
215 | 239 |
216 SetCertificateProvidedByExtension(kExtension2, cert_request_id, cert_info2_); | 240 SetCertificateProvidedByExtension(kExtension2, cert_request_id, cert_info2_); |
217 | 241 |
218 task_runner_->RunUntilIdle(); | 242 task_runner_->RunUntilIdle(); |
219 EXPECT_EQ(2u, certs.size()); | 243 EXPECT_EQ(2u, certs.size()); |
220 | 244 |
| 245 // Verify that the ClientKeyStore returns key handles for the provide certs. |
221 EXPECT_TRUE( | 246 EXPECT_TRUE( |
222 client_key_store_->FetchClientCertPrivateKey(*cert_info1_.certificate)); | 247 client_key_store_->FetchClientCertPrivateKey(*cert_info1_.certificate)); |
223 EXPECT_TRUE( | 248 EXPECT_TRUE( |
224 client_key_store_->FetchClientCertPrivateKey(*cert_info2_.certificate)); | 249 client_key_store_->FetchClientCertPrivateKey(*cert_info2_.certificate)); |
225 | 250 |
226 // Deregister the extensions as certificate providers. The next | 251 // Deregister the extensions as certificate providers. The next |
227 // GetCertificates call must report an empty list of certs. | 252 // GetCertificates call must report an empty list of certs. |
228 test_delegate_->provider_extensions_.clear(); | 253 test_delegate_->provider_extensions_.clear(); |
229 | 254 |
230 // No request expected. | 255 // No request expected. |
231 test_delegate_->ClearAndExpectRequest(TestDelegate::RequestType::NONE); | 256 test_delegate_->ClearAndExpectRequest(TestDelegate::RequestType::NONE); |
232 | 257 |
233 certificate_provider_->GetCertificates( | 258 certificate_provider_->GetCertificates( |
234 base::Bind(&StoreCertificates, &certs)); | 259 base::Bind(&StoreCertificates, &certs)); |
235 | 260 |
236 task_runner_->RunUntilIdle(); | 261 task_runner_->RunUntilIdle(); |
237 // As |certs| was not empty before, this ensures that StoreCertificates() was | 262 // As |certs| was not empty before, this ensures that StoreCertificates() was |
238 // called. | 263 // called. |
239 EXPECT_TRUE(certs.empty()); | 264 EXPECT_TRUE(certs.empty()); |
240 } | 265 } |
241 | 266 |
| 267 TEST_F(CertificateProviderServiceTest, LookUpCertificate) { |
| 268 // Provide only |cert_info1_|. |
| 269 { |
| 270 const int cert_request_id = RequestCertificatesFromExtensions(nullptr); |
| 271 SetCertificateProvidedByExtension(kExtension1, cert_request_id, |
| 272 cert_info1_); |
| 273 task_runner_->RunUntilIdle(); |
| 274 } |
| 275 |
| 276 EXPECT_TRUE(CheckLookUpCertificate(cert_info1_, true /* is known */, |
| 277 true /* is currently provided */, |
| 278 kExtension1)); |
| 279 |
| 280 EXPECT_TRUE(CheckLookUpCertificate(cert_info2_, false /* is not known */, |
| 281 false /* is currently not provided */, |
| 282 std::string())); |
| 283 |
| 284 // Provide only |cert_info2_| from |kExtension2|. |
| 285 test_delegate_->provider_extensions_.insert(kExtension2); |
| 286 { |
| 287 const int cert_request_id = RequestCertificatesFromExtensions(nullptr); |
| 288 service_->SetCertificatesProvidedByExtension( |
| 289 kExtension1, cert_request_id, |
| 290 certificate_provider::CertificateInfoList()); |
| 291 SetCertificateProvidedByExtension(kExtension2, cert_request_id, |
| 292 cert_info2_); |
| 293 task_runner_->RunUntilIdle(); |
| 294 } |
| 295 |
| 296 EXPECT_TRUE(CheckLookUpCertificate(cert_info1_, true /* is known */, |
| 297 false /* is currently not provided */, |
| 298 std::string())); |
| 299 |
| 300 EXPECT_TRUE(CheckLookUpCertificate(cert_info2_, true /* is known */, |
| 301 true /* is currently provided */, |
| 302 kExtension2)); |
| 303 |
| 304 // Deregister |kExtension2| as certificate provider and provide |cert_info1_| |
| 305 // from |kExtension1|. |
| 306 test_delegate_->provider_extensions_.erase(kExtension2); |
| 307 |
| 308 { |
| 309 const int cert_request_id = RequestCertificatesFromExtensions(nullptr); |
| 310 SetCertificateProvidedByExtension(kExtension1, cert_request_id, |
| 311 cert_info1_); |
| 312 task_runner_->RunUntilIdle(); |
| 313 } |
| 314 |
| 315 EXPECT_TRUE(CheckLookUpCertificate(cert_info1_, true /* is known */, |
| 316 true /* is currently provided */, |
| 317 kExtension1)); |
| 318 |
| 319 EXPECT_TRUE(CheckLookUpCertificate(cert_info2_, true /* is known */, |
| 320 false /* is currently not provided */, |
| 321 std::string())); |
| 322 |
| 323 // Provide |cert_info2_| from |kExtension1|. |
| 324 { |
| 325 const int cert_request_id = RequestCertificatesFromExtensions(nullptr); |
| 326 SetCertificateProvidedByExtension(kExtension1, cert_request_id, |
| 327 cert_info2_); |
| 328 task_runner_->RunUntilIdle(); |
| 329 } |
| 330 |
| 331 { |
| 332 bool is_currently_provided = true; |
| 333 std::string extension_id; |
| 334 // |cert_info1_.certificate| was provided before, so this must return true. |
| 335 EXPECT_TRUE(service_->LookUpCertificate( |
| 336 *cert_info1_.certificate, &is_currently_provided, &extension_id)); |
| 337 EXPECT_FALSE(is_currently_provided); |
| 338 EXPECT_TRUE(extension_id.empty()); |
| 339 } |
| 340 |
| 341 { |
| 342 bool is_currently_provided = false; |
| 343 std::string extension_id; |
| 344 EXPECT_TRUE(service_->LookUpCertificate( |
| 345 *cert_info2_.certificate, &is_currently_provided, &extension_id)); |
| 346 EXPECT_TRUE(is_currently_provided); |
| 347 EXPECT_EQ(kExtension1, extension_id); |
| 348 } |
| 349 |
| 350 EXPECT_TRUE(CheckLookUpCertificate(cert_info1_, true /* is known */, |
| 351 false /* is currently not provided */, |
| 352 std::string())); |
| 353 |
| 354 EXPECT_TRUE(CheckLookUpCertificate(cert_info2_, true /* is known */, |
| 355 true /* is currently provided */, |
| 356 kExtension1)); |
| 357 } |
| 358 |
242 TEST_F(CertificateProviderServiceTest, GetCertificatesTimeout) { | 359 TEST_F(CertificateProviderServiceTest, GetCertificatesTimeout) { |
243 test_delegate_->provider_extensions_.insert(kExtension2); | 360 test_delegate_->provider_extensions_.insert(kExtension2); |
244 | 361 |
245 net::CertificateList certs; | 362 net::CertificateList certs; |
246 const int cert_request_id = RequestCertificatesFromExtensions(&certs); | 363 const int cert_request_id = RequestCertificatesFromExtensions(&certs); |
247 | 364 |
248 certificate_provider::CertificateInfoList infos; | 365 certificate_provider::CertificateInfoList infos; |
249 SetCertificateProvidedByExtension(kExtension1, cert_request_id, cert_info1_); | 366 SetCertificateProvidedByExtension(kExtension1, cert_request_id, cert_info1_); |
250 | 367 |
251 task_runner_->RunUntilIdle(); | 368 task_runner_->RunUntilIdle(); |
(...skipping 111 matching lines...) Expand 10 before | Expand all | Expand 10 after Loading... |
363 EXPECT_EQ(net::OK, error); | 480 EXPECT_EQ(net::OK, error); |
364 | 481 |
365 // Unload the extension. | 482 // Unload the extension. |
366 service_->OnExtensionUnloaded(kExtension1); | 483 service_->OnExtensionUnloaded(kExtension1); |
367 | 484 |
368 task_runner_->RunUntilIdle(); | 485 task_runner_->RunUntilIdle(); |
369 EXPECT_EQ(net::ERR_FAILED, error); | 486 EXPECT_EQ(net::ERR_FAILED, error); |
370 } | 487 } |
371 | 488 |
372 } // namespace chromeos | 489 } // namespace chromeos |
OLD | NEW |