| 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 "base/bind.h" | 5 #include "base/bind.h" |
| 6 #include "base/file_util.h" | 6 #include "base/file_util.h" |
| 7 #include "base/files/scoped_temp_dir.h" | 7 #include "base/files/scoped_temp_dir.h" |
| 8 #include "base/memory/ref_counted.h" | 8 #include "base/memory/ref_counted.h" |
| 9 #include "base/memory/scoped_vector.h" | 9 #include "base/memory/scoped_vector.h" |
| 10 #include "base/message_loop.h" | 10 #include "base/message_loop.h" |
| 11 #include "base/run_loop.h" | 11 #include "base/run_loop.h" |
| 12 #include "base/stl_util.h" | 12 #include "base/stl_util.h" |
| 13 #include "base/test/thread_test_helper.h" | |
| 14 #include "chrome/browser/net/sqlite_server_bound_cert_store.h" | 13 #include "chrome/browser/net/sqlite_server_bound_cert_store.h" |
| 15 #include "chrome/common/chrome_constants.h" | 14 #include "chrome/common/chrome_constants.h" |
| 16 #include "content/public/test/test_browser_thread.h" | 15 #include "content/public/browser/browser_thread.h" |
| 16 #include "content/public/test/test_browser_thread_bundle.h" |
| 17 #include "net/base/test_data_directory.h" | 17 #include "net/base/test_data_directory.h" |
| 18 #include "net/test/cert_test_util.h" | 18 #include "net/test/cert_test_util.h" |
| 19 #include "sql/statement.h" | 19 #include "sql/statement.h" |
| 20 #include "testing/gtest/include/gtest/gtest.h" | 20 #include "testing/gtest/include/gtest/gtest.h" |
| 21 #include "webkit/browser/quota/mock_special_storage_policy.h" | 21 #include "webkit/browser/quota/mock_special_storage_policy.h" |
| 22 | 22 |
| 23 using content::BrowserThread; | 23 using content::BrowserThread; |
| 24 | 24 |
| 25 class SQLiteServerBoundCertStoreTest : public testing::Test { | 25 class SQLiteServerBoundCertStoreTest : public testing::Test { |
| 26 public: | 26 public: |
| 27 SQLiteServerBoundCertStoreTest() | 27 SQLiteServerBoundCertStoreTest() |
| 28 : db_thread_(BrowserThread::DB), | 28 : thread_bundle_(content::TestBrowserThreadBundle::IO_MAINLOOP) {} |
| 29 io_thread_(BrowserThread::IO, &message_loop_) {} | |
| 30 | 29 |
| 31 void Load( | 30 void Load( |
| 32 ScopedVector<net::DefaultServerBoundCertStore::ServerBoundCert>* certs) { | 31 ScopedVector<net::DefaultServerBoundCertStore::ServerBoundCert>* certs) { |
| 33 base::RunLoop run_loop; | 32 base::RunLoop run_loop; |
| 34 store_->Load(base::Bind(&SQLiteServerBoundCertStoreTest::OnLoaded, | 33 store_->Load(base::Bind(&SQLiteServerBoundCertStoreTest::OnLoaded, |
| 35 base::Unretained(this), | 34 base::Unretained(this), |
| 36 &run_loop)); | 35 &run_loop)); |
| 37 run_loop.Run(); | 36 run_loop.Run(); |
| 38 certs->swap(certs_); | 37 certs->swap(certs_); |
| 39 certs_.clear(); | 38 certs_.clear(); |
| (...skipping 33 matching lines...) Expand 10 before | Expand all | Expand 10 after Loading... |
| 73 exploded_time.day_of_week = 0; // Unused. | 72 exploded_time.day_of_week = 0; // Unused. |
| 74 exploded_time.day_of_month = 13; | 73 exploded_time.day_of_month = 13; |
| 75 exploded_time.hour = 2; | 74 exploded_time.hour = 2; |
| 76 exploded_time.minute = 23; | 75 exploded_time.minute = 23; |
| 77 exploded_time.second = 45; | 76 exploded_time.second = 45; |
| 78 exploded_time.millisecond = 0; | 77 exploded_time.millisecond = 0; |
| 79 return base::Time::FromUTCExploded(exploded_time); | 78 return base::Time::FromUTCExploded(exploded_time); |
| 80 } | 79 } |
| 81 | 80 |
| 82 virtual void SetUp() { | 81 virtual void SetUp() { |
| 83 db_thread_.Start(); | |
| 84 ASSERT_TRUE(temp_dir_.CreateUniqueTempDir()); | 82 ASSERT_TRUE(temp_dir_.CreateUniqueTempDir()); |
| 85 store_ = new SQLiteServerBoundCertStore( | 83 store_ = new SQLiteServerBoundCertStore( |
| 86 temp_dir_.path().Append(chrome::kOBCertFilename), NULL); | 84 temp_dir_.path().Append(chrome::kOBCertFilename), NULL); |
| 87 ScopedVector<net::DefaultServerBoundCertStore::ServerBoundCert> certs; | 85 ScopedVector<net::DefaultServerBoundCertStore::ServerBoundCert> certs; |
| 88 Load(&certs); | 86 Load(&certs); |
| 89 ASSERT_EQ(0u, certs.size()); | 87 ASSERT_EQ(0u, certs.size()); |
| 90 // Make sure the store gets written at least once. | 88 // Make sure the store gets written at least once. |
| 91 store_->AddServerBoundCert( | 89 store_->AddServerBoundCert( |
| 92 net::DefaultServerBoundCertStore::ServerBoundCert( | 90 net::DefaultServerBoundCertStore::ServerBoundCert( |
| 93 "google.com", | 91 "google.com", |
| 94 net::CLIENT_CERT_RSA_SIGN, | 92 net::CLIENT_CERT_RSA_SIGN, |
| 95 base::Time::FromInternalValue(1), | 93 base::Time::FromInternalValue(1), |
| 96 base::Time::FromInternalValue(2), | 94 base::Time::FromInternalValue(2), |
| 97 "a", "b")); | 95 "a", "b")); |
| 98 } | 96 } |
| 99 | 97 |
| 100 base::MessageLoopForIO message_loop_; | 98 content::TestBrowserThreadBundle thread_bundle_; |
| 101 content::TestBrowserThread db_thread_; | |
| 102 content::TestBrowserThread io_thread_; | |
| 103 base::ScopedTempDir temp_dir_; | 99 base::ScopedTempDir temp_dir_; |
| 104 scoped_refptr<SQLiteServerBoundCertStore> store_; | 100 scoped_refptr<SQLiteServerBoundCertStore> store_; |
| 105 ScopedVector<net::DefaultServerBoundCertStore::ServerBoundCert> certs_; | 101 ScopedVector<net::DefaultServerBoundCertStore::ServerBoundCert> certs_; |
| 106 }; | 102 }; |
| 107 | 103 |
| 108 // Test if data is stored as expected in the SQLite database. | 104 // Test if data is stored as expected in the SQLite database. |
| 109 TEST_F(SQLiteServerBoundCertStoreTest, TestPersistence) { | 105 TEST_F(SQLiteServerBoundCertStoreTest, TestPersistence) { |
| 110 store_->AddServerBoundCert( | 106 store_->AddServerBoundCert( |
| 111 net::DefaultServerBoundCertStore::ServerBoundCert( | 107 net::DefaultServerBoundCertStore::ServerBoundCert( |
| 112 "foo.com", | 108 "foo.com", |
| 113 net::CLIENT_CERT_ECDSA_SIGN, | 109 net::CLIENT_CERT_ECDSA_SIGN, |
| 114 base::Time::FromInternalValue(3), | 110 base::Time::FromInternalValue(3), |
| 115 base::Time::FromInternalValue(4), | 111 base::Time::FromInternalValue(4), |
| 116 "c", "d")); | 112 "c", "d")); |
| 117 | 113 |
| 118 ScopedVector<net::DefaultServerBoundCertStore::ServerBoundCert> certs; | 114 ScopedVector<net::DefaultServerBoundCertStore::ServerBoundCert> certs; |
| 119 // Replace the store effectively destroying the current one and forcing it | 115 // Replace the store effectively destroying the current one and forcing it |
| 120 // to write its data to disk. Then we can see if after loading it again it | 116 // to write its data to disk. Then we can see if after loading it again it |
| 121 // is still there. | 117 // is still there. |
| 122 store_ = NULL; | 118 store_ = NULL; |
| 123 scoped_refptr<base::ThreadTestHelper> helper( | |
| 124 new base::ThreadTestHelper( | |
| 125 BrowserThread::GetMessageLoopProxyForThread(BrowserThread::DB))); | |
| 126 // Make sure we wait until the destructor has run. | 119 // Make sure we wait until the destructor has run. |
| 127 ASSERT_TRUE(helper->Run()); | 120 base::RunLoop().RunUntilIdle(); |
| 128 store_ = new SQLiteServerBoundCertStore( | 121 store_ = new SQLiteServerBoundCertStore( |
| 129 temp_dir_.path().Append(chrome::kOBCertFilename), NULL); | 122 temp_dir_.path().Append(chrome::kOBCertFilename), NULL); |
| 130 | 123 |
| 131 // Reload and test for persistence | 124 // Reload and test for persistence |
| 132 Load(&certs); | 125 Load(&certs); |
| 133 ASSERT_EQ(2U, certs.size()); | 126 ASSERT_EQ(2U, certs.size()); |
| 134 net::DefaultServerBoundCertStore::ServerBoundCert* ec_cert; | 127 net::DefaultServerBoundCertStore::ServerBoundCert* ec_cert; |
| 135 net::DefaultServerBoundCertStore::ServerBoundCert* rsa_cert; | 128 net::DefaultServerBoundCertStore::ServerBoundCert* rsa_cert; |
| 136 if (net::CLIENT_CERT_RSA_SIGN == certs[0]->type()) { | 129 if (net::CLIENT_CERT_RSA_SIGN == certs[0]->type()) { |
| 137 rsa_cert = certs[0]; | 130 rsa_cert = certs[0]; |
| (...skipping 13 matching lines...) Expand all Loading... |
| 151 ASSERT_STREQ("c", ec_cert->private_key().c_str()); | 144 ASSERT_STREQ("c", ec_cert->private_key().c_str()); |
| 152 ASSERT_STREQ("d", ec_cert->cert().c_str()); | 145 ASSERT_STREQ("d", ec_cert->cert().c_str()); |
| 153 ASSERT_EQ(3, ec_cert->creation_time().ToInternalValue()); | 146 ASSERT_EQ(3, ec_cert->creation_time().ToInternalValue()); |
| 154 ASSERT_EQ(4, ec_cert->expiration_time().ToInternalValue()); | 147 ASSERT_EQ(4, ec_cert->expiration_time().ToInternalValue()); |
| 155 | 148 |
| 156 // Now delete the cert and check persistence again. | 149 // Now delete the cert and check persistence again. |
| 157 store_->DeleteServerBoundCert(*certs[0]); | 150 store_->DeleteServerBoundCert(*certs[0]); |
| 158 store_->DeleteServerBoundCert(*certs[1]); | 151 store_->DeleteServerBoundCert(*certs[1]); |
| 159 store_ = NULL; | 152 store_ = NULL; |
| 160 // Make sure we wait until the destructor has run. | 153 // Make sure we wait until the destructor has run. |
| 161 ASSERT_TRUE(helper->Run()); | 154 base::RunLoop().RunUntilIdle(); |
| 162 certs.clear(); | 155 certs.clear(); |
| 163 store_ = new SQLiteServerBoundCertStore( | 156 store_ = new SQLiteServerBoundCertStore( |
| 164 temp_dir_.path().Append(chrome::kOBCertFilename), NULL); | 157 temp_dir_.path().Append(chrome::kOBCertFilename), NULL); |
| 165 | 158 |
| 166 // Reload and check if the cert has been removed. | 159 // Reload and check if the cert has been removed. |
| 167 Load(&certs); | 160 Load(&certs); |
| 168 ASSERT_EQ(0U, certs.size()); | 161 ASSERT_EQ(0U, certs.size()); |
| 169 } | 162 } |
| 170 | 163 |
| 171 TEST_F(SQLiteServerBoundCertStoreTest, TestUpgradeV1) { | 164 TEST_F(SQLiteServerBoundCertStoreTest, TestUpgradeV1) { |
| (...skipping 54 matching lines...) Expand 10 before | Expand all | Expand 10 after Loading... |
| 226 ASSERT_EQ(cert_data, certs[0]->cert()); | 219 ASSERT_EQ(cert_data, certs[0]->cert()); |
| 227 | 220 |
| 228 ASSERT_STREQ("foo.com", certs[1]->server_identifier().c_str()); | 221 ASSERT_STREQ("foo.com", certs[1]->server_identifier().c_str()); |
| 229 ASSERT_EQ(net::CLIENT_CERT_RSA_SIGN, certs[1]->type()); | 222 ASSERT_EQ(net::CLIENT_CERT_RSA_SIGN, certs[1]->type()); |
| 230 // Undecodable cert, expiration time will be uninitialized. | 223 // Undecodable cert, expiration time will be uninitialized. |
| 231 ASSERT_EQ(base::Time(), certs[1]->expiration_time()); | 224 ASSERT_EQ(base::Time(), certs[1]->expiration_time()); |
| 232 ASSERT_STREQ("\xaa", certs[1]->private_key().c_str()); | 225 ASSERT_STREQ("\xaa", certs[1]->private_key().c_str()); |
| 233 ASSERT_STREQ("\xbb", certs[1]->cert().c_str()); | 226 ASSERT_STREQ("\xbb", certs[1]->cert().c_str()); |
| 234 | 227 |
| 235 store_ = NULL; | 228 store_ = NULL; |
| 236 // Make sure we wait until the destructor has run. | 229 base::RunLoop().RunUntilIdle(); |
| 237 scoped_refptr<base::ThreadTestHelper> helper( | |
| 238 new base::ThreadTestHelper( | |
| 239 BrowserThread::GetMessageLoopProxyForThread(BrowserThread::DB))); | |
| 240 ASSERT_TRUE(helper->Run()); | |
| 241 | 230 |
| 242 // Verify the database version is updated. | 231 // Verify the database version is updated. |
| 243 { | 232 { |
| 244 sql::Connection db; | 233 sql::Connection db; |
| 245 ASSERT_TRUE(db.Open(v1_db_path)); | 234 ASSERT_TRUE(db.Open(v1_db_path)); |
| 246 sql::Statement smt(db.GetUniqueStatement( | 235 sql::Statement smt(db.GetUniqueStatement( |
| 247 "SELECT value FROM meta WHERE key = \"version\"")); | 236 "SELECT value FROM meta WHERE key = \"version\"")); |
| 248 ASSERT_TRUE(smt.Step()); | 237 ASSERT_TRUE(smt.Step()); |
| 249 EXPECT_EQ(4, smt.ColumnInt(0)); | 238 EXPECT_EQ(4, smt.ColumnInt(0)); |
| 250 EXPECT_FALSE(smt.Step()); | 239 EXPECT_FALSE(smt.Step()); |
| (...skipping 64 matching lines...) Expand 10 before | Expand all | Expand 10 after Loading... |
| 315 | 304 |
| 316 ASSERT_STREQ("foo.com", certs[1]->server_identifier().c_str()); | 305 ASSERT_STREQ("foo.com", certs[1]->server_identifier().c_str()); |
| 317 ASSERT_EQ(net::CLIENT_CERT_ECDSA_SIGN, certs[1]->type()); | 306 ASSERT_EQ(net::CLIENT_CERT_ECDSA_SIGN, certs[1]->type()); |
| 318 // Undecodable cert, expiration time will be uninitialized. | 307 // Undecodable cert, expiration time will be uninitialized. |
| 319 ASSERT_EQ(base::Time(), certs[1]->expiration_time()); | 308 ASSERT_EQ(base::Time(), certs[1]->expiration_time()); |
| 320 ASSERT_STREQ("\xaa", certs[1]->private_key().c_str()); | 309 ASSERT_STREQ("\xaa", certs[1]->private_key().c_str()); |
| 321 ASSERT_STREQ("\xbb", certs[1]->cert().c_str()); | 310 ASSERT_STREQ("\xbb", certs[1]->cert().c_str()); |
| 322 | 311 |
| 323 store_ = NULL; | 312 store_ = NULL; |
| 324 // Make sure we wait until the destructor has run. | 313 // Make sure we wait until the destructor has run. |
| 325 scoped_refptr<base::ThreadTestHelper> helper( | 314 base::RunLoop().RunUntilIdle(); |
| 326 new base::ThreadTestHelper( | |
| 327 BrowserThread::GetMessageLoopProxyForThread(BrowserThread::DB))); | |
| 328 ASSERT_TRUE(helper->Run()); | |
| 329 | 315 |
| 330 // Verify the database version is updated. | 316 // Verify the database version is updated. |
| 331 { | 317 { |
| 332 sql::Connection db; | 318 sql::Connection db; |
| 333 ASSERT_TRUE(db.Open(v2_db_path)); | 319 ASSERT_TRUE(db.Open(v2_db_path)); |
| 334 sql::Statement smt(db.GetUniqueStatement( | 320 sql::Statement smt(db.GetUniqueStatement( |
| 335 "SELECT value FROM meta WHERE key = \"version\"")); | 321 "SELECT value FROM meta WHERE key = \"version\"")); |
| 336 ASSERT_TRUE(smt.Step()); | 322 ASSERT_TRUE(smt.Step()); |
| 337 EXPECT_EQ(4, smt.ColumnInt(0)); | 323 EXPECT_EQ(4, smt.ColumnInt(0)); |
| 338 EXPECT_FALSE(smt.Step()); | 324 EXPECT_FALSE(smt.Step()); |
| (...skipping 68 matching lines...) Expand 10 before | Expand all | Expand 10 after Loading... |
| 407 ASSERT_STREQ("foo.com", certs[1]->server_identifier().c_str()); | 393 ASSERT_STREQ("foo.com", certs[1]->server_identifier().c_str()); |
| 408 ASSERT_EQ(net::CLIENT_CERT_ECDSA_SIGN, certs[1]->type()); | 394 ASSERT_EQ(net::CLIENT_CERT_ECDSA_SIGN, certs[1]->type()); |
| 409 ASSERT_EQ(2000, certs[1]->expiration_time().ToInternalValue()); | 395 ASSERT_EQ(2000, certs[1]->expiration_time().ToInternalValue()); |
| 410 // Undecodable cert, creation time will be uninitialized. | 396 // Undecodable cert, creation time will be uninitialized. |
| 411 ASSERT_EQ(base::Time(), certs[1]->creation_time()); | 397 ASSERT_EQ(base::Time(), certs[1]->creation_time()); |
| 412 ASSERT_STREQ("\xaa", certs[1]->private_key().c_str()); | 398 ASSERT_STREQ("\xaa", certs[1]->private_key().c_str()); |
| 413 ASSERT_STREQ("\xbb", certs[1]->cert().c_str()); | 399 ASSERT_STREQ("\xbb", certs[1]->cert().c_str()); |
| 414 | 400 |
| 415 store_ = NULL; | 401 store_ = NULL; |
| 416 // Make sure we wait until the destructor has run. | 402 // Make sure we wait until the destructor has run. |
| 417 scoped_refptr<base::ThreadTestHelper> helper( | 403 base::RunLoop().RunUntilIdle(); |
| 418 new base::ThreadTestHelper( | |
| 419 BrowserThread::GetMessageLoopProxyForThread(BrowserThread::DB))); | |
| 420 ASSERT_TRUE(helper->Run()); | |
| 421 | 404 |
| 422 // Verify the database version is updated. | 405 // Verify the database version is updated. |
| 423 { | 406 { |
| 424 sql::Connection db; | 407 sql::Connection db; |
| 425 ASSERT_TRUE(db.Open(v3_db_path)); | 408 ASSERT_TRUE(db.Open(v3_db_path)); |
| 426 sql::Statement smt(db.GetUniqueStatement( | 409 sql::Statement smt(db.GetUniqueStatement( |
| 427 "SELECT value FROM meta WHERE key = \"version\"")); | 410 "SELECT value FROM meta WHERE key = \"version\"")); |
| 428 ASSERT_TRUE(smt.Step()); | 411 ASSERT_TRUE(smt.Step()); |
| 429 EXPECT_EQ(4, smt.ColumnInt(0)); | 412 EXPECT_EQ(4, smt.ColumnInt(0)); |
| 430 EXPECT_FALSE(smt.Step()); | 413 EXPECT_FALSE(smt.Step()); |
| 431 } | 414 } |
| 432 } | 415 } |
| 433 } | 416 } |
| OLD | NEW |