| OLD | NEW |
| 1 // Copyright (c) 2011 The Chromium Authors. All rights reserved. | 1 // Copyright (c) 2011 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/base/default_origin_bound_cert_store.h" | 5 #include "net/base/default_origin_bound_cert_store.h" |
| 6 | 6 |
| 7 #include <map> | 7 #include <map> |
| 8 #include <string> | 8 #include <string> |
| 9 #include <vector> | 9 #include <vector> |
| 10 | 10 |
| (...skipping 59 matching lines...) Expand 10 before | Expand all | Expand 10 after Loading... |
| 70 | 70 |
| 71 void MockPersistentStore::Flush(const base::Closure& completion_task) { | 71 void MockPersistentStore::Flush(const base::Closure& completion_task) { |
| 72 NOTREACHED(); | 72 NOTREACHED(); |
| 73 } | 73 } |
| 74 | 74 |
| 75 TEST(DefaultOriginBoundCertStoreTest, TestLoading) { | 75 TEST(DefaultOriginBoundCertStoreTest, TestLoading) { |
| 76 scoped_refptr<MockPersistentStore> persistent_store(new MockPersistentStore); | 76 scoped_refptr<MockPersistentStore> persistent_store(new MockPersistentStore); |
| 77 | 77 |
| 78 persistent_store->AddOriginBoundCert( | 78 persistent_store->AddOriginBoundCert( |
| 79 DefaultOriginBoundCertStore::OriginBoundCert( | 79 DefaultOriginBoundCertStore::OriginBoundCert( |
| 80 "https://encrypted.google.com/", CLIENT_CERT_RSA_SIGN, "a", "b")); | 80 "https://encrypted.google.com/", |
| 81 CLIENT_CERT_RSA_SIGN, |
| 82 base::Time(), |
| 83 "a", "b")); |
| 81 persistent_store->AddOriginBoundCert( | 84 persistent_store->AddOriginBoundCert( |
| 82 DefaultOriginBoundCertStore::OriginBoundCert( | 85 DefaultOriginBoundCertStore::OriginBoundCert( |
| 83 "https://www.verisign.com/", CLIENT_CERT_ECDSA_SIGN, "c", "d")); | 86 "https://www.verisign.com/", |
| 87 CLIENT_CERT_ECDSA_SIGN, |
| 88 base::Time(), |
| 89 "c", "d")); |
| 84 | 90 |
| 85 // Make sure certs load properly. | 91 // Make sure certs load properly. |
| 86 DefaultOriginBoundCertStore store(persistent_store.get()); | 92 DefaultOriginBoundCertStore store(persistent_store.get()); |
| 87 EXPECT_EQ(2, store.GetCertCount()); | 93 EXPECT_EQ(2, store.GetCertCount()); |
| 88 store.SetOriginBoundCert( | 94 store.SetOriginBoundCert( |
| 89 "https://www.verisign.com/", CLIENT_CERT_RSA_SIGN, "e", "f"); | 95 "https://www.verisign.com/", |
| 96 CLIENT_CERT_RSA_SIGN, |
| 97 base::Time(), |
| 98 "e", "f"); |
| 90 EXPECT_EQ(2, store.GetCertCount()); | 99 EXPECT_EQ(2, store.GetCertCount()); |
| 91 store.SetOriginBoundCert( | 100 store.SetOriginBoundCert( |
| 92 "https://www.twitter.com/", CLIENT_CERT_RSA_SIGN, "g", "h"); | 101 "https://www.twitter.com/", |
| 102 CLIENT_CERT_RSA_SIGN, |
| 103 base::Time(), |
| 104 "g", "h"); |
| 93 EXPECT_EQ(3, store.GetCertCount()); | 105 EXPECT_EQ(3, store.GetCertCount()); |
| 94 } | 106 } |
| 95 | 107 |
| 96 TEST(DefaultOriginBoundCertStoreTest, TestSettingAndGetting) { | 108 TEST(DefaultOriginBoundCertStoreTest, TestSettingAndGetting) { |
| 97 DefaultOriginBoundCertStore store(NULL); | 109 DefaultOriginBoundCertStore store(NULL); |
| 98 SSLClientCertType type; | 110 SSLClientCertType type; |
| 111 base::Time expiration_time; |
| 99 std::string private_key, cert; | 112 std::string private_key, cert; |
| 100 EXPECT_EQ(0, store.GetCertCount()); | 113 EXPECT_EQ(0, store.GetCertCount()); |
| 101 EXPECT_FALSE(store.GetOriginBoundCert("https://www.verisign.com/", | 114 EXPECT_FALSE(store.GetOriginBoundCert("https://www.verisign.com/", |
| 102 &type, | 115 &type, |
| 116 &expiration_time, |
| 103 &private_key, | 117 &private_key, |
| 104 &cert)); | 118 &cert)); |
| 105 EXPECT_TRUE(private_key.empty()); | 119 EXPECT_TRUE(private_key.empty()); |
| 106 EXPECT_TRUE(cert.empty()); | 120 EXPECT_TRUE(cert.empty()); |
| 107 store.SetOriginBoundCert( | 121 store.SetOriginBoundCert( |
| 108 "https://www.verisign.com/", CLIENT_CERT_RSA_SIGN, "i", "j"); | 122 "https://www.verisign.com/", |
| 123 CLIENT_CERT_RSA_SIGN, |
| 124 base::Time::FromInternalValue(123), |
| 125 "i", "j"); |
| 109 EXPECT_TRUE(store.GetOriginBoundCert("https://www.verisign.com/", | 126 EXPECT_TRUE(store.GetOriginBoundCert("https://www.verisign.com/", |
| 110 &type, | 127 &type, |
| 128 &expiration_time, |
| 111 &private_key, | 129 &private_key, |
| 112 &cert)); | 130 &cert)); |
| 113 EXPECT_EQ(CLIENT_CERT_RSA_SIGN, type); | 131 EXPECT_EQ(CLIENT_CERT_RSA_SIGN, type); |
| 132 EXPECT_EQ(123, expiration_time.ToInternalValue()); |
| 114 EXPECT_EQ("i", private_key); | 133 EXPECT_EQ("i", private_key); |
| 115 EXPECT_EQ("j", cert); | 134 EXPECT_EQ("j", cert); |
| 116 } | 135 } |
| 117 | 136 |
| 118 TEST(DefaultOriginBoundCertStoreTest, TestDuplicateCerts) { | 137 TEST(DefaultOriginBoundCertStoreTest, TestDuplicateCerts) { |
| 119 scoped_refptr<MockPersistentStore> persistent_store(new MockPersistentStore); | 138 scoped_refptr<MockPersistentStore> persistent_store(new MockPersistentStore); |
| 120 DefaultOriginBoundCertStore store(persistent_store.get()); | 139 DefaultOriginBoundCertStore store(persistent_store.get()); |
| 121 | 140 |
| 122 SSLClientCertType type; | 141 SSLClientCertType type; |
| 142 base::Time expiration_time; |
| 123 std::string private_key, cert; | 143 std::string private_key, cert; |
| 124 EXPECT_EQ(0, store.GetCertCount()); | 144 EXPECT_EQ(0, store.GetCertCount()); |
| 125 store.SetOriginBoundCert( | 145 store.SetOriginBoundCert( |
| 126 "https://www.verisign.com/", CLIENT_CERT_RSA_SIGN, "a", "b"); | 146 "https://www.verisign.com/", |
| 147 CLIENT_CERT_RSA_SIGN, |
| 148 base::Time::FromInternalValue(123), |
| 149 "a", "b"); |
| 127 store.SetOriginBoundCert( | 150 store.SetOriginBoundCert( |
| 128 "https://www.verisign.com/", CLIENT_CERT_ECDSA_SIGN, "c", "d"); | 151 "https://www.verisign.com/", |
| 152 CLIENT_CERT_ECDSA_SIGN, |
| 153 base::Time::FromInternalValue(456), |
| 154 "c", "d"); |
| 129 | 155 |
| 130 EXPECT_EQ(1, store.GetCertCount()); | 156 EXPECT_EQ(1, store.GetCertCount()); |
| 131 EXPECT_TRUE(store.GetOriginBoundCert("https://www.verisign.com/", | 157 EXPECT_TRUE(store.GetOriginBoundCert("https://www.verisign.com/", |
| 132 &type, | 158 &type, |
| 159 &expiration_time, |
| 133 &private_key, | 160 &private_key, |
| 134 &cert)); | 161 &cert)); |
| 135 EXPECT_EQ(CLIENT_CERT_ECDSA_SIGN, type); | 162 EXPECT_EQ(CLIENT_CERT_ECDSA_SIGN, type); |
| 163 EXPECT_EQ(456, expiration_time.ToInternalValue()); |
| 136 EXPECT_EQ("c", private_key); | 164 EXPECT_EQ("c", private_key); |
| 137 EXPECT_EQ("d", cert); | 165 EXPECT_EQ("d", cert); |
| 138 } | 166 } |
| 139 | 167 |
| 140 TEST(DefaultOriginBoundCertStoreTest, TestDeleteAll) { | 168 TEST(DefaultOriginBoundCertStoreTest, TestDeleteAll) { |
| 141 scoped_refptr<MockPersistentStore> persistent_store(new MockPersistentStore); | 169 scoped_refptr<MockPersistentStore> persistent_store(new MockPersistentStore); |
| 142 DefaultOriginBoundCertStore store(persistent_store.get()); | 170 DefaultOriginBoundCertStore store(persistent_store.get()); |
| 143 | 171 |
| 144 EXPECT_EQ(0, store.GetCertCount()); | 172 EXPECT_EQ(0, store.GetCertCount()); |
| 145 store.SetOriginBoundCert( | 173 store.SetOriginBoundCert( |
| 146 "https://www.verisign.com/", CLIENT_CERT_RSA_SIGN, "a", "b"); | 174 "https://www.verisign.com/", |
| 175 CLIENT_CERT_RSA_SIGN, |
| 176 base::Time(), |
| 177 "a", "b"); |
| 147 store.SetOriginBoundCert( | 178 store.SetOriginBoundCert( |
| 148 "https://www.google.com/", CLIENT_CERT_RSA_SIGN, "c", "d"); | 179 "https://www.google.com/", |
| 180 CLIENT_CERT_RSA_SIGN, |
| 181 base::Time(), |
| 182 "c", "d"); |
| 149 store.SetOriginBoundCert( | 183 store.SetOriginBoundCert( |
| 150 "https://www.harvard.com/", CLIENT_CERT_RSA_SIGN, "e", "f"); | 184 "https://www.harvard.com/", |
| 185 CLIENT_CERT_RSA_SIGN, |
| 186 base::Time(), |
| 187 "e", "f"); |
| 151 | 188 |
| 152 EXPECT_EQ(3, store.GetCertCount()); | 189 EXPECT_EQ(3, store.GetCertCount()); |
| 153 store.DeleteAll(); | 190 store.DeleteAll(); |
| 154 EXPECT_EQ(0, store.GetCertCount()); | 191 EXPECT_EQ(0, store.GetCertCount()); |
| 155 } | 192 } |
| 156 | 193 |
| 157 TEST(DefaultOriginBoundCertStoreTest, TestDelete) { | 194 TEST(DefaultOriginBoundCertStoreTest, TestDelete) { |
| 158 scoped_refptr<MockPersistentStore> persistent_store(new MockPersistentStore); | 195 scoped_refptr<MockPersistentStore> persistent_store(new MockPersistentStore); |
| 159 DefaultOriginBoundCertStore store(persistent_store.get()); | 196 DefaultOriginBoundCertStore store(persistent_store.get()); |
| 160 | 197 |
| 161 SSLClientCertType type; | 198 SSLClientCertType type; |
| 199 base::Time expiration_time; |
| 162 std::string private_key, cert; | 200 std::string private_key, cert; |
| 163 EXPECT_EQ(0, store.GetCertCount()); | 201 EXPECT_EQ(0, store.GetCertCount()); |
| 164 store.SetOriginBoundCert( | 202 store.SetOriginBoundCert( |
| 165 "https://www.verisign.com/", CLIENT_CERT_RSA_SIGN, "a", "b"); | 203 "https://www.verisign.com/", |
| 204 CLIENT_CERT_RSA_SIGN, |
| 205 base::Time(), |
| 206 "a", "b"); |
| 166 store.SetOriginBoundCert( | 207 store.SetOriginBoundCert( |
| 167 "https://www.google.com/", CLIENT_CERT_ECDSA_SIGN, "c", "d"); | 208 "https://www.google.com/", |
| 209 CLIENT_CERT_ECDSA_SIGN, |
| 210 base::Time(), |
| 211 "c", "d"); |
| 168 | 212 |
| 169 EXPECT_EQ(2, store.GetCertCount()); | 213 EXPECT_EQ(2, store.GetCertCount()); |
| 170 store.DeleteOriginBoundCert("https://www.verisign.com/"); | 214 store.DeleteOriginBoundCert("https://www.verisign.com/"); |
| 171 EXPECT_EQ(1, store.GetCertCount()); | 215 EXPECT_EQ(1, store.GetCertCount()); |
| 172 EXPECT_FALSE(store.GetOriginBoundCert("https://www.verisign.com/", | 216 EXPECT_FALSE(store.GetOriginBoundCert("https://www.verisign.com/", |
| 173 &type, | 217 &type, |
| 218 &expiration_time, |
| 174 &private_key, | 219 &private_key, |
| 175 &cert)); | 220 &cert)); |
| 176 EXPECT_TRUE(store.GetOriginBoundCert("https://www.google.com/", | 221 EXPECT_TRUE(store.GetOriginBoundCert("https://www.google.com/", |
| 177 &type, | 222 &type, |
| 223 &expiration_time, |
| 178 &private_key, | 224 &private_key, |
| 179 &cert)); | 225 &cert)); |
| 180 store.DeleteOriginBoundCert("https://www.google.com/"); | 226 store.DeleteOriginBoundCert("https://www.google.com/"); |
| 181 EXPECT_EQ(0, store.GetCertCount()); | 227 EXPECT_EQ(0, store.GetCertCount()); |
| 182 EXPECT_FALSE(store.GetOriginBoundCert("https://www.google.com/", | 228 EXPECT_FALSE(store.GetOriginBoundCert("https://www.google.com/", |
| 183 &type, | 229 &type, |
| 230 &expiration_time, |
| 184 &private_key, | 231 &private_key, |
| 185 &cert)); | 232 &cert)); |
| 186 } | 233 } |
| 187 | 234 |
| 188 TEST(DefaultOriginBoundCertStoreTest, TestGetAll) { | 235 TEST(DefaultOriginBoundCertStoreTest, TestGetAll) { |
| 189 scoped_refptr<MockPersistentStore> persistent_store(new MockPersistentStore); | 236 scoped_refptr<MockPersistentStore> persistent_store(new MockPersistentStore); |
| 190 DefaultOriginBoundCertStore store(persistent_store.get()); | 237 DefaultOriginBoundCertStore store(persistent_store.get()); |
| 191 | 238 |
| 192 EXPECT_EQ(0, store.GetCertCount()); | 239 EXPECT_EQ(0, store.GetCertCount()); |
| 193 store.SetOriginBoundCert( | 240 store.SetOriginBoundCert( |
| 194 "https://www.verisign.com/", CLIENT_CERT_RSA_SIGN, "a", "b"); | 241 "https://www.verisign.com/", |
| 242 CLIENT_CERT_RSA_SIGN, |
| 243 base::Time(), |
| 244 "a", "b"); |
| 195 store.SetOriginBoundCert( | 245 store.SetOriginBoundCert( |
| 196 "https://www.google.com/", CLIENT_CERT_ECDSA_SIGN, "c", "d"); | 246 "https://www.google.com/", |
| 247 CLIENT_CERT_ECDSA_SIGN, |
| 248 base::Time(), |
| 249 "c", "d"); |
| 197 store.SetOriginBoundCert( | 250 store.SetOriginBoundCert( |
| 198 "https://www.harvard.com/", CLIENT_CERT_RSA_SIGN, "e", "f"); | 251 "https://www.harvard.com/", |
| 252 CLIENT_CERT_RSA_SIGN, |
| 253 base::Time(), |
| 254 "e", "f"); |
| 199 store.SetOriginBoundCert( | 255 store.SetOriginBoundCert( |
| 200 "https://www.mit.com/", CLIENT_CERT_RSA_SIGN, "g", "h"); | 256 "https://www.mit.com/", |
| 257 CLIENT_CERT_RSA_SIGN, |
| 258 base::Time(), |
| 259 "g", "h"); |
| 201 | 260 |
| 202 EXPECT_EQ(4, store.GetCertCount()); | 261 EXPECT_EQ(4, store.GetCertCount()); |
| 203 std::vector<OriginBoundCertStore::OriginBoundCert> certs; | 262 std::vector<OriginBoundCertStore::OriginBoundCert> certs; |
| 204 store.GetAllOriginBoundCerts(&certs); | 263 store.GetAllOriginBoundCerts(&certs); |
| 205 EXPECT_EQ(4u, certs.size()); | 264 EXPECT_EQ(4u, certs.size()); |
| 206 } | 265 } |
| 207 | 266 |
| 208 } // namespace net | 267 } // namespace net |
| OLD | NEW |