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

Side by Side Diff: chrome/browser/chromeos/policy/policy_cert_verifier_unittest.cc

Issue 407443008: Fix cert DB usage in PolicyCertVerifier test. (Closed) Base URL: svn://svn.chromium.org/chrome/trunk/src
Patch Set: Created 6 years, 5 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 | Annotate | Revision Log
OLDNEW
1 // Copyright (c) 2013 The Chromium Authors. All rights reserved. 1 // Copyright (c) 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 "chrome/browser/chromeos/policy/policy_cert_verifier.h" 5 #include "chrome/browser/chromeos/policy/policy_cert_verifier.h"
6 6
7 #include "base/bind.h" 7 #include "base/bind.h"
8 #include "base/bind_helpers.h" 8 #include "base/bind_helpers.h"
9 #include "base/callback.h" 9 #include "base/callback.h"
10 #include "base/memory/ref_counted.h" 10 #include "base/memory/ref_counted.h"
11 #include "base/memory/scoped_ptr.h" 11 #include "base/memory/scoped_ptr.h"
12 #include "base/run_loop.h" 12 #include "base/run_loop.h"
13 #include "chrome/browser/chromeos/net/cert_verify_proc_chromeos.h" 13 #include "chrome/browser/chromeos/net/cert_verify_proc_chromeos.h"
14 #include "content/public/browser/browser_thread.h" 14 #include "content/public/browser/browser_thread.h"
15 #include "content/public/test/test_browser_thread_bundle.h" 15 #include "content/public/test/test_browser_thread_bundle.h"
16 #include "crypto/nss_util.h" 16 #include "crypto/nss_util.h"
17 #include "crypto/nss_util_internal.h" 17 #include "crypto/nss_util_internal.h"
18 #include "net/base/net_log.h" 18 #include "net/base/net_log.h"
19 #include "net/base/test_completion_callback.h" 19 #include "net/base/test_completion_callback.h"
20 #include "net/base/test_data_directory.h" 20 #include "net/base/test_data_directory.h"
21 #include "net/cert/cert_trust_anchor_provider.h" 21 #include "net/cert/cert_trust_anchor_provider.h"
22 #include "net/cert/cert_verify_result.h" 22 #include "net/cert/cert_verify_result.h"
23 #include "net/cert/nss_cert_database.h" 23 #include "net/cert/nss_cert_database_chromeos.h"
24 #include "net/cert/x509_certificate.h" 24 #include "net/cert/x509_certificate.h"
25 #include "net/test/cert_test_util.h" 25 #include "net/test/cert_test_util.h"
26 #include "testing/gtest/include/gtest/gtest.h" 26 #include "testing/gtest/include/gtest/gtest.h"
27 27
28 namespace policy { 28 namespace policy {
29 29
30 // This is actually a unit test, but is linked with browser_tests because
31 // importing a certificate into the NSS test database persists for the duration
32 // of a process; since each browser_test runs in a separate process then this
33 // won't affect subsequent tests.
34 // This can be moved to the unittests target once the TODO in ~ScopedTestNSSDB
35 // is fixed.
36 class PolicyCertVerifierTest : public testing::Test { 30 class PolicyCertVerifierTest : public testing::Test {
37 public: 31 public:
38 PolicyCertVerifierTest() : cert_db_(NULL), trust_anchor_used_(false) {} 32 PolicyCertVerifierTest()
33 : trust_anchor_used_(false), test_nss_user_("user1") {}
39 34
40 virtual ~PolicyCertVerifierTest() {} 35 virtual ~PolicyCertVerifierTest() {}
41 36
42 virtual void SetUp() OVERRIDE { 37 virtual void SetUp() OVERRIDE {
43 ASSERT_TRUE(test_nssdb_.is_open()); 38 ASSERT_TRUE(test_nss_user_.constructed_successfully());
44 cert_db_ = net::NSSCertDatabase::GetInstance(); 39 test_nss_user_.FinishInit();
40
41 test_cert_db_.reset(new net::NSSCertDatabaseChromeOS(
42 crypto::GetPublicSlotForChromeOSUser(test_nss_user_.username_hash()),
43 crypto::GetPrivateSlotForChromeOSUser(
44 test_nss_user_.username_hash(),
45 base::Callback<void(crypto::ScopedPK11Slot)>())));
46 test_cert_db_->SetSlowTaskRunnerForTest(base::MessageLoopProxy::current());
45 47
46 cert_verifier_.reset(new PolicyCertVerifier(base::Bind( 48 cert_verifier_.reset(new PolicyCertVerifier(base::Bind(
47 &PolicyCertVerifierTest::OnTrustAnchorUsed, base::Unretained(this)))); 49 &PolicyCertVerifierTest::OnTrustAnchorUsed, base::Unretained(this))));
48 cert_verifier_->InitializeOnIOThread(new chromeos::CertVerifyProcChromeOS( 50 cert_verifier_->InitializeOnIOThread(new chromeos::CertVerifyProcChromeOS(
49 crypto::ScopedPK11Slot(crypto::GetPersistentNSSKeySlot()))); 51 crypto::GetPublicSlotForChromeOSUser(test_nss_user_.username_hash())));
50 52
51 test_ca_cert_ = LoadCertificate("root_ca_cert.pem", net::CA_CERT); 53 test_ca_cert_ = LoadCertificate("root_ca_cert.pem", net::CA_CERT);
52 ASSERT_TRUE(test_ca_cert_); 54 ASSERT_TRUE(test_ca_cert_);
53 test_server_cert_ = LoadCertificate("ok_cert.pem", net::SERVER_CERT); 55 test_server_cert_ = LoadCertificate("ok_cert.pem", net::SERVER_CERT);
54 ASSERT_TRUE(test_server_cert_); 56 ASSERT_TRUE(test_server_cert_);
55 test_ca_cert_list_.push_back(test_ca_cert_); 57 test_ca_cert_list_.push_back(test_ca_cert_);
56 } 58 }
57 59
58 virtual void TearDown() OVERRIDE { 60 virtual void TearDown() OVERRIDE {
59 // Destroy |cert_verifier_| before destroying the ThreadBundle, otherwise 61 // Destroy |cert_verifier_| before destroying the ThreadBundle, otherwise
(...skipping 28 matching lines...) Expand all
88 base::RunLoop().RunUntilIdle(); 90 base::RunLoop().RunUntilIdle();
89 bool result = trust_anchor_used_; 91 bool result = trust_anchor_used_;
90 trust_anchor_used_ = false; 92 trust_anchor_used_ = false;
91 return result; 93 return result;
92 } 94 }
93 95
94 // |test_ca_cert_| is the issuer of |test_server_cert_|. 96 // |test_ca_cert_| is the issuer of |test_server_cert_|.
95 scoped_refptr<net::X509Certificate> test_ca_cert_; 97 scoped_refptr<net::X509Certificate> test_ca_cert_;
96 scoped_refptr<net::X509Certificate> test_server_cert_; 98 scoped_refptr<net::X509Certificate> test_server_cert_;
97 net::CertificateList test_ca_cert_list_; 99 net::CertificateList test_ca_cert_list_;
98 net::NSSCertDatabase* cert_db_; 100 scoped_ptr<net::NSSCertDatabaseChromeOS> test_cert_db_;
99 scoped_ptr<PolicyCertVerifier> cert_verifier_; 101 scoped_ptr<PolicyCertVerifier> cert_verifier_;
100 102
101 private: 103 private:
102 void OnTrustAnchorUsed() { 104 void OnTrustAnchorUsed() {
103 trust_anchor_used_ = true; 105 trust_anchor_used_ = true;
104 } 106 }
105 107
106 scoped_refptr<net::X509Certificate> LoadCertificate(const std::string& name, 108 scoped_refptr<net::X509Certificate> LoadCertificate(const std::string& name,
107 net::CertType type) { 109 net::CertType type) {
108 scoped_refptr<net::X509Certificate> cert = 110 scoped_refptr<net::X509Certificate> cert =
109 net::ImportCertFromFile(net::GetTestCertsDirectory(), name); 111 net::ImportCertFromFile(net::GetTestCertsDirectory(), name);
110 112
111 // No certificate is trusted right after it's loaded. 113 // No certificate is trusted right after it's loaded.
112 net::NSSCertDatabase::TrustBits trust = 114 net::NSSCertDatabase::TrustBits trust =
113 cert_db_->GetCertTrust(cert.get(), type); 115 test_cert_db_->GetCertTrust(cert.get(), type);
114 EXPECT_EQ(net::NSSCertDatabase::TRUST_DEFAULT, trust); 116 EXPECT_EQ(net::NSSCertDatabase::TRUST_DEFAULT, trust);
115 117
116 return cert; 118 return cert;
117 } 119 }
118 120
119 bool trust_anchor_used_; 121 bool trust_anchor_used_;
120 crypto::ScopedTestNSSDB test_nssdb_; 122 crypto::ScopedTestNSSChromeOSUser test_nss_user_;
121 content::TestBrowserThreadBundle thread_bundle_; 123 content::TestBrowserThreadBundle thread_bundle_;
122 }; 124 };
123 125
124 TEST_F(PolicyCertVerifierTest, VerifyUntrustedCert) { 126 TEST_F(PolicyCertVerifierTest, VerifyUntrustedCert) {
125 // |test_server_cert_| is untrusted, so Verify() fails. 127 // |test_server_cert_| is untrusted, so Verify() fails.
126 { 128 {
127 net::CertVerifyResult verify_result; 129 net::CertVerifyResult verify_result;
128 net::TestCompletionCallback callback; 130 net::TestCompletionCallback callback;
129 net::CertVerifier::RequestHandle request_handle = NULL; 131 net::CertVerifier::RequestHandle request_handle = NULL;
130 int error = VerifyTestServerCert(callback, &verify_result, &request_handle); 132 int error = VerifyTestServerCert(callback, &verify_result, &request_handle);
(...skipping 12 matching lines...) Expand all
143 int error = VerifyTestServerCert(callback, &verify_result, &request_handle); 145 int error = VerifyTestServerCert(callback, &verify_result, &request_handle);
144 EXPECT_EQ(net::ERR_CERT_AUTHORITY_INVALID, error); 146 EXPECT_EQ(net::ERR_CERT_AUTHORITY_INVALID, error);
145 } 147 }
146 148
147 EXPECT_FALSE(WasTrustAnchorUsedAndReset()); 149 EXPECT_FALSE(WasTrustAnchorUsedAndReset());
148 } 150 }
149 151
150 TEST_F(PolicyCertVerifierTest, VerifyTrustedCert) { 152 TEST_F(PolicyCertVerifierTest, VerifyTrustedCert) {
151 // Make the database trust |test_ca_cert_|. 153 // Make the database trust |test_ca_cert_|.
152 net::NSSCertDatabase::ImportCertFailureList failure_list; 154 net::NSSCertDatabase::ImportCertFailureList failure_list;
153 ASSERT_TRUE(cert_db_->ImportCACerts( 155 ASSERT_TRUE(test_cert_db_->ImportCACerts(
154 test_ca_cert_list_, net::NSSCertDatabase::TRUSTED_SSL, &failure_list)); 156 test_ca_cert_list_, net::NSSCertDatabase::TRUSTED_SSL, &failure_list));
155 ASSERT_TRUE(failure_list.empty()); 157 ASSERT_TRUE(failure_list.empty());
156 158
157 // Verify that it is now trusted. 159 // Verify that it is now trusted.
158 net::NSSCertDatabase::TrustBits trust = 160 net::NSSCertDatabase::TrustBits trust =
159 cert_db_->GetCertTrust(test_ca_cert_.get(), net::CA_CERT); 161 test_cert_db_->GetCertTrust(test_ca_cert_.get(), net::CA_CERT);
160 EXPECT_EQ(net::NSSCertDatabase::TRUSTED_SSL, trust); 162 EXPECT_EQ(net::NSSCertDatabase::TRUSTED_SSL, trust);
161 163
162 // Verify() successfully verifies |test_server_cert_| after it was imported. 164 // Verify() successfully verifies |test_server_cert_| after it was imported.
163 net::CertVerifyResult verify_result; 165 net::CertVerifyResult verify_result;
164 net::TestCompletionCallback callback; 166 net::TestCompletionCallback callback;
165 net::CertVerifier::RequestHandle request_handle = NULL; 167 net::CertVerifier::RequestHandle request_handle = NULL;
166 int error = VerifyTestServerCert(callback, &verify_result, &request_handle); 168 int error = VerifyTestServerCert(callback, &verify_result, &request_handle);
167 ASSERT_EQ(net::ERR_IO_PENDING, error); 169 ASSERT_EQ(net::ERR_IO_PENDING, error);
168 EXPECT_TRUE(request_handle); 170 EXPECT_TRUE(request_handle);
169 error = callback.WaitForResult(); 171 error = callback.WaitForResult();
(...skipping 54 matching lines...) Expand 10 before | Expand all | Expand 10 after
224 int error = VerifyTestServerCert(callback, &verify_result, &request_handle); 226 int error = VerifyTestServerCert(callback, &verify_result, &request_handle);
225 // Note: this hits the cached result from the first Verify() in this test. 227 // Note: this hits the cached result from the first Verify() in this test.
226 EXPECT_EQ(net::ERR_CERT_AUTHORITY_INVALID, error); 228 EXPECT_EQ(net::ERR_CERT_AUTHORITY_INVALID, error);
227 } 229 }
228 // The additional trust anchors were reset, thus |cert_verifier_| should not 230 // The additional trust anchors were reset, thus |cert_verifier_| should not
229 // signal it's usage anymore. 231 // signal it's usage anymore.
230 EXPECT_FALSE(WasTrustAnchorUsedAndReset()); 232 EXPECT_FALSE(WasTrustAnchorUsedAndReset());
231 } 233 }
232 234
233 } // namespace policy 235 } // namespace policy
OLDNEW
« no previous file with comments | « chrome/browser/chromeos/policy/policy_cert_verifier_browsertest.cc ('k') | chrome/chrome_tests.gypi » ('j') | no next file with comments »

Powered by Google App Engine
This is Rietveld 408576698