Chromium Code Reviews| OLD | NEW |
|---|---|
| (Empty) | |
| 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 | |
| 3 // found in the LICENSE file. | |
| 4 | |
| 5 #include "net/cert/nss_cert_database_chromeos.h" | |
| 6 | |
| 7 #include "base/bind.h" | |
| 8 #include "base/callback.h" | |
| 9 #include "base/run_loop.h" | |
| 10 #include "crypto/nss_util.h" | |
| 11 #include "crypto/nss_util_internal.h" | |
| 12 #include "net/base/test_data_directory.h" | |
| 13 #include "net/cert/cert_database.h" | |
| 14 #include "net/test/cert_test_util.h" | |
| 15 #include "testing/gtest/include/gtest/gtest.h" | |
| 16 | |
| 17 namespace net { | |
| 18 | |
| 19 namespace { | |
| 20 | |
| 21 bool IsCertInCertificateList(const X509Certificate* cert, | |
| 22 const CertificateList& cert_list) { | |
| 23 for (CertificateList::const_iterator it = cert_list.begin(); | |
| 24 it != cert_list.end(); | |
| 25 ++it) { | |
| 26 if ((*it)->os_cert_handle() == cert->os_cert_handle()) | |
|
Ryan Sleevi
2013/12/18 21:28:32
BUG? Are you sure comparing OS cert handle equalit
mattm
2013/12/19 22:35:00
Done.
| |
| 27 return true; | |
| 28 } | |
| 29 return false; | |
| 30 } | |
| 31 | |
| 32 } // namespace | |
| 33 | |
| 34 class NSSCertDatabaseChromeOSTest : public testing::Test, | |
| 35 public CertDatabase::Observer { | |
| 36 public: | |
| 37 NSSCertDatabaseChromeOSTest() : user_1_("user1"), user_2_("user2") {} | |
| 38 | |
| 39 virtual void SetUp() OVERRIDE { | |
| 40 ASSERT_TRUE(user_1_.constructed_successfully()); | |
| 41 ASSERT_TRUE(user_2_.constructed_successfully()); | |
| 42 user_1_.FinishInit(); | |
| 43 user_2_.FinishInit(); | |
| 44 db_1_.reset(new NSSCertDatabaseChromeOS( | |
| 45 crypto::GetPublicSlotForChromeOSUser(user_1_.username_hash()), | |
| 46 crypto::GetPrivateSlotForChromeOSUser( | |
| 47 user_1_.username_hash(), | |
| 48 base::Callback<void(crypto::ScopedPK11Slot)>()))); | |
| 49 db_2_.reset(new NSSCertDatabaseChromeOS( | |
| 50 crypto::GetPublicSlotForChromeOSUser(user_2_.username_hash()), | |
| 51 crypto::GetPrivateSlotForChromeOSUser( | |
| 52 user_2_.username_hash(), | |
| 53 base::Callback<void(crypto::ScopedPK11Slot)>()))); | |
| 54 CertDatabase::GetInstance()->AddObserver(this); | |
| 55 } | |
| 56 | |
| 57 virtual void TearDown() OVERRIDE { | |
| 58 CertDatabase::GetInstance()->RemoveObserver(this); | |
|
Ryan Sleevi
2013/12/18 21:28:32
If either user_1_.constructed_successfully() or us
mattm
2013/12/19 22:35:00
The observer list seems to be fine with trying to
| |
| 59 } | |
| 60 | |
| 61 // CertDatabase::Observer: | |
| 62 virtual void OnCertAdded(const X509Certificate* cert) OVERRIDE { | |
| 63 added_.push_back(cert ? cert->os_cert_handle() : NULL); | |
| 64 } | |
| 65 | |
| 66 virtual void OnCertRemoved(const X509Certificate* cert) OVERRIDE {} | |
| 67 | |
| 68 virtual void OnCACertChanged(const X509Certificate* cert) OVERRIDE { | |
| 69 added_ca_.push_back(cert ? cert->os_cert_handle() : NULL); | |
| 70 } | |
| 71 | |
| 72 protected: | |
| 73 std::vector<CERTCertificate*> added_ca_; | |
| 74 std::vector<CERTCertificate*> added_; | |
|
Ryan Sleevi
2013/12/18 21:28:32
comments for |added_| and |added_ca_|
mattm
2013/12/19 22:35:00
Done.
| |
| 75 crypto::ScopedTestNSSChromeOSUser user_1_; | |
| 76 crypto::ScopedTestNSSChromeOSUser user_2_; | |
| 77 scoped_ptr<NSSCertDatabaseChromeOS> db_1_; | |
| 78 scoped_ptr<NSSCertDatabaseChromeOS> db_2_; | |
| 79 }; | |
| 80 | |
| 81 TEST_F(NSSCertDatabaseChromeOSTest, ListModules) { | |
|
Ryan Sleevi
2013/12/18 21:28:32
Please add a comment before each test describing w
mattm
2013/12/19 22:35:00
Done.
| |
| 82 CryptoModuleList modules_1; | |
| 83 CryptoModuleList modules_2; | |
| 84 | |
| 85 db_1_->ListModules(&modules_1, false /* need_rw */); | |
| 86 db_2_->ListModules(&modules_2, false /* need_rw */); | |
| 87 | |
| 88 bool found_1 = false; | |
| 89 for (CryptoModuleList::iterator it = modules_1.begin(); it != modules_1.end(); | |
| 90 ++it) { | |
| 91 EXPECT_NE(db_2_->GetPublicSlot().get(), (*it)->os_module_handle()); | |
| 92 if ((*it)->os_module_handle() == db_1_->GetPublicSlot().get()) | |
| 93 found_1 = true; | |
| 94 } | |
| 95 EXPECT_TRUE(found_1); | |
| 96 | |
| 97 bool found_2 = false; | |
| 98 for (CryptoModuleList::iterator it = modules_2.begin(); it != modules_2.end(); | |
| 99 ++it) { | |
| 100 EXPECT_NE(db_1_->GetPublicSlot().get(), (*it)->os_module_handle()); | |
| 101 if ((*it)->os_module_handle() == db_2_->GetPublicSlot().get()) | |
| 102 found_2 = true; | |
| 103 } | |
| 104 EXPECT_TRUE(found_2); | |
| 105 } | |
| 106 | |
| 107 TEST_F(NSSCertDatabaseChromeOSTest, ImportCACerts) { | |
| 108 CertificateList certs_1 = | |
| 109 CreateCertificateListFromFile(GetTestCertsDirectory(), | |
| 110 "root_ca_cert.pem", | |
| 111 X509Certificate::FORMAT_AUTO); | |
| 112 ASSERT_EQ(1U, certs_1.size()); | |
| 113 EXPECT_FALSE(certs_1[0]->os_cert_handle()->isperm); | |
| 114 | |
| 115 CertificateList certs_2 = | |
| 116 CreateCertificateListFromFile(GetTestCertsDirectory(), | |
| 117 "2048-rsa-root.pem", | |
| 118 X509Certificate::FORMAT_AUTO); | |
| 119 ASSERT_EQ(1U, certs_2.size()); | |
| 120 EXPECT_FALSE(certs_2[0]->os_cert_handle()->isperm); | |
|
Ryan Sleevi
2013/12/18 21:28:32
I don't understand why you're doing this check. Sa
mattm
2013/12/19 22:35:00
Yeah, it's not necessary here. copy-pasta from the
| |
| 121 | |
| 122 NSSCertDatabase::ImportCertFailureList failed; | |
| 123 EXPECT_TRUE( | |
| 124 db_1_->ImportCACerts(certs_1, NSSCertDatabase::TRUSTED_SSL, &failed)); | |
| 125 EXPECT_EQ(0U, failed.size()); | |
| 126 failed.clear(); | |
| 127 EXPECT_TRUE( | |
| 128 db_2_->ImportCACerts(certs_2, NSSCertDatabase::TRUSTED_SSL, &failed)); | |
| 129 EXPECT_EQ(0U, failed.size()); | |
| 130 | |
| 131 CertificateList user_1_certlist; | |
| 132 CertificateList user_2_certlist; | |
| 133 db_1_->ListCerts(&user_1_certlist); | |
| 134 db_2_->ListCerts(&user_2_certlist); | |
| 135 | |
| 136 EXPECT_TRUE(IsCertInCertificateList(certs_1[0], user_1_certlist)); | |
| 137 EXPECT_FALSE(IsCertInCertificateList(certs_1[0], user_2_certlist)); | |
| 138 | |
| 139 EXPECT_TRUE(IsCertInCertificateList(certs_2[0], user_2_certlist)); | |
| 140 EXPECT_FALSE(IsCertInCertificateList(certs_2[0], user_1_certlist)); | |
| 141 | |
| 142 base::RunLoop().RunUntilIdle(); | |
| 143 ASSERT_EQ(2U, added_ca_.size()); | |
| 144 // TODO(mattm): make NSSCertDatabase actually pass the cert to the callback, | |
| 145 // and enable these checks: | |
| 146 // EXPECT_EQ(certs_1[0]->os_cert_handle(), added_ca_[0]); | |
| 147 // EXPECT_EQ(certs_2[0]->os_cert_handle(), added_ca_[1]); | |
| 148 EXPECT_EQ(0U, added_.size()); | |
| 149 } | |
| 150 | |
| 151 TEST_F(NSSCertDatabaseChromeOSTest, ImportServerCert) { | |
| 152 CertificateList certs_1 = CreateCertificateListFromFile( | |
| 153 GetTestCertsDirectory(), "ok_cert.pem", X509Certificate::FORMAT_AUTO); | |
| 154 ASSERT_EQ(1U, certs_1.size()); | |
| 155 EXPECT_FALSE(certs_1[0]->os_cert_handle()->isperm); | |
| 156 | |
| 157 CertificateList certs_2 = | |
| 158 CreateCertificateListFromFile(GetTestCertsDirectory(), | |
| 159 "2048-rsa-ee-by-2048-rsa-intermediate.pem", | |
| 160 X509Certificate::FORMAT_AUTO); | |
| 161 ASSERT_EQ(1U, certs_2.size()); | |
| 162 EXPECT_FALSE(certs_2[0]->os_cert_handle()->isperm); | |
| 163 | |
| 164 NSSCertDatabase::ImportCertFailureList failed; | |
| 165 EXPECT_TRUE( | |
| 166 db_1_->ImportServerCert(certs_1, NSSCertDatabase::TRUSTED_SSL, &failed)); | |
| 167 EXPECT_EQ(0U, failed.size()); | |
| 168 failed.clear(); | |
| 169 EXPECT_TRUE( | |
| 170 db_2_->ImportServerCert(certs_2, NSSCertDatabase::TRUSTED_SSL, &failed)); | |
| 171 EXPECT_EQ(0U, failed.size()); | |
| 172 | |
| 173 CertificateList user_1_certlist; | |
| 174 CertificateList user_2_certlist; | |
| 175 db_1_->ListCerts(&user_1_certlist); | |
| 176 db_2_->ListCerts(&user_2_certlist); | |
| 177 | |
| 178 EXPECT_TRUE(IsCertInCertificateList(certs_1[0], user_1_certlist)); | |
| 179 EXPECT_FALSE(IsCertInCertificateList(certs_1[0], user_2_certlist)); | |
| 180 | |
| 181 EXPECT_TRUE(IsCertInCertificateList(certs_2[0], user_2_certlist)); | |
| 182 EXPECT_FALSE(IsCertInCertificateList(certs_2[0], user_1_certlist)); | |
| 183 | |
| 184 base::RunLoop().RunUntilIdle(); | |
| 185 // TODO(mattm): ImportServerCert doesn't actually cause any observers to | |
| 186 // fire. Is that correct? | |
| 187 EXPECT_EQ(0U, added_ca_.size()); | |
| 188 EXPECT_EQ(0U, added_.size()); | |
| 189 } | |
| 190 | |
| 191 } // namespace net | |
| OLD | NEW |