| OLD | NEW |
| 1 // Copyright (c) 2012 The Chromium Authors. All rights reserved. | 1 // Copyright (c) 2012 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 "net/cert/nss_cert_database.h" | 5 #include "net/cert/nss_cert_database.h" |
| 6 | 6 |
| 7 #include <cert.h> | 7 #include <cert.h> |
| 8 #include <certdb.h> | 8 #include <certdb.h> |
| 9 #include <pk11pub.h> | 9 #include <pk11pub.h> |
| 10 | 10 |
| 11 #include <algorithm> | 11 #include <algorithm> |
| 12 #include <memory> | 12 #include <memory> |
| 13 | 13 |
| 14 #include "base/bind.h" | 14 #include "base/bind.h" |
| 15 #include "base/files/file_path.h" | 15 #include "base/files/file_path.h" |
| 16 #include "base/files/file_util.h" | 16 #include "base/files/file_util.h" |
| 17 #include "base/lazy_instance.h" | 17 #include "base/lazy_instance.h" |
| 18 #include "base/message_loop/message_loop.h" |
| 18 #include "base/run_loop.h" | 19 #include "base/run_loop.h" |
| 19 #include "base/strings/string16.h" | 20 #include "base/strings/string16.h" |
| 20 #include "base/strings/string_util.h" | 21 #include "base/strings/string_util.h" |
| 21 #include "base/strings/utf_string_conversions.h" | 22 #include "base/strings/utf_string_conversions.h" |
| 23 #include "base/test/scoped_task_scheduler.h" |
| 22 #include "base/threading/thread_task_runner_handle.h" | 24 #include "base/threading/thread_task_runner_handle.h" |
| 23 #include "crypto/scoped_nss_types.h" | 25 #include "crypto/scoped_nss_types.h" |
| 24 #include "crypto/scoped_test_nss_db.h" | 26 #include "crypto/scoped_test_nss_db.h" |
| 25 #include "net/base/crypto_module.h" | 27 #include "net/base/crypto_module.h" |
| 26 #include "net/base/hash_value.h" | 28 #include "net/base/hash_value.h" |
| 27 #include "net/base/net_errors.h" | 29 #include "net/base/net_errors.h" |
| 28 #include "net/cert/cert_status_flags.h" | 30 #include "net/cert/cert_status_flags.h" |
| 29 #include "net/cert/cert_verify_proc_nss.h" | 31 #include "net/cert/cert_verify_proc_nss.h" |
| 30 #include "net/cert/cert_verify_result.h" | 32 #include "net/cert/cert_verify_result.h" |
| 31 #include "net/cert/x509_certificate.h" | 33 #include "net/cert/x509_certificate.h" |
| (...skipping 22 matching lines...) Expand all Loading... |
| 54 void SwapCertList(CertificateList* destination, | 56 void SwapCertList(CertificateList* destination, |
| 55 std::unique_ptr<CertificateList> source) { | 57 std::unique_ptr<CertificateList> source) { |
| 56 ASSERT_TRUE(destination); | 58 ASSERT_TRUE(destination); |
| 57 destination->swap(*source); | 59 destination->swap(*source); |
| 58 } | 60 } |
| 59 | 61 |
| 60 } // namespace | 62 } // namespace |
| 61 | 63 |
| 62 class CertDatabaseNSSTest : public testing::Test { | 64 class CertDatabaseNSSTest : public testing::Test { |
| 63 public: | 65 public: |
| 66 CertDatabaseNSSTest() |
| 67 : scoped_task_scheduler_(base::MessageLoop::current()) {} |
| 68 |
| 64 void SetUp() override { | 69 void SetUp() override { |
| 65 ASSERT_TRUE(test_nssdb_.is_open()); | 70 ASSERT_TRUE(test_nssdb_.is_open()); |
| 66 cert_db_.reset(new NSSCertDatabase( | 71 cert_db_.reset(new NSSCertDatabase( |
| 67 crypto::ScopedPK11Slot( | 72 crypto::ScopedPK11Slot( |
| 68 PK11_ReferenceSlot(test_nssdb_.slot())) /* public slot */, | 73 PK11_ReferenceSlot(test_nssdb_.slot())) /* public slot */, |
| 69 crypto::ScopedPK11Slot( | 74 crypto::ScopedPK11Slot( |
| 70 PK11_ReferenceSlot(test_nssdb_.slot())) /* private slot */)); | 75 PK11_ReferenceSlot(test_nssdb_.slot())) /* private slot */)); |
| 71 public_module_ = cert_db_->GetPublicModule(); | 76 public_module_ = cert_db_->GetPublicModule(); |
| 72 | 77 |
| 73 // Test db should be empty at start of test. | 78 // Test db should be empty at start of test. |
| (...skipping 44 matching lines...) Expand 10 before | Expand all | Expand 10 after Loading... |
| 118 result.begin(), result.end(), | 123 result.begin(), result.end(), |
| 119 [](const scoped_refptr<X509Certificate>& lhs, | 124 [](const scoped_refptr<X509Certificate>& lhs, |
| 120 const scoped_refptr<X509Certificate>& rhs) { | 125 const scoped_refptr<X509Certificate>& rhs) { |
| 121 return SHA256HashValueLessThan()( | 126 return SHA256HashValueLessThan()( |
| 122 X509Certificate::CalculateFingerprint256(lhs->os_cert_handle()), | 127 X509Certificate::CalculateFingerprint256(lhs->os_cert_handle()), |
| 123 X509Certificate::CalculateFingerprint256(rhs->os_cert_handle())); | 128 X509Certificate::CalculateFingerprint256(rhs->os_cert_handle())); |
| 124 }); | 129 }); |
| 125 return result; | 130 return result; |
| 126 } | 131 } |
| 127 | 132 |
| 133 base::test::ScopedTaskScheduler scoped_task_scheduler_; |
| 128 std::unique_ptr<NSSCertDatabase> cert_db_; | 134 std::unique_ptr<NSSCertDatabase> cert_db_; |
| 129 const CertificateList empty_cert_list_; | 135 const CertificateList empty_cert_list_; |
| 130 crypto::ScopedTestNSSDB test_nssdb_; | 136 crypto::ScopedTestNSSDB test_nssdb_; |
| 131 scoped_refptr<CryptoModule> public_module_; | 137 scoped_refptr<CryptoModule> public_module_; |
| 132 }; | 138 }; |
| 133 | 139 |
| 134 TEST_F(CertDatabaseNSSTest, ListCertsSync) { | 140 TEST_F(CertDatabaseNSSTest, ListCertsSync) { |
| 135 // This test isn't terribly useful, though it will at least let valgrind test | 141 // This test isn't terribly useful, though it will at least let valgrind test |
| 136 // for leaks. | 142 // for leaks. |
| 137 CertificateList certs; | 143 CertificateList certs; |
| 138 cert_db_->ListCertsSync(&certs); | 144 cert_db_->ListCertsSync(&certs); |
| 139 // The test DB is empty, but let's assume there will always be something in | 145 // The test DB is empty, but let's assume there will always be something in |
| 140 // the other slots. | 146 // the other slots. |
| 141 EXPECT_LT(0U, certs.size()); | 147 EXPECT_LT(0U, certs.size()); |
| 142 } | 148 } |
| 143 | 149 |
| 144 TEST_F(CertDatabaseNSSTest, ListCerts) { | 150 TEST_F(CertDatabaseNSSTest, ListCerts) { |
| 145 // This test isn't terribly useful, though it will at least let valgrind test | 151 // This test isn't terribly useful, though it will at least let valgrind test |
| 146 // for leaks. | 152 // for leaks. |
| 147 CertificateList certs; | 153 CertificateList certs; |
| 148 cert_db_->SetSlowTaskRunnerForTest(base::ThreadTaskRunnerHandle::Get()); | |
| 149 cert_db_->ListCerts(base::Bind(&SwapCertList, base::Unretained(&certs))); | 154 cert_db_->ListCerts(base::Bind(&SwapCertList, base::Unretained(&certs))); |
| 150 EXPECT_EQ(0U, certs.size()); | 155 EXPECT_EQ(0U, certs.size()); |
| 151 | 156 |
| 152 base::RunLoop().RunUntilIdle(); | 157 base::RunLoop().RunUntilIdle(); |
| 153 | 158 |
| 154 // The test DB is empty, but let's assume there will always be something in | 159 // The test DB is empty, but let's assume there will always be something in |
| 155 // the other slots. | 160 // the other slots. |
| 156 EXPECT_LT(0U, certs.size()); | 161 EXPECT_LT(0U, certs.size()); |
| 157 } | 162 } |
| 158 | 163 |
| (...skipping 851 matching lines...) Expand 10 before | Expand all | Expand 10 after Loading... |
| 1010 EXPECT_EQ(NSSCertDatabase::TRUST_DEFAULT, | 1015 EXPECT_EQ(NSSCertDatabase::TRUST_DEFAULT, |
| 1011 cert_db_->GetCertTrust(certs2[0].get(), SERVER_CERT)); | 1016 cert_db_->GetCertTrust(certs2[0].get(), SERVER_CERT)); |
| 1012 | 1017 |
| 1013 new_certs = ListCerts(); | 1018 new_certs = ListCerts(); |
| 1014 ASSERT_EQ(2U, new_certs.size()); | 1019 ASSERT_EQ(2U, new_certs.size()); |
| 1015 EXPECT_STRNE(new_certs[0]->os_cert_handle()->nickname, | 1020 EXPECT_STRNE(new_certs[0]->os_cert_handle()->nickname, |
| 1016 new_certs[1]->os_cert_handle()->nickname); | 1021 new_certs[1]->os_cert_handle()->nickname); |
| 1017 } | 1022 } |
| 1018 | 1023 |
| 1019 } // namespace net | 1024 } // namespace net |
| OLD | NEW |