OLD | NEW |
1 // Copyright 2014 The Chromium Authors. All rights reserved. | 1 // Copyright 2014 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/extras/sqlite/sqlite_channel_id_store.h" | 5 #include "net/extras/sqlite/sqlite_channel_id_store.h" |
6 | 6 |
7 #include <memory> | 7 #include <memory> |
8 #include <vector> | 8 #include <vector> |
9 | 9 |
10 #include "base/bind.h" | 10 #include "base/bind.h" |
11 #include "base/files/file_util.h" | 11 #include "base/files/file_util.h" |
12 #include "base/files/scoped_temp_dir.h" | 12 #include "base/files/scoped_temp_dir.h" |
13 #include "base/memory/ptr_util.h" | |
14 #include "base/memory/ref_counted.h" | 13 #include "base/memory/ref_counted.h" |
15 #include "base/run_loop.h" | 14 #include "base/run_loop.h" |
16 #include "base/threading/thread_task_runner_handle.h" | 15 #include "base/threading/thread_task_runner_handle.h" |
17 #include "crypto/ec_private_key.h" | 16 #include "crypto/ec_private_key.h" |
18 #include "net/cert/asn1_util.h" | 17 #include "net/cert/asn1_util.h" |
19 #include "net/ssl/channel_id_service.h" | 18 #include "net/ssl/channel_id_service.h" |
20 #include "net/ssl/ssl_client_cert_type.h" | 19 #include "net/ssl/ssl_client_cert_type.h" |
21 #include "net/test/cert_test_util.h" | 20 #include "net/test/cert_test_util.h" |
22 #include "net/test/channel_id_test_util.h" | 21 #include "net/test/channel_id_test_util.h" |
23 #include "net/test/test_data_directory.h" | 22 #include "net/test/test_data_directory.h" |
(...skipping 35 matching lines...) Expand 10 before | Expand all | Expand 10 after Loading... |
59 base::FilePath cert_path = | 58 base::FilePath cert_path = |
60 GetTestCertsDirectory().AppendASCII("unittest.originbound.der"); | 59 GetTestCertsDirectory().AppendASCII("unittest.originbound.der"); |
61 ASSERT_TRUE(base::ReadFileToString(key_path, key_data)); | 60 ASSERT_TRUE(base::ReadFileToString(key_path, key_data)); |
62 ASSERT_TRUE(base::ReadFileToString(cert_path, cert_data)); | 61 ASSERT_TRUE(base::ReadFileToString(cert_path, cert_data)); |
63 std::vector<uint8_t> private_key(key_data->size()); | 62 std::vector<uint8_t> private_key(key_data->size()); |
64 memcpy(private_key.data(), key_data->data(), key_data->size()); | 63 memcpy(private_key.data(), key_data->data(), key_data->size()); |
65 base::StringPiece spki; | 64 base::StringPiece spki; |
66 ASSERT_TRUE(asn1::ExtractSPKIFromDERCert(*cert_data, &spki)); | 65 ASSERT_TRUE(asn1::ExtractSPKIFromDERCert(*cert_data, &spki)); |
67 std::vector<uint8_t> public_key(spki.size()); | 66 std::vector<uint8_t> public_key(spki.size()); |
68 memcpy(public_key.data(), spki.data(), spki.size()); | 67 memcpy(public_key.data(), spki.data(), spki.size()); |
69 key->reset(crypto::ECPrivateKey::CreateFromEncryptedPrivateKeyInfo( | 68 *key = crypto::ECPrivateKey::CreateFromEncryptedPrivateKeyInfo( |
70 ChannelIDService::kEPKIPassword, private_key, public_key)); | 69 ChannelIDService::kEPKIPassword, private_key, public_key); |
71 } | 70 } |
72 | 71 |
73 static base::Time GetTestCertExpirationTime() { | 72 static base::Time GetTestCertExpirationTime() { |
74 // Cert expiration time from 'openssl asn1parse -inform der -in | 73 // Cert expiration time from 'openssl asn1parse -inform der -in |
75 // unittest.originbound.der': | 74 // unittest.originbound.der': |
76 // UTCTIME :160507022239Z | 75 // UTCTIME :160507022239Z |
77 // base::Time::FromUTCExploded can't generate values past 2038 on 32-bit | 76 // base::Time::FromUTCExploded can't generate values past 2038 on 32-bit |
78 // linux, so we use the raw value here. | 77 // linux, so we use the raw value here. |
79 base::Time::Exploded exploded_time; | 78 base::Time::Exploded exploded_time; |
80 exploded_time.year = 2016; | 79 exploded_time.year = 2016; |
(...skipping 23 matching lines...) Expand all Loading... |
104 | 103 |
105 void SetUp() override { | 104 void SetUp() override { |
106 ASSERT_TRUE(temp_dir_.CreateUniqueTempDir()); | 105 ASSERT_TRUE(temp_dir_.CreateUniqueTempDir()); |
107 store_ = new SQLiteChannelIDStore( | 106 store_ = new SQLiteChannelIDStore( |
108 temp_dir_.path().Append(kTestChannelIDFilename), | 107 temp_dir_.path().Append(kTestChannelIDFilename), |
109 base::ThreadTaskRunnerHandle::Get()); | 108 base::ThreadTaskRunnerHandle::Get()); |
110 std::vector<std::unique_ptr<DefaultChannelIDStore::ChannelID>> channel_ids; | 109 std::vector<std::unique_ptr<DefaultChannelIDStore::ChannelID>> channel_ids; |
111 Load(&channel_ids); | 110 Load(&channel_ids); |
112 ASSERT_EQ(0u, channel_ids.size()); | 111 ASSERT_EQ(0u, channel_ids.size()); |
113 // Make sure the store gets written at least once. | 112 // Make sure the store gets written at least once. |
114 google_key_.reset(crypto::ECPrivateKey::Create()); | 113 google_key_ = crypto::ECPrivateKey::Create(); |
115 store_->AddChannelID(DefaultChannelIDStore::ChannelID( | 114 store_->AddChannelID(DefaultChannelIDStore::ChannelID( |
116 "google.com", base::Time::FromInternalValue(1), | 115 "google.com", base::Time::FromInternalValue(1), google_key_->Copy())); |
117 base::WrapUnique(google_key_->Copy()))); | |
118 } | 116 } |
119 | 117 |
120 base::ScopedTempDir temp_dir_; | 118 base::ScopedTempDir temp_dir_; |
121 scoped_refptr<SQLiteChannelIDStore> store_; | 119 scoped_refptr<SQLiteChannelIDStore> store_; |
122 std::vector<std::unique_ptr<DefaultChannelIDStore::ChannelID>> channel_ids_; | 120 std::vector<std::unique_ptr<DefaultChannelIDStore::ChannelID>> channel_ids_; |
123 std::unique_ptr<crypto::ECPrivateKey> google_key_; | 121 std::unique_ptr<crypto::ECPrivateKey> google_key_; |
124 }; | 122 }; |
125 | 123 |
126 // Test if data is stored as expected in the SQLite database. | 124 // Test if data is stored as expected in the SQLite database. |
127 TEST_F(SQLiteChannelIDStoreTest, TestPersistence) { | 125 TEST_F(SQLiteChannelIDStoreTest, TestPersistence) { |
128 std::unique_ptr<crypto::ECPrivateKey> foo_key(crypto::ECPrivateKey::Create()); | 126 std::unique_ptr<crypto::ECPrivateKey> foo_key(crypto::ECPrivateKey::Create()); |
129 store_->AddChannelID(DefaultChannelIDStore::ChannelID( | 127 store_->AddChannelID(DefaultChannelIDStore::ChannelID( |
130 "foo.com", base::Time::FromInternalValue(3), | 128 "foo.com", base::Time::FromInternalValue(3), foo_key->Copy())); |
131 base::WrapUnique(foo_key->Copy()))); | |
132 | 129 |
133 std::vector<std::unique_ptr<DefaultChannelIDStore::ChannelID>> channel_ids; | 130 std::vector<std::unique_ptr<DefaultChannelIDStore::ChannelID>> channel_ids; |
134 // Replace the store effectively destroying the current one and forcing it | 131 // Replace the store effectively destroying the current one and forcing it |
135 // to write its data to disk. Then we can see if after loading it again it | 132 // to write its data to disk. Then we can see if after loading it again it |
136 // is still there. | 133 // is still there. |
137 store_ = NULL; | 134 store_ = NULL; |
138 // Make sure we wait until the destructor has run. | 135 // Make sure we wait until the destructor has run. |
139 base::RunLoop().RunUntilIdle(); | 136 base::RunLoop().RunUntilIdle(); |
140 store_ = | 137 store_ = |
141 new SQLiteChannelIDStore(temp_dir_.path().Append(kTestChannelIDFilename), | 138 new SQLiteChannelIDStore(temp_dir_.path().Append(kTestChannelIDFilename), |
(...skipping 35 matching lines...) Expand 10 before | Expand all | Expand 10 after Loading... |
177 // Close the store. | 174 // Close the store. |
178 store_ = NULL; | 175 store_ = NULL; |
179 // Make sure we wait until the destructor has run. | 176 // Make sure we wait until the destructor has run. |
180 base::RunLoop().RunUntilIdle(); | 177 base::RunLoop().RunUntilIdle(); |
181 } | 178 } |
182 | 179 |
183 // Test if data is stored as expected in the SQLite database. | 180 // Test if data is stored as expected in the SQLite database. |
184 TEST_F(SQLiteChannelIDStoreTest, TestDeleteAll) { | 181 TEST_F(SQLiteChannelIDStoreTest, TestDeleteAll) { |
185 store_->AddChannelID(DefaultChannelIDStore::ChannelID( | 182 store_->AddChannelID(DefaultChannelIDStore::ChannelID( |
186 "foo.com", base::Time::FromInternalValue(3), | 183 "foo.com", base::Time::FromInternalValue(3), |
187 base::WrapUnique(crypto::ECPrivateKey::Create()))); | 184 crypto::ECPrivateKey::Create())); |
188 | 185 |
189 std::vector<std::unique_ptr<DefaultChannelIDStore::ChannelID>> channel_ids; | 186 std::vector<std::unique_ptr<DefaultChannelIDStore::ChannelID>> channel_ids; |
190 // Replace the store effectively destroying the current one and forcing it | 187 // Replace the store effectively destroying the current one and forcing it |
191 // to write its data to disk. Then we can see if after loading it again it | 188 // to write its data to disk. Then we can see if after loading it again it |
192 // is still there. | 189 // is still there. |
193 store_ = NULL; | 190 store_ = NULL; |
194 // Make sure we wait until the destructor has run. | 191 // Make sure we wait until the destructor has run. |
195 base::RunLoop().RunUntilIdle(); | 192 base::RunLoop().RunUntilIdle(); |
196 store_ = | 193 store_ = |
197 new SQLiteChannelIDStore(temp_dir_.path().Append(kTestChannelIDFilename), | 194 new SQLiteChannelIDStore(temp_dir_.path().Append(kTestChannelIDFilename), |
(...skipping 337 matching lines...) Expand 10 before | Expand all | Expand 10 after Loading... |
535 sql::Statement smt(db.GetUniqueStatement( | 532 sql::Statement smt(db.GetUniqueStatement( |
536 "SELECT value FROM meta WHERE key = \"version\"")); | 533 "SELECT value FROM meta WHERE key = \"version\"")); |
537 ASSERT_TRUE(smt.Step()); | 534 ASSERT_TRUE(smt.Step()); |
538 EXPECT_EQ(5, smt.ColumnInt(0)); | 535 EXPECT_EQ(5, smt.ColumnInt(0)); |
539 EXPECT_FALSE(smt.Step()); | 536 EXPECT_FALSE(smt.Step()); |
540 } | 537 } |
541 } | 538 } |
542 } | 539 } |
543 | 540 |
544 } // namespace net | 541 } // namespace net |
OLD | NEW |