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 "chromeos/network/onc/onc_certificate_importer.h" | 5 #include "chromeos/network/onc/onc_certificate_importer.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> |
(...skipping 48 matching lines...) Expand 10 before | Expand all | Expand 10 after Loading... |
59 public: | 59 public: |
60 virtual void SetUp() { | 60 virtual void SetUp() { |
61 ASSERT_TRUE(test_nssdb_.is_open()); | 61 ASSERT_TRUE(test_nssdb_.is_open()); |
62 | 62 |
63 slot_ = net::NSSCertDatabase::GetInstance()->GetPublicModule(); | 63 slot_ = net::NSSCertDatabase::GetInstance()->GetPublicModule(); |
64 | 64 |
65 // Don't run the test if the setup failed. | 65 // Don't run the test if the setup failed. |
66 ASSERT_TRUE(slot_->os_module_handle()); | 66 ASSERT_TRUE(slot_->os_module_handle()); |
67 | 67 |
68 // Test db should be empty at start of test. | 68 // Test db should be empty at start of test. |
69 EXPECT_EQ(0ul, ListCertsInSlot(slot_->os_module_handle()).size()); | 69 EXPECT_EQ(0ul, ListCertsInSlot().size()); |
70 } | 70 } |
71 | 71 |
72 virtual void TearDown() { | 72 virtual void TearDown() { |
73 EXPECT_TRUE(CleanupSlotContents(slot_->os_module_handle())); | 73 EXPECT_TRUE(CleanupSlotContents()); |
74 EXPECT_EQ(0ul, ListCertsInSlot(slot_->os_module_handle()).size()); | 74 EXPECT_EQ(0ul, ListCertsInSlot().size()); |
75 } | 75 } |
76 | 76 |
77 virtual ~ONCCertificateImporterTest() {} | 77 virtual ~ONCCertificateImporterTest() {} |
78 | 78 |
79 protected: | 79 protected: |
80 void AddCertificateFromFile(std::string filename, | 80 void AddCertificatesFromFile( |
81 net::CertType expected_type, | 81 std::string filename, |
82 std::string* guid) { | 82 CertificateImporter::ParseResult expected_parse_result) { |
83 scoped_ptr<base::DictionaryValue> onc = | 83 scoped_ptr<base::DictionaryValue> onc = |
84 test_utils::ReadTestDictionary(filename); | 84 test_utils::ReadTestDictionary(filename); |
| 85 base::Value* certificates_value = NULL; |
85 base::ListValue* certificates = NULL; | 86 base::ListValue* certificates = NULL; |
86 onc->GetListWithoutPathExpansion(toplevel_config::kCertificates, | 87 onc->RemoveWithoutPathExpansion(toplevel_config::kCertificates, |
87 &certificates); | 88 &certificates_value); |
88 | 89 certificates_value->GetAsList(&certificates); |
89 base::DictionaryValue* certificate = NULL; | 90 onc_certificates_.reset(certificates); |
90 certificates->GetDictionary(0, &certificate); | |
91 certificate->GetStringWithoutPathExpansion(certificate::kGUID, guid); | |
92 | 91 |
93 web_trust_certificates_.clear(); | 92 web_trust_certificates_.clear(); |
94 CertificateImporter importer(true /* allow web trust */); | 93 CertificateImporter importer(true /* allow web trust */); |
95 EXPECT_EQ(CertificateImporter::IMPORT_OK, | 94 EXPECT_EQ(expected_parse_result, |
96 importer.ParseAndStoreCertificates(*certificates, | 95 importer.ParseAndStoreCertificates(*certificates, |
97 &web_trust_certificates_)); | 96 &web_trust_certificates_)); |
98 | 97 |
99 result_list_.clear(); | 98 result_list_.clear(); |
| 99 result_list_ = ListCertsInSlot(); |
| 100 } |
| 101 |
| 102 void AddCertificateFromFile(std::string filename, |
| 103 net::CertType expected_type, |
| 104 std::string* guid) { |
| 105 AddCertificatesFromFile(filename, CertificateImporter::IMPORT_OK); |
| 106 EXPECT_EQ(1ul, result_list_.size()); |
| 107 |
| 108 base::DictionaryValue* certificate = NULL; |
| 109 onc_certificates_->GetDictionary(0, &certificate); |
| 110 certificate->GetStringWithoutPathExpansion(certificate::kGUID, guid); |
| 111 |
100 CertificateImporter::ListCertsWithNickname(*guid, &result_list_); | 112 CertificateImporter::ListCertsWithNickname(*guid, &result_list_); |
101 ASSERT_EQ(1ul, result_list_.size()); | 113 ASSERT_EQ(1ul, result_list_.size()); |
102 EXPECT_EQ(expected_type, GetCertType(result_list_[0]->os_cert_handle())); | 114 EXPECT_EQ(expected_type, GetCertType(result_list_[0]->os_cert_handle())); |
103 } | 115 } |
104 | 116 |
| 117 scoped_ptr<base::ListValue> onc_certificates_; |
105 scoped_refptr<net::CryptoModule> slot_; | 118 scoped_refptr<net::CryptoModule> slot_; |
106 net::CertificateList result_list_; | 119 net::CertificateList result_list_; |
107 net::CertificateList web_trust_certificates_; | 120 net::CertificateList web_trust_certificates_; |
108 | 121 |
109 private: | 122 private: |
110 net::CertificateList ListCertsInSlot(PK11SlotInfo* slot) { | 123 net::CertificateList ListCertsInSlot() { |
111 net::CertificateList result; | 124 net::CertificateList result; |
112 CERTCertList* cert_list = PK11_ListCertsInSlot(slot); | 125 CERTCertList* cert_list = PK11_ListCertsInSlot(slot_->os_module_handle()); |
113 for (CERTCertListNode* node = CERT_LIST_HEAD(cert_list); | 126 for (CERTCertListNode* node = CERT_LIST_HEAD(cert_list); |
114 !CERT_LIST_END(node, cert_list); | 127 !CERT_LIST_END(node, cert_list); |
115 node = CERT_LIST_NEXT(node)) { | 128 node = CERT_LIST_NEXT(node)) { |
116 result.push_back(net::X509Certificate::CreateFromHandle( | 129 result.push_back(net::X509Certificate::CreateFromHandle( |
117 node->cert, net::X509Certificate::OSCertHandles())); | 130 node->cert, net::X509Certificate::OSCertHandles())); |
118 } | 131 } |
119 CERT_DestroyCertList(cert_list); | 132 CERT_DestroyCertList(cert_list); |
120 | 133 |
121 // Sort the result so that test comparisons can be deterministic. | 134 // Sort the result so that test comparisons can be deterministic. |
122 std::sort(result.begin(), result.end(), net::X509Certificate::LessThan()); | 135 std::sort(result.begin(), result.end(), net::X509Certificate::LessThan()); |
123 return result; | 136 return result; |
124 } | 137 } |
125 | 138 |
126 bool CleanupSlotContents(PK11SlotInfo* slot) { | 139 bool CleanupSlotContents() { |
127 bool ok = true; | 140 bool ok = true; |
128 net::CertificateList certs = ListCertsInSlot(slot); | 141 net::CertificateList certs = ListCertsInSlot(); |
129 for (size_t i = 0; i < certs.size(); ++i) { | 142 for (size_t i = 0; i < certs.size(); ++i) { |
130 if (!net::NSSCertDatabase::GetInstance()->DeleteCertAndKey(certs[i])) | 143 if (!net::NSSCertDatabase::GetInstance()->DeleteCertAndKey(certs[i])) |
131 ok = false; | 144 ok = false; |
132 } | 145 } |
133 return ok; | 146 return ok; |
134 } | 147 } |
135 | 148 |
136 crypto::ScopedTestNSSDB test_nssdb_; | 149 crypto::ScopedTestNSSDB test_nssdb_; |
137 }; | 150 }; |
138 | 151 |
| 152 TEST_F(ONCCertificateImporterTest, MultipleCertificates) { |
| 153 AddCertificatesFromFile("managed_toplevel2.onc", |
| 154 CertificateImporter::IMPORT_OK); |
| 155 EXPECT_EQ(onc_certificates_->GetSize(), result_list_.size()); |
| 156 } |
| 157 |
| 158 TEST_F(ONCCertificateImporterTest, MultipleCertificatesWithFailures) { |
| 159 AddCertificatesFromFile("toplevel_partially_invalid.onc", |
| 160 CertificateImporter::IMPORT_INCOMPLETE); |
| 161 EXPECT_EQ(2ul, onc_certificates_->GetSize()); |
| 162 EXPECT_EQ(1ul, result_list_.size()); |
| 163 } |
| 164 |
139 TEST_F(ONCCertificateImporterTest, AddClientCertificate) { | 165 TEST_F(ONCCertificateImporterTest, AddClientCertificate) { |
140 std::string guid; | 166 std::string guid; |
141 AddCertificateFromFile("certificate-client.onc", net::USER_CERT, &guid); | 167 AddCertificateFromFile("certificate-client.onc", net::USER_CERT, &guid); |
142 EXPECT_TRUE(web_trust_certificates_.empty()); | 168 EXPECT_TRUE(web_trust_certificates_.empty()); |
143 | 169 |
144 SECKEYPrivateKeyList* privkey_list = | 170 SECKEYPrivateKeyList* privkey_list = |
145 PK11_ListPrivKeysInSlot(slot_->os_module_handle(), NULL, NULL); | 171 PK11_ListPrivKeysInSlot(slot_->os_module_handle(), NULL, NULL); |
146 EXPECT_TRUE(privkey_list); | 172 EXPECT_TRUE(privkey_list); |
147 if (privkey_list) { | 173 if (privkey_list) { |
148 SECKEYPrivateKeyListNode* node = PRIVKEY_LIST_HEAD(privkey_list); | 174 SECKEYPrivateKeyListNode* node = PRIVKEY_LIST_HEAD(privkey_list); |
(...skipping 131 matching lines...) Expand 10 before | Expand all | Expand 10 after Loading... |
280 "certificate-client-update.onc"), | 306 "certificate-client-update.onc"), |
281 CertParam(net::SERVER_CERT, | 307 CertParam(net::SERVER_CERT, |
282 "certificate-server.onc", | 308 "certificate-server.onc", |
283 "certificate-server-update.onc"), | 309 "certificate-server-update.onc"), |
284 CertParam(net::CA_CERT, | 310 CertParam(net::CA_CERT, |
285 "certificate-web-authority.onc", | 311 "certificate-web-authority.onc", |
286 "certificate-web-authority-update.onc"))); | 312 "certificate-web-authority-update.onc"))); |
287 | 313 |
288 } // namespace onc | 314 } // namespace onc |
289 } // namespace chromeos | 315 } // namespace chromeos |
OLD | NEW |