Chromium Code Reviews| 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/message_loop.h" | 10 #include "base/message_loop/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 "chrome/browser/net/sqlite_server_bound_cert_store.h" | |
| 14 #include "chrome/common/chrome_constants.h" | |
| 15 #include "content/public/test/mock_special_storage_policy.h" | |
| 16 #include "content/public/test/test_browser_thread_bundle.h" | |
| 17 #include "net/base/test_data_directory.h" | 13 #include "net/base/test_data_directory.h" |
| 14 #include "net/sqlite/sqlite_server_bound_cert_store.h" | |
| 18 #include "net/ssl/ssl_client_cert_type.h" | 15 #include "net/ssl/ssl_client_cert_type.h" |
| 19 #include "net/test/cert_test_util.h" | 16 #include "net/test/cert_test_util.h" |
| 20 #include "sql/statement.h" | 17 #include "sql/statement.h" |
| 21 #include "testing/gtest/include/gtest/gtest.h" | 18 #include "testing/gtest/include/gtest/gtest.h" |
| 22 | 19 |
| 20 namespace net { | |
| 21 | |
| 22 const base::FilePath::CharType kTestOBCertFilename[] = | |
| 23 FILE_PATH_LITERAL("Origin Bound Certs"); | |
|
Ryan Sleevi
2014/07/10 20:20:25
pedantry: Doesn't need this constant, does it? You
mef
2014/07/11 13:29:24
Um, not sure I understand. SQLiteServerBoundCertSt
Ryan Sleevi
2014/07/14 19:18:04
Acknowledged.
| |
| 24 | |
| 23 class SQLiteServerBoundCertStoreTest : public testing::Test { | 25 class SQLiteServerBoundCertStoreTest : public testing::Test { |
| 24 public: | 26 public: |
| 25 void Load( | 27 void Load( |
| 26 ScopedVector<net::DefaultServerBoundCertStore::ServerBoundCert>* certs) { | 28 ScopedVector<net::DefaultServerBoundCertStore::ServerBoundCert>* certs) { |
| 27 base::RunLoop run_loop; | 29 base::RunLoop run_loop; |
| 28 store_->Load(base::Bind(&SQLiteServerBoundCertStoreTest::OnLoaded, | 30 store_->Load(base::Bind(&SQLiteServerBoundCertStoreTest::OnLoaded, |
| 29 base::Unretained(this), | 31 base::Unretained(this), |
| 30 &run_loop)); | 32 &run_loop)); |
| 31 run_loop.Run(); | 33 run_loop.Run(); |
| 32 certs->swap(certs_); | 34 certs->swap(certs_); |
| (...skipping 36 matching lines...) Expand 10 before | Expand all | Expand 10 after Loading... | |
| 69 exploded_time.hour = 2; | 71 exploded_time.hour = 2; |
| 70 exploded_time.minute = 23; | 72 exploded_time.minute = 23; |
| 71 exploded_time.second = 45; | 73 exploded_time.second = 45; |
| 72 exploded_time.millisecond = 0; | 74 exploded_time.millisecond = 0; |
| 73 return base::Time::FromUTCExploded(exploded_time); | 75 return base::Time::FromUTCExploded(exploded_time); |
| 74 } | 76 } |
| 75 | 77 |
| 76 virtual void SetUp() { | 78 virtual void SetUp() { |
| 77 ASSERT_TRUE(temp_dir_.CreateUniqueTempDir()); | 79 ASSERT_TRUE(temp_dir_.CreateUniqueTempDir()); |
| 78 store_ = new SQLiteServerBoundCertStore( | 80 store_ = new SQLiteServerBoundCertStore( |
| 79 temp_dir_.path().Append(chrome::kOBCertFilename), | 81 temp_dir_.path().Append(kTestOBCertFilename), |
| 80 base::MessageLoopProxy::current(), | 82 base::MessageLoopProxy::current(), |
| 81 NULL); | 83 NULL); |
| 82 ScopedVector<net::DefaultServerBoundCertStore::ServerBoundCert> certs; | 84 ScopedVector<net::DefaultServerBoundCertStore::ServerBoundCert> certs; |
| 83 Load(&certs); | 85 Load(&certs); |
| 84 ASSERT_EQ(0u, certs.size()); | 86 ASSERT_EQ(0u, certs.size()); |
| 85 // Make sure the store gets written at least once. | 87 // Make sure the store gets written at least once. |
| 86 store_->AddServerBoundCert( | 88 store_->AddServerBoundCert( |
| 87 net::DefaultServerBoundCertStore::ServerBoundCert( | 89 net::DefaultServerBoundCertStore::ServerBoundCert( |
| 88 "google.com", | 90 "google.com", |
| 89 base::Time::FromInternalValue(1), | 91 base::Time::FromInternalValue(1), |
| 90 base::Time::FromInternalValue(2), | 92 base::Time::FromInternalValue(2), |
| 91 "a", "b")); | 93 "a", "b")); |
| 92 } | 94 } |
| 93 | 95 |
| 94 content::TestBrowserThreadBundle thread_bundle_; | |
| 95 base::ScopedTempDir temp_dir_; | 96 base::ScopedTempDir temp_dir_; |
| 96 scoped_refptr<SQLiteServerBoundCertStore> store_; | 97 scoped_refptr<SQLiteServerBoundCertStore> store_; |
| 97 ScopedVector<net::DefaultServerBoundCertStore::ServerBoundCert> certs_; | 98 ScopedVector<net::DefaultServerBoundCertStore::ServerBoundCert> certs_; |
| 98 }; | 99 }; |
| 99 | 100 |
| 100 // Test if data is stored as expected in the SQLite database. | 101 // Test if data is stored as expected in the SQLite database. |
| 101 TEST_F(SQLiteServerBoundCertStoreTest, TestPersistence) { | 102 TEST_F(SQLiteServerBoundCertStoreTest, TestPersistence) { |
| 102 store_->AddServerBoundCert( | 103 store_->AddServerBoundCert( |
| 103 net::DefaultServerBoundCertStore::ServerBoundCert( | 104 net::DefaultServerBoundCertStore::ServerBoundCert( |
| 104 "foo.com", | 105 "foo.com", |
| 105 base::Time::FromInternalValue(3), | 106 base::Time::FromInternalValue(3), |
| 106 base::Time::FromInternalValue(4), | 107 base::Time::FromInternalValue(4), |
| 107 "c", "d")); | 108 "c", "d")); |
| 108 | 109 |
| 109 ScopedVector<net::DefaultServerBoundCertStore::ServerBoundCert> certs; | 110 ScopedVector<net::DefaultServerBoundCertStore::ServerBoundCert> certs; |
| 110 // Replace the store effectively destroying the current one and forcing it | 111 // Replace the store effectively destroying the current one and forcing it |
| 111 // to write its data to disk. Then we can see if after loading it again it | 112 // to write its data to disk. Then we can see if after loading it again it |
| 112 // is still there. | 113 // is still there. |
| 113 store_ = NULL; | 114 store_ = NULL; |
| 114 // Make sure we wait until the destructor has run. | 115 // Make sure we wait until the destructor has run. |
| 115 base::RunLoop().RunUntilIdle(); | 116 base::RunLoop().RunUntilIdle(); |
| 116 store_ = new SQLiteServerBoundCertStore( | 117 store_ = new SQLiteServerBoundCertStore( |
| 117 temp_dir_.path().Append(chrome::kOBCertFilename), | 118 temp_dir_.path().Append(kTestOBCertFilename), |
| 118 base::MessageLoopProxy::current(), | 119 base::MessageLoopProxy::current(), |
| 119 NULL); | 120 NULL); |
| 120 | 121 |
| 121 // Reload and test for persistence | 122 // Reload and test for persistence |
| 122 Load(&certs); | 123 Load(&certs); |
| 123 ASSERT_EQ(2U, certs.size()); | 124 ASSERT_EQ(2U, certs.size()); |
| 124 net::DefaultServerBoundCertStore::ServerBoundCert* goog_cert; | 125 net::DefaultServerBoundCertStore::ServerBoundCert* goog_cert; |
| 125 net::DefaultServerBoundCertStore::ServerBoundCert* foo_cert; | 126 net::DefaultServerBoundCertStore::ServerBoundCert* foo_cert; |
| 126 if (certs[0]->server_identifier() == "google.com") { | 127 if (certs[0]->server_identifier() == "google.com") { |
| 127 goog_cert = certs[0]; | 128 goog_cert = certs[0]; |
| (...skipping 14 matching lines...) Expand all Loading... | |
| 142 ASSERT_EQ(4, foo_cert->expiration_time().ToInternalValue()); | 143 ASSERT_EQ(4, foo_cert->expiration_time().ToInternalValue()); |
| 143 | 144 |
| 144 // Now delete the cert and check persistence again. | 145 // Now delete the cert and check persistence again. |
| 145 store_->DeleteServerBoundCert(*certs[0]); | 146 store_->DeleteServerBoundCert(*certs[0]); |
| 146 store_->DeleteServerBoundCert(*certs[1]); | 147 store_->DeleteServerBoundCert(*certs[1]); |
| 147 store_ = NULL; | 148 store_ = NULL; |
| 148 // Make sure we wait until the destructor has run. | 149 // Make sure we wait until the destructor has run. |
| 149 base::RunLoop().RunUntilIdle(); | 150 base::RunLoop().RunUntilIdle(); |
| 150 certs.clear(); | 151 certs.clear(); |
| 151 store_ = new SQLiteServerBoundCertStore( | 152 store_ = new SQLiteServerBoundCertStore( |
| 152 temp_dir_.path().Append(chrome::kOBCertFilename), | 153 temp_dir_.path().Append(kTestOBCertFilename), |
| 153 base::MessageLoopProxy::current(), | 154 base::MessageLoopProxy::current(), |
| 154 NULL); | 155 NULL); |
| 155 | 156 |
| 156 // Reload and check if the cert has been removed. | 157 // Reload and check if the cert has been removed. |
| 157 Load(&certs); | 158 Load(&certs); |
| 158 ASSERT_EQ(0U, certs.size()); | 159 ASSERT_EQ(0U, certs.size()); |
| 159 } | 160 } |
| 160 | 161 |
| 161 TEST_F(SQLiteServerBoundCertStoreTest, TestUpgradeV1) { | 162 TEST_F(SQLiteServerBoundCertStoreTest, TestUpgradeV1) { |
| 162 // Reset the store. We'll be using a different database for this test. | 163 // Reset the store. We'll be using a different database for this test. |
| (...skipping 301 matching lines...) Expand 10 before | Expand all | Expand 10 after Loading... | |
| 464 ASSERT_EQ("google.com", certs[0]->server_identifier()); | 465 ASSERT_EQ("google.com", certs[0]->server_identifier()); |
| 465 ASSERT_EQ(GetTestCertExpirationTime(), | 466 ASSERT_EQ(GetTestCertExpirationTime(), |
| 466 certs[0]->expiration_time()); | 467 certs[0]->expiration_time()); |
| 467 ASSERT_EQ(key_data, certs[0]->private_key()); | 468 ASSERT_EQ(key_data, certs[0]->private_key()); |
| 468 ASSERT_EQ(cert_data, certs[0]->cert()); | 469 ASSERT_EQ(cert_data, certs[0]->cert()); |
| 469 | 470 |
| 470 store_ = NULL; | 471 store_ = NULL; |
| 471 // Make sure we wait until the destructor has run. | 472 // Make sure we wait until the destructor has run. |
| 472 base::RunLoop().RunUntilIdle(); | 473 base::RunLoop().RunUntilIdle(); |
| 473 } | 474 } |
| 475 | |
| 476 } // namespace net | |
| OLD | NEW |