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

Side by Side Diff: chromeos/cert_loader_unittest.cc

Issue 2828713002: Enable client certificate patterns in device ONC policy (Closed)
Patch Set: Clean up. Created 3 years, 8 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
OLDNEW
1 // Copyright 2014 The Chromium Authors. All rights reserved. 1 // Copyright 2014 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/cert_loader.h" 5 #include "chromeos/cert_loader.h"
6 6
7 #include <stddef.h> 7 #include <stddef.h>
8 8
9 #include <memory> 9 #include <memory>
10 #include <utility> 10 #include <utility>
(...skipping 27 matching lines...) Expand all
38 } 38 }
39 39
40 class TestNSSCertDatabase : public net::NSSCertDatabaseChromeOS { 40 class TestNSSCertDatabase : public net::NSSCertDatabaseChromeOS {
41 public: 41 public:
42 TestNSSCertDatabase(crypto::ScopedPK11Slot public_slot, 42 TestNSSCertDatabase(crypto::ScopedPK11Slot public_slot,
43 crypto::ScopedPK11Slot private_slot) 43 crypto::ScopedPK11Slot private_slot)
44 : NSSCertDatabaseChromeOS(std::move(public_slot), 44 : NSSCertDatabaseChromeOS(std::move(public_slot),
45 std::move(private_slot)) {} 45 std::move(private_slot)) {}
46 ~TestNSSCertDatabase() override {} 46 ~TestNSSCertDatabase() override {}
47 47
48 void NotifyOfCertAdded(const net::X509Certificate* cert) { 48 // Make this method visible in the public interface.
49 void NotifyObserversCertDBChanged() {
49 NSSCertDatabaseChromeOS::NotifyObserversCertDBChanged(); 50 NSSCertDatabaseChromeOS::NotifyObserversCertDBChanged();
50 } 51 }
51 }; 52 };
52 53
53 class CertLoaderTest : public testing::Test, 54 class CertLoaderTest : public testing::Test,
54 public CertLoader::Observer { 55 public CertLoader::Observer {
55 public: 56 public:
56 CertLoaderTest() 57 CertLoaderTest()
57 : cert_loader_(nullptr), certificates_loaded_events_count_(0U) {} 58 : cert_loader_(nullptr), certificates_loaded_events_count_(0U) {}
58 59
59 ~CertLoaderTest() override {} 60 ~CertLoaderTest() override {}
60 61
61 void SetUp() override { 62 void SetUp() override {
62 ASSERT_TRUE(primary_db_.is_open()); 63 ASSERT_TRUE(primary_db_.is_open());
63 64
64 CertLoader::Initialize(); 65 CertLoader::Initialize();
65 cert_loader_ = CertLoader::Get(); 66 cert_loader_ = CertLoader::Get();
67 cert_loader_->SetSlowTaskRunnerForTest(message_loop_.task_runner());
66 cert_loader_->AddObserver(this); 68 cert_loader_->AddObserver(this);
67 } 69 }
68 70
69 void TearDown() override { 71 void TearDown() override {
70 cert_loader_->RemoveObserver(this); 72 cert_loader_->RemoveObserver(this);
71 CertLoader::Shutdown(); 73 CertLoader::Shutdown();
72 } 74 }
73 75
74 protected: 76 protected:
75 void StartCertLoaderWithPrimaryDB() { 77 void StartCertLoaderWithPrimaryDB() {
76 CreateCertDatabase(&primary_db_, &primary_certdb_); 78 CreateCertDatabase(&primary_db_, &primary_certdb_);
77 cert_loader_->StartWithNSSDB(primary_certdb_.get()); 79 cert_loader_->StartWithNSSDB(primary_certdb_.get());
78 80
79 base::RunLoop().RunUntilIdle(); 81 base::RunLoop().RunUntilIdle();
80 GetAndResetCertificatesLoadedEventsCount(); 82 GetAndResetCertificatesLoadedEventsCount();
81 } 83 }
82 84
85 // Starts the cert loader with a primary cert database which has access to the
86 // system token.
87 void StartCertLoaderWithPrimaryDBAndSystemToken() {
88 CreateCertDatabase(&primary_db_, &primary_certdb_);
89 AddSystemToken(primary_certdb_.get());
90 cert_loader_->StartWithNSSDB(primary_certdb_.get());
91
92 base::RunLoop().RunUntilIdle();
93 GetAndResetCertificatesLoadedEventsCount();
94 }
95
83 // CertLoader::Observer: 96 // CertLoader::Observer:
84 // The test keeps count of times the observer method was called. 97 // The test keeps count of times the observer method was called.
85 void OnCertificatesLoaded(const net::CertificateList& cert_list, 98 void OnCertificatesLoaded(const net::CertificateList& cert_list,
86 bool initial_load) override { 99 bool initial_load) override {
87 EXPECT_TRUE(certificates_loaded_events_count_ == 0 || !initial_load); 100 EXPECT_TRUE(certificates_loaded_events_count_ == 0 || !initial_load);
88 certificates_loaded_events_count_++; 101 certificates_loaded_events_count_++;
89 } 102 }
90 103
91 // Returns the number of |OnCertificatesLoaded| calls observed since the 104 // Returns the number of |OnCertificatesLoaded| calls observed since the
92 // last call to this method equals |value|. 105 // last call to this method equals |value|.
(...skipping 25 matching lines...) Expand all
118 net::X509Certificate::FORMAT_AUTO); 131 net::X509Certificate::FORMAT_AUTO);
119 ASSERT_EQ(1U, imported_certs->size()); 132 ASSERT_EQ(1U, imported_certs->size());
120 133
121 net::NSSCertDatabase::ImportCertFailureList failed; 134 net::NSSCertDatabase::ImportCertFailureList failed;
122 ASSERT_TRUE(database->ImportCACerts(*imported_certs, 135 ASSERT_TRUE(database->ImportCACerts(*imported_certs,
123 net::NSSCertDatabase::TRUST_DEFAULT, 136 net::NSSCertDatabase::TRUST_DEFAULT,
124 &failed)); 137 &failed));
125 ASSERT_TRUE(failed.empty()); 138 ASSERT_TRUE(failed.empty());
126 } 139 }
127 140
141 // Import a client cert and key into a PKCS11 slot. Then notify
142 // |database_to_notify| (which is presumably using that slot) that new
143 // certificates are available.
128 scoped_refptr<net::X509Certificate> ImportClientCertAndKey( 144 scoped_refptr<net::X509Certificate> ImportClientCertAndKey(
129 TestNSSCertDatabase* database) { 145 TestNSSCertDatabase* database_to_notify,
146 PK11SlotInfo* slot_to_use) {
130 // Import a client cert signed by that CA. 147 // Import a client cert signed by that CA.
131 scoped_refptr<net::X509Certificate> client_cert( 148 scoped_refptr<net::X509Certificate> client_cert(
132 net::ImportClientCertAndKeyFromFile(net::GetTestCertsDirectory(), 149 net::ImportClientCertAndKeyFromFile(net::GetTestCertsDirectory(),
133 "client_1.pem", "client_1.pk8", 150 "client_1.pem", "client_1.pk8",
134 database->GetPrivateSlot().get())); 151 slot_to_use));
135 database->NotifyOfCertAdded(client_cert.get()); 152 database_to_notify->NotifyObserversCertDBChanged();
136 return client_cert; 153 return client_cert;
137 } 154 }
138 155
156 // Import a client cert into |database|'s private slot.
157 scoped_refptr<net::X509Certificate> ImportClientCertAndKey(
158 TestNSSCertDatabase* database) {
159 return ImportClientCertAndKey(database, database->GetPrivateSlot().get());
160 }
161
139 CertLoader* cert_loader_; 162 CertLoader* cert_loader_;
140 163
141 // The user is primary as the one whose certificates CertLoader handles, it 164 // The user is primary as the one whose certificates CertLoader handles, it
142 // has nothing to do with crypto::InitializeNSSForChromeOSUser is_primary_user 165 // has nothing to do with crypto::InitializeNSSForChromeOSUser is_primary_user
143 // parameter (which is irrelevant for these tests). 166 // parameter (which is irrelevant for these tests).
144 crypto::ScopedTestNSSDB primary_db_; 167 crypto::ScopedTestNSSDB primary_db_;
145 std::unique_ptr<TestNSSCertDatabase> primary_certdb_; 168 std::unique_ptr<TestNSSCertDatabase> primary_certdb_;
146 169
170 // Additional NSS DB simulating the system token.
171 crypto::ScopedTestNSSDB system_db_;
172
147 base::MessageLoop message_loop_; 173 base::MessageLoop message_loop_;
148 174
149 private: 175 private:
176 // Adds the PKCS11 slot from |system_db_| to |certdb| as system slot.
177 void AddSystemToken(TestNSSCertDatabase* certdb) {
178 ASSERT_TRUE(system_db_.is_open());
179 certdb->SetSystemSlot(
180 crypto::ScopedPK11Slot(PK11_ReferenceSlot(system_db_.slot())));
181 }
182
150 size_t certificates_loaded_events_count_; 183 size_t certificates_loaded_events_count_;
151 }; 184 };
152 185
153 } // namespace 186 } // namespace
154 187
155 TEST_F(CertLoaderTest, Basic) { 188 TEST_F(CertLoaderTest, Basic) {
156 EXPECT_FALSE(cert_loader_->CertificatesLoading()); 189 EXPECT_FALSE(cert_loader_->CertificatesLoading());
157 EXPECT_FALSE(cert_loader_->certificates_loaded()); 190 EXPECT_FALSE(cert_loader_->certificates_loaded());
158 191
159 CreateCertDatabase(&primary_db_, &primary_certdb_); 192 CreateCertDatabase(&primary_db_, &primary_certdb_);
(...skipping 58 matching lines...) Expand 10 before | Expand all | Expand 10 after
218 scoped_refptr<net::X509Certificate> cert( 251 scoped_refptr<net::X509Certificate> cert(
219 ImportClientCertAndKey(primary_certdb_.get())); 252 ImportClientCertAndKey(primary_certdb_.get()));
220 253
221 ASSERT_EQ(0U, GetAndResetCertificatesLoadedEventsCount()); 254 ASSERT_EQ(0U, GetAndResetCertificatesLoadedEventsCount());
222 base::RunLoop().RunUntilIdle(); 255 base::RunLoop().RunUntilIdle();
223 EXPECT_EQ(1U, GetAndResetCertificatesLoadedEventsCount()); 256 EXPECT_EQ(1U, GetAndResetCertificatesLoadedEventsCount());
224 257
225 EXPECT_TRUE(IsCertInCertificateList(cert.get(), cert_loader_->cert_list())); 258 EXPECT_TRUE(IsCertInCertificateList(cert.get(), cert_loader_->cert_list()));
226 } 259 }
227 260
261 TEST_F(CertLoaderTest, ClientLoaderUpdateOnNewClientCertInSystemToken) {
262 StartCertLoaderWithPrimaryDBAndSystemToken();
263
264 EXPECT_TRUE(cert_loader_->system_cert_list().empty());
265 scoped_refptr<net::X509Certificate> cert(ImportClientCertAndKey(
266 primary_certdb_.get(), primary_certdb_->GetSystemSlot().get()));
267
268 ASSERT_EQ(0U, GetAndResetCertificatesLoadedEventsCount());
269 base::RunLoop().RunUntilIdle();
270 EXPECT_EQ(1U, GetAndResetCertificatesLoadedEventsCount());
271
272 EXPECT_TRUE(IsCertInCertificateList(cert.get(), cert_loader_->cert_list()));
273 EXPECT_EQ(1U, cert_loader_->system_cert_list().size());
274 EXPECT_TRUE(
275 IsCertInCertificateList(cert.get(), cert_loader_->system_cert_list()));
276 }
277
228 TEST_F(CertLoaderTest, CertLoaderNoUpdateOnNewClientCertInSecondaryDb) { 278 TEST_F(CertLoaderTest, CertLoaderNoUpdateOnNewClientCertInSecondaryDb) {
229 crypto::ScopedTestNSSDB secondary_db; 279 crypto::ScopedTestNSSDB secondary_db;
230 std::unique_ptr<TestNSSCertDatabase> secondary_certdb; 280 std::unique_ptr<TestNSSCertDatabase> secondary_certdb;
231 281
232 StartCertLoaderWithPrimaryDB(); 282 StartCertLoaderWithPrimaryDB();
233 CreateCertDatabase(&secondary_db, &secondary_certdb); 283 CreateCertDatabase(&secondary_db, &secondary_certdb);
234 284
235 scoped_refptr<net::X509Certificate> cert( 285 scoped_refptr<net::X509Certificate> cert(
236 ImportClientCertAndKey(secondary_certdb.get())); 286 ImportClientCertAndKey(secondary_certdb.get()));
237 287
(...skipping 39 matching lines...) Expand 10 before | Expand all | Expand 10 after
277 ASSERT_TRUE(primary_certdb_->SetCertTrust(certs[0].get(), net::CA_CERT, 327 ASSERT_TRUE(primary_certdb_->SetCertTrust(certs[0].get(), net::CA_CERT,
278 net::NSSCertDatabase::TRUSTED_SSL)); 328 net::NSSCertDatabase::TRUSTED_SSL));
279 329
280 // Cert trust change should trigger certificate reload in cert_loader_. 330 // Cert trust change should trigger certificate reload in cert_loader_.
281 ASSERT_EQ(0U, GetAndResetCertificatesLoadedEventsCount()); 331 ASSERT_EQ(0U, GetAndResetCertificatesLoadedEventsCount());
282 base::RunLoop().RunUntilIdle(); 332 base::RunLoop().RunUntilIdle();
283 EXPECT_EQ(1U, GetAndResetCertificatesLoadedEventsCount()); 333 EXPECT_EQ(1U, GetAndResetCertificatesLoadedEventsCount());
284 } 334 }
285 335
286 } // namespace chromeos 336 } // namespace chromeos
OLDNEW

Powered by Google App Engine
This is Rietveld 408576698