| OLD | NEW |
| 1 // Copyright 2013 The Chromium Authors. All rights reserved. | 1 // Copyright 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 "chromeos/network/onc/onc_certificate_importer_impl.h" | 5 #include "chromeos/network/onc/onc_certificate_importer_impl.h" |
| 6 | 6 |
| 7 #include <cert.h> | 7 #include <cert.h> |
| 8 #include <certdb.h> | 8 #include <certdb.h> |
| 9 #include <keyhi.h> | 9 #include <keyhi.h> |
| 10 #include <pk11pub.h> | 10 #include <pk11pub.h> |
| 11 #include <string> | 11 #include <string> |
| 12 | 12 |
| 13 #include "base/bind.h" | 13 #include "base/bind.h" |
| 14 #include "base/logging.h" | 14 #include "base/logging.h" |
| 15 #include "base/strings/string_number_conversions.h" | 15 #include "base/strings/string_number_conversions.h" |
| 16 #include "base/test/test_simple_task_runner.h" | 16 #include "base/test/test_simple_task_runner.h" |
| 17 #include "base/thread_task_runner_handle.h" | 17 #include "base/thread_task_runner_handle.h" |
| 18 #include "base/values.h" | 18 #include "base/values.h" |
| 19 #include "chromeos/network/onc/onc_test_utils.h" | 19 #include "chromeos/network/onc/onc_test_utils.h" |
| 20 #include "components/cert_database/public/cert_database_service_io_part.h" |
| 20 #include "components/onc/onc_constants.h" | 21 #include "components/onc/onc_constants.h" |
| 21 #include "crypto/scoped_test_nss_db.h" | 22 #include "crypto/scoped_test_nss_db.h" |
| 22 #include "net/base/crypto_module.h" | 23 #include "net/base/crypto_module.h" |
| 23 #include "net/cert/cert_type.h" | 24 #include "net/cert/cert_type.h" |
| 24 #include "net/cert/nss_cert_database_chromeos.h" | 25 #include "net/cert/nss_cert_database_chromeos.h" |
| 25 #include "net/cert/x509_certificate.h" | 26 #include "net/cert/x509_certificate.h" |
| 26 #include "testing/gtest/include/gtest/gtest.h" | 27 #include "testing/gtest/include/gtest/gtest.h" |
| 27 | 28 |
| 28 namespace chromeos { | 29 namespace chromeos { |
| 29 namespace onc { | 30 namespace onc { |
| (...skipping 23 matching lines...) Expand all Loading... |
| 53 return net::SERVER_CERT; | 54 return net::SERVER_CERT; |
| 54 return net::OTHER_CERT; | 55 return net::OTHER_CERT; |
| 55 } | 56 } |
| 56 #else | 57 #else |
| 57 net::CertType GetCertType(net::X509Certificate::OSCertHandle cert) { | 58 net::CertType GetCertType(net::X509Certificate::OSCertHandle cert) { |
| 58 NOTIMPLEMENTED(); | 59 NOTIMPLEMENTED(); |
| 59 return net::OTHER_CERT; | 60 return net::OTHER_CERT; |
| 60 } | 61 } |
| 61 #endif // USE_NSS | 62 #endif // USE_NSS |
| 62 | 63 |
| 64 class FakeCertDatabaseServiceIOPart : public CertDatabaseServiceIOPart { |
| 65 public: |
| 66 explicit FakeCertDatabaseServiceIOPart(scoped_ptr<net::NSSCertDatabase> db) { |
| 67 DidCreateNSSCertDatabase(db.Pass()); |
| 68 } |
| 69 |
| 70 virtual ~FakeCertDatabaseServiceIOPart() {} |
| 71 }; |
| 72 |
| 63 } // namespace | 73 } // namespace |
| 64 | 74 |
| 65 class ONCCertificateImporterImplTest : public testing::Test { | 75 class ONCCertificateImporterImplTest : public testing::Test { |
| 66 public: | 76 public: |
| 67 ONCCertificateImporterImplTest() {} | 77 ONCCertificateImporterImplTest() {} |
| 68 virtual ~ONCCertificateImporterImplTest() {} | 78 virtual ~ONCCertificateImporterImplTest() {} |
| 69 | 79 |
| 70 virtual void SetUp() override { | 80 virtual void SetUp() override { |
| 71 ASSERT_TRUE(public_nssdb_.is_open()); | 81 ASSERT_TRUE(public_nssdb_.is_open()); |
| 72 ASSERT_TRUE(private_nssdb_.is_open()); | 82 ASSERT_TRUE(private_nssdb_.is_open()); |
| 73 | 83 |
| 74 task_runner_ = new base::TestSimpleTaskRunner(); | 84 task_runner_ = new base::TestSimpleTaskRunner(); |
| 75 thread_task_runner_handle_.reset( | 85 thread_task_runner_handle_.reset( |
| 76 new base::ThreadTaskRunnerHandle(task_runner_)); | 86 new base::ThreadTaskRunnerHandle(task_runner_)); |
| 77 | 87 |
| 78 test_nssdb_.reset(new net::NSSCertDatabaseChromeOS( | 88 scoped_ptr<net::NSSCertDatabase> nss_db(new net::NSSCertDatabaseChromeOS( |
| 79 crypto::ScopedPK11Slot(PK11_ReferenceSlot(public_nssdb_.slot())), | 89 crypto::ScopedPK11Slot(PK11_ReferenceSlot(public_nssdb_.slot())), |
| 80 crypto::ScopedPK11Slot(PK11_ReferenceSlot(private_nssdb_.slot())))); | 90 crypto::ScopedPK11Slot(PK11_ReferenceSlot(private_nssdb_.slot())))); |
| 91 cert_db_io_.reset(new FakeCertDatabaseServiceIOPart(nss_db.Pass())); |
| 81 | 92 |
| 82 // Test db should be empty at start of test. | 93 // Test db should be empty at start of test. |
| 83 EXPECT_TRUE(ListCertsInPublicSlot().empty()); | 94 EXPECT_TRUE(ListCertsInPublicSlot().empty()); |
| 84 EXPECT_TRUE(ListCertsInPrivateSlot().empty()); | 95 EXPECT_TRUE(ListCertsInPrivateSlot().empty()); |
| 85 } | 96 } |
| 86 | 97 |
| 87 virtual void TearDown() override { | 98 virtual void TearDown() override { |
| 88 thread_task_runner_handle_.reset(); | 99 thread_task_runner_handle_.reset(); |
| 89 task_runner_ = NULL; | 100 task_runner_ = NULL; |
| 90 } | 101 } |
| (...skipping 10 matching lines...) Expand all Loading... |
| 101 scoped_ptr<base::DictionaryValue> onc = | 112 scoped_ptr<base::DictionaryValue> onc = |
| 102 test_utils::ReadTestDictionary(filename); | 113 test_utils::ReadTestDictionary(filename); |
| 103 scoped_ptr<base::Value> certificates_value; | 114 scoped_ptr<base::Value> certificates_value; |
| 104 base::ListValue* certificates = NULL; | 115 base::ListValue* certificates = NULL; |
| 105 onc->RemoveWithoutPathExpansion(::onc::toplevel_config::kCertificates, | 116 onc->RemoveWithoutPathExpansion(::onc::toplevel_config::kCertificates, |
| 106 &certificates_value); | 117 &certificates_value); |
| 107 certificates_value.release()->GetAsList(&certificates); | 118 certificates_value.release()->GetAsList(&certificates); |
| 108 onc_certificates_.reset(certificates); | 119 onc_certificates_.reset(certificates); |
| 109 | 120 |
| 110 web_trust_certificates_.clear(); | 121 web_trust_certificates_.clear(); |
| 111 CertificateImporterImpl importer(task_runner_, test_nssdb_.get()); | 122 CertificateImporterImpl importer(task_runner_, cert_db_io_->GetWeakPtr()); |
| 112 importer.ImportCertificates( | 123 importer.ImportCertificates( |
| 113 *certificates, | 124 *certificates, |
| 114 ::onc::ONC_SOURCE_USER_IMPORT, // allow web trust | 125 ::onc::ONC_SOURCE_USER_IMPORT, // allow web trust |
| 115 base::Bind(&ONCCertificateImporterImplTest::OnImportCompleted, | 126 base::Bind(&ONCCertificateImporterImplTest::OnImportCompleted, |
| 116 base::Unretained(this), | 127 base::Unretained(this), |
| 117 expected_success)); | 128 expected_success)); |
| 118 | 129 |
| 119 task_runner_->RunUntilIdle(); | 130 task_runner_->RunUntilIdle(); |
| 120 | 131 |
| 121 public_list_ = ListCertsInPublicSlot(); | 132 public_list_ = ListCertsInPublicSlot(); |
| (...skipping 24 matching lines...) Expand all Loading... |
| 146 certificate->GetStringWithoutPathExpansion(::onc::certificate::kGUID, guid); | 157 certificate->GetStringWithoutPathExpansion(::onc::certificate::kGUID, guid); |
| 147 } | 158 } |
| 148 | 159 |
| 149 // Certificates and the NSSCertDatabase depend on these test DBs. Destroy them | 160 // Certificates and the NSSCertDatabase depend on these test DBs. Destroy them |
| 150 // last. | 161 // last. |
| 151 crypto::ScopedTestNSSDB public_nssdb_; | 162 crypto::ScopedTestNSSDB public_nssdb_; |
| 152 crypto::ScopedTestNSSDB private_nssdb_; | 163 crypto::ScopedTestNSSDB private_nssdb_; |
| 153 | 164 |
| 154 scoped_refptr<base::TestSimpleTaskRunner> task_runner_; | 165 scoped_refptr<base::TestSimpleTaskRunner> task_runner_; |
| 155 scoped_ptr<base::ThreadTaskRunnerHandle> thread_task_runner_handle_; | 166 scoped_ptr<base::ThreadTaskRunnerHandle> thread_task_runner_handle_; |
| 156 scoped_ptr<net::NSSCertDatabaseChromeOS> test_nssdb_; | 167 scoped_ptr<FakeCertDatabaseServiceIOPart> cert_db_io_; |
| 157 scoped_ptr<base::ListValue> onc_certificates_; | 168 scoped_ptr<base::ListValue> onc_certificates_; |
| 158 // List of certs in the nssdb's public slot. | 169 // List of certs in the nssdb's public slot. |
| 159 net::CertificateList public_list_; | 170 net::CertificateList public_list_; |
| 160 // List of certs in the nssdb's "private" slot. | 171 // List of certs in the nssdb's "private" slot. |
| 161 net::CertificateList private_list_; | 172 net::CertificateList private_list_; |
| 162 net::CertificateList web_trust_certificates_; | 173 net::CertificateList web_trust_certificates_; |
| 163 | 174 |
| 164 private: | 175 private: |
| 165 net::CertificateList ListCertsInPublicSlot() { | 176 net::CertificateList ListCertsInPublicSlot() { |
| 166 return ListCertsInSlot(public_nssdb_.slot()); | 177 return ListCertsInSlot(public_nssdb_.slot()); |
| (...skipping 174 matching lines...) Expand 10 before | Expand all | Expand 10 after Loading... |
| 341 "certificate-client-update.onc"), | 352 "certificate-client-update.onc"), |
| 342 CertParam(net::SERVER_CERT, | 353 CertParam(net::SERVER_CERT, |
| 343 "certificate-server.onc", | 354 "certificate-server.onc", |
| 344 "certificate-server-update.onc"), | 355 "certificate-server-update.onc"), |
| 345 CertParam(net::CA_CERT, | 356 CertParam(net::CA_CERT, |
| 346 "certificate-web-authority.onc", | 357 "certificate-web-authority.onc", |
| 347 "certificate-web-authority-update.onc"))); | 358 "certificate-web-authority-update.onc"))); |
| 348 | 359 |
| 349 } // namespace onc | 360 } // namespace onc |
| 350 } // namespace chromeos | 361 } // namespace chromeos |
| OLD | NEW |